rdma.c 50.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/atomic.h>
#include <linux/blk-mq.h>
22
#include <linux/blk-mq-rdma.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#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"


38
#define NVME_RDMA_CONNECT_TIMEOUT_MS	3000		/* 3 second */
39 40 41 42 43 44 45 46 47 48 49

#define NVME_RDMA_MAX_SEGMENTS		256

#define NVME_RDMA_MAX_INLINE_SEGMENTS	1

/*
 * We handle AEN commands ourselves and don't even let the
 * block layer know about them.
 */
#define NVME_RDMA_NR_AEN_COMMANDS      1
#define NVME_RDMA_AQ_BLKMQ_DEPTH       \
50
	(NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

struct nvme_rdma_device {
	struct ib_device       *dev;
	struct ib_pd	       *pd;
	struct kref		ref;
	struct list_head	entry;
};

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

struct nvme_rdma_queue;
struct nvme_rdma_request {
67
	struct nvme_request	req;
68 69 70 71 72 73 74 75 76 77 78 79 80 81
	struct ib_mr		*mr;
	struct nvme_rdma_qe	sqe;
	struct ib_sge		sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
	u32			num_sge;
	int			nents;
	bool			inline_data;
	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 {
82
	NVME_RDMA_Q_LIVE		= 0,
83
	NVME_RDMA_Q_DELETING		= 1,
84 85 86 87
};

struct nvme_rdma_queue {
	struct nvme_rdma_qe	*rsp_ring;
88
	atomic_t		sig_count;
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 117 118 119 120 121
	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	delete_work;
	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;

122 123
	struct sockaddr_storage addr;
	struct sockaddr_storage src_addr;
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 149 150 151 152

	struct nvme_ctrl	ctrl;
};

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);

153 154 155
static const struct blk_mq_ops nvme_rdma_mq_ops;
static const struct blk_mq_ops nvme_rdma_admin_mq_ops;

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 187 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 230 231 232
/* 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);
		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)
{
233 234 235
	pr_debug("QP event %s (%d)\n",
		 ib_event_msg(event->event), event->event);

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}

static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
{
	wait_for_completion_interruptible_timeout(&queue->cm_done,
			msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
	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;
	init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
	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;
}

static int nvme_rdma_reinit_request(void *data, struct request *rq)
{
	struct nvme_rdma_ctrl *ctrl = data;
	struct nvme_rdma_device *dev = ctrl->device;
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
	int ret = 0;

	ib_dereg_mr(req->mr);

	req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
			ctrl->max_fr_pages);
	if (IS_ERR(req->mr)) {
		ret = PTR_ERR(req->mr);
283
		req->mr = NULL;
284
		goto out;
285 286
	}

287
	req->mr->need_inval = false;
288 289 290 291 292

out:
	return ret;
}

293 294
static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
		struct request *rq, unsigned int hctx_idx)
295
{
296
	struct nvme_rdma_ctrl *ctrl = set->driver_data;
297
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
298
	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
299 300 301 302 303 304 305 306 307 308
	struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
	struct nvme_rdma_device *dev = queue->device;

	if (req->mr)
		ib_dereg_mr(req->mr);

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

309 310 311
static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
		struct request *rq, unsigned int hctx_idx,
		unsigned int numa_node)
312
{
313
	struct nvme_rdma_ctrl *ctrl = set->driver_data;
314
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
315
	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
316 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
	struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
	struct nvme_rdma_device *dev = queue->device;
	struct ib_device *ibdev = dev->dev;
	int ret;

	ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
			DMA_TO_DEVICE);
	if (ret)
		return ret;

	req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
			ctrl->max_fr_pages);
	if (IS_ERR(req->mr)) {
		ret = PTR_ERR(req->mr);
		goto out_free_qe;
	}

	req->queue = queue;

	return 0;

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

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];

349
	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	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);

409 410
	ndev->pd = ib_alloc_pd(ndev->dev,
		register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
411 412 413 414 415 416 417
	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");
418
		goto out_free_pd;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	}

	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)
{
437 438
	struct nvme_rdma_device *dev;
	struct ib_device *ibdev;
439

440 441
	dev = queue->device;
	ibdev = dev->dev;
442 443 444 445 446 447 448 449 450
	rdma_destroy_qp(queue->cm_id);
	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);
}

451
static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
452
{
453
	struct ib_device *ibdev;
454 455 456 457 458
	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;

459 460 461 462 463 464 465
	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;
466 467

	/*
468 469
	 * Spread I/O queues completion vectors according their queue index.
	 * Admin queues can always go on completion vector 0.
470
	 */
471
	comp_vector = idx == 0 ? idx : idx - 1;
472 473

	/* +1 for ib_stop_cq */
474 475 476
	queue->ib_cq = ib_alloc_cq(ibdev, queue,
				cq_factor * queue->queue_size + 1,
				comp_vector, IB_POLL_SOFTIRQ);
477 478
	if (IS_ERR(queue->ib_cq)) {
		ret = PTR_ERR(queue->ib_cq);
479
		goto out_put_dev;
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	}

	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;
	}

	return 0;

out_destroy_qp:
	ib_destroy_qp(queue->qp);
out_destroy_ib_cq:
	ib_free_cq(queue->ib_cq);
499 500
out_put_dev:
	nvme_rdma_dev_put(queue->device);
501 502 503
	return ret;
}

504
static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
505 506 507
		int idx, size_t queue_size)
{
	struct nvme_rdma_queue *queue;
508
	struct sockaddr *src_addr = NULL;
509 510 511 512 513 514 515 516 517 518 519 520
	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;
521
	atomic_set(&queue->sig_count, 0);
522 523 524 525 526 527 528 529 530

	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);
	}

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

