blob: ac7462cd7f0f8ef87c9fc0b8af1914cfd03cec75 [file] [log] [blame]
Christoph Hellwig71102302016-07-06 21:55:52 +09001/*
2 * NVMe over Fabrics RDMA host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Christoph Hellwig71102302016-07-06 21:55:52 +090015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
Israel Rukshinf41725b2017-11-26 10:40:55 +000018#include <rdma/mr_pool.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090019#include <linux/err.h>
20#include <linux/string.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090021#include <linux/atomic.h>
22#include <linux/blk-mq.h>
Sagi Grimberg0b366582017-07-13 11:09:44 +030023#include <linux/blk-mq-rdma.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090024#include <linux/types.h>
25#include <linux/list.h>
26#include <linux/mutex.h>
27#include <linux/scatterlist.h>
28#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090029#include <asm/unaligned.h>
30
31#include <rdma/ib_verbs.h>
32#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090033#include <linux/nvme-rdma.h>
34
35#include "nvme.h"
36#include "fabrics.h"
37
38
Sagi Grimberg782d8202017-03-21 16:32:38 +020039#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090040
Christoph Hellwig71102302016-07-06 21:55:52 +090041#define NVME_RDMA_MAX_SEGMENTS 256
42
43#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
44
Christoph Hellwig71102302016-07-06 21:55:52 +090045struct nvme_rdma_device {
Max Gurtovoyf87c89a2017-10-23 12:59:27 +030046 struct ib_device *dev;
47 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090048 struct kref ref;
49 struct list_head entry;
50};
51
52struct nvme_rdma_qe {
53 struct ib_cqe cqe;
54 void *data;
55 u64 dma;
56};
57
58struct nvme_rdma_queue;
59struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080060 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090061 struct ib_mr *mr;
62 struct nvme_rdma_qe sqe;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +020063 union nvme_result result;
64 __le16 status;
65 refcount_t ref;
Christoph Hellwig71102302016-07-06 21:55:52 +090066 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
67 u32 num_sge;
68 int nents;
Christoph Hellwig71102302016-07-06 21:55:52 +090069 struct ib_reg_wr reg_wr;
70 struct ib_cqe reg_cqe;
71 struct nvme_rdma_queue *queue;
72 struct sg_table sg_table;
73 struct scatterlist first_sgl[];
74};
75
76enum nvme_rdma_queue_flags {
Sagi Grimberg5013e982017-10-11 15:29:12 +030077 NVME_RDMA_Q_ALLOCATED = 0,
78 NVME_RDMA_Q_LIVE = 1,
Max Gurtovoyeb1bd242017-11-28 18:28:44 +020079 NVME_RDMA_Q_TR_READY = 2,
Christoph Hellwig71102302016-07-06 21:55:52 +090080};
81
82struct nvme_rdma_queue {
83 struct nvme_rdma_qe *rsp_ring;
Christoph Hellwig71102302016-07-06 21:55:52 +090084 int queue_size;
85 size_t cmnd_capsule_len;
86 struct nvme_rdma_ctrl *ctrl;
87 struct nvme_rdma_device *device;
88 struct ib_cq *ib_cq;
89 struct ib_qp *qp;
90
91 unsigned long flags;
92 struct rdma_cm_id *cm_id;
93 int cm_error;
94 struct completion cm_done;
95};
96
97struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +090098 /* read only in the hot path */
99 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900100
101 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900102 struct blk_mq_tag_set tag_set;
Christoph Hellwig71102302016-07-06 21:55:52 +0900103 struct work_struct err_work;
104
105 struct nvme_rdma_qe async_event_sqe;
106
Christoph Hellwig71102302016-07-06 21:55:52 +0900107 struct delayed_work reconnect_work;
108
109 struct list_head list;
110
111 struct blk_mq_tag_set admin_tag_set;
112 struct nvme_rdma_device *device;
113
Christoph Hellwig71102302016-07-06 21:55:52 +0900114 u32 max_fr_pages;
115
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200116 struct sockaddr_storage addr;
117 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900118
119 struct nvme_ctrl ctrl;
120};
121
122static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
123{
124 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
125}
126
127static LIST_HEAD(device_list);
128static DEFINE_MUTEX(device_list_mutex);
129
130static LIST_HEAD(nvme_rdma_ctrl_list);
131static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
132
Christoph Hellwig71102302016-07-06 21:55:52 +0900133/*
134 * Disabling this option makes small I/O goes faster, but is fundamentally
135 * unsafe. With it turned off we will have to register a global rkey that
136 * allows read and write access to all physical memory.
137 */
138static bool register_always = true;
139module_param(register_always, bool, 0444);
140MODULE_PARM_DESC(register_always,
141 "Use memory registration even for contiguous memory regions");
142
143static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
144 struct rdma_cm_event *event);
145static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900146
Sagi Grimberg90af3512017-07-10 09:22:28 +0300147static const struct blk_mq_ops nvme_rdma_mq_ops;
148static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
149
Christoph Hellwig71102302016-07-06 21:55:52 +0900150/* XXX: really should move to a generic header sooner or later.. */
151static inline void put_unaligned_le24(u32 val, u8 *p)
152{
153 *p++ = val;
154 *p++ = val >> 8;
155 *p++ = val >> 16;
156}
157
158static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
159{
160 return queue - queue->ctrl->queues;
161}
162
163static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
164{
165 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
166}
167
168static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
169 size_t capsule_size, enum dma_data_direction dir)
170{
171 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
172 kfree(qe->data);
173}
174
175static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
176 size_t capsule_size, enum dma_data_direction dir)
177{
178 qe->data = kzalloc(capsule_size, GFP_KERNEL);
179 if (!qe->data)
180 return -ENOMEM;
181
182 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
183 if (ib_dma_mapping_error(ibdev, qe->dma)) {
184 kfree(qe->data);
185 return -ENOMEM;
186 }
187
188 return 0;
189}
190
191static void nvme_rdma_free_ring(struct ib_device *ibdev,
192 struct nvme_rdma_qe *ring, size_t ib_queue_size,
193 size_t capsule_size, enum dma_data_direction dir)
194{
195 int i;
196
197 for (i = 0; i < ib_queue_size; i++)
198 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
199 kfree(ring);
200}
201
202static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
203 size_t ib_queue_size, size_t capsule_size,
204 enum dma_data_direction dir)
205{
206 struct nvme_rdma_qe *ring;
207 int i;
208
209 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
210 if (!ring)
211 return NULL;
212
213 for (i = 0; i < ib_queue_size; i++) {
214 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
215 goto out_free_ring;
216 }
217
218 return ring;
219
220out_free_ring:
221 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
222 return NULL;
223}
224
225static void nvme_rdma_qp_event(struct ib_event *event, void *context)
226{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200227 pr_debug("QP event %s (%d)\n",
228 ib_event_msg(event->event), event->event);
229
Christoph Hellwig71102302016-07-06 21:55:52 +0900230}
231
232static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
233{
234 wait_for_completion_interruptible_timeout(&queue->cm_done,
235 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
236 return queue->cm_error;
237}
238
239static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
240{
241 struct nvme_rdma_device *dev = queue->device;
242 struct ib_qp_init_attr init_attr;
243 int ret;
244
245 memset(&init_attr, 0, sizeof(init_attr));
246 init_attr.event_handler = nvme_rdma_qp_event;
247 /* +1 for drain */
248 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
249 /* +1 for drain */
250 init_attr.cap.max_recv_wr = queue->queue_size + 1;
251 init_attr.cap.max_recv_sge = 1;
252 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
253 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
254 init_attr.qp_type = IB_QPT_RC;
255 init_attr.send_cq = queue->ib_cq;
256 init_attr.recv_cq = queue->ib_cq;
257
258 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
259
260 queue->qp = queue->cm_id->qp;
261 return ret;
262}
263
Christoph Hellwig385475e2017-06-13 09:15:19 +0200264static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
265 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900266{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200267 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900268 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200269 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900270 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
271 struct nvme_rdma_device *dev = queue->device;
272
Christoph Hellwig71102302016-07-06 21:55:52 +0900273 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
274 DMA_TO_DEVICE);
275}
276
Christoph Hellwig385475e2017-06-13 09:15:19 +0200277static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
278 struct request *rq, unsigned int hctx_idx,
279 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900280{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200281 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200283 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900284 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
285 struct nvme_rdma_device *dev = queue->device;
286 struct ib_device *ibdev = dev->dev;
287 int ret;
288
Christoph Hellwig71102302016-07-06 21:55:52 +0900289 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
290 DMA_TO_DEVICE);
291 if (ret)
292 return ret;
293
Christoph Hellwig71102302016-07-06 21:55:52 +0900294 req->queue = queue;
295
296 return 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900297}
298
Christoph Hellwig71102302016-07-06 21:55:52 +0900299static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
300 unsigned int hctx_idx)
301{
302 struct nvme_rdma_ctrl *ctrl = data;
303 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
304
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300305 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900306
307 hctx->driver_data = queue;
308 return 0;
309}
310
311static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
312 unsigned int hctx_idx)
313{
314 struct nvme_rdma_ctrl *ctrl = data;
315 struct nvme_rdma_queue *queue = &ctrl->queues[0];
316
317 BUG_ON(hctx_idx != 0);
318
319 hctx->driver_data = queue;
320 return 0;
321}
322
323static void nvme_rdma_free_dev(struct kref *ref)
324{
325 struct nvme_rdma_device *ndev =
326 container_of(ref, struct nvme_rdma_device, ref);
327
328 mutex_lock(&device_list_mutex);
329 list_del(&ndev->entry);
330 mutex_unlock(&device_list_mutex);
331
Christoph Hellwig71102302016-07-06 21:55:52 +0900332 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900333 kfree(ndev);
334}
335
336static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
337{
338 kref_put(&dev->ref, nvme_rdma_free_dev);
339}
340
341static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
342{
343 return kref_get_unless_zero(&dev->ref);
344}
345
346static struct nvme_rdma_device *
347nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
348{
349 struct nvme_rdma_device *ndev;
350
351 mutex_lock(&device_list_mutex);
352 list_for_each_entry(ndev, &device_list, entry) {
353 if (ndev->dev->node_guid == cm_id->device->node_guid &&
354 nvme_rdma_dev_get(ndev))
355 goto out_unlock;
356 }
357
358 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
359 if (!ndev)
360 goto out_err;
361
362 ndev->dev = cm_id->device;
363 kref_init(&ndev->ref);
364
Christoph Hellwig11975e02016-09-05 12:56:20 +0200365 ndev->pd = ib_alloc_pd(ndev->dev,
366 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900367 if (IS_ERR(ndev->pd))
368 goto out_free_dev;
369
Christoph Hellwig71102302016-07-06 21:55:52 +0900370 if (!(ndev->dev->attrs.device_cap_flags &
371 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
372 dev_err(&ndev->dev->dev,
373 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200374 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900375 }
376
377 list_add(&ndev->entry, &device_list);
378out_unlock:
379 mutex_unlock(&device_list_mutex);
380 return ndev;
381
Christoph Hellwig71102302016-07-06 21:55:52 +0900382out_free_pd:
383 ib_dealloc_pd(ndev->pd);
384out_free_dev:
385 kfree(ndev);
386out_err:
387 mutex_unlock(&device_list_mutex);
388 return NULL;
389}
390
391static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
392{
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200393 struct nvme_rdma_device *dev;
394 struct ib_device *ibdev;
395
396 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
397 return;
398
399 dev = queue->device;
400 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900401
Israel Rukshinf41725b2017-11-26 10:40:55 +0000402 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
403
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200404 /*
405 * The cm_id object might have been destroyed during RDMA connection
406 * establishment error flow to avoid getting other cma events, thus
407 * the destruction of the QP shouldn't use rdma_cm API.
408 */
409 ib_destroy_qp(queue->qp);
Christoph Hellwig71102302016-07-06 21:55:52 +0900410 ib_free_cq(queue->ib_cq);
411
412 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
413 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
414
415 nvme_rdma_dev_put(dev);
416}
417
Israel Rukshinf41725b2017-11-26 10:40:55 +0000418static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
419{
420 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
421 ibdev->attrs.max_fast_reg_page_list_len);
422}
423
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300424static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900425{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300426 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900427 const int send_wr_factor = 3; /* MR, SEND, INV */
428 const int cq_factor = send_wr_factor + 1; /* + RECV */
429 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900430 int ret;
431
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300432 queue->device = nvme_rdma_find_get_device(queue->cm_id);
433 if (!queue->device) {
434 dev_err(queue->cm_id->device->dev.parent,
435 "no client data found!\n");
436 return -ECONNREFUSED;
437 }
438 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900439
440 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300441 * Spread I/O queues completion vectors according their queue index.
442 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900443 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300444 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900445
446 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300447 queue->ib_cq = ib_alloc_cq(ibdev, queue,
448 cq_factor * queue->queue_size + 1,
449 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900450 if (IS_ERR(queue->ib_cq)) {
451 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300452 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900453 }
454
455 ret = nvme_rdma_create_qp(queue, send_wr_factor);
456 if (ret)
457 goto out_destroy_ib_cq;
458
459 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
460 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
461 if (!queue->rsp_ring) {
462 ret = -ENOMEM;
463 goto out_destroy_qp;
464 }
465
Israel Rukshinf41725b2017-11-26 10:40:55 +0000466 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
467 queue->queue_size,
468 IB_MR_TYPE_MEM_REG,
469 nvme_rdma_get_max_fr_pages(ibdev));
470 if (ret) {
471 dev_err(queue->ctrl->ctrl.device,
472 "failed to initialize MR pool sized %d for QID %d\n",
473 queue->queue_size, idx);
474 goto out_destroy_ring;
475 }
476
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200477 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
478
Christoph Hellwig71102302016-07-06 21:55:52 +0900479 return 0;
480
Israel Rukshinf41725b2017-11-26 10:40:55 +0000481out_destroy_ring:
482 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
483 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
Christoph Hellwig71102302016-07-06 21:55:52 +0900484out_destroy_qp:
Max Gurtovoy1f61def2017-11-06 16:18:51 +0200485 rdma_destroy_qp(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900486out_destroy_ib_cq:
487 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300488out_put_dev:
489 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900490 return ret;
491}
492
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300493static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900494 int idx, size_t queue_size)
495{
496 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200497 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900498 int ret;
499
500 queue = &ctrl->queues[idx];
501 queue->ctrl = ctrl;
502 init_completion(&queue->cm_done);
503
504 if (idx > 0)
505 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
506 else
507 queue->cmnd_capsule_len = sizeof(struct nvme_command);
508
509 queue->queue_size = queue_size;
510
511 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
512 RDMA_PS_TCP, IB_QPT_RC);
513 if (IS_ERR(queue->cm_id)) {
514 dev_info(ctrl->ctrl.device,
515 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
516 return PTR_ERR(queue->cm_id);
517 }
518
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200519 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200520 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200521
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200522 queue->cm_error = -ETIMEDOUT;
523 ret = rdma_resolve_addr(queue->cm_id, src_addr,
524 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900525 NVME_RDMA_CONNECT_TIMEOUT_MS);
526 if (ret) {
527 dev_info(ctrl->ctrl.device,
528 "rdma_resolve_addr failed (%d).\n", ret);
529 goto out_destroy_cm_id;
530 }
531
532 ret = nvme_rdma_wait_for_cm(queue);
533 if (ret) {
534 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300535 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900536 goto out_destroy_cm_id;
537 }
538
Sagi Grimberg5013e982017-10-11 15:29:12 +0300539 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900540
541 return 0;
542
543out_destroy_cm_id:
544 rdma_destroy_id(queue->cm_id);
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200545 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900546 return ret;
547}
548
549static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
550{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200551 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
552 return;
553
Christoph Hellwig71102302016-07-06 21:55:52 +0900554 rdma_disconnect(queue->cm_id);
555 ib_drain_qp(queue->qp);
556}
557
558static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
559{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300560 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900561 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200562
Sagi Grimbergbd9f0752017-10-19 16:00:30 +0300563 if (nvme_rdma_queue_idx(queue) == 0) {
564 nvme_rdma_free_qe(queue->device->dev,
565 &queue->ctrl->async_event_sqe,
566 sizeof(struct nvme_command), DMA_TO_DEVICE);
567 }
568
Sagi Grimberga57bd542017-08-28 21:41:10 +0200569 nvme_rdma_destroy_queue_ib(queue);
570 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900571}
572
573static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
574{
575 int i;
576
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300577 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200578 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900579}
580
Sagi Grimberga57bd542017-08-28 21:41:10 +0200581static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
582{
583 int i;
584
585 for (i = 1; i < ctrl->ctrl.queue_count; i++)
586 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900587}
588
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300589static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
590{
591 int ret;
592
593 if (idx)
594 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
595 else
596 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
597
598 if (!ret)
599 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
600 else
601 dev_info(ctrl->ctrl.device,
602 "failed to connect queue: %d ret=%d\n", idx, ret);
603 return ret;
604}
605
606static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900607{
608 int i, ret = 0;
609
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300610 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300611 ret = nvme_rdma_start_queue(ctrl, i);
612 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200613 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900614 }
615
Steve Wisec8dbc372016-11-08 09:16:02 -0800616 return 0;
617
Sagi Grimberga57bd542017-08-28 21:41:10 +0200618out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300619 for (i--; i >= 1; i--)
620 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900621 return ret;
622}
623
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300624static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900625{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200626 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300627 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200628 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900629 int i, ret;
630
Sagi Grimbergc248c642017-03-09 13:26:07 +0200631 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300632
633 /*
634 * we map queues according to the device irq vectors for
635 * optimal locality so we don't need more queues than
636 * completion vectors.
637 */
638 nr_io_queues = min_t(unsigned int, nr_io_queues,
639 ibdev->num_comp_vectors);
640
Sagi Grimbergc248c642017-03-09 13:26:07 +0200641 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
642 if (ret)
643 return ret;
644
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300645 ctrl->ctrl.queue_count = nr_io_queues + 1;
646 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200647 return 0;
648
649 dev_info(ctrl->ctrl.device,
650 "creating %d I/O queues.\n", nr_io_queues);
651
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300652 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300653 ret = nvme_rdma_alloc_queue(ctrl, i,
654 ctrl->ctrl.sqsize + 1);
655 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900656 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900657 }
658
659 return 0;
660
661out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700662 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200663 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900664
665 return ret;
666}
667
Sagi Grimberg60070c72017-10-11 15:29:06 +0300668static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
669 struct blk_mq_tag_set *set)
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300670{
671 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300672
673 blk_mq_free_tag_set(set);
674 nvme_rdma_dev_put(ctrl->device);
675}
676
677static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
678 bool admin)
679{
680 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
681 struct blk_mq_tag_set *set;
682 int ret;
683
684 if (admin) {
685 set = &ctrl->admin_tag_set;
686 memset(set, 0, sizeof(*set));
687 set->ops = &nvme_rdma_admin_mq_ops;
Keith Busch38dabe22017-11-07 15:13:10 -0700688 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300689 set->reserved_tags = 2; /* connect + keep-alive */
690 set->numa_node = NUMA_NO_NODE;
691 set->cmd_size = sizeof(struct nvme_rdma_request) +
692 SG_CHUNK_SIZE * sizeof(struct scatterlist);
693 set->driver_data = ctrl;
694 set->nr_hw_queues = 1;
695 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000696 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300697 } else {
698 set = &ctrl->tag_set;
699 memset(set, 0, sizeof(*set));
700 set->ops = &nvme_rdma_mq_ops;
701 set->queue_depth = nctrl->opts->queue_size;
702 set->reserved_tags = 1; /* fabric connect */
703 set->numa_node = NUMA_NO_NODE;
704 set->flags = BLK_MQ_F_SHOULD_MERGE;
705 set->cmd_size = sizeof(struct nvme_rdma_request) +
706 SG_CHUNK_SIZE * sizeof(struct scatterlist);
707 set->driver_data = ctrl;
708 set->nr_hw_queues = nctrl->queue_count - 1;
709 set->timeout = NVME_IO_TIMEOUT;
710 }
711
712 ret = blk_mq_alloc_tag_set(set);
713 if (ret)
714 goto out;
715
716 /*
717 * We need a reference on the device as long as the tag_set is alive,
718 * as the MRs in the request structures need a valid ib_device.
719 */
720 ret = nvme_rdma_dev_get(ctrl->device);
721 if (!ret) {
722 ret = -EINVAL;
723 goto out_free_tagset;
724 }
725
726 return set;
727
728out_free_tagset:
729 blk_mq_free_tag_set(set);
730out:
731 return ERR_PTR(ret);
732}
733
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300734static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
735 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900736{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200737 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300738 if (remove) {
739 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300740 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300741 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200742 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900743}
744
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300745static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
746 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300747{
748 int error;
749
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300750 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300751 if (error)
752 return error;
753
754 ctrl->device = ctrl->queues[0].device;
755
Israel Rukshinf41725b2017-11-26 10:40:55 +0000756 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300757
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300758 if (new) {
759 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300760 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
761 error = PTR_ERR(ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300762 goto out_free_queue;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300763 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300764
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300765 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
766 if (IS_ERR(ctrl->ctrl.admin_q)) {
767 error = PTR_ERR(ctrl->ctrl.admin_q);
768 goto out_free_tagset;
769 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300770 }
771
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300772 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300773 if (error)
774 goto out_cleanup_queue;
775
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300776 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300777 &ctrl->ctrl.cap);
778 if (error) {
779 dev_err(ctrl->ctrl.device,
780 "prop_get NVME_REG_CAP failed\n");
781 goto out_cleanup_queue;
782 }
783
784 ctrl->ctrl.sqsize =
785 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
786
787 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
788 if (error)
789 goto out_cleanup_queue;
790
791 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700792 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300793
794 error = nvme_init_identify(&ctrl->ctrl);
795 if (error)
796 goto out_cleanup_queue;
797
798 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
799 &ctrl->async_event_sqe, sizeof(struct nvme_command),
800 DMA_TO_DEVICE);
801 if (error)
802 goto out_cleanup_queue;
803
804 return 0;
805
806out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300807 if (new)
808 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300809out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300810 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300811 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300812out_free_queue:
813 nvme_rdma_free_queue(&ctrl->queues[0]);
814 return error;
815}
816
Sagi Grimberga57bd542017-08-28 21:41:10 +0200817static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
818 bool remove)
819{
820 nvme_rdma_stop_io_queues(ctrl);
821 if (remove) {
822 blk_cleanup_queue(ctrl->ctrl.connect_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300823 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200824 }
825 nvme_rdma_free_io_queues(ctrl);
826}
827
828static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
829{
830 int ret;
831
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300832 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200833 if (ret)
834 return ret;
835
836 if (new) {
837 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300838 if (IS_ERR(ctrl->ctrl.tagset)) {
839 ret = PTR_ERR(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200840 goto out_free_io_queues;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300841 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200842
843 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
844 if (IS_ERR(ctrl->ctrl.connect_q)) {
845 ret = PTR_ERR(ctrl->ctrl.connect_q);
846 goto out_free_tag_set;
847 }
848 } else {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200849 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
850 ctrl->ctrl.queue_count - 1);
851 }
852
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300853 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200854 if (ret)
855 goto out_cleanup_connect_q;
856
857 return 0;
858
859out_cleanup_connect_q:
860 if (new)
861 blk_cleanup_queue(ctrl->ctrl.connect_q);
862out_free_tag_set:
863 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300864 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200865out_free_io_queues:
866 nvme_rdma_free_io_queues(ctrl);
867 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900868}
869
Nitzan Carmib435ece2018-03-20 11:07:30 +0000870static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
871{
872 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
873
874 cancel_work_sync(&ctrl->err_work);
875 cancel_delayed_work_sync(&ctrl->reconnect_work);
876}
877
Christoph Hellwig71102302016-07-06 21:55:52 +0900878static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
879{
880 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
881
882 if (list_empty(&ctrl->list))
883 goto free_ctrl;
884
885 mutex_lock(&nvme_rdma_ctrl_mutex);
886 list_del(&ctrl->list);
887 mutex_unlock(&nvme_rdma_ctrl_mutex);
888
Christoph Hellwig71102302016-07-06 21:55:52 +0900889 kfree(ctrl->queues);
890 nvmf_free_options(nctrl->opts);
891free_ctrl:
892 kfree(ctrl);
893}
894
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200895static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
896{
897 /* If we are resetting/deleting then do nothing */
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200898 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200899 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
900 ctrl->ctrl.state == NVME_CTRL_LIVE);
901 return;
902 }
903
904 if (nvmf_should_reconnect(&ctrl->ctrl)) {
905 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
906 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200907 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200908 ctrl->ctrl.opts->reconnect_delay * HZ);
909 } else {
Sagi Grimberg12fa1302017-10-29 14:21:01 +0200910 nvme_delete_ctrl(&ctrl->ctrl);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200911 }
912}
913
Christoph Hellwig71102302016-07-06 21:55:52 +0900914static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
915{
916 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
917 struct nvme_rdma_ctrl, reconnect_work);
918 bool changed;
919 int ret;
920
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300921 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200922
Sagi Grimberg31fdf182017-08-28 21:40:06 +0200923 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900924 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300925 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900926
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300927 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200928 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900929 if (ret)
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300930 goto destroy_admin;
Christoph Hellwig71102302016-07-06 21:55:52 +0900931 }
932
933 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +0300934 if (!changed) {
935 /* state change failure is ok if we're in DELETING state */
936 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
937 return;
938 }
939
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300940 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900941
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300942 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
943 ctrl->ctrl.nr_reconnects);
944
945 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900946
947 return;
948
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300949destroy_admin:
950 nvme_rdma_destroy_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900951requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200952 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300953 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200954 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900955}
956
957static void nvme_rdma_error_recovery_work(struct work_struct *work)
958{
959 struct nvme_rdma_ctrl *ctrl = container_of(work,
960 struct nvme_rdma_ctrl, err_work);
961
Sagi Grimberge4d753d2017-09-21 17:01:38 +0300962 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700963
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300964 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900965 nvme_stop_queues(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900966 blk_mq_tagset_busy_iter(&ctrl->tag_set,
967 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300968 nvme_rdma_destroy_io_queues(ctrl, false);
969 }
970
971 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +0900972 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
973 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300974 nvme_rdma_destroy_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900975
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300976 /*
977 * queues are not a live anymore, so restart the queues to fail fast
978 * new IO
979 */
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300980 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300981 nvme_start_queues(&ctrl->ctrl);
982
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200983 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Nitzan Carmi187c0832018-03-20 11:07:29 +0000984 /* state change failure is ok if we're in DELETING state */
985 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +0200986 return;
987 }
988
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200989 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900990}
991
992static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
993{
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +0200994 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
Christoph Hellwig71102302016-07-06 21:55:52 +0900995 return;
996
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200997 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +0900998}
999
1000static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1001 const char *op)
1002{
1003 struct nvme_rdma_queue *queue = cq->cq_context;
1004 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1005
1006 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1007 dev_info(ctrl->ctrl.device,
1008 "%s for CQE 0x%p failed with status %s (%d)\n",
1009 op, wc->wr_cqe,
1010 ib_wc_status_msg(wc->status), wc->status);
1011 nvme_rdma_error_recovery(ctrl);
1012}
1013
1014static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1015{
1016 if (unlikely(wc->status != IB_WC_SUCCESS))
1017 nvme_rdma_wr_error(cq, wc, "MEMREG");
1018}
1019
1020static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1021{
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001022 struct nvme_rdma_request *req =
1023 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1024 struct request *rq = blk_mq_rq_from_pdu(req);
1025
1026 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001027 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001028 return;
1029 }
1030
1031 if (refcount_dec_and_test(&req->ref))
1032 nvme_end_request(rq, req->status, req->result);
1033
Christoph Hellwig71102302016-07-06 21:55:52 +09001034}
1035
1036static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1037 struct nvme_rdma_request *req)
1038{
1039 struct ib_send_wr *bad_wr;
1040 struct ib_send_wr wr = {
1041 .opcode = IB_WR_LOCAL_INV,
1042 .next = NULL,
1043 .num_sge = 0,
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001044 .send_flags = IB_SEND_SIGNALED,
Christoph Hellwig71102302016-07-06 21:55:52 +09001045 .ex.invalidate_rkey = req->mr->rkey,
1046 };
1047
1048 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1049 wr.wr_cqe = &req->reg_cqe;
1050
1051 return ib_post_send(queue->qp, &wr, &bad_wr);
1052}
1053
1054static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1055 struct request *rq)
1056{
1057 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001058 struct nvme_rdma_device *dev = queue->device;
1059 struct ib_device *ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +09001060
Christoph Hellwig0d309922018-02-22 07:24:09 -08001061 if (!blk_rq_payload_bytes(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001062 return;
1063
Israel Rukshinf41725b2017-11-26 10:40:55 +00001064 if (req->mr) {
1065 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1066 req->mr = NULL;
1067 }
1068
Christoph Hellwig71102302016-07-06 21:55:52 +09001069 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1070 req->nents, rq_data_dir(rq) ==
1071 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1072
1073 nvme_cleanup_cmd(rq);
1074 sg_free_table_chained(&req->sg_table, true);
1075}
1076
1077static int nvme_rdma_set_sg_null(struct nvme_command *c)
1078{
1079 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1080
1081 sg->addr = 0;
1082 put_unaligned_le24(0, sg->length);
1083 put_unaligned_le32(0, sg->key);
1084 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1085 return 0;
1086}
1087
1088static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1089 struct nvme_rdma_request *req, struct nvme_command *c)
1090{
1091 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1092
1093 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1094 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1095 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1096
1097 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1098 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1099 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1100
Christoph Hellwig71102302016-07-06 21:55:52 +09001101 req->num_sge++;
1102 return 0;
1103}
1104
1105static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1106 struct nvme_rdma_request *req, struct nvme_command *c)
1107{
1108 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1109
1110 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1111 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001112 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001113 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1114 return 0;
1115}
1116
1117static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1118 struct nvme_rdma_request *req, struct nvme_command *c,
1119 int count)
1120{
1121 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1122 int nr;
1123
Israel Rukshinf41725b2017-11-26 10:40:55 +00001124 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1125 if (WARN_ON_ONCE(!req->mr))
1126 return -EAGAIN;
1127
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001128 /*
1129 * Align the MR to a 4K page size to match the ctrl page size and
1130 * the block virtual boundary.
1131 */
1132 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001133 if (unlikely(nr < count)) {
Israel Rukshinf41725b2017-11-26 10:40:55 +00001134 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1135 req->mr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001136 if (nr < 0)
1137 return nr;
1138 return -EINVAL;
1139 }
1140
1141 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1142
1143 req->reg_cqe.done = nvme_rdma_memreg_done;
1144 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1145 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1146 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1147 req->reg_wr.wr.num_sge = 0;
1148 req->reg_wr.mr = req->mr;
1149 req->reg_wr.key = req->mr->rkey;
1150 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1151 IB_ACCESS_REMOTE_READ |
1152 IB_ACCESS_REMOTE_WRITE;
1153
Christoph Hellwig71102302016-07-06 21:55:52 +09001154 sg->addr = cpu_to_le64(req->mr->iova);
1155 put_unaligned_le24(req->mr->length, sg->length);
1156 put_unaligned_le32(req->mr->rkey, sg->key);
1157 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1158 NVME_SGL_FMT_INVALIDATE;
1159
1160 return 0;
1161}
1162
1163static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001164 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001165{
1166 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1167 struct nvme_rdma_device *dev = queue->device;
1168 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001169 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001170
1171 req->num_sge = 1;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001172 refcount_set(&req->ref, 2); /* send and recv completions */
Christoph Hellwig71102302016-07-06 21:55:52 +09001173
1174 c->common.flags |= NVME_CMD_SGL_METABUF;
1175
Christoph Hellwig0d309922018-02-22 07:24:09 -08001176 if (!blk_rq_payload_bytes(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001177 return nvme_rdma_set_sg_null(c);
1178
1179 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001180 ret = sg_alloc_table_chained(&req->sg_table,
1181 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001182 if (ret)
1183 return -ENOMEM;
1184
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001185 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001186
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001187 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001188 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1189 if (unlikely(count <= 0)) {
1190 sg_free_table_chained(&req->sg_table, true);
1191 return -EIO;
1192 }
1193
1194 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001195 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1196 blk_rq_payload_bytes(rq) <=
1197 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001198 return nvme_rdma_map_sg_inline(queue, req, c);
1199
Christoph Hellwig11975e02016-09-05 12:56:20 +02001200 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001201 return nvme_rdma_map_sg_single(queue, req, c);
1202 }
1203
1204 return nvme_rdma_map_sg_fr(queue, req, c, count);
1205}
1206
1207static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1208{
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001209 struct nvme_rdma_qe *qe =
1210 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1211 struct nvme_rdma_request *req =
1212 container_of(qe, struct nvme_rdma_request, sqe);
1213 struct request *rq = blk_mq_rq_from_pdu(req);
1214
1215 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001216 nvme_rdma_wr_error(cq, wc, "SEND");
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001217 return;
1218 }
1219
1220 if (refcount_dec_and_test(&req->ref))
1221 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001222}
1223
1224static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1225 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001226 struct ib_send_wr *first)
Christoph Hellwig71102302016-07-06 21:55:52 +09001227{
1228 struct ib_send_wr wr, *bad_wr;
1229 int ret;
1230
1231 sge->addr = qe->dma;
1232 sge->length = sizeof(struct nvme_command),
1233 sge->lkey = queue->device->pd->local_dma_lkey;
1234
Christoph Hellwig71102302016-07-06 21:55:52 +09001235 wr.next = NULL;
1236 wr.wr_cqe = &qe->cqe;
1237 wr.sg_list = sge;
1238 wr.num_sge = num_sge;
1239 wr.opcode = IB_WR_SEND;
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001240 wr.send_flags = IB_SEND_SIGNALED;
Christoph Hellwig71102302016-07-06 21:55:52 +09001241
1242 if (first)
1243 first->next = &wr;
1244 else
1245 first = &wr;
1246
1247 ret = ib_post_send(queue->qp, first, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001248 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001249 dev_err(queue->ctrl->ctrl.device,
1250 "%s failed with error code %d\n", __func__, ret);
1251 }
1252 return ret;
1253}
1254
1255static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1256 struct nvme_rdma_qe *qe)
1257{
1258 struct ib_recv_wr wr, *bad_wr;
1259 struct ib_sge list;
1260 int ret;
1261
1262 list.addr = qe->dma;
1263 list.length = sizeof(struct nvme_completion);
1264 list.lkey = queue->device->pd->local_dma_lkey;
1265
1266 qe->cqe.done = nvme_rdma_recv_done;
1267
1268 wr.next = NULL;
1269 wr.wr_cqe = &qe->cqe;
1270 wr.sg_list = &list;
1271 wr.num_sge = 1;
1272
1273 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001274 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001275 dev_err(queue->ctrl->ctrl.device,
1276 "%s failed with error code %d\n", __func__, ret);
1277 }
1278 return ret;
1279}
1280
1281static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1282{
1283 u32 queue_idx = nvme_rdma_queue_idx(queue);
1284
1285 if (queue_idx == 0)
1286 return queue->ctrl->admin_tag_set.tags[queue_idx];
1287 return queue->ctrl->tag_set.tags[queue_idx - 1];
1288}
1289
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001290static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1291{
1292 if (unlikely(wc->status != IB_WC_SUCCESS))
1293 nvme_rdma_wr_error(cq, wc, "ASYNC");
1294}
1295
Keith Buschad22c352017-11-07 15:13:12 -07001296static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
Christoph Hellwig71102302016-07-06 21:55:52 +09001297{
1298 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1299 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1300 struct ib_device *dev = queue->device->dev;
1301 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1302 struct nvme_command *cmd = sqe->data;
1303 struct ib_sge sge;
1304 int ret;
1305
Christoph Hellwig71102302016-07-06 21:55:52 +09001306 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1307
1308 memset(cmd, 0, sizeof(*cmd));
1309 cmd->common.opcode = nvme_admin_async_event;
Keith Busch38dabe22017-11-07 15:13:10 -07001310 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Christoph Hellwig71102302016-07-06 21:55:52 +09001311 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1312 nvme_rdma_set_sg_null(cmd);
1313
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001314 sqe->cqe.done = nvme_rdma_async_done;
1315
Christoph Hellwig71102302016-07-06 21:55:52 +09001316 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1317 DMA_TO_DEVICE);
1318
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001319 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001320 WARN_ON_ONCE(ret);
1321}
1322
1323static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1324 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1325{
Christoph Hellwig71102302016-07-06 21:55:52 +09001326 struct request *rq;
1327 struct nvme_rdma_request *req;
1328 int ret = 0;
1329
Christoph Hellwig71102302016-07-06 21:55:52 +09001330 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1331 if (!rq) {
1332 dev_err(queue->ctrl->ctrl.device,
1333 "tag 0x%x on QP %#x not found\n",
1334 cqe->command_id, queue->qp->qp_num);
1335 nvme_rdma_error_recovery(queue->ctrl);
1336 return ret;
1337 }
1338 req = blk_mq_rq_to_pdu(rq);
1339
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001340 req->status = cqe->status;
1341 req->result = cqe->result;
Christoph Hellwig71102302016-07-06 21:55:52 +09001342
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001343 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1344 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1345 dev_err(queue->ctrl->ctrl.device,
1346 "Bogus remote invalidation for rkey %#x\n",
1347 req->mr->rkey);
1348 nvme_rdma_error_recovery(queue->ctrl);
1349 }
Israel Rukshinf41725b2017-11-26 10:40:55 +00001350 } else if (req->mr) {
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001351 ret = nvme_rdma_inv_rkey(queue, req);
1352 if (unlikely(ret < 0)) {
1353 dev_err(queue->ctrl->ctrl.device,
1354 "Queueing INV WR for rkey %#x failed (%d)\n",
1355 req->mr->rkey, ret);
1356 nvme_rdma_error_recovery(queue->ctrl);
1357 }
1358 /* the local invalidation completion will end the request */
1359 return 0;
1360 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001361
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001362 if (refcount_dec_and_test(&req->ref)) {
1363 if (rq->tag == tag)
1364 ret = 1;
1365 nvme_end_request(rq, req->status, req->result);
1366 }
1367
Christoph Hellwig71102302016-07-06 21:55:52 +09001368 return ret;
1369}
1370
1371static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1372{
1373 struct nvme_rdma_qe *qe =
1374 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1375 struct nvme_rdma_queue *queue = cq->cq_context;
1376 struct ib_device *ibdev = queue->device->dev;
1377 struct nvme_completion *cqe = qe->data;
1378 const size_t len = sizeof(struct nvme_completion);
1379 int ret = 0;
1380
1381 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1382 nvme_rdma_wr_error(cq, wc, "RECV");
1383 return 0;
1384 }
1385
1386 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1387 /*
1388 * AEN requests are special as they don't time out and can
1389 * survive any kind of queue freeze and often don't respond to
1390 * aborts. We don't even bother to allocate a struct request
1391 * for them but rather special case them here.
1392 */
1393 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
Keith Busch38dabe22017-11-07 15:13:10 -07001394 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001395 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1396 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001397 else
1398 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1399 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1400
1401 nvme_rdma_post_recv(queue, qe);
1402 return ret;
1403}
1404
1405static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1406{
1407 __nvme_rdma_recv_done(cq, wc, -1);
1408}
1409
1410static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1411{
1412 int ret, i;
1413
1414 for (i = 0; i < queue->queue_size; i++) {
1415 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1416 if (ret)
1417 goto out_destroy_queue_ib;
1418 }
1419
1420 return 0;
1421
1422out_destroy_queue_ib:
1423 nvme_rdma_destroy_queue_ib(queue);
1424 return ret;
1425}
1426
1427static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1428 struct rdma_cm_event *ev)
1429{
Steve Wise7f039532016-10-26 12:36:47 -07001430 struct rdma_cm_id *cm_id = queue->cm_id;
1431 int status = ev->status;
1432 const char *rej_msg;
1433 const struct nvme_rdma_cm_rej *rej_data;
1434 u8 rej_data_len;
1435
1436 rej_msg = rdma_reject_msg(cm_id, status);
1437 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1438
1439 if (rej_data && rej_data_len >= sizeof(u16)) {
1440 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001441
1442 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001443 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1444 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001445 } else {
1446 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001447 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001448 }
1449
1450 return -ECONNRESET;
1451}
1452
1453static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1454{
Christoph Hellwig71102302016-07-06 21:55:52 +09001455 int ret;
1456
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001457 ret = nvme_rdma_create_queue_ib(queue);
1458 if (ret)
1459 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001460
1461 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1462 if (ret) {
1463 dev_err(queue->ctrl->ctrl.device,
1464 "rdma_resolve_route failed (%d).\n",
1465 queue->cm_error);
1466 goto out_destroy_queue;
1467 }
1468
1469 return 0;
1470
1471out_destroy_queue:
1472 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001473 return ret;
1474}
1475
1476static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1477{
1478 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1479 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001480 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001481 int ret;
1482
1483 param.qp_num = queue->qp->qp_num;
1484 param.flow_control = 1;
1485
1486 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001487 /* maximum retry count */
1488 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001489 param.rnr_retry_count = 7;
1490 param.private_data = &priv;
1491 param.private_data_len = sizeof(priv);
1492
1493 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1494 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001495 /*
1496 * set the admin queue depth to the minimum size
1497 * specified by the Fabrics standard.
1498 */
1499 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001500 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1501 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001502 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001503 /*
1504 * current interpretation of the fabrics spec
1505 * is at minimum you make hrqsize sqsize+1, or a
1506 * 1's based representation of sqsize.
1507 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001508 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001509 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001510 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001511
1512 ret = rdma_connect(queue->cm_id, &param);
1513 if (ret) {
1514 dev_err(ctrl->ctrl.device,
1515 "rdma_connect failed (%d).\n", ret);
1516 goto out_destroy_queue_ib;
1517 }
1518
1519 return 0;
1520
1521out_destroy_queue_ib:
1522 nvme_rdma_destroy_queue_ib(queue);
1523 return ret;
1524}
1525
Christoph Hellwig71102302016-07-06 21:55:52 +09001526static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1527 struct rdma_cm_event *ev)
1528{
1529 struct nvme_rdma_queue *queue = cm_id->context;
1530 int cm_error = 0;
1531
1532 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1533 rdma_event_msg(ev->event), ev->event,
1534 ev->status, cm_id);
1535
1536 switch (ev->event) {
1537 case RDMA_CM_EVENT_ADDR_RESOLVED:
1538 cm_error = nvme_rdma_addr_resolved(queue);
1539 break;
1540 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1541 cm_error = nvme_rdma_route_resolved(queue);
1542 break;
1543 case RDMA_CM_EVENT_ESTABLISHED:
1544 queue->cm_error = nvme_rdma_conn_established(queue);
1545 /* complete cm_done regardless of success/failure */
1546 complete(&queue->cm_done);
1547 return 0;
1548 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001549 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001550 cm_error = nvme_rdma_conn_rejected(queue, ev);
1551 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001552 case RDMA_CM_EVENT_ROUTE_ERROR:
1553 case RDMA_CM_EVENT_CONNECT_ERROR:
1554 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001555 nvme_rdma_destroy_queue_ib(queue);
1556 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001557 dev_dbg(queue->ctrl->ctrl.device,
1558 "CM error event %d\n", ev->event);
1559 cm_error = -ECONNRESET;
1560 break;
1561 case RDMA_CM_EVENT_DISCONNECTED:
1562 case RDMA_CM_EVENT_ADDR_CHANGE:
1563 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1564 dev_dbg(queue->ctrl->ctrl.device,
1565 "disconnect received - connection closed\n");
1566 nvme_rdma_error_recovery(queue->ctrl);
1567 break;
1568 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001569 /* device removal is handled via the ib_client API */
1570 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001571 default:
1572 dev_err(queue->ctrl->ctrl.device,
1573 "Unexpected RDMA CM event (%d)\n", ev->event);
1574 nvme_rdma_error_recovery(queue->ctrl);
1575 break;
1576 }
1577
1578 if (cm_error) {
1579 queue->cm_error = cm_error;
1580 complete(&queue->cm_done);
1581 }
1582
1583 return 0;
1584}
1585
1586static enum blk_eh_timer_return
1587nvme_rdma_timeout(struct request *rq, bool reserved)
1588{
1589 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1590
Nitzan Carmie62a5382017-10-22 09:37:04 +00001591 dev_warn(req->queue->ctrl->ctrl.device,
1592 "I/O %d QID %d timeout, reset controller\n",
1593 rq->tag, nvme_rdma_queue_idx(req->queue));
1594
Christoph Hellwig71102302016-07-06 21:55:52 +09001595 /* queue error recovery */
1596 nvme_rdma_error_recovery(req->queue->ctrl);
1597
1598 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001599 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001600
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001601 return BLK_EH_DONE;
Christoph Hellwig71102302016-07-06 21:55:52 +09001602}
1603
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001604static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001605 const struct blk_mq_queue_data *bd)
1606{
1607 struct nvme_ns *ns = hctx->queue->queuedata;
1608 struct nvme_rdma_queue *queue = hctx->driver_data;
1609 struct request *rq = bd->rq;
1610 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1611 struct nvme_rdma_qe *sqe = &req->sqe;
1612 struct nvme_command *c = sqe->data;
Christoph Hellwig71102302016-07-06 21:55:52 +09001613 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001614 blk_status_t ret;
1615 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001616
1617 WARN_ON_ONCE(rq->tag < 0);
1618
James Smartbb06ec312018-04-12 09:16:15 -06001619 ret = nvmf_check_if_ready(&queue->ctrl->ctrl, rq,
1620 test_bit(NVME_RDMA_Q_LIVE, &queue->flags), true);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001621 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001622 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001623
Christoph Hellwig71102302016-07-06 21:55:52 +09001624 dev = queue->device->dev;
1625 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1626 sizeof(struct nvme_command), DMA_TO_DEVICE);
1627
1628 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001629 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001630 return ret;
1631
Christoph Hellwig71102302016-07-06 21:55:52 +09001632 blk_mq_start_request(rq);
1633
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001634 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001635 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001636 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001637 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001638 nvme_cleanup_cmd(rq);
1639 goto err;
1640 }
1641
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001642 sqe->cqe.done = nvme_rdma_send_done;
1643
Christoph Hellwig71102302016-07-06 21:55:52 +09001644 ib_dma_sync_single_for_device(dev, sqe->dma,
1645 sizeof(struct nvme_command), DMA_TO_DEVICE);
1646
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001647 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Israel Rukshinf41725b2017-11-26 10:40:55 +00001648 req->mr ? &req->reg_wr.wr : NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001649 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001650 nvme_rdma_unmap_data(queue, rq);
1651 goto err;
1652 }
1653
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001654 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001655err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001656 if (err == -ENOMEM || err == -EAGAIN)
1657 return BLK_STS_RESOURCE;
1658 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001659}
1660
1661static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1662{
1663 struct nvme_rdma_queue *queue = hctx->driver_data;
1664 struct ib_cq *cq = queue->ib_cq;
1665 struct ib_wc wc;
1666 int found = 0;
1667
Christoph Hellwig71102302016-07-06 21:55:52 +09001668 while (ib_poll_cq(cq, 1, &wc) > 0) {
1669 struct ib_cqe *cqe = wc.wr_cqe;
1670
1671 if (cqe) {
1672 if (cqe->done == nvme_rdma_recv_done)
1673 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1674 else
1675 cqe->done(cq, &wc);
1676 }
1677 }
1678
1679 return found;
1680}
1681
1682static void nvme_rdma_complete_rq(struct request *rq)
1683{
1684 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001685
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001686 nvme_rdma_unmap_data(req->queue, rq);
1687 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001688}
1689
Sagi Grimberg0b366582017-07-13 11:09:44 +03001690static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1691{
1692 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1693
1694 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1695}
1696
Eric Biggersf363b082017-03-30 13:39:16 -07001697static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001698 .queue_rq = nvme_rdma_queue_rq,
1699 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001700 .init_request = nvme_rdma_init_request,
1701 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001702 .init_hctx = nvme_rdma_init_hctx,
1703 .poll = nvme_rdma_poll,
1704 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001705 .map_queues = nvme_rdma_map_queues,
Christoph Hellwig71102302016-07-06 21:55:52 +09001706};
1707
Eric Biggersf363b082017-03-30 13:39:16 -07001708static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001709 .queue_rq = nvme_rdma_queue_rq,
1710 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001711 .init_request = nvme_rdma_init_request,
1712 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001713 .init_hctx = nvme_rdma_init_admin_hctx,
1714 .timeout = nvme_rdma_timeout,
1715};
1716
Sagi Grimberg18398af2017-07-10 09:22:31 +03001717static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001718{
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001719 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001720 nvme_stop_queues(&ctrl->ctrl);
1721 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1722 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +02001723 nvme_rdma_destroy_io_queues(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001724 }
1725
Sagi Grimberg18398af2017-07-10 09:22:31 +03001726 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001727 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001728 else
1729 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001730
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001731 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001732 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1733 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001734 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001735 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001736}
1737
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001738static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001739{
Christoph Hellwige9bc2582017-10-29 10:44:30 +02001740 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001741}
1742
Christoph Hellwig71102302016-07-06 21:55:52 +09001743static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1744{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001745 struct nvme_rdma_ctrl *ctrl =
1746 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001747 int ret;
1748 bool changed;
1749
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001750 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001751 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001752
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001753 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001754 /* state change failure should never happen */
1755 WARN_ON_ONCE(1);
1756 return;
1757 }
1758
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001759 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001760 if (ret)
1761 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001762
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001763 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001764 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001765 if (ret)
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001766 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001767 }
1768
1769 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0ad0bfa2017-10-11 12:49:51 +03001770 if (!changed) {
1771 /* state change failure is ok if we're in DELETING state */
1772 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1773 return;
1774 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001775
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001776 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001777
1778 return;
1779
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001780out_fail:
Nitzan Carmi8000d1f2018-01-17 11:01:14 +00001781 ++ctrl->ctrl.nr_reconnects;
1782 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001783}
1784
Christoph Hellwig71102302016-07-06 21:55:52 +09001785static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1786 .name = "rdma",
1787 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001788 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001789 .reg_read32 = nvmf_reg_read32,
1790 .reg_read64 = nvmf_reg_read64,
1791 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001792 .free_ctrl = nvme_rdma_free_ctrl,
1793 .submit_async_event = nvme_rdma_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001794 .delete_ctrl = nvme_rdma_delete_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001795 .get_address = nvmf_get_address,
Nitzan Carmib435ece2018-03-20 11:07:30 +00001796 .stop_ctrl = nvme_rdma_stop_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001797};
1798
James Smart36e835f2017-10-20 16:17:09 -07001799static inline bool
1800__nvme_rdma_options_match(struct nvme_rdma_ctrl *ctrl,
1801 struct nvmf_ctrl_options *opts)
1802{
1803 char *stdport = __stringify(NVME_RDMA_IP_PORT);
1804
1805
1806 if (!nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts) ||
1807 strcmp(opts->traddr, ctrl->ctrl.opts->traddr))
1808 return false;
1809
1810 if (opts->mask & NVMF_OPT_TRSVCID &&
1811 ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1812 if (strcmp(opts->trsvcid, ctrl->ctrl.opts->trsvcid))
1813 return false;
1814 } else if (opts->mask & NVMF_OPT_TRSVCID) {
1815 if (strcmp(opts->trsvcid, stdport))
1816 return false;
1817 } else if (ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1818 if (strcmp(stdport, ctrl->ctrl.opts->trsvcid))
1819 return false;
1820 }
1821 /* else, it's a match as both have stdport. Fall to next checks */
1822
1823 /*
1824 * checking the local address is rough. In most cases, one
1825 * is not specified and the host port is selected by the stack.
1826 *
1827 * Assume no match if:
1828 * local address is specified and address is not the same
1829 * local address is not specified but remote is, or vice versa
1830 * (admin using specific host_traddr when it matters).
1831 */
1832 if (opts->mask & NVMF_OPT_HOST_TRADDR &&
1833 ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
1834 if (strcmp(opts->host_traddr, ctrl->ctrl.opts->host_traddr))
1835 return false;
1836 } else if (opts->mask & NVMF_OPT_HOST_TRADDR ||
1837 ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
1838 return false;
1839 /*
1840 * if neither controller had an host port specified, assume it's
1841 * a match as everything else matched.
1842 */
1843
1844 return true;
1845}
1846
1847/*
1848 * Fails a connection request if it matches an existing controller
1849 * (association) with the same tuple:
1850 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1851 *
1852 * if local address is not specified in the request, it will match an
1853 * existing controller with all the other parameters the same and no
1854 * local port address specified as well.
1855 *
1856 * The ports don't need to be compared as they are intrinsically
1857 * already matched by the port pointers supplied.
1858 */
1859static bool
1860nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1861{
1862 struct nvme_rdma_ctrl *ctrl;
1863 bool found = false;
1864
1865 mutex_lock(&nvme_rdma_ctrl_mutex);
1866 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1867 found = __nvme_rdma_options_match(ctrl, opts);
1868 if (found)
1869 break;
1870 }
1871 mutex_unlock(&nvme_rdma_ctrl_mutex);
1872
1873 return found;
1874}
1875
Christoph Hellwig71102302016-07-06 21:55:52 +09001876static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1877 struct nvmf_ctrl_options *opts)
1878{
1879 struct nvme_rdma_ctrl *ctrl;
1880 int ret;
1881 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001882 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001883
1884 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1885 if (!ctrl)
1886 return ERR_PTR(-ENOMEM);
1887 ctrl->ctrl.opts = opts;
1888 INIT_LIST_HEAD(&ctrl->list);
1889
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001890 if (opts->mask & NVMF_OPT_TRSVCID)
1891 port = opts->trsvcid;
1892 else
1893 port = __stringify(NVME_RDMA_IP_PORT);
1894
1895 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1896 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001897 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001898 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001899 goto out_free_ctrl;
1900 }
1901
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001902 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001903 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1904 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001905 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001906 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001907 opts->host_traddr);
1908 goto out_free_ctrl;
1909 }
1910 }
1911
James Smart36e835f2017-10-20 16:17:09 -07001912 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1913 ret = -EALREADY;
1914 goto out_free_ctrl;
1915 }
1916
Christoph Hellwig71102302016-07-06 21:55:52 +09001917 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1918 0 /* no quirks, we're perfect! */);
1919 if (ret)
1920 goto out_free_ctrl;
1921
Christoph Hellwig71102302016-07-06 21:55:52 +09001922 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1923 nvme_rdma_reconnect_ctrl_work);
1924 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001925 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001926
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001927 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001928 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001929 ctrl->ctrl.kato = opts->kato;
1930
1931 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001932 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001933 GFP_KERNEL);
1934 if (!ctrl->queues)
1935 goto out_uninit_ctrl;
1936
Max Gurtovoyb754a322018-01-31 18:31:25 +02001937 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
1938 WARN_ON_ONCE(!changed);
1939
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001940 ret = nvme_rdma_configure_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001941 if (ret)
1942 goto out_kfree_queues;
1943
1944 /* sanity check icdoff */
1945 if (ctrl->ctrl.icdoff) {
1946 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001947 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001948 goto out_remove_admin_queue;
1949 }
1950
1951 /* sanity check keyed sgls */
1952 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1953 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001954 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001955 goto out_remove_admin_queue;
1956 }
1957
1958 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1959 /* warn if maxcmd is lower than queue_size */
1960 dev_warn(ctrl->ctrl.device,
1961 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1962 opts->queue_size, ctrl->ctrl.maxcmd);
1963 opts->queue_size = ctrl->ctrl.maxcmd;
1964 }
1965
Samuel Jones76c08bf2016-10-25 09:22:34 +02001966 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1967 /* warn if sqsize is lower than queue_size */
1968 dev_warn(ctrl->ctrl.device,
1969 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1970 opts->queue_size, ctrl->ctrl.sqsize + 1);
1971 opts->queue_size = ctrl->ctrl.sqsize + 1;
1972 }
1973
Christoph Hellwig71102302016-07-06 21:55:52 +09001974 if (opts->nr_io_queues) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001975 ret = nvme_rdma_configure_io_queues(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001976 if (ret)
1977 goto out_remove_admin_queue;
1978 }
1979
1980 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1981 WARN_ON_ONCE(!changed);
1982
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001983 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001984 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1985
Christoph Hellwigd22524a2017-10-18 13:25:42 +02001986 nvme_get_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001987
1988 mutex_lock(&nvme_rdma_ctrl_mutex);
1989 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1990 mutex_unlock(&nvme_rdma_ctrl_mutex);
1991
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001992 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001993
1994 return &ctrl->ctrl;
1995
1996out_remove_admin_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001997 nvme_rdma_destroy_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001998out_kfree_queues:
1999 kfree(ctrl->queues);
2000out_uninit_ctrl:
2001 nvme_uninit_ctrl(&ctrl->ctrl);
2002 nvme_put_ctrl(&ctrl->ctrl);
2003 if (ret > 0)
2004 ret = -EIO;
2005 return ERR_PTR(ret);
2006out_free_ctrl:
2007 kfree(ctrl);
2008 return ERR_PTR(ret);
2009}
2010
2011static struct nvmf_transport_ops nvme_rdma_transport = {
2012 .name = "rdma",
Roy Shterman0de5cd32017-12-25 14:18:30 +02002013 .module = THIS_MODULE,
Christoph Hellwig71102302016-07-06 21:55:52 +09002014 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002015 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02002016 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09002017 .create_ctrl = nvme_rdma_create_ctrl,
2018};
2019
Steve Wisee87a9112016-09-02 09:01:54 -07002020static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2021{
2022 struct nvme_rdma_ctrl *ctrl;
Max Gurtovoy9bad0402018-02-28 13:12:39 +02002023 struct nvme_rdma_device *ndev;
2024 bool found = false;
2025
2026 mutex_lock(&device_list_mutex);
2027 list_for_each_entry(ndev, &device_list, entry) {
2028 if (ndev->dev == ib_device) {
2029 found = true;
2030 break;
2031 }
2032 }
2033 mutex_unlock(&device_list_mutex);
2034
2035 if (!found)
2036 return;
Steve Wisee87a9112016-09-02 09:01:54 -07002037
2038 /* Delete all controllers using this device */
2039 mutex_lock(&nvme_rdma_ctrl_mutex);
2040 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2041 if (ctrl->device->dev != ib_device)
2042 continue;
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002043 nvme_delete_ctrl(&ctrl->ctrl);
Steve Wisee87a9112016-09-02 09:01:54 -07002044 }
2045 mutex_unlock(&nvme_rdma_ctrl_mutex);
2046
Roy Shtermanb227c592018-01-14 12:39:02 +02002047 flush_workqueue(nvme_delete_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002048}
2049
2050static struct ib_client nvme_rdma_ib_client = {
2051 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002052 .remove = nvme_rdma_remove_one
2053};
2054
Christoph Hellwig71102302016-07-06 21:55:52 +09002055static int __init nvme_rdma_init_module(void)
2056{
Steve Wisee87a9112016-09-02 09:01:54 -07002057 int ret;
2058
Steve Wisee87a9112016-09-02 09:01:54 -07002059 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002060 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002061 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002062
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002063 ret = nvmf_register_transport(&nvme_rdma_transport);
2064 if (ret)
2065 goto err_unreg_client;
2066
2067 return 0;
2068
2069err_unreg_client:
2070 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002071 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002072}
2073
2074static void __exit nvme_rdma_cleanup_module(void)
2075{
Christoph Hellwig71102302016-07-06 21:55:52 +09002076 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002077 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002078}
2079
2080module_init(nvme_rdma_init_module);
2081module_exit(nvme_rdma_cleanup_module);
2082
2083MODULE_LICENSE("GPL v2");