rdma.c 51.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * NVMe over Fabrics RDMA host code.
 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
Israel Rukshin's avatar
Israel Rukshin committed
18
#include <rdma/mr_pool.h>
19 20 21 22
#include <linux/err.h>
#include <linux/string.h>
#include <linux/atomic.h>
#include <linux/blk-mq.h>
23
#include <linux/blk-mq-rdma.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <linux/types.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/scatterlist.h>
#include <linux/nvme.h>
#include <asm/unaligned.h>

#include <rdma/ib_verbs.h>
#include <rdma/rdma_cm.h>
#include <linux/nvme-rdma.h>

#include "nvme.h"
#include "fabrics.h"


39
#define NVME_RDMA_CONNECT_TIMEOUT_MS	3000		/* 3 second */
40 41 42

#define NVME_RDMA_MAX_SEGMENTS		256

43
#define NVME_RDMA_MAX_INLINE_SEGMENTS	4
44 45

struct nvme_rdma_device {
46 47
	struct ib_device	*dev;
	struct ib_pd		*pd;
48 49
	struct kref		ref;
	struct list_head	entry;
50
	unsigned int		num_inline_segments;
51 52 53 54 55 56 57 58 59 60
};

struct nvme_rdma_qe {
	struct ib_cqe		cqe;
	void			*data;
	u64			dma;
};

struct nvme_rdma_queue;
struct nvme_rdma_request {
61
	struct nvme_request	req;
62 63
	struct ib_mr		*mr;
	struct nvme_rdma_qe	sqe;
64 65 66
	union nvme_result	result;
	__le16			status;
	refcount_t		ref;
67 68 69 70 71 72 73 74 75 76 77
	struct ib_sge		sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
	u32			num_sge;
	int			nents;
	struct ib_reg_wr	reg_wr;
	struct ib_cqe		reg_cqe;
	struct nvme_rdma_queue  *queue;
	struct sg_table		sg_table;
	struct scatterlist	first_sgl[];
};

enum nvme_rdma_queue_flags {
78 79
	NVME_RDMA_Q_ALLOCATED		= 0,
	NVME_RDMA_Q_LIVE		= 1,
80
	NVME_RDMA_Q_TR_READY		= 2,
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
};

struct nvme_rdma_queue {
	struct nvme_rdma_qe	*rsp_ring;
	int			queue_size;
	size_t			cmnd_capsule_len;
	struct nvme_rdma_ctrl	*ctrl;
	struct nvme_rdma_device	*device;
	struct ib_cq		*ib_cq;
	struct ib_qp		*qp;

	unsigned long		flags;
	struct rdma_cm_id	*cm_id;
	int			cm_error;
	struct completion	cm_done;
};

struct nvme_rdma_ctrl {
	/* read only in the hot path */
	struct nvme_rdma_queue	*queues;

	/* other member variables */
	struct blk_mq_tag_set	tag_set;
	struct work_struct	err_work;

	struct nvme_rdma_qe	async_event_sqe;

	struct delayed_work	reconnect_work;

	struct list_head	list;

	struct blk_mq_tag_set	admin_tag_set;
	struct nvme_rdma_device	*device;

	u32			max_fr_pages;

117 118
	struct sockaddr_storage addr;
	struct sockaddr_storage src_addr;
119 120

	struct nvme_ctrl	ctrl;
121
	bool			use_inline_data;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
};

static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
{
	return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
}

static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_mutex);

static LIST_HEAD(nvme_rdma_ctrl_list);
static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);

/*
 * Disabling this option makes small I/O goes faster, but is fundamentally
 * unsafe.  With it turned off we will have to register a global rkey that
 * allows read and write access to all physical memory.
 */
static bool register_always = true;
module_param(register_always, bool, 0444);
MODULE_PARM_DESC(register_always,
	 "Use memory registration even for contiguous memory regions");

static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
		struct rdma_cm_event *event);
static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);

149 150 151
static const struct blk_mq_ops nvme_rdma_mq_ops;
static const struct blk_mq_ops nvme_rdma_admin_mq_ops;

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/* XXX: really should move to a generic header sooner or later.. */
static inline void put_unaligned_le24(u32 val, u8 *p)
{
	*p++ = val;
	*p++ = val >> 8;
	*p++ = val >> 16;
}

static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
{
	return queue - queue->ctrl->queues;
}

static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
{
	return queue->cmnd_capsule_len - sizeof(struct nvme_command);
}

static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
		size_t capsule_size, enum dma_data_direction dir)
{
	ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
	kfree(qe->data);
}