534 535 536
	queue->cm_error = -ETIMEDOUT;
	ret = rdma_resolve_addr(queue->cm_id, src_addr,
			(struct sockaddr *)&ctrl->addr,
537 538 539 540 541 542 543 544 545 546 547 548 549 550
			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,
			"rdma_resolve_addr wait failed (%d).\n", ret);
		goto out_destroy_cm_id;
	}

551
	clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
552 553 554 555 556 557 558 559 560 561

	return 0;

out_destroy_cm_id:
	rdma_destroy_id(queue->cm_id);
	return ret;
}

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

565 566 567 568 569 570
	rdma_disconnect(queue->cm_id);
	ib_drain_qp(queue->qp);
}

static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
{
571 572 573
	if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
		return;

574 575 576 577 578 579
	if (nvme_rdma_queue_idx(queue) == 0) {
		nvme_rdma_free_qe(queue->device->dev,
			&queue->ctrl->async_event_sqe,
			sizeof(struct nvme_command), DMA_TO_DEVICE);
	}

580 581 582 583
	nvme_rdma_destroy_queue_ib(queue);
	rdma_destroy_id(queue->cm_id);
}

584
static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
585
{
586 587 588 589
	int i;

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

592
static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
593 594 595
{
	int i;

596
	for (i = 1; i < ctrl->ctrl.queue_count; i++)
597
		nvme_rdma_stop_queue(&ctrl->queues[i]);
598 599
}

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
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)
618 619 620
{
	int i, ret = 0;

621
	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
622 623
		ret = nvme_rdma_start_queue(ctrl, i);
		if (ret)
624
			goto out_stop_queues;
625 626
	}

627 628
	return 0;

629
out_stop_queues:
630 631
	for (i--; i >= 1; i--)
		nvme_rdma_stop_queue(&ctrl->queues[i]);
632 633 634
	return ret;
}

635
static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
636
{
637
	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
638
	struct ib_device *ibdev = ctrl->device->dev;
639
	unsigned int nr_io_queues;
640 641
	int i, ret;

642
	nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
643 644 645 646 647 648 649 650 651

	/*
	 * 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);

652 653 654 655
	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
	if (ret)
		return ret;

656 657
	ctrl->ctrl.queue_count = nr_io_queues + 1;
	if (ctrl->ctrl.queue_count < 2)
658 659 660 661 662
		return 0;

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

663
	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
664 665 666
		ret = nvme_rdma_alloc_queue(ctrl, i,
				ctrl->ctrl.sqsize + 1);
		if (ret)
667 668 669 670 671 672
			goto out_free_queues;
	}

	return 0;

out_free_queues:
673
	for (i--; i >= 1; i--)
674
		nvme_rdma_free_queue(&ctrl->queues[i]);
675 676 677 678

	return ret;
}

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 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 741 742 743 744
static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl, bool admin)
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
	struct blk_mq_tag_set *set = admin ?
			&ctrl->admin_tag_set : &ctrl->tag_set;

	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;
		set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
		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;
	} else {
		set = &ctrl->tag_set;
		memset(set, 0, sizeof(*set));
		set->ops = &nvme_rdma_mq_ops;
		set->queue_depth = nctrl->opts->queue_size;
		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);
}

745 746
static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
		bool remove)
747
{
748
	nvme_rdma_stop_queue(&ctrl->queues[0]);
749 750 751 752
	if (remove) {
		blk_cleanup_queue(ctrl->ctrl.admin_q);
		nvme_rdma_free_tagset(&ctrl->ctrl, true);
	}
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 767 768 769
	if (error)
		return error;

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

	ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
		ctrl->device->dev->attrs.max_fast_reg_page_list_len);

770 771 772 773
	if (new) {
		ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
		if (IS_ERR(ctrl->ctrl.admin_tagset))
			goto out_free_queue;
774

775 776 777 778 779 780 781 782 783 784
		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;
		}
	} else {
		error = blk_mq_reinit_tagset(&ctrl->admin_tag_set,
					     nvme_rdma_reinit_request);
		if (error)
			goto out_free_queue;
785 786
	}

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

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

	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)
		goto out_cleanup_queue;

	ctrl->ctrl.max_hw_sectors =
807
		(ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
808 809 810 811 812 813 814 815 816 817 818 819 820 821

	error = nvme_init_identify(&ctrl->ctrl);
	if (error)
		goto out_cleanup_queue;

	error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
			&ctrl->async_event_sqe, sizeof(struct nvme_command),
			DMA_TO_DEVICE);
	if (error)
		goto out_cleanup_queue;

	return 0;

out_cleanup_queue:
822 823
	if (new)
		blk_cleanup_queue(ctrl->ctrl.admin_q);
824
out_free_tagset:
825 826
	if (new)
		nvme_rdma_free_tagset(&ctrl->ctrl, true);
827 828 829 830 831
out_free_queue:
	nvme_rdma_free_queue(&ctrl->queues[0]);
	return error;
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
		bool remove)
{
	nvme_rdma_stop_io_queues(ctrl);
	if (remove) {
		blk_cleanup_queue(ctrl->ctrl.connect_q);
		nvme_rdma_free_tagset(&ctrl->ctrl, false);
	}
	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 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	if (ret)
		return ret;

	if (new) {
		ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
		if (IS_ERR(ctrl->ctrl.tagset))
			goto out_free_io_queues;

		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 {
		ret = blk_mq_reinit_tagset(&ctrl->tag_set,
					   nvme_rdma_reinit_request);
		if (ret)
			goto out_free_io_queues;

		blk_mq_update_nr_hw_queues(&ctrl->tag_set,
			ctrl->ctrl.queue_count - 1);
	}

871
	ret = nvme_rdma_start_io_queues(ctrl);
872 873 874 875 876 877 878 879 880 881 882 883 884 885
	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)
		nvme_rdma_free_tagset(&ctrl->ctrl, false);
out_free_io_queues:
	nvme_rdma_free_io_queues(ctrl);
	return ret;
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
}

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);

	kfree(ctrl->queues);
	nvmf_free_options(nctrl->opts);
free_ctrl:
	kfree(ctrl);
}

S
Sagi Grimberg 已提交
905 906 907 908 909 910 911 912 913 914 915 916
static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
{
	/* If we are resetting/deleting then do nothing */
	if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
		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);
