blob: 4dbee893a047f364b6271089b10420ace8dad3b5 [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
Sagi Grimbergbd9f0752017-10-19 16:00:30 +0300574 if (nvme_rdma_queue_idx(queue) == 0) {
575 nvme_rdma_free_qe(queue->device->dev,
576 &queue->ctrl->async_event_sqe,
577 sizeof(struct nvme_command), DMA_TO_DEVICE);
578 }
579
Sagi Grimberga57bd542017-08-28 21:41:10 +0200580 nvme_rdma_destroy_queue_ib(queue);
581 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900582}
583
584static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
585{
586 int i;
587
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300588 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200589 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900590}
591
Sagi Grimberga57bd542017-08-28 21:41:10 +0200592static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
593{
594 int i;
595
596 for (i = 1; i < ctrl->ctrl.queue_count; i++)
597 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900598}
599
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300600static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
601{
602 int ret;
603
604 if (idx)
605 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
606 else
607 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
608
609 if (!ret)
610 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
611 else
612 dev_info(ctrl->ctrl.device,
613 "failed to connect queue: %d ret=%d\n", idx, ret);
614 return ret;
615}
616
617static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900618{
619 int i, ret = 0;
620
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300621 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300622 ret = nvme_rdma_start_queue(ctrl, i);
623 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200624 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900625 }
626
Steve Wisec8dbc372016-11-08 09:16:02 -0800627 return 0;
628
Sagi Grimberga57bd542017-08-28 21:41:10 +0200629out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300630 for (i--; i >= 1; i--)
631 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900632 return ret;
633}
634
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300635static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900636{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200637 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300638 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200639 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900640 int i, ret;
641
Sagi Grimbergc248c642017-03-09 13:26:07 +0200642 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300643
644 /*
645 * we map queues according to the device irq vectors for
646 * optimal locality so we don't need more queues than
647 * completion vectors.
648 */
649 nr_io_queues = min_t(unsigned int, nr_io_queues,
650 ibdev->num_comp_vectors);
651
Sagi Grimbergc248c642017-03-09 13:26:07 +0200652 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
653 if (ret)
654 return ret;
655
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300656 ctrl->ctrl.queue_count = nr_io_queues + 1;
657 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200658 return 0;
659
660 dev_info(ctrl->ctrl.device,
661 "creating %d I/O queues.\n", nr_io_queues);
662
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300663 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300664 ret = nvme_rdma_alloc_queue(ctrl, i,
665 ctrl->ctrl.sqsize + 1);
666 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900667 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900668 }
669
670 return 0;
671
672out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700673 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200674 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900675
676 return ret;
677}
678
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300679static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl, bool admin)
680{
681 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
682 struct blk_mq_tag_set *set = admin ?
683 &ctrl->admin_tag_set : &ctrl->tag_set;
684
685 blk_mq_free_tag_set(set);
686 nvme_rdma_dev_put(ctrl->device);
687}
688
689static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
690 bool admin)
691{
692 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
693 struct blk_mq_tag_set *set;
694 int ret;
695
696 if (admin) {
697 set = &ctrl->admin_tag_set;
698 memset(set, 0, sizeof(*set));
699 set->ops = &nvme_rdma_admin_mq_ops;
700 set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
701 set->reserved_tags = 2; /* connect + keep-alive */
702 set->numa_node = NUMA_NO_NODE;
703 set->cmd_size = sizeof(struct nvme_rdma_request) +
704 SG_CHUNK_SIZE * sizeof(struct scatterlist);
705 set->driver_data = ctrl;
706 set->nr_hw_queues = 1;
707 set->timeout = ADMIN_TIMEOUT;
708 } else {
709 set = &ctrl->tag_set;
710 memset(set, 0, sizeof(*set));
711 set->ops = &nvme_rdma_mq_ops;
712 set->queue_depth = nctrl->opts->queue_size;
713 set->reserved_tags = 1; /* fabric connect */
714 set->numa_node = NUMA_NO_NODE;
715 set->flags = BLK_MQ_F_SHOULD_MERGE;
716 set->cmd_size = sizeof(struct nvme_rdma_request) +
717 SG_CHUNK_SIZE * sizeof(struct scatterlist);
718 set->driver_data = ctrl;
719 set->nr_hw_queues = nctrl->queue_count - 1;
720 set->timeout = NVME_IO_TIMEOUT;
721 }
722
723 ret = blk_mq_alloc_tag_set(set);
724 if (ret)
725 goto out;
726
727 /*
728 * We need a reference on the device as long as the tag_set is alive,
729 * as the MRs in the request structures need a valid ib_device.
730 */
731 ret = nvme_rdma_dev_get(ctrl->device);
732 if (!ret) {
733 ret = -EINVAL;
734 goto out_free_tagset;
735 }
736
737 return set;
738
739out_free_tagset:
740 blk_mq_free_tag_set(set);
741out:
742 return ERR_PTR(ret);
743}
744
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300745static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
746 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900747{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200748 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300749 if (remove) {
750 blk_cleanup_queue(ctrl->ctrl.admin_q);
751 nvme_rdma_free_tagset(&ctrl->ctrl, true);
752 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200753 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900754}
755
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300756static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
757 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300758{
759 int error;
760
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300761 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300762 if (error)
763 return error;
764
765 ctrl->device = ctrl->queues[0].device;
766
Sagi Grimberg90af3512017-07-10 09:22:28 +0300767 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
768 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
769
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300770 if (new) {
771 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
772 if (IS_ERR(ctrl->ctrl.admin_tagset))
773 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300774
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300775 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
776 if (IS_ERR(ctrl->ctrl.admin_q)) {
777 error = PTR_ERR(ctrl->ctrl.admin_q);
778 goto out_free_tagset;
779 }
780 } else {
781 error = blk_mq_reinit_tagset(&ctrl->admin_tag_set,
782 nvme_rdma_reinit_request);
783 if (error)
784 goto out_free_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300785 }
786
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300787 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300788 if (error)
789 goto out_cleanup_queue;
790
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300791 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300792 &ctrl->ctrl.cap);
793 if (error) {
794 dev_err(ctrl->ctrl.device,
795 "prop_get NVME_REG_CAP failed\n");
796 goto out_cleanup_queue;
797 }
798
799 ctrl->ctrl.sqsize =
800 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
801
802 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
803 if (error)
804 goto out_cleanup_queue;
805
806 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700807 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300808
809 error = nvme_init_identify(&ctrl->ctrl);
810 if (error)
811 goto out_cleanup_queue;
812
813 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
814 &ctrl->async_event_sqe, sizeof(struct nvme_command),
815 DMA_TO_DEVICE);
816 if (error)
817 goto out_cleanup_queue;
818
819 return 0;
820
821out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300822 if (new)
823 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300824out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300825 if (new)
826 nvme_rdma_free_tagset(&ctrl->ctrl, true);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300827out_free_queue:
828 nvme_rdma_free_queue(&ctrl->queues[0]);
829 return error;
830}
831
Sagi Grimberga57bd542017-08-28 21:41:10 +0200832static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
833 bool remove)
834{
835 nvme_rdma_stop_io_queues(ctrl);
836 if (remove) {
837 blk_cleanup_queue(ctrl->ctrl.connect_q);
838 nvme_rdma_free_tagset(&ctrl->ctrl, false);
839 }
840 nvme_rdma_free_io_queues(ctrl);
841}
842
843static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
844{
845 int ret;
846
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300847 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200848 if (ret)
849 return ret;
850
851 if (new) {
852 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
853 if (IS_ERR(ctrl->ctrl.tagset))
854 goto out_free_io_queues;
855
856 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
857 if (IS_ERR(ctrl->ctrl.connect_q)) {
858 ret = PTR_ERR(ctrl->ctrl.connect_q);
859 goto out_free_tag_set;
860 }
861 } else {
862 ret = blk_mq_reinit_tagset(&ctrl->tag_set,
863 nvme_rdma_reinit_request);
864 if (ret)
865 goto out_free_io_queues;
866
867 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
868 ctrl->ctrl.queue_count - 1);
869 }
870
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300871 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200872 if (ret)
873 goto out_cleanup_connect_q;
874
875 return 0;
876
877out_cleanup_connect_q:
878 if (new)
879 blk_cleanup_queue(ctrl->ctrl.connect_q);
880out_free_tag_set:
881 if (new)
882 nvme_rdma_free_tagset(&ctrl->ctrl, false);
883out_free_io_queues:
884 nvme_rdma_free_io_queues(ctrl);
885 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900886}
887
888static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
889{
890 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
891
892 if (list_empty(&ctrl->list))
893 goto free_ctrl;
894
895 mutex_lock(&nvme_rdma_ctrl_mutex);
896 list_del(&ctrl->list);
897 mutex_unlock(&nvme_rdma_ctrl_mutex);
898
Christoph Hellwig71102302016-07-06 21:55:52 +0900899 kfree(ctrl->queues);
900 nvmf_free_options(nctrl->opts);
901free_ctrl:
902 kfree(ctrl);
903}
904
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200905static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
906{
907 /* If we are resetting/deleting then do nothing */
908 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
909 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
910 ctrl->ctrl.state == NVME_CTRL_LIVE);
911 return;
912 }
913
914 if (nvmf_should_reconnect(&ctrl->ctrl)) {
915 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
916 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200917 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200918 ctrl->ctrl.opts->reconnect_delay * HZ);
919 } else {
920 dev_info(ctrl->ctrl.device, "Removing controller...\n");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200921 queue_work(nvme_wq, &ctrl->delete_work);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200922 }
923}
924
Christoph Hellwig71102302016-07-06 21:55:52 +0900925static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
926{
927 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
928 struct nvme_rdma_ctrl, reconnect_work);
929 bool changed;
930 int ret;
931
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300932 ++ctrl->ctrl.nr_reconnects;
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200933
Sagi Grimberga57bd542017-08-28 21:41:10 +0200934 if (ctrl->ctrl.queue_count > 1)
935 nvme_rdma_destroy_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900936
Sagi Grimberg31fdf182017-08-28 21:40:06 +0200937 nvme_rdma_destroy_admin_queue(ctrl, false);
938 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900939 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300940 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900941
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300942 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200943 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +0900944 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300945 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900946 }
947
948 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +0300949 if (!changed) {
950 /* state change failure is ok if we're in DELETING state */
951 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
952 return;
953 }
954
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300955 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900956
Sagi Grimbergd09f2b42017-07-02 10:56:43 +0300957 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900958
959 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
960
961 return;
962
Christoph Hellwig71102302016-07-06 21:55:52 +0900963requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200964 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +0300965 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200966 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900967}
968
969static void nvme_rdma_error_recovery_work(struct work_struct *work)
970{
971 struct nvme_rdma_ctrl *ctrl = container_of(work,
972 struct nvme_rdma_ctrl, err_work);
973
Sagi Grimberge4d753d2017-09-21 17:01:38 +0300974 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700975
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300976 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900977 nvme_stop_queues(&ctrl->ctrl);
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300978 nvme_rdma_stop_io_queues(ctrl);
979 }
Sagi Grimbergfb051332017-07-02 15:33:32 +0300980 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg148b4e72017-07-10 09:22:35 +0300981 nvme_rdma_stop_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900982
983 /* We must take care of fastfail/requeue all our inflight requests */
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300984 if (ctrl->ctrl.queue_count > 1)
Christoph Hellwig71102302016-07-06 21:55:52 +0900985 blk_mq_tagset_busy_iter(&ctrl->tag_set,
986 nvme_cancel_request, &ctrl->ctrl);
987 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
988 nvme_cancel_request, &ctrl->ctrl);
989
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300990 /*
991 * queues are not a live anymore, so restart the queues to fail fast
992 * new IO
993 */
Sagi Grimbergfb051332017-07-02 15:33:32 +0300994 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300995 nvme_start_queues(&ctrl->ctrl);
996
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200997 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900998}
999
1000static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1001{
1002 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
1003 return;
1004
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001005 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001006}
1007
1008static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1009 const char *op)
1010{
1011 struct nvme_rdma_queue *queue = cq->cq_context;
1012 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1013
1014 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1015 dev_info(ctrl->ctrl.device,
1016 "%s for CQE 0x%p failed with status %s (%d)\n",
1017 op, wc->wr_cqe,
1018 ib_wc_status_msg(wc->status), wc->status);
1019 nvme_rdma_error_recovery(ctrl);
1020}
1021
1022static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1023{
1024 if (unlikely(wc->status != IB_WC_SUCCESS))
1025 nvme_rdma_wr_error(cq, wc, "MEMREG");
1026}
1027
1028static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1029{
1030 if (unlikely(wc->status != IB_WC_SUCCESS))
1031 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1032}
1033
1034static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1035 struct nvme_rdma_request *req)
1036{
1037 struct ib_send_wr *bad_wr;
1038 struct ib_send_wr wr = {
1039 .opcode = IB_WR_LOCAL_INV,
1040 .next = NULL,
1041 .num_sge = 0,
1042 .send_flags = 0,
1043 .ex.invalidate_rkey = req->mr->rkey,
1044 };
1045
1046 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1047 wr.wr_cqe = &req->reg_cqe;
1048
1049 return ib_post_send(queue->qp, &wr, &bad_wr);
1050}
1051
1052static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1053 struct request *rq)
1054{
1055 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1056 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1057 struct nvme_rdma_device *dev = queue->device;
1058 struct ib_device *ibdev = dev->dev;
1059 int res;
1060
1061 if (!blk_rq_bytes(rq))
1062 return;
1063
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001064 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001065 res = nvme_rdma_inv_rkey(queue, req);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001066 if (unlikely(res < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001067 dev_err(ctrl->ctrl.device,
1068 "Queueing INV WR for rkey %#x failed (%d)\n",
1069 req->mr->rkey, res);
1070 nvme_rdma_error_recovery(queue->ctrl);
1071 }
1072 }
1073
1074 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1075 req->nents, rq_data_dir(rq) ==
1076 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1077
1078 nvme_cleanup_cmd(rq);
1079 sg_free_table_chained(&req->sg_table, true);
1080}
1081
1082static int nvme_rdma_set_sg_null(struct nvme_command *c)
1083{
1084 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1085
1086 sg->addr = 0;
1087 put_unaligned_le24(0, sg->length);
1088 put_unaligned_le32(0, sg->key);
1089 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1090 return 0;
1091}
1092
1093static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1094 struct nvme_rdma_request *req, struct nvme_command *c)
1095{
1096 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1097
1098 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1099 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1100 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1101
1102 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1103 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1104 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1105
1106 req->inline_data = true;
1107 req->num_sge++;
1108 return 0;
1109}
1110
1111static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1112 struct nvme_rdma_request *req, struct nvme_command *c)
1113{
1114 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1115
1116 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1117 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001118 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001119 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1120 return 0;
1121}
1122
1123static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1124 struct nvme_rdma_request *req, struct nvme_command *c,
1125 int count)
1126{
1127 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1128 int nr;
1129
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001130 /*
1131 * Align the MR to a 4K page size to match the ctrl page size and
1132 * the block virtual boundary.
1133 */
1134 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001135 if (unlikely(nr < count)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001136 if (nr < 0)
1137 return nr;
1138 return -EINVAL;
1139 }
1140
1141 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1142
1143 req->reg_cqe.done = nvme_rdma_memreg_done;
1144 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1145 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1146 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1147 req->reg_wr.wr.num_sge = 0;
1148 req->reg_wr.mr = req->mr;
1149 req->reg_wr.key = req->mr->rkey;
1150 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1151 IB_ACCESS_REMOTE_READ |
1152 IB_ACCESS_REMOTE_WRITE;
1153
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001154 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001155
1156 sg->addr = cpu_to_le64(req->mr->iova);
1157 put_unaligned_le24(req->mr->length, sg->length);
1158 put_unaligned_le32(req->mr->rkey, sg->key);
1159 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1160 NVME_SGL_FMT_INVALIDATE;
1161
1162 return 0;
1163}
1164
1165static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001166 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001167{
1168 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1169 struct nvme_rdma_device *dev = queue->device;
1170 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001171 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001172
1173 req->num_sge = 1;
1174 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001175 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001176
1177 c->common.flags |= NVME_CMD_SGL_METABUF;
1178
1179 if (!blk_rq_bytes(rq))
1180 return nvme_rdma_set_sg_null(c);
1181
1182 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001183 ret = sg_alloc_table_chained(&req->sg_table,
1184 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001185 if (ret)
1186 return -ENOMEM;
1187
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001188 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001189
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001190 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001191 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1192 if (unlikely(count <= 0)) {
1193 sg_free_table_chained(&req->sg_table, true);
1194 return -EIO;
1195 }
1196
1197 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001198 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1199 blk_rq_payload_bytes(rq) <=
1200 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001201 return nvme_rdma_map_sg_inline(queue, req, c);
1202
Christoph Hellwig11975e02016-09-05 12:56:20 +02001203 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001204 return nvme_rdma_map_sg_single(queue, req, c);
1205 }
1206
1207 return nvme_rdma_map_sg_fr(queue, req, c, count);
1208}
1209
1210static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1211{
1212 if (unlikely(wc->status != IB_WC_SUCCESS))
1213 nvme_rdma_wr_error(cq, wc, "SEND");
1214}
1215
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001216/*
1217 * We want to signal completion at least every queue depth/2. This returns the
1218 * largest power of two that is not above half of (queue size + 1) to optimize
1219 * (avoid divisions).
1220 */
1221static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
Marta Rybczynska0544f542017-04-10 17:12:34 +02001222{
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001223 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
Marta Rybczynska0544f542017-04-10 17:12:34 +02001224
Marta Rybczynska5e599d72017-06-06 13:27:21 +02001225 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
Marta Rybczynska0544f542017-04-10 17:12:34 +02001226}
1227
Christoph Hellwig71102302016-07-06 21:55:52 +09001228static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1229 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1230 struct ib_send_wr *first, bool flush)
1231{
1232 struct ib_send_wr wr, *bad_wr;
1233 int ret;
1234
1235 sge->addr = qe->dma;
1236 sge->length = sizeof(struct nvme_command),
1237 sge->lkey = queue->device->pd->local_dma_lkey;
1238
1239 qe->cqe.done = nvme_rdma_send_done;
1240
1241 wr.next = NULL;
1242 wr.wr_cqe = &qe->cqe;
1243 wr.sg_list = sge;
1244 wr.num_sge = num_sge;
1245 wr.opcode = IB_WR_SEND;
1246 wr.send_flags = 0;
1247
1248 /*
1249 * Unsignalled send completions are another giant desaster in the
1250 * IB Verbs spec: If we don't regularly post signalled sends
1251 * the send queue will fill up and only a QP reset will rescue us.
1252 * Would have been way to obvious to handle this in hardware or
1253 * at least the RDMA stack..
1254 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001255 * Always signal the flushes. The magic request used for the flush
1256 * sequencer is not allocated in our driver's tagset and it's
1257 * triggered to be freed by blk_cleanup_queue(). So we need to
1258 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001259 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001260 * calls wr_cqe->done().
1261 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001262 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001263 wr.send_flags |= IB_SEND_SIGNALED;
1264
1265 if (first)
1266 first->next = &wr;
1267 else
1268 first = &wr;
1269
1270 ret = ib_post_send(queue->qp, first, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001271 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001272 dev_err(queue->ctrl->ctrl.device,
1273 "%s failed with error code %d\n", __func__, ret);
1274 }
1275 return ret;
1276}
1277
1278static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1279 struct nvme_rdma_qe *qe)
1280{
1281 struct ib_recv_wr wr, *bad_wr;
1282 struct ib_sge list;
1283 int ret;
1284
1285 list.addr = qe->dma;
1286 list.length = sizeof(struct nvme_completion);
1287 list.lkey = queue->device->pd->local_dma_lkey;
1288
1289 qe->cqe.done = nvme_rdma_recv_done;
1290
1291 wr.next = NULL;
1292 wr.wr_cqe = &qe->cqe;
1293 wr.sg_list = &list;
1294 wr.num_sge = 1;
1295
1296 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001297 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001298 dev_err(queue->ctrl->ctrl.device,
1299 "%s failed with error code %d\n", __func__, ret);
1300 }
1301 return ret;
1302}
1303
1304static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1305{
1306 u32 queue_idx = nvme_rdma_queue_idx(queue);
1307
1308 if (queue_idx == 0)
1309 return queue->ctrl->admin_tag_set.tags[queue_idx];
1310 return queue->ctrl->tag_set.tags[queue_idx - 1];
1311}
1312
1313static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1314{
1315 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1316 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1317 struct ib_device *dev = queue->device->dev;
1318 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1319 struct nvme_command *cmd = sqe->data;
1320 struct ib_sge sge;
1321 int ret;
1322
1323 if (WARN_ON_ONCE(aer_idx != 0))
1324 return;
1325
1326 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1327
1328 memset(cmd, 0, sizeof(*cmd));
1329 cmd->common.opcode = nvme_admin_async_event;
1330 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1331 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1332 nvme_rdma_set_sg_null(cmd);
1333
1334 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1335 DMA_TO_DEVICE);
1336
1337 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1338 WARN_ON_ONCE(ret);
1339}
1340
1341static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1342 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1343{
Christoph Hellwig71102302016-07-06 21:55:52 +09001344 struct request *rq;
1345 struct nvme_rdma_request *req;
1346 int ret = 0;
1347
Christoph Hellwig71102302016-07-06 21:55:52 +09001348 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1349 if (!rq) {
1350 dev_err(queue->ctrl->ctrl.device,
1351 "tag 0x%x on QP %#x not found\n",
1352 cqe->command_id, queue->qp->qp_num);
1353 nvme_rdma_error_recovery(queue->ctrl);
1354 return ret;
1355 }
1356 req = blk_mq_rq_to_pdu(rq);
1357
Christoph Hellwig71102302016-07-06 21:55:52 +09001358 if (rq->tag == tag)
1359 ret = 1;
1360
1361 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1362 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001363 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001364
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001365 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001366 return ret;
1367}
1368
1369static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1370{
1371 struct nvme_rdma_qe *qe =
1372 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1373 struct nvme_rdma_queue *queue = cq->cq_context;
1374 struct ib_device *ibdev = queue->device->dev;
1375 struct nvme_completion *cqe = qe->data;
1376 const size_t len = sizeof(struct nvme_completion);
1377 int ret = 0;
1378
1379 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1380 nvme_rdma_wr_error(cq, wc, "RECV");
1381 return 0;
1382 }
1383
1384 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1385 /*
1386 * AEN requests are special as they don't time out and can
1387 * survive any kind of queue freeze and often don't respond to
1388 * aborts. We don't even bother to allocate a struct request
1389 * for them but rather special case them here.
1390 */
1391 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1392 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001393 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1394 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001395 else
1396 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1397 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1398
1399 nvme_rdma_post_recv(queue, qe);
1400 return ret;
1401}
1402
1403static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1404{
1405 __nvme_rdma_recv_done(cq, wc, -1);
1406}
1407
1408static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1409{
1410 int ret, i;
1411
1412 for (i = 0; i < queue->queue_size; i++) {
1413 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1414 if (ret)
1415 goto out_destroy_queue_ib;
1416 }
1417
1418 return 0;
1419
1420out_destroy_queue_ib:
1421 nvme_rdma_destroy_queue_ib(queue);
1422 return ret;
1423}
1424
1425static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1426 struct rdma_cm_event *ev)
1427{
Steve Wise7f039532016-10-26 12:36:47 -07001428 struct rdma_cm_id *cm_id = queue->cm_id;
1429 int status = ev->status;
1430 const char *rej_msg;
1431 const struct nvme_rdma_cm_rej *rej_data;
1432 u8 rej_data_len;
1433
1434 rej_msg = rdma_reject_msg(cm_id, status);
1435 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1436
1437 if (rej_data && rej_data_len >= sizeof(u16)) {
1438 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001439
1440 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001441 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1442 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001443 } else {
1444 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001445 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001446 }
1447
1448 return -ECONNRESET;
1449}
1450
1451static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1452{
Christoph Hellwig71102302016-07-06 21:55:52 +09001453 int ret;
1454
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001455 ret = nvme_rdma_create_queue_ib(queue);
1456 if (ret)
1457 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001458
1459 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1460 if (ret) {
1461 dev_err(queue->ctrl->ctrl.device,
1462 "rdma_resolve_route failed (%d).\n",
1463 queue->cm_error);
1464 goto out_destroy_queue;
1465 }
1466
1467 return 0;
1468
1469out_destroy_queue:
1470 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001471 return ret;
1472}
1473
1474static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1475{
1476 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1477 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001478 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001479 int ret;
1480
1481 param.qp_num = queue->qp->qp_num;
1482 param.flow_control = 1;
1483
1484 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001485 /* maximum retry count */
1486 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001487 param.rnr_retry_count = 7;
1488 param.private_data = &priv;
1489 param.private_data_len = sizeof(priv);
1490
1491 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1492 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001493 /*
1494 * set the admin queue depth to the minimum size
1495 * specified by the Fabrics standard.
1496 */
1497 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001498 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1499 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001500 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001501 /*
1502 * current interpretation of the fabrics spec
1503 * is at minimum you make hrqsize sqsize+1, or a
1504 * 1's based representation of sqsize.
1505 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001506 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001507 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001508 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001509
1510 ret = rdma_connect(queue->cm_id, &param);
1511 if (ret) {
1512 dev_err(ctrl->ctrl.device,
1513 "rdma_connect failed (%d).\n", ret);
1514 goto out_destroy_queue_ib;
1515 }
1516
1517 return 0;
1518
1519out_destroy_queue_ib:
1520 nvme_rdma_destroy_queue_ib(queue);
1521 return ret;
1522}
1523
Christoph Hellwig71102302016-07-06 21:55:52 +09001524static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1525 struct rdma_cm_event *ev)
1526{
1527 struct nvme_rdma_queue *queue = cm_id->context;
1528 int cm_error = 0;
1529
1530 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1531 rdma_event_msg(ev->event), ev->event,
1532 ev->status, cm_id);
1533
1534 switch (ev->event) {
1535 case RDMA_CM_EVENT_ADDR_RESOLVED:
1536 cm_error = nvme_rdma_addr_resolved(queue);
1537 break;
1538 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1539 cm_error = nvme_rdma_route_resolved(queue);
1540 break;
1541 case RDMA_CM_EVENT_ESTABLISHED:
1542 queue->cm_error = nvme_rdma_conn_established(queue);
1543 /* complete cm_done regardless of success/failure */
1544 complete(&queue->cm_done);
1545 return 0;
1546 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001547 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001548 cm_error = nvme_rdma_conn_rejected(queue, ev);
1549 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001550 case RDMA_CM_EVENT_ROUTE_ERROR:
1551 case RDMA_CM_EVENT_CONNECT_ERROR:
1552 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001553 nvme_rdma_destroy_queue_ib(queue);
1554 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001555 dev_dbg(queue->ctrl->ctrl.device,
1556 "CM error event %d\n", ev->event);
1557 cm_error = -ECONNRESET;
1558 break;
1559 case RDMA_CM_EVENT_DISCONNECTED:
1560 case RDMA_CM_EVENT_ADDR_CHANGE:
1561 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1562 dev_dbg(queue->ctrl->ctrl.device,
1563 "disconnect received - connection closed\n");
1564 nvme_rdma_error_recovery(queue->ctrl);
1565 break;
1566 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001567 /* device removal is handled via the ib_client API */
1568 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001569 default:
1570 dev_err(queue->ctrl->ctrl.device,
1571 "Unexpected RDMA CM event (%d)\n", ev->event);
1572 nvme_rdma_error_recovery(queue->ctrl);
1573 break;
1574 }
1575
1576 if (cm_error) {
1577 queue->cm_error = cm_error;
1578 complete(&queue->cm_done);
1579 }
1580
1581 return 0;
1582}
1583
1584static enum blk_eh_timer_return
1585nvme_rdma_timeout(struct request *rq, bool reserved)
1586{
1587 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1588
1589 /* queue error recovery */
1590 nvme_rdma_error_recovery(req->queue->ctrl);
1591
1592 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001593 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001594
1595 return BLK_EH_HANDLED;
1596}
1597
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001598/*
1599 * We cannot accept any other command until the Connect command has completed.
1600 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001601static inline blk_status_t
1602nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001603{
1604 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001605 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001606
Christoph Hellwig57292b52017-01-31 16:57:29 +01001607 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001608 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001609 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1610 /*
1611 * reconnecting state means transport disruption, which
1612 * can take a long time and even might fail permanently,
1613 * so we can't let incoming I/O be requeued forever.
1614 * fail it fast to allow upper layers a chance to
1615 * failover.
1616 */
1617 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001618 return BLK_STS_IOERR;
1619 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001620 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001621 }
1622
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001623 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001624}
1625
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001626static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001627 const struct blk_mq_queue_data *bd)
1628{
1629 struct nvme_ns *ns = hctx->queue->queuedata;
1630 struct nvme_rdma_queue *queue = hctx->driver_data;
1631 struct request *rq = bd->rq;
1632 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1633 struct nvme_rdma_qe *sqe = &req->sqe;
1634 struct nvme_command *c = sqe->data;
1635 bool flush = false;
1636 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001637 blk_status_t ret;
1638 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001639
1640 WARN_ON_ONCE(rq->tag < 0);
1641
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001642 ret = nvme_rdma_queue_is_ready(queue, rq);
1643 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001644 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001645
Christoph Hellwig71102302016-07-06 21:55:52 +09001646 dev = queue->device->dev;
1647 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1648 sizeof(struct nvme_command), DMA_TO_DEVICE);
1649
1650 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001651 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001652 return ret;
1653
Christoph Hellwig71102302016-07-06 21:55:52 +09001654 blk_mq_start_request(rq);
1655
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001656 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001657 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001658 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001659 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001660 nvme_cleanup_cmd(rq);
1661 goto err;
1662 }
1663
1664 ib_dma_sync_single_for_device(dev, sqe->dma,
1665 sizeof(struct nvme_command), DMA_TO_DEVICE);
1666
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001667 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001668 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001669 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001670 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001671 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001672 nvme_rdma_unmap_data(queue, rq);
1673 goto err;
1674 }
1675
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001676 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001677err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001678 if (err == -ENOMEM || err == -EAGAIN)
1679 return BLK_STS_RESOURCE;
1680 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001681}
1682
1683static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1684{
1685 struct nvme_rdma_queue *queue = hctx->driver_data;
1686 struct ib_cq *cq = queue->ib_cq;
1687 struct ib_wc wc;
1688 int found = 0;
1689
Christoph Hellwig71102302016-07-06 21:55:52 +09001690 while (ib_poll_cq(cq, 1, &wc) > 0) {
1691 struct ib_cqe *cqe = wc.wr_cqe;
1692
1693 if (cqe) {
1694 if (cqe->done == nvme_rdma_recv_done)
1695 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1696 else
1697 cqe->done(cq, &wc);
1698 }
1699 }
1700
1701 return found;
1702}
1703
1704static void nvme_rdma_complete_rq(struct request *rq)
1705{
1706 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001707
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001708 nvme_rdma_unmap_data(req->queue, rq);
1709 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001710}
1711
Sagi Grimberg0b366582017-07-13 11:09:44 +03001712static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1713{
1714 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1715
1716 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1717}
1718
Eric Biggersf363b082017-03-30 13:39:16 -07001719static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001720 .queue_rq = nvme_rdma_queue_rq,
1721 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001722 .init_request = nvme_rdma_init_request,
1723 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001724 .init_hctx = nvme_rdma_init_hctx,
1725 .poll = nvme_rdma_poll,
1726 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001727 .map_queues = nvme_rdma_map_queues,
Christoph Hellwig71102302016-07-06 21:55:52 +09001728};
1729
Eric Biggersf363b082017-03-30 13:39:16 -07001730static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001731 .queue_rq = nvme_rdma_queue_rq,
1732 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001733 .init_request = nvme_rdma_init_request,
1734 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001735 .init_hctx = nvme_rdma_init_admin_hctx,
1736 .timeout = nvme_rdma_timeout,
1737};
1738
Sagi Grimberg18398af2017-07-10 09:22:31 +03001739static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001740{
Christoph Hellwig71102302016-07-06 21:55:52 +09001741 cancel_work_sync(&ctrl->err_work);
1742 cancel_delayed_work_sync(&ctrl->reconnect_work);
1743
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001744 if (ctrl->ctrl.queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001745 nvme_stop_queues(&ctrl->ctrl);
1746 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1747 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +02001748 nvme_rdma_destroy_io_queues(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001749 }
1750
Sagi Grimberg18398af2017-07-10 09:22:31 +03001751 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001752 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001753 else
1754 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Christoph Hellwig71102302016-07-06 21:55:52 +09001755
Sagi Grimbergfb051332017-07-02 15:33:32 +03001756 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Christoph Hellwig71102302016-07-06 21:55:52 +09001757 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1758 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimbergfb051332017-07-02 15:33:32 +03001759 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001760 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001761}
1762
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001763static void nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001764{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001765 nvme_remove_namespaces(&ctrl->ctrl);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001766 nvme_rdma_shutdown_ctrl(ctrl, true);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001767 nvme_uninit_ctrl(&ctrl->ctrl);
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001768 nvme_put_ctrl(&ctrl->ctrl);
1769}
1770
Christoph Hellwig71102302016-07-06 21:55:52 +09001771static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1772{
1773 struct nvme_rdma_ctrl *ctrl = container_of(work,
1774 struct nvme_rdma_ctrl, delete_work);
1775
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001776 nvme_stop_ctrl(&ctrl->ctrl);
1777 nvme_rdma_remove_ctrl(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001778}
1779
1780static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1781{
1782 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1783 return -EBUSY;
1784
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001785 if (!queue_work(nvme_wq, &ctrl->delete_work))
Christoph Hellwig71102302016-07-06 21:55:52 +09001786 return -EBUSY;
1787
1788 return 0;
1789}
1790
1791static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1792{
1793 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001794 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001795
Steve Wisecdbecc82016-09-01 09:12:25 -07001796 /*
1797 * Keep a reference until all work is flushed since
1798 * __nvme_rdma_del_ctrl can free the ctrl mem
1799 */
1800 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1801 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001802 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001803 if (!ret)
1804 flush_work(&ctrl->delete_work);
1805 nvme_put_ctrl(&ctrl->ctrl);
1806 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001807}
1808
Christoph Hellwig71102302016-07-06 21:55:52 +09001809static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1810{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001811 struct nvme_rdma_ctrl *ctrl =
1812 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001813 int ret;
1814 bool changed;
1815
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001816 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001817 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001818
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001819 ret = nvme_rdma_configure_admin_queue(ctrl, false);
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001820 if (ret)
1821 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001822
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001823 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001824 ret = nvme_rdma_configure_io_queues(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001825 if (ret)
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001826 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001827 }
1828
1829 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1830 WARN_ON_ONCE(!changed);
1831
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001832 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001833
1834 return;
1835
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001836out_fail:
Christoph Hellwig71102302016-07-06 21:55:52 +09001837 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001838 nvme_rdma_remove_ctrl(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001839}
1840
Christoph Hellwig71102302016-07-06 21:55:52 +09001841static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1842 .name = "rdma",
1843 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001844 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001845 .reg_read32 = nvmf_reg_read32,
1846 .reg_read64 = nvmf_reg_read64,
1847 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001848 .free_ctrl = nvme_rdma_free_ctrl,
1849 .submit_async_event = nvme_rdma_submit_async_event,
1850 .delete_ctrl = nvme_rdma_del_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001851 .get_address = nvmf_get_address,
1852};
1853
Christoph Hellwig71102302016-07-06 21:55:52 +09001854static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1855 struct nvmf_ctrl_options *opts)
1856{
1857 struct nvme_rdma_ctrl *ctrl;
1858 int ret;
1859 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001860 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001861
1862 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1863 if (!ctrl)
1864 return ERR_PTR(-ENOMEM);
1865 ctrl->ctrl.opts = opts;
1866 INIT_LIST_HEAD(&ctrl->list);
1867
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001868 if (opts->mask & NVMF_OPT_TRSVCID)
1869 port = opts->trsvcid;
1870 else
1871 port = __stringify(NVME_RDMA_IP_PORT);
1872
1873 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1874 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001875 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001876 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001877 goto out_free_ctrl;
1878 }
1879
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001880 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001881 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1882 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001883 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001884 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001885 opts->host_traddr);
1886 goto out_free_ctrl;
1887 }
1888 }
1889
Christoph Hellwig71102302016-07-06 21:55:52 +09001890 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1891 0 /* no quirks, we're perfect! */);
1892 if (ret)
1893 goto out_free_ctrl;
1894
Christoph Hellwig71102302016-07-06 21:55:52 +09001895 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1896 nvme_rdma_reconnect_ctrl_work);
1897 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1898 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001899 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001900
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001901 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001902 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001903 ctrl->ctrl.kato = opts->kato;
1904
1905 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001906 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001907 GFP_KERNEL);
1908 if (!ctrl->queues)
1909 goto out_uninit_ctrl;
1910
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001911 ret = nvme_rdma_configure_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001912 if (ret)
1913 goto out_kfree_queues;
1914
1915 /* sanity check icdoff */
1916 if (ctrl->ctrl.icdoff) {
1917 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001918 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001919 goto out_remove_admin_queue;
1920 }
1921
1922 /* sanity check keyed sgls */
1923 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1924 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
Dan Carpenterbb472ba2017-06-14 13:46:45 +03001925 ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001926 goto out_remove_admin_queue;
1927 }
1928
1929 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1930 /* warn if maxcmd is lower than queue_size */
1931 dev_warn(ctrl->ctrl.device,
1932 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1933 opts->queue_size, ctrl->ctrl.maxcmd);
1934 opts->queue_size = ctrl->ctrl.maxcmd;
1935 }
1936
Samuel Jones76c08bf2016-10-25 09:22:34 +02001937 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1938 /* warn if sqsize is lower than queue_size */
1939 dev_warn(ctrl->ctrl.device,
1940 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1941 opts->queue_size, ctrl->ctrl.sqsize + 1);
1942 opts->queue_size = ctrl->ctrl.sqsize + 1;
1943 }
1944
Christoph Hellwig71102302016-07-06 21:55:52 +09001945 if (opts->nr_io_queues) {
Sagi Grimberga57bd542017-08-28 21:41:10 +02001946 ret = nvme_rdma_configure_io_queues(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001947 if (ret)
1948 goto out_remove_admin_queue;
1949 }
1950
1951 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1952 WARN_ON_ONCE(!changed);
1953
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001954 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001955 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1956
1957 kref_get(&ctrl->ctrl.kref);
1958
1959 mutex_lock(&nvme_rdma_ctrl_mutex);
1960 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1961 mutex_unlock(&nvme_rdma_ctrl_mutex);
1962
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001963 nvme_start_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001964
1965 return &ctrl->ctrl;
1966
1967out_remove_admin_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +03001968 nvme_rdma_destroy_admin_queue(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001969out_kfree_queues:
1970 kfree(ctrl->queues);
1971out_uninit_ctrl:
1972 nvme_uninit_ctrl(&ctrl->ctrl);
1973 nvme_put_ctrl(&ctrl->ctrl);
1974 if (ret > 0)
1975 ret = -EIO;
1976 return ERR_PTR(ret);
1977out_free_ctrl:
1978 kfree(ctrl);
1979 return ERR_PTR(ret);
1980}
1981
1982static struct nvmf_transport_ops nvme_rdma_transport = {
1983 .name = "rdma",
1984 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001985 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001986 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001987 .create_ctrl = nvme_rdma_create_ctrl,
1988};
1989
Steve Wisee87a9112016-09-02 09:01:54 -07001990static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1991{
1992 struct nvme_rdma_ctrl *ctrl;
1993
1994 /* Delete all controllers using this device */
1995 mutex_lock(&nvme_rdma_ctrl_mutex);
1996 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1997 if (ctrl->device->dev != ib_device)
1998 continue;
1999 dev_info(ctrl->ctrl.device,
2000 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2001 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2002 __nvme_rdma_del_ctrl(ctrl);
2003 }
2004 mutex_unlock(&nvme_rdma_ctrl_mutex);
2005
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002006 flush_workqueue(nvme_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002007}
2008
2009static struct ib_client nvme_rdma_ib_client = {
2010 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002011 .remove = nvme_rdma_remove_one
2012};
2013
Christoph Hellwig71102302016-07-06 21:55:52 +09002014static int __init nvme_rdma_init_module(void)
2015{
Steve Wisee87a9112016-09-02 09:01:54 -07002016 int ret;
2017
Steve Wisee87a9112016-09-02 09:01:54 -07002018 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002019 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002020 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002021
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002022 ret = nvmf_register_transport(&nvme_rdma_transport);
2023 if (ret)
2024 goto err_unreg_client;
2025
2026 return 0;
2027
2028err_unreg_client:
2029 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002030 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002031}
2032
2033static void __exit nvme_rdma_cleanup_module(void)
2034{
Christoph Hellwig71102302016-07-06 21:55:52 +09002035 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002036 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002037}
2038
2039module_init(nvme_rdma_init_module);
2040module_exit(nvme_rdma_cleanup_module);
2041
2042MODULE_LICENSE("GPL v2");