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