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