blob: 40a0a3b6476c36970f3c0ad54cd81c547c0c5417 [file] [log] [blame]
Christoph Hellwig5d8762d2019-02-18 09:34:21 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwig71102302016-07-06 21:55:52 +09002/*
3 * NVMe over Fabrics RDMA host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
Christoph Hellwig71102302016-07-06 21:55:52 +09005 */
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Christoph Hellwig71102302016-07-06 21:55:52 +09007#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/slab.h>
Israel Rukshinf41725b2017-11-26 10:40:55 +000010#include <rdma/mr_pool.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090011#include <linux/err.h>
12#include <linux/string.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090013#include <linux/atomic.h>
14#include <linux/blk-mq.h>
Sagi Grimberg0b366582017-07-13 11:09:44 +030015#include <linux/blk-mq-rdma.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090016#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/mutex.h>
19#include <linux/scatterlist.h>
20#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090021#include <asm/unaligned.h>
22
23#include <rdma/ib_verbs.h>
24#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090025#include <linux/nvme-rdma.h>
26
27#include "nvme.h"
28#include "fabrics.h"
29
30
Sagi Grimberg782d8202017-03-21 16:32:38 +020031#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090032
Christoph Hellwig71102302016-07-06 21:55:52 +090033#define NVME_RDMA_MAX_SEGMENTS 256
34
Steve Wise64a741c2018-06-20 07:15:05 -070035#define NVME_RDMA_MAX_INLINE_SEGMENTS 4
Christoph Hellwig71102302016-07-06 21:55:52 +090036
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +030037#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
Christoph Hellwig71102302016-07-06 21:55:52 +090042struct nvme_rdma_device {
Max Gurtovoyf87c89a2017-10-23 12:59:27 +030043 struct ib_device *dev;
44 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090045 struct kref ref;
46 struct list_head entry;
Steve Wise64a741c2018-06-20 07:15:05 -070047 unsigned int num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +090048};
49
50struct nvme_rdma_qe {
51 struct ib_cqe cqe;
52 void *data;
53 u64 dma;
54};
55
Israel Rukshin324d9e72020-05-19 17:05:55 +030056struct nvme_rdma_sgl {
57 int nents;
58 struct sg_table sg_table;
59};
60
Christoph Hellwig71102302016-07-06 21:55:52 +090061struct nvme_rdma_queue;
62struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080063 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090064 struct ib_mr *mr;
65 struct nvme_rdma_qe sqe;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +020066 union nvme_result result;
67 __le16 status;
68 refcount_t ref;
Christoph Hellwig71102302016-07-06 21:55:52 +090069 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
70 u32 num_sge;
Christoph Hellwig71102302016-07-06 21:55:52 +090071 struct ib_reg_wr reg_wr;
72 struct ib_cqe reg_cqe;
73 struct nvme_rdma_queue *queue;
Israel Rukshin324d9e72020-05-19 17:05:55 +030074 struct nvme_rdma_sgl data_sgl;
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +030075 struct nvme_rdma_sgl *metadata_sgl;
76 bool use_sig_mr;
Christoph Hellwig71102302016-07-06 21:55:52 +090077};
78
79enum nvme_rdma_queue_flags {
Sagi Grimberg5013e982017-10-11 15:29:12 +030080 NVME_RDMA_Q_ALLOCATED = 0,
81 NVME_RDMA_Q_LIVE = 1,
Max Gurtovoyeb1bd242017-11-28 18:28:44 +020082 NVME_RDMA_Q_TR_READY = 2,
Christoph Hellwig71102302016-07-06 21:55:52 +090083};
84
85struct nvme_rdma_queue {
86 struct nvme_rdma_qe *rsp_ring;
Christoph Hellwig71102302016-07-06 21:55:52 +090087 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;
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +030098 bool pi_support;
Yamin Friedman287f3292020-07-13 11:53:29 +030099 int cq_size;
Christoph Hellwig71102302016-07-06 21:55:52 +0900100};
101
102struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +0900103 /* read only in the hot path */
104 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900105
106 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900107 struct blk_mq_tag_set tag_set;
Christoph Hellwig71102302016-07-06 21:55:52 +0900108 struct work_struct err_work;
109
110 struct nvme_rdma_qe async_event_sqe;
111
Christoph Hellwig71102302016-07-06 21:55:52 +0900112 struct delayed_work reconnect_work;
113
114 struct list_head list;
115
116 struct blk_mq_tag_set admin_tag_set;
117 struct nvme_rdma_device *device;
118
Christoph Hellwig71102302016-07-06 21:55:52 +0900119 u32 max_fr_pages;
120
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200121 struct sockaddr_storage addr;
122 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900123
124 struct nvme_ctrl ctrl;
Sagi Grimberg5110f402020-08-05 18:13:58 -0700125 struct mutex teardown_lock;
Steve Wise64a741c2018-06-20 07:15:05 -0700126 bool use_inline_data;
Sagi Grimbergb1064d32019-01-18 16:43:24 -0800127 u32 io_queues[HCTX_MAX_TYPES];
Christoph Hellwig71102302016-07-06 21:55:52 +0900128};
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
Christoph Hellwig71102302016-07-06 21:55:52 +0900141/*
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);
Christoph Hellwigff029452020-06-11 08:44:52 +0200154static void nvme_rdma_complete_rq(struct request *rq);
Christoph Hellwig71102302016-07-06 21:55:52 +0900155
Sagi Grimberg90af3512017-07-10 09:22:28 +0300156static const struct blk_mq_ops nvme_rdma_mq_ops;
157static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
158
Christoph Hellwig71102302016-07-06 21:55:52 +0900159static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
160{
161 return queue - queue->ctrl->queues;
162}
163
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800164static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
165{
166 return nvme_rdma_queue_idx(queue) >
Sagi Grimbergb1064d32019-01-18 16:43:24 -0800167 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
168 queue->ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800169}
170
Christoph Hellwig71102302016-07-06 21:55:52 +0900171static 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);
Prabhath Sajeepa6344d022018-11-28 11:11:29 -0700193 qe->data = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900194 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
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300222 /*
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 */
Christoph Hellwig71102302016-07-06 21:55:52 +0900227 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{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200241 pr_debug("QP event %s (%d)\n",
242 ib_event_msg(event->event), event->event);
243
Christoph Hellwig71102302016-07-06 21:55:52 +0900244}
245
246static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
247{
Bart Van Assche35da77d2018-10-08 14:28:54 -0700248 int ret;
249
250 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
Christoph Hellwig71102302016-07-06 21:55:52 +0900251 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
Bart Van Assche35da77d2018-10-08 14:28:54 -0700252 if (ret < 0)
253 return ret;
254 if (ret == 0)
255 return -ETIMEDOUT;
256 WARN_ON_ONCE(queue->cm_error > 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900257 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;
Steve Wise64a741c2018-06-20 07:15:05 -0700273 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +0900274 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;
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300278 if (queue->pi_support)
279 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
Yamin Friedman287f3292020-07-13 11:53:29 +0300280 init_attr.qp_context = queue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900281
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
Christoph Hellwig385475e2017-06-13 09:15:19 +0200288static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
289 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900290{
291 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +0900292
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300293 kfree(req->sqe.data);
Christoph Hellwig71102302016-07-06 21:55:52 +0900294}
295
Christoph Hellwig385475e2017-06-13 09:15:19 +0200296static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
297 struct request *rq, unsigned int hctx_idx,
298 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900299{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200300 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900301 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200302 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900303 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
Christoph Hellwig71102302016-07-06 21:55:52 +0900304
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600305 nvme_req(rq)->ctrl = &ctrl->ctrl;
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300306 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
307 if (!req->sqe.data)
308 return -ENOMEM;
Christoph Hellwig71102302016-07-06 21:55:52 +0900309
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300310 /* 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
Christoph Hellwig71102302016-07-06 21:55:52 +0900316 req->queue = queue;
317
318 return 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900319}
320
Christoph Hellwig71102302016-07-06 21:55:52 +0900321static 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
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300327 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900328
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
Christoph Hellwig71102302016-07-06 21:55:52 +0900354 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900355 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
Christoph Hellwig11975e02016-09-05 12:56:20 +0200387 ndev->pd = ib_alloc_pd(ndev->dev,
388 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900389 if (IS_ERR(ndev->pd))
390 goto out_free_dev;
391
Christoph Hellwig71102302016-07-06 21:55:52 +0900392 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");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200396 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900397 }
398
Steve Wise64a741c2018-06-20 07:15:05 -0700399 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
Jason Gunthorpe0a3173a2018-08-16 14:13:03 -0600400 ndev->dev->attrs.max_send_sge - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900401 list_add(&ndev->entry, &device_list);
402out_unlock:
403 mutex_unlock(&device_list_mutex);
404 return ndev;
405
Christoph Hellwig71102302016-07-06 21:55:52 +0900406out_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
Yamin Friedman287f3292020-07-13 11:53:29 +0300415static 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
Christoph Hellwig71102302016-07-06 21:55:52 +0900423static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
424{
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200425 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;
Christoph Hellwig71102302016-07-06 21:55:52 +0900433
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300434 if (queue->pi_support)
435 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs);
Israel Rukshinf41725b2017-11-26 10:40:55 +0000436 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
437
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200438 /*
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);
Yamin Friedman287f3292020-07-13 11:53:29 +0300444 nvme_rdma_free_cq(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900445
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
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300452static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support)
Israel Rukshinf41725b2017-11-26 10:40:55 +0000453{
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300454 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);
Israel Rukshinf41725b2017-11-26 10:40:55 +0000462}
463
Yamin Friedman287f3292020-07-13 11:53:29 +0300464static 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;
493}
494
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300495static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900496{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300497 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900498 const int send_wr_factor = 3; /* MR, SEND, INV */
499 const int cq_factor = send_wr_factor + 1; /* + RECV */
Max Gurtovoyff13c1b2019-09-21 23:58:19 +0300500 int ret, pages_per_mr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900501
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300502 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;
Christoph Hellwig71102302016-07-06 21:55:52 +0900509
Christoph Hellwig71102302016-07-06 21:55:52 +0900510 /* +1 for ib_stop_cq */
Yamin Friedman287f3292020-07-13 11:53:29 +0300511 queue->cq_size = cq_factor * queue->queue_size + 1;
512
513 ret = nvme_rdma_create_cq(ibdev, queue);
514 if (ret)
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300515 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900516
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
Max Gurtovoyff13c1b2019-09-21 23:58:19 +0300528 /*
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 */
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300533 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1;
Israel Rukshinf41725b2017-11-26 10:40:55 +0000534 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
535 queue->queue_size,
536 IB_MR_TYPE_MEM_REG,
Max Gurtovoyff13c1b2019-09-21 23:58:19 +0300537 pages_per_mr, 0);
Israel Rukshinf41725b2017-11-26 10:40:55 +0000538 if (ret) {
539 dev_err(queue->ctrl->ctrl.device,
540 "failed to initialize MR pool sized %d for QID %d\n",
Yamin Friedman287f3292020-07-13 11:53:29 +0300541 queue->queue_size, nvme_rdma_queue_idx(queue));
Israel Rukshinf41725b2017-11-26 10:40:55 +0000542 goto out_destroy_ring;
543 }
544
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300545 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",
Yamin Friedman287f3292020-07-13 11:53:29 +0300552 queue->queue_size, nvme_rdma_queue_idx(queue));
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300553 goto out_destroy_mr_pool;
554 }
555 }
556
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200557 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
558
Christoph Hellwig71102302016-07-06 21:55:52 +0900559 return 0;
560
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300561out_destroy_mr_pool:
562 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
Israel Rukshinf41725b2017-11-26 10:40:55 +0000563out_destroy_ring:
564 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
565 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
Christoph Hellwig71102302016-07-06 21:55:52 +0900566out_destroy_qp:
Max Gurtovoy1f61def2017-11-06 16:18:51 +0200567 rdma_destroy_qp(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900568out_destroy_ib_cq:
Yamin Friedman287f3292020-07-13 11:53:29 +0300569 nvme_rdma_free_cq(queue);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300570out_put_dev:
571 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900572 return ret;
573}
574
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300575static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900576 int idx, size_t queue_size)
577{
578 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200579 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900580 int ret;
581
582 queue = &ctrl->queues[idx];
583 queue->ctrl = ctrl;
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300584 if (idx && ctrl->ctrl.max_integrity_segments)
585 queue->pi_support = true;
586 else
587 queue->pi_support = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900588 init_completion(&queue->cm_done);
589
590 if (idx > 0)
591 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
592 else
593 queue->cmnd_capsule_len = sizeof(struct nvme_command);
594
595 queue->queue_size = queue_size;
596
597 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
598 RDMA_PS_TCP, IB_QPT_RC);
599 if (IS_ERR(queue->cm_id)) {
600 dev_info(ctrl->ctrl.device,
601 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
602 return PTR_ERR(queue->cm_id);
603 }
604
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200605 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200606 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200607
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200608 queue->cm_error = -ETIMEDOUT;
609 ret = rdma_resolve_addr(queue->cm_id, src_addr,
610 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900611 NVME_RDMA_CONNECT_TIMEOUT_MS);
612 if (ret) {
613 dev_info(ctrl->ctrl.device,
614 "rdma_resolve_addr failed (%d).\n", ret);
615 goto out_destroy_cm_id;
616 }
617
618 ret = nvme_rdma_wait_for_cm(queue);
619 if (ret) {
620 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300621 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900622 goto out_destroy_cm_id;
623 }
624
Sagi Grimberg5013e982017-10-11 15:29:12 +0300625 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900626
627 return 0;
628
629out_destroy_cm_id:
630 rdma_destroy_id(queue->cm_id);
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200631 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900632 return ret;
633}
634
Sagi Grimbergd94211b2019-07-26 10:29:49 -0700635static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
636{
637 rdma_disconnect(queue->cm_id);
638 ib_drain_qp(queue->qp);
639}
640
Christoph Hellwig71102302016-07-06 21:55:52 +0900641static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
642{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200643 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
644 return;
Sagi Grimbergd94211b2019-07-26 10:29:49 -0700645 __nvme_rdma_stop_queue(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900646}
647
648static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
649{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300650 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900651 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200652
653 nvme_rdma_destroy_queue_ib(queue);
654 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900655}
656
657static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
658{
659 int i;
660
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300661 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200662 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900663}
664
Sagi Grimberga57bd542017-08-28 21:41:10 +0200665static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
666{
667 int i;
668
669 for (i = 1; i < ctrl->ctrl.queue_count; i++)
670 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900671}
672
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300673static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
674{
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800675 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
676 bool poll = nvme_rdma_poll_queue(queue);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300677 int ret;
678
679 if (idx)
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800680 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300681 else
682 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
683
Sagi Grimbergd94211b2019-07-26 10:29:49 -0700684 if (!ret) {
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800685 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Sagi Grimbergd94211b2019-07-26 10:29:49 -0700686 } else {
Sagi Grimberg67b483d2019-09-24 11:27:05 -0700687 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
688 __nvme_rdma_stop_queue(queue);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300689 dev_info(ctrl->ctrl.device,
690 "failed to connect queue: %d ret=%d\n", idx, ret);
Sagi Grimbergd94211b2019-07-26 10:29:49 -0700691 }
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300692 return ret;
693}
694
695static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900696{
697 int i, ret = 0;
698
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300699 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300700 ret = nvme_rdma_start_queue(ctrl, i);
701 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200702 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900703 }
704
Steve Wisec8dbc372016-11-08 09:16:02 -0800705 return 0;
706
Sagi Grimberga57bd542017-08-28 21:41:10 +0200707out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300708 for (i--; i >= 1; i--)
709 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900710 return ret;
711}
712
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300713static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900714{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200715 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300716 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimberg5651cd32019-05-28 22:49:04 -0700717 unsigned int nr_io_queues, nr_default_queues;
718 unsigned int nr_read_queues, nr_poll_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900719 int i, ret;
720
Sagi Grimberg5651cd32019-05-28 22:49:04 -0700721 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
722 min(opts->nr_io_queues, num_online_cpus()));
723 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
724 min(opts->nr_write_queues, num_online_cpus()));
725 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
726 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
Sagi Grimbergb65bb772018-12-11 23:38:58 -0800727
Sagi Grimbergc248c642017-03-09 13:26:07 +0200728 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
729 if (ret)
730 return ret;
731
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300732 ctrl->ctrl.queue_count = nr_io_queues + 1;
733 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200734 return 0;
735
736 dev_info(ctrl->ctrl.device,
737 "creating %d I/O queues.\n", nr_io_queues);
738
Sagi Grimberg5651cd32019-05-28 22:49:04 -0700739 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
740 /*
741 * separate read/write queues
742 * hand out dedicated default queues only after we have
743 * sufficient read queues.
744 */
745 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
746 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
747 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
748 min(nr_default_queues, nr_io_queues);
749 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
750 } else {
751 /*
752 * shared read/write queues
753 * either no write queues were requested, or we don't have
754 * sufficient queue count to have dedicated default queues.
755 */
756 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
757 min(nr_read_queues, nr_io_queues);
758 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
759 }
760
761 if (opts->nr_poll_queues && nr_io_queues) {
762 /* map dedicated poll queues only if we have queues left */
763 ctrl->io_queues[HCTX_TYPE_POLL] =
764 min(nr_poll_queues, nr_io_queues);
765 }
766
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300767 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300768 ret = nvme_rdma_alloc_queue(ctrl, i,
769 ctrl->ctrl.sqsize + 1);
770 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900771 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900772 }
773
774 return 0;
775
776out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700777 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200778 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900779
780 return ret;
781}
782
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300783static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
784 bool admin)
785{
786 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
787 struct blk_mq_tag_set *set;
788 int ret;
789
790 if (admin) {
791 set = &ctrl->admin_tag_set;
792 memset(set, 0, sizeof(*set));
793 set->ops = &nvme_rdma_admin_mq_ops;
Keith Busch38dabe22017-11-07 15:13:10 -0700794 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300795 set->reserved_tags = 2; /* connect + keep-alive */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100796 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300797 set->cmd_size = sizeof(struct nvme_rdma_request) +
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300798 NVME_RDMA_DATA_SGL_SIZE;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300799 set->driver_data = ctrl;
800 set->nr_hw_queues = 1;
801 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000802 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300803 } else {
804 set = &ctrl->tag_set;
805 memset(set, 0, sizeof(*set));
806 set->ops = &nvme_rdma_mq_ops;
Sagi Grimberg5e77d612018-06-19 15:34:13 +0300807 set->queue_depth = nctrl->sqsize + 1;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300808 set->reserved_tags = 1; /* fabric connect */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100809 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300810 set->flags = BLK_MQ_F_SHOULD_MERGE;
811 set->cmd_size = sizeof(struct nvme_rdma_request) +
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300812 NVME_RDMA_DATA_SGL_SIZE;
813 if (nctrl->max_integrity_segments)
814 set->cmd_size += sizeof(struct nvme_rdma_sgl) +
815 NVME_RDMA_METADATA_SGL_SIZE;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300816 set->driver_data = ctrl;
817 set->nr_hw_queues = nctrl->queue_count - 1;
818 set->timeout = NVME_IO_TIMEOUT;
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800819 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300820 }
821
822 ret = blk_mq_alloc_tag_set(set);
823 if (ret)
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300824 return ERR_PTR(ret);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300825
826 return set;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300827}
828
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300829static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
830 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900831{
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300832 if (remove) {
833 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -0700834 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300835 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300836 }
Sagi Grimberg682630f2018-06-25 20:58:17 +0300837 if (ctrl->async_event_sqe.data) {
David Milburn925dd042020-09-02 17:42:52 -0500838 cancel_work_sync(&ctrl->ctrl.async_event_work);
Sagi Grimberg682630f2018-06-25 20:58:17 +0300839 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
840 sizeof(struct nvme_command), DMA_TO_DEVICE);
841 ctrl->async_event_sqe.data = NULL;
842 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200843 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900844}
845
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300846static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
847 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300848{
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300849 bool pi_capable = false;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300850 int error;
851
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300852 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300853 if (error)
854 return error;
855
856 ctrl->device = ctrl->queues[0].device;
Hannes Reinecke103e5152018-11-16 09:22:29 +0100857 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300858
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300859 /* T10-PI support */
860 if (ctrl->device->dev->attrs.device_cap_flags &
861 IB_DEVICE_INTEGRITY_HANDOVER)
862 pi_capable = true;
863
864 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev,
865 pi_capable);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300866
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300867 /*
868 * Bind the async event SQE DMA mapping to the admin queue lifetime.
869 * It's safe, since any chage in the underlying RDMA device will issue
870 * error recovery and queue re-creation.
871 */
Sagi Grimberg94e42212018-06-19 15:34:10 +0300872 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
873 sizeof(struct nvme_command), DMA_TO_DEVICE);
874 if (error)
875 goto out_free_queue;
876
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300877 if (new) {
878 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300879 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
880 error = PTR_ERR(ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300881 goto out_free_async_qe;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300882 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300883
Sagi Grimberge7832cb2019-08-02 19:33:59 -0700884 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
885 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
886 error = PTR_ERR(ctrl->ctrl.fabrics_q);
887 goto out_free_tagset;
888 }
889
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300890 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
891 if (IS_ERR(ctrl->ctrl.admin_q)) {
892 error = PTR_ERR(ctrl->ctrl.admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -0700893 goto out_cleanup_fabrics_q;
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300894 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300895 }
896
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300897 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300898 if (error)
899 goto out_cleanup_queue;
900
Sagi Grimbergc0f2f452019-07-22 17:06:53 -0700901 error = nvme_enable_ctrl(&ctrl->ctrl);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300902 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800903 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300904
Max Gurtovoyff13c1b2019-09-21 23:58:19 +0300905 ctrl->ctrl.max_segments = ctrl->max_fr_pages;
906 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9);
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +0300907 if (pi_capable)
908 ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages;
909 else
910 ctrl->ctrl.max_integrity_segments = 0;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300911
Sagi Grimberge7832cb2019-08-02 19:33:59 -0700912 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
913
Sagi Grimberg90af3512017-07-10 09:22:28 +0300914 error = nvme_init_identify(&ctrl->ctrl);
915 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800916 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300917
Sagi Grimberg90af3512017-07-10 09:22:28 +0300918 return 0;
919
Jianchao Wang2e050f02018-05-24 09:27:38 +0800920out_stop_queue:
921 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300922out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300923 if (new)
924 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -0700925out_cleanup_fabrics_q:
926 if (new)
927 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300928out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300929 if (new)
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300930 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300931out_free_async_qe:
Prabhath Sajeepa9134ae22020-03-09 15:07:53 -0600932 if (ctrl->async_event_sqe.data) {
933 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
934 sizeof(struct nvme_command), DMA_TO_DEVICE);
935 ctrl->async_event_sqe.data = NULL;
936 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300937out_free_queue:
938 nvme_rdma_free_queue(&ctrl->queues[0]);
939 return error;
940}
941
Sagi Grimberga57bd542017-08-28 21:41:10 +0200942static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
943 bool remove)
944{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200945 if (remove) {
946 blk_cleanup_queue(ctrl->ctrl.connect_q);
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300947 blk_mq_free_tag_set(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200948 }
949 nvme_rdma_free_io_queues(ctrl);
950}
951
952static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
953{
954 int ret;
955
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300956 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200957 if (ret)
958 return ret;
959
960 if (new) {
961 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300962 if (IS_ERR(ctrl->ctrl.tagset)) {
963 ret = PTR_ERR(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200964 goto out_free_io_queues;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300965 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200966
967 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
968 if (IS_ERR(ctrl->ctrl.connect_q)) {
969 ret = PTR_ERR(ctrl->ctrl.connect_q);
970 goto out_free_tag_set;
971 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200972 }
973
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300974 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200975 if (ret)
976 goto out_cleanup_connect_q;
977
Sagi Grimberg9f987722020-07-27 17:32:09 -0700978 if (!new) {
979 nvme_start_queues(&ctrl->ctrl);
Sagi Grimberg2362acb2020-07-30 13:42:42 -0700980 if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) {
981 /*
982 * If we timed out waiting for freeze we are likely to
983 * be stuck. Fail the controller initialization just
984 * to be safe.
985 */
986 ret = -ENODEV;
987 goto out_wait_freeze_timed_out;
988 }
Sagi Grimberg9f987722020-07-27 17:32:09 -0700989 blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset,
990 ctrl->ctrl.queue_count - 1);
991 nvme_unfreeze(&ctrl->ctrl);
992 }
993
Sagi Grimberga57bd542017-08-28 21:41:10 +0200994 return 0;
995
Sagi Grimberg2362acb2020-07-30 13:42:42 -0700996out_wait_freeze_timed_out:
997 nvme_stop_queues(&ctrl->ctrl);
998 nvme_rdma_stop_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200999out_cleanup_connect_q:
1000 if (new)
1001 blk_cleanup_queue(ctrl->ctrl.connect_q);
1002out_free_tag_set:
1003 if (new)
Max Gurtovoy87fd1252019-05-06 13:47:55 +03001004 blk_mq_free_tag_set(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +02001005out_free_io_queues:
1006 nvme_rdma_free_io_queues(ctrl);
1007 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001008}
1009
Sagi Grimberg75862c72018-07-09 12:49:07 +03001010static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
1011 bool remove)
1012{
Sagi Grimberg5110f402020-08-05 18:13:58 -07001013 mutex_lock(&ctrl->teardown_lock);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001014 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1015 nvme_rdma_stop_queue(&ctrl->queues[0]);
Ming Lei622b8b62019-07-24 11:48:42 +08001016 if (ctrl->ctrl.admin_tagset) {
Sagi Grimberg10077092019-04-24 11:53:18 -07001017 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
1018 nvme_cancel_request, &ctrl->ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001019 blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset);
1020 }
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001021 if (remove)
1022 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001023 nvme_rdma_destroy_admin_queue(ctrl, remove);
Sagi Grimberg5110f402020-08-05 18:13:58 -07001024 mutex_unlock(&ctrl->teardown_lock);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001025}
1026
1027static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
1028 bool remove)
1029{
Sagi Grimberg5110f402020-08-05 18:13:58 -07001030 mutex_lock(&ctrl->teardown_lock);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001031 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberg9f987722020-07-27 17:32:09 -07001032 nvme_start_freeze(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001033 nvme_stop_queues(&ctrl->ctrl);
1034 nvme_rdma_stop_io_queues(ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001035 if (ctrl->ctrl.tagset) {
Sagi Grimberg10077092019-04-24 11:53:18 -07001036 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
1037 nvme_cancel_request, &ctrl->ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001038 blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset);
1039 }
Sagi Grimberg75862c72018-07-09 12:49:07 +03001040 if (remove)
1041 nvme_start_queues(&ctrl->ctrl);
1042 nvme_rdma_destroy_io_queues(ctrl, remove);
1043 }
Sagi Grimberg5110f402020-08-05 18:13:58 -07001044 mutex_unlock(&ctrl->teardown_lock);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001045}
1046
Christoph Hellwig71102302016-07-06 21:55:52 +09001047static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
1048{
1049 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1050
1051 if (list_empty(&ctrl->list))
1052 goto free_ctrl;
1053
1054 mutex_lock(&nvme_rdma_ctrl_mutex);
1055 list_del(&ctrl->list);
1056 mutex_unlock(&nvme_rdma_ctrl_mutex);
1057
Christoph Hellwig71102302016-07-06 21:55:52 +09001058 nvmf_free_options(nctrl->opts);
1059free_ctrl:
Sagi Grimberg3d064102018-06-19 15:34:09 +03001060 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +09001061 kfree(ctrl);
1062}
1063
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001064static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
1065{
1066 /* If we are resetting/deleting then do nothing */
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001067 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001068 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
1069 ctrl->ctrl.state == NVME_CTRL_LIVE);
1070 return;
1071 }
1072
1073 if (nvmf_should_reconnect(&ctrl->ctrl)) {
1074 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
1075 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001076 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001077 ctrl->ctrl.opts->reconnect_delay * HZ);
1078 } else {
Sagi Grimberg12fa1302017-10-29 14:21:01 +02001079 nvme_delete_ctrl(&ctrl->ctrl);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001080 }
1081}
1082
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001083static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
Christoph Hellwig71102302016-07-06 21:55:52 +09001084{
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001085 int ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001086 bool changed;
Christoph Hellwig71102302016-07-06 21:55:52 +09001087
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001088 ret = nvme_rdma_configure_admin_queue(ctrl, new);
Christoph Hellwig71102302016-07-06 21:55:52 +09001089 if (ret)
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001090 return ret;
1091
1092 if (ctrl->ctrl.icdoff) {
1093 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1094 goto destroy_admin;
1095 }
1096
1097 if (!(ctrl->ctrl.sgls & (1 << 2))) {
1098 dev_err(ctrl->ctrl.device,
1099 "Mandatory keyed sgls are not supported!\n");
1100 goto destroy_admin;
1101 }
1102
1103 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
1104 dev_warn(ctrl->ctrl.device,
1105 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1106 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
1107 }
1108
1109 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1110 dev_warn(ctrl->ctrl.device,
1111 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1112 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1113 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1114 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001115
Steve Wise64a741c2018-06-20 07:15:05 -07001116 if (ctrl->ctrl.sgls & (1 << 20))
1117 ctrl->use_inline_data = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001118
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001119 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001120 ret = nvme_rdma_configure_io_queues(ctrl, new);
Christoph Hellwig71102302016-07-06 21:55:52 +09001121 if (ret)
Sagi Grimberg5e1fe612017-10-11 15:29:11 +03001122 goto destroy_admin;
Christoph Hellwig71102302016-07-06 21:55:52 +09001123 }
1124
1125 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +03001126 if (!changed) {
Israel Rukshin96135862020-03-24 17:29:44 +02001127 /*
Sagi Grimbergecca390e2020-07-22 16:32:19 -07001128 * state change failure is ok if we started ctrl delete,
Israel Rukshin96135862020-03-24 17:29:44 +02001129 * unless we're during creation of a new controller to
1130 * avoid races with teardown flow.
1131 */
Sagi Grimbergecca390e2020-07-22 16:32:19 -07001132 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1133 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
Israel Rukshin96135862020-03-24 17:29:44 +02001134 WARN_ON_ONCE(new);
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001135 ret = -EINVAL;
1136 goto destroy_io;
Sagi Grimberg0a960afd2017-09-21 17:01:37 +03001137 }
1138
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001139 nvme_start_ctrl(&ctrl->ctrl);
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001140 return 0;
1141
1142destroy_io:
1143 if (ctrl->ctrl.queue_count > 1)
1144 nvme_rdma_destroy_io_queues(ctrl, new);
1145destroy_admin:
1146 nvme_rdma_stop_queue(&ctrl->queues[0]);
1147 nvme_rdma_destroy_admin_queue(ctrl, new);
1148 return ret;
1149}
1150
1151static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1152{
1153 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1154 struct nvme_rdma_ctrl, reconnect_work);
1155
1156 ++ctrl->ctrl.nr_reconnects;
1157
1158 if (nvme_rdma_setup_ctrl(ctrl, false))
1159 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +09001160
Sagi Grimberg5e1fe612017-10-11 15:29:11 +03001161 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1162 ctrl->ctrl.nr_reconnects);
1163
1164 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001165
1166 return;
1167
Christoph Hellwig71102302016-07-06 21:55:52 +09001168requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001169 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03001170 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001171 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001172}
1173
1174static void nvme_rdma_error_recovery_work(struct work_struct *work)
1175{
1176 struct nvme_rdma_ctrl *ctrl = container_of(work,
1177 struct nvme_rdma_ctrl, err_work);
1178
Sagi Grimberge4d753d2017-09-21 17:01:38 +03001179 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001180 nvme_rdma_teardown_io_queues(ctrl, false);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001181 nvme_start_queues(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001182 nvme_rdma_teardown_admin_queue(ctrl, false);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001183 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001184
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001185 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergecca390e2020-07-22 16:32:19 -07001186 /* state change failure is ok if we started ctrl delete */
1187 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1188 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001189 return;
1190 }
1191
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001192 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001193}
1194
1195static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1196{
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001197 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
Christoph Hellwig71102302016-07-06 21:55:52 +09001198 return;
1199
Sagi Grimberg0475a8d2020-07-29 02:36:03 -07001200 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
Nigel Kirkland97b25122020-02-10 16:01:45 -08001201 queue_work(nvme_reset_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001202}
1203
Christoph Hellwig84465462020-06-11 08:44:51 +02001204static void nvme_rdma_end_request(struct nvme_rdma_request *req)
1205{
1206 struct request *rq = blk_mq_rq_from_pdu(req);
1207
1208 if (!refcount_dec_and_test(&req->ref))
1209 return;
Christoph Hellwig2eb81a32020-08-18 09:11:29 +02001210 if (!nvme_try_complete_req(rq, req->status, req->result))
Christoph Hellwigff029452020-06-11 08:44:52 +02001211 nvme_rdma_complete_rq(rq);
Christoph Hellwig84465462020-06-11 08:44:51 +02001212}
1213
Christoph Hellwig71102302016-07-06 21:55:52 +09001214static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1215 const char *op)
1216{
Yamin Friedman287f3292020-07-13 11:53:29 +03001217 struct nvme_rdma_queue *queue = wc->qp->qp_context;
Christoph Hellwig71102302016-07-06 21:55:52 +09001218 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1219
1220 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1221 dev_info(ctrl->ctrl.device,
1222 "%s for CQE 0x%p failed with status %s (%d)\n",
1223 op, wc->wr_cqe,
1224 ib_wc_status_msg(wc->status), wc->status);
1225 nvme_rdma_error_recovery(ctrl);
1226}
1227
1228static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1229{
1230 if (unlikely(wc->status != IB_WC_SUCCESS))
1231 nvme_rdma_wr_error(cq, wc, "MEMREG");
1232}
1233
1234static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1235{
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001236 struct nvme_rdma_request *req =
1237 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001238
Christoph Hellwig84465462020-06-11 08:44:51 +02001239 if (unlikely(wc->status != IB_WC_SUCCESS))
Christoph Hellwig71102302016-07-06 21:55:52 +09001240 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
Christoph Hellwig84465462020-06-11 08:44:51 +02001241 else
1242 nvme_rdma_end_request(req);
Christoph Hellwig71102302016-07-06 21:55:52 +09001243}
1244
1245static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1246 struct nvme_rdma_request *req)
1247{
Christoph Hellwig71102302016-07-06 21:55:52 +09001248 struct ib_send_wr wr = {
1249 .opcode = IB_WR_LOCAL_INV,
1250 .next = NULL,
1251 .num_sge = 0,
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001252 .send_flags = IB_SEND_SIGNALED,
Christoph Hellwig71102302016-07-06 21:55:52 +09001253 .ex.invalidate_rkey = req->mr->rkey,
1254 };
1255
1256 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1257 wr.wr_cqe = &req->reg_cqe;
1258
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001259 return ib_post_send(queue->qp, &wr, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001260}
1261
1262static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1263 struct request *rq)
1264{
1265 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001266 struct nvme_rdma_device *dev = queue->device;
1267 struct ib_device *ibdev = dev->dev;
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001268 struct list_head *pool = &queue->qp->rdma_mrs;
Christoph Hellwig71102302016-07-06 21:55:52 +09001269
Chaitanya Kulkarni34e08192019-02-20 20:13:34 -08001270 if (!blk_rq_nr_phys_segments(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001271 return;
1272
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001273 if (blk_integrity_rq(rq)) {
1274 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1275 req->metadata_sgl->nents, rq_dma_dir(rq));
1276 sg_free_table_chained(&req->metadata_sgl->sg_table,
1277 NVME_INLINE_METADATA_SG_CNT);
1278 }
1279
1280 if (req->use_sig_mr)
1281 pool = &queue->qp->sig_mrs;
1282
Israel Rukshinf41725b2017-11-26 10:40:55 +00001283 if (req->mr) {
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001284 ib_mr_pool_put(queue->qp, pool, req->mr);
Israel Rukshinf41725b2017-11-26 10:40:55 +00001285 req->mr = NULL;
1286 }
1287
Israel Rukshin324d9e72020-05-19 17:05:55 +03001288 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1289 rq_dma_dir(rq));
1290 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
Christoph Hellwig71102302016-07-06 21:55:52 +09001291}
1292
1293static int nvme_rdma_set_sg_null(struct nvme_command *c)
1294{
1295 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1296
1297 sg->addr = 0;
1298 put_unaligned_le24(0, sg->length);
1299 put_unaligned_le32(0, sg->key);
1300 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1301 return 0;
1302}
1303
1304static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
Steve Wise64a741c2018-06-20 07:15:05 -07001305 struct nvme_rdma_request *req, struct nvme_command *c,
1306 int count)
Christoph Hellwig71102302016-07-06 21:55:52 +09001307{
1308 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
Israel Rukshin324d9e72020-05-19 17:05:55 +03001309 struct scatterlist *sgl = req->data_sgl.sg_table.sgl;
Steve Wise64a741c2018-06-20 07:15:05 -07001310 struct ib_sge *sge = &req->sge[1];
1311 u32 len = 0;
1312 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +09001313
Steve Wise64a741c2018-06-20 07:15:05 -07001314 for (i = 0; i < count; i++, sgl++, sge++) {
1315 sge->addr = sg_dma_address(sgl);
1316 sge->length = sg_dma_len(sgl);
1317 sge->lkey = queue->device->pd->local_dma_lkey;
1318 len += sge->length;
1319 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001320
1321 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
Steve Wise64a741c2018-06-20 07:15:05 -07001322 sg->length = cpu_to_le32(len);
Christoph Hellwig71102302016-07-06 21:55:52 +09001323 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1324
Steve Wise64a741c2018-06-20 07:15:05 -07001325 req->num_sge += count;
Christoph Hellwig71102302016-07-06 21:55:52 +09001326 return 0;
1327}
1328
1329static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1330 struct nvme_rdma_request *req, struct nvme_command *c)
1331{
1332 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1333
Israel Rukshin324d9e72020-05-19 17:05:55 +03001334 sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl));
1335 put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001336 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001337 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1338 return 0;
1339}
1340
1341static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1342 struct nvme_rdma_request *req, struct nvme_command *c,
1343 int count)
1344{
1345 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1346 int nr;
1347
Israel Rukshinf41725b2017-11-26 10:40:55 +00001348 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1349 if (WARN_ON_ONCE(!req->mr))
1350 return -EAGAIN;
1351
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001352 /*
1353 * Align the MR to a 4K page size to match the ctrl page size and
1354 * the block virtual boundary.
1355 */
Israel Rukshin324d9e72020-05-19 17:05:55 +03001356 nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL,
1357 SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001358 if (unlikely(nr < count)) {
Israel Rukshinf41725b2017-11-26 10:40:55 +00001359 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1360 req->mr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001361 if (nr < 0)
1362 return nr;
1363 return -EINVAL;
1364 }
1365
1366 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1367
1368 req->reg_cqe.done = nvme_rdma_memreg_done;
1369 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1370 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1371 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1372 req->reg_wr.wr.num_sge = 0;
1373 req->reg_wr.mr = req->mr;
1374 req->reg_wr.key = req->mr->rkey;
1375 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1376 IB_ACCESS_REMOTE_READ |
1377 IB_ACCESS_REMOTE_WRITE;
1378
Christoph Hellwig71102302016-07-06 21:55:52 +09001379 sg->addr = cpu_to_le64(req->mr->iova);
1380 put_unaligned_le24(req->mr->length, sg->length);
1381 put_unaligned_le32(req->mr->rkey, sg->key);
1382 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1383 NVME_SGL_FMT_INVALIDATE;
1384
1385 return 0;
1386}
1387
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001388static void nvme_rdma_set_sig_domain(struct blk_integrity *bi,
1389 struct nvme_command *cmd, struct ib_sig_domain *domain,
1390 u16 control, u8 pi_type)
1391{
1392 domain->sig_type = IB_SIG_TYPE_T10_DIF;
1393 domain->sig.dif.bg_type = IB_T10DIF_CRC;
1394 domain->sig.dif.pi_interval = 1 << bi->interval_exp;
1395 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
1396 if (control & NVME_RW_PRINFO_PRCHK_REF)
1397 domain->sig.dif.ref_remap = true;
1398
1399 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
1400 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
1401 domain->sig.dif.app_escape = true;
1402 if (pi_type == NVME_NS_DPS_PI_TYPE3)
1403 domain->sig.dif.ref_escape = true;
1404}
1405
1406static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi,
1407 struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs,
1408 u8 pi_type)
1409{
1410 u16 control = le16_to_cpu(cmd->rw.control);
1411
1412 memset(sig_attrs, 0, sizeof(*sig_attrs));
1413 if (control & NVME_RW_PRINFO_PRACT) {
1414 /* for WRITE_INSERT/READ_STRIP no memory domain */
1415 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1416 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1417 pi_type);
1418 /* Clear the PRACT bit since HCA will generate/verify the PI */
1419 control &= ~NVME_RW_PRINFO_PRACT;
1420 cmd->rw.control = cpu_to_le16(control);
1421 } else {
1422 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
1423 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1424 pi_type);
1425 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
1426 pi_type);
1427 }
1428}
1429
1430static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask)
1431{
1432 *mask = 0;
1433 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF)
1434 *mask |= IB_SIG_CHECK_REFTAG;
1435 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD)
1436 *mask |= IB_SIG_CHECK_GUARD;
1437}
1438
1439static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc)
1440{
1441 if (unlikely(wc->status != IB_WC_SUCCESS))
1442 nvme_rdma_wr_error(cq, wc, "SIG");
1443}
1444
1445static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
1446 struct nvme_rdma_request *req, struct nvme_command *c,
1447 int count, int pi_count)
1448{
1449 struct nvme_rdma_sgl *sgl = &req->data_sgl;
1450 struct ib_reg_wr *wr = &req->reg_wr;
1451 struct request *rq = blk_mq_rq_from_pdu(req);
1452 struct nvme_ns *ns = rq->q->queuedata;
1453 struct bio *bio = rq->bio;
1454 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1455 int nr;
1456
1457 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs);
1458 if (WARN_ON_ONCE(!req->mr))
1459 return -EAGAIN;
1460
1461 nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL,
1462 req->metadata_sgl->sg_table.sgl, pi_count, NULL,
1463 SZ_4K);
1464 if (unlikely(nr))
1465 goto mr_put;
1466
1467 nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_disk), c,
1468 req->mr->sig_attrs, ns->pi_type);
1469 nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask);
1470
1471 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1472
1473 req->reg_cqe.done = nvme_rdma_sig_done;
1474 memset(wr, 0, sizeof(*wr));
1475 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
1476 wr->wr.wr_cqe = &req->reg_cqe;
1477 wr->wr.num_sge = 0;
1478 wr->wr.send_flags = 0;
1479 wr->mr = req->mr;
1480 wr->key = req->mr->rkey;
1481 wr->access = IB_ACCESS_LOCAL_WRITE |
1482 IB_ACCESS_REMOTE_READ |
1483 IB_ACCESS_REMOTE_WRITE;
1484
1485 sg->addr = cpu_to_le64(req->mr->iova);
1486 put_unaligned_le24(req->mr->length, sg->length);
1487 put_unaligned_le32(req->mr->rkey, sg->key);
1488 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1489
1490 return 0;
1491
1492mr_put:
1493 ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr);
1494 req->mr = NULL;
1495 if (nr < 0)
1496 return nr;
1497 return -EINVAL;
1498}
1499
Christoph Hellwig71102302016-07-06 21:55:52 +09001500static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001501 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001502{
1503 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1504 struct nvme_rdma_device *dev = queue->device;
1505 struct ib_device *ibdev = dev->dev;
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001506 int pi_count = 0;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001507 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001508
1509 req->num_sge = 1;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001510 refcount_set(&req->ref, 2); /* send and recv completions */
Christoph Hellwig71102302016-07-06 21:55:52 +09001511
1512 c->common.flags |= NVME_CMD_SGL_METABUF;
1513
Chaitanya Kulkarni34e08192019-02-20 20:13:34 -08001514 if (!blk_rq_nr_phys_segments(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001515 return nvme_rdma_set_sg_null(c);
1516
Israel Rukshin324d9e72020-05-19 17:05:55 +03001517 req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1);
1518 ret = sg_alloc_table_chained(&req->data_sgl.sg_table,
1519 blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl,
Israel Rukshin38e18002019-11-24 18:38:30 +02001520 NVME_INLINE_SG_CNT);
Christoph Hellwig71102302016-07-06 21:55:52 +09001521 if (ret)
1522 return -ENOMEM;
1523
Israel Rukshin324d9e72020-05-19 17:05:55 +03001524 req->data_sgl.nents = blk_rq_map_sg(rq->q, rq,
1525 req->data_sgl.sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001526
Israel Rukshin324d9e72020-05-19 17:05:55 +03001527 count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl,
1528 req->data_sgl.nents, rq_dma_dir(rq));
Christoph Hellwig71102302016-07-06 21:55:52 +09001529 if (unlikely(count <= 0)) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001530 ret = -EIO;
1531 goto out_free_table;
Christoph Hellwig71102302016-07-06 21:55:52 +09001532 }
1533
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001534 if (blk_integrity_rq(rq)) {
1535 req->metadata_sgl->sg_table.sgl =
1536 (struct scatterlist *)(req->metadata_sgl + 1);
1537 ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table,
1538 blk_rq_count_integrity_sg(rq->q, rq->bio),
1539 req->metadata_sgl->sg_table.sgl,
1540 NVME_INLINE_METADATA_SG_CNT);
1541 if (unlikely(ret)) {
1542 ret = -ENOMEM;
1543 goto out_unmap_sg;
1544 }
1545
1546 req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q,
1547 rq->bio, req->metadata_sgl->sg_table.sgl);
1548 pi_count = ib_dma_map_sg(ibdev,
1549 req->metadata_sgl->sg_table.sgl,
1550 req->metadata_sgl->nents,
1551 rq_dma_dir(rq));
1552 if (unlikely(pi_count <= 0)) {
1553 ret = -EIO;
1554 goto out_free_pi_table;
1555 }
1556 }
1557
1558 if (req->use_sig_mr) {
1559 ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count);
1560 goto out;
1561 }
1562
Steve Wise64a741c2018-06-20 07:15:05 -07001563 if (count <= dev->num_inline_segments) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001564 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
Steve Wise64a741c2018-06-20 07:15:05 -07001565 queue->ctrl->use_inline_data &&
Christoph Hellwigb131c612017-01-13 12:29:12 +01001566 blk_rq_payload_bytes(rq) <=
Max Gurtovoy94423a82018-06-10 16:58:29 +03001567 nvme_rdma_inline_data_size(queue)) {
Steve Wise64a741c2018-06-20 07:15:05 -07001568 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001569 goto out;
1570 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001571
Steve Wise64a741c2018-06-20 07:15:05 -07001572 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001573 ret = nvme_rdma_map_sg_single(queue, req, c);
1574 goto out;
1575 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001576 }
1577
Max Gurtovoy94423a82018-06-10 16:58:29 +03001578 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1579out:
1580 if (unlikely(ret))
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001581 goto out_unmap_pi_sg;
Max Gurtovoy94423a82018-06-10 16:58:29 +03001582
1583 return 0;
1584
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03001585out_unmap_pi_sg:
1586 if (blk_integrity_rq(rq))
1587 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1588 req->metadata_sgl->nents, rq_dma_dir(rq));
1589out_free_pi_table:
1590 if (blk_integrity_rq(rq))
1591 sg_free_table_chained(&req->metadata_sgl->sg_table,
1592 NVME_INLINE_METADATA_SG_CNT);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001593out_unmap_sg:
Israel Rukshin324d9e72020-05-19 17:05:55 +03001594 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1595 rq_dma_dir(rq));
Max Gurtovoy94423a82018-06-10 16:58:29 +03001596out_free_table:
Israel Rukshin324d9e72020-05-19 17:05:55 +03001597 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001598 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001599}
1600
1601static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1602{
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001603 struct nvme_rdma_qe *qe =
1604 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1605 struct nvme_rdma_request *req =
1606 container_of(qe, struct nvme_rdma_request, sqe);
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001607
Christoph Hellwig84465462020-06-11 08:44:51 +02001608 if (unlikely(wc->status != IB_WC_SUCCESS))
Christoph Hellwig71102302016-07-06 21:55:52 +09001609 nvme_rdma_wr_error(cq, wc, "SEND");
Christoph Hellwig84465462020-06-11 08:44:51 +02001610 else
1611 nvme_rdma_end_request(req);
Christoph Hellwig71102302016-07-06 21:55:52 +09001612}
1613
1614static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1615 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001616 struct ib_send_wr *first)
Christoph Hellwig71102302016-07-06 21:55:52 +09001617{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001618 struct ib_send_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001619 int ret;
1620
1621 sge->addr = qe->dma;
Israel Rukshina62315b2020-03-31 15:46:33 +03001622 sge->length = sizeof(struct nvme_command);
Christoph Hellwig71102302016-07-06 21:55:52 +09001623 sge->lkey = queue->device->pd->local_dma_lkey;
1624
Christoph Hellwig71102302016-07-06 21:55:52 +09001625 wr.next = NULL;
1626 wr.wr_cqe = &qe->cqe;
1627 wr.sg_list = sge;
1628 wr.num_sge = num_sge;
1629 wr.opcode = IB_WR_SEND;
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001630 wr.send_flags = IB_SEND_SIGNALED;
Christoph Hellwig71102302016-07-06 21:55:52 +09001631
1632 if (first)
1633 first->next = &wr;
1634 else
1635 first = &wr;
1636
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001637 ret = ib_post_send(queue->qp, first, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001638 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001639 dev_err(queue->ctrl->ctrl.device,
1640 "%s failed with error code %d\n", __func__, ret);
1641 }
1642 return ret;
1643}
1644
1645static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1646 struct nvme_rdma_qe *qe)
1647{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001648 struct ib_recv_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001649 struct ib_sge list;
1650 int ret;
1651
1652 list.addr = qe->dma;
1653 list.length = sizeof(struct nvme_completion);
1654 list.lkey = queue->device->pd->local_dma_lkey;
1655
1656 qe->cqe.done = nvme_rdma_recv_done;
1657
1658 wr.next = NULL;
1659 wr.wr_cqe = &qe->cqe;
1660 wr.sg_list = &list;
1661 wr.num_sge = 1;
1662
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001663 ret = ib_post_recv(queue->qp, &wr, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001664 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001665 dev_err(queue->ctrl->ctrl.device,
1666 "%s failed with error code %d\n", __func__, ret);
1667 }
1668 return ret;
1669}
1670
1671static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1672{
1673 u32 queue_idx = nvme_rdma_queue_idx(queue);
1674
1675 if (queue_idx == 0)
1676 return queue->ctrl->admin_tag_set.tags[queue_idx];
1677 return queue->ctrl->tag_set.tags[queue_idx - 1];
1678}
1679
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001680static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1681{
1682 if (unlikely(wc->status != IB_WC_SUCCESS))
1683 nvme_rdma_wr_error(cq, wc, "ASYNC");
1684}
1685
Keith Buschad22c352017-11-07 15:13:12 -07001686static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
Christoph Hellwig71102302016-07-06 21:55:52 +09001687{
1688 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1689 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1690 struct ib_device *dev = queue->device->dev;
1691 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1692 struct nvme_command *cmd = sqe->data;
1693 struct ib_sge sge;
1694 int ret;
1695
Christoph Hellwig71102302016-07-06 21:55:52 +09001696 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1697
1698 memset(cmd, 0, sizeof(*cmd));
1699 cmd->common.opcode = nvme_admin_async_event;
Keith Busch38dabe22017-11-07 15:13:10 -07001700 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Christoph Hellwig71102302016-07-06 21:55:52 +09001701 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1702 nvme_rdma_set_sg_null(cmd);
1703
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001704 sqe->cqe.done = nvme_rdma_async_done;
1705
Christoph Hellwig71102302016-07-06 21:55:52 +09001706 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1707 DMA_TO_DEVICE);
1708
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001709 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001710 WARN_ON_ONCE(ret);
1711}
1712
Jens Axboe1052b8a2018-11-26 08:21:49 -07001713static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1714 struct nvme_completion *cqe, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001715{
Christoph Hellwig71102302016-07-06 21:55:52 +09001716 struct request *rq;
1717 struct nvme_rdma_request *req;
Christoph Hellwig71102302016-07-06 21:55:52 +09001718
Christoph Hellwig71102302016-07-06 21:55:52 +09001719 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1720 if (!rq) {
1721 dev_err(queue->ctrl->ctrl.device,
1722 "tag 0x%x on QP %#x not found\n",
1723 cqe->command_id, queue->qp->qp_num);
1724 nvme_rdma_error_recovery(queue->ctrl);
Jens Axboe1052b8a2018-11-26 08:21:49 -07001725 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001726 }
1727 req = blk_mq_rq_to_pdu(rq);
1728
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001729 req->status = cqe->status;
1730 req->result = cqe->result;
Christoph Hellwig71102302016-07-06 21:55:52 +09001731
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001732 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
Chao Lenga87da502020-10-12 16:55:37 +08001733 if (unlikely(!req->mr ||
1734 wc->ex.invalidate_rkey != req->mr->rkey)) {
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001735 dev_err(queue->ctrl->ctrl.device,
1736 "Bogus remote invalidation for rkey %#x\n",
Chao Lenga87da502020-10-12 16:55:37 +08001737 req->mr ? req->mr->rkey : 0);
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001738 nvme_rdma_error_recovery(queue->ctrl);
1739 }
Israel Rukshinf41725b2017-11-26 10:40:55 +00001740 } else if (req->mr) {
Jens Axboe1052b8a2018-11-26 08:21:49 -07001741 int ret;
1742
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001743 ret = nvme_rdma_inv_rkey(queue, req);
1744 if (unlikely(ret < 0)) {
1745 dev_err(queue->ctrl->ctrl.device,
1746 "Queueing INV WR for rkey %#x failed (%d)\n",
1747 req->mr->rkey, ret);
1748 nvme_rdma_error_recovery(queue->ctrl);
1749 }
1750 /* the local invalidation completion will end the request */
Christoph Hellwig7a804c32020-06-23 18:22:39 +02001751 return;
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001752 }
Christoph Hellwig7a804c32020-06-23 18:22:39 +02001753
1754 nvme_rdma_end_request(req);
Christoph Hellwig71102302016-07-06 21:55:52 +09001755}
1756
Jens Axboe1052b8a2018-11-26 08:21:49 -07001757static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001758{
1759 struct nvme_rdma_qe *qe =
1760 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
Yamin Friedman287f3292020-07-13 11:53:29 +03001761 struct nvme_rdma_queue *queue = wc->qp->qp_context;
Christoph Hellwig71102302016-07-06 21:55:52 +09001762 struct ib_device *ibdev = queue->device->dev;
1763 struct nvme_completion *cqe = qe->data;
1764 const size_t len = sizeof(struct nvme_completion);
Christoph Hellwig71102302016-07-06 21:55:52 +09001765
1766 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1767 nvme_rdma_wr_error(cq, wc, "RECV");
Jens Axboe1052b8a2018-11-26 08:21:49 -07001768 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001769 }
1770
zhenwei pi25c1ca62020-10-25 19:51:24 +08001771 /* sanity checking for received data length */
1772 if (unlikely(wc->byte_len < len)) {
1773 dev_err(queue->ctrl->ctrl.device,
1774 "Unexpected nvme completion length(%d)\n", wc->byte_len);
1775 nvme_rdma_error_recovery(queue->ctrl);
1776 return;
1777 }
1778
Christoph Hellwig71102302016-07-06 21:55:52 +09001779 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1780 /*
1781 * AEN requests are special as they don't time out and can
1782 * survive any kind of queue freeze and often don't respond to
1783 * aborts. We don't even bother to allocate a struct request
1784 * for them but rather special case them here.
1785 */
Israel Rukshin58a8df62019-10-13 19:57:31 +03001786 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue),
1787 cqe->command_id)))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001788 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1789 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001790 else
Jens Axboe1052b8a2018-11-26 08:21:49 -07001791 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
Christoph Hellwig71102302016-07-06 21:55:52 +09001792 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1793
1794 nvme_rdma_post_recv(queue, qe);
Christoph Hellwig71102302016-07-06 21:55:52 +09001795}
1796
1797static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1798{
1799 int ret, i;
1800
1801 for (i = 0; i < queue->queue_size; i++) {
1802 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1803 if (ret)
1804 goto out_destroy_queue_ib;
1805 }
1806
1807 return 0;
1808
1809out_destroy_queue_ib:
1810 nvme_rdma_destroy_queue_ib(queue);
1811 return ret;
1812}
1813
1814static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1815 struct rdma_cm_event *ev)
1816{
Steve Wise7f039532016-10-26 12:36:47 -07001817 struct rdma_cm_id *cm_id = queue->cm_id;
1818 int status = ev->status;
1819 const char *rej_msg;
1820 const struct nvme_rdma_cm_rej *rej_data;
1821 u8 rej_data_len;
1822
1823 rej_msg = rdma_reject_msg(cm_id, status);
1824 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1825
1826 if (rej_data && rej_data_len >= sizeof(u16)) {
1827 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001828
1829 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001830 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1831 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001832 } else {
1833 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001834 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001835 }
1836
1837 return -ECONNRESET;
1838}
1839
1840static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1841{
Israel Rukshine63440d2019-08-18 12:08:52 +03001842 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001843 int ret;
1844
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001845 ret = nvme_rdma_create_queue_ib(queue);
1846 if (ret)
1847 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001848
Israel Rukshine63440d2019-08-18 12:08:52 +03001849 if (ctrl->opts->tos >= 0)
1850 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
Christoph Hellwig71102302016-07-06 21:55:52 +09001851 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1852 if (ret) {
Israel Rukshine63440d2019-08-18 12:08:52 +03001853 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001854 queue->cm_error);
1855 goto out_destroy_queue;
1856 }
1857
1858 return 0;
1859
1860out_destroy_queue:
1861 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001862 return ret;
1863}
1864
1865static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1866{
1867 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1868 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001869 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001870 int ret;
1871
1872 param.qp_num = queue->qp->qp_num;
1873 param.flow_control = 1;
1874
1875 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001876 /* maximum retry count */
1877 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001878 param.rnr_retry_count = 7;
1879 param.private_data = &priv;
1880 param.private_data_len = sizeof(priv);
1881
1882 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1883 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001884 /*
1885 * set the admin queue depth to the minimum size
1886 * specified by the Fabrics standard.
1887 */
1888 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001889 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1890 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001891 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001892 /*
1893 * current interpretation of the fabrics spec
1894 * is at minimum you make hrqsize sqsize+1, or a
1895 * 1's based representation of sqsize.
1896 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001897 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001898 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001899 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001900
1901 ret = rdma_connect(queue->cm_id, &param);
1902 if (ret) {
1903 dev_err(ctrl->ctrl.device,
1904 "rdma_connect failed (%d).\n", ret);
1905 goto out_destroy_queue_ib;
1906 }
1907
1908 return 0;
1909
1910out_destroy_queue_ib:
1911 nvme_rdma_destroy_queue_ib(queue);
1912 return ret;
1913}
1914
Christoph Hellwig71102302016-07-06 21:55:52 +09001915static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1916 struct rdma_cm_event *ev)
1917{
1918 struct nvme_rdma_queue *queue = cm_id->context;
1919 int cm_error = 0;
1920
1921 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1922 rdma_event_msg(ev->event), ev->event,
1923 ev->status, cm_id);
1924
1925 switch (ev->event) {
1926 case RDMA_CM_EVENT_ADDR_RESOLVED:
1927 cm_error = nvme_rdma_addr_resolved(queue);
1928 break;
1929 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1930 cm_error = nvme_rdma_route_resolved(queue);
1931 break;
1932 case RDMA_CM_EVENT_ESTABLISHED:
1933 queue->cm_error = nvme_rdma_conn_established(queue);
1934 /* complete cm_done regardless of success/failure */
1935 complete(&queue->cm_done);
1936 return 0;
1937 case RDMA_CM_EVENT_REJECTED:
1938 cm_error = nvme_rdma_conn_rejected(queue, ev);
1939 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001940 case RDMA_CM_EVENT_ROUTE_ERROR:
1941 case RDMA_CM_EVENT_CONNECT_ERROR:
1942 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001943 nvme_rdma_destroy_queue_ib(queue);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001944 fallthrough;
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001945 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001946 dev_dbg(queue->ctrl->ctrl.device,
1947 "CM error event %d\n", ev->event);
1948 cm_error = -ECONNRESET;
1949 break;
1950 case RDMA_CM_EVENT_DISCONNECTED:
1951 case RDMA_CM_EVENT_ADDR_CHANGE:
1952 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1953 dev_dbg(queue->ctrl->ctrl.device,
1954 "disconnect received - connection closed\n");
1955 nvme_rdma_error_recovery(queue->ctrl);
1956 break;
1957 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001958 /* device removal is handled via the ib_client API */
1959 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001960 default:
1961 dev_err(queue->ctrl->ctrl.device,
1962 "Unexpected RDMA CM event (%d)\n", ev->event);
1963 nvme_rdma_error_recovery(queue->ctrl);
1964 break;
1965 }
1966
1967 if (cm_error) {
1968 queue->cm_error = cm_error;
1969 complete(&queue->cm_done);
1970 }
1971
1972 return 0;
1973}
1974
Sagi Grimberg0475a8d2020-07-29 02:36:03 -07001975static void nvme_rdma_complete_timed_out(struct request *rq)
1976{
1977 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1978 struct nvme_rdma_queue *queue = req->queue;
1979 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1980
1981 /* fence other contexts that may complete the command */
1982 mutex_lock(&ctrl->teardown_lock);
1983 nvme_rdma_stop_queue(queue);
1984 if (!blk_mq_request_completed(rq)) {
1985 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
1986 blk_mq_complete_request(rq);
1987 }
1988 mutex_unlock(&ctrl->teardown_lock);
1989}
1990
Christoph Hellwig71102302016-07-06 21:55:52 +09001991static enum blk_eh_timer_return
1992nvme_rdma_timeout(struct request *rq, bool reserved)
1993{
1994 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001995 struct nvme_rdma_queue *queue = req->queue;
1996 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001997
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001998 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1999 rq->tag, nvme_rdma_queue_idx(queue));
Nitzan Carmie62a5382017-10-22 09:37:04 +00002000
Sagi Grimberg4c174e62019-01-08 00:53:22 -08002001 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
2002 /*
Sagi Grimberg0475a8d2020-07-29 02:36:03 -07002003 * If we are resetting, connecting or deleting we should
2004 * complete immediately because we may block controller
2005 * teardown or setup sequence
2006 * - ctrl disable/shutdown fabrics requests
2007 * - connect requests
2008 * - initialization admin requests
2009 * - I/O requests that entered after unquiescing and
2010 * the controller stopped responding
2011 *
2012 * All other requests should be cancelled by the error
2013 * recovery work, so it's fine that we fail it here.
Sagi Grimberg4c174e62019-01-08 00:53:22 -08002014 */
Sagi Grimberg0475a8d2020-07-29 02:36:03 -07002015 nvme_rdma_complete_timed_out(rq);
Sagi Grimberg4c174e62019-01-08 00:53:22 -08002016 return BLK_EH_DONE;
2017 }
Christoph Hellwig71102302016-07-06 21:55:52 +09002018
Sagi Grimberg0475a8d2020-07-29 02:36:03 -07002019 /*
2020 * LIVE state should trigger the normal error recovery which will
2021 * handle completing this request.
2022 */
Sagi Grimberg4c174e62019-01-08 00:53:22 -08002023 nvme_rdma_error_recovery(ctrl);
Sagi Grimberg4c174e62019-01-08 00:53:22 -08002024 return BLK_EH_RESET_TIMER;
Christoph Hellwig71102302016-07-06 21:55:52 +09002025}
2026
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002027static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09002028 const struct blk_mq_queue_data *bd)
2029{
2030 struct nvme_ns *ns = hctx->queue->queuedata;
2031 struct nvme_rdma_queue *queue = hctx->driver_data;
2032 struct request *rq = bd->rq;
2033 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2034 struct nvme_rdma_qe *sqe = &req->sqe;
2035 struct nvme_command *c = sqe->data;
Christoph Hellwig71102302016-07-06 21:55:52 +09002036 struct ib_device *dev;
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02002037 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002038 blk_status_t ret;
2039 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09002040
2041 WARN_ON_ONCE(rq->tag < 0);
2042
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02002043 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
James Smart6cdefc62018-07-20 15:49:48 -07002044 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06002045
Christoph Hellwig71102302016-07-06 21:55:52 +09002046 dev = queue->device->dev;
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002047
2048 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
2049 sizeof(struct nvme_command),
2050 DMA_TO_DEVICE);
2051 err = ib_dma_mapping_error(dev, req->sqe.dma);
2052 if (unlikely(err))
2053 return BLK_STS_RESOURCE;
2054
Christoph Hellwig71102302016-07-06 21:55:52 +09002055 ib_dma_sync_single_for_cpu(dev, sqe->dma,
2056 sizeof(struct nvme_command), DMA_TO_DEVICE);
2057
2058 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002059 if (ret)
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002060 goto unmap_qe;
Christoph Hellwig71102302016-07-06 21:55:52 +09002061
Christoph Hellwig71102302016-07-06 21:55:52 +09002062 blk_mq_start_request(rq);
2063
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03002064 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
2065 queue->pi_support &&
2066 (c->common.opcode == nvme_cmd_write ||
2067 c->common.opcode == nvme_cmd_read) &&
2068 nvme_ns_has_pi(ns))
2069 req->use_sig_mr = true;
2070 else
2071 req->use_sig_mr = false;
2072
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002073 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03002074 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09002075 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002076 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09002077 goto err;
2078 }
2079
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02002080 sqe->cqe.done = nvme_rdma_send_done;
2081
Christoph Hellwig71102302016-07-06 21:55:52 +09002082 ib_dma_sync_single_for_device(dev, sqe->dma,
2083 sizeof(struct nvme_command), DMA_TO_DEVICE);
2084
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002085 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Israel Rukshinf41725b2017-11-26 10:40:55 +00002086 req->mr ? &req->reg_wr.wr : NULL);
Max Gurtovoy16686f32019-10-13 19:57:36 +03002087 if (unlikely(err))
2088 goto err_unmap;
Christoph Hellwig71102302016-07-06 21:55:52 +09002089
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002090 return BLK_STS_OK;
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002091
Max Gurtovoy16686f32019-10-13 19:57:36 +03002092err_unmap:
2093 nvme_rdma_unmap_data(queue, rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09002094err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02002095 if (err == -ENOMEM || err == -EAGAIN)
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002096 ret = BLK_STS_RESOURCE;
2097 else
2098 ret = BLK_STS_IOERR;
Max Gurtovoy16686f32019-10-13 19:57:36 +03002099 nvme_cleanup_cmd(rq);
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002100unmap_qe:
2101 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
2102 DMA_TO_DEVICE);
2103 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002104}
2105
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002106static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
2107{
2108 struct nvme_rdma_queue *queue = hctx->driver_data;
2109
2110 return ib_process_cq_direct(queue->ib_cq, -1);
2111}
2112
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03002113static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req)
2114{
2115 struct request *rq = blk_mq_rq_from_pdu(req);
2116 struct ib_mr_status mr_status;
2117 int ret;
2118
2119 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
2120 if (ret) {
2121 pr_err("ib_check_mr_status failed, ret %d\n", ret);
2122 nvme_req(rq)->status = NVME_SC_INVALID_PI;
2123 return;
2124 }
2125
2126 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
2127 switch (mr_status.sig_err.err_type) {
2128 case IB_SIG_BAD_GUARD:
2129 nvme_req(rq)->status = NVME_SC_GUARD_CHECK;
2130 break;
2131 case IB_SIG_BAD_REFTAG:
2132 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK;
2133 break;
2134 case IB_SIG_BAD_APPTAG:
2135 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK;
2136 break;
2137 }
2138 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
2139 mr_status.sig_err.err_type, mr_status.sig_err.expected,
2140 mr_status.sig_err.actual);
2141 }
2142}
2143
Christoph Hellwig71102302016-07-06 21:55:52 +09002144static void nvme_rdma_complete_rq(struct request *rq)
2145{
2146 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002147 struct nvme_rdma_queue *queue = req->queue;
2148 struct ib_device *ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +09002149
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03002150 if (req->use_sig_mr)
2151 nvme_rdma_check_pi_status(req);
2152
Max Gurtovoy62f99b62019-06-06 12:27:36 +03002153 nvme_rdma_unmap_data(queue, rq);
2154 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
2155 DMA_TO_DEVICE);
Christoph Hellwig77f02a72017-03-30 13:41:32 +02002156 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09002157}
2158
Sagi Grimberg0b366582017-07-13 11:09:44 +03002159static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
2160{
2161 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002162 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +03002163
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002164 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002165 /* separate read/write queues */
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002166 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2167 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2168 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2169 set->map[HCTX_TYPE_READ].nr_queues =
2170 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002171 set->map[HCTX_TYPE_READ].queue_offset =
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002172 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002173 } else {
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002174 /* shared read/write queues */
2175 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2176 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2177 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2178 set->map[HCTX_TYPE_READ].nr_queues =
2179 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002180 set->map[HCTX_TYPE_READ].queue_offset = 0;
2181 }
2182 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
2183 ctrl->device->dev, 0);
2184 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
2185 ctrl->device->dev, 0);
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002186
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002187 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2188 /* map dedicated poll queues only if we have queues left */
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002189 set->map[HCTX_TYPE_POLL].nr_queues =
Sagi Grimbergb1064d32019-01-18 16:43:24 -08002190 ctrl->io_queues[HCTX_TYPE_POLL];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002191 set->map[HCTX_TYPE_POLL].queue_offset =
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002192 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2193 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002194 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2195 }
Sagi Grimberg5651cd32019-05-28 22:49:04 -07002196
2197 dev_info(ctrl->ctrl.device,
2198 "mapped %d/%d/%d default/read/poll queues.\n",
2199 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2200 ctrl->io_queues[HCTX_TYPE_READ],
2201 ctrl->io_queues[HCTX_TYPE_POLL]);
2202
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002203 return 0;
Sagi Grimberg0b366582017-07-13 11:09:44 +03002204}
2205
Eric Biggersf363b082017-03-30 13:39:16 -07002206static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09002207 .queue_rq = nvme_rdma_queue_rq,
2208 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09002209 .init_request = nvme_rdma_init_request,
2210 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09002211 .init_hctx = nvme_rdma_init_hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09002212 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03002213 .map_queues = nvme_rdma_map_queues,
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002214 .poll = nvme_rdma_poll,
Christoph Hellwig71102302016-07-06 21:55:52 +09002215};
2216
Eric Biggersf363b082017-03-30 13:39:16 -07002217static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09002218 .queue_rq = nvme_rdma_queue_rq,
2219 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02002220 .init_request = nvme_rdma_init_request,
2221 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09002222 .init_hctx = nvme_rdma_init_admin_hctx,
2223 .timeout = nvme_rdma_timeout,
2224};
2225
Sagi Grimberg18398af2017-07-10 09:22:31 +03002226static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09002227{
Sagi Grimberg794a4cb2019-01-01 00:19:30 -08002228 cancel_work_sync(&ctrl->err_work);
2229 cancel_delayed_work_sync(&ctrl->reconnect_work);
2230
Sagi Grimberg75862c72018-07-09 12:49:07 +03002231 nvme_rdma_teardown_io_queues(ctrl, shutdown);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07002232 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg18398af2017-07-10 09:22:31 +03002233 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09002234 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03002235 else
Sagi Grimbergb5b05042019-07-22 17:06:54 -07002236 nvme_disable_ctrl(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03002237 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09002238}
2239
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002240static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03002241{
Christoph Hellwige9bc2582017-10-29 10:44:30 +02002242 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
Christoph Hellwig71102302016-07-06 21:55:52 +09002243}
2244
Christoph Hellwig71102302016-07-06 21:55:52 +09002245static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
2246{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002247 struct nvme_rdma_ctrl *ctrl =
2248 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09002249
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002250 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03002251 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09002252
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002253 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02002254 /* state change failure should never happen */
2255 WARN_ON_ONCE(1);
2256 return;
2257 }
2258
Sagi Grimbergc66e2992018-07-09 12:49:06 +03002259 if (nvme_rdma_setup_ctrl(ctrl, false))
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03002260 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09002261
Christoph Hellwig71102302016-07-06 21:55:52 +09002262 return;
2263
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03002264out_fail:
Nitzan Carmi8000d1f2018-01-17 11:01:14 +00002265 ++ctrl->ctrl.nr_reconnects;
2266 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09002267}
2268
Christoph Hellwig71102302016-07-06 21:55:52 +09002269static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
2270 .name = "rdma",
2271 .module = THIS_MODULE,
Max Gurtovoy5ec5d3b2020-05-19 17:05:56 +03002272 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED,
Christoph Hellwig71102302016-07-06 21:55:52 +09002273 .reg_read32 = nvmf_reg_read32,
2274 .reg_read64 = nvmf_reg_read64,
2275 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09002276 .free_ctrl = nvme_rdma_free_ctrl,
2277 .submit_async_event = nvme_rdma_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002278 .delete_ctrl = nvme_rdma_delete_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09002279 .get_address = nvmf_get_address,
2280};
2281
James Smart36e835f2017-10-20 16:17:09 -07002282/*
2283 * Fails a connection request if it matches an existing controller
2284 * (association) with the same tuple:
2285 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
2286 *
2287 * if local address is not specified in the request, it will match an
2288 * existing controller with all the other parameters the same and no
2289 * local port address specified as well.
2290 *
2291 * The ports don't need to be compared as they are intrinsically
2292 * already matched by the port pointers supplied.
2293 */
2294static bool
2295nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
2296{
2297 struct nvme_rdma_ctrl *ctrl;
2298 bool found = false;
2299
2300 mutex_lock(&nvme_rdma_ctrl_mutex);
2301 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
Sagi Grimbergb7c7be6f62018-10-18 17:40:40 -07002302 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
James Smart36e835f2017-10-20 16:17:09 -07002303 if (found)
2304 break;
2305 }
2306 mutex_unlock(&nvme_rdma_ctrl_mutex);
2307
2308 return found;
2309}
2310
Christoph Hellwig71102302016-07-06 21:55:52 +09002311static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
2312 struct nvmf_ctrl_options *opts)
2313{
2314 struct nvme_rdma_ctrl *ctrl;
2315 int ret;
2316 bool changed;
2317
2318 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2319 if (!ctrl)
2320 return ERR_PTR(-ENOMEM);
2321 ctrl->ctrl.opts = opts;
2322 INIT_LIST_HEAD(&ctrl->list);
Sagi Grimberg5110f402020-08-05 18:13:58 -07002323 mutex_init(&ctrl->teardown_lock);
Christoph Hellwig71102302016-07-06 21:55:52 +09002324
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07002325 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2326 opts->trsvcid =
2327 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
2328 if (!opts->trsvcid) {
2329 ret = -ENOMEM;
2330 goto out_free_ctrl;
2331 }
2332 opts->mask |= NVMF_OPT_TRSVCID;
2333 }
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02002334
2335 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07002336 opts->traddr, opts->trsvcid, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09002337 if (ret) {
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07002338 pr_err("malformed address passed: %s:%s\n",
2339 opts->traddr, opts->trsvcid);
Christoph Hellwig71102302016-07-06 21:55:52 +09002340 goto out_free_ctrl;
2341 }
2342
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002343 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02002344 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2345 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002346 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02002347 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002348 opts->host_traddr);
2349 goto out_free_ctrl;
2350 }
2351 }
2352
James Smart36e835f2017-10-20 16:17:09 -07002353 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
2354 ret = -EALREADY;
2355 goto out_free_ctrl;
2356 }
2357
Christoph Hellwig71102302016-07-06 21:55:52 +09002358 INIT_DELAYED_WORK(&ctrl->reconnect_work,
2359 nvme_rdma_reconnect_ctrl_work);
2360 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002361 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09002362
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002363 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2364 opts->nr_poll_queues + 1;
Jay Freyenseec5af8652016-08-17 15:00:27 -07002365 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09002366 ctrl->ctrl.kato = opts->kato;
2367
2368 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03002369 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09002370 GFP_KERNEL);
2371 if (!ctrl->queues)
Sagi Grimberg3d064102018-06-19 15:34:09 +03002372 goto out_free_ctrl;
2373
2374 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2375 0 /* no quirks, we're perfect! */);
2376 if (ret)
2377 goto out_kfree_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +09002378
Max Gurtovoyb754a322018-01-31 18:31:25 +02002379 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2380 WARN_ON_ONCE(!changed);
2381
Sagi Grimbergc66e2992018-07-09 12:49:06 +03002382 ret = nvme_rdma_setup_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09002383 if (ret)
Sagi Grimberg3d064102018-06-19 15:34:09 +03002384 goto out_uninit_ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09002385
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02002386 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09002387 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2388
Christoph Hellwig71102302016-07-06 21:55:52 +09002389 mutex_lock(&nvme_rdma_ctrl_mutex);
2390 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2391 mutex_unlock(&nvme_rdma_ctrl_mutex);
2392
Christoph Hellwig71102302016-07-06 21:55:52 +09002393 return &ctrl->ctrl;
2394
Christoph Hellwig71102302016-07-06 21:55:52 +09002395out_uninit_ctrl:
2396 nvme_uninit_ctrl(&ctrl->ctrl);
2397 nvme_put_ctrl(&ctrl->ctrl);
2398 if (ret > 0)
2399 ret = -EIO;
2400 return ERR_PTR(ret);
Sagi Grimberg3d064102018-06-19 15:34:09 +03002401out_kfree_queues:
2402 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +09002403out_free_ctrl:
2404 kfree(ctrl);
2405 return ERR_PTR(ret);
2406}
2407
2408static struct nvmf_transport_ops nvme_rdma_transport = {
2409 .name = "rdma",
Roy Shterman0de5cd32017-12-25 14:18:30 +02002410 .module = THIS_MODULE,
Christoph Hellwig71102302016-07-06 21:55:52 +09002411 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002412 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002413 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
Israel Rukshine63440d2019-08-18 12:08:52 +03002414 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2415 NVMF_OPT_TOS,
Christoph Hellwig71102302016-07-06 21:55:52 +09002416 .create_ctrl = nvme_rdma_create_ctrl,
2417};
2418
Steve Wisee87a9112016-09-02 09:01:54 -07002419static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2420{
2421 struct nvme_rdma_ctrl *ctrl;
Max Gurtovoy9bad0402018-02-28 13:12:39 +02002422 struct nvme_rdma_device *ndev;
2423 bool found = false;
2424
2425 mutex_lock(&device_list_mutex);
2426 list_for_each_entry(ndev, &device_list, entry) {
2427 if (ndev->dev == ib_device) {
2428 found = true;
2429 break;
2430 }
2431 }
2432 mutex_unlock(&device_list_mutex);
2433
2434 if (!found)
2435 return;
Steve Wisee87a9112016-09-02 09:01:54 -07002436
2437 /* Delete all controllers using this device */
2438 mutex_lock(&nvme_rdma_ctrl_mutex);
2439 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2440 if (ctrl->device->dev != ib_device)
2441 continue;
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002442 nvme_delete_ctrl(&ctrl->ctrl);
Steve Wisee87a9112016-09-02 09:01:54 -07002443 }
2444 mutex_unlock(&nvme_rdma_ctrl_mutex);
2445
Roy Shtermanb227c592018-01-14 12:39:02 +02002446 flush_workqueue(nvme_delete_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002447}
2448
2449static struct ib_client nvme_rdma_ib_client = {
2450 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002451 .remove = nvme_rdma_remove_one
2452};
2453
Christoph Hellwig71102302016-07-06 21:55:52 +09002454static int __init nvme_rdma_init_module(void)
2455{
Steve Wisee87a9112016-09-02 09:01:54 -07002456 int ret;
2457
Steve Wisee87a9112016-09-02 09:01:54 -07002458 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002459 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002460 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002461
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002462 ret = nvmf_register_transport(&nvme_rdma_transport);
2463 if (ret)
2464 goto err_unreg_client;
2465
2466 return 0;
2467
2468err_unreg_client:
2469 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002470 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002471}
2472
2473static void __exit nvme_rdma_cleanup_module(void)
2474{
Max Gurtovoy9ad9e8d62019-10-29 16:42:27 +02002475 struct nvme_rdma_ctrl *ctrl;
2476
Christoph Hellwig71102302016-07-06 21:55:52 +09002477 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002478 ib_unregister_client(&nvme_rdma_ib_client);
Max Gurtovoy9ad9e8d62019-10-29 16:42:27 +02002479
2480 mutex_lock(&nvme_rdma_ctrl_mutex);
2481 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2482 nvme_delete_ctrl(&ctrl->ctrl);
2483 mutex_unlock(&nvme_rdma_ctrl_mutex);
2484 flush_workqueue(nvme_delete_wq);
Christoph Hellwig71102302016-07-06 21:55:52 +09002485}
2486
2487module_init(nvme_rdma_init_module);
2488module_exit(nvme_rdma_cleanup_module);
2489
2490MODULE_LICENSE("GPL v2");