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