static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
		size_t capsule_size, enum dma_data_direction dir)
{
	qe->data = kzalloc(capsule_size, GFP_KERNEL);
	if (!qe->data)
		return -ENOMEM;

	qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
	if (ib_dma_mapping_error(ibdev, qe->dma)) {
		kfree(qe->data);
187
		qe->data = NULL;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		return -ENOMEM;
	}

	return 0;
}

static void nvme_rdma_free_ring(struct ib_device *ibdev,
		struct nvme_rdma_qe *ring, size_t ib_queue_size,
		size_t capsule_size, enum dma_data_direction dir)
{
	int i;

	for (i = 0; i < ib_queue_size; i++)
		nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
	kfree(ring);
}

static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
		size_t ib_queue_size, size_t capsule_size,
		enum dma_data_direction dir)
{
	struct nvme_rdma_qe *ring;
	int i;

	ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
	if (!ring)
		return NULL;

	for (i = 0; i < ib_queue_size; i++) {
		if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
			goto out_free_ring;
	}

	return ring;

out_free_ring:
	nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
	return NULL;
}

static void nvme_rdma_qp_event(struct ib_event *event, void *context)
{
230 231 232
	pr_debug("QP event %s (%d)\n",
		 ib_event_msg(event->event), event->event);

233 234 235 236
}

static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
{
237 238 239
	int ret;

	ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
240
			msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
241 242 243 244 245
	if (ret < 0)
		return ret;
	if (ret == 0)
		return -ETIMEDOUT;
	WARN_ON_ONCE(queue->cm_error > 0);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	return queue->cm_error;
}

static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
{
	struct nvme_rdma_device *dev = queue->device;
	struct ib_qp_init_attr init_attr;
	int ret;

	memset(&init_attr, 0, sizeof(init_attr));
	init_attr.event_handler = nvme_rdma_qp_event;
	/* +1 for drain */
	init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
	/* +1 for drain */
	init_attr.cap.max_recv_wr = queue->queue_size + 1;
	init_attr.cap.max_recv_sge = 1;
262
	init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
263 264 265 266 267 268 269 270 271 272 273
	init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
	init_attr.qp_type = IB_QPT_RC;
	init_attr.send_cq = queue->ib_cq;
	init_attr.recv_cq = queue->ib_cq;

	ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);

	queue->qp = queue->cm_id->qp;
	return ret;
}

274 275
static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
		struct request *rq, unsigned int hctx_idx)
276
{
277
	struct nvme_rdma_ctrl *ctrl = set->driver_data;
278
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
279
	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
280 281 282 283 284 285 286
	struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
	struct nvme_rdma_device *dev = queue->device;

	nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
			DMA_TO_DEVICE);
}

287 288 289
static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
		struct request *rq, unsigned int hctx_idx,
		unsigned int numa_node)
290
{
291
	struct nvme_rdma_ctrl *ctrl = set->driver_data;
292
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
293
	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
294 295 296 297 298
	struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
	struct nvme_rdma_device *dev = queue->device;
	struct ib_device *ibdev = dev->dev;
	int ret;

299
	nvme_req(rq)->ctrl = &ctrl->ctrl;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
			DMA_TO_DEVICE);
	if (ret)
		return ret;

	req->queue = queue;

	return 0;
}

static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
		unsigned int hctx_idx)
{
	struct nvme_rdma_ctrl *ctrl = data;
	struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];

316
	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

	hctx->driver_data = queue;
	return 0;
}

static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
		unsigned int hctx_idx)
{
	struct nvme_rdma_ctrl *ctrl = data;
	struct nvme_rdma_queue *queue = &ctrl->queues[0];

	BUG_ON(hctx_idx != 0);

	hctx->driver_data = queue;
	return 0;
}

static void nvme_rdma_free_dev(struct kref *ref)
{
	struct nvme_rdma_device *ndev =
		container_of(ref, struct nvme_rdma_device, ref);

	mutex_lock(&device_list_mutex);
	list_del(&ndev->entry);
	mutex_unlock(&device_list_mutex);

	ib_dealloc_pd(ndev->pd);
	kfree(ndev);
}

static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
{
	kref_put(&dev->ref, nvme_rdma_free_dev);
}

static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
{
	return kref_get_unless_zero(&dev->ref);
}

