blob: 557f29b1f1bb23e4e5cded156db21b4f981b585a [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
37#define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
38
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
Steve Wise7f039532016-10-26 12:36:47 -070045static const char *const nvme_rdma_cm_status_strs[] = {
46 [NVME_RDMA_CM_INVALID_LEN] = "invalid length",
47 [NVME_RDMA_CM_INVALID_RECFMT] = "invalid record format",
48 [NVME_RDMA_CM_INVALID_QID] = "invalid queue ID",
49 [NVME_RDMA_CM_INVALID_HSQSIZE] = "invalid host SQ size",
50 [NVME_RDMA_CM_INVALID_HRQSIZE] = "invalid host RQ size",
51 [NVME_RDMA_CM_NO_RSC] = "resource not found",
52 [NVME_RDMA_CM_INVALID_IRD] = "invalid IRD",
53 [NVME_RDMA_CM_INVALID_ORD] = "Invalid ORD",
54};
55
56static const char *nvme_rdma_cm_msg(enum nvme_rdma_cm_status status)
57{
58 size_t index = status;
59
60 if (index < ARRAY_SIZE(nvme_rdma_cm_status_strs) &&
61 nvme_rdma_cm_status_strs[index])
62 return nvme_rdma_cm_status_strs[index];
63 else
64 return "unrecognized reason";
65};
66
Christoph Hellwig71102302016-07-06 21:55:52 +090067/*
68 * We handle AEN commands ourselves and don't even let the
69 * block layer know about them.
70 */
71#define NVME_RDMA_NR_AEN_COMMANDS 1
72#define NVME_RDMA_AQ_BLKMQ_DEPTH \
73 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
74
75struct nvme_rdma_device {
76 struct ib_device *dev;
77 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090078 struct kref ref;
79 struct list_head entry;
80};
81
82struct nvme_rdma_qe {
83 struct ib_cqe cqe;
84 void *data;
85 u64 dma;
86};
87
88struct nvme_rdma_queue;
89struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080090 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090091 struct ib_mr *mr;
92 struct nvme_rdma_qe sqe;
93 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
94 u32 num_sge;
95 int nents;
96 bool inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +090097 struct ib_reg_wr reg_wr;
98 struct ib_cqe reg_cqe;
99 struct nvme_rdma_queue *queue;
100 struct sg_table sg_table;
101 struct scatterlist first_sgl[];
102};
103
104enum nvme_rdma_queue_flags {
105 NVME_RDMA_Q_CONNECTED = (1 << 0),
Steve Wisef361e5a2016-09-02 09:01:27 -0700106 NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1),
Sagi Grimberge89ca582016-09-02 09:01:54 -0700107 NVME_RDMA_Q_DELETING = (1 << 2),
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600108 NVME_RDMA_Q_LIVE = (1 << 3),
Christoph Hellwig71102302016-07-06 21:55:52 +0900109};
110
111struct nvme_rdma_queue {
112 struct nvme_rdma_qe *rsp_ring;
113 u8 sig_count;
114 int queue_size;
115 size_t cmnd_capsule_len;
116 struct nvme_rdma_ctrl *ctrl;
117 struct nvme_rdma_device *device;
118 struct ib_cq *ib_cq;
119 struct ib_qp *qp;
120
121 unsigned long flags;
122 struct rdma_cm_id *cm_id;
123 int cm_error;
124 struct completion cm_done;
125};
126
127struct nvme_rdma_ctrl {
128 /* read and written in the hot path */
129 spinlock_t lock;
130
131 /* read only in the hot path */
132 struct nvme_rdma_queue *queues;
133 u32 queue_count;
134
135 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900136 struct blk_mq_tag_set tag_set;
137 struct work_struct delete_work;
138 struct work_struct reset_work;
139 struct work_struct err_work;
140
141 struct nvme_rdma_qe async_event_sqe;
142
143 int reconnect_delay;
144 struct delayed_work reconnect_work;
145
146 struct list_head list;
147
148 struct blk_mq_tag_set admin_tag_set;
149 struct nvme_rdma_device *device;
150
151 u64 cap;
152 u32 max_fr_pages;
153
154 union {
155 struct sockaddr addr;
156 struct sockaddr_in addr_in;
157 };
158
159 struct nvme_ctrl ctrl;
160};
161
162static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
163{
164 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
165}
166
167static LIST_HEAD(device_list);
168static DEFINE_MUTEX(device_list_mutex);
169
170static LIST_HEAD(nvme_rdma_ctrl_list);
171static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
172
173static struct workqueue_struct *nvme_rdma_wq;
174
175/*
176 * Disabling this option makes small I/O goes faster, but is fundamentally
177 * unsafe. With it turned off we will have to register a global rkey that
178 * allows read and write access to all physical memory.
179 */
180static bool register_always = true;
181module_param(register_always, bool, 0444);
182MODULE_PARM_DESC(register_always,
183 "Use memory registration even for contiguous memory regions");
184
185static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
186 struct rdma_cm_event *event);
187static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900188
189/* XXX: really should move to a generic header sooner or later.. */
190static inline void put_unaligned_le24(u32 val, u8 *p)
191{
192 *p++ = val;
193 *p++ = val >> 8;
194 *p++ = val >> 16;
195}
196
197static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
198{
199 return queue - queue->ctrl->queues;
200}
201
202static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
203{
204 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
205}
206
207static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
208 size_t capsule_size, enum dma_data_direction dir)
209{
210 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
211 kfree(qe->data);
212}
213
214static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
215 size_t capsule_size, enum dma_data_direction dir)
216{
217 qe->data = kzalloc(capsule_size, GFP_KERNEL);
218 if (!qe->data)
219 return -ENOMEM;
220
221 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
222 if (ib_dma_mapping_error(ibdev, qe->dma)) {
223 kfree(qe->data);
224 return -ENOMEM;
225 }
226
227 return 0;
228}
229
230static void nvme_rdma_free_ring(struct ib_device *ibdev,
231 struct nvme_rdma_qe *ring, size_t ib_queue_size,
232 size_t capsule_size, enum dma_data_direction dir)
233{
234 int i;
235
236 for (i = 0; i < ib_queue_size; i++)
237 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
238 kfree(ring);
239}
240
241static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
242 size_t ib_queue_size, size_t capsule_size,
243 enum dma_data_direction dir)
244{
245 struct nvme_rdma_qe *ring;
246 int i;
247
248 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
249 if (!ring)
250 return NULL;
251
252 for (i = 0; i < ib_queue_size; i++) {
253 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
254 goto out_free_ring;
255 }
256
257 return ring;
258
259out_free_ring:
260 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
261 return NULL;
262}
263
264static void nvme_rdma_qp_event(struct ib_event *event, void *context)
265{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200266 pr_debug("QP event %s (%d)\n",
267 ib_event_msg(event->event), event->event);
268
Christoph Hellwig71102302016-07-06 21:55:52 +0900269}
270
271static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
272{
273 wait_for_completion_interruptible_timeout(&queue->cm_done,
274 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
275 return queue->cm_error;
276}
277
278static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
279{
280 struct nvme_rdma_device *dev = queue->device;
281 struct ib_qp_init_attr init_attr;
282 int ret;
283
284 memset(&init_attr, 0, sizeof(init_attr));
285 init_attr.event_handler = nvme_rdma_qp_event;
286 /* +1 for drain */
287 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
288 /* +1 for drain */
289 init_attr.cap.max_recv_wr = queue->queue_size + 1;
290 init_attr.cap.max_recv_sge = 1;
291 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
292 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
293 init_attr.qp_type = IB_QPT_RC;
294 init_attr.send_cq = queue->ib_cq;
295 init_attr.recv_cq = queue->ib_cq;
296
297 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
298
299 queue->qp = queue->cm_id->qp;
300 return ret;
301}
302
303static int nvme_rdma_reinit_request(void *data, struct request *rq)
304{
305 struct nvme_rdma_ctrl *ctrl = data;
306 struct nvme_rdma_device *dev = ctrl->device;
307 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
308 int ret = 0;
309
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300310 if (!req->mr->need_inval)
Christoph Hellwig71102302016-07-06 21:55:52 +0900311 goto out;
312
313 ib_dereg_mr(req->mr);
314
315 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
316 ctrl->max_fr_pages);
317 if (IS_ERR(req->mr)) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900318 ret = PTR_ERR(req->mr);
Wei Yongjun458a9632016-07-12 11:06:17 +0000319 req->mr = NULL;
Colin Ian King1bda18d2016-09-05 16:24:38 +0100320 goto out;
Christoph Hellwig71102302016-07-06 21:55:52 +0900321 }
322
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300323 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900324
325out:
326 return ret;
327}
328
329static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
330 struct request *rq, unsigned int queue_idx)
331{
332 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
333 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
334 struct nvme_rdma_device *dev = queue->device;
335
336 if (req->mr)
337 ib_dereg_mr(req->mr);
338
339 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
340 DMA_TO_DEVICE);
341}
342
343static void nvme_rdma_exit_request(void *data, struct request *rq,
344 unsigned int hctx_idx, unsigned int rq_idx)
345{
346 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
347}
348
349static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
350 unsigned int hctx_idx, unsigned int rq_idx)
351{
352 return __nvme_rdma_exit_request(data, rq, 0);
353}
354
355static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
356 struct request *rq, unsigned int queue_idx)
357{
358 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
359 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
360 struct nvme_rdma_device *dev = queue->device;
361 struct ib_device *ibdev = dev->dev;
362 int ret;
363
364 BUG_ON(queue_idx >= ctrl->queue_count);
365
366 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
367 DMA_TO_DEVICE);
368 if (ret)
369 return ret;
370
371 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
372 ctrl->max_fr_pages);
373 if (IS_ERR(req->mr)) {
374 ret = PTR_ERR(req->mr);
375 goto out_free_qe;
376 }
377
378 req->queue = queue;
379
380 return 0;
381
382out_free_qe:
383 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
384 DMA_TO_DEVICE);
385 return -ENOMEM;
386}
387
388static int nvme_rdma_init_request(void *data, struct request *rq,
389 unsigned int hctx_idx, unsigned int rq_idx,
390 unsigned int numa_node)
391{
392 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
393}
394
395static int nvme_rdma_init_admin_request(void *data, struct request *rq,
396 unsigned int hctx_idx, unsigned int rq_idx,
397 unsigned int numa_node)
398{
399 return __nvme_rdma_init_request(data, rq, 0);
400}
401
402static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
403 unsigned int hctx_idx)
404{
405 struct nvme_rdma_ctrl *ctrl = data;
406 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
407
408 BUG_ON(hctx_idx >= ctrl->queue_count);
409
410 hctx->driver_data = queue;
411 return 0;
412}
413
414static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
415 unsigned int hctx_idx)
416{
417 struct nvme_rdma_ctrl *ctrl = data;
418 struct nvme_rdma_queue *queue = &ctrl->queues[0];
419
420 BUG_ON(hctx_idx != 0);
421
422 hctx->driver_data = queue;
423 return 0;
424}
425
426static void nvme_rdma_free_dev(struct kref *ref)
427{
428 struct nvme_rdma_device *ndev =
429 container_of(ref, struct nvme_rdma_device, ref);
430
431 mutex_lock(&device_list_mutex);
432 list_del(&ndev->entry);
433 mutex_unlock(&device_list_mutex);
434
Christoph Hellwig71102302016-07-06 21:55:52 +0900435 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900436 kfree(ndev);
437}
438
439static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
440{
441 kref_put(&dev->ref, nvme_rdma_free_dev);
442}
443
444static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
445{
446 return kref_get_unless_zero(&dev->ref);
447}
448
449static struct nvme_rdma_device *
450nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
451{
452 struct nvme_rdma_device *ndev;
453
454 mutex_lock(&device_list_mutex);
455 list_for_each_entry(ndev, &device_list, entry) {
456 if (ndev->dev->node_guid == cm_id->device->node_guid &&
457 nvme_rdma_dev_get(ndev))
458 goto out_unlock;
459 }
460
461 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
462 if (!ndev)
463 goto out_err;
464
465 ndev->dev = cm_id->device;
466 kref_init(&ndev->ref);
467
Christoph Hellwig11975e02016-09-05 12:56:20 +0200468 ndev->pd = ib_alloc_pd(ndev->dev,
469 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900470 if (IS_ERR(ndev->pd))
471 goto out_free_dev;
472
Christoph Hellwig71102302016-07-06 21:55:52 +0900473 if (!(ndev->dev->attrs.device_cap_flags &
474 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
475 dev_err(&ndev->dev->dev,
476 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200477 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900478 }
479
480 list_add(&ndev->entry, &device_list);
481out_unlock:
482 mutex_unlock(&device_list_mutex);
483 return ndev;
484
Christoph Hellwig71102302016-07-06 21:55:52 +0900485out_free_pd:
486 ib_dealloc_pd(ndev->pd);
487out_free_dev:
488 kfree(ndev);
489out_err:
490 mutex_unlock(&device_list_mutex);
491 return NULL;
492}
493
494static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
495{
Steve Wisef361e5a2016-09-02 09:01:27 -0700496 struct nvme_rdma_device *dev;
497 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900498
Steve Wisef361e5a2016-09-02 09:01:27 -0700499 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
500 return;
501
502 dev = queue->device;
503 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900504 rdma_destroy_qp(queue->cm_id);
505 ib_free_cq(queue->ib_cq);
506
507 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
508 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
509
510 nvme_rdma_dev_put(dev);
511}
512
513static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
514 struct nvme_rdma_device *dev)
515{
516 struct ib_device *ibdev = dev->dev;
517 const int send_wr_factor = 3; /* MR, SEND, INV */
518 const int cq_factor = send_wr_factor + 1; /* + RECV */
519 int comp_vector, idx = nvme_rdma_queue_idx(queue);
520
521 int ret;
522
523 queue->device = dev;
524
525 /*
526 * The admin queue is barely used once the controller is live, so don't
527 * bother to spread it out.
528 */
529 if (idx == 0)
530 comp_vector = 0;
531 else
532 comp_vector = idx % ibdev->num_comp_vectors;
533
534
535 /* +1 for ib_stop_cq */
536 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
537 cq_factor * queue->queue_size + 1, comp_vector,
538 IB_POLL_SOFTIRQ);
539 if (IS_ERR(queue->ib_cq)) {
540 ret = PTR_ERR(queue->ib_cq);
541 goto out;
542 }
543
544 ret = nvme_rdma_create_qp(queue, send_wr_factor);
545 if (ret)
546 goto out_destroy_ib_cq;
547
548 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
549 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
550 if (!queue->rsp_ring) {
551 ret = -ENOMEM;
552 goto out_destroy_qp;
553 }
Steve Wisef361e5a2016-09-02 09:01:27 -0700554 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900555
556 return 0;
557
558out_destroy_qp:
559 ib_destroy_qp(queue->qp);
560out_destroy_ib_cq:
561 ib_free_cq(queue->ib_cq);
562out:
563 return ret;
564}
565
566static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
567 int idx, size_t queue_size)
568{
569 struct nvme_rdma_queue *queue;
570 int ret;
571
572 queue = &ctrl->queues[idx];
573 queue->ctrl = ctrl;
574 init_completion(&queue->cm_done);
575
576 if (idx > 0)
577 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
578 else
579 queue->cmnd_capsule_len = sizeof(struct nvme_command);
580
581 queue->queue_size = queue_size;
582
583 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
584 RDMA_PS_TCP, IB_QPT_RC);
585 if (IS_ERR(queue->cm_id)) {
586 dev_info(ctrl->ctrl.device,
587 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
588 return PTR_ERR(queue->cm_id);
589 }
590
591 queue->cm_error = -ETIMEDOUT;
592 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
593 NVME_RDMA_CONNECT_TIMEOUT_MS);
594 if (ret) {
595 dev_info(ctrl->ctrl.device,
596 "rdma_resolve_addr failed (%d).\n", ret);
597 goto out_destroy_cm_id;
598 }
599
600 ret = nvme_rdma_wait_for_cm(queue);
601 if (ret) {
602 dev_info(ctrl->ctrl.device,
603 "rdma_resolve_addr wait failed (%d).\n", ret);
604 goto out_destroy_cm_id;
605 }
606
Sagi Grimberg3b4ac782016-09-22 19:58:17 -0600607 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900608 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
609
610 return 0;
611
612out_destroy_cm_id:
Steve Wisef361e5a2016-09-02 09:01:27 -0700613 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900614 rdma_destroy_id(queue->cm_id);
615 return ret;
616}
617
618static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
619{
620 rdma_disconnect(queue->cm_id);
621 ib_drain_qp(queue->qp);
622}
623
624static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
625{
626 nvme_rdma_destroy_queue_ib(queue);
627 rdma_destroy_id(queue->cm_id);
628}
629
630static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
631{
Sagi Grimberge89ca582016-09-02 09:01:54 -0700632 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900633 return;
634 nvme_rdma_stop_queue(queue);
635 nvme_rdma_free_queue(queue);
636}
637
638static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
639{
640 int i;
641
642 for (i = 1; i < ctrl->queue_count; i++)
643 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
644}
645
646static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
647{
648 int i, ret = 0;
649
650 for (i = 1; i < ctrl->queue_count; i++) {
651 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
Steve Wisec8dbc372016-11-08 09:16:02 -0800652 if (ret) {
653 dev_info(ctrl->ctrl.device,
654 "failed to connect i/o queue: %d\n", ret);
655 goto out_free_queues;
656 }
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600657 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900658 }
659
Steve Wisec8dbc372016-11-08 09:16:02 -0800660 return 0;
661
662out_free_queues:
663 nvme_rdma_free_io_queues(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +0900664 return ret;
665}
666
667static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
668{
669 int i, ret;
670
671 for (i = 1; i < ctrl->queue_count; i++) {
Jay Freyenseec5af8652016-08-17 15:00:27 -0700672 ret = nvme_rdma_init_queue(ctrl, i,
673 ctrl->ctrl.opts->queue_size);
Christoph Hellwig71102302016-07-06 21:55:52 +0900674 if (ret) {
675 dev_info(ctrl->ctrl.device,
676 "failed to initialize i/o queue: %d\n", ret);
677 goto out_free_queues;
678 }
679 }
680
681 return 0;
682
683out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700684 for (i--; i >= 1; i--)
Christoph Hellwig71102302016-07-06 21:55:52 +0900685 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
686
687 return ret;
688}
689
690static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
691{
692 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
693 sizeof(struct nvme_command), DMA_TO_DEVICE);
694 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
695 blk_cleanup_queue(ctrl->ctrl.admin_q);
696 blk_mq_free_tag_set(&ctrl->admin_tag_set);
697 nvme_rdma_dev_put(ctrl->device);
698}
699
700static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
701{
702 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
703
704 if (list_empty(&ctrl->list))
705 goto free_ctrl;
706
707 mutex_lock(&nvme_rdma_ctrl_mutex);
708 list_del(&ctrl->list);
709 mutex_unlock(&nvme_rdma_ctrl_mutex);
710
Christoph Hellwig71102302016-07-06 21:55:52 +0900711 kfree(ctrl->queues);
712 nvmf_free_options(nctrl->opts);
713free_ctrl:
714 kfree(ctrl);
715}
716
717static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
718{
719 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
720 struct nvme_rdma_ctrl, reconnect_work);
721 bool changed;
722 int ret;
723
724 if (ctrl->queue_count > 1) {
725 nvme_rdma_free_io_queues(ctrl);
726
727 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
728 if (ret)
729 goto requeue;
730 }
731
732 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
733
734 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
735 if (ret)
736 goto requeue;
737
738 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
739 if (ret)
740 goto requeue;
741
742 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
743
744 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
745 if (ret)
746 goto stop_admin_q;
747
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600748 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
749
Christoph Hellwig71102302016-07-06 21:55:52 +0900750 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
751 if (ret)
752 goto stop_admin_q;
753
754 nvme_start_keep_alive(&ctrl->ctrl);
755
756 if (ctrl->queue_count > 1) {
757 ret = nvme_rdma_init_io_queues(ctrl);
758 if (ret)
759 goto stop_admin_q;
760
761 ret = nvme_rdma_connect_io_queues(ctrl);
762 if (ret)
763 goto stop_admin_q;
764 }
765
766 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
767 WARN_ON_ONCE(!changed);
768
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300769 if (ctrl->queue_count > 1) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900770 nvme_start_queues(&ctrl->ctrl);
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300771 nvme_queue_scan(&ctrl->ctrl);
Sagi Grimberg3ef1b4b2016-08-04 13:46:19 +0300772 nvme_queue_async_events(&ctrl->ctrl);
Sagi Grimberg5f372eb2016-07-31 18:43:15 +0300773 }
Christoph Hellwig71102302016-07-06 21:55:52 +0900774
775 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
776
777 return;
778
779stop_admin_q:
780 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
781requeue:
782 /* Make sure we are not resetting/deleting */
783 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
784 dev_info(ctrl->ctrl.device,
785 "Failed reconnect attempt, requeueing...\n");
786 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
787 ctrl->reconnect_delay * HZ);
788 }
789}
790
791static void nvme_rdma_error_recovery_work(struct work_struct *work)
792{
793 struct nvme_rdma_ctrl *ctrl = container_of(work,
794 struct nvme_rdma_ctrl, err_work);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700795 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +0900796
797 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberge89ca582016-09-02 09:01:54 -0700798
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600799 for (i = 0; i < ctrl->queue_count; i++) {
Sagi Grimberge89ca582016-09-02 09:01:54 -0700800 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
Christoph Hellwig553cd9e2016-11-02 08:49:18 -0600801 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
802 }
Sagi Grimberge89ca582016-09-02 09:01:54 -0700803
Christoph Hellwig71102302016-07-06 21:55:52 +0900804 if (ctrl->queue_count > 1)
805 nvme_stop_queues(&ctrl->ctrl);
806 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
807
808 /* We must take care of fastfail/requeue all our inflight requests */
809 if (ctrl->queue_count > 1)
810 blk_mq_tagset_busy_iter(&ctrl->tag_set,
811 nvme_cancel_request, &ctrl->ctrl);
812 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
813 nvme_cancel_request, &ctrl->ctrl);
814
815 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
816 ctrl->reconnect_delay);
817
818 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
819 ctrl->reconnect_delay * HZ);
820}
821
822static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
823{
824 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
825 return;
826
827 queue_work(nvme_rdma_wq, &ctrl->err_work);
828}
829
830static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
831 const char *op)
832{
833 struct nvme_rdma_queue *queue = cq->cq_context;
834 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
835
836 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
837 dev_info(ctrl->ctrl.device,
838 "%s for CQE 0x%p failed with status %s (%d)\n",
839 op, wc->wr_cqe,
840 ib_wc_status_msg(wc->status), wc->status);
841 nvme_rdma_error_recovery(ctrl);
842}
843
844static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
845{
846 if (unlikely(wc->status != IB_WC_SUCCESS))
847 nvme_rdma_wr_error(cq, wc, "MEMREG");
848}
849
850static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
851{
852 if (unlikely(wc->status != IB_WC_SUCCESS))
853 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
854}
855
856static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
857 struct nvme_rdma_request *req)
858{
859 struct ib_send_wr *bad_wr;
860 struct ib_send_wr wr = {
861 .opcode = IB_WR_LOCAL_INV,
862 .next = NULL,
863 .num_sge = 0,
864 .send_flags = 0,
865 .ex.invalidate_rkey = req->mr->rkey,
866 };
867
868 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
869 wr.wr_cqe = &req->reg_cqe;
870
871 return ib_post_send(queue->qp, &wr, &bad_wr);
872}
873
874static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
875 struct request *rq)
876{
877 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
878 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
879 struct nvme_rdma_device *dev = queue->device;
880 struct ib_device *ibdev = dev->dev;
881 int res;
882
883 if (!blk_rq_bytes(rq))
884 return;
885
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300886 if (req->mr->need_inval) {
Christoph Hellwig71102302016-07-06 21:55:52 +0900887 res = nvme_rdma_inv_rkey(queue, req);
888 if (res < 0) {
889 dev_err(ctrl->ctrl.device,
890 "Queueing INV WR for rkey %#x failed (%d)\n",
891 req->mr->rkey, res);
892 nvme_rdma_error_recovery(queue->ctrl);
893 }
894 }
895
896 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
897 req->nents, rq_data_dir(rq) ==
898 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
899
900 nvme_cleanup_cmd(rq);
901 sg_free_table_chained(&req->sg_table, true);
902}
903
904static int nvme_rdma_set_sg_null(struct nvme_command *c)
905{
906 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
907
908 sg->addr = 0;
909 put_unaligned_le24(0, sg->length);
910 put_unaligned_le32(0, sg->key);
911 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
912 return 0;
913}
914
915static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
916 struct nvme_rdma_request *req, struct nvme_command *c)
917{
918 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
919
920 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
921 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
922 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
923
924 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
925 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
926 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
927
928 req->inline_data = true;
929 req->num_sge++;
930 return 0;
931}
932
933static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
934 struct nvme_rdma_request *req, struct nvme_command *c)
935{
936 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
937
938 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
939 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +0200940 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +0900941 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
942 return 0;
943}
944
945static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
946 struct nvme_rdma_request *req, struct nvme_command *c,
947 int count)
948{
949 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
950 int nr;
951
952 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
953 if (nr < count) {
954 if (nr < 0)
955 return nr;
956 return -EINVAL;
957 }
958
959 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
960
961 req->reg_cqe.done = nvme_rdma_memreg_done;
962 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
963 req->reg_wr.wr.opcode = IB_WR_REG_MR;
964 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
965 req->reg_wr.wr.num_sge = 0;
966 req->reg_wr.mr = req->mr;
967 req->reg_wr.key = req->mr->rkey;
968 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
969 IB_ACCESS_REMOTE_READ |
970 IB_ACCESS_REMOTE_WRITE;
971
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300972 req->mr->need_inval = true;
Christoph Hellwig71102302016-07-06 21:55:52 +0900973
974 sg->addr = cpu_to_le64(req->mr->iova);
975 put_unaligned_le24(req->mr->length, sg->length);
976 put_unaligned_le32(req->mr->rkey, sg->key);
977 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
978 NVME_SGL_FMT_INVALIDATE;
979
980 return 0;
981}
982
983static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +0100984 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +0900985{
986 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
987 struct nvme_rdma_device *dev = queue->device;
988 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700989 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900990
991 req->num_sge = 1;
992 req->inline_data = false;
Sagi Grimbergf5b7b552016-08-24 12:25:56 +0300993 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +0900994
995 c->common.flags |= NVME_CMD_SGL_METABUF;
996
997 if (!blk_rq_bytes(rq))
998 return nvme_rdma_set_sg_null(c);
999
1000 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001001 ret = sg_alloc_table_chained(&req->sg_table,
1002 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001003 if (ret)
1004 return -ENOMEM;
1005
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001006 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001007
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001008 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001009 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1010 if (unlikely(count <= 0)) {
1011 sg_free_table_chained(&req->sg_table, true);
1012 return -EIO;
1013 }
1014
1015 if (count == 1) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001016 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1017 blk_rq_payload_bytes(rq) <=
1018 nvme_rdma_inline_data_size(queue))
Christoph Hellwig71102302016-07-06 21:55:52 +09001019 return nvme_rdma_map_sg_inline(queue, req, c);
1020
Christoph Hellwig11975e02016-09-05 12:56:20 +02001021 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
Christoph Hellwig71102302016-07-06 21:55:52 +09001022 return nvme_rdma_map_sg_single(queue, req, c);
1023 }
1024
1025 return nvme_rdma_map_sg_fr(queue, req, c, count);
1026}
1027
1028static void nvme_rdma_send_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, "SEND");
1032}
1033
1034static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1035 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1036 struct ib_send_wr *first, bool flush)
1037{
1038 struct ib_send_wr wr, *bad_wr;
1039 int ret;
1040
1041 sge->addr = qe->dma;
1042 sge->length = sizeof(struct nvme_command),
1043 sge->lkey = queue->device->pd->local_dma_lkey;
1044
1045 qe->cqe.done = nvme_rdma_send_done;
1046
1047 wr.next = NULL;
1048 wr.wr_cqe = &qe->cqe;
1049 wr.sg_list = sge;
1050 wr.num_sge = num_sge;
1051 wr.opcode = IB_WR_SEND;
1052 wr.send_flags = 0;
1053
1054 /*
1055 * Unsignalled send completions are another giant desaster in the
1056 * IB Verbs spec: If we don't regularly post signalled sends
1057 * the send queue will fill up and only a QP reset will rescue us.
1058 * Would have been way to obvious to handle this in hardware or
1059 * at least the RDMA stack..
1060 *
1061 * This messy and racy code sniplet is copy and pasted from the iSER
1062 * initiator, and the magic '32' comes from there as well.
1063 *
1064 * Always signal the flushes. The magic request used for the flush
1065 * sequencer is not allocated in our driver's tagset and it's
1066 * triggered to be freed by blk_cleanup_queue(). So we need to
1067 * always mark it as signaled to ensure that the "wr_cqe", which is
1068 * embeded in request's payload, is not freed when __ib_process_cq()
1069 * calls wr_cqe->done().
1070 */
1071 if ((++queue->sig_count % 32) == 0 || flush)
1072 wr.send_flags |= IB_SEND_SIGNALED;
1073
1074 if (first)
1075 first->next = &wr;
1076 else
1077 first = &wr;
1078
1079 ret = ib_post_send(queue->qp, first, &bad_wr);
1080 if (ret) {
1081 dev_err(queue->ctrl->ctrl.device,
1082 "%s failed with error code %d\n", __func__, ret);
1083 }
1084 return ret;
1085}
1086
1087static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1088 struct nvme_rdma_qe *qe)
1089{
1090 struct ib_recv_wr wr, *bad_wr;
1091 struct ib_sge list;
1092 int ret;
1093
1094 list.addr = qe->dma;
1095 list.length = sizeof(struct nvme_completion);
1096 list.lkey = queue->device->pd->local_dma_lkey;
1097
1098 qe->cqe.done = nvme_rdma_recv_done;
1099
1100 wr.next = NULL;
1101 wr.wr_cqe = &qe->cqe;
1102 wr.sg_list = &list;
1103 wr.num_sge = 1;
1104
1105 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1106 if (ret) {
1107 dev_err(queue->ctrl->ctrl.device,
1108 "%s failed with error code %d\n", __func__, ret);
1109 }
1110 return ret;
1111}
1112
1113static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1114{
1115 u32 queue_idx = nvme_rdma_queue_idx(queue);
1116
1117 if (queue_idx == 0)
1118 return queue->ctrl->admin_tag_set.tags[queue_idx];
1119 return queue->ctrl->tag_set.tags[queue_idx - 1];
1120}
1121
1122static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1123{
1124 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1125 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1126 struct ib_device *dev = queue->device->dev;
1127 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1128 struct nvme_command *cmd = sqe->data;
1129 struct ib_sge sge;
1130 int ret;
1131
1132 if (WARN_ON_ONCE(aer_idx != 0))
1133 return;
1134
1135 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1136
1137 memset(cmd, 0, sizeof(*cmd));
1138 cmd->common.opcode = nvme_admin_async_event;
1139 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1140 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1141 nvme_rdma_set_sg_null(cmd);
1142
1143 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1144 DMA_TO_DEVICE);
1145
1146 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1147 WARN_ON_ONCE(ret);
1148}
1149
1150static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1151 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1152{
Christoph Hellwig71102302016-07-06 21:55:52 +09001153 struct request *rq;
1154 struct nvme_rdma_request *req;
1155 int ret = 0;
1156
Christoph Hellwig71102302016-07-06 21:55:52 +09001157 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1158 if (!rq) {
1159 dev_err(queue->ctrl->ctrl.device,
1160 "tag 0x%x on QP %#x not found\n",
1161 cqe->command_id, queue->qp->qp_num);
1162 nvme_rdma_error_recovery(queue->ctrl);
1163 return ret;
1164 }
1165 req = blk_mq_rq_to_pdu(rq);
1166
Christoph Hellwig71102302016-07-06 21:55:52 +09001167 if (rq->tag == tag)
1168 ret = 1;
1169
1170 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1171 wc->ex.invalidate_rkey == req->mr->rkey)
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001172 req->mr->need_inval = false;
Christoph Hellwig71102302016-07-06 21:55:52 +09001173
Christoph Hellwigd49187e2016-11-10 07:32:33 -08001174 req->req.result = cqe->result;
1175 blk_mq_complete_request(rq, le16_to_cpu(cqe->status) >> 1);
Christoph Hellwig71102302016-07-06 21:55:52 +09001176 return ret;
1177}
1178
1179static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1180{
1181 struct nvme_rdma_qe *qe =
1182 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1183 struct nvme_rdma_queue *queue = cq->cq_context;
1184 struct ib_device *ibdev = queue->device->dev;
1185 struct nvme_completion *cqe = qe->data;
1186 const size_t len = sizeof(struct nvme_completion);
1187 int ret = 0;
1188
1189 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1190 nvme_rdma_wr_error(cq, wc, "RECV");
1191 return 0;
1192 }
1193
1194 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1195 /*
1196 * AEN requests are special as they don't time out and can
1197 * survive any kind of queue freeze and often don't respond to
1198 * aborts. We don't even bother to allocate a struct request
1199 * for them but rather special case them here.
1200 */
1201 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1202 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001203 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1204 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001205 else
1206 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1207 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1208
1209 nvme_rdma_post_recv(queue, qe);
1210 return ret;
1211}
1212
1213static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1214{
1215 __nvme_rdma_recv_done(cq, wc, -1);
1216}
1217
1218static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1219{
1220 int ret, i;
1221
1222 for (i = 0; i < queue->queue_size; i++) {
1223 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1224 if (ret)
1225 goto out_destroy_queue_ib;
1226 }
1227
1228 return 0;
1229
1230out_destroy_queue_ib:
1231 nvme_rdma_destroy_queue_ib(queue);
1232 return ret;
1233}
1234
1235static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1236 struct rdma_cm_event *ev)
1237{
Steve Wise7f039532016-10-26 12:36:47 -07001238 struct rdma_cm_id *cm_id = queue->cm_id;
1239 int status = ev->status;
1240 const char *rej_msg;
1241 const struct nvme_rdma_cm_rej *rej_data;
1242 u8 rej_data_len;
1243
1244 rej_msg = rdma_reject_msg(cm_id, status);
1245 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1246
1247 if (rej_data && rej_data_len >= sizeof(u16)) {
1248 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001249
1250 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001251 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1252 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001253 } else {
1254 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001255 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001256 }
1257
1258 return -ECONNRESET;
1259}
1260
1261static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1262{
1263 struct nvme_rdma_device *dev;
1264 int ret;
1265
1266 dev = nvme_rdma_find_get_device(queue->cm_id);
1267 if (!dev) {
1268 dev_err(queue->cm_id->device->dma_device,
1269 "no client data found!\n");
1270 return -ECONNREFUSED;
1271 }
1272
1273 ret = nvme_rdma_create_queue_ib(queue, dev);
1274 if (ret) {
1275 nvme_rdma_dev_put(dev);
1276 goto out;
1277 }
1278
1279 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1280 if (ret) {
1281 dev_err(queue->ctrl->ctrl.device,
1282 "rdma_resolve_route failed (%d).\n",
1283 queue->cm_error);
1284 goto out_destroy_queue;
1285 }
1286
1287 return 0;
1288
1289out_destroy_queue:
1290 nvme_rdma_destroy_queue_ib(queue);
1291out:
1292 return ret;
1293}
1294
1295static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1296{
1297 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1298 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001299 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001300 int ret;
1301
1302 param.qp_num = queue->qp->qp_num;
1303 param.flow_control = 1;
1304
1305 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001306 /* maximum retry count */
1307 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001308 param.rnr_retry_count = 7;
1309 param.private_data = &priv;
1310 param.private_data_len = sizeof(priv);
1311
1312 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1313 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001314 /*
1315 * set the admin queue depth to the minimum size
1316 * specified by the Fabrics standard.
1317 */
1318 if (priv.qid == 0) {
1319 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1320 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1321 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001322 /*
1323 * current interpretation of the fabrics spec
1324 * is at minimum you make hrqsize sqsize+1, or a
1325 * 1's based representation of sqsize.
1326 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001327 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001328 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001329 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001330
1331 ret = rdma_connect(queue->cm_id, &param);
1332 if (ret) {
1333 dev_err(ctrl->ctrl.device,
1334 "rdma_connect failed (%d).\n", ret);
1335 goto out_destroy_queue_ib;
1336 }
1337
1338 return 0;
1339
1340out_destroy_queue_ib:
1341 nvme_rdma_destroy_queue_ib(queue);
1342 return ret;
1343}
1344
Christoph Hellwig71102302016-07-06 21:55:52 +09001345static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1346 struct rdma_cm_event *ev)
1347{
1348 struct nvme_rdma_queue *queue = cm_id->context;
1349 int cm_error = 0;
1350
1351 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1352 rdma_event_msg(ev->event), ev->event,
1353 ev->status, cm_id);
1354
1355 switch (ev->event) {
1356 case RDMA_CM_EVENT_ADDR_RESOLVED:
1357 cm_error = nvme_rdma_addr_resolved(queue);
1358 break;
1359 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1360 cm_error = nvme_rdma_route_resolved(queue);
1361 break;
1362 case RDMA_CM_EVENT_ESTABLISHED:
1363 queue->cm_error = nvme_rdma_conn_established(queue);
1364 /* complete cm_done regardless of success/failure */
1365 complete(&queue->cm_done);
1366 return 0;
1367 case RDMA_CM_EVENT_REJECTED:
1368 cm_error = nvme_rdma_conn_rejected(queue, ev);
1369 break;
1370 case RDMA_CM_EVENT_ADDR_ERROR:
1371 case RDMA_CM_EVENT_ROUTE_ERROR:
1372 case RDMA_CM_EVENT_CONNECT_ERROR:
1373 case RDMA_CM_EVENT_UNREACHABLE:
1374 dev_dbg(queue->ctrl->ctrl.device,
1375 "CM error event %d\n", ev->event);
1376 cm_error = -ECONNRESET;
1377 break;
1378 case RDMA_CM_EVENT_DISCONNECTED:
1379 case RDMA_CM_EVENT_ADDR_CHANGE:
1380 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1381 dev_dbg(queue->ctrl->ctrl.device,
1382 "disconnect received - connection closed\n");
1383 nvme_rdma_error_recovery(queue->ctrl);
1384 break;
1385 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001386 /* device removal is handled via the ib_client API */
1387 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001388 default:
1389 dev_err(queue->ctrl->ctrl.device,
1390 "Unexpected RDMA CM event (%d)\n", ev->event);
1391 nvme_rdma_error_recovery(queue->ctrl);
1392 break;
1393 }
1394
1395 if (cm_error) {
1396 queue->cm_error = cm_error;
1397 complete(&queue->cm_done);
1398 }
1399
1400 return 0;
1401}
1402
1403static enum blk_eh_timer_return
1404nvme_rdma_timeout(struct request *rq, bool reserved)
1405{
1406 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1407
1408 /* queue error recovery */
1409 nvme_rdma_error_recovery(req->queue->ctrl);
1410
1411 /* fail with DNR on cmd timeout */
1412 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1413
1414 return BLK_EH_HANDLED;
1415}
1416
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001417/*
1418 * We cannot accept any other command until the Connect command has completed.
1419 */
1420static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1421 struct request *rq)
1422{
1423 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
Christoph Hellwig1392370e2017-01-03 14:29:02 +03001424 struct nvme_command *cmd = nvme_req(rq)->cmd;
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001425
1426 if (rq->cmd_type != REQ_TYPE_DRV_PRIV ||
1427 cmd->common.opcode != nvme_fabrics_command ||
1428 cmd->fabrics.fctype != nvme_fabrics_type_connect)
1429 return false;
1430 }
1431
1432 return true;
1433}
1434
Christoph Hellwig71102302016-07-06 21:55:52 +09001435static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1436 const struct blk_mq_queue_data *bd)
1437{
1438 struct nvme_ns *ns = hctx->queue->queuedata;
1439 struct nvme_rdma_queue *queue = hctx->driver_data;
1440 struct request *rq = bd->rq;
1441 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1442 struct nvme_rdma_qe *sqe = &req->sqe;
1443 struct nvme_command *c = sqe->data;
1444 bool flush = false;
1445 struct ib_device *dev;
Christoph Hellwig71102302016-07-06 21:55:52 +09001446 int ret;
1447
1448 WARN_ON_ONCE(rq->tag < 0);
1449
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001450 if (!nvme_rdma_queue_is_ready(queue, rq))
1451 return BLK_MQ_RQ_QUEUE_BUSY;
1452
Christoph Hellwig71102302016-07-06 21:55:52 +09001453 dev = queue->device->dev;
1454 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1455 sizeof(struct nvme_command), DMA_TO_DEVICE);
1456
1457 ret = nvme_setup_cmd(ns, rq, c);
Omar Sandovalbac00002016-11-15 11:11:58 -08001458 if (ret != BLK_MQ_RQ_QUEUE_OK)
Christoph Hellwig71102302016-07-06 21:55:52 +09001459 return ret;
1460
Christoph Hellwig71102302016-07-06 21:55:52 +09001461 blk_mq_start_request(rq);
1462
Christoph Hellwigb131c612017-01-13 12:29:12 +01001463 ret = nvme_rdma_map_data(queue, rq, c);
Christoph Hellwig71102302016-07-06 21:55:52 +09001464 if (ret < 0) {
1465 dev_err(queue->ctrl->ctrl.device,
1466 "Failed to map data (%d)\n", ret);
1467 nvme_cleanup_cmd(rq);
1468 goto err;
1469 }
1470
1471 ib_dma_sync_single_for_device(dev, sqe->dma,
1472 sizeof(struct nvme_command), DMA_TO_DEVICE);
1473
1474 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1475 flush = true;
1476 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Sagi Grimbergf5b7b552016-08-24 12:25:56 +03001477 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
Christoph Hellwig71102302016-07-06 21:55:52 +09001478 if (ret) {
1479 nvme_rdma_unmap_data(queue, rq);
1480 goto err;
1481 }
1482
1483 return BLK_MQ_RQ_QUEUE_OK;
1484err:
1485 return (ret == -ENOMEM || ret == -EAGAIN) ?
1486 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1487}
1488
1489static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1490{
1491 struct nvme_rdma_queue *queue = hctx->driver_data;
1492 struct ib_cq *cq = queue->ib_cq;
1493 struct ib_wc wc;
1494 int found = 0;
1495
1496 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1497 while (ib_poll_cq(cq, 1, &wc) > 0) {
1498 struct ib_cqe *cqe = wc.wr_cqe;
1499
1500 if (cqe) {
1501 if (cqe->done == nvme_rdma_recv_done)
1502 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1503 else
1504 cqe->done(cq, &wc);
1505 }
1506 }
1507
1508 return found;
1509}
1510
1511static void nvme_rdma_complete_rq(struct request *rq)
1512{
1513 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1514 struct nvme_rdma_queue *queue = req->queue;
1515 int error = 0;
1516
1517 nvme_rdma_unmap_data(queue, rq);
1518
1519 if (unlikely(rq->errors)) {
1520 if (nvme_req_needs_retry(rq, rq->errors)) {
1521 nvme_requeue_req(rq);
1522 return;
1523 }
1524
1525 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1526 error = rq->errors;
1527 else
1528 error = nvme_error_status(rq->errors);
1529 }
1530
1531 blk_mq_end_request(rq, error);
1532}
1533
1534static struct blk_mq_ops nvme_rdma_mq_ops = {
1535 .queue_rq = nvme_rdma_queue_rq,
1536 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001537 .init_request = nvme_rdma_init_request,
1538 .exit_request = nvme_rdma_exit_request,
1539 .reinit_request = nvme_rdma_reinit_request,
1540 .init_hctx = nvme_rdma_init_hctx,
1541 .poll = nvme_rdma_poll,
1542 .timeout = nvme_rdma_timeout,
1543};
1544
1545static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1546 .queue_rq = nvme_rdma_queue_rq,
1547 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001548 .init_request = nvme_rdma_init_admin_request,
1549 .exit_request = nvme_rdma_exit_admin_request,
1550 .reinit_request = nvme_rdma_reinit_request,
1551 .init_hctx = nvme_rdma_init_admin_hctx,
1552 .timeout = nvme_rdma_timeout,
1553};
1554
1555static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1556{
1557 int error;
1558
1559 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1560 if (error)
1561 return error;
1562
1563 ctrl->device = ctrl->queues[0].device;
1564
1565 /*
1566 * We need a reference on the device as long as the tag_set is alive,
1567 * as the MRs in the request structures need a valid ib_device.
1568 */
1569 error = -EINVAL;
1570 if (!nvme_rdma_dev_get(ctrl->device))
1571 goto out_free_queue;
1572
1573 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1574 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1575
1576 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1577 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1578 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1579 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1580 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1581 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1582 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1583 ctrl->admin_tag_set.driver_data = ctrl;
1584 ctrl->admin_tag_set.nr_hw_queues = 1;
1585 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1586
1587 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1588 if (error)
1589 goto out_put_dev;
1590
1591 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1592 if (IS_ERR(ctrl->ctrl.admin_q)) {
1593 error = PTR_ERR(ctrl->ctrl.admin_q);
1594 goto out_free_tagset;
1595 }
1596
1597 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1598 if (error)
1599 goto out_cleanup_queue;
1600
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001601 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1602
Christoph Hellwig71102302016-07-06 21:55:52 +09001603 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1604 if (error) {
1605 dev_err(ctrl->ctrl.device,
1606 "prop_get NVME_REG_CAP failed\n");
1607 goto out_cleanup_queue;
1608 }
1609
1610 ctrl->ctrl.sqsize =
1611 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1612
1613 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1614 if (error)
1615 goto out_cleanup_queue;
1616
1617 ctrl->ctrl.max_hw_sectors =
1618 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1619
1620 error = nvme_init_identify(&ctrl->ctrl);
1621 if (error)
1622 goto out_cleanup_queue;
1623
1624 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1625 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1626 DMA_TO_DEVICE);
1627 if (error)
1628 goto out_cleanup_queue;
1629
1630 nvme_start_keep_alive(&ctrl->ctrl);
1631
1632 return 0;
1633
1634out_cleanup_queue:
1635 blk_cleanup_queue(ctrl->ctrl.admin_q);
1636out_free_tagset:
1637 /* disconnect and drain the queue before freeing the tagset */
1638 nvme_rdma_stop_queue(&ctrl->queues[0]);
1639 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1640out_put_dev:
1641 nvme_rdma_dev_put(ctrl->device);
1642out_free_queue:
1643 nvme_rdma_free_queue(&ctrl->queues[0]);
1644 return error;
1645}
1646
1647static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1648{
1649 nvme_stop_keep_alive(&ctrl->ctrl);
1650 cancel_work_sync(&ctrl->err_work);
1651 cancel_delayed_work_sync(&ctrl->reconnect_work);
1652
1653 if (ctrl->queue_count > 1) {
1654 nvme_stop_queues(&ctrl->ctrl);
1655 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1656 nvme_cancel_request, &ctrl->ctrl);
1657 nvme_rdma_free_io_queues(ctrl);
1658 }
1659
Sagi Grimberg45862eb2016-07-24 09:26:16 +03001660 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
Christoph Hellwig71102302016-07-06 21:55:52 +09001661 nvme_shutdown_ctrl(&ctrl->ctrl);
1662
1663 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1664 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1665 nvme_cancel_request, &ctrl->ctrl);
1666 nvme_rdma_destroy_admin_queue(ctrl);
1667}
1668
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001669static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1670{
1671 nvme_uninit_ctrl(&ctrl->ctrl);
1672 if (shutdown)
1673 nvme_rdma_shutdown_ctrl(ctrl);
Sagi Grimberga34ca172016-07-24 09:22:19 +03001674
1675 if (ctrl->ctrl.tagset) {
1676 blk_cleanup_queue(ctrl->ctrl.connect_q);
1677 blk_mq_free_tag_set(&ctrl->tag_set);
1678 nvme_rdma_dev_put(ctrl->device);
1679 }
1680
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001681 nvme_put_ctrl(&ctrl->ctrl);
1682}
1683
Christoph Hellwig71102302016-07-06 21:55:52 +09001684static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1685{
1686 struct nvme_rdma_ctrl *ctrl = container_of(work,
1687 struct nvme_rdma_ctrl, delete_work);
1688
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001689 __nvme_rdma_remove_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001690}
1691
1692static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1693{
1694 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1695 return -EBUSY;
1696
1697 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1698 return -EBUSY;
1699
1700 return 0;
1701}
1702
1703static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1704{
1705 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001706 int ret = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001707
Steve Wisecdbecc82016-09-01 09:12:25 -07001708 /*
1709 * Keep a reference until all work is flushed since
1710 * __nvme_rdma_del_ctrl can free the ctrl mem
1711 */
1712 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1713 return -EBUSY;
Christoph Hellwig71102302016-07-06 21:55:52 +09001714 ret = __nvme_rdma_del_ctrl(ctrl);
Steve Wisecdbecc82016-09-01 09:12:25 -07001715 if (!ret)
1716 flush_work(&ctrl->delete_work);
1717 nvme_put_ctrl(&ctrl->ctrl);
1718 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001719}
1720
1721static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1722{
1723 struct nvme_rdma_ctrl *ctrl = container_of(work,
1724 struct nvme_rdma_ctrl, delete_work);
1725
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001726 __nvme_rdma_remove_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001727}
1728
1729static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1730{
1731 struct nvme_rdma_ctrl *ctrl = container_of(work,
1732 struct nvme_rdma_ctrl, reset_work);
1733 int ret;
1734 bool changed;
1735
1736 nvme_rdma_shutdown_ctrl(ctrl);
1737
1738 ret = nvme_rdma_configure_admin_queue(ctrl);
1739 if (ret) {
1740 /* ctrl is already shutdown, just remove the ctrl */
1741 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1742 goto del_dead_ctrl;
1743 }
1744
1745 if (ctrl->queue_count > 1) {
1746 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1747 if (ret)
1748 goto del_dead_ctrl;
1749
1750 ret = nvme_rdma_init_io_queues(ctrl);
1751 if (ret)
1752 goto del_dead_ctrl;
1753
1754 ret = nvme_rdma_connect_io_queues(ctrl);
1755 if (ret)
1756 goto del_dead_ctrl;
1757 }
1758
1759 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1760 WARN_ON_ONCE(!changed);
1761
1762 if (ctrl->queue_count > 1) {
1763 nvme_start_queues(&ctrl->ctrl);
1764 nvme_queue_scan(&ctrl->ctrl);
Sagi Grimberg3ef1b4b2016-08-04 13:46:19 +03001765 nvme_queue_async_events(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001766 }
1767
1768 return;
1769
1770del_dead_ctrl:
1771 /* Deleting this dead controller... */
1772 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1773 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1774}
1775
1776static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1777{
1778 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1779
1780 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1781 return -EBUSY;
1782
1783 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1784 return -EBUSY;
1785
1786 flush_work(&ctrl->reset_work);
1787
1788 return 0;
1789}
1790
1791static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1792 .name = "rdma",
1793 .module = THIS_MODULE,
1794 .is_fabrics = true,
1795 .reg_read32 = nvmf_reg_read32,
1796 .reg_read64 = nvmf_reg_read64,
1797 .reg_write32 = nvmf_reg_write32,
1798 .reset_ctrl = nvme_rdma_reset_ctrl,
1799 .free_ctrl = nvme_rdma_free_ctrl,
1800 .submit_async_event = nvme_rdma_submit_async_event,
1801 .delete_ctrl = nvme_rdma_del_ctrl,
1802 .get_subsysnqn = nvmf_get_subsysnqn,
1803 .get_address = nvmf_get_address,
1804};
1805
1806static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1807{
1808 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1809 int ret;
1810
1811 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1812 if (ret)
1813 return ret;
1814
1815 ctrl->queue_count = opts->nr_io_queues + 1;
1816 if (ctrl->queue_count < 2)
1817 return 0;
1818
1819 dev_info(ctrl->ctrl.device,
1820 "creating %d I/O queues.\n", opts->nr_io_queues);
1821
1822 ret = nvme_rdma_init_io_queues(ctrl);
1823 if (ret)
1824 return ret;
1825
1826 /*
1827 * We need a reference on the device as long as the tag_set is alive,
1828 * as the MRs in the request structures need a valid ib_device.
1829 */
1830 ret = -EINVAL;
1831 if (!nvme_rdma_dev_get(ctrl->device))
1832 goto out_free_io_queues;
1833
1834 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1835 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001836 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
Christoph Hellwig71102302016-07-06 21:55:52 +09001837 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1838 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1839 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1840 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1841 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1842 ctrl->tag_set.driver_data = ctrl;
1843 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1844 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1845
1846 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1847 if (ret)
1848 goto out_put_dev;
1849 ctrl->ctrl.tagset = &ctrl->tag_set;
1850
1851 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1852 if (IS_ERR(ctrl->ctrl.connect_q)) {
1853 ret = PTR_ERR(ctrl->ctrl.connect_q);
1854 goto out_free_tag_set;
1855 }
1856
1857 ret = nvme_rdma_connect_io_queues(ctrl);
1858 if (ret)
1859 goto out_cleanup_connect_q;
1860
1861 return 0;
1862
1863out_cleanup_connect_q:
1864 blk_cleanup_queue(ctrl->ctrl.connect_q);
1865out_free_tag_set:
1866 blk_mq_free_tag_set(&ctrl->tag_set);
1867out_put_dev:
1868 nvme_rdma_dev_put(ctrl->device);
1869out_free_io_queues:
1870 nvme_rdma_free_io_queues(ctrl);
1871 return ret;
1872}
1873
1874static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1875{
1876 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1877 size_t buflen = strlen(p);
1878
1879 /* XXX: handle IPv6 addresses */
1880
1881 if (buflen > INET_ADDRSTRLEN)
1882 return -EINVAL;
1883 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1884 return -EINVAL;
1885 in_addr->sin_family = AF_INET;
1886 return 0;
1887}
1888
1889static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1890 struct nvmf_ctrl_options *opts)
1891{
1892 struct nvme_rdma_ctrl *ctrl;
1893 int ret;
1894 bool changed;
1895
1896 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1897 if (!ctrl)
1898 return ERR_PTR(-ENOMEM);
1899 ctrl->ctrl.opts = opts;
1900 INIT_LIST_HEAD(&ctrl->list);
1901
1902 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1903 if (ret) {
1904 pr_err("malformed IP address passed: %s\n", opts->traddr);
1905 goto out_free_ctrl;
1906 }
1907
1908 if (opts->mask & NVMF_OPT_TRSVCID) {
1909 u16 port;
1910
1911 ret = kstrtou16(opts->trsvcid, 0, &port);
1912 if (ret)
1913 goto out_free_ctrl;
1914
1915 ctrl->addr_in.sin_port = cpu_to_be16(port);
1916 } else {
1917 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1918 }
1919
1920 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1921 0 /* no quirks, we're perfect! */);
1922 if (ret)
1923 goto out_free_ctrl;
1924
1925 ctrl->reconnect_delay = opts->reconnect_delay;
1926 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1927 nvme_rdma_reconnect_ctrl_work);
1928 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1929 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1930 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1931 spin_lock_init(&ctrl->lock);
1932
1933 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001934 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001935 ctrl->ctrl.kato = opts->kato;
1936
1937 ret = -ENOMEM;
1938 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1939 GFP_KERNEL);
1940 if (!ctrl->queues)
1941 goto out_uninit_ctrl;
1942
1943 ret = nvme_rdma_configure_admin_queue(ctrl);
1944 if (ret)
1945 goto out_kfree_queues;
1946
1947 /* sanity check icdoff */
1948 if (ctrl->ctrl.icdoff) {
1949 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1950 goto out_remove_admin_queue;
1951 }
1952
1953 /* sanity check keyed sgls */
1954 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1955 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1956 goto out_remove_admin_queue;
1957 }
1958
1959 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1960 /* warn if maxcmd is lower than queue_size */
1961 dev_warn(ctrl->ctrl.device,
1962 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1963 opts->queue_size, ctrl->ctrl.maxcmd);
1964 opts->queue_size = ctrl->ctrl.maxcmd;
1965 }
1966
Samuel Jones76c08bf2016-10-25 09:22:34 +02001967 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1968 /* warn if sqsize is lower than queue_size */
1969 dev_warn(ctrl->ctrl.device,
1970 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1971 opts->queue_size, ctrl->ctrl.sqsize + 1);
1972 opts->queue_size = ctrl->ctrl.sqsize + 1;
1973 }
1974
Christoph Hellwig71102302016-07-06 21:55:52 +09001975 if (opts->nr_io_queues) {
1976 ret = nvme_rdma_create_io_queues(ctrl);
1977 if (ret)
1978 goto out_remove_admin_queue;
1979 }
1980
1981 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1982 WARN_ON_ONCE(!changed);
1983
1984 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1985 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1986
1987 kref_get(&ctrl->ctrl.kref);
1988
1989 mutex_lock(&nvme_rdma_ctrl_mutex);
1990 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1991 mutex_unlock(&nvme_rdma_ctrl_mutex);
1992
1993 if (opts->nr_io_queues) {
1994 nvme_queue_scan(&ctrl->ctrl);
1995 nvme_queue_async_events(&ctrl->ctrl);
1996 }
1997
1998 return &ctrl->ctrl;
1999
2000out_remove_admin_queue:
2001 nvme_stop_keep_alive(&ctrl->ctrl);
2002 nvme_rdma_destroy_admin_queue(ctrl);
2003out_kfree_queues:
2004 kfree(ctrl->queues);
2005out_uninit_ctrl:
2006 nvme_uninit_ctrl(&ctrl->ctrl);
2007 nvme_put_ctrl(&ctrl->ctrl);
2008 if (ret > 0)
2009 ret = -EIO;
2010 return ERR_PTR(ret);
2011out_free_ctrl:
2012 kfree(ctrl);
2013 return ERR_PTR(ret);
2014}
2015
2016static struct nvmf_transport_ops nvme_rdma_transport = {
2017 .name = "rdma",
2018 .required_opts = NVMF_OPT_TRADDR,
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03002019 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
Christoph Hellwig71102302016-07-06 21:55:52 +09002020 .create_ctrl = nvme_rdma_create_ctrl,
2021};
2022
Steve Wisee87a9112016-09-02 09:01:54 -07002023static void nvme_rdma_add_one(struct ib_device *ib_device)
2024{
2025}
2026
2027static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2028{
2029 struct nvme_rdma_ctrl *ctrl;
2030
2031 /* Delete all controllers using this device */
2032 mutex_lock(&nvme_rdma_ctrl_mutex);
2033 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2034 if (ctrl->device->dev != ib_device)
2035 continue;
2036 dev_info(ctrl->ctrl.device,
2037 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2038 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2039 __nvme_rdma_del_ctrl(ctrl);
2040 }
2041 mutex_unlock(&nvme_rdma_ctrl_mutex);
2042
2043 flush_workqueue(nvme_rdma_wq);
2044}
2045
2046static struct ib_client nvme_rdma_ib_client = {
2047 .name = "nvme_rdma",
2048 .add = nvme_rdma_add_one,
2049 .remove = nvme_rdma_remove_one
2050};
2051
Christoph Hellwig71102302016-07-06 21:55:52 +09002052static int __init nvme_rdma_init_module(void)
2053{
Steve Wisee87a9112016-09-02 09:01:54 -07002054 int ret;
2055
Christoph Hellwig71102302016-07-06 21:55:52 +09002056 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2057 if (!nvme_rdma_wq)
2058 return -ENOMEM;
2059
Steve Wisee87a9112016-09-02 09:01:54 -07002060 ret = ib_register_client(&nvme_rdma_ib_client);
2061 if (ret) {
2062 destroy_workqueue(nvme_rdma_wq);
2063 return ret;
2064 }
2065
Christoph Hellwig71102302016-07-06 21:55:52 +09002066 nvmf_register_transport(&nvme_rdma_transport);
2067 return 0;
2068}
2069
2070static void __exit nvme_rdma_cleanup_module(void)
2071{
Christoph Hellwig71102302016-07-06 21:55:52 +09002072 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002073 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002074 destroy_workqueue(nvme_rdma_wq);
2075}
2076
2077module_init(nvme_rdma_init_module);
2078module_exit(nvme_rdma_cleanup_module);
2079
2080MODULE_LICENSE("GPL v2");