917
		queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
S
Sagi Grimberg 已提交
918 919 920
				ctrl->ctrl.opts->reconnect_delay * HZ);
	} else {
		dev_info(ctrl->ctrl.device, "Removing controller...\n");
921
		queue_work(nvme_wq, &ctrl->delete_work);
S
Sagi Grimberg 已提交
922 923 924
	}
}

925 926 927 928 929 930 931
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);
	bool changed;
	int ret;

932
	++ctrl->ctrl.nr_reconnects;
S
Sagi Grimberg 已提交
933

934 935
	if (ctrl->ctrl.queue_count > 1)
		nvme_rdma_destroy_io_queues(ctrl, false);
936

937 938
	nvme_rdma_destroy_admin_queue(ctrl, false);
	ret = nvme_rdma_configure_admin_queue(ctrl, false);
939
	if (ret)
940
		goto requeue;
941

942
	if (ctrl->ctrl.queue_count > 1) {
943
		ret = nvme_rdma_configure_io_queues(ctrl, false);
944
		if (ret)
945
			goto requeue;
946 947 948
	}

	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
949 950 951 952 953 954
	if (!changed) {
		/* state change failure is ok if we're in DELETING state */
		WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
		return;
	}

955
	ctrl->ctrl.nr_reconnects = 0;
956

957
	nvme_start_ctrl(&ctrl->ctrl);
958 959 960 961 962 963

	dev_info(ctrl->ctrl.device, "Successfully reconnected\n");

	return;

requeue:
S
Sagi Grimberg 已提交
964
	dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
965
			ctrl->ctrl.nr_reconnects);
S
Sagi Grimberg 已提交
966
	nvme_rdma_reconnect_or_remove(ctrl);
967 968 969 970 971 972 973
}

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);

974
	nvme_stop_keep_alive(&ctrl->ctrl);
975

976
	if (ctrl->ctrl.queue_count > 1) {
977
		nvme_stop_queues(&ctrl->ctrl);
978 979
		nvme_rdma_stop_io_queues(ctrl);
	}
980
	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
981
	nvme_rdma_stop_queue(&ctrl->queues[0]);
982 983

	/* We must take care of fastfail/requeue all our inflight requests */
984
	if (ctrl->ctrl.queue_count > 1)
985 986 987 988 989
		blk_mq_tagset_busy_iter(&ctrl->tag_set,
					nvme_cancel_request, &ctrl->ctrl);
	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
				nvme_cancel_request, &ctrl->ctrl);

990 991 992 993
	/*
	 * queues are not a live anymore, so restart the queues to fail fast
	 * new IO
	 */
994
	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
995 996
	nvme_start_queues(&ctrl->ctrl);

S
Sagi Grimberg 已提交
997
	nvme_rdma_reconnect_or_remove(ctrl);
998 999 1000 1001 1002 1003 1004
}

static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
{
	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
		return;

1005
	queue_work(nvme_wq, &ctrl->err_work);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
}

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)
{
	if (unlikely(wc->status != IB_WC_SUCCESS))
		nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
}

static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
		struct nvme_rdma_request *req)
{
	struct ib_send_wr *bad_wr;
	struct ib_send_wr wr = {
		.opcode		    = IB_WR_LOCAL_INV,
		.next		    = NULL,
		.num_sge	    = 0,
		.send_flags	    = 0,
		.ex.invalidate_rkey = req->mr->rkey,
	};

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

	return ib_post_send(queue->qp, &wr, &bad_wr);
}

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_ctrl *ctrl = queue->ctrl;
	struct nvme_rdma_device *dev = queue->device;
	struct ib_device *ibdev = dev->dev;
	int res;

	if (!blk_rq_bytes(rq))
		return;

1064
	if (req->mr->need_inval) {
1065
		res = nvme_rdma_inv_rkey(queue, req);
1066
		if (unlikely(res < 0)) {
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
			dev_err(ctrl->ctrl.device,
				"Queueing INV WR for rkey %#x failed (%d)\n",
				req->mr->rkey, res);
			nvme_rdma_error_recovery(queue->ctrl);
		}
	}

	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,
		struct nvme_rdma_request *req, struct nvme_command *c)
{
	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;

	req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
	req->sge[1].length = sg_dma_len(req->sg_table.sgl);
	req->sge[1].lkey = queue->device->pd->local_dma_lkey;

	sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
	sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
	sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;

	req->inline_data = true;
	req->num_sge++;
	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);
1118
	put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	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;

