blob: ff4635d317b5bfa1a7ce9f52d05ac969a4bbf04f [file] [log] [blame]
Christoph Hellwig71102302016-07-06 21:55:52 +09001/*
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
Christoph Hellwig71102302016-07-06 21:55:52 +090015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/err.h>
19#include <linux/string.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090020#include <linux/atomic.h>
21#include <linux/blk-mq.h>
Sagi Grimberg0b366582017-07-13 11:09:44 +030022#include <linux/blk-mq-rdma.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090023#include <linux/types.h>
24#include <linux/list.h>
25#include <linux/mutex.h>
26#include <linux/scatterlist.h>
27#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090028#include <asm/unaligned.h>
29
30#include <rdma/ib_verbs.h>
31#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090032#include <linux/nvme-rdma.h>
33
34#include "nvme.h"
35#include "fabrics.h"
36
37
Sagi Grimberg782d8202017-03-21 16:32:38 +020038#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090039
Christoph Hellwig71102302016-07-06 21:55:52 +090040#define NVME_RDMA_MAX_SEGMENTS 256
41
42#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
43
Christoph Hellwig71102302016-07-06 21:55:52 +090044/*
45 * We handle AEN commands ourselves and don't even let the
46 * block layer know about them.
47 */
48#define NVME_RDMA_NR_AEN_COMMANDS 1
49#define NVME_RDMA_AQ_BLKMQ_DEPTH \
Sagi Grimberg7aa1f422017-06-18 16:15:59 +030050 (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
Christoph Hellwig71102302016-07-06 21:55:52 +090051
52struct nvme_rdma_device {
Max Gurtovoyf87c89a2017-10-23 12:59:27 +030053 struct ib_device *dev;
54 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090055 struct kref ref;
56 struct list_head entry;
57};
58
59struct nvme_rdma_qe {
60 struct ib_cqe cqe;
61 void *data;
62 u64 dma;
63};
64
65struct nvme_rdma_queue;
66struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080067 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090068 struct ib_mr *mr;
69 struct nvme_rdma_qe sqe;
70 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
71 u32 num_sge;
72 int nents;
73 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090074 struct ib_reg_wr reg_wr;
75 struct ib_cqe reg_cqe;
76 struct nvme_rdma_queue *queue;
77 struct sg_table sg_table;
78 struct scatterlist first_sgl[];
79};
80
81enum nvme_rdma_queue_flags {
Sagi Grimberg5013e982017-10-11 15:29:12 +030082 NVME_RDMA_Q_ALLOCATED = 0,
83 NVME_RDMA_Q_LIVE = 1,
Christoph Hellwig71102302016-07-06 21:55:52 +090084};
85
86struct nvme_rdma_queue {
87 struct nvme_rdma_qe *rsp_ring;
Marta Rybczynska5e599d72017-06-06 13:27:21 +020088 atomic_t sig_count;
Christoph Hellwig71102302016-07-06 21:55:52 +090089 int queue_size;
90 size_t cmnd_capsule_len;
91 struct nvme_rdma_ctrl *ctrl;
92 struct nvme_rdma_device *device;
93 struct ib_cq *ib_cq;
94 struct ib_qp *qp;
95
96 unsigned long flags;
97 struct rdma_cm_id *cm_id;
98 int cm_error;
99 struct completion cm_done;
100};
101
102struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +0900103 /* read only in the hot path */
104 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900105
106 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900107 struct blk_mq_tag_set tag_set;
108 struct work_struct delete_work;
Christoph Hellwig71102302016-07-06 21:55:52 +0900109 struct work_struct err_work;
110
111 struct nvme_rdma_qe async_event_sqe;
112
Christoph Hellwig71102302016-07-06 21:55:52 +0900113 struct delayed_work reconnect_work;
114
115 struct list_head list;
116
117 struct blk_mq_tag_set admin_tag_set;
118 struct nvme_rdma_device *device;
119
Christoph Hellwig71102302016-07-06 21:55:52 +0900120 u32 max_fr_pages;
121
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200122 struct sockaddr_storage addr;
123 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900124
125 struct nvme_ctrl ctrl;
126};
127
128static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
129{
130 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
131}
132
133static LIST_HEAD(device_list);
134static DEFINE_MUTEX(device_list_mutex);
135
136static LIST_HEAD(nvme_rdma_ctrl_list);
137static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
138
Christoph Hellwig71102302016-07-06 21:55:52 +0900139/*
140 * Disabling this option makes small I/O goes faster, but is fundamentally
141 * unsafe. With it turned off we will have to register a global rkey that
142 * allows read and write access to all physical memory.
143 */
144static bool register_always = true;
145module_param(register_always, bool, 0444);
146MODULE_PARM_DESC(register_always,
147 "Use memory registration even for contiguous memory regions");
148
149static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
150 struct rdma_cm_event *event);
151static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900152
Sagi Grimberg90af3512017-07-10 09:22:28 +0300153static const struct blk_mq_ops nvme_rdma_mq_ops;
154static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
155
Christoph Hellwig71102302016-07-06 21:55:52 +0900156/* XXX: really should move to a generic header sooner or later.. */
157static inline void put_unaligned_le24(u32 val, u8 *p)
158{
159 *p++ = val;
160 *p++ = val >> 8;
161 *p++ = val >> 16;
162}
163
164static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
165{
166 return queue - queue->ctrl->queues;
167}
168
169static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
170{
171 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
172}
173
174static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
175 size_t capsule_size, enum dma_data_direction dir)
176{
177 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
178 kfree(qe->data);
179}
180
181static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
182 size_t capsule_size, enum dma_data_direction dir)
183{
184 qe->data = kzalloc(capsule_size, GFP_KERNEL);
185 if (!qe->data)
186 return -ENOMEM;
187
188 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
189 if (ib_dma_mapping_error(ibdev, qe->dma)) {
190 kfree(qe->data);
191 return -ENOMEM;
192 }
193
194 return 0;
195}
196
197static void nvme_rdma_free_ring(struct ib_device *ibdev,
198 struct nvme_rdma_qe *ring, size_t ib_queue_size,
199 size_t capsule_size, enum dma_data_direction dir)
200{
201 int i;
202
203 for (i = 0; i < ib_queue_size; i++)
204 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
205 kfree(ring);
206}
207
208static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
209 size_t ib_queue_size, size_t capsule_size,
210 enum dma_data_direction dir)
211{
212 struct nvme_rdma_qe *ring;
213 int i;
214
215 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
216 if (!ring)
217 return NULL;
218
219 for (i = 0; i < ib_queue_size; i++) {
220 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
221 goto out_free_ring;
222 }
223
224 return ring;
225
226out_free_ring:
227 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
228 return NULL;
229}
230
231static void nvme_rdma_qp_event(struct ib_event *event, void *context)
232{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200233 pr_debug("QP event %s (%d)\n",
234 ib_event_msg(event->event), event->event);
235
Christoph Hellwig71102302016-07-06 21:55:52 +0900236}
237
238static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
239{
240 wait_for_completion_interruptible_timeout(&queue->cm_done,
241 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
242 return queue->cm_error;
243}
244
245static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
246{
247 struct nvme_rdma_device *dev = queue->device;
248 struct ib_qp_init_attr init_attr;
249 int ret;
250
251 memset(&init_attr, 0, sizeof(init_attr));
252 init_attr.event_handler = nvme_rdma_qp_event;
253 /* +1 for drain */
254 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
255 /* +1 for drain */
256 init_attr.cap.max_recv_wr = queue->queue_size + 1;
257 init_attr.cap.max_recv_sge = 1;
258 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
259 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
260 init_attr.qp_type = IB_QPT_RC;
261 init_attr.send_cq = queue->ib_cq;
262 init_attr.recv_cq = queue->ib_cq;
263
264 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
265
266 queue->qp = queue->cm_id->qp;
267 return ret;
268}
269
270static int nvme_rdma_reinit_request(void *data, struct request *rq)
271{
272 struct nvme_rdma_ctrl *ctrl = data;
273 struct nvme_rdma_device *dev = ctrl->device;
274 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
275 int ret = 0;
276
Sagi Grimberg0fc176d2017-10-11 15:29:09 +0300277 if (WARN_ON_ONCE(!req->mr))
278 return 0;
279
Christoph Hellwig71102302016-07-06 21:55:52 +0900280 ib_dereg_mr(req->mr);
281
282 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
283 ctrl->max_fr_pages);
284 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900285 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000286 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100287 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900288 }
289
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300290 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900291
292out:
293 return ret;
294}
295
Christoph Hellwig385475e2017-06-13 09:15:19 +0200296static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
297 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900298{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200299 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900300 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200301 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900302 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
303 struct nvme_rdma_device *dev = queue->device;
304
305 if (req->mr)
306 ib_dereg_mr(req->mr);
307
308 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
309 DMA_TO_DEVICE);
310}
311
Christoph Hellwig385475e2017-06-13 09:15:19 +0200312static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
313 struct request *rq, unsigned int hctx_idx,
314 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900315{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200316 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900317 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200318 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900319 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
320 struct nvme_rdma_device *dev = queue->device;
321 struct ib_device *ibdev = dev->dev;
322 int ret;
323
Christoph Hellwig71102302016-07-06 21:55:52 +0900324 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
325 DMA_TO_DEVICE);
326 if (ret)
327 return ret;
328
329 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
330 ctrl->max_fr_pages);
331 if (IS_ERR(req->mr)) {
332 ret = PTR_ERR(req->mr);
333 goto out_free_qe;
334 }
335
336 req->queue = queue;
337
338 return 0;
339
340out_free_qe:
341 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
342 DMA_TO_DEVICE);
343 return -ENOMEM;
344}
345
Christoph Hellwig71102302016-07-06 21:55:52 +0900346static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
347 unsigned int hctx_idx)
348{
349 struct nvme_rdma_ctrl *ctrl = data;
350 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
351
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300352 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900353
354 hctx->driver_data = queue;
355 return 0;
356}
357
358static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
359 unsigned int hctx_idx)
360{
361 struct nvme_rdma_ctrl *ctrl = data;
362 struct nvme_rdma_queue *queue = &ctrl->queues[0];
363
364 BUG_ON(hctx_idx != 0);
365
366 hctx->driver_data = queue;
367 return 0;
368}
369
370static void nvme_rdma_free_dev(struct kref *ref)
371{
372 struct nvme_rdma_device *ndev =
373 container_of(ref, struct nvme_rdma_device, ref);
374
375 mutex_lock(&device_list_mutex);
376 list_del(&ndev->entry);
377 mutex_unlock(&device_list_mutex);
378
Christoph Hellwig71102302016-07-06 21:55:52 +0900379 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900380 kfree(ndev);
381}
382
383static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
384{
385 kref_put(&dev->ref, nvme_rdma_free_dev);
386}
387
388static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
389{
390 return kref_get_unless_zero(&dev->ref);
391}
392
393static struct nvme_rdma_device *
394nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
395{
396 struct nvme_rdma_device *ndev;
397
398 mutex_lock(&device_list_mutex);
399 list_for_each_entry(ndev, &device_list, entry) {
400 if (ndev->dev->node_guid == cm_id->device->node_guid &&
401 nvme_rdma_dev_get(ndev))
402 goto out_unlock;
403 }
404
405 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
406 if (!ndev)
407 goto out_err;
408
409 ndev->dev = cm_id->device;
410 kref_init(&ndev->ref);
411
Christoph Hellwig11975e02016-09-05 12:56:20 +0200412 ndev->pd = ib_alloc_pd(ndev->dev,
413 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900414 if (IS_ERR(ndev->pd))
415 goto out_free_dev;
416
Christoph Hellwig71102302016-07-06 21:55:52 +0900417 if (!(ndev->dev->attrs.device_cap_flags &
418 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
419 dev_err(&ndev->dev->dev,
420 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200421 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900422 }
423
424 list_add(&ndev->entry, &device_list);
425out_unlock:
426 mutex_unlock(&device_list_mutex);
427 return ndev;
428
Christoph Hellwig71102302016-07-06 21:55:52 +0900429out_free_pd:
430 ib_dealloc_pd(ndev->pd);
431out_free_dev:
432 kfree(ndev);
433out_err:
434 mutex_unlock(&device_list_mutex);
435 return NULL;
436}
437
438static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
439{
Sagi Grimberg0c5b43b2017-10-11 15:29:08 +0300440 struct nvme_rdma_device *dev = queue->device;
441 struct ib_device *ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900442
443 rdma_destroy_qp(queue->cm_id);
444 ib_free_cq(queue->ib_cq);
445
446 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
447 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
448
449 nvme_rdma_dev_put(dev);
450}
451
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300452static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900453{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300454 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900455 const int send_wr_factor = 3; /* MR, SEND, INV */
456 const int cq_factor = send_wr_factor + 1; /* + RECV */
457 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900458 int ret;
459
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300460 queue->device = nvme_rdma_find_get_device(queue->cm_id);
461 if (!queue->device) {
462 dev_err(queue->cm_id->device->dev.parent,
463 "no client data found!\n");
464 return -ECONNREFUSED;
465 }
466 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900467
468 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300469 * Spread I/O queues completion vectors according their queue index.
470 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900471 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300472 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900473
474 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300475 queue->ib_cq = ib_alloc_cq(ibdev, queue,
476 cq_factor * queue->queue_size + 1,
477 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900478 if (IS_ERR(queue->ib_cq)) {
479 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300480 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900481 }
482
483 ret = nvme_rdma_create_qp(queue, send_wr_factor);
484 if (ret)
485 goto out_destroy_ib_cq;
486
487 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
488 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
489 if (!queue->rsp_ring) {
490 ret = -ENOMEM;
491 goto out_destroy_qp;
492 }
493
494 return 0;
495
496out_destroy_qp:
497 ib_destroy_qp(queue->qp);
498out_destroy_ib_cq:
499 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300500out_put_dev:
501 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900502 return ret;
503}
504
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300505static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900506 int idx, size_t queue_size)
507{
508 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200509 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900510 int ret;
511
512 queue = &ctrl->queues[idx];
513 queue->ctrl = ctrl;
514 init_completion(&queue->cm_done);
515
516 if (idx > 0)
517 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
518 else
519 queue->cmnd_capsule_len = sizeof(struct nvme_command);
520
521 queue->queue_size = queue_size;
Marta Rybczynska5e599d72017-06-06 13:27:21 +0200522 atomic_set(&queue->sig_count, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900523
524 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
525 RDMA_PS_TCP, IB_QPT_RC);
526 if (IS_ERR(queue->cm_id)) {
527 dev_info(ctrl->ctrl.device,
528 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
529 return PTR_ERR(queue->cm_id);
530 }
531
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200532 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200533 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200534
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200535 queue->cm_error = -ETIMEDOUT;
536 ret = rdma_resolve_addr(queue->cm_id, src_addr,
537 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900538 NVME_RDMA_CONNECT_TIMEOUT_MS);
539 if (ret) {
540 dev_info(ctrl->ctrl.device,
541 "rdma_resolve_addr failed (%d).\n", ret);
542 goto out_destroy_cm_id;
543 }
544
545 ret = nvme_rdma_wait_for_cm(queue);
546 if (ret) {
547 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300548 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900549 goto out_destroy_cm_id;
550 }
551
Sagi Grimberg5013e982017-10-11 15:29:12 +0300552 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900553
554 return 0;
555
556out_destroy_cm_id:
557 rdma_destroy_id(queue->cm_id);
558 return ret;
559}
560
561static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
562{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200563 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
564 return;
565
Christoph Hellwig71102302016-07-06 21:55:52 +0900566 rdma_disconnect(queue->cm_id);
567 ib_drain_qp(queue->qp);
568}
569
570static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
571{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300572 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900573 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200574
575 nvme_rdma_destroy_queue_ib(queue);
576 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900577}
578
579static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
580{
581 int i;
582
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300583 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200584 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900585}
586
Sagi Grimberga57bd542017-08-28 21:41:10 +0200587static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
588{
589 int i;
590
591 for (i = 1; i < ctrl->ctrl.queue_count; i++)
592 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900593}
594
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300595static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
596{
597 int ret;
598
599 if (idx)
600 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
601 else
602 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
603
604 if (!ret)
605 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
606 else
607 dev_info(ctrl->ctrl.device,
608 "failed to connect queue: %d ret=%d\n", idx, ret);
609 return ret;
610}
611
612static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900613{
614 int i, ret = 0;
615
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300616 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300617 ret = nvme_rdma_start_queue(ctrl, i);
618 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200619 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900620 }
621
Steve Wisec8dbc372016-11-08 09:16:02 -0800622 return 0;
623
Sagi Grimberga57bd542017-08-28 21:41:10 +0200624out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300625 for (i--; i >= 1; i--)
626 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900627 return ret;
628}
629
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300630static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900631{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200632 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300633 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200634 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900635 int i, ret;
636
Sagi Grimbergc248c642017-03-09 13:26:07 +0200637 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300638
639 /*
640 * we map queues according to the device irq vectors for
641 * optimal locality so we don't need more queues than
642 * completion vectors.
643 */
644 nr_io_queues = min_t(unsigned int, nr_io_queues,
645 ibdev->num_comp_vectors);
646
Sagi Grimbergc248c642017-03-09 13:26:07 +0200647 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
648 if (ret)
649 return ret;
650
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300651 ctrl->ctrl.queue_count = nr_io_queues + 1;
652 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200653 return 0;
654
655 dev_info(ctrl->ctrl.device,
656 "creating %d I/O queues.\n", nr_io_queues);
657
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300658 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300659 ret = nvme_rdma_alloc_queue(ctrl, i,
660 ctrl->ctrl.sqsize + 1);
661 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900662 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900663 }
664
665 return 0;
666
667out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700668 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200669 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900670
671 return ret;
672}
673
Sagi Grimberg60070c72017-10-11 15:29:06 +0300674static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
675 struct blk_mq_tag_set *set)
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300676{
677 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300678
679 blk_mq_free_tag_set(set);
680 nvme_rdma_dev_put(ctrl->device);
681}
682
683static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
684 bool admin)
685{
686 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
687 struct blk_mq_tag_set *set;
688 int ret;
689
690 if (admin) {
691 set = &ctrl->admin_tag_set;
692 memset(set, 0, sizeof(*set));
693 set->ops = &nvme_rdma_admin_mq_ops;
694 set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
695 set->reserved_tags = 2; /* connect + keep-alive */
696 set->numa_node = NUMA_NO_NODE;
697 set->cmd_size = sizeof(struct nvme_rdma_request) +
698 SG_CHUNK_SIZE * sizeof(struct scatterlist);
699 set->driver_data = ctrl;
700 set->nr_hw_queues = 1;
701 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000702 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300703 } else {
704 set = &ctrl->tag_set;
705 memset(set, 0, sizeof(*set));
706 set->ops = &nvme_rdma_mq_ops;
707 set->queue_depth = nctrl->opts->queue_size;
708 set->reserved_tags = 1; /* fabric connect */
709 set->numa_node = NUMA_NO_NODE;
710 set->flags = BLK_MQ_F_SHOULD_MERGE;
711 set->cmd_size = sizeof(struct nvme_rdma_request) +
712 SG_CHUNK_SIZE * sizeof(struct scatterlist);
713 set->driver_data = ctrl;
714 set->nr_hw_queues = nctrl->queue_count - 1;
715 set->timeout = NVME_IO_TIMEOUT;
716 }
717
718 ret = blk_mq_alloc_tag_set(set);
719 if (ret)
720 goto out;
721
722 /*
723 * We need a reference on the device as long as the tag_set is alive,
724 * as the MRs in the request structures need a valid ib_device.
725 */
726 ret = nvme_rdma_dev_get(ctrl->device);
727 if (!ret) {
728 ret = -EINVAL;
729 goto out_free_tagset;
730 }
731
732 return set;
733
734out_free_tagset:
735 blk_mq_free_tag_set(set);
736out:
737 return ERR_PTR(ret);
738}
739
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300740static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
741 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900742{
743 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
744 sizeof(struct nvme_command), DMA_TO_DEVICE);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200745 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300746 if (remove) {
747 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300748 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300749 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200750 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900751}
752
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300753static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
754 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300755{
756 int error;
757
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300758 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300759 if (error)
760 return error;
761
762 ctrl->device = ctrl->queues[0].device;
763
Sagi Grimberg90af3512017-07-10 09:22:28 +0300764 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
765 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
766
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300767 if (new) {
768 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
769 if (IS_ERR(ctrl->ctrl.admin_tagset))
770 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300771
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300772 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
773 if (IS_ERR(ctrl->ctrl.admin_q)) {
774 error = PTR_ERR(ctrl->ctrl.admin_q);
775 goto out_free_tagset;
776 }
777 } else {
Sagi Grimberg31b84462017-10-11 12:53:07 +0300778 error = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300779 if (error)
780 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300781 }
782
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300783 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300784 if (error)
785 goto out_cleanup_queue;
786
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300787 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300788 &ctrl->ctrl.cap);
789 if (error) {
790 dev_err(ctrl->ctrl.device,
791 "prop_get NVME_REG_CAP failed\n");
792 goto out_cleanup_queue;
793 }
794
795 ctrl->ctrl.sqsize =
796 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
797
798 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
799 if (error)
800 goto out_cleanup_queue;
801
802 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700803 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300804
805 error = nvme_init_identify(&ctrl->ctrl);
806 if (error)
807 goto out_cleanup_queue;
808
809 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
810 &ctrl->async_event_sqe, sizeof(struct nvme_command),
811 DMA_TO_DEVICE);
812 if (error)
813 goto out_cleanup_queue;
814
815 return 0;
816
817out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300818 if (new)
819 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300820out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300821 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300822 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300823out_free_queue:
824 nvme_rdma_free_queue(&ctrl->queues[0]);
825 return error;
826}
827
Sagi Grimberga57bd542017-08-28 21:41:10 +0200828static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
829 bool remove)
830{
831 nvme_rdma_stop_io_queues(ctrl);
832 if (remove) {
833 blk_cleanup_queue(ctrl->ctrl.connect_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300834 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200835 }
836 nvme_rdma_free_io_queues(ctrl);
837}
838
839static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
840{
841 int ret;
842
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300843 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200844 if (ret)
845 return ret;
846
847 if (new) {
848 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
849 if (IS_ERR(ctrl->ctrl.tagset))
850 goto out_free_io_queues;
851
852 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
853 if (IS_ERR(ctrl->ctrl.connect_q)) {
854 ret = PTR_ERR(ctrl->ctrl.connect_q);
855 goto out_free_tag_set;
856 }
857 } else {
Sagi Grimberg31b84462017-10-11 12:53:07 +0300858 ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200859 if (ret)
860 goto out_free_io_queues;
861
862 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
863 ctrl->ctrl.queue_count - 1);
864 }
865
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300866 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200867 if (ret)
868 goto out_cleanup_connect_q;
869
870 return 0;
871
872out_cleanup_connect_q:
873 if (new)
874 blk_cleanup_queue(ctrl->ctrl.connect_q);
875out_free_tag_set:
876 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300877 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200878out_free_io_queues:
879 nvme_rdma_free_io_queues(ctrl);
880 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900881}
882
883static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
884{
885 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
886
887 if (list_empty(&ctrl->list))
888 goto free_ctrl;
889
890 mutex_lock(&nvme_rdma_ctrl_mutex);
891 list_del(&ctrl->list);
892 mutex_unlock(&nvme_rdma_ctrl_mutex);
893
Christoph Hellwig71102302016-07-06 21:55:52 +0900894 kfree(ctrl->queues);
895 nvmf_free_options(nctrl->opts);
896free_ctrl:
897 kfree(ctrl);
898}
899
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200900static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
901{
902 /* If we are resetting/deleting then do nothing */
903 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
904 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
905 ctrl->ctrl.state == NVME_CTRL_LIVE);
906 return;
907 }
908
909 if (nvmf_should_reconnect(&ctrl->ctrl)) {
910 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
911 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200912 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200913 ctrl->ctrl.opts->reconnect_delay * HZ);
914 } else {
915 dev_info(ctrl->ctrl.device, "Removing controller...\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200916 queue_work(nvme_wq, &ctrl->delete_work);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200917 }
918}
919
Christoph Hellwig71102302016-07-06 21:55:52 +0900920static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
921{
922 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
923 struct nvme_rdma_ctrl, reconnect_work);
924 bool changed;
925 int ret;
926
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300927 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200928
Sagi Grimberg31fdf182017-08-28 21:40:06 +0200929 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900930 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300931 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900932
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300933 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200934 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900935 if (ret)
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300936 goto destroy_admin;
Christoph Hellwig71102302016-07-06 21:55:52 +0900937 }
938
939 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +0300940 if (!changed) {
941 /* state change failure is ok if we're in DELETING state */
942 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
943 return;
944 }
945
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300946 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900947
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300948 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
949 ctrl->ctrl.nr_reconnects);
950
951 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900952
953 return;
954
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300955destroy_admin:
956 nvme_rdma_destroy_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900957requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200958 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300959 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200960 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900961}
962
963static void nvme_rdma_error_recovery_work(struct work_struct *work)
964{
965 struct nvme_rdma_ctrl *ctrl = container_of(work,
966 struct nvme_rdma_ctrl, err_work);
967
Sagi Grimberge4d753d2017-09-21 17:01:38 +0300968 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700969
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300970 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900971 nvme_stop_queues(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900972 blk_mq_tagset_busy_iter(&ctrl->tag_set,
973 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300974 nvme_rdma_destroy_io_queues(ctrl, false);
975 }
976
977 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +0900978 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
979 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300980 nvme_rdma_destroy_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900981
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300982 /*
983 * queues are not a live anymore, so restart the queues to fail fast
984 * new IO
985 */
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300986 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300987 nvme_start_queues(&ctrl->ctrl);
988
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200989 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900990}
991
992static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
993{
994 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
995 return;
996
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200997 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +0900998}
999
1000static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1001 const char *op)
1002{
1003 struct nvme_rdma_queue *queue = cq->cq_context;
1004 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1005
1006 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1007 dev_info(ctrl->ctrl.device,
1008 "%s for CQE 0x%p failed with status %s (%d)\n",
1009 op, wc->wr_cqe,
1010 ib_wc_status_msg(wc->status), wc->status);
1011 nvme_rdma_error_recovery(ctrl);
1012}
1013
1014static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1015{
1016 if (unlikely(wc->status != IB_WC_SUCCESS))
1017 nvme_rdma_wr_error(cq, wc, "MEMREG");
1018}
1019
1020static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1021{
1022 if (unlikely(wc->status != IB_WC_SUCCESS))
1023 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1024}
1025
1026static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1027 struct nvme_rdma_request *req)
1028{
1029 struct ib_send_wr *bad_wr;
1030 struct ib_send_wr wr = {
1031 .opcode = IB_WR_LOCAL_INV,
1032 .next = NULL,
1033 .num_sge = 0,
1034 .send_flags = 0,
1035 .ex.invalidate_rkey = req->mr->rkey,
1036 };
1037
1038 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1039 wr.wr_cqe = &req->reg_cqe;
1040
1041 return ib_post_send(queue->qp, &wr, &bad_wr);
1042}
1043
1044static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1045 struct request *rq)
1046{
1047 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1048 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1049 struct nvme_rdma_device *dev = queue->device;
1050 struct ib_device *ibdev = dev->dev;
1051 int res;
1052
1053 if (!blk_rq_bytes(rq))
1054 return;
1055
Sagi Grimberg60a51882017-10-11 15:29:10 +03001056 if (req->mr->need_inval && test_bit(NVME_RDMA_Q_LIVE, &req->queue->flags)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001057 res = nvme_rdma_inv_rkey(queue, req);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001058 if (unlikely(res < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001059 dev_err(ctrl->ctrl.device,
1060 "Queueing INV WR for rkey %#x failed (%d)\n",
1061 req->mr->rkey, res);
1062 nvme_rdma_error_recovery(queue->ctrl);
1063 }
1064 }
1065
1066 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1067 req->nents, rq_data_dir(rq) ==
1068 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1069
1070 nvme_cleanup_cmd(rq);
1071 sg_free_table_chained(&req->sg_table, true);
1072}
1073
1074static int nvme_rdma_set_sg_null(struct nvme_command *c)
1075{
1076 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1077
1078 sg->addr = 0;
1079 put_unaligned_le24(0, sg->length);
1080 put_unaligned_le32(0, sg->key);
1081 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1082 return 0;
1083}
1084
1085static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1086 struct nvme_rdma_request *req, struct nvme_command *c)
1087{
1088 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1089
1090 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1091 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1092 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1093
1094 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1095 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1096 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1097
1098 req->inline_data = true;
1099 req->num_sge++;
1100 return 0;
1101}
1102
1103static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1104 struct nvme_rdma_request *req, struct nvme_command *c)
1105{
1106 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1107
1108 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1109 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001110 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001111 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1112 return 0;
1113}
1114
1115static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1116 struct nvme_rdma_request *req, struct nvme_command *c,
1117 int count)
1118{
1119 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1120 int nr;
1121
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001122 /*
1123 * Align the MR to a 4K page size to match the ctrl page size and
1124 * the block virtual boundary.
1125 */
1126 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001127 if (unlikely(nr < count)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001128 if (nr < 0)
1129 return nr;
1130 return -EINVAL;
1131 }
1132
1133 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1134
1135 req->reg_cqe.done = nvme_rdma_memreg_done;
1136 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1137 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1138 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1139 req->reg_wr.wr.num_sge = 0;
1140 req->reg_wr.mr = req->mr;
1141 req->reg_wr.key = req->mr->rkey;
1142 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1143 IB_ACCESS_REMOTE_READ |
1144 IB_ACCESS_REMOTE_WRITE;
1145
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001146 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001147
1148 sg->addr = cpu_to_le64(req->mr->iova);
1149 put_unaligned_le24(req->mr->length, sg->length);
1150 put_unaligned_le32(req->mr->rkey, sg->key);
1151 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1152 NVME_SGL_FMT_INVALIDATE;
1153
1154 return 0;
1155}
1156
1157static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001158 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001159{
1160 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1161 struct nvme_rdma_device *dev = queue->device;
1162 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001163 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001164
1165 req->num_sge = 1;
1166 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001167 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001168
1169 c->common.flags |= NVME_CMD_SGL_METABUF;
1170
1171 if (!blk_rq_bytes(rq))
1172 return nvme_rdma_set_sg_null(c);
1173
1174 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001175 ret = sg_alloc_table_chained(&req->sg_table,
1176 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001177 if (ret)
1178 return -ENOMEM;
1179
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001180 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001181
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001182 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001183 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1184 if (unlikely(count <= 0)) {
1185 sg_free_table_chained(&req->sg_table, true);
1186 return -EIO;
1187 }
1188
1189 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001190 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1191 blk_rq_payload_bytes(rq) <=
1192 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001193 return nvme_rdma_map_sg_inline(queue, req, c);
1194
Christoph Hellwig11975e02016-09-05 12:56:20 +02001195 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001196 return nvme_rdma_map_sg_single(queue, req, c);
1197 }
1198
1199 return nvme_rdma_map_sg_fr(queue, req, c, count);
1200}
1201
1202static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1203{
1204 if (unlikely(wc->status != IB_WC_SUCCESS))
1205 nvme_rdma_wr_error(cq, wc, "SEND");
1206}
1207
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001208/*
1209 * We want to signal completion at least every queue depth/2. This returns the
1210 * largest power of two that is not above half of (queue size + 1) to optimize
1211 * (avoid divisions).
1212 */
1213static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
Marta Rybczynska0544f542017-04-10 17:12:34 +02001214{
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001215 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
Marta Rybczynska0544f542017-04-10 17:12:34 +02001216
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001217 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
Marta Rybczynska0544f542017-04-10 17:12:34 +02001218}
1219
Christoph Hellwig71102302016-07-06 21:55:52 +09001220static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1221 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1222 struct ib_send_wr *first, bool flush)
1223{
1224 struct ib_send_wr wr, *bad_wr;
1225 int ret;
1226
1227 sge->addr = qe->dma;
1228 sge->length = sizeof(struct nvme_command),
1229 sge->lkey = queue->device->pd->local_dma_lkey;
1230
1231 qe->cqe.done = nvme_rdma_send_done;
1232
1233 wr.next = NULL;
1234 wr.wr_cqe = &qe->cqe;
1235 wr.sg_list = sge;
1236 wr.num_sge = num_sge;
1237 wr.opcode = IB_WR_SEND;
1238 wr.send_flags = 0;
1239
1240 /*
1241 * Unsignalled send completions are another giant desaster in the
1242 * IB Verbs spec: If we don't regularly post signalled sends
1243 * the send queue will fill up and only a QP reset will rescue us.
1244 * Would have been way to obvious to handle this in hardware or
1245 * at least the RDMA stack..
1246 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001247 * Always signal the flushes. The magic request used for the flush
1248 * sequencer is not allocated in our driver's tagset and it's
1249 * triggered to be freed by blk_cleanup_queue(). So we need to
1250 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001251 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001252 * calls wr_cqe->done().
1253 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001254 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001255 wr.send_flags |= IB_SEND_SIGNALED;
1256
1257 if (first)
1258 first->next = &wr;
1259 else
1260 first = &wr;
1261
1262 ret = ib_post_send(queue->qp, first, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001263 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001264 dev_err(queue->ctrl->ctrl.device,
1265 "%s failed with error code %d\n", __func__, ret);
1266 }
1267 return ret;
1268}
1269
1270static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1271 struct nvme_rdma_qe *qe)
1272{
1273 struct ib_recv_wr wr, *bad_wr;
1274 struct ib_sge list;
1275 int ret;
1276
1277 list.addr = qe->dma;
1278 list.length = sizeof(struct nvme_completion);
1279 list.lkey = queue->device->pd->local_dma_lkey;
1280
1281 qe->cqe.done = nvme_rdma_recv_done;
1282
1283 wr.next = NULL;
1284 wr.wr_cqe = &qe->cqe;
1285 wr.sg_list = &list;
1286 wr.num_sge = 1;
1287
1288 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001289 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001290 dev_err(queue->ctrl->ctrl.device,
1291 "%s failed with error code %d\n", __func__, ret);
1292 }
1293 return ret;
1294}
1295
1296static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1297{
1298 u32 queue_idx = nvme_rdma_queue_idx(queue);
1299
1300 if (queue_idx == 0)
1301 return queue->ctrl->admin_tag_set.tags[queue_idx];
1302 return queue->ctrl->tag_set.tags[queue_idx - 1];
1303}
1304
1305static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1306{
1307 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1308 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1309 struct ib_device *dev = queue->device->dev;
1310 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1311 struct nvme_command *cmd = sqe->data;
1312 struct ib_sge sge;
1313 int ret;
1314
1315 if (WARN_ON_ONCE(aer_idx != 0))
1316 return;
1317
1318 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1319
1320 memset(cmd, 0, sizeof(*cmd));
1321 cmd->common.opcode = nvme_admin_async_event;
1322 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1323 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1324 nvme_rdma_set_sg_null(cmd);
1325
1326 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1327 DMA_TO_DEVICE);
1328
1329 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1330 WARN_ON_ONCE(ret);
1331}
1332
1333static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1334 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1335{
Christoph Hellwig71102302016-07-06 21:55:52 +09001336 struct request *rq;
1337 struct nvme_rdma_request *req;
1338 int ret = 0;
1339
Christoph Hellwig71102302016-07-06 21:55:52 +09001340 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1341 if (!rq) {
1342 dev_err(queue->ctrl->ctrl.device,
1343 "tag 0x%x on QP %#x not found\n",
1344 cqe->command_id, queue->qp->qp_num);
1345 nvme_rdma_error_recovery(queue->ctrl);
1346 return ret;
1347 }
1348 req = blk_mq_rq_to_pdu(rq);
1349
Christoph Hellwig71102302016-07-06 21:55:52 +09001350 if (rq->tag == tag)
1351 ret = 1;
1352
1353 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1354 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001355 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001356
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001357 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001358 return ret;
1359}
1360
1361static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1362{
1363 struct nvme_rdma_qe *qe =
1364 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1365 struct nvme_rdma_queue *queue = cq->cq_context;
1366 struct ib_device *ibdev = queue->device->dev;
1367 struct nvme_completion *cqe = qe->data;
1368 const size_t len = sizeof(struct nvme_completion);
1369 int ret = 0;
1370
1371 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1372 nvme_rdma_wr_error(cq, wc, "RECV");
1373 return 0;
1374 }
1375
1376 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1377 /*
1378 * AEN requests are special as they don't time out and can
1379 * survive any kind of queue freeze and often don't respond to
1380 * aborts. We don't even bother to allocate a struct request
1381 * for them but rather special case them here.
1382 */
1383 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1384 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001385 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1386 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001387 else
1388 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1389 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1390
1391 nvme_rdma_post_recv(queue, qe);
1392 return ret;
1393}
1394
1395static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1396{
1397 __nvme_rdma_recv_done(cq, wc, -1);
1398}
1399
1400static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1401{
1402 int ret, i;
1403
1404 for (i = 0; i < queue->queue_size; i++) {
1405 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1406 if (ret)
1407 goto out_destroy_queue_ib;
1408 }
1409
1410 return 0;
1411
1412out_destroy_queue_ib:
1413 nvme_rdma_destroy_queue_ib(queue);
1414 return ret;
1415}
1416
1417static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1418 struct rdma_cm_event *ev)
1419{
Steve Wise7f039532016-10-26 12:36:47 -07001420 struct rdma_cm_id *cm_id = queue->cm_id;
1421 int status = ev->status;
1422 const char *rej_msg;
1423 const struct nvme_rdma_cm_rej *rej_data;
1424 u8 rej_data_len;
1425
1426 rej_msg = rdma_reject_msg(cm_id, status);
1427 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1428
1429 if (rej_data && rej_data_len >= sizeof(u16)) {
1430 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001431
1432 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001433 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1434 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001435 } else {
1436 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001437 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001438 }
1439
1440 return -ECONNRESET;
1441}
1442
1443static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1444{
Christoph Hellwig71102302016-07-06 21:55:52 +09001445 int ret;
1446
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001447 ret = nvme_rdma_create_queue_ib(queue);
1448 if (ret)
1449 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001450
1451 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1452 if (ret) {
1453 dev_err(queue->ctrl->ctrl.device,
1454 "rdma_resolve_route failed (%d).\n",
1455 queue->cm_error);
1456 goto out_destroy_queue;
1457 }
1458
1459 return 0;
1460
1461out_destroy_queue:
1462 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001463 return ret;
1464}
1465
1466static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1467{
1468 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1469 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001470 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001471 int ret;
1472
1473 param.qp_num = queue->qp->qp_num;
1474 param.flow_control = 1;
1475
1476 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001477 /* maximum retry count */
1478 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001479 param.rnr_retry_count = 7;
1480 param.private_data = &priv;
1481 param.private_data_len = sizeof(priv);
1482
1483 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1484 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001485 /*
1486 * set the admin queue depth to the minimum size
1487 * specified by the Fabrics standard.
1488 */
1489 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001490 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1491 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001492 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001493 /*
1494 * current interpretation of the fabrics spec
1495 * is at minimum you make hrqsize sqsize+1, or a
1496 * 1's based representation of sqsize.
1497 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001498 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001499 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001500 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001501
1502 ret = rdma_connect(queue->cm_id, &param);
1503 if (ret) {
1504 dev_err(ctrl->ctrl.device,
1505 "rdma_connect failed (%d).\n", ret);
1506 goto out_destroy_queue_ib;
1507 }
1508
1509 return 0;
1510
1511out_destroy_queue_ib:
1512 nvme_rdma_destroy_queue_ib(queue);
1513 return ret;
1514}
1515
Christoph Hellwig71102302016-07-06 21:55:52 +09001516static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1517 struct rdma_cm_event *ev)
1518{
1519 struct nvme_rdma_queue *queue = cm_id->context;
1520 int cm_error = 0;
1521
1522 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1523 rdma_event_msg(ev->event), ev->event,
1524 ev->status, cm_id);
1525
1526 switch (ev->event) {
1527 case RDMA_CM_EVENT_ADDR_RESOLVED:
1528 cm_error = nvme_rdma_addr_resolved(queue);
1529 break;
1530 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1531 cm_error = nvme_rdma_route_resolved(queue);
1532 break;
1533 case RDMA_CM_EVENT_ESTABLISHED:
1534 queue->cm_error = nvme_rdma_conn_established(queue);
1535 /* complete cm_done regardless of success/failure */
1536 complete(&queue->cm_done);
1537 return 0;
1538 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001539 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001540 cm_error = nvme_rdma_conn_rejected(queue, ev);
1541 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001542 case RDMA_CM_EVENT_ROUTE_ERROR:
1543 case RDMA_CM_EVENT_CONNECT_ERROR:
1544 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001545 nvme_rdma_destroy_queue_ib(queue);
1546 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001547 dev_dbg(queue->ctrl->ctrl.device,
1548 "CM error event %d\n", ev->event);
1549 cm_error = -ECONNRESET;
1550 break;
1551 case RDMA_CM_EVENT_DISCONNECTED:
1552 case RDMA_CM_EVENT_ADDR_CHANGE:
1553 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1554 dev_dbg(queue->ctrl->ctrl.device,
1555 "disconnect received - connection closed\n");
1556 nvme_rdma_error_recovery(queue->ctrl);
1557 break;
1558 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001559 /* device removal is handled via the ib_client API */
1560 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001561 default:
1562 dev_err(queue->ctrl->ctrl.device,
1563 "Unexpected RDMA CM event (%d)\n", ev->event);
1564 nvme_rdma_error_recovery(queue->ctrl);
1565 break;
1566 }
1567
1568 if (cm_error) {
1569 queue->cm_error = cm_error;
1570 complete(&queue->cm_done);
1571 }
1572
1573 return 0;
1574}
1575
1576static enum blk_eh_timer_return
1577nvme_rdma_timeout(struct request *rq, bool reserved)
1578{
1579 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1580
1581 /* queue error recovery */
1582 nvme_rdma_error_recovery(req->queue->ctrl);
1583
1584 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001585 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001586
1587 return BLK_EH_HANDLED;
1588}
1589
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001590/*
1591 * We cannot accept any other command until the Connect command has completed.
1592 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001593static inline blk_status_t
1594nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001595{
1596 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001597 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001598
Christoph Hellwig57292b52017-01-31 16:57:29 +01001599 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001600 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001601 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1602 /*
1603 * reconnecting state means transport disruption, which
1604 * can take a long time and even might fail permanently,
1605 * so we can't let incoming I/O be requeued forever.
1606 * fail it fast to allow upper layers a chance to
1607 * failover.
1608 */
1609 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001610 return BLK_STS_IOERR;
1611 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001612 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001613 }
1614
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001615 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001616}
1617
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001618static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001619 const struct blk_mq_queue_data *bd)
1620{
1621 struct nvme_ns *ns = hctx->queue->queuedata;
1622 struct nvme_rdma_queue *queue = hctx->driver_data;
1623 struct request *rq = bd->rq;
1624 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1625 struct nvme_rdma_qe *sqe = &req->sqe;
1626 struct nvme_command *c = sqe->data;
1627 bool flush = false;
1628 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001629 blk_status_t ret;
1630 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001631
1632 WARN_ON_ONCE(rq->tag < 0);
1633
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001634 ret = nvme_rdma_queue_is_ready(queue, rq);
1635 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001636 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001637
Christoph Hellwig71102302016-07-06 21:55:52 +09001638 dev = queue->device->dev;
1639 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1640 sizeof(struct nvme_command), DMA_TO_DEVICE);
1641
1642 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001643 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001644 return ret;
1645
Christoph Hellwig71102302016-07-06 21:55:52 +09001646 blk_mq_start_request(rq);
1647
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001648 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001649 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001650 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001651 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001652 nvme_cleanup_cmd(rq);
1653 goto err;
1654 }
1655
1656 ib_dma_sync_single_for_device(dev, sqe->dma,
1657 sizeof(struct nvme_command), DMA_TO_DEVICE);
1658
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001659 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001660 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001661 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001662 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001663 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001664 nvme_rdma_unmap_data(queue, rq);
1665 goto err;
1666 }
1667
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001668 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001669err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001670 if (err == -ENOMEM || err == -EAGAIN)
1671 return BLK_STS_RESOURCE;
1672 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001673}
1674
1675static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1676{
1677 struct nvme_rdma_queue *queue = hctx->driver_data;
1678 struct ib_cq *cq = queue->ib_cq;
1679 struct ib_wc wc;
1680 int found = 0;
1681
Christoph Hellwig71102302016-07-06 21:55:52 +09001682 while (ib_poll_cq(cq, 1, &wc) > 0) {
1683 struct ib_cqe *cqe = wc.wr_cqe;
1684
1685 if (cqe) {
1686 if (cqe->done == nvme_rdma_recv_done)
1687 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1688 else
1689 cqe->done(cq, &wc);
1690 }
1691 }
1692
1693 return found;
1694}
1695
1696static void nvme_rdma_complete_rq(struct request *rq)
1697{
1698 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001699
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001700 nvme_rdma_unmap_data(req->queue, rq);
1701 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001702}
1703
Sagi Grimberg0b366582017-07-13 11:09:44 +03001704static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1705{
1706 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1707
1708 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1709}
1710
Eric Biggersf363b082017-03-30 13:39:16 -07001711static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001712 .queue_rq = nvme_rdma_queue_rq,
1713 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001714 .init_request = nvme_rdma_init_request,
1715 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001716 .init_hctx = nvme_rdma_init_hctx,
1717 .poll = nvme_rdma_poll,
1718 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001719 .map_queues = nvme_rdma_map_queues,
Christoph Hellwig71102302016-07-06 21:55:52 +09001720};
1721
Eric Biggersf363b082017-03-30 13:39:16 -07001722static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001723 .queue_rq = nvme_rdma_queue_rq,
1724 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001725 .init_request = nvme_rdma_init_request,
1726 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001727 .init_hctx = nvme_rdma_init_admin_hctx,
1728 .timeout = nvme_rdma_timeout,
1729};
1730
Sagi Grimberg18398af2017-07-10 09:22:31 +03001731static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001732{
Christoph Hellwig71102302016-07-06 21:55:52 +09001733 cancel_work_sync(&ctrl->err_work);
1734 cancel_delayed_work_sync(&ctrl->reconnect_work);
1735
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001736 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001737 nvme_stop_queues(&ctrl->ctrl);
1738 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1739 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +02001740 nvme_rdma_destroy_io_queues(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001741 }
1742
Sagi Grimberg18398af2017-07-10 09:22:31 +03001743 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001744 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001745 else
1746 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001747
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001748 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001749 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1750 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001751 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001752 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001753}
1754
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001755static void nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001756{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001757 nvme_remove_namespaces(&ctrl->ctrl);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001758 nvme_rdma_shutdown_ctrl(ctrl, true);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001759 nvme_uninit_ctrl(&ctrl->ctrl);
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001760 nvme_put_ctrl(&ctrl->ctrl);
1761}
1762
Christoph Hellwig71102302016-07-06 21:55:52 +09001763static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1764{
1765 struct nvme_rdma_ctrl *ctrl = container_of(work,
1766 struct nvme_rdma_ctrl, delete_work);
1767
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001768 nvme_stop_ctrl(&ctrl->ctrl);
1769 nvme_rdma_remove_ctrl(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001770}
1771
1772static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1773{
1774 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1775 return -EBUSY;
1776
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001777 if (!queue_work(nvme_wq, &ctrl->delete_work))
Christoph Hellwig71102302016-07-06 21:55:52 +09001778 return -EBUSY;
1779
1780 return 0;
1781}
1782
1783static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1784{
1785 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001786 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001787
Steve Wisecdbecc82016-09-01 09:12:25 -07001788 /*
1789 * Keep a reference until all work is flushed since
1790 * __nvme_rdma_del_ctrl can free the ctrl mem
1791 */
1792 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1793 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001794 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001795 if (!ret)
1796 flush_work(&ctrl->delete_work);
1797 nvme_put_ctrl(&ctrl->ctrl);
1798 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001799}
1800
Christoph Hellwig71102302016-07-06 21:55:52 +09001801static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1802{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001803 struct nvme_rdma_ctrl *ctrl =
1804 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001805 int ret;
1806 bool changed;
1807
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001808 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001809 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001810
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001811 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001812 if (ret)
1813 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001814
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001815 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001816 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001817 if (ret)
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001818 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001819 }
1820
1821 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0ad0bfa2017-10-11 12:49:51 +03001822 if (!changed) {
1823 /* state change failure is ok if we're in DELETING state */
1824 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1825 return;
1826 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001827
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001828 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001829
1830 return;
1831
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001832out_fail:
Christoph Hellwig71102302016-07-06 21:55:52 +09001833 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001834 nvme_rdma_remove_ctrl(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001835}
1836
Christoph Hellwig71102302016-07-06 21:55:52 +09001837static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1838 .name = "rdma",
1839 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001840 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001841 .reg_read32 = nvmf_reg_read32,
1842 .reg_read64 = nvmf_reg_read64,
1843 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001844 .free_ctrl = nvme_rdma_free_ctrl,
1845 .submit_async_event = nvme_rdma_submit_async_event,
1846 .delete_ctrl = nvme_rdma_del_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001847 .get_address = nvmf_get_address,
Sagi Grimberg31b84462017-10-11 12:53:07 +03001848 .reinit_request = nvme_rdma_reinit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001849};
1850
Christoph Hellwig71102302016-07-06 21:55:52 +09001851static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1852 struct nvmf_ctrl_options *opts)
1853{
1854 struct nvme_rdma_ctrl *ctrl;
1855 int ret;
1856 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001857 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001858
1859 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1860 if (!ctrl)
1861 return ERR_PTR(-ENOMEM);
1862 ctrl->ctrl.opts = opts;
1863 INIT_LIST_HEAD(&ctrl->list);
1864
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001865 if (opts->mask & NVMF_OPT_TRSVCID)
1866 port = opts->trsvcid;
1867 else
1868 port = __stringify(NVME_RDMA_IP_PORT);
1869
1870 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1871 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001872 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001873 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001874 goto out_free_ctrl;
1875 }
1876
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001877 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001878 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1879 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001880 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001881 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001882 opts->host_traddr);
1883 goto out_free_ctrl;
1884 }
1885 }
1886
Christoph Hellwig71102302016-07-06 21:55:52 +09001887 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1888 0 /* no quirks, we're perfect! */);
1889 if (ret)
1890 goto out_free_ctrl;
1891
Christoph Hellwig71102302016-07-06 21:55:52 +09001892 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1893 nvme_rdma_reconnect_ctrl_work);
1894 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1895 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001896 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001897
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001898 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001899 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001900 ctrl->ctrl.kato = opts->kato;
1901
1902 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001903 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001904 GFP_KERNEL);
1905 if (!ctrl->queues)
1906 goto out_uninit_ctrl;
1907
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001908 ret = nvme_rdma_configure_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001909 if (ret)
1910 goto out_kfree_queues;
1911
1912 /* sanity check icdoff */
1913 if (ctrl->ctrl.icdoff) {
1914 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001915 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001916 goto out_remove_admin_queue;
1917 }
1918
1919 /* sanity check keyed sgls */
1920 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1921 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001922 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001923 goto out_remove_admin_queue;
1924 }
1925
1926 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1927 /* warn if maxcmd is lower than queue_size */
1928 dev_warn(ctrl->ctrl.device,
1929 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1930 opts->queue_size, ctrl->ctrl.maxcmd);
1931 opts->queue_size = ctrl->ctrl.maxcmd;
1932 }
1933
Samuel Jones76c08bf2016-10-25 09:22:34 +02001934 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1935 /* warn if sqsize is lower than queue_size */
1936 dev_warn(ctrl->ctrl.device,
1937 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1938 opts->queue_size, ctrl->ctrl.sqsize + 1);
1939 opts->queue_size = ctrl->ctrl.sqsize + 1;
1940 }
1941
Christoph Hellwig71102302016-07-06 21:55:52 +09001942 if (opts->nr_io_queues) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001943 ret = nvme_rdma_configure_io_queues(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001944 if (ret)
1945 goto out_remove_admin_queue;
1946 }
1947
1948 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1949 WARN_ON_ONCE(!changed);
1950
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001951 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001952 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1953
1954 kref_get(&ctrl->ctrl.kref);
1955
1956 mutex_lock(&nvme_rdma_ctrl_mutex);
1957 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1958 mutex_unlock(&nvme_rdma_ctrl_mutex);
1959
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001960 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001961
1962 return &ctrl->ctrl;
1963
1964out_remove_admin_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001965 nvme_rdma_destroy_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001966out_kfree_queues:
1967 kfree(ctrl->queues);
1968out_uninit_ctrl:
1969 nvme_uninit_ctrl(&ctrl->ctrl);
1970 nvme_put_ctrl(&ctrl->ctrl);
1971 if (ret > 0)
1972 ret = -EIO;
1973 return ERR_PTR(ret);
1974out_free_ctrl:
1975 kfree(ctrl);
1976 return ERR_PTR(ret);
1977}
1978
1979static struct nvmf_transport_ops nvme_rdma_transport = {
1980 .name = "rdma",
1981 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001982 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001983 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001984 .create_ctrl = nvme_rdma_create_ctrl,
1985};
1986
Steve Wisee87a9112016-09-02 09:01:54 -07001987static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1988{
1989 struct nvme_rdma_ctrl *ctrl;
1990
1991 /* Delete all controllers using this device */
1992 mutex_lock(&nvme_rdma_ctrl_mutex);
1993 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1994 if (ctrl->device->dev != ib_device)
1995 continue;
1996 dev_info(ctrl->ctrl.device,
1997 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1998 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1999 __nvme_rdma_del_ctrl(ctrl);
2000 }
2001 mutex_unlock(&nvme_rdma_ctrl_mutex);
2002
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002003 flush_workqueue(nvme_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002004}
2005
2006static struct ib_client nvme_rdma_ib_client = {
2007 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002008 .remove = nvme_rdma_remove_one
2009};
2010
Christoph Hellwig71102302016-07-06 21:55:52 +09002011static int __init nvme_rdma_init_module(void)
2012{
Steve Wisee87a9112016-09-02 09:01:54 -07002013 int ret;
2014
Steve Wisee87a9112016-09-02 09:01:54 -07002015 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002016 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002017 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002018
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002019 ret = nvmf_register_transport(&nvme_rdma_transport);
2020 if (ret)
2021 goto err_unreg_client;
2022
2023 return 0;
2024
2025err_unreg_client:
2026 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002027 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002028}
2029
2030static void __exit nvme_rdma_cleanup_module(void)
2031{
Christoph Hellwig71102302016-07-06 21:55:52 +09002032 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002033 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002034}
2035
2036module_init(nvme_rdma_init_module);
2037module_exit(nvme_rdma_cleanup_module);
2038
2039MODULE_LICENSE("GPL v2");