static struct nvme_rdma_device *
nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
{
	struct nvme_rdma_device *ndev;

	mutex_lock(&device_list_mutex);
	list_for_each_entry(ndev, &device_list, entry) {
		if (ndev->dev->node_guid == cm_id->device->node_guid &&
		    nvme_rdma_dev_get(ndev))
			goto out_unlock;
	}

	ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
	if (!ndev)
		goto out_err;

	ndev->dev = cm_id->device;
	kref_init(&ndev->ref);

376 377
	ndev->pd = ib_alloc_pd(ndev->dev,
		register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
378 379 380 381 382 383 384
	if (IS_ERR(ndev->pd))
		goto out_free_dev;

	if (!(ndev->dev->attrs.device_cap_flags &
	      IB_DEVICE_MEM_MGT_EXTENSIONS)) {
		dev_err(&ndev->dev->dev,
			"Memory registrations not supported.\n");
385
		goto out_free_pd;
386 387
	}

388
	ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
389
					ndev->dev->attrs.max_send_sge - 1);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	list_add(&ndev->entry, &device_list);
out_unlock:
	mutex_unlock(&device_list_mutex);
	return ndev;

out_free_pd:
	ib_dealloc_pd(ndev->pd);
out_free_dev:
	kfree(ndev);
out_err:
	mutex_unlock(&device_list_mutex);
	return NULL;
}

static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
{
406 407 408 409 410 411 412 413
	struct nvme_rdma_device *dev;
	struct ib_device *ibdev;

	if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
		return;

	dev = queue->device;
	ibdev = dev->dev;
414

Israel Rukshin's avatar
Israel Rukshin committed
415 416
	ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);

417 418 419 420 421 422
	/*
	 * The cm_id object might have been destroyed during RDMA connection
	 * establishment error flow to avoid getting other cma events, thus
	 * the destruction of the QP shouldn't use rdma_cm API.
	 */
	ib_destroy_qp(queue->qp);
423 424 425 426 427 428 429 430
	ib_free_cq(queue->ib_cq);

	nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
			sizeof(struct nvme_completion), DMA_FROM_DEVICE);

	nvme_rdma_dev_put(dev);
}

Israel Rukshin's avatar
Israel Rukshin committed
431 432 433 434 435 436
static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
{
	return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
		     ibdev->attrs.max_fast_reg_page_list_len);
}

437
static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
438
{
439
	struct ib_device *ibdev;
440 441 442 443 444
	const int send_wr_factor = 3;			/* MR, SEND, INV */
	const int cq_factor = send_wr_factor + 1;	/* + RECV */
	int comp_vector, idx = nvme_rdma_queue_idx(queue);
	int ret;

445 446 447 448 449 450 451
	queue->device = nvme_rdma_find_get_device(queue->cm_id);
	if (!queue->device) {
		dev_err(queue->cm_id->device->dev.parent,
			"no client data found!\n");
		return -ECONNREFUSED;
	}
	ibdev = queue->device->dev;
452 453

	/*
454 455
	 * Spread I/O queues completion vectors according their queue index.
	 * Admin queues can always go on completion vector 0.
456
	 */
457
	comp_vector = idx == 0 ? idx : idx - 1;
458 459

	/* +1 for ib_stop_cq */
460 461 462
	queue->ib_cq = ib_alloc_cq(ibdev, queue,
				cq_factor * queue->queue_size + 1,
				comp_vector, IB_POLL_SOFTIRQ);
463 464
	if (IS_ERR(queue->ib_cq)) {
		ret = PTR_ERR(queue->ib_cq);
465
		goto out_put_dev;
466 467 468 469 470 471 472 473 474 475 476 477 478
	}

	ret = nvme_rdma_create_qp(queue, send_wr_factor);
	if (ret)
		goto out_destroy_ib_cq;

	queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
			sizeof(struct nvme_completion), DMA_FROM_DEVICE);
	if (!queue->rsp_ring) {
		ret = -ENOMEM;
		goto out_destroy_qp;
	}

Israel Rukshin's avatar
Israel Rukshin committed
479 480 481 482 483 484 485 486 487 488 489
	ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
			      queue->queue_size,
			      IB_MR_TYPE_MEM_REG,
			      nvme_rdma_get_max_fr_pages(ibdev));
	if (ret) {
		dev_err(queue->ctrl->ctrl.device,
			"failed to initialize MR pool sized %d for QID %d\n",
			queue->queue_size, idx);
		goto out_destroy_ring;
	}

490 491
	set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);

492 493
	return 0;

Israel Rukshin's avatar
Israel Rukshin committed
494 495 496
out_destroy_ring:
	nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
			    sizeof(struct nvme_completion), DMA_FROM_DEVICE);
497
out_destroy_qp:
498
	rdma_destroy_qp(queue->cm_id);
