xref: /spdk/lib/nvme/nvme_rdma.c (revision d58eef2a29f5d65b15a72162d9d79db68f27aa81)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2a6dbe372Spaul luse  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
347afb928SAlexey Marchuk  *   Copyright (c) 2019-2021 Mellanox Technologies LTD. All rights reserved.
40568555aSEvgeniy Kochetov  *   Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5246c39a7SZiye Yang  */
6246c39a7SZiye Yang 
7246c39a7SZiye Yang /*
8246c39a7SZiye Yang  * NVMe over RDMA transport
9246c39a7SZiye Yang  */
10246c39a7SZiye Yang 
11b961d9ccSBen Walker #include "spdk/stdinc.h"
12b961d9ccSBen Walker 
13246c39a7SZiye Yang #include "spdk/assert.h"
14d06b6097SAlexey Marchuk #include "spdk/dma.h"
15246c39a7SZiye Yang #include "spdk/log.h"
16246c39a7SZiye Yang #include "spdk/trace.h"
17246c39a7SZiye Yang #include "spdk/queue.h"
18246c39a7SZiye Yang #include "spdk/nvme.h"
19246c39a7SZiye Yang #include "spdk/nvmf_spec.h"
209a40113aSDaniel Verkamp #include "spdk/string.h"
21ad20a6ddSDaniel Verkamp #include "spdk/endian.h"
226b504fdaSDaniel Verkamp #include "spdk/likely.h"
2394966468SAlexey Marchuk #include "spdk/config.h"
24246c39a7SZiye Yang 
25246c39a7SZiye Yang #include "nvme_internal.h"
26cf151d60SAlexey Marchuk #include "spdk_internal/rdma_provider.h"
278a01b4d6SAlexey Marchuk #include "spdk_internal/rdma_utils.h"
28246c39a7SZiye Yang 
29246c39a7SZiye Yang #define NVME_RDMA_TIME_OUT_IN_MS 2000
30246c39a7SZiye Yang #define NVME_RDMA_RW_BUFFER_SIZE 131072
31246c39a7SZiye Yang 
32246c39a7SZiye Yang /*
33935cdbe4SSeth Howell  * NVME RDMA qpair Resource Defaults
34246c39a7SZiye Yang  */
35246c39a7SZiye Yang #define NVME_RDMA_DEFAULT_TX_SGE		2
36246c39a7SZiye Yang #define NVME_RDMA_DEFAULT_RX_SGE		1
37246c39a7SZiye Yang 
38935cdbe4SSeth Howell /* Max number of NVMe-oF SGL descriptors supported by the host */
39935cdbe4SSeth Howell #define NVME_RDMA_MAX_SGL_DESCRIPTORS		16
405ac814e3SSeth Howell 
415ac814e3SSeth Howell /* number of STAILQ entries for holding pending RDMA CM events. */
425ac814e3SSeth Howell #define NVME_RDMA_NUM_CM_EVENTS			256
435ac814e3SSeth Howell 
448bef6f0bSSeth Howell /* The default size for a shared rdma completion queue. */
458bef6f0bSSeth Howell #define DEFAULT_NVME_RDMA_CQ_SIZE		4096
468bef6f0bSSeth Howell 
47e9e3f615SSeth Howell /*
4820cf9080SShuhei Matsumoto  * In the special case of a stale connection we don't expose a mechanism
4920cf9080SShuhei Matsumoto  * for the user to retry the connection so we need to handle it internally.
5020cf9080SShuhei Matsumoto  */
5120cf9080SShuhei Matsumoto #define NVME_RDMA_STALE_CONN_RETRY_MAX		5
5220cf9080SShuhei Matsumoto #define NVME_RDMA_STALE_CONN_RETRY_DELAY_US	10000
5320cf9080SShuhei Matsumoto 
5420cf9080SShuhei Matsumoto /*
55f1539c28SAlexey Marchuk  * Maximum value of transport_retry_count used by RDMA controller
56f1539c28SAlexey Marchuk  */
57f1539c28SAlexey Marchuk #define NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT	7
58f1539c28SAlexey Marchuk 
5994966468SAlexey Marchuk /*
6094966468SAlexey Marchuk  * Maximum value of transport_ack_timeout used by RDMA controller
6194966468SAlexey Marchuk  */
6294966468SAlexey Marchuk #define NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT	31
6394966468SAlexey Marchuk 
648bef6f0bSSeth Howell /*
654b732235SShuhei Matsumoto  * Number of microseconds to wait until the lingering qpair becomes quiet.
668bef6f0bSSeth Howell  */
674b732235SShuhei Matsumoto #define NVME_RDMA_DISCONNECTED_QPAIR_TIMEOUT_US	1000000ull
688bef6f0bSSeth Howell 
69e7625088SAlexey Marchuk /*
70e7625088SAlexey Marchuk  * The max length of keyed SGL data block (3 bytes)
71e7625088SAlexey Marchuk  */
72e7625088SAlexey Marchuk #define NVME_RDMA_MAX_KEYED_SGL_LENGTH ((1u << 24u) - 1)
73e7625088SAlexey Marchuk 
7410392543SSeth Howell #define WC_PER_QPAIR(queue_depth)	(queue_depth * 2)
7510392543SSeth Howell 
7600277bc5SAlexey Marchuk #define NVME_RDMA_POLL_GROUP_CHECK_QPN(_rqpair, qpn)				\
7700277bc5SAlexey Marchuk 	((_rqpair)->rdma_qp && (_rqpair)->rdma_qp->qp->qp_num == (qpn))	\
7800277bc5SAlexey Marchuk 
79fadfef63SSeth Howell enum nvme_rdma_wr_type {
80fadfef63SSeth Howell 	RDMA_WR_TYPE_RECV,
81fadfef63SSeth Howell 	RDMA_WR_TYPE_SEND,
82fadfef63SSeth Howell };
83fadfef63SSeth Howell 
84fadfef63SSeth Howell struct nvme_rdma_wr {
85fadfef63SSeth Howell 	/* Using this instead of the enum allows this struct to only occupy one byte. */
86fadfef63SSeth Howell 	uint8_t	type;
87fadfef63SSeth Howell };
88fadfef63SSeth Howell 
89935cdbe4SSeth Howell struct spdk_nvmf_cmd {
90935cdbe4SSeth Howell 	struct spdk_nvme_cmd cmd;
91935cdbe4SSeth Howell 	struct spdk_nvme_sgl_descriptor sgl[NVME_RDMA_MAX_SGL_DESCRIPTORS];
92935cdbe4SSeth Howell };
93935cdbe4SSeth Howell 
949fb69476Szkhatami88 struct spdk_nvme_rdma_hooks g_nvme_hooks = {};
959fb69476Szkhatami88 
965ac814e3SSeth Howell /* STAILQ wrapper for cm events. */
975ac814e3SSeth Howell struct nvme_rdma_cm_event_entry {
985ac814e3SSeth Howell 	struct rdma_cm_event			*evt;
995ac814e3SSeth Howell 	STAILQ_ENTRY(nvme_rdma_cm_event_entry)	link;
1005ac814e3SSeth Howell };
1015ac814e3SSeth Howell 
102246c39a7SZiye Yang /* NVMe RDMA transport extensions for spdk_nvme_ctrlr */
103246c39a7SZiye Yang struct nvme_rdma_ctrlr {
104246c39a7SZiye Yang 	struct spdk_nvme_ctrlr			ctrlr;
1059fb69476Szkhatami88 
1068b539eb5SShuhei Matsumoto 	uint16_t				max_sge;
1075ac814e3SSeth Howell 
1085ac814e3SSeth Howell 	struct rdma_event_channel		*cm_channel;
1095ac814e3SSeth Howell 
1105ac814e3SSeth Howell 	STAILQ_HEAD(, nvme_rdma_cm_event_entry)	pending_cm_events;
1115ac814e3SSeth Howell 
1125ac814e3SSeth Howell 	STAILQ_HEAD(, nvme_rdma_cm_event_entry)	free_cm_events;
1135ac814e3SSeth Howell 
1145ac814e3SSeth Howell 	struct nvme_rdma_cm_event_entry		*cm_events;
115246c39a7SZiye Yang };
116246c39a7SZiye Yang 
117527f406bSAlexey Marchuk struct nvme_rdma_poller_stats {
11850569293SAlexey Marchuk 	uint64_t polls;
11950569293SAlexey Marchuk 	uint64_t idle_polls;
12050569293SAlexey Marchuk 	uint64_t queued_requests;
12150569293SAlexey Marchuk 	uint64_t completions;
122cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_qp_stats rdma_stats;
123527f406bSAlexey Marchuk };
124527f406bSAlexey Marchuk 
1251439f9c7SShuhei Matsumoto struct nvme_rdma_poll_group;
126bcd987eaSShuhei Matsumoto struct nvme_rdma_rsps;
1271439f9c7SShuhei Matsumoto 
1288bef6f0bSSeth Howell struct nvme_rdma_poller {
1298bef6f0bSSeth Howell 	struct ibv_context		*device;
1308bef6f0bSSeth Howell 	struct ibv_cq			*cq;
131cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_srq	*srq;
132bcd987eaSShuhei Matsumoto 	struct nvme_rdma_rsps		*rsps;
133bcd987eaSShuhei Matsumoto 	struct ibv_pd			*pd;
1348a01b4d6SAlexey Marchuk 	struct spdk_rdma_utils_mem_map	*mr_map;
1353b26e2c5SEvgeniy Kochetov 	uint32_t			refcnt;
13610392543SSeth Howell 	int				required_num_wc;
13710392543SSeth Howell 	int				current_num_wc;
138527f406bSAlexey Marchuk 	struct nvme_rdma_poller_stats	stats;
1391439f9c7SShuhei Matsumoto 	struct nvme_rdma_poll_group	*group;
1408bef6f0bSSeth Howell 	STAILQ_ENTRY(nvme_rdma_poller)	link;
1418bef6f0bSSeth Howell };
1428bef6f0bSSeth Howell 
143f8fc1e11SShuhei Matsumoto struct nvme_rdma_qpair;
144f8fc1e11SShuhei Matsumoto 
1451b818a28SSeth Howell struct nvme_rdma_poll_group {
1461b818a28SSeth Howell 	struct spdk_nvme_transport_poll_group		group;
1478bef6f0bSSeth Howell 	STAILQ_HEAD(, nvme_rdma_poller)			pollers;
1483fcda8e7SAlexey Marchuk 	uint32_t					num_pollers;
149f8fc1e11SShuhei Matsumoto 	TAILQ_HEAD(, nvme_rdma_qpair)			connecting_qpairs;
150fa457d63SShuhei Matsumoto 	TAILQ_HEAD(, nvme_rdma_qpair)			active_qpairs;
1511b818a28SSeth Howell };
1521b818a28SSeth Howell 
15329974dc8SShuhei Matsumoto enum nvme_rdma_qpair_state {
15429974dc8SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_INVALID = 0,
15520cf9080SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_STALE_CONN,
1569717b0c3SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_INITIALIZING,
15729974dc8SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_SEND,
15829974dc8SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_POLL,
159ffd098cfSKonrad Sztyber 	NVME_RDMA_QPAIR_STATE_AUTHENTICATING,
16029974dc8SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_RUNNING,
1619717b0c3SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_EXITING,
1624b732235SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_LINGERING,
1639717b0c3SShuhei Matsumoto 	NVME_RDMA_QPAIR_STATE_EXITED,
16429974dc8SShuhei Matsumoto };
16529974dc8SShuhei Matsumoto 
166cf7f2533SShuhei Matsumoto typedef int (*nvme_rdma_cm_event_cb)(struct nvme_rdma_qpair *rqpair, int ret);
167cf7f2533SShuhei Matsumoto 
1684999a985SShuhei Matsumoto struct nvme_rdma_rsp_opts {
1694999a985SShuhei Matsumoto 	uint16_t				num_entries;
1704999a985SShuhei Matsumoto 	struct nvme_rdma_qpair			*rqpair;
171cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_srq		*srq;
1728a01b4d6SAlexey Marchuk 	struct spdk_rdma_utils_mem_map		*mr_map;
1734999a985SShuhei Matsumoto };
1744999a985SShuhei Matsumoto 
1754999a985SShuhei Matsumoto struct nvme_rdma_rsps {
1764999a985SShuhei Matsumoto 	/* Parallel arrays of response buffers + response SGLs of size num_entries */
1774999a985SShuhei Matsumoto 	struct ibv_sge				*rsp_sgls;
1784999a985SShuhei Matsumoto 	struct spdk_nvme_rdma_rsp		*rsps;
1794999a985SShuhei Matsumoto 
1804999a985SShuhei Matsumoto 	struct ibv_recv_wr			*rsp_recv_wrs;
1814999a985SShuhei Matsumoto 
1824999a985SShuhei Matsumoto 	/* Count of outstanding recv objects */
1834999a985SShuhei Matsumoto 	uint16_t				current_num_recvs;
1844999a985SShuhei Matsumoto 
1854999a985SShuhei Matsumoto 	uint16_t				num_entries;
1864999a985SShuhei Matsumoto };
1874999a985SShuhei Matsumoto 
188246c39a7SZiye Yang /* NVMe RDMA qpair extensions for spdk_nvme_qpair */
189246c39a7SZiye Yang struct nvme_rdma_qpair {
190246c39a7SZiye Yang 	struct spdk_nvme_qpair			qpair;
191246c39a7SZiye Yang 
192cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_qp		*rdma_qp;
193246c39a7SZiye Yang 	struct rdma_cm_id			*cm_id;
194ac9b92c8SBen Walker 	struct ibv_cq				*cq;
195cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_srq		*srq;
196ac9b92c8SBen Walker 
197246c39a7SZiye Yang 	struct	spdk_nvme_rdma_req		*rdma_reqs;
198246c39a7SZiye Yang 
199e688d1ccSSeth Howell 	uint32_t				max_send_sge;
200e688d1ccSSeth Howell 
201df8129fbSDaniel Verkamp 	uint16_t				num_entries;
202df8129fbSDaniel Verkamp 
203581e1bb5SAlexey Marchuk 	bool					delay_cmd_submit;
204cec5ba28SAlexey Marchuk 	/* Append copy task even if no accel sequence is attached to IO.
205cec5ba28SAlexey Marchuk 	 * Result is UMR configured per IO data buffer */
206cec5ba28SAlexey Marchuk 	bool					append_copy;
207581e1bb5SAlexey Marchuk 
20863732d88SSeth Howell 	uint32_t				num_completions;
209fa457d63SShuhei Matsumoto 	uint32_t				num_outstanding_reqs;
21063732d88SSeth Howell 
2114999a985SShuhei Matsumoto 	struct nvme_rdma_rsps			*rsps;
2126ab28a20SDaniel Verkamp 
213fb31963cSDaniel Verkamp 	/*
214df8129fbSDaniel Verkamp 	 * Array of num_entries NVMe commands registered as RDMA message buffers.
215fb31963cSDaniel Verkamp 	 * Indexed by rdma_req->id.
216fb31963cSDaniel Verkamp 	 */
217935cdbe4SSeth Howell 	struct spdk_nvmf_cmd			*cmds;
218fb31963cSDaniel Verkamp 
2198a01b4d6SAlexey Marchuk 	struct spdk_rdma_utils_mem_map		*mr_map;
22083e55653SDaniel Verkamp 
221bc165a26SDaniel Verkamp 	TAILQ_HEAD(, spdk_nvme_rdma_req)	free_reqs;
222d65c23b9SDaniel Verkamp 	TAILQ_HEAD(, spdk_nvme_rdma_req)	outstanding_reqs;
223e688d1ccSSeth Howell 
2244999a985SShuhei Matsumoto 	/* Count of outstanding send objects */
22567b0dcfeSSeth Howell 	uint16_t				current_num_sends;
22667b0dcfeSSeth Howell 
227fa457d63SShuhei Matsumoto 	TAILQ_ENTRY(nvme_rdma_qpair)		link_active;
228fa457d63SShuhei Matsumoto 
229e688d1ccSSeth Howell 	/* Placed at the end of the struct since it is not used frequently */
230579d44b0SSeth Howell 	struct rdma_cm_event			*evt;
231527f406bSAlexey Marchuk 	struct nvme_rdma_poller			*poller;
2328bef6f0bSSeth Howell 
233531c1b0fSShuhei Matsumoto 	uint64_t				evt_timeout_ticks;
234531c1b0fSShuhei Matsumoto 	nvme_rdma_cm_event_cb			evt_cb;
235531c1b0fSShuhei Matsumoto 	enum rdma_cm_event_type			expected_evt_type;
236531c1b0fSShuhei Matsumoto 
23729974dc8SShuhei Matsumoto 	enum nvme_rdma_qpair_state		state;
23829974dc8SShuhei Matsumoto 
23929974dc8SShuhei Matsumoto 	bool					in_connect_poll;
24029974dc8SShuhei Matsumoto 
24120cf9080SShuhei Matsumoto 	uint8_t					stale_conn_retry_count;
242e44d6317Ssijie.sun 	bool					need_destroy;
243f8fc1e11SShuhei Matsumoto 	TAILQ_ENTRY(nvme_rdma_qpair)		link_connecting;
244246c39a7SZiye Yang };
245246c39a7SZiye Yang 
246581e1bb5SAlexey Marchuk enum NVME_RDMA_COMPLETION_FLAGS {
247581e1bb5SAlexey Marchuk 	NVME_RDMA_SEND_COMPLETED = 1u << 0,
248581e1bb5SAlexey Marchuk 	NVME_RDMA_RECV_COMPLETED = 1u << 1,
249581e1bb5SAlexey Marchuk };
250246c39a7SZiye Yang 
251581e1bb5SAlexey Marchuk struct spdk_nvme_rdma_req {
252581e1bb5SAlexey Marchuk 	uint16_t				id;
253581e1bb5SAlexey Marchuk 	uint16_t				completion_flags: 2;
2542c140f58SAlexey Marchuk 	uint16_t				in_progress_accel: 1;
2552c140f58SAlexey Marchuk 	uint16_t				reserved: 13;
256581e1bb5SAlexey Marchuk 	/* if completion of RDMA_RECV received before RDMA_SEND, we will complete nvme request
257851a8dfeSShuhei Matsumoto 	 * during processing of RDMA_SEND. To complete the request we must know the response
258851a8dfeSShuhei Matsumoto 	 * received in RDMA_RECV, so store it in this field */
259851a8dfeSShuhei Matsumoto 	struct spdk_nvme_rdma_rsp		*rdma_rsp;
26018508424SSeth Howell 
261fadfef63SSeth Howell 	struct nvme_rdma_wr			rdma_wr;
262fadfef63SSeth Howell 
263c21e9fa5SZiye Yang 	struct ibv_send_wr			send_wr;
264c21e9fa5SZiye Yang 
265246c39a7SZiye Yang 	struct nvme_request			*req;
266246c39a7SZiye Yang 
26794f87a2dSPotnuri Bharat Teja 	struct ibv_sge				send_sgl[NVME_RDMA_DEFAULT_TX_SGE];
268246c39a7SZiye Yang 
269bc165a26SDaniel Verkamp 	TAILQ_ENTRY(spdk_nvme_rdma_req)		link;
2702c140f58SAlexey Marchuk 
2712c140f58SAlexey Marchuk 	/* Fields below are not used in regular IO path, keep them last */
2722c140f58SAlexey Marchuk 	spdk_memory_domain_data_cpl_cb		transfer_cpl_cb;
2732c140f58SAlexey Marchuk 	void					*transfer_cpl_cb_arg;
2742c140f58SAlexey Marchuk 	/* Accel sequence API works with iovec pointer, we need to store result of next_sge callback */
2752c140f58SAlexey Marchuk 	struct iovec				iovs[NVME_RDMA_MAX_SGL_DESCRIPTORS];
276246c39a7SZiye Yang };
277246c39a7SZiye Yang 
278fadfef63SSeth Howell struct spdk_nvme_rdma_rsp {
279fadfef63SSeth Howell 	struct spdk_nvme_cpl	cpl;
280fadfef63SSeth Howell 	struct nvme_rdma_qpair	*rqpair;
281851a8dfeSShuhei Matsumoto 	struct ibv_recv_wr	*recv_wr;
282fadfef63SSeth Howell 	struct nvme_rdma_wr	rdma_wr;
283fadfef63SSeth Howell };
284fadfef63SSeth Howell 
285110335f1SAlexey Marchuk struct nvme_rdma_memory_translation_ctx {
286110335f1SAlexey Marchuk 	void *addr;
287110335f1SAlexey Marchuk 	size_t length;
288110335f1SAlexey Marchuk 	uint32_t lkey;
289110335f1SAlexey Marchuk 	uint32_t rkey;
290110335f1SAlexey Marchuk };
291110335f1SAlexey Marchuk 
292b8a5cb99SZiye Yang static const char *rdma_cm_event_str[] = {
293b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_ADDR_RESOLVED",
294b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_ADDR_ERROR",
295b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_ROUTE_RESOLVED",
296b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_ROUTE_ERROR",
297b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_CONNECT_REQUEST",
298b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_CONNECT_RESPONSE",
299b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_CONNECT_ERROR",
300b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_UNREACHABLE",
301b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_REJECTED",
302b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_ESTABLISHED",
303b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_DISCONNECTED",
304b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_DEVICE_REMOVAL",
305b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_MULTICAST_JOIN",
306b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_MULTICAST_ERROR",
307b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_ADDR_CHANGE",
308b8a5cb99SZiye Yang 	"RDMA_CM_EVENT_TIMEWAIT_EXIT"
309b8a5cb99SZiye Yang };
310b8a5cb99SZiye Yang 
3113b26e2c5SEvgeniy Kochetov static struct nvme_rdma_poller *nvme_rdma_poll_group_get_poller(struct nvme_rdma_poll_group *group,
3123b26e2c5SEvgeniy Kochetov 		struct ibv_context *device);
3133b26e2c5SEvgeniy Kochetov static void nvme_rdma_poll_group_put_poller(struct nvme_rdma_poll_group *group,
3143b26e2c5SEvgeniy Kochetov 		struct nvme_rdma_poller *poller);
315cdaf4fd9SDaniel Verkamp 
31654a022ddSBen Walker static int nvme_rdma_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr,
317b2225ff5SSeth Howell 		struct spdk_nvme_qpair *qpair);
318a9e43691SDaniel Verkamp 
3192c140f58SAlexey Marchuk static inline int nvme_rdma_memory_domain_transfer_data(struct spdk_memory_domain *dst_domain,
3202c140f58SAlexey Marchuk 		void *dst_domain_ctx,
3212c140f58SAlexey Marchuk 		struct iovec *dst_iov, uint32_t dst_iovcnt,
3222c140f58SAlexey Marchuk 		struct spdk_memory_domain *src_domain, void *src_domain_ctx,
3232c140f58SAlexey Marchuk 		struct iovec *src_iov, uint32_t src_iovcnt,
3242c140f58SAlexey Marchuk 		struct spdk_memory_domain_translation_result *translation,
3252c140f58SAlexey Marchuk 		spdk_memory_domain_data_cpl_cb cpl_cb, void *cpl_cb_arg);
3262c140f58SAlexey Marchuk 
3272c140f58SAlexey Marchuk static inline int _nvme_rdma_qpair_submit_request(struct nvme_rdma_qpair *rqpair,
3282c140f58SAlexey Marchuk 		struct spdk_nvme_rdma_req *rdma_req);
3292c140f58SAlexey Marchuk 
330246c39a7SZiye Yang static inline struct nvme_rdma_qpair *
331246c39a7SZiye Yang nvme_rdma_qpair(struct spdk_nvme_qpair *qpair)
332246c39a7SZiye Yang {
3335b8c0c5aSBen Walker 	assert(qpair->trtype == SPDK_NVME_TRANSPORT_RDMA);
3344a9dce9aSDaniel Verkamp 	return SPDK_CONTAINEROF(qpair, struct nvme_rdma_qpair, qpair);
335246c39a7SZiye Yang }
336246c39a7SZiye Yang 
3378bef6f0bSSeth Howell static inline struct nvme_rdma_poll_group *
3388bef6f0bSSeth Howell nvme_rdma_poll_group(struct spdk_nvme_transport_poll_group *group)
3398bef6f0bSSeth Howell {
3408bef6f0bSSeth Howell 	return (SPDK_CONTAINEROF(group, struct nvme_rdma_poll_group, group));
3418bef6f0bSSeth Howell }
3428bef6f0bSSeth Howell 
343246c39a7SZiye Yang static inline struct nvme_rdma_ctrlr *
344246c39a7SZiye Yang nvme_rdma_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
345246c39a7SZiye Yang {
34632e838afSBen Walker 	assert(ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_RDMA);
3474a9dce9aSDaniel Verkamp 	return SPDK_CONTAINEROF(ctrlr, struct nvme_rdma_ctrlr, ctrlr);
348246c39a7SZiye Yang }
349246c39a7SZiye Yang 
350f0e4b91fSAlexey Marchuk static inline struct spdk_nvme_rdma_req *
351246c39a7SZiye Yang nvme_rdma_req_get(struct nvme_rdma_qpair *rqpair)
352246c39a7SZiye Yang {
353246c39a7SZiye Yang 	struct spdk_nvme_rdma_req *rdma_req;
354246c39a7SZiye Yang 
355bc165a26SDaniel Verkamp 	rdma_req = TAILQ_FIRST(&rqpair->free_reqs);
356f0e4b91fSAlexey Marchuk 	if (spdk_likely(rdma_req)) {
357bc165a26SDaniel Verkamp 		TAILQ_REMOVE(&rqpair->free_reqs, rdma_req, link);
35811a2f1cfSDaniel Verkamp 	}
359246c39a7SZiye Yang 
360246c39a7SZiye Yang 	return rdma_req;
361246c39a7SZiye Yang }
362246c39a7SZiye Yang 
363f0e4b91fSAlexey Marchuk static inline void
364aa2ea2beSShuhei Matsumoto nvme_rdma_req_put(struct nvme_rdma_qpair *rqpair, struct spdk_nvme_rdma_req *rdma_req)
365aa2ea2beSShuhei Matsumoto {
366aa2ea2beSShuhei Matsumoto 	rdma_req->completion_flags = 0;
367aa2ea2beSShuhei Matsumoto 	rdma_req->req = NULL;
368c71f33eeSShuhei Matsumoto 	rdma_req->rdma_rsp = NULL;
3692c140f58SAlexey Marchuk 	assert(rdma_req->transfer_cpl_cb == NULL);
370aa2ea2beSShuhei Matsumoto 	TAILQ_INSERT_HEAD(&rqpair->free_reqs, rdma_req, link);
371aa2ea2beSShuhei Matsumoto }
372aa2ea2beSShuhei Matsumoto 
3732c140f58SAlexey Marchuk static inline void
3742c140f58SAlexey Marchuk nvme_rdma_finish_data_transfer(struct spdk_nvme_rdma_req *rdma_req, int rc)
3752c140f58SAlexey Marchuk {
3762c140f58SAlexey Marchuk 	spdk_memory_domain_data_cpl_cb cb = rdma_req->transfer_cpl_cb;
3772c140f58SAlexey Marchuk 
3782c140f58SAlexey Marchuk 	SPDK_DEBUGLOG(nvme, "req %p, finish data transfer, rc %d\n", rdma_req, rc);
3792c140f58SAlexey Marchuk 	rdma_req->transfer_cpl_cb = NULL;
3802c140f58SAlexey Marchuk 	assert(cb);
3812c140f58SAlexey Marchuk 	cb(rdma_req->transfer_cpl_cb_arg, rc);
3822c140f58SAlexey Marchuk }
3832c140f58SAlexey Marchuk 
384aa2ea2beSShuhei Matsumoto static void
385465b2f8aSShuhei Matsumoto nvme_rdma_req_complete(struct spdk_nvme_rdma_req *rdma_req,
386e415bf00SJim Harris 		       struct spdk_nvme_cpl *rsp,
387e415bf00SJim Harris 		       bool print_on_error)
388246c39a7SZiye Yang {
389465b2f8aSShuhei Matsumoto 	struct nvme_request *req = rdma_req->req;
390aa2ea2beSShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair;
391e415bf00SJim Harris 	struct spdk_nvme_qpair *qpair;
392e415bf00SJim Harris 	bool error, print_error;
393465b2f8aSShuhei Matsumoto 
394465b2f8aSShuhei Matsumoto 	assert(req != NULL);
395465b2f8aSShuhei Matsumoto 
396e415bf00SJim Harris 	qpair = req->qpair;
397e415bf00SJim Harris 	rqpair = nvme_rdma_qpair(qpair);
398e415bf00SJim Harris 
399e415bf00SJim Harris 	error = spdk_nvme_cpl_is_error(rsp);
400e415bf00SJim Harris 	print_error = error && print_on_error && !qpair->ctrlr->opts.disable_error_logging;
401e415bf00SJim Harris 
402e415bf00SJim Harris 	if (print_error) {
403e415bf00SJim Harris 		spdk_nvme_qpair_print_command(qpair, &req->cmd);
404e415bf00SJim Harris 	}
405e415bf00SJim Harris 
406e415bf00SJim Harris 	if (print_error || SPDK_DEBUGLOG_FLAG_ENABLED("nvme")) {
407e415bf00SJim Harris 		spdk_nvme_qpair_print_completion(qpair, rsp);
408e415bf00SJim Harris 	}
409e415bf00SJim Harris 
410fa457d63SShuhei Matsumoto 	assert(rqpair->num_outstanding_reqs > 0);
411fa457d63SShuhei Matsumoto 	rqpair->num_outstanding_reqs--;
412fa457d63SShuhei Matsumoto 
413aa2ea2beSShuhei Matsumoto 	TAILQ_REMOVE(&rqpair->outstanding_reqs, rdma_req, link);
414aa2ea2beSShuhei Matsumoto 
415e415bf00SJim Harris 	nvme_complete_request(req->cb_fn, req->cb_arg, qpair, req, rsp);
416a6704e45SJim Harris 	nvme_rdma_req_put(rqpair, rdma_req);
417246c39a7SZiye Yang }
418246c39a7SZiye Yang 
419b8a5cb99SZiye Yang static const char *
420b8a5cb99SZiye Yang nvme_rdma_cm_event_str_get(uint32_t event)
421b8a5cb99SZiye Yang {
422b8a5cb99SZiye Yang 	if (event < SPDK_COUNTOF(rdma_cm_event_str)) {
423b8a5cb99SZiye Yang 		return rdma_cm_event_str[event];
424b8a5cb99SZiye Yang 	} else {
425b8a5cb99SZiye Yang 		return "Undefined";
426b8a5cb99SZiye Yang 	}
427b8a5cb99SZiye Yang }
428b8a5cb99SZiye Yang 
429579d44b0SSeth Howell 
430579d44b0SSeth Howell static int
431579d44b0SSeth Howell nvme_rdma_qpair_process_cm_event(struct nvme_rdma_qpair *rqpair)
432579d44b0SSeth Howell {
433579d44b0SSeth Howell 	struct rdma_cm_event				*event = rqpair->evt;
434579d44b0SSeth Howell 	struct spdk_nvmf_rdma_accept_private_data	*accept_data;
435579d44b0SSeth Howell 	int						rc = 0;
436579d44b0SSeth Howell 
437579d44b0SSeth Howell 	if (event) {
438579d44b0SSeth Howell 		switch (event->event) {
439579d44b0SSeth Howell 		case RDMA_CM_EVENT_ADDR_RESOLVED:
440579d44b0SSeth Howell 		case RDMA_CM_EVENT_ADDR_ERROR:
441579d44b0SSeth Howell 		case RDMA_CM_EVENT_ROUTE_RESOLVED:
442579d44b0SSeth Howell 		case RDMA_CM_EVENT_ROUTE_ERROR:
443579d44b0SSeth Howell 			break;
444579d44b0SSeth Howell 		case RDMA_CM_EVENT_CONNECT_REQUEST:
445579d44b0SSeth Howell 			break;
446579d44b0SSeth Howell 		case RDMA_CM_EVENT_CONNECT_ERROR:
447579d44b0SSeth Howell 			break;
448579d44b0SSeth Howell 		case RDMA_CM_EVENT_UNREACHABLE:
449579d44b0SSeth Howell 		case RDMA_CM_EVENT_REJECTED:
450579d44b0SSeth Howell 			break;
451daee62a0SAlexey Marchuk 		case RDMA_CM_EVENT_CONNECT_RESPONSE:
452cf151d60SAlexey Marchuk 			rc = spdk_rdma_provider_qp_complete_connect(rqpair->rdma_qp);
453daee62a0SAlexey Marchuk 		/* fall through */
454579d44b0SSeth Howell 		case RDMA_CM_EVENT_ESTABLISHED:
455579d44b0SSeth Howell 			accept_data = (struct spdk_nvmf_rdma_accept_private_data *)event->param.conn.private_data;
456579d44b0SSeth Howell 			if (accept_data == NULL) {
457579d44b0SSeth Howell 				rc = -1;
458579d44b0SSeth Howell 			} else {
45948642652SEvgeniy Kochetov 				SPDK_DEBUGLOG(nvme, "Requested queue depth %d. Target receive queue depth %d.\n",
460834e3c5aSEvgeniy Kochetov 					      rqpair->num_entries + 1, accept_data->crqsize);
461579d44b0SSeth Howell 			}
462579d44b0SSeth Howell 			break;
463579d44b0SSeth Howell 		case RDMA_CM_EVENT_DISCONNECTED:
46424bca2eaSSeth Howell 			rqpair->qpair.transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_REMOTE;
46524bca2eaSSeth Howell 			break;
466579d44b0SSeth Howell 		case RDMA_CM_EVENT_DEVICE_REMOVAL:
46724bca2eaSSeth Howell 			rqpair->qpair.transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_LOCAL;
468e44d6317Ssijie.sun 			rqpair->need_destroy = true;
469579d44b0SSeth Howell 			break;
470579d44b0SSeth Howell 		case RDMA_CM_EVENT_MULTICAST_JOIN:
471579d44b0SSeth Howell 		case RDMA_CM_EVENT_MULTICAST_ERROR:
472579d44b0SSeth Howell 			break;
473579d44b0SSeth Howell 		case RDMA_CM_EVENT_ADDR_CHANGE:
47424bca2eaSSeth Howell 			rqpair->qpair.transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_LOCAL;
475579d44b0SSeth Howell 			break;
476579d44b0SSeth Howell 		case RDMA_CM_EVENT_TIMEWAIT_EXIT:
477579d44b0SSeth Howell 			break;
478579d44b0SSeth Howell 		default:
479579d44b0SSeth Howell 			SPDK_ERRLOG("Unexpected Acceptor Event [%d]\n", event->event);
480579d44b0SSeth Howell 			break;
481579d44b0SSeth Howell 		}
482579d44b0SSeth Howell 		rqpair->evt = NULL;
483579d44b0SSeth Howell 		rdma_ack_cm_event(event);
484579d44b0SSeth Howell 	}
485579d44b0SSeth Howell 
486579d44b0SSeth Howell 	return rc;
487579d44b0SSeth Howell }
488579d44b0SSeth Howell 
4895ac814e3SSeth Howell /*
4905ac814e3SSeth Howell  * This function must be called under the nvme controller's lock
4915ac814e3SSeth Howell  * because it touches global controller variables. The lock is taken
4925ac814e3SSeth Howell  * by the generic transport code before invoking a few of the functions
4935ac814e3SSeth Howell  * in this file: nvme_rdma_ctrlr_connect_qpair, nvme_rdma_ctrlr_delete_io_qpair,
4945ac814e3SSeth Howell  * and conditionally nvme_rdma_qpair_process_completions when it is calling
4955ac814e3SSeth Howell  * completions on the admin qpair. When adding a new call to this function, please
4965ac814e3SSeth Howell  * verify that it is in a situation where it falls under the lock.
4975ac814e3SSeth Howell  */
4985ac814e3SSeth Howell static int
4995ac814e3SSeth Howell nvme_rdma_poll_events(struct nvme_rdma_ctrlr *rctrlr)
5005ac814e3SSeth Howell {
5015ac814e3SSeth Howell 	struct nvme_rdma_cm_event_entry	*entry, *tmp;
5025ac814e3SSeth Howell 	struct nvme_rdma_qpair		*event_qpair;
5035ac814e3SSeth Howell 	struct rdma_cm_event		*event;
5045ac814e3SSeth Howell 	struct rdma_event_channel	*channel = rctrlr->cm_channel;
5055ac814e3SSeth Howell 
5065ac814e3SSeth Howell 	STAILQ_FOREACH_SAFE(entry, &rctrlr->pending_cm_events, link, tmp) {
507622ceb7fSAlexey Marchuk 		event_qpair = entry->evt->id->context;
5085ac814e3SSeth Howell 		if (event_qpair->evt == NULL) {
5095ac814e3SSeth Howell 			event_qpair->evt = entry->evt;
5105ac814e3SSeth Howell 			STAILQ_REMOVE(&rctrlr->pending_cm_events, entry, nvme_rdma_cm_event_entry, link);
5115ac814e3SSeth Howell 			STAILQ_INSERT_HEAD(&rctrlr->free_cm_events, entry, link);
5125ac814e3SSeth Howell 		}
5135ac814e3SSeth Howell 	}
5145ac814e3SSeth Howell 
5155ac814e3SSeth Howell 	while (rdma_get_cm_event(channel, &event) == 0) {
516622ceb7fSAlexey Marchuk 		event_qpair = event->id->context;
5175ac814e3SSeth Howell 		if (event_qpair->evt == NULL) {
5185ac814e3SSeth Howell 			event_qpair->evt = event;
5195ac814e3SSeth Howell 		} else {
5205ac814e3SSeth Howell 			assert(rctrlr == nvme_rdma_ctrlr(event_qpair->qpair.ctrlr));
5215ac814e3SSeth Howell 			entry = STAILQ_FIRST(&rctrlr->free_cm_events);
5225ac814e3SSeth Howell 			if (entry == NULL) {
5235ac814e3SSeth Howell 				rdma_ack_cm_event(event);
5245ac814e3SSeth Howell 				return -ENOMEM;
5255ac814e3SSeth Howell 			}
5265ac814e3SSeth Howell 			STAILQ_REMOVE(&rctrlr->free_cm_events, entry, nvme_rdma_cm_event_entry, link);
5275ac814e3SSeth Howell 			entry->evt = event;
5285ac814e3SSeth Howell 			STAILQ_INSERT_TAIL(&rctrlr->pending_cm_events, entry, link);
5295ac814e3SSeth Howell 		}
5305ac814e3SSeth Howell 	}
5315ac814e3SSeth Howell 
532791ee7deSShuhei Matsumoto 	/* rdma_get_cm_event() returns -1 on error. If an error occurs, errno
533791ee7deSShuhei Matsumoto 	 * will be set to indicate the failure reason. So return negated errno here.
534791ee7deSShuhei Matsumoto 	 */
535791ee7deSShuhei Matsumoto 	return -errno;
5365ac814e3SSeth Howell }
5375ac814e3SSeth Howell 
538579d44b0SSeth Howell static int
539208fbb67SSeth Howell nvme_rdma_validate_cm_event(enum rdma_cm_event_type expected_evt_type,
540208fbb67SSeth Howell 			    struct rdma_cm_event *reaped_evt)
541208fbb67SSeth Howell {
542208fbb67SSeth Howell 	int rc = -EBADMSG;
543208fbb67SSeth Howell 
544208fbb67SSeth Howell 	if (expected_evt_type == reaped_evt->event) {
545208fbb67SSeth Howell 		return 0;
546208fbb67SSeth Howell 	}
547208fbb67SSeth Howell 
5486b87dd80SSeth Howell 	switch (expected_evt_type) {
5496b87dd80SSeth Howell 	case RDMA_CM_EVENT_ESTABLISHED:
5506b87dd80SSeth Howell 		/*
5516b87dd80SSeth Howell 		 * There is an enum ib_cm_rej_reason in the kernel headers that sets 10 as
5526b87dd80SSeth Howell 		 * IB_CM_REJ_STALE_CONN. I can't find the corresponding userspace but we get
5536b87dd80SSeth Howell 		 * the same values here.
5546b87dd80SSeth Howell 		 */
5556b87dd80SSeth Howell 		if (reaped_evt->event == RDMA_CM_EVENT_REJECTED && reaped_evt->status == 10) {
5566b87dd80SSeth Howell 			rc = -ESTALE;
557daee62a0SAlexey Marchuk 		} else if (reaped_evt->event == RDMA_CM_EVENT_CONNECT_RESPONSE) {
558daee62a0SAlexey Marchuk 			/*
559daee62a0SAlexey Marchuk 			 *  If we are using a qpair which is not created using rdma cm API
560daee62a0SAlexey Marchuk 			 *  then we will receive RDMA_CM_EVENT_CONNECT_RESPONSE instead of
561daee62a0SAlexey Marchuk 			 *  RDMA_CM_EVENT_ESTABLISHED.
562daee62a0SAlexey Marchuk 			 */
563daee62a0SAlexey Marchuk 			return 0;
5646b87dd80SSeth Howell 		}
5656b87dd80SSeth Howell 		break;
5666b87dd80SSeth Howell 	default:
5676b87dd80SSeth Howell 		break;
5686b87dd80SSeth Howell 	}
5696b87dd80SSeth Howell 
570208fbb67SSeth Howell 	SPDK_ERRLOG("Expected %s but received %s (%d) from CM event channel (status = %d)\n",
571208fbb67SSeth Howell 		    nvme_rdma_cm_event_str_get(expected_evt_type),
572208fbb67SSeth Howell 		    nvme_rdma_cm_event_str_get(reaped_evt->event), reaped_evt->event,
573208fbb67SSeth Howell 		    reaped_evt->status);
574208fbb67SSeth Howell 	return rc;
575208fbb67SSeth Howell }
576208fbb67SSeth Howell 
577208fbb67SSeth Howell static int
578531c1b0fSShuhei Matsumoto nvme_rdma_process_event_start(struct nvme_rdma_qpair *rqpair,
579cf7f2533SShuhei Matsumoto 			      enum rdma_cm_event_type evt,
580cf7f2533SShuhei Matsumoto 			      nvme_rdma_cm_event_cb evt_cb)
58142dc2836SBen Walker {
582531c1b0fSShuhei Matsumoto 	int	rc;
583531c1b0fSShuhei Matsumoto 
584531c1b0fSShuhei Matsumoto 	assert(evt_cb != NULL);
585579d44b0SSeth Howell 
586579d44b0SSeth Howell 	if (rqpair->evt != NULL) {
5875ac814e3SSeth Howell 		rc = nvme_rdma_qpair_process_cm_event(rqpair);
5885ac814e3SSeth Howell 		if (rc) {
589531c1b0fSShuhei Matsumoto 			return rc;
5905ac814e3SSeth Howell 		}
591579d44b0SSeth Howell 	}
59242dc2836SBen Walker 
593531c1b0fSShuhei Matsumoto 	rqpair->expected_evt_type = evt;
594531c1b0fSShuhei Matsumoto 	rqpair->evt_cb = evt_cb;
5950568555aSEvgeniy Kochetov 	rqpair->evt_timeout_ticks = (g_spdk_nvme_transport_opts.rdma_cm_event_timeout_ms * 1000 *
5960568555aSEvgeniy Kochetov 				     spdk_get_ticks_hz()) / SPDK_SEC_TO_USEC + spdk_get_ticks();
597531c1b0fSShuhei Matsumoto 
598531c1b0fSShuhei Matsumoto 	return 0;
599531c1b0fSShuhei Matsumoto }
600531c1b0fSShuhei Matsumoto 
601531c1b0fSShuhei Matsumoto static int
602531c1b0fSShuhei Matsumoto nvme_rdma_process_event_poll(struct nvme_rdma_qpair *rqpair)
603531c1b0fSShuhei Matsumoto {
604531c1b0fSShuhei Matsumoto 	struct nvme_rdma_ctrlr	*rctrlr;
605531c1b0fSShuhei Matsumoto 	int	rc = 0, rc2;
606531c1b0fSShuhei Matsumoto 
6075ac814e3SSeth Howell 	rctrlr = nvme_rdma_ctrlr(rqpair->qpair.ctrlr);
6085ac814e3SSeth Howell 	assert(rctrlr != NULL);
60913fb1b69SSeth Howell 
610531c1b0fSShuhei Matsumoto 	if (!rqpair->evt && spdk_get_ticks() < rqpair->evt_timeout_ticks) {
6115ac814e3SSeth Howell 		rc = nvme_rdma_poll_events(rctrlr);
612791ee7deSShuhei Matsumoto 		if (rc == -EAGAIN || rc == -EWOULDBLOCK) {
613531c1b0fSShuhei Matsumoto 			return rc;
614791ee7deSShuhei Matsumoto 		}
615ad7a01bdSSeth Howell 	}
616ad7a01bdSSeth Howell 
61713fb1b69SSeth Howell 	if (rqpair->evt == NULL) {
618cf7f2533SShuhei Matsumoto 		rc = -EADDRNOTAVAIL;
619cf7f2533SShuhei Matsumoto 		goto exit;
62013fb1b69SSeth Howell 	}
62113fb1b69SSeth Howell 
622531c1b0fSShuhei Matsumoto 	rc = nvme_rdma_validate_cm_event(rqpair->expected_evt_type, rqpair->evt);
62342dc2836SBen Walker 
6245ac814e3SSeth Howell 	rc2 = nvme_rdma_qpair_process_cm_event(rqpair);
6255ac814e3SSeth Howell 	/* bad message takes precedence over the other error codes from processing the event. */
626cf7f2533SShuhei Matsumoto 	rc = rc == 0 ? rc2 : rc;
627cf7f2533SShuhei Matsumoto 
628cf7f2533SShuhei Matsumoto exit:
629531c1b0fSShuhei Matsumoto 	assert(rqpair->evt_cb != NULL);
630531c1b0fSShuhei Matsumoto 	return rqpair->evt_cb(rqpair, rc);
631531c1b0fSShuhei Matsumoto }
632531c1b0fSShuhei Matsumoto 
633531c1b0fSShuhei Matsumoto static int
6340a61427eSShuhei Matsumoto nvme_rdma_resize_cq(struct nvme_rdma_qpair *rqpair, struct nvme_rdma_poller *poller)
6350a61427eSShuhei Matsumoto {
6360a61427eSShuhei Matsumoto 	int	current_num_wc, required_num_wc;
637d1357fd8SShuhei Matsumoto 	int	max_cq_size;
6380a61427eSShuhei Matsumoto 
6390a61427eSShuhei Matsumoto 	required_num_wc = poller->required_num_wc + WC_PER_QPAIR(rqpair->num_entries);
6400a61427eSShuhei Matsumoto 	current_num_wc = poller->current_num_wc;
6410a61427eSShuhei Matsumoto 	if (current_num_wc < required_num_wc) {
6420a61427eSShuhei Matsumoto 		current_num_wc = spdk_max(current_num_wc * 2, required_num_wc);
6430a61427eSShuhei Matsumoto 	}
6440a61427eSShuhei Matsumoto 
645d1357fd8SShuhei Matsumoto 	max_cq_size = g_spdk_nvme_transport_opts.rdma_max_cq_size;
646d1357fd8SShuhei Matsumoto 	if (max_cq_size != 0 && current_num_wc > max_cq_size) {
647d1357fd8SShuhei Matsumoto 		current_num_wc = max_cq_size;
648d1357fd8SShuhei Matsumoto 	}
649d1357fd8SShuhei Matsumoto 
6500a61427eSShuhei Matsumoto 	if (poller->current_num_wc != current_num_wc) {
6510a61427eSShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "Resize RDMA CQ from %d to %d\n", poller->current_num_wc,
6520a61427eSShuhei Matsumoto 			      current_num_wc);
6530a61427eSShuhei Matsumoto 		if (ibv_resize_cq(poller->cq, current_num_wc)) {
6540a61427eSShuhei Matsumoto 			SPDK_ERRLOG("RDMA CQ resize failed: errno %d: %s\n", errno, spdk_strerror(errno));
6550a61427eSShuhei Matsumoto 			return -1;
6560a61427eSShuhei Matsumoto 		}
6570a61427eSShuhei Matsumoto 
6580a61427eSShuhei Matsumoto 		poller->current_num_wc = current_num_wc;
6590a61427eSShuhei Matsumoto 	}
6600a61427eSShuhei Matsumoto 
6610a61427eSShuhei Matsumoto 	poller->required_num_wc = required_num_wc;
6620a61427eSShuhei Matsumoto 	return 0;
6630a61427eSShuhei Matsumoto }
6640a61427eSShuhei Matsumoto 
6650a61427eSShuhei Matsumoto static int
6661c57fa1aSShuhei Matsumoto nvme_rdma_qpair_set_poller(struct spdk_nvme_qpair *qpair)
6670a61427eSShuhei Matsumoto {
6680a61427eSShuhei Matsumoto 	struct nvme_rdma_qpair          *rqpair = nvme_rdma_qpair(qpair);
6690a61427eSShuhei Matsumoto 	struct nvme_rdma_poll_group     *group = nvme_rdma_poll_group(qpair->poll_group);
6700a61427eSShuhei Matsumoto 	struct nvme_rdma_poller         *poller;
6710a61427eSShuhei Matsumoto 
6720a61427eSShuhei Matsumoto 	assert(rqpair->cq == NULL);
6730a61427eSShuhei Matsumoto 
6743b26e2c5SEvgeniy Kochetov 	poller = nvme_rdma_poll_group_get_poller(group, rqpair->cm_id->verbs);
6753b26e2c5SEvgeniy Kochetov 	if (!poller) {
6760a61427eSShuhei Matsumoto 		SPDK_ERRLOG("Unable to find a cq for qpair %p on poll group %p\n", qpair, qpair->poll_group);
6770a61427eSShuhei Matsumoto 		return -EINVAL;
6780a61427eSShuhei Matsumoto 	}
6790a61427eSShuhei Matsumoto 
680bcd987eaSShuhei Matsumoto 	if (!poller->srq) {
6813b26e2c5SEvgeniy Kochetov 		if (nvme_rdma_resize_cq(rqpair, poller)) {
6823b26e2c5SEvgeniy Kochetov 			nvme_rdma_poll_group_put_poller(group, poller);
6833b26e2c5SEvgeniy Kochetov 			return -EPROTO;
6843b26e2c5SEvgeniy Kochetov 		}
685bcd987eaSShuhei Matsumoto 	}
6863b26e2c5SEvgeniy Kochetov 
6873b26e2c5SEvgeniy Kochetov 	rqpair->cq = poller->cq;
688bcd987eaSShuhei Matsumoto 	rqpair->srq = poller->srq;
6899aabfb59SShuhei Matsumoto 	if (rqpair->srq) {
690bcd987eaSShuhei Matsumoto 		rqpair->rsps = poller->rsps;
6919aabfb59SShuhei Matsumoto 	}
6923b26e2c5SEvgeniy Kochetov 	rqpair->poller = poller;
6930a61427eSShuhei Matsumoto 	return 0;
6940a61427eSShuhei Matsumoto }
6950a61427eSShuhei Matsumoto 
6960a61427eSShuhei Matsumoto static int
697246c39a7SZiye Yang nvme_rdma_qpair_init(struct nvme_rdma_qpair *rqpair)
698246c39a7SZiye Yang {
699246c39a7SZiye Yang 	int			rc;
700cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_qp_init_attr	attr = {};
701e688d1ccSSeth Howell 	struct ibv_device_attr	dev_attr;
7029fb69476Szkhatami88 	struct nvme_rdma_ctrlr	*rctrlr;
703d1357fd8SShuhei Matsumoto 	uint32_t num_cqe, max_num_cqe;
704246c39a7SZiye Yang 
705e688d1ccSSeth Howell 	rc = ibv_query_device(rqpair->cm_id->verbs, &dev_attr);
706e688d1ccSSeth Howell 	if (rc != 0) {
707e688d1ccSSeth Howell 		SPDK_ERRLOG("Failed to query RDMA device attributes.\n");
708e688d1ccSSeth Howell 		return -1;
709e688d1ccSSeth Howell 	}
710e688d1ccSSeth Howell 
7118bef6f0bSSeth Howell 	if (rqpair->qpair.poll_group) {
7128bef6f0bSSeth Howell 		assert(!rqpair->cq);
7131c57fa1aSShuhei Matsumoto 		rc = nvme_rdma_qpair_set_poller(&rqpair->qpair);
7148bef6f0bSSeth Howell 		if (rc) {
7158bef6f0bSSeth Howell 			SPDK_ERRLOG("Unable to activate the rdmaqpair.\n");
7168bef6f0bSSeth Howell 			return -1;
7178bef6f0bSSeth Howell 		}
7188bef6f0bSSeth Howell 		assert(rqpair->cq);
7198bef6f0bSSeth Howell 	} else {
720d1357fd8SShuhei Matsumoto 		num_cqe = rqpair->num_entries * 2;
721d1357fd8SShuhei Matsumoto 		max_num_cqe = g_spdk_nvme_transport_opts.rdma_max_cq_size;
722d1357fd8SShuhei Matsumoto 		if (max_num_cqe != 0 && num_cqe > max_num_cqe) {
723d1357fd8SShuhei Matsumoto 			num_cqe = max_num_cqe;
724d1357fd8SShuhei Matsumoto 		}
725d1357fd8SShuhei Matsumoto 		rqpair->cq = ibv_create_cq(rqpair->cm_id->verbs, num_cqe, rqpair, NULL, 0);
726ac9b92c8SBen Walker 		if (!rqpair->cq) {
727891c12a6SPawel Wodkowski 			SPDK_ERRLOG("Unable to create completion queue: errno %d: %s\n", errno, spdk_strerror(errno));
728ac9b92c8SBen Walker 			return -1;
729ac9b92c8SBen Walker 		}
7308bef6f0bSSeth Howell 	}
731ac9b92c8SBen Walker 
7329fb69476Szkhatami88 	rctrlr = nvme_rdma_ctrlr(rqpair->qpair.ctrlr);
733bf1a82cfSBen Walker 	if (g_nvme_hooks.get_ibv_pd) {
734d75daea5SShuhei Matsumoto 		attr.pd = g_nvme_hooks.get_ibv_pd(&rctrlr->ctrlr.trid, rqpair->cm_id->verbs);
7359fb69476Szkhatami88 	} else {
7368a01b4d6SAlexey Marchuk 		attr.pd = spdk_rdma_utils_get_pd(rqpair->cm_id->verbs);
7379fb69476Szkhatami88 	}
7389fb69476Szkhatami88 
739527f406bSAlexey Marchuk 	attr.stats		= rqpair->poller ? &rqpair->poller->stats.rdma_stats : NULL;
740ac9b92c8SBen Walker 	attr.send_cq		= rqpair->cq;
741ac9b92c8SBen Walker 	attr.recv_cq		= rqpair->cq;
742df8129fbSDaniel Verkamp 	attr.cap.max_send_wr	= rqpair->num_entries; /* SEND operations */
743bcd987eaSShuhei Matsumoto 	if (rqpair->srq) {
744bcd987eaSShuhei Matsumoto 		attr.srq	= rqpair->srq->srq;
745bcd987eaSShuhei Matsumoto 	} else {
746df8129fbSDaniel Verkamp 		attr.cap.max_recv_wr = rqpair->num_entries; /* RECV operations */
747bcd987eaSShuhei Matsumoto 	}
748e688d1ccSSeth Howell 	attr.cap.max_send_sge	= spdk_min(NVME_RDMA_DEFAULT_TX_SGE, dev_attr.max_sge);
749e688d1ccSSeth Howell 	attr.cap.max_recv_sge	= spdk_min(NVME_RDMA_DEFAULT_RX_SGE, dev_attr.max_sge);
7502c140f58SAlexey Marchuk 	attr.domain_transfer	= spdk_rdma_provider_accel_sequence_supported() ?
7512c140f58SAlexey Marchuk 				  nvme_rdma_memory_domain_transfer_data : NULL;
752246c39a7SZiye Yang 
753cf151d60SAlexey Marchuk 	rqpair->rdma_qp = spdk_rdma_provider_qp_create(rqpair->cm_id, &attr);
754e688d1ccSSeth Howell 
755b4a9d7d3SAlexey Marchuk 	if (!rqpair->rdma_qp) {
756246c39a7SZiye Yang 		return -1;
757246c39a7SZiye Yang 	}
758e688d1ccSSeth Howell 
759e688d1ccSSeth Howell 	/* ibv_create_qp will change the values in attr.cap. Make sure we store the proper value. */
760e688d1ccSSeth Howell 	rqpair->max_send_sge = spdk_min(NVME_RDMA_DEFAULT_TX_SGE, attr.cap.max_send_sge);
76167b0dcfeSSeth Howell 	rqpair->current_num_sends = 0;
762e688d1ccSSeth Howell 
763622ceb7fSAlexey Marchuk 	rqpair->cm_id->context = rqpair;
764246c39a7SZiye Yang 
765246c39a7SZiye Yang 	return 0;
766246c39a7SZiye Yang }
767246c39a7SZiye Yang 
768e22dcc07SShuhei Matsumoto static void
769e22dcc07SShuhei Matsumoto nvme_rdma_reset_failed_sends(struct nvme_rdma_qpair *rqpair,
770a333974eSAlex Michon 			     struct ibv_send_wr *bad_send_wr)
771e22dcc07SShuhei Matsumoto {
772e22dcc07SShuhei Matsumoto 	while (bad_send_wr != NULL) {
773e22dcc07SShuhei Matsumoto 		assert(rqpair->current_num_sends > 0);
774e22dcc07SShuhei Matsumoto 		rqpair->current_num_sends--;
775e22dcc07SShuhei Matsumoto 		bad_send_wr = bad_send_wr->next;
776e22dcc07SShuhei Matsumoto 	}
777e22dcc07SShuhei Matsumoto }
778e22dcc07SShuhei Matsumoto 
779e22dcc07SShuhei Matsumoto static void
7804999a985SShuhei Matsumoto nvme_rdma_reset_failed_recvs(struct nvme_rdma_rsps *rsps,
781e22dcc07SShuhei Matsumoto 			     struct ibv_recv_wr *bad_recv_wr, int rc)
782e22dcc07SShuhei Matsumoto {
783e22dcc07SShuhei Matsumoto 	SPDK_ERRLOG("Failed to post WRs on receive queue, errno %d (%s), bad_wr %p\n",
784e22dcc07SShuhei Matsumoto 		    rc, spdk_strerror(rc), bad_recv_wr);
785e22dcc07SShuhei Matsumoto 	while (bad_recv_wr != NULL) {
7864999a985SShuhei Matsumoto 		assert(rsps->current_num_recvs > 0);
7874999a985SShuhei Matsumoto 		rsps->current_num_recvs--;
788e22dcc07SShuhei Matsumoto 		bad_recv_wr = bad_recv_wr->next;
789e22dcc07SShuhei Matsumoto 	}
790e22dcc07SShuhei Matsumoto }
791e22dcc07SShuhei Matsumoto 
792731dca3dSEvgeniy Kochetov static inline int
793731dca3dSEvgeniy Kochetov nvme_rdma_qpair_submit_sends(struct nvme_rdma_qpair *rqpair)
794731dca3dSEvgeniy Kochetov {
795846bb4b9Syidong0635 	struct ibv_send_wr *bad_send_wr = NULL;
796731dca3dSEvgeniy Kochetov 	int rc;
797731dca3dSEvgeniy Kochetov 
798cf151d60SAlexey Marchuk 	rc = spdk_rdma_provider_qp_flush_send_wrs(rqpair->rdma_qp, &bad_send_wr);
7998c6a3455SAlexey Marchuk 
800731dca3dSEvgeniy Kochetov 	if (spdk_unlikely(rc)) {
801a333974eSAlex Michon 		SPDK_ERRLOG("Failed to post WRs on send queue, errno %d (%s), bad_wr %p\n",
802a333974eSAlex Michon 			    rc, spdk_strerror(rc), bad_send_wr);
803a333974eSAlex Michon 		nvme_rdma_reset_failed_sends(rqpair, bad_send_wr);
804731dca3dSEvgeniy Kochetov 	}
8058c6a3455SAlexey Marchuk 
806e22dcc07SShuhei Matsumoto 	return rc;
807731dca3dSEvgeniy Kochetov }
808731dca3dSEvgeniy Kochetov 
809731dca3dSEvgeniy Kochetov static inline int
810731dca3dSEvgeniy Kochetov nvme_rdma_qpair_submit_recvs(struct nvme_rdma_qpair *rqpair)
811731dca3dSEvgeniy Kochetov {
812731dca3dSEvgeniy Kochetov 	struct ibv_recv_wr *bad_recv_wr;
813d76951c7SJin Yu 	int rc = 0;
814731dca3dSEvgeniy Kochetov 
815cf151d60SAlexey Marchuk 	rc = spdk_rdma_provider_qp_flush_recv_wrs(rqpair->rdma_qp, &bad_recv_wr);
816731dca3dSEvgeniy Kochetov 	if (spdk_unlikely(rc)) {
8174999a985SShuhei Matsumoto 		nvme_rdma_reset_failed_recvs(rqpair->rsps, bad_recv_wr, rc);
818731dca3dSEvgeniy Kochetov 	}
8199b86f31aSAlexey Marchuk 
820d76951c7SJin Yu 	return rc;
821731dca3dSEvgeniy Kochetov }
822731dca3dSEvgeniy Kochetov 
823bcd987eaSShuhei Matsumoto static inline int
824bcd987eaSShuhei Matsumoto nvme_rdma_poller_submit_recvs(struct nvme_rdma_poller *poller)
825bcd987eaSShuhei Matsumoto {
826bcd987eaSShuhei Matsumoto 	struct ibv_recv_wr *bad_recv_wr;
827bcd987eaSShuhei Matsumoto 	int rc;
828bcd987eaSShuhei Matsumoto 
829cf151d60SAlexey Marchuk 	rc = spdk_rdma_provider_srq_flush_recv_wrs(poller->srq, &bad_recv_wr);
830bcd987eaSShuhei Matsumoto 	if (spdk_unlikely(rc)) {
831bcd987eaSShuhei Matsumoto 		nvme_rdma_reset_failed_recvs(poller->rsps, bad_recv_wr, rc);
832bcd987eaSShuhei Matsumoto 	}
833bcd987eaSShuhei Matsumoto 
834bcd987eaSShuhei Matsumoto 	return rc;
835bcd987eaSShuhei Matsumoto }
836bcd987eaSShuhei Matsumoto 
8371712d1b7SDaniel Verkamp #define nvme_rdma_trace_ibv_sge(sg_list) \
8381712d1b7SDaniel Verkamp 	if (sg_list) { \
8392172c432STomasz Zawadzki 		SPDK_DEBUGLOG(nvme, "local addr %p length 0x%x lkey 0x%x\n", \
8401712d1b7SDaniel Verkamp 			      (void *)(sg_list)->addr, (sg_list)->length, (sg_list)->lkey); \
841246c39a7SZiye Yang 	}
842246c39a7SZiye Yang 
8436949c71dSJim Harris static void
8444999a985SShuhei Matsumoto nvme_rdma_free_rsps(struct nvme_rdma_rsps *rsps)
8456949c71dSJim Harris {
8464999a985SShuhei Matsumoto 	if (!rsps) {
8474999a985SShuhei Matsumoto 		return;
848246c39a7SZiye Yang 	}
849246c39a7SZiye Yang 
8504999a985SShuhei Matsumoto 	spdk_free(rsps->rsps);
8514999a985SShuhei Matsumoto 	spdk_free(rsps->rsp_sgls);
8524999a985SShuhei Matsumoto 	spdk_free(rsps->rsp_recv_wrs);
8534999a985SShuhei Matsumoto 	spdk_free(rsps);
8544999a985SShuhei Matsumoto }
8554999a985SShuhei Matsumoto 
8564999a985SShuhei Matsumoto static struct nvme_rdma_rsps *
8574999a985SShuhei Matsumoto nvme_rdma_create_rsps(struct nvme_rdma_rsp_opts *opts)
858246c39a7SZiye Yang {
8594999a985SShuhei Matsumoto 	struct nvme_rdma_rsps *rsps;
8608a01b4d6SAlexey Marchuk 	struct spdk_rdma_utils_memory_translation translation;
8614cef00cbSShuhei Matsumoto 	uint16_t i;
8624cef00cbSShuhei Matsumoto 	int rc;
8634cef00cbSShuhei Matsumoto 
864186b109dSJim Harris 	rsps = spdk_zmalloc(sizeof(*rsps), 0, NULL, SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
8654999a985SShuhei Matsumoto 	if (!rsps) {
8664999a985SShuhei Matsumoto 		SPDK_ERRLOG("Failed to allocate rsps object\n");
8674999a985SShuhei Matsumoto 		return NULL;
8684999a985SShuhei Matsumoto 	}
869246c39a7SZiye Yang 
8704999a985SShuhei Matsumoto 	rsps->rsp_sgls = spdk_zmalloc(opts->num_entries * sizeof(*rsps->rsp_sgls), 0, NULL,
871186b109dSJim Harris 				      SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
8724999a985SShuhei Matsumoto 	if (!rsps->rsp_sgls) {
8736ab28a20SDaniel Verkamp 		SPDK_ERRLOG("Failed to allocate rsp_sgls\n");
874246c39a7SZiye Yang 		goto fail;
875246c39a7SZiye Yang 	}
876246c39a7SZiye Yang 
8774999a985SShuhei Matsumoto 	rsps->rsp_recv_wrs = spdk_zmalloc(opts->num_entries * sizeof(*rsps->rsp_recv_wrs), 0, NULL,
878186b109dSJim Harris 					  SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
8794999a985SShuhei Matsumoto 	if (!rsps->rsp_recv_wrs) {
880c21e9fa5SZiye Yang 		SPDK_ERRLOG("Failed to allocate rsp_recv_wrs\n");
881c21e9fa5SZiye Yang 		goto fail;
882c21e9fa5SZiye Yang 	}
883c21e9fa5SZiye Yang 
8844999a985SShuhei Matsumoto 	rsps->rsps = spdk_zmalloc(opts->num_entries * sizeof(*rsps->rsps), 0, NULL,
885186b109dSJim Harris 				  SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
8864999a985SShuhei Matsumoto 	if (!rsps->rsps) {
8876ab28a20SDaniel Verkamp 		SPDK_ERRLOG("can not allocate rdma rsps\n");
8886ab28a20SDaniel Verkamp 		goto fail;
8896ab28a20SDaniel Verkamp 	}
890246c39a7SZiye Yang 
8914999a985SShuhei Matsumoto 	for (i = 0; i < opts->num_entries; i++) {
8924999a985SShuhei Matsumoto 		struct ibv_sge *rsp_sgl = &rsps->rsp_sgls[i];
8934999a985SShuhei Matsumoto 		struct spdk_nvme_rdma_rsp *rsp = &rsps->rsps[i];
8944999a985SShuhei Matsumoto 		struct ibv_recv_wr *recv_wr = &rsps->rsp_recv_wrs[i];
8956ab28a20SDaniel Verkamp 
8964999a985SShuhei Matsumoto 		rsp->rqpair = opts->rqpair;
897fadfef63SSeth Howell 		rsp->rdma_wr.type = RDMA_WR_TYPE_RECV;
898851a8dfeSShuhei Matsumoto 		rsp->recv_wr = recv_wr;
899851a8dfeSShuhei Matsumoto 		rsp_sgl->addr = (uint64_t)rsp;
900fadfef63SSeth Howell 		rsp_sgl->length = sizeof(struct spdk_nvme_cpl);
9018a01b4d6SAlexey Marchuk 		rc = spdk_rdma_utils_get_translation(opts->mr_map, rsp, sizeof(*rsp), &translation);
90277aef307SAleksey Marchuk 		if (rc) {
9034cef00cbSShuhei Matsumoto 			goto fail;
90477aef307SAleksey Marchuk 		}
9058a01b4d6SAlexey Marchuk 		rsp_sgl->lkey = spdk_rdma_utils_memory_translation_get_lkey(&translation);
9066ab28a20SDaniel Verkamp 
907851a8dfeSShuhei Matsumoto 		recv_wr->wr_id = (uint64_t)&rsp->rdma_wr;
908851a8dfeSShuhei Matsumoto 		recv_wr->next = NULL;
909851a8dfeSShuhei Matsumoto 		recv_wr->sg_list = rsp_sgl;
910851a8dfeSShuhei Matsumoto 		recv_wr->num_sge = 1;
91127cf11d0SZiye Yang 
9126275f844SShuhei Matsumoto 		nvme_rdma_trace_ibv_sge(recv_wr->sg_list);
913cd640f62SShuhei Matsumoto 
914bcd987eaSShuhei Matsumoto 		if (opts->rqpair) {
915cf151d60SAlexey Marchuk 			spdk_rdma_provider_qp_queue_recv_wrs(opts->rqpair->rdma_qp, recv_wr);
916bcd987eaSShuhei Matsumoto 		} else {
917cf151d60SAlexey Marchuk 			spdk_rdma_provider_srq_queue_recv_wrs(opts->srq, recv_wr);
918bcd987eaSShuhei Matsumoto 		}
919246c39a7SZiye Yang 	}
920246c39a7SZiye Yang 
9214999a985SShuhei Matsumoto 	rsps->num_entries = opts->num_entries;
9224999a985SShuhei Matsumoto 	rsps->current_num_recvs = opts->num_entries;
923cd640f62SShuhei Matsumoto 
9244999a985SShuhei Matsumoto 	return rsps;
9254cef00cbSShuhei Matsumoto fail:
9264999a985SShuhei Matsumoto 	nvme_rdma_free_rsps(rsps);
9274999a985SShuhei Matsumoto 	return NULL;
9286949c71dSJim Harris }
9296949c71dSJim Harris 
9306949c71dSJim Harris static void
931246c39a7SZiye Yang nvme_rdma_free_reqs(struct nvme_rdma_qpair *rqpair)
932246c39a7SZiye Yang {
933246c39a7SZiye Yang 	if (!rqpair->rdma_reqs) {
934246c39a7SZiye Yang 		return;
935246c39a7SZiye Yang 	}
936246c39a7SZiye Yang 
937c66b68e9SAleksey Marchuk 	spdk_free(rqpair->cmds);
938fb31963cSDaniel Verkamp 	rqpair->cmds = NULL;
939fb31963cSDaniel Verkamp 
940c66b68e9SAleksey Marchuk 	spdk_free(rqpair->rdma_reqs);
941fb31963cSDaniel Verkamp 	rqpair->rdma_reqs = NULL;
942246c39a7SZiye Yang }
943246c39a7SZiye Yang 
944246c39a7SZiye Yang static int
9454cef00cbSShuhei Matsumoto nvme_rdma_create_reqs(struct nvme_rdma_qpair *rqpair)
946246c39a7SZiye Yang {
9478a01b4d6SAlexey Marchuk 	struct spdk_rdma_utils_memory_translation translation;
948581e1bb5SAlexey Marchuk 	uint16_t i;
9494cef00cbSShuhei Matsumoto 	int rc;
9506b314fb5SSeth Howell 
9519aabfb59SShuhei Matsumoto 	assert(!rqpair->rdma_reqs);
952c66b68e9SAleksey Marchuk 	rqpair->rdma_reqs = spdk_zmalloc(rqpair->num_entries * sizeof(struct spdk_nvme_rdma_req), 0, NULL,
953186b109dSJim Harris 					 SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
954be92f47fSDaniel Verkamp 	if (rqpair->rdma_reqs == NULL) {
955be92f47fSDaniel Verkamp 		SPDK_ERRLOG("Failed to allocate rdma_reqs\n");
956be92f47fSDaniel Verkamp 		goto fail;
957be92f47fSDaniel Verkamp 	}
958be92f47fSDaniel Verkamp 
9599aabfb59SShuhei Matsumoto 	assert(!rqpair->cmds);
960c66b68e9SAleksey Marchuk 	rqpair->cmds = spdk_zmalloc(rqpair->num_entries * sizeof(*rqpair->cmds), 0, NULL,
961186b109dSJim Harris 				    SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
962fb31963cSDaniel Verkamp 	if (!rqpair->cmds) {
963fb31963cSDaniel Verkamp 		SPDK_ERRLOG("Failed to allocate RDMA cmds\n");
964fb31963cSDaniel Verkamp 		goto fail;
965fb31963cSDaniel Verkamp 	}
966fb31963cSDaniel Verkamp 
9676b314fb5SSeth Howell 	TAILQ_INIT(&rqpair->free_reqs);
9686b314fb5SSeth Howell 	TAILQ_INIT(&rqpair->outstanding_reqs);
9696b314fb5SSeth Howell 	for (i = 0; i < rqpair->num_entries; i++) {
9706b314fb5SSeth Howell 		struct spdk_nvme_rdma_req	*rdma_req;
9716b314fb5SSeth Howell 		struct spdk_nvmf_cmd		*cmd;
9726b314fb5SSeth Howell 
9736b314fb5SSeth Howell 		rdma_req = &rqpair->rdma_reqs[i];
974fadfef63SSeth Howell 		rdma_req->rdma_wr.type = RDMA_WR_TYPE_SEND;
9756b314fb5SSeth Howell 		cmd = &rqpair->cmds[i];
9766b314fb5SSeth Howell 
9776b314fb5SSeth Howell 		rdma_req->id = i;
9786b314fb5SSeth Howell 
9798a01b4d6SAlexey Marchuk 		rc = spdk_rdma_utils_get_translation(rqpair->mr_map, cmd, sizeof(*cmd), &translation);
9804cef00cbSShuhei Matsumoto 		if (rc) {
9814cef00cbSShuhei Matsumoto 			goto fail;
9824cef00cbSShuhei Matsumoto 		}
9838a01b4d6SAlexey Marchuk 		rdma_req->send_sgl[0].lkey = spdk_rdma_utils_memory_translation_get_lkey(&translation);
9844cef00cbSShuhei Matsumoto 
9856b314fb5SSeth Howell 		/* The first RDMA sgl element will always point
9866b314fb5SSeth Howell 		 * at this data structure. Depending on whether
9876b314fb5SSeth Howell 		 * an NVMe-oF SGL is required, the length of
9886b314fb5SSeth Howell 		 * this element may change. */
9896b314fb5SSeth Howell 		rdma_req->send_sgl[0].addr = (uint64_t)cmd;
990fadfef63SSeth Howell 		rdma_req->send_wr.wr_id = (uint64_t)&rdma_req->rdma_wr;
9916b314fb5SSeth Howell 		rdma_req->send_wr.next = NULL;
9926b314fb5SSeth Howell 		rdma_req->send_wr.opcode = IBV_WR_SEND;
9936b314fb5SSeth Howell 		rdma_req->send_wr.send_flags = IBV_SEND_SIGNALED;
9946b314fb5SSeth Howell 		rdma_req->send_wr.sg_list = rdma_req->send_sgl;
9956b314fb5SSeth Howell 		rdma_req->send_wr.imm_data = 0;
9966b314fb5SSeth Howell 
9976b314fb5SSeth Howell 		TAILQ_INSERT_TAIL(&rqpair->free_reqs, rdma_req, link);
9986b314fb5SSeth Howell 	}
9996b314fb5SSeth Howell 
10006949c71dSJim Harris 	return 0;
10016949c71dSJim Harris fail:
10026949c71dSJim Harris 	nvme_rdma_free_reqs(rqpair);
10036949c71dSJim Harris 	return -ENOMEM;
10046949c71dSJim Harris }
10056949c71dSJim Harris 
1006bcf08457SShuhei Matsumoto static int nvme_rdma_connect(struct nvme_rdma_qpair *rqpair);
1007246c39a7SZiye Yang 
1008bcf08457SShuhei Matsumoto static int
1009bcf08457SShuhei Matsumoto nvme_rdma_route_resolved(struct nvme_rdma_qpair *rqpair, int ret)
1010bcf08457SShuhei Matsumoto {
1011246c39a7SZiye Yang 	if (ret) {
1012bcf08457SShuhei Matsumoto 		SPDK_ERRLOG("RDMA route resolution error\n");
1013bcf08457SShuhei Matsumoto 		return -1;
1014246c39a7SZiye Yang 	}
1015246c39a7SZiye Yang 
1016bcf08457SShuhei Matsumoto 	ret = nvme_rdma_qpair_init(rqpair);
1017bcf08457SShuhei Matsumoto 	if (ret < 0) {
1018bcf08457SShuhei Matsumoto 		SPDK_ERRLOG("nvme_rdma_qpair_init() failed\n");
1019bcf08457SShuhei Matsumoto 		return -1;
1020bcf08457SShuhei Matsumoto 	}
1021bcf08457SShuhei Matsumoto 
1022bcf08457SShuhei Matsumoto 	return nvme_rdma_connect(rqpair);
1023bcf08457SShuhei Matsumoto }
1024bcf08457SShuhei Matsumoto 
1025bcf08457SShuhei Matsumoto static int
1026bcf08457SShuhei Matsumoto nvme_rdma_addr_resolved(struct nvme_rdma_qpair *rqpair, int ret)
1027bcf08457SShuhei Matsumoto {
1028579d44b0SSeth Howell 	if (ret) {
102942dc2836SBen Walker 		SPDK_ERRLOG("RDMA address resolution error\n");
1030246c39a7SZiye Yang 		return -1;
1031246c39a7SZiye Yang 	}
1032246c39a7SZiye Yang 
103394966468SAlexey Marchuk 	if (rqpair->qpair.ctrlr->opts.transport_ack_timeout != SPDK_NVME_TRANSPORT_ACK_TIMEOUT_DISABLED) {
103494966468SAlexey Marchuk #ifdef SPDK_CONFIG_RDMA_SET_ACK_TIMEOUT
103594966468SAlexey Marchuk 		uint8_t timeout = rqpair->qpair.ctrlr->opts.transport_ack_timeout;
103694966468SAlexey Marchuk 		ret = rdma_set_option(rqpair->cm_id, RDMA_OPTION_ID,
103794966468SAlexey Marchuk 				      RDMA_OPTION_ID_ACK_TIMEOUT,
103894966468SAlexey Marchuk 				      &timeout, sizeof(timeout));
103994966468SAlexey Marchuk 		if (ret) {
104094966468SAlexey Marchuk 			SPDK_NOTICELOG("Can't apply RDMA_OPTION_ID_ACK_TIMEOUT %d, ret %d\n", timeout, ret);
104194966468SAlexey Marchuk 		}
104294966468SAlexey Marchuk #else
10432172c432STomasz Zawadzki 		SPDK_DEBUGLOG(nvme, "transport_ack_timeout is not supported\n");
104494966468SAlexey Marchuk #endif
104594966468SAlexey Marchuk 	}
104694966468SAlexey Marchuk 
10477706450fSMichael Haeuptle 	if (rqpair->qpair.ctrlr->opts.transport_tos != SPDK_NVME_TRANSPORT_TOS_DISABLED) {
10487706450fSMichael Haeuptle #ifdef SPDK_CONFIG_RDMA_SET_TOS
10497706450fSMichael Haeuptle 		uint8_t tos = rqpair->qpair.ctrlr->opts.transport_tos;
10507706450fSMichael Haeuptle 		ret = rdma_set_option(rqpair->cm_id, RDMA_OPTION_ID, RDMA_OPTION_ID_TOS, &tos, sizeof(tos));
10517706450fSMichael Haeuptle 		if (ret) {
10527706450fSMichael Haeuptle 			SPDK_NOTICELOG("Can't apply RDMA_OPTION_ID_TOS %u, ret %d\n", tos, ret);
10537706450fSMichael Haeuptle 		}
10547706450fSMichael Haeuptle #else
10557706450fSMichael Haeuptle 		SPDK_DEBUGLOG(nvme, "transport_tos is not supported\n");
10567706450fSMichael Haeuptle #endif
10577706450fSMichael Haeuptle 	}
10587706450fSMichael Haeuptle 
1059246c39a7SZiye Yang 	ret = rdma_resolve_route(rqpair->cm_id, NVME_RDMA_TIME_OUT_IN_MS);
1060246c39a7SZiye Yang 	if (ret) {
1061246c39a7SZiye Yang 		SPDK_ERRLOG("rdma_resolve_route\n");
1062246c39a7SZiye Yang 		return ret;
1063246c39a7SZiye Yang 	}
106442dc2836SBen Walker 
10659717b0c3SShuhei Matsumoto 	return nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_ROUTE_RESOLVED,
1066cf7f2533SShuhei Matsumoto 					     nvme_rdma_route_resolved);
1067bcf08457SShuhei Matsumoto }
1068bcf08457SShuhei Matsumoto 
1069bcf08457SShuhei Matsumoto static int
1070bcf08457SShuhei Matsumoto nvme_rdma_resolve_addr(struct nvme_rdma_qpair *rqpair,
1071bcf08457SShuhei Matsumoto 		       struct sockaddr *src_addr,
1072bcf08457SShuhei Matsumoto 		       struct sockaddr *dst_addr)
1073bcf08457SShuhei Matsumoto {
1074bcf08457SShuhei Matsumoto 	int ret;
1075bcf08457SShuhei Matsumoto 
10764a6f8588SShuhei Matsumoto 	if (src_addr) {
10774a6f8588SShuhei Matsumoto 		int reuse = 1;
10784a6f8588SShuhei Matsumoto 
10794a6f8588SShuhei Matsumoto 		ret = rdma_set_option(rqpair->cm_id, RDMA_OPTION_ID, RDMA_OPTION_ID_REUSEADDR,
10804a6f8588SShuhei Matsumoto 				      &reuse, sizeof(reuse));
10814a6f8588SShuhei Matsumoto 		if (ret) {
10824a6f8588SShuhei Matsumoto 			SPDK_NOTICELOG("Can't apply RDMA_OPTION_ID_REUSEADDR %d, ret %d\n",
10834a6f8588SShuhei Matsumoto 				       reuse, ret);
10844a6f8588SShuhei Matsumoto 			/* It is likely that rdma_resolve_addr() returns -EADDRINUSE, but
10854a6f8588SShuhei Matsumoto 			 * we may missing something. We rely on rdma_resolve_addr().
10864a6f8588SShuhei Matsumoto 			 */
10874a6f8588SShuhei Matsumoto 		}
10884a6f8588SShuhei Matsumoto 	}
10894a6f8588SShuhei Matsumoto 
1090bcf08457SShuhei Matsumoto 	ret = rdma_resolve_addr(rqpair->cm_id, src_addr, dst_addr,
1091bcf08457SShuhei Matsumoto 				NVME_RDMA_TIME_OUT_IN_MS);
1092579d44b0SSeth Howell 	if (ret) {
1093bcf08457SShuhei Matsumoto 		SPDK_ERRLOG("rdma_resolve_addr, %d\n", errno);
1094bcf08457SShuhei Matsumoto 		return ret;
1095bcf08457SShuhei Matsumoto 	}
1096bcf08457SShuhei Matsumoto 
10979717b0c3SShuhei Matsumoto 	return nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_ADDR_RESOLVED,
1098cf7f2533SShuhei Matsumoto 					     nvme_rdma_addr_resolved);
1099bcf08457SShuhei Matsumoto }
1100bcf08457SShuhei Matsumoto 
110120cf9080SShuhei Matsumoto static int nvme_rdma_stale_conn_retry(struct nvme_rdma_qpair *rqpair);
110220cf9080SShuhei Matsumoto 
1103bcf08457SShuhei Matsumoto static int
1104bcf08457SShuhei Matsumoto nvme_rdma_connect_established(struct nvme_rdma_qpair *rqpair, int ret)
1105bcf08457SShuhei Matsumoto {
11064999a985SShuhei Matsumoto 	struct nvme_rdma_rsp_opts opts = {};
11074999a985SShuhei Matsumoto 
110820cf9080SShuhei Matsumoto 	if (ret == -ESTALE) {
110920cf9080SShuhei Matsumoto 		return nvme_rdma_stale_conn_retry(rqpair);
111020cf9080SShuhei Matsumoto 	} else if (ret) {
1111bcf08457SShuhei Matsumoto 		SPDK_ERRLOG("RDMA connect error %d\n", ret);
1112bcf08457SShuhei Matsumoto 		return ret;
1113bcf08457SShuhei Matsumoto 	}
1114bcf08457SShuhei Matsumoto 
11159aabfb59SShuhei Matsumoto 	assert(!rqpair->mr_map);
11168a01b4d6SAlexey Marchuk 	rqpair->mr_map = spdk_rdma_utils_create_mem_map(rqpair->rdma_qp->qp->pd, &g_nvme_hooks,
11178ffb2c09SAlexey Marchuk 			 IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
111877aef307SAleksey Marchuk 	if (!rqpair->mr_map) {
111977aef307SAleksey Marchuk 		SPDK_ERRLOG("Unable to register RDMA memory translation map\n");
112077aef307SAleksey Marchuk 		return -1;
112177aef307SAleksey Marchuk 	}
112277aef307SAleksey Marchuk 
11234cef00cbSShuhei Matsumoto 	ret = nvme_rdma_create_reqs(rqpair);
11248e48517fSShuhei Matsumoto 	SPDK_DEBUGLOG(nvme, "rc =%d\n", ret);
11258e48517fSShuhei Matsumoto 	if (ret) {
11264cef00cbSShuhei Matsumoto 		SPDK_ERRLOG("Unable to create rqpair RDMA requests\n");
11278e48517fSShuhei Matsumoto 		return -1;
11288e48517fSShuhei Matsumoto 	}
11294cef00cbSShuhei Matsumoto 	SPDK_DEBUGLOG(nvme, "RDMA requests created\n");
11308e48517fSShuhei Matsumoto 
1131bcd987eaSShuhei Matsumoto 	if (!rqpair->srq) {
11324999a985SShuhei Matsumoto 		opts.num_entries = rqpair->num_entries;
11334999a985SShuhei Matsumoto 		opts.rqpair = rqpair;
1134bcd987eaSShuhei Matsumoto 		opts.srq = NULL;
11354999a985SShuhei Matsumoto 		opts.mr_map = rqpair->mr_map;
11364999a985SShuhei Matsumoto 
11379aabfb59SShuhei Matsumoto 		assert(!rqpair->rsps);
11384999a985SShuhei Matsumoto 		rqpair->rsps = nvme_rdma_create_rsps(&opts);
11394999a985SShuhei Matsumoto 		if (!rqpair->rsps) {
11404cef00cbSShuhei Matsumoto 			SPDK_ERRLOG("Unable to create rqpair RDMA responses\n");
11418e48517fSShuhei Matsumoto 			return -1;
11428e48517fSShuhei Matsumoto 		}
11434cef00cbSShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "RDMA responses created\n");
1144bcf08457SShuhei Matsumoto 
114566022917SShuhei Matsumoto 		ret = nvme_rdma_qpair_submit_recvs(rqpair);
114666022917SShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "rc =%d\n", ret);
114766022917SShuhei Matsumoto 		if (ret) {
114866022917SShuhei Matsumoto 			SPDK_ERRLOG("Unable to submit rqpair RDMA responses\n");
114966022917SShuhei Matsumoto 			return -1;
115066022917SShuhei Matsumoto 		}
115166022917SShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "RDMA responses submitted\n");
1152bcd987eaSShuhei Matsumoto 	}
115366022917SShuhei Matsumoto 
1154bcf08457SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_SEND;
1155246c39a7SZiye Yang 
1156246c39a7SZiye Yang 	return 0;
1157246c39a7SZiye Yang }
1158246c39a7SZiye Yang 
1159246c39a7SZiye Yang static int
1160246c39a7SZiye Yang nvme_rdma_connect(struct nvme_rdma_qpair *rqpair)
1161246c39a7SZiye Yang {
116210aed301SBen Walker 	struct rdma_conn_param				param = {};
116310aed301SBen Walker 	struct spdk_nvmf_rdma_request_private_data	request_data = {};
1164da43f64bSZiye Yang 	struct ibv_device_attr				attr;
1165246c39a7SZiye Yang 	int						ret;
1166d77c0301SBen Walker 	struct spdk_nvme_ctrlr				*ctrlr;
1167246c39a7SZiye Yang 
1168da43f64bSZiye Yang 	ret = ibv_query_device(rqpair->cm_id->verbs, &attr);
1169da43f64bSZiye Yang 	if (ret != 0) {
1170da43f64bSZiye Yang 		SPDK_ERRLOG("Failed to query RDMA device attributes.\n");
1171da43f64bSZiye Yang 		return ret;
1172da43f64bSZiye Yang 	}
1173da43f64bSZiye Yang 
117494494579SAlexey Marchuk 	param.responder_resources = attr.max_qp_rd_atom;
1175246c39a7SZiye Yang 
1176d77c0301SBen Walker 	ctrlr = rqpair->qpair.ctrlr;
1177d77c0301SBen Walker 	if (!ctrlr) {
1178d77c0301SBen Walker 		return -1;
1179d77c0301SBen Walker 	}
1180d77c0301SBen Walker 
118110aed301SBen Walker 	request_data.qid = rqpair->qpair.id;
1182834e3c5aSEvgeniy Kochetov 	request_data.hrqsize = rqpair->num_entries + 1;
1183834e3c5aSEvgeniy Kochetov 	request_data.hsqsize = rqpair->num_entries;
11849f5fb75dSDaniel Verkamp 	request_data.cntlid = ctrlr->cntlid;
1185246c39a7SZiye Yang 
118610aed301SBen Walker 	param.private_data = &request_data;
118710aed301SBen Walker 	param.private_data_len = sizeof(request_data);
1188f1539c28SAlexey Marchuk 	param.retry_count = ctrlr->opts.transport_retry_count;
1189438bae79SBen Walker 	param.rnr_retry_count = 7;
119010aed301SBen Walker 
1191daee62a0SAlexey Marchuk 	/* Fields below are ignored by rdma cm if qpair has been
1192daee62a0SAlexey Marchuk 	 * created using rdma cm API. */
1193daee62a0SAlexey Marchuk 	param.srq = 0;
1194daee62a0SAlexey Marchuk 	param.qp_num = rqpair->rdma_qp->qp->qp_num;
1195daee62a0SAlexey Marchuk 
119610aed301SBen Walker 	ret = rdma_connect(rqpair->cm_id, &param);
1197246c39a7SZiye Yang 	if (ret) {
1198be4f8d05SDaniel Verkamp 		SPDK_ERRLOG("nvme rdma connect error\n");
1199246c39a7SZiye Yang 		return ret;
1200246c39a7SZiye Yang 	}
1201fa97f35fSBen Walker 
1202dc2dd173SJim Harris 	ctrlr->numa.id_valid = 1;
1203dc2dd173SJim Harris 	ctrlr->numa.id = spdk_rdma_cm_id_get_numa_id(rqpair->cm_id);
1204dc2dd173SJim Harris 
12059717b0c3SShuhei Matsumoto 	return nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_ESTABLISHED,
1206cf7f2533SShuhei Matsumoto 					     nvme_rdma_connect_established);
1207e9e3f615SSeth Howell }
1208246c39a7SZiye Yang 
1209246c39a7SZiye Yang static int
121029974dc8SShuhei Matsumoto nvme_rdma_ctrlr_connect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
1211246c39a7SZiye Yang {
12126847a679SDaniel Verkamp 	struct sockaddr_storage dst_addr;
12136847a679SDaniel Verkamp 	struct sockaddr_storage src_addr;
12146847a679SDaniel Verkamp 	bool src_addr_specified;
12156e98729cSKonrad Sztyber 	long int port, src_port = 0;
1216246c39a7SZiye Yang 	int rc;
12175ac814e3SSeth Howell 	struct nvme_rdma_ctrlr *rctrlr;
1218b2225ff5SSeth Howell 	struct nvme_rdma_qpair *rqpair;
1219f8fc1e11SShuhei Matsumoto 	struct nvme_rdma_poll_group *group;
122023fc6682SDaniel Verkamp 	int family;
1221ad7a01bdSSeth Howell 
1222b2225ff5SSeth Howell 	rqpair = nvme_rdma_qpair(qpair);
12235ac814e3SSeth Howell 	rctrlr = nvme_rdma_ctrlr(ctrlr);
12245ac814e3SSeth Howell 	assert(rctrlr != NULL);
122523fc6682SDaniel Verkamp 
122623fc6682SDaniel Verkamp 	switch (ctrlr->trid.adrfam) {
122723fc6682SDaniel Verkamp 	case SPDK_NVMF_ADRFAM_IPV4:
122823fc6682SDaniel Verkamp 		family = AF_INET;
122923fc6682SDaniel Verkamp 		break;
123023fc6682SDaniel Verkamp 	case SPDK_NVMF_ADRFAM_IPV6:
123123fc6682SDaniel Verkamp 		family = AF_INET6;
123223fc6682SDaniel Verkamp 		break;
123323fc6682SDaniel Verkamp 	default:
123423fc6682SDaniel Verkamp 		SPDK_ERRLOG("Unhandled ADRFAM %d\n", ctrlr->trid.adrfam);
123523fc6682SDaniel Verkamp 		return -1;
123623fc6682SDaniel Verkamp 	}
123723fc6682SDaniel Verkamp 
12382172c432STomasz Zawadzki 	SPDK_DEBUGLOG(nvme, "adrfam %d ai_family %d\n", ctrlr->trid.adrfam, family);
123923fc6682SDaniel Verkamp 
12406847a679SDaniel Verkamp 	memset(&dst_addr, 0, sizeof(dst_addr));
1241246c39a7SZiye Yang 
12422172c432STomasz Zawadzki 	SPDK_DEBUGLOG(nvme, "trsvcid is %s\n", ctrlr->trid.trsvcid);
1243bd7c9e07SJim Harris 	rc = nvme_parse_addr(&dst_addr, family, ctrlr->trid.traddr, ctrlr->trid.trsvcid, &port);
1244e2a21655SDaniel Verkamp 	if (rc != 0) {
124501bbc271SJim Harris 		SPDK_ERRLOG("dst_addr nvme_parse_addr() failed\n");
1246be4f8d05SDaniel Verkamp 		return -1;
1247246c39a7SZiye Yang 	}
1248246c39a7SZiye Yang 
12496847a679SDaniel Verkamp 	if (ctrlr->opts.src_addr[0] || ctrlr->opts.src_svcid[0]) {
12506847a679SDaniel Verkamp 		memset(&src_addr, 0, sizeof(src_addr));
12516e98729cSKonrad Sztyber 		rc = nvme_parse_addr(&src_addr, family,
12526e98729cSKonrad Sztyber 				     ctrlr->opts.src_addr[0] ? ctrlr->opts.src_addr : NULL,
12536e98729cSKonrad Sztyber 				     ctrlr->opts.src_svcid[0] ? ctrlr->opts.src_svcid : NULL,
12546e98729cSKonrad Sztyber 				     &src_port);
12556847a679SDaniel Verkamp 		if (rc != 0) {
125601bbc271SJim Harris 			SPDK_ERRLOG("src_addr nvme_parse_addr() failed\n");
12576847a679SDaniel Verkamp 			return -1;
12586847a679SDaniel Verkamp 		}
12596847a679SDaniel Verkamp 		src_addr_specified = true;
12606847a679SDaniel Verkamp 	} else {
12616847a679SDaniel Verkamp 		src_addr_specified = false;
12626847a679SDaniel Verkamp 	}
12636847a679SDaniel Verkamp 
12645ac814e3SSeth Howell 	rc = rdma_create_id(rctrlr->cm_channel, &rqpair->cm_id, rqpair, RDMA_PS_TCP);
1265246c39a7SZiye Yang 	if (rc < 0) {
1266be4f8d05SDaniel Verkamp 		SPDK_ERRLOG("rdma_create_id() failed\n");
1267be4f8d05SDaniel Verkamp 		return -1;
1268246c39a7SZiye Yang 	}
1269246c39a7SZiye Yang 
12706847a679SDaniel Verkamp 	rc = nvme_rdma_resolve_addr(rqpair,
12716847a679SDaniel Verkamp 				    src_addr_specified ? (struct sockaddr *)&src_addr : NULL,
1272e5927c02SShuhei Matsumoto 				    (struct sockaddr *)&dst_addr);
1273246c39a7SZiye Yang 	if (rc < 0) {
127441470242SBen Walker 		SPDK_ERRLOG("nvme_rdma_resolve_addr() failed\n");
1275be4f8d05SDaniel Verkamp 		return -1;
1276246c39a7SZiye Yang 	}
1277246c39a7SZiye Yang 
12789717b0c3SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_INITIALIZING;
12799717b0c3SShuhei Matsumoto 
1280*d58eef2aSAlex Michon 	if (qpair->poll_group != NULL && rqpair->link_connecting.tqe_prev == NULL) {
1281f8fc1e11SShuhei Matsumoto 		group = nvme_rdma_poll_group(qpair->poll_group);
1282f8fc1e11SShuhei Matsumoto 		TAILQ_INSERT_TAIL(&group->connecting_qpairs, rqpair, link_connecting);
1283f8fc1e11SShuhei Matsumoto 	}
1284f8fc1e11SShuhei Matsumoto 
1285246c39a7SZiye Yang 	return 0;
1286246c39a7SZiye Yang }
1287246c39a7SZiye Yang 
12887f82fb65SSeth Howell static int
128920cf9080SShuhei Matsumoto nvme_rdma_stale_conn_reconnect(struct nvme_rdma_qpair *rqpair)
129020cf9080SShuhei Matsumoto {
129120cf9080SShuhei Matsumoto 	struct spdk_nvme_qpair *qpair = &rqpair->qpair;
129220cf9080SShuhei Matsumoto 
129320cf9080SShuhei Matsumoto 	if (spdk_get_ticks() < rqpair->evt_timeout_ticks) {
129420cf9080SShuhei Matsumoto 		return -EAGAIN;
129520cf9080SShuhei Matsumoto 	}
129620cf9080SShuhei Matsumoto 
129720cf9080SShuhei Matsumoto 	return nvme_rdma_ctrlr_connect_qpair(qpair->ctrlr, qpair);
129820cf9080SShuhei Matsumoto }
129920cf9080SShuhei Matsumoto 
130020cf9080SShuhei Matsumoto static int
130129974dc8SShuhei Matsumoto nvme_rdma_ctrlr_connect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr,
130229974dc8SShuhei Matsumoto 				   struct spdk_nvme_qpair *qpair)
13037f82fb65SSeth Howell {
130429974dc8SShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
13057f82fb65SSeth Howell 	int rc;
13067f82fb65SSeth Howell 
130729974dc8SShuhei Matsumoto 	if (rqpair->in_connect_poll) {
130829974dc8SShuhei Matsumoto 		return -EAGAIN;
1309ea0aaf5eSBen Walker 	}
1310ea0aaf5eSBen Walker 
131129974dc8SShuhei Matsumoto 	rqpair->in_connect_poll = true;
131229974dc8SShuhei Matsumoto 
131329974dc8SShuhei Matsumoto 	switch (rqpair->state) {
131429974dc8SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_INVALID:
131529974dc8SShuhei Matsumoto 		rc = -EAGAIN;
131629974dc8SShuhei Matsumoto 		break;
13179717b0c3SShuhei Matsumoto 
13189717b0c3SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_INITIALIZING:
131920cf9080SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_EXITING:
13209717b0c3SShuhei Matsumoto 		if (!nvme_qpair_is_admin_queue(qpair)) {
1321e10b4806SJim Harris 			nvme_ctrlr_lock(ctrlr);
13229717b0c3SShuhei Matsumoto 		}
13239717b0c3SShuhei Matsumoto 
13249717b0c3SShuhei Matsumoto 		rc = nvme_rdma_process_event_poll(rqpair);
13259717b0c3SShuhei Matsumoto 
13269717b0c3SShuhei Matsumoto 		if (!nvme_qpair_is_admin_queue(qpair)) {
1327e10b4806SJim Harris 			nvme_ctrlr_unlock(ctrlr);
13289717b0c3SShuhei Matsumoto 		}
13299717b0c3SShuhei Matsumoto 
13309717b0c3SShuhei Matsumoto 		if (rc == 0) {
13319717b0c3SShuhei Matsumoto 			rc = -EAGAIN;
13329717b0c3SShuhei Matsumoto 		}
13339717b0c3SShuhei Matsumoto 		rqpair->in_connect_poll = false;
13349717b0c3SShuhei Matsumoto 
13359717b0c3SShuhei Matsumoto 		return rc;
13369717b0c3SShuhei Matsumoto 
133720cf9080SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_STALE_CONN:
133820cf9080SShuhei Matsumoto 		rc = nvme_rdma_stale_conn_reconnect(rqpair);
133920cf9080SShuhei Matsumoto 		if (rc == 0) {
134020cf9080SShuhei Matsumoto 			rc = -EAGAIN;
134120cf9080SShuhei Matsumoto 		}
134220cf9080SShuhei Matsumoto 		break;
134329974dc8SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_SEND:
134429974dc8SShuhei Matsumoto 		rc = nvme_fabric_qpair_connect_async(qpair, rqpair->num_entries + 1);
134529974dc8SShuhei Matsumoto 		if (rc == 0) {
134629974dc8SShuhei Matsumoto 			rqpair->state = NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_POLL;
134729974dc8SShuhei Matsumoto 			rc = -EAGAIN;
134829974dc8SShuhei Matsumoto 		} else {
134929974dc8SShuhei Matsumoto 			SPDK_ERRLOG("Failed to send an NVMe-oF Fabric CONNECT command\n");
135029974dc8SShuhei Matsumoto 		}
135129974dc8SShuhei Matsumoto 		break;
135229974dc8SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_FABRIC_CONNECT_POLL:
135329974dc8SShuhei Matsumoto 		rc = nvme_fabric_qpair_connect_poll(qpair);
135429974dc8SShuhei Matsumoto 		if (rc == 0) {
1355ffd098cfSKonrad Sztyber 			if (nvme_fabric_qpair_auth_required(qpair)) {
1356ffd098cfSKonrad Sztyber 				rc = nvme_fabric_qpair_authenticate_async(qpair);
1357ffd098cfSKonrad Sztyber 				if (rc == 0) {
1358ffd098cfSKonrad Sztyber 					rqpair->state = NVME_RDMA_QPAIR_STATE_AUTHENTICATING;
1359ffd098cfSKonrad Sztyber 					rc = -EAGAIN;
1360ffd098cfSKonrad Sztyber 				}
1361ffd098cfSKonrad Sztyber 			} else {
136229974dc8SShuhei Matsumoto 				rqpair->state = NVME_RDMA_QPAIR_STATE_RUNNING;
136329974dc8SShuhei Matsumoto 				nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
1364ffd098cfSKonrad Sztyber 			}
136529974dc8SShuhei Matsumoto 		} else if (rc != -EAGAIN) {
136629974dc8SShuhei Matsumoto 			SPDK_ERRLOG("Failed to poll NVMe-oF Fabric CONNECT command\n");
136729974dc8SShuhei Matsumoto 		}
136829974dc8SShuhei Matsumoto 		break;
1369ffd098cfSKonrad Sztyber 	case NVME_RDMA_QPAIR_STATE_AUTHENTICATING:
1370ffd098cfSKonrad Sztyber 		rc = nvme_fabric_qpair_authenticate_poll(qpair);
1371ffd098cfSKonrad Sztyber 		if (rc == 0) {
1372ffd098cfSKonrad Sztyber 			rqpair->state = NVME_RDMA_QPAIR_STATE_RUNNING;
1373ffd098cfSKonrad Sztyber 			nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTED);
1374ffd098cfSKonrad Sztyber 		}
1375ffd098cfSKonrad Sztyber 		break;
137629974dc8SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_RUNNING:
137729974dc8SShuhei Matsumoto 		rc = 0;
137829974dc8SShuhei Matsumoto 		break;
137929974dc8SShuhei Matsumoto 	default:
138029974dc8SShuhei Matsumoto 		assert(false);
138129974dc8SShuhei Matsumoto 		rc = -EINVAL;
138229974dc8SShuhei Matsumoto 		break;
138329974dc8SShuhei Matsumoto 	}
138429974dc8SShuhei Matsumoto 
138529974dc8SShuhei Matsumoto 	rqpair->in_connect_poll = false;
138629974dc8SShuhei Matsumoto 
1387203ed4f6SSeth Howell 	return rc;
13887f82fb65SSeth Howell }
13897f82fb65SSeth Howell 
1390110335f1SAlexey Marchuk static inline int
1391110335f1SAlexey Marchuk nvme_rdma_get_memory_translation(struct nvme_request *req, struct nvme_rdma_qpair *rqpair,
1392110335f1SAlexey Marchuk 				 struct nvme_rdma_memory_translation_ctx *_ctx)
1393110335f1SAlexey Marchuk {
1394110335f1SAlexey Marchuk 	struct spdk_memory_domain_translation_ctx ctx;
13953e937f07SJaylyn Ren 	struct spdk_memory_domain_translation_result dma_translation = {.iov_count = 0};
13968a01b4d6SAlexey Marchuk 	struct spdk_rdma_utils_memory_translation rdma_translation;
1397110335f1SAlexey Marchuk 	int rc;
1398110335f1SAlexey Marchuk 
1399110335f1SAlexey Marchuk 	assert(req);
1400110335f1SAlexey Marchuk 	assert(rqpair);
1401110335f1SAlexey Marchuk 	assert(_ctx);
1402110335f1SAlexey Marchuk 
1403110335f1SAlexey Marchuk 	if (req->payload.opts && req->payload.opts->memory_domain) {
1404110335f1SAlexey Marchuk 		ctx.size = sizeof(struct spdk_memory_domain_translation_ctx);
1405110335f1SAlexey Marchuk 		ctx.rdma.ibv_qp = rqpair->rdma_qp->qp;
1406110335f1SAlexey Marchuk 		dma_translation.size = sizeof(struct spdk_memory_domain_translation_result);
1407110335f1SAlexey Marchuk 
1408110335f1SAlexey Marchuk 		rc = spdk_memory_domain_translate_data(req->payload.opts->memory_domain,
1409110335f1SAlexey Marchuk 						       req->payload.opts->memory_domain_ctx,
14101794c395SAlexey Marchuk 						       rqpair->rdma_qp->domain, &ctx, _ctx->addr,
1411110335f1SAlexey Marchuk 						       _ctx->length, &dma_translation);
14122696886cSAlexey Marchuk 		if (spdk_unlikely(rc) || dma_translation.iov_count != 1) {
14132696886cSAlexey Marchuk 			SPDK_ERRLOG("DMA memory translation failed, rc %d, iov count %u\n", rc, dma_translation.iov_count);
1414110335f1SAlexey Marchuk 			return rc;
1415110335f1SAlexey Marchuk 		}
1416110335f1SAlexey Marchuk 
1417110335f1SAlexey Marchuk 		_ctx->lkey = dma_translation.rdma.lkey;
1418110335f1SAlexey Marchuk 		_ctx->rkey = dma_translation.rdma.rkey;
14192696886cSAlexey Marchuk 		_ctx->addr = dma_translation.iov.iov_base;
14202696886cSAlexey Marchuk 		_ctx->length = dma_translation.iov.iov_len;
1421110335f1SAlexey Marchuk 	} else {
14228a01b4d6SAlexey Marchuk 		rc = spdk_rdma_utils_get_translation(rqpair->mr_map, _ctx->addr, _ctx->length, &rdma_translation);
1423110335f1SAlexey Marchuk 		if (spdk_unlikely(rc)) {
1424110335f1SAlexey Marchuk 			SPDK_ERRLOG("RDMA memory translation failed, rc %d\n", rc);
1425110335f1SAlexey Marchuk 			return rc;
1426110335f1SAlexey Marchuk 		}
14278a01b4d6SAlexey Marchuk 		if (rdma_translation.translation_type == SPDK_RDMA_UTILS_TRANSLATION_MR) {
1428110335f1SAlexey Marchuk 			_ctx->lkey = rdma_translation.mr_or_key.mr->lkey;
1429110335f1SAlexey Marchuk 			_ctx->rkey = rdma_translation.mr_or_key.mr->rkey;
1430110335f1SAlexey Marchuk 		} else {
1431110335f1SAlexey Marchuk 			_ctx->lkey = _ctx->rkey = (uint32_t)rdma_translation.mr_or_key.key;
1432110335f1SAlexey Marchuk 		}
1433110335f1SAlexey Marchuk 	}
1434110335f1SAlexey Marchuk 
1435110335f1SAlexey Marchuk 	return 0;
1436110335f1SAlexey Marchuk }
1437110335f1SAlexey Marchuk 
1438110335f1SAlexey Marchuk 
143983e55653SDaniel Verkamp /*
144083e55653SDaniel Verkamp  * Build SGL describing empty payload.
144190f13aa6SZiye Yang  */
144290f13aa6SZiye Yang static int
1443935cdbe4SSeth Howell nvme_rdma_build_null_request(struct spdk_nvme_rdma_req *rdma_req)
144490f13aa6SZiye Yang {
1445935cdbe4SSeth Howell 	struct nvme_request *req = rdma_req->req;
144690f13aa6SZiye Yang 
1447a02c0062SDaniel Verkamp 	req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
144890f13aa6SZiye Yang 
1449b9913608SBen Walker 	/* The first element of this SGL is pointing at an
1450b9913608SBen Walker 	 * spdk_nvmf_cmd object. For this particular command,
1451b9913608SBen Walker 	 * we only need the first 64 bytes corresponding to
1452b9913608SBen Walker 	 * the NVMe command. */
1453935cdbe4SSeth Howell 	rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
1454b9913608SBen Walker 
14553fe0db6cSBen Walker 	/* The RDMA SGL needs one element describing the NVMe command. */
14563fe0db6cSBen Walker 	rdma_req->send_wr.num_sge = 1;
14573fe0db6cSBen Walker 
1458e3cd058cSBen Walker 	req->cmd.dptr.sgl1.keyed.type = SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK;
1459e3cd058cSBen Walker 	req->cmd.dptr.sgl1.keyed.subtype = SPDK_NVME_SGL_SUBTYPE_ADDRESS;
1460e3cd058cSBen Walker 	req->cmd.dptr.sgl1.keyed.length = 0;
1461e3cd058cSBen Walker 	req->cmd.dptr.sgl1.keyed.key = 0;
1462e3cd058cSBen Walker 	req->cmd.dptr.sgl1.address = 0;
146390f13aa6SZiye Yang 
146483e55653SDaniel Verkamp 	return 0;
146583e55653SDaniel Verkamp }
146683e55653SDaniel Verkamp 
146751bde662SAlexey Marchuk static inline void
146851bde662SAlexey Marchuk nvme_rdma_configure_contig_inline_request(struct spdk_nvme_rdma_req *rdma_req,
146951bde662SAlexey Marchuk 		struct nvme_request *req, struct nvme_rdma_memory_translation_ctx *ctx)
147051bde662SAlexey Marchuk {
147151bde662SAlexey Marchuk 	rdma_req->send_sgl[1].lkey = ctx->lkey;
147251bde662SAlexey Marchuk 
147351bde662SAlexey Marchuk 	/* The first element of this SGL is pointing at an
147451bde662SAlexey Marchuk 	 * spdk_nvmf_cmd object. For this particular command,
147551bde662SAlexey Marchuk 	 * we only need the first 64 bytes corresponding to
147651bde662SAlexey Marchuk 	 * the NVMe command. */
147751bde662SAlexey Marchuk 	rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
147851bde662SAlexey Marchuk 
147951bde662SAlexey Marchuk 	rdma_req->send_sgl[1].addr = (uint64_t)ctx->addr;
148051bde662SAlexey Marchuk 	rdma_req->send_sgl[1].length = (uint32_t)ctx->length;
148151bde662SAlexey Marchuk 
148251bde662SAlexey Marchuk 	/* The RDMA SGL contains two elements. The first describes
148351bde662SAlexey Marchuk 	 * the NVMe command and the second describes the data
148451bde662SAlexey Marchuk 	 * payload. */
148551bde662SAlexey Marchuk 	rdma_req->send_wr.num_sge = 2;
148651bde662SAlexey Marchuk 
148751bde662SAlexey Marchuk 	req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
148851bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
148951bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
149051bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.unkeyed.length = (uint32_t)ctx->length;
149151bde662SAlexey Marchuk 	/* Inline only supported for icdoff == 0 currently.  This function will
149251bde662SAlexey Marchuk 	 * not get called for controllers with other values. */
149351bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.address = (uint64_t)0;
149451bde662SAlexey Marchuk }
149551bde662SAlexey Marchuk 
149683e55653SDaniel Verkamp /*
149794f87a2dSPotnuri Bharat Teja  * Build inline SGL describing contiguous payload buffer.
149894f87a2dSPotnuri Bharat Teja  */
1499f0e4b91fSAlexey Marchuk static inline int
150094f87a2dSPotnuri Bharat Teja nvme_rdma_build_contig_inline_request(struct nvme_rdma_qpair *rqpair,
150194f87a2dSPotnuri Bharat Teja 				      struct spdk_nvme_rdma_req *rdma_req)
150294f87a2dSPotnuri Bharat Teja {
150394f87a2dSPotnuri Bharat Teja 	struct nvme_request *req = rdma_req->req;
1504110335f1SAlexey Marchuk 	struct nvme_rdma_memory_translation_ctx ctx = {
1505075d422fSKonrad Sztyber 		.addr = (uint8_t *)req->payload.contig_or_cb_arg + req->payload_offset,
1506110335f1SAlexey Marchuk 		.length = req->payload_size
1507110335f1SAlexey Marchuk 	};
1508b6efb964SAlexey Marchuk 	int rc;
150994f87a2dSPotnuri Bharat Teja 
1510110335f1SAlexey Marchuk 	assert(ctx.length != 0);
151194f87a2dSPotnuri Bharat Teja 	assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
151294f87a2dSPotnuri Bharat Teja 
1513110335f1SAlexey Marchuk 	rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
1514b6efb964SAlexey Marchuk 	if (spdk_unlikely(rc)) {
151514425544SAlexey Marchuk 		return -1;
1516a451c838SSeth Howell 	}
1517655d54f3SDarek Stojaczyk 
151851bde662SAlexey Marchuk 	nvme_rdma_configure_contig_inline_request(rdma_req, req, &ctx);
151951bde662SAlexey Marchuk 
152051bde662SAlexey Marchuk 	return 0;
152151bde662SAlexey Marchuk }
152251bde662SAlexey Marchuk 
152351bde662SAlexey Marchuk static inline void
152451bde662SAlexey Marchuk nvme_rdma_configure_contig_request(struct spdk_nvme_rdma_req *rdma_req, struct nvme_request *req,
152551bde662SAlexey Marchuk 				   struct nvme_rdma_memory_translation_ctx *ctx)
152651bde662SAlexey Marchuk {
152751bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.keyed.key = ctx->rkey;
152894f87a2dSPotnuri Bharat Teja 
1529b9913608SBen Walker 	/* The first element of this SGL is pointing at an
1530b9913608SBen Walker 	 * spdk_nvmf_cmd object. For this particular command,
1531b9913608SBen Walker 	 * we only need the first 64 bytes corresponding to
1532b9913608SBen Walker 	 * the NVMe command. */
1533b9913608SBen Walker 	rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
1534b9913608SBen Walker 
153551bde662SAlexey Marchuk 	/* The RDMA SGL needs one element describing the NVMe command. */
153651bde662SAlexey Marchuk 	rdma_req->send_wr.num_sge = 1;
1537b9913608SBen Walker 
153894f87a2dSPotnuri Bharat Teja 	req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
153951bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.keyed.type = SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK;
154051bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.keyed.subtype = SPDK_NVME_SGL_SUBTYPE_ADDRESS;
154151bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.keyed.length = (uint32_t)ctx->length;
154251bde662SAlexey Marchuk 	req->cmd.dptr.sgl1.address = (uint64_t)ctx->addr;
154394f87a2dSPotnuri Bharat Teja }
154494f87a2dSPotnuri Bharat Teja 
154594f87a2dSPotnuri Bharat Teja /*
154683e55653SDaniel Verkamp  * Build SGL describing contiguous payload buffer.
154783e55653SDaniel Verkamp  */
1548f0e4b91fSAlexey Marchuk static inline int
154994f87a2dSPotnuri Bharat Teja nvme_rdma_build_contig_request(struct nvme_rdma_qpair *rqpair,
155094f87a2dSPotnuri Bharat Teja 			       struct spdk_nvme_rdma_req *rdma_req)
155183e55653SDaniel Verkamp {
155294f87a2dSPotnuri Bharat Teja 	struct nvme_request *req = rdma_req->req;
1553110335f1SAlexey Marchuk 	struct nvme_rdma_memory_translation_ctx ctx = {
1554075d422fSKonrad Sztyber 		.addr = (uint8_t *)req->payload.contig_or_cb_arg + req->payload_offset,
1555110335f1SAlexey Marchuk 		.length = req->payload_size
1556110335f1SAlexey Marchuk 	};
1557b6efb964SAlexey Marchuk 	int rc;
155883e55653SDaniel Verkamp 
155983e55653SDaniel Verkamp 	assert(req->payload_size != 0);
15605c2ccd06SDaniel Verkamp 	assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_CONTIG);
156183e55653SDaniel Verkamp 
1562e7625088SAlexey Marchuk 	if (spdk_unlikely(req->payload_size > NVME_RDMA_MAX_KEYED_SGL_LENGTH)) {
1563e7625088SAlexey Marchuk 		SPDK_ERRLOG("SGL length %u exceeds max keyed SGL block size %u\n",
1564e7625088SAlexey Marchuk 			    req->payload_size, NVME_RDMA_MAX_KEYED_SGL_LENGTH);
1565e7625088SAlexey Marchuk 		return -1;
1566e7625088SAlexey Marchuk 	}
1567e7625088SAlexey Marchuk 
1568110335f1SAlexey Marchuk 	rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
1569b6efb964SAlexey Marchuk 	if (spdk_unlikely(rc)) {
15709fb69476Szkhatami88 		return -1;
15719fb69476Szkhatami88 	}
15729fb69476Szkhatami88 
157351bde662SAlexey Marchuk 	nvme_rdma_configure_contig_request(rdma_req, req, &ctx);
157483e55653SDaniel Verkamp 
157583e55653SDaniel Verkamp 	return 0;
157690f13aa6SZiye Yang }
157790f13aa6SZiye Yang 
157883e55653SDaniel Verkamp /*
157983e55653SDaniel Verkamp  * Build SGL describing scattered payload buffer.
158083e55653SDaniel Verkamp  */
1581f0e4b91fSAlexey Marchuk static inline int
158294f87a2dSPotnuri Bharat Teja nvme_rdma_build_sgl_request(struct nvme_rdma_qpair *rqpair,
158394f87a2dSPotnuri Bharat Teja 			    struct spdk_nvme_rdma_req *rdma_req)
158483e55653SDaniel Verkamp {
158594f87a2dSPotnuri Bharat Teja 	struct nvme_request *req = rdma_req->req;
1586935cdbe4SSeth Howell 	struct spdk_nvmf_cmd *cmd = &rqpair->cmds[rdma_req->id];
1587110335f1SAlexey Marchuk 	struct nvme_rdma_memory_translation_ctx ctx;
158814425544SAlexey Marchuk 	uint32_t remaining_size;
15895d5181dbSwuzhouhui 	uint32_t sge_length;
1590935cdbe4SSeth Howell 	int rc, max_num_sgl, num_sgl_desc;
159183e55653SDaniel Verkamp 
159283e55653SDaniel Verkamp 	assert(req->payload_size != 0);
15935c2ccd06SDaniel Verkamp 	assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
15947dff719fSDaniel Verkamp 	assert(req->payload.reset_sgl_fn != NULL);
15957dff719fSDaniel Verkamp 	assert(req->payload.next_sge_fn != NULL);
15967dff719fSDaniel Verkamp 	req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
159783e55653SDaniel Verkamp 
1598935cdbe4SSeth Howell 	max_num_sgl = req->qpair->ctrlr->max_sges;
1599935cdbe4SSeth Howell 
1600935cdbe4SSeth Howell 	remaining_size = req->payload_size;
1601935cdbe4SSeth Howell 	num_sgl_desc = 0;
1602935cdbe4SSeth Howell 	do {
1603110335f1SAlexey Marchuk 		rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &ctx.addr, &sge_length);
1604f0e4b91fSAlexey Marchuk 		if (spdk_unlikely(rc)) {
160583e55653SDaniel Verkamp 			return -1;
160683e55653SDaniel Verkamp 		}
160783e55653SDaniel Verkamp 
1608935cdbe4SSeth Howell 		sge_length = spdk_min(remaining_size, sge_length);
1609935cdbe4SSeth Howell 
1610e7625088SAlexey Marchuk 		if (spdk_unlikely(sge_length > NVME_RDMA_MAX_KEYED_SGL_LENGTH)) {
1611e7625088SAlexey Marchuk 			SPDK_ERRLOG("SGL length %u exceeds max keyed SGL block size %u\n",
1612e7625088SAlexey Marchuk 				    sge_length, NVME_RDMA_MAX_KEYED_SGL_LENGTH);
1613e7625088SAlexey Marchuk 			return -1;
1614e7625088SAlexey Marchuk 		}
1615110335f1SAlexey Marchuk 		ctx.length = sge_length;
1616110335f1SAlexey Marchuk 		rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
1617b6efb964SAlexey Marchuk 		if (spdk_unlikely(rc)) {
161883e55653SDaniel Verkamp 			return -1;
161983e55653SDaniel Verkamp 		}
162083e55653SDaniel Verkamp 
1621110335f1SAlexey Marchuk 		cmd->sgl[num_sgl_desc].keyed.key = ctx.rkey;
1622935cdbe4SSeth Howell 		cmd->sgl[num_sgl_desc].keyed.type = SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK;
1623935cdbe4SSeth Howell 		cmd->sgl[num_sgl_desc].keyed.subtype = SPDK_NVME_SGL_SUBTYPE_ADDRESS;
1624110335f1SAlexey Marchuk 		cmd->sgl[num_sgl_desc].keyed.length = (uint32_t)ctx.length;
1625110335f1SAlexey Marchuk 		cmd->sgl[num_sgl_desc].address = (uint64_t)ctx.addr;
1626935cdbe4SSeth Howell 
1627110335f1SAlexey Marchuk 		remaining_size -= ctx.length;
1628935cdbe4SSeth Howell 		num_sgl_desc++;
1629935cdbe4SSeth Howell 	} while (remaining_size > 0 && num_sgl_desc < max_num_sgl);
1630935cdbe4SSeth Howell 
1631935cdbe4SSeth Howell 
1632935cdbe4SSeth Howell 	/* Should be impossible if we did our sgl checks properly up the stack, but do a sanity check here. */
1633f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(remaining_size > 0)) {
1634935cdbe4SSeth Howell 		return -1;
1635935cdbe4SSeth Howell 	}
1636935cdbe4SSeth Howell 
1637a02c0062SDaniel Verkamp 	req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
16383fe0db6cSBen Walker 
16393fe0db6cSBen Walker 	/* The RDMA SGL needs one element describing some portion
16403fe0db6cSBen Walker 	 * of the spdk_nvmf_cmd structure. */
1641935cdbe4SSeth Howell 	rdma_req->send_wr.num_sge = 1;
1642935cdbe4SSeth Howell 
1643935cdbe4SSeth Howell 	/*
1644935cdbe4SSeth Howell 	 * If only one SGL descriptor is required, it can be embedded directly in the command
1645935cdbe4SSeth Howell 	 * as a data block descriptor.
1646935cdbe4SSeth Howell 	 */
1647935cdbe4SSeth Howell 	if (num_sgl_desc == 1) {
1648b9913608SBen Walker 		/* The first element of this SGL is pointing at an
1649b9913608SBen Walker 		 * spdk_nvmf_cmd object. For this particular command,
1650b9913608SBen Walker 		 * we only need the first 64 bytes corresponding to
1651b9913608SBen Walker 		 * the NVMe command. */
1652935cdbe4SSeth Howell 		rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
1653b9913608SBen Walker 
16549fb69476Szkhatami88 		req->cmd.dptr.sgl1.keyed.type = cmd->sgl[0].keyed.type;
16559fb69476Szkhatami88 		req->cmd.dptr.sgl1.keyed.subtype = cmd->sgl[0].keyed.subtype;
16569fb69476Szkhatami88 		req->cmd.dptr.sgl1.keyed.length = cmd->sgl[0].keyed.length;
16579fb69476Szkhatami88 		req->cmd.dptr.sgl1.keyed.key = cmd->sgl[0].keyed.key;
16589fb69476Szkhatami88 		req->cmd.dptr.sgl1.address = cmd->sgl[0].address;
1659935cdbe4SSeth Howell 	} else {
1660935cdbe4SSeth Howell 		/*
1661935cdbe4SSeth Howell 		 * Otherwise, The SGL descriptor embedded in the command must point to the list of
1662935cdbe4SSeth Howell 		 * SGL descriptors used to describe the operation. In that case it is a last segment descriptor.
1663935cdbe4SSeth Howell 		 */
1664eb78b90cSAlexey Marchuk 		uint32_t descriptors_size = sizeof(struct spdk_nvme_sgl_descriptor) * num_sgl_desc;
1665eb78b90cSAlexey Marchuk 
1666eb78b90cSAlexey Marchuk 		if (spdk_unlikely(descriptors_size > rqpair->qpair.ctrlr->ioccsz_bytes)) {
1667eb78b90cSAlexey Marchuk 			SPDK_ERRLOG("Size of SGL descriptors (%u) exceeds ICD (%u)\n",
1668eb78b90cSAlexey Marchuk 				    descriptors_size, rqpair->qpair.ctrlr->ioccsz_bytes);
1669eb78b90cSAlexey Marchuk 			return -1;
1670eb78b90cSAlexey Marchuk 		}
1671eb78b90cSAlexey Marchuk 		rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd) + descriptors_size;
1672b9913608SBen Walker 
1673935cdbe4SSeth Howell 		req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
1674935cdbe4SSeth Howell 		req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
1675eb78b90cSAlexey Marchuk 		req->cmd.dptr.sgl1.unkeyed.length = descriptors_size;
1676935cdbe4SSeth Howell 		req->cmd.dptr.sgl1.address = (uint64_t)0;
1677935cdbe4SSeth Howell 	}
167883e55653SDaniel Verkamp 
167990f13aa6SZiye Yang 	return 0;
168090f13aa6SZiye Yang }
168190f13aa6SZiye Yang 
168294f87a2dSPotnuri Bharat Teja /*
168394f87a2dSPotnuri Bharat Teja  * Build inline SGL describing sgl payload buffer.
168494f87a2dSPotnuri Bharat Teja  */
1685f0e4b91fSAlexey Marchuk static inline int
168694f87a2dSPotnuri Bharat Teja nvme_rdma_build_sgl_inline_request(struct nvme_rdma_qpair *rqpair,
168794f87a2dSPotnuri Bharat Teja 				   struct spdk_nvme_rdma_req *rdma_req)
168894f87a2dSPotnuri Bharat Teja {
168994f87a2dSPotnuri Bharat Teja 	struct nvme_request *req = rdma_req->req;
1690110335f1SAlexey Marchuk 	struct nvme_rdma_memory_translation_ctx ctx;
169194f87a2dSPotnuri Bharat Teja 	uint32_t length;
169214425544SAlexey Marchuk 	int rc;
169394f87a2dSPotnuri Bharat Teja 
169494f87a2dSPotnuri Bharat Teja 	assert(req->payload_size != 0);
169594f87a2dSPotnuri Bharat Teja 	assert(nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL);
169694f87a2dSPotnuri Bharat Teja 	assert(req->payload.reset_sgl_fn != NULL);
169794f87a2dSPotnuri Bharat Teja 	assert(req->payload.next_sge_fn != NULL);
169894f87a2dSPotnuri Bharat Teja 	req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
169994f87a2dSPotnuri Bharat Teja 
1700110335f1SAlexey Marchuk 	rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &ctx.addr, &length);
1701f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(rc)) {
170294f87a2dSPotnuri Bharat Teja 		return -1;
170394f87a2dSPotnuri Bharat Teja 	}
170494f87a2dSPotnuri Bharat Teja 
1705eb6006c2SSeth Howell 	if (length < req->payload_size) {
17062172c432STomasz Zawadzki 		SPDK_DEBUGLOG(nvme, "Inline SGL request split so sending separately.\n");
1707eb6006c2SSeth Howell 		return nvme_rdma_build_sgl_request(rqpair, rdma_req);
1708eb6006c2SSeth Howell 	}
1709eb6006c2SSeth Howell 
1710eb6006c2SSeth Howell 	if (length > req->payload_size) {
1711eb6006c2SSeth Howell 		length = req->payload_size;
17123f2553a3SSeth Howell 	}
171394f87a2dSPotnuri Bharat Teja 
1714110335f1SAlexey Marchuk 	ctx.length = length;
1715110335f1SAlexey Marchuk 	rc = nvme_rdma_get_memory_translation(req, rqpair, &ctx);
1716b6efb964SAlexey Marchuk 	if (spdk_unlikely(rc)) {
171794f87a2dSPotnuri Bharat Teja 		return -1;
171894f87a2dSPotnuri Bharat Teja 	}
171994f87a2dSPotnuri Bharat Teja 
1720110335f1SAlexey Marchuk 	rdma_req->send_sgl[1].addr = (uint64_t)ctx.addr;
1721110335f1SAlexey Marchuk 	rdma_req->send_sgl[1].length = (uint32_t)ctx.length;
1722110335f1SAlexey Marchuk 	rdma_req->send_sgl[1].lkey = ctx.lkey;
17233018bf90SSeth Howell 
1724eb6006c2SSeth Howell 	rdma_req->send_wr.num_sge = 2;
17253018bf90SSeth Howell 
1726b9913608SBen Walker 	/* The first element of this SGL is pointing at an
1727b9913608SBen Walker 	 * spdk_nvmf_cmd object. For this particular command,
1728b9913608SBen Walker 	 * we only need the first 64 bytes corresponding to
1729b9913608SBen Walker 	 * the NVMe command. */
1730b9913608SBen Walker 	rdma_req->send_sgl[0].length = sizeof(struct spdk_nvme_cmd);
1731b9913608SBen Walker 
173294f87a2dSPotnuri Bharat Teja 	req->cmd.psdt = SPDK_NVME_PSDT_SGL_MPTR_CONTIG;
173394f87a2dSPotnuri Bharat Teja 	req->cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
173494f87a2dSPotnuri Bharat Teja 	req->cmd.dptr.sgl1.unkeyed.subtype = SPDK_NVME_SGL_SUBTYPE_OFFSET;
1735110335f1SAlexey Marchuk 	req->cmd.dptr.sgl1.unkeyed.length = (uint32_t)ctx.length;
17366c391ec2SJim Harris 	/* Inline only supported for icdoff == 0 currently.  This function will
17376c391ec2SJim Harris 	 * not get called for controllers with other values. */
17386c391ec2SJim Harris 	req->cmd.dptr.sgl1.address = (uint64_t)0;
173994f87a2dSPotnuri Bharat Teja 
174094f87a2dSPotnuri Bharat Teja 	return 0;
174194f87a2dSPotnuri Bharat Teja }
174294f87a2dSPotnuri Bharat Teja 
1743f0e4b91fSAlexey Marchuk static inline int
17442c140f58SAlexey Marchuk nvme_rdma_accel_append_copy(struct spdk_nvme_poll_group *pg, void **seq,
17452c140f58SAlexey Marchuk 			    struct spdk_memory_domain *rdma_domain, struct spdk_nvme_rdma_req *rdma_req,
17462c140f58SAlexey Marchuk 			    struct iovec *iovs, uint32_t iovcnt,
17472c140f58SAlexey Marchuk 			    struct spdk_memory_domain *src_domain, void *src_domain_ctx)
17482c140f58SAlexey Marchuk {
17492c140f58SAlexey Marchuk 	return pg->accel_fn_table.append_copy(pg->ctx, seq, iovs, iovcnt, rdma_domain, rdma_req, iovs,
17502c140f58SAlexey Marchuk 					      iovcnt, src_domain, src_domain_ctx, NULL, NULL);
17512c140f58SAlexey Marchuk }
17522c140f58SAlexey Marchuk 
17532c140f58SAlexey Marchuk static inline void
17542c140f58SAlexey Marchuk nvme_rdma_accel_reverse(struct spdk_nvme_poll_group *pg, void *seq)
17552c140f58SAlexey Marchuk {
17562c140f58SAlexey Marchuk 	pg->accel_fn_table.reverse_sequence(seq);
17572c140f58SAlexey Marchuk }
17582c140f58SAlexey Marchuk 
17592c140f58SAlexey Marchuk static inline void
17602c140f58SAlexey Marchuk nvme_rdma_accel_finish(struct spdk_nvme_poll_group *pg, void *seq,
17612c140f58SAlexey Marchuk 		       spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
17622c140f58SAlexey Marchuk {
17632c140f58SAlexey Marchuk 	pg->accel_fn_table.finish_sequence(seq, cb_fn, cb_arg);
17642c140f58SAlexey Marchuk }
17652c140f58SAlexey Marchuk 
17662c140f58SAlexey Marchuk static inline void
17672c140f58SAlexey Marchuk nvme_rdma_accel_completion_cb(void *cb_arg, int status)
17682c140f58SAlexey Marchuk {
17692c140f58SAlexey Marchuk 	struct spdk_nvme_rdma_req *rdma_req = cb_arg;
17702c140f58SAlexey Marchuk 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(rdma_req->req->qpair);
17712c140f58SAlexey Marchuk 	struct spdk_nvme_cpl cpl;
17722c140f58SAlexey Marchuk 	enum spdk_nvme_generic_command_status_code sc;
17732c140f58SAlexey Marchuk 	uint16_t dnr = 0;
17742c140f58SAlexey Marchuk 
17752c140f58SAlexey Marchuk 	rdma_req->in_progress_accel = 0;
17762c140f58SAlexey Marchuk 	rdma_req->req->accel_sequence = NULL;
17772c140f58SAlexey Marchuk 	SPDK_DEBUGLOG(nvme, "rdma_req %p qpair %p, accel completion rc %d\n", rdma_req, rqpair, status);
17782c140f58SAlexey Marchuk 
17792c140f58SAlexey Marchuk 	/* nvme_rdma driver may fail data transfer on WC_FLUSH error completion which is expected.
17802c140f58SAlexey Marchuk 	 * To prevent false errors from accel, first check if qpair is in the process of disconnect */
17812c140f58SAlexey Marchuk 	if (spdk_unlikely(!spdk_nvme_qpair_is_connected(&rqpair->qpair))) {
1782cec5ba28SAlexey Marchuk 		struct spdk_nvmf_fabric_connect_cmd *cmd = (struct spdk_nvmf_fabric_connect_cmd *)
1783cec5ba28SAlexey Marchuk 				&rdma_req->req->cmd;
1784cec5ba28SAlexey Marchuk 
1785cec5ba28SAlexey Marchuk 		if (cmd->opcode != SPDK_NVME_OPC_FABRIC && cmd->fctype != SPDK_NVMF_FABRIC_COMMAND_CONNECT) {
17862c140f58SAlexey Marchuk 			SPDK_DEBUGLOG(nvme, "qpair %p, req %p accel cpl in disconnecting, outstanding %u\n",
17872c140f58SAlexey Marchuk 				      rqpair, rdma_req, rqpair->qpair.num_outstanding_reqs);
17882c140f58SAlexey Marchuk 			sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
17892c140f58SAlexey Marchuk 			goto fail_req;
17902c140f58SAlexey Marchuk 		}
1791cec5ba28SAlexey Marchuk 	}
17922c140f58SAlexey Marchuk 	if (spdk_unlikely(status)) {
17932c140f58SAlexey Marchuk 		SPDK_ERRLOG("qpair %p, req %p, accel sequence status %d\n", rdma_req->req->qpair, rdma_req, status);
17942c140f58SAlexey Marchuk 		sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
17952c140f58SAlexey Marchuk 		/* Something wrong happened, let the upper layer know that retry is no desired */
17962c140f58SAlexey Marchuk 		dnr = 1;
17972c140f58SAlexey Marchuk 		goto fail_req;
17982c140f58SAlexey Marchuk 	}
17992c140f58SAlexey Marchuk 
18002c140f58SAlexey Marchuk 	nvme_rdma_req_complete(rdma_req, &rdma_req->rdma_rsp->cpl, true);
18012c140f58SAlexey Marchuk 	return;
18022c140f58SAlexey Marchuk 
18032c140f58SAlexey Marchuk fail_req:
18042c140f58SAlexey Marchuk 	memset(&cpl, 0, sizeof(cpl));
18052c140f58SAlexey Marchuk 	cpl.status.sc = sc;
18062c140f58SAlexey Marchuk 	cpl.status.sct = SPDK_NVME_SCT_GENERIC;
18072c140f58SAlexey Marchuk 	cpl.status.dnr = dnr;
18082c140f58SAlexey Marchuk 	nvme_rdma_req_complete(rdma_req, &cpl, true);
18092c140f58SAlexey Marchuk }
18102c140f58SAlexey Marchuk 
18112c140f58SAlexey Marchuk static inline int
18122c140f58SAlexey Marchuk nvme_rdma_apply_accel_sequence(struct nvme_rdma_qpair *rqpair, struct nvme_request *req,
18132c140f58SAlexey Marchuk 			       struct spdk_nvme_rdma_req *rdma_req)
18142c140f58SAlexey Marchuk {
18152c140f58SAlexey Marchuk 	struct spdk_nvme_poll_group *pg = rqpair->qpair.poll_group->group;
18162c140f58SAlexey Marchuk 	struct spdk_memory_domain *src_domain;
18172c140f58SAlexey Marchuk 	void *src_domain_ctx;
18182c140f58SAlexey Marchuk 	void *accel_seq = req->accel_sequence;
18192c140f58SAlexey Marchuk 	uint32_t iovcnt = 0;
18202c140f58SAlexey Marchuk 	int rc;
18212c140f58SAlexey Marchuk 
18222c140f58SAlexey Marchuk 	SPDK_DEBUGLOG(nvme, "req %p, start accel seq %p\n", rdma_req, accel_seq);
18232c140f58SAlexey Marchuk 	if (nvme_payload_type(&req->payload) == NVME_PAYLOAD_TYPE_SGL) {
18242c140f58SAlexey Marchuk 		void *addr;
18252c140f58SAlexey Marchuk 		uint32_t sge_length, payload_size;
18262c140f58SAlexey Marchuk 
18272c140f58SAlexey Marchuk 		payload_size = req->payload_size;
18282c140f58SAlexey Marchuk 		assert(payload_size);
18292c140f58SAlexey Marchuk 		req->payload.reset_sgl_fn(req->payload.contig_or_cb_arg, req->payload_offset);
18302c140f58SAlexey Marchuk 		do {
18312c140f58SAlexey Marchuk 			rc = req->payload.next_sge_fn(req->payload.contig_or_cb_arg, &addr, &sge_length);
18322c140f58SAlexey Marchuk 			if (spdk_unlikely(rc)) {
18332c140f58SAlexey Marchuk 				return -1;
18342c140f58SAlexey Marchuk 			}
18352c140f58SAlexey Marchuk 			sge_length = spdk_min(payload_size, sge_length);
18362c140f58SAlexey Marchuk 			rdma_req->iovs[iovcnt].iov_base = addr;
18372c140f58SAlexey Marchuk 			rdma_req->iovs[iovcnt].iov_len = sge_length;
18382c140f58SAlexey Marchuk 			iovcnt++;
18392c140f58SAlexey Marchuk 			payload_size -= sge_length;
18402c140f58SAlexey Marchuk 		} while (payload_size && iovcnt < NVME_RDMA_MAX_SGL_DESCRIPTORS);
18412c140f58SAlexey Marchuk 
18422c140f58SAlexey Marchuk 		if (spdk_unlikely(payload_size)) {
18432c140f58SAlexey Marchuk 			SPDK_ERRLOG("not enough iovs to handle req %p, remaining len %u\n", rdma_req, payload_size);
18442c140f58SAlexey Marchuk 			return -E2BIG;
18452c140f58SAlexey Marchuk 		}
18462c140f58SAlexey Marchuk 	} else {
18472c140f58SAlexey Marchuk 		rdma_req->iovs[iovcnt].iov_base = req->payload.contig_or_cb_arg;
18482c140f58SAlexey Marchuk 		rdma_req->iovs[iovcnt].iov_len = req->payload_size;
18492c140f58SAlexey Marchuk 		iovcnt = 1;
18502c140f58SAlexey Marchuk 	}
18512c140f58SAlexey Marchuk 	if (req->payload.opts && req->payload.opts->memory_domain) {
18522c140f58SAlexey Marchuk 		if (accel_seq) {
18532c140f58SAlexey Marchuk 			src_domain = rqpair->rdma_qp->domain;
18542c140f58SAlexey Marchuk 			src_domain_ctx = rdma_req;
18552c140f58SAlexey Marchuk 		} else {
18562c140f58SAlexey Marchuk 			src_domain = req->payload.opts->memory_domain;
18572c140f58SAlexey Marchuk 			src_domain_ctx = req->payload.opts->memory_domain_ctx;
18582c140f58SAlexey Marchuk 		}
18592c140f58SAlexey Marchuk 	} else {
18602c140f58SAlexey Marchuk 		src_domain = NULL;
18612c140f58SAlexey Marchuk 		src_domain_ctx = NULL;
18622c140f58SAlexey Marchuk 	}
18632c140f58SAlexey Marchuk 
18642c140f58SAlexey Marchuk 	rc = nvme_rdma_accel_append_copy(pg, &accel_seq, rqpair->rdma_qp->domain, rdma_req, rdma_req->iovs,
18652c140f58SAlexey Marchuk 					 iovcnt, src_domain, src_domain_ctx);
18662c140f58SAlexey Marchuk 	if (spdk_unlikely(rc)) {
18672c140f58SAlexey Marchuk 		return rc;
18682c140f58SAlexey Marchuk 	}
18692c140f58SAlexey Marchuk 
18702c140f58SAlexey Marchuk 	if (spdk_nvme_opc_get_data_transfer(req->cmd.opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) {
18712c140f58SAlexey Marchuk 		nvme_rdma_accel_reverse(pg, accel_seq);
18722c140f58SAlexey Marchuk 	}
18732c140f58SAlexey Marchuk 
18742c140f58SAlexey Marchuk 	rdma_req->in_progress_accel = 1;
18752c140f58SAlexey Marchuk 	TAILQ_INSERT_TAIL(&rqpair->outstanding_reqs, rdma_req, link);
18762c140f58SAlexey Marchuk 	rqpair->num_outstanding_reqs++;
18772c140f58SAlexey Marchuk 
18782c140f58SAlexey Marchuk 	SPDK_DEBUGLOG(nvme, "req %p, finish accel seq %p\n", rdma_req, accel_seq);
18792c140f58SAlexey Marchuk 	nvme_rdma_accel_finish(pg, accel_seq, nvme_rdma_accel_completion_cb, rdma_req);
18802c140f58SAlexey Marchuk 
18812c140f58SAlexey Marchuk 	return 0;
18822c140f58SAlexey Marchuk }
18832c140f58SAlexey Marchuk 
18842c140f58SAlexey Marchuk static inline int
18852c140f58SAlexey Marchuk nvme_rdma_memory_domain_transfer_data(struct spdk_memory_domain *dst_domain, void *dst_domain_ctx,
18862c140f58SAlexey Marchuk 				      struct iovec *dst_iov, uint32_t dst_iovcnt,
18872c140f58SAlexey Marchuk 				      struct spdk_memory_domain *src_domain, void *src_domain_ctx,
18882c140f58SAlexey Marchuk 				      struct iovec *src_iov, uint32_t src_iovcnt,
18892c140f58SAlexey Marchuk 				      struct spdk_memory_domain_translation_result *translation,
18902c140f58SAlexey Marchuk 				      spdk_memory_domain_data_cpl_cb cpl_cb, void *cpl_cb_arg)
18912c140f58SAlexey Marchuk {
18922c140f58SAlexey Marchuk 	struct nvme_rdma_memory_translation_ctx ctx;
18932c140f58SAlexey Marchuk 	struct spdk_nvme_rdma_req *rdma_req = dst_domain_ctx;
18942c140f58SAlexey Marchuk 	struct nvme_request *req = rdma_req->req;
18952c140f58SAlexey Marchuk 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(rdma_req->req->qpair);
18962c140f58SAlexey Marchuk 	struct spdk_nvme_ctrlr *ctrlr = rqpair->qpair.ctrlr;
18972c140f58SAlexey Marchuk 	bool icd_supported;
18982c140f58SAlexey Marchuk 
18992c140f58SAlexey Marchuk 	assert(dst_domain == rqpair->rdma_qp->domain);
19002c140f58SAlexey Marchuk 	assert(src_domain);
19012c140f58SAlexey Marchuk 	assert(spdk_memory_domain_get_dma_device_type(src_domain) == SPDK_DMA_DEVICE_TYPE_RDMA);
19022c140f58SAlexey Marchuk 	/* We expect "inplace" operation */
19032c140f58SAlexey Marchuk 	assert(dst_iov == src_iov);
19042c140f58SAlexey Marchuk 	assert(dst_iovcnt == src_iovcnt);
19052c140f58SAlexey Marchuk 
19062c140f58SAlexey Marchuk 	if (spdk_unlikely(!src_domain ||
19072c140f58SAlexey Marchuk 			  spdk_memory_domain_get_dma_device_type(src_domain) != SPDK_DMA_DEVICE_TYPE_RDMA)) {
19082c140f58SAlexey Marchuk 		SPDK_ERRLOG("Unexpected source memory domain %p, type %d\n", src_domain,
19092c140f58SAlexey Marchuk 			    src_domain ? (int)spdk_memory_domain_get_dma_device_type(src_domain) : -1);
19102c140f58SAlexey Marchuk 		return -ENOTSUP;
19112c140f58SAlexey Marchuk 	}
19122c140f58SAlexey Marchuk 	if (spdk_unlikely(dst_iovcnt != 1 || !translation || translation->iov_count != 1)) {
19132c140f58SAlexey Marchuk 		SPDK_ERRLOG("Unexpected iovcnt %u or missed translation, rdma_req %p\n", dst_iovcnt, rdma_req);
19142c140f58SAlexey Marchuk 		return -ENOTSUP;
19152c140f58SAlexey Marchuk 	}
19162c140f58SAlexey Marchuk 	ctx.addr = translation->iov.iov_base;
19172c140f58SAlexey Marchuk 	ctx.length = translation->iov.iov_len;
19182c140f58SAlexey Marchuk 	ctx.lkey = translation->rdma.lkey;
19192c140f58SAlexey Marchuk 	ctx.rkey = translation->rdma.rkey;
19202c140f58SAlexey Marchuk 
19212c140f58SAlexey Marchuk 	SPDK_DEBUGLOG(nvme, "req %p, addr %p, len %zu, key %u\n", rdma_req, ctx.addr, ctx.length, ctx.rkey);
19222c140f58SAlexey Marchuk 	icd_supported = spdk_nvme_opc_get_data_transfer(req->cmd.opc) == SPDK_NVME_DATA_HOST_TO_CONTROLLER
19232c140f58SAlexey Marchuk 			&& req->payload_size <= ctrlr->ioccsz_bytes && ctrlr->icdoff == 0;
19242c140f58SAlexey Marchuk 
19252c140f58SAlexey Marchuk 	/* We expect that result of accel sequence is a Memory Key which describes a virtually contig address space.
19262c140f58SAlexey Marchuk 	 * That means we prepare a contig request even if original payload was scattered */
19272c140f58SAlexey Marchuk 	if (icd_supported) {
19282c140f58SAlexey Marchuk 		nvme_rdma_configure_contig_inline_request(rdma_req, req, &ctx);
19292c140f58SAlexey Marchuk 	} else {
19302c140f58SAlexey Marchuk 		nvme_rdma_configure_contig_request(rdma_req, req, &ctx);
19312c140f58SAlexey Marchuk 	}
19322c140f58SAlexey Marchuk 	rdma_req->transfer_cpl_cb = cpl_cb;
19332c140f58SAlexey Marchuk 	rdma_req->transfer_cpl_cb_arg = cpl_cb_arg;
19342c140f58SAlexey Marchuk 
19352c140f58SAlexey Marchuk 	memcpy(&rqpair->cmds[rdma_req->id], &req->cmd, sizeof(req->cmd));
19362c140f58SAlexey Marchuk 
19372c140f58SAlexey Marchuk 	return _nvme_rdma_qpair_submit_request(rqpair, rdma_req);
19382c140f58SAlexey Marchuk }
19392c140f58SAlexey Marchuk 
19402c140f58SAlexey Marchuk static inline int
194151bde662SAlexey Marchuk nvme_rdma_req_init(struct nvme_rdma_qpair *rqpair, struct spdk_nvme_rdma_req *rdma_req)
1942246c39a7SZiye Yang {
194351bde662SAlexey Marchuk 	struct nvme_request *req = rdma_req->req;
194494f87a2dSPotnuri Bharat Teja 	struct spdk_nvme_ctrlr *ctrlr = rqpair->qpair.ctrlr;
1945d2510a56SAlexey Marchuk 	enum nvme_payload_type payload_type;
1946d2510a56SAlexey Marchuk 	bool icd_supported;
1947f0e4b91fSAlexey Marchuk 	int rc = -1;
1948246c39a7SZiye Yang 
1949d2510a56SAlexey Marchuk 	payload_type = nvme_payload_type(&req->payload);
195094f87a2dSPotnuri Bharat Teja 	/*
195194f87a2dSPotnuri Bharat Teja 	 * Check if icdoff is non zero, to avoid interop conflicts with
19526c391ec2SJim Harris 	 * targets with non-zero icdoff.  Both SPDK and the Linux kernel
19536c391ec2SJim Harris 	 * targets use icdoff = 0.  For targets with non-zero icdoff, we
19546c391ec2SJim Harris 	 * will currently just not use inline data for now.
195594f87a2dSPotnuri Bharat Teja 	 */
1956d2510a56SAlexey Marchuk 	icd_supported = spdk_nvme_opc_get_data_transfer(req->cmd.opc) == SPDK_NVME_DATA_HOST_TO_CONTROLLER
1957d2510a56SAlexey Marchuk 			&& req->payload_size <= ctrlr->ioccsz_bytes && ctrlr->icdoff == 0;
1958d2510a56SAlexey Marchuk 
1959f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(req->payload_size == 0)) {
1960d2510a56SAlexey Marchuk 		rc = nvme_rdma_build_null_request(rdma_req);
1961d2510a56SAlexey Marchuk 	} else if (payload_type == NVME_PAYLOAD_TYPE_CONTIG) {
1962d2510a56SAlexey Marchuk 		if (icd_supported) {
196394f87a2dSPotnuri Bharat Teja 			rc = nvme_rdma_build_contig_inline_request(rqpair, rdma_req);
196494f87a2dSPotnuri Bharat Teja 		} else {
196594f87a2dSPotnuri Bharat Teja 			rc = nvme_rdma_build_contig_request(rqpair, rdma_req);
196694f87a2dSPotnuri Bharat Teja 		}
1967d2510a56SAlexey Marchuk 	} else if (payload_type == NVME_PAYLOAD_TYPE_SGL) {
1968d2510a56SAlexey Marchuk 		if (icd_supported) {
196994f87a2dSPotnuri Bharat Teja 			rc = nvme_rdma_build_sgl_inline_request(rqpair, rdma_req);
197094f87a2dSPotnuri Bharat Teja 		} else {
197194f87a2dSPotnuri Bharat Teja 			rc = nvme_rdma_build_sgl_request(rqpair, rdma_req);
197294f87a2dSPotnuri Bharat Teja 		}
1973246c39a7SZiye Yang 	}
1974246c39a7SZiye Yang 
1975f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(rc)) {
197683e55653SDaniel Verkamp 		return rc;
197790f13aa6SZiye Yang 	}
197890f13aa6SZiye Yang 
1979fb31963cSDaniel Verkamp 	memcpy(&rqpair->cmds[rdma_req->id], &req->cmd, sizeof(req->cmd));
1980ee827136SDaniel Verkamp 	return 0;
1981246c39a7SZiye Yang }
1982246c39a7SZiye Yang 
1983246c39a7SZiye Yang static struct spdk_nvme_qpair *
19840ebf93e3SBen Walker nvme_rdma_ctrlr_create_qpair(struct spdk_nvme_ctrlr *ctrlr,
19850ebf93e3SBen Walker 			     uint16_t qid, uint32_t qsize,
19865742e9b9SDaniel Verkamp 			     enum spdk_nvme_qprio qprio,
198754f81b37SEvgeniy Kochetov 			     uint32_t num_requests,
19889717b0c3SShuhei Matsumoto 			     bool delay_cmd_submit,
19899717b0c3SShuhei Matsumoto 			     bool async)
1990246c39a7SZiye Yang {
1991246c39a7SZiye Yang 	struct nvme_rdma_qpair *rqpair;
1992246c39a7SZiye Yang 	struct spdk_nvme_qpair *qpair;
19937f82fb65SSeth Howell 	int rc;
1994246c39a7SZiye Yang 
1995834e3c5aSEvgeniy Kochetov 	if (qsize < SPDK_NVME_QUEUE_MIN_ENTRIES) {
1996834e3c5aSEvgeniy Kochetov 		SPDK_ERRLOG("Failed to create qpair with size %u. Minimum queue size is %d.\n",
1997834e3c5aSEvgeniy Kochetov 			    qsize, SPDK_NVME_QUEUE_MIN_ENTRIES);
1998834e3c5aSEvgeniy Kochetov 		return NULL;
1999834e3c5aSEvgeniy Kochetov 	}
2000834e3c5aSEvgeniy Kochetov 
2001186b109dSJim Harris 	rqpair = spdk_zmalloc(sizeof(struct nvme_rdma_qpair), 0, NULL, SPDK_ENV_NUMA_ID_ANY,
2002c66b68e9SAleksey Marchuk 			      SPDK_MALLOC_DMA);
2003246c39a7SZiye Yang 	if (!rqpair) {
2004246c39a7SZiye Yang 		SPDK_ERRLOG("failed to get create rqpair\n");
2005246c39a7SZiye Yang 		return NULL;
2006246c39a7SZiye Yang 	}
2007246c39a7SZiye Yang 
2008834e3c5aSEvgeniy Kochetov 	/* Set num_entries one less than queue size. According to NVMe
2009834e3c5aSEvgeniy Kochetov 	 * and NVMe-oF specs we can not submit queue size requests,
2010834e3c5aSEvgeniy Kochetov 	 * one slot shall always remain empty.
2011834e3c5aSEvgeniy Kochetov 	 */
2012834e3c5aSEvgeniy Kochetov 	rqpair->num_entries = qsize - 1;
201354f81b37SEvgeniy Kochetov 	rqpair->delay_cmd_submit = delay_cmd_submit;
201429974dc8SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_INVALID;
2015cec5ba28SAlexey Marchuk 	rqpair->append_copy = g_spdk_nvme_transport_opts.rdma_umr_per_io &&
2016cec5ba28SAlexey Marchuk 			      spdk_rdma_provider_accel_sequence_supported() && qid != 0;
2017cec5ba28SAlexey Marchuk 	SPDK_DEBUGLOG(nvme, "rqpair %p, append_copy %s\n", rqpair,
2018cec5ba28SAlexey Marchuk 		      rqpair->append_copy ? "enabled" : "diabled");
2019246c39a7SZiye Yang 	qpair = &rqpair->qpair;
20209717b0c3SShuhei Matsumoto 	rc = nvme_qpair_init(qpair, qid, ctrlr, qprio, num_requests, async);
2021246c39a7SZiye Yang 	if (rc != 0) {
2022c66b68e9SAleksey Marchuk 		spdk_free(rqpair);
2023246c39a7SZiye Yang 		return NULL;
2024246c39a7SZiye Yang 	}
2025246c39a7SZiye Yang 
2026246c39a7SZiye Yang 	return qpair;
2027246c39a7SZiye Yang }
2028246c39a7SZiye Yang 
202977c46571SShuhei Matsumoto static void
203077c46571SShuhei Matsumoto nvme_rdma_qpair_destroy(struct nvme_rdma_qpair *rqpair)
2031bcf08457SShuhei Matsumoto {
20322c13441bSShuhei Matsumoto 	struct spdk_nvme_qpair *qpair = &rqpair->qpair;
20332c13441bSShuhei Matsumoto 	struct nvme_rdma_ctrlr *rctrlr;
20342c13441bSShuhei Matsumoto 	struct nvme_rdma_cm_event_entry *entry, *tmp;
20352c13441bSShuhei Matsumoto 
20368a01b4d6SAlexey Marchuk 	spdk_rdma_utils_free_mem_map(&rqpair->mr_map);
20372c13441bSShuhei Matsumoto 
20382c13441bSShuhei Matsumoto 	if (rqpair->evt) {
20392c13441bSShuhei Matsumoto 		rdma_ack_cm_event(rqpair->evt);
20402c13441bSShuhei Matsumoto 		rqpair->evt = NULL;
20412c13441bSShuhei Matsumoto 	}
20422c13441bSShuhei Matsumoto 
20432c13441bSShuhei Matsumoto 	/*
20442c13441bSShuhei Matsumoto 	 * This works because we have the controller lock both in
20452c13441bSShuhei Matsumoto 	 * this function and in the function where we add new events.
20462c13441bSShuhei Matsumoto 	 */
20472c13441bSShuhei Matsumoto 	if (qpair->ctrlr != NULL) {
20482c13441bSShuhei Matsumoto 		rctrlr = nvme_rdma_ctrlr(qpair->ctrlr);
20492c13441bSShuhei Matsumoto 		STAILQ_FOREACH_SAFE(entry, &rctrlr->pending_cm_events, link, tmp) {
2050622ceb7fSAlexey Marchuk 			if (entry->evt->id->context == rqpair) {
20512c13441bSShuhei Matsumoto 				STAILQ_REMOVE(&rctrlr->pending_cm_events, entry, nvme_rdma_cm_event_entry, link);
20522c13441bSShuhei Matsumoto 				rdma_ack_cm_event(entry->evt);
20532c13441bSShuhei Matsumoto 				STAILQ_INSERT_HEAD(&rctrlr->free_cm_events, entry, link);
20542c13441bSShuhei Matsumoto 			}
20552c13441bSShuhei Matsumoto 		}
20562c13441bSShuhei Matsumoto 	}
20572c13441bSShuhei Matsumoto 
2058bcf08457SShuhei Matsumoto 	if (rqpair->cm_id) {
2059bcf08457SShuhei Matsumoto 		if (rqpair->rdma_qp) {
20608a01b4d6SAlexey Marchuk 			spdk_rdma_utils_put_pd(rqpair->rdma_qp->qp->pd);
2061cf151d60SAlexey Marchuk 			spdk_rdma_provider_qp_destroy(rqpair->rdma_qp);
2062bcf08457SShuhei Matsumoto 			rqpair->rdma_qp = NULL;
2063bcf08457SShuhei Matsumoto 		}
2064bcf08457SShuhei Matsumoto 	}
2065bcf08457SShuhei Matsumoto 
20661d58eb03SShuhei Matsumoto 	if (rqpair->poller) {
20671d58eb03SShuhei Matsumoto 		struct nvme_rdma_poll_group     *group;
20681d58eb03SShuhei Matsumoto 
20691d58eb03SShuhei Matsumoto 		assert(qpair->poll_group);
20701d58eb03SShuhei Matsumoto 		group = nvme_rdma_poll_group(qpair->poll_group);
20711d58eb03SShuhei Matsumoto 
20721d58eb03SShuhei Matsumoto 		nvme_rdma_poll_group_put_poller(group, rqpair->poller);
20731d58eb03SShuhei Matsumoto 
20741d58eb03SShuhei Matsumoto 		rqpair->poller = NULL;
20751d58eb03SShuhei Matsumoto 		rqpair->cq = NULL;
20769aabfb59SShuhei Matsumoto 		if (rqpair->srq) {
2077bcd987eaSShuhei Matsumoto 			rqpair->srq = NULL;
2078bcd987eaSShuhei Matsumoto 			rqpair->rsps = NULL;
20799aabfb59SShuhei Matsumoto 		}
20801d58eb03SShuhei Matsumoto 	} else if (rqpair->cq) {
2081bcf08457SShuhei Matsumoto 		ibv_destroy_cq(rqpair->cq);
2082bcf08457SShuhei Matsumoto 		rqpair->cq = NULL;
2083bcf08457SShuhei Matsumoto 	}
20849aabfb59SShuhei Matsumoto 
20859aabfb59SShuhei Matsumoto 	nvme_rdma_free_reqs(rqpair);
20869aabfb59SShuhei Matsumoto 	nvme_rdma_free_rsps(rqpair->rsps);
20879aabfb59SShuhei Matsumoto 	rqpair->rsps = NULL;
2088e44d6317Ssijie.sun 
2089e44d6317Ssijie.sun 	/* destroy cm_id last so cma device will not be freed before we destroy the cq. */
2090e44d6317Ssijie.sun 	if (rqpair->cm_id) {
2091e44d6317Ssijie.sun 		rdma_destroy_id(rqpair->cm_id);
2092e44d6317Ssijie.sun 		rqpair->cm_id = NULL;
2093e44d6317Ssijie.sun 	}
209477c46571SShuhei Matsumoto }
209577c46571SShuhei Matsumoto 
2096813756e7SBen Walker static void nvme_rdma_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr);
2097813756e7SBen Walker 
2098a333974eSAlex Michon static void
2099a333974eSAlex Michon nvme_rdma_qpair_flush_send_wrs(struct nvme_rdma_qpair *rqpair)
2100a333974eSAlex Michon {
2101a333974eSAlex Michon 	struct ibv_send_wr *bad_wr = NULL;
2102a333974eSAlex Michon 	int rc;
2103a333974eSAlex Michon 
2104a333974eSAlex Michon 	rc = spdk_rdma_provider_qp_flush_send_wrs(rqpair->rdma_qp, &bad_wr);
2105a333974eSAlex Michon 	if (rc) {
2106a333974eSAlex Michon 		nvme_rdma_reset_failed_sends(rqpair, bad_wr);
2107a333974eSAlex Michon 	}
2108a333974eSAlex Michon }
2109a333974eSAlex Michon 
211077c46571SShuhei Matsumoto static int
211177c46571SShuhei Matsumoto nvme_rdma_qpair_disconnected(struct nvme_rdma_qpair *rqpair, int ret)
211277c46571SShuhei Matsumoto {
21134b732235SShuhei Matsumoto 	if (ret) {
21144b732235SShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "Target did not respond to qpair disconnect.\n");
21154b732235SShuhei Matsumoto 		goto quiet;
21164b732235SShuhei Matsumoto 	}
21174b732235SShuhei Matsumoto 
21180e4b13dcSShuhei Matsumoto 	if (rqpair->poller == NULL) {
2119113075beSAlexey Marchuk 		/* If poller is not used, cq is not shared.
21200e4b13dcSShuhei Matsumoto 		 * So complete disconnecting qpair immediately.
21214b732235SShuhei Matsumoto 		 */
21224b732235SShuhei Matsumoto 		goto quiet;
21234b732235SShuhei Matsumoto 	}
21244b732235SShuhei Matsumoto 
21254999a985SShuhei Matsumoto 	if (rqpair->rsps == NULL) {
21264999a985SShuhei Matsumoto 		goto quiet;
21274999a985SShuhei Matsumoto 	}
21284999a985SShuhei Matsumoto 
2129a333974eSAlex Michon 	nvme_rdma_qpair_flush_send_wrs(rqpair);
2130a333974eSAlex Michon 
2131e44d6317Ssijie.sun 	if (rqpair->need_destroy ||
2132e44d6317Ssijie.sun 	    (rqpair->current_num_sends != 0 ||
21332c140f58SAlexey Marchuk 	     (!rqpair->srq && rqpair->rsps->current_num_recvs != 0)) ||
21342c140f58SAlexey Marchuk 	    ((rqpair->qpair.ctrlr->flags & SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED) &&
21352c140f58SAlexey Marchuk 	     (!TAILQ_EMPTY(&rqpair->outstanding_reqs)))) {
21364b732235SShuhei Matsumoto 		rqpair->state = NVME_RDMA_QPAIR_STATE_LINGERING;
21374b732235SShuhei Matsumoto 		rqpair->evt_timeout_ticks = (NVME_RDMA_DISCONNECTED_QPAIR_TIMEOUT_US * spdk_get_ticks_hz()) /
21384b732235SShuhei Matsumoto 					    SPDK_SEC_TO_USEC + spdk_get_ticks();
21394b732235SShuhei Matsumoto 
21404b732235SShuhei Matsumoto 		return -EAGAIN;
21414b732235SShuhei Matsumoto 	}
21424b732235SShuhei Matsumoto 
21434b732235SShuhei Matsumoto quiet:
21444b732235SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_EXITED;
21454b732235SShuhei Matsumoto 
2146d352a9ecSShuhei Matsumoto 	nvme_rdma_qpair_abort_reqs(&rqpair->qpair, rqpair->qpair.abort_dnr);
21470e4b13dcSShuhei Matsumoto 	nvme_rdma_qpair_destroy(rqpair);
21484b732235SShuhei Matsumoto 	nvme_transport_ctrlr_disconnect_qpair_done(&rqpair->qpair);
21494b732235SShuhei Matsumoto 
21504b732235SShuhei Matsumoto 	return 0;
21514b732235SShuhei Matsumoto }
21524b732235SShuhei Matsumoto 
21534b732235SShuhei Matsumoto static int
21544b732235SShuhei Matsumoto nvme_rdma_qpair_wait_until_quiet(struct nvme_rdma_qpair *rqpair)
21554b732235SShuhei Matsumoto {
21564985289aSAlexey Marchuk 	struct spdk_nvme_qpair *qpair = &rqpair->qpair;
21574985289aSAlexey Marchuk 	struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
21584985289aSAlexey Marchuk 
21594b732235SShuhei Matsumoto 	if (spdk_get_ticks() < rqpair->evt_timeout_ticks &&
2160bcd987eaSShuhei Matsumoto 	    (rqpair->current_num_sends != 0 ||
2161bcd987eaSShuhei Matsumoto 	     (!rqpair->srq && rqpair->rsps->current_num_recvs != 0))) {
21624b732235SShuhei Matsumoto 		return -EAGAIN;
21634b732235SShuhei Matsumoto 	}
21644b732235SShuhei Matsumoto 
21659717b0c3SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_EXITED;
2166d352a9ecSShuhei Matsumoto 	nvme_rdma_qpair_abort_reqs(qpair, qpair->abort_dnr);
21674985289aSAlexey Marchuk 	if (!nvme_qpair_is_admin_queue(qpair)) {
21684985289aSAlexey Marchuk 		nvme_robust_mutex_lock(&ctrlr->ctrlr_lock);
21694985289aSAlexey Marchuk 	}
21700e4b13dcSShuhei Matsumoto 	nvme_rdma_qpair_destroy(rqpair);
21714985289aSAlexey Marchuk 	if (!nvme_qpair_is_admin_queue(qpair)) {
21724985289aSAlexey Marchuk 		nvme_robust_mutex_unlock(&ctrlr->ctrlr_lock);
21734985289aSAlexey Marchuk 	}
2174bcf08457SShuhei Matsumoto 	nvme_transport_ctrlr_disconnect_qpair_done(&rqpair->qpair);
2175bcf08457SShuhei Matsumoto 
2176bcf08457SShuhei Matsumoto 	return 0;
2177bcf08457SShuhei Matsumoto }
2178bcf08457SShuhei Matsumoto 
217954a022ddSBen Walker static void
2180aa36c181SShuhei Matsumoto _nvme_rdma_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair,
2181aa36c181SShuhei Matsumoto 				  nvme_rdma_cm_event_cb disconnected_qpair_cb)
2182246c39a7SZiye Yang {
2183e7d8c05bSJim Harris 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
218474b2916cSZiye Yang 	int rc;
2185e15a704eSDaniel Verkamp 
2186aa36c181SShuhei Matsumoto 	assert(disconnected_qpair_cb != NULL);
2187aa36c181SShuhei Matsumoto 
21889717b0c3SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_EXITING;
21899717b0c3SShuhei Matsumoto 
2190eb09178aSAlexey Marchuk 	if (rqpair->cm_id) {
2191eb09178aSAlexey Marchuk 		if (rqpair->rdma_qp) {
2192cf151d60SAlexey Marchuk 			rc = spdk_rdma_provider_qp_disconnect(rqpair->rdma_qp);
21932c13441bSShuhei Matsumoto 			if ((qpair->ctrlr != NULL) && (rc == 0)) {
21949717b0c3SShuhei Matsumoto 				rc = nvme_rdma_process_event_start(rqpair, RDMA_CM_EVENT_DISCONNECTED,
2195aa36c181SShuhei Matsumoto 								   disconnected_qpair_cb);
21969717b0c3SShuhei Matsumoto 				if (rc == 0) {
2197cf7f2533SShuhei Matsumoto 					return;
2198c8f986c7SShuhei Matsumoto 				}
2199bcf08457SShuhei Matsumoto 			}
2200eb09178aSAlexey Marchuk 		}
22019717b0c3SShuhei Matsumoto 	}
2202c8f986c7SShuhei Matsumoto 
2203aa36c181SShuhei Matsumoto 	disconnected_qpair_cb(rqpair, 0);
2204c8f986c7SShuhei Matsumoto }
2205eb09178aSAlexey Marchuk 
22069717b0c3SShuhei Matsumoto static int
22079717b0c3SShuhei Matsumoto nvme_rdma_ctrlr_disconnect_qpair_poll(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
22089717b0c3SShuhei Matsumoto {
22099717b0c3SShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
22109717b0c3SShuhei Matsumoto 	int rc;
22119717b0c3SShuhei Matsumoto 
22129717b0c3SShuhei Matsumoto 	switch (rqpair->state) {
22139717b0c3SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_EXITING:
22149717b0c3SShuhei Matsumoto 		if (!nvme_qpair_is_admin_queue(qpair)) {
2215e10b4806SJim Harris 			nvme_ctrlr_lock(ctrlr);
22169717b0c3SShuhei Matsumoto 		}
22179717b0c3SShuhei Matsumoto 
22189717b0c3SShuhei Matsumoto 		rc = nvme_rdma_process_event_poll(rqpair);
22199717b0c3SShuhei Matsumoto 
22209717b0c3SShuhei Matsumoto 		if (!nvme_qpair_is_admin_queue(qpair)) {
2221e10b4806SJim Harris 			nvme_ctrlr_unlock(ctrlr);
22229717b0c3SShuhei Matsumoto 		}
22239717b0c3SShuhei Matsumoto 		break;
22249717b0c3SShuhei Matsumoto 
22254b732235SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_LINGERING:
22264b732235SShuhei Matsumoto 		rc = nvme_rdma_qpair_wait_until_quiet(rqpair);
22274b732235SShuhei Matsumoto 		break;
22289717b0c3SShuhei Matsumoto 	case NVME_RDMA_QPAIR_STATE_EXITED:
22299717b0c3SShuhei Matsumoto 		rc = 0;
22309717b0c3SShuhei Matsumoto 		break;
22319717b0c3SShuhei Matsumoto 
22329717b0c3SShuhei Matsumoto 	default:
22339717b0c3SShuhei Matsumoto 		assert(false);
22349717b0c3SShuhei Matsumoto 		rc = -EAGAIN;
22359717b0c3SShuhei Matsumoto 		break;
22369717b0c3SShuhei Matsumoto 	}
22379717b0c3SShuhei Matsumoto 
22389717b0c3SShuhei Matsumoto 	return rc;
22399717b0c3SShuhei Matsumoto }
22409717b0c3SShuhei Matsumoto 
22419717b0c3SShuhei Matsumoto static void
22429717b0c3SShuhei Matsumoto nvme_rdma_ctrlr_disconnect_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
22439717b0c3SShuhei Matsumoto {
22449717b0c3SShuhei Matsumoto 	int rc;
22459717b0c3SShuhei Matsumoto 
2246aa36c181SShuhei Matsumoto 	_nvme_rdma_ctrlr_disconnect_qpair(ctrlr, qpair, nvme_rdma_qpair_disconnected);
22479717b0c3SShuhei Matsumoto 
22486a59daadSShuhei Matsumoto 	/* If the async mode is disabled, poll the qpair until it is actually disconnected.
22496a59daadSShuhei Matsumoto 	 * It is ensured that poll_group_process_completions() calls disconnected_qpair_cb
22506a59daadSShuhei Matsumoto 	 * for any disconnected qpair. Hence, we do not have to check if the qpair is in
22516a59daadSShuhei Matsumoto 	 * a poll group or not.
2252113075beSAlexey Marchuk 	 * At the same time, if the qpair is being destroyed, i.e. this function is called by
2253113075beSAlexey Marchuk 	 * spdk_nvme_ctrlr_free_io_qpair then we need to wait until qpair is disconnected, otherwise
2254113075beSAlexey Marchuk 	 * we may leak some resources.
22559717b0c3SShuhei Matsumoto 	 */
2256113075beSAlexey Marchuk 	if (qpair->async && !qpair->destroy_in_progress) {
22579717b0c3SShuhei Matsumoto 		return;
22589717b0c3SShuhei Matsumoto 	}
22599717b0c3SShuhei Matsumoto 
22609717b0c3SShuhei Matsumoto 	while (1) {
22619717b0c3SShuhei Matsumoto 		rc = nvme_rdma_ctrlr_disconnect_qpair_poll(ctrlr, qpair);
22629717b0c3SShuhei Matsumoto 		if (rc != -EAGAIN) {
22639717b0c3SShuhei Matsumoto 			break;
22649717b0c3SShuhei Matsumoto 		}
22659717b0c3SShuhei Matsumoto 	}
22669717b0c3SShuhei Matsumoto }
22679717b0c3SShuhei Matsumoto 
226820cf9080SShuhei Matsumoto static int
226920cf9080SShuhei Matsumoto nvme_rdma_stale_conn_disconnected(struct nvme_rdma_qpair *rqpair, int ret)
227020cf9080SShuhei Matsumoto {
227120cf9080SShuhei Matsumoto 	struct spdk_nvme_qpair *qpair = &rqpair->qpair;
227220cf9080SShuhei Matsumoto 
227320cf9080SShuhei Matsumoto 	if (ret) {
227420cf9080SShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "Target did not respond to qpair disconnect.\n");
227520cf9080SShuhei Matsumoto 	}
227620cf9080SShuhei Matsumoto 
227720cf9080SShuhei Matsumoto 	nvme_rdma_qpair_destroy(rqpair);
227820cf9080SShuhei Matsumoto 
227920cf9080SShuhei Matsumoto 	qpair->last_transport_failure_reason = qpair->transport_failure_reason;
228020cf9080SShuhei Matsumoto 	qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_NONE;
228120cf9080SShuhei Matsumoto 
228220cf9080SShuhei Matsumoto 	rqpair->state = NVME_RDMA_QPAIR_STATE_STALE_CONN;
228320cf9080SShuhei Matsumoto 	rqpair->evt_timeout_ticks = (NVME_RDMA_STALE_CONN_RETRY_DELAY_US * spdk_get_ticks_hz()) /
228420cf9080SShuhei Matsumoto 				    SPDK_SEC_TO_USEC + spdk_get_ticks();
228520cf9080SShuhei Matsumoto 
228620cf9080SShuhei Matsumoto 	return 0;
228720cf9080SShuhei Matsumoto }
228820cf9080SShuhei Matsumoto 
228920cf9080SShuhei Matsumoto static int
229020cf9080SShuhei Matsumoto nvme_rdma_stale_conn_retry(struct nvme_rdma_qpair *rqpair)
229120cf9080SShuhei Matsumoto {
229220cf9080SShuhei Matsumoto 	struct spdk_nvme_qpair *qpair = &rqpair->qpair;
229320cf9080SShuhei Matsumoto 
229420cf9080SShuhei Matsumoto 	if (rqpair->stale_conn_retry_count >= NVME_RDMA_STALE_CONN_RETRY_MAX) {
229520cf9080SShuhei Matsumoto 		SPDK_ERRLOG("Retry failed %d times, give up stale connection to qpair (cntlid:%u, qid:%u).\n",
229620cf9080SShuhei Matsumoto 			    NVME_RDMA_STALE_CONN_RETRY_MAX, qpair->ctrlr->cntlid, qpair->id);
229720cf9080SShuhei Matsumoto 		return -ESTALE;
229820cf9080SShuhei Matsumoto 	}
229920cf9080SShuhei Matsumoto 
230020cf9080SShuhei Matsumoto 	rqpair->stale_conn_retry_count++;
230120cf9080SShuhei Matsumoto 
23023f912cf0SMichal Berger 	SPDK_NOTICELOG("%d times, retry stale connection to qpair (cntlid:%u, qid:%u).\n",
230320cf9080SShuhei Matsumoto 		       rqpair->stale_conn_retry_count, qpair->ctrlr->cntlid, qpair->id);
230420cf9080SShuhei Matsumoto 
230520cf9080SShuhei Matsumoto 	_nvme_rdma_ctrlr_disconnect_qpair(qpair->ctrlr, qpair, nvme_rdma_stale_conn_disconnected);
230620cf9080SShuhei Matsumoto 
230720cf9080SShuhei Matsumoto 	return 0;
230820cf9080SShuhei Matsumoto }
230920cf9080SShuhei Matsumoto 
2310c8f986c7SShuhei Matsumoto static int
2311c8f986c7SShuhei Matsumoto nvme_rdma_ctrlr_delete_io_qpair(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_qpair *qpair)
2312c8f986c7SShuhei Matsumoto {
2313c8f986c7SShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair;
2314c8f986c7SShuhei Matsumoto 
2315c8f986c7SShuhei Matsumoto 	assert(qpair != NULL);
2316c8f986c7SShuhei Matsumoto 	rqpair = nvme_rdma_qpair(qpair);
2317c8f986c7SShuhei Matsumoto 
23181003e286SAlexey Marchuk 	if (rqpair->state != NVME_RDMA_QPAIR_STATE_EXITED) {
23191003e286SAlexey Marchuk 		int rc __attribute__((unused));
23201003e286SAlexey Marchuk 
23211003e286SAlexey Marchuk 		/* qpair was removed from the poll group while the disconnect is not finished.
23221003e286SAlexey Marchuk 		 * Destroy rdma resources forcefully. */
23231003e286SAlexey Marchuk 		rc = nvme_rdma_qpair_disconnected(rqpair, 0);
23241003e286SAlexey Marchuk 		assert(rc == 0);
23251003e286SAlexey Marchuk 	}
23261003e286SAlexey Marchuk 
2327d352a9ecSShuhei Matsumoto 	nvme_rdma_qpair_abort_reqs(qpair, qpair->abort_dnr);
2328c8f986c7SShuhei Matsumoto 	nvme_qpair_deinit(qpair);
2329c8f986c7SShuhei Matsumoto 
2330c66b68e9SAleksey Marchuk 	spdk_free(rqpair);
2331246c39a7SZiye Yang 
2332246c39a7SZiye Yang 	return 0;
2333246c39a7SZiye Yang }
2334246c39a7SZiye Yang 
233554a022ddSBen Walker static struct spdk_nvme_qpair *
2336246c39a7SZiye Yang nvme_rdma_ctrlr_create_io_qpair(struct spdk_nvme_ctrlr *ctrlr, uint16_t qid,
2337ce4fcbceSDaniel Verkamp 				const struct spdk_nvme_io_qpair_opts *opts)
2338246c39a7SZiye Yang {
2339ce4fcbceSDaniel Verkamp 	return nvme_rdma_ctrlr_create_qpair(ctrlr, qid, opts->io_queue_size, opts->qprio,
234054f81b37SEvgeniy Kochetov 					    opts->io_queue_requests,
23419717b0c3SShuhei Matsumoto 					    opts->delay_cmd_submit,
23429717b0c3SShuhei Matsumoto 					    opts->async_mode);
2343246c39a7SZiye Yang }
2344246c39a7SZiye Yang 
234554a022ddSBen Walker static int
2346246c39a7SZiye Yang nvme_rdma_ctrlr_enable(struct spdk_nvme_ctrlr *ctrlr)
2347246c39a7SZiye Yang {
2348246c39a7SZiye Yang 	/* do nothing here */
2349246c39a7SZiye Yang 	return 0;
2350246c39a7SZiye Yang }
2351246c39a7SZiye Yang 
235254a022ddSBen Walker static int nvme_rdma_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr);
235354a022ddSBen Walker 
23548dd1cd21SBen Walker /* We have to use the typedef in the function declaration to appease astyle. */
23558dd1cd21SBen Walker typedef struct spdk_nvme_ctrlr spdk_nvme_ctrlr_t;
23568dd1cd21SBen Walker 
23578dd1cd21SBen Walker static spdk_nvme_ctrlr_t *
23588dd1cd21SBen Walker nvme_rdma_ctrlr_construct(const struct spdk_nvme_transport_id *trid,
2359ba16e463SDaniel Verkamp 			  const struct spdk_nvme_ctrlr_opts *opts,
2360246c39a7SZiye Yang 			  void *devhandle)
2361246c39a7SZiye Yang {
2362246c39a7SZiye Yang 	struct nvme_rdma_ctrlr *rctrlr;
23638b539eb5SShuhei Matsumoto 	struct ibv_context **contexts;
23648b539eb5SShuhei Matsumoto 	struct ibv_device_attr dev_attr;
23655ac814e3SSeth Howell 	int i, flag, rc;
2366246c39a7SZiye Yang 
2367186b109dSJim Harris 	rctrlr = spdk_zmalloc(sizeof(struct nvme_rdma_ctrlr), 0, NULL, SPDK_ENV_NUMA_ID_ANY,
2368c66b68e9SAleksey Marchuk 			      SPDK_MALLOC_DMA);
2369246c39a7SZiye Yang 	if (rctrlr == NULL) {
2370246c39a7SZiye Yang 		SPDK_ERRLOG("could not allocate ctrlr\n");
2371246c39a7SZiye Yang 		return NULL;
2372246c39a7SZiye Yang 	}
2373246c39a7SZiye Yang 
2374f1539c28SAlexey Marchuk 	rctrlr->ctrlr.opts = *opts;
237562e0342eSJacek Kalwas 	rctrlr->ctrlr.trid = *trid;
237662e0342eSJacek Kalwas 
237762e0342eSJacek Kalwas 	if (opts->transport_retry_count > NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT) {
2378f1539c28SAlexey Marchuk 		SPDK_NOTICELOG("transport_retry_count exceeds max value %d, use max value\n",
2379f1539c28SAlexey Marchuk 			       NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT);
2380f1539c28SAlexey Marchuk 		rctrlr->ctrlr.opts.transport_retry_count = NVME_RDMA_CTRLR_MAX_TRANSPORT_RETRY_COUNT;
2381f1539c28SAlexey Marchuk 	}
238262e0342eSJacek Kalwas 
238362e0342eSJacek Kalwas 	if (opts->transport_ack_timeout > NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT) {
238494966468SAlexey Marchuk 		SPDK_NOTICELOG("transport_ack_timeout exceeds max value %d, use max value\n",
238594966468SAlexey Marchuk 			       NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT);
238694966468SAlexey Marchuk 		rctrlr->ctrlr.opts.transport_ack_timeout = NVME_RDMA_CTRLR_MAX_TRANSPORT_ACK_TIMEOUT;
238794966468SAlexey Marchuk 	}
2388246c39a7SZiye Yang 
23898b539eb5SShuhei Matsumoto 	contexts = rdma_get_devices(NULL);
23908b539eb5SShuhei Matsumoto 	if (contexts == NULL) {
23918b539eb5SShuhei Matsumoto 		SPDK_ERRLOG("rdma_get_devices() failed: %s (%d)\n", spdk_strerror(errno), errno);
2392c66b68e9SAleksey Marchuk 		spdk_free(rctrlr);
23938b539eb5SShuhei Matsumoto 		return NULL;
23948b539eb5SShuhei Matsumoto 	}
23958b539eb5SShuhei Matsumoto 
23968b539eb5SShuhei Matsumoto 	i = 0;
23978b539eb5SShuhei Matsumoto 	rctrlr->max_sge = NVME_RDMA_MAX_SGL_DESCRIPTORS;
23988b539eb5SShuhei Matsumoto 
23998b539eb5SShuhei Matsumoto 	while (contexts[i] != NULL) {
24008b539eb5SShuhei Matsumoto 		rc = ibv_query_device(contexts[i], &dev_attr);
24018b539eb5SShuhei Matsumoto 		if (rc < 0) {
24028b539eb5SShuhei Matsumoto 			SPDK_ERRLOG("Failed to query RDMA device attributes.\n");
24038b539eb5SShuhei Matsumoto 			rdma_free_devices(contexts);
2404c66b68e9SAleksey Marchuk 			spdk_free(rctrlr);
24058b539eb5SShuhei Matsumoto 			return NULL;
24068b539eb5SShuhei Matsumoto 		}
24078b539eb5SShuhei Matsumoto 		rctrlr->max_sge = spdk_min(rctrlr->max_sge, (uint16_t)dev_attr.max_sge);
24088b539eb5SShuhei Matsumoto 		i++;
24098b539eb5SShuhei Matsumoto 	}
24108b539eb5SShuhei Matsumoto 
24118b539eb5SShuhei Matsumoto 	rdma_free_devices(contexts);
24128b539eb5SShuhei Matsumoto 
2413246c39a7SZiye Yang 	rc = nvme_ctrlr_construct(&rctrlr->ctrlr);
2414246c39a7SZiye Yang 	if (rc != 0) {
2415c66b68e9SAleksey Marchuk 		spdk_free(rctrlr);
2416246c39a7SZiye Yang 		return NULL;
2417246c39a7SZiye Yang 	}
2418246c39a7SZiye Yang 
24195ac814e3SSeth Howell 	STAILQ_INIT(&rctrlr->pending_cm_events);
24205ac814e3SSeth Howell 	STAILQ_INIT(&rctrlr->free_cm_events);
2421c66b68e9SAleksey Marchuk 	rctrlr->cm_events = spdk_zmalloc(NVME_RDMA_NUM_CM_EVENTS * sizeof(*rctrlr->cm_events), 0, NULL,
2422186b109dSJim Harris 					 SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
24235ac814e3SSeth Howell 	if (rctrlr->cm_events == NULL) {
2424cc6920a4SJosh Soref 		SPDK_ERRLOG("unable to allocate buffers to hold CM events.\n");
242520564d42Syidong0635 		goto destruct_ctrlr;
24265ac814e3SSeth Howell 	}
24275ac814e3SSeth Howell 
24285ac814e3SSeth Howell 	for (i = 0; i < NVME_RDMA_NUM_CM_EVENTS; i++) {
24295ac814e3SSeth Howell 		STAILQ_INSERT_TAIL(&rctrlr->free_cm_events, &rctrlr->cm_events[i], link);
24305ac814e3SSeth Howell 	}
24315ac814e3SSeth Howell 
24325ac814e3SSeth Howell 	rctrlr->cm_channel = rdma_create_event_channel();
24335ac814e3SSeth Howell 	if (rctrlr->cm_channel == NULL) {
24345ac814e3SSeth Howell 		SPDK_ERRLOG("rdma_create_event_channel() failed\n");
243520564d42Syidong0635 		goto destruct_ctrlr;
24365ac814e3SSeth Howell 	}
24375ac814e3SSeth Howell 
24385ac814e3SSeth Howell 	flag = fcntl(rctrlr->cm_channel->fd, F_GETFL);
24395ac814e3SSeth Howell 	if (fcntl(rctrlr->cm_channel->fd, F_SETFL, flag | O_NONBLOCK) < 0) {
24405ac814e3SSeth Howell 		SPDK_ERRLOG("Cannot set event channel to non blocking\n");
244120564d42Syidong0635 		goto destruct_ctrlr;
24425ac814e3SSeth Howell 	}
24435ac814e3SSeth Howell 
24440ebf93e3SBen Walker 	rctrlr->ctrlr.adminq = nvme_rdma_ctrlr_create_qpair(&rctrlr->ctrlr, 0,
2445daa8f941SJacek Kalwas 			       rctrlr->ctrlr.opts.admin_queue_size, 0,
24469717b0c3SShuhei Matsumoto 			       rctrlr->ctrlr.opts.admin_queue_size, false, true);
2447c26c655dSBen Walker 	if (!rctrlr->ctrlr.adminq) {
2448c26c655dSBen Walker 		SPDK_ERRLOG("failed to create admin qpair\n");
244920564d42Syidong0635 		goto destruct_ctrlr;
2450246c39a7SZiye Yang 	}
24512c140f58SAlexey Marchuk 	if (spdk_rdma_provider_accel_sequence_supported()) {
24522c140f58SAlexey Marchuk 		rctrlr->ctrlr.flags |= SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
24532c140f58SAlexey Marchuk 	}
2454246c39a7SZiye Yang 
24553148c480SDaniel Verkamp 	if (nvme_ctrlr_add_process(&rctrlr->ctrlr, 0) != 0) {
24563148c480SDaniel Verkamp 		SPDK_ERRLOG("nvme_ctrlr_add_process() failed\n");
245720564d42Syidong0635 		goto destruct_ctrlr;
24583148c480SDaniel Verkamp 	}
24593148c480SDaniel Verkamp 
24602172c432STomasz Zawadzki 	SPDK_DEBUGLOG(nvme, "successfully initialized the nvmf ctrlr\n");
2461246c39a7SZiye Yang 	return &rctrlr->ctrlr;
246220564d42Syidong0635 
246320564d42Syidong0635 destruct_ctrlr:
246420564d42Syidong0635 	nvme_ctrlr_destruct(&rctrlr->ctrlr);
246520564d42Syidong0635 	return NULL;
2466246c39a7SZiye Yang }
2467246c39a7SZiye Yang 
246854a022ddSBen Walker static int
2469246c39a7SZiye Yang nvme_rdma_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
2470246c39a7SZiye Yang {
2471246c39a7SZiye Yang 	struct nvme_rdma_ctrlr *rctrlr = nvme_rdma_ctrlr(ctrlr);
24725ac814e3SSeth Howell 	struct nvme_rdma_cm_event_entry *entry;
2473246c39a7SZiye Yang 
2474a9e43691SDaniel Verkamp 	if (ctrlr->adminq) {
2475b2225ff5SSeth Howell 		nvme_rdma_ctrlr_delete_io_qpair(ctrlr, ctrlr->adminq);
2476a9e43691SDaniel Verkamp 	}
2477a9e43691SDaniel Verkamp 
24785ac814e3SSeth Howell 	STAILQ_FOREACH(entry, &rctrlr->pending_cm_events, link) {
24795ac814e3SSeth Howell 		rdma_ack_cm_event(entry->evt);
24805ac814e3SSeth Howell 	}
24815ac814e3SSeth Howell 
24825ac814e3SSeth Howell 	STAILQ_INIT(&rctrlr->free_cm_events);
24835ac814e3SSeth Howell 	STAILQ_INIT(&rctrlr->pending_cm_events);
2484c66b68e9SAleksey Marchuk 	spdk_free(rctrlr->cm_events);
24855ac814e3SSeth Howell 
24865ac814e3SSeth Howell 	if (rctrlr->cm_channel) {
24875ac814e3SSeth Howell 		rdma_destroy_event_channel(rctrlr->cm_channel);
24885ac814e3SSeth Howell 		rctrlr->cm_channel = NULL;
24895ac814e3SSeth Howell 	}
24905ac814e3SSeth Howell 
2491c83cd937SEhud Naim 	nvme_ctrlr_destruct_finish(ctrlr);
2492c83cd937SEhud Naim 
2493c66b68e9SAleksey Marchuk 	spdk_free(rctrlr);
2494246c39a7SZiye Yang 
2495246c39a7SZiye Yang 	return 0;
2496246c39a7SZiye Yang }
2497246c39a7SZiye Yang 
2498cc533a3eSAlexey Marchuk static inline int
2499cc533a3eSAlexey Marchuk _nvme_rdma_qpair_submit_request(struct nvme_rdma_qpair *rqpair,
2500cc533a3eSAlexey Marchuk 				struct spdk_nvme_rdma_req *rdma_req)
2501cc533a3eSAlexey Marchuk {
2502cc533a3eSAlexey Marchuk 	struct spdk_nvme_qpair *qpair = &rqpair->qpair;
2503cc533a3eSAlexey Marchuk 	struct ibv_send_wr *wr;
2504cc533a3eSAlexey Marchuk 	struct nvme_rdma_poll_group *group;
2505cc533a3eSAlexey Marchuk 
2506cc533a3eSAlexey Marchuk 	if (!rqpair->link_active.tqe_prev && qpair->poll_group) {
2507cc533a3eSAlexey Marchuk 		group = nvme_rdma_poll_group(qpair->poll_group);
2508cc533a3eSAlexey Marchuk 		TAILQ_INSERT_TAIL(&group->active_qpairs, rqpair, link_active);
2509cc533a3eSAlexey Marchuk 	}
2510cc533a3eSAlexey Marchuk 	assert(rqpair->current_num_sends < rqpair->num_entries);
2511cc533a3eSAlexey Marchuk 	rqpair->current_num_sends++;
2512cc533a3eSAlexey Marchuk 
2513cc533a3eSAlexey Marchuk 	wr = &rdma_req->send_wr;
2514cc533a3eSAlexey Marchuk 	wr->next = NULL;
2515cc533a3eSAlexey Marchuk 	nvme_rdma_trace_ibv_sge(wr->sg_list);
2516cc533a3eSAlexey Marchuk 
2517cc533a3eSAlexey Marchuk 	spdk_rdma_provider_qp_queue_send_wrs(rqpair->rdma_qp, wr);
2518cc533a3eSAlexey Marchuk 
2519cc533a3eSAlexey Marchuk 	if (!rqpair->delay_cmd_submit) {
2520cc533a3eSAlexey Marchuk 		return nvme_rdma_qpair_submit_sends(rqpair);
2521cc533a3eSAlexey Marchuk 	}
2522cc533a3eSAlexey Marchuk 
2523cc533a3eSAlexey Marchuk 	return 0;
2524cc533a3eSAlexey Marchuk }
2525cc533a3eSAlexey Marchuk 
252654a022ddSBen Walker static int
2527246c39a7SZiye Yang nvme_rdma_qpair_submit_request(struct spdk_nvme_qpair *qpair,
2528246c39a7SZiye Yang 			       struct nvme_request *req)
2529246c39a7SZiye Yang {
2530246c39a7SZiye Yang 	struct nvme_rdma_qpair *rqpair;
2531246c39a7SZiye Yang 	struct spdk_nvme_rdma_req *rdma_req;
2532f0e4b91fSAlexey Marchuk 	int rc;
2533246c39a7SZiye Yang 
2534246c39a7SZiye Yang 	rqpair = nvme_rdma_qpair(qpair);
253590f13aa6SZiye Yang 	assert(rqpair != NULL);
253690f13aa6SZiye Yang 	assert(req != NULL);
2537ee827136SDaniel Verkamp 
2538ee827136SDaniel Verkamp 	rdma_req = nvme_rdma_req_get(rqpair);
253950569293SAlexey Marchuk 	if (spdk_unlikely(!rdma_req)) {
254050569293SAlexey Marchuk 		if (rqpair->poller) {
254150569293SAlexey Marchuk 			rqpair->poller->stats.queued_requests++;
254250569293SAlexey Marchuk 		}
25437630daa2SSeth Howell 		/* Inform the upper layer to try again later. */
25447630daa2SSeth Howell 		return -EAGAIN;
2545ee827136SDaniel Verkamp 	}
2546ee827136SDaniel Verkamp 
254751bde662SAlexey Marchuk 	assert(rdma_req->req == NULL);
254851bde662SAlexey Marchuk 	rdma_req->req = req;
254951bde662SAlexey Marchuk 	req->cmd.cid = rdma_req->id;
2550cec5ba28SAlexey Marchuk 	if (req->accel_sequence || rqpair->append_copy) {
25512c140f58SAlexey Marchuk 		assert(spdk_rdma_provider_accel_sequence_supported());
25522c140f58SAlexey Marchuk 		assert(rqpair->qpair.poll_group->group);
25532c140f58SAlexey Marchuk 		assert(rqpair->qpair.poll_group->group->accel_fn_table.append_copy);
25542c140f58SAlexey Marchuk 		assert(rqpair->qpair.poll_group->group->accel_fn_table.reverse_sequence);
25552c140f58SAlexey Marchuk 		assert(rqpair->qpair.poll_group->group->accel_fn_table.finish_sequence);
25562c140f58SAlexey Marchuk 
25572c140f58SAlexey Marchuk 		rc = nvme_rdma_apply_accel_sequence(rqpair, req, rdma_req);
25582c140f58SAlexey Marchuk 		if (spdk_unlikely(rc)) {
25592c140f58SAlexey Marchuk 			SPDK_ERRLOG("failed to apply accel seq, rqpair %p, req %p, rc %d\n", rqpair, rdma_req, rc);
25602c140f58SAlexey Marchuk 			nvme_rdma_req_put(rqpair, rdma_req);
25612c140f58SAlexey Marchuk 			return rc;
25622c140f58SAlexey Marchuk 		}
25632c140f58SAlexey Marchuk 		/* Capsule will be sent in data_transfer callback */
25642c140f58SAlexey Marchuk 		return 0;
25652c140f58SAlexey Marchuk 	}
25662c140f58SAlexey Marchuk 
2567f0e4b91fSAlexey Marchuk 	rc = nvme_rdma_req_init(rqpair, rdma_req);
2568f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(rc)) {
2569ee827136SDaniel Verkamp 		SPDK_ERRLOG("nvme_rdma_req_init() failed\n");
2570aa2ea2beSShuhei Matsumoto 		nvme_rdma_req_put(rqpair, rdma_req);
2571246c39a7SZiye Yang 		return -1;
2572246c39a7SZiye Yang 	}
2573246c39a7SZiye Yang 
25741e90edaaSShuhei Matsumoto 	TAILQ_INSERT_TAIL(&rqpair->outstanding_reqs, rdma_req, link);
2575fa457d63SShuhei Matsumoto 	rqpair->num_outstanding_reqs++;
2576fa457d63SShuhei Matsumoto 
2577cc533a3eSAlexey Marchuk 	return _nvme_rdma_qpair_submit_request(rqpair, rdma_req);
2578246c39a7SZiye Yang }
2579246c39a7SZiye Yang 
258054a022ddSBen Walker static int
2581246c39a7SZiye Yang nvme_rdma_qpair_reset(struct spdk_nvme_qpair *qpair)
2582246c39a7SZiye Yang {
2583246c39a7SZiye Yang 	/* Currently, doing nothing here */
2584246c39a7SZiye Yang 	return 0;
2585246c39a7SZiye Yang }
2586246c39a7SZiye Yang 
258754a022ddSBen Walker static void
2588783a2a20SJim Harris nvme_rdma_qpair_abort_reqs(struct spdk_nvme_qpair *qpair, uint32_t dnr)
2589246c39a7SZiye Yang {
25908b4534e4SSeth Howell 	struct spdk_nvme_rdma_req *rdma_req, *tmp;
25918b4534e4SSeth Howell 	struct spdk_nvme_cpl cpl;
25928b4534e4SSeth Howell 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
25938b4534e4SSeth Howell 
25947413e1e4SShuhei Matsumoto 	cpl.sqid = qpair->id;
2595d9e865a8SChangpeng Liu 	cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
25968b4534e4SSeth Howell 	cpl.status.sct = SPDK_NVME_SCT_GENERIC;
2597783a2a20SJim Harris 	cpl.status.dnr = dnr;
25988b4534e4SSeth Howell 
25996d18ea42SSeth Howell 	/*
26006d18ea42SSeth Howell 	 * We cannot abort requests at the RDMA layer without
26016d18ea42SSeth Howell 	 * unregistering them. If we do, we can still get error
26026d18ea42SSeth Howell 	 * free completions on the shared completion queue.
26036d18ea42SSeth Howell 	 */
26046d18ea42SSeth Howell 	if (nvme_qpair_get_state(qpair) > NVME_QPAIR_DISCONNECTING &&
26056d18ea42SSeth Howell 	    nvme_qpair_get_state(qpair) != NVME_QPAIR_DESTROYING) {
26066d18ea42SSeth Howell 		nvme_ctrlr_disconnect_qpair(qpair);
26076d18ea42SSeth Howell 	}
26086d18ea42SSeth Howell 
26098b4534e4SSeth Howell 	TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
26102c140f58SAlexey Marchuk 		if (rdma_req->in_progress_accel) {
26112c140f58SAlexey Marchuk 			/* We should wait for accel completion */
26122c140f58SAlexey Marchuk 			continue;
26132c140f58SAlexey Marchuk 		}
2614e415bf00SJim Harris 		nvme_rdma_req_complete(rdma_req, &cpl, true);
26158b4534e4SSeth Howell 	}
2616246c39a7SZiye Yang }
2617246c39a7SZiye Yang 
26186b504fdaSDaniel Verkamp static void
26196b504fdaSDaniel Verkamp nvme_rdma_qpair_check_timeout(struct spdk_nvme_qpair *qpair)
26206b504fdaSDaniel Verkamp {
26216b504fdaSDaniel Verkamp 	uint64_t t02;
26226b504fdaSDaniel Verkamp 	struct spdk_nvme_rdma_req *rdma_req, *tmp;
26236b504fdaSDaniel Verkamp 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
26246b504fdaSDaniel Verkamp 	struct spdk_nvme_ctrlr *ctrlr = qpair->ctrlr;
26256b504fdaSDaniel Verkamp 	struct spdk_nvme_ctrlr_process *active_proc;
26266b504fdaSDaniel Verkamp 
26276b504fdaSDaniel Verkamp 	/* Don't check timeouts during controller initialization. */
26286b504fdaSDaniel Verkamp 	if (ctrlr->state != NVME_CTRLR_STATE_READY) {
26296b504fdaSDaniel Verkamp 		return;
26306b504fdaSDaniel Verkamp 	}
26316b504fdaSDaniel Verkamp 
26326b504fdaSDaniel Verkamp 	if (nvme_qpair_is_admin_queue(qpair)) {
26331a9c19a9SSeth Howell 		active_proc = nvme_ctrlr_get_current_process(ctrlr);
26346b504fdaSDaniel Verkamp 	} else {
26356b504fdaSDaniel Verkamp 		active_proc = qpair->active_proc;
26366b504fdaSDaniel Verkamp 	}
26376b504fdaSDaniel Verkamp 
26386b504fdaSDaniel Verkamp 	/* Only check timeouts if the current process has a timeout callback. */
26396b504fdaSDaniel Verkamp 	if (active_proc == NULL || active_proc->timeout_cb_fn == NULL) {
26406b504fdaSDaniel Verkamp 		return;
26416b504fdaSDaniel Verkamp 	}
26426b504fdaSDaniel Verkamp 
26436b504fdaSDaniel Verkamp 	t02 = spdk_get_ticks();
26446b504fdaSDaniel Verkamp 	TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
26456b504fdaSDaniel Verkamp 		assert(rdma_req->req != NULL);
26466b504fdaSDaniel Verkamp 
26476b504fdaSDaniel Verkamp 		if (nvme_request_check_timeout(rdma_req->req, rdma_req->id, active_proc, t02)) {
26486b504fdaSDaniel Verkamp 			/*
26496b504fdaSDaniel Verkamp 			 * The requests are in order, so as soon as one has not timed out,
26506b504fdaSDaniel Verkamp 			 * stop iterating.
26516b504fdaSDaniel Verkamp 			 */
26526b504fdaSDaniel Verkamp 			break;
26536b504fdaSDaniel Verkamp 		}
26546b504fdaSDaniel Verkamp 	}
26556b504fdaSDaniel Verkamp }
26566b504fdaSDaniel Verkamp 
2657ecd9234dSShuhei Matsumoto static inline void
2658581e1bb5SAlexey Marchuk nvme_rdma_request_ready(struct nvme_rdma_qpair *rqpair, struct spdk_nvme_rdma_req *rdma_req)
2659581e1bb5SAlexey Marchuk {
2660851a8dfeSShuhei Matsumoto 	struct spdk_nvme_rdma_rsp *rdma_rsp = rdma_req->rdma_rsp;
26616275f844SShuhei Matsumoto 	struct ibv_recv_wr *recv_wr = rdma_rsp->recv_wr;
2662851a8dfeSShuhei Matsumoto 
26632c140f58SAlexey Marchuk 	if (rdma_req->transfer_cpl_cb) {
26642c140f58SAlexey Marchuk 		int rc = 0;
26652c140f58SAlexey Marchuk 
26662c140f58SAlexey Marchuk 		if (spdk_unlikely(spdk_nvme_cpl_is_error(&rdma_rsp->cpl))) {
26672c140f58SAlexey Marchuk 			SPDK_WARNLOG("req %p, error cpl sct %d, sc %d\n", rdma_req, rdma_rsp->cpl.status.sct,
26682c140f58SAlexey Marchuk 				     rdma_rsp->cpl.status.sc);
26692c140f58SAlexey Marchuk 			rc = -EIO;
26702c140f58SAlexey Marchuk 		}
26712c140f58SAlexey Marchuk 		nvme_rdma_finish_data_transfer(rdma_req, rc);
26722c140f58SAlexey Marchuk 	} else {
2673851a8dfeSShuhei Matsumoto 		nvme_rdma_req_complete(rdma_req, &rdma_rsp->cpl, true);
26742c140f58SAlexey Marchuk 	}
26756275f844SShuhei Matsumoto 
26762b867217SAlex Michon 	if (spdk_unlikely(rqpair->state >= NVME_RDMA_QPAIR_STATE_EXITING && !rqpair->srq)) {
26772b867217SAlex Michon 		/* Skip posting back recv wr if we are in a disconnection process. We may never get
26782b867217SAlex Michon 		 * a WC and we may end up stuck in LINGERING state until the timeout. */
26792b867217SAlex Michon 		return;
26802b867217SAlex Michon 	}
26812b867217SAlex Michon 
26824999a985SShuhei Matsumoto 	assert(rqpair->rsps->current_num_recvs < rqpair->rsps->num_entries);
26834999a985SShuhei Matsumoto 	rqpair->rsps->current_num_recvs++;
2684cd640f62SShuhei Matsumoto 
26856275f844SShuhei Matsumoto 	recv_wr->next = NULL;
26866275f844SShuhei Matsumoto 	nvme_rdma_trace_ibv_sge(recv_wr->sg_list);
2687cd640f62SShuhei Matsumoto 
2688bcd987eaSShuhei Matsumoto 	if (!rqpair->srq) {
2689cf151d60SAlexey Marchuk 		spdk_rdma_provider_qp_queue_recv_wrs(rqpair->rdma_qp, recv_wr);
2690bcd987eaSShuhei Matsumoto 	} else {
2691cf151d60SAlexey Marchuk 		spdk_rdma_provider_srq_queue_recv_wrs(rqpair->srq, recv_wr);
2692bcd987eaSShuhei Matsumoto 	}
2693581e1bb5SAlexey Marchuk }
2694581e1bb5SAlexey Marchuk 
2695eb2ec1b0SBen Walker #define MAX_COMPLETIONS_PER_POLL 128
2696eb2ec1b0SBen Walker 
269763732d88SSeth Howell static void
269863732d88SSeth Howell nvme_rdma_fail_qpair(struct spdk_nvme_qpair *qpair, int failure_reason)
269963732d88SSeth Howell {
270063732d88SSeth Howell 	if (failure_reason == IBV_WC_RETRY_EXC_ERR) {
270163732d88SSeth Howell 		qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_REMOTE;
270263732d88SSeth Howell 	} else if (qpair->transport_failure_reason == SPDK_NVME_QPAIR_FAILURE_NONE) {
270363732d88SSeth Howell 		qpair->transport_failure_reason = SPDK_NVME_QPAIR_FAILURE_UNKNOWN;
270463732d88SSeth Howell 	}
270563732d88SSeth Howell 
270663732d88SSeth Howell 	nvme_ctrlr_disconnect_qpair(qpair);
270763732d88SSeth Howell }
270863732d88SSeth Howell 
270919404724SShuhei Matsumoto static struct nvme_rdma_qpair *
271019404724SShuhei Matsumoto get_rdma_qpair_from_wc(struct nvme_rdma_poll_group *group, struct ibv_wc *wc)
271119404724SShuhei Matsumoto {
271219404724SShuhei Matsumoto 	struct spdk_nvme_qpair *qpair;
271319404724SShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair;
271419404724SShuhei Matsumoto 
271519404724SShuhei Matsumoto 	STAILQ_FOREACH(qpair, &group->group.connected_qpairs, poll_group_stailq) {
271619404724SShuhei Matsumoto 		rqpair = nvme_rdma_qpair(qpair);
271719404724SShuhei Matsumoto 		if (NVME_RDMA_POLL_GROUP_CHECK_QPN(rqpair, wc->qp_num)) {
271819404724SShuhei Matsumoto 			return rqpair;
271919404724SShuhei Matsumoto 		}
272019404724SShuhei Matsumoto 	}
272119404724SShuhei Matsumoto 
272219404724SShuhei Matsumoto 	STAILQ_FOREACH(qpair, &group->group.disconnected_qpairs, poll_group_stailq) {
272319404724SShuhei Matsumoto 		rqpair = nvme_rdma_qpair(qpair);
272419404724SShuhei Matsumoto 		if (NVME_RDMA_POLL_GROUP_CHECK_QPN(rqpair, wc->qp_num)) {
272519404724SShuhei Matsumoto 			return rqpair;
272619404724SShuhei Matsumoto 		}
272719404724SShuhei Matsumoto 	}
272819404724SShuhei Matsumoto 
272919404724SShuhei Matsumoto 	return NULL;
273019404724SShuhei Matsumoto }
273119404724SShuhei Matsumoto 
2732abc45c46SAlexey Marchuk static inline void
2733abc45c46SAlexey Marchuk nvme_rdma_log_wc_status(struct nvme_rdma_qpair *rqpair, struct ibv_wc *wc)
2734abc45c46SAlexey Marchuk {
2735abc45c46SAlexey Marchuk 	struct nvme_rdma_wr *rdma_wr = (struct nvme_rdma_wr *)wc->wr_id;
2736abc45c46SAlexey Marchuk 
2737abc45c46SAlexey Marchuk 	if (wc->status == IBV_WC_WR_FLUSH_ERR) {
2738abc45c46SAlexey Marchuk 		/* If qpair is in ERR state, we will receive completions for all posted and not completed
2739abc45c46SAlexey Marchuk 		 * Work Requests with IBV_WC_WR_FLUSH_ERR status. Don't log an error in that case */
2740c8f986c7SShuhei Matsumoto 		SPDK_DEBUGLOG(nvme, "WC error, qid %u, qp state %d, request 0x%lu type %d, status: (%d): %s\n",
2741abc45c46SAlexey Marchuk 			      rqpair->qpair.id, rqpair->qpair.state, wc->wr_id, rdma_wr->type, wc->status,
2742abc45c46SAlexey Marchuk 			      ibv_wc_status_str(wc->status));
2743abc45c46SAlexey Marchuk 	} else {
2744c8f986c7SShuhei Matsumoto 		SPDK_ERRLOG("WC error, qid %u, qp state %d, request 0x%lu type %d, status: (%d): %s\n",
2745abc45c46SAlexey Marchuk 			    rqpair->qpair.id, rqpair->qpair.state, wc->wr_id, rdma_wr->type, wc->status,
2746abc45c46SAlexey Marchuk 			    ibv_wc_status_str(wc->status));
2747abc45c46SAlexey Marchuk 	}
2748abc45c46SAlexey Marchuk }
2749abc45c46SAlexey Marchuk 
2750cce99060SShuhei Matsumoto static inline int
2751bcd987eaSShuhei Matsumoto nvme_rdma_process_recv_completion(struct nvme_rdma_poller *poller, struct ibv_wc *wc,
2752bcd987eaSShuhei Matsumoto 				  struct nvme_rdma_wr *rdma_wr)
2753cce99060SShuhei Matsumoto {
2754cce99060SShuhei Matsumoto 	struct nvme_rdma_qpair		*rqpair;
2755cce99060SShuhei Matsumoto 	struct spdk_nvme_rdma_req	*rdma_req;
2756cce99060SShuhei Matsumoto 	struct spdk_nvme_rdma_rsp	*rdma_rsp;
2757cce99060SShuhei Matsumoto 
2758cce99060SShuhei Matsumoto 	rdma_rsp = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvme_rdma_rsp, rdma_wr);
2759bcd987eaSShuhei Matsumoto 
2760bcd987eaSShuhei Matsumoto 	if (poller && poller->srq) {
2761bcd987eaSShuhei Matsumoto 		rqpair = get_rdma_qpair_from_wc(poller->group, wc);
2762bcd987eaSShuhei Matsumoto 		if (spdk_unlikely(!rqpair)) {
2763bcd987eaSShuhei Matsumoto 			/* Since we do not handle the LAST_WQE_REACHED event, we do not know when
2764bcd987eaSShuhei Matsumoto 			 * a Receive Queue in a QP, that is associated with an SRQ, is flushed.
2765bcd987eaSShuhei Matsumoto 			 * We may get a WC for a already destroyed QP.
2766bcd987eaSShuhei Matsumoto 			 *
2767bcd987eaSShuhei Matsumoto 			 * However, for the SRQ, this is not any error. Hence, just re-post the
2768bcd987eaSShuhei Matsumoto 			 * receive request to the SRQ to reuse for other QPs, and return 0.
2769bcd987eaSShuhei Matsumoto 			 */
2770cf151d60SAlexey Marchuk 			spdk_rdma_provider_srq_queue_recv_wrs(poller->srq, rdma_rsp->recv_wr);
2771bcd987eaSShuhei Matsumoto 			return 0;
2772bcd987eaSShuhei Matsumoto 		}
2773bcd987eaSShuhei Matsumoto 	} else {
2774cce99060SShuhei Matsumoto 		rqpair = rdma_rsp->rqpair;
2775bbd3d96bSShuhei Matsumoto 		if (spdk_unlikely(!rqpair)) {
2776bbd3d96bSShuhei Matsumoto 			/* TODO: Fix forceful QP destroy when it is not async mode.
2777bbd3d96bSShuhei Matsumoto 			 * CQ itself did not cause any error. Hence, return 0 for now.
2778bbd3d96bSShuhei Matsumoto 			 */
2779bbd3d96bSShuhei Matsumoto 			SPDK_WARNLOG("QP might be already destroyed.\n");
2780bbd3d96bSShuhei Matsumoto 			return 0;
2781bcd987eaSShuhei Matsumoto 		}
2782bbd3d96bSShuhei Matsumoto 	}
2783bbd3d96bSShuhei Matsumoto 
2784bcd987eaSShuhei Matsumoto 
27854999a985SShuhei Matsumoto 	assert(rqpair->rsps->current_num_recvs > 0);
27864999a985SShuhei Matsumoto 	rqpair->rsps->current_num_recvs--;
2787cce99060SShuhei Matsumoto 
2788f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(wc->status)) {
2789cce99060SShuhei Matsumoto 		nvme_rdma_log_wc_status(rqpair, wc);
2790bcd987eaSShuhei Matsumoto 		goto err_wc;
2791cce99060SShuhei Matsumoto 	}
2792cce99060SShuhei Matsumoto 
2793cce99060SShuhei Matsumoto 	SPDK_DEBUGLOG(nvme, "CQ recv completion\n");
2794cce99060SShuhei Matsumoto 
2795f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(wc->byte_len < sizeof(struct spdk_nvme_cpl))) {
2796cce99060SShuhei Matsumoto 		SPDK_ERRLOG("recv length %u less than expected response size\n", wc->byte_len);
2797bcd987eaSShuhei Matsumoto 		goto err_wc;
2798cce99060SShuhei Matsumoto 	}
2799cce99060SShuhei Matsumoto 	rdma_req = &rqpair->rdma_reqs[rdma_rsp->cpl.cid];
2800cce99060SShuhei Matsumoto 	rdma_req->completion_flags |= NVME_RDMA_RECV_COMPLETED;
2801851a8dfeSShuhei Matsumoto 	rdma_req->rdma_rsp = rdma_rsp;
2802cce99060SShuhei Matsumoto 
2803cce99060SShuhei Matsumoto 	if ((rdma_req->completion_flags & NVME_RDMA_SEND_COMPLETED) == 0) {
2804cce99060SShuhei Matsumoto 		return 0;
2805cce99060SShuhei Matsumoto 	}
2806cce99060SShuhei Matsumoto 
2807b659997eSShuhei Matsumoto 	rqpair->num_completions++;
2808b659997eSShuhei Matsumoto 
2809ecd9234dSShuhei Matsumoto 	nvme_rdma_request_ready(rqpair, rdma_req);
2810ecd9234dSShuhei Matsumoto 
2811ecd9234dSShuhei Matsumoto 	if (!rqpair->delay_cmd_submit) {
2812ecd9234dSShuhei Matsumoto 		if (spdk_unlikely(nvme_rdma_qpair_submit_recvs(rqpair))) {
2813cce99060SShuhei Matsumoto 			SPDK_ERRLOG("Unable to re-post rx descriptor\n");
2814cce99060SShuhei Matsumoto 			nvme_rdma_fail_qpair(&rqpair->qpair, 0);
2815cce99060SShuhei Matsumoto 			return -ENXIO;
2816cce99060SShuhei Matsumoto 		}
2817ecd9234dSShuhei Matsumoto 	}
2818ecd9234dSShuhei Matsumoto 
2819cce99060SShuhei Matsumoto 	return 1;
2820bcd987eaSShuhei Matsumoto 
2821bcd987eaSShuhei Matsumoto err_wc:
2822bcd987eaSShuhei Matsumoto 	nvme_rdma_fail_qpair(&rqpair->qpair, 0);
2823bcd987eaSShuhei Matsumoto 	if (poller && poller->srq) {
2824cf151d60SAlexey Marchuk 		spdk_rdma_provider_srq_queue_recv_wrs(poller->srq, rdma_rsp->recv_wr);
2825bcd987eaSShuhei Matsumoto 	}
28262c140f58SAlexey Marchuk 	rdma_req = &rqpair->rdma_reqs[rdma_rsp->cpl.cid];
28272c140f58SAlexey Marchuk 	if (rdma_req->transfer_cpl_cb) {
28282c140f58SAlexey Marchuk 		nvme_rdma_finish_data_transfer(rdma_req, -ENXIO);
28292c140f58SAlexey Marchuk 	}
2830bcd987eaSShuhei Matsumoto 	return -ENXIO;
2831cce99060SShuhei Matsumoto }
2832cce99060SShuhei Matsumoto 
2833cce99060SShuhei Matsumoto static inline int
2834cce99060SShuhei Matsumoto nvme_rdma_process_send_completion(struct nvme_rdma_poller *poller,
2835cce99060SShuhei Matsumoto 				  struct nvme_rdma_qpair *rdma_qpair,
2836cce99060SShuhei Matsumoto 				  struct ibv_wc *wc, struct nvme_rdma_wr *rdma_wr)
2837cce99060SShuhei Matsumoto {
2838cce99060SShuhei Matsumoto 	struct nvme_rdma_qpair		*rqpair;
2839cce99060SShuhei Matsumoto 	struct spdk_nvme_rdma_req	*rdma_req;
2840cce99060SShuhei Matsumoto 
2841cce99060SShuhei Matsumoto 	rdma_req = SPDK_CONTAINEROF(rdma_wr, struct spdk_nvme_rdma_req, rdma_wr);
2842cce99060SShuhei Matsumoto 	rqpair = rdma_req->req ? nvme_rdma_qpair(rdma_req->req->qpair) : NULL;
2843f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(!rqpair)) {
2844cce99060SShuhei Matsumoto 		rqpair = rdma_qpair != NULL ? rdma_qpair : get_rdma_qpair_from_wc(poller->group, wc);
2845cce99060SShuhei Matsumoto 	}
2846e44d6317Ssijie.sun 
2847e44d6317Ssijie.sun 	/* If we are flushing I/O */
2848f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(wc->status)) {
2849cce99060SShuhei Matsumoto 		if (!rqpair) {
2850cce99060SShuhei Matsumoto 			/* When poll_group is used, several qpairs share the same CQ and it is possible to
2851cce99060SShuhei Matsumoto 			 * receive a completion with error (e.g. IBV_WC_WR_FLUSH_ERR) for already disconnected qpair
2852cce99060SShuhei Matsumoto 			 * That happens due to qpair is destroyed while there are submitted but not completed send/receive
2853cce99060SShuhei Matsumoto 			 * Work Requests */
2854cce99060SShuhei Matsumoto 			assert(poller);
2855cce99060SShuhei Matsumoto 			return 0;
2856cce99060SShuhei Matsumoto 		}
2857cce99060SShuhei Matsumoto 		assert(rqpair->current_num_sends > 0);
2858cce99060SShuhei Matsumoto 		rqpair->current_num_sends--;
2859cce99060SShuhei Matsumoto 		nvme_rdma_log_wc_status(rqpair, wc);
2860cce99060SShuhei Matsumoto 		nvme_rdma_fail_qpair(&rqpair->qpair, 0);
2861bcd987eaSShuhei Matsumoto 		if (rdma_req->rdma_rsp && poller && poller->srq) {
2862cf151d60SAlexey Marchuk 			spdk_rdma_provider_srq_queue_recv_wrs(poller->srq, rdma_req->rdma_rsp->recv_wr);
2863bcd987eaSShuhei Matsumoto 		}
28642c140f58SAlexey Marchuk 		if (rdma_req->transfer_cpl_cb) {
28652c140f58SAlexey Marchuk 			nvme_rdma_finish_data_transfer(rdma_req, -ENXIO);
28662c140f58SAlexey Marchuk 		}
2867cce99060SShuhei Matsumoto 		return -ENXIO;
2868cce99060SShuhei Matsumoto 	}
2869cce99060SShuhei Matsumoto 
2870cce99060SShuhei Matsumoto 	/* We do not support Soft Roce anymore. Other than Soft Roce's bug, we should not
2871cce99060SShuhei Matsumoto 	 * receive a completion without error status after qpair is disconnected/destroyed.
2872cce99060SShuhei Matsumoto 	 */
2873e44d6317Ssijie.sun 	if (spdk_unlikely(rdma_req->req == NULL)) {
2874e44d6317Ssijie.sun 		/*
2875e44d6317Ssijie.sun 		 * Some infiniband drivers do not guarantee the previous assumption after we
2876e44d6317Ssijie.sun 		 * received a RDMA_CM_EVENT_DEVICE_REMOVAL event.
2877e44d6317Ssijie.sun 		 */
2878e44d6317Ssijie.sun 		SPDK_ERRLOG("Received malformed completion: request 0x%"PRIx64" type %d\n", wc->wr_id,
2879e44d6317Ssijie.sun 			    rdma_wr->type);
2880e44d6317Ssijie.sun 		if (!rqpair || !rqpair->need_destroy) {
2881e44d6317Ssijie.sun 			assert(0);
2882e44d6317Ssijie.sun 		}
2883e44d6317Ssijie.sun 		return -ENXIO;
2884e44d6317Ssijie.sun 	}
2885cce99060SShuhei Matsumoto 
2886cce99060SShuhei Matsumoto 	rdma_req->completion_flags |= NVME_RDMA_SEND_COMPLETED;
2887cce99060SShuhei Matsumoto 	assert(rqpair->current_num_sends > 0);
2888cce99060SShuhei Matsumoto 	rqpair->current_num_sends--;
2889cce99060SShuhei Matsumoto 
2890cce99060SShuhei Matsumoto 	if ((rdma_req->completion_flags & NVME_RDMA_RECV_COMPLETED) == 0) {
2891cce99060SShuhei Matsumoto 		return 0;
2892cce99060SShuhei Matsumoto 	}
2893cce99060SShuhei Matsumoto 
2894b659997eSShuhei Matsumoto 	rqpair->num_completions++;
2895b659997eSShuhei Matsumoto 
2896ecd9234dSShuhei Matsumoto 	nvme_rdma_request_ready(rqpair, rdma_req);
2897ecd9234dSShuhei Matsumoto 
2898ecd9234dSShuhei Matsumoto 	if (!rqpair->delay_cmd_submit) {
2899ecd9234dSShuhei Matsumoto 		if (spdk_unlikely(nvme_rdma_qpair_submit_recvs(rqpair))) {
2900cce99060SShuhei Matsumoto 			SPDK_ERRLOG("Unable to re-post rx descriptor\n");
2901cce99060SShuhei Matsumoto 			nvme_rdma_fail_qpair(&rqpair->qpair, 0);
2902cce99060SShuhei Matsumoto 			return -ENXIO;
2903cce99060SShuhei Matsumoto 		}
2904ecd9234dSShuhei Matsumoto 	}
2905ecd9234dSShuhei Matsumoto 
2906cce99060SShuhei Matsumoto 	return 1;
2907cce99060SShuhei Matsumoto }
2908cce99060SShuhei Matsumoto 
2909f0e4b91fSAlexey Marchuk static inline int
29108bef6f0bSSeth Howell nvme_rdma_cq_process_completions(struct ibv_cq *cq, uint32_t batch_size,
29111439f9c7SShuhei Matsumoto 				 struct nvme_rdma_poller *poller,
291250569293SAlexey Marchuk 				 struct nvme_rdma_qpair *rdma_qpair,
291350569293SAlexey Marchuk 				 uint64_t *rdma_completions)
291463732d88SSeth Howell {
291563732d88SSeth Howell 	struct ibv_wc			wc[MAX_COMPLETIONS_PER_POLL];
291663732d88SSeth Howell 	struct nvme_rdma_wr		*rdma_wr;
291763732d88SSeth Howell 	uint32_t			reaped = 0;
291863732d88SSeth Howell 	int				completion_rc = 0;
2919cce99060SShuhei Matsumoto 	int				rc, _rc, i;
292063732d88SSeth Howell 
292163732d88SSeth Howell 	rc = ibv_poll_cq(cq, batch_size, wc);
2922f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(rc < 0)) {
292363732d88SSeth Howell 		SPDK_ERRLOG("Error polling CQ! (%d): %s\n",
292463732d88SSeth Howell 			    errno, spdk_strerror(errno));
292563732d88SSeth Howell 		return -ECANCELED;
292663732d88SSeth Howell 	} else if (rc == 0) {
292763732d88SSeth Howell 		return 0;
292863732d88SSeth Howell 	}
292963732d88SSeth Howell 
293063732d88SSeth Howell 	for (i = 0; i < rc; i++) {
293163732d88SSeth Howell 		rdma_wr = (struct nvme_rdma_wr *)wc[i].wr_id;
293263732d88SSeth Howell 		switch (rdma_wr->type) {
293363732d88SSeth Howell 		case RDMA_WR_TYPE_RECV:
2934bcd987eaSShuhei Matsumoto 			_rc = nvme_rdma_process_recv_completion(poller, &wc[i], rdma_wr);
293563732d88SSeth Howell 			break;
293663732d88SSeth Howell 
293763732d88SSeth Howell 		case RDMA_WR_TYPE_SEND:
2938cce99060SShuhei Matsumoto 			_rc = nvme_rdma_process_send_completion(poller, rdma_qpair, &wc[i], rdma_wr);
293963732d88SSeth Howell 			break;
294063732d88SSeth Howell 
294163732d88SSeth Howell 		default:
294263732d88SSeth Howell 			SPDK_ERRLOG("Received an unexpected opcode on the CQ: %d\n", rdma_wr->type);
294363732d88SSeth Howell 			return -ECANCELED;
294463732d88SSeth Howell 		}
2945cce99060SShuhei Matsumoto 		if (spdk_likely(_rc >= 0)) {
2946cce99060SShuhei Matsumoto 			reaped += _rc;
2947cce99060SShuhei Matsumoto 		} else {
2948cce99060SShuhei Matsumoto 			completion_rc = _rc;
2949cce99060SShuhei Matsumoto 		}
295063732d88SSeth Howell 	}
295163732d88SSeth Howell 
295250569293SAlexey Marchuk 	*rdma_completions += rc;
295350569293SAlexey Marchuk 
2954f0e4b91fSAlexey Marchuk 	if (spdk_unlikely(completion_rc)) {
295563732d88SSeth Howell 		return completion_rc;
295663732d88SSeth Howell 	}
295763732d88SSeth Howell 
295863732d88SSeth Howell 	return reaped;
295963732d88SSeth Howell }
296063732d88SSeth Howell 
29618bef6f0bSSeth Howell static void
29628bef6f0bSSeth Howell dummy_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
29638bef6f0bSSeth Howell {
29648bef6f0bSSeth Howell 
29658bef6f0bSSeth Howell }
29668bef6f0bSSeth Howell 
296754a022ddSBen Walker static int
2968246c39a7SZiye Yang nvme_rdma_qpair_process_completions(struct spdk_nvme_qpair *qpair,
2969246c39a7SZiye Yang 				    uint32_t max_completions)
2970246c39a7SZiye Yang {
2971eb2ec1b0SBen Walker 	struct nvme_rdma_qpair		*rqpair = nvme_rdma_qpair(qpair);
29720c77cf90SShuhei Matsumoto 	struct nvme_rdma_ctrlr		*rctrlr = nvme_rdma_ctrlr(qpair->ctrlr);
297363732d88SSeth Howell 	int				rc = 0, batch_size;
2974ac9b92c8SBen Walker 	struct ibv_cq			*cq;
297550569293SAlexey Marchuk 	uint64_t			rdma_completions = 0;
2976246c39a7SZiye Yang 
29778bef6f0bSSeth Howell 	/*
29788bef6f0bSSeth Howell 	 * This is used during the connection phase. It's possible that we are still reaping error completions
29798bef6f0bSSeth Howell 	 * from other qpairs so we need to call the poll group function. Also, it's more correct since the cq
29808bef6f0bSSeth Howell 	 * is shared.
29818bef6f0bSSeth Howell 	 */
29828bef6f0bSSeth Howell 	if (qpair->poll_group != NULL) {
29838bef6f0bSSeth Howell 		return spdk_nvme_poll_group_process_completions(qpair->poll_group->group, max_completions,
29848bef6f0bSSeth Howell 				dummy_disconnected_qpair_cb);
29858bef6f0bSSeth Howell 	}
29868bef6f0bSSeth Howell 
2987eb2ec1b0SBen Walker 	if (max_completions == 0) {
2988eb2ec1b0SBen Walker 		max_completions = rqpair->num_entries;
2989246c39a7SZiye Yang 	} else {
299084d90484SDaniel Verkamp 		max_completions = spdk_min(max_completions, rqpair->num_entries);
2991246c39a7SZiye Yang 	}
2992246c39a7SZiye Yang 
29939717b0c3SShuhei Matsumoto 	switch (nvme_qpair_get_state(qpair)) {
29949717b0c3SShuhei Matsumoto 	case NVME_QPAIR_CONNECTING:
299529974dc8SShuhei Matsumoto 		rc = nvme_rdma_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
299629974dc8SShuhei Matsumoto 		if (rc == 0) {
299729974dc8SShuhei Matsumoto 			/* Once the connection is completed, we can submit queued requests */
299829974dc8SShuhei Matsumoto 			nvme_qpair_resubmit_requests(qpair, rqpair->num_entries);
299929974dc8SShuhei Matsumoto 		} else if (rc != -EAGAIN) {
300029974dc8SShuhei Matsumoto 			SPDK_ERRLOG("Failed to connect rqpair=%p\n", rqpair);
300129974dc8SShuhei Matsumoto 			goto failed;
30029717b0c3SShuhei Matsumoto 		} else if (rqpair->state <= NVME_RDMA_QPAIR_STATE_INITIALIZING) {
30039717b0c3SShuhei Matsumoto 			return 0;
300429974dc8SShuhei Matsumoto 		}
30059717b0c3SShuhei Matsumoto 		break;
300629974dc8SShuhei Matsumoto 
30079717b0c3SShuhei Matsumoto 	case NVME_QPAIR_DISCONNECTING:
30089717b0c3SShuhei Matsumoto 		nvme_rdma_ctrlr_disconnect_qpair_poll(qpair->ctrlr, qpair);
30099717b0c3SShuhei Matsumoto 		return -ENXIO;
30109717b0c3SShuhei Matsumoto 
30119717b0c3SShuhei Matsumoto 	default:
30120c77cf90SShuhei Matsumoto 		if (nvme_qpair_is_admin_queue(qpair)) {
30135ac814e3SSeth Howell 			nvme_rdma_poll_events(rctrlr);
30145ac814e3SSeth Howell 		}
30155ac814e3SSeth Howell 		nvme_rdma_qpair_process_cm_event(rqpair);
30169717b0c3SShuhei Matsumoto 		break;
30179717b0c3SShuhei Matsumoto 	}
30185ac814e3SSeth Howell 
30196338af34SSeth Howell 	if (spdk_unlikely(qpair->transport_failure_reason != SPDK_NVME_QPAIR_FAILURE_NONE)) {
30200c77cf90SShuhei Matsumoto 		goto failed;
30210a42e658SSeth Howell 	}
30220a42e658SSeth Howell 
3023ac9b92c8SBen Walker 	cq = rqpair->cq;
3024246c39a7SZiye Yang 
302563732d88SSeth Howell 	rqpair->num_completions = 0;
3026eb2ec1b0SBen Walker 	do {
302763732d88SSeth Howell 		batch_size = spdk_min((max_completions - rqpair->num_completions), MAX_COMPLETIONS_PER_POLL);
302850569293SAlexey Marchuk 		rc = nvme_rdma_cq_process_completions(cq, batch_size, NULL, rqpair, &rdma_completions);
302963732d88SSeth Howell 
303063732d88SSeth Howell 		if (rc == 0) {
3031246c39a7SZiye Yang 			break;
303263732d88SSeth Howell 			/* Handle the case where we fail to poll the cq. */
303363732d88SSeth Howell 		} else if (rc == -ECANCELED) {
30340c77cf90SShuhei Matsumoto 			goto failed;
303563732d88SSeth Howell 		} else if (rc == -ENXIO) {
303663732d88SSeth Howell 			return rc;
3037246c39a7SZiye Yang 		}
303863732d88SSeth Howell 	} while (rqpair->num_completions < max_completions);
3039eb2ec1b0SBen Walker 
30409b86f31aSAlexey Marchuk 	if (spdk_unlikely(nvme_rdma_qpair_submit_sends(rqpair) ||
30419b86f31aSAlexey Marchuk 			  nvme_rdma_qpair_submit_recvs(rqpair))) {
30420c77cf90SShuhei Matsumoto 		goto failed;
30439b86f31aSAlexey Marchuk 	}
3044c3d0a833SShuhei Matsumoto 
30450c77cf90SShuhei Matsumoto 	if (spdk_unlikely(qpair->ctrlr->timeout_enabled)) {
30466b504fdaSDaniel Verkamp 		nvme_rdma_qpair_check_timeout(qpair);
30476b504fdaSDaniel Verkamp 	}
30486b504fdaSDaniel Verkamp 
304963732d88SSeth Howell 	return rqpair->num_completions;
30500c77cf90SShuhei Matsumoto 
30510c77cf90SShuhei Matsumoto failed:
30520c77cf90SShuhei Matsumoto 	nvme_rdma_fail_qpair(qpair, 0);
30530c77cf90SShuhei Matsumoto 	return -ENXIO;
3054246c39a7SZiye Yang }
3055246c39a7SZiye Yang 
305654a022ddSBen Walker static uint32_t
3057246c39a7SZiye Yang nvme_rdma_ctrlr_get_max_xfer_size(struct spdk_nvme_ctrlr *ctrlr)
3058246c39a7SZiye Yang {
30598b539eb5SShuhei Matsumoto 	/* max_mr_size by ibv_query_device indicates the largest value that we can
30608b539eb5SShuhei Matsumoto 	 * set for a registered memory region.  It is independent from the actual
30618b539eb5SShuhei Matsumoto 	 * I/O size and is very likely to be larger than 2 MiB which is the
30628b539eb5SShuhei Matsumoto 	 * granularity we currently register memory regions.  Hence return
30638b539eb5SShuhei Matsumoto 	 * UINT32_MAX here and let the generic layer use the controller data to
30648b539eb5SShuhei Matsumoto 	 * moderate this value.
30658b539eb5SShuhei Matsumoto 	 */
30668b539eb5SShuhei Matsumoto 	return UINT32_MAX;
3067246c39a7SZiye Yang }
306898890613SDaniel Verkamp 
306954a022ddSBen Walker static uint16_t
3070002660c4SJim Harris nvme_rdma_ctrlr_get_max_sges(struct spdk_nvme_ctrlr *ctrlr)
3071002660c4SJim Harris {
30728b539eb5SShuhei Matsumoto 	struct nvme_rdma_ctrlr *rctrlr = nvme_rdma_ctrlr(ctrlr);
30735c80b1e5SEvgeniy Kochetov 	uint32_t max_sge = rctrlr->max_sge;
30745c80b1e5SEvgeniy Kochetov 	uint32_t max_in_capsule_sge = (ctrlr->cdata.nvmf_specific.ioccsz * 16 -
30755c80b1e5SEvgeniy Kochetov 				       sizeof(struct spdk_nvme_cmd)) /
30765c80b1e5SEvgeniy Kochetov 				      sizeof(struct spdk_nvme_sgl_descriptor);
30778b539eb5SShuhei Matsumoto 
30785c80b1e5SEvgeniy Kochetov 	/* Max SGE is limited by capsule size */
30795c80b1e5SEvgeniy Kochetov 	max_sge = spdk_min(max_sge, max_in_capsule_sge);
308062638991SAlexey Marchuk 	/* Max SGE may be limited by MSDBD.
308162638991SAlexey Marchuk 	 * If umr_per_io is enabled and supported, we always use virtually contig buffer, we don't limit max_sge by
308262638991SAlexey Marchuk 	 * MSDBD in that case */
308362638991SAlexey Marchuk 	if (!(g_spdk_nvme_transport_opts.rdma_umr_per_io &&
308462638991SAlexey Marchuk 	      spdk_rdma_provider_accel_sequence_supported()) &&
308562638991SAlexey Marchuk 	    ctrlr->cdata.nvmf_specific.msdbd != 0) {
30865c80b1e5SEvgeniy Kochetov 		max_sge = spdk_min(max_sge, ctrlr->cdata.nvmf_specific.msdbd);
30875c80b1e5SEvgeniy Kochetov 	}
30885c80b1e5SEvgeniy Kochetov 
30895c80b1e5SEvgeniy Kochetov 	/* Max SGE can't be less than 1 */
30905c80b1e5SEvgeniy Kochetov 	max_sge = spdk_max(1, max_sge);
30915c80b1e5SEvgeniy Kochetov 	return max_sge;
3092002660c4SJim Harris }
309338396397SDaniel Verkamp 
3094f2bd635eSShuhei Matsumoto static int
3095f2bd635eSShuhei Matsumoto nvme_rdma_qpair_iterate_requests(struct spdk_nvme_qpair *qpair,
3096f2bd635eSShuhei Matsumoto 				 int (*iter_fn)(struct nvme_request *req, void *arg),
3097f2bd635eSShuhei Matsumoto 				 void *arg)
3098f2bd635eSShuhei Matsumoto {
3099f2bd635eSShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
3100f2bd635eSShuhei Matsumoto 	struct spdk_nvme_rdma_req *rdma_req, *tmp;
3101f2bd635eSShuhei Matsumoto 	int rc;
3102f2bd635eSShuhei Matsumoto 
3103f2bd635eSShuhei Matsumoto 	assert(iter_fn != NULL);
3104f2bd635eSShuhei Matsumoto 
3105f2bd635eSShuhei Matsumoto 	TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
3106f2bd635eSShuhei Matsumoto 		assert(rdma_req->req != NULL);
3107f2bd635eSShuhei Matsumoto 
3108f2bd635eSShuhei Matsumoto 		rc = iter_fn(rdma_req->req, arg);
3109f2bd635eSShuhei Matsumoto 		if (rc != 0) {
3110f2bd635eSShuhei Matsumoto 			return rc;
3111f2bd635eSShuhei Matsumoto 		}
3112f2bd635eSShuhei Matsumoto 	}
3113f2bd635eSShuhei Matsumoto 
3114f2bd635eSShuhei Matsumoto 	return 0;
3115f2bd635eSShuhei Matsumoto }
3116f2bd635eSShuhei Matsumoto 
31174ddd77b2SKonrad Sztyber static int
31184ddd77b2SKonrad Sztyber nvme_rdma_qpair_authenticate(struct spdk_nvme_qpair *qpair)
31194ddd77b2SKonrad Sztyber {
31204ddd77b2SKonrad Sztyber 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
31214ddd77b2SKonrad Sztyber 	int rc;
31224ddd77b2SKonrad Sztyber 
31234ddd77b2SKonrad Sztyber 	/* If the qpair is still connecting, it'll be forced to authenticate later on */
31244ddd77b2SKonrad Sztyber 	if (rqpair->state < NVME_RDMA_QPAIR_STATE_RUNNING) {
31254ddd77b2SKonrad Sztyber 		return 0;
31264ddd77b2SKonrad Sztyber 	} else if (rqpair->state != NVME_RDMA_QPAIR_STATE_RUNNING) {
31274ddd77b2SKonrad Sztyber 		return -ENOTCONN;
31284ddd77b2SKonrad Sztyber 	}
31294ddd77b2SKonrad Sztyber 
31304ddd77b2SKonrad Sztyber 	rc = nvme_fabric_qpair_authenticate_async(qpair);
31314ddd77b2SKonrad Sztyber 	if (rc == 0) {
31324ddd77b2SKonrad Sztyber 		nvme_qpair_set_state(qpair, NVME_QPAIR_CONNECTING);
31334ddd77b2SKonrad Sztyber 		rqpair->state = NVME_RDMA_QPAIR_STATE_AUTHENTICATING;
31344ddd77b2SKonrad Sztyber 	}
31354ddd77b2SKonrad Sztyber 
31364ddd77b2SKonrad Sztyber 	return rc;
31374ddd77b2SKonrad Sztyber }
31384ddd77b2SKonrad Sztyber 
313954a022ddSBen Walker static void
3140f366e261SJim Harris nvme_rdma_admin_qpair_abort_aers(struct spdk_nvme_qpair *qpair)
3141f366e261SJim Harris {
3142f366e261SJim Harris 	struct spdk_nvme_rdma_req *rdma_req, *tmp;
3143f366e261SJim Harris 	struct spdk_nvme_cpl cpl;
3144f366e261SJim Harris 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
3145f366e261SJim Harris 
3146f366e261SJim Harris 	cpl.status.sc = SPDK_NVME_SC_ABORTED_SQ_DELETION;
3147f366e261SJim Harris 	cpl.status.sct = SPDK_NVME_SCT_GENERIC;
3148f366e261SJim Harris 
3149f366e261SJim Harris 	TAILQ_FOREACH_SAFE(rdma_req, &rqpair->outstanding_reqs, link, tmp) {
3150f366e261SJim Harris 		assert(rdma_req->req != NULL);
3151f366e261SJim Harris 
3152465b2f8aSShuhei Matsumoto 		if (rdma_req->req->cmd.opc != SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) {
3153a57aeac1SShuhei Matsumoto 			continue;
3154a57aeac1SShuhei Matsumoto 		}
3155a57aeac1SShuhei Matsumoto 
3156e415bf00SJim Harris 		nvme_rdma_req_complete(rdma_req, &cpl, false);
3157f366e261SJim Harris 	}
3158f366e261SJim Harris }
3159f366e261SJim Harris 
31606ea9de5fSShuhei Matsumoto static void
31616ea9de5fSShuhei Matsumoto nvme_rdma_poller_destroy(struct nvme_rdma_poller *poller)
31626ea9de5fSShuhei Matsumoto {
31636ea9de5fSShuhei Matsumoto 	if (poller->cq) {
31646ea9de5fSShuhei Matsumoto 		ibv_destroy_cq(poller->cq);
31656ea9de5fSShuhei Matsumoto 	}
3166bcd987eaSShuhei Matsumoto 	if (poller->rsps) {
3167bcd987eaSShuhei Matsumoto 		nvme_rdma_free_rsps(poller->rsps);
3168bcd987eaSShuhei Matsumoto 	}
3169bcd987eaSShuhei Matsumoto 	if (poller->srq) {
3170cf151d60SAlexey Marchuk 		spdk_rdma_provider_srq_destroy(poller->srq);
3171bcd987eaSShuhei Matsumoto 	}
3172bcd987eaSShuhei Matsumoto 	if (poller->mr_map) {
31738a01b4d6SAlexey Marchuk 		spdk_rdma_utils_free_mem_map(&poller->mr_map);
3174bcd987eaSShuhei Matsumoto 	}
3175bcd987eaSShuhei Matsumoto 	if (poller->pd) {
31768a01b4d6SAlexey Marchuk 		spdk_rdma_utils_put_pd(poller->pd);
3177bcd987eaSShuhei Matsumoto 	}
31786ea9de5fSShuhei Matsumoto 	free(poller);
31796ea9de5fSShuhei Matsumoto }
31806ea9de5fSShuhei Matsumoto 
31813b26e2c5SEvgeniy Kochetov static struct nvme_rdma_poller *
31828bef6f0bSSeth Howell nvme_rdma_poller_create(struct nvme_rdma_poll_group *group, struct ibv_context *ctx)
31838bef6f0bSSeth Howell {
31848bef6f0bSSeth Howell 	struct nvme_rdma_poller *poller;
3185bcd987eaSShuhei Matsumoto 	struct ibv_device_attr dev_attr;
3186cf151d60SAlexey Marchuk 	struct spdk_rdma_provider_srq_init_attr srq_init_attr = {};
3187bcd987eaSShuhei Matsumoto 	struct nvme_rdma_rsp_opts opts;
3188d1357fd8SShuhei Matsumoto 	int num_cqe, max_num_cqe;
3189bcd987eaSShuhei Matsumoto 	int rc;
31908bef6f0bSSeth Howell 
31918bef6f0bSSeth Howell 	poller = calloc(1, sizeof(*poller));
31928bef6f0bSSeth Howell 	if (poller == NULL) {
31938bef6f0bSSeth Howell 		SPDK_ERRLOG("Unable to allocate poller.\n");
31943b26e2c5SEvgeniy Kochetov 		return NULL;
31958bef6f0bSSeth Howell 	}
31968bef6f0bSSeth Howell 
31971439f9c7SShuhei Matsumoto 	poller->group = group;
31988bef6f0bSSeth Howell 	poller->device = ctx;
3199bcd987eaSShuhei Matsumoto 
3200bcd987eaSShuhei Matsumoto 	if (g_spdk_nvme_transport_opts.rdma_srq_size != 0) {
3201bcd987eaSShuhei Matsumoto 		rc = ibv_query_device(ctx, &dev_attr);
3202bcd987eaSShuhei Matsumoto 		if (rc) {
3203bcd987eaSShuhei Matsumoto 			SPDK_ERRLOG("Unable to query RDMA device.\n");
3204bcd987eaSShuhei Matsumoto 			goto fail;
3205bcd987eaSShuhei Matsumoto 		}
3206bcd987eaSShuhei Matsumoto 
32078a01b4d6SAlexey Marchuk 		poller->pd = spdk_rdma_utils_get_pd(ctx);
3208bcd987eaSShuhei Matsumoto 		if (poller->pd == NULL) {
3209bcd987eaSShuhei Matsumoto 			SPDK_ERRLOG("Unable to get PD.\n");
3210bcd987eaSShuhei Matsumoto 			goto fail;
3211bcd987eaSShuhei Matsumoto 		}
3212bcd987eaSShuhei Matsumoto 
32138a01b4d6SAlexey Marchuk 		poller->mr_map = spdk_rdma_utils_create_mem_map(poller->pd, &g_nvme_hooks,
32148ffb2c09SAlexey Marchuk 				 IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
3215bcd987eaSShuhei Matsumoto 		if (poller->mr_map == NULL) {
3216bcd987eaSShuhei Matsumoto 			SPDK_ERRLOG("Unable to create memory map.\n");
3217bcd987eaSShuhei Matsumoto 			goto fail;
3218bcd987eaSShuhei Matsumoto 		}
3219bcd987eaSShuhei Matsumoto 
3220bcd987eaSShuhei Matsumoto 		srq_init_attr.stats = &poller->stats.rdma_stats.recv;
3221bcd987eaSShuhei Matsumoto 		srq_init_attr.pd = poller->pd;
3222bcd987eaSShuhei Matsumoto 		srq_init_attr.srq_init_attr.attr.max_wr = spdk_min((uint32_t)dev_attr.max_srq_wr,
3223bcd987eaSShuhei Matsumoto 				g_spdk_nvme_transport_opts.rdma_srq_size);
3224bcd987eaSShuhei Matsumoto 		srq_init_attr.srq_init_attr.attr.max_sge = spdk_min(dev_attr.max_sge,
3225bcd987eaSShuhei Matsumoto 				NVME_RDMA_DEFAULT_RX_SGE);
3226bcd987eaSShuhei Matsumoto 
3227cf151d60SAlexey Marchuk 		poller->srq = spdk_rdma_provider_srq_create(&srq_init_attr);
3228bcd987eaSShuhei Matsumoto 		if (poller->srq == NULL) {
3229bcd987eaSShuhei Matsumoto 			SPDK_ERRLOG("Unable to create SRQ.\n");
3230bcd987eaSShuhei Matsumoto 			goto fail;
3231bcd987eaSShuhei Matsumoto 		}
3232bcd987eaSShuhei Matsumoto 
3233bcd987eaSShuhei Matsumoto 		opts.num_entries = g_spdk_nvme_transport_opts.rdma_srq_size;
3234bcd987eaSShuhei Matsumoto 		opts.rqpair = NULL;
3235bcd987eaSShuhei Matsumoto 		opts.srq = poller->srq;
3236bcd987eaSShuhei Matsumoto 		opts.mr_map = poller->mr_map;
3237bcd987eaSShuhei Matsumoto 
3238bcd987eaSShuhei Matsumoto 		poller->rsps = nvme_rdma_create_rsps(&opts);
3239bcd987eaSShuhei Matsumoto 		if (poller->rsps == NULL) {
3240bcd987eaSShuhei Matsumoto 			SPDK_ERRLOG("Unable to create poller RDMA responses.\n");
3241bcd987eaSShuhei Matsumoto 			goto fail;
3242bcd987eaSShuhei Matsumoto 		}
3243bcd987eaSShuhei Matsumoto 
3244bcd987eaSShuhei Matsumoto 		rc = nvme_rdma_poller_submit_recvs(poller);
3245bcd987eaSShuhei Matsumoto 		if (rc) {
3246bcd987eaSShuhei Matsumoto 			SPDK_ERRLOG("Unable to submit poller RDMA responses.\n");
3247bcd987eaSShuhei Matsumoto 			goto fail;
3248bcd987eaSShuhei Matsumoto 		}
3249bcd987eaSShuhei Matsumoto 
3250bcd987eaSShuhei Matsumoto 		/*
3251bcd987eaSShuhei Matsumoto 		 * When using an srq, fix the size of the completion queue at startup.
3252bcd987eaSShuhei Matsumoto 		 * The initiator sends only send and recv WRs. Hence, the multiplier is 2.
3253bcd987eaSShuhei Matsumoto 		 * (The target sends also data WRs. Hence, the multiplier is 3.)
3254bcd987eaSShuhei Matsumoto 		 */
3255bcd987eaSShuhei Matsumoto 		num_cqe = g_spdk_nvme_transport_opts.rdma_srq_size * 2;
3256bcd987eaSShuhei Matsumoto 	} else {
3257bcd987eaSShuhei Matsumoto 		num_cqe = DEFAULT_NVME_RDMA_CQ_SIZE;
3258bcd987eaSShuhei Matsumoto 	}
3259bcd987eaSShuhei Matsumoto 
3260d1357fd8SShuhei Matsumoto 	max_num_cqe = g_spdk_nvme_transport_opts.rdma_max_cq_size;
3261d1357fd8SShuhei Matsumoto 	if (max_num_cqe != 0 && num_cqe > max_num_cqe) {
3262d1357fd8SShuhei Matsumoto 		num_cqe = max_num_cqe;
3263d1357fd8SShuhei Matsumoto 	}
3264d1357fd8SShuhei Matsumoto 
3265bcd987eaSShuhei Matsumoto 	poller->cq = ibv_create_cq(poller->device, num_cqe, group, NULL, 0);
32668bef6f0bSSeth Howell 
32678bef6f0bSSeth Howell 	if (poller->cq == NULL) {
32683b26e2c5SEvgeniy Kochetov 		SPDK_ERRLOG("Unable to create CQ, errno %d.\n", errno);
32696ea9de5fSShuhei Matsumoto 		goto fail;
32708bef6f0bSSeth Howell 	}
32718bef6f0bSSeth Howell 
32728bef6f0bSSeth Howell 	STAILQ_INSERT_HEAD(&group->pollers, poller, link);
32738bef6f0bSSeth Howell 	group->num_pollers++;
3274bcd987eaSShuhei Matsumoto 	poller->current_num_wc = num_cqe;
327510392543SSeth Howell 	poller->required_num_wc = 0;
32763b26e2c5SEvgeniy Kochetov 	return poller;
32776ea9de5fSShuhei Matsumoto 
32786ea9de5fSShuhei Matsumoto fail:
32796ea9de5fSShuhei Matsumoto 	nvme_rdma_poller_destroy(poller);
32806ea9de5fSShuhei Matsumoto 	return NULL;
32818bef6f0bSSeth Howell }
32828bef6f0bSSeth Howell 
32838bef6f0bSSeth Howell static void
32848bef6f0bSSeth Howell nvme_rdma_poll_group_free_pollers(struct nvme_rdma_poll_group *group)
32858bef6f0bSSeth Howell {
32868bef6f0bSSeth Howell 	struct nvme_rdma_poller	*poller, *tmp_poller;
32878bef6f0bSSeth Howell 
32888bef6f0bSSeth Howell 	STAILQ_FOREACH_SAFE(poller, &group->pollers, link, tmp_poller) {
32893b26e2c5SEvgeniy Kochetov 		assert(poller->refcnt == 0);
32903b26e2c5SEvgeniy Kochetov 		if (poller->refcnt) {
32913b26e2c5SEvgeniy Kochetov 			SPDK_WARNLOG("Destroying poller with non-zero ref count: poller %p, refcnt %d\n",
32923b26e2c5SEvgeniy Kochetov 				     poller, poller->refcnt);
32933b26e2c5SEvgeniy Kochetov 		}
32943b26e2c5SEvgeniy Kochetov 
32958bef6f0bSSeth Howell 		STAILQ_REMOVE(&group->pollers, poller, nvme_rdma_poller, link);
32966ea9de5fSShuhei Matsumoto 		nvme_rdma_poller_destroy(poller);
32978bef6f0bSSeth Howell 	}
32988bef6f0bSSeth Howell }
32998bef6f0bSSeth Howell 
33003b26e2c5SEvgeniy Kochetov static struct nvme_rdma_poller *
33013b26e2c5SEvgeniy Kochetov nvme_rdma_poll_group_get_poller(struct nvme_rdma_poll_group *group, struct ibv_context *device)
33023b26e2c5SEvgeniy Kochetov {
33033b26e2c5SEvgeniy Kochetov 	struct nvme_rdma_poller *poller = NULL;
33043b26e2c5SEvgeniy Kochetov 
33053b26e2c5SEvgeniy Kochetov 	STAILQ_FOREACH(poller, &group->pollers, link) {
33063b26e2c5SEvgeniy Kochetov 		if (poller->device == device) {
33073b26e2c5SEvgeniy Kochetov 			break;
33083b26e2c5SEvgeniy Kochetov 		}
33093b26e2c5SEvgeniy Kochetov 	}
33103b26e2c5SEvgeniy Kochetov 
33113b26e2c5SEvgeniy Kochetov 	if (!poller) {
33123b26e2c5SEvgeniy Kochetov 		poller = nvme_rdma_poller_create(group, device);
33133b26e2c5SEvgeniy Kochetov 		if (!poller) {
33143b26e2c5SEvgeniy Kochetov 			SPDK_ERRLOG("Failed to create a poller for device %p\n", device);
33153b26e2c5SEvgeniy Kochetov 			return NULL;
33163b26e2c5SEvgeniy Kochetov 		}
33173b26e2c5SEvgeniy Kochetov 	}
33183b26e2c5SEvgeniy Kochetov 
33193b26e2c5SEvgeniy Kochetov 	poller->refcnt++;
33203b26e2c5SEvgeniy Kochetov 	return poller;
33213b26e2c5SEvgeniy Kochetov }
33223b26e2c5SEvgeniy Kochetov 
33233b26e2c5SEvgeniy Kochetov static void
33243b26e2c5SEvgeniy Kochetov nvme_rdma_poll_group_put_poller(struct nvme_rdma_poll_group *group, struct nvme_rdma_poller *poller)
33253b26e2c5SEvgeniy Kochetov {
33263b26e2c5SEvgeniy Kochetov 	assert(poller->refcnt > 0);
33273b26e2c5SEvgeniy Kochetov 	if (--poller->refcnt == 0) {
33283b26e2c5SEvgeniy Kochetov 		STAILQ_REMOVE(&group->pollers, poller, nvme_rdma_poller, link);
33293b26e2c5SEvgeniy Kochetov 		group->num_pollers--;
33306ea9de5fSShuhei Matsumoto 		nvme_rdma_poller_destroy(poller);
33313b26e2c5SEvgeniy Kochetov 	}
33323b26e2c5SEvgeniy Kochetov }
33333b26e2c5SEvgeniy Kochetov 
3334c998c6c6SSeth Howell static struct spdk_nvme_transport_poll_group *
3335c998c6c6SSeth Howell nvme_rdma_poll_group_create(void)
3336c998c6c6SSeth Howell {
33378bef6f0bSSeth Howell 	struct nvme_rdma_poll_group	*group;
33381b818a28SSeth Howell 
33398bef6f0bSSeth Howell 	group = calloc(1, sizeof(*group));
33401b818a28SSeth Howell 	if (group == NULL) {
33411b818a28SSeth Howell 		SPDK_ERRLOG("Unable to allocate poll group.\n");
3342c998c6c6SSeth Howell 		return NULL;
3343c998c6c6SSeth Howell 	}
3344c998c6c6SSeth Howell 
33458bef6f0bSSeth Howell 	STAILQ_INIT(&group->pollers);
3346f8fc1e11SShuhei Matsumoto 	TAILQ_INIT(&group->connecting_qpairs);
3347fa457d63SShuhei Matsumoto 	TAILQ_INIT(&group->active_qpairs);
33481b818a28SSeth Howell 	return &group->group;
33491b818a28SSeth Howell }
33501b818a28SSeth Howell 
335110392543SSeth Howell static int
3352fc86e792SSeth Howell nvme_rdma_poll_group_connect_qpair(struct spdk_nvme_qpair *qpair)
3353c998c6c6SSeth Howell {
3354c998c6c6SSeth Howell 	return 0;
3355c998c6c6SSeth Howell }
3356c998c6c6SSeth Howell 
3357c998c6c6SSeth Howell static int
3358fc86e792SSeth Howell nvme_rdma_poll_group_disconnect_qpair(struct spdk_nvme_qpair *qpair)
3359c998c6c6SSeth Howell {
3360f8fc1e11SShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
3361fa457d63SShuhei Matsumoto 	struct nvme_rdma_poll_group *group = nvme_rdma_poll_group(qpair->poll_group);
3362f8fc1e11SShuhei Matsumoto 
3363f8fc1e11SShuhei Matsumoto 	if (rqpair->link_connecting.tqe_prev) {
3364f8fc1e11SShuhei Matsumoto 		TAILQ_REMOVE(&group->connecting_qpairs, rqpair, link_connecting);
3365f8fc1e11SShuhei Matsumoto 		/* We use prev pointer to check if qpair is in connecting list or not .
3366f8fc1e11SShuhei Matsumoto 		 * TAILQ_REMOVE doesn't do it. So, we do it manually.
3367f8fc1e11SShuhei Matsumoto 		 */
3368f8fc1e11SShuhei Matsumoto 		rqpair->link_connecting.tqe_prev = NULL;
3369f8fc1e11SShuhei Matsumoto 	}
3370f8fc1e11SShuhei Matsumoto 
3371c998c6c6SSeth Howell 	return 0;
3372c998c6c6SSeth Howell }
3373c998c6c6SSeth Howell 
3374c998c6c6SSeth Howell static int
3375c998c6c6SSeth Howell nvme_rdma_poll_group_add(struct spdk_nvme_transport_poll_group *tgroup,
3376c998c6c6SSeth Howell 			 struct spdk_nvme_qpair *qpair)
3377c998c6c6SSeth Howell {
33781b818a28SSeth Howell 	return 0;
3379c998c6c6SSeth Howell }
3380c998c6c6SSeth Howell 
3381c998c6c6SSeth Howell static int
3382c998c6c6SSeth Howell nvme_rdma_poll_group_remove(struct spdk_nvme_transport_poll_group *tgroup,
3383c998c6c6SSeth Howell 			    struct spdk_nvme_qpair *qpair)
3384c998c6c6SSeth Howell {
3385fa457d63SShuhei Matsumoto 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(qpair);
3386fa457d63SShuhei Matsumoto 	struct nvme_rdma_poll_group *group = nvme_rdma_poll_group(qpair->poll_group);
3387fa457d63SShuhei Matsumoto 
33880354bb8eSAlexey Marchuk 	if (rqpair->poller) {
33890354bb8eSAlexey Marchuk 		/* A qpair may skip transport disconnect part if it was already disconnecting. But on RDMA level a qpair
33900354bb8eSAlexey Marchuk 		 * may still have a poller reference. In that case we should continue transport disconnect here
33910354bb8eSAlexey Marchuk 		 * because a poller depends on the poll group reference which is going to be removed */
33920354bb8eSAlexey Marchuk 		SPDK_INFOLOG(nvme, "qpair %p, id %u, nvme state %d, rdma state %d, force disconnect\n",
33930354bb8eSAlexey Marchuk 			     qpair, qpair->id, qpair->state, rqpair->state);
33940354bb8eSAlexey Marchuk 		nvme_rdma_ctrlr_disconnect_qpair(qpair->ctrlr, qpair);
33950354bb8eSAlexey Marchuk 	}
33960354bb8eSAlexey Marchuk 
3397fa457d63SShuhei Matsumoto 	if (rqpair->link_active.tqe_prev) {
3398fa457d63SShuhei Matsumoto 		TAILQ_REMOVE(&group->active_qpairs, rqpair, link_active);
3399fa457d63SShuhei Matsumoto 		rqpair->link_active.tqe_prev = NULL;
3400fa457d63SShuhei Matsumoto 	}
3401fa457d63SShuhei Matsumoto 
34021b818a28SSeth Howell 	return 0;
3403c998c6c6SSeth Howell }
3404c998c6c6SSeth Howell 
340577967a60SShuhei Matsumoto static inline void
3406fa457d63SShuhei Matsumoto nvme_rdma_qpair_process_submits(struct nvme_rdma_poll_group *group,
3407fa457d63SShuhei Matsumoto 				struct nvme_rdma_qpair *rqpair)
340877967a60SShuhei Matsumoto {
3409fa457d63SShuhei Matsumoto 	struct spdk_nvme_qpair	*qpair = &rqpair->qpair;
341077967a60SShuhei Matsumoto 
3411fa457d63SShuhei Matsumoto 	assert(rqpair->link_active.tqe_prev != NULL);
3412fa457d63SShuhei Matsumoto 
3413fa457d63SShuhei Matsumoto 	if (spdk_unlikely(rqpair->state <= NVME_RDMA_QPAIR_STATE_INITIALIZING ||
3414fa457d63SShuhei Matsumoto 			  rqpair->state >= NVME_RDMA_QPAIR_STATE_EXITING)) {
341577967a60SShuhei Matsumoto 		return;
341677967a60SShuhei Matsumoto 	}
341777967a60SShuhei Matsumoto 
341877967a60SShuhei Matsumoto 	if (spdk_unlikely(qpair->ctrlr->timeout_enabled)) {
341977967a60SShuhei Matsumoto 		nvme_rdma_qpair_check_timeout(qpair);
342077967a60SShuhei Matsumoto 	}
342177967a60SShuhei Matsumoto 
342277967a60SShuhei Matsumoto 	nvme_rdma_qpair_submit_sends(rqpair);
342377967a60SShuhei Matsumoto 	if (!rqpair->srq) {
342477967a60SShuhei Matsumoto 		nvme_rdma_qpair_submit_recvs(rqpair);
342577967a60SShuhei Matsumoto 	}
342677967a60SShuhei Matsumoto 	if (rqpair->num_completions > 0) {
342777967a60SShuhei Matsumoto 		nvme_qpair_resubmit_requests(qpair, rqpair->num_completions);
3428f7b85051SShuhei Matsumoto 		rqpair->num_completions = 0;
342977967a60SShuhei Matsumoto 	}
3430fa457d63SShuhei Matsumoto 
3431fa457d63SShuhei Matsumoto 	if (rqpair->num_outstanding_reqs == 0 && STAILQ_EMPTY(&qpair->queued_req)) {
3432fa457d63SShuhei Matsumoto 		TAILQ_REMOVE(&group->active_qpairs, rqpair, link_active);
3433fa457d63SShuhei Matsumoto 		/* We use prev pointer to check if qpair is in active list or not.
3434fa457d63SShuhei Matsumoto 		 * TAILQ_REMOVE doesn't do it. So, we do it manually.
3435fa457d63SShuhei Matsumoto 		 */
3436fa457d63SShuhei Matsumoto 		rqpair->link_active.tqe_prev = NULL;
3437fa457d63SShuhei Matsumoto 	}
343877967a60SShuhei Matsumoto }
343977967a60SShuhei Matsumoto 
3440c998c6c6SSeth Howell static int64_t
3441c998c6c6SSeth Howell nvme_rdma_poll_group_process_completions(struct spdk_nvme_transport_poll_group *tgroup,
3442fc86e792SSeth Howell 		uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
3443c998c6c6SSeth Howell {
34441b818a28SSeth Howell 	struct spdk_nvme_qpair			*qpair, *tmp_qpair;
3445f8fc1e11SShuhei Matsumoto 	struct nvme_rdma_qpair			*rqpair, *tmp_rqpair;
34468bef6f0bSSeth Howell 	struct nvme_rdma_poll_group		*group;
34478bef6f0bSSeth Howell 	struct nvme_rdma_poller			*poller;
3448db97955bSShuhei Matsumoto 	int					batch_size, rc, rc2 = 0;
34491b818a28SSeth Howell 	int64_t					total_completions = 0;
34508bef6f0bSSeth Howell 	uint64_t				completions_allowed = 0;
34518bef6f0bSSeth Howell 	uint64_t				completions_per_poller = 0;
34528bef6f0bSSeth Howell 	uint64_t				poller_completions = 0;
345350569293SAlexey Marchuk 	uint64_t				rdma_completions;
34541b818a28SSeth Howell 
34558bef6f0bSSeth Howell 	if (completions_per_qpair == 0) {
34568bef6f0bSSeth Howell 		completions_per_qpair = MAX_COMPLETIONS_PER_POLL;
34578bef6f0bSSeth Howell 	}
34588bef6f0bSSeth Howell 
34598bef6f0bSSeth Howell 	group = nvme_rdma_poll_group(tgroup);
3460f8fc1e11SShuhei Matsumoto 
34611b818a28SSeth Howell 	STAILQ_FOREACH_SAFE(qpair, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_qpair) {
34629717b0c3SShuhei Matsumoto 		rc = nvme_rdma_ctrlr_disconnect_qpair_poll(qpair->ctrlr, qpair);
34639717b0c3SShuhei Matsumoto 		if (rc == 0) {
34641b818a28SSeth Howell 			disconnected_qpair_cb(qpair, tgroup->group->ctx);
34651b818a28SSeth Howell 		}
34669717b0c3SShuhei Matsumoto 	}
34671b818a28SSeth Howell 
3468f8fc1e11SShuhei Matsumoto 	TAILQ_FOREACH_SAFE(rqpair, &group->connecting_qpairs, link_connecting, tmp_rqpair) {
3469f8fc1e11SShuhei Matsumoto 		qpair = &rqpair->qpair;
347029974dc8SShuhei Matsumoto 
347129974dc8SShuhei Matsumoto 		rc = nvme_rdma_ctrlr_connect_qpair_poll(qpair->ctrlr, qpair);
3472f8fc1e11SShuhei Matsumoto 		if (rc == 0 || rc != -EAGAIN) {
3473f8fc1e11SShuhei Matsumoto 			TAILQ_REMOVE(&group->connecting_qpairs, rqpair, link_connecting);
3474f8fc1e11SShuhei Matsumoto 			/* We use prev pointer to check if qpair is in connecting list or not.
3475f8fc1e11SShuhei Matsumoto 			 * TAILQ_REMOVE does not do it. So, we do it manually.
3476f8fc1e11SShuhei Matsumoto 			 */
3477f8fc1e11SShuhei Matsumoto 			rqpair->link_connecting.tqe_prev = NULL;
3478f8fc1e11SShuhei Matsumoto 
347929974dc8SShuhei Matsumoto 			if (rc == 0) {
348029974dc8SShuhei Matsumoto 				/* Once the connection is completed, we can submit queued requests */
348129974dc8SShuhei Matsumoto 				nvme_qpair_resubmit_requests(qpair, rqpair->num_entries);
348229974dc8SShuhei Matsumoto 			} else if (rc != -EAGAIN) {
348329974dc8SShuhei Matsumoto 				SPDK_ERRLOG("Failed to connect rqpair=%p\n", rqpair);
348429974dc8SShuhei Matsumoto 				nvme_rdma_fail_qpair(qpair, 0);
348529974dc8SShuhei Matsumoto 			}
3486f8fc1e11SShuhei Matsumoto 		}
3487f8fc1e11SShuhei Matsumoto 	}
3488f8fc1e11SShuhei Matsumoto 
3489f8fc1e11SShuhei Matsumoto 	STAILQ_FOREACH_SAFE(qpair, &tgroup->connected_qpairs, poll_group_stailq, tmp_qpair) {
3490f8fc1e11SShuhei Matsumoto 		rqpair = nvme_rdma_qpair(qpair);
3491f8fc1e11SShuhei Matsumoto 
3492f8fc1e11SShuhei Matsumoto 		if (spdk_likely(nvme_qpair_get_state(qpair) != NVME_QPAIR_CONNECTING)) {
34938bef6f0bSSeth Howell 			nvme_rdma_qpair_process_cm_event(rqpair);
34949717b0c3SShuhei Matsumoto 		}
34958bef6f0bSSeth Howell 
34968bef6f0bSSeth Howell 		if (spdk_unlikely(qpair->transport_failure_reason != SPDK_NVME_QPAIR_FAILURE_NONE)) {
349775d38a30SShuhei Matsumoto 			rc2 = -ENXIO;
34988bef6f0bSSeth Howell 			nvme_rdma_fail_qpair(qpair, 0);
34991b818a28SSeth Howell 		}
35008bef6f0bSSeth Howell 	}
35018bef6f0bSSeth Howell 
3502db97955bSShuhei Matsumoto 	completions_allowed = completions_per_qpair * tgroup->num_connected_qpairs;
3503f0e4b91fSAlexey Marchuk 	if (spdk_likely(group->num_pollers)) {
35048bef6f0bSSeth Howell 		completions_per_poller = spdk_max(completions_allowed / group->num_pollers, 1);
35053b26e2c5SEvgeniy Kochetov 	}
35068bef6f0bSSeth Howell 
35078bef6f0bSSeth Howell 	STAILQ_FOREACH(poller, &group->pollers, link) {
35088bef6f0bSSeth Howell 		poller_completions = 0;
350950569293SAlexey Marchuk 		rdma_completions = 0;
35108bef6f0bSSeth Howell 		do {
351150569293SAlexey Marchuk 			poller->stats.polls++;
35128bef6f0bSSeth Howell 			batch_size = spdk_min((completions_per_poller - poller_completions), MAX_COMPLETIONS_PER_POLL);
35131439f9c7SShuhei Matsumoto 			rc = nvme_rdma_cq_process_completions(poller->cq, batch_size, poller, NULL, &rdma_completions);
35148bef6f0bSSeth Howell 			if (rc <= 0) {
35158bef6f0bSSeth Howell 				if (rc == -ECANCELED) {
35168bef6f0bSSeth Howell 					return -EIO;
351750569293SAlexey Marchuk 				} else if (rc == 0) {
351850569293SAlexey Marchuk 					poller->stats.idle_polls++;
35198bef6f0bSSeth Howell 				}
35208bef6f0bSSeth Howell 				break;
35218bef6f0bSSeth Howell 			}
35228bef6f0bSSeth Howell 
35238bef6f0bSSeth Howell 			poller_completions += rc;
35248bef6f0bSSeth Howell 		} while (poller_completions < completions_per_poller);
35258bef6f0bSSeth Howell 		total_completions += poller_completions;
352650569293SAlexey Marchuk 		poller->stats.completions += rdma_completions;
3527bcd987eaSShuhei Matsumoto 		if (poller->srq) {
3528bcd987eaSShuhei Matsumoto 			nvme_rdma_poller_submit_recvs(poller);
3529bcd987eaSShuhei Matsumoto 		}
35308bef6f0bSSeth Howell 	}
35318bef6f0bSSeth Howell 
3532fa457d63SShuhei Matsumoto 	TAILQ_FOREACH_SAFE(rqpair, &group->active_qpairs, link_active, tmp_rqpair) {
3533fa457d63SShuhei Matsumoto 		nvme_rdma_qpair_process_submits(group, rqpair);
353459c8bb52SJim Harris 	}
35358bef6f0bSSeth Howell 
353675d38a30SShuhei Matsumoto 	return rc2 != 0 ? rc2 : total_completions;
3537c998c6c6SSeth Howell }
3538c998c6c6SSeth Howell 
35391efa1b16SAnkit Kumar /*
35401efa1b16SAnkit Kumar  * Handle disconnected qpairs when interrupt support gets added.
35411efa1b16SAnkit Kumar  */
35421efa1b16SAnkit Kumar static void
35431efa1b16SAnkit Kumar nvme_rdma_poll_group_check_disconnected_qpairs(struct spdk_nvme_transport_poll_group *tgroup,
35441efa1b16SAnkit Kumar 		spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
35451efa1b16SAnkit Kumar {
35461efa1b16SAnkit Kumar }
35471efa1b16SAnkit Kumar 
3548c998c6c6SSeth Howell static int
3549c998c6c6SSeth Howell nvme_rdma_poll_group_destroy(struct spdk_nvme_transport_poll_group *tgroup)
3550c998c6c6SSeth Howell {
35518bef6f0bSSeth Howell 	struct nvme_rdma_poll_group	*group = nvme_rdma_poll_group(tgroup);
35528bef6f0bSSeth Howell 
35531b818a28SSeth Howell 	if (!STAILQ_EMPTY(&tgroup->connected_qpairs) || !STAILQ_EMPTY(&tgroup->disconnected_qpairs)) {
35541b818a28SSeth Howell 		return -EBUSY;
35551b818a28SSeth Howell 	}
35561b818a28SSeth Howell 
35578bef6f0bSSeth Howell 	nvme_rdma_poll_group_free_pollers(group);
35588bef6f0bSSeth Howell 	free(group);
35591b818a28SSeth Howell 
35601b818a28SSeth Howell 	return 0;
3561c998c6c6SSeth Howell }
3562c998c6c6SSeth Howell 
35633fcda8e7SAlexey Marchuk static int
35643fcda8e7SAlexey Marchuk nvme_rdma_poll_group_get_stats(struct spdk_nvme_transport_poll_group *tgroup,
35653fcda8e7SAlexey Marchuk 			       struct spdk_nvme_transport_poll_group_stat **_stats)
35663fcda8e7SAlexey Marchuk {
35673fcda8e7SAlexey Marchuk 	struct nvme_rdma_poll_group *group;
35683fcda8e7SAlexey Marchuk 	struct spdk_nvme_transport_poll_group_stat *stats;
35693fcda8e7SAlexey Marchuk 	struct spdk_nvme_rdma_device_stat *device_stat;
35703fcda8e7SAlexey Marchuk 	struct nvme_rdma_poller *poller;
35713fcda8e7SAlexey Marchuk 	uint32_t i = 0;
35723fcda8e7SAlexey Marchuk 
35733fcda8e7SAlexey Marchuk 	if (tgroup == NULL || _stats == NULL) {
35743fcda8e7SAlexey Marchuk 		SPDK_ERRLOG("Invalid stats or group pointer\n");
35753fcda8e7SAlexey Marchuk 		return -EINVAL;
35763fcda8e7SAlexey Marchuk 	}
35773fcda8e7SAlexey Marchuk 
35783fcda8e7SAlexey Marchuk 	group = nvme_rdma_poll_group(tgroup);
35793fcda8e7SAlexey Marchuk 	stats = calloc(1, sizeof(*stats));
35803fcda8e7SAlexey Marchuk 	if (!stats) {
35813fcda8e7SAlexey Marchuk 		SPDK_ERRLOG("Can't allocate memory for RDMA stats\n");
35823fcda8e7SAlexey Marchuk 		return -ENOMEM;
35833fcda8e7SAlexey Marchuk 	}
35843fcda8e7SAlexey Marchuk 	stats->trtype = SPDK_NVME_TRANSPORT_RDMA;
35853fcda8e7SAlexey Marchuk 	stats->rdma.num_devices = group->num_pollers;
35863b26e2c5SEvgeniy Kochetov 
35873b26e2c5SEvgeniy Kochetov 	if (stats->rdma.num_devices == 0) {
35883b26e2c5SEvgeniy Kochetov 		*_stats = stats;
35893b26e2c5SEvgeniy Kochetov 		return 0;
35903b26e2c5SEvgeniy Kochetov 	}
35913b26e2c5SEvgeniy Kochetov 
35923fcda8e7SAlexey Marchuk 	stats->rdma.device_stats = calloc(stats->rdma.num_devices, sizeof(*stats->rdma.device_stats));
35933fcda8e7SAlexey Marchuk 	if (!stats->rdma.device_stats) {
35943fcda8e7SAlexey Marchuk 		SPDK_ERRLOG("Can't allocate memory for RDMA device stats\n");
35953fcda8e7SAlexey Marchuk 		free(stats);
35963fcda8e7SAlexey Marchuk 		return -ENOMEM;
35973fcda8e7SAlexey Marchuk 	}
35983fcda8e7SAlexey Marchuk 
35993fcda8e7SAlexey Marchuk 	STAILQ_FOREACH(poller, &group->pollers, link) {
36003fcda8e7SAlexey Marchuk 		device_stat = &stats->rdma.device_stats[i];
36013fcda8e7SAlexey Marchuk 		device_stat->name = poller->device->device->name;
36023fcda8e7SAlexey Marchuk 		device_stat->polls = poller->stats.polls;
36033fcda8e7SAlexey Marchuk 		device_stat->idle_polls = poller->stats.idle_polls;
36043fcda8e7SAlexey Marchuk 		device_stat->completions = poller->stats.completions;
36053fcda8e7SAlexey Marchuk 		device_stat->queued_requests = poller->stats.queued_requests;
36063fcda8e7SAlexey Marchuk 		device_stat->total_send_wrs = poller->stats.rdma_stats.send.num_submitted_wrs;
36073fcda8e7SAlexey Marchuk 		device_stat->send_doorbell_updates = poller->stats.rdma_stats.send.doorbell_updates;
36083fcda8e7SAlexey Marchuk 		device_stat->total_recv_wrs = poller->stats.rdma_stats.recv.num_submitted_wrs;
36093fcda8e7SAlexey Marchuk 		device_stat->recv_doorbell_updates = poller->stats.rdma_stats.recv.doorbell_updates;
36103fcda8e7SAlexey Marchuk 		i++;
36113fcda8e7SAlexey Marchuk 	}
36123fcda8e7SAlexey Marchuk 
36133fcda8e7SAlexey Marchuk 	*_stats = stats;
36143fcda8e7SAlexey Marchuk 
36153fcda8e7SAlexey Marchuk 	return 0;
36163fcda8e7SAlexey Marchuk }
36173fcda8e7SAlexey Marchuk 
36183fcda8e7SAlexey Marchuk static void
36193fcda8e7SAlexey Marchuk nvme_rdma_poll_group_free_stats(struct spdk_nvme_transport_poll_group *tgroup,
36203fcda8e7SAlexey Marchuk 				struct spdk_nvme_transport_poll_group_stat *stats)
36213fcda8e7SAlexey Marchuk {
36223fcda8e7SAlexey Marchuk 	if (stats) {
36233fcda8e7SAlexey Marchuk 		free(stats->rdma.device_stats);
36243fcda8e7SAlexey Marchuk 	}
36253fcda8e7SAlexey Marchuk 	free(stats);
36263fcda8e7SAlexey Marchuk }
36273fcda8e7SAlexey Marchuk 
36289381d8d3SAlexey Marchuk static int
36299381d8d3SAlexey Marchuk nvme_rdma_ctrlr_get_memory_domains(const struct spdk_nvme_ctrlr *ctrlr,
36309381d8d3SAlexey Marchuk 				   struct spdk_memory_domain **domains, int array_size)
3631a422d8b0SAlexey Marchuk {
3632a422d8b0SAlexey Marchuk 	struct nvme_rdma_qpair *rqpair = nvme_rdma_qpair(ctrlr->adminq);
3633a422d8b0SAlexey Marchuk 
36349381d8d3SAlexey Marchuk 	if (domains && array_size > 0) {
36351794c395SAlexey Marchuk 		domains[0] = rqpair->rdma_qp->domain;
36369381d8d3SAlexey Marchuk 	}
36379381d8d3SAlexey Marchuk 
36389381d8d3SAlexey Marchuk 	return 1;
3639a422d8b0SAlexey Marchuk }
3640a422d8b0SAlexey Marchuk 
3641f366e261SJim Harris void
36429fb69476Szkhatami88 spdk_nvme_rdma_init_hooks(struct spdk_nvme_rdma_hooks *hooks)
36439fb69476Szkhatami88 {
36449fb69476Szkhatami88 	g_nvme_hooks = *hooks;
36459fb69476Szkhatami88 }
3646e4eef697SSeth Howell 
3647e4eef697SSeth Howell const struct spdk_nvme_transport_ops rdma_ops = {
3648e4eef697SSeth Howell 	.name = "RDMA",
3649e4eef697SSeth Howell 	.type = SPDK_NVME_TRANSPORT_RDMA,
3650e4eef697SSeth Howell 	.ctrlr_construct = nvme_rdma_ctrlr_construct,
3651e4eef697SSeth Howell 	.ctrlr_scan = nvme_fabric_ctrlr_scan,
3652e4eef697SSeth Howell 	.ctrlr_destruct = nvme_rdma_ctrlr_destruct,
3653e4eef697SSeth Howell 	.ctrlr_enable = nvme_rdma_ctrlr_enable,
3654e4eef697SSeth Howell 
3655e4eef697SSeth Howell 	.ctrlr_set_reg_4 = nvme_fabric_ctrlr_set_reg_4,
3656e4eef697SSeth Howell 	.ctrlr_set_reg_8 = nvme_fabric_ctrlr_set_reg_8,
3657e4eef697SSeth Howell 	.ctrlr_get_reg_4 = nvme_fabric_ctrlr_get_reg_4,
3658e4eef697SSeth Howell 	.ctrlr_get_reg_8 = nvme_fabric_ctrlr_get_reg_8,
3659b0f4249cSAlexey Marchuk 	.ctrlr_set_reg_4_async = nvme_fabric_ctrlr_set_reg_4_async,
3660b0f4249cSAlexey Marchuk 	.ctrlr_set_reg_8_async = nvme_fabric_ctrlr_set_reg_8_async,
3661b0f4249cSAlexey Marchuk 	.ctrlr_get_reg_4_async = nvme_fabric_ctrlr_get_reg_4_async,
3662b0f4249cSAlexey Marchuk 	.ctrlr_get_reg_8_async = nvme_fabric_ctrlr_get_reg_8_async,
3663e4eef697SSeth Howell 
3664e4eef697SSeth Howell 	.ctrlr_get_max_xfer_size = nvme_rdma_ctrlr_get_max_xfer_size,
3665e4eef697SSeth Howell 	.ctrlr_get_max_sges = nvme_rdma_ctrlr_get_max_sges,
3666e4eef697SSeth Howell 
3667e4eef697SSeth Howell 	.ctrlr_create_io_qpair = nvme_rdma_ctrlr_create_io_qpair,
3668e4eef697SSeth Howell 	.ctrlr_delete_io_qpair = nvme_rdma_ctrlr_delete_io_qpair,
3669e4eef697SSeth Howell 	.ctrlr_connect_qpair = nvme_rdma_ctrlr_connect_qpair,
3670e4eef697SSeth Howell 	.ctrlr_disconnect_qpair = nvme_rdma_ctrlr_disconnect_qpair,
3671e4eef697SSeth Howell 
36729381d8d3SAlexey Marchuk 	.ctrlr_get_memory_domains = nvme_rdma_ctrlr_get_memory_domains,
3673a422d8b0SAlexey Marchuk 
3674e4eef697SSeth Howell 	.qpair_abort_reqs = nvme_rdma_qpair_abort_reqs,
3675e4eef697SSeth Howell 	.qpair_reset = nvme_rdma_qpair_reset,
3676e4eef697SSeth Howell 	.qpair_submit_request = nvme_rdma_qpair_submit_request,
3677e4eef697SSeth Howell 	.qpair_process_completions = nvme_rdma_qpair_process_completions,
3678f2bd635eSShuhei Matsumoto 	.qpair_iterate_requests = nvme_rdma_qpair_iterate_requests,
36794ddd77b2SKonrad Sztyber 	.qpair_authenticate = nvme_rdma_qpair_authenticate,
3680e4eef697SSeth Howell 	.admin_qpair_abort_aers = nvme_rdma_admin_qpair_abort_aers,
3681c998c6c6SSeth Howell 
3682c998c6c6SSeth Howell 	.poll_group_create = nvme_rdma_poll_group_create,
3683fc86e792SSeth Howell 	.poll_group_connect_qpair = nvme_rdma_poll_group_connect_qpair,
3684fc86e792SSeth Howell 	.poll_group_disconnect_qpair = nvme_rdma_poll_group_disconnect_qpair,
3685c998c6c6SSeth Howell 	.poll_group_add = nvme_rdma_poll_group_add,
3686c998c6c6SSeth Howell 	.poll_group_remove = nvme_rdma_poll_group_remove,
3687c998c6c6SSeth Howell 	.poll_group_process_completions = nvme_rdma_poll_group_process_completions,
36881efa1b16SAnkit Kumar 	.poll_group_check_disconnected_qpairs = nvme_rdma_poll_group_check_disconnected_qpairs,
3689c998c6c6SSeth Howell 	.poll_group_destroy = nvme_rdma_poll_group_destroy,
36903fcda8e7SAlexey Marchuk 	.poll_group_get_stats = nvme_rdma_poll_group_get_stats,
36913fcda8e7SAlexey Marchuk 	.poll_group_free_stats = nvme_rdma_poll_group_free_stats,
3692e4eef697SSeth Howell };
3693e4eef697SSeth Howell 
3694e4eef697SSeth Howell SPDK_NVME_TRANSPORT_REGISTER(rdma, &rdma_ops);
3695