1130 1131 1132 1133 1134
	/*
	 * 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);
1135
	if (unlikely(nr < count)) {
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
		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;

1154
	req->mr->need_inval = true;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

	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,
1166
		struct request *rq, struct nvme_command *c)
1167 1168 1169 1170
{
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
	struct nvme_rdma_device *dev = queue->device;
	struct ib_device *ibdev = dev->dev;
1171
	int count, ret;
1172 1173 1174

	req->num_sge = 1;
	req->inline_data = false;
1175
	req->mr->need_inval = false;
1176 1177 1178 1179 1180 1181 1182

	c->common.flags |= NVME_CMD_SGL_METABUF;

	if (!blk_rq_bytes(rq))
		return nvme_rdma_set_sg_null(c);

	req->sg_table.sgl = req->first_sgl;
1183 1184
	ret = sg_alloc_table_chained(&req->sg_table,
			blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1185 1186 1187
	if (ret)
		return -ENOMEM;

1188
	req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1189

1190
	count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1191 1192 1193 1194 1195 1196 1197
		    rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
	if (unlikely(count <= 0)) {
		sg_free_table_chained(&req->sg_table, true);
		return -EIO;
	}

	if (count == 1) {
1198 1199 1200
		if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
		    blk_rq_payload_bytes(rq) <=
				nvme_rdma_inline_data_size(queue))
1201 1202
			return nvme_rdma_map_sg_inline(queue, req, c);

1203
		if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
			return nvme_rdma_map_sg_single(queue, req, c);
	}

	return nvme_rdma_map_sg_fr(queue, req, c, count);
}

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

1216 1217 1218 1219 1220 1221
/*
 * We want to signal completion at least every queue depth/2.  This returns the
 * largest power of two that is not above half of (queue size + 1) to optimize
 * (avoid divisions).
 */
static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1222
{
1223
	int limit = 1 << ilog2((queue->queue_size + 1) / 2);
1224

1225
	return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
1226 1227
}

1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
		struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
		struct ib_send_wr *first, bool flush)
{
	struct ib_send_wr wr, *bad_wr;
	int ret;

	sge->addr   = qe->dma;
	sge->length = sizeof(struct nvme_command),
	sge->lkey   = queue->device->pd->local_dma_lkey;

	qe->cqe.done = nvme_rdma_send_done;

	wr.next       = NULL;
	wr.wr_cqe     = &qe->cqe;
	wr.sg_list    = sge;
	wr.num_sge    = num_sge;
	wr.opcode     = IB_WR_SEND;
	wr.send_flags = 0;

	/*
	 * Unsignalled send completions are another giant desaster in the
	 * IB Verbs spec:  If we don't regularly post signalled sends
	 * the send queue will fill up and only a QP reset will rescue us.
	 * Would have been way to obvious to handle this in hardware or
	 * at least the RDMA stack..
	 *
	 * Always signal the flushes. The magic request used for the flush
	 * sequencer is not allocated in our driver's tagset and it's
	 * triggered to be freed by blk_cleanup_queue(). So we need to
	 * always mark it as signaled to ensure that the "wr_cqe", which is
1259
	 * embedded in request's payload, is not freed when __ib_process_cq()
1260 1261
	 * calls wr_cqe->done().
	 */
1262
	if (nvme_rdma_queue_sig_limit(queue) || flush)
1263 1264 1265 1266 1267 1268 1269 1270
		wr.send_flags |= IB_SEND_SIGNALED;

	if (first)
		first->next = &wr;
	else
		first = &wr;

	ret = ib_post_send(queue->qp, first, &bad_wr);
1271
	if (unlikely(ret)) {
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
		dev_err(queue->ctrl->ctrl.device,
			     "%s failed with error code %d\n", __func__, ret);
	}
	return ret;
}

static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
		struct nvme_rdma_qe *qe)
{
	struct ib_recv_wr wr, *bad_wr;
	struct ib_sge list;
	int ret;

	list.addr   = qe->dma;
	list.length = sizeof(struct nvme_completion);
	list.lkey   = queue->device->pd->local_dma_lkey;

	qe->cqe.done = nvme_rdma_recv_done;

	wr.next     = NULL;
	wr.wr_cqe   = &qe->cqe;
	wr.sg_list  = &list;
	wr.num_sge  = 1;

	ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1297
	if (unlikely(ret)) {
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
		dev_err(queue->ctrl->ctrl.device,
			"%s failed with error code %d\n", __func__, ret);
	}
	return ret;
}

static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
{
	u32 queue_idx = nvme_rdma_queue_idx(queue);

	if (queue_idx == 0)
		return queue->ctrl->admin_tag_set.tags[queue_idx];
	return queue->ctrl->tag_set.tags[queue_idx - 1];
}

static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
	struct nvme_rdma_queue *queue = &ctrl->queues[0];
	struct ib_device *dev = queue->device->dev;
	struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
	struct nvme_command *cmd = sqe->data;
	struct ib_sge sge;
	int ret;

	if (WARN_ON_ONCE(aer_idx != 0))
		return;

	ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);

	memset(cmd, 0, sizeof(*cmd));
	cmd->common.opcode = nvme_admin_async_event;
	cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
	cmd->common.flags |= NVME_CMD_SGL_METABUF;
	nvme_rdma_set_sg_null(cmd);

	ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
			DMA_TO_DEVICE);

	ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
	WARN_ON_ONCE(ret);
}

static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
		struct nvme_completion *cqe, struct ib_wc *wc, int tag)
{
	struct request *rq;
	struct nvme_rdma_request *req;
	int ret = 0;

	rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
	if (!rq) {
		dev_err(queue->ctrl->ctrl.device,
			"tag 0x%x on QP %#x not found\n",
			cqe->command_id, queue->qp->qp_num);
		nvme_rdma_error_recovery(queue->ctrl);
		return ret;
	}
	req = blk_mq_rq_to_pdu(rq);

	if (rq->tag == tag)
		ret = 1;

	if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
	    wc->ex.invalidate_rkey == req->mr->rkey)
