blob: e1824c2e0a1c0e90be05a0c6cce072eb50a0c3a6 [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
216 for (i = 0; i < ib_queue_size; i++) {
217 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
218 goto out_free_ring;
219 }
220
221 return ring;
222
223out_free_ring:
224 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
225 return NULL;
226}
227
228static void nvme_rdma_qp_event(struct ib_event *event, void *context)
229{
Max Gurtovoy27a4bee2016-11-23 11:38:48 +0200230 pr_debug("QP event %s (%d)\n",
231 ib_event_msg(event->event), event->event);
232
Christoph Hellwig71102302016-07-06 21:55:52 +0900233}
234
235static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
236{
Bart Van Assche35da77d2018-10-08 14:28:54 -0700237 int ret;
238
239 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
Christoph Hellwig71102302016-07-06 21:55:52 +0900240 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
Bart Van Assche35da77d2018-10-08 14:28:54 -0700241 if (ret < 0)
242 return ret;
243 if (ret == 0)
244 return -ETIMEDOUT;
245 WARN_ON_ONCE(queue->cm_error > 0);
Christoph Hellwig71102302016-07-06 21:55:52 +0900246 return queue->cm_error;
247}
248
249static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
250{
251 struct nvme_rdma_device *dev = queue->device;
252 struct ib_qp_init_attr init_attr;
253 int ret;
254
255 memset(&init_attr, 0, sizeof(init_attr));
256 init_attr.event_handler = nvme_rdma_qp_event;
257 /* +1 for drain */
258 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
259 /* +1 for drain */
260 init_attr.cap.max_recv_wr = queue->queue_size + 1;
261 init_attr.cap.max_recv_sge = 1;
Steve Wise64a741c2018-06-20 07:15:05 -0700262 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
Christoph Hellwig71102302016-07-06 21:55:52 +0900263 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
264 init_attr.qp_type = IB_QPT_RC;
265 init_attr.send_cq = queue->ib_cq;
266 init_attr.recv_cq = queue->ib_cq;
267
268 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
269
270 queue->qp = queue->cm_id->qp;
271 return ret;
272}
273
Christoph Hellwig385475e2017-06-13 09:15:19 +0200274static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
275 struct request *rq, unsigned int hctx_idx)
Christoph Hellwig71102302016-07-06 21:55:52 +0900276{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200277 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900278 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200279 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900280 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
281 struct nvme_rdma_device *dev = queue->device;
282
Christoph Hellwig71102302016-07-06 21:55:52 +0900283 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
284 DMA_TO_DEVICE);
285}
286
Christoph Hellwig385475e2017-06-13 09:15:19 +0200287static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
288 struct request *rq, unsigned int hctx_idx,
289 unsigned int numa_node)
Christoph Hellwig71102302016-07-06 21:55:52 +0900290{
Christoph Hellwig385475e2017-06-13 09:15:19 +0200291 struct nvme_rdma_ctrl *ctrl = set->driver_data;
Christoph Hellwig71102302016-07-06 21:55:52 +0900292 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig385475e2017-06-13 09:15:19 +0200293 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900294 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
295 struct nvme_rdma_device *dev = queue->device;
296 struct ib_device *ibdev = dev->dev;
297 int ret;
298
Sagi Grimberg59e29ce2018-06-29 16:50:00 -0600299 nvme_req(rq)->ctrl = &ctrl->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +0900300 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
301 DMA_TO_DEVICE);
302 if (ret)
303 return ret;
304
Christoph Hellwig71102302016-07-06 21:55:52 +0900305 req->queue = queue;
306
307 return 0;
Christoph Hellwig71102302016-07-06 21:55:52 +0900308}
309
Christoph Hellwig71102302016-07-06 21:55:52 +0900310static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
311 unsigned int hctx_idx)
312{
313 struct nvme_rdma_ctrl *ctrl = data;
314 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
315
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300316 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
Christoph Hellwig71102302016-07-06 21:55:52 +0900317
318 hctx->driver_data = queue;
319 return 0;
320}
321
322static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
323 unsigned int hctx_idx)
324{
325 struct nvme_rdma_ctrl *ctrl = data;
326 struct nvme_rdma_queue *queue = &ctrl->queues[0];
327
328 BUG_ON(hctx_idx != 0);
329
330 hctx->driver_data = queue;
331 return 0;
332}
333
334static void nvme_rdma_free_dev(struct kref *ref)
335{
336 struct nvme_rdma_device *ndev =
337 container_of(ref, struct nvme_rdma_device, ref);
338
339 mutex_lock(&device_list_mutex);
340 list_del(&ndev->entry);
341 mutex_unlock(&device_list_mutex);
342
Christoph Hellwig71102302016-07-06 21:55:52 +0900343 ib_dealloc_pd(ndev->pd);
Christoph Hellwig71102302016-07-06 21:55:52 +0900344 kfree(ndev);
345}
346
347static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
348{
349 kref_put(&dev->ref, nvme_rdma_free_dev);
350}
351
352static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
353{
354 return kref_get_unless_zero(&dev->ref);
355}
356
357static struct nvme_rdma_device *
358nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
359{
360 struct nvme_rdma_device *ndev;
361
362 mutex_lock(&device_list_mutex);
363 list_for_each_entry(ndev, &device_list, entry) {
364 if (ndev->dev->node_guid == cm_id->device->node_guid &&
365 nvme_rdma_dev_get(ndev))
366 goto out_unlock;
367 }
368
369 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
370 if (!ndev)
371 goto out_err;
372
373 ndev->dev = cm_id->device;
374 kref_init(&ndev->ref);
375
Christoph Hellwig11975e02016-09-05 12:56:20 +0200376 ndev->pd = ib_alloc_pd(ndev->dev,
377 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
Christoph Hellwig71102302016-07-06 21:55:52 +0900378 if (IS_ERR(ndev->pd))
379 goto out_free_dev;
380
Christoph Hellwig71102302016-07-06 21:55:52 +0900381 if (!(ndev->dev->attrs.device_cap_flags &
382 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
383 dev_err(&ndev->dev->dev,
384 "Memory registrations not supported.\n");
Christoph Hellwig11975e02016-09-05 12:56:20 +0200385 goto out_free_pd;
Christoph Hellwig71102302016-07-06 21:55:52 +0900386 }
387
Steve Wise64a741c2018-06-20 07:15:05 -0700388 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
Jason Gunthorpe0a3173a2018-08-16 14:13:03 -0600389 ndev->dev->attrs.max_send_sge - 1);
Christoph Hellwig71102302016-07-06 21:55:52 +0900390 list_add(&ndev->entry, &device_list);
391out_unlock:
392 mutex_unlock(&device_list_mutex);
393 return ndev;
394
Christoph Hellwig71102302016-07-06 21:55:52 +0900395out_free_pd:
396 ib_dealloc_pd(ndev->pd);
397out_free_dev:
398 kfree(ndev);
399out_err:
400 mutex_unlock(&device_list_mutex);
401 return NULL;
402}
403
404static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
405{
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200406 struct nvme_rdma_device *dev;
407 struct ib_device *ibdev;
408
409 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
410 return;
411
412 dev = queue->device;
413 ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900414
Israel Rukshinf41725b2017-11-26 10:40:55 +0000415 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
416
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200417 /*
418 * The cm_id object might have been destroyed during RDMA connection
419 * establishment error flow to avoid getting other cma events, thus
420 * the destruction of the QP shouldn't use rdma_cm API.
421 */
422 ib_destroy_qp(queue->qp);
Christoph Hellwig71102302016-07-06 21:55:52 +0900423 ib_free_cq(queue->ib_cq);
424
425 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
426 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
427
428 nvme_rdma_dev_put(dev);
429}
430
Israel Rukshinf41725b2017-11-26 10:40:55 +0000431static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
432{
433 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
434 ibdev->attrs.max_fast_reg_page_list_len);
435}
436
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300437static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
Christoph Hellwig71102302016-07-06 21:55:52 +0900438{
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300439 struct ib_device *ibdev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900440 const int send_wr_factor = 3; /* MR, SEND, INV */
441 const int cq_factor = send_wr_factor + 1; /* + RECV */
442 int comp_vector, idx = nvme_rdma_queue_idx(queue);
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800443 enum ib_poll_context poll_ctx;
Christoph Hellwig71102302016-07-06 21:55:52 +0900444 int ret;
445
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300446 queue->device = nvme_rdma_find_get_device(queue->cm_id);
447 if (!queue->device) {
448 dev_err(queue->cm_id->device->dev.parent,
449 "no client data found!\n");
450 return -ECONNREFUSED;
451 }
452 ibdev = queue->device->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900453
454 /*
Sagi Grimberg0b366582017-07-13 11:09:44 +0300455 * Spread I/O queues completion vectors according their queue index.
456 * Admin queues can always go on completion vector 0.
Christoph Hellwig71102302016-07-06 21:55:52 +0900457 */
Sagi Grimberg0b366582017-07-13 11:09:44 +0300458 comp_vector = idx == 0 ? idx : idx - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +0900459
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800460 /* Polling queues need direct cq polling context */
461 if (nvme_rdma_poll_queue(queue))
462 poll_ctx = IB_POLL_DIRECT;
463 else
464 poll_ctx = IB_POLL_SOFTIRQ;
465
Christoph Hellwig71102302016-07-06 21:55:52 +0900466 /* +1 for ib_stop_cq */
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300467 queue->ib_cq = ib_alloc_cq(ibdev, queue,
468 cq_factor * queue->queue_size + 1,
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800469 comp_vector, poll_ctx);
Christoph Hellwig71102302016-07-06 21:55:52 +0900470 if (IS_ERR(queue->ib_cq)) {
471 ret = PTR_ERR(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300472 goto out_put_dev;
Christoph Hellwig71102302016-07-06 21:55:52 +0900473 }
474
475 ret = nvme_rdma_create_qp(queue, send_wr_factor);
476 if (ret)
477 goto out_destroy_ib_cq;
478
479 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
480 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
481 if (!queue->rsp_ring) {
482 ret = -ENOMEM;
483 goto out_destroy_qp;
484 }
485
Israel Rukshinf41725b2017-11-26 10:40:55 +0000486 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
487 queue->queue_size,
488 IB_MR_TYPE_MEM_REG,
489 nvme_rdma_get_max_fr_pages(ibdev));
490 if (ret) {
491 dev_err(queue->ctrl->ctrl.device,
492 "failed to initialize MR pool sized %d for QID %d\n",
493 queue->queue_size, idx);
494 goto out_destroy_ring;
495 }
496
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200497 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
498
Christoph Hellwig71102302016-07-06 21:55:52 +0900499 return 0;
500
Israel Rukshinf41725b2017-11-26 10:40:55 +0000501out_destroy_ring:
502 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
503 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
Christoph Hellwig71102302016-07-06 21:55:52 +0900504out_destroy_qp:
Max Gurtovoy1f61def2017-11-06 16:18:51 +0200505 rdma_destroy_qp(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900506out_destroy_ib_cq:
507 ib_free_cq(queue->ib_cq);
Sagi Grimbergca6e95b2017-05-04 13:33:09 +0300508out_put_dev:
509 nvme_rdma_dev_put(queue->device);
Christoph Hellwig71102302016-07-06 21:55:52 +0900510 return ret;
511}
512
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300513static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +0900514 int idx, size_t queue_size)
515{
516 struct nvme_rdma_queue *queue;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200517 struct sockaddr *src_addr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900518 int ret;
519
520 queue = &ctrl->queues[idx];
521 queue->ctrl = ctrl;
522 init_completion(&queue->cm_done);
523
524 if (idx > 0)
525 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
526 else
527 queue->cmnd_capsule_len = sizeof(struct nvme_command);
528
529 queue->queue_size = queue_size;
530
531 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
532 RDMA_PS_TCP, IB_QPT_RC);
533 if (IS_ERR(queue->cm_id)) {
534 dev_info(ctrl->ctrl.device,
535 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
536 return PTR_ERR(queue->cm_id);
537 }
538
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200539 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200540 src_addr = (struct sockaddr *)&ctrl->src_addr;
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +0200541
Sagi Grimberg0928f9b2017-02-05 21:49:32 +0200542 queue->cm_error = -ETIMEDOUT;
543 ret = rdma_resolve_addr(queue->cm_id, src_addr,
544 (struct sockaddr *)&ctrl->addr,
Christoph Hellwig71102302016-07-06 21:55:52 +0900545 NVME_RDMA_CONNECT_TIMEOUT_MS);
546 if (ret) {
547 dev_info(ctrl->ctrl.device,
548 "rdma_resolve_addr failed (%d).\n", ret);
549 goto out_destroy_cm_id;
550 }
551
552 ret = nvme_rdma_wait_for_cm(queue);
553 if (ret) {
554 dev_info(ctrl->ctrl.device,
Sagi Grimbergd8bfcee2017-10-11 15:29:07 +0300555 "rdma connection establishment failed (%d)\n", ret);
Christoph Hellwig71102302016-07-06 21:55:52 +0900556 goto out_destroy_cm_id;
557 }
558
Sagi Grimberg5013e982017-10-11 15:29:12 +0300559 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
Christoph Hellwig71102302016-07-06 21:55:52 +0900560
561 return 0;
562
563out_destroy_cm_id:
564 rdma_destroy_id(queue->cm_id);
Max Gurtovoyeb1bd242017-11-28 18:28:44 +0200565 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +0900566 return ret;
567}
568
569static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
570{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200571 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
572 return;
573
Christoph Hellwig71102302016-07-06 21:55:52 +0900574 rdma_disconnect(queue->cm_id);
575 ib_drain_qp(queue->qp);
576}
577
578static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
579{
Sagi Grimberg5013e982017-10-11 15:29:12 +0300580 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
Christoph Hellwig71102302016-07-06 21:55:52 +0900581 return;
Sagi Grimberga57bd542017-08-28 21:41:10 +0200582
583 nvme_rdma_destroy_queue_ib(queue);
584 rdma_destroy_id(queue->cm_id);
Christoph Hellwig71102302016-07-06 21:55:52 +0900585}
586
587static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
588{
589 int i;
590
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300591 for (i = 1; i < ctrl->ctrl.queue_count; i++)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200592 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900593}
594
Sagi Grimberga57bd542017-08-28 21:41:10 +0200595static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
596{
597 int i;
598
599 for (i = 1; i < ctrl->ctrl.queue_count; i++)
600 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900601}
602
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300603static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
604{
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800605 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
606 bool poll = nvme_rdma_poll_queue(queue);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300607 int ret;
608
609 if (idx)
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800610 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300611 else
612 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
613
614 if (!ret)
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800615 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300616 else
617 dev_info(ctrl->ctrl.device,
618 "failed to connect queue: %d ret=%d\n", idx, ret);
619 return ret;
620}
621
622static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900623{
624 int i, ret = 0;
625
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300626 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300627 ret = nvme_rdma_start_queue(ctrl, i);
628 if (ret)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200629 goto out_stop_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900630 }
631
Steve Wisec8dbc372016-11-08 09:16:02 -0800632 return 0;
633
Sagi Grimberga57bd542017-08-28 21:41:10 +0200634out_stop_queues:
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300635 for (i--; i >= 1; i--)
636 nvme_rdma_stop_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900637 return ret;
638}
639
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300640static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
Christoph Hellwig71102302016-07-06 21:55:52 +0900641{
Sagi Grimbergc248c642017-03-09 13:26:07 +0200642 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg0b366582017-07-13 11:09:44 +0300643 struct ib_device *ibdev = ctrl->device->dev;
Sagi Grimbergc248c642017-03-09 13:26:07 +0200644 unsigned int nr_io_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900645 int i, ret;
646
Sagi Grimbergc248c642017-03-09 13:26:07 +0200647 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
Sagi Grimberg0b366582017-07-13 11:09:44 +0300648
649 /*
650 * we map queues according to the device irq vectors for
651 * optimal locality so we don't need more queues than
652 * completion vectors.
653 */
654 nr_io_queues = min_t(unsigned int, nr_io_queues,
655 ibdev->num_comp_vectors);
656
Sagi Grimbergb1064d32019-01-18 16:43:24 -0800657 if (opts->nr_write_queues) {
658 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
659 min(opts->nr_write_queues, nr_io_queues);
660 nr_io_queues += ctrl->io_queues[HCTX_TYPE_DEFAULT];
661 } else {
662 ctrl->io_queues[HCTX_TYPE_DEFAULT] = nr_io_queues;
663 }
664
665 ctrl->io_queues[HCTX_TYPE_READ] = nr_io_queues;
666
667 if (opts->nr_poll_queues) {
668 ctrl->io_queues[HCTX_TYPE_POLL] =
669 min(opts->nr_poll_queues, num_online_cpus());
670 nr_io_queues += ctrl->io_queues[HCTX_TYPE_POLL];
671 }
Sagi Grimbergb65bb772018-12-11 23:38:58 -0800672
Sagi Grimbergc248c642017-03-09 13:26:07 +0200673 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
674 if (ret)
675 return ret;
676
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300677 ctrl->ctrl.queue_count = nr_io_queues + 1;
678 if (ctrl->ctrl.queue_count < 2)
Sagi Grimbergc248c642017-03-09 13:26:07 +0200679 return 0;
680
681 dev_info(ctrl->ctrl.device,
682 "creating %d I/O queues.\n", nr_io_queues);
683
Sagi Grimbergd858e5f2017-04-24 10:58:29 +0300684 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300685 ret = nvme_rdma_alloc_queue(ctrl, i,
686 ctrl->ctrl.sqsize + 1);
687 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +0900688 goto out_free_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +0900689 }
690
691 return 0;
692
693out_free_queues:
Steve Wisef361e5a2016-09-02 09:01:27 -0700694 for (i--; i >= 1; i--)
Sagi Grimberga57bd542017-08-28 21:41:10 +0200695 nvme_rdma_free_queue(&ctrl->queues[i]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900696
697 return ret;
698}
699
Sagi Grimberg60070c72017-10-11 15:29:06 +0300700static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
701 struct blk_mq_tag_set *set)
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300702{
703 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300704
705 blk_mq_free_tag_set(set);
706 nvme_rdma_dev_put(ctrl->device);
707}
708
709static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
710 bool admin)
711{
712 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
713 struct blk_mq_tag_set *set;
714 int ret;
715
716 if (admin) {
717 set = &ctrl->admin_tag_set;
718 memset(set, 0, sizeof(*set));
719 set->ops = &nvme_rdma_admin_mq_ops;
Keith Busch38dabe22017-11-07 15:13:10 -0700720 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300721 set->reserved_tags = 2; /* connect + keep-alive */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100722 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300723 set->cmd_size = sizeof(struct nvme_rdma_request) +
724 SG_CHUNK_SIZE * sizeof(struct scatterlist);
725 set->driver_data = ctrl;
726 set->nr_hw_queues = 1;
727 set->timeout = ADMIN_TIMEOUT;
Israel Rukshin94f29d42017-10-18 12:38:24 +0000728 set->flags = BLK_MQ_F_NO_SCHED;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300729 } else {
730 set = &ctrl->tag_set;
731 memset(set, 0, sizeof(*set));
732 set->ops = &nvme_rdma_mq_ops;
Sagi Grimberg5e77d612018-06-19 15:34:13 +0300733 set->queue_depth = nctrl->sqsize + 1;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300734 set->reserved_tags = 1; /* fabric connect */
Hannes Reinecke103e5152018-11-16 09:22:29 +0100735 set->numa_node = nctrl->numa_node;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300736 set->flags = BLK_MQ_F_SHOULD_MERGE;
737 set->cmd_size = sizeof(struct nvme_rdma_request) +
738 SG_CHUNK_SIZE * sizeof(struct scatterlist);
739 set->driver_data = ctrl;
740 set->nr_hw_queues = nctrl->queue_count - 1;
741 set->timeout = NVME_IO_TIMEOUT;
Sagi Grimbergff8519f2018-12-14 11:06:10 -0800742 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Sagi Grimbergb28a3082017-07-10 09:22:30 +0300743 }
744
745 ret = blk_mq_alloc_tag_set(set);
746 if (ret)
747 goto out;
748
749 /*
750 * We need a reference on the device as long as the tag_set is alive,
751 * as the MRs in the request structures need a valid ib_device.
752 */
753 ret = nvme_rdma_dev_get(ctrl->device);
754 if (!ret) {
755 ret = -EINVAL;
756 goto out_free_tagset;
757 }
758
759 return set;
760
761out_free_tagset:
762 blk_mq_free_tag_set(set);
763out:
764 return ERR_PTR(ret);
765}
766
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300767static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
768 bool remove)
Christoph Hellwig71102302016-07-06 21:55:52 +0900769{
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300770 if (remove) {
771 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300772 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300773 }
Sagi Grimberg682630f2018-06-25 20:58:17 +0300774 if (ctrl->async_event_sqe.data) {
775 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
776 sizeof(struct nvme_command), DMA_TO_DEVICE);
777 ctrl->async_event_sqe.data = NULL;
778 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200779 nvme_rdma_free_queue(&ctrl->queues[0]);
Christoph Hellwig71102302016-07-06 21:55:52 +0900780}
781
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300782static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
783 bool new)
Sagi Grimberg90af3512017-07-10 09:22:28 +0300784{
785 int error;
786
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300787 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300788 if (error)
789 return error;
790
791 ctrl->device = ctrl->queues[0].device;
Hannes Reinecke103e5152018-11-16 09:22:29 +0100792 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300793
Israel Rukshinf41725b2017-11-26 10:40:55 +0000794 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300795
Sagi Grimberg94e42212018-06-19 15:34:10 +0300796 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
797 sizeof(struct nvme_command), DMA_TO_DEVICE);
798 if (error)
799 goto out_free_queue;
800
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300801 if (new) {
802 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300803 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
804 error = PTR_ERR(ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300805 goto out_free_async_qe;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300806 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300807
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300808 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
809 if (IS_ERR(ctrl->ctrl.admin_q)) {
810 error = PTR_ERR(ctrl->ctrl.admin_q);
811 goto out_free_tagset;
812 }
Sagi Grimberg90af3512017-07-10 09:22:28 +0300813 }
814
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300815 error = nvme_rdma_start_queue(ctrl, 0);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300816 if (error)
817 goto out_cleanup_queue;
818
Sagi Grimberg09fdc232017-07-10 09:22:39 +0300819 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
Sagi Grimberg90af3512017-07-10 09:22:28 +0300820 &ctrl->ctrl.cap);
821 if (error) {
822 dev_err(ctrl->ctrl.device,
823 "prop_get NVME_REG_CAP failed\n");
Jianchao Wang2e050f02018-05-24 09:27:38 +0800824 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300825 }
826
827 ctrl->ctrl.sqsize =
828 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
829
830 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
831 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800832 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300833
834 ctrl->ctrl.max_hw_sectors =
Linus Torvalds126e76f2017-09-09 12:49:01 -0700835 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300836
837 error = nvme_init_identify(&ctrl->ctrl);
838 if (error)
Jianchao Wang2e050f02018-05-24 09:27:38 +0800839 goto out_stop_queue;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300840
Sagi Grimberg90af3512017-07-10 09:22:28 +0300841 return 0;
842
Jianchao Wang2e050f02018-05-24 09:27:38 +0800843out_stop_queue:
844 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300845out_cleanup_queue:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300846 if (new)
847 blk_cleanup_queue(ctrl->ctrl.admin_q);
Sagi Grimberg90af3512017-07-10 09:22:28 +0300848out_free_tagset:
Sagi Grimberg3f02fff2017-07-10 09:22:32 +0300849 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300850 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
Sagi Grimberg94e42212018-06-19 15:34:10 +0300851out_free_async_qe:
852 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
853 sizeof(struct nvme_command), DMA_TO_DEVICE);
Prabhath Sajeepa6344d022018-11-28 11:11:29 -0700854 ctrl->async_event_sqe.data = NULL;
Sagi Grimberg90af3512017-07-10 09:22:28 +0300855out_free_queue:
856 nvme_rdma_free_queue(&ctrl->queues[0]);
857 return error;
858}
859
Sagi Grimberga57bd542017-08-28 21:41:10 +0200860static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
861 bool remove)
862{
Sagi Grimberga57bd542017-08-28 21:41:10 +0200863 if (remove) {
864 blk_cleanup_queue(ctrl->ctrl.connect_q);
Sagi Grimberg60070c72017-10-11 15:29:06 +0300865 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200866 }
867 nvme_rdma_free_io_queues(ctrl);
868}
869
870static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
871{
872 int ret;
873
Sagi Grimberg41e8cfa2017-07-10 09:22:36 +0300874 ret = nvme_rdma_alloc_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200875 if (ret)
876 return ret;
877
878 if (new) {
879 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300880 if (IS_ERR(ctrl->ctrl.tagset)) {
881 ret = PTR_ERR(ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200882 goto out_free_io_queues;
Sagi Grimbergf04b9cc2017-10-19 18:10:53 +0300883 }
Sagi Grimberga57bd542017-08-28 21:41:10 +0200884
885 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
886 if (IS_ERR(ctrl->ctrl.connect_q)) {
887 ret = PTR_ERR(ctrl->ctrl.connect_q);
888 goto out_free_tag_set;
889 }
890 } else {
Sagi Grimberga57bd542017-08-28 21:41:10 +0200891 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
892 ctrl->ctrl.queue_count - 1);
893 }
894
Sagi Grimberg68e16fc2017-07-10 09:22:37 +0300895 ret = nvme_rdma_start_io_queues(ctrl);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200896 if (ret)
897 goto out_cleanup_connect_q;
898
899 return 0;
900
901out_cleanup_connect_q:
902 if (new)
903 blk_cleanup_queue(ctrl->ctrl.connect_q);
904out_free_tag_set:
905 if (new)
Sagi Grimberg60070c72017-10-11 15:29:06 +0300906 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
Sagi Grimberga57bd542017-08-28 21:41:10 +0200907out_free_io_queues:
908 nvme_rdma_free_io_queues(ctrl);
909 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +0900910}
911
Sagi Grimberg75862c72018-07-09 12:49:07 +0300912static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
913 bool remove)
914{
915 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
916 nvme_rdma_stop_queue(&ctrl->queues[0]);
Sagi Grimberg10077092019-04-24 11:53:18 -0700917 if (ctrl->ctrl.admin_tagset)
918 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
919 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +0300920 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
921 nvme_rdma_destroy_admin_queue(ctrl, remove);
922}
923
924static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
925 bool remove)
926{
927 if (ctrl->ctrl.queue_count > 1) {
928 nvme_stop_queues(&ctrl->ctrl);
929 nvme_rdma_stop_io_queues(ctrl);
Sagi Grimberg10077092019-04-24 11:53:18 -0700930 if (ctrl->ctrl.tagset)
931 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
932 nvme_cancel_request, &ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +0300933 if (remove)
934 nvme_start_queues(&ctrl->ctrl);
935 nvme_rdma_destroy_io_queues(ctrl, remove);
936 }
937}
938
Christoph Hellwig71102302016-07-06 21:55:52 +0900939static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
940{
941 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
942
943 if (list_empty(&ctrl->list))
944 goto free_ctrl;
945
946 mutex_lock(&nvme_rdma_ctrl_mutex);
947 list_del(&ctrl->list);
948 mutex_unlock(&nvme_rdma_ctrl_mutex);
949
Christoph Hellwig71102302016-07-06 21:55:52 +0900950 nvmf_free_options(nctrl->opts);
951free_ctrl:
Sagi Grimberg3d064102018-06-19 15:34:09 +0300952 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +0900953 kfree(ctrl);
954}
955
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200956static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
957{
958 /* If we are resetting/deleting then do nothing */
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200959 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200960 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
961 ctrl->ctrl.state == NVME_CTRL_LIVE);
962 return;
963 }
964
965 if (nvmf_should_reconnect(&ctrl->ctrl)) {
966 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
967 ctrl->ctrl.opts->reconnect_delay);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +0200968 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200969 ctrl->ctrl.opts->reconnect_delay * HZ);
970 } else {
Sagi Grimberg12fa1302017-10-29 14:21:01 +0200971 nvme_delete_ctrl(&ctrl->ctrl);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +0200972 }
973}
974
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300975static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
Christoph Hellwig71102302016-07-06 21:55:52 +0900976{
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300977 int ret = -EINVAL;
Christoph Hellwig71102302016-07-06 21:55:52 +0900978 bool changed;
Christoph Hellwig71102302016-07-06 21:55:52 +0900979
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300980 ret = nvme_rdma_configure_admin_queue(ctrl, new);
Christoph Hellwig71102302016-07-06 21:55:52 +0900981 if (ret)
Sagi Grimbergc66e2992018-07-09 12:49:06 +0300982 return ret;
983
984 if (ctrl->ctrl.icdoff) {
985 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
986 goto destroy_admin;
987 }
988
989 if (!(ctrl->ctrl.sgls & (1 << 2))) {
990 dev_err(ctrl->ctrl.device,
991 "Mandatory keyed sgls are not supported!\n");
992 goto destroy_admin;
993 }
994
995 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
996 dev_warn(ctrl->ctrl.device,
997 "queue_size %zu > ctrl sqsize %u, clamping down\n",
998 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
999 }
1000
1001 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1002 dev_warn(ctrl->ctrl.device,
1003 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1004 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1005 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1006 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001007
Steve Wise64a741c2018-06-20 07:15:05 -07001008 if (ctrl->ctrl.sgls & (1 << 20))
1009 ctrl->use_inline_data = true;
Christoph Hellwig71102302016-07-06 21:55:52 +09001010
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001011 if (ctrl->ctrl.queue_count > 1) {
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001012 ret = nvme_rdma_configure_io_queues(ctrl, new);
Christoph Hellwig71102302016-07-06 21:55:52 +09001013 if (ret)
Sagi Grimberg5e1fe612017-10-11 15:29:11 +03001014 goto destroy_admin;
Christoph Hellwig71102302016-07-06 21:55:52 +09001015 }
1016
1017 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
Sagi Grimberg0a960afd2017-09-21 17:01:37 +03001018 if (!changed) {
1019 /* state change failure is ok if we're in DELETING state */
1020 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001021 ret = -EINVAL;
1022 goto destroy_io;
Sagi Grimberg0a960afd2017-09-21 17:01:37 +03001023 }
1024
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001025 nvme_start_ctrl(&ctrl->ctrl);
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001026 return 0;
1027
1028destroy_io:
1029 if (ctrl->ctrl.queue_count > 1)
1030 nvme_rdma_destroy_io_queues(ctrl, new);
1031destroy_admin:
1032 nvme_rdma_stop_queue(&ctrl->queues[0]);
1033 nvme_rdma_destroy_admin_queue(ctrl, new);
1034 return ret;
1035}
1036
1037static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1038{
1039 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1040 struct nvme_rdma_ctrl, reconnect_work);
1041
1042 ++ctrl->ctrl.nr_reconnects;
1043
1044 if (nvme_rdma_setup_ctrl(ctrl, false))
1045 goto requeue;
Christoph Hellwig71102302016-07-06 21:55:52 +09001046
Sagi Grimberg5e1fe612017-10-11 15:29:11 +03001047 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1048 ctrl->ctrl.nr_reconnects);
1049
1050 ctrl->ctrl.nr_reconnects = 0;
Christoph Hellwig71102302016-07-06 21:55:52 +09001051
1052 return;
1053
Christoph Hellwig71102302016-07-06 21:55:52 +09001054requeue:
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001055 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
Sagi Grimbergfdf9dfa2017-05-04 13:33:15 +03001056 ctrl->ctrl.nr_reconnects);
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001057 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001058}
1059
1060static void nvme_rdma_error_recovery_work(struct work_struct *work)
1061{
1062 struct nvme_rdma_ctrl *ctrl = container_of(work,
1063 struct nvme_rdma_ctrl, err_work);
1064
Sagi Grimberge4d753d2017-09-21 17:01:38 +03001065 nvme_stop_keep_alive(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001066 nvme_rdma_teardown_io_queues(ctrl, false);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001067 nvme_start_queues(&ctrl->ctrl);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001068 nvme_rdma_teardown_admin_queue(ctrl, false);
Sagi Grimberge818a5b2017-06-05 20:35:56 +03001069
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001070 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Nitzan Carmi187c0832018-03-20 11:07:29 +00001071 /* state change failure is ok if we're in DELETING state */
1072 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001073 return;
1074 }
1075
Sagi Grimbergfd8563c2017-03-18 20:58:29 +02001076 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001077}
1078
1079static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1080{
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001081 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
Christoph Hellwig71102302016-07-06 21:55:52 +09001082 return;
1083
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02001084 queue_work(nvme_wq, &ctrl->err_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001085}
1086
1087static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1088 const char *op)
1089{
1090 struct nvme_rdma_queue *queue = cq->cq_context;
1091 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1092
1093 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1094 dev_info(ctrl->ctrl.device,
1095 "%s for CQE 0x%p failed with status %s (%d)\n",
1096 op, wc->wr_cqe,
1097 ib_wc_status_msg(wc->status), wc->status);
1098 nvme_rdma_error_recovery(ctrl);
1099}
1100
1101static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1102{
1103 if (unlikely(wc->status != IB_WC_SUCCESS))
1104 nvme_rdma_wr_error(cq, wc, "MEMREG");
1105}
1106
1107static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1108{
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001109 struct nvme_rdma_request *req =
1110 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1111 struct request *rq = blk_mq_rq_from_pdu(req);
1112
1113 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001114 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001115 return;
1116 }
1117
1118 if (refcount_dec_and_test(&req->ref))
1119 nvme_end_request(rq, req->status, req->result);
1120
Christoph Hellwig71102302016-07-06 21:55:52 +09001121}
1122
1123static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1124 struct nvme_rdma_request *req)
1125{
Christoph Hellwig71102302016-07-06 21:55:52 +09001126 struct ib_send_wr wr = {
1127 .opcode = IB_WR_LOCAL_INV,
1128 .next = NULL,
1129 .num_sge = 0,
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001130 .send_flags = IB_SEND_SIGNALED,
Christoph Hellwig71102302016-07-06 21:55:52 +09001131 .ex.invalidate_rkey = req->mr->rkey,
1132 };
1133
1134 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1135 wr.wr_cqe = &req->reg_cqe;
1136
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001137 return ib_post_send(queue->qp, &wr, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001138}
1139
1140static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1141 struct request *rq)
1142{
1143 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001144 struct nvme_rdma_device *dev = queue->device;
1145 struct ib_device *ibdev = dev->dev;
Christoph Hellwig71102302016-07-06 21:55:52 +09001146
Chaitanya Kulkarni34e08192019-02-20 20:13:34 -08001147 if (!blk_rq_nr_phys_segments(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001148 return;
1149
Israel Rukshinf41725b2017-11-26 10:40:55 +00001150 if (req->mr) {
1151 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1152 req->mr = NULL;
1153 }
1154
Christoph Hellwig71102302016-07-06 21:55:52 +09001155 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1156 req->nents, rq_data_dir(rq) ==
1157 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1158
1159 nvme_cleanup_cmd(rq);
1160 sg_free_table_chained(&req->sg_table, true);
1161}
1162
1163static int nvme_rdma_set_sg_null(struct nvme_command *c)
1164{
1165 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1166
1167 sg->addr = 0;
1168 put_unaligned_le24(0, sg->length);
1169 put_unaligned_le32(0, sg->key);
1170 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1171 return 0;
1172}
1173
1174static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
Steve Wise64a741c2018-06-20 07:15:05 -07001175 struct nvme_rdma_request *req, struct nvme_command *c,
1176 int count)
Christoph Hellwig71102302016-07-06 21:55:52 +09001177{
1178 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
Steve Wise64a741c2018-06-20 07:15:05 -07001179 struct scatterlist *sgl = req->sg_table.sgl;
1180 struct ib_sge *sge = &req->sge[1];
1181 u32 len = 0;
1182 int i;
Christoph Hellwig71102302016-07-06 21:55:52 +09001183
Steve Wise64a741c2018-06-20 07:15:05 -07001184 for (i = 0; i < count; i++, sgl++, sge++) {
1185 sge->addr = sg_dma_address(sgl);
1186 sge->length = sg_dma_len(sgl);
1187 sge->lkey = queue->device->pd->local_dma_lkey;
1188 len += sge->length;
1189 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001190
1191 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
Steve Wise64a741c2018-06-20 07:15:05 -07001192 sg->length = cpu_to_le32(len);
Christoph Hellwig71102302016-07-06 21:55:52 +09001193 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1194
Steve Wise64a741c2018-06-20 07:15:05 -07001195 req->num_sge += count;
Christoph Hellwig71102302016-07-06 21:55:52 +09001196 return 0;
1197}
1198
1199static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1200 struct nvme_rdma_request *req, struct nvme_command *c)
1201{
1202 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1203
1204 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1205 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
Christoph Hellwig11975e02016-09-05 12:56:20 +02001206 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
Christoph Hellwig71102302016-07-06 21:55:52 +09001207 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1208 return 0;
1209}
1210
1211static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1212 struct nvme_rdma_request *req, struct nvme_command *c,
1213 int count)
1214{
1215 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1216 int nr;
1217
Israel Rukshinf41725b2017-11-26 10:40:55 +00001218 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1219 if (WARN_ON_ONCE(!req->mr))
1220 return -EAGAIN;
1221
Max Gurtovoyb925a2d2017-08-28 12:52:27 +03001222 /*
1223 * Align the MR to a 4K page size to match the ctrl page size and
1224 * the block virtual boundary.
1225 */
1226 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001227 if (unlikely(nr < count)) {
Israel Rukshinf41725b2017-11-26 10:40:55 +00001228 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1229 req->mr = NULL;
Christoph Hellwig71102302016-07-06 21:55:52 +09001230 if (nr < 0)
1231 return nr;
1232 return -EINVAL;
1233 }
1234
1235 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1236
1237 req->reg_cqe.done = nvme_rdma_memreg_done;
1238 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1239 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1240 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1241 req->reg_wr.wr.num_sge = 0;
1242 req->reg_wr.mr = req->mr;
1243 req->reg_wr.key = req->mr->rkey;
1244 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1245 IB_ACCESS_REMOTE_READ |
1246 IB_ACCESS_REMOTE_WRITE;
1247
Christoph Hellwig71102302016-07-06 21:55:52 +09001248 sg->addr = cpu_to_le64(req->mr->iova);
1249 put_unaligned_le24(req->mr->length, sg->length);
1250 put_unaligned_le32(req->mr->rkey, sg->key);
1251 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1252 NVME_SGL_FMT_INVALIDATE;
1253
1254 return 0;
1255}
1256
1257static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
Christoph Hellwigb131c612017-01-13 12:29:12 +01001258 struct request *rq, struct nvme_command *c)
Christoph Hellwig71102302016-07-06 21:55:52 +09001259{
1260 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1261 struct nvme_rdma_device *dev = queue->device;
1262 struct ib_device *ibdev = dev->dev;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001263 int count, ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001264
1265 req->num_sge = 1;
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001266 refcount_set(&req->ref, 2); /* send and recv completions */
Christoph Hellwig71102302016-07-06 21:55:52 +09001267
1268 c->common.flags |= NVME_CMD_SGL_METABUF;
1269
Chaitanya Kulkarni34e08192019-02-20 20:13:34 -08001270 if (!blk_rq_nr_phys_segments(rq))
Christoph Hellwig71102302016-07-06 21:55:52 +09001271 return nvme_rdma_set_sg_null(c);
1272
1273 req->sg_table.sgl = req->first_sgl;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001274 ret = sg_alloc_table_chained(&req->sg_table,
1275 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001276 if (ret)
1277 return -ENOMEM;
1278
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001279 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001280
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07001281 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
Christoph Hellwig71102302016-07-06 21:55:52 +09001282 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1283 if (unlikely(count <= 0)) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001284 ret = -EIO;
1285 goto out_free_table;
Christoph Hellwig71102302016-07-06 21:55:52 +09001286 }
1287
Steve Wise64a741c2018-06-20 07:15:05 -07001288 if (count <= dev->num_inline_segments) {
Christoph Hellwigb131c612017-01-13 12:29:12 +01001289 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
Steve Wise64a741c2018-06-20 07:15:05 -07001290 queue->ctrl->use_inline_data &&
Christoph Hellwigb131c612017-01-13 12:29:12 +01001291 blk_rq_payload_bytes(rq) <=
Max Gurtovoy94423a82018-06-10 16:58:29 +03001292 nvme_rdma_inline_data_size(queue)) {
Steve Wise64a741c2018-06-20 07:15:05 -07001293 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
Max Gurtovoy94423a82018-06-10 16:58:29 +03001294 goto out;
1295 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001296
Steve Wise64a741c2018-06-20 07:15:05 -07001297 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
Max Gurtovoy94423a82018-06-10 16:58:29 +03001298 ret = nvme_rdma_map_sg_single(queue, req, c);
1299 goto out;
1300 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001301 }
1302
Max Gurtovoy94423a82018-06-10 16:58:29 +03001303 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1304out:
1305 if (unlikely(ret))
1306 goto out_unmap_sg;
1307
1308 return 0;
1309
1310out_unmap_sg:
1311 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1312 req->nents, rq_data_dir(rq) ==
1313 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1314out_free_table:
1315 sg_free_table_chained(&req->sg_table, true);
1316 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001317}
1318
1319static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1320{
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001321 struct nvme_rdma_qe *qe =
1322 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1323 struct nvme_rdma_request *req =
1324 container_of(qe, struct nvme_rdma_request, sqe);
1325 struct request *rq = blk_mq_rq_from_pdu(req);
1326
1327 if (unlikely(wc->status != IB_WC_SUCCESS)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001328 nvme_rdma_wr_error(cq, wc, "SEND");
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001329 return;
1330 }
1331
1332 if (refcount_dec_and_test(&req->ref))
1333 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001334}
1335
1336static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1337 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001338 struct ib_send_wr *first)
Christoph Hellwig71102302016-07-06 21:55:52 +09001339{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001340 struct ib_send_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001341 int ret;
1342
1343 sge->addr = qe->dma;
1344 sge->length = sizeof(struct nvme_command),
1345 sge->lkey = queue->device->pd->local_dma_lkey;
1346
Christoph Hellwig71102302016-07-06 21:55:52 +09001347 wr.next = NULL;
1348 wr.wr_cqe = &qe->cqe;
1349 wr.sg_list = sge;
1350 wr.num_sge = num_sge;
1351 wr.opcode = IB_WR_SEND;
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001352 wr.send_flags = IB_SEND_SIGNALED;
Christoph Hellwig71102302016-07-06 21:55:52 +09001353
1354 if (first)
1355 first->next = &wr;
1356 else
1357 first = &wr;
1358
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001359 ret = ib_post_send(queue->qp, first, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001360 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001361 dev_err(queue->ctrl->ctrl.device,
1362 "%s failed with error code %d\n", __func__, ret);
1363 }
1364 return ret;
1365}
1366
1367static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1368 struct nvme_rdma_qe *qe)
1369{
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001370 struct ib_recv_wr wr;
Christoph Hellwig71102302016-07-06 21:55:52 +09001371 struct ib_sge list;
1372 int ret;
1373
1374 list.addr = qe->dma;
1375 list.length = sizeof(struct nvme_completion);
1376 list.lkey = queue->device->pd->local_dma_lkey;
1377
1378 qe->cqe.done = nvme_rdma_recv_done;
1379
1380 wr.next = NULL;
1381 wr.wr_cqe = &qe->cqe;
1382 wr.sg_list = &list;
1383 wr.num_sge = 1;
1384
Bart Van Assche45e3cc1a2018-07-18 09:25:23 -07001385 ret = ib_post_recv(queue->qp, &wr, NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001386 if (unlikely(ret)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001387 dev_err(queue->ctrl->ctrl.device,
1388 "%s failed with error code %d\n", __func__, ret);
1389 }
1390 return ret;
1391}
1392
1393static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1394{
1395 u32 queue_idx = nvme_rdma_queue_idx(queue);
1396
1397 if (queue_idx == 0)
1398 return queue->ctrl->admin_tag_set.tags[queue_idx];
1399 return queue->ctrl->tag_set.tags[queue_idx - 1];
1400}
1401
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001402static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1403{
1404 if (unlikely(wc->status != IB_WC_SUCCESS))
1405 nvme_rdma_wr_error(cq, wc, "ASYNC");
1406}
1407
Keith Buschad22c352017-11-07 15:13:12 -07001408static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
Christoph Hellwig71102302016-07-06 21:55:52 +09001409{
1410 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1411 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1412 struct ib_device *dev = queue->device->dev;
1413 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1414 struct nvme_command *cmd = sqe->data;
1415 struct ib_sge sge;
1416 int ret;
1417
Christoph Hellwig71102302016-07-06 21:55:52 +09001418 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1419
1420 memset(cmd, 0, sizeof(*cmd));
1421 cmd->common.opcode = nvme_admin_async_event;
Keith Busch38dabe22017-11-07 15:13:10 -07001422 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
Christoph Hellwig71102302016-07-06 21:55:52 +09001423 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1424 nvme_rdma_set_sg_null(cmd);
1425
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001426 sqe->cqe.done = nvme_rdma_async_done;
1427
Christoph Hellwig71102302016-07-06 21:55:52 +09001428 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1429 DMA_TO_DEVICE);
1430
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001431 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
Christoph Hellwig71102302016-07-06 21:55:52 +09001432 WARN_ON_ONCE(ret);
1433}
1434
Jens Axboe1052b8a2018-11-26 08:21:49 -07001435static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1436 struct nvme_completion *cqe, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001437{
Christoph Hellwig71102302016-07-06 21:55:52 +09001438 struct request *rq;
1439 struct nvme_rdma_request *req;
Christoph Hellwig71102302016-07-06 21:55:52 +09001440
Christoph Hellwig71102302016-07-06 21:55:52 +09001441 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1442 if (!rq) {
1443 dev_err(queue->ctrl->ctrl.device,
1444 "tag 0x%x on QP %#x not found\n",
1445 cqe->command_id, queue->qp->qp_num);
1446 nvme_rdma_error_recovery(queue->ctrl);
Jens Axboe1052b8a2018-11-26 08:21:49 -07001447 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001448 }
1449 req = blk_mq_rq_to_pdu(rq);
1450
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001451 req->status = cqe->status;
1452 req->result = cqe->result;
Christoph Hellwig71102302016-07-06 21:55:52 +09001453
Sagi Grimberg3ef02792017-11-23 17:35:24 +02001454 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1455 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1456 dev_err(queue->ctrl->ctrl.device,
1457 "Bogus remote invalidation for rkey %#x\n",
1458 req->mr->rkey);
1459 nvme_rdma_error_recovery(queue->ctrl);
1460 }
Israel Rukshinf41725b2017-11-26 10:40:55 +00001461 } else if (req->mr) {
Jens Axboe1052b8a2018-11-26 08:21:49 -07001462 int ret;
1463
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001464 ret = nvme_rdma_inv_rkey(queue, req);
1465 if (unlikely(ret < 0)) {
1466 dev_err(queue->ctrl->ctrl.device,
1467 "Queueing INV WR for rkey %#x failed (%d)\n",
1468 req->mr->rkey, ret);
1469 nvme_rdma_error_recovery(queue->ctrl);
1470 }
1471 /* the local invalidation completion will end the request */
Jens Axboe1052b8a2018-11-26 08:21:49 -07001472 return;
Sagi Grimberg2f122e42017-11-23 17:35:23 +02001473 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001474
Jens Axboe1052b8a2018-11-26 08:21:49 -07001475 if (refcount_dec_and_test(&req->ref))
Sagi Grimberg4af7f7f2017-11-23 17:35:22 +02001476 nvme_end_request(rq, req->status, req->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001477}
1478
Jens Axboe1052b8a2018-11-26 08:21:49 -07001479static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
Christoph Hellwig71102302016-07-06 21:55:52 +09001480{
1481 struct nvme_rdma_qe *qe =
1482 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1483 struct nvme_rdma_queue *queue = cq->cq_context;
1484 struct ib_device *ibdev = queue->device->dev;
1485 struct nvme_completion *cqe = qe->data;
1486 const size_t len = sizeof(struct nvme_completion);
Christoph Hellwig71102302016-07-06 21:55:52 +09001487
1488 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1489 nvme_rdma_wr_error(cq, wc, "RECV");
Jens Axboe1052b8a2018-11-26 08:21:49 -07001490 return;
Christoph Hellwig71102302016-07-06 21:55:52 +09001491 }
1492
1493 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1494 /*
1495 * AEN requests are special as they don't time out and can
1496 * survive any kind of queue freeze and often don't respond to
1497 * aborts. We don't even bother to allocate a struct request
1498 * for them but rather special case them here.
1499 */
1500 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
Keith Busch38dabe22017-11-07 15:13:10 -07001501 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
Christoph Hellwig7bf58532016-11-10 07:32:34 -08001502 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1503 &cqe->result);
Christoph Hellwig71102302016-07-06 21:55:52 +09001504 else
Jens Axboe1052b8a2018-11-26 08:21:49 -07001505 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
Christoph Hellwig71102302016-07-06 21:55:52 +09001506 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1507
1508 nvme_rdma_post_recv(queue, qe);
Christoph Hellwig71102302016-07-06 21:55:52 +09001509}
1510
1511static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1512{
1513 int ret, i;
1514
1515 for (i = 0; i < queue->queue_size; i++) {
1516 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1517 if (ret)
1518 goto out_destroy_queue_ib;
1519 }
1520
1521 return 0;
1522
1523out_destroy_queue_ib:
1524 nvme_rdma_destroy_queue_ib(queue);
1525 return ret;
1526}
1527
1528static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1529 struct rdma_cm_event *ev)
1530{
Steve Wise7f039532016-10-26 12:36:47 -07001531 struct rdma_cm_id *cm_id = queue->cm_id;
1532 int status = ev->status;
1533 const char *rej_msg;
1534 const struct nvme_rdma_cm_rej *rej_data;
1535 u8 rej_data_len;
1536
1537 rej_msg = rdma_reject_msg(cm_id, status);
1538 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1539
1540 if (rej_data && rej_data_len >= sizeof(u16)) {
1541 u16 sts = le16_to_cpu(rej_data->sts);
Christoph Hellwig71102302016-07-06 21:55:52 +09001542
1543 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001544 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1545 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
Christoph Hellwig71102302016-07-06 21:55:52 +09001546 } else {
1547 dev_err(queue->ctrl->ctrl.device,
Steve Wise7f039532016-10-26 12:36:47 -07001548 "Connect rejected: status %d (%s).\n", status, rej_msg);
Christoph Hellwig71102302016-07-06 21:55:52 +09001549 }
1550
1551 return -ECONNRESET;
1552}
1553
1554static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1555{
Christoph Hellwig71102302016-07-06 21:55:52 +09001556 int ret;
1557
Sagi Grimbergca6e95b2017-05-04 13:33:09 +03001558 ret = nvme_rdma_create_queue_ib(queue);
1559 if (ret)
1560 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09001561
1562 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1563 if (ret) {
1564 dev_err(queue->ctrl->ctrl.device,
1565 "rdma_resolve_route failed (%d).\n",
1566 queue->cm_error);
1567 goto out_destroy_queue;
1568 }
1569
1570 return 0;
1571
1572out_destroy_queue:
1573 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001574 return ret;
1575}
1576
1577static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1578{
1579 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1580 struct rdma_conn_param param = { };
Roland Dreier0b857b42016-07-31 00:27:39 -07001581 struct nvme_rdma_cm_req priv = { };
Christoph Hellwig71102302016-07-06 21:55:52 +09001582 int ret;
1583
1584 param.qp_num = queue->qp->qp_num;
1585 param.flow_control = 1;
1586
1587 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
Sagi Grimberg2ac17c22016-06-22 15:06:00 +03001588 /* maximum retry count */
1589 param.retry_count = 7;
Christoph Hellwig71102302016-07-06 21:55:52 +09001590 param.rnr_retry_count = 7;
1591 param.private_data = &priv;
1592 param.private_data_len = sizeof(priv);
1593
1594 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1595 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001596 /*
1597 * set the admin queue depth to the minimum size
1598 * specified by the Fabrics standard.
1599 */
1600 if (priv.qid == 0) {
Sagi Grimberg7aa1f422017-06-18 16:15:59 +03001601 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1602 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001603 } else {
Jay Freyenseec5af8652016-08-17 15:00:27 -07001604 /*
1605 * current interpretation of the fabrics spec
1606 * is at minimum you make hrqsize sqsize+1, or a
1607 * 1's based representation of sqsize.
1608 */
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001609 priv.hrqsize = cpu_to_le16(queue->queue_size);
Jay Freyenseec5af8652016-08-17 15:00:27 -07001610 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
Jay Freyenseef994d9d2016-08-17 15:00:26 -07001611 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001612
1613 ret = rdma_connect(queue->cm_id, &param);
1614 if (ret) {
1615 dev_err(ctrl->ctrl.device,
1616 "rdma_connect failed (%d).\n", ret);
1617 goto out_destroy_queue_ib;
1618 }
1619
1620 return 0;
1621
1622out_destroy_queue_ib:
1623 nvme_rdma_destroy_queue_ib(queue);
1624 return ret;
1625}
1626
Christoph Hellwig71102302016-07-06 21:55:52 +09001627static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1628 struct rdma_cm_event *ev)
1629{
1630 struct nvme_rdma_queue *queue = cm_id->context;
1631 int cm_error = 0;
1632
1633 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1634 rdma_event_msg(ev->event), ev->event,
1635 ev->status, cm_id);
1636
1637 switch (ev->event) {
1638 case RDMA_CM_EVENT_ADDR_RESOLVED:
1639 cm_error = nvme_rdma_addr_resolved(queue);
1640 break;
1641 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1642 cm_error = nvme_rdma_route_resolved(queue);
1643 break;
1644 case RDMA_CM_EVENT_ESTABLISHED:
1645 queue->cm_error = nvme_rdma_conn_established(queue);
1646 /* complete cm_done regardless of success/failure */
1647 complete(&queue->cm_done);
1648 return 0;
1649 case RDMA_CM_EVENT_REJECTED:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001650 nvme_rdma_destroy_queue_ib(queue);
Christoph Hellwig71102302016-07-06 21:55:52 +09001651 cm_error = nvme_rdma_conn_rejected(queue, ev);
1652 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001653 case RDMA_CM_EVENT_ROUTE_ERROR:
1654 case RDMA_CM_EVENT_CONNECT_ERROR:
1655 case RDMA_CM_EVENT_UNREACHABLE:
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001656 nvme_rdma_destroy_queue_ib(queue);
Gustavo A. R. Silva249090f2018-07-05 08:14:00 -05001657 /* fall through */
Sagi Grimbergabf87d52017-05-04 13:33:10 +03001658 case RDMA_CM_EVENT_ADDR_ERROR:
Christoph Hellwig71102302016-07-06 21:55:52 +09001659 dev_dbg(queue->ctrl->ctrl.device,
1660 "CM error event %d\n", ev->event);
1661 cm_error = -ECONNRESET;
1662 break;
1663 case RDMA_CM_EVENT_DISCONNECTED:
1664 case RDMA_CM_EVENT_ADDR_CHANGE:
1665 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1666 dev_dbg(queue->ctrl->ctrl.device,
1667 "disconnect received - connection closed\n");
1668 nvme_rdma_error_recovery(queue->ctrl);
1669 break;
1670 case RDMA_CM_EVENT_DEVICE_REMOVAL:
Steve Wisee87a9112016-09-02 09:01:54 -07001671 /* device removal is handled via the ib_client API */
1672 break;
Christoph Hellwig71102302016-07-06 21:55:52 +09001673 default:
1674 dev_err(queue->ctrl->ctrl.device,
1675 "Unexpected RDMA CM event (%d)\n", ev->event);
1676 nvme_rdma_error_recovery(queue->ctrl);
1677 break;
1678 }
1679
1680 if (cm_error) {
1681 queue->cm_error = cm_error;
1682 complete(&queue->cm_done);
1683 }
1684
1685 return 0;
1686}
1687
1688static enum blk_eh_timer_return
1689nvme_rdma_timeout(struct request *rq, bool reserved)
1690{
1691 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001692 struct nvme_rdma_queue *queue = req->queue;
1693 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001694
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001695 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1696 rq->tag, nvme_rdma_queue_idx(queue));
Nitzan Carmie62a5382017-10-22 09:37:04 +00001697
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001698 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1699 /*
1700 * Teardown immediately if controller times out while starting
1701 * or we are already started error recovery. all outstanding
1702 * requests are completed on shutdown, so we return BLK_EH_DONE.
1703 */
1704 flush_work(&ctrl->err_work);
1705 nvme_rdma_teardown_io_queues(ctrl, false);
1706 nvme_rdma_teardown_admin_queue(ctrl, false);
1707 return BLK_EH_DONE;
1708 }
Christoph Hellwig71102302016-07-06 21:55:52 +09001709
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001710 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1711 nvme_rdma_error_recovery(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001712
Sagi Grimberg4c174e62019-01-08 00:53:22 -08001713 return BLK_EH_RESET_TIMER;
Christoph Hellwig71102302016-07-06 21:55:52 +09001714}
1715
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001716static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001717 const struct blk_mq_queue_data *bd)
1718{
1719 struct nvme_ns *ns = hctx->queue->queuedata;
1720 struct nvme_rdma_queue *queue = hctx->driver_data;
1721 struct request *rq = bd->rq;
1722 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1723 struct nvme_rdma_qe *sqe = &req->sqe;
1724 struct nvme_command *c = sqe->data;
Christoph Hellwig71102302016-07-06 21:55:52 +09001725 struct ib_device *dev;
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02001726 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001727 blk_status_t ret;
1728 int err;
Christoph Hellwig71102302016-07-06 21:55:52 +09001729
1730 WARN_ON_ONCE(rq->tag < 0);
1731
Christoph Hellwig3bc32bb2018-06-11 17:34:06 +02001732 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
James Smart6cdefc62018-07-20 15:49:48 -07001733 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
Christoph Hellwig553cd9e2016-11-02 08:49:18 -06001734
Christoph Hellwig71102302016-07-06 21:55:52 +09001735 dev = queue->device->dev;
1736 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1737 sizeof(struct nvme_command), DMA_TO_DEVICE);
1738
1739 ret = nvme_setup_cmd(ns, rq, c);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001740 if (ret)
Christoph Hellwig71102302016-07-06 21:55:52 +09001741 return ret;
1742
Christoph Hellwig71102302016-07-06 21:55:52 +09001743 blk_mq_start_request(rq);
1744
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001745 err = nvme_rdma_map_data(queue, rq, c);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001746 if (unlikely(err < 0)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001747 dev_err(queue->ctrl->ctrl.device,
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001748 "Failed to map data (%d)\n", err);
Christoph Hellwig71102302016-07-06 21:55:52 +09001749 nvme_cleanup_cmd(rq);
1750 goto err;
1751 }
1752
Sagi Grimbergb4b591c2017-11-23 17:35:21 +02001753 sqe->cqe.done = nvme_rdma_send_done;
1754
Christoph Hellwig71102302016-07-06 21:55:52 +09001755 ib_dma_sync_single_for_device(dev, sqe->dma,
1756 sizeof(struct nvme_command), DMA_TO_DEVICE);
1757
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001758 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
Israel Rukshinf41725b2017-11-26 10:40:55 +00001759 req->mr ? &req->reg_wr.wr : NULL);
Max Gurtovoya7b7c7a2017-08-14 15:29:26 +03001760 if (unlikely(err)) {
Christoph Hellwig71102302016-07-06 21:55:52 +09001761 nvme_rdma_unmap_data(queue, rq);
1762 goto err;
1763 }
1764
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001765 return BLK_STS_OK;
Christoph Hellwig71102302016-07-06 21:55:52 +09001766err:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001767 if (err == -ENOMEM || err == -EAGAIN)
1768 return BLK_STS_RESOURCE;
1769 return BLK_STS_IOERR;
Christoph Hellwig71102302016-07-06 21:55:52 +09001770}
1771
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001772static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
1773{
1774 struct nvme_rdma_queue *queue = hctx->driver_data;
1775
1776 return ib_process_cq_direct(queue->ib_cq, -1);
1777}
1778
Christoph Hellwig71102302016-07-06 21:55:52 +09001779static void nvme_rdma_complete_rq(struct request *rq)
1780{
1781 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001782
Christoph Hellwig77f02a72017-03-30 13:41:32 +02001783 nvme_rdma_unmap_data(req->queue, rq);
1784 nvme_complete_rq(rq);
Christoph Hellwig71102302016-07-06 21:55:52 +09001785}
1786
Sagi Grimberg0b366582017-07-13 11:09:44 +03001787static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1788{
1789 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1790
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001791 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
Sagi Grimbergb1064d32019-01-18 16:43:24 -08001792 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1793 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1794 set->map[HCTX_TYPE_READ].nr_queues = ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001795 if (ctrl->ctrl.opts->nr_write_queues) {
1796 /* separate read/write queues */
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001797 set->map[HCTX_TYPE_READ].queue_offset =
Sagi Grimbergb1064d32019-01-18 16:43:24 -08001798 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001799 } else {
1800 /* mixed read/write queues */
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001801 set->map[HCTX_TYPE_READ].queue_offset = 0;
1802 }
1803 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
1804 ctrl->device->dev, 0);
1805 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
1806 ctrl->device->dev, 0);
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001807
1808 if (ctrl->ctrl.opts->nr_poll_queues) {
1809 set->map[HCTX_TYPE_POLL].nr_queues =
Sagi Grimbergb1064d32019-01-18 16:43:24 -08001810 ctrl->io_queues[HCTX_TYPE_POLL];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001811 set->map[HCTX_TYPE_POLL].queue_offset =
Sagi Grimbergb1064d32019-01-18 16:43:24 -08001812 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001813 if (ctrl->ctrl.opts->nr_write_queues)
1814 set->map[HCTX_TYPE_POLL].queue_offset +=
Sagi Grimbergb1064d32019-01-18 16:43:24 -08001815 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001816 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
1817 }
Sagi Grimbergb65bb772018-12-11 23:38:58 -08001818 return 0;
Sagi Grimberg0b366582017-07-13 11:09:44 +03001819}
1820
Eric Biggersf363b082017-03-30 13:39:16 -07001821static const struct blk_mq_ops nvme_rdma_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001822 .queue_rq = nvme_rdma_queue_rq,
1823 .complete = nvme_rdma_complete_rq,
Christoph Hellwig71102302016-07-06 21:55:52 +09001824 .init_request = nvme_rdma_init_request,
1825 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001826 .init_hctx = nvme_rdma_init_hctx,
Christoph Hellwig71102302016-07-06 21:55:52 +09001827 .timeout = nvme_rdma_timeout,
Sagi Grimberg0b366582017-07-13 11:09:44 +03001828 .map_queues = nvme_rdma_map_queues,
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001829 .poll = nvme_rdma_poll,
Christoph Hellwig71102302016-07-06 21:55:52 +09001830};
1831
Eric Biggersf363b082017-03-30 13:39:16 -07001832static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
Christoph Hellwig71102302016-07-06 21:55:52 +09001833 .queue_rq = nvme_rdma_queue_rq,
1834 .complete = nvme_rdma_complete_rq,
Christoph Hellwig385475e2017-06-13 09:15:19 +02001835 .init_request = nvme_rdma_init_request,
1836 .exit_request = nvme_rdma_exit_request,
Christoph Hellwig71102302016-07-06 21:55:52 +09001837 .init_hctx = nvme_rdma_init_admin_hctx,
1838 .timeout = nvme_rdma_timeout,
1839};
1840
Sagi Grimberg18398af2017-07-10 09:22:31 +03001841static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001842{
Sagi Grimberg794a4cb2019-01-01 00:19:30 -08001843 cancel_work_sync(&ctrl->err_work);
1844 cancel_delayed_work_sync(&ctrl->reconnect_work);
1845
Sagi Grimberg75862c72018-07-09 12:49:07 +03001846 nvme_rdma_teardown_io_queues(ctrl, shutdown);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001847 if (shutdown)
Christoph Hellwig71102302016-07-06 21:55:52 +09001848 nvme_shutdown_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001849 else
1850 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
Sagi Grimberg75862c72018-07-09 12:49:07 +03001851 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
Christoph Hellwig71102302016-07-06 21:55:52 +09001852}
1853
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001854static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
Sagi Grimberg2461a8d2016-07-24 09:29:51 +03001855{
Christoph Hellwige9bc2582017-10-29 10:44:30 +02001856 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001857}
1858
Christoph Hellwig71102302016-07-06 21:55:52 +09001859static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1860{
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001861 struct nvme_rdma_ctrl *ctrl =
1862 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001863
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03001864 nvme_stop_ctrl(&ctrl->ctrl);
Sagi Grimberg18398af2017-07-10 09:22:31 +03001865 nvme_rdma_shutdown_ctrl(ctrl, false);
Christoph Hellwig71102302016-07-06 21:55:52 +09001866
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02001867 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergd5bf4b72017-12-21 14:54:15 +02001868 /* state change failure should never happen */
1869 WARN_ON_ONCE(1);
1870 return;
1871 }
1872
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001873 if (nvme_rdma_setup_ctrl(ctrl, false))
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001874 goto out_fail;
Christoph Hellwig71102302016-07-06 21:55:52 +09001875
Christoph Hellwig71102302016-07-06 21:55:52 +09001876 return;
1877
Sagi Grimberg370ae6e2017-07-10 09:22:38 +03001878out_fail:
Nitzan Carmi8000d1f2018-01-17 11:01:14 +00001879 ++ctrl->ctrl.nr_reconnects;
1880 nvme_rdma_reconnect_or_remove(ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09001881}
1882
Christoph Hellwig71102302016-07-06 21:55:52 +09001883static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1884 .name = "rdma",
1885 .module = THIS_MODULE,
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001886 .flags = NVME_F_FABRICS,
Christoph Hellwig71102302016-07-06 21:55:52 +09001887 .reg_read32 = nvmf_reg_read32,
1888 .reg_read64 = nvmf_reg_read64,
1889 .reg_write32 = nvmf_reg_write32,
Christoph Hellwig71102302016-07-06 21:55:52 +09001890 .free_ctrl = nvme_rdma_free_ctrl,
1891 .submit_async_event = nvme_rdma_submit_async_event,
Christoph Hellwigc5017e82017-10-29 10:44:29 +02001892 .delete_ctrl = nvme_rdma_delete_ctrl,
Christoph Hellwig71102302016-07-06 21:55:52 +09001893 .get_address = nvmf_get_address,
1894};
1895
James Smart36e835f2017-10-20 16:17:09 -07001896/*
1897 * Fails a connection request if it matches an existing controller
1898 * (association) with the same tuple:
1899 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1900 *
1901 * if local address is not specified in the request, it will match an
1902 * existing controller with all the other parameters the same and no
1903 * local port address specified as well.
1904 *
1905 * The ports don't need to be compared as they are intrinsically
1906 * already matched by the port pointers supplied.
1907 */
1908static bool
1909nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1910{
1911 struct nvme_rdma_ctrl *ctrl;
1912 bool found = false;
1913
1914 mutex_lock(&nvme_rdma_ctrl_mutex);
1915 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
Sagi Grimbergb7c7be6f62018-10-18 17:40:40 -07001916 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
James Smart36e835f2017-10-20 16:17:09 -07001917 if (found)
1918 break;
1919 }
1920 mutex_unlock(&nvme_rdma_ctrl_mutex);
1921
1922 return found;
1923}
1924
Christoph Hellwig71102302016-07-06 21:55:52 +09001925static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1926 struct nvmf_ctrl_options *opts)
1927{
1928 struct nvme_rdma_ctrl *ctrl;
1929 int ret;
1930 bool changed;
1931
1932 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1933 if (!ctrl)
1934 return ERR_PTR(-ENOMEM);
1935 ctrl->ctrl.opts = opts;
1936 INIT_LIST_HEAD(&ctrl->list);
1937
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001938 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
1939 opts->trsvcid =
1940 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
1941 if (!opts->trsvcid) {
1942 ret = -ENOMEM;
1943 goto out_free_ctrl;
1944 }
1945 opts->mask |= NVMF_OPT_TRSVCID;
1946 }
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001947
1948 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001949 opts->traddr, opts->trsvcid, &ctrl->addr);
Christoph Hellwig71102302016-07-06 21:55:52 +09001950 if (ret) {
Sagi Grimbergbb59b8e2018-10-19 00:50:29 -07001951 pr_err("malformed address passed: %s:%s\n",
1952 opts->traddr, opts->trsvcid);
Christoph Hellwig71102302016-07-06 21:55:52 +09001953 goto out_free_ctrl;
1954 }
1955
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001956 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001957 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1958 opts->host_traddr, NULL, &ctrl->src_addr);
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001959 if (ret) {
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001960 pr_err("malformed src address passed: %s\n",
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02001961 opts->host_traddr);
1962 goto out_free_ctrl;
1963 }
1964 }
1965
James Smart36e835f2017-10-20 16:17:09 -07001966 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1967 ret = -EALREADY;
1968 goto out_free_ctrl;
1969 }
1970
Christoph Hellwig71102302016-07-06 21:55:52 +09001971 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1972 nvme_rdma_reconnect_ctrl_work);
1973 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001974 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
Christoph Hellwig71102302016-07-06 21:55:52 +09001975
Sagi Grimbergff8519f2018-12-14 11:06:10 -08001976 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
1977 opts->nr_poll_queues + 1;
Jay Freyenseec5af8652016-08-17 15:00:27 -07001978 ctrl->ctrl.sqsize = opts->queue_size - 1;
Christoph Hellwig71102302016-07-06 21:55:52 +09001979 ctrl->ctrl.kato = opts->kato;
1980
1981 ret = -ENOMEM;
Sagi Grimbergd858e5f2017-04-24 10:58:29 +03001982 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Christoph Hellwig71102302016-07-06 21:55:52 +09001983 GFP_KERNEL);
1984 if (!ctrl->queues)
Sagi Grimberg3d064102018-06-19 15:34:09 +03001985 goto out_free_ctrl;
1986
1987 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1988 0 /* no quirks, we're perfect! */);
1989 if (ret)
1990 goto out_kfree_queues;
Christoph Hellwig71102302016-07-06 21:55:52 +09001991
Max Gurtovoyb754a322018-01-31 18:31:25 +02001992 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
1993 WARN_ON_ONCE(!changed);
1994
Sagi Grimbergc66e2992018-07-09 12:49:06 +03001995 ret = nvme_rdma_setup_ctrl(ctrl, true);
Christoph Hellwig71102302016-07-06 21:55:52 +09001996 if (ret)
Sagi Grimberg3d064102018-06-19 15:34:09 +03001997 goto out_uninit_ctrl;
Christoph Hellwig71102302016-07-06 21:55:52 +09001998
Sagi Grimberg0928f9b2017-02-05 21:49:32 +02001999 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
Christoph Hellwig71102302016-07-06 21:55:52 +09002000 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2001
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002002 nvme_get_ctrl(&ctrl->ctrl);
Christoph Hellwig71102302016-07-06 21:55:52 +09002003
2004 mutex_lock(&nvme_rdma_ctrl_mutex);
2005 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2006 mutex_unlock(&nvme_rdma_ctrl_mutex);
2007
Christoph Hellwig71102302016-07-06 21:55:52 +09002008 return &ctrl->ctrl;
2009
Christoph Hellwig71102302016-07-06 21:55:52 +09002010out_uninit_ctrl:
2011 nvme_uninit_ctrl(&ctrl->ctrl);
2012 nvme_put_ctrl(&ctrl->ctrl);
2013 if (ret > 0)
2014 ret = -EIO;
2015 return ERR_PTR(ret);
Sagi Grimberg3d064102018-06-19 15:34:09 +03002016out_kfree_queues:
2017 kfree(ctrl->queues);
Christoph Hellwig71102302016-07-06 21:55:52 +09002018out_free_ctrl:
2019 kfree(ctrl);
2020 return ERR_PTR(ret);
2021}
2022
2023static struct nvmf_transport_ops nvme_rdma_transport = {
2024 .name = "rdma",
Roy Shterman0de5cd32017-12-25 14:18:30 +02002025 .module = THIS_MODULE,
Christoph Hellwig71102302016-07-06 21:55:52 +09002026 .required_opts = NVMF_OPT_TRADDR,
Max Gurtovoy8f4e8da2017-02-19 20:08:03 +02002027 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
Sagi Grimbergb65bb772018-12-11 23:38:58 -08002028 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
Sagi Grimbergff8519f2018-12-14 11:06:10 -08002029 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES,
Christoph Hellwig71102302016-07-06 21:55:52 +09002030 .create_ctrl = nvme_rdma_create_ctrl,
2031};
2032
Steve Wisee87a9112016-09-02 09:01:54 -07002033static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2034{
2035 struct nvme_rdma_ctrl *ctrl;
Max Gurtovoy9bad0402018-02-28 13:12:39 +02002036 struct nvme_rdma_device *ndev;
2037 bool found = false;
2038
2039 mutex_lock(&device_list_mutex);
2040 list_for_each_entry(ndev, &device_list, entry) {
2041 if (ndev->dev == ib_device) {
2042 found = true;
2043 break;
2044 }
2045 }
2046 mutex_unlock(&device_list_mutex);
2047
2048 if (!found)
2049 return;
Steve Wisee87a9112016-09-02 09:01:54 -07002050
2051 /* Delete all controllers using this device */
2052 mutex_lock(&nvme_rdma_ctrl_mutex);
2053 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2054 if (ctrl->device->dev != ib_device)
2055 continue;
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002056 nvme_delete_ctrl(&ctrl->ctrl);
Steve Wisee87a9112016-09-02 09:01:54 -07002057 }
2058 mutex_unlock(&nvme_rdma_ctrl_mutex);
2059
Roy Shtermanb227c592018-01-14 12:39:02 +02002060 flush_workqueue(nvme_delete_wq);
Steve Wisee87a9112016-09-02 09:01:54 -07002061}
2062
2063static struct ib_client nvme_rdma_ib_client = {
2064 .name = "nvme_rdma",
Steve Wisee87a9112016-09-02 09:01:54 -07002065 .remove = nvme_rdma_remove_one
2066};
2067
Christoph Hellwig71102302016-07-06 21:55:52 +09002068static int __init nvme_rdma_init_module(void)
2069{
Steve Wisee87a9112016-09-02 09:01:54 -07002070 int ret;
2071
Steve Wisee87a9112016-09-02 09:01:54 -07002072 ret = ib_register_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002073 if (ret)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002074 return ret;
Steve Wisee87a9112016-09-02 09:01:54 -07002075
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002076 ret = nvmf_register_transport(&nvme_rdma_transport);
2077 if (ret)
2078 goto err_unreg_client;
2079
2080 return 0;
2081
2082err_unreg_client:
2083 ib_unregister_client(&nvme_rdma_ib_client);
Sagi Grimberga56c79c2017-03-19 06:21:42 +02002084 return ret;
Christoph Hellwig71102302016-07-06 21:55:52 +09002085}
2086
2087static void __exit nvme_rdma_cleanup_module(void)
2088{
Christoph Hellwig71102302016-07-06 21:55:52 +09002089 nvmf_unregister_transport(&nvme_rdma_transport);
Steve Wisee87a9112016-09-02 09:01:54 -07002090 ib_unregister_client(&nvme_rdma_ib_client);
Christoph Hellwig71102302016-07-06 21:55:52 +09002091}
2092
2093module_init(nvme_rdma_init_module);
2094module_exit(nvme_rdma_cleanup_module);
2095
2096MODULE_LICENSE("GPL v2");