blob: a9d43f09963fae6d814eb67f328afa2f0160ea7e [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>
31#include <rdma/ib_cm.h>
32#include <linux/nvme-rdma.h>
33
34#include "nvme.h"
35#include "fabrics.h"
36
37
38#define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
39
40#define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
41
42#define NVME_RDMA_MAX_SEGMENTS 256
43
44#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
45
Christoph Hellwig71102302016-07-06 21:55:52 +090046/*
47 * We handle AEN commands ourselves and don't even let the
48 * block layer know about them.
49 */
50#define NVME_RDMA_NR_AEN_COMMANDS 1
51#define NVME_RDMA_AQ_BLKMQ_DEPTH \
52 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
53
54struct nvme_rdma_device {
55 struct ib_device *dev;
56 struct ib_pd *pd;
57 struct ib_mr *mr;
58 struct kref ref;
59 struct list_head entry;
60};
61
62struct nvme_rdma_qe {
63 struct ib_cqe cqe;
64 void *data;
65 u64 dma;
66};
67
68struct nvme_rdma_queue;
69struct nvme_rdma_request {
70 struct ib_mr *mr;
71 struct nvme_rdma_qe sqe;
72 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
73 u32 num_sge;
74 int nents;
75 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090076 struct ib_reg_wr reg_wr;
77 struct ib_cqe reg_cqe;
78 struct nvme_rdma_queue *queue;
79 struct sg_table sg_table;
80 struct scatterlist first_sgl[];
81};
82
83enum nvme_rdma_queue_flags {
84 NVME_RDMA_Q_CONNECTED = (1 << 0),
Steve Wisef361e5a2016-09-02 09:01:27 -070085 NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1),
Christoph Hellwig71102302016-07-06 21:55:52 +090086};
87
88struct nvme_rdma_queue {
89 struct nvme_rdma_qe *rsp_ring;
90 u8 sig_count;
91 int queue_size;
92 size_t cmnd_capsule_len;
93 struct nvme_rdma_ctrl *ctrl;
94 struct nvme_rdma_device *device;
95 struct ib_cq *ib_cq;
96 struct ib_qp *qp;
97
98 unsigned long flags;
99 struct rdma_cm_id *cm_id;
100 int cm_error;
101 struct completion cm_done;
102};
103
104struct nvme_rdma_ctrl {
105 /* read and written in the hot path */
106 spinlock_t lock;
107
108 /* read only in the hot path */
109 struct nvme_rdma_queue *queues;
110 u32 queue_count;
111
112 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900113 struct blk_mq_tag_set tag_set;
114 struct work_struct delete_work;
115 struct work_struct reset_work;
116 struct work_struct err_work;
117
118 struct nvme_rdma_qe async_event_sqe;
119
120 int reconnect_delay;
121 struct delayed_work reconnect_work;
122
123 struct list_head list;
124
125 struct blk_mq_tag_set admin_tag_set;
126 struct nvme_rdma_device *device;
127
128 u64 cap;
129 u32 max_fr_pages;
130
131 union {
132 struct sockaddr addr;
133 struct sockaddr_in addr_in;
134 };
135
136 struct nvme_ctrl ctrl;
137};
138
139static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
140{
141 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
142}
143
144static LIST_HEAD(device_list);
145static DEFINE_MUTEX(device_list_mutex);
146
147static LIST_HEAD(nvme_rdma_ctrl_list);
148static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
149
150static struct workqueue_struct *nvme_rdma_wq;
151
152/*
153 * Disabling this option makes small I/O goes faster, but is fundamentally
154 * unsafe. With it turned off we will have to register a global rkey that
155 * allows read and write access to all physical memory.
156 */
157static bool register_always = true;
158module_param(register_always, bool, 0444);
159MODULE_PARM_DESC(register_always,
160 "Use memory registration even for contiguous memory regions");
161
162static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
163 struct rdma_cm_event *event);
164static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900165
166/* XXX: really should move to a generic header sooner or later.. */
167static inline void put_unaligned_le24(u32 val, u8 *p)
168{
169 *p++ = val;
170 *p++ = val >> 8;
171 *p++ = val >> 16;
172}
173
174static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
175{
176 return queue - queue->ctrl->queues;
177}
178
179static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
180{
181 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
182}
183
184static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
185 size_t capsule_size, enum dma_data_direction dir)
186{
187 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
188 kfree(qe->data);
189}
190
191static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
192 size_t capsule_size, enum dma_data_direction dir)
193{
194 qe->data = kzalloc(capsule_size, GFP_KERNEL);
195 if (!qe->data)
196 return -ENOMEM;
197
198 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
199 if (ib_dma_mapping_error(ibdev, qe->dma)) {
200 kfree(qe->data);
201 return -ENOMEM;
202 }
203
204 return 0;
205}
206
207static void nvme_rdma_free_ring(struct ib_device *ibdev,
208 struct nvme_rdma_qe *ring, size_t ib_queue_size,
209 size_t capsule_size, enum dma_data_direction dir)
210{
211 int i;
212
213 for (i = 0; i < ib_queue_size; i++)
214 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
215 kfree(ring);
216}
217
218static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
219 size_t ib_queue_size, size_t capsule_size,
220 enum dma_data_direction dir)
221{
222 struct nvme_rdma_qe *ring;
223 int i;
224
225 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
226 if (!ring)
227 return NULL;
228
229 for (i = 0; i < ib_queue_size; i++) {
230 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
231 goto out_free_ring;
232 }
233
234 return ring;
235
236out_free_ring:
237 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
238 return NULL;
239}
240
241static void nvme_rdma_qp_event(struct ib_event *event, void *context)
242{
243 pr_debug("QP event %d\n", event->event);
244}
245
246static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
247{
248 wait_for_completion_interruptible_timeout(&queue->cm_done,
249 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
250 return queue->cm_error;
251}
252
253static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
254{
255 struct nvme_rdma_device *dev = queue->device;
256 struct ib_qp_init_attr init_attr;
257 int ret;
258
259 memset(&init_attr, 0, sizeof(init_attr));
260 init_attr.event_handler = nvme_rdma_qp_event;
261 /* +1 for drain */
262 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
263 /* +1 for drain */
264 init_attr.cap.max_recv_wr = queue->queue_size + 1;
265 init_attr.cap.max_recv_sge = 1;
266 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
267 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
268 init_attr.qp_type = IB_QPT_RC;
269 init_attr.send_cq = queue->ib_cq;
270 init_attr.recv_cq = queue->ib_cq;
271
272 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
273
274 queue->qp = queue->cm_id->qp;
275 return ret;
276}
277
278static int nvme_rdma_reinit_request(void *data, struct request *rq)
279{
280 struct nvme_rdma_ctrl *ctrl = data;
281 struct nvme_rdma_device *dev = ctrl->device;
282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
283 int ret = 0;
284
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300285 if (!req->mr->need_inval)
Christoph Hellwig71102302016-07-06 21:55:52 +0900286 goto out;
287
288 ib_dereg_mr(req->mr);
289
290 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
291 ctrl->max_fr_pages);
292 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900293 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000294 req->mr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900295 }
296
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300297 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900298
299out:
300 return ret;
301}
302
303static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
304 struct request *rq, unsigned int queue_idx)
305{
306 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
307 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
308 struct nvme_rdma_device *dev = queue->device;
309
310 if (req->mr)
311 ib_dereg_mr(req->mr);
312
313 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
314 DMA_TO_DEVICE);
315}
316
317static void nvme_rdma_exit_request(void *data, struct request *rq,
318 unsigned int hctx_idx, unsigned int rq_idx)
319{
320 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
321}
322
323static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
324 unsigned int hctx_idx, unsigned int rq_idx)
325{
326 return __nvme_rdma_exit_request(data, rq, 0);
327}
328
329static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
330 struct request *rq, unsigned int queue_idx)
331{
332 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
333 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
334 struct nvme_rdma_device *dev = queue->device;
335 struct ib_device *ibdev = dev->dev;
336 int ret;
337
338 BUG_ON(queue_idx >= ctrl->queue_count);
339
340 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
341 DMA_TO_DEVICE);
342 if (ret)
343 return ret;
344
345 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
346 ctrl->max_fr_pages);
347 if (IS_ERR(req->mr)) {
348 ret = PTR_ERR(req->mr);
349 goto out_free_qe;
350 }
351
352 req->queue = queue;
353
354 return 0;
355
356out_free_qe:
357 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
358 DMA_TO_DEVICE);
359 return -ENOMEM;
360}
361
362static int nvme_rdma_init_request(void *data, struct request *rq,
363 unsigned int hctx_idx, unsigned int rq_idx,
364 unsigned int numa_node)
365{
366 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
367}
368
369static int nvme_rdma_init_admin_request(void *data, struct request *rq,
370 unsigned int hctx_idx, unsigned int rq_idx,
371 unsigned int numa_node)
372{
373 return __nvme_rdma_init_request(data, rq, 0);
374}
375
376static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
377 unsigned int hctx_idx)
378{
379 struct nvme_rdma_ctrl *ctrl = data;
380 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
381
382 BUG_ON(hctx_idx >= ctrl->queue_count);
383
384 hctx->driver_data = queue;
385 return 0;
386}
387
388static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
389 unsigned int hctx_idx)
390{
391 struct nvme_rdma_ctrl *ctrl = data;
392 struct nvme_rdma_queue *queue = &ctrl->queues[0];
393
394 BUG_ON(hctx_idx != 0);
395
396 hctx->driver_data = queue;
397 return 0;
398}
399
400static void nvme_rdma_free_dev(struct kref *ref)
401{
402 struct nvme_rdma_device *ndev =
403 container_of(ref, struct nvme_rdma_device, ref);
404
405 mutex_lock(&device_list_mutex);
406 list_del(&ndev->entry);
407 mutex_unlock(&device_list_mutex);
408
409 if (!register_always)
410 ib_dereg_mr(ndev->mr);
411 ib_dealloc_pd(ndev->pd);
412
413 kfree(ndev);
414}
415
416static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
417{
418 kref_put(&dev->ref, nvme_rdma_free_dev);
419}
420
421static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
422{
423 return kref_get_unless_zero(&dev->ref);
424}
425
426static struct nvme_rdma_device *
427nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
428{
429 struct nvme_rdma_device *ndev;
430
431 mutex_lock(&device_list_mutex);
432 list_for_each_entry(ndev, &device_list, entry) {
433 if (ndev->dev->node_guid == cm_id->device->node_guid &&
434 nvme_rdma_dev_get(ndev))
435 goto out_unlock;
436 }
437
438 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
439 if (!ndev)
440 goto out_err;
441
442 ndev->dev = cm_id->device;
443 kref_init(&ndev->ref);
444
445 ndev->pd = ib_alloc_pd(ndev->dev);
446 if (IS_ERR(ndev->pd))
447 goto out_free_dev;
448
449 if (!register_always) {
450 ndev->mr = ib_get_dma_mr(ndev->pd,
451 IB_ACCESS_LOCAL_WRITE |
452 IB_ACCESS_REMOTE_READ |
453 IB_ACCESS_REMOTE_WRITE);
454 if (IS_ERR(ndev->mr))
455 goto out_free_pd;
456 }
457
458 if (!(ndev->dev->attrs.device_cap_flags &
459 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
460 dev_err(&ndev->dev->dev,
461 "Memory registrations not supported.\n");
462 goto out_free_mr;
463 }
464
465 list_add(&ndev->entry, &device_list);
466out_unlock:
467 mutex_unlock(&device_list_mutex);
468 return ndev;
469
470out_free_mr:
471 if (!register_always)
472 ib_dereg_mr(ndev->mr);
473out_free_pd:
474 ib_dealloc_pd(ndev->pd);
475out_free_dev:
476 kfree(ndev);
477out_err:
478 mutex_unlock(&device_list_mutex);
479 return NULL;
480}
481
482static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
483{
Steve Wisef361e5a2016-09-02 09:01:27 -0700484 struct nvme_rdma_device *dev;
485 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900486
Steve Wisef361e5a2016-09-02 09:01:27 -0700487 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
488 return;
489
490 dev = queue->device;
491 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900492 rdma_destroy_qp(queue->cm_id);
493 ib_free_cq(queue->ib_cq);
494
495 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
496 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
497
498 nvme_rdma_dev_put(dev);
499}
500
501static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
502 struct nvme_rdma_device *dev)
503{
504 struct ib_device *ibdev = dev->dev;
505 const int send_wr_factor = 3; /* MR, SEND, INV */
506 const int cq_factor = send_wr_factor + 1; /* + RECV */
507 int comp_vector, idx = nvme_rdma_queue_idx(queue);
508
509 int ret;
510
511 queue->device = dev;
512
513 /*
514 * The admin queue is barely used once the controller is live, so don't
515 * bother to spread it out.
516 */
517 if (idx == 0)
518 comp_vector = 0;
519 else
520 comp_vector = idx % ibdev->num_comp_vectors;
521
522
523 /* +1 for ib_stop_cq */
524 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
525 cq_factor * queue->queue_size + 1, comp_vector,
526 IB_POLL_SOFTIRQ);
527 if (IS_ERR(queue->ib_cq)) {
528 ret = PTR_ERR(queue->ib_cq);
529 goto out;
530 }
531
532 ret = nvme_rdma_create_qp(queue, send_wr_factor);
533 if (ret)
534 goto out_destroy_ib_cq;
535
536 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
537 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
538 if (!queue->rsp_ring) {
539 ret = -ENOMEM;
540 goto out_destroy_qp;
541 }
Steve Wisef361e5a2016-09-02 09:01:27 -0700542 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900543
544 return 0;
545
546out_destroy_qp:
547 ib_destroy_qp(queue->qp);
548out_destroy_ib_cq:
549 ib_free_cq(queue->ib_cq);
550out:
551 return ret;
552}
553
554static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
555 int idx, size_t queue_size)
556{
557 struct nvme_rdma_queue *queue;
558 int ret;
559
560 queue = &ctrl->queues[idx];
561 queue->ctrl = ctrl;
562 init_completion(&queue->cm_done);
563
564 if (idx > 0)
565 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
566 else
567 queue->cmnd_capsule_len = sizeof(struct nvme_command);
568
569 queue->queue_size = queue_size;
570
571 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
572 RDMA_PS_TCP, IB_QPT_RC);
573 if (IS_ERR(queue->cm_id)) {
574 dev_info(ctrl->ctrl.device,
575 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
576 return PTR_ERR(queue->cm_id);
577 }
578
579 queue->cm_error = -ETIMEDOUT;
580 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
581 NVME_RDMA_CONNECT_TIMEOUT_MS);
582 if (ret) {
583 dev_info(ctrl->ctrl.device,
584 "rdma_resolve_addr failed (%d).\n", ret);
585 goto out_destroy_cm_id;
586 }
587
588 ret = nvme_rdma_wait_for_cm(queue);
589 if (ret) {
590 dev_info(ctrl->ctrl.device,
591 "rdma_resolve_addr wait failed (%d).\n", ret);
592 goto out_destroy_cm_id;
593 }
594
595 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
596
597 return 0;
598
599out_destroy_cm_id:
Steve Wisef361e5a2016-09-02 09:01:27 -0700600 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900601 rdma_destroy_id(queue->cm_id);
602 return ret;
603}
604
605static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
606{
607 rdma_disconnect(queue->cm_id);
608 ib_drain_qp(queue->qp);
609}
610
611static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
612{
613 nvme_rdma_destroy_queue_ib(queue);
614 rdma_destroy_id(queue->cm_id);
615}
616
617static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
618{
619 if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags))
620 return;
621 nvme_rdma_stop_queue(queue);
622 nvme_rdma_free_queue(queue);
623}
624
625static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
626{
627 int i;
628
629 for (i = 1; i < ctrl->queue_count; i++)
630 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
631}
632
633static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
634{
635 int i, ret = 0;
636
637 for (i = 1; i < ctrl->queue_count; i++) {
638 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
639 if (ret)
640 break;
641 }
642
643 return ret;
644}
645
646static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
647{
648 int i, ret;
649
650 for (i = 1; i < ctrl->queue_count; i++) {
Jay Freyenseec5af8652016-08-17 15:00:27 -0700651 ret = nvme_rdma_init_queue(ctrl, i,
652 ctrl->ctrl.opts->queue_size);
Christoph Hellwig71102302016-07-06 21:55:52 +0900653 if (ret) {
654 dev_info(ctrl->ctrl.device,
655 "failed to initialize i/o queue: %d\n", ret);
656 goto out_free_queues;
657 }
658 }
659
660 return 0;
661
662out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700663 for (i--; i >= 1; i--)
Christoph Hellwig71102302016-07-06 21:55:52 +0900664 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
665
666 return ret;
667}
668
669static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
670{
671 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
672 sizeof(struct nvme_command), DMA_TO_DEVICE);
673 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
674 blk_cleanup_queue(ctrl->ctrl.admin_q);
675 blk_mq_free_tag_set(&ctrl->admin_tag_set);
676 nvme_rdma_dev_put(ctrl->device);
677}
678
679static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
680{
681 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
682
683 if (list_empty(&ctrl->list))
684 goto free_ctrl;
685
686 mutex_lock(&nvme_rdma_ctrl_mutex);
687 list_del(&ctrl->list);
688 mutex_unlock(&nvme_rdma_ctrl_mutex);
689
Christoph Hellwig71102302016-07-06 21:55:52 +0900690 kfree(ctrl->queues);
691 nvmf_free_options(nctrl->opts);
692free_ctrl:
693 kfree(ctrl);
694}
695
696static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
697{
698 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
699 struct nvme_rdma_ctrl, reconnect_work);
700 bool changed;
701 int ret;
702
703 if (ctrl->queue_count > 1) {
704 nvme_rdma_free_io_queues(ctrl);
705
706 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
707 if (ret)
708 goto requeue;
709 }
710
711 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
712
713 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
714 if (ret)
715 goto requeue;
716
717 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
718 if (ret)
719 goto requeue;
720
721 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
722
723 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
724 if (ret)
725 goto stop_admin_q;
726
727 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
728 if (ret)
729 goto stop_admin_q;
730
731 nvme_start_keep_alive(&ctrl->ctrl);
732
733 if (ctrl->queue_count > 1) {
734 ret = nvme_rdma_init_io_queues(ctrl);
735 if (ret)
736 goto stop_admin_q;
737
738 ret = nvme_rdma_connect_io_queues(ctrl);
739 if (ret)
740 goto stop_admin_q;
741 }
742
743 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
744 WARN_ON_ONCE(!changed);
745
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300746 if (ctrl->queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900747 nvme_start_queues(&ctrl->ctrl);
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300748 nvme_queue_scan(&ctrl->ctrl);
Sagi Grimberg3ef1b4b2016-08-04 13:46:19 +0300749 nvme_queue_async_events(&ctrl->ctrl);
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300750 }
Christoph Hellwig71102302016-07-06 21:55:52 +0900751
752 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
753
754 return;
755
756stop_admin_q:
757 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
758requeue:
759 /* Make sure we are not resetting/deleting */
760 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
761 dev_info(ctrl->ctrl.device,
762 "Failed reconnect attempt, requeueing...\n");
763 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
764 ctrl->reconnect_delay * HZ);
765 }
766}
767
768static void nvme_rdma_error_recovery_work(struct work_struct *work)
769{
770 struct nvme_rdma_ctrl *ctrl = container_of(work,
771 struct nvme_rdma_ctrl, err_work);
772
773 nvme_stop_keep_alive(&ctrl->ctrl);
774 if (ctrl->queue_count > 1)
775 nvme_stop_queues(&ctrl->ctrl);
776 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
777
778 /* We must take care of fastfail/requeue all our inflight requests */
779 if (ctrl->queue_count > 1)
780 blk_mq_tagset_busy_iter(&ctrl->tag_set,
781 nvme_cancel_request, &ctrl->ctrl);
782 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
783 nvme_cancel_request, &ctrl->ctrl);
784
785 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
786 ctrl->reconnect_delay);
787
788 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
789 ctrl->reconnect_delay * HZ);
790}
791
792static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
793{
794 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
795 return;
796
797 queue_work(nvme_rdma_wq, &ctrl->err_work);
798}
799
800static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
801 const char *op)
802{
803 struct nvme_rdma_queue *queue = cq->cq_context;
804 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
805
806 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
807 dev_info(ctrl->ctrl.device,
808 "%s for CQE 0x%p failed with status %s (%d)\n",
809 op, wc->wr_cqe,
810 ib_wc_status_msg(wc->status), wc->status);
811 nvme_rdma_error_recovery(ctrl);
812}
813
814static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
815{
816 if (unlikely(wc->status != IB_WC_SUCCESS))
817 nvme_rdma_wr_error(cq, wc, "MEMREG");
818}
819
820static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
821{
822 if (unlikely(wc->status != IB_WC_SUCCESS))
823 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
824}
825
826static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
827 struct nvme_rdma_request *req)
828{
829 struct ib_send_wr *bad_wr;
830 struct ib_send_wr wr = {
831 .opcode = IB_WR_LOCAL_INV,
832 .next = NULL,
833 .num_sge = 0,
834 .send_flags = 0,
835 .ex.invalidate_rkey = req->mr->rkey,
836 };
837
838 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
839 wr.wr_cqe = &req->reg_cqe;
840
841 return ib_post_send(queue->qp, &wr, &bad_wr);
842}
843
844static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
845 struct request *rq)
846{
847 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
848 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
849 struct nvme_rdma_device *dev = queue->device;
850 struct ib_device *ibdev = dev->dev;
851 int res;
852
853 if (!blk_rq_bytes(rq))
854 return;
855
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300856 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900857 res = nvme_rdma_inv_rkey(queue, req);
858 if (res < 0) {
859 dev_err(ctrl->ctrl.device,
860 "Queueing INV WR for rkey %#x failed (%d)\n",
861 req->mr->rkey, res);
862 nvme_rdma_error_recovery(queue->ctrl);
863 }
864 }
865
866 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
867 req->nents, rq_data_dir(rq) ==
868 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
869
870 nvme_cleanup_cmd(rq);
871 sg_free_table_chained(&req->sg_table, true);
872}
873
874static int nvme_rdma_set_sg_null(struct nvme_command *c)
875{
876 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
877
878 sg->addr = 0;
879 put_unaligned_le24(0, sg->length);
880 put_unaligned_le32(0, sg->key);
881 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
882 return 0;
883}
884
885static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
886 struct nvme_rdma_request *req, struct nvme_command *c)
887{
888 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
889
890 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
891 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
892 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
893
894 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
895 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
896 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
897
898 req->inline_data = true;
899 req->num_sge++;
900 return 0;
901}
902
903static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
904 struct nvme_rdma_request *req, struct nvme_command *c)
905{
906 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
907
908 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
909 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
910 put_unaligned_le32(queue->device->mr->rkey, sg->key);
911 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
912 return 0;
913}
914
915static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
916 struct nvme_rdma_request *req, struct nvme_command *c,
917 int count)
918{
919 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
920 int nr;
921
922 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
923 if (nr < count) {
924 if (nr < 0)
925 return nr;
926 return -EINVAL;
927 }
928
929 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
930
931 req->reg_cqe.done = nvme_rdma_memreg_done;
932 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
933 req->reg_wr.wr.opcode = IB_WR_REG_MR;
934 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
935 req->reg_wr.wr.num_sge = 0;
936 req->reg_wr.mr = req->mr;
937 req->reg_wr.key = req->mr->rkey;
938 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
939 IB_ACCESS_REMOTE_READ |
940 IB_ACCESS_REMOTE_WRITE;
941
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300942 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +0900943
944 sg->addr = cpu_to_le64(req->mr->iova);
945 put_unaligned_le24(req->mr->length, sg->length);
946 put_unaligned_le32(req->mr->rkey, sg->key);
947 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
948 NVME_SGL_FMT_INVALIDATE;
949
950 return 0;
951}
952
953static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
954 struct request *rq, unsigned int map_len,
955 struct nvme_command *c)
956{
957 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
958 struct nvme_rdma_device *dev = queue->device;
959 struct ib_device *ibdev = dev->dev;
960 int nents, count;
961 int ret;
962
963 req->num_sge = 1;
964 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300965 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900966
967 c->common.flags |= NVME_CMD_SGL_METABUF;
968
969 if (!blk_rq_bytes(rq))
970 return nvme_rdma_set_sg_null(c);
971
972 req->sg_table.sgl = req->first_sgl;
973 ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments,
974 req->sg_table.sgl);
975 if (ret)
976 return -ENOMEM;
977
978 nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
979 BUG_ON(nents > rq->nr_phys_segments);
980 req->nents = nents;
981
982 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents,
983 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) {
990 if (rq_data_dir(rq) == WRITE &&
991 map_len <= nvme_rdma_inline_data_size(queue) &&
992 nvme_rdma_queue_idx(queue))
993 return nvme_rdma_map_sg_inline(queue, req, c);
994
995 if (!register_always)
996 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
1008static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1009 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1010 struct ib_send_wr *first, bool flush)
1011{
1012 struct ib_send_wr wr, *bad_wr;
1013 int ret;
1014
1015 sge->addr = qe->dma;
1016 sge->length = sizeof(struct nvme_command),
1017 sge->lkey = queue->device->pd->local_dma_lkey;
1018
1019 qe->cqe.done = nvme_rdma_send_done;
1020
1021 wr.next = NULL;
1022 wr.wr_cqe = &qe->cqe;
1023 wr.sg_list = sge;
1024 wr.num_sge = num_sge;
1025 wr.opcode = IB_WR_SEND;
1026 wr.send_flags = 0;
1027
1028 /*
1029 * Unsignalled send completions are another giant desaster in the
1030 * IB Verbs spec: If we don't regularly post signalled sends
1031 * the send queue will fill up and only a QP reset will rescue us.
1032 * Would have been way to obvious to handle this in hardware or
1033 * at least the RDMA stack..
1034 *
1035 * This messy and racy code sniplet is copy and pasted from the iSER
1036 * initiator, and the magic '32' comes from there as well.
1037 *
1038 * Always signal the flushes. The magic request used for the flush
1039 * sequencer is not allocated in our driver's tagset and it's
1040 * triggered to be freed by blk_cleanup_queue(). So we need to
1041 * always mark it as signaled to ensure that the "wr_cqe", which is
1042 * embeded in request's payload, is not freed when __ib_process_cq()
1043 * calls wr_cqe->done().
1044 */
1045 if ((++queue->sig_count % 32) == 0 || flush)
1046 wr.send_flags |= IB_SEND_SIGNALED;
1047
1048 if (first)
1049 first->next = &wr;
1050 else
1051 first = &wr;
1052
1053 ret = ib_post_send(queue->qp, first, &bad_wr);
1054 if (ret) {
1055 dev_err(queue->ctrl->ctrl.device,
1056 "%s failed with error code %d\n", __func__, ret);
1057 }
1058 return ret;
1059}
1060
1061static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1062 struct nvme_rdma_qe *qe)
1063{
1064 struct ib_recv_wr wr, *bad_wr;
1065 struct ib_sge list;
1066 int ret;
1067
1068 list.addr = qe->dma;
1069 list.length = sizeof(struct nvme_completion);
1070 list.lkey = queue->device->pd->local_dma_lkey;
1071
1072 qe->cqe.done = nvme_rdma_recv_done;
1073
1074 wr.next = NULL;
1075 wr.wr_cqe = &qe->cqe;
1076 wr.sg_list = &list;
1077 wr.num_sge = 1;
1078
1079 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1080 if (ret) {
1081 dev_err(queue->ctrl->ctrl.device,
1082 "%s failed with error code %d\n", __func__, ret);
1083 }
1084 return ret;
1085}
1086
1087static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1088{
1089 u32 queue_idx = nvme_rdma_queue_idx(queue);
1090
1091 if (queue_idx == 0)
1092 return queue->ctrl->admin_tag_set.tags[queue_idx];
1093 return queue->ctrl->tag_set.tags[queue_idx - 1];
1094}
1095
1096static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1097{
1098 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1099 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1100 struct ib_device *dev = queue->device->dev;
1101 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1102 struct nvme_command *cmd = sqe->data;
1103 struct ib_sge sge;
1104 int ret;
1105
1106 if (WARN_ON_ONCE(aer_idx != 0))
1107 return;
1108
1109 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1110
1111 memset(cmd, 0, sizeof(*cmd));
1112 cmd->common.opcode = nvme_admin_async_event;
1113 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1114 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1115 nvme_rdma_set_sg_null(cmd);
1116
1117 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1118 DMA_TO_DEVICE);
1119
1120 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1121 WARN_ON_ONCE(ret);
1122}
1123
1124static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1125 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1126{
1127 u16 status = le16_to_cpu(cqe->status);
1128 struct request *rq;
1129 struct nvme_rdma_request *req;
1130 int ret = 0;
1131
1132 status >>= 1;
1133
1134 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1135 if (!rq) {
1136 dev_err(queue->ctrl->ctrl.device,
1137 "tag 0x%x on QP %#x not found\n",
1138 cqe->command_id, queue->qp->qp_num);
1139 nvme_rdma_error_recovery(queue->ctrl);
1140 return ret;
1141 }
1142 req = blk_mq_rq_to_pdu(rq);
1143
1144 if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special)
1145 memcpy(rq->special, cqe, sizeof(*cqe));
1146
1147 if (rq->tag == tag)
1148 ret = 1;
1149
1150 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1151 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001152 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001153
1154 blk_mq_complete_request(rq, status);
1155
1156 return ret;
1157}
1158
1159static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1160{
1161 struct nvme_rdma_qe *qe =
1162 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1163 struct nvme_rdma_queue *queue = cq->cq_context;
1164 struct ib_device *ibdev = queue->device->dev;
1165 struct nvme_completion *cqe = qe->data;
1166 const size_t len = sizeof(struct nvme_completion);
1167 int ret = 0;
1168
1169 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1170 nvme_rdma_wr_error(cq, wc, "RECV");
1171 return 0;
1172 }
1173
1174 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1175 /*
1176 * AEN requests are special as they don't time out and can
1177 * survive any kind of queue freeze and often don't respond to
1178 * aborts. We don't even bother to allocate a struct request
1179 * for them but rather special case them here.
1180 */
1181 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1182 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1183 nvme_complete_async_event(&queue->ctrl->ctrl, cqe);
1184 else
1185 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1186 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1187
1188 nvme_rdma_post_recv(queue, qe);
1189 return ret;
1190}
1191
1192static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1193{
1194 __nvme_rdma_recv_done(cq, wc, -1);
1195}
1196
1197static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1198{
1199 int ret, i;
1200
1201 for (i = 0; i < queue->queue_size; i++) {
1202 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1203 if (ret)
1204 goto out_destroy_queue_ib;
1205 }
1206
1207 return 0;
1208
1209out_destroy_queue_ib:
1210 nvme_rdma_destroy_queue_ib(queue);
1211 return ret;
1212}
1213
1214static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1215 struct rdma_cm_event *ev)
1216{
1217 if (ev->param.conn.private_data_len) {
1218 struct nvme_rdma_cm_rej *rej =
1219 (struct nvme_rdma_cm_rej *)ev->param.conn.private_data;
1220
1221 dev_err(queue->ctrl->ctrl.device,
1222 "Connect rejected, status %d.", le16_to_cpu(rej->sts));
1223 /* XXX: Think of something clever to do here... */
1224 } else {
1225 dev_err(queue->ctrl->ctrl.device,
1226 "Connect rejected, no private data.\n");
1227 }
1228
1229 return -ECONNRESET;
1230}
1231
1232static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1233{
1234 struct nvme_rdma_device *dev;
1235 int ret;
1236
1237 dev = nvme_rdma_find_get_device(queue->cm_id);
1238 if (!dev) {
1239 dev_err(queue->cm_id->device->dma_device,
1240 "no client data found!\n");
1241 return -ECONNREFUSED;
1242 }
1243
1244 ret = nvme_rdma_create_queue_ib(queue, dev);
1245 if (ret) {
1246 nvme_rdma_dev_put(dev);
1247 goto out;
1248 }
1249
1250 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1251 if (ret) {
1252 dev_err(queue->ctrl->ctrl.device,
1253 "rdma_resolve_route failed (%d).\n",
1254 queue->cm_error);
1255 goto out_destroy_queue;
1256 }
1257
1258 return 0;
1259
1260out_destroy_queue:
1261 nvme_rdma_destroy_queue_ib(queue);
1262out:
1263 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) {
1290 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1291 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1292 } 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
1316/**
1317 * nvme_rdma_device_unplug() - Handle RDMA device unplug
1318 * @queue: Queue that owns the cm_id that caught the event
1319 *
1320 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1321 * to unplug so we should take care of destroying our RDMA resources.
1322 * This event will be generated for each allocated cm_id.
1323 *
1324 * In our case, the RDMA resources are managed per controller and not
1325 * only per queue. So the way we handle this is we trigger an implicit
1326 * controller deletion upon the first DEVICE_REMOVAL event we see, and
1327 * hold the event inflight until the controller deletion is completed.
1328 *
1329 * One exception that we need to handle is the destruction of the cm_id
1330 * that caught the event. Since we hold the callout until the controller
1331 * deletion is completed, we'll deadlock if the controller deletion will
1332 * call rdma_destroy_id on this queue's cm_id. Thus, we claim ownership
Sagi Grimberg57de5a02016-07-14 17:39:47 +03001333 * of destroying this queue before-hand, destroy the queue resources,
1334 * then queue the controller deletion which won't destroy this queue and
1335 * we destroy the cm_id implicitely by returning a non-zero rc to the callout.
Christoph Hellwig71102302016-07-06 21:55:52 +09001336 */
1337static int nvme_rdma_device_unplug(struct nvme_rdma_queue *queue)
1338{
1339 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
Colin Ian King39bbee42016-08-16 09:24:39 +01001340 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001341
Sagi Grimberg57de5a02016-07-14 17:39:47 +03001342 /* Own the controller deletion */
1343 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1344 return 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001345
Sagi Grimberg57de5a02016-07-14 17:39:47 +03001346 dev_warn(ctrl->ctrl.device,
1347 "Got rdma device removal event, deleting ctrl\n");
1348
1349 /* Get rid of reconnect work if its running */
1350 cancel_delayed_work_sync(&ctrl->reconnect_work);
1351
1352 /* Disable the queue so ctrl delete won't free it */
1353 if (test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags)) {
1354 /* Free this queue ourselves */
1355 nvme_rdma_stop_queue(queue);
1356 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001357
1358 /* Return non-zero so the cm_id will destroy implicitly */
Sagi Grimberg57de5a02016-07-14 17:39:47 +03001359 ret = 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001360 }
1361
Steve Wisecdbecc82016-09-01 09:12:25 -07001362 /*
1363 * Queue controller deletion. Keep a reference until all
1364 * work is flushed since delete_work will free the ctrl mem
1365 */
1366 kref_get(&ctrl->ctrl.kref);
Sagi Grimberg57de5a02016-07-14 17:39:47 +03001367 queue_work(nvme_rdma_wq, &ctrl->delete_work);
1368 flush_work(&ctrl->delete_work);
Steve Wisecdbecc82016-09-01 09:12:25 -07001369 nvme_put_ctrl(&ctrl->ctrl);
1370
Sagi Grimberg57de5a02016-07-14 17:39:47 +03001371 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001372}
1373
1374static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1375 struct rdma_cm_event *ev)
1376{
1377 struct nvme_rdma_queue *queue = cm_id->context;
1378 int cm_error = 0;
1379
1380 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1381 rdma_event_msg(ev->event), ev->event,
1382 ev->status, cm_id);
1383
1384 switch (ev->event) {
1385 case RDMA_CM_EVENT_ADDR_RESOLVED:
1386 cm_error = nvme_rdma_addr_resolved(queue);
1387 break;
1388 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1389 cm_error = nvme_rdma_route_resolved(queue);
1390 break;
1391 case RDMA_CM_EVENT_ESTABLISHED:
1392 queue->cm_error = nvme_rdma_conn_established(queue);
1393 /* complete cm_done regardless of success/failure */
1394 complete(&queue->cm_done);
1395 return 0;
1396 case RDMA_CM_EVENT_REJECTED:
1397 cm_error = nvme_rdma_conn_rejected(queue, ev);
1398 break;
1399 case RDMA_CM_EVENT_ADDR_ERROR:
1400 case RDMA_CM_EVENT_ROUTE_ERROR:
1401 case RDMA_CM_EVENT_CONNECT_ERROR:
1402 case RDMA_CM_EVENT_UNREACHABLE:
1403 dev_dbg(queue->ctrl->ctrl.device,
1404 "CM error event %d\n", ev->event);
1405 cm_error = -ECONNRESET;
1406 break;
1407 case RDMA_CM_EVENT_DISCONNECTED:
1408 case RDMA_CM_EVENT_ADDR_CHANGE:
1409 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1410 dev_dbg(queue->ctrl->ctrl.device,
1411 "disconnect received - connection closed\n");
1412 nvme_rdma_error_recovery(queue->ctrl);
1413 break;
1414 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1415 /* return 1 means impliciy CM ID destroy */
1416 return nvme_rdma_device_unplug(queue);
1417 default:
1418 dev_err(queue->ctrl->ctrl.device,
1419 "Unexpected RDMA CM event (%d)\n", ev->event);
1420 nvme_rdma_error_recovery(queue->ctrl);
1421 break;
1422 }
1423
1424 if (cm_error) {
1425 queue->cm_error = cm_error;
1426 complete(&queue->cm_done);
1427 }
1428
1429 return 0;
1430}
1431
1432static enum blk_eh_timer_return
1433nvme_rdma_timeout(struct request *rq, bool reserved)
1434{
1435 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1436
1437 /* queue error recovery */
1438 nvme_rdma_error_recovery(req->queue->ctrl);
1439
1440 /* fail with DNR on cmd timeout */
1441 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1442
1443 return BLK_EH_HANDLED;
1444}
1445
1446static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1447 const struct blk_mq_queue_data *bd)
1448{
1449 struct nvme_ns *ns = hctx->queue->queuedata;
1450 struct nvme_rdma_queue *queue = hctx->driver_data;
1451 struct request *rq = bd->rq;
1452 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1453 struct nvme_rdma_qe *sqe = &req->sqe;
1454 struct nvme_command *c = sqe->data;
1455 bool flush = false;
1456 struct ib_device *dev;
1457 unsigned int map_len;
1458 int ret;
1459
1460 WARN_ON_ONCE(rq->tag < 0);
1461
1462 dev = queue->device->dev;
1463 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1464 sizeof(struct nvme_command), DMA_TO_DEVICE);
1465
1466 ret = nvme_setup_cmd(ns, rq, c);
1467 if (ret)
1468 return ret;
1469
1470 c->common.command_id = rq->tag;
1471 blk_mq_start_request(rq);
1472
1473 map_len = nvme_map_len(rq);
1474 ret = nvme_rdma_map_data(queue, rq, map_len, c);
1475 if (ret < 0) {
1476 dev_err(queue->ctrl->ctrl.device,
1477 "Failed to map data (%d)\n", ret);
1478 nvme_cleanup_cmd(rq);
1479 goto err;
1480 }
1481
1482 ib_dma_sync_single_for_device(dev, sqe->dma,
1483 sizeof(struct nvme_command), DMA_TO_DEVICE);
1484
1485 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1486 flush = true;
1487 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001488 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Christoph Hellwig71102302016-07-06 21:55:52 +09001489 if (ret) {
1490 nvme_rdma_unmap_data(queue, rq);
1491 goto err;
1492 }
1493
1494 return BLK_MQ_RQ_QUEUE_OK;
1495err:
1496 return (ret == -ENOMEM || ret == -EAGAIN) ?
1497 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1498}
1499
1500static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1501{
1502 struct nvme_rdma_queue *queue = hctx->driver_data;
1503 struct ib_cq *cq = queue->ib_cq;
1504 struct ib_wc wc;
1505 int found = 0;
1506
1507 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1508 while (ib_poll_cq(cq, 1, &wc) > 0) {
1509 struct ib_cqe *cqe = wc.wr_cqe;
1510
1511 if (cqe) {
1512 if (cqe->done == nvme_rdma_recv_done)
1513 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1514 else
1515 cqe->done(cq, &wc);
1516 }
1517 }
1518
1519 return found;
1520}
1521
1522static void nvme_rdma_complete_rq(struct request *rq)
1523{
1524 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1525 struct nvme_rdma_queue *queue = req->queue;
1526 int error = 0;
1527
1528 nvme_rdma_unmap_data(queue, rq);
1529
1530 if (unlikely(rq->errors)) {
1531 if (nvme_req_needs_retry(rq, rq->errors)) {
1532 nvme_requeue_req(rq);
1533 return;
1534 }
1535
1536 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1537 error = rq->errors;
1538 else
1539 error = nvme_error_status(rq->errors);
1540 }
1541
1542 blk_mq_end_request(rq, error);
1543}
1544
1545static struct blk_mq_ops nvme_rdma_mq_ops = {
1546 .queue_rq = nvme_rdma_queue_rq,
1547 .complete = nvme_rdma_complete_rq,
1548 .map_queue = blk_mq_map_queue,
1549 .init_request = nvme_rdma_init_request,
1550 .exit_request = nvme_rdma_exit_request,
1551 .reinit_request = nvme_rdma_reinit_request,
1552 .init_hctx = nvme_rdma_init_hctx,
1553 .poll = nvme_rdma_poll,
1554 .timeout = nvme_rdma_timeout,
1555};
1556
1557static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1558 .queue_rq = nvme_rdma_queue_rq,
1559 .complete = nvme_rdma_complete_rq,
1560 .map_queue = blk_mq_map_queue,
1561 .init_request = nvme_rdma_init_admin_request,
1562 .exit_request = nvme_rdma_exit_admin_request,
1563 .reinit_request = nvme_rdma_reinit_request,
1564 .init_hctx = nvme_rdma_init_admin_hctx,
1565 .timeout = nvme_rdma_timeout,
1566};
1567
1568static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1569{
1570 int error;
1571
1572 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1573 if (error)
1574 return error;
1575
1576 ctrl->device = ctrl->queues[0].device;
1577
1578 /*
1579 * We need a reference on the device as long as the tag_set is alive,
1580 * as the MRs in the request structures need a valid ib_device.
1581 */
1582 error = -EINVAL;
1583 if (!nvme_rdma_dev_get(ctrl->device))
1584 goto out_free_queue;
1585
1586 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1587 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1588
1589 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1590 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1591 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1592 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1593 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1594 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1595 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1596 ctrl->admin_tag_set.driver_data = ctrl;
1597 ctrl->admin_tag_set.nr_hw_queues = 1;
1598 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1599
1600 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1601 if (error)
1602 goto out_put_dev;
1603
1604 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1605 if (IS_ERR(ctrl->ctrl.admin_q)) {
1606 error = PTR_ERR(ctrl->ctrl.admin_q);
1607 goto out_free_tagset;
1608 }
1609
1610 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1611 if (error)
1612 goto out_cleanup_queue;
1613
1614 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1615 if (error) {
1616 dev_err(ctrl->ctrl.device,
1617 "prop_get NVME_REG_CAP failed\n");
1618 goto out_cleanup_queue;
1619 }
1620
1621 ctrl->ctrl.sqsize =
1622 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1623
1624 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1625 if (error)
1626 goto out_cleanup_queue;
1627
1628 ctrl->ctrl.max_hw_sectors =
1629 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1630
1631 error = nvme_init_identify(&ctrl->ctrl);
1632 if (error)
1633 goto out_cleanup_queue;
1634
1635 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1636 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1637 DMA_TO_DEVICE);
1638 if (error)
1639 goto out_cleanup_queue;
1640
1641 nvme_start_keep_alive(&ctrl->ctrl);
1642
1643 return 0;
1644
1645out_cleanup_queue:
1646 blk_cleanup_queue(ctrl->ctrl.admin_q);
1647out_free_tagset:
1648 /* disconnect and drain the queue before freeing the tagset */
1649 nvme_rdma_stop_queue(&ctrl->queues[0]);
1650 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1651out_put_dev:
1652 nvme_rdma_dev_put(ctrl->device);
1653out_free_queue:
1654 nvme_rdma_free_queue(&ctrl->queues[0]);
1655 return error;
1656}
1657
1658static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1659{
1660 nvme_stop_keep_alive(&ctrl->ctrl);
1661 cancel_work_sync(&ctrl->err_work);
1662 cancel_delayed_work_sync(&ctrl->reconnect_work);
1663
1664 if (ctrl->queue_count > 1) {
1665 nvme_stop_queues(&ctrl->ctrl);
1666 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1667 nvme_cancel_request, &ctrl->ctrl);
1668 nvme_rdma_free_io_queues(ctrl);
1669 }
1670
Sagi Grimberg45862eb2016-07-24 09:26:16 +03001671 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
Christoph Hellwig71102302016-07-06 21:55:52 +09001672 nvme_shutdown_ctrl(&ctrl->ctrl);
1673
1674 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1675 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1676 nvme_cancel_request, &ctrl->ctrl);
1677 nvme_rdma_destroy_admin_queue(ctrl);
1678}
1679
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001680static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1681{
1682 nvme_uninit_ctrl(&ctrl->ctrl);
1683 if (shutdown)
1684 nvme_rdma_shutdown_ctrl(ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001685
1686 if (ctrl->ctrl.tagset) {
1687 blk_cleanup_queue(ctrl->ctrl.connect_q);
1688 blk_mq_free_tag_set(&ctrl->tag_set);
1689 nvme_rdma_dev_put(ctrl->device);
1690 }
1691
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001692 nvme_put_ctrl(&ctrl->ctrl);
1693}
1694
Christoph Hellwig71102302016-07-06 21:55:52 +09001695static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1696{
1697 struct nvme_rdma_ctrl *ctrl = container_of(work,
1698 struct nvme_rdma_ctrl, delete_work);
1699
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001700 __nvme_rdma_remove_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001701}
1702
1703static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1704{
1705 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1706 return -EBUSY;
1707
1708 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1709 return -EBUSY;
1710
1711 return 0;
1712}
1713
1714static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1715{
1716 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001717 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001718
Steve Wisecdbecc82016-09-01 09:12:25 -07001719 /*
1720 * Keep a reference until all work is flushed since
1721 * __nvme_rdma_del_ctrl can free the ctrl mem
1722 */
1723 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1724 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001725 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001726 if (!ret)
1727 flush_work(&ctrl->delete_work);
1728 nvme_put_ctrl(&ctrl->ctrl);
1729 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001730}
1731
1732static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1733{
1734 struct nvme_rdma_ctrl *ctrl = container_of(work,
1735 struct nvme_rdma_ctrl, delete_work);
1736
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001737 __nvme_rdma_remove_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001738}
1739
1740static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1741{
1742 struct nvme_rdma_ctrl *ctrl = container_of(work,
1743 struct nvme_rdma_ctrl, reset_work);
1744 int ret;
1745 bool changed;
1746
1747 nvme_rdma_shutdown_ctrl(ctrl);
1748
1749 ret = nvme_rdma_configure_admin_queue(ctrl);
1750 if (ret) {
1751 /* ctrl is already shutdown, just remove the ctrl */
1752 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1753 goto del_dead_ctrl;
1754 }
1755
1756 if (ctrl->queue_count > 1) {
1757 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1758 if (ret)
1759 goto del_dead_ctrl;
1760
1761 ret = nvme_rdma_init_io_queues(ctrl);
1762 if (ret)
1763 goto del_dead_ctrl;
1764
1765 ret = nvme_rdma_connect_io_queues(ctrl);
1766 if (ret)
1767 goto del_dead_ctrl;
1768 }
1769
1770 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1771 WARN_ON_ONCE(!changed);
1772
1773 if (ctrl->queue_count > 1) {
1774 nvme_start_queues(&ctrl->ctrl);
1775 nvme_queue_scan(&ctrl->ctrl);
Sagi Grimberg3ef1b4b2016-08-04 13:46:19 +03001776 nvme_queue_async_events(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001777 }
1778
1779 return;
1780
1781del_dead_ctrl:
1782 /* Deleting this dead controller... */
1783 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1784 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1785}
1786
1787static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1788{
1789 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1790
1791 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1792 return -EBUSY;
1793
1794 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1795 return -EBUSY;
1796
1797 flush_work(&ctrl->reset_work);
1798
1799 return 0;
1800}
1801
1802static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1803 .name = "rdma",
1804 .module = THIS_MODULE,
1805 .is_fabrics = true,
1806 .reg_read32 = nvmf_reg_read32,
1807 .reg_read64 = nvmf_reg_read64,
1808 .reg_write32 = nvmf_reg_write32,
1809 .reset_ctrl = nvme_rdma_reset_ctrl,
1810 .free_ctrl = nvme_rdma_free_ctrl,
1811 .submit_async_event = nvme_rdma_submit_async_event,
1812 .delete_ctrl = nvme_rdma_del_ctrl,
1813 .get_subsysnqn = nvmf_get_subsysnqn,
1814 .get_address = nvmf_get_address,
1815};
1816
1817static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1818{
1819 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1820 int ret;
1821
1822 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1823 if (ret)
1824 return ret;
1825
1826 ctrl->queue_count = opts->nr_io_queues + 1;
1827 if (ctrl->queue_count < 2)
1828 return 0;
1829
1830 dev_info(ctrl->ctrl.device,
1831 "creating %d I/O queues.\n", opts->nr_io_queues);
1832
1833 ret = nvme_rdma_init_io_queues(ctrl);
1834 if (ret)
1835 return ret;
1836
1837 /*
1838 * We need a reference on the device as long as the tag_set is alive,
1839 * as the MRs in the request structures need a valid ib_device.
1840 */
1841 ret = -EINVAL;
1842 if (!nvme_rdma_dev_get(ctrl->device))
1843 goto out_free_io_queues;
1844
1845 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1846 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001847 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
Christoph Hellwig71102302016-07-06 21:55:52 +09001848 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1849 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1850 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1851 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1852 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1853 ctrl->tag_set.driver_data = ctrl;
1854 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1855 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1856
1857 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1858 if (ret)
1859 goto out_put_dev;
1860 ctrl->ctrl.tagset = &ctrl->tag_set;
1861
1862 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1863 if (IS_ERR(ctrl->ctrl.connect_q)) {
1864 ret = PTR_ERR(ctrl->ctrl.connect_q);
1865 goto out_free_tag_set;
1866 }
1867
1868 ret = nvme_rdma_connect_io_queues(ctrl);
1869 if (ret)
1870 goto out_cleanup_connect_q;
1871
1872 return 0;
1873
1874out_cleanup_connect_q:
1875 blk_cleanup_queue(ctrl->ctrl.connect_q);
1876out_free_tag_set:
1877 blk_mq_free_tag_set(&ctrl->tag_set);
1878out_put_dev:
1879 nvme_rdma_dev_put(ctrl->device);
1880out_free_io_queues:
1881 nvme_rdma_free_io_queues(ctrl);
1882 return ret;
1883}
1884
1885static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1886{
1887 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1888 size_t buflen = strlen(p);
1889
1890 /* XXX: handle IPv6 addresses */
1891
1892 if (buflen > INET_ADDRSTRLEN)
1893 return -EINVAL;
1894 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1895 return -EINVAL;
1896 in_addr->sin_family = AF_INET;
1897 return 0;
1898}
1899
1900static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1901 struct nvmf_ctrl_options *opts)
1902{
1903 struct nvme_rdma_ctrl *ctrl;
1904 int ret;
1905 bool changed;
1906
1907 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1908 if (!ctrl)
1909 return ERR_PTR(-ENOMEM);
1910 ctrl->ctrl.opts = opts;
1911 INIT_LIST_HEAD(&ctrl->list);
1912
1913 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1914 if (ret) {
1915 pr_err("malformed IP address passed: %s\n", opts->traddr);
1916 goto out_free_ctrl;
1917 }
1918
1919 if (opts->mask & NVMF_OPT_TRSVCID) {
1920 u16 port;
1921
1922 ret = kstrtou16(opts->trsvcid, 0, &port);
1923 if (ret)
1924 goto out_free_ctrl;
1925
1926 ctrl->addr_in.sin_port = cpu_to_be16(port);
1927 } else {
1928 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1929 }
1930
1931 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1932 0 /* no quirks, we're perfect! */);
1933 if (ret)
1934 goto out_free_ctrl;
1935
1936 ctrl->reconnect_delay = opts->reconnect_delay;
1937 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1938 nvme_rdma_reconnect_ctrl_work);
1939 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1940 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1941 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1942 spin_lock_init(&ctrl->lock);
1943
1944 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001945 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001946 ctrl->ctrl.kato = opts->kato;
1947
1948 ret = -ENOMEM;
1949 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1950 GFP_KERNEL);
1951 if (!ctrl->queues)
1952 goto out_uninit_ctrl;
1953
1954 ret = nvme_rdma_configure_admin_queue(ctrl);
1955 if (ret)
1956 goto out_kfree_queues;
1957
1958 /* sanity check icdoff */
1959 if (ctrl->ctrl.icdoff) {
1960 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1961 goto out_remove_admin_queue;
1962 }
1963
1964 /* sanity check keyed sgls */
1965 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1966 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1967 goto out_remove_admin_queue;
1968 }
1969
1970 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1971 /* warn if maxcmd is lower than queue_size */
1972 dev_warn(ctrl->ctrl.device,
1973 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1974 opts->queue_size, ctrl->ctrl.maxcmd);
1975 opts->queue_size = ctrl->ctrl.maxcmd;
1976 }
1977
1978 if (opts->nr_io_queues) {
1979 ret = nvme_rdma_create_io_queues(ctrl);
1980 if (ret)
1981 goto out_remove_admin_queue;
1982 }
1983
1984 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1985 WARN_ON_ONCE(!changed);
1986
1987 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1988 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1989
1990 kref_get(&ctrl->ctrl.kref);
1991
1992 mutex_lock(&nvme_rdma_ctrl_mutex);
1993 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1994 mutex_unlock(&nvme_rdma_ctrl_mutex);
1995
1996 if (opts->nr_io_queues) {
1997 nvme_queue_scan(&ctrl->ctrl);
1998 nvme_queue_async_events(&ctrl->ctrl);
1999 }
2000
2001 return &ctrl->ctrl;
2002
2003out_remove_admin_queue:
2004 nvme_stop_keep_alive(&ctrl->ctrl);
2005 nvme_rdma_destroy_admin_queue(ctrl);
2006out_kfree_queues:
2007 kfree(ctrl->queues);
2008out_uninit_ctrl:
2009 nvme_uninit_ctrl(&ctrl->ctrl);
2010 nvme_put_ctrl(&ctrl->ctrl);
2011 if (ret > 0)
2012 ret = -EIO;
2013 return ERR_PTR(ret);
2014out_free_ctrl:
2015 kfree(ctrl);
2016 return ERR_PTR(ret);
2017}
2018
2019static struct nvmf_transport_ops nvme_rdma_transport = {
2020 .name = "rdma",
2021 .required_opts = NVMF_OPT_TRADDR,
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03002022 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
Christoph Hellwig71102302016-07-06 21:55:52 +09002023 .create_ctrl = nvme_rdma_create_ctrl,
2024};
2025
2026static int __init nvme_rdma_init_module(void)
2027{
2028 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2029 if (!nvme_rdma_wq)
2030 return -ENOMEM;
2031
2032 nvmf_register_transport(&nvme_rdma_transport);
2033 return 0;
2034}
2035
2036static void __exit nvme_rdma_cleanup_module(void)
2037{
2038 struct nvme_rdma_ctrl *ctrl;
2039
2040 nvmf_unregister_transport(&nvme_rdma_transport);
2041
2042 mutex_lock(&nvme_rdma_ctrl_mutex);
2043 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2044 __nvme_rdma_del_ctrl(ctrl);
2045 mutex_unlock(&nvme_rdma_ctrl_mutex);
2046
2047 destroy_workqueue(nvme_rdma_wq);
2048}
2049
2050module_init(nvme_rdma_init_module);
2051module_exit(nvme_rdma_cleanup_module);
2052
2053MODULE_LICENSE("GPL v2");