1363
		req->mr->need_inval = false;
1364

1365
	nvme_end_request(rq, cqe->status, cqe->result);
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	return ret;
}

static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
{
	struct nvme_rdma_qe *qe =
		container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
	struct nvme_rdma_queue *queue = cq->cq_context;
	struct ib_device *ibdev = queue->device->dev;
	struct nvme_completion *cqe = qe->data;
	const size_t len = sizeof(struct nvme_completion);
	int ret = 0;

	if (unlikely(wc->status != IB_WC_SUCCESS)) {
		nvme_rdma_wr_error(cq, wc, "RECV");
		return 0;
	}

	ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
	/*
	 * AEN requests are special as they don't time out and can
	 * survive any kind of queue freeze and often don't respond to
	 * aborts.  We don't even bother to allocate a struct request
	 * for them but rather special case them here.
	 */
	if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
			cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1393 1394
		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
				&cqe->result);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
	else
		ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
	ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);

	nvme_rdma_post_recv(queue, qe);
	return ret;
}

static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
{
	__nvme_rdma_recv_done(cq, wc, -1);
}

static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
{
	int ret, i;

	for (i = 0; i < queue->queue_size; i++) {
		ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
		if (ret)
			goto out_destroy_queue_ib;
	}

	return 0;

out_destroy_queue_ib:
	nvme_rdma_destroy_queue_ib(queue);
	return ret;
}

static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
		struct rdma_cm_event *ev)
{
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	struct rdma_cm_id *cm_id = queue->cm_id;
	int status = ev->status;
	const char *rej_msg;
	const struct nvme_rdma_cm_rej *rej_data;
	u8 rej_data_len;

	rej_msg = rdma_reject_msg(cm_id, status);
	rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);

	if (rej_data && rej_data_len >= sizeof(u16)) {
		u16 sts = le16_to_cpu(rej_data->sts);
1439 1440

		dev_err(queue->ctrl->ctrl.device,
1441 1442
		      "Connect rejected: status %d (%s) nvme status %d (%s).\n",
		      status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1443 1444
	} else {
		dev_err(queue->ctrl->ctrl.device,
1445
			"Connect rejected: status %d (%s).\n", status, rej_msg);
1446 1447 1448 1449 1450 1451 1452 1453 1454
	}

	return -ECONNRESET;
}

static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
{
	int ret;

1455 1456 1457
	ret = nvme_rdma_create_queue_ib(queue);
	if (ret)
		return ret;
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

	ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
	if (ret) {
		dev_err(queue->ctrl->ctrl.device,
			"rdma_resolve_route failed (%d).\n",
			queue->cm_error);
		goto out_destroy_queue;
	}

	return 0;

out_destroy_queue:
	nvme_rdma_destroy_queue_ib(queue);
	return ret;
}

static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
{
	struct nvme_rdma_ctrl *ctrl = queue->ctrl;
	struct rdma_conn_param param = { };
1478
	struct nvme_rdma_cm_req priv = { };
1479 1480 1481 1482 1483 1484
	int ret;

	param.qp_num = queue->qp->qp_num;
	param.flow_control = 1;

	param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1485 1486
	/* maximum retry count */
	param.retry_count = 7;
1487 1488 1489 1490 1491 1492
	param.rnr_retry_count = 7;
	param.private_data = &priv;
	param.private_data_len = sizeof(priv);

	priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
	priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1493 1494 1495 1496 1497
	/*
	 * set the admin queue depth to the minimum size
	 * specified by the Fabrics standard.
	 */
	if (priv.qid == 0) {
1498 1499
		priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
		priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1500
	} else {
1501 1502 1503 1504 1505
		/*
		 * current interpretation of the fabrics spec
		 * is at minimum you make hrqsize sqsize+1, or a
		 * 1's based representation of sqsize.
		 */
1506
		priv.hrqsize = cpu_to_le16(queue->queue_size);
1507
		priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1508
	}
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

	ret = rdma_connect(queue->cm_id, &param);
	if (ret) {
		dev_err(ctrl->ctrl.device,
			"rdma_connect failed (%d).\n", ret);
		goto out_destroy_queue_ib;
	}

	return 0;

out_destroy_queue_ib:
	nvme_rdma_destroy_queue_ib(queue);
	return ret;
}

static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
		struct rdma_cm_event *ev)
{
	struct nvme_rdma_queue *queue = cm_id->context;
	int cm_error = 0;

	dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
		rdma_event_msg(ev->event), ev->event,
		ev->status, cm_id);

	switch (ev->event) {
	case RDMA_CM_EVENT_ADDR_RESOLVED:
		cm_error = nvme_rdma_addr_resolved(queue);
		break;
	case RDMA_CM_EVENT_ROUTE_RESOLVED:
		cm_error = nvme_rdma_route_resolved(queue);
		break;
	case RDMA_CM_EVENT_ESTABLISHED:
		queue->cm_error = nvme_rdma_conn_established(queue);
		/* complete cm_done regardless of success/failure */
		complete(&queue->cm_done);
		return 0;
	case RDMA_CM_EVENT_REJECTED:
1547
		nvme_rdma_destroy_queue_ib(queue);
1548 1549 1550 1551 1552
		cm_error = nvme_rdma_conn_rejected(queue, ev);
		break;
	case RDMA_CM_EVENT_ROUTE_ERROR:
	case RDMA_CM_EVENT_CONNECT_ERROR:
	case RDMA_CM_EVENT_UNREACHABLE:
1553 1554
		nvme_rdma_destroy_queue_ib(queue);
	case RDMA_CM_EVENT_ADDR_ERROR:
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
		dev_dbg(queue->ctrl->ctrl.device,
			"CM error event %d\n", ev->event);
		cm_error = -ECONNRESET;
		break;
	case RDMA_CM_EVENT_DISCONNECTED:
	case RDMA_CM_EVENT_ADDR_CHANGE:
	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
		dev_dbg(queue->ctrl->ctrl.device,
			"disconnect received - connection closed\n");
		nvme_rdma_error_recovery(queue->ctrl);
		break;
	case RDMA_CM_EVENT_DEVICE_REMOVAL:
1567 1568
		/* device removal is handled via the ib_client API */
		break;
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
	default:
		dev_err(queue->ctrl->ctrl.device,
			"Unexpected RDMA CM event (%d)\n", ev->event);
		nvme_rdma_error_recovery(queue->ctrl);
		break;
	}

	if (cm_error) {
		queue->cm_error = cm_error;
		complete(&queue->cm_done);
	}

	return 0;
}

