blob: aea78d67f8beba6c221559c91c0412507e047ef5 [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>
22#include <linux/types.h>
23#include <linux/list.h>
24#include <linux/mutex.h>
25#include <linux/scatterlist.h>
26#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090027#include <asm/unaligned.h>
28
29#include <rdma/ib_verbs.h>
30#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090031#include <linux/nvme-rdma.h>
32
33#include "nvme.h"
34#include "fabrics.h"
35
36
Sagi Grimberg782d8202017-03-21 16:32:38 +020037#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090038
39#define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
40
41#define NVME_RDMA_MAX_SEGMENTS 256
42
43#define NVME_RDMA_MAX_INLINE_SEGMENTS 1
44
Christoph Hellwig71102302016-07-06 21:55:52 +090045/*
46 * We handle AEN commands ourselves and don't even let the
47 * block layer know about them.
48 */
49#define NVME_RDMA_NR_AEN_COMMANDS 1
50#define NVME_RDMA_AQ_BLKMQ_DEPTH \
51 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
52
53struct nvme_rdma_device {
54 struct ib_device *dev;
55 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090056 struct kref ref;
57 struct list_head entry;
58};
59
60struct nvme_rdma_qe {
61 struct ib_cqe cqe;
62 void *data;
63 u64 dma;
64};
65
66struct nvme_rdma_queue;
67struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080068 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090069 struct ib_mr *mr;
70 struct nvme_rdma_qe sqe;
71 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
72 u32 num_sge;
73 int nents;
74 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090075 struct ib_reg_wr reg_wr;
76 struct ib_cqe reg_cqe;
77 struct nvme_rdma_queue *queue;
78 struct sg_table sg_table;
79 struct scatterlist first_sgl[];
80};
81
82enum nvme_rdma_queue_flags {
Sagi Grimbergdc5bc6a2017-05-04 13:33:07 +030083 NVME_RDMA_Q_CONNECTED = 0,
Sagi Grimbergabf87d52017-05-04 13:33:10 +030084 NVME_RDMA_Q_DELETING = 1,
85 NVME_RDMA_Q_LIVE = 2,
Christoph Hellwig71102302016-07-06 21:55:52 +090086};
87
88struct nvme_rdma_queue {
89 struct nvme_rdma_qe *rsp_ring;
90 u8 sig_count;
91 int queue_size;
92 size_t cmnd_capsule_len;
93 struct nvme_rdma_ctrl *ctrl;
94 struct nvme_rdma_device *device;
95 struct ib_cq *ib_cq;
96 struct ib_qp *qp;
97
98 unsigned long flags;
99 struct rdma_cm_id *cm_id;
100 int cm_error;
101 struct completion cm_done;
102};
103
104struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +0900105 /* read only in the hot path */
106 struct nvme_rdma_queue *queues;
107 u32 queue_count;
108
109 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900110 struct blk_mq_tag_set tag_set;
111 struct work_struct delete_work;
112 struct work_struct reset_work;
113 struct work_struct err_work;
114
115 struct nvme_rdma_qe async_event_sqe;
116
Christoph Hellwig71102302016-07-06 21:55:52 +0900117 struct delayed_work reconnect_work;
118
119 struct list_head list;
120
121 struct blk_mq_tag_set admin_tag_set;
122 struct nvme_rdma_device *device;
123
124 u64 cap;
125 u32 max_fr_pages;
126
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200127 struct sockaddr_storage addr;
128 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900129
130 struct nvme_ctrl ctrl;
131};
132
133static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
134{
135 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
136}
137
138static LIST_HEAD(device_list);
139static DEFINE_MUTEX(device_list_mutex);
140
141static LIST_HEAD(nvme_rdma_ctrl_list);
142static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
143
144static struct workqueue_struct *nvme_rdma_wq;
145
146/*
147 * Disabling this option makes small I/O goes faster, but is fundamentally
148 * unsafe. With it turned off we will have to register a global rkey that
149 * allows read and write access to all physical memory.
150 */
151static bool register_always = true;
152module_param(register_always, bool, 0444);
153MODULE_PARM_DESC(register_always,
154 "Use memory registration even for contiguous memory regions");
155
156static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
157 struct rdma_cm_event *event);
158static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900159
160/* XXX: really should move to a generic header sooner or later.. */
161static inline void put_unaligned_le24(u32 val, u8 *p)
162{
163 *p++ = val;
164 *p++ = val >> 8;
165 *p++ = val >> 16;
166}
167
168static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
169{
170 return queue - queue->ctrl->queues;
171}
172
173static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
174{
175 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
176}
177
178static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
179 size_t capsule_size, enum dma_data_direction dir)
180{
181 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
182 kfree(qe->data);
183}
184
185static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
186 size_t capsule_size, enum dma_data_direction dir)
187{
188 qe->data = kzalloc(capsule_size, GFP_KERNEL);
189 if (!qe->data)
190 return -ENOMEM;
191
192 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
193 if (ib_dma_mapping_error(ibdev, qe->dma)) {
194 kfree(qe->data);
195 return -ENOMEM;
196 }
197
198 return 0;
199}
200
201static void nvme_rdma_free_ring(struct ib_device *ibdev,
202 struct nvme_rdma_qe *ring, size_t ib_queue_size,
203 size_t capsule_size, enum dma_data_direction dir)
204{
205 int i;
206
207 for (i = 0; i < ib_queue_size; i++)
208 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
209 kfree(ring);
210}
211
212static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
213 size_t ib_queue_size, size_t capsule_size,
214 enum dma_data_direction dir)
215{
216 struct nvme_rdma_qe *ring;
217 int i;
218
219 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
220 if (!ring)
221 return NULL;
222
223 for (i = 0; i < ib_queue_size; i++) {
224 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
225 goto out_free_ring;
226 }
227
228 return ring;
229
230out_free_ring:
231 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
232 return NULL;
233}
234
235static void nvme_rdma_qp_event(struct ib_event *event, void *context)
236{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200237 pr_debug("QP event %s (%d)\n",
238 ib_event_msg(event->event), event->event);
239
Christoph Hellwig71102302016-07-06 21:55:52 +0900240}
241
242static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
243{
244 wait_for_completion_interruptible_timeout(&queue->cm_done,
245 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
246 return queue->cm_error;
247}
248
249static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
250{
251 struct nvme_rdma_device *dev = queue->device;
252 struct ib_qp_init_attr init_attr;
253 int ret;
254
255 memset(&init_attr, 0, sizeof(init_attr));
256 init_attr.event_handler = nvme_rdma_qp_event;
257 /* +1 for drain */
258 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
259 /* +1 for drain */
260 init_attr.cap.max_recv_wr = queue->queue_size + 1;
261 init_attr.cap.max_recv_sge = 1;
262 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
263 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
264 init_attr.qp_type = IB_QPT_RC;
265 init_attr.send_cq = queue->ib_cq;
266 init_attr.recv_cq = queue->ib_cq;
267
268 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
269
270 queue->qp = queue->cm_id->qp;
271 return ret;
272}
273
274static int nvme_rdma_reinit_request(void *data, struct request *rq)
275{
276 struct nvme_rdma_ctrl *ctrl = data;
277 struct nvme_rdma_device *dev = ctrl->device;
278 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
279 int ret = 0;
280
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300281 if (!req->mr->need_inval)
Christoph Hellwig71102302016-07-06 21:55:52 +0900282 goto out;
283
284 ib_dereg_mr(req->mr);
285
286 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
287 ctrl->max_fr_pages);
288 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900289 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000290 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100291 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900292 }
293
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300294 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900295
296out:
297 return ret;
298}
299
300static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
301 struct request *rq, unsigned int queue_idx)
302{
303 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
304 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
305 struct nvme_rdma_device *dev = queue->device;
306
307 if (req->mr)
308 ib_dereg_mr(req->mr);
309
310 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
311 DMA_TO_DEVICE);
312}
313
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600314static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
315 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900316{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600317 return __nvme_rdma_exit_request(set->driver_data, rq, hctx_idx + 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900318}
319
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600320static void nvme_rdma_exit_admin_request(struct blk_mq_tag_set *set,
321 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900322{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600323 return __nvme_rdma_exit_request(set->driver_data, rq, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900324}
325
326static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
327 struct request *rq, unsigned int queue_idx)
328{
329 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
330 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
331 struct nvme_rdma_device *dev = queue->device;
332 struct ib_device *ibdev = dev->dev;
333 int ret;
334
Christoph Hellwig71102302016-07-06 21:55:52 +0900335 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
336 DMA_TO_DEVICE);
337 if (ret)
338 return ret;
339
340 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
341 ctrl->max_fr_pages);
342 if (IS_ERR(req->mr)) {
343 ret = PTR_ERR(req->mr);
344 goto out_free_qe;
345 }
346
347 req->queue = queue;
348
349 return 0;
350
351out_free_qe:
352 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
353 DMA_TO_DEVICE);
354 return -ENOMEM;
355}
356
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600357static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
358 struct request *rq, unsigned int hctx_idx,
359 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900360{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600361 return __nvme_rdma_init_request(set->driver_data, rq, hctx_idx + 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900362}
363
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600364static int nvme_rdma_init_admin_request(struct blk_mq_tag_set *set,
365 struct request *rq, unsigned int hctx_idx,
366 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900367{
Christoph Hellwigd6296d392017-05-01 10:19:08 -0600368 return __nvme_rdma_init_request(set->driver_data, rq, 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900369}
370
371static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
372 unsigned int hctx_idx)
373{
374 struct nvme_rdma_ctrl *ctrl = data;
375 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
376
377 BUG_ON(hctx_idx >= ctrl->queue_count);
378
379 hctx->driver_data = queue;
380 return 0;
381}
382
383static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
384 unsigned int hctx_idx)
385{
386 struct nvme_rdma_ctrl *ctrl = data;
387 struct nvme_rdma_queue *queue = &ctrl->queues[0];
388
389 BUG_ON(hctx_idx != 0);
390
391 hctx->driver_data = queue;
392 return 0;
393}
394
395static void nvme_rdma_free_dev(struct kref *ref)
396{
397 struct nvme_rdma_device *ndev =
398 container_of(ref, struct nvme_rdma_device, ref);
399
400 mutex_lock(&device_list_mutex);
401 list_del(&ndev->entry);
402 mutex_unlock(&device_list_mutex);
403
Christoph Hellwig71102302016-07-06 21:55:52 +0900404 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900405 kfree(ndev);
406}
407
408static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
409{
410 kref_put(&dev->ref, nvme_rdma_free_dev);
411}
412
413static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
414{
415 return kref_get_unless_zero(&dev->ref);
416}
417
418static struct nvme_rdma_device *
419nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
420{
421 struct nvme_rdma_device *ndev;
422
423 mutex_lock(&device_list_mutex);
424 list_for_each_entry(ndev, &device_list, entry) {
425 if (ndev->dev->node_guid == cm_id->device->node_guid &&
426 nvme_rdma_dev_get(ndev))
427 goto out_unlock;
428 }
429
430 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
431 if (!ndev)
432 goto out_err;
433
434 ndev->dev = cm_id->device;
435 kref_init(&ndev->ref);
436
Christoph Hellwig11975e02016-09-05 12:56:20 +0200437 ndev->pd = ib_alloc_pd(ndev->dev,
438 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900439 if (IS_ERR(ndev->pd))
440 goto out_free_dev;
441
Christoph Hellwig71102302016-07-06 21:55:52 +0900442 if (!(ndev->dev->attrs.device_cap_flags &
443 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
444 dev_err(&ndev->dev->dev,
445 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200446 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900447 }
448
449 list_add(&ndev->entry, &device_list);
450out_unlock:
451 mutex_unlock(&device_list_mutex);
452 return ndev;
453
Christoph Hellwig71102302016-07-06 21:55:52 +0900454out_free_pd:
455 ib_dealloc_pd(ndev->pd);
456out_free_dev:
457 kfree(ndev);
458out_err:
459 mutex_unlock(&device_list_mutex);
460 return NULL;
461}
462
463static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
464{
Steve Wisef361e5a2016-09-02 09:01:27 -0700465 struct nvme_rdma_device *dev;
466 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900467
Steve Wisef361e5a2016-09-02 09:01:27 -0700468 dev = queue->device;
469 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900470 rdma_destroy_qp(queue->cm_id);
471 ib_free_cq(queue->ib_cq);
472
473 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
474 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
475
476 nvme_rdma_dev_put(dev);
477}
478
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300479static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900480{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300481 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900482 const int send_wr_factor = 3; /* MR, SEND, INV */
483 const int cq_factor = send_wr_factor + 1; /* + RECV */
484 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900485 int ret;
486
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300487 queue->device = nvme_rdma_find_get_device(queue->cm_id);
488 if (!queue->device) {
489 dev_err(queue->cm_id->device->dev.parent,
490 "no client data found!\n");
491 return -ECONNREFUSED;
492 }
493 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900494
495 /*
496 * The admin queue is barely used once the controller is live, so don't
497 * bother to spread it out.
498 */
499 if (idx == 0)
500 comp_vector = 0;
501 else
502 comp_vector = idx % ibdev->num_comp_vectors;
503
504
505 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300506 queue->ib_cq = ib_alloc_cq(ibdev, queue,
507 cq_factor * queue->queue_size + 1,
508 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900509 if (IS_ERR(queue->ib_cq)) {
510 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300511 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900512 }
513
514 ret = nvme_rdma_create_qp(queue, send_wr_factor);
515 if (ret)
516 goto out_destroy_ib_cq;
517
518 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
519 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
520 if (!queue->rsp_ring) {
521 ret = -ENOMEM;
522 goto out_destroy_qp;
523 }
524
525 return 0;
526
527out_destroy_qp:
528 ib_destroy_qp(queue->qp);
529out_destroy_ib_cq:
530 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300531out_put_dev:
532 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900533 return ret;
534}
535
536static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
537 int idx, size_t queue_size)
538{
539 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200540 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900541 int ret;
542
543 queue = &ctrl->queues[idx];
544 queue->ctrl = ctrl;
545 init_completion(&queue->cm_done);
546
547 if (idx > 0)
548 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
549 else
550 queue->cmnd_capsule_len = sizeof(struct nvme_command);
551
552 queue->queue_size = queue_size;
553
554 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
555 RDMA_PS_TCP, IB_QPT_RC);
556 if (IS_ERR(queue->cm_id)) {
557 dev_info(ctrl->ctrl.device,
558 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
559 return PTR_ERR(queue->cm_id);
560 }
561
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200562 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200563 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200564
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200565 queue->cm_error = -ETIMEDOUT;
566 ret = rdma_resolve_addr(queue->cm_id, src_addr,
567 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900568 NVME_RDMA_CONNECT_TIMEOUT_MS);
569 if (ret) {
570 dev_info(ctrl->ctrl.device,
571 "rdma_resolve_addr failed (%d).\n", ret);
572 goto out_destroy_cm_id;
573 }
574
575 ret = nvme_rdma_wait_for_cm(queue);
576 if (ret) {
577 dev_info(ctrl->ctrl.device,
578 "rdma_resolve_addr wait failed (%d).\n", ret);
579 goto out_destroy_cm_id;
580 }
581
Sagi Grimberg3b4ac782016-09-22 19:58:17 -0600582 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900583 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
584
585 return 0;
586
587out_destroy_cm_id:
588 rdma_destroy_id(queue->cm_id);
589 return ret;
590}
591
592static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
593{
594 rdma_disconnect(queue->cm_id);
595 ib_drain_qp(queue->qp);
596}
597
598static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
599{
600 nvme_rdma_destroy_queue_ib(queue);
601 rdma_destroy_id(queue->cm_id);
602}
603
604static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
605{
Sagi Grimberge89ca582016-09-02 09:01:54 -0700606 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900607 return;
608 nvme_rdma_stop_queue(queue);
609 nvme_rdma_free_queue(queue);
610}
611
612static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
613{
614 int i;
615
616 for (i = 1; i < ctrl->queue_count; i++)
617 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
618}
619
620static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
621{
622 int i, ret = 0;
623
624 for (i = 1; i < ctrl->queue_count; i++) {
625 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
Steve Wisec8dbc372016-11-08 09:16:02 -0800626 if (ret) {
627 dev_info(ctrl->ctrl.device,
628 "failed to connect i/o queue: %d\n", ret);
629 goto out_free_queues;
630 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600631 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900632 }
633
Steve Wisec8dbc372016-11-08 09:16:02 -0800634 return 0;
635
636out_free_queues:
637 nvme_rdma_free_io_queues(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900638 return ret;
639}
640
641static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
642{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200643 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
644 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900645 int i, ret;
646
Sagi Grimbergc248c642017-03-09 13:26:07 +0200647 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
648 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
649 if (ret)
650 return ret;
651
652 ctrl->queue_count = nr_io_queues + 1;
653 if (ctrl->queue_count < 2)
654 return 0;
655
656 dev_info(ctrl->ctrl.device,
657 "creating %d I/O queues.\n", nr_io_queues);
658
Christoph Hellwig71102302016-07-06 21:55:52 +0900659 for (i = 1; i < ctrl->queue_count; i++) {
Jay Freyenseec5af8652016-08-17 15:00:27 -0700660 ret = nvme_rdma_init_queue(ctrl, i,
661 ctrl->ctrl.opts->queue_size);
Christoph Hellwig71102302016-07-06 21:55:52 +0900662 if (ret) {
663 dev_info(ctrl->ctrl.device,
664 "failed to initialize i/o queue: %d\n", ret);
665 goto out_free_queues;
666 }
667 }
668
669 return 0;
670
671out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700672 for (i--; i >= 1; i--)
Christoph Hellwig71102302016-07-06 21:55:52 +0900673 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
674
675 return ret;
676}
677
678static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
679{
680 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
681 sizeof(struct nvme_command), DMA_TO_DEVICE);
682 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
683 blk_cleanup_queue(ctrl->ctrl.admin_q);
684 blk_mq_free_tag_set(&ctrl->admin_tag_set);
685 nvme_rdma_dev_put(ctrl->device);
686}
687
688static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
689{
690 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
691
692 if (list_empty(&ctrl->list))
693 goto free_ctrl;
694
695 mutex_lock(&nvme_rdma_ctrl_mutex);
696 list_del(&ctrl->list);
697 mutex_unlock(&nvme_rdma_ctrl_mutex);
698
Christoph Hellwig71102302016-07-06 21:55:52 +0900699 kfree(ctrl->queues);
700 nvmf_free_options(nctrl->opts);
701free_ctrl:
702 kfree(ctrl);
703}
704
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200705static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
706{
707 /* If we are resetting/deleting then do nothing */
708 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
709 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
710 ctrl->ctrl.state == NVME_CTRL_LIVE);
711 return;
712 }
713
714 if (nvmf_should_reconnect(&ctrl->ctrl)) {
715 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
716 ctrl->ctrl.opts->reconnect_delay);
717 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
718 ctrl->ctrl.opts->reconnect_delay * HZ);
719 } else {
720 dev_info(ctrl->ctrl.device, "Removing controller...\n");
721 queue_work(nvme_rdma_wq, &ctrl->delete_work);
722 }
723}
724
Christoph Hellwig71102302016-07-06 21:55:52 +0900725static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
726{
727 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
728 struct nvme_rdma_ctrl, reconnect_work);
729 bool changed;
730 int ret;
731
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200732 ++ctrl->ctrl.opts->nr_reconnects;
733
Christoph Hellwig71102302016-07-06 21:55:52 +0900734 if (ctrl->queue_count > 1) {
735 nvme_rdma_free_io_queues(ctrl);
736
737 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
738 if (ret)
739 goto requeue;
740 }
741
742 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
743
744 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
745 if (ret)
746 goto requeue;
747
748 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
749 if (ret)
750 goto requeue;
751
Christoph Hellwig71102302016-07-06 21:55:52 +0900752 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
753 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300754 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900755
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600756 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
757
Christoph Hellwig71102302016-07-06 21:55:52 +0900758 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
759 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300760 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900761
762 nvme_start_keep_alive(&ctrl->ctrl);
763
764 if (ctrl->queue_count > 1) {
765 ret = nvme_rdma_init_io_queues(ctrl);
766 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300767 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900768
769 ret = nvme_rdma_connect_io_queues(ctrl);
770 if (ret)
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300771 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +0900772 }
773
774 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
775 WARN_ON_ONCE(!changed);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200776 ctrl->ctrl.opts->nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900777
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300778 if (ctrl->queue_count > 1) {
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300779 nvme_queue_scan(&ctrl->ctrl);
Sagi Grimberg3ef1b4b2016-08-04 13:46:19 +0300780 nvme_queue_async_events(&ctrl->ctrl);
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300781 }
Christoph Hellwig71102302016-07-06 21:55:52 +0900782
783 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
784
785 return;
786
Christoph Hellwig71102302016-07-06 21:55:52 +0900787requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200788 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
789 ctrl->ctrl.opts->nr_reconnects);
790 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900791}
792
793static void nvme_rdma_error_recovery_work(struct work_struct *work)
794{
795 struct nvme_rdma_ctrl *ctrl = container_of(work,
796 struct nvme_rdma_ctrl, err_work);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700797 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +0900798
799 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700800
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600801 for (i = 0; i < ctrl->queue_count; i++) {
Sagi Grimberge89ca582016-09-02 09:01:54 -0700802 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600803 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
804 }
Sagi Grimberge89ca582016-09-02 09:01:54 -0700805
Christoph Hellwig71102302016-07-06 21:55:52 +0900806 if (ctrl->queue_count > 1)
807 nvme_stop_queues(&ctrl->ctrl);
808 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
809
810 /* We must take care of fastfail/requeue all our inflight requests */
811 if (ctrl->queue_count > 1)
812 blk_mq_tagset_busy_iter(&ctrl->tag_set,
813 nvme_cancel_request, &ctrl->ctrl);
814 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
815 nvme_cancel_request, &ctrl->ctrl);
816
Sagi Grimberge818a5b2017-06-05 20:35:56 +0300817 /*
818 * queues are not a live anymore, so restart the queues to fail fast
819 * new IO
820 */
821 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
822 nvme_start_queues(&ctrl->ctrl);
823
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200824 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900825}
826
827static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
828{
829 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
830 return;
831
832 queue_work(nvme_rdma_wq, &ctrl->err_work);
833}
834
835static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
836 const char *op)
837{
838 struct nvme_rdma_queue *queue = cq->cq_context;
839 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
840
841 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
842 dev_info(ctrl->ctrl.device,
843 "%s for CQE 0x%p failed with status %s (%d)\n",
844 op, wc->wr_cqe,
845 ib_wc_status_msg(wc->status), wc->status);
846 nvme_rdma_error_recovery(ctrl);
847}
848
849static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
850{
851 if (unlikely(wc->status != IB_WC_SUCCESS))
852 nvme_rdma_wr_error(cq, wc, "MEMREG");
853}
854
855static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
856{
857 if (unlikely(wc->status != IB_WC_SUCCESS))
858 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
859}
860
861static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
862 struct nvme_rdma_request *req)
863{
864 struct ib_send_wr *bad_wr;
865 struct ib_send_wr wr = {
866 .opcode = IB_WR_LOCAL_INV,
867 .next = NULL,
868 .num_sge = 0,
869 .send_flags = 0,
870 .ex.invalidate_rkey = req->mr->rkey,
871 };
872
873 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
874 wr.wr_cqe = &req->reg_cqe;
875
876 return ib_post_send(queue->qp, &wr, &bad_wr);
877}
878
879static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
880 struct request *rq)
881{
882 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
883 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
884 struct nvme_rdma_device *dev = queue->device;
885 struct ib_device *ibdev = dev->dev;
886 int res;
887
888 if (!blk_rq_bytes(rq))
889 return;
890
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300891 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900892 res = nvme_rdma_inv_rkey(queue, req);
893 if (res < 0) {
894 dev_err(ctrl->ctrl.device,
895 "Queueing INV WR for rkey %#x failed (%d)\n",
896 req->mr->rkey, res);
897 nvme_rdma_error_recovery(queue->ctrl);
898 }
899 }
900
901 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
902 req->nents, rq_data_dir(rq) ==
903 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
904
905 nvme_cleanup_cmd(rq);
906 sg_free_table_chained(&req->sg_table, true);
907}
908
909static int nvme_rdma_set_sg_null(struct nvme_command *c)
910{
911 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
912
913 sg->addr = 0;
914 put_unaligned_le24(0, sg->length);
915 put_unaligned_le32(0, sg->key);
916 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
917 return 0;
918}
919
920static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
921 struct nvme_rdma_request *req, struct nvme_command *c)
922{
923 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
924
925 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
926 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
927 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
928
929 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
930 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
931 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
932
933 req->inline_data = true;
934 req->num_sge++;
935 return 0;
936}
937
938static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
939 struct nvme_rdma_request *req, struct nvme_command *c)
940{
941 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
942
943 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
944 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +0200945 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +0900946 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
947 return 0;
948}
949
950static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
951 struct nvme_rdma_request *req, struct nvme_command *c,
952 int count)
953{
954 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
955 int nr;
956
957 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
958 if (nr < count) {
959 if (nr < 0)
960 return nr;
961 return -EINVAL;
962 }
963
964 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
965
966 req->reg_cqe.done = nvme_rdma_memreg_done;
967 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
968 req->reg_wr.wr.opcode = IB_WR_REG_MR;
969 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
970 req->reg_wr.wr.num_sge = 0;
971 req->reg_wr.mr = req->mr;
972 req->reg_wr.key = req->mr->rkey;
973 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
974 IB_ACCESS_REMOTE_READ |
975 IB_ACCESS_REMOTE_WRITE;
976
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300977 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +0900978
979 sg->addr = cpu_to_le64(req->mr->iova);
980 put_unaligned_le24(req->mr->length, sg->length);
981 put_unaligned_le32(req->mr->rkey, sg->key);
982 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
983 NVME_SGL_FMT_INVALIDATE;
984
985 return 0;
986}
987
988static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100989 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +0900990{
991 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
992 struct nvme_rdma_device *dev = queue->device;
993 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700994 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900995
996 req->num_sge = 1;
997 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300998 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900999
1000 c->common.flags |= NVME_CMD_SGL_METABUF;
1001
1002 if (!blk_rq_bytes(rq))
1003 return nvme_rdma_set_sg_null(c);
1004
1005 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001006 ret = sg_alloc_table_chained(&req->sg_table,
1007 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001008 if (ret)
1009 return -ENOMEM;
1010
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001011 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001012
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001013 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001014 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1015 if (unlikely(count <= 0)) {
1016 sg_free_table_chained(&req->sg_table, true);
1017 return -EIO;
1018 }
1019
1020 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001021 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1022 blk_rq_payload_bytes(rq) <=
1023 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001024 return nvme_rdma_map_sg_inline(queue, req, c);
1025
Christoph Hellwig11975e02016-09-05 12:56:20 +02001026 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001027 return nvme_rdma_map_sg_single(queue, req, c);
1028 }
1029
1030 return nvme_rdma_map_sg_fr(queue, req, c, count);
1031}
1032
1033static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1034{
1035 if (unlikely(wc->status != IB_WC_SUCCESS))
1036 nvme_rdma_wr_error(cq, wc, "SEND");
1037}
1038
Marta Rybczynska0544f542017-04-10 17:12:34 +02001039static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1040{
1041 int sig_limit;
1042
1043 /*
1044 * We signal completion every queue depth/2 and also handle the
1045 * degenerated case of a device with queue_depth=1, where we
1046 * would need to signal every message.
1047 */
1048 sig_limit = max(queue->queue_size / 2, 1);
1049 return (++queue->sig_count % sig_limit) == 0;
1050}
1051
Christoph Hellwig71102302016-07-06 21:55:52 +09001052static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1053 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1054 struct ib_send_wr *first, bool flush)
1055{
1056 struct ib_send_wr wr, *bad_wr;
1057 int ret;
1058
1059 sge->addr = qe->dma;
1060 sge->length = sizeof(struct nvme_command),
1061 sge->lkey = queue->device->pd->local_dma_lkey;
1062
1063 qe->cqe.done = nvme_rdma_send_done;
1064
1065 wr.next = NULL;
1066 wr.wr_cqe = &qe->cqe;
1067 wr.sg_list = sge;
1068 wr.num_sge = num_sge;
1069 wr.opcode = IB_WR_SEND;
1070 wr.send_flags = 0;
1071
1072 /*
1073 * Unsignalled send completions are another giant desaster in the
1074 * IB Verbs spec: If we don't regularly post signalled sends
1075 * the send queue will fill up and only a QP reset will rescue us.
1076 * Would have been way to obvious to handle this in hardware or
1077 * at least the RDMA stack..
1078 *
Christoph Hellwig71102302016-07-06 21:55:52 +09001079 * Always signal the flushes. The magic request used for the flush
1080 * sequencer is not allocated in our driver's tagset and it's
1081 * triggered to be freed by blk_cleanup_queue(). So we need to
1082 * always mark it as signaled to ensure that the "wr_cqe", which is
Masahiro Yamadab43daed2017-02-27 14:29:09 -08001083 * embedded in request's payload, is not freed when __ib_process_cq()
Christoph Hellwig71102302016-07-06 21:55:52 +09001084 * calls wr_cqe->done().
1085 */
Marta Rybczynska0544f542017-04-10 17:12:34 +02001086 if (nvme_rdma_queue_sig_limit(queue) || flush)
Christoph Hellwig71102302016-07-06 21:55:52 +09001087 wr.send_flags |= IB_SEND_SIGNALED;
1088
1089 if (first)
1090 first->next = &wr;
1091 else
1092 first = &wr;
1093
1094 ret = ib_post_send(queue->qp, first, &bad_wr);
1095 if (ret) {
1096 dev_err(queue->ctrl->ctrl.device,
1097 "%s failed with error code %d\n", __func__, ret);
1098 }
1099 return ret;
1100}
1101
1102static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1103 struct nvme_rdma_qe *qe)
1104{
1105 struct ib_recv_wr wr, *bad_wr;
1106 struct ib_sge list;
1107 int ret;
1108
1109 list.addr = qe->dma;
1110 list.length = sizeof(struct nvme_completion);
1111 list.lkey = queue->device->pd->local_dma_lkey;
1112
1113 qe->cqe.done = nvme_rdma_recv_done;
1114
1115 wr.next = NULL;
1116 wr.wr_cqe = &qe->cqe;
1117 wr.sg_list = &list;
1118 wr.num_sge = 1;
1119
1120 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1121 if (ret) {
1122 dev_err(queue->ctrl->ctrl.device,
1123 "%s failed with error code %d\n", __func__, ret);
1124 }
1125 return ret;
1126}
1127
1128static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1129{
1130 u32 queue_idx = nvme_rdma_queue_idx(queue);
1131
1132 if (queue_idx == 0)
1133 return queue->ctrl->admin_tag_set.tags[queue_idx];
1134 return queue->ctrl->tag_set.tags[queue_idx - 1];
1135}
1136
1137static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1138{
1139 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1140 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1141 struct ib_device *dev = queue->device->dev;
1142 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1143 struct nvme_command *cmd = sqe->data;
1144 struct ib_sge sge;
1145 int ret;
1146
1147 if (WARN_ON_ONCE(aer_idx != 0))
1148 return;
1149
1150 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1151
1152 memset(cmd, 0, sizeof(*cmd));
1153 cmd->common.opcode = nvme_admin_async_event;
1154 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1155 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1156 nvme_rdma_set_sg_null(cmd);
1157
1158 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1159 DMA_TO_DEVICE);
1160
1161 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1162 WARN_ON_ONCE(ret);
1163}
1164
1165static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1166 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1167{
Christoph Hellwig71102302016-07-06 21:55:52 +09001168 struct request *rq;
1169 struct nvme_rdma_request *req;
1170 int ret = 0;
1171
Christoph Hellwig71102302016-07-06 21:55:52 +09001172 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1173 if (!rq) {
1174 dev_err(queue->ctrl->ctrl.device,
1175 "tag 0x%x on QP %#x not found\n",
1176 cqe->command_id, queue->qp->qp_num);
1177 nvme_rdma_error_recovery(queue->ctrl);
1178 return ret;
1179 }
1180 req = blk_mq_rq_to_pdu(rq);
1181
Christoph Hellwig71102302016-07-06 21:55:52 +09001182 if (rq->tag == tag)
1183 ret = 1;
1184
1185 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1186 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001187 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001188
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001189 nvme_end_request(rq, cqe->status, cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001190 return ret;
1191}
1192
1193static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1194{
1195 struct nvme_rdma_qe *qe =
1196 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1197 struct nvme_rdma_queue *queue = cq->cq_context;
1198 struct ib_device *ibdev = queue->device->dev;
1199 struct nvme_completion *cqe = qe->data;
1200 const size_t len = sizeof(struct nvme_completion);
1201 int ret = 0;
1202
1203 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1204 nvme_rdma_wr_error(cq, wc, "RECV");
1205 return 0;
1206 }
1207
1208 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1209 /*
1210 * AEN requests are special as they don't time out and can
1211 * survive any kind of queue freeze and often don't respond to
1212 * aborts. We don't even bother to allocate a struct request
1213 * for them but rather special case them here.
1214 */
1215 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1216 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001217 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1218 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001219 else
1220 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1221 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1222
1223 nvme_rdma_post_recv(queue, qe);
1224 return ret;
1225}
1226
1227static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1228{
1229 __nvme_rdma_recv_done(cq, wc, -1);
1230}
1231
1232static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1233{
1234 int ret, i;
1235
1236 for (i = 0; i < queue->queue_size; i++) {
1237 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1238 if (ret)
1239 goto out_destroy_queue_ib;
1240 }
1241
1242 return 0;
1243
1244out_destroy_queue_ib:
1245 nvme_rdma_destroy_queue_ib(queue);
1246 return ret;
1247}
1248
1249static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1250 struct rdma_cm_event *ev)
1251{
Steve Wise7f039532016-10-26 12:36:47 -07001252 struct rdma_cm_id *cm_id = queue->cm_id;
1253 int status = ev->status;
1254 const char *rej_msg;
1255 const struct nvme_rdma_cm_rej *rej_data;
1256 u8 rej_data_len;
1257
1258 rej_msg = rdma_reject_msg(cm_id, status);
1259 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1260
1261 if (rej_data && rej_data_len >= sizeof(u16)) {
1262 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001263
1264 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001265 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1266 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001267 } else {
1268 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001269 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001270 }
1271
1272 return -ECONNRESET;
1273}
1274
1275static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1276{
Christoph Hellwig71102302016-07-06 21:55:52 +09001277 int ret;
1278
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001279 ret = nvme_rdma_create_queue_ib(queue);
1280 if (ret)
1281 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001282
1283 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1284 if (ret) {
1285 dev_err(queue->ctrl->ctrl.device,
1286 "rdma_resolve_route failed (%d).\n",
1287 queue->cm_error);
1288 goto out_destroy_queue;
1289 }
1290
1291 return 0;
1292
1293out_destroy_queue:
1294 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001295 return ret;
1296}
1297
1298static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1299{
1300 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1301 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001302 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001303 int ret;
1304
1305 param.qp_num = queue->qp->qp_num;
1306 param.flow_control = 1;
1307
1308 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001309 /* maximum retry count */
1310 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001311 param.rnr_retry_count = 7;
1312 param.private_data = &priv;
1313 param.private_data_len = sizeof(priv);
1314
1315 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1316 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001317 /*
1318 * set the admin queue depth to the minimum size
1319 * specified by the Fabrics standard.
1320 */
1321 if (priv.qid == 0) {
1322 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1323 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1324 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001325 /*
1326 * current interpretation of the fabrics spec
1327 * is at minimum you make hrqsize sqsize+1, or a
1328 * 1's based representation of sqsize.
1329 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001330 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001331 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001332 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001333
1334 ret = rdma_connect(queue->cm_id, &param);
1335 if (ret) {
1336 dev_err(ctrl->ctrl.device,
1337 "rdma_connect failed (%d).\n", ret);
1338 goto out_destroy_queue_ib;
1339 }
1340
1341 return 0;
1342
1343out_destroy_queue_ib:
1344 nvme_rdma_destroy_queue_ib(queue);
1345 return ret;
1346}
1347
Christoph Hellwig71102302016-07-06 21:55:52 +09001348static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1349 struct rdma_cm_event *ev)
1350{
1351 struct nvme_rdma_queue *queue = cm_id->context;
1352 int cm_error = 0;
1353
1354 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1355 rdma_event_msg(ev->event), ev->event,
1356 ev->status, cm_id);
1357
1358 switch (ev->event) {
1359 case RDMA_CM_EVENT_ADDR_RESOLVED:
1360 cm_error = nvme_rdma_addr_resolved(queue);
1361 break;
1362 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1363 cm_error = nvme_rdma_route_resolved(queue);
1364 break;
1365 case RDMA_CM_EVENT_ESTABLISHED:
1366 queue->cm_error = nvme_rdma_conn_established(queue);
1367 /* complete cm_done regardless of success/failure */
1368 complete(&queue->cm_done);
1369 return 0;
1370 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001371 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001372 cm_error = nvme_rdma_conn_rejected(queue, ev);
1373 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001374 case RDMA_CM_EVENT_ROUTE_ERROR:
1375 case RDMA_CM_EVENT_CONNECT_ERROR:
1376 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001377 nvme_rdma_destroy_queue_ib(queue);
1378 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001379 dev_dbg(queue->ctrl->ctrl.device,
1380 "CM error event %d\n", ev->event);
1381 cm_error = -ECONNRESET;
1382 break;
1383 case RDMA_CM_EVENT_DISCONNECTED:
1384 case RDMA_CM_EVENT_ADDR_CHANGE:
1385 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1386 dev_dbg(queue->ctrl->ctrl.device,
1387 "disconnect received - connection closed\n");
1388 nvme_rdma_error_recovery(queue->ctrl);
1389 break;
1390 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001391 /* device removal is handled via the ib_client API */
1392 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001393 default:
1394 dev_err(queue->ctrl->ctrl.device,
1395 "Unexpected RDMA CM event (%d)\n", ev->event);
1396 nvme_rdma_error_recovery(queue->ctrl);
1397 break;
1398 }
1399
1400 if (cm_error) {
1401 queue->cm_error = cm_error;
1402 complete(&queue->cm_done);
1403 }
1404
1405 return 0;
1406}
1407
1408static enum blk_eh_timer_return
1409nvme_rdma_timeout(struct request *rq, bool reserved)
1410{
1411 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1412
1413 /* queue error recovery */
1414 nvme_rdma_error_recovery(req->queue->ctrl);
1415
1416 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001417 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001418
1419 return BLK_EH_HANDLED;
1420}
1421
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001422/*
1423 * We cannot accept any other command until the Connect command has completed.
1424 */
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001425static inline blk_status_t
1426nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001427{
1428 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001429 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001430
Christoph Hellwig57292b52017-01-31 16:57:29 +01001431 if (!blk_rq_is_passthrough(rq) ||
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001432 cmd->common.opcode != nvme_fabrics_command ||
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001433 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1434 /*
1435 * reconnecting state means transport disruption, which
1436 * can take a long time and even might fail permanently,
1437 * so we can't let incoming I/O be requeued forever.
1438 * fail it fast to allow upper layers a chance to
1439 * failover.
1440 */
1441 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001442 return BLK_STS_IOERR;
1443 return BLK_STS_RESOURCE; /* try again later */
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001444 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001445 }
1446
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001447 return 0;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001448}
1449
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001450static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001451 const struct blk_mq_queue_data *bd)
1452{
1453 struct nvme_ns *ns = hctx->queue->queuedata;
1454 struct nvme_rdma_queue *queue = hctx->driver_data;
1455 struct request *rq = bd->rq;
1456 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1457 struct nvme_rdma_qe *sqe = &req->sqe;
1458 struct nvme_command *c = sqe->data;
1459 bool flush = false;
1460 struct ib_device *dev;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001461 blk_status_t ret;
1462 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001463
1464 WARN_ON_ONCE(rq->tag < 0);
1465
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001466 ret = nvme_rdma_queue_is_ready(queue, rq);
1467 if (unlikely(ret))
Christoph Hellwiga104c9f2017-06-12 18:26:06 +02001468 return ret;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001469
Christoph Hellwig71102302016-07-06 21:55:52 +09001470 dev = queue->device->dev;
1471 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1472 sizeof(struct nvme_command), DMA_TO_DEVICE);
1473
1474 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001475 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001476 return ret;
1477
Christoph Hellwig71102302016-07-06 21:55:52 +09001478 blk_mq_start_request(rq);
1479
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001480 err = nvme_rdma_map_data(queue, rq, c);
1481 if (err < 0) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001482 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001483 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001484 nvme_cleanup_cmd(rq);
1485 goto err;
1486 }
1487
1488 ib_dma_sync_single_for_device(dev, sqe->dma,
1489 sizeof(struct nvme_command), DMA_TO_DEVICE);
1490
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001491 if (req_op(rq) == REQ_OP_FLUSH)
Christoph Hellwig71102302016-07-06 21:55:52 +09001492 flush = true;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001493 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001494 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001495 if (err) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001496 nvme_rdma_unmap_data(queue, rq);
1497 goto err;
1498 }
1499
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001500 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001501err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001502 if (err == -ENOMEM || err == -EAGAIN)
1503 return BLK_STS_RESOURCE;
1504 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001505}
1506
1507static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1508{
1509 struct nvme_rdma_queue *queue = hctx->driver_data;
1510 struct ib_cq *cq = queue->ib_cq;
1511 struct ib_wc wc;
1512 int found = 0;
1513
Christoph Hellwig71102302016-07-06 21:55:52 +09001514 while (ib_poll_cq(cq, 1, &wc) > 0) {
1515 struct ib_cqe *cqe = wc.wr_cqe;
1516
1517 if (cqe) {
1518 if (cqe->done == nvme_rdma_recv_done)
1519 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1520 else
1521 cqe->done(cq, &wc);
1522 }
1523 }
1524
1525 return found;
1526}
1527
1528static void nvme_rdma_complete_rq(struct request *rq)
1529{
1530 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001531
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001532 nvme_rdma_unmap_data(req->queue, rq);
1533 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001534}
1535
Eric Biggersf363b082017-03-30 13:39:16 -07001536static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001537 .queue_rq = nvme_rdma_queue_rq,
1538 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001539 .init_request = nvme_rdma_init_request,
1540 .exit_request = nvme_rdma_exit_request,
1541 .reinit_request = nvme_rdma_reinit_request,
1542 .init_hctx = nvme_rdma_init_hctx,
1543 .poll = nvme_rdma_poll,
1544 .timeout = nvme_rdma_timeout,
1545};
1546
Eric Biggersf363b082017-03-30 13:39:16 -07001547static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001548 .queue_rq = nvme_rdma_queue_rq,
1549 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001550 .init_request = nvme_rdma_init_admin_request,
1551 .exit_request = nvme_rdma_exit_admin_request,
1552 .reinit_request = nvme_rdma_reinit_request,
1553 .init_hctx = nvme_rdma_init_admin_hctx,
1554 .timeout = nvme_rdma_timeout,
1555};
1556
1557static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1558{
1559 int error;
1560
1561 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1562 if (error)
1563 return error;
1564
1565 ctrl->device = ctrl->queues[0].device;
1566
1567 /*
1568 * We need a reference on the device as long as the tag_set is alive,
1569 * as the MRs in the request structures need a valid ib_device.
1570 */
1571 error = -EINVAL;
1572 if (!nvme_rdma_dev_get(ctrl->device))
1573 goto out_free_queue;
1574
1575 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1576 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1577
1578 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1579 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1580 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1581 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1582 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1583 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1584 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1585 ctrl->admin_tag_set.driver_data = ctrl;
1586 ctrl->admin_tag_set.nr_hw_queues = 1;
1587 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1588
1589 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1590 if (error)
1591 goto out_put_dev;
1592
1593 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1594 if (IS_ERR(ctrl->ctrl.admin_q)) {
1595 error = PTR_ERR(ctrl->ctrl.admin_q);
1596 goto out_free_tagset;
1597 }
1598
1599 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1600 if (error)
1601 goto out_cleanup_queue;
1602
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001603 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1604
Christoph Hellwig71102302016-07-06 21:55:52 +09001605 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1606 if (error) {
1607 dev_err(ctrl->ctrl.device,
1608 "prop_get NVME_REG_CAP failed\n");
1609 goto out_cleanup_queue;
1610 }
1611
1612 ctrl->ctrl.sqsize =
Sagi Grimberg1af76dd2017-04-06 09:15:51 +03001613 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
Christoph Hellwig71102302016-07-06 21:55:52 +09001614
1615 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1616 if (error)
1617 goto out_cleanup_queue;
1618
1619 ctrl->ctrl.max_hw_sectors =
1620 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1621
1622 error = nvme_init_identify(&ctrl->ctrl);
1623 if (error)
1624 goto out_cleanup_queue;
1625
1626 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1627 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1628 DMA_TO_DEVICE);
1629 if (error)
1630 goto out_cleanup_queue;
1631
1632 nvme_start_keep_alive(&ctrl->ctrl);
1633
1634 return 0;
1635
1636out_cleanup_queue:
1637 blk_cleanup_queue(ctrl->ctrl.admin_q);
1638out_free_tagset:
1639 /* disconnect and drain the queue before freeing the tagset */
1640 nvme_rdma_stop_queue(&ctrl->queues[0]);
1641 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1642out_put_dev:
1643 nvme_rdma_dev_put(ctrl->device);
1644out_free_queue:
1645 nvme_rdma_free_queue(&ctrl->queues[0]);
1646 return error;
1647}
1648
1649static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1650{
1651 nvme_stop_keep_alive(&ctrl->ctrl);
1652 cancel_work_sync(&ctrl->err_work);
1653 cancel_delayed_work_sync(&ctrl->reconnect_work);
1654
1655 if (ctrl->queue_count > 1) {
1656 nvme_stop_queues(&ctrl->ctrl);
1657 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1658 nvme_cancel_request, &ctrl->ctrl);
1659 nvme_rdma_free_io_queues(ctrl);
1660 }
1661
Sagi Grimberg45862eb2016-07-24 09:26:16 +03001662 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
Christoph Hellwig71102302016-07-06 21:55:52 +09001663 nvme_shutdown_ctrl(&ctrl->ctrl);
1664
1665 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1666 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1667 nvme_cancel_request, &ctrl->ctrl);
1668 nvme_rdma_destroy_admin_queue(ctrl);
1669}
1670
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001671static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1672{
1673 nvme_uninit_ctrl(&ctrl->ctrl);
1674 if (shutdown)
1675 nvme_rdma_shutdown_ctrl(ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001676
1677 if (ctrl->ctrl.tagset) {
1678 blk_cleanup_queue(ctrl->ctrl.connect_q);
1679 blk_mq_free_tag_set(&ctrl->tag_set);
1680 nvme_rdma_dev_put(ctrl->device);
1681 }
1682
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001683 nvme_put_ctrl(&ctrl->ctrl);
1684}
1685
Christoph Hellwig71102302016-07-06 21:55:52 +09001686static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1687{
1688 struct nvme_rdma_ctrl *ctrl = container_of(work,
1689 struct nvme_rdma_ctrl, delete_work);
1690
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001691 __nvme_rdma_remove_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001692}
1693
1694static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1695{
1696 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1697 return -EBUSY;
1698
1699 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1700 return -EBUSY;
1701
1702 return 0;
1703}
1704
1705static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1706{
1707 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001708 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001709
Steve Wisecdbecc82016-09-01 09:12:25 -07001710 /*
1711 * Keep a reference until all work is flushed since
1712 * __nvme_rdma_del_ctrl can free the ctrl mem
1713 */
1714 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1715 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001716 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001717 if (!ret)
1718 flush_work(&ctrl->delete_work);
1719 nvme_put_ctrl(&ctrl->ctrl);
1720 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001721}
1722
1723static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1724{
1725 struct nvme_rdma_ctrl *ctrl = container_of(work,
1726 struct nvme_rdma_ctrl, delete_work);
1727
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001728 __nvme_rdma_remove_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001729}
1730
1731static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1732{
1733 struct nvme_rdma_ctrl *ctrl = container_of(work,
1734 struct nvme_rdma_ctrl, reset_work);
1735 int ret;
1736 bool changed;
1737
1738 nvme_rdma_shutdown_ctrl(ctrl);
1739
1740 ret = nvme_rdma_configure_admin_queue(ctrl);
1741 if (ret) {
1742 /* ctrl is already shutdown, just remove the ctrl */
1743 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1744 goto del_dead_ctrl;
1745 }
1746
1747 if (ctrl->queue_count > 1) {
1748 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1749 if (ret)
1750 goto del_dead_ctrl;
1751
1752 ret = nvme_rdma_init_io_queues(ctrl);
1753 if (ret)
1754 goto del_dead_ctrl;
1755
1756 ret = nvme_rdma_connect_io_queues(ctrl);
1757 if (ret)
1758 goto del_dead_ctrl;
1759 }
1760
1761 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1762 WARN_ON_ONCE(!changed);
1763
1764 if (ctrl->queue_count > 1) {
1765 nvme_start_queues(&ctrl->ctrl);
1766 nvme_queue_scan(&ctrl->ctrl);
Sagi Grimberg3ef1b4b2016-08-04 13:46:19 +03001767 nvme_queue_async_events(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001768 }
1769
1770 return;
1771
1772del_dead_ctrl:
1773 /* Deleting this dead controller... */
1774 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1775 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1776}
1777
1778static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1779{
1780 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1781
1782 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1783 return -EBUSY;
1784
1785 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1786 return -EBUSY;
1787
1788 flush_work(&ctrl->reset_work);
1789
1790 return 0;
1791}
1792
1793static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1794 .name = "rdma",
1795 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001796 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001797 .reg_read32 = nvmf_reg_read32,
1798 .reg_read64 = nvmf_reg_read64,
1799 .reg_write32 = nvmf_reg_write32,
1800 .reset_ctrl = nvme_rdma_reset_ctrl,
1801 .free_ctrl = nvme_rdma_free_ctrl,
1802 .submit_async_event = nvme_rdma_submit_async_event,
1803 .delete_ctrl = nvme_rdma_del_ctrl,
1804 .get_subsysnqn = nvmf_get_subsysnqn,
1805 .get_address = nvmf_get_address,
1806};
1807
1808static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1809{
Christoph Hellwig71102302016-07-06 21:55:52 +09001810 int ret;
1811
Christoph Hellwig71102302016-07-06 21:55:52 +09001812 ret = nvme_rdma_init_io_queues(ctrl);
1813 if (ret)
1814 return ret;
1815
1816 /*
1817 * We need a reference on the device as long as the tag_set is alive,
1818 * as the MRs in the request structures need a valid ib_device.
1819 */
1820 ret = -EINVAL;
1821 if (!nvme_rdma_dev_get(ctrl->device))
1822 goto out_free_io_queues;
1823
1824 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1825 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001826 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
Christoph Hellwig71102302016-07-06 21:55:52 +09001827 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1828 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1829 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1830 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1831 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1832 ctrl->tag_set.driver_data = ctrl;
1833 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1834 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1835
1836 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1837 if (ret)
1838 goto out_put_dev;
1839 ctrl->ctrl.tagset = &ctrl->tag_set;
1840
1841 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1842 if (IS_ERR(ctrl->ctrl.connect_q)) {
1843 ret = PTR_ERR(ctrl->ctrl.connect_q);
1844 goto out_free_tag_set;
1845 }
1846
1847 ret = nvme_rdma_connect_io_queues(ctrl);
1848 if (ret)
1849 goto out_cleanup_connect_q;
1850
1851 return 0;
1852
1853out_cleanup_connect_q:
1854 blk_cleanup_queue(ctrl->ctrl.connect_q);
1855out_free_tag_set:
1856 blk_mq_free_tag_set(&ctrl->tag_set);
1857out_put_dev:
1858 nvme_rdma_dev_put(ctrl->device);
1859out_free_io_queues:
1860 nvme_rdma_free_io_queues(ctrl);
1861 return ret;
1862}
1863
Christoph Hellwig71102302016-07-06 21:55:52 +09001864static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1865 struct nvmf_ctrl_options *opts)
1866{
1867 struct nvme_rdma_ctrl *ctrl;
1868 int ret;
1869 bool changed;
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001870 char *port;
Christoph Hellwig71102302016-07-06 21:55:52 +09001871
1872 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1873 if (!ctrl)
1874 return ERR_PTR(-ENOMEM);
1875 ctrl->ctrl.opts = opts;
1876 INIT_LIST_HEAD(&ctrl->list);
1877
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001878 if (opts->mask & NVMF_OPT_TRSVCID)
1879 port = opts->trsvcid;
1880 else
1881 port = __stringify(NVME_RDMA_IP_PORT);
1882
1883 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1884 opts->traddr, port, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001885 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001886 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
Christoph Hellwig71102302016-07-06 21:55:52 +09001887 goto out_free_ctrl;
1888 }
1889
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001890 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001891 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1892 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001893 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001894 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001895 opts->host_traddr);
1896 goto out_free_ctrl;
1897 }
1898 }
1899
Christoph Hellwig71102302016-07-06 21:55:52 +09001900 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1901 0 /* no quirks, we're perfect! */);
1902 if (ret)
1903 goto out_free_ctrl;
1904
Christoph Hellwig71102302016-07-06 21:55:52 +09001905 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1906 nvme_rdma_reconnect_ctrl_work);
1907 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1908 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1909 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001910
1911 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001912 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001913 ctrl->ctrl.kato = opts->kato;
1914
1915 ret = -ENOMEM;
1916 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1917 GFP_KERNEL);
1918 if (!ctrl->queues)
1919 goto out_uninit_ctrl;
1920
1921 ret = nvme_rdma_configure_admin_queue(ctrl);
1922 if (ret)
1923 goto out_kfree_queues;
1924
1925 /* sanity check icdoff */
1926 if (ctrl->ctrl.icdoff) {
1927 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1928 goto out_remove_admin_queue;
1929 }
1930
1931 /* sanity check keyed sgls */
1932 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1933 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1934 goto out_remove_admin_queue;
1935 }
1936
1937 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1938 /* warn if maxcmd is lower than queue_size */
1939 dev_warn(ctrl->ctrl.device,
1940 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1941 opts->queue_size, ctrl->ctrl.maxcmd);
1942 opts->queue_size = ctrl->ctrl.maxcmd;
1943 }
1944
Samuel Jones76c08bf2016-10-25 09:22:34 +02001945 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1946 /* warn if sqsize is lower than queue_size */
1947 dev_warn(ctrl->ctrl.device,
1948 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1949 opts->queue_size, ctrl->ctrl.sqsize + 1);
1950 opts->queue_size = ctrl->ctrl.sqsize + 1;
1951 }
1952
Christoph Hellwig71102302016-07-06 21:55:52 +09001953 if (opts->nr_io_queues) {
1954 ret = nvme_rdma_create_io_queues(ctrl);
1955 if (ret)
1956 goto out_remove_admin_queue;
1957 }
1958
1959 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1960 WARN_ON_ONCE(!changed);
1961
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001962 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001963 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1964
1965 kref_get(&ctrl->ctrl.kref);
1966
1967 mutex_lock(&nvme_rdma_ctrl_mutex);
1968 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1969 mutex_unlock(&nvme_rdma_ctrl_mutex);
1970
1971 if (opts->nr_io_queues) {
1972 nvme_queue_scan(&ctrl->ctrl);
1973 nvme_queue_async_events(&ctrl->ctrl);
1974 }
1975
1976 return &ctrl->ctrl;
1977
1978out_remove_admin_queue:
1979 nvme_stop_keep_alive(&ctrl->ctrl);
1980 nvme_rdma_destroy_admin_queue(ctrl);
1981out_kfree_queues:
1982 kfree(ctrl->queues);
1983out_uninit_ctrl:
1984 nvme_uninit_ctrl(&ctrl->ctrl);
1985 nvme_put_ctrl(&ctrl->ctrl);
1986 if (ret > 0)
1987 ret = -EIO;
1988 return ERR_PTR(ret);
1989out_free_ctrl:
1990 kfree(ctrl);
1991 return ERR_PTR(ret);
1992}
1993
1994static struct nvmf_transport_ops nvme_rdma_transport = {
1995 .name = "rdma",
1996 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001997 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001998 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001999 .create_ctrl = nvme_rdma_create_ctrl,
2000};
2001
Steve Wisee87a9112016-09-02 09:01:54 -07002002static void nvme_rdma_add_one(struct ib_device *ib_device)
2003{
2004}
2005
2006static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2007{
2008 struct nvme_rdma_ctrl *ctrl;
2009
2010 /* Delete all controllers using this device */
2011 mutex_lock(&nvme_rdma_ctrl_mutex);
2012 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2013 if (ctrl->device->dev != ib_device)
2014 continue;
2015 dev_info(ctrl->ctrl.device,
2016 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2017 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2018 __nvme_rdma_del_ctrl(ctrl);
2019 }
2020 mutex_unlock(&nvme_rdma_ctrl_mutex);
2021
2022 flush_workqueue(nvme_rdma_wq);
2023}
2024
2025static struct ib_client nvme_rdma_ib_client = {
2026 .name = "nvme_rdma",
2027 .add = nvme_rdma_add_one,
2028 .remove = nvme_rdma_remove_one
2029};
2030
Christoph Hellwig71102302016-07-06 21:55:52 +09002031static int __init nvme_rdma_init_module(void)
2032{
Steve Wisee87a9112016-09-02 09:01:54 -07002033 int ret;
2034
Christoph Hellwig71102302016-07-06 21:55:52 +09002035 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2036 if (!nvme_rdma_wq)
2037 return -ENOMEM;
2038
Steve Wisee87a9112016-09-02 09:01:54 -07002039 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002040 if (ret)
2041 goto err_destroy_wq;
Steve Wisee87a9112016-09-02 09:01:54 -07002042
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002043 ret = nvmf_register_transport(&nvme_rdma_transport);
2044 if (ret)
2045 goto err_unreg_client;
2046
2047 return 0;
2048
2049err_unreg_client:
2050 ib_unregister_client(&nvme_rdma_ib_client);
2051err_destroy_wq:
2052 destroy_workqueue(nvme_rdma_wq);
2053 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002054}
2055
2056static void __exit nvme_rdma_cleanup_module(void)
2057{
Christoph Hellwig71102302016-07-06 21:55:52 +09002058 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002059 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002060 destroy_workqueue(nvme_rdma_wq);
2061}
2062
2063module_init(nvme_rdma_init_module);
2064module_exit(nvme_rdma_cleanup_module);
2065
2066MODULE_LICENSE("GPL v2");