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