static enum blk_eh_timer_return
nvme_rdma_timeout(struct request *rq, bool reserved)
{
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);

	/* queue error recovery */
	nvme_rdma_error_recovery(req->queue->ctrl);

	/* fail with DNR on cmd timeout */
1593
	nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1594 1595 1596 1597

	return BLK_EH_HANDLED;
}

1598 1599 1600
/*
 * We cannot accept any other command until the Connect command has completed.
 */
C
Christoph Hellwig 已提交
1601 1602
static inline blk_status_t
nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
1603 1604
{
	if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1605
		struct nvme_command *cmd = nvme_req(rq)->cmd;
1606

1607
		if (!blk_rq_is_passthrough(rq) ||
1608
		    cmd->common.opcode != nvme_fabrics_command ||
1609 1610 1611 1612 1613 1614 1615 1616 1617
		    cmd->fabrics.fctype != nvme_fabrics_type_connect) {
			/*
			 * reconnecting state means transport disruption, which
			 * can take a long time and even might fail permanently,
			 * so we can't let incoming I/O be requeued forever.
			 * fail it fast to allow upper layers a chance to
			 * failover.
			 */
			if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
C
Christoph Hellwig 已提交
1618 1619
				return BLK_STS_IOERR;
			return BLK_STS_RESOURCE; /* try again later */
1620
		}
1621 1622
	}

1623
	return 0;
1624 1625
}

1626
static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
		const struct blk_mq_queue_data *bd)
{
	struct nvme_ns *ns = hctx->queue->queuedata;
	struct nvme_rdma_queue *queue = hctx->driver_data;
	struct request *rq = bd->rq;
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
	struct nvme_rdma_qe *sqe = &req->sqe;
	struct nvme_command *c = sqe->data;
	bool flush = false;
	struct ib_device *dev;
1637 1638
	blk_status_t ret;
	int err;
1639 1640 1641

	WARN_ON_ONCE(rq->tag < 0);

1642 1643
	ret = nvme_rdma_queue_is_ready(queue, rq);
	if (unlikely(ret))
C
Christoph Hellwig 已提交
1644
		return ret;
1645

1646 1647 1648 1649 1650
	dev = queue->device->dev;
	ib_dma_sync_single_for_cpu(dev, sqe->dma,
			sizeof(struct nvme_command), DMA_TO_DEVICE);

	ret = nvme_setup_cmd(ns, rq, c);
1651
	if (ret)
1652 1653 1654 1655
		return ret;

	blk_mq_start_request(rq);

1656
	err = nvme_rdma_map_data(queue, rq, c);
1657
	if (unlikely(err < 0)) {
1658
		dev_err(queue->ctrl->ctrl.device,
1659
			     "Failed to map data (%d)\n", err);
1660 1661 1662 1663 1664 1665 1666
		nvme_cleanup_cmd(rq);
		goto err;
	}

	ib_dma_sync_single_for_device(dev, sqe->dma,
			sizeof(struct nvme_command), DMA_TO_DEVICE);

1667
	if (req_op(rq) == REQ_OP_FLUSH)
1668
		flush = true;
1669
	err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1670
			req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1671
	if (unlikely(err)) {
1672 1673 1674 1675
		nvme_rdma_unmap_data(queue, rq);
		goto err;
	}

1676
	return BLK_STS_OK;
1677
err:
1678 1679 1680
	if (err == -ENOMEM || err == -EAGAIN)
		return BLK_STS_RESOURCE;
	return BLK_STS_IOERR;
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
}

static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
{
	struct nvme_rdma_queue *queue = hctx->driver_data;
	struct ib_cq *cq = queue->ib_cq;
	struct ib_wc wc;
	int found = 0;

	while (ib_poll_cq(cq, 1, &wc) > 0) {
		struct ib_cqe *cqe = wc.wr_cqe;

		if (cqe) {
			if (cqe->done == nvme_rdma_recv_done)
				found |= __nvme_rdma_recv_done(cq, &wc, tag);
			else
				cqe->done(cq, &wc);
		}
	}

	return found;
}

static void nvme_rdma_complete_rq(struct request *rq)
{
	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);

1708 1709
	nvme_rdma_unmap_data(req->queue, rq);
	nvme_complete_rq(rq);
1710 1711
}

1712 1713 1714 1715 1716 1717 1718
static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
{
	struct nvme_rdma_ctrl *ctrl = set->driver_data;

	return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
}