499 500
out_destroy_ib_cq:
	ib_free_cq(queue->ib_cq);
501 502
out_put_dev:
	nvme_rdma_dev_put(queue->device);
503 504 505
	return ret;
}

506
static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
507 508 509
		int idx, size_t queue_size)
{
	struct nvme_rdma_queue *queue;
510
	struct sockaddr *src_addr = NULL;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	int ret;

	queue = &ctrl->queues[idx];
	queue->ctrl = ctrl;
	init_completion(&queue->cm_done);

	if (idx > 0)
		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
	else
		queue->cmnd_capsule_len = sizeof(struct nvme_command);

	queue->queue_size = queue_size;

	queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
			RDMA_PS_TCP, IB_QPT_RC);
	if (IS_ERR(queue->cm_id)) {
		dev_info(ctrl->ctrl.device,
			"failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
		return PTR_ERR(queue->cm_id);
	}

532
	if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
533
		src_addr = (struct sockaddr *)&ctrl->src_addr;
534

535 536 537
	queue->cm_error = -ETIMEDOUT;
	ret = rdma_resolve_addr(queue->cm_id, src_addr,
			(struct sockaddr *)&ctrl->addr,
538 539 540 541 542 543 544 545 546 547
			NVME_RDMA_CONNECT_TIMEOUT_MS);
	if (ret) {
		dev_info(ctrl->ctrl.device,
			"rdma_resolve_addr failed (%d).\n", ret);
		goto out_destroy_cm_id;
	}

	ret = nvme_rdma_wait_for_cm(queue);
	if (ret) {
		dev_info(ctrl->ctrl.device,
548
			"rdma connection establishment failed (%d)\n", ret);
549 550 551
		goto out_destroy_cm_id;
	}

552
	set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
553 554 555 556 557

	return 0;

out_destroy_cm_id:
	rdma_destroy_id(queue->cm_id);
558
	nvme_rdma_destroy_queue_ib(queue);
559 560 561 562 563
	return ret;
}

static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
{
564 565 566
	if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
		return;

567 568 569 570 571 572
	rdma_disconnect(queue->cm_id);
	ib_drain_qp(queue->qp);
}

static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
{
573
	if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
574 575
		return;

576 577 578 579
	nvme_rdma_destroy_queue_ib(queue);
	rdma_destroy_id(queue->cm_id);
}

580
static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
581
{
582 583 584 585
	int i;

	for (i = 1; i < ctrl->ctrl.queue_count; i++)
		nvme_rdma_free_queue(&ctrl->queues[i]);
586 587
}

588
static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
589 590 591
{
	int i;

592
	for (i = 1; i < ctrl->ctrl.queue_count; i++)
593
		nvme_rdma_stop_queue(&ctrl->queues[i]);
594 595
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
{
	int ret;

	if (idx)
		ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
	else
		ret = nvmf_connect_admin_queue(&ctrl->ctrl);

	if (!ret)
		set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
	else
		dev_info(ctrl->ctrl.device,
			"failed to connect queue: %d ret=%d\n", idx, ret);
	return ret;
}

static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
614 615 616
{
	int i, ret = 0;

617
	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
618 619
		ret = nvme_rdma_start_queue(ctrl, i);
		if (ret)
620
			goto out_stop_queues;
621 622
	}

623 624
	return 0;

625
out_stop_queues:
626 627
	for (i--; i >= 1; i--)
		nvme_rdma_stop_queue(&ctrl->queues[i]);
628 629 630
	return ret;
}

631
static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
632
{
633
	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
634
	struct ib_device *ibdev = ctrl->device->dev;
635
	unsigned int nr_io_queues;
636 637
	int i, ret;

638
	nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
639 640 641 642 643 644 645 646 647

	/*
	 * we map queues according to the device irq vectors for
	 * optimal locality so we don't need more queues than
	 * completion vectors.
	 */
	nr_io_queues = min_t(unsigned int, nr_io_queues,
				ibdev->num_comp_vectors);

648 649 650 651
	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
	if (ret)
		return ret;

652 653
	ctrl->ctrl.queue_count = nr_io_queues + 1;
	if (ctrl->ctrl.queue_count < 2)
654 655 656 657 658
		return 0;

	dev_info(ctrl->ctrl.device,
		"creating %d I/O queues.\n", nr_io_queues);

659
	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
660 661 662
		ret = nvme_rdma_alloc_queue(ctrl, i,
				ctrl->ctrl.sqsize + 1);
		if (ret)
663 664 665 666 667 668
			goto out_free_queues;
	}

	return 0;

