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