blob: 32e21ab1ae5205315b61661909a04f7dd9af0baf [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>
18#include <linux/err.h>
19#include <linux/string.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090020#include <linux/atomic.h>
21#include <linux/blk-mq.h>
Sagi Grimberg0b366582017-07-13 11:09:44 +030022#include <linux/blk-mq-rdma.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090023#include <linux/types.h>
24#include <linux/list.h>
25#include <linux/mutex.h>
26#include <linux/scatterlist.h>
27#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090028#include <asm/unaligned.h>
29
30#include <rdma/ib_verbs.h>
31#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090032#include <linux/nvme-rdma.h>
33
34#include "nvme.h"
35#include "fabrics.h"
36
37
Sagi Grimberg782d8202017-03-21 16:32:38 +020038#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090039
Christoph Hellwig71102302016-07-06 21:55:52 +090040#define NVME_RDMA_MAX_SEGMENTS 256
41
42#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
43
Christoph Hellwig71102302016-07-06 21:55:52 +090044/*
45 * We handle AEN commands ourselves and don't even let the
46 * block layer know about them.
47 */
48#define NVME_RDMA_NR_AEN_COMMANDS 1
49#define NVME_RDMA_AQ_BLKMQ_DEPTH \
Sagi Grimberg7aa1f422017-06-18 16:15:59 +030050 (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
Christoph Hellwig71102302016-07-06 21:55:52 +090051
52struct nvme_rdma_device {
Max Gurtovoyf87c89a2017-10-23 12:59:27 +030053 struct ib_device *dev;
54 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090055 struct kref ref;
56 struct list_head entry;
57};
58
59struct nvme_rdma_qe {
60 struct ib_cqe cqe;
61 void *data;
62 u64 dma;
63};
64
65struct nvme_rdma_queue;
66struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080067 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090068 struct ib_mr *mr;
69 struct nvme_rdma_qe sqe;
70 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
71 u32 num_sge;
72 int nents;
73 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090074 struct ib_reg_wr reg_wr;
75 struct ib_cqe reg_cqe;
76 struct nvme_rdma_queue *queue;
77 struct sg_table sg_table;
78 struct scatterlist first_sgl[];
79};
80
81enum nvme_rdma_queue_flags {
Sagi Grimberg5013e982017-10-11 15:29:12 +030082 NVME_RDMA_Q_ALLOCATED = 0,
83 NVME_RDMA_Q_LIVE = 1,
Christoph Hellwig71102302016-07-06 21:55:52 +090084};
85
86struct nvme_rdma_queue {
87 struct nvme_rdma_qe *rsp_ring;
Marta Rybczynska5e599d72017-06-06 13:27:21 +020088 atomic_t sig_count;
Christoph Hellwig71102302016-07-06 21:55:52 +090089 int queue_size;
90 size_t cmnd_capsule_len;
91 struct nvme_rdma_ctrl *ctrl;
92 struct nvme_rdma_device *device;
93 struct ib_cq *ib_cq;
94 struct ib_qp *qp;
95
96 unsigned long flags;
97 struct rdma_cm_id *cm_id;
98 int cm_error;
99 struct completion cm_done;
100};
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;
125};
126
127static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
128{
129 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
130}
131
132static LIST_HEAD(device_list);
133static DEFINE_MUTEX(device_list_mutex);
134
135static LIST_HEAD(nvme_rdma_ctrl_list);
136static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
137
Christoph Hellwig71102302016-07-06 21:55:52 +0900138/*
139 * Disabling this option makes small I/O goes faster, but is fundamentally
140 * unsafe. With it turned off we will have to register a global rkey that
141 * allows read and write access to all physical memory.
142 */
143static bool register_always = true;
144module_param(register_always, bool, 0444);
145MODULE_PARM_DESC(register_always,
146 "Use memory registration even for contiguous memory regions");
147
148static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
149 struct rdma_cm_event *event);
150static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900151
Sagi Grimberg90af3512017-07-10 09:22:28 +0300152static const struct blk_mq_ops nvme_rdma_mq_ops;
153static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
154
Christoph Hellwig71102302016-07-06 21:55:52 +0900155/* XXX: really should move to a generic header sooner or later.. */
156static inline void put_unaligned_le24(u32 val, u8 *p)
157{
158 *p++ = val;
159 *p++ = val >> 8;
160 *p++ = val >> 16;
161}
162
163static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
164{
165 return queue - queue->ctrl->queues;
166}
167
168static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
169{
170 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
171}
172
173static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
174 size_t capsule_size, enum dma_data_direction dir)
175{
176 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
177 kfree(qe->data);
178}
179
180static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
181 size_t capsule_size, enum dma_data_direction dir)
182{
183 qe->data = kzalloc(capsule_size, GFP_KERNEL);
184 if (!qe->data)
185 return -ENOMEM;
186
187 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
188 if (ib_dma_mapping_error(ibdev, qe->dma)) {
189 kfree(qe->data);
190 return -ENOMEM;
191 }
192
193 return 0;
194}
195
196static void nvme_rdma_free_ring(struct ib_device *ibdev,
197 struct nvme_rdma_qe *ring, size_t ib_queue_size,
198 size_t capsule_size, enum dma_data_direction dir)
199{
200 int i;
201
202 for (i = 0; i < ib_queue_size; i++)
203 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
204 kfree(ring);
205}
206
207static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
208 size_t ib_queue_size, size_t capsule_size,
209 enum dma_data_direction dir)
210{
211 struct nvme_rdma_qe *ring;
212 int i;
213
214 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
215 if (!ring)
216 return NULL;
217
218 for (i = 0; i < ib_queue_size; i++) {
219 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
220 goto out_free_ring;
221 }
222
223 return ring;
224
225out_free_ring:
226 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
227 return NULL;
228}
229
230static void nvme_rdma_qp_event(struct ib_event *event, void *context)
231{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200232 pr_debug("QP event %s (%d)\n",
233 ib_event_msg(event->event), event->event);
234
Christoph Hellwig71102302016-07-06 21:55:52 +0900235}
236
237static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
238{
239 wait_for_completion_interruptible_timeout(&queue->cm_done,
240 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
241 return queue->cm_error;
242}
243
244static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
245{
246 struct nvme_rdma_device *dev = queue->device;
247 struct ib_qp_init_attr init_attr;
248 int ret;
249
250 memset(&init_attr, 0, sizeof(init_attr));
251 init_attr.event_handler = nvme_rdma_qp_event;
252 /* +1 for drain */
253 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
254 /* +1 for drain */
255 init_attr.cap.max_recv_wr = queue->queue_size + 1;
256 init_attr.cap.max_recv_sge = 1;
257 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
258 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
259 init_attr.qp_type = IB_QPT_RC;
260 init_attr.send_cq = queue->ib_cq;
261 init_attr.recv_cq = queue->ib_cq;
262
263 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
264
265 queue->qp = queue->cm_id->qp;
266 return ret;
267}
268
269static int nvme_rdma_reinit_request(void *data, struct request *rq)
270{
271 struct nvme_rdma_ctrl *ctrl = data;
272 struct nvme_rdma_device *dev = ctrl->device;
273 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
274 int ret = 0;
275
Sagi Grimberg0fc176d2017-10-11 15:29:09 +0300276 if (WARN_ON_ONCE(!req->mr))
277 return 0;
278
Christoph Hellwig71102302016-07-06 21:55:52 +0900279 ib_dereg_mr(req->mr);
280
281 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
282 ctrl->max_fr_pages);
283 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900284 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000285 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100286 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900287 }
288
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300289 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900290
291out:
292 return ret;
293}
294
Christoph Hellwig385475e2017-06-13 09:15:19 +0200295static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
296 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900297{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200298 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900299 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200300 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900301 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
302 struct nvme_rdma_device *dev = queue->device;
303
304 if (req->mr)
305 ib_dereg_mr(req->mr);
306
307 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
308 DMA_TO_DEVICE);
309}
310
Christoph Hellwig385475e2017-06-13 09:15:19 +0200311static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
312 struct request *rq, unsigned int hctx_idx,
313 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900314{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200315 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900316 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200317 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900318 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
319 struct nvme_rdma_device *dev = queue->device;
320 struct ib_device *ibdev = dev->dev;
321 int ret;
322
Christoph Hellwig71102302016-07-06 21:55:52 +0900323 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
324 DMA_TO_DEVICE);
325 if (ret)
326 return ret;
327
328 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
329 ctrl->max_fr_pages);
330 if (IS_ERR(req->mr)) {
331 ret = PTR_ERR(req->mr);
332 goto out_free_qe;
333 }
334
335 req->queue = queue;
336
337 return 0;
338
339out_free_qe:
340 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
341 DMA_TO_DEVICE);
342 return -ENOMEM;
343}
344
Christoph Hellwig71102302016-07-06 21:55:52 +0900345static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
346 unsigned int hctx_idx)
347{
348 struct nvme_rdma_ctrl *ctrl = data;
349 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
350
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300351 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900352
353 hctx->driver_data = queue;
354 return 0;
355}
356
357static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
358 unsigned int hctx_idx)
359{
360 struct nvme_rdma_ctrl *ctrl = data;
361 struct nvme_rdma_queue *queue = &ctrl->queues[0];
362
363 BUG_ON(hctx_idx != 0);
364
365 hctx->driver_data = queue;
366 return 0;
367}
368
369static void nvme_rdma_free_dev(struct kref *ref)
370{
371 struct nvme_rdma_device *ndev =
372 container_of(ref, struct nvme_rdma_device, ref);
373
374 mutex_lock(&device_list_mutex);
375 list_del(&ndev->entry);
376 mutex_unlock(&device_list_mutex);
377
Christoph Hellwig71102302016-07-06 21:55:52 +0900378 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900379 kfree(ndev);
380}
381
382static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
383{
384 kref_put(&dev->ref, nvme_rdma_free_dev);
385}
386
387static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
388{
389 return kref_get_unless_zero(&dev->ref);
390}
391
392static struct nvme_rdma_device *
393nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
394{
395 struct nvme_rdma_device *ndev;
396
397 mutex_lock(&device_list_mutex);
398 list_for_each_entry(ndev, &device_list, entry) {
399 if (ndev->dev->node_guid == cm_id->device->node_guid &&
400 nvme_rdma_dev_get(ndev))
401 goto out_unlock;
402 }
403
404 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
405 if (!ndev)
406 goto out_err;
407
408 ndev->dev = cm_id->device;
409 kref_init(&ndev->ref);
410
Christoph Hellwig11975e02016-09-05 12:56:20 +0200411 ndev->pd = ib_alloc_pd(ndev->dev,
412 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900413 if (IS_ERR(ndev->pd))
414 goto out_free_dev;
415
Christoph Hellwig71102302016-07-06 21:55:52 +0900416 if (!(ndev->dev->attrs.device_cap_flags &
417 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
418 dev_err(&ndev->dev->dev,
419 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200420 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900421 }
422
423 list_add(&ndev->entry, &device_list);
424out_unlock:
425 mutex_unlock(&device_list_mutex);
426 return ndev;
427
Christoph Hellwig71102302016-07-06 21:55:52 +0900428out_free_pd:
429 ib_dealloc_pd(ndev->pd);
430out_free_dev:
431 kfree(ndev);
432out_err:
433 mutex_unlock(&device_list_mutex);
434 return NULL;
435}
436
437static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
438{
Sagi Grimberg0c5b43b2017-10-11 15:29:08 +0300439 struct nvme_rdma_device *dev = queue->device;
440 struct ib_device *ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900441
442 rdma_destroy_qp(queue->cm_id);
443 ib_free_cq(queue->ib_cq);
444
445 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
446 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
447
448 nvme_rdma_dev_put(dev);
449}
450
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300451static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900452{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300453 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900454 const int send_wr_factor = 3; /* MR, SEND, INV */
455 const int cq_factor = send_wr_factor + 1; /* + RECV */
456 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900457 int ret;
458
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300459 queue->device = nvme_rdma_find_get_device(queue->cm_id);
460 if (!queue->device) {
461 dev_err(queue->cm_id->device->dev.parent,
462 "no client data found!\n");
463 return -ECONNREFUSED;
464 }
465 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900466
467 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300468 * Spread I/O queues completion vectors according their queue index.
469 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900470 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300471 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900472
473 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300474 queue->ib_cq = ib_alloc_cq(ibdev, queue,
475 cq_factor * queue->queue_size + 1,
476 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900477 if (IS_ERR(queue->ib_cq)) {
478 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300479 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900480 }
481
482 ret = nvme_rdma_create_qp(queue, send_wr_factor);
483 if (ret)
484 goto out_destroy_ib_cq;
485
486 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
487 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
488 if (!queue->rsp_ring) {
489 ret = -ENOMEM;
490 goto out_destroy_qp;
491 }
492
493 return 0;
494
495out_destroy_qp:
Max Gurtovoy1f61def2017-11-06 16:18:51 +0200496 rdma_destroy_qp(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900497out_destroy_ib_cq:
498 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300499out_put_dev:
500 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900501 return ret;
502}
503
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300504static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900505 int idx, size_t queue_size)
506{
507 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200508 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900509 int ret;
510
511 queue = &ctrl->queues[idx];
512 queue->ctrl = ctrl;
513 init_completion(&queue->cm_done);
514
515 if (idx > 0)
516 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
517 else
518 queue->cmnd_capsule_len = sizeof(struct nvme_command);
519
520 queue->queue_size = queue_size;
Marta Rybczynska5e599d72017-06-06 13:27:21 +0200521 atomic_set(&queue->sig_count, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900522
523 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
524 RDMA_PS_TCP, IB_QPT_RC);
525 if (IS_ERR(queue->cm_id)) {
526 dev_info(ctrl->ctrl.device,
527 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
528 return PTR_ERR(queue->cm_id);
529 }
530
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200531 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200532 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200533
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200534 queue->cm_error = -ETIMEDOUT;
535 ret = rdma_resolve_addr(queue->cm_id, src_addr,
536 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900537 NVME_RDMA_CONNECT_TIMEOUT_MS);
538 if (ret) {
539 dev_info(ctrl->ctrl.device,
540 "rdma_resolve_addr failed (%d).\n", ret);
541 goto out_destroy_cm_id;
542 }
543
544 ret = nvme_rdma_wait_for_cm(queue);
545 if (ret) {
546 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300547 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900548 goto out_destroy_cm_id;
549 }
550
Sagi Grimberg5013e982017-10-11 15:29:12 +0300551 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900552
553 return 0;
554
555out_destroy_cm_id:
556 rdma_destroy_id(queue->cm_id);
557 return ret;
558}
559
560static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
561{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200562 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
563 return;
564
Christoph Hellwig71102302016-07-06 21:55:52 +0900565 rdma_disconnect(queue->cm_id);
566 ib_drain_qp(queue->qp);
567}
568
569static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
570{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300571 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900572 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200573
574 nvme_rdma_destroy_queue_ib(queue);
575 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900576}
577
578static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
579{
580 int i;
581
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300582 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200583 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900584}
585
Sagi Grimberga57bd542017-08-28 21:41:10 +0200586static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
587{
588 int i;
589
590 for (i = 1; i < ctrl->ctrl.queue_count; i++)
591 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900592}
593
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300594static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
595{
596 int ret;
597
598 if (idx)
599 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
600 else
601 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
602
603 if (!ret)
604 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
605 else
606 dev_info(ctrl->ctrl.device,
607 "failed to connect queue: %d ret=%d\n", idx, ret);
608 return ret;
609}
610
611static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900612{
613 int i, ret = 0;
614
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300615 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300616 ret = nvme_rdma_start_queue(ctrl, i);
617 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200618 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900619 }
620
Steve Wisec8dbc372016-11-08 09:16:02 -0800621 return 0;
622
Sagi Grimberga57bd542017-08-28 21:41:10 +0200623out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300624 for (i--; i >= 1; i--)
625 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900626 return ret;
627}
628
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300629static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900630{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200631 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300632 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200633 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900634 int i, ret;
635
Sagi Grimbergc248c642017-03-09 13:26:07 +0200636 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300637
638 /*
639 * we map queues according to the device irq vectors for
640 * optimal locality so we don't need more queues than
641 * completion vectors.
642 */
643 nr_io_queues = min_t(unsigned int, nr_io_queues,
644 ibdev->num_comp_vectors);
645
Sagi Grimbergc248c642017-03-09 13:26:07 +0200646 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
647 if (ret)
648 return ret;
649
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300650 ctrl->ctrl.queue_count = nr_io_queues + 1;
651 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200652 return 0;
653
654 dev_info(ctrl->ctrl.device,
655 "creating %d I/O queues.\n", nr_io_queues);
656
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300657 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300658 ret = nvme_rdma_alloc_queue(ctrl, i,
659 ctrl->ctrl.sqsize + 1);
660 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900661 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900662 }
663
664 return 0;
665
666out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700667 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200668 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900669
670 return ret;
671}
672
Sagi Grimberg60070c72017-10-11 15:29:06 +0300673static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
674 struct blk_mq_tag_set *set)
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300675{
676 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300677
678 blk_mq_free_tag_set(set);
679 nvme_rdma_dev_put(ctrl->device);
680}
681
682static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
683 bool admin)
684{
685 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
686 struct blk_mq_tag_set *set;
687 int ret;
688
689 if (admin) {
690 set = &ctrl->admin_tag_set;
691 memset(set, 0, sizeof(*set));
692 set->ops = &nvme_rdma_admin_mq_ops;
693 set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
694 set->reserved_tags = 2; /* connect + keep-alive */
695 set->numa_node = NUMA_NO_NODE;
696 set->cmd_size = sizeof(struct nvme_rdma_request) +
697 SG_CHUNK_SIZE * sizeof(struct scatterlist);
698 set->driver_data = ctrl;
699 set->nr_hw_queues = 1;
700 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000701 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300702 } else {
703 set = &ctrl->tag_set;
704 memset(set, 0, sizeof(*set));
705 set->ops = &nvme_rdma_mq_ops;
706 set->queue_depth = nctrl->opts->queue_size;
707 set->reserved_tags = 1; /* fabric connect */
708 set->numa_node = NUMA_NO_NODE;
709 set->flags = BLK_MQ_F_SHOULD_MERGE;
710 set->cmd_size = sizeof(struct nvme_rdma_request) +
711 SG_CHUNK_SIZE * sizeof(struct scatterlist);
712 set->driver_data = ctrl;
713 set->nr_hw_queues = nctrl->queue_count - 1;
714 set->timeout = NVME_IO_TIMEOUT;
715 }
716
717 ret = blk_mq_alloc_tag_set(set);
718 if (ret)
719 goto out;
720
721 /*
722 * We need a reference on the device as long as the tag_set is alive,
723 * as the MRs in the request structures need a valid ib_device.
724 */
725 ret = nvme_rdma_dev_get(ctrl->device);
726 if (!ret) {
727 ret = -EINVAL;
728 goto out_free_tagset;
729 }
730
731 return set;
732
733out_free_tagset:
734 blk_mq_free_tag_set(set);
735out:
736 return ERR_PTR(ret);
737}
738
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300739static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
740 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900741{
742 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
743 sizeof(struct nvme_command), DMA_TO_DEVICE);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200744 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300745 if (remove) {
746 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300747 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300748 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200749 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900750}
751
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300752static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
753 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300754{
755 int error;
756
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300757 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300758 if (error)
759 return error;
760
761 ctrl->device = ctrl->queues[0].device;
762
Sagi Grimberg90af3512017-07-10 09:22:28 +0300763 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
764 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
765
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300766 if (new) {
767 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
768 if (IS_ERR(ctrl->ctrl.admin_tagset))
769 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300770
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300771 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
772 if (IS_ERR(ctrl->ctrl.admin_q)) {
773 error = PTR_ERR(ctrl->ctrl.admin_q);
774 goto out_free_tagset;
775 }
776 } else {
Sagi Grimberg31b84462017-10-11 12:53:07 +0300777 error = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300778 if (error)
779 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300780 }
781
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300782 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300783 if (error)
784 goto out_cleanup_queue;
785
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300786 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300787 &ctrl->ctrl.cap);
788 if (error) {
789 dev_err(ctrl->ctrl.device,
790 "prop_get NVME_REG_CAP failed\n");
791 goto out_cleanup_queue;
792 }
793
794 ctrl->ctrl.sqsize =
795 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
796
797 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
798 if (error)
799 goto out_cleanup_queue;
800
801 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700802 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300803
804 error = nvme_init_identify(&ctrl->ctrl);
805 if (error)
806 goto out_cleanup_queue;
807
808 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
809 &ctrl->async_event_sqe, sizeof(struct nvme_command),
810 DMA_TO_DEVICE);
811 if (error)
812 goto out_cleanup_queue;
813
814 return 0;
815
816out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300817 if (new)
818 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300819out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300820 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300821 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300822out_free_queue:
823 nvme_rdma_free_queue(&ctrl->queues[0]);
824 return error;
825}
826
Sagi Grimberga57bd542017-08-28 21:41:10 +0200827static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
828 bool remove)
829{
830 nvme_rdma_stop_io_queues(ctrl);
831 if (remove) {
832 blk_cleanup_queue(ctrl->ctrl.connect_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300833 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200834 }
835 nvme_rdma_free_io_queues(ctrl);
836}
837
838static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
839{
840 int ret;
841
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300842 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200843 if (ret)
844 return ret;
845
846 if (new) {
847 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
848 if (IS_ERR(ctrl->ctrl.tagset))
849 goto out_free_io_queues;
850
851 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
852 if (IS_ERR(ctrl->ctrl.connect_q)) {
853 ret = PTR_ERR(ctrl->ctrl.connect_q);
854 goto out_free_tag_set;
855 }
856 } else {
Sagi Grimberg31b84462017-10-11 12:53:07 +0300857 ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200858 if (ret)
859 goto out_free_io_queues;
860
861 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
862 ctrl->ctrl.queue_count - 1);
863 }
864
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300865 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200866 if (ret)
867 goto out_cleanup_connect_q;
868
869 return 0;
870
871out_cleanup_connect_q:
872 if (new)
873 blk_cleanup_queue(ctrl->ctrl.connect_q);
874out_free_tag_set:
875 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300876 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200877out_free_io_queues:
878 nvme_rdma_free_io_queues(ctrl);
879 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900880}
881
882static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
883{
884 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
885
886 if (list_empty(&ctrl->list))
887 goto free_ctrl;
888
889 mutex_lock(&nvme_rdma_ctrl_mutex);
890 list_del(&ctrl->list);
891 mutex_unlock(&nvme_rdma_ctrl_mutex);
892
Christoph Hellwig71102302016-07-06 21:55:52 +0900893 kfree(ctrl->queues);
894 nvmf_free_options(nctrl->opts);
895free_ctrl:
896 kfree(ctrl);
897}
898
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200899static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
900{
901 /* If we are resetting/deleting then do nothing */
902 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
903 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
904 ctrl->ctrl.state == NVME_CTRL_LIVE);
905 return;
906 }
907
908 if (nvmf_should_reconnect(&ctrl->ctrl)) {
909 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
910 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200911 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200912 ctrl->ctrl.opts->reconnect_delay * HZ);
913 } else {
914 dev_info(ctrl->ctrl.device, "Removing controller...\n");
Sagi Grimberg12fa1302017-10-29 14:21:01 +0200915 nvme_delete_ctrl(&ctrl->ctrl);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200916 }
917}
918
Christoph Hellwig71102302016-07-06 21:55:52 +0900919static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
920{
921 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
922 struct nvme_rdma_ctrl, reconnect_work);
923 bool changed;
924 int ret;
925
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300926 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200927
Sagi Grimberg31fdf182017-08-28 21:40:06 +0200928 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900929 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300930 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900931
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300932 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200933 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900934 if (ret)
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300935 goto destroy_admin;
Christoph Hellwig71102302016-07-06 21:55:52 +0900936 }
937
938 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +0300939 if (!changed) {
940 /* state change failure is ok if we're in DELETING state */
941 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
942 return;
943 }
944
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300945 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900946
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300947 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
948 ctrl->ctrl.nr_reconnects);
949
950 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900951
952 return;
953
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300954destroy_admin:
955 nvme_rdma_destroy_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900956requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200957 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300958 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200959 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900960}
961
962static void nvme_rdma_error_recovery_work(struct work_struct *work)
963{
964 struct nvme_rdma_ctrl *ctrl = container_of(work,
965 struct nvme_rdma_ctrl, err_work);
966
Sagi Grimberge4d753d2017-09-21 17:01:38 +0300967 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700968
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300969 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900970 nvme_stop_queues(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900971 blk_mq_tagset_busy_iter(&ctrl->tag_set,
972 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300973 nvme_rdma_destroy_io_queues(ctrl, false);
974 }
975
976 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +0900977 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
978 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300979 nvme_rdma_destroy_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900980
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300981 /*
982 * queues are not a live anymore, so restart the queues to fail fast
983 * new IO
984 */
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300985 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300986 nvme_start_queues(&ctrl->ctrl);
987
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200988 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900989}
990
991static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
992{
993 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
994 return;
995
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200996 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +0900997}
998
999static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1000 const char *op)
1001{
1002 struct nvme_rdma_queue *queue = cq->cq_context;
1003 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1004
1005 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1006 dev_info(ctrl->ctrl.device,
1007 "%s for CQE 0x%p failed with status %s (%d)\n",
1008 op, wc->wr_cqe,
1009 ib_wc_status_msg(wc->status), wc->status);
1010 nvme_rdma_error_recovery(ctrl);
1011}
1012
1013static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1014{
1015 if (unlikely(wc->status != IB_WC_SUCCESS))
1016 nvme_rdma_wr_error(cq, wc, "MEMREG");
1017}
1018
1019static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1020{
1021 if (unlikely(wc->status != IB_WC_SUCCESS))
1022 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1023}
1024
1025static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1026 struct nvme_rdma_request *req)
1027{
1028 struct ib_send_wr *bad_wr;
1029 struct ib_send_wr wr = {
1030 .opcode = IB_WR_LOCAL_INV,
1031 .next = NULL,
1032 .num_sge = 0,
1033 .send_flags = 0,
1034 .ex.invalidate_rkey = req->mr->rkey,
1035 };
1036
1037 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1038 wr.wr_cqe = &req->reg_cqe;
1039
1040 return ib_post_send(queue->qp, &wr, &bad_wr);
1041}
1042
1043static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1044 struct request *rq)
1045{
1046 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1047 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1048 struct nvme_rdma_device *dev = queue->device;
1049 struct ib_device *ibdev = dev->dev;
1050 int res;
1051
1052 if (!blk_rq_bytes(rq))
1053 return;
1054
Sagi Grimberg60a51882017-10-11 15:29:10 +03001055 if (req->mr->need_inval && test_bit(NVME_RDMA_Q_LIVE, &req->queue->flags)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001056 res = nvme_rdma_inv_rkey(queue, req);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001057 if (unlikely(res < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001058 dev_err(ctrl->ctrl.device,
1059 "Queueing INV WR for rkey %#x failed (%d)\n",
1060 req->mr->rkey, res);
1061 nvme_rdma_error_recovery(queue->ctrl);
1062 }
1063 }
1064
1065 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1066 req->nents, rq_data_dir(rq) ==
1067 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1068
1069 nvme_cleanup_cmd(rq);
1070 sg_free_table_chained(&req->sg_table, true);
1071}
1072
1073static int nvme_rdma_set_sg_null(struct nvme_command *c)
1074{
1075 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1076
1077 sg->addr = 0;
1078 put_unaligned_le24(0, sg->length);
1079 put_unaligned_le32(0, sg->key);
1080 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1081 return 0;
1082}
1083
1084static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1085 struct nvme_rdma_request *req, struct nvme_command *c)
1086{
1087 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1088
1089 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1090 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1091 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1092
1093 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1094 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1095 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1096
1097 req->inline_data = true;
1098 req->num_sge++;
1099 return 0;
1100}
1101
1102static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1103 struct nvme_rdma_request *req, struct nvme_command *c)
1104{
1105 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1106
1107 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1108 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001109 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001110 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1111 return 0;
1112}
1113
1114static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1115 struct nvme_rdma_request *req, struct nvme_command *c,
1116 int count)
1117{
1118 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1119 int nr;
1120
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001121 /*
1122 * Align the MR to a 4K page size to match the ctrl page size and
1123 * the block virtual boundary.
1124 */
1125 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001126 if (unlikely(nr < count)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001127 if (nr < 0)
1128 return nr;
1129 return -EINVAL;
1130 }
1131
1132 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1133
1134 req->reg_cqe.done = nvme_rdma_memreg_done;
1135 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1136 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1137 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1138 req->reg_wr.wr.num_sge = 0;
1139 req->reg_wr.mr = req->mr;
1140 req->reg_wr.key = req->mr->rkey;
1141 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1142 IB_ACCESS_REMOTE_READ |
1143 IB_ACCESS_REMOTE_WRITE;
1144
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001145 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001146
1147 sg->addr = cpu_to_le64(req->mr->iova);
1148 put_unaligned_le24(req->mr->length, sg->length);
1149 put_unaligned_le32(req->mr->rkey, sg->key);
1150 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1151 NVME_SGL_FMT_INVALIDATE;
1152
1153 return 0;
1154}
1155
1156static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001157 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001158{
1159 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1160 struct nvme_rdma_device *dev = queue->device;
1161 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001162 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001163
1164 req->num_sge = 1;
1165 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001166 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001167
1168 c->common.flags |= NVME_CMD_SGL_METABUF;
1169
1170 if (!blk_rq_bytes(rq))
1171 return nvme_rdma_set_sg_null(c);
1172
1173 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001174 ret = sg_alloc_table_chained(&req->sg_table,
1175 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001176 if (ret)
1177 return -ENOMEM;
1178
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001179 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001180
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001181 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001182 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1183 if (unlikely(count <= 0)) {
1184 sg_free_table_chained(&req->sg_table, true);
1185 return -EIO;
1186 }
1187
1188 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001189 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1190 blk_rq_payload_bytes(rq) <=
1191 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001192 return nvme_rdma_map_sg_inline(queue, req, c);
1193
Christoph Hellwig11975e02016-09-05 12:56:20 +02001194 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001195 return nvme_rdma_map_sg_single(queue, req, c);
1196 }
1197
1198 return nvme_rdma_map_sg_fr(queue, req, c, count);
1199}
1200
1201static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1202{
1203 if (unlikely(wc->status != IB_WC_SUCCESS))
1204 nvme_rdma_wr_error(cq, wc, "SEND");
1205}
1206
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001207/*
1208 * We want to signal completion at least every queue depth/2. This returns the
1209 * largest power of two that is not above half of (queue size + 1) to optimize
1210 * (avoid divisions).
1211 */
1212static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
Marta Rybczynska0544f542017-04-10 17:12:34 +02001213{
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001214 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
Marta Rybczynska0544f542017-04-10 17:12:34 +02001215
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001216 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
Marta Rybczynska0544f542017-04-10 17:12:34 +02001217}
1218
Christoph Hellwig71102302016-07-06 21:55:52 +09001219static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1220 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1221 struct ib_send_wr *first, bool flush)
1222{
1223 struct ib_send_wr wr, *bad_wr;
1224 int ret;
1225
1226 sge->addr = qe->dma;
1227 sge->length = sizeof(struct nvme_command),
1228 sge->lkey = queue->device->pd->local_dma_lkey;
1229
1230 qe->cqe.done = nvme_rdma_send_done;
1231
1232 wr.next = NULL;
1233 wr.wr_cqe = &qe->cqe;
1234 wr.sg_list = sge;
1235 wr.num_sge = num_sge;
1236 wr.opcode = IB_WR_SEND;
1237 wr.send_flags = 0;
1238
1239 /*
1240 * Unsignalled send completions are another giant desaster in the
1241 * IB Verbs spec: If we don't regularly post signalled sends
1242 * the send queue will fill up and only a QP reset will rescue us.
1243 * Would have been way to obvious to handle this in hardware or
1244 * at least the RDMA stack..
1245 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001246 * Always signal the flushes. The magic request used for the flush
1247 * sequencer is not allocated in our driver's tagset and it's
1248 * triggered to be freed by blk_cleanup_queue(). So we need to
1249 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001250 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001251 * calls wr_cqe->done().
1252 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001253 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001254 wr.send_flags |= IB_SEND_SIGNALED;
1255
1256 if (first)
1257 first->next = &wr;
1258 else
1259 first = &wr;
1260
1261 ret = ib_post_send(queue->qp, first, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001262 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001263 dev_err(queue->ctrl->ctrl.device,
1264 "%s failed with error code %d\n", __func__, ret);
1265 }
1266 return ret;
1267}
1268
1269static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1270 struct nvme_rdma_qe *qe)
1271{
1272 struct ib_recv_wr wr, *bad_wr;
1273 struct ib_sge list;
1274 int ret;
1275
1276 list.addr = qe->dma;
1277 list.length = sizeof(struct nvme_completion);
1278 list.lkey = queue->device->pd->local_dma_lkey;
1279
1280 qe->cqe.done = nvme_rdma_recv_done;
1281
1282 wr.next = NULL;
1283 wr.wr_cqe = &qe->cqe;
1284 wr.sg_list = &list;
1285 wr.num_sge = 1;
1286
1287 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001288 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001289 dev_err(queue->ctrl->ctrl.device,
1290 "%s failed with error code %d\n", __func__, ret);
1291 }
1292 return ret;
1293}
1294
1295static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1296{
1297 u32 queue_idx = nvme_rdma_queue_idx(queue);
1298
1299 if (queue_idx == 0)
1300 return queue->ctrl->admin_tag_set.tags[queue_idx];
1301 return queue->ctrl->tag_set.tags[queue_idx - 1];
1302}
1303
1304static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1305{
1306 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1307 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1308 struct ib_device *dev = queue->device->dev;
1309 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1310 struct nvme_command *cmd = sqe->data;
1311 struct ib_sge sge;
1312 int ret;
1313
1314 if (WARN_ON_ONCE(aer_idx != 0))
1315 return;
1316
1317 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1318
1319 memset(cmd, 0, sizeof(*cmd));
1320 cmd->common.opcode = nvme_admin_async_event;
1321 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1322 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1323 nvme_rdma_set_sg_null(cmd);
1324
1325 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1326 DMA_TO_DEVICE);
1327
1328 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1329 WARN_ON_ONCE(ret);
1330}
1331
1332static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1333 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1334{
Christoph Hellwig71102302016-07-06 21:55:52 +09001335 struct request *rq;
1336 struct nvme_rdma_request *req;
1337 int ret = 0;
1338
Christoph Hellwig71102302016-07-06 21:55:52 +09001339 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1340 if (!rq) {
1341 dev_err(queue->ctrl->ctrl.device,
1342 "tag 0x%x on QP %#x not found\n",
1343 cqe->command_id, queue->qp->qp_num);
1344 nvme_rdma_error_recovery(queue->ctrl);
1345 return ret;
1346 }
1347 req = blk_mq_rq_to_pdu(rq);
1348
Christoph Hellwig71102302016-07-06 21:55:52 +09001349 if (rq->tag == tag)
1350 ret = 1;
1351
1352 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1353 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001354 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001355
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001356 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001357 return ret;
1358}
1359
1360static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1361{
1362 struct nvme_rdma_qe *qe =
1363 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1364 struct nvme_rdma_queue *queue = cq->cq_context;
1365 struct ib_device *ibdev = queue->device->dev;
1366 struct nvme_completion *cqe = qe->data;
1367 const size_t len = sizeof(struct nvme_completion);
1368 int ret = 0;
1369
1370 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1371 nvme_rdma_wr_error(cq, wc, "RECV");
1372 return 0;
1373 }
1374
1375 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1376 /*
1377 * AEN requests are special as they don't time out and can
1378 * survive any kind of queue freeze and often don't respond to
1379 * aborts. We don't even bother to allocate a struct request
1380 * for them but rather special case them here.
1381 */
1382 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1383 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001384 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1385 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001386 else
1387 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1388 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1389
1390 nvme_rdma_post_recv(queue, qe);
1391 return ret;
1392}
1393
1394static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1395{
1396 __nvme_rdma_recv_done(cq, wc, -1);
1397}
1398
1399static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1400{
1401 int ret, i;
1402
1403 for (i = 0; i < queue->queue_size; i++) {
1404 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1405 if (ret)
1406 goto out_destroy_queue_ib;
1407 }
1408
1409 return 0;
1410
1411out_destroy_queue_ib:
1412 nvme_rdma_destroy_queue_ib(queue);
1413 return ret;
1414}
1415
1416static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1417 struct rdma_cm_event *ev)
1418{
Steve Wise7f039532016-10-26 12:36:47 -07001419 struct rdma_cm_id *cm_id = queue->cm_id;
1420 int status = ev->status;
1421 const char *rej_msg;
1422 const struct nvme_rdma_cm_rej *rej_data;
1423 u8 rej_data_len;
1424
1425 rej_msg = rdma_reject_msg(cm_id, status);
1426 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1427
1428 if (rej_data && rej_data_len >= sizeof(u16)) {
1429 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001430
1431 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001432 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1433 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001434 } else {
1435 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001436 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001437 }
1438
1439 return -ECONNRESET;
1440}
1441
1442static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1443{
Christoph Hellwig71102302016-07-06 21:55:52 +09001444 int ret;
1445
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001446 ret = nvme_rdma_create_queue_ib(queue);
1447 if (ret)
1448 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001449
1450 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1451 if (ret) {
1452 dev_err(queue->ctrl->ctrl.device,
1453 "rdma_resolve_route failed (%d).\n",
1454 queue->cm_error);
1455 goto out_destroy_queue;
1456 }
1457
1458 return 0;
1459
1460out_destroy_queue:
1461 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001462 return ret;
1463}
1464
1465static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1466{
1467 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1468 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001469 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001470 int ret;
1471
1472 param.qp_num = queue->qp->qp_num;
1473 param.flow_control = 1;
1474
1475 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001476 /* maximum retry count */
1477 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001478 param.rnr_retry_count = 7;
1479 param.private_data = &priv;
1480 param.private_data_len = sizeof(priv);
1481
1482 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1483 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001484 /*
1485 * set the admin queue depth to the minimum size
1486 * specified by the Fabrics standard.
1487 */
1488 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001489 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1490 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001491 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001492 /*
1493 * current interpretation of the fabrics spec
1494 * is at minimum you make hrqsize sqsize+1, or a
1495 * 1's based representation of sqsize.
1496 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001497 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001498 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001499 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001500
1501 ret = rdma_connect(queue->cm_id, &param);
1502 if (ret) {
1503 dev_err(ctrl->ctrl.device,
1504 "rdma_connect failed (%d).\n", ret);
1505 goto out_destroy_queue_ib;
1506 }
1507
1508 return 0;
1509
1510out_destroy_queue_ib:
1511 nvme_rdma_destroy_queue_ib(queue);
1512 return ret;
1513}
1514
Christoph Hellwig71102302016-07-06 21:55:52 +09001515static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1516 struct rdma_cm_event *ev)
1517{
1518 struct nvme_rdma_queue *queue = cm_id->context;
1519 int cm_error = 0;
1520
1521 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1522 rdma_event_msg(ev->event), ev->event,
1523 ev->status, cm_id);
1524
1525 switch (ev->event) {
1526 case RDMA_CM_EVENT_ADDR_RESOLVED:
1527 cm_error = nvme_rdma_addr_resolved(queue);
1528 break;
1529 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1530 cm_error = nvme_rdma_route_resolved(queue);
1531 break;
1532 case RDMA_CM_EVENT_ESTABLISHED:
1533 queue->cm_error = nvme_rdma_conn_established(queue);
1534 /* complete cm_done regardless of success/failure */
1535 complete(&queue->cm_done);
1536 return 0;
1537 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001538 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001539 cm_error = nvme_rdma_conn_rejected(queue, ev);
1540 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001541 case RDMA_CM_EVENT_ROUTE_ERROR:
1542 case RDMA_CM_EVENT_CONNECT_ERROR:
1543 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001544 nvme_rdma_destroy_queue_ib(queue);
1545 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001546 dev_dbg(queue->ctrl->ctrl.device,
1547 "CM error event %d\n", ev->event);
1548 cm_error = -ECONNRESET;
1549 break;
1550 case RDMA_CM_EVENT_DISCONNECTED:
1551 case RDMA_CM_EVENT_ADDR_CHANGE:
1552 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1553 dev_dbg(queue->ctrl->ctrl.device,
1554 "disconnect received - connection closed\n");
1555 nvme_rdma_error_recovery(queue->ctrl);
1556 break;
1557 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001558 /* device removal is handled via the ib_client API */
1559 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001560 default:
1561 dev_err(queue->ctrl->ctrl.device,
1562 "Unexpected RDMA CM event (%d)\n", ev->event);
1563 nvme_rdma_error_recovery(queue->ctrl);
1564 break;
1565 }
1566
1567 if (cm_error) {
1568 queue->cm_error = cm_error;
1569 complete(&queue->cm_done);
1570 }
1571
1572 return 0;
1573}
1574
1575static enum blk_eh_timer_return
1576nvme_rdma_timeout(struct request *rq, bool reserved)
1577{
1578 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1579
Nitzan Carmie62a5382017-10-22 09:37:04 +00001580 dev_warn(req->queue->ctrl->ctrl.device,
1581 "I/O %d QID %d timeout, reset controller\n",
1582 rq->tag, nvme_rdma_queue_idx(req->queue));
1583
Christoph Hellwig71102302016-07-06 21:55:52 +09001584 /* queue error recovery */
1585 nvme_rdma_error_recovery(req->queue->ctrl);
1586
1587 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001588 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001589
1590 return BLK_EH_HANDLED;
1591}
1592
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001593/*
1594 * We cannot accept any other command until the Connect command has completed.
1595 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001596static inline blk_status_t
1597nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001598{
1599 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001600 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001601
Christoph Hellwig57292b52017-01-31 16:57:29 +01001602 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001603 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001604 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1605 /*
1606 * reconnecting state means transport disruption, which
1607 * can take a long time and even might fail permanently,
1608 * so we can't let incoming I/O be requeued forever.
1609 * fail it fast to allow upper layers a chance to
1610 * failover.
1611 */
1612 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001613 return BLK_STS_IOERR;
1614 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001615 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001616 }
1617
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001618 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001619}
1620
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001621static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001622 const struct blk_mq_queue_data *bd)
1623{
1624 struct nvme_ns *ns = hctx->queue->queuedata;
1625 struct nvme_rdma_queue *queue = hctx->driver_data;
1626 struct request *rq = bd->rq;
1627 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1628 struct nvme_rdma_qe *sqe = &req->sqe;
1629 struct nvme_command *c = sqe->data;
1630 bool flush = false;
1631 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001632 blk_status_t ret;
1633 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001634
1635 WARN_ON_ONCE(rq->tag < 0);
1636
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001637 ret = nvme_rdma_queue_is_ready(queue, rq);
1638 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001639 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001640
Christoph Hellwig71102302016-07-06 21:55:52 +09001641 dev = queue->device->dev;
1642 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1643 sizeof(struct nvme_command), DMA_TO_DEVICE);
1644
1645 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001646 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001647 return ret;
1648
Christoph Hellwig71102302016-07-06 21:55:52 +09001649 blk_mq_start_request(rq);
1650
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001651 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001652 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001653 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001654 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001655 nvme_cleanup_cmd(rq);
1656 goto err;
1657 }
1658
1659 ib_dma_sync_single_for_device(dev, sqe->dma,
1660 sizeof(struct nvme_command), DMA_TO_DEVICE);
1661
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001662 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001663 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001664 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001665 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001666 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001667 nvme_rdma_unmap_data(queue, rq);
1668 goto err;
1669 }
1670
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001671 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001672err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001673 if (err == -ENOMEM || err == -EAGAIN)
1674 return BLK_STS_RESOURCE;
1675 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001676}
1677
1678static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1679{
1680 struct nvme_rdma_queue *queue = hctx->driver_data;
1681 struct ib_cq *cq = queue->ib_cq;
1682 struct ib_wc wc;
1683 int found = 0;
1684
Christoph Hellwig71102302016-07-06 21:55:52 +09001685 while (ib_poll_cq(cq, 1, &wc) > 0) {
1686 struct ib_cqe *cqe = wc.wr_cqe;
1687
1688 if (cqe) {
1689 if (cqe->done == nvme_rdma_recv_done)
1690 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1691 else
1692 cqe->done(cq, &wc);
1693 }
1694 }
1695
1696 return found;
1697}
1698
1699static void nvme_rdma_complete_rq(struct request *rq)
1700{
1701 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001702
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001703 nvme_rdma_unmap_data(req->queue, rq);
1704 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001705}
1706
Sagi Grimberg0b366582017-07-13 11:09:44 +03001707static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1708{
1709 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1710
1711 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1712}
1713
Eric Biggersf363b082017-03-30 13:39:16 -07001714static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001715 .queue_rq = nvme_rdma_queue_rq,
1716 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001717 .init_request = nvme_rdma_init_request,
1718 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001719 .init_hctx = nvme_rdma_init_hctx,
1720 .poll = nvme_rdma_poll,
1721 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001722 .map_queues = nvme_rdma_map_queues,
Christoph Hellwig71102302016-07-06 21:55:52 +09001723};
1724
Eric Biggersf363b082017-03-30 13:39:16 -07001725static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001726 .queue_rq = nvme_rdma_queue_rq,
1727 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001728 .init_request = nvme_rdma_init_request,
1729 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001730 .init_hctx = nvme_rdma_init_admin_hctx,
1731 .timeout = nvme_rdma_timeout,
1732};
1733
Sagi Grimberg18398af2017-07-10 09:22:31 +03001734static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001735{
Christoph Hellwig71102302016-07-06 21:55:52 +09001736 cancel_work_sync(&ctrl->err_work);
1737 cancel_delayed_work_sync(&ctrl->reconnect_work);
1738
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001739 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001740 nvme_stop_queues(&ctrl->ctrl);
1741 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1742 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +02001743 nvme_rdma_destroy_io_queues(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001744 }
1745
Sagi Grimberg18398af2017-07-10 09:22:31 +03001746 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001747 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001748 else
1749 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001750
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001751 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001752 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1753 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001754 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001755 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001756}
1757
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001758static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +09001759{
Christoph Hellwige9bc2582017-10-29 10:44:30 +02001760 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001761}
1762
Christoph Hellwig71102302016-07-06 21:55:52 +09001763static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1764{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001765 struct nvme_rdma_ctrl *ctrl =
1766 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001767 int ret;
1768 bool changed;
1769
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001770 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001771 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001772
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001773 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001774 if (ret)
1775 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001776
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001777 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001778 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001779 if (ret)
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001780 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001781 }
1782
1783 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0ad0bfa2017-10-11 12:49:51 +03001784 if (!changed) {
1785 /* state change failure is ok if we're in DELETING state */
1786 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1787 return;
1788 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001789
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001790 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001791
1792 return;
1793
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001794out_fail:
Christoph Hellwig71102302016-07-06 21:55:52 +09001795 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
Christoph Hellwige9bc2582017-10-29 10:44:30 +02001796 nvme_remove_namespaces(&ctrl->ctrl);
1797 nvme_rdma_shutdown_ctrl(ctrl, true);
1798 nvme_uninit_ctrl(&ctrl->ctrl);
1799 nvme_put_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001800}
1801
Christoph Hellwig71102302016-07-06 21:55:52 +09001802static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1803 .name = "rdma",
1804 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001805 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001806 .reg_read32 = nvmf_reg_read32,
1807 .reg_read64 = nvmf_reg_read64,
1808 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001809 .free_ctrl = nvme_rdma_free_ctrl,
1810 .submit_async_event = nvme_rdma_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001811 .delete_ctrl = nvme_rdma_delete_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001812 .get_address = nvmf_get_address,
Sagi Grimberg31b84462017-10-11 12:53:07 +03001813 .reinit_request = nvme_rdma_reinit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001814};
1815
James Smart36e835f2017-10-20 16:17:09 -07001816static inline bool
1817__nvme_rdma_options_match(struct nvme_rdma_ctrl *ctrl,
1818 struct nvmf_ctrl_options *opts)
1819{
1820 char *stdport = __stringify(NVME_RDMA_IP_PORT);
1821
1822
1823 if (!nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts) ||
1824 strcmp(opts->traddr, ctrl->ctrl.opts->traddr))
1825 return false;
1826
1827 if (opts->mask & NVMF_OPT_TRSVCID &&
1828 ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1829 if (strcmp(opts->trsvcid, ctrl->ctrl.opts->trsvcid))
1830 return false;
1831 } else if (opts->mask & NVMF_OPT_TRSVCID) {
1832 if (strcmp(opts->trsvcid, stdport))
1833 return false;
1834 } else if (ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1835 if (strcmp(stdport, ctrl->ctrl.opts->trsvcid))
1836 return false;
1837 }
1838 /* else, it's a match as both have stdport. Fall to next checks */
1839
1840 /*
1841 * checking the local address is rough. In most cases, one
1842 * is not specified and the host port is selected by the stack.
1843 *
1844 * Assume no match if:
1845 * local address is specified and address is not the same
1846 * local address is not specified but remote is, or vice versa
1847 * (admin using specific host_traddr when it matters).
1848 */
1849 if (opts->mask & NVMF_OPT_HOST_TRADDR &&
1850 ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
1851 if (strcmp(opts->host_traddr, ctrl->ctrl.opts->host_traddr))
1852 return false;
1853 } else if (opts->mask & NVMF_OPT_HOST_TRADDR ||
1854 ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
1855 return false;
1856 /*
1857 * if neither controller had an host port specified, assume it's
1858 * a match as everything else matched.
1859 */
1860
1861 return true;
1862}
1863
1864/*
1865 * Fails a connection request if it matches an existing controller
1866 * (association) with the same tuple:
1867 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1868 *
1869 * if local address is not specified in the request, it will match an
1870 * existing controller with all the other parameters the same and no
1871 * local port address specified as well.
1872 *
1873 * The ports don't need to be compared as they are intrinsically
1874 * already matched by the port pointers supplied.
1875 */
1876static bool
1877nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1878{
1879 struct nvme_rdma_ctrl *ctrl;
1880 bool found = false;
1881
1882 mutex_lock(&nvme_rdma_ctrl_mutex);
1883 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1884 found = __nvme_rdma_options_match(ctrl, opts);
1885 if (found)
1886 break;
1887 }
1888 mutex_unlock(&nvme_rdma_ctrl_mutex);
1889
1890 return found;
1891}
1892
Christoph Hellwig71102302016-07-06 21:55:52 +09001893static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1894 struct nvmf_ctrl_options *opts)
1895{
1896 struct nvme_rdma_ctrl *ctrl;
1897 int ret;
1898 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001899 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001900
1901 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1902 if (!ctrl)
1903 return ERR_PTR(-ENOMEM);
1904 ctrl->ctrl.opts = opts;
1905 INIT_LIST_HEAD(&ctrl->list);
1906
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001907 if (opts->mask & NVMF_OPT_TRSVCID)
1908 port = opts->trsvcid;
1909 else
1910 port = __stringify(NVME_RDMA_IP_PORT);
1911
1912 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1913 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001914 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001915 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001916 goto out_free_ctrl;
1917 }
1918
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001919 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001920 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1921 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001922 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001923 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001924 opts->host_traddr);
1925 goto out_free_ctrl;
1926 }
1927 }
1928
James Smart36e835f2017-10-20 16:17:09 -07001929 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1930 ret = -EALREADY;
1931 goto out_free_ctrl;
1932 }
1933
Christoph Hellwig71102302016-07-06 21:55:52 +09001934 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1935 0 /* no quirks, we're perfect! */);
1936 if (ret)
1937 goto out_free_ctrl;
1938
Christoph Hellwig71102302016-07-06 21:55:52 +09001939 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1940 nvme_rdma_reconnect_ctrl_work);
1941 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001942 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001943
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001944 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001945 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001946 ctrl->ctrl.kato = opts->kato;
1947
1948 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001949 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001950 GFP_KERNEL);
1951 if (!ctrl->queues)
1952 goto out_uninit_ctrl;
1953
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001954 ret = nvme_rdma_configure_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001955 if (ret)
1956 goto out_kfree_queues;
1957
1958 /* sanity check icdoff */
1959 if (ctrl->ctrl.icdoff) {
1960 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001961 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001962 goto out_remove_admin_queue;
1963 }
1964
1965 /* sanity check keyed sgls */
1966 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1967 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001968 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001969 goto out_remove_admin_queue;
1970 }
1971
1972 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1973 /* warn if maxcmd is lower than queue_size */
1974 dev_warn(ctrl->ctrl.device,
1975 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1976 opts->queue_size, ctrl->ctrl.maxcmd);
1977 opts->queue_size = ctrl->ctrl.maxcmd;
1978 }
1979
Samuel Jones76c08bf2016-10-25 09:22:34 +02001980 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1981 /* warn if sqsize is lower than queue_size */
1982 dev_warn(ctrl->ctrl.device,
1983 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1984 opts->queue_size, ctrl->ctrl.sqsize + 1);
1985 opts->queue_size = ctrl->ctrl.sqsize + 1;
1986 }
1987
Christoph Hellwig71102302016-07-06 21:55:52 +09001988 if (opts->nr_io_queues) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001989 ret = nvme_rdma_configure_io_queues(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001990 if (ret)
1991 goto out_remove_admin_queue;
1992 }
1993
1994 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1995 WARN_ON_ONCE(!changed);
1996
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001997 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001998 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1999
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002000 nvme_get_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09002001
2002 mutex_lock(&nvme_rdma_ctrl_mutex);
2003 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2004 mutex_unlock(&nvme_rdma_ctrl_mutex);
2005
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002006 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09002007
2008 return &ctrl->ctrl;
2009
2010out_remove_admin_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03002011 nvme_rdma_destroy_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09002012out_kfree_queues:
2013 kfree(ctrl->queues);
2014out_uninit_ctrl:
2015 nvme_uninit_ctrl(&ctrl->ctrl);
2016 nvme_put_ctrl(&ctrl->ctrl);
2017 if (ret > 0)
2018 ret = -EIO;
2019 return ERR_PTR(ret);
2020out_free_ctrl:
2021 kfree(ctrl);
2022 return ERR_PTR(ret);
2023}
2024
2025static struct nvmf_transport_ops nvme_rdma_transport = {
2026 .name = "rdma",
2027 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002028 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02002029 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09002030 .create_ctrl = nvme_rdma_create_ctrl,
2031};
2032
Steve Wisee87a9112016-09-02 09:01:54 -07002033static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2034{
2035 struct nvme_rdma_ctrl *ctrl;
2036
2037 /* Delete all controllers using this device */
2038 mutex_lock(&nvme_rdma_ctrl_mutex);
2039 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2040 if (ctrl->device->dev != ib_device)
2041 continue;
2042 dev_info(ctrl->ctrl.device,
2043 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2044 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002045 nvme_delete_ctrl(&ctrl->ctrl);
Steve Wisee87a9112016-09-02 09:01:54 -07002046 }
2047 mutex_unlock(&nvme_rdma_ctrl_mutex);
2048
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002049 flush_workqueue(nvme_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002050}
2051
2052static struct ib_client nvme_rdma_ib_client = {
2053 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002054 .remove = nvme_rdma_remove_one
2055};
2056
Christoph Hellwig71102302016-07-06 21:55:52 +09002057static int __init nvme_rdma_init_module(void)
2058{
Steve Wisee87a9112016-09-02 09:01:54 -07002059 int ret;
2060
Steve Wisee87a9112016-09-02 09:01:54 -07002061 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002062 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002063 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002064
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002065 ret = nvmf_register_transport(&nvme_rdma_transport);
2066 if (ret)
2067 goto err_unreg_client;
2068
2069 return 0;
2070
2071err_unreg_client:
2072 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002073 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002074}
2075
2076static void __exit nvme_rdma_cleanup_module(void)
2077{
Christoph Hellwig71102302016-07-06 21:55:52 +09002078 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002079 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002080}
2081
2082module_init(nvme_rdma_init_module);
2083module_exit(nvme_rdma_cleanup_module);
2084
2085MODULE_LICENSE("GPL v2");