blob: 5143e2a5d54c5614d22e5afa711d33b5029aab80 [file] [log] [blame]
Christoph Hellwig5d8762d2019-02-18 09:34:21 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwig71102302016-07-06 21:55:52 +09002/*
3 * NVMe over Fabrics RDMA host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
Christoph Hellwig71102302016-07-06 21:55:52 +09005 */
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Christoph Hellwig71102302016-07-06 21:55:52 +09007#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/slab.h>
Israel Rukshinf41725b2017-11-26 10:40:55 +000010#include <rdma/mr_pool.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090011#include <linux/err.h>
12#include <linux/string.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090013#include <linux/atomic.h>
14#include <linux/blk-mq.h>
Sagi Grimberg0b366582017-07-13 11:09:44 +030015#include <linux/blk-mq-rdma.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090016#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/mutex.h>
19#include <linux/scatterlist.h>
20#include <linux/nvme.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090021#include <asm/unaligned.h>
22
23#include <rdma/ib_verbs.h>
24#include <rdma/rdma_cm.h>
Christoph Hellwig71102302016-07-06 21:55:52 +090025#include <linux/nvme-rdma.h>
26
27#include "nvme.h"
28#include "fabrics.h"
29
30
Sagi Grimberg782d8202017-03-21 16:32:38 +020031#define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
Christoph Hellwig71102302016-07-06 21:55:52 +090032
Christoph Hellwig71102302016-07-06 21:55:52 +090033#define NVME_RDMA_MAX_SEGMENTS 256
34
Steve Wise64a741c2018-06-20 07:15:05 -070035#define NVME_RDMA_MAX_INLINE_SEGMENTS 4
Christoph Hellwig71102302016-07-06 21:55:52 +090036
Christoph Hellwig71102302016-07-06 21:55:52 +090037struct nvme_rdma_device {
Max Gurtovoyf87c89a2017-10-23 12:59:27 +030038 struct ib_device *dev;
39 struct ib_pd *pd;
Christoph Hellwig71102302016-07-06 21:55:52 +090040 struct kref ref;
41 struct list_head entry;
Steve Wise64a741c2018-06-20 07:15:05 -070042 unsigned int num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +090043};
44
45struct nvme_rdma_qe {
46 struct ib_cqe cqe;
47 void *data;
48 u64 dma;
49};
50
51struct nvme_rdma_queue;
52struct nvme_rdma_request {
Christoph Hellwigd49187e2016-11-10 07:32:33 -080053 struct nvme_request req;
Christoph Hellwig71102302016-07-06 21:55:52 +090054 struct ib_mr *mr;
55 struct nvme_rdma_qe sqe;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +020056 union nvme_result result;
57 __le16 status;
58 refcount_t ref;
Christoph Hellwig71102302016-07-06 21:55:52 +090059 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
60 u32 num_sge;
61 int nents;
Christoph Hellwig71102302016-07-06 21:55:52 +090062 struct ib_reg_wr reg_wr;
63 struct ib_cqe reg_cqe;
64 struct nvme_rdma_queue *queue;
65 struct sg_table sg_table;
66 struct scatterlist first_sgl[];
67};
68
69enum nvme_rdma_queue_flags {
Sagi Grimberg5013e982017-10-11 15:29:12 +030070 NVME_RDMA_Q_ALLOCATED = 0,
71 NVME_RDMA_Q_LIVE = 1,
Max Gurtovoyeb1bd242017-11-28 18:28:44 +020072 NVME_RDMA_Q_TR_READY = 2,
Christoph Hellwig71102302016-07-06 21:55:52 +090073};
74
75struct nvme_rdma_queue {
76 struct nvme_rdma_qe *rsp_ring;
Christoph Hellwig71102302016-07-06 21:55:52 +090077 int queue_size;
78 size_t cmnd_capsule_len;
79 struct nvme_rdma_ctrl *ctrl;
80 struct nvme_rdma_device *device;
81 struct ib_cq *ib_cq;
82 struct ib_qp *qp;
83
84 unsigned long flags;
85 struct rdma_cm_id *cm_id;
86 int cm_error;
87 struct completion cm_done;
88};
89
90struct nvme_rdma_ctrl {
Christoph Hellwig71102302016-07-06 21:55:52 +090091 /* read only in the hot path */
92 struct nvme_rdma_queue *queues;
Christoph Hellwig71102302016-07-06 21:55:52 +090093
94 /* other member variables */
Christoph Hellwig71102302016-07-06 21:55:52 +090095 struct blk_mq_tag_set tag_set;
Christoph Hellwig71102302016-07-06 21:55:52 +090096 struct work_struct err_work;
97
98 struct nvme_rdma_qe async_event_sqe;
99
Christoph Hellwig71102302016-07-06 21:55:52 +0900100 struct delayed_work reconnect_work;
101
102 struct list_head list;
103
104 struct blk_mq_tag_set admin_tag_set;
105 struct nvme_rdma_device *device;
106
Christoph Hellwig71102302016-07-06 21:55:52 +0900107 u32 max_fr_pages;
108
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200109 struct sockaddr_storage addr;
110 struct sockaddr_storage src_addr;
Christoph Hellwig71102302016-07-06 21:55:52 +0900111
112 struct nvme_ctrl ctrl;
Steve Wise64a741c2018-06-20 07:15:05 -0700113 bool use_inline_data;
Sagi Grimbergb1064d32019-01-18 16:43:24 -0800114 u32 io_queues[HCTX_MAX_TYPES];
Christoph Hellwig71102302016-07-06 21:55:52 +0900115};
116
117static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
118{
119 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
120}
121
122static LIST_HEAD(device_list);
123static DEFINE_MUTEX(device_list_mutex);
124
125static LIST_HEAD(nvme_rdma_ctrl_list);
126static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
127
Christoph Hellwig71102302016-07-06 21:55:52 +0900128/*
129 * Disabling this option makes small I/O goes faster, but is fundamentally
130 * unsafe. With it turned off we will have to register a global rkey that
131 * allows read and write access to all physical memory.
132 */
133static bool register_always = true;
134module_param(register_always, bool, 0444);
135MODULE_PARM_DESC(register_always,
136 "Use memory registration even for contiguous memory regions");
137
138static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
139 struct rdma_cm_event *event);
140static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
Christoph Hellwig71102302016-07-06 21:55:52 +0900141
Sagi Grimberg90af3512017-07-10 09:22:28 +0300142static const struct blk_mq_ops nvme_rdma_mq_ops;
143static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
144
Christoph Hellwig71102302016-07-06 21:55:52 +0900145/* XXX: really should move to a generic header sooner or later.. */
146static inline void put_unaligned_le24(u32 val, u8 *p)
147{
148 *p++ = val;
149 *p++ = val >> 8;
150 *p++ = val >> 16;
151}
152
153static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
154{
155 return queue - queue->ctrl->queues;
156}
157
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800158static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
159{
160 return nvme_rdma_queue_idx(queue) >
Sagi Grimbergb1064d32019-01-18 16:43:24 -0800161 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
162 queue->ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800163}
164
Christoph Hellwig71102302016-07-06 21:55:52 +0900165static 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
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300216 /*
217 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
218 * lifetime. It's safe, since any chage in the underlying RDMA device
219 * will issue error recovery and queue re-creation.
220 */
Christoph Hellwig71102302016-07-06 21:55:52 +0900221 for (i = 0; i < ib_queue_size; i++) {
222 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
223 goto out_free_ring;
224 }
225
226 return ring;
227
228out_free_ring:
229 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
230 return NULL;
231}
232
233static void nvme_rdma_qp_event(struct ib_event *event, void *context)
234{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200235 pr_debug("QP event %s (%d)\n",
236 ib_event_msg(event->event), event->event);
237
Christoph Hellwig71102302016-07-06 21:55:52 +0900238}
239
240static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
241{
Bart Van Assche35da77d2018-10-08 14:28:54 -0700242 int ret;
243
244 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
Christoph Hellwig71102302016-07-06 21:55:52 +0900245 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
Bart Van Assche35da77d2018-10-08 14:28:54 -0700246 if (ret < 0)
247 return ret;
248 if (ret == 0)
249 return -ETIMEDOUT;
250 WARN_ON_ONCE(queue->cm_error > 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900251 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;
Steve Wise64a741c2018-06-20 07:15:05 -0700267 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +0900268 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
Christoph Hellwig385475e2017-06-13 09:15:19 +0200279static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
280 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900281{
282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +0900283
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300284 kfree(req->sqe.data);
Christoph Hellwig71102302016-07-06 21:55:52 +0900285}
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];
Christoph Hellwig71102302016-07-06 21:55:52 +0900295
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600296 nvme_req(rq)->ctrl = &ctrl->ctrl;
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300297 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
298 if (!req->sqe.data)
299 return -ENOMEM;
Christoph Hellwig71102302016-07-06 21:55:52 +0900300
Christoph Hellwig71102302016-07-06 21:55:52 +0900301 req->queue = queue;
302
303 return 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900304}
305
Christoph Hellwig71102302016-07-06 21:55:52 +0900306static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
307 unsigned int hctx_idx)
308{
309 struct nvme_rdma_ctrl *ctrl = data;
310 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
311
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300312 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900313
314 hctx->driver_data = queue;
315 return 0;
316}
317
318static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
319 unsigned int hctx_idx)
320{
321 struct nvme_rdma_ctrl *ctrl = data;
322 struct nvme_rdma_queue *queue = &ctrl->queues[0];
323
324 BUG_ON(hctx_idx != 0);
325
326 hctx->driver_data = queue;
327 return 0;
328}
329
330static void nvme_rdma_free_dev(struct kref *ref)
331{
332 struct nvme_rdma_device *ndev =
333 container_of(ref, struct nvme_rdma_device, ref);
334
335 mutex_lock(&device_list_mutex);
336 list_del(&ndev->entry);
337 mutex_unlock(&device_list_mutex);
338
Christoph Hellwig71102302016-07-06 21:55:52 +0900339 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900340 kfree(ndev);
341}
342
343static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
344{
345 kref_put(&dev->ref, nvme_rdma_free_dev);
346}
347
348static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
349{
350 return kref_get_unless_zero(&dev->ref);
351}
352
353static struct nvme_rdma_device *
354nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
355{
356 struct nvme_rdma_device *ndev;
357
358 mutex_lock(&device_list_mutex);
359 list_for_each_entry(ndev, &device_list, entry) {
360 if (ndev->dev->node_guid == cm_id->device->node_guid &&
361 nvme_rdma_dev_get(ndev))
362 goto out_unlock;
363 }
364
365 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
366 if (!ndev)
367 goto out_err;
368
369 ndev->dev = cm_id->device;
370 kref_init(&ndev->ref);
371
Christoph Hellwig11975e02016-09-05 12:56:20 +0200372 ndev->pd = ib_alloc_pd(ndev->dev,
373 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900374 if (IS_ERR(ndev->pd))
375 goto out_free_dev;
376
Christoph Hellwig71102302016-07-06 21:55:52 +0900377 if (!(ndev->dev->attrs.device_cap_flags &
378 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
379 dev_err(&ndev->dev->dev,
380 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200381 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900382 }
383
Steve Wise64a741c2018-06-20 07:15:05 -0700384 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
Jason Gunthorpe0a3173a2018-08-16 14:13:03 -0600385 ndev->dev->attrs.max_send_sge - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900386 list_add(&ndev->entry, &device_list);
387out_unlock:
388 mutex_unlock(&device_list_mutex);
389 return ndev;
390
Christoph Hellwig71102302016-07-06 21:55:52 +0900391out_free_pd:
392 ib_dealloc_pd(ndev->pd);
393out_free_dev:
394 kfree(ndev);
395out_err:
396 mutex_unlock(&device_list_mutex);
397 return NULL;
398}
399
400static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
401{
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200402 struct nvme_rdma_device *dev;
403 struct ib_device *ibdev;
404
405 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
406 return;
407
408 dev = queue->device;
409 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900410
Israel Rukshinf41725b2017-11-26 10:40:55 +0000411 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
412
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200413 /*
414 * The cm_id object might have been destroyed during RDMA connection
415 * establishment error flow to avoid getting other cma events, thus
416 * the destruction of the QP shouldn't use rdma_cm API.
417 */
418 ib_destroy_qp(queue->qp);
Christoph Hellwig71102302016-07-06 21:55:52 +0900419 ib_free_cq(queue->ib_cq);
420
421 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
422 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
423
424 nvme_rdma_dev_put(dev);
425}
426
Israel Rukshinf41725b2017-11-26 10:40:55 +0000427static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
428{
429 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
430 ibdev->attrs.max_fast_reg_page_list_len);
431}
432
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300433static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900434{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300435 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900436 const int send_wr_factor = 3; /* MR, SEND, INV */
437 const int cq_factor = send_wr_factor + 1; /* + RECV */
438 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800439 enum ib_poll_context poll_ctx;
Christoph Hellwig71102302016-07-06 21:55:52 +0900440 int ret;
441
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300442 queue->device = nvme_rdma_find_get_device(queue->cm_id);
443 if (!queue->device) {
444 dev_err(queue->cm_id->device->dev.parent,
445 "no client data found!\n");
446 return -ECONNREFUSED;
447 }
448 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900449
450 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300451 * Spread I/O queues completion vectors according their queue index.
452 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900453 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300454 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900455
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800456 /* Polling queues need direct cq polling context */
457 if (nvme_rdma_poll_queue(queue))
458 poll_ctx = IB_POLL_DIRECT;
459 else
460 poll_ctx = IB_POLL_SOFTIRQ;
461
Christoph Hellwig71102302016-07-06 21:55:52 +0900462 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300463 queue->ib_cq = ib_alloc_cq(ibdev, queue,
464 cq_factor * queue->queue_size + 1,
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800465 comp_vector, poll_ctx);
Christoph Hellwig71102302016-07-06 21:55:52 +0900466 if (IS_ERR(queue->ib_cq)) {
467 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300468 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900469 }
470
471 ret = nvme_rdma_create_qp(queue, send_wr_factor);
472 if (ret)
473 goto out_destroy_ib_cq;
474
475 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
476 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
477 if (!queue->rsp_ring) {
478 ret = -ENOMEM;
479 goto out_destroy_qp;
480 }
481
Israel Rukshinf41725b2017-11-26 10:40:55 +0000482 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
483 queue->queue_size,
484 IB_MR_TYPE_MEM_REG,
Israel Rukshin5a6781a2019-06-11 18:52:49 +0300485 nvme_rdma_get_max_fr_pages(ibdev), 0);
Israel Rukshinf41725b2017-11-26 10:40:55 +0000486 if (ret) {
487 dev_err(queue->ctrl->ctrl.device,
488 "failed to initialize MR pool sized %d for QID %d\n",
489 queue->queue_size, idx);
490 goto out_destroy_ring;
491 }
492
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200493 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
494
Christoph Hellwig71102302016-07-06 21:55:52 +0900495 return 0;
496
Israel Rukshinf41725b2017-11-26 10:40:55 +0000497out_destroy_ring:
498 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
499 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
Christoph Hellwig71102302016-07-06 21:55:52 +0900500out_destroy_qp:
Max Gurtovoy1f61def2017-11-06 16:18:51 +0200501 rdma_destroy_qp(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900502out_destroy_ib_cq:
503 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300504out_put_dev:
505 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900506 return ret;
507}
508
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300509static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900510 int idx, size_t queue_size)
511{
512 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200513 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900514 int ret;
515
516 queue = &ctrl->queues[idx];
517 queue->ctrl = ctrl;
518 init_completion(&queue->cm_done);
519
520 if (idx > 0)
521 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
522 else
523 queue->cmnd_capsule_len = sizeof(struct nvme_command);
524
525 queue->queue_size = queue_size;
526
527 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
528 RDMA_PS_TCP, IB_QPT_RC);
529 if (IS_ERR(queue->cm_id)) {
530 dev_info(ctrl->ctrl.device,
531 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
532 return PTR_ERR(queue->cm_id);
533 }
534
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200535 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200536 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200537
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200538 queue->cm_error = -ETIMEDOUT;
539 ret = rdma_resolve_addr(queue->cm_id, src_addr,
540 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900541 NVME_RDMA_CONNECT_TIMEOUT_MS);
542 if (ret) {
543 dev_info(ctrl->ctrl.device,
544 "rdma_resolve_addr failed (%d).\n", ret);
545 goto out_destroy_cm_id;
546 }
547
548 ret = nvme_rdma_wait_for_cm(queue);
549 if (ret) {
550 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300551 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900552 goto out_destroy_cm_id;
553 }
554
Sagi Grimberg5013e982017-10-11 15:29:12 +0300555 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900556
557 return 0;
558
559out_destroy_cm_id:
560 rdma_destroy_id(queue->cm_id);
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200561 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900562 return ret;
563}
564
565static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
566{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200567 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
568 return;
569
Christoph Hellwig71102302016-07-06 21:55:52 +0900570 rdma_disconnect(queue->cm_id);
571 ib_drain_qp(queue->qp);
572}
573
574static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
575{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300576 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900577 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200578
579 nvme_rdma_destroy_queue_ib(queue);
580 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900581}
582
583static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
584{
585 int i;
586
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300587 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200588 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900589}
590
Sagi Grimberga57bd542017-08-28 21:41:10 +0200591static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
592{
593 int i;
594
595 for (i = 1; i < ctrl->ctrl.queue_count; i++)
596 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900597}
598
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300599static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
600{
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800601 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
602 bool poll = nvme_rdma_poll_queue(queue);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300603 int ret;
604
605 if (idx)
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800606 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300607 else
608 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
609
610 if (!ret)
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800611 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300612 else
613 dev_info(ctrl->ctrl.device,
614 "failed to connect queue: %d ret=%d\n", idx, ret);
615 return ret;
616}
617
618static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900619{
620 int i, ret = 0;
621
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300622 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300623 ret = nvme_rdma_start_queue(ctrl, i);
624 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200625 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900626 }
627
Steve Wisec8dbc372016-11-08 09:16:02 -0800628 return 0;
629
Sagi Grimberga57bd542017-08-28 21:41:10 +0200630out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300631 for (i--; i >= 1; i--)
632 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900633 return ret;
634}
635
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300636static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900637{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200638 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300639 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimberg5651cd32019-05-28 22:49:04 -0700640 unsigned int nr_io_queues, nr_default_queues;
641 unsigned int nr_read_queues, nr_poll_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900642 int i, ret;
643
Sagi Grimberg5651cd32019-05-28 22:49:04 -0700644 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
645 min(opts->nr_io_queues, num_online_cpus()));
646 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
647 min(opts->nr_write_queues, num_online_cpus()));
648 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
649 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
Sagi Grimbergb65bb772018-12-11 23:38:58 -0800650
Sagi Grimbergc248c642017-03-09 13:26:07 +0200651 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
652 if (ret)
653 return ret;
654
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300655 ctrl->ctrl.queue_count = nr_io_queues + 1;
656 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200657 return 0;
658
659 dev_info(ctrl->ctrl.device,
660 "creating %d I/O queues.\n", nr_io_queues);
661
Sagi Grimberg5651cd32019-05-28 22:49:04 -0700662 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
663 /*
664 * separate read/write queues
665 * hand out dedicated default queues only after we have
666 * sufficient read queues.
667 */
668 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
669 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
670 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
671 min(nr_default_queues, nr_io_queues);
672 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
673 } else {
674 /*
675 * shared read/write queues
676 * either no write queues were requested, or we don't have
677 * sufficient queue count to have dedicated default queues.
678 */
679 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
680 min(nr_read_queues, nr_io_queues);
681 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
682 }
683
684 if (opts->nr_poll_queues && nr_io_queues) {
685 /* map dedicated poll queues only if we have queues left */
686 ctrl->io_queues[HCTX_TYPE_POLL] =
687 min(nr_poll_queues, nr_io_queues);
688 }
689
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300690 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300691 ret = nvme_rdma_alloc_queue(ctrl, i,
692 ctrl->ctrl.sqsize + 1);
693 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900694 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900695 }
696
697 return 0;
698
699out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700700 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200701 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900702
703 return ret;
704}
705
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300706static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
707 bool admin)
708{
709 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
710 struct blk_mq_tag_set *set;
711 int ret;
712
713 if (admin) {
714 set = &ctrl->admin_tag_set;
715 memset(set, 0, sizeof(*set));
716 set->ops = &nvme_rdma_admin_mq_ops;
Keith Busch38dabe22017-11-07 15:13:10 -0700717 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300718 set->reserved_tags = 2; /* connect + keep-alive */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100719 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300720 set->cmd_size = sizeof(struct nvme_rdma_request) +
721 SG_CHUNK_SIZE * sizeof(struct scatterlist);
722 set->driver_data = ctrl;
723 set->nr_hw_queues = 1;
724 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000725 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300726 } else {
727 set = &ctrl->tag_set;
728 memset(set, 0, sizeof(*set));
729 set->ops = &nvme_rdma_mq_ops;
Sagi Grimberg5e77d612018-06-19 15:34:13 +0300730 set->queue_depth = nctrl->sqsize + 1;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300731 set->reserved_tags = 1; /* fabric connect */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100732 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300733 set->flags = BLK_MQ_F_SHOULD_MERGE;
734 set->cmd_size = sizeof(struct nvme_rdma_request) +
735 SG_CHUNK_SIZE * sizeof(struct scatterlist);
736 set->driver_data = ctrl;
737 set->nr_hw_queues = nctrl->queue_count - 1;
738 set->timeout = NVME_IO_TIMEOUT;
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800739 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300740 }
741
742 ret = blk_mq_alloc_tag_set(set);
743 if (ret)
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300744 return ERR_PTR(ret);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300745
746 return set;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300747}
748
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300749static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
750 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900751{
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300752 if (remove) {
753 blk_cleanup_queue(ctrl->ctrl.admin_q);
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300754 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300755 }
Sagi Grimberg682630f2018-06-25 20:58:17 +0300756 if (ctrl->async_event_sqe.data) {
757 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
758 sizeof(struct nvme_command), DMA_TO_DEVICE);
759 ctrl->async_event_sqe.data = NULL;
760 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200761 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900762}
763
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300764static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
765 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300766{
767 int error;
768
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300769 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300770 if (error)
771 return error;
772
773 ctrl->device = ctrl->queues[0].device;
Hannes Reinecke103e5152018-11-16 09:22:29 +0100774 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300775
Israel Rukshinf41725b2017-11-26 10:40:55 +0000776 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300777
Max Gurtovoy62f99b62019-06-06 12:27:36 +0300778 /*
779 * Bind the async event SQE DMA mapping to the admin queue lifetime.
780 * It's safe, since any chage in the underlying RDMA device will issue
781 * error recovery and queue re-creation.
782 */
Sagi Grimberg94e42212018-06-19 15:34:10 +0300783 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
784 sizeof(struct nvme_command), DMA_TO_DEVICE);
785 if (error)
786 goto out_free_queue;
787
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300788 if (new) {
789 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300790 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
791 error = PTR_ERR(ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300792 goto out_free_async_qe;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300793 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300794
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300795 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
796 if (IS_ERR(ctrl->ctrl.admin_q)) {
797 error = PTR_ERR(ctrl->ctrl.admin_q);
798 goto out_free_tagset;
799 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300800 }
801
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300802 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300803 if (error)
804 goto out_cleanup_queue;
805
Sagi Grimbergc0f2f452019-07-22 17:06:53 -0700806 error = nvme_enable_ctrl(&ctrl->ctrl);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300807 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800808 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300809
810 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700811 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300812
813 error = nvme_init_identify(&ctrl->ctrl);
814 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800815 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300816
Sagi Grimberg90af3512017-07-10 09:22:28 +0300817 return 0;
818
Jianchao Wang2e050f02018-05-24 09:27:38 +0800819out_stop_queue:
820 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300821out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300822 if (new)
823 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300824out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300825 if (new)
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300826 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300827out_free_async_qe:
828 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
829 sizeof(struct nvme_command), DMA_TO_DEVICE);
Prabhath Sajeepa6344d022018-11-28 11:11:29 -0700830 ctrl->async_event_sqe.data = NULL;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300831out_free_queue:
832 nvme_rdma_free_queue(&ctrl->queues[0]);
833 return error;
834}
835
Sagi Grimberga57bd542017-08-28 21:41:10 +0200836static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
837 bool remove)
838{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200839 if (remove) {
840 blk_cleanup_queue(ctrl->ctrl.connect_q);
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300841 blk_mq_free_tag_set(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200842 }
843 nvme_rdma_free_io_queues(ctrl);
844}
845
846static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
847{
848 int ret;
849
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300850 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200851 if (ret)
852 return ret;
853
854 if (new) {
855 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300856 if (IS_ERR(ctrl->ctrl.tagset)) {
857 ret = PTR_ERR(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200858 goto out_free_io_queues;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300859 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200860
861 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
862 if (IS_ERR(ctrl->ctrl.connect_q)) {
863 ret = PTR_ERR(ctrl->ctrl.connect_q);
864 goto out_free_tag_set;
865 }
866 } else {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200867 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
868 ctrl->ctrl.queue_count - 1);
869 }
870
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300871 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200872 if (ret)
873 goto out_cleanup_connect_q;
874
875 return 0;
876
877out_cleanup_connect_q:
878 if (new)
879 blk_cleanup_queue(ctrl->ctrl.connect_q);
880out_free_tag_set:
881 if (new)
Max Gurtovoy87fd1252019-05-06 13:47:55 +0300882 blk_mq_free_tag_set(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200883out_free_io_queues:
884 nvme_rdma_free_io_queues(ctrl);
885 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900886}
887
Sagi Grimberg75862c72018-07-09 12:49:07 +0300888static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
889 bool remove)
890{
891 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
892 nvme_rdma_stop_queue(&ctrl->queues[0]);
Ming Lei622b8b62019-07-24 11:48:42 +0800893 if (ctrl->ctrl.admin_tagset) {
Sagi Grimberg10077092019-04-24 11:53:18 -0700894 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
895 nvme_cancel_request, &ctrl->ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +0800896 blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset);
897 }
Sagi Grimberg75862c72018-07-09 12:49:07 +0300898 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
899 nvme_rdma_destroy_admin_queue(ctrl, remove);
900}
901
902static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
903 bool remove)
904{
905 if (ctrl->ctrl.queue_count > 1) {
906 nvme_stop_queues(&ctrl->ctrl);
907 nvme_rdma_stop_io_queues(ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +0800908 if (ctrl->ctrl.tagset) {
Sagi Grimberg10077092019-04-24 11:53:18 -0700909 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
910 nvme_cancel_request, &ctrl->ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +0800911 blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset);
912 }
Sagi Grimberg75862c72018-07-09 12:49:07 +0300913 if (remove)
914 nvme_start_queues(&ctrl->ctrl);
915 nvme_rdma_destroy_io_queues(ctrl, remove);
916 }
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
Chaitanya Kulkarni34e08192019-02-20 20:13:34 -08001127 if (!blk_rq_nr_phys_segments(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);
Ming Lei46358732019-04-28 15:39:30 +08001140 sg_free_table_chained(&req->sg_table, SG_CHUNK_SIZE);
Christoph Hellwig71102302016-07-06 21:55:52 +09001141}
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
Chaitanya Kulkarni34e08192019-02-20 20:13:34 -08001250 if (!blk_rq_nr_phys_segments(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,
Ming Lei46358732019-04-28 15:39:30 +08001255 blk_rq_nr_phys_segments(rq), req->sg_table.sgl,
1256 SG_CHUNK_SIZE);
Christoph Hellwig71102302016-07-06 21:55:52 +09001257 if (ret)
1258 return -ENOMEM;
1259
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001260 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001261
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001262 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001263 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1264 if (unlikely(count <= 0)) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001265 ret = -EIO;
1266 goto out_free_table;
Christoph Hellwig71102302016-07-06 21:55:52 +09001267 }
1268
Steve Wise64a741c2018-06-20 07:15:05 -07001269 if (count <= dev->num_inline_segments) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001270 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
Steve Wise64a741c2018-06-20 07:15:05 -07001271 queue->ctrl->use_inline_data &&
Christoph Hellwigb131c612017-01-13 12:29:12 +01001272 blk_rq_payload_bytes(rq) <=
Max Gurtovoy94423a82018-06-10 16:58:29 +03001273 nvme_rdma_inline_data_size(queue)) {
Steve Wise64a741c2018-06-20 07:15:05 -07001274 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001275 goto out;
1276 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001277
Steve Wise64a741c2018-06-20 07:15:05 -07001278 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001279 ret = nvme_rdma_map_sg_single(queue, req, c);
1280 goto out;
1281 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001282 }
1283
Max Gurtovoy94423a82018-06-10 16:58:29 +03001284 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1285out:
1286 if (unlikely(ret))
1287 goto out_unmap_sg;
1288
1289 return 0;
1290
1291out_unmap_sg:
1292 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1293 req->nents, rq_data_dir(rq) ==
1294 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1295out_free_table:
Ming Lei46358732019-04-28 15:39:30 +08001296 sg_free_table_chained(&req->sg_table, SG_CHUNK_SIZE);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001297 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001298}
1299
1300static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1301{
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001302 struct nvme_rdma_qe *qe =
1303 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1304 struct nvme_rdma_request *req =
1305 container_of(qe, struct nvme_rdma_request, sqe);
1306 struct request *rq = blk_mq_rq_from_pdu(req);
1307
1308 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001309 nvme_rdma_wr_error(cq, wc, "SEND");
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001310 return;
1311 }
1312
1313 if (refcount_dec_and_test(&req->ref))
1314 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001315}
1316
1317static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1318 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001319 struct ib_send_wr *first)
Christoph Hellwig71102302016-07-06 21:55:52 +09001320{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001321 struct ib_send_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001322 int ret;
1323
1324 sge->addr = qe->dma;
1325 sge->length = sizeof(struct nvme_command),
1326 sge->lkey = queue->device->pd->local_dma_lkey;
1327
Christoph Hellwig71102302016-07-06 21:55:52 +09001328 wr.next = NULL;
1329 wr.wr_cqe = &qe->cqe;
1330 wr.sg_list = sge;
1331 wr.num_sge = num_sge;
1332 wr.opcode = IB_WR_SEND;
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001333 wr.send_flags = IB_SEND_SIGNALED;
Christoph Hellwig71102302016-07-06 21:55:52 +09001334
1335 if (first)
1336 first->next = &wr;
1337 else
1338 first = &wr;
1339
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001340 ret = ib_post_send(queue->qp, first, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001341 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001342 dev_err(queue->ctrl->ctrl.device,
1343 "%s failed with error code %d\n", __func__, ret);
1344 }
1345 return ret;
1346}
1347
1348static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1349 struct nvme_rdma_qe *qe)
1350{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001351 struct ib_recv_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001352 struct ib_sge list;
1353 int ret;
1354
1355 list.addr = qe->dma;
1356 list.length = sizeof(struct nvme_completion);
1357 list.lkey = queue->device->pd->local_dma_lkey;
1358
1359 qe->cqe.done = nvme_rdma_recv_done;
1360
1361 wr.next = NULL;
1362 wr.wr_cqe = &qe->cqe;
1363 wr.sg_list = &list;
1364 wr.num_sge = 1;
1365
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001366 ret = ib_post_recv(queue->qp, &wr, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001367 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001368 dev_err(queue->ctrl->ctrl.device,
1369 "%s failed with error code %d\n", __func__, ret);
1370 }
1371 return ret;
1372}
1373
1374static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1375{
1376 u32 queue_idx = nvme_rdma_queue_idx(queue);
1377
1378 if (queue_idx == 0)
1379 return queue->ctrl->admin_tag_set.tags[queue_idx];
1380 return queue->ctrl->tag_set.tags[queue_idx - 1];
1381}
1382
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001383static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1384{
1385 if (unlikely(wc->status != IB_WC_SUCCESS))
1386 nvme_rdma_wr_error(cq, wc, "ASYNC");
1387}
1388
Keith Buschad22c352017-11-07 15:13:12 -07001389static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
Christoph Hellwig71102302016-07-06 21:55:52 +09001390{
1391 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1392 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1393 struct ib_device *dev = queue->device->dev;
1394 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1395 struct nvme_command *cmd = sqe->data;
1396 struct ib_sge sge;
1397 int ret;
1398
Christoph Hellwig71102302016-07-06 21:55:52 +09001399 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1400
1401 memset(cmd, 0, sizeof(*cmd));
1402 cmd->common.opcode = nvme_admin_async_event;
Keith Busch38dabe22017-11-07 15:13:10 -07001403 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Christoph Hellwig71102302016-07-06 21:55:52 +09001404 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1405 nvme_rdma_set_sg_null(cmd);
1406
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001407 sqe->cqe.done = nvme_rdma_async_done;
1408
Christoph Hellwig71102302016-07-06 21:55:52 +09001409 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1410 DMA_TO_DEVICE);
1411
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001412 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001413 WARN_ON_ONCE(ret);
1414}
1415
Jens Axboe1052b8a2018-11-26 08:21:49 -07001416static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1417 struct nvme_completion *cqe, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001418{
Christoph Hellwig71102302016-07-06 21:55:52 +09001419 struct request *rq;
1420 struct nvme_rdma_request *req;
Christoph Hellwig71102302016-07-06 21:55:52 +09001421
Christoph Hellwig71102302016-07-06 21:55:52 +09001422 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1423 if (!rq) {
1424 dev_err(queue->ctrl->ctrl.device,
1425 "tag 0x%x on QP %#x not found\n",
1426 cqe->command_id, queue->qp->qp_num);
1427 nvme_rdma_error_recovery(queue->ctrl);
Jens Axboe1052b8a2018-11-26 08:21:49 -07001428 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001429 }
1430 req = blk_mq_rq_to_pdu(rq);
1431
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001432 req->status = cqe->status;
1433 req->result = cqe->result;
Christoph Hellwig71102302016-07-06 21:55:52 +09001434
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001435 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1436 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1437 dev_err(queue->ctrl->ctrl.device,
1438 "Bogus remote invalidation for rkey %#x\n",
1439 req->mr->rkey);
1440 nvme_rdma_error_recovery(queue->ctrl);
1441 }
Israel Rukshinf41725b2017-11-26 10:40:55 +00001442 } else if (req->mr) {
Jens Axboe1052b8a2018-11-26 08:21:49 -07001443 int ret;
1444
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001445 ret = nvme_rdma_inv_rkey(queue, req);
1446 if (unlikely(ret < 0)) {
1447 dev_err(queue->ctrl->ctrl.device,
1448 "Queueing INV WR for rkey %#x failed (%d)\n",
1449 req->mr->rkey, ret);
1450 nvme_rdma_error_recovery(queue->ctrl);
1451 }
1452 /* the local invalidation completion will end the request */
Jens Axboe1052b8a2018-11-26 08:21:49 -07001453 return;
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001454 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001455
Jens Axboe1052b8a2018-11-26 08:21:49 -07001456 if (refcount_dec_and_test(&req->ref))
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001457 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001458}
1459
Jens Axboe1052b8a2018-11-26 08:21:49 -07001460static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001461{
1462 struct nvme_rdma_qe *qe =
1463 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1464 struct nvme_rdma_queue *queue = cq->cq_context;
1465 struct ib_device *ibdev = queue->device->dev;
1466 struct nvme_completion *cqe = qe->data;
1467 const size_t len = sizeof(struct nvme_completion);
Christoph Hellwig71102302016-07-06 21:55:52 +09001468
1469 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1470 nvme_rdma_wr_error(cq, wc, "RECV");
Jens Axboe1052b8a2018-11-26 08:21:49 -07001471 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001472 }
1473
1474 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1475 /*
1476 * AEN requests are special as they don't time out and can
1477 * survive any kind of queue freeze and often don't respond to
1478 * aborts. We don't even bother to allocate a struct request
1479 * for them but rather special case them here.
1480 */
1481 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
Keith Busch38dabe22017-11-07 15:13:10 -07001482 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001483 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1484 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001485 else
Jens Axboe1052b8a2018-11-26 08:21:49 -07001486 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
Christoph Hellwig71102302016-07-06 21:55:52 +09001487 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1488
1489 nvme_rdma_post_recv(queue, qe);
Christoph Hellwig71102302016-07-06 21:55:52 +09001490}
1491
1492static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1493{
1494 int ret, i;
1495
1496 for (i = 0; i < queue->queue_size; i++) {
1497 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1498 if (ret)
1499 goto out_destroy_queue_ib;
1500 }
1501
1502 return 0;
1503
1504out_destroy_queue_ib:
1505 nvme_rdma_destroy_queue_ib(queue);
1506 return ret;
1507}
1508
1509static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1510 struct rdma_cm_event *ev)
1511{
Steve Wise7f039532016-10-26 12:36:47 -07001512 struct rdma_cm_id *cm_id = queue->cm_id;
1513 int status = ev->status;
1514 const char *rej_msg;
1515 const struct nvme_rdma_cm_rej *rej_data;
1516 u8 rej_data_len;
1517
1518 rej_msg = rdma_reject_msg(cm_id, status);
1519 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1520
1521 if (rej_data && rej_data_len >= sizeof(u16)) {
1522 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001523
1524 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001525 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1526 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001527 } else {
1528 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001529 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001530 }
1531
1532 return -ECONNRESET;
1533}
1534
1535static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1536{
Israel Rukshine63440d2019-08-18 12:08:52 +03001537 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001538 int ret;
1539
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001540 ret = nvme_rdma_create_queue_ib(queue);
1541 if (ret)
1542 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001543
Israel Rukshine63440d2019-08-18 12:08:52 +03001544 if (ctrl->opts->tos >= 0)
1545 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
Christoph Hellwig71102302016-07-06 21:55:52 +09001546 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1547 if (ret) {
Israel Rukshine63440d2019-08-18 12:08:52 +03001548 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09001549 queue->cm_error);
1550 goto out_destroy_queue;
1551 }
1552
1553 return 0;
1554
1555out_destroy_queue:
1556 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001557 return ret;
1558}
1559
1560static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1561{
1562 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1563 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001564 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001565 int ret;
1566
1567 param.qp_num = queue->qp->qp_num;
1568 param.flow_control = 1;
1569
1570 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001571 /* maximum retry count */
1572 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001573 param.rnr_retry_count = 7;
1574 param.private_data = &priv;
1575 param.private_data_len = sizeof(priv);
1576
1577 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1578 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001579 /*
1580 * set the admin queue depth to the minimum size
1581 * specified by the Fabrics standard.
1582 */
1583 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001584 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1585 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001586 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001587 /*
1588 * current interpretation of the fabrics spec
1589 * is at minimum you make hrqsize sqsize+1, or a
1590 * 1's based representation of sqsize.
1591 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001592 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001593 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001594 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001595
1596 ret = rdma_connect(queue->cm_id, &param);
1597 if (ret) {
1598 dev_err(ctrl->ctrl.device,
1599 "rdma_connect failed (%d).\n", ret);
1600 goto out_destroy_queue_ib;
1601 }
1602
1603 return 0;
1604
1605out_destroy_queue_ib:
1606 nvme_rdma_destroy_queue_ib(queue);
1607 return ret;
1608}
1609
Christoph Hellwig71102302016-07-06 21:55:52 +09001610static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1611 struct rdma_cm_event *ev)
1612{
1613 struct nvme_rdma_queue *queue = cm_id->context;
1614 int cm_error = 0;
1615
1616 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1617 rdma_event_msg(ev->event), ev->event,
1618 ev->status, cm_id);
1619
1620 switch (ev->event) {
1621 case RDMA_CM_EVENT_ADDR_RESOLVED:
1622 cm_error = nvme_rdma_addr_resolved(queue);
1623 break;
1624 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1625 cm_error = nvme_rdma_route_resolved(queue);
1626 break;
1627 case RDMA_CM_EVENT_ESTABLISHED:
1628 queue->cm_error = nvme_rdma_conn_established(queue);
1629 /* complete cm_done regardless of success/failure */
1630 complete(&queue->cm_done);
1631 return 0;
1632 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001633 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001634 cm_error = nvme_rdma_conn_rejected(queue, ev);
1635 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001636 case RDMA_CM_EVENT_ROUTE_ERROR:
1637 case RDMA_CM_EVENT_CONNECT_ERROR:
1638 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001639 nvme_rdma_destroy_queue_ib(queue);
Gustavo A. R. Silva249090f2018-07-05 08:14:00 -05001640 /* fall through */
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001641 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001642 dev_dbg(queue->ctrl->ctrl.device,
1643 "CM error event %d\n", ev->event);
1644 cm_error = -ECONNRESET;
1645 break;
1646 case RDMA_CM_EVENT_DISCONNECTED:
1647 case RDMA_CM_EVENT_ADDR_CHANGE:
1648 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1649 dev_dbg(queue->ctrl->ctrl.device,
1650 "disconnect received - connection closed\n");
1651 nvme_rdma_error_recovery(queue->ctrl);
1652 break;
1653 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001654 /* device removal is handled via the ib_client API */
1655 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001656 default:
1657 dev_err(queue->ctrl->ctrl.device,
1658 "Unexpected RDMA CM event (%d)\n", ev->event);
1659 nvme_rdma_error_recovery(queue->ctrl);
1660 break;
1661 }
1662
1663 if (cm_error) {
1664 queue->cm_error = cm_error;
1665 complete(&queue->cm_done);
1666 }
1667
1668 return 0;
1669}
1670
1671static enum blk_eh_timer_return
1672nvme_rdma_timeout(struct request *rq, bool reserved)
1673{
1674 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001675 struct nvme_rdma_queue *queue = req->queue;
1676 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001677
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001678 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1679 rq->tag, nvme_rdma_queue_idx(queue));
Nitzan Carmie62a5382017-10-22 09:37:04 +00001680
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001681 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1682 /*
1683 * Teardown immediately if controller times out while starting
1684 * or we are already started error recovery. all outstanding
1685 * requests are completed on shutdown, so we return BLK_EH_DONE.
1686 */
1687 flush_work(&ctrl->err_work);
1688 nvme_rdma_teardown_io_queues(ctrl, false);
1689 nvme_rdma_teardown_admin_queue(ctrl, false);
1690 return BLK_EH_DONE;
1691 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001692
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001693 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1694 nvme_rdma_error_recovery(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001695
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001696 return BLK_EH_RESET_TIMER;
Christoph Hellwig71102302016-07-06 21:55:52 +09001697}
1698
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001699static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001700 const struct blk_mq_queue_data *bd)
1701{
1702 struct nvme_ns *ns = hctx->queue->queuedata;
1703 struct nvme_rdma_queue *queue = hctx->driver_data;
1704 struct request *rq = bd->rq;
1705 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1706 struct nvme_rdma_qe *sqe = &req->sqe;
1707 struct nvme_command *c = sqe->data;
Christoph Hellwig71102302016-07-06 21:55:52 +09001708 struct ib_device *dev;
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02001709 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001710 blk_status_t ret;
1711 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001712
1713 WARN_ON_ONCE(rq->tag < 0);
1714
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02001715 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
James Smart6cdefc62018-07-20 15:49:48 -07001716 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001717
Christoph Hellwig71102302016-07-06 21:55:52 +09001718 dev = queue->device->dev;
Max Gurtovoy62f99b62019-06-06 12:27:36 +03001719
1720 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
1721 sizeof(struct nvme_command),
1722 DMA_TO_DEVICE);
1723 err = ib_dma_mapping_error(dev, req->sqe.dma);
1724 if (unlikely(err))
1725 return BLK_STS_RESOURCE;
1726
Christoph Hellwig71102302016-07-06 21:55:52 +09001727 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1728 sizeof(struct nvme_command), DMA_TO_DEVICE);
1729
1730 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001731 if (ret)
Max Gurtovoy62f99b62019-06-06 12:27:36 +03001732 goto unmap_qe;
Christoph Hellwig71102302016-07-06 21:55:52 +09001733
Christoph Hellwig71102302016-07-06 21:55:52 +09001734 blk_mq_start_request(rq);
1735
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001736 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001737 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001738 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001739 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001740 nvme_cleanup_cmd(rq);
1741 goto err;
1742 }
1743
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001744 sqe->cqe.done = nvme_rdma_send_done;
1745
Christoph Hellwig71102302016-07-06 21:55:52 +09001746 ib_dma_sync_single_for_device(dev, sqe->dma,
1747 sizeof(struct nvme_command), DMA_TO_DEVICE);
1748
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001749 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Israel Rukshinf41725b2017-11-26 10:40:55 +00001750 req->mr ? &req->reg_wr.wr : NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001751 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001752 nvme_rdma_unmap_data(queue, rq);
1753 goto err;
1754 }
1755
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001756 return BLK_STS_OK;
Max Gurtovoy62f99b62019-06-06 12:27:36 +03001757
Christoph Hellwig71102302016-07-06 21:55:52 +09001758err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001759 if (err == -ENOMEM || err == -EAGAIN)
Max Gurtovoy62f99b62019-06-06 12:27:36 +03001760 ret = BLK_STS_RESOURCE;
1761 else
1762 ret = BLK_STS_IOERR;
1763unmap_qe:
1764 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
1765 DMA_TO_DEVICE);
1766 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001767}
1768
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001769static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
1770{
1771 struct nvme_rdma_queue *queue = hctx->driver_data;
1772
1773 return ib_process_cq_direct(queue->ib_cq, -1);
1774}
1775
Christoph Hellwig71102302016-07-06 21:55:52 +09001776static void nvme_rdma_complete_rq(struct request *rq)
1777{
1778 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Max Gurtovoy62f99b62019-06-06 12:27:36 +03001779 struct nvme_rdma_queue *queue = req->queue;
1780 struct ib_device *ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +09001781
Max Gurtovoy62f99b62019-06-06 12:27:36 +03001782 nvme_rdma_unmap_data(queue, rq);
1783 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
1784 DMA_TO_DEVICE);
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001785 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001786}
1787
Sagi Grimberg0b366582017-07-13 11:09:44 +03001788static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1789{
1790 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001791 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +03001792
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001793 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001794 /* separate read/write queues */
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001795 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1796 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1797 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
1798 set->map[HCTX_TYPE_READ].nr_queues =
1799 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001800 set->map[HCTX_TYPE_READ].queue_offset =
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001801 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001802 } else {
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001803 /* shared read/write queues */
1804 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1805 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1806 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
1807 set->map[HCTX_TYPE_READ].nr_queues =
1808 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001809 set->map[HCTX_TYPE_READ].queue_offset = 0;
1810 }
1811 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
1812 ctrl->device->dev, 0);
1813 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
1814 ctrl->device->dev, 0);
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001815
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001816 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
1817 /* map dedicated poll queues only if we have queues left */
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001818 set->map[HCTX_TYPE_POLL].nr_queues =
Sagi Grimbergb1064d32019-01-18 16:43:24 -08001819 ctrl->io_queues[HCTX_TYPE_POLL];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001820 set->map[HCTX_TYPE_POLL].queue_offset =
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001821 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1822 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001823 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
1824 }
Sagi Grimberg5651cd32019-05-28 22:49:04 -07001825
1826 dev_info(ctrl->ctrl.device,
1827 "mapped %d/%d/%d default/read/poll queues.\n",
1828 ctrl->io_queues[HCTX_TYPE_DEFAULT],
1829 ctrl->io_queues[HCTX_TYPE_READ],
1830 ctrl->io_queues[HCTX_TYPE_POLL]);
1831
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001832 return 0;
Sagi Grimberg0b366582017-07-13 11:09:44 +03001833}
1834
Eric Biggersf363b082017-03-30 13:39:16 -07001835static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001836 .queue_rq = nvme_rdma_queue_rq,
1837 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001838 .init_request = nvme_rdma_init_request,
1839 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001840 .init_hctx = nvme_rdma_init_hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001841 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001842 .map_queues = nvme_rdma_map_queues,
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001843 .poll = nvme_rdma_poll,
Christoph Hellwig71102302016-07-06 21:55:52 +09001844};
1845
Eric Biggersf363b082017-03-30 13:39:16 -07001846static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001847 .queue_rq = nvme_rdma_queue_rq,
1848 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001849 .init_request = nvme_rdma_init_request,
1850 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001851 .init_hctx = nvme_rdma_init_admin_hctx,
1852 .timeout = nvme_rdma_timeout,
1853};
1854
Sagi Grimberg18398af2017-07-10 09:22:31 +03001855static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001856{
Sagi Grimberg794a4cb2019-01-01 00:19:30 -08001857 cancel_work_sync(&ctrl->err_work);
1858 cancel_delayed_work_sync(&ctrl->reconnect_work);
1859
Sagi Grimberg75862c72018-07-09 12:49:07 +03001860 nvme_rdma_teardown_io_queues(ctrl, shutdown);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001861 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001862 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001863 else
Sagi Grimbergb5b05042019-07-22 17:06:54 -07001864 nvme_disable_ctrl(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001865 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001866}
1867
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001868static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001869{
Christoph Hellwige9bc2582017-10-29 10:44:30 +02001870 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001871}
1872
Christoph Hellwig71102302016-07-06 21:55:52 +09001873static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1874{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001875 struct nvme_rdma_ctrl *ctrl =
1876 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001877
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001878 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001879 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001880
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001881 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001882 /* state change failure should never happen */
1883 WARN_ON_ONCE(1);
1884 return;
1885 }
1886
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001887 if (nvme_rdma_setup_ctrl(ctrl, false))
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001888 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001889
Christoph Hellwig71102302016-07-06 21:55:52 +09001890 return;
1891
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001892out_fail:
Nitzan Carmi8000d1f2018-01-17 11:01:14 +00001893 ++ctrl->ctrl.nr_reconnects;
1894 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001895}
1896
Christoph Hellwig71102302016-07-06 21:55:52 +09001897static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1898 .name = "rdma",
1899 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001900 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001901 .reg_read32 = nvmf_reg_read32,
1902 .reg_read64 = nvmf_reg_read64,
1903 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001904 .free_ctrl = nvme_rdma_free_ctrl,
1905 .submit_async_event = nvme_rdma_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001906 .delete_ctrl = nvme_rdma_delete_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001907 .get_address = nvmf_get_address,
1908};
1909
James Smart36e835f2017-10-20 16:17:09 -07001910/*
1911 * Fails a connection request if it matches an existing controller
1912 * (association) with the same tuple:
1913 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1914 *
1915 * if local address is not specified in the request, it will match an
1916 * existing controller with all the other parameters the same and no
1917 * local port address specified as well.
1918 *
1919 * The ports don't need to be compared as they are intrinsically
1920 * already matched by the port pointers supplied.
1921 */
1922static bool
1923nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1924{
1925 struct nvme_rdma_ctrl *ctrl;
1926 bool found = false;
1927
1928 mutex_lock(&nvme_rdma_ctrl_mutex);
1929 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
Sagi Grimbergb7c7be6f62018-10-18 17:40:40 -07001930 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
James Smart36e835f2017-10-20 16:17:09 -07001931 if (found)
1932 break;
1933 }
1934 mutex_unlock(&nvme_rdma_ctrl_mutex);
1935
1936 return found;
1937}
1938
Christoph Hellwig71102302016-07-06 21:55:52 +09001939static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1940 struct nvmf_ctrl_options *opts)
1941{
1942 struct nvme_rdma_ctrl *ctrl;
1943 int ret;
1944 bool changed;
1945
1946 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1947 if (!ctrl)
1948 return ERR_PTR(-ENOMEM);
1949 ctrl->ctrl.opts = opts;
1950 INIT_LIST_HEAD(&ctrl->list);
1951
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001952 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
1953 opts->trsvcid =
1954 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
1955 if (!opts->trsvcid) {
1956 ret = -ENOMEM;
1957 goto out_free_ctrl;
1958 }
1959 opts->mask |= NVMF_OPT_TRSVCID;
1960 }
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001961
1962 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001963 opts->traddr, opts->trsvcid, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001964 if (ret) {
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001965 pr_err("malformed address passed: %s:%s\n",
1966 opts->traddr, opts->trsvcid);
Christoph Hellwig71102302016-07-06 21:55:52 +09001967 goto out_free_ctrl;
1968 }
1969
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001970 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001971 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1972 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001973 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001974 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001975 opts->host_traddr);
1976 goto out_free_ctrl;
1977 }
1978 }
1979
James Smart36e835f2017-10-20 16:17:09 -07001980 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1981 ret = -EALREADY;
1982 goto out_free_ctrl;
1983 }
1984
Christoph Hellwig71102302016-07-06 21:55:52 +09001985 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1986 nvme_rdma_reconnect_ctrl_work);
1987 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001988 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001989
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001990 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
1991 opts->nr_poll_queues + 1;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001992 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001993 ctrl->ctrl.kato = opts->kato;
1994
1995 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001996 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001997 GFP_KERNEL);
1998 if (!ctrl->queues)
Sagi Grimberg3d064102018-06-19 15:34:09 +03001999 goto out_free_ctrl;
2000
2001 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2002 0 /* no quirks, we're perfect! */);
2003 if (ret)
2004 goto out_kfree_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +09002005
Max Gurtovoyb754a322018-01-31 18:31:25 +02002006 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2007 WARN_ON_ONCE(!changed);
2008
Sagi Grimbergc66e2992018-07-09 12:49:06 +03002009 ret = nvme_rdma_setup_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09002010 if (ret)
Sagi Grimberg3d064102018-06-19 15:34:09 +03002011 goto out_uninit_ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09002012
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02002013 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09002014 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2015
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002016 nvme_get_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09002017
2018 mutex_lock(&nvme_rdma_ctrl_mutex);
2019 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2020 mutex_unlock(&nvme_rdma_ctrl_mutex);
2021
Christoph Hellwig71102302016-07-06 21:55:52 +09002022 return &ctrl->ctrl;
2023
Christoph Hellwig71102302016-07-06 21:55:52 +09002024out_uninit_ctrl:
2025 nvme_uninit_ctrl(&ctrl->ctrl);
2026 nvme_put_ctrl(&ctrl->ctrl);
2027 if (ret > 0)
2028 ret = -EIO;
2029 return ERR_PTR(ret);
Sagi Grimberg3d064102018-06-19 15:34:09 +03002030out_kfree_queues:
2031 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +09002032out_free_ctrl:
2033 kfree(ctrl);
2034 return ERR_PTR(ret);
2035}
2036
2037static struct nvmf_transport_ops nvme_rdma_transport = {
2038 .name = "rdma",
Roy Shterman0de5cd32017-12-25 14:18:30 +02002039 .module = THIS_MODULE,
Christoph Hellwig71102302016-07-06 21:55:52 +09002040 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002041 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002042 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
Israel Rukshine63440d2019-08-18 12:08:52 +03002043 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2044 NVMF_OPT_TOS,
Christoph Hellwig71102302016-07-06 21:55:52 +09002045 .create_ctrl = nvme_rdma_create_ctrl,
2046};
2047
Steve Wisee87a9112016-09-02 09:01:54 -07002048static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2049{
2050 struct nvme_rdma_ctrl *ctrl;
Max Gurtovoy9bad0402018-02-28 13:12:39 +02002051 struct nvme_rdma_device *ndev;
2052 bool found = false;
2053
2054 mutex_lock(&device_list_mutex);
2055 list_for_each_entry(ndev, &device_list, entry) {
2056 if (ndev->dev == ib_device) {
2057 found = true;
2058 break;
2059 }
2060 }
2061 mutex_unlock(&device_list_mutex);
2062
2063 if (!found)
2064 return;
Steve Wisee87a9112016-09-02 09:01:54 -07002065
2066 /* Delete all controllers using this device */
2067 mutex_lock(&nvme_rdma_ctrl_mutex);
2068 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2069 if (ctrl->device->dev != ib_device)
2070 continue;
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002071 nvme_delete_ctrl(&ctrl->ctrl);
Steve Wisee87a9112016-09-02 09:01:54 -07002072 }
2073 mutex_unlock(&nvme_rdma_ctrl_mutex);
2074
Roy Shtermanb227c592018-01-14 12:39:02 +02002075 flush_workqueue(nvme_delete_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002076}
2077
2078static struct ib_client nvme_rdma_ib_client = {
2079 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002080 .remove = nvme_rdma_remove_one
2081};
2082
Christoph Hellwig71102302016-07-06 21:55:52 +09002083static int __init nvme_rdma_init_module(void)
2084{
Steve Wisee87a9112016-09-02 09:01:54 -07002085 int ret;
2086
Steve Wisee87a9112016-09-02 09:01:54 -07002087 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002088 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002089 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002090
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002091 ret = nvmf_register_transport(&nvme_rdma_transport);
2092 if (ret)
2093 goto err_unreg_client;
2094
2095 return 0;
2096
2097err_unreg_client:
2098 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002099 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002100}
2101
2102static void __exit nvme_rdma_cleanup_module(void)
2103{
Christoph Hellwig71102302016-07-06 21:55:52 +09002104 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002105 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002106}
2107
2108module_init(nvme_rdma_init_module);
2109module_exit(nvme_rdma_cleanup_module);
2110
2111MODULE_LICENSE("GPL v2");