out_free_queues:
669
	for (i--; i >= 1; i--)
670
		nvme_rdma_free_queue(&ctrl->queues[i]);
671 672 673 674

	return ret;
}

675 676
static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
		struct blk_mq_tag_set *set)
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);

	blk_mq_free_tag_set(set);
	nvme_rdma_dev_put(ctrl->device);
}

static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
		bool admin)
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
	struct blk_mq_tag_set *set;
	int ret;

	if (admin) {
		set = &ctrl->admin_tag_set;
		memset(set, 0, sizeof(*set));
		set->ops = &nvme_rdma_admin_mq_ops;
Keith Busch's avatar
Keith Busch committed
695
		set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
696 697 698 699 700 701 702
		set->reserved_tags = 2; /* connect + keep-alive */
		set->numa_node = NUMA_NO_NODE;
		set->cmd_size = sizeof(struct nvme_rdma_request) +
			SG_CHUNK_SIZE * sizeof(struct scatterlist);
		set->driver_data = ctrl;
		set->nr_hw_queues = 1;
		set->timeout = ADMIN_TIMEOUT;
703
		set->flags = BLK_MQ_F_NO_SCHED;
704 705 706 707
	} else {
		set = &ctrl->tag_set;
		memset(set, 0, sizeof(*set));
		set->ops = &nvme_rdma_mq_ops;
708
		set->queue_depth = nctrl->sqsize + 1;
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
		set->reserved_tags = 1; /* fabric connect */
		set->numa_node = NUMA_NO_NODE;
		set->flags = BLK_MQ_F_SHOULD_MERGE;
		set->cmd_size = sizeof(struct nvme_rdma_request) +
			SG_CHUNK_SIZE * sizeof(struct scatterlist);
		set->driver_data = ctrl;
		set->nr_hw_queues = nctrl->queue_count - 1;
		set->timeout = NVME_IO_TIMEOUT;
	}

	ret = blk_mq_alloc_tag_set(set);
	if (ret)
		goto out;

	/*
	 * We need a reference on the device as long as the tag_set is alive,
	 * as the MRs in the request structures need a valid ib_device.
	 */
	ret = nvme_rdma_dev_get(ctrl->device);
	if (!ret) {
		ret = -EINVAL;
		goto out_free_tagset;
	}

	return set;

out_free_tagset:
	blk_mq_free_tag_set(set);
out:
	return ERR_PTR(ret);
}

741 742
static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
		bool remove)
743
{
744 745
	if (remove) {
		blk_cleanup_queue(ctrl->ctrl.admin_q);
746
		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
747
	}
748 749 750 751 752
	if (ctrl->async_event_sqe.data) {
		nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
				sizeof(struct nvme_command), DMA_TO_DEVICE);
		ctrl->async_event_sqe.data = NULL;
	}
753
	nvme_rdma_free_queue(&ctrl->queues[0]);
754 755
}

756 757
static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
		bool new)
758 759 760
{
	int error;

761
	error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
762 763 764 765 766
	if (error)
		return error;

	ctrl->device = ctrl->queues[0].device;

Israel Rukshin's avatar
Israel Rukshin committed
767
	ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
768

769 770 771 772 773
	error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
			sizeof(struct nvme_command), DMA_TO_DEVICE);
	if (error)
		goto out_free_queue;

774 775
	if (new) {
		ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
776 777
		if (IS_ERR(ctrl->ctrl.admin_tagset)) {
			error = PTR_ERR(ctrl->ctrl.admin_tagset);
778
			goto out_free_async_qe;
779
		}
780

781 782 783 784 785
		ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
		if (IS_ERR(ctrl->ctrl.admin_q)) {
			error = PTR_ERR(ctrl->ctrl.admin_q);
			goto out_free_tagset;
		}
786 787
	}

788
	error = nvme_rdma_start_queue(ctrl, 0);
789 790 791
	if (error)
		goto out_cleanup_queue;

792
	error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
793 794 795 796
			&ctrl->ctrl.cap);
	if (error) {
		dev_err(ctrl->ctrl.device,
			"prop_get NVME_REG_CAP failed\n");
797
		goto out_stop_queue;
798 799 800 801 802 803 804
	}

	ctrl->ctrl.sqsize =
		min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);

	error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
	if (error)
805
		goto out_stop_queue;
806 807

	ctrl->ctrl.max_hw_sectors =