1719
static const struct blk_mq_ops nvme_rdma_mq_ops = {
1720 1721 1722 1723 1724 1725 1726
	.queue_rq	= nvme_rdma_queue_rq,
	.complete	= nvme_rdma_complete_rq,
	.init_request	= nvme_rdma_init_request,
	.exit_request	= nvme_rdma_exit_request,
	.init_hctx	= nvme_rdma_init_hctx,
	.poll		= nvme_rdma_poll,
	.timeout	= nvme_rdma_timeout,
1727
	.map_queues	= nvme_rdma_map_queues,
1728 1729
};

1730
static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1731 1732
	.queue_rq	= nvme_rdma_queue_rq,
	.complete	= nvme_rdma_complete_rq,
1733 1734
	.init_request	= nvme_rdma_init_request,
	.exit_request	= nvme_rdma_exit_request,
1735 1736 1737 1738
	.init_hctx	= nvme_rdma_init_admin_hctx,
	.timeout	= nvme_rdma_timeout,
};

1739
static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1740 1741 1742 1743
{
	cancel_work_sync(&ctrl->err_work);
	cancel_delayed_work_sync(&ctrl->reconnect_work);

1744
	if (ctrl->ctrl.queue_count > 1) {
1745 1746 1747
		nvme_stop_queues(&ctrl->ctrl);
		blk_mq_tagset_busy_iter(&ctrl->tag_set,
					nvme_cancel_request, &ctrl->ctrl);
1748
		nvme_rdma_destroy_io_queues(ctrl, shutdown);
1749 1750
	}

1751
	if (shutdown)
1752
		nvme_shutdown_ctrl(&ctrl->ctrl);
1753 1754
	else
		nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
1755

1756
	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1757 1758
	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
				nvme_cancel_request, &ctrl->ctrl);
1759
	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1760
	nvme_rdma_destroy_admin_queue(ctrl, shutdown);
1761 1762
}

1763
static void nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl)
1764
{
1765
	nvme_remove_namespaces(&ctrl->ctrl);
1766
	nvme_rdma_shutdown_ctrl(ctrl, true);
1767
	nvme_uninit_ctrl(&ctrl->ctrl);
1768 1769 1770
	nvme_put_ctrl(&ctrl->ctrl);
}

1771 1772 1773 1774 1775
static void nvme_rdma_del_ctrl_work(struct work_struct *work)
{
	struct nvme_rdma_ctrl *ctrl = container_of(work,
				struct nvme_rdma_ctrl, delete_work);

1776 1777
	nvme_stop_ctrl(&ctrl->ctrl);
	nvme_rdma_remove_ctrl(ctrl);
1778 1779 1780 1781 1782 1783 1784
}

static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
{
	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
		return -EBUSY;

1785
	if (!queue_work(nvme_wq, &ctrl->delete_work))
1786 1787 1788 1789 1790 1791 1792 1793
		return -EBUSY;

	return 0;
}

static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
{
	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1794
	int ret = 0;
1795

1796 1797 1798 1799 1800 1801
	/*
	 * Keep a reference until all work is flushed since
	 * __nvme_rdma_del_ctrl can free the ctrl mem
	 */
	if (!kref_get_unless_zero(&ctrl->ctrl.kref))
		return -EBUSY;
1802
	ret = __nvme_rdma_del_ctrl(ctrl);
1803 1804 1805 1806
	if (!ret)
		flush_work(&ctrl->delete_work);
	nvme_put_ctrl(&ctrl->ctrl);
	return ret;
1807 1808 1809 1810
}

static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
{
1811 1812
	struct nvme_rdma_ctrl *ctrl =
		container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1813 1814 1815
	int ret;
	bool changed;

1816
	nvme_stop_ctrl(&ctrl->ctrl);
1817
	nvme_rdma_shutdown_ctrl(ctrl, false);
1818

1819
	ret = nvme_rdma_configure_admin_queue(ctrl, false);
1820 1821
	if (ret)
		goto out_fail;
1822

1823
	if (ctrl->ctrl.queue_count > 1) {
1824
		ret = nvme_rdma_configure_io_queues(ctrl, false);
1825
		if (ret)
1826
			goto out_fail;
1827 1828 1829 1830 1831
	}

	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
	WARN_ON_ONCE(!changed);

1832
	nvme_start_ctrl(&ctrl->ctrl);
1833 1834 1835

	return;

1836
out_fail:
1837
	dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1838
	nvme_rdma_remove_ctrl(ctrl);
1839 1840 1841 1842 1843
}

static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
	.name			= "rdma",
	.module			= THIS_MODULE,
1844
	.flags			= NVME_F_FABRICS,
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	.reg_read32		= nvmf_reg_read32,
	.reg_read64		= nvmf_reg_read64,
	.reg_write32		= nvmf_reg_write32,
	.free_ctrl		= nvme_rdma_free_ctrl,
	.submit_async_event	= nvme_rdma_submit_async_event,
	.delete_ctrl		= nvme_rdma_del_ctrl,
	.get_address		= nvmf_get_address,
};

static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
		struct nvmf_ctrl_options *opts)
{
	struct nvme_rdma_ctrl *ctrl;
	int ret;
	bool changed;
1860
	char *port;
1861 1862 1863 1864 1865 1866 1867

	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
	if (!ctrl)
		return ERR_PTR(-ENOMEM);
	ctrl->ctrl.opts = opts;
	INIT_LIST_HEAD(&ctrl->list);

1868 1869 1870 1871 1872 1873 1874
	if (opts->mask & NVMF_OPT_TRSVCID)
		port = opts->trsvcid;
	else
		port = __stringify(NVME_RDMA_IP_PORT);

	ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
			opts->traddr, port, &ctrl->addr);
1875
	if (ret) {
1876
		pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1877 1878 1879
		goto out_free_ctrl;
	}

1880
	if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1881 1882
		ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
			opts->host_traddr, NULL, &ctrl->src_addr);
