blob: 93c8578a2ddc2233c2abda88d46a8779ced9dd12 [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 {
53 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 Grimbergb282a88d2017-05-04 13:33:11 +030082 NVME_RDMA_Q_LIVE = 0,
Sagi Grimbergabf87d52017-05-04 13:33:10 +030083 NVME_RDMA_Q_DELETING = 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
Christoph Hellwig71102302016-07-06 21:55:52 +0900277 ib_dereg_mr(req->mr);
278
279 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
280 ctrl->max_fr_pages);
281 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900282 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000283 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100284 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900285 }
286
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300287 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900288
289out:
290 return ret;
291}
292
Christoph Hellwig385475e2017-06-13 09:15:19 +0200293static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
294 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900295{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200296 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900297 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200298 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900299 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
300 struct nvme_rdma_device *dev = queue->device;
301
302 if (req->mr)
303 ib_dereg_mr(req->mr);
304
305 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
306 DMA_TO_DEVICE);
307}
308
Christoph Hellwig385475e2017-06-13 09:15:19 +0200309static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
310 struct request *rq, unsigned int hctx_idx,
311 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900312{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200313 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900314 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200315 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900316 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
317 struct nvme_rdma_device *dev = queue->device;
318 struct ib_device *ibdev = dev->dev;
319 int ret;
320
Christoph Hellwig71102302016-07-06 21:55:52 +0900321 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
322 DMA_TO_DEVICE);
323 if (ret)
324 return ret;
325
326 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
327 ctrl->max_fr_pages);
328 if (IS_ERR(req->mr)) {
329 ret = PTR_ERR(req->mr);
330 goto out_free_qe;
331 }
332
333 req->queue = queue;
334
335 return 0;
336
337out_free_qe:
338 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
339 DMA_TO_DEVICE);
340 return -ENOMEM;
341}
342
Christoph Hellwig71102302016-07-06 21:55:52 +0900343static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
344 unsigned int hctx_idx)
345{
346 struct nvme_rdma_ctrl *ctrl = data;
347 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
348
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300349 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900350
351 hctx->driver_data = queue;
352 return 0;
353}
354
355static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
356 unsigned int hctx_idx)
357{
358 struct nvme_rdma_ctrl *ctrl = data;
359 struct nvme_rdma_queue *queue = &ctrl->queues[0];
360
361 BUG_ON(hctx_idx != 0);
362
363 hctx->driver_data = queue;
364 return 0;
365}
366
367static void nvme_rdma_free_dev(struct kref *ref)
368{
369 struct nvme_rdma_device *ndev =
370 container_of(ref, struct nvme_rdma_device, ref);
371
372 mutex_lock(&device_list_mutex);
373 list_del(&ndev->entry);
374 mutex_unlock(&device_list_mutex);
375
Christoph Hellwig71102302016-07-06 21:55:52 +0900376 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900377 kfree(ndev);
378}
379
380static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
381{
382 kref_put(&dev->ref, nvme_rdma_free_dev);
383}
384
385static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
386{
387 return kref_get_unless_zero(&dev->ref);
388}
389
390static struct nvme_rdma_device *
391nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
392{
393 struct nvme_rdma_device *ndev;
394
395 mutex_lock(&device_list_mutex);
396 list_for_each_entry(ndev, &device_list, entry) {
397 if (ndev->dev->node_guid == cm_id->device->node_guid &&
398 nvme_rdma_dev_get(ndev))
399 goto out_unlock;
400 }
401
402 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
403 if (!ndev)
404 goto out_err;
405
406 ndev->dev = cm_id->device;
407 kref_init(&ndev->ref);
408
Christoph Hellwig11975e02016-09-05 12:56:20 +0200409 ndev->pd = ib_alloc_pd(ndev->dev,
410 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900411 if (IS_ERR(ndev->pd))
412 goto out_free_dev;
413
Christoph Hellwig71102302016-07-06 21:55:52 +0900414 if (!(ndev->dev->attrs.device_cap_flags &
415 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
416 dev_err(&ndev->dev->dev,
417 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200418 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900419 }
420
421 list_add(&ndev->entry, &device_list);
422out_unlock:
423 mutex_unlock(&device_list_mutex);
424 return ndev;
425
Christoph Hellwig71102302016-07-06 21:55:52 +0900426out_free_pd:
427 ib_dealloc_pd(ndev->pd);
428out_free_dev:
429 kfree(ndev);
430out_err:
431 mutex_unlock(&device_list_mutex);
432 return NULL;
433}
434
435static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
436{
Steve Wisef361e5a2016-09-02 09:01:27 -0700437 struct nvme_rdma_device *dev;
438 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900439
Steve Wisef361e5a2016-09-02 09:01:27 -0700440 dev = queue->device;
441 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900442 rdma_destroy_qp(queue->cm_id);
443 ib_free_cq(queue->ib_cq);
444
445 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
446 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
447
448 nvme_rdma_dev_put(dev);
449}
450
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300451static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900452{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300453 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900454 const int send_wr_factor = 3; /* MR, SEND, INV */
455 const int cq_factor = send_wr_factor + 1; /* + RECV */
456 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900457 int ret;
458
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300459 queue->device = nvme_rdma_find_get_device(queue->cm_id);
460 if (!queue->device) {
461 dev_err(queue->cm_id->device->dev.parent,
462 "no client data found!\n");
463 return -ECONNREFUSED;
464 }
465 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900466
467 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300468 * Spread I/O queues completion vectors according their queue index.
469 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900470 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300471 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900472
473 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300474 queue->ib_cq = ib_alloc_cq(ibdev, queue,
475 cq_factor * queue->queue_size + 1,
476 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900477 if (IS_ERR(queue->ib_cq)) {
478 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300479 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900480 }
481
482 ret = nvme_rdma_create_qp(queue, send_wr_factor);
483 if (ret)
484 goto out_destroy_ib_cq;
485
486 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
487 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
488 if (!queue->rsp_ring) {
489 ret = -ENOMEM;
490 goto out_destroy_qp;
491 }
492
493 return 0;
494
495out_destroy_qp:
496 ib_destroy_qp(queue->qp);
497out_destroy_ib_cq:
498 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300499out_put_dev:
500 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900501 return ret;
502}
503
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300504static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900505 int idx, size_t queue_size)
506{
507 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200508 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900509 int ret;
510
511 queue = &ctrl->queues[idx];
512 queue->ctrl = ctrl;
513 init_completion(&queue->cm_done);
514
515 if (idx > 0)
516 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
517 else
518 queue->cmnd_capsule_len = sizeof(struct nvme_command);
519
520 queue->queue_size = queue_size;
Marta Rybczynska5e599d72017-06-06 13:27:21 +0200521 atomic_set(&queue->sig_count, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900522
523 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
524 RDMA_PS_TCP, IB_QPT_RC);
525 if (IS_ERR(queue->cm_id)) {
526 dev_info(ctrl->ctrl.device,
527 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
528 return PTR_ERR(queue->cm_id);
529 }
530
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200531 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200532 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200533
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200534 queue->cm_error = -ETIMEDOUT;
535 ret = rdma_resolve_addr(queue->cm_id, src_addr,
536 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900537 NVME_RDMA_CONNECT_TIMEOUT_MS);
538 if (ret) {
539 dev_info(ctrl->ctrl.device,
540 "rdma_resolve_addr failed (%d).\n", ret);
541 goto out_destroy_cm_id;
542 }
543
544 ret = nvme_rdma_wait_for_cm(queue);
545 if (ret) {
546 dev_info(ctrl->ctrl.device,
547 "rdma_resolve_addr wait failed (%d).\n", ret);
548 goto out_destroy_cm_id;
549 }
550
Sagi Grimberg3b4ac782016-09-22 19:58:17 -0600551 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900552
553 return 0;
554
555out_destroy_cm_id:
556 rdma_destroy_id(queue->cm_id);
557 return ret;
558}
559
560static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
561{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200562 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
563 return;
564
Christoph Hellwig71102302016-07-06 21:55:52 +0900565 rdma_disconnect(queue->cm_id);
566 ib_drain_qp(queue->qp);
567}
568
569static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
570{
Sagi Grimberge89ca582016-09-02 09:01:54 -0700571 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900572 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200573
574 nvme_rdma_destroy_queue_ib(queue);
575 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900576}
577
578static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
579{
580 int i;
581
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300582 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200583 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900584}
585
Sagi Grimberga57bd542017-08-28 21:41:10 +0200586static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
587{
588 int i;
589
590 for (i = 1; i < ctrl->ctrl.queue_count; i++)
591 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900592}
593
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300594static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
595{
596 int ret;
597
598 if (idx)
599 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
600 else
601 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
602
603 if (!ret)
604 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
605 else
606 dev_info(ctrl->ctrl.device,
607 "failed to connect queue: %d ret=%d\n", idx, ret);
608 return ret;
609}
610
611static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900612{
613 int i, ret = 0;
614
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300615 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300616 ret = nvme_rdma_start_queue(ctrl, i);
617 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200618 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900619 }
620
Steve Wisec8dbc372016-11-08 09:16:02 -0800621 return 0;
622
Sagi Grimberga57bd542017-08-28 21:41:10 +0200623out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300624 for (i--; i >= 1; i--)
625 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900626 return ret;
627}
628
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300629static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900630{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200631 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300632 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200633 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900634 int i, ret;
635
Sagi Grimbergc248c642017-03-09 13:26:07 +0200636 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300637
638 /*
639 * we map queues according to the device irq vectors for
640 * optimal locality so we don't need more queues than
641 * completion vectors.
642 */
643 nr_io_queues = min_t(unsigned int, nr_io_queues,
644 ibdev->num_comp_vectors);
645
Sagi Grimbergc248c642017-03-09 13:26:07 +0200646 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
647 if (ret)
648 return ret;
649
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300650 ctrl->ctrl.queue_count = nr_io_queues + 1;
651 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200652 return 0;
653
654 dev_info(ctrl->ctrl.device,
655 "creating %d I/O queues.\n", nr_io_queues);
656
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300657 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300658 ret = nvme_rdma_alloc_queue(ctrl, i,
659 ctrl->ctrl.sqsize + 1);
660 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900661 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900662 }
663
664 return 0;
665
666out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700667 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200668 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900669
670 return ret;
671}
672
Sagi Grimberg60070c72017-10-11 15:29:06 +0300673static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
674 struct blk_mq_tag_set *set)
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300675{
676 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300677
678 blk_mq_free_tag_set(set);
679 nvme_rdma_dev_put(ctrl->device);
680}
681
682static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
683 bool admin)
684{
685 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
686 struct blk_mq_tag_set *set;
687 int ret;
688
689 if (admin) {
690 set = &ctrl->admin_tag_set;
691 memset(set, 0, sizeof(*set));
692 set->ops = &nvme_rdma_admin_mq_ops;
693 set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
694 set->reserved_tags = 2; /* connect + keep-alive */
695 set->numa_node = NUMA_NO_NODE;
696 set->cmd_size = sizeof(struct nvme_rdma_request) +
697 SG_CHUNK_SIZE * sizeof(struct scatterlist);
698 set->driver_data = ctrl;
699 set->nr_hw_queues = 1;
700 set->timeout = ADMIN_TIMEOUT;
701 } else {
702 set = &ctrl->tag_set;
703 memset(set, 0, sizeof(*set));
704 set->ops = &nvme_rdma_mq_ops;
705 set->queue_depth = nctrl->opts->queue_size;
706 set->reserved_tags = 1; /* fabric connect */
707 set->numa_node = NUMA_NO_NODE;
708 set->flags = BLK_MQ_F_SHOULD_MERGE;
709 set->cmd_size = sizeof(struct nvme_rdma_request) +
710 SG_CHUNK_SIZE * sizeof(struct scatterlist);
711 set->driver_data = ctrl;
712 set->nr_hw_queues = nctrl->queue_count - 1;
713 set->timeout = NVME_IO_TIMEOUT;
714 }
715
716 ret = blk_mq_alloc_tag_set(set);
717 if (ret)
718 goto out;
719
720 /*
721 * We need a reference on the device as long as the tag_set is alive,
722 * as the MRs in the request structures need a valid ib_device.
723 */
724 ret = nvme_rdma_dev_get(ctrl->device);
725 if (!ret) {
726 ret = -EINVAL;
727 goto out_free_tagset;
728 }
729
730 return set;
731
732out_free_tagset:
733 blk_mq_free_tag_set(set);
734out:
735 return ERR_PTR(ret);
736}
737
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300738static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
739 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900740{
741 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
742 sizeof(struct nvme_command), DMA_TO_DEVICE);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200743 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300744 if (remove) {
745 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300746 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300747 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200748 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900749}
750
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300751static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
752 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300753{
754 int error;
755
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300756 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300757 if (error)
758 return error;
759
760 ctrl->device = ctrl->queues[0].device;
761
Sagi Grimberg90af3512017-07-10 09:22:28 +0300762 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
763 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
764
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300765 if (new) {
766 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
767 if (IS_ERR(ctrl->ctrl.admin_tagset))
768 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300769
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300770 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
771 if (IS_ERR(ctrl->ctrl.admin_q)) {
772 error = PTR_ERR(ctrl->ctrl.admin_q);
773 goto out_free_tagset;
774 }
775 } else {
Sagi Grimberg31b84462017-10-11 12:53:07 +0300776 error = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300777 if (error)
778 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300779 }
780
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300781 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300782 if (error)
783 goto out_cleanup_queue;
784
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300785 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300786 &ctrl->ctrl.cap);
787 if (error) {
788 dev_err(ctrl->ctrl.device,
789 "prop_get NVME_REG_CAP failed\n");
790 goto out_cleanup_queue;
791 }
792
793 ctrl->ctrl.sqsize =
794 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
795
796 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
797 if (error)
798 goto out_cleanup_queue;
799
800 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700801 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300802
803 error = nvme_init_identify(&ctrl->ctrl);
804 if (error)
805 goto out_cleanup_queue;
806
807 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
808 &ctrl->async_event_sqe, sizeof(struct nvme_command),
809 DMA_TO_DEVICE);
810 if (error)
811 goto out_cleanup_queue;
812
813 return 0;
814
815out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300816 if (new)
817 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300818out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300819 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300820 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300821out_free_queue:
822 nvme_rdma_free_queue(&ctrl->queues[0]);
823 return error;
824}
825
Sagi Grimberga57bd542017-08-28 21:41:10 +0200826static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
827 bool remove)
828{
829 nvme_rdma_stop_io_queues(ctrl);
830 if (remove) {
831 blk_cleanup_queue(ctrl->ctrl.connect_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300832 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200833 }
834 nvme_rdma_free_io_queues(ctrl);
835}
836
837static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
838{
839 int ret;
840
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300841 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200842 if (ret)
843 return ret;
844
845 if (new) {
846 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
847 if (IS_ERR(ctrl->ctrl.tagset))
848 goto out_free_io_queues;
849
850 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
851 if (IS_ERR(ctrl->ctrl.connect_q)) {
852 ret = PTR_ERR(ctrl->ctrl.connect_q);
853 goto out_free_tag_set;
854 }
855 } else {
Sagi Grimberg31b84462017-10-11 12:53:07 +0300856 ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200857 if (ret)
858 goto out_free_io_queues;
859
860 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
861 ctrl->ctrl.queue_count - 1);
862 }
863
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300864 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200865 if (ret)
866 goto out_cleanup_connect_q;
867
868 return 0;
869
870out_cleanup_connect_q:
871 if (new)
872 blk_cleanup_queue(ctrl->ctrl.connect_q);
873out_free_tag_set:
874 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300875 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200876out_free_io_queues:
877 nvme_rdma_free_io_queues(ctrl);
878 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900879}
880
881static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
882{
883 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
884
885 if (list_empty(&ctrl->list))
886 goto free_ctrl;
887
888 mutex_lock(&nvme_rdma_ctrl_mutex);
889 list_del(&ctrl->list);
890 mutex_unlock(&nvme_rdma_ctrl_mutex);
891
Christoph Hellwig71102302016-07-06 21:55:52 +0900892 kfree(ctrl->queues);
893 nvmf_free_options(nctrl->opts);
894free_ctrl:
895 kfree(ctrl);
896}
897
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200898static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
899{
900 /* If we are resetting/deleting then do nothing */
901 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
902 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
903 ctrl->ctrl.state == NVME_CTRL_LIVE);
904 return;
905 }
906
907 if (nvmf_should_reconnect(&ctrl->ctrl)) {
908 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
909 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200910 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200911 ctrl->ctrl.opts->reconnect_delay * HZ);
912 } else {
913 dev_info(ctrl->ctrl.device, "Removing controller...\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200914 queue_work(nvme_wq, &ctrl->delete_work);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200915 }
916}
917
Christoph Hellwig71102302016-07-06 21:55:52 +0900918static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
919{
920 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
921 struct nvme_rdma_ctrl, reconnect_work);
922 bool changed;
923 int ret;
924
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300925 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200926
Sagi Grimberga57bd542017-08-28 21:41:10 +0200927 if (ctrl->ctrl.queue_count > 1)
928 nvme_rdma_destroy_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900929
Sagi Grimberg31fdf182017-08-28 21:40:06 +0200930 nvme_rdma_destroy_admin_queue(ctrl, false);
931 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900932 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300933 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900934
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300935 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200936 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900937 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300938 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900939 }
940
941 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +0300942 if (!changed) {
943 /* state change failure is ok if we're in DELETING state */
944 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
945 return;
946 }
947
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300948 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900949
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300950 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900951
952 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
953
954 return;
955
Christoph Hellwig71102302016-07-06 21:55:52 +0900956requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200957 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300958 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200959 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900960}
961
962static void nvme_rdma_error_recovery_work(struct work_struct *work)
963{
964 struct nvme_rdma_ctrl *ctrl = container_of(work,
965 struct nvme_rdma_ctrl, err_work);
966
Sagi Grimberge4d753d2017-09-21 17:01:38 +0300967 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700968
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300969 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900970 nvme_stop_queues(&ctrl->ctrl);
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300971 nvme_rdma_stop_io_queues(ctrl);
972 }
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300973 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300974 nvme_rdma_stop_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900975
976 /* We must take care of fastfail/requeue all our inflight requests */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300977 if (ctrl->ctrl.queue_count > 1)
Christoph Hellwig71102302016-07-06 21:55:52 +0900978 blk_mq_tagset_busy_iter(&ctrl->tag_set,
979 nvme_cancel_request, &ctrl->ctrl);
980 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
981 nvme_cancel_request, &ctrl->ctrl);
982
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300983 /*
984 * queues are not a live anymore, so restart the queues to fail fast
985 * new IO
986 */
Sagi Grimbergfb0513392017-07-02 15:33:32 +0300987 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300988 nvme_start_queues(&ctrl->ctrl);
989
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200990 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900991}
992
993static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
994{
995 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
996 return;
997
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200998 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +0900999}
1000
1001static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1002 const char *op)
1003{
1004 struct nvme_rdma_queue *queue = cq->cq_context;
1005 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1006
1007 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1008 dev_info(ctrl->ctrl.device,
1009 "%s for CQE 0x%p failed with status %s (%d)\n",
1010 op, wc->wr_cqe,
1011 ib_wc_status_msg(wc->status), wc->status);
1012 nvme_rdma_error_recovery(ctrl);
1013}
1014
1015static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1016{
1017 if (unlikely(wc->status != IB_WC_SUCCESS))
1018 nvme_rdma_wr_error(cq, wc, "MEMREG");
1019}
1020
1021static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1022{
1023 if (unlikely(wc->status != IB_WC_SUCCESS))
1024 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1025}
1026
1027static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1028 struct nvme_rdma_request *req)
1029{
1030 struct ib_send_wr *bad_wr;
1031 struct ib_send_wr wr = {
1032 .opcode = IB_WR_LOCAL_INV,
1033 .next = NULL,
1034 .num_sge = 0,
1035 .send_flags = 0,
1036 .ex.invalidate_rkey = req->mr->rkey,
1037 };
1038
1039 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1040 wr.wr_cqe = &req->reg_cqe;
1041
1042 return ib_post_send(queue->qp, &wr, &bad_wr);
1043}
1044
1045static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1046 struct request *rq)
1047{
1048 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1049 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1050 struct nvme_rdma_device *dev = queue->device;
1051 struct ib_device *ibdev = dev->dev;
1052 int res;
1053
1054 if (!blk_rq_bytes(rq))
1055 return;
1056
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001057 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001058 res = nvme_rdma_inv_rkey(queue, req);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001059 if (unlikely(res < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001060 dev_err(ctrl->ctrl.device,
1061 "Queueing INV WR for rkey %#x failed (%d)\n",
1062 req->mr->rkey, res);
1063 nvme_rdma_error_recovery(queue->ctrl);
1064 }
1065 }
1066
1067 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1068 req->nents, rq_data_dir(rq) ==
1069 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1070
1071 nvme_cleanup_cmd(rq);
1072 sg_free_table_chained(&req->sg_table, true);
1073}
1074
1075static int nvme_rdma_set_sg_null(struct nvme_command *c)
1076{
1077 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1078
1079 sg->addr = 0;
1080 put_unaligned_le24(0, sg->length);
1081 put_unaligned_le32(0, sg->key);
1082 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1083 return 0;
1084}
1085
1086static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1087 struct nvme_rdma_request *req, struct nvme_command *c)
1088{
1089 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1090
1091 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1092 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1093 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1094
1095 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1096 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1097 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1098
1099 req->inline_data = true;
1100 req->num_sge++;
1101 return 0;
1102}
1103
1104static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1105 struct nvme_rdma_request *req, struct nvme_command *c)
1106{
1107 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1108
1109 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1110 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001111 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001112 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1113 return 0;
1114}
1115
1116static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1117 struct nvme_rdma_request *req, struct nvme_command *c,
1118 int count)
1119{
1120 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1121 int nr;
1122
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001123 /*
1124 * Align the MR to a 4K page size to match the ctrl page size and
1125 * the block virtual boundary.
1126 */
1127 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001128 if (unlikely(nr < count)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001129 if (nr < 0)
1130 return nr;
1131 return -EINVAL;
1132 }
1133
1134 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1135
1136 req->reg_cqe.done = nvme_rdma_memreg_done;
1137 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1138 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1139 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1140 req->reg_wr.wr.num_sge = 0;
1141 req->reg_wr.mr = req->mr;
1142 req->reg_wr.key = req->mr->rkey;
1143 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1144 IB_ACCESS_REMOTE_READ |
1145 IB_ACCESS_REMOTE_WRITE;
1146
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001147 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001148
1149 sg->addr = cpu_to_le64(req->mr->iova);
1150 put_unaligned_le24(req->mr->length, sg->length);
1151 put_unaligned_le32(req->mr->rkey, sg->key);
1152 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1153 NVME_SGL_FMT_INVALIDATE;
1154
1155 return 0;
1156}
1157
1158static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001159 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001160{
1161 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1162 struct nvme_rdma_device *dev = queue->device;
1163 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001164 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001165
1166 req->num_sge = 1;
1167 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001168 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001169
1170 c->common.flags |= NVME_CMD_SGL_METABUF;
1171
1172 if (!blk_rq_bytes(rq))
1173 return nvme_rdma_set_sg_null(c);
1174
1175 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001176 ret = sg_alloc_table_chained(&req->sg_table,
1177 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001178 if (ret)
1179 return -ENOMEM;
1180
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001181 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001182
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001183 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001184 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1185 if (unlikely(count <= 0)) {
1186 sg_free_table_chained(&req->sg_table, true);
1187 return -EIO;
1188 }
1189
1190 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001191 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1192 blk_rq_payload_bytes(rq) <=
1193 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001194 return nvme_rdma_map_sg_inline(queue, req, c);
1195
Christoph Hellwig11975e02016-09-05 12:56:20 +02001196 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001197 return nvme_rdma_map_sg_single(queue, req, c);
1198 }
1199
1200 return nvme_rdma_map_sg_fr(queue, req, c, count);
1201}
1202
1203static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1204{
1205 if (unlikely(wc->status != IB_WC_SUCCESS))
1206 nvme_rdma_wr_error(cq, wc, "SEND");
1207}
1208
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001209/*
1210 * We want to signal completion at least every queue depth/2. This returns the
1211 * largest power of two that is not above half of (queue size + 1) to optimize
1212 * (avoid divisions).
1213 */
1214static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
Marta Rybczynska0544f542017-04-10 17:12:34 +02001215{
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001216 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
Marta Rybczynska0544f542017-04-10 17:12:34 +02001217
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001218 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
Marta Rybczynska0544f542017-04-10 17:12:34 +02001219}
1220
Christoph Hellwig71102302016-07-06 21:55:52 +09001221static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1222 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1223 struct ib_send_wr *first, bool flush)
1224{
1225 struct ib_send_wr wr, *bad_wr;
1226 int ret;
1227
1228 sge->addr = qe->dma;
1229 sge->length = sizeof(struct nvme_command),
1230 sge->lkey = queue->device->pd->local_dma_lkey;
1231
1232 qe->cqe.done = nvme_rdma_send_done;
1233
1234 wr.next = NULL;
1235 wr.wr_cqe = &qe->cqe;
1236 wr.sg_list = sge;
1237 wr.num_sge = num_sge;
1238 wr.opcode = IB_WR_SEND;
1239 wr.send_flags = 0;
1240
1241 /*
1242 * Unsignalled send completions are another giant desaster in the
1243 * IB Verbs spec: If we don't regularly post signalled sends
1244 * the send queue will fill up and only a QP reset will rescue us.
1245 * Would have been way to obvious to handle this in hardware or
1246 * at least the RDMA stack..
1247 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001248 * Always signal the flushes. The magic request used for the flush
1249 * sequencer is not allocated in our driver's tagset and it's
1250 * triggered to be freed by blk_cleanup_queue(). So we need to
1251 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001252 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001253 * calls wr_cqe->done().
1254 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001255 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001256 wr.send_flags |= IB_SEND_SIGNALED;
1257
1258 if (first)
1259 first->next = &wr;
1260 else
1261 first = &wr;
1262
1263 ret = ib_post_send(queue->qp, first, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001264 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001265 dev_err(queue->ctrl->ctrl.device,
1266 "%s failed with error code %d\n", __func__, ret);
1267 }
1268 return ret;
1269}
1270
1271static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1272 struct nvme_rdma_qe *qe)
1273{
1274 struct ib_recv_wr wr, *bad_wr;
1275 struct ib_sge list;
1276 int ret;
1277
1278 list.addr = qe->dma;
1279 list.length = sizeof(struct nvme_completion);
1280 list.lkey = queue->device->pd->local_dma_lkey;
1281
1282 qe->cqe.done = nvme_rdma_recv_done;
1283
1284 wr.next = NULL;
1285 wr.wr_cqe = &qe->cqe;
1286 wr.sg_list = &list;
1287 wr.num_sge = 1;
1288
1289 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001290 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001291 dev_err(queue->ctrl->ctrl.device,
1292 "%s failed with error code %d\n", __func__, ret);
1293 }
1294 return ret;
1295}
1296
1297static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1298{
1299 u32 queue_idx = nvme_rdma_queue_idx(queue);
1300
1301 if (queue_idx == 0)
1302 return queue->ctrl->admin_tag_set.tags[queue_idx];
1303 return queue->ctrl->tag_set.tags[queue_idx - 1];
1304}
1305
1306static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1307{
1308 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1309 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1310 struct ib_device *dev = queue->device->dev;
1311 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1312 struct nvme_command *cmd = sqe->data;
1313 struct ib_sge sge;
1314 int ret;
1315
1316 if (WARN_ON_ONCE(aer_idx != 0))
1317 return;
1318
1319 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1320
1321 memset(cmd, 0, sizeof(*cmd));
1322 cmd->common.opcode = nvme_admin_async_event;
1323 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1324 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1325 nvme_rdma_set_sg_null(cmd);
1326
1327 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1328 DMA_TO_DEVICE);
1329
1330 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1331 WARN_ON_ONCE(ret);
1332}
1333
1334static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1335 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1336{
Christoph Hellwig71102302016-07-06 21:55:52 +09001337 struct request *rq;
1338 struct nvme_rdma_request *req;
1339 int ret = 0;
1340
Christoph Hellwig71102302016-07-06 21:55:52 +09001341 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1342 if (!rq) {
1343 dev_err(queue->ctrl->ctrl.device,
1344 "tag 0x%x on QP %#x not found\n",
1345 cqe->command_id, queue->qp->qp_num);
1346 nvme_rdma_error_recovery(queue->ctrl);
1347 return ret;
1348 }
1349 req = blk_mq_rq_to_pdu(rq);
1350
Christoph Hellwig71102302016-07-06 21:55:52 +09001351 if (rq->tag == tag)
1352 ret = 1;
1353
1354 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1355 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001356 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001357
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001358 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001359 return ret;
1360}
1361
1362static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1363{
1364 struct nvme_rdma_qe *qe =
1365 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1366 struct nvme_rdma_queue *queue = cq->cq_context;
1367 struct ib_device *ibdev = queue->device->dev;
1368 struct nvme_completion *cqe = qe->data;
1369 const size_t len = sizeof(struct nvme_completion);
1370 int ret = 0;
1371
1372 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1373 nvme_rdma_wr_error(cq, wc, "RECV");
1374 return 0;
1375 }
1376
1377 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1378 /*
1379 * AEN requests are special as they don't time out and can
1380 * survive any kind of queue freeze and often don't respond to
1381 * aborts. We don't even bother to allocate a struct request
1382 * for them but rather special case them here.
1383 */
1384 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1385 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001386 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1387 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001388 else
1389 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1390 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1391
1392 nvme_rdma_post_recv(queue, qe);
1393 return ret;
1394}
1395
1396static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1397{
1398 __nvme_rdma_recv_done(cq, wc, -1);
1399}
1400
1401static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1402{
1403 int ret, i;
1404
1405 for (i = 0; i < queue->queue_size; i++) {
1406 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1407 if (ret)
1408 goto out_destroy_queue_ib;
1409 }
1410
1411 return 0;
1412
1413out_destroy_queue_ib:
1414 nvme_rdma_destroy_queue_ib(queue);
1415 return ret;
1416}
1417
1418static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1419 struct rdma_cm_event *ev)
1420{
Steve Wise7f039532016-10-26 12:36:47 -07001421 struct rdma_cm_id *cm_id = queue->cm_id;
1422 int status = ev->status;
1423 const char *rej_msg;
1424 const struct nvme_rdma_cm_rej *rej_data;
1425 u8 rej_data_len;
1426
1427 rej_msg = rdma_reject_msg(cm_id, status);
1428 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1429
1430 if (rej_data && rej_data_len >= sizeof(u16)) {
1431 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001432
1433 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001434 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1435 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001436 } else {
1437 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001438 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001439 }
1440
1441 return -ECONNRESET;
1442}
1443
1444static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1445{
Christoph Hellwig71102302016-07-06 21:55:52 +09001446 int ret;
1447
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001448 ret = nvme_rdma_create_queue_ib(queue);
1449 if (ret)
1450 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001451
1452 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1453 if (ret) {
1454 dev_err(queue->ctrl->ctrl.device,
1455 "rdma_resolve_route failed (%d).\n",
1456 queue->cm_error);
1457 goto out_destroy_queue;
1458 }
1459
1460 return 0;
1461
1462out_destroy_queue:
1463 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001464 return ret;
1465}
1466
1467static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1468{
1469 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1470 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001471 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001472 int ret;
1473
1474 param.qp_num = queue->qp->qp_num;
1475 param.flow_control = 1;
1476
1477 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001478 /* maximum retry count */
1479 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001480 param.rnr_retry_count = 7;
1481 param.private_data = &priv;
1482 param.private_data_len = sizeof(priv);
1483
1484 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1485 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001486 /*
1487 * set the admin queue depth to the minimum size
1488 * specified by the Fabrics standard.
1489 */
1490 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001491 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1492 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001493 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001494 /*
1495 * current interpretation of the fabrics spec
1496 * is at minimum you make hrqsize sqsize+1, or a
1497 * 1's based representation of sqsize.
1498 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001499 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001500 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001501 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001502
1503 ret = rdma_connect(queue->cm_id, &param);
1504 if (ret) {
1505 dev_err(ctrl->ctrl.device,
1506 "rdma_connect failed (%d).\n", ret);
1507 goto out_destroy_queue_ib;
1508 }
1509
1510 return 0;
1511
1512out_destroy_queue_ib:
1513 nvme_rdma_destroy_queue_ib(queue);
1514 return ret;
1515}
1516
Christoph Hellwig71102302016-07-06 21:55:52 +09001517static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1518 struct rdma_cm_event *ev)
1519{
1520 struct nvme_rdma_queue *queue = cm_id->context;
1521 int cm_error = 0;
1522
1523 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1524 rdma_event_msg(ev->event), ev->event,
1525 ev->status, cm_id);
1526
1527 switch (ev->event) {
1528 case RDMA_CM_EVENT_ADDR_RESOLVED:
1529 cm_error = nvme_rdma_addr_resolved(queue);
1530 break;
1531 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1532 cm_error = nvme_rdma_route_resolved(queue);
1533 break;
1534 case RDMA_CM_EVENT_ESTABLISHED:
1535 queue->cm_error = nvme_rdma_conn_established(queue);
1536 /* complete cm_done regardless of success/failure */
1537 complete(&queue->cm_done);
1538 return 0;
1539 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001540 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001541 cm_error = nvme_rdma_conn_rejected(queue, ev);
1542 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001543 case RDMA_CM_EVENT_ROUTE_ERROR:
1544 case RDMA_CM_EVENT_CONNECT_ERROR:
1545 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001546 nvme_rdma_destroy_queue_ib(queue);
1547 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001548 dev_dbg(queue->ctrl->ctrl.device,
1549 "CM error event %d\n", ev->event);
1550 cm_error = -ECONNRESET;
1551 break;
1552 case RDMA_CM_EVENT_DISCONNECTED:
1553 case RDMA_CM_EVENT_ADDR_CHANGE:
1554 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1555 dev_dbg(queue->ctrl->ctrl.device,
1556 "disconnect received - connection closed\n");
1557 nvme_rdma_error_recovery(queue->ctrl);
1558 break;
1559 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001560 /* device removal is handled via the ib_client API */
1561 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001562 default:
1563 dev_err(queue->ctrl->ctrl.device,
1564 "Unexpected RDMA CM event (%d)\n", ev->event);
1565 nvme_rdma_error_recovery(queue->ctrl);
1566 break;
1567 }
1568
1569 if (cm_error) {
1570 queue->cm_error = cm_error;
1571 complete(&queue->cm_done);
1572 }
1573
1574 return 0;
1575}
1576
1577static enum blk_eh_timer_return
1578nvme_rdma_timeout(struct request *rq, bool reserved)
1579{
1580 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1581
1582 /* queue error recovery */
1583 nvme_rdma_error_recovery(req->queue->ctrl);
1584
1585 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001586 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001587
1588 return BLK_EH_HANDLED;
1589}
1590
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001591/*
1592 * We cannot accept any other command until the Connect command has completed.
1593 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001594static inline blk_status_t
1595nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001596{
1597 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001598 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001599
Christoph Hellwig57292b52017-01-31 16:57:29 +01001600 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001601 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001602 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1603 /*
1604 * reconnecting state means transport disruption, which
1605 * can take a long time and even might fail permanently,
1606 * so we can't let incoming I/O be requeued forever.
1607 * fail it fast to allow upper layers a chance to
1608 * failover.
1609 */
1610 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001611 return BLK_STS_IOERR;
1612 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001613 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001614 }
1615
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001616 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001617}
1618
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001619static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001620 const struct blk_mq_queue_data *bd)
1621{
1622 struct nvme_ns *ns = hctx->queue->queuedata;
1623 struct nvme_rdma_queue *queue = hctx->driver_data;
1624 struct request *rq = bd->rq;
1625 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1626 struct nvme_rdma_qe *sqe = &req->sqe;
1627 struct nvme_command *c = sqe->data;
1628 bool flush = false;
1629 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001630 blk_status_t ret;
1631 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001632
1633 WARN_ON_ONCE(rq->tag < 0);
1634
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001635 ret = nvme_rdma_queue_is_ready(queue, rq);
1636 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001637 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001638
Christoph Hellwig71102302016-07-06 21:55:52 +09001639 dev = queue->device->dev;
1640 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1641 sizeof(struct nvme_command), DMA_TO_DEVICE);
1642
1643 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001644 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001645 return ret;
1646
Christoph Hellwig71102302016-07-06 21:55:52 +09001647 blk_mq_start_request(rq);
1648
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001649 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001650 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001651 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001652 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001653 nvme_cleanup_cmd(rq);
1654 goto err;
1655 }
1656
1657 ib_dma_sync_single_for_device(dev, sqe->dma,
1658 sizeof(struct nvme_command), DMA_TO_DEVICE);
1659
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001660 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001661 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001662 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001663 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001664 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001665 nvme_rdma_unmap_data(queue, rq);
1666 goto err;
1667 }
1668
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001669 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001670err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001671 if (err == -ENOMEM || err == -EAGAIN)
1672 return BLK_STS_RESOURCE;
1673 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001674}
1675
1676static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1677{
1678 struct nvme_rdma_queue *queue = hctx->driver_data;
1679 struct ib_cq *cq = queue->ib_cq;
1680 struct ib_wc wc;
1681 int found = 0;
1682
Christoph Hellwig71102302016-07-06 21:55:52 +09001683 while (ib_poll_cq(cq, 1, &wc) > 0) {
1684 struct ib_cqe *cqe = wc.wr_cqe;
1685
1686 if (cqe) {
1687 if (cqe->done == nvme_rdma_recv_done)
1688 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1689 else
1690 cqe->done(cq, &wc);
1691 }
1692 }
1693
1694 return found;
1695}
1696
1697static void nvme_rdma_complete_rq(struct request *rq)
1698{
1699 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001700
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001701 nvme_rdma_unmap_data(req->queue, rq);
1702 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001703}
1704
Sagi Grimberg0b366582017-07-13 11:09:44 +03001705static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1706{
1707 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1708
1709 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1710}
1711
Eric Biggersf363b082017-03-30 13:39:16 -07001712static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001713 .queue_rq = nvme_rdma_queue_rq,
1714 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001715 .init_request = nvme_rdma_init_request,
1716 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001717 .init_hctx = nvme_rdma_init_hctx,
1718 .poll = nvme_rdma_poll,
1719 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001720 .map_queues = nvme_rdma_map_queues,
Christoph Hellwig71102302016-07-06 21:55:52 +09001721};
1722
Eric Biggersf363b082017-03-30 13:39:16 -07001723static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001724 .queue_rq = nvme_rdma_queue_rq,
1725 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001726 .init_request = nvme_rdma_init_request,
1727 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001728 .init_hctx = nvme_rdma_init_admin_hctx,
1729 .timeout = nvme_rdma_timeout,
1730};
1731
Sagi Grimberg18398af2017-07-10 09:22:31 +03001732static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001733{
Christoph Hellwig71102302016-07-06 21:55:52 +09001734 cancel_work_sync(&ctrl->err_work);
1735 cancel_delayed_work_sync(&ctrl->reconnect_work);
1736
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001737 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001738 nvme_stop_queues(&ctrl->ctrl);
1739 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1740 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +02001741 nvme_rdma_destroy_io_queues(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001742 }
1743
Sagi Grimberg18398af2017-07-10 09:22:31 +03001744 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001745 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001746 else
1747 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001748
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001749 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001750 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1751 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb0513392017-07-02 15:33:32 +03001752 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001753 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001754}
1755
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001756static void nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001757{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001758 nvme_remove_namespaces(&ctrl->ctrl);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001759 nvme_rdma_shutdown_ctrl(ctrl, true);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001760 nvme_uninit_ctrl(&ctrl->ctrl);
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001761 nvme_put_ctrl(&ctrl->ctrl);
1762}
1763
Christoph Hellwig71102302016-07-06 21:55:52 +09001764static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1765{
1766 struct nvme_rdma_ctrl *ctrl = container_of(work,
1767 struct nvme_rdma_ctrl, delete_work);
1768
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001769 nvme_stop_ctrl(&ctrl->ctrl);
1770 nvme_rdma_remove_ctrl(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001771}
1772
1773static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1774{
1775 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1776 return -EBUSY;
1777
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001778 if (!queue_work(nvme_wq, &ctrl->delete_work))
Christoph Hellwig71102302016-07-06 21:55:52 +09001779 return -EBUSY;
1780
1781 return 0;
1782}
1783
1784static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1785{
1786 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001787 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001788
Steve Wisecdbecc82016-09-01 09:12:25 -07001789 /*
1790 * Keep a reference until all work is flushed since
1791 * __nvme_rdma_del_ctrl can free the ctrl mem
1792 */
1793 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1794 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001795 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001796 if (!ret)
1797 flush_work(&ctrl->delete_work);
1798 nvme_put_ctrl(&ctrl->ctrl);
1799 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001800}
1801
Christoph Hellwig71102302016-07-06 21:55:52 +09001802static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1803{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001804 struct nvme_rdma_ctrl *ctrl =
1805 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001806 int ret;
1807 bool changed;
1808
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001809 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001810 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001811
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001812 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001813 if (ret)
1814 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001815
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001816 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001817 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001818 if (ret)
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001819 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001820 }
1821
1822 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1823 WARN_ON_ONCE(!changed);
1824
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001825 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001826
1827 return;
1828
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001829out_fail:
Christoph Hellwig71102302016-07-06 21:55:52 +09001830 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001831 nvme_rdma_remove_ctrl(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001832}
1833
Christoph Hellwig71102302016-07-06 21:55:52 +09001834static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1835 .name = "rdma",
1836 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001837 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001838 .reg_read32 = nvmf_reg_read32,
1839 .reg_read64 = nvmf_reg_read64,
1840 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001841 .free_ctrl = nvme_rdma_free_ctrl,
1842 .submit_async_event = nvme_rdma_submit_async_event,
1843 .delete_ctrl = nvme_rdma_del_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001844 .get_address = nvmf_get_address,
Sagi Grimberg31b84462017-10-11 12:53:07 +03001845 .reinit_request = nvme_rdma_reinit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001846};
1847
Christoph Hellwig71102302016-07-06 21:55:52 +09001848static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1849 struct nvmf_ctrl_options *opts)
1850{
1851 struct nvme_rdma_ctrl *ctrl;
1852 int ret;
1853 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001854 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001855
1856 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1857 if (!ctrl)
1858 return ERR_PTR(-ENOMEM);
1859 ctrl->ctrl.opts = opts;
1860 INIT_LIST_HEAD(&ctrl->list);
1861
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001862 if (opts->mask & NVMF_OPT_TRSVCID)
1863 port = opts->trsvcid;
1864 else
1865 port = __stringify(NVME_RDMA_IP_PORT);
1866
1867 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1868 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001869 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001870 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001871 goto out_free_ctrl;
1872 }
1873
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001874 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001875 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1876 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001877 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001878 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001879 opts->host_traddr);
1880 goto out_free_ctrl;
1881 }
1882 }
1883
Christoph Hellwig71102302016-07-06 21:55:52 +09001884 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1885 0 /* no quirks, we're perfect! */);
1886 if (ret)
1887 goto out_free_ctrl;
1888
Christoph Hellwig71102302016-07-06 21:55:52 +09001889 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1890 nvme_rdma_reconnect_ctrl_work);
1891 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1892 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001893 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001894
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001895 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001896 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001897 ctrl->ctrl.kato = opts->kato;
1898
1899 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001900 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001901 GFP_KERNEL);
1902 if (!ctrl->queues)
1903 goto out_uninit_ctrl;
1904
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001905 ret = nvme_rdma_configure_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001906 if (ret)
1907 goto out_kfree_queues;
1908
1909 /* sanity check icdoff */
1910 if (ctrl->ctrl.icdoff) {
1911 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001912 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001913 goto out_remove_admin_queue;
1914 }
1915
1916 /* sanity check keyed sgls */
1917 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1918 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001919 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001920 goto out_remove_admin_queue;
1921 }
1922
1923 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1924 /* warn if maxcmd is lower than queue_size */
1925 dev_warn(ctrl->ctrl.device,
1926 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1927 opts->queue_size, ctrl->ctrl.maxcmd);
1928 opts->queue_size = ctrl->ctrl.maxcmd;
1929 }
1930
Samuel Jones76c08bf2016-10-25 09:22:34 +02001931 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1932 /* warn if sqsize is lower than queue_size */
1933 dev_warn(ctrl->ctrl.device,
1934 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1935 opts->queue_size, ctrl->ctrl.sqsize + 1);
1936 opts->queue_size = ctrl->ctrl.sqsize + 1;
1937 }
1938
Christoph Hellwig71102302016-07-06 21:55:52 +09001939 if (opts->nr_io_queues) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001940 ret = nvme_rdma_configure_io_queues(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001941 if (ret)
1942 goto out_remove_admin_queue;
1943 }
1944
1945 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1946 WARN_ON_ONCE(!changed);
1947
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001948 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001949 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1950
1951 kref_get(&ctrl->ctrl.kref);
1952
1953 mutex_lock(&nvme_rdma_ctrl_mutex);
1954 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1955 mutex_unlock(&nvme_rdma_ctrl_mutex);
1956
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001957 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001958
1959 return &ctrl->ctrl;
1960
1961out_remove_admin_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001962 nvme_rdma_destroy_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001963out_kfree_queues:
1964 kfree(ctrl->queues);
1965out_uninit_ctrl:
1966 nvme_uninit_ctrl(&ctrl->ctrl);
1967 nvme_put_ctrl(&ctrl->ctrl);
1968 if (ret > 0)
1969 ret = -EIO;
1970 return ERR_PTR(ret);
1971out_free_ctrl:
1972 kfree(ctrl);
1973 return ERR_PTR(ret);
1974}
1975
1976static struct nvmf_transport_ops nvme_rdma_transport = {
1977 .name = "rdma",
1978 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001979 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001980 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001981 .create_ctrl = nvme_rdma_create_ctrl,
1982};
1983
Steve Wisee87a9112016-09-02 09:01:54 -07001984static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1985{
1986 struct nvme_rdma_ctrl *ctrl;
1987
1988 /* Delete all controllers using this device */
1989 mutex_lock(&nvme_rdma_ctrl_mutex);
1990 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1991 if (ctrl->device->dev != ib_device)
1992 continue;
1993 dev_info(ctrl->ctrl.device,
1994 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1995 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1996 __nvme_rdma_del_ctrl(ctrl);
1997 }
1998 mutex_unlock(&nvme_rdma_ctrl_mutex);
1999
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002000 flush_workqueue(nvme_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002001}
2002
2003static struct ib_client nvme_rdma_ib_client = {
2004 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002005 .remove = nvme_rdma_remove_one
2006};
2007
Christoph Hellwig71102302016-07-06 21:55:52 +09002008static int __init nvme_rdma_init_module(void)
2009{
Steve Wisee87a9112016-09-02 09:01:54 -07002010 int ret;
2011
Steve Wisee87a9112016-09-02 09:01:54 -07002012 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002013 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002014 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002015
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002016 ret = nvmf_register_transport(&nvme_rdma_transport);
2017 if (ret)
2018 goto err_unreg_client;
2019
2020 return 0;
2021
2022err_unreg_client:
2023 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002024 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002025}
2026
2027static void __exit nvme_rdma_cleanup_module(void)
2028{
Christoph Hellwig71102302016-07-06 21:55:52 +09002029 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002030 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002031}
2032
2033module_init(nvme_rdma_init_module);
2034module_exit(nvme_rdma_cleanup_module);
2035
2036MODULE_LICENSE("GPL v2");