808
		(ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
809 810 811

	error = nvme_init_identify(&ctrl->ctrl);
	if (error)
812
		goto out_stop_queue;
813 814 815

	return 0;

816 817
out_stop_queue:
	nvme_rdma_stop_queue(&ctrl->queues[0]);
818
out_cleanup_queue:
819 820
	if (new)
		blk_cleanup_queue(ctrl->ctrl.admin_q);
821
out_free_tagset:
822
	if (new)
823
		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
824 825 826
out_free_async_qe:
	nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
		sizeof(struct nvme_command), DMA_TO_DEVICE);
827
	ctrl->async_event_sqe.data = NULL;
828 829 830 831 832
out_free_queue:
	nvme_rdma_free_queue(&ctrl->queues[0]);
	return error;
}

833 834 835 836 837
static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
		bool remove)
{
	if (remove) {
		blk_cleanup_queue(ctrl->ctrl.connect_q);
838
		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
839 840 841 842 843 844 845 846
	}
	nvme_rdma_free_io_queues(ctrl);
}

static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
{
	int ret;

847
	ret = nvme_rdma_alloc_io_queues(ctrl);
848 849 850 851 852
	if (ret)
		return ret;

	if (new) {
		ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
853 854
		if (IS_ERR(ctrl->ctrl.tagset)) {
			ret = PTR_ERR(ctrl->ctrl.tagset);
855
			goto out_free_io_queues;
856
		}
857 858 859 860 861 862 863 864 865 866 867

		ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
		if (IS_ERR(ctrl->ctrl.connect_q)) {
			ret = PTR_ERR(ctrl->ctrl.connect_q);
			goto out_free_tag_set;
		}
	} else {
		blk_mq_update_nr_hw_queues(&ctrl->tag_set,
			ctrl->ctrl.queue_count - 1);
	}

868
	ret = nvme_rdma_start_io_queues(ctrl);
869 870 871 872 873 874 875 876 877 878
	if (ret)
		goto out_cleanup_connect_q;

	return 0;

out_cleanup_connect_q:
	if (new)
		blk_cleanup_queue(ctrl->ctrl.connect_q);
out_free_tag_set:
	if (new)
879
		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
880 881 882
out_free_io_queues:
	nvme_rdma_free_io_queues(ctrl);
	return ret;
883 884
}

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
		bool remove)
{
	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
	nvme_rdma_stop_queue(&ctrl->queues[0]);
	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, nvme_cancel_request,
			&ctrl->ctrl);
	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
	nvme_rdma_destroy_admin_queue(ctrl, remove);
}

static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
		bool remove)
{
	if (ctrl->ctrl.queue_count > 1) {
		nvme_stop_queues(&ctrl->ctrl);
		nvme_rdma_stop_io_queues(ctrl);
		blk_mq_tagset_busy_iter(&ctrl->tag_set, nvme_cancel_request,
				&ctrl->ctrl);
		if (remove)
			nvme_start_queues(&ctrl->ctrl);
		nvme_rdma_destroy_io_queues(ctrl, remove);
	}
}

910 911 912 913 914 915 916 917
static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);

	cancel_work_sync(&ctrl->err_work);
	cancel_delayed_work_sync(&ctrl->reconnect_work);
}

918 919 920 921 922 923 924 925 926 927 928 929 930
static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);

	if (list_empty(&ctrl->list))
		goto free_ctrl;

	mutex_lock(&nvme_rdma_ctrl_mutex);
	list_del(&ctrl->list);
	mutex_unlock(&nvme_rdma_ctrl_mutex);

	nvmf_free_options(nctrl->opts);
free_ctrl:
931
	kfree(ctrl->queues);
932 933 934
	kfree(ctrl);
}

935 936 937
static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
{
	/* If we are resetting/deleting then do nothing */
938
	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
939 940 941 942 943 944 945 946
		WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
			ctrl->ctrl.state == NVME_CTRL_LIVE);
		return;
	}

	if (nvmf_should_reconnect(&ctrl->ctrl)) {
		dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
			ctrl->ctrl.opts->reconnect_delay);
947
		queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
948 949
				ctrl->ctrl.opts->reconnect_delay * HZ);
	} else {
950
		nvme_delete_ctrl(&ctrl->ctrl);
951 952 953
	}
}

