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