blob: 3cecb087ee3a2cde0bfcc13029a1e1a2bf7c545b [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
Christoph Hellwig71102302016-07-06 21:55:52 +090039#define NVME_RDMA_MAX_SEGMENTS 256
40
41#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
42
Christoph Hellwig71102302016-07-06 21:55:52 +090043/*
44 * We handle AEN commands ourselves and don't even let the
45 * block layer know about them.
46 */
47#define NVME_RDMA_NR_AEN_COMMANDS 1
48#define NVME_RDMA_AQ_BLKMQ_DEPTH \
Sagi Grimberg7aa1f422017-06-18 16:15:59 +030049 (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
Christoph Hellwig71102302016-07-06 21:55:52 +090050
51struct nvme_rdma_device {
52 struct ib_device *dev;
53 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090054 struct kref ref;
55 struct list_head entry;
56};
57
58struct nvme_rdma_qe {
59 struct ib_cqe cqe;
60 void *data;
61 u64 dma;
62};
63
64struct nvme_rdma_queue;
65struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080066 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090067 struct ib_mr *mr;
68 struct nvme_rdma_qe sqe;
69 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
70 u32 num_sge;
71 int nents;
72 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090073 struct ib_reg_wr reg_wr;
74 struct ib_cqe reg_cqe;
75 struct nvme_rdma_queue *queue;
76 struct sg_table sg_table;
77 struct scatterlist first_sgl[];
78};
79
80enum nvme_rdma_queue_flags {
Sagi Grimbergb282a88d2017-05-04 13:33:11 +030081 NVME_RDMA_Q_LIVE = 0,
Sagi Grimbergabf87d52017-05-04 13:33:10 +030082 NVME_RDMA_Q_DELETING = 1,
Christoph Hellwig71102302016-07-06 21:55:52 +090083};
84
85struct nvme_rdma_queue {
86 struct nvme_rdma_qe *rsp_ring;
Marta Rybczynska5e599d72017-06-06 13:27:21 +020087 atomic_t sig_count;
Christoph Hellwig71102302016-07-06 21:55:52 +090088 int queue_size;
89 size_t cmnd_capsule_len;
90 struct nvme_rdma_ctrl *ctrl;
91 struct nvme_rdma_device *device;
92 struct ib_cq *ib_cq;
93 struct ib_qp *qp;
94
95 unsigned long flags;
96 struct rdma_cm_id *cm_id;
97 int cm_error;
98 struct completion cm_done;
99};
100
101struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +0900102 /* read only in the hot path */
103 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900104
105 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900106 struct blk_mq_tag_set tag_set;
107 struct work_struct delete_work;
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
Christoph Hellwig71102302016-07-06 21:55:52 +0900276 ib_dereg_mr(req->mr);
277
278 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
279 ctrl->max_fr_pages);
280 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900281 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000282 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100283 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900284 }
285
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300286 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900287
288out:
289 return ret;
290}
291
Christoph Hellwig385475e2017-06-13 09:15:19 +0200292static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
293 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900294{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200295 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900296 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200297 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900298 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
299 struct nvme_rdma_device *dev = queue->device;
300
301 if (req->mr)
302 ib_dereg_mr(req->mr);
303
304 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
305 DMA_TO_DEVICE);
306}
307
Christoph Hellwig385475e2017-06-13 09:15:19 +0200308static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
309 struct request *rq, unsigned int hctx_idx,
310 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900311{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200312 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900313 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200314 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900315 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
316 struct nvme_rdma_device *dev = queue->device;
317 struct ib_device *ibdev = dev->dev;
318 int ret;
319
Christoph Hellwig71102302016-07-06 21:55:52 +0900320 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
321 DMA_TO_DEVICE);
322 if (ret)
323 return ret;
324
325 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
326 ctrl->max_fr_pages);
327 if (IS_ERR(req->mr)) {
328 ret = PTR_ERR(req->mr);
329 goto out_free_qe;
330 }
331
332 req->queue = queue;
333
334 return 0;
335
336out_free_qe:
337 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
338 DMA_TO_DEVICE);
339 return -ENOMEM;
340}
341
Christoph Hellwig71102302016-07-06 21:55:52 +0900342static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
343 unsigned int hctx_idx)
344{
345 struct nvme_rdma_ctrl *ctrl = data;
346 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
347
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300348 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900349
350 hctx->driver_data = queue;
351 return 0;
352}
353
354static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
355 unsigned int hctx_idx)
356{
357 struct nvme_rdma_ctrl *ctrl = data;
358 struct nvme_rdma_queue *queue = &ctrl->queues[0];
359
360 BUG_ON(hctx_idx != 0);
361
362 hctx->driver_data = queue;
363 return 0;
364}
365
366static void nvme_rdma_free_dev(struct kref *ref)
367{
368 struct nvme_rdma_device *ndev =
369 container_of(ref, struct nvme_rdma_device, ref);
370
371 mutex_lock(&device_list_mutex);
372 list_del(&ndev->entry);
373 mutex_unlock(&device_list_mutex);
374
Christoph Hellwig71102302016-07-06 21:55:52 +0900375 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900376 kfree(ndev);
377}
378
379static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
380{
381 kref_put(&dev->ref, nvme_rdma_free_dev);
382}
383
384static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
385{
386 return kref_get_unless_zero(&dev->ref);
387}
388
389static struct nvme_rdma_device *
390nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
391{
392 struct nvme_rdma_device *ndev;
393
394 mutex_lock(&device_list_mutex);
395 list_for_each_entry(ndev, &device_list, entry) {
396 if (ndev->dev->node_guid == cm_id->device->node_guid &&
397 nvme_rdma_dev_get(ndev))
398 goto out_unlock;
399 }
400
401 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
402 if (!ndev)
403 goto out_err;
404
405 ndev->dev = cm_id->device;
406 kref_init(&ndev->ref);
407
Christoph Hellwig11975e02016-09-05 12:56:20 +0200408 ndev->pd = ib_alloc_pd(ndev->dev,
409 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900410 if (IS_ERR(ndev->pd))
411 goto out_free_dev;
412
Christoph Hellwig71102302016-07-06 21:55:52 +0900413 if (!(ndev->dev->attrs.device_cap_flags &
414 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
415 dev_err(&ndev->dev->dev,
416 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200417 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900418 }
419
420 list_add(&ndev->entry, &device_list);
421out_unlock:
422 mutex_unlock(&device_list_mutex);
423 return ndev;
424
Christoph Hellwig71102302016-07-06 21:55:52 +0900425out_free_pd:
426 ib_dealloc_pd(ndev->pd);
427out_free_dev:
428 kfree(ndev);
429out_err:
430 mutex_unlock(&device_list_mutex);
431 return NULL;
432}
433
434static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
435{
Steve Wisef361e5a2016-09-02 09:01:27 -0700436 struct nvme_rdma_device *dev;
437 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900438
Steve Wisef361e5a2016-09-02 09:01:27 -0700439 dev = queue->device;
440 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900441 rdma_destroy_qp(queue->cm_id);
442 ib_free_cq(queue->ib_cq);
443
444 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
445 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
446
447 nvme_rdma_dev_put(dev);
448}
449
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300450static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900451{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300452 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900453 const int send_wr_factor = 3; /* MR, SEND, INV */
454 const int cq_factor = send_wr_factor + 1; /* + RECV */
455 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900456 int ret;
457
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300458 queue->device = nvme_rdma_find_get_device(queue->cm_id);
459 if (!queue->device) {
460 dev_err(queue->cm_id->device->dev.parent,
461 "no client data found!\n");
462 return -ECONNREFUSED;
463 }
464 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900465
466 /*
467 * The admin queue is barely used once the controller is live, so don't
468 * bother to spread it out.
469 */
470 if (idx == 0)
471 comp_vector = 0;
472 else
473 comp_vector = idx % ibdev->num_comp_vectors;
474
475
476 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300477 queue->ib_cq = ib_alloc_cq(ibdev, queue,
478 cq_factor * queue->queue_size + 1,
479 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900480 if (IS_ERR(queue->ib_cq)) {
481 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300482 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900483 }
484
485 ret = nvme_rdma_create_qp(queue, send_wr_factor);
486 if (ret)
487 goto out_destroy_ib_cq;
488
489 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
490 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
491 if (!queue->rsp_ring) {
492 ret = -ENOMEM;
493 goto out_destroy_qp;
494 }
495
496 return 0;
497
498out_destroy_qp:
499 ib_destroy_qp(queue->qp);
500out_destroy_ib_cq:
501 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300502out_put_dev:
503 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900504 return ret;
505}
506
507static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
508 int idx, size_t queue_size)
509{
510 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200511 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900512 int ret;
513
514 queue = &ctrl->queues[idx];
515 queue->ctrl = ctrl;
516 init_completion(&queue->cm_done);
517
518 if (idx > 0)
519 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
520 else
521 queue->cmnd_capsule_len = sizeof(struct nvme_command);
522
523 queue->queue_size = queue_size;
Marta Rybczynska5e599d72017-06-06 13:27:21 +0200524 atomic_set(&queue->sig_count, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900525
526 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
527 RDMA_PS_TCP, IB_QPT_RC);
528 if (IS_ERR(queue->cm_id)) {
529 dev_info(ctrl->ctrl.device,
530 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
531 return PTR_ERR(queue->cm_id);
532 }
533
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200534 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200535 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200536
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200537 queue->cm_error = -ETIMEDOUT;
538 ret = rdma_resolve_addr(queue->cm_id, src_addr,
539 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900540 NVME_RDMA_CONNECT_TIMEOUT_MS);
541 if (ret) {
542 dev_info(ctrl->ctrl.device,
543 "rdma_resolve_addr failed (%d).\n", ret);
544 goto out_destroy_cm_id;
545 }
546
547 ret = nvme_rdma_wait_for_cm(queue);
548 if (ret) {
549 dev_info(ctrl->ctrl.device,
550 "rdma_resolve_addr wait failed (%d).\n", ret);
551 goto out_destroy_cm_id;
552 }
553
Sagi Grimberg3b4ac782016-09-22 19:58:17 -0600554 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900555
556 return 0;
557
558out_destroy_cm_id:
559 rdma_destroy_id(queue->cm_id);
560 return ret;
561}
562
563static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
564{
565 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{
571 nvme_rdma_destroy_queue_ib(queue);
572 rdma_destroy_id(queue->cm_id);
573}
574
575static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
576{
Sagi Grimberge89ca582016-09-02 09:01:54 -0700577 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900578 return;
579 nvme_rdma_stop_queue(queue);
580 nvme_rdma_free_queue(queue);
581}
582
583static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
584{
585 int i;
586
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300587 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Christoph Hellwig71102302016-07-06 21:55:52 +0900588 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
589}
590
591static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
592{
593 int i, ret = 0;
594
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300595 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900596 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
Steve Wisec8dbc372016-11-08 09:16:02 -0800597 if (ret) {
598 dev_info(ctrl->ctrl.device,
599 "failed to connect i/o queue: %d\n", ret);
600 goto out_free_queues;
601 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600602 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900603 }
604
Steve Wisec8dbc372016-11-08 09:16:02 -0800605 return 0;
606
607out_free_queues:
608 nvme_rdma_free_io_queues(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900609 return ret;
610}
611
612static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
613{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200614 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
615 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900616 int i, ret;
617
Sagi Grimbergc248c642017-03-09 13:26:07 +0200618 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
619 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
620 if (ret)
621 return ret;
622
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300623 ctrl->ctrl.queue_count = nr_io_queues + 1;
624 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200625 return 0;
626
627 dev_info(ctrl->ctrl.device,
628 "creating %d I/O queues.\n", nr_io_queues);
629
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300630 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Jay Freyenseec5af8652016-08-17 15:00:27 -0700631 ret = nvme_rdma_init_queue(ctrl, i,
632 ctrl->ctrl.opts->queue_size);
Christoph Hellwig71102302016-07-06 21:55:52 +0900633 if (ret) {
634 dev_info(ctrl->ctrl.device,
635 "failed to initialize i/o queue: %d\n", ret);
636 goto out_free_queues;
637 }
638 }
639
640 return 0;
641
642out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700643 for (i--; i >= 1; i--)
Christoph Hellwig71102302016-07-06 21:55:52 +0900644 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
645
646 return ret;
647}
648
649static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
650{
651 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
652 sizeof(struct nvme_command), DMA_TO_DEVICE);
653 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
654 blk_cleanup_queue(ctrl->ctrl.admin_q);
655 blk_mq_free_tag_set(&ctrl->admin_tag_set);
656 nvme_rdma_dev_put(ctrl->device);
657}
658
Sagi Grimberg90af3512017-07-10 09:22:28 +0300659static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
660{
661 int error;
662
663 error = nvme_rdma_init_queue(ctrl, 0, NVME_AQ_DEPTH);
664 if (error)
665 return error;
666
667 ctrl->device = ctrl->queues[0].device;
668
669 /*
670 * We need a reference on the device as long as the tag_set is alive,
671 * as the MRs in the request structures need a valid ib_device.
672 */
673 error = -EINVAL;
674 if (!nvme_rdma_dev_get(ctrl->device))
675 goto out_free_queue;
676
677 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
678 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
679
680 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
681 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
682 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
683 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
684 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
685 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
686 SG_CHUNK_SIZE * sizeof(struct scatterlist);
687 ctrl->admin_tag_set.driver_data = ctrl;
688 ctrl->admin_tag_set.nr_hw_queues = 1;
689 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
690
691 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
692 if (error)
693 goto out_put_dev;
694
695 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
696 if (IS_ERR(ctrl->ctrl.admin_q)) {
697 error = PTR_ERR(ctrl->ctrl.admin_q);
698 goto out_free_tagset;
699 }
700
701 error = nvmf_connect_admin_queue(&ctrl->ctrl);
702 if (error)
703 goto out_cleanup_queue;
704
705 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
706
707 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP,
708 &ctrl->ctrl.cap);
709 if (error) {
710 dev_err(ctrl->ctrl.device,
711 "prop_get NVME_REG_CAP failed\n");
712 goto out_cleanup_queue;
713 }
714
715 ctrl->ctrl.sqsize =
716 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
717
718 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
719 if (error)
720 goto out_cleanup_queue;
721
722 ctrl->ctrl.max_hw_sectors =
723 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
724
725 error = nvme_init_identify(&ctrl->ctrl);
726 if (error)
727 goto out_cleanup_queue;
728
729 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
730 &ctrl->async_event_sqe, sizeof(struct nvme_command),
731 DMA_TO_DEVICE);
732 if (error)
733 goto out_cleanup_queue;
734
735 return 0;
736
737out_cleanup_queue:
738 blk_cleanup_queue(ctrl->ctrl.admin_q);
739out_free_tagset:
740 /* disconnect and drain the queue before freeing the tagset */
741 nvme_rdma_stop_queue(&ctrl->queues[0]);
742 blk_mq_free_tag_set(&ctrl->admin_tag_set);
743out_put_dev:
744 nvme_rdma_dev_put(ctrl->device);
745out_free_queue:
746 nvme_rdma_free_queue(&ctrl->queues[0]);
747 return error;
748}
749
Christoph Hellwig71102302016-07-06 21:55:52 +0900750static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
751{
752 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
753
754 if (list_empty(&ctrl->list))
755 goto free_ctrl;
756
757 mutex_lock(&nvme_rdma_ctrl_mutex);
758 list_del(&ctrl->list);
759 mutex_unlock(&nvme_rdma_ctrl_mutex);
760
Christoph Hellwig71102302016-07-06 21:55:52 +0900761 kfree(ctrl->queues);
762 nvmf_free_options(nctrl->opts);
763free_ctrl:
764 kfree(ctrl);
765}
766
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200767static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
768{
769 /* If we are resetting/deleting then do nothing */
770 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
771 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
772 ctrl->ctrl.state == NVME_CTRL_LIVE);
773 return;
774 }
775
776 if (nvmf_should_reconnect(&ctrl->ctrl)) {
777 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
778 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200779 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200780 ctrl->ctrl.opts->reconnect_delay * HZ);
781 } else {
782 dev_info(ctrl->ctrl.device, "Removing controller...\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200783 queue_work(nvme_wq, &ctrl->delete_work);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200784 }
785}
786
Christoph Hellwig71102302016-07-06 21:55:52 +0900787static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
788{
789 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
790 struct nvme_rdma_ctrl, reconnect_work);
791 bool changed;
792 int ret;
793
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300794 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200795
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300796 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900797 nvme_rdma_free_io_queues(ctrl);
798
Bart Van Assched352ae22017-08-17 16:23:03 -0700799 ret = blk_mq_reinit_tagset(&ctrl->tag_set,
800 nvme_rdma_reinit_request);
Christoph Hellwig71102302016-07-06 21:55:52 +0900801 if (ret)
802 goto requeue;
803 }
804
805 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
806
Bart Van Assched352ae22017-08-17 16:23:03 -0700807 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set,
808 nvme_rdma_reinit_request);
Christoph Hellwig71102302016-07-06 21:55:52 +0900809 if (ret)
810 goto requeue;
811
Sagi Grimberg7aa1f422017-06-18 16:15:59 +0300812 ret = nvme_rdma_init_queue(ctrl, 0, NVME_AQ_DEPTH);
Christoph Hellwig71102302016-07-06 21:55:52 +0900813 if (ret)
814 goto requeue;
815
Christoph Hellwig71102302016-07-06 21:55:52 +0900816 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
817 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300818 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900819
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600820 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
821
Sagi Grimberg20d0dfe2017-06-27 22:16:38 +0300822 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +0900823 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300824 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900825
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300826 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900827 ret = nvme_rdma_init_io_queues(ctrl);
828 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300829 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900830
831 ret = nvme_rdma_connect_io_queues(ctrl);
832 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300833 goto requeue;
Sagi Grimberg4c8b99f2017-06-29 11:10:44 +0300834
835 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
836 ctrl->ctrl.queue_count - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900837 }
838
839 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
840 WARN_ON_ONCE(!changed);
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300841 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900842
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300843 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900844
845 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
846
847 return;
848
Christoph Hellwig71102302016-07-06 21:55:52 +0900849requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200850 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300851 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200852 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900853}
854
855static void nvme_rdma_error_recovery_work(struct work_struct *work)
856{
857 struct nvme_rdma_ctrl *ctrl = container_of(work,
858 struct nvme_rdma_ctrl, err_work);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700859 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +0900860
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300861 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700862
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300863 for (i = 0; i < ctrl->ctrl.queue_count; i++)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600864 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700865
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300866 if (ctrl->ctrl.queue_count > 1)
Christoph Hellwig71102302016-07-06 21:55:52 +0900867 nvme_stop_queues(&ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300868 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +0900869
870 /* We must take care of fastfail/requeue all our inflight requests */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300871 if (ctrl->ctrl.queue_count > 1)
Christoph Hellwig71102302016-07-06 21:55:52 +0900872 blk_mq_tagset_busy_iter(&ctrl->tag_set,
873 nvme_cancel_request, &ctrl->ctrl);
874 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
875 nvme_cancel_request, &ctrl->ctrl);
876
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300877 /*
878 * queues are not a live anymore, so restart the queues to fail fast
879 * new IO
880 */
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300881 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300882 nvme_start_queues(&ctrl->ctrl);
883
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200884 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900885}
886
887static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
888{
889 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
890 return;
891
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200892 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +0900893}
894
895static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
896 const char *op)
897{
898 struct nvme_rdma_queue *queue = cq->cq_context;
899 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
900
901 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
902 dev_info(ctrl->ctrl.device,
903 "%s for CQE 0x%p failed with status %s (%d)\n",
904 op, wc->wr_cqe,
905 ib_wc_status_msg(wc->status), wc->status);
906 nvme_rdma_error_recovery(ctrl);
907}
908
909static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
910{
911 if (unlikely(wc->status != IB_WC_SUCCESS))
912 nvme_rdma_wr_error(cq, wc, "MEMREG");
913}
914
915static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
916{
917 if (unlikely(wc->status != IB_WC_SUCCESS))
918 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
919}
920
921static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
922 struct nvme_rdma_request *req)
923{
924 struct ib_send_wr *bad_wr;
925 struct ib_send_wr wr = {
926 .opcode = IB_WR_LOCAL_INV,
927 .next = NULL,
928 .num_sge = 0,
929 .send_flags = 0,
930 .ex.invalidate_rkey = req->mr->rkey,
931 };
932
933 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
934 wr.wr_cqe = &req->reg_cqe;
935
936 return ib_post_send(queue->qp, &wr, &bad_wr);
937}
938
939static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
940 struct request *rq)
941{
942 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
943 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
944 struct nvme_rdma_device *dev = queue->device;
945 struct ib_device *ibdev = dev->dev;
946 int res;
947
948 if (!blk_rq_bytes(rq))
949 return;
950
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300951 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900952 res = nvme_rdma_inv_rkey(queue, req);
953 if (res < 0) {
954 dev_err(ctrl->ctrl.device,
955 "Queueing INV WR for rkey %#x failed (%d)\n",
956 req->mr->rkey, res);
957 nvme_rdma_error_recovery(queue->ctrl);
958 }
959 }
960
961 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
962 req->nents, rq_data_dir(rq) ==
963 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
964
965 nvme_cleanup_cmd(rq);
966 sg_free_table_chained(&req->sg_table, true);
967}
968
969static int nvme_rdma_set_sg_null(struct nvme_command *c)
970{
971 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
972
973 sg->addr = 0;
974 put_unaligned_le24(0, sg->length);
975 put_unaligned_le32(0, sg->key);
976 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
977 return 0;
978}
979
980static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
981 struct nvme_rdma_request *req, struct nvme_command *c)
982{
983 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
984
985 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
986 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
987 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
988
989 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
990 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
991 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
992
993 req->inline_data = true;
994 req->num_sge++;
995 return 0;
996}
997
998static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
999 struct nvme_rdma_request *req, struct nvme_command *c)
1000{
1001 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1002
1003 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1004 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001005 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001006 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1007 return 0;
1008}
1009
1010static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1011 struct nvme_rdma_request *req, struct nvme_command *c,
1012 int count)
1013{
1014 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1015 int nr;
1016
1017 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
1018 if (nr < count) {
1019 if (nr < 0)
1020 return nr;
1021 return -EINVAL;
1022 }
1023
1024 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1025
1026 req->reg_cqe.done = nvme_rdma_memreg_done;
1027 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1028 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1029 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1030 req->reg_wr.wr.num_sge = 0;
1031 req->reg_wr.mr = req->mr;
1032 req->reg_wr.key = req->mr->rkey;
1033 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1034 IB_ACCESS_REMOTE_READ |
1035 IB_ACCESS_REMOTE_WRITE;
1036
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001037 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001038
1039 sg->addr = cpu_to_le64(req->mr->iova);
1040 put_unaligned_le24(req->mr->length, sg->length);
1041 put_unaligned_le32(req->mr->rkey, sg->key);
1042 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1043 NVME_SGL_FMT_INVALIDATE;
1044
1045 return 0;
1046}
1047
1048static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001049 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001050{
1051 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1052 struct nvme_rdma_device *dev = queue->device;
1053 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001054 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001055
1056 req->num_sge = 1;
1057 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001058 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001059
1060 c->common.flags |= NVME_CMD_SGL_METABUF;
1061
1062 if (!blk_rq_bytes(rq))
1063 return nvme_rdma_set_sg_null(c);
1064
1065 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001066 ret = sg_alloc_table_chained(&req->sg_table,
1067 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001068 if (ret)
1069 return -ENOMEM;
1070
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001071 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001072
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001073 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001074 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1075 if (unlikely(count <= 0)) {
1076 sg_free_table_chained(&req->sg_table, true);
1077 return -EIO;
1078 }
1079
1080 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001081 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1082 blk_rq_payload_bytes(rq) <=
1083 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001084 return nvme_rdma_map_sg_inline(queue, req, c);
1085
Christoph Hellwig11975e02016-09-05 12:56:20 +02001086 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001087 return nvme_rdma_map_sg_single(queue, req, c);
1088 }
1089
1090 return nvme_rdma_map_sg_fr(queue, req, c, count);
1091}
1092
1093static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1094{
1095 if (unlikely(wc->status != IB_WC_SUCCESS))
1096 nvme_rdma_wr_error(cq, wc, "SEND");
1097}
1098
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001099/*
1100 * We want to signal completion at least every queue depth/2. This returns the
1101 * largest power of two that is not above half of (queue size + 1) to optimize
1102 * (avoid divisions).
1103 */
1104static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
Marta Rybczynska0544f542017-04-10 17:12:34 +02001105{
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001106 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
Marta Rybczynska0544f542017-04-10 17:12:34 +02001107
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001108 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
Marta Rybczynska0544f542017-04-10 17:12:34 +02001109}
1110
Christoph Hellwig71102302016-07-06 21:55:52 +09001111static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1112 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1113 struct ib_send_wr *first, bool flush)
1114{
1115 struct ib_send_wr wr, *bad_wr;
1116 int ret;
1117
1118 sge->addr = qe->dma;
1119 sge->length = sizeof(struct nvme_command),
1120 sge->lkey = queue->device->pd->local_dma_lkey;
1121
1122 qe->cqe.done = nvme_rdma_send_done;
1123
1124 wr.next = NULL;
1125 wr.wr_cqe = &qe->cqe;
1126 wr.sg_list = sge;
1127 wr.num_sge = num_sge;
1128 wr.opcode = IB_WR_SEND;
1129 wr.send_flags = 0;
1130
1131 /*
1132 * Unsignalled send completions are another giant desaster in the
1133 * IB Verbs spec: If we don't regularly post signalled sends
1134 * the send queue will fill up and only a QP reset will rescue us.
1135 * Would have been way to obvious to handle this in hardware or
1136 * at least the RDMA stack..
1137 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001138 * Always signal the flushes. The magic request used for the flush
1139 * sequencer is not allocated in our driver's tagset and it's
1140 * triggered to be freed by blk_cleanup_queue(). So we need to
1141 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001142 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001143 * calls wr_cqe->done().
1144 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001145 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001146 wr.send_flags |= IB_SEND_SIGNALED;
1147
1148 if (first)
1149 first->next = &wr;
1150 else
1151 first = &wr;
1152
1153 ret = ib_post_send(queue->qp, first, &bad_wr);
1154 if (ret) {
1155 dev_err(queue->ctrl->ctrl.device,
1156 "%s failed with error code %d\n", __func__, ret);
1157 }
1158 return ret;
1159}
1160
1161static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1162 struct nvme_rdma_qe *qe)
1163{
1164 struct ib_recv_wr wr, *bad_wr;
1165 struct ib_sge list;
1166 int ret;
1167
1168 list.addr = qe->dma;
1169 list.length = sizeof(struct nvme_completion);
1170 list.lkey = queue->device->pd->local_dma_lkey;
1171
1172 qe->cqe.done = nvme_rdma_recv_done;
1173
1174 wr.next = NULL;
1175 wr.wr_cqe = &qe->cqe;
1176 wr.sg_list = &list;
1177 wr.num_sge = 1;
1178
1179 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1180 if (ret) {
1181 dev_err(queue->ctrl->ctrl.device,
1182 "%s failed with error code %d\n", __func__, ret);
1183 }
1184 return ret;
1185}
1186
1187static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1188{
1189 u32 queue_idx = nvme_rdma_queue_idx(queue);
1190
1191 if (queue_idx == 0)
1192 return queue->ctrl->admin_tag_set.tags[queue_idx];
1193 return queue->ctrl->tag_set.tags[queue_idx - 1];
1194}
1195
1196static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1197{
1198 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1199 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1200 struct ib_device *dev = queue->device->dev;
1201 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1202 struct nvme_command *cmd = sqe->data;
1203 struct ib_sge sge;
1204 int ret;
1205
1206 if (WARN_ON_ONCE(aer_idx != 0))
1207 return;
1208
1209 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1210
1211 memset(cmd, 0, sizeof(*cmd));
1212 cmd->common.opcode = nvme_admin_async_event;
1213 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1214 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1215 nvme_rdma_set_sg_null(cmd);
1216
1217 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1218 DMA_TO_DEVICE);
1219
1220 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1221 WARN_ON_ONCE(ret);
1222}
1223
1224static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1225 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1226{
Christoph Hellwig71102302016-07-06 21:55:52 +09001227 struct request *rq;
1228 struct nvme_rdma_request *req;
1229 int ret = 0;
1230
Christoph Hellwig71102302016-07-06 21:55:52 +09001231 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1232 if (!rq) {
1233 dev_err(queue->ctrl->ctrl.device,
1234 "tag 0x%x on QP %#x not found\n",
1235 cqe->command_id, queue->qp->qp_num);
1236 nvme_rdma_error_recovery(queue->ctrl);
1237 return ret;
1238 }
1239 req = blk_mq_rq_to_pdu(rq);
1240
Christoph Hellwig71102302016-07-06 21:55:52 +09001241 if (rq->tag == tag)
1242 ret = 1;
1243
1244 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1245 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001246 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001247
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001248 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001249 return ret;
1250}
1251
1252static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1253{
1254 struct nvme_rdma_qe *qe =
1255 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1256 struct nvme_rdma_queue *queue = cq->cq_context;
1257 struct ib_device *ibdev = queue->device->dev;
1258 struct nvme_completion *cqe = qe->data;
1259 const size_t len = sizeof(struct nvme_completion);
1260 int ret = 0;
1261
1262 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1263 nvme_rdma_wr_error(cq, wc, "RECV");
1264 return 0;
1265 }
1266
1267 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1268 /*
1269 * AEN requests are special as they don't time out and can
1270 * survive any kind of queue freeze and often don't respond to
1271 * aborts. We don't even bother to allocate a struct request
1272 * for them but rather special case them here.
1273 */
1274 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1275 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001276 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1277 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001278 else
1279 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1280 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1281
1282 nvme_rdma_post_recv(queue, qe);
1283 return ret;
1284}
1285
1286static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1287{
1288 __nvme_rdma_recv_done(cq, wc, -1);
1289}
1290
1291static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1292{
1293 int ret, i;
1294
1295 for (i = 0; i < queue->queue_size; i++) {
1296 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1297 if (ret)
1298 goto out_destroy_queue_ib;
1299 }
1300
1301 return 0;
1302
1303out_destroy_queue_ib:
1304 nvme_rdma_destroy_queue_ib(queue);
1305 return ret;
1306}
1307
1308static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1309 struct rdma_cm_event *ev)
1310{
Steve Wise7f039532016-10-26 12:36:47 -07001311 struct rdma_cm_id *cm_id = queue->cm_id;
1312 int status = ev->status;
1313 const char *rej_msg;
1314 const struct nvme_rdma_cm_rej *rej_data;
1315 u8 rej_data_len;
1316
1317 rej_msg = rdma_reject_msg(cm_id, status);
1318 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1319
1320 if (rej_data && rej_data_len >= sizeof(u16)) {
1321 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001322
1323 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001324 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1325 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001326 } else {
1327 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001328 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001329 }
1330
1331 return -ECONNRESET;
1332}
1333
1334static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1335{
Christoph Hellwig71102302016-07-06 21:55:52 +09001336 int ret;
1337
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001338 ret = nvme_rdma_create_queue_ib(queue);
1339 if (ret)
1340 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001341
1342 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1343 if (ret) {
1344 dev_err(queue->ctrl->ctrl.device,
1345 "rdma_resolve_route failed (%d).\n",
1346 queue->cm_error);
1347 goto out_destroy_queue;
1348 }
1349
1350 return 0;
1351
1352out_destroy_queue:
1353 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001354 return ret;
1355}
1356
1357static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1358{
1359 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1360 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001361 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001362 int ret;
1363
1364 param.qp_num = queue->qp->qp_num;
1365 param.flow_control = 1;
1366
1367 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001368 /* maximum retry count */
1369 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001370 param.rnr_retry_count = 7;
1371 param.private_data = &priv;
1372 param.private_data_len = sizeof(priv);
1373
1374 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1375 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001376 /*
1377 * set the admin queue depth to the minimum size
1378 * specified by the Fabrics standard.
1379 */
1380 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001381 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1382 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001383 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001384 /*
1385 * current interpretation of the fabrics spec
1386 * is at minimum you make hrqsize sqsize+1, or a
1387 * 1's based representation of sqsize.
1388 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001389 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001390 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001391 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001392
1393 ret = rdma_connect(queue->cm_id, &param);
1394 if (ret) {
1395 dev_err(ctrl->ctrl.device,
1396 "rdma_connect failed (%d).\n", ret);
1397 goto out_destroy_queue_ib;
1398 }
1399
1400 return 0;
1401
1402out_destroy_queue_ib:
1403 nvme_rdma_destroy_queue_ib(queue);
1404 return ret;
1405}
1406
Christoph Hellwig71102302016-07-06 21:55:52 +09001407static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1408 struct rdma_cm_event *ev)
1409{
1410 struct nvme_rdma_queue *queue = cm_id->context;
1411 int cm_error = 0;
1412
1413 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1414 rdma_event_msg(ev->event), ev->event,
1415 ev->status, cm_id);
1416
1417 switch (ev->event) {
1418 case RDMA_CM_EVENT_ADDR_RESOLVED:
1419 cm_error = nvme_rdma_addr_resolved(queue);
1420 break;
1421 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1422 cm_error = nvme_rdma_route_resolved(queue);
1423 break;
1424 case RDMA_CM_EVENT_ESTABLISHED:
1425 queue->cm_error = nvme_rdma_conn_established(queue);
1426 /* complete cm_done regardless of success/failure */
1427 complete(&queue->cm_done);
1428 return 0;
1429 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001430 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001431 cm_error = nvme_rdma_conn_rejected(queue, ev);
1432 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001433 case RDMA_CM_EVENT_ROUTE_ERROR:
1434 case RDMA_CM_EVENT_CONNECT_ERROR:
1435 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001436 nvme_rdma_destroy_queue_ib(queue);
1437 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001438 dev_dbg(queue->ctrl->ctrl.device,
1439 "CM error event %d\n", ev->event);
1440 cm_error = -ECONNRESET;
1441 break;
1442 case RDMA_CM_EVENT_DISCONNECTED:
1443 case RDMA_CM_EVENT_ADDR_CHANGE:
1444 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1445 dev_dbg(queue->ctrl->ctrl.device,
1446 "disconnect received - connection closed\n");
1447 nvme_rdma_error_recovery(queue->ctrl);
1448 break;
1449 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001450 /* device removal is handled via the ib_client API */
1451 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001452 default:
1453 dev_err(queue->ctrl->ctrl.device,
1454 "Unexpected RDMA CM event (%d)\n", ev->event);
1455 nvme_rdma_error_recovery(queue->ctrl);
1456 break;
1457 }
1458
1459 if (cm_error) {
1460 queue->cm_error = cm_error;
1461 complete(&queue->cm_done);
1462 }
1463
1464 return 0;
1465}
1466
1467static enum blk_eh_timer_return
1468nvme_rdma_timeout(struct request *rq, bool reserved)
1469{
1470 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1471
1472 /* queue error recovery */
1473 nvme_rdma_error_recovery(req->queue->ctrl);
1474
1475 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001476 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001477
1478 return BLK_EH_HANDLED;
1479}
1480
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001481/*
1482 * We cannot accept any other command until the Connect command has completed.
1483 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001484static inline blk_status_t
1485nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001486{
1487 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001488 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001489
Christoph Hellwig57292b52017-01-31 16:57:29 +01001490 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001491 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001492 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1493 /*
1494 * reconnecting state means transport disruption, which
1495 * can take a long time and even might fail permanently,
1496 * so we can't let incoming I/O be requeued forever.
1497 * fail it fast to allow upper layers a chance to
1498 * failover.
1499 */
1500 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001501 return BLK_STS_IOERR;
1502 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001503 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001504 }
1505
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001506 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001507}
1508
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001509static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001510 const struct blk_mq_queue_data *bd)
1511{
1512 struct nvme_ns *ns = hctx->queue->queuedata;
1513 struct nvme_rdma_queue *queue = hctx->driver_data;
1514 struct request *rq = bd->rq;
1515 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1516 struct nvme_rdma_qe *sqe = &req->sqe;
1517 struct nvme_command *c = sqe->data;
1518 bool flush = false;
1519 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001520 blk_status_t ret;
1521 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001522
1523 WARN_ON_ONCE(rq->tag < 0);
1524
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001525 ret = nvme_rdma_queue_is_ready(queue, rq);
1526 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001527 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001528
Christoph Hellwig71102302016-07-06 21:55:52 +09001529 dev = queue->device->dev;
1530 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1531 sizeof(struct nvme_command), DMA_TO_DEVICE);
1532
1533 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001534 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001535 return ret;
1536
Christoph Hellwig71102302016-07-06 21:55:52 +09001537 blk_mq_start_request(rq);
1538
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001539 err = nvme_rdma_map_data(queue, rq, c);
1540 if (err < 0) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001541 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001542 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001543 nvme_cleanup_cmd(rq);
1544 goto err;
1545 }
1546
1547 ib_dma_sync_single_for_device(dev, sqe->dma,
1548 sizeof(struct nvme_command), DMA_TO_DEVICE);
1549
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001550 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001551 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001552 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001553 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001554 if (err) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001555 nvme_rdma_unmap_data(queue, rq);
1556 goto err;
1557 }
1558
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001559 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001560err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001561 if (err == -ENOMEM || err == -EAGAIN)
1562 return BLK_STS_RESOURCE;
1563 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001564}
1565
1566static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1567{
1568 struct nvme_rdma_queue *queue = hctx->driver_data;
1569 struct ib_cq *cq = queue->ib_cq;
1570 struct ib_wc wc;
1571 int found = 0;
1572
Christoph Hellwig71102302016-07-06 21:55:52 +09001573 while (ib_poll_cq(cq, 1, &wc) > 0) {
1574 struct ib_cqe *cqe = wc.wr_cqe;
1575
1576 if (cqe) {
1577 if (cqe->done == nvme_rdma_recv_done)
1578 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1579 else
1580 cqe->done(cq, &wc);
1581 }
1582 }
1583
1584 return found;
1585}
1586
1587static void nvme_rdma_complete_rq(struct request *rq)
1588{
1589 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001590
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001591 nvme_rdma_unmap_data(req->queue, rq);
1592 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001593}
1594
Eric Biggersf363b082017-03-30 13:39:16 -07001595static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001596 .queue_rq = nvme_rdma_queue_rq,
1597 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001598 .init_request = nvme_rdma_init_request,
1599 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001600 .init_hctx = nvme_rdma_init_hctx,
1601 .poll = nvme_rdma_poll,
1602 .timeout = nvme_rdma_timeout,
1603};
1604
Eric Biggersf363b082017-03-30 13:39:16 -07001605static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001606 .queue_rq = nvme_rdma_queue_rq,
1607 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001608 .init_request = nvme_rdma_init_request,
1609 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001610 .init_hctx = nvme_rdma_init_admin_hctx,
1611 .timeout = nvme_rdma_timeout,
1612};
1613
Christoph Hellwig71102302016-07-06 21:55:52 +09001614static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1615{
Christoph Hellwig71102302016-07-06 21:55:52 +09001616 cancel_work_sync(&ctrl->err_work);
1617 cancel_delayed_work_sync(&ctrl->reconnect_work);
1618
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001619 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001620 nvme_stop_queues(&ctrl->ctrl);
1621 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1622 nvme_cancel_request, &ctrl->ctrl);
1623 nvme_rdma_free_io_queues(ctrl);
1624 }
1625
Sagi Grimbergb282a88d2017-05-04 13:33:11 +03001626 if (test_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags))
Christoph Hellwig71102302016-07-06 21:55:52 +09001627 nvme_shutdown_ctrl(&ctrl->ctrl);
1628
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001629 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001630 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1631 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001632 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001633 nvme_rdma_destroy_admin_queue(ctrl);
1634}
1635
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001636static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1637{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001638 nvme_stop_ctrl(&ctrl->ctrl);
1639 nvme_remove_namespaces(&ctrl->ctrl);
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001640 if (shutdown)
1641 nvme_rdma_shutdown_ctrl(ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001642
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001643 nvme_uninit_ctrl(&ctrl->ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001644 if (ctrl->ctrl.tagset) {
1645 blk_cleanup_queue(ctrl->ctrl.connect_q);
1646 blk_mq_free_tag_set(&ctrl->tag_set);
1647 nvme_rdma_dev_put(ctrl->device);
1648 }
1649
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001650 nvme_put_ctrl(&ctrl->ctrl);
1651}
1652
Christoph Hellwig71102302016-07-06 21:55:52 +09001653static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1654{
1655 struct nvme_rdma_ctrl *ctrl = container_of(work,
1656 struct nvme_rdma_ctrl, delete_work);
1657
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001658 __nvme_rdma_remove_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001659}
1660
1661static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1662{
1663 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1664 return -EBUSY;
1665
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001666 if (!queue_work(nvme_wq, &ctrl->delete_work))
Christoph Hellwig71102302016-07-06 21:55:52 +09001667 return -EBUSY;
1668
1669 return 0;
1670}
1671
1672static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1673{
1674 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001675 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001676
Steve Wisecdbecc82016-09-01 09:12:25 -07001677 /*
1678 * Keep a reference until all work is flushed since
1679 * __nvme_rdma_del_ctrl can free the ctrl mem
1680 */
1681 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1682 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001683 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001684 if (!ret)
1685 flush_work(&ctrl->delete_work);
1686 nvme_put_ctrl(&ctrl->ctrl);
1687 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001688}
1689
1690static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1691{
1692 struct nvme_rdma_ctrl *ctrl = container_of(work,
1693 struct nvme_rdma_ctrl, delete_work);
1694
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001695 __nvme_rdma_remove_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001696}
1697
1698static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1699{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001700 struct nvme_rdma_ctrl *ctrl =
1701 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001702 int ret;
1703 bool changed;
1704
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001705 nvme_stop_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001706 nvme_rdma_shutdown_ctrl(ctrl);
1707
1708 ret = nvme_rdma_configure_admin_queue(ctrl);
1709 if (ret) {
1710 /* ctrl is already shutdown, just remove the ctrl */
1711 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1712 goto del_dead_ctrl;
1713 }
1714
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001715 if (ctrl->ctrl.queue_count > 1) {
Bart Van Assched352ae22017-08-17 16:23:03 -07001716 ret = blk_mq_reinit_tagset(&ctrl->tag_set,
1717 nvme_rdma_reinit_request);
Christoph Hellwig71102302016-07-06 21:55:52 +09001718 if (ret)
1719 goto del_dead_ctrl;
1720
1721 ret = nvme_rdma_init_io_queues(ctrl);
1722 if (ret)
1723 goto del_dead_ctrl;
1724
1725 ret = nvme_rdma_connect_io_queues(ctrl);
1726 if (ret)
1727 goto del_dead_ctrl;
Sagi Grimberg4c8b99f2017-06-29 11:10:44 +03001728
1729 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
1730 ctrl->ctrl.queue_count - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +09001731 }
1732
1733 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1734 WARN_ON_ONCE(!changed);
1735
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001736 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001737
1738 return;
1739
1740del_dead_ctrl:
1741 /* Deleting this dead controller... */
1742 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001743 WARN_ON(!queue_work(nvme_wq, &ctrl->delete_work));
Christoph Hellwig71102302016-07-06 21:55:52 +09001744}
1745
Christoph Hellwig71102302016-07-06 21:55:52 +09001746static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1747 .name = "rdma",
1748 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001749 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001750 .reg_read32 = nvmf_reg_read32,
1751 .reg_read64 = nvmf_reg_read64,
1752 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001753 .free_ctrl = nvme_rdma_free_ctrl,
1754 .submit_async_event = nvme_rdma_submit_async_event,
1755 .delete_ctrl = nvme_rdma_del_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001756 .get_address = nvmf_get_address,
1757};
1758
1759static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1760{
Christoph Hellwig71102302016-07-06 21:55:52 +09001761 int ret;
1762
Christoph Hellwig71102302016-07-06 21:55:52 +09001763 ret = nvme_rdma_init_io_queues(ctrl);
1764 if (ret)
1765 return ret;
1766
1767 /*
1768 * We need a reference on the device as long as the tag_set is alive,
1769 * as the MRs in the request structures need a valid ib_device.
1770 */
1771 ret = -EINVAL;
1772 if (!nvme_rdma_dev_get(ctrl->device))
1773 goto out_free_io_queues;
1774
1775 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1776 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001777 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
Christoph Hellwig71102302016-07-06 21:55:52 +09001778 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1779 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1780 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1781 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1782 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1783 ctrl->tag_set.driver_data = ctrl;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001784 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001785 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1786
1787 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1788 if (ret)
1789 goto out_put_dev;
1790 ctrl->ctrl.tagset = &ctrl->tag_set;
1791
1792 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1793 if (IS_ERR(ctrl->ctrl.connect_q)) {
1794 ret = PTR_ERR(ctrl->ctrl.connect_q);
1795 goto out_free_tag_set;
1796 }
1797
1798 ret = nvme_rdma_connect_io_queues(ctrl);
1799 if (ret)
1800 goto out_cleanup_connect_q;
1801
1802 return 0;
1803
1804out_cleanup_connect_q:
1805 blk_cleanup_queue(ctrl->ctrl.connect_q);
1806out_free_tag_set:
1807 blk_mq_free_tag_set(&ctrl->tag_set);
1808out_put_dev:
1809 nvme_rdma_dev_put(ctrl->device);
1810out_free_io_queues:
1811 nvme_rdma_free_io_queues(ctrl);
1812 return ret;
1813}
1814
Christoph Hellwig71102302016-07-06 21:55:52 +09001815static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1816 struct nvmf_ctrl_options *opts)
1817{
1818 struct nvme_rdma_ctrl *ctrl;
1819 int ret;
1820 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001821 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001822
1823 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1824 if (!ctrl)
1825 return ERR_PTR(-ENOMEM);
1826 ctrl->ctrl.opts = opts;
1827 INIT_LIST_HEAD(&ctrl->list);
1828
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001829 if (opts->mask & NVMF_OPT_TRSVCID)
1830 port = opts->trsvcid;
1831 else
1832 port = __stringify(NVME_RDMA_IP_PORT);
1833
1834 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1835 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001836 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001837 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001838 goto out_free_ctrl;
1839 }
1840
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001841 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001842 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1843 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001844 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001845 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001846 opts->host_traddr);
1847 goto out_free_ctrl;
1848 }
1849 }
1850
Christoph Hellwig71102302016-07-06 21:55:52 +09001851 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1852 0 /* no quirks, we're perfect! */);
1853 if (ret)
1854 goto out_free_ctrl;
1855
Christoph Hellwig71102302016-07-06 21:55:52 +09001856 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1857 nvme_rdma_reconnect_ctrl_work);
1858 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1859 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001860 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001861
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001862 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001863 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001864 ctrl->ctrl.kato = opts->kato;
1865
1866 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001867 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001868 GFP_KERNEL);
1869 if (!ctrl->queues)
1870 goto out_uninit_ctrl;
1871
1872 ret = nvme_rdma_configure_admin_queue(ctrl);
1873 if (ret)
1874 goto out_kfree_queues;
1875
1876 /* sanity check icdoff */
1877 if (ctrl->ctrl.icdoff) {
1878 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001879 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001880 goto out_remove_admin_queue;
1881 }
1882
1883 /* sanity check keyed sgls */
1884 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1885 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001886 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001887 goto out_remove_admin_queue;
1888 }
1889
1890 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1891 /* warn if maxcmd is lower than queue_size */
1892 dev_warn(ctrl->ctrl.device,
1893 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1894 opts->queue_size, ctrl->ctrl.maxcmd);
1895 opts->queue_size = ctrl->ctrl.maxcmd;
1896 }
1897
Samuel Jones76c08bf2016-10-25 09:22:34 +02001898 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1899 /* warn if sqsize is lower than queue_size */
1900 dev_warn(ctrl->ctrl.device,
1901 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1902 opts->queue_size, ctrl->ctrl.sqsize + 1);
1903 opts->queue_size = ctrl->ctrl.sqsize + 1;
1904 }
1905
Christoph Hellwig71102302016-07-06 21:55:52 +09001906 if (opts->nr_io_queues) {
1907 ret = nvme_rdma_create_io_queues(ctrl);
1908 if (ret)
1909 goto out_remove_admin_queue;
1910 }
1911
1912 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1913 WARN_ON_ONCE(!changed);
1914
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001915 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001916 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1917
1918 kref_get(&ctrl->ctrl.kref);
1919
1920 mutex_lock(&nvme_rdma_ctrl_mutex);
1921 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1922 mutex_unlock(&nvme_rdma_ctrl_mutex);
1923
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001924 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001925
1926 return &ctrl->ctrl;
1927
1928out_remove_admin_queue:
Christoph Hellwig71102302016-07-06 21:55:52 +09001929 nvme_rdma_destroy_admin_queue(ctrl);
1930out_kfree_queues:
1931 kfree(ctrl->queues);
1932out_uninit_ctrl:
1933 nvme_uninit_ctrl(&ctrl->ctrl);
1934 nvme_put_ctrl(&ctrl->ctrl);
1935 if (ret > 0)
1936 ret = -EIO;
1937 return ERR_PTR(ret);
1938out_free_ctrl:
1939 kfree(ctrl);
1940 return ERR_PTR(ret);
1941}
1942
1943static struct nvmf_transport_ops nvme_rdma_transport = {
1944 .name = "rdma",
1945 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001946 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001947 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001948 .create_ctrl = nvme_rdma_create_ctrl,
1949};
1950
Steve Wisee87a9112016-09-02 09:01:54 -07001951static void nvme_rdma_add_one(struct ib_device *ib_device)
1952{
1953}
1954
1955static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1956{
1957 struct nvme_rdma_ctrl *ctrl;
1958
1959 /* Delete all controllers using this device */
1960 mutex_lock(&nvme_rdma_ctrl_mutex);
1961 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1962 if (ctrl->device->dev != ib_device)
1963 continue;
1964 dev_info(ctrl->ctrl.device,
1965 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1966 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1967 __nvme_rdma_del_ctrl(ctrl);
1968 }
1969 mutex_unlock(&nvme_rdma_ctrl_mutex);
1970
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001971 flush_workqueue(nvme_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07001972}
1973
1974static struct ib_client nvme_rdma_ib_client = {
1975 .name = "nvme_rdma",
1976 .add = nvme_rdma_add_one,
1977 .remove = nvme_rdma_remove_one
1978};
1979
Christoph Hellwig71102302016-07-06 21:55:52 +09001980static int __init nvme_rdma_init_module(void)
1981{
Steve Wisee87a9112016-09-02 09:01:54 -07001982 int ret;
1983
Steve Wisee87a9112016-09-02 09:01:54 -07001984 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02001985 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001986 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07001987
Sagi Grimberga56c79c2017-03-19 06:21:42 +02001988 ret = nvmf_register_transport(&nvme_rdma_transport);
1989 if (ret)
1990 goto err_unreg_client;
1991
1992 return 0;
1993
1994err_unreg_client:
1995 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02001996 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001997}
1998
1999static void __exit nvme_rdma_cleanup_module(void)
2000{
Christoph Hellwig71102302016-07-06 21:55:52 +09002001 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002002 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002003}
2004
2005module_init(nvme_rdma_init_module);
2006module_exit(nvme_rdma_cleanup_module);
2007
2008MODULE_LICENSE("GPL v2");