954
static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
955
{
956
	int ret = -EINVAL;
957 958
	bool changed;

959
	ret = nvme_rdma_configure_admin_queue(ctrl, new);
960
	if (ret)
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
		return ret;

	if (ctrl->ctrl.icdoff) {
		dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
		goto destroy_admin;
	}

	if (!(ctrl->ctrl.sgls & (1 << 2))) {
		dev_err(ctrl->ctrl.device,
			"Mandatory keyed sgls are not supported!\n");
		goto destroy_admin;
	}

	if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
		dev_warn(ctrl->ctrl.device,
			"queue_size %zu > ctrl sqsize %u, clamping down\n",
			ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
	}

	if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
		dev_warn(ctrl->ctrl.device,
			"sqsize %u > ctrl maxcmd %u, clamping down\n",
			ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
		ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
	}
986

987 988
	if (ctrl->ctrl.sgls & (1 << 20))
		ctrl->use_inline_data = true;
989

990
	if (ctrl->ctrl.queue_count > 1) {
991
		ret = nvme_rdma_configure_io_queues(ctrl, new);
992
		if (ret)
993
			goto destroy_admin;
994 995 996
	}

	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
997 998 999
	if (!changed) {
		/* state change failure is ok if we're in DELETING state */
		WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1000 1001
		ret = -EINVAL;
		goto destroy_io;
1002 1003
	}

1004
	nvme_start_ctrl(&ctrl->ctrl);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	return 0;

destroy_io:
	if (ctrl->ctrl.queue_count > 1)
		nvme_rdma_destroy_io_queues(ctrl, new);
destroy_admin:
	nvme_rdma_stop_queue(&ctrl->queues[0]);
	nvme_rdma_destroy_admin_queue(ctrl, new);
	return ret;
}

static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
{
	struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
			struct nvme_rdma_ctrl, reconnect_work);

	++ctrl->ctrl.nr_reconnects;

	if (nvme_rdma_setup_ctrl(ctrl, false))
		goto requeue;
1025

1026 1027 1028 1029
	dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
			ctrl->ctrl.nr_reconnects);

	ctrl->ctrl.nr_reconnects = 0;
1030 1031 1032 1033

	return;

requeue:
1034
	dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1035
			ctrl->ctrl.nr_reconnects);
1036
	nvme_rdma_reconnect_or_remove(ctrl);
1037 1038 1039 1040 1041 1042 1043
}

static void nvme_rdma_error_recovery_work(struct work_struct *work)
{
	struct nvme_rdma_ctrl *ctrl = container_of(work,
			struct nvme_rdma_ctrl, err_work);

1044
	nvme_stop_keep_alive(&ctrl->ctrl);
1045
	nvme_rdma_teardown_io_queues(ctrl, false);
1046
	nvme_start_queues(&ctrl->ctrl);
1047
	nvme_rdma_teardown_admin_queue(ctrl, false);
1048

1049
	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1050 1051
		/* state change failure is ok if we're in DELETING state */
		WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1052 1053 1054
		return;
	}

1055
	nvme_rdma_reconnect_or_remove(ctrl);
1056 1057 1058 1059
}

static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
{
1060
	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1061 1062
		return;

1063
	queue_work(nvme_wq, &ctrl->err_work);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
}

static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
		const char *op)
{
	struct nvme_rdma_queue *queue = cq->cq_context;
	struct nvme_rdma_ctrl *ctrl = queue->ctrl;

	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
		dev_info(ctrl->ctrl.device,
			     "%s for CQE 0x%p failed with status %s (%d)\n",
			     op, wc->wr_cqe,
			     ib_wc_status_msg(wc->status), wc->status);
	nvme_rdma_error_recovery(ctrl);
}

static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
{
	if (unlikely(wc->status != IB_WC_SUCCESS))
		nvme_rdma_wr_error(cq, wc, "MEMREG");
}

static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
{
1088 1089 1090 1091 1092
	struct nvme_rdma_request *req =
		container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
	struct request *rq = blk_mq_rq_from_pdu(req);

	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1093
		nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1094 1095 1096 1097 1098 1099
		return;
	}

	if (refcount_dec_and_test(&req->ref))
		nvme_end_request(rq, req->status, req->result);

1100 1101 1102 1103 1104 1105 1106 1107 1108
}

static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
		struct nvme_rdma_request *req)
{
	struct ib_send_wr wr = {
		.opcode		    = IB_WR_LOCAL_INV,
		.next		    = NULL,
		.num_sge	    = 0,
1109
		.send_flags	    = IB_SEND_SIGNALED,
1110 1111 1112 1113 1114 1115
		.ex.invalidate_rkey = req->mr->rkey,
	};

	req->reg_cqe.done = nvme_rdma_inv_rkey_done;
	wr.wr_cqe = &req->reg_cqe;

1116
	return ib_post_send(queue->qp, &wr, NULL);
1117 1118 1119 1120 1121 1122 1123 1124 1125
}

