blob: f2db848f6985421e26320d1f5da079b089aebe2f [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>
Israel Rukshinf41725b2017-11-26 10:40:55 +000018#include <rdma/mr_pool.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090019#include <linux/err.h>
20#include <linux/string.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090021#include <linux/atomic.h>
22#include <linux/blk-mq.h>
Sagi Grimberg0b366582017-07-13 11:09:44 +030023#include <linux/blk-mq-rdma.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090024#include <linux/types.h>
25#include <linux/list.h>
26#include <linux/mutex.h>
27#include <linux/scatterlist.h>
28#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090029#include <asm/unaligned.h>
30
31#include <rdma/ib_verbs.h>
32#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090033#include <linux/nvme-rdma.h>
34
35#include "nvme.h"
36#include "fabrics.h"
37
38
Sagi Grimberg782d8202017-03-21 16:32:38 +020039#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090040
Christoph Hellwig71102302016-07-06 21:55:52 +090041#define NVME_RDMA_MAX_SEGMENTS 256
42
Steve Wise64a741c2018-06-20 07:15:05 -070043#define NVME_RDMA_MAX_INLINE_SEGMENTS 4
Christoph Hellwig71102302016-07-06 21:55:52 +090044
Christoph Hellwig71102302016-07-06 21:55:52 +090045struct nvme_rdma_device {
Max Gurtovoyf87c89a2017-10-23 12:59:27 +030046 struct ib_device *dev;
47 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090048 struct kref ref;
49 struct list_head entry;
Steve Wise64a741c2018-06-20 07:15:05 -070050 unsigned int num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +090051};
52
53struct nvme_rdma_qe {
54 struct ib_cqe cqe;
55 void *data;
56 u64 dma;
57};
58
59struct nvme_rdma_queue;
60struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080061 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090062 struct ib_mr *mr;
63 struct nvme_rdma_qe sqe;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +020064 union nvme_result result;
65 __le16 status;
66 refcount_t ref;
Christoph Hellwig71102302016-07-06 21:55:52 +090067 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
68 u32 num_sge;
69 int nents;
Christoph Hellwig71102302016-07-06 21:55:52 +090070 struct ib_reg_wr reg_wr;
71 struct ib_cqe reg_cqe;
72 struct nvme_rdma_queue *queue;
73 struct sg_table sg_table;
74 struct scatterlist first_sgl[];
75};
76
77enum nvme_rdma_queue_flags {
Sagi Grimberg5013e982017-10-11 15:29:12 +030078 NVME_RDMA_Q_ALLOCATED = 0,
79 NVME_RDMA_Q_LIVE = 1,
Max Gurtovoyeb1bd242017-11-28 18:28:44 +020080 NVME_RDMA_Q_TR_READY = 2,
Christoph Hellwig71102302016-07-06 21:55:52 +090081};
82
83struct nvme_rdma_queue {
84 struct nvme_rdma_qe *rsp_ring;
Christoph Hellwig71102302016-07-06 21:55:52 +090085 int queue_size;
86 size_t cmnd_capsule_len;
87 struct nvme_rdma_ctrl *ctrl;
88 struct nvme_rdma_device *device;
89 struct ib_cq *ib_cq;
90 struct ib_qp *qp;
91
92 unsigned long flags;
93 struct rdma_cm_id *cm_id;
94 int cm_error;
95 struct completion cm_done;
96};
97
98struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +090099 /* read only in the hot path */
100 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900101
102 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +0900103 struct blk_mq_tag_set tag_set;
Christoph Hellwig71102302016-07-06 21:55:52 +0900104 struct work_struct err_work;
105
106 struct nvme_rdma_qe async_event_sqe;
107
Christoph Hellwig71102302016-07-06 21:55:52 +0900108 struct delayed_work reconnect_work;
109
110 struct list_head list;
111
112 struct blk_mq_tag_set admin_tag_set;
113 struct nvme_rdma_device *device;
114
Christoph Hellwig71102302016-07-06 21:55:52 +0900115 u32 max_fr_pages;
116
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200117 struct sockaddr_storage addr;
118 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900119
120 struct nvme_ctrl ctrl;
Steve Wise64a741c2018-06-20 07:15:05 -0700121 bool use_inline_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900122};
123
124static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
125{
126 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
127}
128
129static LIST_HEAD(device_list);
130static DEFINE_MUTEX(device_list_mutex);
131
132static LIST_HEAD(nvme_rdma_ctrl_list);
133static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
134
Christoph Hellwig71102302016-07-06 21:55:52 +0900135/*
136 * Disabling this option makes small I/O goes faster, but is fundamentally
137 * unsafe. With it turned off we will have to register a global rkey that
138 * allows read and write access to all physical memory.
139 */
140static bool register_always = true;
141module_param(register_always, bool, 0444);
142MODULE_PARM_DESC(register_always,
143 "Use memory registration even for contiguous memory regions");
144
145static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
146 struct rdma_cm_event *event);
147static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900148
Sagi Grimberg90af3512017-07-10 09:22:28 +0300149static const struct blk_mq_ops nvme_rdma_mq_ops;
150static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
151
Christoph Hellwig71102302016-07-06 21:55:52 +0900152/* XXX: really should move to a generic header sooner or later.. */
153static inline void put_unaligned_le24(u32 val, u8 *p)
154{
155 *p++ = val;
156 *p++ = val >> 8;
157 *p++ = val >> 16;
158}
159
160static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
161{
162 return queue - queue->ctrl->queues;
163}
164
165static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
166{
167 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
168}
169
170static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
171 size_t capsule_size, enum dma_data_direction dir)
172{
173 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
174 kfree(qe->data);
175}
176
177static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
178 size_t capsule_size, enum dma_data_direction dir)
179{
180 qe->data = kzalloc(capsule_size, GFP_KERNEL);
181 if (!qe->data)
182 return -ENOMEM;
183
184 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
185 if (ib_dma_mapping_error(ibdev, qe->dma)) {
186 kfree(qe->data);
Prabhath Sajeepa6344d022018-11-28 11:11:29 -0700187 qe->data = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900188 return -ENOMEM;
189 }
190
191 return 0;
192}
193
194static void nvme_rdma_free_ring(struct ib_device *ibdev,
195 struct nvme_rdma_qe *ring, size_t ib_queue_size,
196 size_t capsule_size, enum dma_data_direction dir)
197{
198 int i;
199
200 for (i = 0; i < ib_queue_size; i++)
201 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
202 kfree(ring);
203}
204
205static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
206 size_t ib_queue_size, size_t capsule_size,
207 enum dma_data_direction dir)
208{
209 struct nvme_rdma_qe *ring;
210 int i;
211
212 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
213 if (!ring)
214 return NULL;
215
216 for (i = 0; i < ib_queue_size; i++) {
217 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
218 goto out_free_ring;
219 }
220
221 return ring;
222
223out_free_ring:
224 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
225 return NULL;
226}
227
228static void nvme_rdma_qp_event(struct ib_event *event, void *context)
229{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200230 pr_debug("QP event %s (%d)\n",
231 ib_event_msg(event->event), event->event);
232
Christoph Hellwig71102302016-07-06 21:55:52 +0900233}
234
235static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
236{
Bart Van Assche35da77d2018-10-08 14:28:54 -0700237 int ret;
238
239 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
Christoph Hellwig71102302016-07-06 21:55:52 +0900240 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
Bart Van Assche35da77d2018-10-08 14:28:54 -0700241 if (ret < 0)
242 return ret;
243 if (ret == 0)
244 return -ETIMEDOUT;
245 WARN_ON_ONCE(queue->cm_error > 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900246 return queue->cm_error;
247}
248
249static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
250{
251 struct nvme_rdma_device *dev = queue->device;
252 struct ib_qp_init_attr init_attr;
253 int ret;
254
255 memset(&init_attr, 0, sizeof(init_attr));
256 init_attr.event_handler = nvme_rdma_qp_event;
257 /* +1 for drain */
258 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
259 /* +1 for drain */
260 init_attr.cap.max_recv_wr = queue->queue_size + 1;
261 init_attr.cap.max_recv_sge = 1;
Steve Wise64a741c2018-06-20 07:15:05 -0700262 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +0900263 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
264 init_attr.qp_type = IB_QPT_RC;
265 init_attr.send_cq = queue->ib_cq;
266 init_attr.recv_cq = queue->ib_cq;
267
268 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
269
270 queue->qp = queue->cm_id->qp;
271 return ret;
272}
273
Christoph Hellwig385475e2017-06-13 09:15:19 +0200274static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
275 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900276{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200277 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900278 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200279 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900280 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
281 struct nvme_rdma_device *dev = queue->device;
282
Christoph Hellwig71102302016-07-06 21:55:52 +0900283 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
284 DMA_TO_DEVICE);
285}
286
Christoph Hellwig385475e2017-06-13 09:15:19 +0200287static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
288 struct request *rq, unsigned int hctx_idx,
289 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900290{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200291 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900292 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200293 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900294 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
295 struct nvme_rdma_device *dev = queue->device;
296 struct ib_device *ibdev = dev->dev;
297 int ret;
298
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600299 nvme_req(rq)->ctrl = &ctrl->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +0900300 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
301 DMA_TO_DEVICE);
302 if (ret)
303 return ret;
304
Christoph Hellwig71102302016-07-06 21:55:52 +0900305 req->queue = queue;
306
307 return 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900308}
309
Christoph Hellwig71102302016-07-06 21:55:52 +0900310static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
311 unsigned int hctx_idx)
312{
313 struct nvme_rdma_ctrl *ctrl = data;
314 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
315
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300316 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900317
318 hctx->driver_data = queue;
319 return 0;
320}
321
322static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
323 unsigned int hctx_idx)
324{
325 struct nvme_rdma_ctrl *ctrl = data;
326 struct nvme_rdma_queue *queue = &ctrl->queues[0];
327
328 BUG_ON(hctx_idx != 0);
329
330 hctx->driver_data = queue;
331 return 0;
332}
333
334static void nvme_rdma_free_dev(struct kref *ref)
335{
336 struct nvme_rdma_device *ndev =
337 container_of(ref, struct nvme_rdma_device, ref);
338
339 mutex_lock(&device_list_mutex);
340 list_del(&ndev->entry);
341 mutex_unlock(&device_list_mutex);
342
Christoph Hellwig71102302016-07-06 21:55:52 +0900343 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900344 kfree(ndev);
345}
346
347static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
348{
349 kref_put(&dev->ref, nvme_rdma_free_dev);
350}
351
352static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
353{
354 return kref_get_unless_zero(&dev->ref);
355}
356
357static struct nvme_rdma_device *
358nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
359{
360 struct nvme_rdma_device *ndev;
361
362 mutex_lock(&device_list_mutex);
363 list_for_each_entry(ndev, &device_list, entry) {
364 if (ndev->dev->node_guid == cm_id->device->node_guid &&
365 nvme_rdma_dev_get(ndev))
366 goto out_unlock;
367 }
368
369 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
370 if (!ndev)
371 goto out_err;
372
373 ndev->dev = cm_id->device;
374 kref_init(&ndev->ref);
375
Christoph Hellwig11975e02016-09-05 12:56:20 +0200376 ndev->pd = ib_alloc_pd(ndev->dev,
377 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900378 if (IS_ERR(ndev->pd))
379 goto out_free_dev;
380
Christoph Hellwig71102302016-07-06 21:55:52 +0900381 if (!(ndev->dev->attrs.device_cap_flags &
382 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
383 dev_err(&ndev->dev->dev,
384 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200385 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900386 }
387
Steve Wise64a741c2018-06-20 07:15:05 -0700388 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
Jason Gunthorpe0a3173a2018-08-16 14:13:03 -0600389 ndev->dev->attrs.max_send_sge - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900390 list_add(&ndev->entry, &device_list);
391out_unlock:
392 mutex_unlock(&device_list_mutex);
393 return ndev;
394
Christoph Hellwig71102302016-07-06 21:55:52 +0900395out_free_pd:
396 ib_dealloc_pd(ndev->pd);
397out_free_dev:
398 kfree(ndev);
399out_err:
400 mutex_unlock(&device_list_mutex);
401 return NULL;
402}
403
404static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
405{
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200406 struct nvme_rdma_device *dev;
407 struct ib_device *ibdev;
408
409 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
410 return;
411
412 dev = queue->device;
413 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900414
Israel Rukshinf41725b2017-11-26 10:40:55 +0000415 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
416
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200417 /*
418 * The cm_id object might have been destroyed during RDMA connection
419 * establishment error flow to avoid getting other cma events, thus
420 * the destruction of the QP shouldn't use rdma_cm API.
421 */
422 ib_destroy_qp(queue->qp);
Christoph Hellwig71102302016-07-06 21:55:52 +0900423 ib_free_cq(queue->ib_cq);
424
425 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
426 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
427
428 nvme_rdma_dev_put(dev);
429}
430
Israel Rukshinf41725b2017-11-26 10:40:55 +0000431static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
432{
433 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
434 ibdev->attrs.max_fast_reg_page_list_len);
435}
436
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300437static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900438{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300439 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900440 const int send_wr_factor = 3; /* MR, SEND, INV */
441 const int cq_factor = send_wr_factor + 1; /* + RECV */
442 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900443 int ret;
444
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300445 queue->device = nvme_rdma_find_get_device(queue->cm_id);
446 if (!queue->device) {
447 dev_err(queue->cm_id->device->dev.parent,
448 "no client data found!\n");
449 return -ECONNREFUSED;
450 }
451 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900452
453 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300454 * Spread I/O queues completion vectors according their queue index.
455 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900456 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300457 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900458
459 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300460 queue->ib_cq = ib_alloc_cq(ibdev, queue,
461 cq_factor * queue->queue_size + 1,
462 comp_vector, IB_POLL_SOFTIRQ);
Christoph Hellwig71102302016-07-06 21:55:52 +0900463 if (IS_ERR(queue->ib_cq)) {
464 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300465 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900466 }
467
468 ret = nvme_rdma_create_qp(queue, send_wr_factor);
469 if (ret)
470 goto out_destroy_ib_cq;
471
472 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
473 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
474 if (!queue->rsp_ring) {
475 ret = -ENOMEM;
476 goto out_destroy_qp;
477 }
478
Israel Rukshinf41725b2017-11-26 10:40:55 +0000479 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
480 queue->queue_size,
481 IB_MR_TYPE_MEM_REG,
482 nvme_rdma_get_max_fr_pages(ibdev));
483 if (ret) {
484 dev_err(queue->ctrl->ctrl.device,
485 "failed to initialize MR pool sized %d for QID %d\n",
486 queue->queue_size, idx);
487 goto out_destroy_ring;
488 }
489
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200490 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
491
Christoph Hellwig71102302016-07-06 21:55:52 +0900492 return 0;
493
Israel Rukshinf41725b2017-11-26 10:40:55 +0000494out_destroy_ring:
495 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
496 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
Christoph Hellwig71102302016-07-06 21:55:52 +0900497out_destroy_qp:
Max Gurtovoy1f61def2017-11-06 16:18:51 +0200498 rdma_destroy_qp(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900499out_destroy_ib_cq:
500 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300501out_put_dev:
502 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900503 return ret;
504}
505
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300506static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900507 int idx, size_t queue_size)
508{
509 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200510 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900511 int ret;
512
513 queue = &ctrl->queues[idx];
514 queue->ctrl = ctrl;
515 init_completion(&queue->cm_done);
516
517 if (idx > 0)
518 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
519 else
520 queue->cmnd_capsule_len = sizeof(struct nvme_command);
521
522 queue->queue_size = queue_size;
523
524 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
525 RDMA_PS_TCP, IB_QPT_RC);
526 if (IS_ERR(queue->cm_id)) {
527 dev_info(ctrl->ctrl.device,
528 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
529 return PTR_ERR(queue->cm_id);
530 }
531
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200532 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200533 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200534
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200535 queue->cm_error = -ETIMEDOUT;
536 ret = rdma_resolve_addr(queue->cm_id, src_addr,
537 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900538 NVME_RDMA_CONNECT_TIMEOUT_MS);
539 if (ret) {
540 dev_info(ctrl->ctrl.device,
541 "rdma_resolve_addr failed (%d).\n", ret);
542 goto out_destroy_cm_id;
543 }
544
545 ret = nvme_rdma_wait_for_cm(queue);
546 if (ret) {
547 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300548 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900549 goto out_destroy_cm_id;
550 }
551
Sagi Grimberg5013e982017-10-11 15:29:12 +0300552 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900553
554 return 0;
555
556out_destroy_cm_id:
557 rdma_destroy_id(queue->cm_id);
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200558 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900559 return ret;
560}
561
562static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
563{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200564 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
565 return;
566
Christoph Hellwig71102302016-07-06 21:55:52 +0900567 rdma_disconnect(queue->cm_id);
568 ib_drain_qp(queue->qp);
569}
570
571static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
572{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300573 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900574 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200575
576 nvme_rdma_destroy_queue_ib(queue);
577 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900578}
579
580static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
581{
582 int i;
583
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300584 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200585 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900586}
587
Sagi Grimberga57bd542017-08-28 21:41:10 +0200588static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
589{
590 int i;
591
592 for (i = 1; i < ctrl->ctrl.queue_count; i++)
593 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900594}
595
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300596static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
597{
598 int ret;
599
600 if (idx)
601 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
602 else
603 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
604
605 if (!ret)
606 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
607 else
608 dev_info(ctrl->ctrl.device,
609 "failed to connect queue: %d ret=%d\n", idx, ret);
610 return ret;
611}
612
613static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900614{
615 int i, ret = 0;
616
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300617 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300618 ret = nvme_rdma_start_queue(ctrl, i);
619 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200620 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900621 }
622
Steve Wisec8dbc372016-11-08 09:16:02 -0800623 return 0;
624
Sagi Grimberga57bd542017-08-28 21:41:10 +0200625out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300626 for (i--; i >= 1; i--)
627 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900628 return ret;
629}
630
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300631static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900632{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200633 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300634 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200635 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900636 int i, ret;
637
Sagi Grimbergc248c642017-03-09 13:26:07 +0200638 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300639
640 /*
641 * we map queues according to the device irq vectors for
642 * optimal locality so we don't need more queues than
643 * completion vectors.
644 */
645 nr_io_queues = min_t(unsigned int, nr_io_queues,
646 ibdev->num_comp_vectors);
647
Sagi Grimbergc248c642017-03-09 13:26:07 +0200648 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
649 if (ret)
650 return ret;
651
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300652 ctrl->ctrl.queue_count = nr_io_queues + 1;
653 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200654 return 0;
655
656 dev_info(ctrl->ctrl.device,
657 "creating %d I/O queues.\n", nr_io_queues);
658
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300659 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300660 ret = nvme_rdma_alloc_queue(ctrl, i,
661 ctrl->ctrl.sqsize + 1);
662 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900663 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900664 }
665
666 return 0;
667
668out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700669 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200670 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900671
672 return ret;
673}
674
Sagi Grimberg60070c72017-10-11 15:29:06 +0300675static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
676 struct blk_mq_tag_set *set)
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300677{
678 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300679
680 blk_mq_free_tag_set(set);
681 nvme_rdma_dev_put(ctrl->device);
682}
683
684static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
685 bool admin)
686{
687 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
688 struct blk_mq_tag_set *set;
689 int ret;
690
691 if (admin) {
692 set = &ctrl->admin_tag_set;
693 memset(set, 0, sizeof(*set));
694 set->ops = &nvme_rdma_admin_mq_ops;
Keith Busch38dabe22017-11-07 15:13:10 -0700695 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300696 set->reserved_tags = 2; /* connect + keep-alive */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100697 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300698 set->cmd_size = sizeof(struct nvme_rdma_request) +
699 SG_CHUNK_SIZE * sizeof(struct scatterlist);
700 set->driver_data = ctrl;
701 set->nr_hw_queues = 1;
702 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000703 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300704 } else {
705 set = &ctrl->tag_set;
706 memset(set, 0, sizeof(*set));
707 set->ops = &nvme_rdma_mq_ops;
Sagi Grimberg5e77d612018-06-19 15:34:13 +0300708 set->queue_depth = nctrl->sqsize + 1;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300709 set->reserved_tags = 1; /* fabric connect */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100710 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300711 set->flags = BLK_MQ_F_SHOULD_MERGE;
712 set->cmd_size = sizeof(struct nvme_rdma_request) +
713 SG_CHUNK_SIZE * sizeof(struct scatterlist);
714 set->driver_data = ctrl;
715 set->nr_hw_queues = nctrl->queue_count - 1;
716 set->timeout = NVME_IO_TIMEOUT;
717 }
718
719 ret = blk_mq_alloc_tag_set(set);
720 if (ret)
721 goto out;
722
723 /*
724 * We need a reference on the device as long as the tag_set is alive,
725 * as the MRs in the request structures need a valid ib_device.
726 */
727 ret = nvme_rdma_dev_get(ctrl->device);
728 if (!ret) {
729 ret = -EINVAL;
730 goto out_free_tagset;
731 }
732
733 return set;
734
735out_free_tagset:
736 blk_mq_free_tag_set(set);
737out:
738 return ERR_PTR(ret);
739}
740
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300741static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
742 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900743{
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300744 if (remove) {
745 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300746 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300747 }
Sagi Grimberg682630f2018-06-25 20:58:17 +0300748 if (ctrl->async_event_sqe.data) {
749 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
750 sizeof(struct nvme_command), DMA_TO_DEVICE);
751 ctrl->async_event_sqe.data = NULL;
752 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200753 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900754}
755
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300756static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
757 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300758{
759 int error;
760
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300761 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300762 if (error)
763 return error;
764
765 ctrl->device = ctrl->queues[0].device;
Hannes Reinecke103e5152018-11-16 09:22:29 +0100766 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300767
Israel Rukshinf41725b2017-11-26 10:40:55 +0000768 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300769
Sagi Grimberg94e42212018-06-19 15:34:10 +0300770 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
771 sizeof(struct nvme_command), DMA_TO_DEVICE);
772 if (error)
773 goto out_free_queue;
774
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300775 if (new) {
776 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300777 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
778 error = PTR_ERR(ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300779 goto out_free_async_qe;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300780 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300781
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300782 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
783 if (IS_ERR(ctrl->ctrl.admin_q)) {
784 error = PTR_ERR(ctrl->ctrl.admin_q);
785 goto out_free_tagset;
786 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300787 }
788
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300789 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300790 if (error)
791 goto out_cleanup_queue;
792
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300793 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300794 &ctrl->ctrl.cap);
795 if (error) {
796 dev_err(ctrl->ctrl.device,
797 "prop_get NVME_REG_CAP failed\n");
Jianchao Wang2e050f02018-05-24 09:27:38 +0800798 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300799 }
800
801 ctrl->ctrl.sqsize =
802 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
803
804 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
805 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800806 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300807
808 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700809 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300810
811 error = nvme_init_identify(&ctrl->ctrl);
812 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800813 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300814
Sagi Grimberg90af3512017-07-10 09:22:28 +0300815 return 0;
816
Jianchao Wang2e050f02018-05-24 09:27:38 +0800817out_stop_queue:
818 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300819out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300820 if (new)
821 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300822out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300823 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300824 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300825out_free_async_qe:
826 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
827 sizeof(struct nvme_command), DMA_TO_DEVICE);
Prabhath Sajeepa6344d022018-11-28 11:11:29 -0700828 ctrl->async_event_sqe.data = NULL;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300829out_free_queue:
830 nvme_rdma_free_queue(&ctrl->queues[0]);
831 return error;
832}
833
Sagi Grimberga57bd542017-08-28 21:41:10 +0200834static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
835 bool remove)
836{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200837 if (remove) {
838 blk_cleanup_queue(ctrl->ctrl.connect_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300839 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200840 }
841 nvme_rdma_free_io_queues(ctrl);
842}
843
844static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
845{
846 int ret;
847
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300848 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200849 if (ret)
850 return ret;
851
852 if (new) {
853 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300854 if (IS_ERR(ctrl->ctrl.tagset)) {
855 ret = PTR_ERR(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200856 goto out_free_io_queues;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300857 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200858
859 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
860 if (IS_ERR(ctrl->ctrl.connect_q)) {
861 ret = PTR_ERR(ctrl->ctrl.connect_q);
862 goto out_free_tag_set;
863 }
864 } else {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200865 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
866 ctrl->ctrl.queue_count - 1);
867 }
868
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300869 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200870 if (ret)
871 goto out_cleanup_connect_q;
872
873 return 0;
874
875out_cleanup_connect_q:
876 if (new)
877 blk_cleanup_queue(ctrl->ctrl.connect_q);
878out_free_tag_set:
879 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300880 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200881out_free_io_queues:
882 nvme_rdma_free_io_queues(ctrl);
883 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900884}
885
Sagi Grimberg75862c72018-07-09 12:49:07 +0300886static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
887 bool remove)
888{
889 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
890 nvme_rdma_stop_queue(&ctrl->queues[0]);
891 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, nvme_cancel_request,
892 &ctrl->ctrl);
893 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
894 nvme_rdma_destroy_admin_queue(ctrl, remove);
895}
896
897static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
898 bool remove)
899{
900 if (ctrl->ctrl.queue_count > 1) {
901 nvme_stop_queues(&ctrl->ctrl);
902 nvme_rdma_stop_io_queues(ctrl);
903 blk_mq_tagset_busy_iter(&ctrl->tag_set, nvme_cancel_request,
904 &ctrl->ctrl);
905 if (remove)
906 nvme_start_queues(&ctrl->ctrl);
907 nvme_rdma_destroy_io_queues(ctrl, remove);
908 }
909}
910
Nitzan Carmib435ece2018-03-20 11:07:30 +0000911static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
912{
913 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
914
915 cancel_work_sync(&ctrl->err_work);
916 cancel_delayed_work_sync(&ctrl->reconnect_work);
917}
918
Christoph Hellwig71102302016-07-06 21:55:52 +0900919static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
920{
921 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
922
923 if (list_empty(&ctrl->list))
924 goto free_ctrl;
925
926 mutex_lock(&nvme_rdma_ctrl_mutex);
927 list_del(&ctrl->list);
928 mutex_unlock(&nvme_rdma_ctrl_mutex);
929
Christoph Hellwig71102302016-07-06 21:55:52 +0900930 nvmf_free_options(nctrl->opts);
931free_ctrl:
Sagi Grimberg3d064102018-06-19 15:34:09 +0300932 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +0900933 kfree(ctrl);
934}
935
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200936static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
937{
938 /* If we are resetting/deleting then do nothing */
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200939 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200940 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
941 ctrl->ctrl.state == NVME_CTRL_LIVE);
942 return;
943 }
944
945 if (nvmf_should_reconnect(&ctrl->ctrl)) {
946 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
947 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200948 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200949 ctrl->ctrl.opts->reconnect_delay * HZ);
950 } else {
Sagi Grimberg12fa1302017-10-29 14:21:01 +0200951 nvme_delete_ctrl(&ctrl->ctrl);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200952 }
953}
954
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300955static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
Christoph Hellwig71102302016-07-06 21:55:52 +0900956{
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300957 int ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900958 bool changed;
Christoph Hellwig71102302016-07-06 21:55:52 +0900959
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300960 ret = nvme_rdma_configure_admin_queue(ctrl, new);
Christoph Hellwig71102302016-07-06 21:55:52 +0900961 if (ret)
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300962 return ret;
963
964 if (ctrl->ctrl.icdoff) {
965 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
966 goto destroy_admin;
967 }
968
969 if (!(ctrl->ctrl.sgls & (1 << 2))) {
970 dev_err(ctrl->ctrl.device,
971 "Mandatory keyed sgls are not supported!\n");
972 goto destroy_admin;
973 }
974
975 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
976 dev_warn(ctrl->ctrl.device,
977 "queue_size %zu > ctrl sqsize %u, clamping down\n",
978 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
979 }
980
981 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
982 dev_warn(ctrl->ctrl.device,
983 "sqsize %u > ctrl maxcmd %u, clamping down\n",
984 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
985 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
986 }
Christoph Hellwig71102302016-07-06 21:55:52 +0900987
Steve Wise64a741c2018-06-20 07:15:05 -0700988 if (ctrl->ctrl.sgls & (1 << 20))
989 ctrl->use_inline_data = true;
Christoph Hellwig71102302016-07-06 21:55:52 +0900990
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300991 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300992 ret = nvme_rdma_configure_io_queues(ctrl, new);
Christoph Hellwig71102302016-07-06 21:55:52 +0900993 if (ret)
Sagi Grimberg5e1fe612017-10-11 15:29:11 +0300994 goto destroy_admin;
Christoph Hellwig71102302016-07-06 21:55:52 +0900995 }
996
997 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +0300998 if (!changed) {
999 /* state change failure is ok if we're in DELETING state */
1000 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001001 ret = -EINVAL;
1002 goto destroy_io;
Sagi Grimberg0a960afd2017-09-21 17:01:37 +03001003 }
1004
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001005 nvme_start_ctrl(&ctrl->ctrl);
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001006 return 0;
1007
1008destroy_io:
1009 if (ctrl->ctrl.queue_count > 1)
1010 nvme_rdma_destroy_io_queues(ctrl, new);
1011destroy_admin:
1012 nvme_rdma_stop_queue(&ctrl->queues[0]);
1013 nvme_rdma_destroy_admin_queue(ctrl, new);
1014 return ret;
1015}
1016
1017static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1018{
1019 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1020 struct nvme_rdma_ctrl, reconnect_work);
1021
1022 ++ctrl->ctrl.nr_reconnects;
1023
1024 if (nvme_rdma_setup_ctrl(ctrl, false))
1025 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +09001026
Sagi Grimberg5e1fe612017-10-11 15:29:11 +03001027 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1028 ctrl->ctrl.nr_reconnects);
1029
1030 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001031
1032 return;
1033
Christoph Hellwig71102302016-07-06 21:55:52 +09001034requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001035 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03001036 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001037 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001038}
1039
1040static void nvme_rdma_error_recovery_work(struct work_struct *work)
1041{
1042 struct nvme_rdma_ctrl *ctrl = container_of(work,
1043 struct nvme_rdma_ctrl, err_work);
1044
Sagi Grimberge4d753d2017-09-21 17:01:38 +03001045 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001046 nvme_rdma_teardown_io_queues(ctrl, false);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001047 nvme_start_queues(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001048 nvme_rdma_teardown_admin_queue(ctrl, false);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001049
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001050 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Nitzan Carmi187c0832018-03-20 11:07:29 +00001051 /* state change failure is ok if we're in DELETING state */
1052 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001053 return;
1054 }
1055
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001056 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001057}
1058
1059static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1060{
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001061 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
Christoph Hellwig71102302016-07-06 21:55:52 +09001062 return;
1063
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001064 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001065}
1066
1067static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1068 const char *op)
1069{
1070 struct nvme_rdma_queue *queue = cq->cq_context;
1071 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1072
1073 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1074 dev_info(ctrl->ctrl.device,
1075 "%s for CQE 0x%p failed with status %s (%d)\n",
1076 op, wc->wr_cqe,
1077 ib_wc_status_msg(wc->status), wc->status);
1078 nvme_rdma_error_recovery(ctrl);
1079}
1080
1081static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1082{
1083 if (unlikely(wc->status != IB_WC_SUCCESS))
1084 nvme_rdma_wr_error(cq, wc, "MEMREG");
1085}
1086
1087static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1088{
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001089 struct nvme_rdma_request *req =
1090 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1091 struct request *rq = blk_mq_rq_from_pdu(req);
1092
1093 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001094 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001095 return;
1096 }
1097
1098 if (refcount_dec_and_test(&req->ref))
1099 nvme_end_request(rq, req->status, req->result);
1100
Christoph Hellwig71102302016-07-06 21:55:52 +09001101}
1102
1103static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1104 struct nvme_rdma_request *req)
1105{
Christoph Hellwig71102302016-07-06 21:55:52 +09001106 struct ib_send_wr wr = {
1107 .opcode = IB_WR_LOCAL_INV,
1108 .next = NULL,
1109 .num_sge = 0,
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001110 .send_flags = IB_SEND_SIGNALED,
Christoph Hellwig71102302016-07-06 21:55:52 +09001111 .ex.invalidate_rkey = req->mr->rkey,
1112 };
1113
1114 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1115 wr.wr_cqe = &req->reg_cqe;
1116
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001117 return ib_post_send(queue->qp, &wr, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001118}
1119
1120static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1121 struct request *rq)
1122{
1123 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001124 struct nvme_rdma_device *dev = queue->device;
1125 struct ib_device *ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +09001126
Christoph Hellwig0d309922018-02-22 07:24:09 -08001127 if (!blk_rq_payload_bytes(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001128 return;
1129
Israel Rukshinf41725b2017-11-26 10:40:55 +00001130 if (req->mr) {
1131 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1132 req->mr = NULL;
1133 }
1134
Christoph Hellwig71102302016-07-06 21:55:52 +09001135 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1136 req->nents, rq_data_dir(rq) ==
1137 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1138
1139 nvme_cleanup_cmd(rq);
1140 sg_free_table_chained(&req->sg_table, true);
1141}
1142
1143static int nvme_rdma_set_sg_null(struct nvme_command *c)
1144{
1145 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1146
1147 sg->addr = 0;
1148 put_unaligned_le24(0, sg->length);
1149 put_unaligned_le32(0, sg->key);
1150 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1151 return 0;
1152}
1153
1154static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
Steve Wise64a741c2018-06-20 07:15:05 -07001155 struct nvme_rdma_request *req, struct nvme_command *c,
1156 int count)
Christoph Hellwig71102302016-07-06 21:55:52 +09001157{
1158 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
Steve Wise64a741c2018-06-20 07:15:05 -07001159 struct scatterlist *sgl = req->sg_table.sgl;
1160 struct ib_sge *sge = &req->sge[1];
1161 u32 len = 0;
1162 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +09001163
Steve Wise64a741c2018-06-20 07:15:05 -07001164 for (i = 0; i < count; i++, sgl++, sge++) {
1165 sge->addr = sg_dma_address(sgl);
1166 sge->length = sg_dma_len(sgl);
1167 sge->lkey = queue->device->pd->local_dma_lkey;
1168 len += sge->length;
1169 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001170
1171 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
Steve Wise64a741c2018-06-20 07:15:05 -07001172 sg->length = cpu_to_le32(len);
Christoph Hellwig71102302016-07-06 21:55:52 +09001173 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1174
Steve Wise64a741c2018-06-20 07:15:05 -07001175 req->num_sge += count;
Christoph Hellwig71102302016-07-06 21:55:52 +09001176 return 0;
1177}
1178
1179static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1180 struct nvme_rdma_request *req, struct nvme_command *c)
1181{
1182 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1183
1184 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1185 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001186 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001187 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1188 return 0;
1189}
1190
1191static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1192 struct nvme_rdma_request *req, struct nvme_command *c,
1193 int count)
1194{
1195 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1196 int nr;
1197
Israel Rukshinf41725b2017-11-26 10:40:55 +00001198 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1199 if (WARN_ON_ONCE(!req->mr))
1200 return -EAGAIN;
1201
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001202 /*
1203 * Align the MR to a 4K page size to match the ctrl page size and
1204 * the block virtual boundary.
1205 */
1206 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001207 if (unlikely(nr < count)) {
Israel Rukshinf41725b2017-11-26 10:40:55 +00001208 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1209 req->mr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001210 if (nr < 0)
1211 return nr;
1212 return -EINVAL;
1213 }
1214
1215 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1216
1217 req->reg_cqe.done = nvme_rdma_memreg_done;
1218 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1219 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1220 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1221 req->reg_wr.wr.num_sge = 0;
1222 req->reg_wr.mr = req->mr;
1223 req->reg_wr.key = req->mr->rkey;
1224 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1225 IB_ACCESS_REMOTE_READ |
1226 IB_ACCESS_REMOTE_WRITE;
1227
Christoph Hellwig71102302016-07-06 21:55:52 +09001228 sg->addr = cpu_to_le64(req->mr->iova);
1229 put_unaligned_le24(req->mr->length, sg->length);
1230 put_unaligned_le32(req->mr->rkey, sg->key);
1231 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1232 NVME_SGL_FMT_INVALIDATE;
1233
1234 return 0;
1235}
1236
1237static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001238 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001239{
1240 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1241 struct nvme_rdma_device *dev = queue->device;
1242 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001243 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001244
1245 req->num_sge = 1;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001246 refcount_set(&req->ref, 2); /* send and recv completions */
Christoph Hellwig71102302016-07-06 21:55:52 +09001247
1248 c->common.flags |= NVME_CMD_SGL_METABUF;
1249
Christoph Hellwig0d309922018-02-22 07:24:09 -08001250 if (!blk_rq_payload_bytes(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001251 return nvme_rdma_set_sg_null(c);
1252
1253 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001254 ret = sg_alloc_table_chained(&req->sg_table,
1255 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001256 if (ret)
1257 return -ENOMEM;
1258
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001259 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001260
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001261 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001262 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1263 if (unlikely(count <= 0)) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001264 ret = -EIO;
1265 goto out_free_table;
Christoph Hellwig71102302016-07-06 21:55:52 +09001266 }
1267
Steve Wise64a741c2018-06-20 07:15:05 -07001268 if (count <= dev->num_inline_segments) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001269 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
Steve Wise64a741c2018-06-20 07:15:05 -07001270 queue->ctrl->use_inline_data &&
Christoph Hellwigb131c612017-01-13 12:29:12 +01001271 blk_rq_payload_bytes(rq) <=
Max Gurtovoy94423a82018-06-10 16:58:29 +03001272 nvme_rdma_inline_data_size(queue)) {
Steve Wise64a741c2018-06-20 07:15:05 -07001273 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001274 goto out;
1275 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001276
Steve Wise64a741c2018-06-20 07:15:05 -07001277 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001278 ret = nvme_rdma_map_sg_single(queue, req, c);
1279 goto out;
1280 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001281 }
1282
Max Gurtovoy94423a82018-06-10 16:58:29 +03001283 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1284out:
1285 if (unlikely(ret))
1286 goto out_unmap_sg;
1287
1288 return 0;
1289
1290out_unmap_sg:
1291 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1292 req->nents, rq_data_dir(rq) ==
1293 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1294out_free_table:
1295 sg_free_table_chained(&req->sg_table, true);
1296 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001297}
1298
1299static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1300{
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001301 struct nvme_rdma_qe *qe =
1302 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1303 struct nvme_rdma_request *req =
1304 container_of(qe, struct nvme_rdma_request, sqe);
1305 struct request *rq = blk_mq_rq_from_pdu(req);
1306
1307 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001308 nvme_rdma_wr_error(cq, wc, "SEND");
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001309 return;
1310 }
1311
1312 if (refcount_dec_and_test(&req->ref))
1313 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001314}
1315
1316static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1317 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001318 struct ib_send_wr *first)
Christoph Hellwig71102302016-07-06 21:55:52 +09001319{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001320 struct ib_send_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001321 int ret;
1322
1323 sge->addr = qe->dma;
1324 sge->length = sizeof(struct nvme_command),
1325 sge->lkey = queue->device->pd->local_dma_lkey;
1326
Christoph Hellwig71102302016-07-06 21:55:52 +09001327 wr.next = NULL;
1328 wr.wr_cqe = &qe->cqe;
1329 wr.sg_list = sge;
1330 wr.num_sge = num_sge;
1331 wr.opcode = IB_WR_SEND;
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001332 wr.send_flags = IB_SEND_SIGNALED;
Christoph Hellwig71102302016-07-06 21:55:52 +09001333
1334 if (first)
1335 first->next = &wr;
1336 else
1337 first = &wr;
1338
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001339 ret = ib_post_send(queue->qp, first, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001340 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001341 dev_err(queue->ctrl->ctrl.device,
1342 "%s failed with error code %d\n", __func__, ret);
1343 }
1344 return ret;
1345}
1346
1347static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1348 struct nvme_rdma_qe *qe)
1349{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001350 struct ib_recv_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001351 struct ib_sge list;
1352 int ret;
1353
1354 list.addr = qe->dma;
1355 list.length = sizeof(struct nvme_completion);
1356 list.lkey = queue->device->pd->local_dma_lkey;
1357
1358 qe->cqe.done = nvme_rdma_recv_done;
1359
1360 wr.next = NULL;
1361 wr.wr_cqe = &qe->cqe;
1362 wr.sg_list = &list;
1363 wr.num_sge = 1;
1364
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001365 ret = ib_post_recv(queue->qp, &wr, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001366 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001367 dev_err(queue->ctrl->ctrl.device,
1368 "%s failed with error code %d\n", __func__, ret);
1369 }
1370 return ret;
1371}
1372
1373static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1374{
1375 u32 queue_idx = nvme_rdma_queue_idx(queue);
1376
1377 if (queue_idx == 0)
1378 return queue->ctrl->admin_tag_set.tags[queue_idx];
1379 return queue->ctrl->tag_set.tags[queue_idx - 1];
1380}
1381
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001382static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1383{
1384 if (unlikely(wc->status != IB_WC_SUCCESS))
1385 nvme_rdma_wr_error(cq, wc, "ASYNC");
1386}
1387
Keith Buschad22c352017-11-07 15:13:12 -07001388static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
Christoph Hellwig71102302016-07-06 21:55:52 +09001389{
1390 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1391 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1392 struct ib_device *dev = queue->device->dev;
1393 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1394 struct nvme_command *cmd = sqe->data;
1395 struct ib_sge sge;
1396 int ret;
1397
Christoph Hellwig71102302016-07-06 21:55:52 +09001398 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1399
1400 memset(cmd, 0, sizeof(*cmd));
1401 cmd->common.opcode = nvme_admin_async_event;
Keith Busch38dabe22017-11-07 15:13:10 -07001402 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Christoph Hellwig71102302016-07-06 21:55:52 +09001403 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1404 nvme_rdma_set_sg_null(cmd);
1405
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001406 sqe->cqe.done = nvme_rdma_async_done;
1407
Christoph Hellwig71102302016-07-06 21:55:52 +09001408 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1409 DMA_TO_DEVICE);
1410
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001411 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001412 WARN_ON_ONCE(ret);
1413}
1414
Jens Axboe1052b8a2018-11-26 08:21:49 -07001415static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1416 struct nvme_completion *cqe, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001417{
Christoph Hellwig71102302016-07-06 21:55:52 +09001418 struct request *rq;
1419 struct nvme_rdma_request *req;
Christoph Hellwig71102302016-07-06 21:55:52 +09001420
Christoph Hellwig71102302016-07-06 21:55:52 +09001421 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1422 if (!rq) {
1423 dev_err(queue->ctrl->ctrl.device,
1424 "tag 0x%x on QP %#x not found\n",
1425 cqe->command_id, queue->qp->qp_num);
1426 nvme_rdma_error_recovery(queue->ctrl);
Jens Axboe1052b8a2018-11-26 08:21:49 -07001427 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001428 }
1429 req = blk_mq_rq_to_pdu(rq);
1430
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001431 req->status = cqe->status;
1432 req->result = cqe->result;
Christoph Hellwig71102302016-07-06 21:55:52 +09001433
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001434 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1435 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1436 dev_err(queue->ctrl->ctrl.device,
1437 "Bogus remote invalidation for rkey %#x\n",
1438 req->mr->rkey);
1439 nvme_rdma_error_recovery(queue->ctrl);
1440 }
Israel Rukshinf41725b2017-11-26 10:40:55 +00001441 } else if (req->mr) {
Jens Axboe1052b8a2018-11-26 08:21:49 -07001442 int ret;
1443
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001444 ret = nvme_rdma_inv_rkey(queue, req);
1445 if (unlikely(ret < 0)) {
1446 dev_err(queue->ctrl->ctrl.device,
1447 "Queueing INV WR for rkey %#x failed (%d)\n",
1448 req->mr->rkey, ret);
1449 nvme_rdma_error_recovery(queue->ctrl);
1450 }
1451 /* the local invalidation completion will end the request */
Jens Axboe1052b8a2018-11-26 08:21:49 -07001452 return;
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001453 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001454
Jens Axboe1052b8a2018-11-26 08:21:49 -07001455 if (refcount_dec_and_test(&req->ref))
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001456 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001457}
1458
Jens Axboe1052b8a2018-11-26 08:21:49 -07001459static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001460{
1461 struct nvme_rdma_qe *qe =
1462 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1463 struct nvme_rdma_queue *queue = cq->cq_context;
1464 struct ib_device *ibdev = queue->device->dev;
1465 struct nvme_completion *cqe = qe->data;
1466 const size_t len = sizeof(struct nvme_completion);
Christoph Hellwig71102302016-07-06 21:55:52 +09001467
1468 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1469 nvme_rdma_wr_error(cq, wc, "RECV");
Jens Axboe1052b8a2018-11-26 08:21:49 -07001470 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001471 }
1472
1473 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1474 /*
1475 * AEN requests are special as they don't time out and can
1476 * survive any kind of queue freeze and often don't respond to
1477 * aborts. We don't even bother to allocate a struct request
1478 * for them but rather special case them here.
1479 */
1480 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
Keith Busch38dabe22017-11-07 15:13:10 -07001481 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001482 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1483 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001484 else
Jens Axboe1052b8a2018-11-26 08:21:49 -07001485 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
Christoph Hellwig71102302016-07-06 21:55:52 +09001486 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1487
1488 nvme_rdma_post_recv(queue, qe);
Christoph Hellwig71102302016-07-06 21:55:52 +09001489}
1490
1491static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1492{
1493 int ret, i;
1494
1495 for (i = 0; i < queue->queue_size; i++) {
1496 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1497 if (ret)
1498 goto out_destroy_queue_ib;
1499 }
1500
1501 return 0;
1502
1503out_destroy_queue_ib:
1504 nvme_rdma_destroy_queue_ib(queue);
1505 return ret;
1506}
1507
1508static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1509 struct rdma_cm_event *ev)
1510{
Steve Wise7f039532016-10-26 12:36:47 -07001511 struct rdma_cm_id *cm_id = queue->cm_id;
1512 int status = ev->status;
1513 const char *rej_msg;
1514 const struct nvme_rdma_cm_rej *rej_data;
1515 u8 rej_data_len;
1516
1517 rej_msg = rdma_reject_msg(cm_id, status);
1518 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1519
1520 if (rej_data && rej_data_len >= sizeof(u16)) {
1521 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001522
1523 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001524 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1525 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001526 } else {
1527 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001528 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001529 }
1530
1531 return -ECONNRESET;
1532}
1533
1534static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1535{
Christoph Hellwig71102302016-07-06 21:55:52 +09001536 int ret;
1537
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001538 ret = nvme_rdma_create_queue_ib(queue);
1539 if (ret)
1540 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001541
1542 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1543 if (ret) {
1544 dev_err(queue->ctrl->ctrl.device,
1545 "rdma_resolve_route failed (%d).\n",
1546 queue->cm_error);
1547 goto out_destroy_queue;
1548 }
1549
1550 return 0;
1551
1552out_destroy_queue:
1553 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001554 return ret;
1555}
1556
1557static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1558{
1559 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1560 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001561 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001562 int ret;
1563
1564 param.qp_num = queue->qp->qp_num;
1565 param.flow_control = 1;
1566
1567 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001568 /* maximum retry count */
1569 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001570 param.rnr_retry_count = 7;
1571 param.private_data = &priv;
1572 param.private_data_len = sizeof(priv);
1573
1574 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1575 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001576 /*
1577 * set the admin queue depth to the minimum size
1578 * specified by the Fabrics standard.
1579 */
1580 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001581 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1582 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001583 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001584 /*
1585 * current interpretation of the fabrics spec
1586 * is at minimum you make hrqsize sqsize+1, or a
1587 * 1's based representation of sqsize.
1588 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001589 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001590 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001591 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001592
1593 ret = rdma_connect(queue->cm_id, &param);
1594 if (ret) {
1595 dev_err(ctrl->ctrl.device,
1596 "rdma_connect failed (%d).\n", ret);
1597 goto out_destroy_queue_ib;
1598 }
1599
1600 return 0;
1601
1602out_destroy_queue_ib:
1603 nvme_rdma_destroy_queue_ib(queue);
1604 return ret;
1605}
1606
Christoph Hellwig71102302016-07-06 21:55:52 +09001607static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1608 struct rdma_cm_event *ev)
1609{
1610 struct nvme_rdma_queue *queue = cm_id->context;
1611 int cm_error = 0;
1612
1613 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1614 rdma_event_msg(ev->event), ev->event,
1615 ev->status, cm_id);
1616
1617 switch (ev->event) {
1618 case RDMA_CM_EVENT_ADDR_RESOLVED:
1619 cm_error = nvme_rdma_addr_resolved(queue);
1620 break;
1621 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1622 cm_error = nvme_rdma_route_resolved(queue);
1623 break;
1624 case RDMA_CM_EVENT_ESTABLISHED:
1625 queue->cm_error = nvme_rdma_conn_established(queue);
1626 /* complete cm_done regardless of success/failure */
1627 complete(&queue->cm_done);
1628 return 0;
1629 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001630 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001631 cm_error = nvme_rdma_conn_rejected(queue, ev);
1632 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001633 case RDMA_CM_EVENT_ROUTE_ERROR:
1634 case RDMA_CM_EVENT_CONNECT_ERROR:
1635 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001636 nvme_rdma_destroy_queue_ib(queue);
Gustavo A. R. Silva249090f2018-07-05 08:14:00 -05001637 /* fall through */
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001638 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001639 dev_dbg(queue->ctrl->ctrl.device,
1640 "CM error event %d\n", ev->event);
1641 cm_error = -ECONNRESET;
1642 break;
1643 case RDMA_CM_EVENT_DISCONNECTED:
1644 case RDMA_CM_EVENT_ADDR_CHANGE:
1645 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1646 dev_dbg(queue->ctrl->ctrl.device,
1647 "disconnect received - connection closed\n");
1648 nvme_rdma_error_recovery(queue->ctrl);
1649 break;
1650 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001651 /* device removal is handled via the ib_client API */
1652 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001653 default:
1654 dev_err(queue->ctrl->ctrl.device,
1655 "Unexpected RDMA CM event (%d)\n", ev->event);
1656 nvme_rdma_error_recovery(queue->ctrl);
1657 break;
1658 }
1659
1660 if (cm_error) {
1661 queue->cm_error = cm_error;
1662 complete(&queue->cm_done);
1663 }
1664
1665 return 0;
1666}
1667
1668static enum blk_eh_timer_return
1669nvme_rdma_timeout(struct request *rq, bool reserved)
1670{
1671 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1672
Nitzan Carmie62a5382017-10-22 09:37:04 +00001673 dev_warn(req->queue->ctrl->ctrl.device,
1674 "I/O %d QID %d timeout, reset controller\n",
1675 rq->tag, nvme_rdma_queue_idx(req->queue));
1676
Christoph Hellwig71102302016-07-06 21:55:52 +09001677 /* queue error recovery */
1678 nvme_rdma_error_recovery(req->queue->ctrl);
1679
1680 /* fail with DNR on cmd timeout */
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +02001681 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001682
Christoph Hellwigdb8c48e2018-05-29 15:52:30 +02001683 return BLK_EH_DONE;
Christoph Hellwig71102302016-07-06 21:55:52 +09001684}
1685
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001686static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001687 const struct blk_mq_queue_data *bd)
1688{
1689 struct nvme_ns *ns = hctx->queue->queuedata;
1690 struct nvme_rdma_queue *queue = hctx->driver_data;
1691 struct request *rq = bd->rq;
1692 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1693 struct nvme_rdma_qe *sqe = &req->sqe;
1694 struct nvme_command *c = sqe->data;
Christoph Hellwig71102302016-07-06 21:55:52 +09001695 struct ib_device *dev;
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02001696 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001697 blk_status_t ret;
1698 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001699
1700 WARN_ON_ONCE(rq->tag < 0);
1701
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02001702 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
James Smart6cdefc62018-07-20 15:49:48 -07001703 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001704
Christoph Hellwig71102302016-07-06 21:55:52 +09001705 dev = queue->device->dev;
1706 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1707 sizeof(struct nvme_command), DMA_TO_DEVICE);
1708
1709 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001710 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001711 return ret;
1712
Christoph Hellwig71102302016-07-06 21:55:52 +09001713 blk_mq_start_request(rq);
1714
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001715 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001716 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001717 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001718 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001719 nvme_cleanup_cmd(rq);
1720 goto err;
1721 }
1722
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001723 sqe->cqe.done = nvme_rdma_send_done;
1724
Christoph Hellwig71102302016-07-06 21:55:52 +09001725 ib_dma_sync_single_for_device(dev, sqe->dma,
1726 sizeof(struct nvme_command), DMA_TO_DEVICE);
1727
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001728 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Israel Rukshinf41725b2017-11-26 10:40:55 +00001729 req->mr ? &req->reg_wr.wr : NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001730 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001731 nvme_rdma_unmap_data(queue, rq);
1732 goto err;
1733 }
1734
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001735 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001736err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001737 if (err == -ENOMEM || err == -EAGAIN)
1738 return BLK_STS_RESOURCE;
1739 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001740}
1741
Christoph Hellwig71102302016-07-06 21:55:52 +09001742static void nvme_rdma_complete_rq(struct request *rq)
1743{
1744 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001745
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001746 nvme_rdma_unmap_data(req->queue, rq);
1747 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001748}
1749
Sagi Grimberg0b366582017-07-13 11:09:44 +03001750static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1751{
1752 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1753
1754 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1755}
1756
Eric Biggersf363b082017-03-30 13:39:16 -07001757static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001758 .queue_rq = nvme_rdma_queue_rq,
1759 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001760 .init_request = nvme_rdma_init_request,
1761 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001762 .init_hctx = nvme_rdma_init_hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001763 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001764 .map_queues = nvme_rdma_map_queues,
Christoph Hellwig71102302016-07-06 21:55:52 +09001765};
1766
Eric Biggersf363b082017-03-30 13:39:16 -07001767static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001768 .queue_rq = nvme_rdma_queue_rq,
1769 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001770 .init_request = nvme_rdma_init_request,
1771 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001772 .init_hctx = nvme_rdma_init_admin_hctx,
1773 .timeout = nvme_rdma_timeout,
1774};
1775
Sagi Grimberg18398af2017-07-10 09:22:31 +03001776static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001777{
Sagi Grimberg75862c72018-07-09 12:49:07 +03001778 nvme_rdma_teardown_io_queues(ctrl, shutdown);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001779 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001780 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001781 else
1782 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001783 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001784}
1785
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001786static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001787{
Christoph Hellwige9bc2582017-10-29 10:44:30 +02001788 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001789}
1790
Christoph Hellwig71102302016-07-06 21:55:52 +09001791static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1792{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001793 struct nvme_rdma_ctrl *ctrl =
1794 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001795
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001796 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001797 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001798
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001799 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001800 /* state change failure should never happen */
1801 WARN_ON_ONCE(1);
1802 return;
1803 }
1804
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001805 if (nvme_rdma_setup_ctrl(ctrl, false))
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001806 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001807
Christoph Hellwig71102302016-07-06 21:55:52 +09001808 return;
1809
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001810out_fail:
Nitzan Carmi8000d1f2018-01-17 11:01:14 +00001811 ++ctrl->ctrl.nr_reconnects;
1812 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001813}
1814
Christoph Hellwig71102302016-07-06 21:55:52 +09001815static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1816 .name = "rdma",
1817 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001818 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001819 .reg_read32 = nvmf_reg_read32,
1820 .reg_read64 = nvmf_reg_read64,
1821 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001822 .free_ctrl = nvme_rdma_free_ctrl,
1823 .submit_async_event = nvme_rdma_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001824 .delete_ctrl = nvme_rdma_delete_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001825 .get_address = nvmf_get_address,
Nitzan Carmib435ece2018-03-20 11:07:30 +00001826 .stop_ctrl = nvme_rdma_stop_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001827};
1828
James Smart36e835f2017-10-20 16:17:09 -07001829/*
1830 * Fails a connection request if it matches an existing controller
1831 * (association) with the same tuple:
1832 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1833 *
1834 * if local address is not specified in the request, it will match an
1835 * existing controller with all the other parameters the same and no
1836 * local port address specified as well.
1837 *
1838 * The ports don't need to be compared as they are intrinsically
1839 * already matched by the port pointers supplied.
1840 */
1841static bool
1842nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1843{
1844 struct nvme_rdma_ctrl *ctrl;
1845 bool found = false;
1846
1847 mutex_lock(&nvme_rdma_ctrl_mutex);
1848 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
Sagi Grimbergb7c7be6f62018-10-18 17:40:40 -07001849 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
James Smart36e835f2017-10-20 16:17:09 -07001850 if (found)
1851 break;
1852 }
1853 mutex_unlock(&nvme_rdma_ctrl_mutex);
1854
1855 return found;
1856}
1857
Christoph Hellwig71102302016-07-06 21:55:52 +09001858static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1859 struct nvmf_ctrl_options *opts)
1860{
1861 struct nvme_rdma_ctrl *ctrl;
1862 int ret;
1863 bool changed;
1864
1865 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1866 if (!ctrl)
1867 return ERR_PTR(-ENOMEM);
1868 ctrl->ctrl.opts = opts;
1869 INIT_LIST_HEAD(&ctrl->list);
1870
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001871 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
1872 opts->trsvcid =
1873 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
1874 if (!opts->trsvcid) {
1875 ret = -ENOMEM;
1876 goto out_free_ctrl;
1877 }
1878 opts->mask |= NVMF_OPT_TRSVCID;
1879 }
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001880
1881 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001882 opts->traddr, opts->trsvcid, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001883 if (ret) {
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001884 pr_err("malformed address passed: %s:%s\n",
1885 opts->traddr, opts->trsvcid);
Christoph Hellwig71102302016-07-06 21:55:52 +09001886 goto out_free_ctrl;
1887 }
1888
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001889 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001890 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1891 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001892 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001893 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001894 opts->host_traddr);
1895 goto out_free_ctrl;
1896 }
1897 }
1898
James Smart36e835f2017-10-20 16:17:09 -07001899 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1900 ret = -EALREADY;
1901 goto out_free_ctrl;
1902 }
1903
Christoph Hellwig71102302016-07-06 21:55:52 +09001904 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1905 nvme_rdma_reconnect_ctrl_work);
1906 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001907 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001908
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001909 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
Jay Freyenseec5af8652016-08-17 15:00:27 -07001910 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001911 ctrl->ctrl.kato = opts->kato;
1912
1913 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001914 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001915 GFP_KERNEL);
1916 if (!ctrl->queues)
Sagi Grimberg3d064102018-06-19 15:34:09 +03001917 goto out_free_ctrl;
1918
1919 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1920 0 /* no quirks, we're perfect! */);
1921 if (ret)
1922 goto out_kfree_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +09001923
Max Gurtovoyb754a322018-01-31 18:31:25 +02001924 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
1925 WARN_ON_ONCE(!changed);
1926
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001927 ret = nvme_rdma_setup_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001928 if (ret)
Sagi Grimberg3d064102018-06-19 15:34:09 +03001929 goto out_uninit_ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001930
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001931 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001932 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1933
Christoph Hellwigd22524a2017-10-18 13:25:42 +02001934 nvme_get_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001935
1936 mutex_lock(&nvme_rdma_ctrl_mutex);
1937 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1938 mutex_unlock(&nvme_rdma_ctrl_mutex);
1939
Christoph Hellwig71102302016-07-06 21:55:52 +09001940 return &ctrl->ctrl;
1941
Christoph Hellwig71102302016-07-06 21:55:52 +09001942out_uninit_ctrl:
1943 nvme_uninit_ctrl(&ctrl->ctrl);
1944 nvme_put_ctrl(&ctrl->ctrl);
1945 if (ret > 0)
1946 ret = -EIO;
1947 return ERR_PTR(ret);
Sagi Grimberg3d064102018-06-19 15:34:09 +03001948out_kfree_queues:
1949 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +09001950out_free_ctrl:
1951 kfree(ctrl);
1952 return ERR_PTR(ret);
1953}
1954
1955static struct nvmf_transport_ops nvme_rdma_transport = {
1956 .name = "rdma",
Roy Shterman0de5cd32017-12-25 14:18:30 +02001957 .module = THIS_MODULE,
Christoph Hellwig71102302016-07-06 21:55:52 +09001958 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001959 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001960 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
Christoph Hellwig71102302016-07-06 21:55:52 +09001961 .create_ctrl = nvme_rdma_create_ctrl,
1962};
1963
Steve Wisee87a9112016-09-02 09:01:54 -07001964static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1965{
1966 struct nvme_rdma_ctrl *ctrl;
Max Gurtovoy9bad0402018-02-28 13:12:39 +02001967 struct nvme_rdma_device *ndev;
1968 bool found = false;
1969
1970 mutex_lock(&device_list_mutex);
1971 list_for_each_entry(ndev, &device_list, entry) {
1972 if (ndev->dev == ib_device) {
1973 found = true;
1974 break;
1975 }
1976 }
1977 mutex_unlock(&device_list_mutex);
1978
1979 if (!found)
1980 return;
Steve Wisee87a9112016-09-02 09:01:54 -07001981
1982 /* Delete all controllers using this device */
1983 mutex_lock(&nvme_rdma_ctrl_mutex);
1984 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1985 if (ctrl->device->dev != ib_device)
1986 continue;
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001987 nvme_delete_ctrl(&ctrl->ctrl);
Steve Wisee87a9112016-09-02 09:01:54 -07001988 }
1989 mutex_unlock(&nvme_rdma_ctrl_mutex);
1990
Roy Shtermanb227c592018-01-14 12:39:02 +02001991 flush_workqueue(nvme_delete_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07001992}
1993
1994static struct ib_client nvme_rdma_ib_client = {
1995 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07001996 .remove = nvme_rdma_remove_one
1997};
1998
Christoph Hellwig71102302016-07-06 21:55:52 +09001999static int __init nvme_rdma_init_module(void)
2000{
Steve Wisee87a9112016-09-02 09:01:54 -07002001 int ret;
2002
Steve Wisee87a9112016-09-02 09:01:54 -07002003 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002004 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002005 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002006
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002007 ret = nvmf_register_transport(&nvme_rdma_transport);
2008 if (ret)
2009 goto err_unreg_client;
2010
2011 return 0;
2012
2013err_unreg_client:
2014 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002015 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002016}
2017
2018static void __exit nvme_rdma_cleanup_module(void)
2019{
Christoph Hellwig71102302016-07-06 21:55:52 +09002020 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002021 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002022}
2023
2024module_init(nvme_rdma_init_module);
2025module_exit(nvme_rdma_cleanup_module);
2026
2027MODULE_LICENSE("GPL v2");