blob: 8eacc9bd58f5acfe6ce034e4869d75ef545fd71b [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * NVMe over Fabrics RDMA host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <rdma/mr_pool.h>
11#include <linux/err.h>
12#include <linux/string.h>
13#include <linux/atomic.h>
14#include <linux/blk-mq.h>
15#include <linux/blk-mq-rdma.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/mutex.h>
19#include <linux/scatterlist.h>
20#include <linux/nvme.h>
21#include <asm/unaligned.h>
22
23#include <rdma/ib_verbs.h>
24#include <rdma/rdma_cm.h>
25#include <linux/nvme-rdma.h>
26
27#include "nvme.h"
28#include "fabrics.h"
29
30
31#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
32
33#define NVME_RDMA_MAX_SEGMENTS 256
34
35#define NVME_RDMA_MAX_INLINE_SEGMENTS 4
36
Olivier Deprez157378f2022-04-04 15:47:50 +020037#define NVME_RDMA_DATA_SGL_SIZE \
38 (sizeof(struct scatterlist) * NVME_INLINE_SG_CNT)
39#define NVME_RDMA_METADATA_SGL_SIZE \
40 (sizeof(struct scatterlist) * NVME_INLINE_METADATA_SG_CNT)
41
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042struct nvme_rdma_device {
43 struct ib_device *dev;
44 struct ib_pd *pd;
45 struct kref ref;
46 struct list_head entry;
47 unsigned int num_inline_segments;
48};
49
50struct nvme_rdma_qe {
51 struct ib_cqe cqe;
52 void *data;
53 u64 dma;
54};
55
Olivier Deprez157378f2022-04-04 15:47:50 +020056struct nvme_rdma_sgl {
57 int nents;
58 struct sg_table sg_table;
59};
60
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061struct nvme_rdma_queue;
62struct nvme_rdma_request {
63 struct nvme_request req;
64 struct ib_mr *mr;
65 struct nvme_rdma_qe sqe;
66 union nvme_result result;
67 __le16 status;
68 refcount_t ref;
69 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
70 u32 num_sge;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 struct ib_reg_wr reg_wr;
72 struct ib_cqe reg_cqe;
73 struct nvme_rdma_queue *queue;
Olivier Deprez157378f2022-04-04 15:47:50 +020074 struct nvme_rdma_sgl data_sgl;
75 struct nvme_rdma_sgl *metadata_sgl;
76 bool use_sig_mr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077};
78
79enum nvme_rdma_queue_flags {
80 NVME_RDMA_Q_ALLOCATED = 0,
81 NVME_RDMA_Q_LIVE = 1,
82 NVME_RDMA_Q_TR_READY = 2,
83};
84
85struct nvme_rdma_queue {
86 struct nvme_rdma_qe *rsp_ring;
87 int queue_size;
88 size_t cmnd_capsule_len;
89 struct nvme_rdma_ctrl *ctrl;
90 struct nvme_rdma_device *device;
91 struct ib_cq *ib_cq;
92 struct ib_qp *qp;
93
94 unsigned long flags;
95 struct rdma_cm_id *cm_id;
96 int cm_error;
97 struct completion cm_done;
Olivier Deprez157378f2022-04-04 15:47:50 +020098 bool pi_support;
99 int cq_size;
100 struct mutex queue_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101};
102
103struct nvme_rdma_ctrl {
104 /* read only in the hot path */
105 struct nvme_rdma_queue *queues;
106
107 /* other member variables */
108 struct blk_mq_tag_set tag_set;
109 struct work_struct err_work;
110
111 struct nvme_rdma_qe async_event_sqe;
112
113 struct delayed_work reconnect_work;
114
115 struct list_head list;
116
117 struct blk_mq_tag_set admin_tag_set;
118 struct nvme_rdma_device *device;
119
120 u32 max_fr_pages;
121
122 struct sockaddr_storage addr;
123 struct sockaddr_storage src_addr;
124
125 struct nvme_ctrl ctrl;
126 bool use_inline_data;
David Brazdil0f672f62019-12-10 10:32:29 +0000127 u32 io_queues[HCTX_MAX_TYPES];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128};
129
130static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
131{
132 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
133}
134
135static LIST_HEAD(device_list);
136static DEFINE_MUTEX(device_list_mutex);
137
138static LIST_HEAD(nvme_rdma_ctrl_list);
139static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
140
141/*
142 * Disabling this option makes small I/O goes faster, but is fundamentally
143 * unsafe. With it turned off we will have to register a global rkey that
144 * allows read and write access to all physical memory.
145 */
146static bool register_always = true;
147module_param(register_always, bool, 0444);
148MODULE_PARM_DESC(register_always,
149 "Use memory registration even for contiguous memory regions");
150
151static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
152 struct rdma_cm_event *event);
153static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Olivier Deprez157378f2022-04-04 15:47:50 +0200154static void nvme_rdma_complete_rq(struct request *rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000155
156static const struct blk_mq_ops nvme_rdma_mq_ops;
157static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
158
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
160{
161 return queue - queue->ctrl->queues;
162}
163
David Brazdil0f672f62019-12-10 10:32:29 +0000164static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
165{
166 return nvme_rdma_queue_idx(queue) >
167 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
168 queue->ctrl->io_queues[HCTX_TYPE_READ];
169}
170
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
172{
173 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
174}
175
176static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
177 size_t capsule_size, enum dma_data_direction dir)
178{
179 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
180 kfree(qe->data);
181}
182
183static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
184 size_t capsule_size, enum dma_data_direction dir)
185{
186 qe->data = kzalloc(capsule_size, GFP_KERNEL);
187 if (!qe->data)
188 return -ENOMEM;
189
190 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
191 if (ib_dma_mapping_error(ibdev, qe->dma)) {
192 kfree(qe->data);
193 qe->data = NULL;
194 return -ENOMEM;
195 }
196
197 return 0;
198}
199
200static void nvme_rdma_free_ring(struct ib_device *ibdev,
201 struct nvme_rdma_qe *ring, size_t ib_queue_size,
202 size_t capsule_size, enum dma_data_direction dir)
203{
204 int i;
205
206 for (i = 0; i < ib_queue_size; i++)
207 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
208 kfree(ring);
209}
210
211static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
212 size_t ib_queue_size, size_t capsule_size,
213 enum dma_data_direction dir)
214{
215 struct nvme_rdma_qe *ring;
216 int i;
217
218 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
219 if (!ring)
220 return NULL;
221
David Brazdil0f672f62019-12-10 10:32:29 +0000222 /*
223 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
224 * lifetime. It's safe, since any chage in the underlying RDMA device
225 * will issue error recovery and queue re-creation.
226 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 for (i = 0; i < ib_queue_size; i++) {
228 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
229 goto out_free_ring;
230 }
231
232 return ring;
233
234out_free_ring:
235 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
236 return NULL;
237}
238
239static void nvme_rdma_qp_event(struct ib_event *event, void *context)
240{
241 pr_debug("QP event %s (%d)\n",
242 ib_event_msg(event->event), event->event);
243
244}
245
246static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
247{
David Brazdil0f672f62019-12-10 10:32:29 +0000248 int ret;
249
250 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
David Brazdil0f672f62019-12-10 10:32:29 +0000252 if (ret < 0)
253 return ret;
254 if (ret == 0)
255 return -ETIMEDOUT;
256 WARN_ON_ONCE(queue->cm_error > 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 return queue->cm_error;
258}
259
260static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
261{
262 struct nvme_rdma_device *dev = queue->device;
263 struct ib_qp_init_attr init_attr;
264 int ret;
265
266 memset(&init_attr, 0, sizeof(init_attr));
267 init_attr.event_handler = nvme_rdma_qp_event;
268 /* +1 for drain */
269 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
270 /* +1 for drain */
271 init_attr.cap.max_recv_wr = queue->queue_size + 1;
272 init_attr.cap.max_recv_sge = 1;
273 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
274 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
275 init_attr.qp_type = IB_QPT_RC;
276 init_attr.send_cq = queue->ib_cq;
277 init_attr.recv_cq = queue->ib_cq;
Olivier Deprez157378f2022-04-04 15:47:50 +0200278 if (queue->pi_support)
279 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
280 init_attr.qp_context = queue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281
282 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
283
284 queue->qp = queue->cm_id->qp;
285 return ret;
286}
287
288static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
289 struct request *rq, unsigned int hctx_idx)
290{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292
David Brazdil0f672f62019-12-10 10:32:29 +0000293 kfree(req->sqe.data);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000294}
295
296static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
297 struct request *rq, unsigned int hctx_idx,
298 unsigned int numa_node)
299{
300 struct nvme_rdma_ctrl *ctrl = set->driver_data;
301 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
302 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
303 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304
305 nvme_req(rq)->ctrl = &ctrl->ctrl;
David Brazdil0f672f62019-12-10 10:32:29 +0000306 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
307 if (!req->sqe.data)
308 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000309
Olivier Deprez157378f2022-04-04 15:47:50 +0200310 /* metadata nvme_rdma_sgl struct is located after command's data SGL */
311 if (queue->pi_support)
312 req->metadata_sgl = (void *)nvme_req(rq) +
313 sizeof(struct nvme_rdma_request) +
314 NVME_RDMA_DATA_SGL_SIZE;
315
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 req->queue = queue;
317
318 return 0;
319}
320
321static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
322 unsigned int hctx_idx)
323{
324 struct nvme_rdma_ctrl *ctrl = data;
325 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
326
327 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
328
329 hctx->driver_data = queue;
330 return 0;
331}
332
333static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
334 unsigned int hctx_idx)
335{
336 struct nvme_rdma_ctrl *ctrl = data;
337 struct nvme_rdma_queue *queue = &ctrl->queues[0];
338
339 BUG_ON(hctx_idx != 0);
340
341 hctx->driver_data = queue;
342 return 0;
343}
344
345static void nvme_rdma_free_dev(struct kref *ref)
346{
347 struct nvme_rdma_device *ndev =
348 container_of(ref, struct nvme_rdma_device, ref);
349
350 mutex_lock(&device_list_mutex);
351 list_del(&ndev->entry);
352 mutex_unlock(&device_list_mutex);
353
354 ib_dealloc_pd(ndev->pd);
355 kfree(ndev);
356}
357
358static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
359{
360 kref_put(&dev->ref, nvme_rdma_free_dev);
361}
362
363static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
364{
365 return kref_get_unless_zero(&dev->ref);
366}
367
368static struct nvme_rdma_device *
369nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
370{
371 struct nvme_rdma_device *ndev;
372
373 mutex_lock(&device_list_mutex);
374 list_for_each_entry(ndev, &device_list, entry) {
375 if (ndev->dev->node_guid == cm_id->device->node_guid &&
376 nvme_rdma_dev_get(ndev))
377 goto out_unlock;
378 }
379
380 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
381 if (!ndev)
382 goto out_err;
383
384 ndev->dev = cm_id->device;
385 kref_init(&ndev->ref);
386
387 ndev->pd = ib_alloc_pd(ndev->dev,
388 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
389 if (IS_ERR(ndev->pd))
390 goto out_free_dev;
391
392 if (!(ndev->dev->attrs.device_cap_flags &
393 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
394 dev_err(&ndev->dev->dev,
395 "Memory registrations not supported.\n");
396 goto out_free_pd;
397 }
398
399 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
400 ndev->dev->attrs.max_send_sge - 1);
401 list_add(&ndev->entry, &device_list);
402out_unlock:
403 mutex_unlock(&device_list_mutex);
404 return ndev;
405
406out_free_pd:
407 ib_dealloc_pd(ndev->pd);
408out_free_dev:
409 kfree(ndev);
410out_err:
411 mutex_unlock(&device_list_mutex);
412 return NULL;
413}
414
Olivier Deprez157378f2022-04-04 15:47:50 +0200415static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue)
416{
417 if (nvme_rdma_poll_queue(queue))
418 ib_free_cq(queue->ib_cq);
419 else
420 ib_cq_pool_put(queue->ib_cq, queue->cq_size);
421}
422
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
424{
425 struct nvme_rdma_device *dev;
426 struct ib_device *ibdev;
427
428 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
429 return;
430
431 dev = queue->device;
432 ibdev = dev->dev;
433
Olivier Deprez157378f2022-04-04 15:47:50 +0200434 if (queue->pi_support)
435 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
437
438 /*
439 * The cm_id object might have been destroyed during RDMA connection
440 * establishment error flow to avoid getting other cma events, thus
441 * the destruction of the QP shouldn't use rdma_cm API.
442 */
443 ib_destroy_qp(queue->qp);
Olivier Deprez157378f2022-04-04 15:47:50 +0200444 nvme_rdma_free_cq(queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000445
446 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
447 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
448
449 nvme_rdma_dev_put(dev);
450}
451
Olivier Deprez157378f2022-04-04 15:47:50 +0200452static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453{
Olivier Deprez157378f2022-04-04 15:47:50 +0200454 u32 max_page_list_len;
455
456 if (pi_support)
457 max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len;
458 else
459 max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len;
460
461 return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1);
462}
463
464static int nvme_rdma_create_cq(struct ib_device *ibdev,
465 struct nvme_rdma_queue *queue)
466{
467 int ret, comp_vector, idx = nvme_rdma_queue_idx(queue);
468 enum ib_poll_context poll_ctx;
469
470 /*
471 * Spread I/O queues completion vectors according their queue index.
472 * Admin queues can always go on completion vector 0.
473 */
474 comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors;
475
476 /* Polling queues need direct cq polling context */
477 if (nvme_rdma_poll_queue(queue)) {
478 poll_ctx = IB_POLL_DIRECT;
479 queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size,
480 comp_vector, poll_ctx);
481 } else {
482 poll_ctx = IB_POLL_SOFTIRQ;
483 queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size,
484 comp_vector, poll_ctx);
485 }
486
487 if (IS_ERR(queue->ib_cq)) {
488 ret = PTR_ERR(queue->ib_cq);
489 return ret;
490 }
491
492 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000493}
494
495static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
496{
497 struct ib_device *ibdev;
498 const int send_wr_factor = 3; /* MR, SEND, INV */
499 const int cq_factor = send_wr_factor + 1; /* + RECV */
David Brazdil0f672f62019-12-10 10:32:29 +0000500 int ret, pages_per_mr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000501
502 queue->device = nvme_rdma_find_get_device(queue->cm_id);
503 if (!queue->device) {
504 dev_err(queue->cm_id->device->dev.parent,
505 "no client data found!\n");
506 return -ECONNREFUSED;
507 }
508 ibdev = queue->device->dev;
509
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 /* +1 for ib_stop_cq */
Olivier Deprez157378f2022-04-04 15:47:50 +0200511 queue->cq_size = cq_factor * queue->queue_size + 1;
512
513 ret = nvme_rdma_create_cq(ibdev, queue);
514 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515 goto out_put_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516
517 ret = nvme_rdma_create_qp(queue, send_wr_factor);
518 if (ret)
519 goto out_destroy_ib_cq;
520
521 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
522 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
523 if (!queue->rsp_ring) {
524 ret = -ENOMEM;
525 goto out_destroy_qp;
526 }
527
David Brazdil0f672f62019-12-10 10:32:29 +0000528 /*
529 * Currently we don't use SG_GAPS MR's so if the first entry is
530 * misaligned we'll end up using two entries for a single data page,
531 * so one additional entry is required.
532 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200533 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
535 queue->queue_size,
536 IB_MR_TYPE_MEM_REG,
David Brazdil0f672f62019-12-10 10:32:29 +0000537 pages_per_mr, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538 if (ret) {
539 dev_err(queue->ctrl->ctrl.device,
540 "failed to initialize MR pool sized %d for QID %d\n",
Olivier Deprez157378f2022-04-04 15:47:50 +0200541 queue->queue_size, nvme_rdma_queue_idx(queue));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542 goto out_destroy_ring;
543 }
544
Olivier Deprez157378f2022-04-04 15:47:50 +0200545 if (queue->pi_support) {
546 ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs,
547 queue->queue_size, IB_MR_TYPE_INTEGRITY,
548 pages_per_mr, pages_per_mr);
549 if (ret) {
550 dev_err(queue->ctrl->ctrl.device,
551 "failed to initialize PI MR pool sized %d for QID %d\n",
552 queue->queue_size, nvme_rdma_queue_idx(queue));
553 goto out_destroy_mr_pool;
554 }
555 }
556
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
558
559 return 0;
560
Olivier Deprez157378f2022-04-04 15:47:50 +0200561out_destroy_mr_pool:
562 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563out_destroy_ring:
564 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
565 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
566out_destroy_qp:
567 rdma_destroy_qp(queue->cm_id);
568out_destroy_ib_cq:
Olivier Deprez157378f2022-04-04 15:47:50 +0200569 nvme_rdma_free_cq(queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570out_put_dev:
571 nvme_rdma_dev_put(queue->device);
572 return ret;
573}
574
575static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
576 int idx, size_t queue_size)
577{
578 struct nvme_rdma_queue *queue;
579 struct sockaddr *src_addr = NULL;
580 int ret;
581
582 queue = &ctrl->queues[idx];
Olivier Deprez157378f2022-04-04 15:47:50 +0200583 mutex_init(&queue->queue_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 queue->ctrl = ctrl;
Olivier Deprez157378f2022-04-04 15:47:50 +0200585 if (idx && ctrl->ctrl.max_integrity_segments)
586 queue->pi_support = true;
587 else
588 queue->pi_support = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589 init_completion(&queue->cm_done);
590
591 if (idx > 0)
592 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
593 else
594 queue->cmnd_capsule_len = sizeof(struct nvme_command);
595
596 queue->queue_size = queue_size;
597
598 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
599 RDMA_PS_TCP, IB_QPT_RC);
600 if (IS_ERR(queue->cm_id)) {
601 dev_info(ctrl->ctrl.device,
602 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
Olivier Deprez157378f2022-04-04 15:47:50 +0200603 ret = PTR_ERR(queue->cm_id);
604 goto out_destroy_mutex;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000605 }
606
607 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
608 src_addr = (struct sockaddr *)&ctrl->src_addr;
609
610 queue->cm_error = -ETIMEDOUT;
611 ret = rdma_resolve_addr(queue->cm_id, src_addr,
612 (struct sockaddr *)&ctrl->addr,
613 NVME_RDMA_CONNECT_TIMEOUT_MS);
614 if (ret) {
615 dev_info(ctrl->ctrl.device,
616 "rdma_resolve_addr failed (%d).\n", ret);
617 goto out_destroy_cm_id;
618 }
619
620 ret = nvme_rdma_wait_for_cm(queue);
621 if (ret) {
622 dev_info(ctrl->ctrl.device,
623 "rdma connection establishment failed (%d)\n", ret);
624 goto out_destroy_cm_id;
625 }
626
627 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
628
629 return 0;
630
631out_destroy_cm_id:
632 rdma_destroy_id(queue->cm_id);
633 nvme_rdma_destroy_queue_ib(queue);
Olivier Deprez157378f2022-04-04 15:47:50 +0200634out_destroy_mutex:
635 mutex_destroy(&queue->queue_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000636 return ret;
637}
638
David Brazdil0f672f62019-12-10 10:32:29 +0000639static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
640{
641 rdma_disconnect(queue->cm_id);
642 ib_drain_qp(queue->qp);
643}
644
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
646{
Olivier Deprez157378f2022-04-04 15:47:50 +0200647 mutex_lock(&queue->queue_lock);
648 if (test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
649 __nvme_rdma_stop_queue(queue);
650 mutex_unlock(&queue->queue_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651}
652
653static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
654{
655 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
656 return;
657
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000658 rdma_destroy_id(queue->cm_id);
Olivier Deprez157378f2022-04-04 15:47:50 +0200659 nvme_rdma_destroy_queue_ib(queue);
660 mutex_destroy(&queue->queue_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661}
662
663static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
664{
665 int i;
666
667 for (i = 1; i < ctrl->ctrl.queue_count; i++)
668 nvme_rdma_free_queue(&ctrl->queues[i]);
669}
670
671static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
672{
673 int i;
674
675 for (i = 1; i < ctrl->ctrl.queue_count; i++)
676 nvme_rdma_stop_queue(&ctrl->queues[i]);
677}
678
679static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
680{
David Brazdil0f672f62019-12-10 10:32:29 +0000681 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
682 bool poll = nvme_rdma_poll_queue(queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000683 int ret;
684
685 if (idx)
David Brazdil0f672f62019-12-10 10:32:29 +0000686 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000687 else
688 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
689
David Brazdil0f672f62019-12-10 10:32:29 +0000690 if (!ret) {
691 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
692 } else {
693 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
694 __nvme_rdma_stop_queue(queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695 dev_info(ctrl->ctrl.device,
696 "failed to connect queue: %d ret=%d\n", idx, ret);
David Brazdil0f672f62019-12-10 10:32:29 +0000697 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698 return ret;
699}
700
701static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
702{
703 int i, ret = 0;
704
705 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
706 ret = nvme_rdma_start_queue(ctrl, i);
707 if (ret)
708 goto out_stop_queues;
709 }
710
711 return 0;
712
713out_stop_queues:
714 for (i--; i >= 1; i--)
715 nvme_rdma_stop_queue(&ctrl->queues[i]);
716 return ret;
717}
718
719static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
720{
721 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
722 struct ib_device *ibdev = ctrl->device->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000723 unsigned int nr_io_queues, nr_default_queues;
724 unsigned int nr_read_queues, nr_poll_queues;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000725 int i, ret;
726
David Brazdil0f672f62019-12-10 10:32:29 +0000727 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
728 min(opts->nr_io_queues, num_online_cpus()));
729 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
730 min(opts->nr_write_queues, num_online_cpus()));
731 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
732 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000733
734 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
735 if (ret)
736 return ret;
737
Olivier Deprez0e641232021-09-23 10:07:05 +0200738 if (nr_io_queues == 0) {
739 dev_err(ctrl->ctrl.device,
740 "unable to set any I/O queues\n");
741 return -ENOMEM;
742 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000743
Olivier Deprez0e641232021-09-23 10:07:05 +0200744 ctrl->ctrl.queue_count = nr_io_queues + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745 dev_info(ctrl->ctrl.device,
746 "creating %d I/O queues.\n", nr_io_queues);
747
David Brazdil0f672f62019-12-10 10:32:29 +0000748 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
749 /*
750 * separate read/write queues
751 * hand out dedicated default queues only after we have
752 * sufficient read queues.
753 */
754 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
755 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
756 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
757 min(nr_default_queues, nr_io_queues);
758 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
759 } else {
760 /*
761 * shared read/write queues
762 * either no write queues were requested, or we don't have
763 * sufficient queue count to have dedicated default queues.
764 */
765 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
766 min(nr_read_queues, nr_io_queues);
767 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
768 }
769
770 if (opts->nr_poll_queues && nr_io_queues) {
771 /* map dedicated poll queues only if we have queues left */
772 ctrl->io_queues[HCTX_TYPE_POLL] =
773 min(nr_poll_queues, nr_io_queues);
774 }
775
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
777 ret = nvme_rdma_alloc_queue(ctrl, i,
778 ctrl->ctrl.sqsize + 1);
779 if (ret)
780 goto out_free_queues;
781 }
782
783 return 0;
784
785out_free_queues:
786 for (i--; i >= 1; i--)
787 nvme_rdma_free_queue(&ctrl->queues[i]);
788
789 return ret;
790}
791
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000792static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
793 bool admin)
794{
795 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
796 struct blk_mq_tag_set *set;
797 int ret;
798
799 if (admin) {
800 set = &ctrl->admin_tag_set;
801 memset(set, 0, sizeof(*set));
802 set->ops = &nvme_rdma_admin_mq_ops;
803 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
804 set->reserved_tags = 2; /* connect + keep-alive */
David Brazdil0f672f62019-12-10 10:32:29 +0000805 set->numa_node = nctrl->numa_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000806 set->cmd_size = sizeof(struct nvme_rdma_request) +
Olivier Deprez157378f2022-04-04 15:47:50 +0200807 NVME_RDMA_DATA_SGL_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000808 set->driver_data = ctrl;
809 set->nr_hw_queues = 1;
810 set->timeout = ADMIN_TIMEOUT;
811 set->flags = BLK_MQ_F_NO_SCHED;
812 } else {
813 set = &ctrl->tag_set;
814 memset(set, 0, sizeof(*set));
815 set->ops = &nvme_rdma_mq_ops;
816 set->queue_depth = nctrl->sqsize + 1;
817 set->reserved_tags = 1; /* fabric connect */
David Brazdil0f672f62019-12-10 10:32:29 +0000818 set->numa_node = nctrl->numa_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000819 set->flags = BLK_MQ_F_SHOULD_MERGE;
820 set->cmd_size = sizeof(struct nvme_rdma_request) +
Olivier Deprez157378f2022-04-04 15:47:50 +0200821 NVME_RDMA_DATA_SGL_SIZE;
822 if (nctrl->max_integrity_segments)
823 set->cmd_size += sizeof(struct nvme_rdma_sgl) +
824 NVME_RDMA_METADATA_SGL_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000825 set->driver_data = ctrl;
826 set->nr_hw_queues = nctrl->queue_count - 1;
827 set->timeout = NVME_IO_TIMEOUT;
David Brazdil0f672f62019-12-10 10:32:29 +0000828 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000829 }
830
831 ret = blk_mq_alloc_tag_set(set);
832 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000833 return ERR_PTR(ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834
835 return set;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000836}
837
838static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
839 bool remove)
840{
841 if (remove) {
842 blk_cleanup_queue(ctrl->ctrl.admin_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000843 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
844 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000845 }
846 if (ctrl->async_event_sqe.data) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200847 cancel_work_sync(&ctrl->ctrl.async_event_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000848 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
849 sizeof(struct nvme_command), DMA_TO_DEVICE);
850 ctrl->async_event_sqe.data = NULL;
851 }
852 nvme_rdma_free_queue(&ctrl->queues[0]);
853}
854
855static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
856 bool new)
857{
Olivier Deprez157378f2022-04-04 15:47:50 +0200858 bool pi_capable = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000859 int error;
860
861 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
862 if (error)
863 return error;
864
865 ctrl->device = ctrl->queues[0].device;
Olivier Deprez157378f2022-04-04 15:47:50 +0200866 ctrl->ctrl.numa_node = ibdev_to_node(ctrl->device->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000867
Olivier Deprez157378f2022-04-04 15:47:50 +0200868 /* T10-PI support */
869 if (ctrl->device->dev->attrs.device_cap_flags &
870 IB_DEVICE_INTEGRITY_HANDOVER)
871 pi_capable = true;
872
873 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev,
874 pi_capable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000875
David Brazdil0f672f62019-12-10 10:32:29 +0000876 /*
877 * Bind the async event SQE DMA mapping to the admin queue lifetime.
878 * It's safe, since any chage in the underlying RDMA device will issue
879 * error recovery and queue re-creation.
880 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000881 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
882 sizeof(struct nvme_command), DMA_TO_DEVICE);
883 if (error)
884 goto out_free_queue;
885
886 if (new) {
887 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
888 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
889 error = PTR_ERR(ctrl->ctrl.admin_tagset);
890 goto out_free_async_qe;
891 }
892
David Brazdil0f672f62019-12-10 10:32:29 +0000893 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
894 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
895 error = PTR_ERR(ctrl->ctrl.fabrics_q);
896 goto out_free_tagset;
897 }
898
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000899 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
900 if (IS_ERR(ctrl->ctrl.admin_q)) {
901 error = PTR_ERR(ctrl->ctrl.admin_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000902 goto out_cleanup_fabrics_q;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000903 }
904 }
905
906 error = nvme_rdma_start_queue(ctrl, 0);
907 if (error)
908 goto out_cleanup_queue;
909
David Brazdil0f672f62019-12-10 10:32:29 +0000910 error = nvme_enable_ctrl(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911 if (error)
912 goto out_stop_queue;
913
David Brazdil0f672f62019-12-10 10:32:29 +0000914 ctrl->ctrl.max_segments = ctrl->max_fr_pages;
915 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9);
Olivier Deprez157378f2022-04-04 15:47:50 +0200916 if (pi_capable)
917 ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages;
918 else
919 ctrl->ctrl.max_integrity_segments = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000920
921 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922
923 error = nvme_init_identify(&ctrl->ctrl);
924 if (error)
Olivier Deprez0e641232021-09-23 10:07:05 +0200925 goto out_quiesce_queue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926
927 return 0;
928
Olivier Deprez0e641232021-09-23 10:07:05 +0200929out_quiesce_queue:
930 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
931 blk_sync_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000932out_stop_queue:
933 nvme_rdma_stop_queue(&ctrl->queues[0]);
Olivier Deprez0e641232021-09-23 10:07:05 +0200934 nvme_cancel_admin_tagset(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000935out_cleanup_queue:
936 if (new)
937 blk_cleanup_queue(ctrl->ctrl.admin_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000938out_cleanup_fabrics_q:
939 if (new)
940 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941out_free_tagset:
942 if (new)
David Brazdil0f672f62019-12-10 10:32:29 +0000943 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944out_free_async_qe:
Olivier Deprez0e641232021-09-23 10:07:05 +0200945 if (ctrl->async_event_sqe.data) {
946 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
947 sizeof(struct nvme_command), DMA_TO_DEVICE);
948 ctrl->async_event_sqe.data = NULL;
949 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000950out_free_queue:
951 nvme_rdma_free_queue(&ctrl->queues[0]);
952 return error;
953}
954
955static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
956 bool remove)
957{
958 if (remove) {
959 blk_cleanup_queue(ctrl->ctrl.connect_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000960 blk_mq_free_tag_set(ctrl->ctrl.tagset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000961 }
962 nvme_rdma_free_io_queues(ctrl);
963}
964
965static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
966{
967 int ret;
968
969 ret = nvme_rdma_alloc_io_queues(ctrl);
970 if (ret)
971 return ret;
972
973 if (new) {
974 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
975 if (IS_ERR(ctrl->ctrl.tagset)) {
976 ret = PTR_ERR(ctrl->ctrl.tagset);
977 goto out_free_io_queues;
978 }
979
980 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
981 if (IS_ERR(ctrl->ctrl.connect_q)) {
982 ret = PTR_ERR(ctrl->ctrl.connect_q);
983 goto out_free_tag_set;
984 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000985 }
986
987 ret = nvme_rdma_start_io_queues(ctrl);
988 if (ret)
989 goto out_cleanup_connect_q;
990
Olivier Deprez0e641232021-09-23 10:07:05 +0200991 if (!new) {
992 nvme_start_queues(&ctrl->ctrl);
993 if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) {
994 /*
995 * If we timed out waiting for freeze we are likely to
996 * be stuck. Fail the controller initialization just
997 * to be safe.
998 */
999 ret = -ENODEV;
1000 goto out_wait_freeze_timed_out;
1001 }
1002 blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset,
1003 ctrl->ctrl.queue_count - 1);
1004 nvme_unfreeze(&ctrl->ctrl);
1005 }
1006
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007 return 0;
1008
Olivier Deprez0e641232021-09-23 10:07:05 +02001009out_wait_freeze_timed_out:
1010 nvme_stop_queues(&ctrl->ctrl);
1011 nvme_sync_io_queues(&ctrl->ctrl);
1012 nvme_rdma_stop_io_queues(ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001013out_cleanup_connect_q:
Olivier Deprez0e641232021-09-23 10:07:05 +02001014 nvme_cancel_tagset(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001015 if (new)
1016 blk_cleanup_queue(ctrl->ctrl.connect_q);
1017out_free_tag_set:
1018 if (new)
David Brazdil0f672f62019-12-10 10:32:29 +00001019 blk_mq_free_tag_set(ctrl->ctrl.tagset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001020out_free_io_queues:
1021 nvme_rdma_free_io_queues(ctrl);
1022 return ret;
1023}
1024
1025static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
1026 bool remove)
1027{
1028 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Olivier Deprez0e641232021-09-23 10:07:05 +02001029 blk_sync_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 nvme_rdma_stop_queue(&ctrl->queues[0]);
David Brazdil0f672f62019-12-10 10:32:29 +00001031 if (ctrl->ctrl.admin_tagset) {
1032 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
1033 nvme_cancel_request, &ctrl->ctrl);
1034 blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset);
1035 }
1036 if (remove)
1037 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 nvme_rdma_destroy_admin_queue(ctrl, remove);
1039}
1040
1041static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
1042 bool remove)
1043{
1044 if (ctrl->ctrl.queue_count > 1) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001045 nvme_start_freeze(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001046 nvme_stop_queues(&ctrl->ctrl);
Olivier Deprez0e641232021-09-23 10:07:05 +02001047 nvme_sync_io_queues(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001048 nvme_rdma_stop_io_queues(ctrl);
David Brazdil0f672f62019-12-10 10:32:29 +00001049 if (ctrl->ctrl.tagset) {
1050 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
1051 nvme_cancel_request, &ctrl->ctrl);
1052 blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset);
1053 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001054 if (remove)
1055 nvme_start_queues(&ctrl->ctrl);
1056 nvme_rdma_destroy_io_queues(ctrl, remove);
1057 }
1058}
1059
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001060static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
1061{
1062 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1063
1064 if (list_empty(&ctrl->list))
1065 goto free_ctrl;
1066
1067 mutex_lock(&nvme_rdma_ctrl_mutex);
1068 list_del(&ctrl->list);
1069 mutex_unlock(&nvme_rdma_ctrl_mutex);
1070
1071 nvmf_free_options(nctrl->opts);
1072free_ctrl:
1073 kfree(ctrl->queues);
1074 kfree(ctrl);
1075}
1076
1077static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
1078{
1079 /* If we are resetting/deleting then do nothing */
1080 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
1081 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
1082 ctrl->ctrl.state == NVME_CTRL_LIVE);
1083 return;
1084 }
1085
1086 if (nvmf_should_reconnect(&ctrl->ctrl)) {
1087 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
1088 ctrl->ctrl.opts->reconnect_delay);
1089 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
1090 ctrl->ctrl.opts->reconnect_delay * HZ);
1091 } else {
1092 nvme_delete_ctrl(&ctrl->ctrl);
1093 }
1094}
1095
1096static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
1097{
1098 int ret = -EINVAL;
1099 bool changed;
1100
1101 ret = nvme_rdma_configure_admin_queue(ctrl, new);
1102 if (ret)
1103 return ret;
1104
1105 if (ctrl->ctrl.icdoff) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001106 ret = -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1108 goto destroy_admin;
1109 }
1110
1111 if (!(ctrl->ctrl.sgls & (1 << 2))) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001112 ret = -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001113 dev_err(ctrl->ctrl.device,
1114 "Mandatory keyed sgls are not supported!\n");
1115 goto destroy_admin;
1116 }
1117
1118 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
1119 dev_warn(ctrl->ctrl.device,
1120 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1121 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
1122 }
1123
1124 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1125 dev_warn(ctrl->ctrl.device,
1126 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1127 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1128 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1129 }
1130
1131 if (ctrl->ctrl.sgls & (1 << 20))
1132 ctrl->use_inline_data = true;
1133
1134 if (ctrl->ctrl.queue_count > 1) {
1135 ret = nvme_rdma_configure_io_queues(ctrl, new);
1136 if (ret)
1137 goto destroy_admin;
1138 }
1139
1140 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1141 if (!changed) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001142 /*
1143 * state change failure is ok if we started ctrl delete,
1144 * unless we're during creation of a new controller to
1145 * avoid races with teardown flow.
1146 */
1147 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1148 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1149 WARN_ON_ONCE(new);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001150 ret = -EINVAL;
1151 goto destroy_io;
1152 }
1153
1154 nvme_start_ctrl(&ctrl->ctrl);
1155 return 0;
1156
1157destroy_io:
Olivier Deprez0e641232021-09-23 10:07:05 +02001158 if (ctrl->ctrl.queue_count > 1) {
1159 nvme_stop_queues(&ctrl->ctrl);
1160 nvme_sync_io_queues(&ctrl->ctrl);
1161 nvme_rdma_stop_io_queues(ctrl);
1162 nvme_cancel_tagset(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001163 nvme_rdma_destroy_io_queues(ctrl, new);
Olivier Deprez0e641232021-09-23 10:07:05 +02001164 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001165destroy_admin:
Olivier Deprez0e641232021-09-23 10:07:05 +02001166 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1167 blk_sync_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001168 nvme_rdma_stop_queue(&ctrl->queues[0]);
Olivier Deprez0e641232021-09-23 10:07:05 +02001169 nvme_cancel_admin_tagset(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001170 nvme_rdma_destroy_admin_queue(ctrl, new);
1171 return ret;
1172}
1173
1174static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1175{
1176 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1177 struct nvme_rdma_ctrl, reconnect_work);
1178
1179 ++ctrl->ctrl.nr_reconnects;
1180
1181 if (nvme_rdma_setup_ctrl(ctrl, false))
1182 goto requeue;
1183
1184 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1185 ctrl->ctrl.nr_reconnects);
1186
1187 ctrl->ctrl.nr_reconnects = 0;
1188
1189 return;
1190
1191requeue:
1192 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1193 ctrl->ctrl.nr_reconnects);
1194 nvme_rdma_reconnect_or_remove(ctrl);
1195}
1196
1197static void nvme_rdma_error_recovery_work(struct work_struct *work)
1198{
1199 struct nvme_rdma_ctrl *ctrl = container_of(work,
1200 struct nvme_rdma_ctrl, err_work);
1201
1202 nvme_stop_keep_alive(&ctrl->ctrl);
Olivier Deprez157378f2022-04-04 15:47:50 +02001203 flush_work(&ctrl->ctrl.async_event_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204 nvme_rdma_teardown_io_queues(ctrl, false);
1205 nvme_start_queues(&ctrl->ctrl);
1206 nvme_rdma_teardown_admin_queue(ctrl, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001207 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001208
1209 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001210 /* state change failure is ok if we started ctrl delete */
1211 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1212 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001213 return;
1214 }
1215
1216 nvme_rdma_reconnect_or_remove(ctrl);
1217}
1218
1219static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1220{
1221 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1222 return;
1223
Olivier Deprez0e641232021-09-23 10:07:05 +02001224 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1225 queue_work(nvme_reset_wq, &ctrl->err_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001226}
1227
Olivier Deprez157378f2022-04-04 15:47:50 +02001228static void nvme_rdma_end_request(struct nvme_rdma_request *req)
1229{
1230 struct request *rq = blk_mq_rq_from_pdu(req);
1231
1232 if (!refcount_dec_and_test(&req->ref))
1233 return;
1234 if (!nvme_try_complete_req(rq, req->status, req->result))
1235 nvme_rdma_complete_rq(rq);
1236}
1237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001238static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1239 const char *op)
1240{
Olivier Deprez157378f2022-04-04 15:47:50 +02001241 struct nvme_rdma_queue *queue = wc->qp->qp_context;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001242 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1243
1244 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1245 dev_info(ctrl->ctrl.device,
1246 "%s for CQE 0x%p failed with status %s (%d)\n",
1247 op, wc->wr_cqe,
1248 ib_wc_status_msg(wc->status), wc->status);
1249 nvme_rdma_error_recovery(ctrl);
1250}
1251
1252static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1253{
1254 if (unlikely(wc->status != IB_WC_SUCCESS))
1255 nvme_rdma_wr_error(cq, wc, "MEMREG");
1256}
1257
1258static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1259{
1260 struct nvme_rdma_request *req =
1261 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001262
Olivier Deprez157378f2022-04-04 15:47:50 +02001263 if (unlikely(wc->status != IB_WC_SUCCESS))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001264 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
Olivier Deprez157378f2022-04-04 15:47:50 +02001265 else
1266 nvme_rdma_end_request(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001267}
1268
1269static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1270 struct nvme_rdma_request *req)
1271{
1272 struct ib_send_wr wr = {
1273 .opcode = IB_WR_LOCAL_INV,
1274 .next = NULL,
1275 .num_sge = 0,
1276 .send_flags = IB_SEND_SIGNALED,
1277 .ex.invalidate_rkey = req->mr->rkey,
1278 };
1279
1280 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1281 wr.wr_cqe = &req->reg_cqe;
1282
1283 return ib_post_send(queue->qp, &wr, NULL);
1284}
1285
1286static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1287 struct request *rq)
1288{
1289 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1290 struct nvme_rdma_device *dev = queue->device;
1291 struct ib_device *ibdev = dev->dev;
Olivier Deprez157378f2022-04-04 15:47:50 +02001292 struct list_head *pool = &queue->qp->rdma_mrs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001293
David Brazdil0f672f62019-12-10 10:32:29 +00001294 if (!blk_rq_nr_phys_segments(rq))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001295 return;
1296
Olivier Deprez157378f2022-04-04 15:47:50 +02001297 if (blk_integrity_rq(rq)) {
1298 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1299 req->metadata_sgl->nents, rq_dma_dir(rq));
1300 sg_free_table_chained(&req->metadata_sgl->sg_table,
1301 NVME_INLINE_METADATA_SG_CNT);
1302 }
1303
1304 if (req->use_sig_mr)
1305 pool = &queue->qp->sig_mrs;
1306
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001307 if (req->mr) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001308 ib_mr_pool_put(queue->qp, pool, req->mr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001309 req->mr = NULL;
1310 }
1311
Olivier Deprez157378f2022-04-04 15:47:50 +02001312 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1313 rq_dma_dir(rq));
1314 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001315}
1316
1317static int nvme_rdma_set_sg_null(struct nvme_command *c)
1318{
1319 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1320
1321 sg->addr = 0;
1322 put_unaligned_le24(0, sg->length);
1323 put_unaligned_le32(0, sg->key);
1324 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1325 return 0;
1326}
1327
1328static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1329 struct nvme_rdma_request *req, struct nvme_command *c,
1330 int count)
1331{
1332 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001333 struct ib_sge *sge = &req->sge[1];
Olivier Deprez157378f2022-04-04 15:47:50 +02001334 struct scatterlist *sgl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001335 u32 len = 0;
1336 int i;
1337
Olivier Deprez157378f2022-04-04 15:47:50 +02001338 for_each_sg(req->data_sgl.sg_table.sgl, sgl, count, i) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001339 sge->addr = sg_dma_address(sgl);
1340 sge->length = sg_dma_len(sgl);
1341 sge->lkey = queue->device->pd->local_dma_lkey;
1342 len += sge->length;
Olivier Deprez157378f2022-04-04 15:47:50 +02001343 sge++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001344 }
1345
1346 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1347 sg->length = cpu_to_le32(len);
1348 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1349
1350 req->num_sge += count;
1351 return 0;
1352}
1353
1354static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1355 struct nvme_rdma_request *req, struct nvme_command *c)
1356{
1357 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1358
Olivier Deprez157378f2022-04-04 15:47:50 +02001359 sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl));
1360 put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001361 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1362 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1363 return 0;
1364}
1365
1366static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1367 struct nvme_rdma_request *req, struct nvme_command *c,
1368 int count)
1369{
1370 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1371 int nr;
1372
1373 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1374 if (WARN_ON_ONCE(!req->mr))
1375 return -EAGAIN;
1376
1377 /*
1378 * Align the MR to a 4K page size to match the ctrl page size and
1379 * the block virtual boundary.
1380 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001381 nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL,
1382 SZ_4K);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001383 if (unlikely(nr < count)) {
1384 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1385 req->mr = NULL;
1386 if (nr < 0)
1387 return nr;
1388 return -EINVAL;
1389 }
1390
1391 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1392
1393 req->reg_cqe.done = nvme_rdma_memreg_done;
1394 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1395 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1396 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1397 req->reg_wr.wr.num_sge = 0;
1398 req->reg_wr.mr = req->mr;
1399 req->reg_wr.key = req->mr->rkey;
1400 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1401 IB_ACCESS_REMOTE_READ |
1402 IB_ACCESS_REMOTE_WRITE;
1403
1404 sg->addr = cpu_to_le64(req->mr->iova);
1405 put_unaligned_le24(req->mr->length, sg->length);
1406 put_unaligned_le32(req->mr->rkey, sg->key);
1407 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1408 NVME_SGL_FMT_INVALIDATE;
1409
1410 return 0;
1411}
1412
Olivier Deprez157378f2022-04-04 15:47:50 +02001413static void nvme_rdma_set_sig_domain(struct blk_integrity *bi,
1414 struct nvme_command *cmd, struct ib_sig_domain *domain,
1415 u16 control, u8 pi_type)
1416{
1417 domain->sig_type = IB_SIG_TYPE_T10_DIF;
1418 domain->sig.dif.bg_type = IB_T10DIF_CRC;
1419 domain->sig.dif.pi_interval = 1 << bi->interval_exp;
1420 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
1421 if (control & NVME_RW_PRINFO_PRCHK_REF)
1422 domain->sig.dif.ref_remap = true;
1423
1424 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
1425 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
1426 domain->sig.dif.app_escape = true;
1427 if (pi_type == NVME_NS_DPS_PI_TYPE3)
1428 domain->sig.dif.ref_escape = true;
1429}
1430
1431static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi,
1432 struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs,
1433 u8 pi_type)
1434{
1435 u16 control = le16_to_cpu(cmd->rw.control);
1436
1437 memset(sig_attrs, 0, sizeof(*sig_attrs));
1438 if (control & NVME_RW_PRINFO_PRACT) {
1439 /* for WRITE_INSERT/READ_STRIP no memory domain */
1440 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1441 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1442 pi_type);
1443 /* Clear the PRACT bit since HCA will generate/verify the PI */
1444 control &= ~NVME_RW_PRINFO_PRACT;
1445 cmd->rw.control = cpu_to_le16(control);
1446 } else {
1447 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
1448 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1449 pi_type);
1450 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
1451 pi_type);
1452 }
1453}
1454
1455static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask)
1456{
1457 *mask = 0;
1458 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF)
1459 *mask |= IB_SIG_CHECK_REFTAG;
1460 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD)
1461 *mask |= IB_SIG_CHECK_GUARD;
1462}
1463
1464static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc)
1465{
1466 if (unlikely(wc->status != IB_WC_SUCCESS))
1467 nvme_rdma_wr_error(cq, wc, "SIG");
1468}
1469
1470static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
1471 struct nvme_rdma_request *req, struct nvme_command *c,
1472 int count, int pi_count)
1473{
1474 struct nvme_rdma_sgl *sgl = &req->data_sgl;
1475 struct ib_reg_wr *wr = &req->reg_wr;
1476 struct request *rq = blk_mq_rq_from_pdu(req);
1477 struct nvme_ns *ns = rq->q->queuedata;
1478 struct bio *bio = rq->bio;
1479 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1480 int nr;
1481
1482 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs);
1483 if (WARN_ON_ONCE(!req->mr))
1484 return -EAGAIN;
1485
1486 nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL,
1487 req->metadata_sgl->sg_table.sgl, pi_count, NULL,
1488 SZ_4K);
1489 if (unlikely(nr))
1490 goto mr_put;
1491
1492 nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_disk), c,
1493 req->mr->sig_attrs, ns->pi_type);
1494 nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask);
1495
1496 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1497
1498 req->reg_cqe.done = nvme_rdma_sig_done;
1499 memset(wr, 0, sizeof(*wr));
1500 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
1501 wr->wr.wr_cqe = &req->reg_cqe;
1502 wr->wr.num_sge = 0;
1503 wr->wr.send_flags = 0;
1504 wr->mr = req->mr;
1505 wr->key = req->mr->rkey;
1506 wr->access = IB_ACCESS_LOCAL_WRITE |
1507 IB_ACCESS_REMOTE_READ |
1508 IB_ACCESS_REMOTE_WRITE;
1509
1510 sg->addr = cpu_to_le64(req->mr->iova);
1511 put_unaligned_le24(req->mr->length, sg->length);
1512 put_unaligned_le32(req->mr->rkey, sg->key);
1513 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1514
1515 return 0;
1516
1517mr_put:
1518 ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr);
1519 req->mr = NULL;
1520 if (nr < 0)
1521 return nr;
1522 return -EINVAL;
1523}
1524
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001525static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1526 struct request *rq, struct nvme_command *c)
1527{
1528 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1529 struct nvme_rdma_device *dev = queue->device;
1530 struct ib_device *ibdev = dev->dev;
Olivier Deprez157378f2022-04-04 15:47:50 +02001531 int pi_count = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001532 int count, ret;
1533
1534 req->num_sge = 1;
1535 refcount_set(&req->ref, 2); /* send and recv completions */
1536
1537 c->common.flags |= NVME_CMD_SGL_METABUF;
1538
David Brazdil0f672f62019-12-10 10:32:29 +00001539 if (!blk_rq_nr_phys_segments(rq))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001540 return nvme_rdma_set_sg_null(c);
1541
Olivier Deprez157378f2022-04-04 15:47:50 +02001542 req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1);
1543 ret = sg_alloc_table_chained(&req->data_sgl.sg_table,
1544 blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl,
1545 NVME_INLINE_SG_CNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001546 if (ret)
1547 return -ENOMEM;
1548
Olivier Deprez157378f2022-04-04 15:47:50 +02001549 req->data_sgl.nents = blk_rq_map_sg(rq->q, rq,
1550 req->data_sgl.sg_table.sgl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001551
Olivier Deprez157378f2022-04-04 15:47:50 +02001552 count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl,
1553 req->data_sgl.nents, rq_dma_dir(rq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554 if (unlikely(count <= 0)) {
1555 ret = -EIO;
1556 goto out_free_table;
1557 }
1558
Olivier Deprez157378f2022-04-04 15:47:50 +02001559 if (blk_integrity_rq(rq)) {
1560 req->metadata_sgl->sg_table.sgl =
1561 (struct scatterlist *)(req->metadata_sgl + 1);
1562 ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table,
1563 blk_rq_count_integrity_sg(rq->q, rq->bio),
1564 req->metadata_sgl->sg_table.sgl,
1565 NVME_INLINE_METADATA_SG_CNT);
1566 if (unlikely(ret)) {
1567 ret = -ENOMEM;
1568 goto out_unmap_sg;
1569 }
1570
1571 req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q,
1572 rq->bio, req->metadata_sgl->sg_table.sgl);
1573 pi_count = ib_dma_map_sg(ibdev,
1574 req->metadata_sgl->sg_table.sgl,
1575 req->metadata_sgl->nents,
1576 rq_dma_dir(rq));
1577 if (unlikely(pi_count <= 0)) {
1578 ret = -EIO;
1579 goto out_free_pi_table;
1580 }
1581 }
1582
1583 if (req->use_sig_mr) {
1584 ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count);
1585 goto out;
1586 }
1587
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588 if (count <= dev->num_inline_segments) {
1589 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1590 queue->ctrl->use_inline_data &&
1591 blk_rq_payload_bytes(rq) <=
1592 nvme_rdma_inline_data_size(queue)) {
1593 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1594 goto out;
1595 }
1596
1597 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1598 ret = nvme_rdma_map_sg_single(queue, req, c);
1599 goto out;
1600 }
1601 }
1602
1603 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1604out:
1605 if (unlikely(ret))
Olivier Deprez157378f2022-04-04 15:47:50 +02001606 goto out_unmap_pi_sg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001607
1608 return 0;
1609
Olivier Deprez157378f2022-04-04 15:47:50 +02001610out_unmap_pi_sg:
1611 if (blk_integrity_rq(rq))
1612 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1613 req->metadata_sgl->nents, rq_dma_dir(rq));
1614out_free_pi_table:
1615 if (blk_integrity_rq(rq))
1616 sg_free_table_chained(&req->metadata_sgl->sg_table,
1617 NVME_INLINE_METADATA_SG_CNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001618out_unmap_sg:
Olivier Deprez157378f2022-04-04 15:47:50 +02001619 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1620 rq_dma_dir(rq));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001621out_free_table:
Olivier Deprez157378f2022-04-04 15:47:50 +02001622 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001623 return ret;
1624}
1625
1626static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1627{
1628 struct nvme_rdma_qe *qe =
1629 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1630 struct nvme_rdma_request *req =
1631 container_of(qe, struct nvme_rdma_request, sqe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001632
Olivier Deprez157378f2022-04-04 15:47:50 +02001633 if (unlikely(wc->status != IB_WC_SUCCESS))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001634 nvme_rdma_wr_error(cq, wc, "SEND");
Olivier Deprez157378f2022-04-04 15:47:50 +02001635 else
1636 nvme_rdma_end_request(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001637}
1638
1639static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1640 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1641 struct ib_send_wr *first)
1642{
1643 struct ib_send_wr wr;
1644 int ret;
1645
1646 sge->addr = qe->dma;
Olivier Deprez157378f2022-04-04 15:47:50 +02001647 sge->length = sizeof(struct nvme_command);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001648 sge->lkey = queue->device->pd->local_dma_lkey;
1649
1650 wr.next = NULL;
1651 wr.wr_cqe = &qe->cqe;
1652 wr.sg_list = sge;
1653 wr.num_sge = num_sge;
1654 wr.opcode = IB_WR_SEND;
1655 wr.send_flags = IB_SEND_SIGNALED;
1656
1657 if (first)
1658 first->next = &wr;
1659 else
1660 first = &wr;
1661
1662 ret = ib_post_send(queue->qp, first, NULL);
1663 if (unlikely(ret)) {
1664 dev_err(queue->ctrl->ctrl.device,
1665 "%s failed with error code %d\n", __func__, ret);
1666 }
1667 return ret;
1668}
1669
1670static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1671 struct nvme_rdma_qe *qe)
1672{
1673 struct ib_recv_wr wr;
1674 struct ib_sge list;
1675 int ret;
1676
1677 list.addr = qe->dma;
1678 list.length = sizeof(struct nvme_completion);
1679 list.lkey = queue->device->pd->local_dma_lkey;
1680
1681 qe->cqe.done = nvme_rdma_recv_done;
1682
1683 wr.next = NULL;
1684 wr.wr_cqe = &qe->cqe;
1685 wr.sg_list = &list;
1686 wr.num_sge = 1;
1687
1688 ret = ib_post_recv(queue->qp, &wr, NULL);
1689 if (unlikely(ret)) {
1690 dev_err(queue->ctrl->ctrl.device,
1691 "%s failed with error code %d\n", __func__, ret);
1692 }
1693 return ret;
1694}
1695
1696static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1697{
1698 u32 queue_idx = nvme_rdma_queue_idx(queue);
1699
1700 if (queue_idx == 0)
1701 return queue->ctrl->admin_tag_set.tags[queue_idx];
1702 return queue->ctrl->tag_set.tags[queue_idx - 1];
1703}
1704
1705static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1706{
1707 if (unlikely(wc->status != IB_WC_SUCCESS))
1708 nvme_rdma_wr_error(cq, wc, "ASYNC");
1709}
1710
1711static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1712{
1713 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1714 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1715 struct ib_device *dev = queue->device->dev;
1716 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1717 struct nvme_command *cmd = sqe->data;
1718 struct ib_sge sge;
1719 int ret;
1720
1721 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1722
1723 memset(cmd, 0, sizeof(*cmd));
1724 cmd->common.opcode = nvme_admin_async_event;
1725 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1726 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1727 nvme_rdma_set_sg_null(cmd);
1728
1729 sqe->cqe.done = nvme_rdma_async_done;
1730
1731 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1732 DMA_TO_DEVICE);
1733
1734 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1735 WARN_ON_ONCE(ret);
1736}
1737
David Brazdil0f672f62019-12-10 10:32:29 +00001738static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1739 struct nvme_completion *cqe, struct ib_wc *wc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001740{
1741 struct request *rq;
1742 struct nvme_rdma_request *req;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001743
Olivier Deprez157378f2022-04-04 15:47:50 +02001744 rq = nvme_find_rq(nvme_rdma_tagset(queue), cqe->command_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001745 if (!rq) {
1746 dev_err(queue->ctrl->ctrl.device,
Olivier Deprez157378f2022-04-04 15:47:50 +02001747 "got bad command_id %#x on QP %#x\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001748 cqe->command_id, queue->qp->qp_num);
1749 nvme_rdma_error_recovery(queue->ctrl);
David Brazdil0f672f62019-12-10 10:32:29 +00001750 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001751 }
1752 req = blk_mq_rq_to_pdu(rq);
1753
1754 req->status = cqe->status;
1755 req->result = cqe->result;
1756
1757 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001758 if (unlikely(!req->mr ||
1759 wc->ex.invalidate_rkey != req->mr->rkey)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001760 dev_err(queue->ctrl->ctrl.device,
1761 "Bogus remote invalidation for rkey %#x\n",
Olivier Deprez157378f2022-04-04 15:47:50 +02001762 req->mr ? req->mr->rkey : 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001763 nvme_rdma_error_recovery(queue->ctrl);
1764 }
1765 } else if (req->mr) {
David Brazdil0f672f62019-12-10 10:32:29 +00001766 int ret;
1767
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001768 ret = nvme_rdma_inv_rkey(queue, req);
1769 if (unlikely(ret < 0)) {
1770 dev_err(queue->ctrl->ctrl.device,
1771 "Queueing INV WR for rkey %#x failed (%d)\n",
1772 req->mr->rkey, ret);
1773 nvme_rdma_error_recovery(queue->ctrl);
1774 }
1775 /* the local invalidation completion will end the request */
David Brazdil0f672f62019-12-10 10:32:29 +00001776 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001777 }
1778
Olivier Deprez157378f2022-04-04 15:47:50 +02001779 nvme_rdma_end_request(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001780}
1781
David Brazdil0f672f62019-12-10 10:32:29 +00001782static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001783{
1784 struct nvme_rdma_qe *qe =
1785 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
Olivier Deprez157378f2022-04-04 15:47:50 +02001786 struct nvme_rdma_queue *queue = wc->qp->qp_context;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001787 struct ib_device *ibdev = queue->device->dev;
1788 struct nvme_completion *cqe = qe->data;
1789 const size_t len = sizeof(struct nvme_completion);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001790
1791 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1792 nvme_rdma_wr_error(cq, wc, "RECV");
David Brazdil0f672f62019-12-10 10:32:29 +00001793 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001794 }
1795
Olivier Deprez0e641232021-09-23 10:07:05 +02001796 /* sanity checking for received data length */
1797 if (unlikely(wc->byte_len < len)) {
1798 dev_err(queue->ctrl->ctrl.device,
1799 "Unexpected nvme completion length(%d)\n", wc->byte_len);
1800 nvme_rdma_error_recovery(queue->ctrl);
1801 return;
1802 }
1803
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001804 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1805 /*
1806 * AEN requests are special as they don't time out and can
1807 * survive any kind of queue freeze and often don't respond to
1808 * aborts. We don't even bother to allocate a struct request
1809 * for them but rather special case them here.
1810 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001811 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue),
1812 cqe->command_id)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1814 &cqe->result);
1815 else
David Brazdil0f672f62019-12-10 10:32:29 +00001816 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001817 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1818
1819 nvme_rdma_post_recv(queue, qe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001820}
1821
1822static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1823{
1824 int ret, i;
1825
1826 for (i = 0; i < queue->queue_size; i++) {
1827 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1828 if (ret)
Olivier Deprez157378f2022-04-04 15:47:50 +02001829 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001830 }
1831
1832 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001833}
1834
1835static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1836 struct rdma_cm_event *ev)
1837{
1838 struct rdma_cm_id *cm_id = queue->cm_id;
1839 int status = ev->status;
1840 const char *rej_msg;
1841 const struct nvme_rdma_cm_rej *rej_data;
1842 u8 rej_data_len;
1843
1844 rej_msg = rdma_reject_msg(cm_id, status);
1845 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1846
1847 if (rej_data && rej_data_len >= sizeof(u16)) {
1848 u16 sts = le16_to_cpu(rej_data->sts);
1849
1850 dev_err(queue->ctrl->ctrl.device,
1851 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1852 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1853 } else {
1854 dev_err(queue->ctrl->ctrl.device,
1855 "Connect rejected: status %d (%s).\n", status, rej_msg);
1856 }
1857
1858 return -ECONNRESET;
1859}
1860
1861static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1862{
David Brazdil0f672f62019-12-10 10:32:29 +00001863 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864 int ret;
1865
1866 ret = nvme_rdma_create_queue_ib(queue);
1867 if (ret)
1868 return ret;
1869
David Brazdil0f672f62019-12-10 10:32:29 +00001870 if (ctrl->opts->tos >= 0)
1871 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001872 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1873 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00001874 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001875 queue->cm_error);
1876 goto out_destroy_queue;
1877 }
1878
1879 return 0;
1880
1881out_destroy_queue:
1882 nvme_rdma_destroy_queue_ib(queue);
1883 return ret;
1884}
1885
1886static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1887{
1888 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1889 struct rdma_conn_param param = { };
1890 struct nvme_rdma_cm_req priv = { };
1891 int ret;
1892
1893 param.qp_num = queue->qp->qp_num;
1894 param.flow_control = 1;
1895
1896 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1897 /* maximum retry count */
1898 param.retry_count = 7;
1899 param.rnr_retry_count = 7;
1900 param.private_data = &priv;
1901 param.private_data_len = sizeof(priv);
1902
1903 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1904 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1905 /*
1906 * set the admin queue depth to the minimum size
1907 * specified by the Fabrics standard.
1908 */
1909 if (priv.qid == 0) {
1910 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1911 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1912 } else {
1913 /*
1914 * current interpretation of the fabrics spec
1915 * is at minimum you make hrqsize sqsize+1, or a
1916 * 1's based representation of sqsize.
1917 */
1918 priv.hrqsize = cpu_to_le16(queue->queue_size);
1919 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1920 }
1921
Olivier Deprez157378f2022-04-04 15:47:50 +02001922 ret = rdma_connect_locked(queue->cm_id, &param);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001923 if (ret) {
1924 dev_err(ctrl->ctrl.device,
Olivier Deprez157378f2022-04-04 15:47:50 +02001925 "rdma_connect_locked failed (%d).\n", ret);
1926 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001927 }
1928
1929 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001930}
1931
1932static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1933 struct rdma_cm_event *ev)
1934{
1935 struct nvme_rdma_queue *queue = cm_id->context;
1936 int cm_error = 0;
1937
1938 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1939 rdma_event_msg(ev->event), ev->event,
1940 ev->status, cm_id);
1941
1942 switch (ev->event) {
1943 case RDMA_CM_EVENT_ADDR_RESOLVED:
1944 cm_error = nvme_rdma_addr_resolved(queue);
1945 break;
1946 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1947 cm_error = nvme_rdma_route_resolved(queue);
1948 break;
1949 case RDMA_CM_EVENT_ESTABLISHED:
1950 queue->cm_error = nvme_rdma_conn_established(queue);
1951 /* complete cm_done regardless of success/failure */
1952 complete(&queue->cm_done);
1953 return 0;
1954 case RDMA_CM_EVENT_REJECTED:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001955 cm_error = nvme_rdma_conn_rejected(queue, ev);
1956 break;
1957 case RDMA_CM_EVENT_ROUTE_ERROR:
1958 case RDMA_CM_EVENT_CONNECT_ERROR:
1959 case RDMA_CM_EVENT_UNREACHABLE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001960 case RDMA_CM_EVENT_ADDR_ERROR:
1961 dev_dbg(queue->ctrl->ctrl.device,
1962 "CM error event %d\n", ev->event);
1963 cm_error = -ECONNRESET;
1964 break;
1965 case RDMA_CM_EVENT_DISCONNECTED:
1966 case RDMA_CM_EVENT_ADDR_CHANGE:
1967 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1968 dev_dbg(queue->ctrl->ctrl.device,
1969 "disconnect received - connection closed\n");
1970 nvme_rdma_error_recovery(queue->ctrl);
1971 break;
1972 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1973 /* device removal is handled via the ib_client API */
1974 break;
1975 default:
1976 dev_err(queue->ctrl->ctrl.device,
1977 "Unexpected RDMA CM event (%d)\n", ev->event);
1978 nvme_rdma_error_recovery(queue->ctrl);
1979 break;
1980 }
1981
1982 if (cm_error) {
1983 queue->cm_error = cm_error;
1984 complete(&queue->cm_done);
1985 }
1986
1987 return 0;
1988}
1989
Olivier Deprez0e641232021-09-23 10:07:05 +02001990static void nvme_rdma_complete_timed_out(struct request *rq)
1991{
1992 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1993 struct nvme_rdma_queue *queue = req->queue;
1994
1995 nvme_rdma_stop_queue(queue);
1996 if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
1997 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
1998 blk_mq_complete_request(rq);
1999 }
2000}
2001
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002002static enum blk_eh_timer_return
2003nvme_rdma_timeout(struct request *rq, bool reserved)
2004{
2005 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
David Brazdil0f672f62019-12-10 10:32:29 +00002006 struct nvme_rdma_queue *queue = req->queue;
2007 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002008
David Brazdil0f672f62019-12-10 10:32:29 +00002009 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
2010 rq->tag, nvme_rdma_queue_idx(queue));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002011
David Brazdil0f672f62019-12-10 10:32:29 +00002012 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
2013 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02002014 * If we are resetting, connecting or deleting we should
2015 * complete immediately because we may block controller
2016 * teardown or setup sequence
2017 * - ctrl disable/shutdown fabrics requests
2018 * - connect requests
2019 * - initialization admin requests
2020 * - I/O requests that entered after unquiescing and
2021 * the controller stopped responding
2022 *
2023 * All other requests should be cancelled by the error
2024 * recovery work, so it's fine that we fail it here.
David Brazdil0f672f62019-12-10 10:32:29 +00002025 */
Olivier Deprez0e641232021-09-23 10:07:05 +02002026 nvme_rdma_complete_timed_out(rq);
David Brazdil0f672f62019-12-10 10:32:29 +00002027 return BLK_EH_DONE;
2028 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002029
Olivier Deprez0e641232021-09-23 10:07:05 +02002030 /*
2031 * LIVE state should trigger the normal error recovery which will
2032 * handle completing this request.
2033 */
David Brazdil0f672f62019-12-10 10:32:29 +00002034 nvme_rdma_error_recovery(ctrl);
David Brazdil0f672f62019-12-10 10:32:29 +00002035 return BLK_EH_RESET_TIMER;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002036}
2037
2038static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
2039 const struct blk_mq_queue_data *bd)
2040{
2041 struct nvme_ns *ns = hctx->queue->queuedata;
2042 struct nvme_rdma_queue *queue = hctx->driver_data;
2043 struct request *rq = bd->rq;
2044 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2045 struct nvme_rdma_qe *sqe = &req->sqe;
2046 struct nvme_command *c = sqe->data;
2047 struct ib_device *dev;
2048 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
2049 blk_status_t ret;
2050 int err;
2051
2052 WARN_ON_ONCE(rq->tag < 0);
2053
2054 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2055 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2056
2057 dev = queue->device->dev;
David Brazdil0f672f62019-12-10 10:32:29 +00002058
2059 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
2060 sizeof(struct nvme_command),
2061 DMA_TO_DEVICE);
2062 err = ib_dma_mapping_error(dev, req->sqe.dma);
2063 if (unlikely(err))
2064 return BLK_STS_RESOURCE;
2065
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002066 ib_dma_sync_single_for_cpu(dev, sqe->dma,
2067 sizeof(struct nvme_command), DMA_TO_DEVICE);
2068
2069 ret = nvme_setup_cmd(ns, rq, c);
2070 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +00002071 goto unmap_qe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002072
2073 blk_mq_start_request(rq);
2074
Olivier Deprez157378f2022-04-04 15:47:50 +02002075 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
2076 queue->pi_support &&
2077 (c->common.opcode == nvme_cmd_write ||
2078 c->common.opcode == nvme_cmd_read) &&
2079 nvme_ns_has_pi(ns))
2080 req->use_sig_mr = true;
2081 else
2082 req->use_sig_mr = false;
2083
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002084 err = nvme_rdma_map_data(queue, rq, c);
2085 if (unlikely(err < 0)) {
2086 dev_err(queue->ctrl->ctrl.device,
2087 "Failed to map data (%d)\n", err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002088 goto err;
2089 }
2090
2091 sqe->cqe.done = nvme_rdma_send_done;
2092
2093 ib_dma_sync_single_for_device(dev, sqe->dma,
2094 sizeof(struct nvme_command), DMA_TO_DEVICE);
2095
2096 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
2097 req->mr ? &req->reg_wr.wr : NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +02002098 if (unlikely(err))
2099 goto err_unmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002100
2101 return BLK_STS_OK;
David Brazdil0f672f62019-12-10 10:32:29 +00002102
Olivier Deprez157378f2022-04-04 15:47:50 +02002103err_unmap:
2104 nvme_rdma_unmap_data(queue, rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002105err:
2106 if (err == -ENOMEM || err == -EAGAIN)
David Brazdil0f672f62019-12-10 10:32:29 +00002107 ret = BLK_STS_RESOURCE;
2108 else
2109 ret = BLK_STS_IOERR;
Olivier Deprez157378f2022-04-04 15:47:50 +02002110 nvme_cleanup_cmd(rq);
David Brazdil0f672f62019-12-10 10:32:29 +00002111unmap_qe:
2112 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
2113 DMA_TO_DEVICE);
2114 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002115}
2116
David Brazdil0f672f62019-12-10 10:32:29 +00002117static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002118{
2119 struct nvme_rdma_queue *queue = hctx->driver_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002120
David Brazdil0f672f62019-12-10 10:32:29 +00002121 return ib_process_cq_direct(queue->ib_cq, -1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002122}
2123
Olivier Deprez157378f2022-04-04 15:47:50 +02002124static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req)
2125{
2126 struct request *rq = blk_mq_rq_from_pdu(req);
2127 struct ib_mr_status mr_status;
2128 int ret;
2129
2130 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
2131 if (ret) {
2132 pr_err("ib_check_mr_status failed, ret %d\n", ret);
2133 nvme_req(rq)->status = NVME_SC_INVALID_PI;
2134 return;
2135 }
2136
2137 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
2138 switch (mr_status.sig_err.err_type) {
2139 case IB_SIG_BAD_GUARD:
2140 nvme_req(rq)->status = NVME_SC_GUARD_CHECK;
2141 break;
2142 case IB_SIG_BAD_REFTAG:
2143 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK;
2144 break;
2145 case IB_SIG_BAD_APPTAG:
2146 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK;
2147 break;
2148 }
2149 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
2150 mr_status.sig_err.err_type, mr_status.sig_err.expected,
2151 mr_status.sig_err.actual);
2152 }
2153}
2154
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002155static void nvme_rdma_complete_rq(struct request *rq)
2156{
2157 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
David Brazdil0f672f62019-12-10 10:32:29 +00002158 struct nvme_rdma_queue *queue = req->queue;
2159 struct ib_device *ibdev = queue->device->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002160
Olivier Deprez157378f2022-04-04 15:47:50 +02002161 if (req->use_sig_mr)
2162 nvme_rdma_check_pi_status(req);
2163
David Brazdil0f672f62019-12-10 10:32:29 +00002164 nvme_rdma_unmap_data(queue, rq);
2165 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
2166 DMA_TO_DEVICE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002167 nvme_complete_rq(rq);
2168}
2169
2170static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
2171{
2172 struct nvme_rdma_ctrl *ctrl = set->driver_data;
David Brazdil0f672f62019-12-10 10:32:29 +00002173 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002174
David Brazdil0f672f62019-12-10 10:32:29 +00002175 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2176 /* separate read/write queues */
2177 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2178 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2179 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2180 set->map[HCTX_TYPE_READ].nr_queues =
2181 ctrl->io_queues[HCTX_TYPE_READ];
2182 set->map[HCTX_TYPE_READ].queue_offset =
2183 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2184 } else {
2185 /* shared read/write queues */
2186 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2187 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2188 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2189 set->map[HCTX_TYPE_READ].nr_queues =
2190 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2191 set->map[HCTX_TYPE_READ].queue_offset = 0;
2192 }
2193 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
2194 ctrl->device->dev, 0);
2195 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
2196 ctrl->device->dev, 0);
2197
2198 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2199 /* map dedicated poll queues only if we have queues left */
2200 set->map[HCTX_TYPE_POLL].nr_queues =
2201 ctrl->io_queues[HCTX_TYPE_POLL];
2202 set->map[HCTX_TYPE_POLL].queue_offset =
2203 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2204 ctrl->io_queues[HCTX_TYPE_READ];
2205 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2206 }
2207
2208 dev_info(ctrl->ctrl.device,
2209 "mapped %d/%d/%d default/read/poll queues.\n",
2210 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2211 ctrl->io_queues[HCTX_TYPE_READ],
2212 ctrl->io_queues[HCTX_TYPE_POLL]);
2213
2214 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002215}
2216
2217static const struct blk_mq_ops nvme_rdma_mq_ops = {
2218 .queue_rq = nvme_rdma_queue_rq,
2219 .complete = nvme_rdma_complete_rq,
2220 .init_request = nvme_rdma_init_request,
2221 .exit_request = nvme_rdma_exit_request,
2222 .init_hctx = nvme_rdma_init_hctx,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002223 .timeout = nvme_rdma_timeout,
2224 .map_queues = nvme_rdma_map_queues,
David Brazdil0f672f62019-12-10 10:32:29 +00002225 .poll = nvme_rdma_poll,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002226};
2227
2228static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
2229 .queue_rq = nvme_rdma_queue_rq,
2230 .complete = nvme_rdma_complete_rq,
2231 .init_request = nvme_rdma_init_request,
2232 .exit_request = nvme_rdma_exit_request,
2233 .init_hctx = nvme_rdma_init_admin_hctx,
2234 .timeout = nvme_rdma_timeout,
2235};
2236
2237static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
2238{
David Brazdil0f672f62019-12-10 10:32:29 +00002239 cancel_work_sync(&ctrl->err_work);
2240 cancel_delayed_work_sync(&ctrl->reconnect_work);
2241
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002242 nvme_rdma_teardown_io_queues(ctrl, shutdown);
David Brazdil0f672f62019-12-10 10:32:29 +00002243 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002244 if (shutdown)
2245 nvme_shutdown_ctrl(&ctrl->ctrl);
2246 else
David Brazdil0f672f62019-12-10 10:32:29 +00002247 nvme_disable_ctrl(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002248 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
2249}
2250
2251static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
2252{
2253 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
2254}
2255
2256static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
2257{
2258 struct nvme_rdma_ctrl *ctrl =
2259 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
2260
2261 nvme_stop_ctrl(&ctrl->ctrl);
2262 nvme_rdma_shutdown_ctrl(ctrl, false);
2263
2264 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2265 /* state change failure should never happen */
2266 WARN_ON_ONCE(1);
2267 return;
2268 }
2269
2270 if (nvme_rdma_setup_ctrl(ctrl, false))
2271 goto out_fail;
2272
2273 return;
2274
2275out_fail:
2276 ++ctrl->ctrl.nr_reconnects;
2277 nvme_rdma_reconnect_or_remove(ctrl);
2278}
2279
2280static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
2281 .name = "rdma",
2282 .module = THIS_MODULE,
Olivier Deprez157378f2022-04-04 15:47:50 +02002283 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002284 .reg_read32 = nvmf_reg_read32,
2285 .reg_read64 = nvmf_reg_read64,
2286 .reg_write32 = nvmf_reg_write32,
2287 .free_ctrl = nvme_rdma_free_ctrl,
2288 .submit_async_event = nvme_rdma_submit_async_event,
2289 .delete_ctrl = nvme_rdma_delete_ctrl,
2290 .get_address = nvmf_get_address,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002291};
2292
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002293/*
2294 * Fails a connection request if it matches an existing controller
2295 * (association) with the same tuple:
2296 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
2297 *
2298 * if local address is not specified in the request, it will match an
2299 * existing controller with all the other parameters the same and no
2300 * local port address specified as well.
2301 *
2302 * The ports don't need to be compared as they are intrinsically
2303 * already matched by the port pointers supplied.
2304 */
2305static bool
2306nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
2307{
2308 struct nvme_rdma_ctrl *ctrl;
2309 bool found = false;
2310
2311 mutex_lock(&nvme_rdma_ctrl_mutex);
2312 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
David Brazdil0f672f62019-12-10 10:32:29 +00002313 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002314 if (found)
2315 break;
2316 }
2317 mutex_unlock(&nvme_rdma_ctrl_mutex);
2318
2319 return found;
2320}
2321
2322static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
2323 struct nvmf_ctrl_options *opts)
2324{
2325 struct nvme_rdma_ctrl *ctrl;
2326 int ret;
2327 bool changed;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002328
2329 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2330 if (!ctrl)
2331 return ERR_PTR(-ENOMEM);
2332 ctrl->ctrl.opts = opts;
2333 INIT_LIST_HEAD(&ctrl->list);
2334
David Brazdil0f672f62019-12-10 10:32:29 +00002335 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2336 opts->trsvcid =
2337 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
2338 if (!opts->trsvcid) {
2339 ret = -ENOMEM;
2340 goto out_free_ctrl;
2341 }
2342 opts->mask |= NVMF_OPT_TRSVCID;
2343 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002344
2345 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
David Brazdil0f672f62019-12-10 10:32:29 +00002346 opts->traddr, opts->trsvcid, &ctrl->addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002347 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00002348 pr_err("malformed address passed: %s:%s\n",
2349 opts->traddr, opts->trsvcid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002350 goto out_free_ctrl;
2351 }
2352
2353 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2354 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2355 opts->host_traddr, NULL, &ctrl->src_addr);
2356 if (ret) {
2357 pr_err("malformed src address passed: %s\n",
2358 opts->host_traddr);
2359 goto out_free_ctrl;
2360 }
2361 }
2362
2363 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
2364 ret = -EALREADY;
2365 goto out_free_ctrl;
2366 }
2367
2368 INIT_DELAYED_WORK(&ctrl->reconnect_work,
2369 nvme_rdma_reconnect_ctrl_work);
2370 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
2371 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
2372
David Brazdil0f672f62019-12-10 10:32:29 +00002373 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2374 opts->nr_poll_queues + 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002375 ctrl->ctrl.sqsize = opts->queue_size - 1;
2376 ctrl->ctrl.kato = opts->kato;
2377
2378 ret = -ENOMEM;
2379 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2380 GFP_KERNEL);
2381 if (!ctrl->queues)
2382 goto out_free_ctrl;
2383
2384 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2385 0 /* no quirks, we're perfect! */);
2386 if (ret)
2387 goto out_kfree_queues;
2388
2389 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2390 WARN_ON_ONCE(!changed);
2391
2392 ret = nvme_rdma_setup_ctrl(ctrl, true);
2393 if (ret)
2394 goto out_uninit_ctrl;
2395
2396 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2397 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2398
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002399 mutex_lock(&nvme_rdma_ctrl_mutex);
2400 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2401 mutex_unlock(&nvme_rdma_ctrl_mutex);
2402
2403 return &ctrl->ctrl;
2404
2405out_uninit_ctrl:
2406 nvme_uninit_ctrl(&ctrl->ctrl);
2407 nvme_put_ctrl(&ctrl->ctrl);
2408 if (ret > 0)
2409 ret = -EIO;
2410 return ERR_PTR(ret);
2411out_kfree_queues:
2412 kfree(ctrl->queues);
2413out_free_ctrl:
2414 kfree(ctrl);
2415 return ERR_PTR(ret);
2416}
2417
2418static struct nvmf_transport_ops nvme_rdma_transport = {
2419 .name = "rdma",
2420 .module = THIS_MODULE,
2421 .required_opts = NVMF_OPT_TRADDR,
2422 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
David Brazdil0f672f62019-12-10 10:32:29 +00002423 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2424 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2425 NVMF_OPT_TOS,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002426 .create_ctrl = nvme_rdma_create_ctrl,
2427};
2428
2429static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2430{
2431 struct nvme_rdma_ctrl *ctrl;
2432 struct nvme_rdma_device *ndev;
2433 bool found = false;
2434
2435 mutex_lock(&device_list_mutex);
2436 list_for_each_entry(ndev, &device_list, entry) {
2437 if (ndev->dev == ib_device) {
2438 found = true;
2439 break;
2440 }
2441 }
2442 mutex_unlock(&device_list_mutex);
2443
2444 if (!found)
2445 return;
2446
2447 /* Delete all controllers using this device */
2448 mutex_lock(&nvme_rdma_ctrl_mutex);
2449 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2450 if (ctrl->device->dev != ib_device)
2451 continue;
2452 nvme_delete_ctrl(&ctrl->ctrl);
2453 }
2454 mutex_unlock(&nvme_rdma_ctrl_mutex);
2455
2456 flush_workqueue(nvme_delete_wq);
2457}
2458
2459static struct ib_client nvme_rdma_ib_client = {
2460 .name = "nvme_rdma",
2461 .remove = nvme_rdma_remove_one
2462};
2463
2464static int __init nvme_rdma_init_module(void)
2465{
2466 int ret;
2467
2468 ret = ib_register_client(&nvme_rdma_ib_client);
2469 if (ret)
2470 return ret;
2471
2472 ret = nvmf_register_transport(&nvme_rdma_transport);
2473 if (ret)
2474 goto err_unreg_client;
2475
2476 return 0;
2477
2478err_unreg_client:
2479 ib_unregister_client(&nvme_rdma_ib_client);
2480 return ret;
2481}
2482
2483static void __exit nvme_rdma_cleanup_module(void)
2484{
David Brazdil0f672f62019-12-10 10:32:29 +00002485 struct nvme_rdma_ctrl *ctrl;
2486
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002487 nvmf_unregister_transport(&nvme_rdma_transport);
2488 ib_unregister_client(&nvme_rdma_ib_client);
David Brazdil0f672f62019-12-10 10:32:29 +00002489
2490 mutex_lock(&nvme_rdma_ctrl_mutex);
2491 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2492 nvme_delete_ctrl(&ctrl->ctrl);
2493 mutex_unlock(&nvme_rdma_ctrl_mutex);
2494 flush_workqueue(nvme_delete_wq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002495}
2496
2497module_init(nvme_rdma_init_module);
2498module_exit(nvme_rdma_cleanup_module);
2499
2500MODULE_LICENSE("GPL v2");