static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
		struct request *rq)
{
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
	struct nvme_rdma_device *dev = queue->device;
	struct ib_device *ibdev = dev->dev;

1126
	if (!blk_rq_payload_bytes(rq))
1127 1128
		return;

Israel Rukshin's avatar
Israel Rukshin committed
1129 1130 1131 1132 1133
	if (req->mr) {
		ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
		req->mr = NULL;
	}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
			req->nents, rq_data_dir(rq) ==
				    WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);

	nvme_cleanup_cmd(rq);
	sg_free_table_chained(&req->sg_table, true);
}

static int nvme_rdma_set_sg_null(struct nvme_command *c)
{
	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;

	sg->addr = 0;
	put_unaligned_le24(0, sg->length);
	put_unaligned_le32(0, sg->key);
	sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
	return 0;
}

static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1154 1155
		struct nvme_rdma_request *req, struct nvme_command *c,
		int count)
1156 1157
{
	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1158 1159 1160 1161
	struct scatterlist *sgl = req->sg_table.sgl;
	struct ib_sge *sge = &req->sge[1];
	u32 len = 0;
	int i;
1162

1163 1164 1165 1166 1167 1168
	for (i = 0; i < count; i++, sgl++, sge++) {
		sge->addr = sg_dma_address(sgl);
		sge->length = sg_dma_len(sgl);
		sge->lkey = queue->device->pd->local_dma_lkey;
		len += sge->length;
	}
1169 1170

	sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1171
	sg->length = cpu_to_le32(len);
1172 1173
	sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;

1174
	req->num_sge += count;
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	return 0;
}

static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
		struct nvme_rdma_request *req, struct nvme_command *c)
{
	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;

	sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
	put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1185
	put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
	return 0;
}

static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
		struct nvme_rdma_request *req, struct nvme_command *c,
		int count)
{
	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
	int nr;

Israel Rukshin's avatar
Israel Rukshin committed
1197 1198 1199 1200
	req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
	if (WARN_ON_ONCE(!req->mr))
		return -EAGAIN;

1201 1202 1203 1204 1205
	/*
	 * Align the MR to a 4K page size to match the ctrl page size and
	 * the block virtual boundary.
	 */
	nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1206
	if (unlikely(nr < count)) {
Israel Rukshin's avatar
Israel Rukshin committed
1207 1208
		ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
		req->mr = NULL;
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
		if (nr < 0)
			return nr;
		return -EINVAL;
	}

	ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));

	req->reg_cqe.done = nvme_rdma_memreg_done;
	memset(&req->reg_wr, 0, sizeof(req->reg_wr));
	req->reg_wr.wr.opcode = IB_WR_REG_MR;
	req->reg_wr.wr.wr_cqe = &req->reg_cqe;
	req->reg_wr.wr.num_sge = 0;
	req->reg_wr.mr = req->mr;
	req->reg_wr.key = req->mr->rkey;
	req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
			     IB_ACCESS_REMOTE_READ |
			     IB_ACCESS_REMOTE_WRITE;

	sg->addr = cpu_to_le64(req->mr->iova);
	put_unaligned_le24(req->mr->length, sg->length);
	put_unaligned_le32(req->mr->rkey, sg->key);
	sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
			NVME_SGL_FMT_INVALIDATE;

	return 0;
}

static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1237
		struct request *rq, struct nvme_command *c)
1238 1239 1240 1241
{
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
	struct nvme_rdma_device *dev = queue->device;
	struct ib_device *ibdev = dev->dev;
1242
	int count, ret;
1243 1244

	req->num_sge = 1;
1245
	refcount_set(&req->ref, 2); /* send and recv completions */
1246 1247 1248

	c->common.flags |= NVME_CMD_SGL_METABUF;

1249
	if (!blk_rq_payload_bytes(rq))
1250 1251 1252
		return nvme_rdma_set_sg_null(c);

	req->sg_table.sgl = req->first_sgl;
1253 1254
	ret = sg_alloc_table_chained(&req->sg_table,
			blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1255 1256 1257
	if (ret)
		return -ENOMEM;

1258
	req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1259

1260
	count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1261 1262
		    rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
	if (unlikely(count <= 0)) {
1263 1264
		ret = -EIO;
		goto out_free_table;
1265 1266
	}

1267
	if (count <= dev->num_inline_segments) {
1268
		if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1269
		    queue->ctrl->use_inline_data &&
1270
		    blk_rq_payload_bytes(rq) <=
1271
				nvme_rdma_inline_data_size(queue)) {
1272
			ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1273 1274
			goto out;
		}