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