blob: ccdbd99d5a1c89a1041083f90d44c826f1965732 [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>
22#include <linux/types.h>
23#include <linux/list.h>
24#include <linux/mutex.h>
25#include <linux/scatterlist.h>
26#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090027#include <asm/unaligned.h>
28
29#include <rdma/ib_verbs.h>
30#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090031#include <linux/nvme-rdma.h>
32
33#include "nvme.h"
34#include "fabrics.h"
35
36
Sagi Grimberg782d8202017-03-21 16:32:38 +020037#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090038
39#define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
40
41#define NVME_RDMA_MAX_SEGMENTS 256
42
43#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
44
Christoph Hellwig71102302016-07-06 21:55:52 +090045/*
46 * We handle AEN commands ourselves and don't even let the
47 * block layer know about them.
48 */
49#define NVME_RDMA_NR_AEN_COMMANDS 1
50#define NVME_RDMA_AQ_BLKMQ_DEPTH \
Sagi Grimberg7aa1f422017-06-18 16:15:59 +030051 (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
Christoph Hellwig71102302016-07-06 21:55:52 +090052
53struct nvme_rdma_device {
54 struct ib_device *dev;
55 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090056 struct kref ref;
57 struct list_head entry;
58};
59
60struct nvme_rdma_qe {
61 struct ib_cqe cqe;
62 void *data;
63 u64 dma;
64};
65
66struct nvme_rdma_queue;
67struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080068 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090069 struct ib_mr *mr;
70 struct nvme_rdma_qe sqe;
71 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
72 u32 num_sge;
73 int nents;
74 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090075 struct ib_reg_wr reg_wr;
76 struct ib_cqe reg_cqe;
77 struct nvme_rdma_queue *queue;
78 struct sg_table sg_table;
79 struct scatterlist first_sgl[];
80};
81
82enum nvme_rdma_queue_flags {
Sagi Grimbergb282a88d2017-05-04 13:33:11 +030083 NVME_RDMA_Q_LIVE = 0,
Sagi Grimbergabf87d52017-05-04 13:33:10 +030084 NVME_RDMA_Q_DELETING = 1,
Christoph Hellwig71102302016-07-06 21:55:52 +090085};
86
87struct nvme_rdma_queue {
88 struct nvme_rdma_qe *rsp_ring;
Marta Rybczynska5e599d72017-06-06 13:27:21 +020089 atomic_t sig_count;
Christoph Hellwig71102302016-07-06 21:55:52 +090090 int queue_size;
91 size_t cmnd_capsule_len;
92 struct nvme_rdma_ctrl *ctrl;
93 struct nvme_rdma_device *device;
94 struct ib_cq *ib_cq;
95 struct ib_qp *qp;
96
97 unsigned long flags;
98 struct rdma_cm_id *cm_id;
99 int cm_error;
100 struct completion cm_done;
101};
102
103struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +0900104 /* read only in the hot path */
105 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900106
107 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900108 struct blk_mq_tag_set tag_set;
109 struct work_struct delete_work;
Christoph Hellwig71102302016-07-06 21:55:52 +0900110 struct work_struct err_work;
111
112 struct nvme_rdma_qe async_event_sqe;
113
Christoph Hellwig71102302016-07-06 21:55:52 +0900114 struct delayed_work reconnect_work;
115
116 struct list_head list;
117
118 struct blk_mq_tag_set admin_tag_set;
119 struct nvme_rdma_device *device;
120
Christoph Hellwig71102302016-07-06 21:55:52 +0900121 u32 max_fr_pages;
122
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200123 struct sockaddr_storage addr;
124 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900125
126 struct nvme_ctrl ctrl;
127};
128
129static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
130{
131 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
132}
133
134static LIST_HEAD(device_list);
135static DEFINE_MUTEX(device_list_mutex);
136
137static LIST_HEAD(nvme_rdma_ctrl_list);
138static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
139
Christoph Hellwig71102302016-07-06 21:55:52 +0900140/*
141 * Disabling this option makes small I/O goes faster, but is fundamentally
142 * unsafe. With it turned off we will have to register a global rkey that
143 * allows read and write access to all physical memory.
144 */
145static bool register_always = true;
146module_param(register_always, bool, 0444);
147MODULE_PARM_DESC(register_always,
148 "Use memory registration even for contiguous memory regions");
149
150static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
151 struct rdma_cm_event *event);
152static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900153
154/* XXX: really should move to a generic header sooner or later.. */
155static inline void put_unaligned_le24(u32 val, u8 *p)
156{
157 *p++ = val;
158 *p++ = val >> 8;
159 *p++ = val >> 16;
160}
161
162static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
163{
164 return queue - queue->ctrl->queues;
165}
166
167static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
168{
169 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
170}
171
172static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
173 size_t capsule_size, enum dma_data_direction dir)
174{
175 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
176 kfree(qe->data);
177}
178
179static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
180 size_t capsule_size, enum dma_data_direction dir)
181{
182 qe->data = kzalloc(capsule_size, GFP_KERNEL);
183 if (!qe->data)
184 return -ENOMEM;
185
186 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
187 if (ib_dma_mapping_error(ibdev, qe->dma)) {
188 kfree(qe->data);
189 return -ENOMEM;
190 }
191
192 return 0;
193}
194
195static void nvme_rdma_free_ring(struct ib_device *ibdev,
196 struct nvme_rdma_qe *ring, size_t ib_queue_size,
197 size_t capsule_size, enum dma_data_direction dir)
198{
199 int i;
200
201 for (i = 0; i < ib_queue_size; i++)
202 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
203 kfree(ring);
204}
205
206static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
207 size_t ib_queue_size, size_t capsule_size,
208 enum dma_data_direction dir)
209{
210 struct nvme_rdma_qe *ring;
211 int i;
212
213 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
214 if (!ring)
215 return NULL;
216
217 for (i = 0; i < ib_queue_size; i++) {
218 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
219 goto out_free_ring;
220 }
221
222 return ring;
223
224out_free_ring:
225 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
226 return NULL;
227}
228
229static void nvme_rdma_qp_event(struct ib_event *event, void *context)
230{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200231 pr_debug("QP event %s (%d)\n",
232 ib_event_msg(event->event), event->event);
233
Christoph Hellwig71102302016-07-06 21:55:52 +0900234}
235
236static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
237{
238 wait_for_completion_interruptible_timeout(&queue->cm_done,
239 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
240 return queue->cm_error;
241}
242
243static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
244{
245 struct nvme_rdma_device *dev = queue->device;
246 struct ib_qp_init_attr init_attr;
247 int ret;
248
249 memset(&init_attr, 0, sizeof(init_attr));
250 init_attr.event_handler = nvme_rdma_qp_event;
251 /* +1 for drain */
252 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
253 /* +1 for drain */
254 init_attr.cap.max_recv_wr = queue->queue_size + 1;
255 init_attr.cap.max_recv_sge = 1;
256 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
257 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
258 init_attr.qp_type = IB_QPT_RC;
259 init_attr.send_cq = queue->ib_cq;
260 init_attr.recv_cq = queue->ib_cq;
261
262 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
263
264 queue->qp = queue->cm_id->qp;
265 return ret;
266}
267
268static int nvme_rdma_reinit_request(void *data, struct request *rq)
269{
270 struct nvme_rdma_ctrl *ctrl = data;
271 struct nvme_rdma_device *dev = ctrl->device;
272 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
273 int ret = 0;
274
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300275 if (!req->mr->need_inval)
Christoph Hellwig71102302016-07-06 21:55:52 +0900276 goto out;
277
278 ib_dereg_mr(req->mr);
279
280 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
281 ctrl->max_fr_pages);
282 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900283 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000284 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100285 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900286 }
287
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300288 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900289
290out:
291 return ret;
292}
293
Christoph Hellwig385475e2017-06-13 09:15:19 +0200294static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
295 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900296{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200297 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900298 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200299 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900300 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
301 struct nvme_rdma_device *dev = queue->device;
302
303 if (req->mr)
304 ib_dereg_mr(req->mr);
305
306 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
307 DMA_TO_DEVICE);
308}
309
Christoph Hellwig385475e2017-06-13 09:15:19 +0200310static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
311 struct request *rq, unsigned int hctx_idx,
312 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900313{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200314 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900315 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200316 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900317 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
318 struct nvme_rdma_device *dev = queue->device;
319 struct ib_device *ibdev = dev->dev;
320 int ret;
321
Christoph Hellwig71102302016-07-06 21:55:52 +0900322 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
323 DMA_TO_DEVICE);
324 if (ret)
325 return ret;
326
327 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
328 ctrl->max_fr_pages);
329 if (IS_ERR(req->mr)) {
330 ret = PTR_ERR(req->mr);
331 goto out_free_qe;
332 }
333
334 req->queue = queue;
335
336 return 0;
337
338out_free_qe:
339 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
340 DMA_TO_DEVICE);
341 return -ENOMEM;
342}
343
Christoph Hellwig71102302016-07-06 21:55:52 +0900344static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
345 unsigned int hctx_idx)
346{
347 struct nvme_rdma_ctrl *ctrl = data;
348 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
349
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300350 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900351
352 hctx->driver_data = queue;
353 return 0;
354}
355
356static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
357 unsigned int hctx_idx)
358{
359 struct nvme_rdma_ctrl *ctrl = data;
360 struct nvme_rdma_queue *queue = &ctrl->queues[0];
361
362 BUG_ON(hctx_idx != 0);
363
364 hctx->driver_data = queue;
365 return 0;
366}
367
368static void nvme_rdma_free_dev(struct kref *ref)
369{
370 struct nvme_rdma_device *ndev =
371 container_of(ref, struct nvme_rdma_device, ref);
372
373 mutex_lock(&device_list_mutex);
374 list_del(&ndev->entry);
375 mutex_unlock(&device_list_mutex);
376
Christoph Hellwig71102302016-07-06 21:55:52 +0900377 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900378 kfree(ndev);
379}
380
381static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
382{
383 kref_put(&dev->ref, nvme_rdma_free_dev);
384}
385
386static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
387{
388 return kref_get_unless_zero(&dev->ref);
389}
390
391static struct nvme_rdma_device *
392nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
393{
394 struct nvme_rdma_device *ndev;
395
396 mutex_lock(&device_list_mutex);
397 list_for_each_entry(ndev, &device_list, entry) {
398 if (ndev->dev->node_guid == cm_id->device->node_guid &&
399 nvme_rdma_dev_get(ndev))
400 goto out_unlock;
401 }
402
403 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
404 if (!ndev)
405 goto out_err;
406
407 ndev->dev = cm_id->device;
408 kref_init(&ndev->ref);
409
Christoph Hellwig11975e02016-09-05 12:56:20 +0200410 ndev->pd = ib_alloc_pd(ndev->dev,
411 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900412 if (IS_ERR(ndev->pd))
413 goto out_free_dev;
414
Christoph Hellwig71102302016-07-06 21:55:52 +0900415 if (!(ndev->dev->attrs.device_cap_flags &
416 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
417 dev_err(&ndev->dev->dev,
418 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200419 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900420 }
421
422 list_add(&ndev->entry, &device_list);
423out_unlock:
424 mutex_unlock(&device_list_mutex);
425 return ndev;
426
Christoph Hellwig71102302016-07-06 21:55:52 +0900427out_free_pd:
428 ib_dealloc_pd(ndev->pd);
429out_free_dev:
430 kfree(ndev);
431out_err:
432 mutex_unlock(&device_list_mutex);
433 return NULL;
434}
435
436static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
437{
Steve Wisef361e5a2016-09-02 09:01:27 -0700438 struct nvme_rdma_device *dev;
439 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900440
Steve Wisef361e5a2016-09-02 09:01:27 -0700441 dev = queue->device;
442 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900443 rdma_destroy_qp(queue->cm_id);
444 ib_free_cq(queue->ib_cq);
445
446 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
447 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
448
449 nvme_rdma_dev_put(dev);
450}
451
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300452static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900453{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300454 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900455 const int send_wr_factor = 3; /* MR, SEND, INV */
456 const int cq_factor = send_wr_factor + 1; /* + RECV */
457 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900458 int ret;
459
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300460 queue->device = nvme_rdma_find_get_device(queue->cm_id);
461 if (!queue->device) {
462 dev_err(queue->cm_id->device->dev.parent,
463 "no client data found!\n");
464 return -ECONNREFUSED;
465 }
466 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900467
468 /*
469 * The admin queue is barely used once the controller is live, so don't
470 * bother to spread it out.
471 */
472 if (idx == 0)
473 comp_vector = 0;
474 else
475 comp_vector = idx % ibdev->num_comp_vectors;
476
477
478 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300479 queue->ib_cq = ib_alloc_cq(ibdev, queue,
480 cq_factor * queue->queue_size + 1,
481 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900482 if (IS_ERR(queue->ib_cq)) {
483 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300484 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900485 }
486
487 ret = nvme_rdma_create_qp(queue, send_wr_factor);
488 if (ret)
489 goto out_destroy_ib_cq;
490
491 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
492 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
493 if (!queue->rsp_ring) {
494 ret = -ENOMEM;
495 goto out_destroy_qp;
496 }
497
498 return 0;
499
500out_destroy_qp:
501 ib_destroy_qp(queue->qp);
502out_destroy_ib_cq:
503 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300504out_put_dev:
505 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900506 return ret;
507}
508
509static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
510 int idx, size_t queue_size)
511{
512 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200513 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900514 int ret;
515
516 queue = &ctrl->queues[idx];
517 queue->ctrl = ctrl;
518 init_completion(&queue->cm_done);
519
520 if (idx > 0)
521 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
522 else
523 queue->cmnd_capsule_len = sizeof(struct nvme_command);
524
525 queue->queue_size = queue_size;
Marta Rybczynska5e599d72017-06-06 13:27:21 +0200526 atomic_set(&queue->sig_count, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900527
528 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
529 RDMA_PS_TCP, IB_QPT_RC);
530 if (IS_ERR(queue->cm_id)) {
531 dev_info(ctrl->ctrl.device,
532 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
533 return PTR_ERR(queue->cm_id);
534 }
535
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200536 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200537 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200538
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200539 queue->cm_error = -ETIMEDOUT;
540 ret = rdma_resolve_addr(queue->cm_id, src_addr,
541 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900542 NVME_RDMA_CONNECT_TIMEOUT_MS);
543 if (ret) {
544 dev_info(ctrl->ctrl.device,
545 "rdma_resolve_addr failed (%d).\n", ret);
546 goto out_destroy_cm_id;
547 }
548
549 ret = nvme_rdma_wait_for_cm(queue);
550 if (ret) {
551 dev_info(ctrl->ctrl.device,
552 "rdma_resolve_addr wait failed (%d).\n", ret);
553 goto out_destroy_cm_id;
554 }
555
Sagi Grimberg3b4ac782016-09-22 19:58:17 -0600556 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900557
558 return 0;
559
560out_destroy_cm_id:
561 rdma_destroy_id(queue->cm_id);
562 return ret;
563}
564
565static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
566{
567 rdma_disconnect(queue->cm_id);
568 ib_drain_qp(queue->qp);
569}
570
571static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
572{
573 nvme_rdma_destroy_queue_ib(queue);
574 rdma_destroy_id(queue->cm_id);
575}
576
577static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
578{
Sagi Grimberge89ca582016-09-02 09:01:54 -0700579 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900580 return;
581 nvme_rdma_stop_queue(queue);
582 nvme_rdma_free_queue(queue);
583}
584
585static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
586{
587 int i;
588
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300589 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Christoph Hellwig71102302016-07-06 21:55:52 +0900590 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
591}
592
593static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
594{
595 int i, ret = 0;
596
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300597 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900598 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
Steve Wisec8dbc372016-11-08 09:16:02 -0800599 if (ret) {
600 dev_info(ctrl->ctrl.device,
601 "failed to connect i/o queue: %d\n", ret);
602 goto out_free_queues;
603 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600604 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900605 }
606
Steve Wisec8dbc372016-11-08 09:16:02 -0800607 return 0;
608
609out_free_queues:
610 nvme_rdma_free_io_queues(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900611 return ret;
612}
613
614static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
615{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200616 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
617 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900618 int i, ret;
619
Sagi Grimbergc248c642017-03-09 13:26:07 +0200620 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
621 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
622 if (ret)
623 return ret;
624
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300625 ctrl->ctrl.queue_count = nr_io_queues + 1;
626 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200627 return 0;
628
629 dev_info(ctrl->ctrl.device,
630 "creating %d I/O queues.\n", nr_io_queues);
631
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300632 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Jay Freyenseec5af8652016-08-17 15:00:27 -0700633 ret = nvme_rdma_init_queue(ctrl, i,
634 ctrl->ctrl.opts->queue_size);
Christoph Hellwig71102302016-07-06 21:55:52 +0900635 if (ret) {
636 dev_info(ctrl->ctrl.device,
637 "failed to initialize i/o queue: %d\n", ret);
638 goto out_free_queues;
639 }
640 }
641
642 return 0;
643
644out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700645 for (i--; i >= 1; i--)
Christoph Hellwig71102302016-07-06 21:55:52 +0900646 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
647
648 return ret;
649}
650
651static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
652{
653 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
654 sizeof(struct nvme_command), DMA_TO_DEVICE);
655 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
656 blk_cleanup_queue(ctrl->ctrl.admin_q);
657 blk_mq_free_tag_set(&ctrl->admin_tag_set);
658 nvme_rdma_dev_put(ctrl->device);
659}
660
661static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
662{
663 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
664
665 if (list_empty(&ctrl->list))
666 goto free_ctrl;
667
668 mutex_lock(&nvme_rdma_ctrl_mutex);
669 list_del(&ctrl->list);
670 mutex_unlock(&nvme_rdma_ctrl_mutex);
671
Christoph Hellwig71102302016-07-06 21:55:52 +0900672 kfree(ctrl->queues);
673 nvmf_free_options(nctrl->opts);
674free_ctrl:
675 kfree(ctrl);
676}
677
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200678static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
679{
680 /* If we are resetting/deleting then do nothing */
681 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
682 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
683 ctrl->ctrl.state == NVME_CTRL_LIVE);
684 return;
685 }
686
687 if (nvmf_should_reconnect(&ctrl->ctrl)) {
688 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
689 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200690 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200691 ctrl->ctrl.opts->reconnect_delay * HZ);
692 } else {
693 dev_info(ctrl->ctrl.device, "Removing controller...\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200694 queue_work(nvme_wq, &ctrl->delete_work);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200695 }
696}
697
Christoph Hellwig71102302016-07-06 21:55:52 +0900698static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
699{
700 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
701 struct nvme_rdma_ctrl, reconnect_work);
702 bool changed;
703 int ret;
704
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300705 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200706
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300707 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900708 nvme_rdma_free_io_queues(ctrl);
709
710 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
711 if (ret)
712 goto requeue;
713 }
714
715 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
716
717 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
718 if (ret)
719 goto requeue;
720
Sagi Grimberg7aa1f422017-06-18 16:15:59 +0300721 ret = nvme_rdma_init_queue(ctrl, 0, NVME_AQ_DEPTH);
Christoph Hellwig71102302016-07-06 21:55:52 +0900722 if (ret)
723 goto requeue;
724
Christoph Hellwig71102302016-07-06 21:55:52 +0900725 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
726 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300727 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900728
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600729 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
730
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +0300731 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +0900732 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300733 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900734
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300735 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900736 ret = nvme_rdma_init_io_queues(ctrl);
737 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300738 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900739
740 ret = nvme_rdma_connect_io_queues(ctrl);
741 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300742 goto requeue;
Sagi Grimberg4c8b99f2017-06-29 11:10:44 +0300743
744 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
745 ctrl->ctrl.queue_count - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900746 }
747
748 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
749 WARN_ON_ONCE(!changed);
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300750 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900751
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300752 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900753
754 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
755
756 return;
757
Christoph Hellwig71102302016-07-06 21:55:52 +0900758requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200759 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300760 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200761 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900762}
763
764static void nvme_rdma_error_recovery_work(struct work_struct *work)
765{
766 struct nvme_rdma_ctrl *ctrl = container_of(work,
767 struct nvme_rdma_ctrl, err_work);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700768 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +0900769
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300770 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700771
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300772 for (i = 0; i < ctrl->ctrl.queue_count; i++)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600773 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700774
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300775 if (ctrl->ctrl.queue_count > 1)
Christoph Hellwig71102302016-07-06 21:55:52 +0900776 nvme_stop_queues(&ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300777 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +0900778
779 /* We must take care of fastfail/requeue all our inflight requests */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300780 if (ctrl->ctrl.queue_count > 1)
Christoph Hellwig71102302016-07-06 21:55:52 +0900781 blk_mq_tagset_busy_iter(&ctrl->tag_set,
782 nvme_cancel_request, &ctrl->ctrl);
783 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
784 nvme_cancel_request, &ctrl->ctrl);
785
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300786 /*
787 * queues are not a live anymore, so restart the queues to fail fast
788 * new IO
789 */
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300790 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300791 nvme_start_queues(&ctrl->ctrl);
792
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200793 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900794}
795
796static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
797{
798 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
799 return;
800
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200801 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +0900802}
803
804static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
805 const char *op)
806{
807 struct nvme_rdma_queue *queue = cq->cq_context;
808 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
809
810 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
811 dev_info(ctrl->ctrl.device,
812 "%s for CQE 0x%p failed with status %s (%d)\n",
813 op, wc->wr_cqe,
814 ib_wc_status_msg(wc->status), wc->status);
815 nvme_rdma_error_recovery(ctrl);
816}
817
818static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
819{
820 if (unlikely(wc->status != IB_WC_SUCCESS))
821 nvme_rdma_wr_error(cq, wc, "MEMREG");
822}
823
824static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
825{
826 if (unlikely(wc->status != IB_WC_SUCCESS))
827 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
828}
829
830static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
831 struct nvme_rdma_request *req)
832{
833 struct ib_send_wr *bad_wr;
834 struct ib_send_wr wr = {
835 .opcode = IB_WR_LOCAL_INV,
836 .next = NULL,
837 .num_sge = 0,
838 .send_flags = 0,
839 .ex.invalidate_rkey = req->mr->rkey,
840 };
841
842 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
843 wr.wr_cqe = &req->reg_cqe;
844
845 return ib_post_send(queue->qp, &wr, &bad_wr);
846}
847
848static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
849 struct request *rq)
850{
851 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
852 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
853 struct nvme_rdma_device *dev = queue->device;
854 struct ib_device *ibdev = dev->dev;
855 int res;
856
857 if (!blk_rq_bytes(rq))
858 return;
859
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300860 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900861 res = nvme_rdma_inv_rkey(queue, req);
862 if (res < 0) {
863 dev_err(ctrl->ctrl.device,
864 "Queueing INV WR for rkey %#x failed (%d)\n",
865 req->mr->rkey, res);
866 nvme_rdma_error_recovery(queue->ctrl);
867 }
868 }
869
870 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
871 req->nents, rq_data_dir(rq) ==
872 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
873
874 nvme_cleanup_cmd(rq);
875 sg_free_table_chained(&req->sg_table, true);
876}
877
878static int nvme_rdma_set_sg_null(struct nvme_command *c)
879{
880 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
881
882 sg->addr = 0;
883 put_unaligned_le24(0, sg->length);
884 put_unaligned_le32(0, sg->key);
885 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
886 return 0;
887}
888
889static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
890 struct nvme_rdma_request *req, struct nvme_command *c)
891{
892 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
893
894 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
895 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
896 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
897
898 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
899 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
900 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
901
902 req->inline_data = true;
903 req->num_sge++;
904 return 0;
905}
906
907static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
908 struct nvme_rdma_request *req, struct nvme_command *c)
909{
910 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
911
912 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
913 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +0200914 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +0900915 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
916 return 0;
917}
918
919static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
920 struct nvme_rdma_request *req, struct nvme_command *c,
921 int count)
922{
923 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
924 int nr;
925
926 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
927 if (nr < count) {
928 if (nr < 0)
929 return nr;
930 return -EINVAL;
931 }
932
933 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
934
935 req->reg_cqe.done = nvme_rdma_memreg_done;
936 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
937 req->reg_wr.wr.opcode = IB_WR_REG_MR;
938 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
939 req->reg_wr.wr.num_sge = 0;
940 req->reg_wr.mr = req->mr;
941 req->reg_wr.key = req->mr->rkey;
942 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
943 IB_ACCESS_REMOTE_READ |
944 IB_ACCESS_REMOTE_WRITE;
945
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300946 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +0900947
948 sg->addr = cpu_to_le64(req->mr->iova);
949 put_unaligned_le24(req->mr->length, sg->length);
950 put_unaligned_le32(req->mr->rkey, sg->key);
951 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
952 NVME_SGL_FMT_INVALIDATE;
953
954 return 0;
955}
956
957static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100958 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +0900959{
960 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
961 struct nvme_rdma_device *dev = queue->device;
962 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700963 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900964
965 req->num_sge = 1;
966 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300967 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900968
969 c->common.flags |= NVME_CMD_SGL_METABUF;
970
971 if (!blk_rq_bytes(rq))
972 return nvme_rdma_set_sg_null(c);
973
974 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700975 ret = sg_alloc_table_chained(&req->sg_table,
976 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900977 if (ret)
978 return -ENOMEM;
979
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700980 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900981
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700982 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +0900983 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
984 if (unlikely(count <= 0)) {
985 sg_free_table_chained(&req->sg_table, true);
986 return -EIO;
987 }
988
989 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +0100990 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
991 blk_rq_payload_bytes(rq) <=
992 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +0900993 return nvme_rdma_map_sg_inline(queue, req, c);
994
Christoph Hellwig11975e02016-09-05 12:56:20 +0200995 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +0900996 return nvme_rdma_map_sg_single(queue, req, c);
997 }
998
999 return nvme_rdma_map_sg_fr(queue, req, c, count);
1000}
1001
1002static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1003{
1004 if (unlikely(wc->status != IB_WC_SUCCESS))
1005 nvme_rdma_wr_error(cq, wc, "SEND");
1006}
1007
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001008/*
1009 * We want to signal completion at least every queue depth/2. This returns the
1010 * largest power of two that is not above half of (queue size + 1) to optimize
1011 * (avoid divisions).
1012 */
1013static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
Marta Rybczynska0544f542017-04-10 17:12:34 +02001014{
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001015 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
Marta Rybczynska0544f542017-04-10 17:12:34 +02001016
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001017 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
Marta Rybczynska0544f542017-04-10 17:12:34 +02001018}
1019
Christoph Hellwig71102302016-07-06 21:55:52 +09001020static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1021 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1022 struct ib_send_wr *first, bool flush)
1023{
1024 struct ib_send_wr wr, *bad_wr;
1025 int ret;
1026
1027 sge->addr = qe->dma;
1028 sge->length = sizeof(struct nvme_command),
1029 sge->lkey = queue->device->pd->local_dma_lkey;
1030
1031 qe->cqe.done = nvme_rdma_send_done;
1032
1033 wr.next = NULL;
1034 wr.wr_cqe = &qe->cqe;
1035 wr.sg_list = sge;
1036 wr.num_sge = num_sge;
1037 wr.opcode = IB_WR_SEND;
1038 wr.send_flags = 0;
1039
1040 /*
1041 * Unsignalled send completions are another giant desaster in the
1042 * IB Verbs spec: If we don't regularly post signalled sends
1043 * the send queue will fill up and only a QP reset will rescue us.
1044 * Would have been way to obvious to handle this in hardware or
1045 * at least the RDMA stack..
1046 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001047 * Always signal the flushes. The magic request used for the flush
1048 * sequencer is not allocated in our driver's tagset and it's
1049 * triggered to be freed by blk_cleanup_queue(). So we need to
1050 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001051 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001052 * calls wr_cqe->done().
1053 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001054 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001055 wr.send_flags |= IB_SEND_SIGNALED;
1056
1057 if (first)
1058 first->next = &wr;
1059 else
1060 first = &wr;
1061
1062 ret = ib_post_send(queue->qp, first, &bad_wr);
1063 if (ret) {
1064 dev_err(queue->ctrl->ctrl.device,
1065 "%s failed with error code %d\n", __func__, ret);
1066 }
1067 return ret;
1068}
1069
1070static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1071 struct nvme_rdma_qe *qe)
1072{
1073 struct ib_recv_wr wr, *bad_wr;
1074 struct ib_sge list;
1075 int ret;
1076
1077 list.addr = qe->dma;
1078 list.length = sizeof(struct nvme_completion);
1079 list.lkey = queue->device->pd->local_dma_lkey;
1080
1081 qe->cqe.done = nvme_rdma_recv_done;
1082
1083 wr.next = NULL;
1084 wr.wr_cqe = &qe->cqe;
1085 wr.sg_list = &list;
1086 wr.num_sge = 1;
1087
1088 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1089 if (ret) {
1090 dev_err(queue->ctrl->ctrl.device,
1091 "%s failed with error code %d\n", __func__, ret);
1092 }
1093 return ret;
1094}
1095
1096static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1097{
1098 u32 queue_idx = nvme_rdma_queue_idx(queue);
1099
1100 if (queue_idx == 0)
1101 return queue->ctrl->admin_tag_set.tags[queue_idx];
1102 return queue->ctrl->tag_set.tags[queue_idx - 1];
1103}
1104
1105static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1106{
1107 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1108 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1109 struct ib_device *dev = queue->device->dev;
1110 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1111 struct nvme_command *cmd = sqe->data;
1112 struct ib_sge sge;
1113 int ret;
1114
1115 if (WARN_ON_ONCE(aer_idx != 0))
1116 return;
1117
1118 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1119
1120 memset(cmd, 0, sizeof(*cmd));
1121 cmd->common.opcode = nvme_admin_async_event;
1122 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1123 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1124 nvme_rdma_set_sg_null(cmd);
1125
1126 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1127 DMA_TO_DEVICE);
1128
1129 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1130 WARN_ON_ONCE(ret);
1131}
1132
1133static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1134 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1135{
Christoph Hellwig71102302016-07-06 21:55:52 +09001136 struct request *rq;
1137 struct nvme_rdma_request *req;
1138 int ret = 0;
1139
Christoph Hellwig71102302016-07-06 21:55:52 +09001140 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1141 if (!rq) {
1142 dev_err(queue->ctrl->ctrl.device,
1143 "tag 0x%x on QP %#x not found\n",
1144 cqe->command_id, queue->qp->qp_num);
1145 nvme_rdma_error_recovery(queue->ctrl);
1146 return ret;
1147 }
1148 req = blk_mq_rq_to_pdu(rq);
1149
Christoph Hellwig71102302016-07-06 21:55:52 +09001150 if (rq->tag == tag)
1151 ret = 1;
1152
1153 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1154 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001155 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001156
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001157 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001158 return ret;
1159}
1160
1161static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1162{
1163 struct nvme_rdma_qe *qe =
1164 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1165 struct nvme_rdma_queue *queue = cq->cq_context;
1166 struct ib_device *ibdev = queue->device->dev;
1167 struct nvme_completion *cqe = qe->data;
1168 const size_t len = sizeof(struct nvme_completion);
1169 int ret = 0;
1170
1171 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1172 nvme_rdma_wr_error(cq, wc, "RECV");
1173 return 0;
1174 }
1175
1176 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1177 /*
1178 * AEN requests are special as they don't time out and can
1179 * survive any kind of queue freeze and often don't respond to
1180 * aborts. We don't even bother to allocate a struct request
1181 * for them but rather special case them here.
1182 */
1183 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1184 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001185 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1186 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001187 else
1188 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1189 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1190
1191 nvme_rdma_post_recv(queue, qe);
1192 return ret;
1193}
1194
1195static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1196{
1197 __nvme_rdma_recv_done(cq, wc, -1);
1198}
1199
1200static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1201{
1202 int ret, i;
1203
1204 for (i = 0; i < queue->queue_size; i++) {
1205 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1206 if (ret)
1207 goto out_destroy_queue_ib;
1208 }
1209
1210 return 0;
1211
1212out_destroy_queue_ib:
1213 nvme_rdma_destroy_queue_ib(queue);
1214 return ret;
1215}
1216
1217static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1218 struct rdma_cm_event *ev)
1219{
Steve Wise7f039532016-10-26 12:36:47 -07001220 struct rdma_cm_id *cm_id = queue->cm_id;
1221 int status = ev->status;
1222 const char *rej_msg;
1223 const struct nvme_rdma_cm_rej *rej_data;
1224 u8 rej_data_len;
1225
1226 rej_msg = rdma_reject_msg(cm_id, status);
1227 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1228
1229 if (rej_data && rej_data_len >= sizeof(u16)) {
1230 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001231
1232 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001233 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1234 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001235 } else {
1236 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001237 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001238 }
1239
1240 return -ECONNRESET;
1241}
1242
1243static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1244{
Christoph Hellwig71102302016-07-06 21:55:52 +09001245 int ret;
1246
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001247 ret = nvme_rdma_create_queue_ib(queue);
1248 if (ret)
1249 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001250
1251 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1252 if (ret) {
1253 dev_err(queue->ctrl->ctrl.device,
1254 "rdma_resolve_route failed (%d).\n",
1255 queue->cm_error);
1256 goto out_destroy_queue;
1257 }
1258
1259 return 0;
1260
1261out_destroy_queue:
1262 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001263 return ret;
1264}
1265
1266static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1267{
1268 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1269 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001270 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001271 int ret;
1272
1273 param.qp_num = queue->qp->qp_num;
1274 param.flow_control = 1;
1275
1276 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001277 /* maximum retry count */
1278 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001279 param.rnr_retry_count = 7;
1280 param.private_data = &priv;
1281 param.private_data_len = sizeof(priv);
1282
1283 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1284 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001285 /*
1286 * set the admin queue depth to the minimum size
1287 * specified by the Fabrics standard.
1288 */
1289 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001290 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1291 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001292 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001293 /*
1294 * current interpretation of the fabrics spec
1295 * is at minimum you make hrqsize sqsize+1, or a
1296 * 1's based representation of sqsize.
1297 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001298 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001299 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001300 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001301
1302 ret = rdma_connect(queue->cm_id, &param);
1303 if (ret) {
1304 dev_err(ctrl->ctrl.device,
1305 "rdma_connect failed (%d).\n", ret);
1306 goto out_destroy_queue_ib;
1307 }
1308
1309 return 0;
1310
1311out_destroy_queue_ib:
1312 nvme_rdma_destroy_queue_ib(queue);
1313 return ret;
1314}
1315
Christoph Hellwig71102302016-07-06 21:55:52 +09001316static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1317 struct rdma_cm_event *ev)
1318{
1319 struct nvme_rdma_queue *queue = cm_id->context;
1320 int cm_error = 0;
1321
1322 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1323 rdma_event_msg(ev->event), ev->event,
1324 ev->status, cm_id);
1325
1326 switch (ev->event) {
1327 case RDMA_CM_EVENT_ADDR_RESOLVED:
1328 cm_error = nvme_rdma_addr_resolved(queue);
1329 break;
1330 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1331 cm_error = nvme_rdma_route_resolved(queue);
1332 break;
1333 case RDMA_CM_EVENT_ESTABLISHED:
1334 queue->cm_error = nvme_rdma_conn_established(queue);
1335 /* complete cm_done regardless of success/failure */
1336 complete(&queue->cm_done);
1337 return 0;
1338 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001339 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001340 cm_error = nvme_rdma_conn_rejected(queue, ev);
1341 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001342 case RDMA_CM_EVENT_ROUTE_ERROR:
1343 case RDMA_CM_EVENT_CONNECT_ERROR:
1344 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001345 nvme_rdma_destroy_queue_ib(queue);
1346 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001347 dev_dbg(queue->ctrl->ctrl.device,
1348 "CM error event %d\n", ev->event);
1349 cm_error = -ECONNRESET;
1350 break;
1351 case RDMA_CM_EVENT_DISCONNECTED:
1352 case RDMA_CM_EVENT_ADDR_CHANGE:
1353 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1354 dev_dbg(queue->ctrl->ctrl.device,
1355 "disconnect received - connection closed\n");
1356 nvme_rdma_error_recovery(queue->ctrl);
1357 break;
1358 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001359 /* device removal is handled via the ib_client API */
1360 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001361 default:
1362 dev_err(queue->ctrl->ctrl.device,
1363 "Unexpected RDMA CM event (%d)\n", ev->event);
1364 nvme_rdma_error_recovery(queue->ctrl);
1365 break;
1366 }
1367
1368 if (cm_error) {
1369 queue->cm_error = cm_error;
1370 complete(&queue->cm_done);
1371 }
1372
1373 return 0;
1374}
1375
1376static enum blk_eh_timer_return
1377nvme_rdma_timeout(struct request *rq, bool reserved)
1378{
1379 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1380
1381 /* queue error recovery */
1382 nvme_rdma_error_recovery(req->queue->ctrl);
1383
1384 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001385 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001386
1387 return BLK_EH_HANDLED;
1388}
1389
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001390/*
1391 * We cannot accept any other command until the Connect command has completed.
1392 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001393static inline blk_status_t
1394nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001395{
1396 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001397 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001398
Christoph Hellwig57292b52017-01-31 16:57:29 +01001399 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001400 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001401 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1402 /*
1403 * reconnecting state means transport disruption, which
1404 * can take a long time and even might fail permanently,
1405 * so we can't let incoming I/O be requeued forever.
1406 * fail it fast to allow upper layers a chance to
1407 * failover.
1408 */
1409 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001410 return BLK_STS_IOERR;
1411 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001412 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001413 }
1414
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001415 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001416}
1417
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001418static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001419 const struct blk_mq_queue_data *bd)
1420{
1421 struct nvme_ns *ns = hctx->queue->queuedata;
1422 struct nvme_rdma_queue *queue = hctx->driver_data;
1423 struct request *rq = bd->rq;
1424 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1425 struct nvme_rdma_qe *sqe = &req->sqe;
1426 struct nvme_command *c = sqe->data;
1427 bool flush = false;
1428 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001429 blk_status_t ret;
1430 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001431
1432 WARN_ON_ONCE(rq->tag < 0);
1433
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001434 ret = nvme_rdma_queue_is_ready(queue, rq);
1435 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001436 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001437
Christoph Hellwig71102302016-07-06 21:55:52 +09001438 dev = queue->device->dev;
1439 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1440 sizeof(struct nvme_command), DMA_TO_DEVICE);
1441
1442 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001443 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001444 return ret;
1445
Christoph Hellwig71102302016-07-06 21:55:52 +09001446 blk_mq_start_request(rq);
1447
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001448 err = nvme_rdma_map_data(queue, rq, c);
1449 if (err < 0) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001450 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001451 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001452 nvme_cleanup_cmd(rq);
1453 goto err;
1454 }
1455
1456 ib_dma_sync_single_for_device(dev, sqe->dma,
1457 sizeof(struct nvme_command), DMA_TO_DEVICE);
1458
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001459 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001460 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001461 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001462 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001463 if (err) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001464 nvme_rdma_unmap_data(queue, rq);
1465 goto err;
1466 }
1467
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001468 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001469err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001470 if (err == -ENOMEM || err == -EAGAIN)
1471 return BLK_STS_RESOURCE;
1472 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001473}
1474
1475static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1476{
1477 struct nvme_rdma_queue *queue = hctx->driver_data;
1478 struct ib_cq *cq = queue->ib_cq;
1479 struct ib_wc wc;
1480 int found = 0;
1481
Christoph Hellwig71102302016-07-06 21:55:52 +09001482 while (ib_poll_cq(cq, 1, &wc) > 0) {
1483 struct ib_cqe *cqe = wc.wr_cqe;
1484
1485 if (cqe) {
1486 if (cqe->done == nvme_rdma_recv_done)
1487 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1488 else
1489 cqe->done(cq, &wc);
1490 }
1491 }
1492
1493 return found;
1494}
1495
1496static void nvme_rdma_complete_rq(struct request *rq)
1497{
1498 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001499
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001500 nvme_rdma_unmap_data(req->queue, rq);
1501 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001502}
1503
Eric Biggersf363b082017-03-30 13:39:16 -07001504static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001505 .queue_rq = nvme_rdma_queue_rq,
1506 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001507 .init_request = nvme_rdma_init_request,
1508 .exit_request = nvme_rdma_exit_request,
1509 .reinit_request = nvme_rdma_reinit_request,
1510 .init_hctx = nvme_rdma_init_hctx,
1511 .poll = nvme_rdma_poll,
1512 .timeout = nvme_rdma_timeout,
1513};
1514
Eric Biggersf363b082017-03-30 13:39:16 -07001515static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001516 .queue_rq = nvme_rdma_queue_rq,
1517 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001518 .init_request = nvme_rdma_init_request,
1519 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001520 .reinit_request = nvme_rdma_reinit_request,
1521 .init_hctx = nvme_rdma_init_admin_hctx,
1522 .timeout = nvme_rdma_timeout,
1523};
1524
1525static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1526{
1527 int error;
1528
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001529 error = nvme_rdma_init_queue(ctrl, 0, NVME_AQ_DEPTH);
Christoph Hellwig71102302016-07-06 21:55:52 +09001530 if (error)
1531 return error;
1532
1533 ctrl->device = ctrl->queues[0].device;
1534
1535 /*
1536 * We need a reference on the device as long as the tag_set is alive,
1537 * as the MRs in the request structures need a valid ib_device.
1538 */
1539 error = -EINVAL;
1540 if (!nvme_rdma_dev_get(ctrl->device))
1541 goto out_free_queue;
1542
1543 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1544 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1545
1546 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1547 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1548 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1549 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1550 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1551 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1552 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1553 ctrl->admin_tag_set.driver_data = ctrl;
1554 ctrl->admin_tag_set.nr_hw_queues = 1;
1555 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1556
1557 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1558 if (error)
1559 goto out_put_dev;
1560
1561 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1562 if (IS_ERR(ctrl->ctrl.admin_q)) {
1563 error = PTR_ERR(ctrl->ctrl.admin_q);
1564 goto out_free_tagset;
1565 }
1566
1567 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1568 if (error)
1569 goto out_cleanup_queue;
1570
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001571 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1572
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001573 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP,
1574 &ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001575 if (error) {
1576 dev_err(ctrl->ctrl.device,
1577 "prop_get NVME_REG_CAP failed\n");
1578 goto out_cleanup_queue;
1579 }
1580
1581 ctrl->ctrl.sqsize =
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001582 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
Christoph Hellwig71102302016-07-06 21:55:52 +09001583
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +03001584 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001585 if (error)
1586 goto out_cleanup_queue;
1587
1588 ctrl->ctrl.max_hw_sectors =
1589 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1590
1591 error = nvme_init_identify(&ctrl->ctrl);
1592 if (error)
1593 goto out_cleanup_queue;
1594
1595 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1596 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1597 DMA_TO_DEVICE);
1598 if (error)
1599 goto out_cleanup_queue;
1600
Christoph Hellwig71102302016-07-06 21:55:52 +09001601 return 0;
1602
1603out_cleanup_queue:
1604 blk_cleanup_queue(ctrl->ctrl.admin_q);
1605out_free_tagset:
1606 /* disconnect and drain the queue before freeing the tagset */
1607 nvme_rdma_stop_queue(&ctrl->queues[0]);
1608 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1609out_put_dev:
1610 nvme_rdma_dev_put(ctrl->device);
1611out_free_queue:
1612 nvme_rdma_free_queue(&ctrl->queues[0]);
1613 return error;
1614}
1615
1616static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1617{
Christoph Hellwig71102302016-07-06 21:55:52 +09001618 cancel_work_sync(&ctrl->err_work);
1619 cancel_delayed_work_sync(&ctrl->reconnect_work);
1620
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001621 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001622 nvme_stop_queues(&ctrl->ctrl);
1623 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1624 nvme_cancel_request, &ctrl->ctrl);
1625 nvme_rdma_free_io_queues(ctrl);
1626 }
1627
Sagi Grimbergb282a88d2017-05-04 13:33:11 +03001628 if (test_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags))
Christoph Hellwig71102302016-07-06 21:55:52 +09001629 nvme_shutdown_ctrl(&ctrl->ctrl);
1630
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001631 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001632 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1633 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001634 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001635 nvme_rdma_destroy_admin_queue(ctrl);
1636}
1637
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001638static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1639{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001640 nvme_stop_ctrl(&ctrl->ctrl);
1641 nvme_remove_namespaces(&ctrl->ctrl);
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001642 if (shutdown)
1643 nvme_rdma_shutdown_ctrl(ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001644
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001645 nvme_uninit_ctrl(&ctrl->ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001646 if (ctrl->ctrl.tagset) {
1647 blk_cleanup_queue(ctrl->ctrl.connect_q);
1648 blk_mq_free_tag_set(&ctrl->tag_set);
1649 nvme_rdma_dev_put(ctrl->device);
1650 }
1651
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001652 nvme_put_ctrl(&ctrl->ctrl);
1653}
1654
Christoph Hellwig71102302016-07-06 21:55:52 +09001655static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1656{
1657 struct nvme_rdma_ctrl *ctrl = container_of(work,
1658 struct nvme_rdma_ctrl, delete_work);
1659
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001660 __nvme_rdma_remove_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001661}
1662
1663static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1664{
1665 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1666 return -EBUSY;
1667
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001668 if (!queue_work(nvme_wq, &ctrl->delete_work))
Christoph Hellwig71102302016-07-06 21:55:52 +09001669 return -EBUSY;
1670
1671 return 0;
1672}
1673
1674static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1675{
1676 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001677 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001678
Steve Wisecdbecc82016-09-01 09:12:25 -07001679 /*
1680 * Keep a reference until all work is flushed since
1681 * __nvme_rdma_del_ctrl can free the ctrl mem
1682 */
1683 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1684 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001685 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001686 if (!ret)
1687 flush_work(&ctrl->delete_work);
1688 nvme_put_ctrl(&ctrl->ctrl);
1689 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001690}
1691
1692static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1693{
1694 struct nvme_rdma_ctrl *ctrl = container_of(work,
1695 struct nvme_rdma_ctrl, delete_work);
1696
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001697 __nvme_rdma_remove_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001698}
1699
1700static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1701{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001702 struct nvme_rdma_ctrl *ctrl =
1703 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001704 int ret;
1705 bool changed;
1706
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001707 nvme_stop_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001708 nvme_rdma_shutdown_ctrl(ctrl);
1709
1710 ret = nvme_rdma_configure_admin_queue(ctrl);
1711 if (ret) {
1712 /* ctrl is already shutdown, just remove the ctrl */
1713 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1714 goto del_dead_ctrl;
1715 }
1716
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001717 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001718 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1719 if (ret)
1720 goto del_dead_ctrl;
1721
1722 ret = nvme_rdma_init_io_queues(ctrl);
1723 if (ret)
1724 goto del_dead_ctrl;
1725
1726 ret = nvme_rdma_connect_io_queues(ctrl);
1727 if (ret)
1728 goto del_dead_ctrl;
Sagi Grimberg4c8b99f2017-06-29 11:10:44 +03001729
1730 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
1731 ctrl->ctrl.queue_count - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +09001732 }
1733
1734 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1735 WARN_ON_ONCE(!changed);
1736
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001737 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001738
1739 return;
1740
1741del_dead_ctrl:
1742 /* Deleting this dead controller... */
1743 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001744 WARN_ON(!queue_work(nvme_wq, &ctrl->delete_work));
Christoph Hellwig71102302016-07-06 21:55:52 +09001745}
1746
Christoph Hellwig71102302016-07-06 21:55:52 +09001747static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1748 .name = "rdma",
1749 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001750 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001751 .reg_read32 = nvmf_reg_read32,
1752 .reg_read64 = nvmf_reg_read64,
1753 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001754 .free_ctrl = nvme_rdma_free_ctrl,
1755 .submit_async_event = nvme_rdma_submit_async_event,
1756 .delete_ctrl = nvme_rdma_del_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001757 .get_address = nvmf_get_address,
1758};
1759
1760static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1761{
Christoph Hellwig71102302016-07-06 21:55:52 +09001762 int ret;
1763
Christoph Hellwig71102302016-07-06 21:55:52 +09001764 ret = nvme_rdma_init_io_queues(ctrl);
1765 if (ret)
1766 return ret;
1767
1768 /*
1769 * We need a reference on the device as long as the tag_set is alive,
1770 * as the MRs in the request structures need a valid ib_device.
1771 */
1772 ret = -EINVAL;
1773 if (!nvme_rdma_dev_get(ctrl->device))
1774 goto out_free_io_queues;
1775
1776 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1777 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001778 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
Christoph Hellwig71102302016-07-06 21:55:52 +09001779 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1780 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1781 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1782 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1783 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1784 ctrl->tag_set.driver_data = ctrl;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001785 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001786 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1787
1788 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1789 if (ret)
1790 goto out_put_dev;
1791 ctrl->ctrl.tagset = &ctrl->tag_set;
1792
1793 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1794 if (IS_ERR(ctrl->ctrl.connect_q)) {
1795 ret = PTR_ERR(ctrl->ctrl.connect_q);
1796 goto out_free_tag_set;
1797 }
1798
1799 ret = nvme_rdma_connect_io_queues(ctrl);
1800 if (ret)
1801 goto out_cleanup_connect_q;
1802
1803 return 0;
1804
1805out_cleanup_connect_q:
1806 blk_cleanup_queue(ctrl->ctrl.connect_q);
1807out_free_tag_set:
1808 blk_mq_free_tag_set(&ctrl->tag_set);
1809out_put_dev:
1810 nvme_rdma_dev_put(ctrl->device);
1811out_free_io_queues:
1812 nvme_rdma_free_io_queues(ctrl);
1813 return ret;
1814}
1815
Christoph Hellwig71102302016-07-06 21:55:52 +09001816static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1817 struct nvmf_ctrl_options *opts)
1818{
1819 struct nvme_rdma_ctrl *ctrl;
1820 int ret;
1821 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001822 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001823
1824 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1825 if (!ctrl)
1826 return ERR_PTR(-ENOMEM);
1827 ctrl->ctrl.opts = opts;
1828 INIT_LIST_HEAD(&ctrl->list);
1829
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001830 if (opts->mask & NVMF_OPT_TRSVCID)
1831 port = opts->trsvcid;
1832 else
1833 port = __stringify(NVME_RDMA_IP_PORT);
1834
1835 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1836 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001837 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001838 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001839 goto out_free_ctrl;
1840 }
1841
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001842 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001843 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1844 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001845 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001846 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001847 opts->host_traddr);
1848 goto out_free_ctrl;
1849 }
1850 }
1851
Christoph Hellwig71102302016-07-06 21:55:52 +09001852 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1853 0 /* no quirks, we're perfect! */);
1854 if (ret)
1855 goto out_free_ctrl;
1856
Christoph Hellwig71102302016-07-06 21:55:52 +09001857 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1858 nvme_rdma_reconnect_ctrl_work);
1859 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1860 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001861 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001862
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001863 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001864 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001865 ctrl->ctrl.kato = opts->kato;
1866
1867 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001868 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001869 GFP_KERNEL);
1870 if (!ctrl->queues)
1871 goto out_uninit_ctrl;
1872
1873 ret = nvme_rdma_configure_admin_queue(ctrl);
1874 if (ret)
1875 goto out_kfree_queues;
1876
1877 /* sanity check icdoff */
1878 if (ctrl->ctrl.icdoff) {
1879 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001880 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001881 goto out_remove_admin_queue;
1882 }
1883
1884 /* sanity check keyed sgls */
1885 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1886 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001887 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001888 goto out_remove_admin_queue;
1889 }
1890
1891 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1892 /* warn if maxcmd is lower than queue_size */
1893 dev_warn(ctrl->ctrl.device,
1894 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1895 opts->queue_size, ctrl->ctrl.maxcmd);
1896 opts->queue_size = ctrl->ctrl.maxcmd;
1897 }
1898
Samuel Jones76c08bf2016-10-25 09:22:34 +02001899 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1900 /* warn if sqsize is lower than queue_size */
1901 dev_warn(ctrl->ctrl.device,
1902 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1903 opts->queue_size, ctrl->ctrl.sqsize + 1);
1904 opts->queue_size = ctrl->ctrl.sqsize + 1;
1905 }
1906
Christoph Hellwig71102302016-07-06 21:55:52 +09001907 if (opts->nr_io_queues) {
1908 ret = nvme_rdma_create_io_queues(ctrl);
1909 if (ret)
1910 goto out_remove_admin_queue;
1911 }
1912
1913 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1914 WARN_ON_ONCE(!changed);
1915
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001916 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001917 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1918
1919 kref_get(&ctrl->ctrl.kref);
1920
1921 mutex_lock(&nvme_rdma_ctrl_mutex);
1922 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1923 mutex_unlock(&nvme_rdma_ctrl_mutex);
1924
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001925 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001926
1927 return &ctrl->ctrl;
1928
1929out_remove_admin_queue:
Christoph Hellwig71102302016-07-06 21:55:52 +09001930 nvme_rdma_destroy_admin_queue(ctrl);
1931out_kfree_queues:
1932 kfree(ctrl->queues);
1933out_uninit_ctrl:
1934 nvme_uninit_ctrl(&ctrl->ctrl);
1935 nvme_put_ctrl(&ctrl->ctrl);
1936 if (ret > 0)
1937 ret = -EIO;
1938 return ERR_PTR(ret);
1939out_free_ctrl:
1940 kfree(ctrl);
1941 return ERR_PTR(ret);
1942}
1943
1944static struct nvmf_transport_ops nvme_rdma_transport = {
1945 .name = "rdma",
1946 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001947 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001948 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001949 .create_ctrl = nvme_rdma_create_ctrl,
1950};
1951
Steve Wisee87a9112016-09-02 09:01:54 -07001952static void nvme_rdma_add_one(struct ib_device *ib_device)
1953{
1954}
1955
1956static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1957{
1958 struct nvme_rdma_ctrl *ctrl;
1959
1960 /* Delete all controllers using this device */
1961 mutex_lock(&nvme_rdma_ctrl_mutex);
1962 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1963 if (ctrl->device->dev != ib_device)
1964 continue;
1965 dev_info(ctrl->ctrl.device,
1966 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1967 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1968 __nvme_rdma_del_ctrl(ctrl);
1969 }
1970 mutex_unlock(&nvme_rdma_ctrl_mutex);
1971
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001972 flush_workqueue(nvme_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07001973}
1974
1975static struct ib_client nvme_rdma_ib_client = {
1976 .name = "nvme_rdma",
1977 .add = nvme_rdma_add_one,
1978 .remove = nvme_rdma_remove_one
1979};
1980
Christoph Hellwig71102302016-07-06 21:55:52 +09001981static int __init nvme_rdma_init_module(void)
1982{
Steve Wisee87a9112016-09-02 09:01:54 -07001983 int ret;
1984
Steve Wisee87a9112016-09-02 09:01:54 -07001985 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02001986 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001987 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07001988
Sagi Grimberga56c79c2017-03-19 06:21:42 +02001989 ret = nvmf_register_transport(&nvme_rdma_transport);
1990 if (ret)
1991 goto err_unreg_client;
1992
1993 return 0;
1994
1995err_unreg_client:
1996 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02001997 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001998}
1999
2000static void __exit nvme_rdma_cleanup_module(void)
2001{
Christoph Hellwig71102302016-07-06 21:55:52 +09002002 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002003 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002004}
2005
2006module_init(nvme_rdma_init_module);
2007module_exit(nvme_rdma_cleanup_module);
2008
2009MODULE_LICENSE("GPL v2");