1883
		if (ret) {
1884
			pr_err("malformed src address passed: %s\n",
1885 1886 1887 1888 1889
			       opts->host_traddr);
			goto out_free_ctrl;
		}
	}

1890 1891 1892 1893 1894 1895 1896 1897 1898
	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
				0 /* no quirks, we're perfect! */);
	if (ret)
		goto out_free_ctrl;

	INIT_DELAYED_WORK(&ctrl->reconnect_work,
			nvme_rdma_reconnect_ctrl_work);
	INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
	INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1899
	INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
1900

1901
	ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1902
	ctrl->ctrl.sqsize = opts->queue_size - 1;
1903 1904 1905
	ctrl->ctrl.kato = opts->kato;

	ret = -ENOMEM;
1906
	ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
1907 1908 1909 1910
				GFP_KERNEL);
	if (!ctrl->queues)
		goto out_uninit_ctrl;

1911
	ret = nvme_rdma_configure_admin_queue(ctrl, true);
1912 1913 1914 1915 1916 1917
	if (ret)
		goto out_kfree_queues;

	/* sanity check icdoff */
	if (ctrl->ctrl.icdoff) {
		dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1918
		ret = -EINVAL;
1919 1920 1921 1922 1923 1924
		goto out_remove_admin_queue;
	}

	/* sanity check keyed sgls */
	if (!(ctrl->ctrl.sgls & (1 << 20))) {
		dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1925
		ret = -EINVAL;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
		goto out_remove_admin_queue;
	}

	if (opts->queue_size > ctrl->ctrl.maxcmd) {
		/* warn if maxcmd is lower than queue_size */
		dev_warn(ctrl->ctrl.device,
			"queue_size %zu > ctrl maxcmd %u, clamping down\n",
			opts->queue_size, ctrl->ctrl.maxcmd);
		opts->queue_size = ctrl->ctrl.maxcmd;
	}

1937 1938 1939 1940 1941 1942 1943 1944
	if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
		/* warn if sqsize is lower than queue_size */
		dev_warn(ctrl->ctrl.device,
			"queue_size %zu > ctrl sqsize %u, clamping down\n",
			opts->queue_size, ctrl->ctrl.sqsize + 1);
		opts->queue_size = ctrl->ctrl.sqsize + 1;
	}

1945
	if (opts->nr_io_queues) {
1946
		ret = nvme_rdma_configure_io_queues(ctrl, true);
1947 1948 1949 1950 1951 1952 1953
		if (ret)
			goto out_remove_admin_queue;
	}

	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
	WARN_ON_ONCE(!changed);

1954
	dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1955 1956 1957 1958 1959 1960 1961 1962
		ctrl->ctrl.opts->subsysnqn, &ctrl->addr);

	kref_get(&ctrl->ctrl.kref);

	mutex_lock(&nvme_rdma_ctrl_mutex);
	list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
	mutex_unlock(&nvme_rdma_ctrl_mutex);

1963
	nvme_start_ctrl(&ctrl->ctrl);
1964 1965 1966 1967

	return &ctrl->ctrl;

out_remove_admin_queue:
1968
	nvme_rdma_destroy_admin_queue(ctrl, true);
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
out_kfree_queues:
	kfree(ctrl->queues);
out_uninit_ctrl:
	nvme_uninit_ctrl(&ctrl->ctrl);
	nvme_put_ctrl(&ctrl->ctrl);
	if (ret > 0)
		ret = -EIO;
	return ERR_PTR(ret);
out_free_ctrl:
	kfree(ctrl);
	return ERR_PTR(ret);
}

static struct nvmf_transport_ops nvme_rdma_transport = {
	.name		= "rdma",
	.required_opts	= NVMF_OPT_TRADDR,
1985
	.allowed_opts	= NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
S
Sagi Grimberg 已提交
1986
			  NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
1987 1988 1989
	.create_ctrl	= nvme_rdma_create_ctrl,
};

1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
{
	struct nvme_rdma_ctrl *ctrl;

	/* Delete all controllers using this device */
	mutex_lock(&nvme_rdma_ctrl_mutex);
	list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
		if (ctrl->device->dev != ib_device)
			continue;
		dev_info(ctrl->ctrl.device,
			"Removing ctrl: NQN \"%s\", addr %pISp\n",
			ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
		__nvme_rdma_del_ctrl(ctrl);
	}
	mutex_unlock(&nvme_rdma_ctrl_mutex);

2006
	flush_workqueue(nvme_wq);
2007 2008 2009 2010 2011 2012 2013
}

static struct ib_client nvme_rdma_ib_client = {
	.name   = "nvme_rdma",
	.remove = nvme_rdma_remove_one
};

2014 2015
static int __init nvme_rdma_init_module(void)
{
2016 2017 2018
	int ret;

	ret = ib_register_client(&nvme_rdma_ib_client);
2019
	if (ret)
2020
		return ret;
2021 2022 2023 2024

	ret = nvmf_register_transport(&nvme_rdma_transport);
	if (ret)
		goto err_unreg_client;
2025

2026
	return 0;
2027

2028 2029 2030
err_unreg_client:
	ib_unregister_client(&nvme_rdma_ib_client);
	return ret;
2031 2032 2033 2034 2035
}

static void __exit nvme_rdma_cleanup_module(void)
{
	nvmf_unregister_transport(&nvme_rdma_transport);
2036
	ib_unregister_client(&nvme_rdma_ib_client);
2037 2038 2039 2040 2041 2042
}

module_init(nvme_rdma_init_module);
module_exit(nvme_rdma_cleanup_module);

MODULE_LICENSE("GPL v2");