odp.c 46.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

H
Haggai Eran 已提交
33 34
#include <rdma/ib_umem.h>
#include <rdma/ib_umem_odp.h>
J
Jérémy Lefaure 已提交
35
#include <linux/kernel.h>
H
Haggai Eran 已提交
36

37
#include "mlx5_ib.h"
38
#include "cmd.h"
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <linux/mlx5/eq.h>

/* Contains the details of a pagefault. */
struct mlx5_pagefault {
	u32			bytes_committed;
	u32			token;
	u8			event_subtype;
	u8			type;
	union {
		/* Initiator or send message responder pagefault details. */
		struct {
			/* Received packet size, only valid for responders. */
			u32	packet_size;
			/*
			 * Number of resource holding WQE, depends on type.
			 */
			u32	wq_num;
			/*
			 * WQE index. Refers to either the send queue or
			 * receive queue, according to event_subtype.
			 */
			u16	wqe_index;
		} wqe;
		/* RDMA responder pagefault details */
		struct {
			u32	r_key;
			/*
			 * Received packet size, minimal size page fault
			 * resolution required for forward progress.
			 */
			u32	packet_size;
			u32	rdma_op_len;
			u64	rdma_va;
		} rdma;
	};

	struct mlx5_ib_pf_eq	*eq;
	struct work_struct	work;
};

80 81
#define MAX_PREFETCH_LEN (4*1024*1024U)

82 83 84 85
/* Timeout in ms to wait for an active mmu notifier to complete when handling
 * a pagefault. */
#define MMU_NOTIFIER_TIMEOUT 1000

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define MLX5_IMR_MTT_BITS (30 - PAGE_SHIFT)
#define MLX5_IMR_MTT_SHIFT (MLX5_IMR_MTT_BITS + PAGE_SHIFT)
#define MLX5_IMR_MTT_ENTRIES BIT_ULL(MLX5_IMR_MTT_BITS)
#define MLX5_IMR_MTT_SIZE BIT_ULL(MLX5_IMR_MTT_SHIFT)
#define MLX5_IMR_MTT_MASK (~(MLX5_IMR_MTT_SIZE - 1))

#define MLX5_KSM_PAGE_SHIFT MLX5_IMR_MTT_SHIFT

static u64 mlx5_imr_ksm_entries;

static int check_parent(struct ib_umem_odp *odp,
			       struct mlx5_ib_mr *parent)
{
	struct mlx5_ib_mr *mr = odp->private;

A
Artemy Kovalyov 已提交
101
	return mr && mr->parent == parent && !odp->dying;
102 103
}

104
static struct ib_ucontext_per_mm *mr_to_per_mm(struct mlx5_ib_mr *mr)
105
{
106
	if (WARN_ON(!mr || !is_odp_mr(mr)))
107 108 109 110 111
		return NULL;

	return to_ib_umem_odp(mr->umem)->per_mm;
}

112 113 114
static struct ib_umem_odp *odp_next(struct ib_umem_odp *odp)
{
	struct mlx5_ib_mr *mr = odp->private, *parent = mr->parent;
115
	struct ib_ucontext_per_mm *per_mm = odp->per_mm;
116 117
	struct rb_node *rb;

118
	down_read(&per_mm->umem_rwsem);
119 120 121 122 123 124 125 126 127 128 129
	while (1) {
		rb = rb_next(&odp->interval_tree.rb);
		if (!rb)
			goto not_found;
		odp = rb_entry(rb, struct ib_umem_odp, interval_tree.rb);
		if (check_parent(odp, parent))
			goto end;
	}
not_found:
	odp = NULL;
end:
130
	up_read(&per_mm->umem_rwsem);
131 132 133
	return odp;
}

134
static struct ib_umem_odp *odp_lookup(u64 start, u64 length,
135 136
				      struct mlx5_ib_mr *parent)
{
137
	struct ib_ucontext_per_mm *per_mm = mr_to_per_mm(parent);
138 139 140
	struct ib_umem_odp *odp;
	struct rb_node *rb;

141 142
	down_read(&per_mm->umem_rwsem);
	odp = rbt_ib_umem_lookup(&per_mm->umem_tree, start, length);
143 144 145 146 147 148 149 150 151 152
	if (!odp)
		goto end;

	while (1) {
		if (check_parent(odp, parent))
			goto end;
		rb = rb_next(&odp->interval_tree.rb);
		if (!rb)
			goto not_found;
		odp = rb_entry(rb, struct ib_umem_odp, interval_tree.rb);
153
		if (ib_umem_start(odp) > start + length)
154 155 156 157 158
			goto not_found;
	}
not_found:
	odp = NULL;
end:
159
	up_read(&per_mm->umem_rwsem);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	return odp;
}

void mlx5_odp_populate_klm(struct mlx5_klm *pklm, size_t offset,
			   size_t nentries, struct mlx5_ib_mr *mr, int flags)
{
	struct ib_pd *pd = mr->ibmr.pd;
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct ib_umem_odp *odp;
	unsigned long va;
	int i;

	if (flags & MLX5_IB_UPD_XLT_ZAP) {
		for (i = 0; i < nentries; i++, pklm++) {
			pklm->bcount = cpu_to_be32(MLX5_IMR_MTT_SIZE);
			pklm->key = cpu_to_be32(dev->null_mkey);
			pklm->va = 0;
		}
		return;
	}

181 182
	odp = odp_lookup(offset * MLX5_IMR_MTT_SIZE,
			 nentries * MLX5_IMR_MTT_SIZE, mr);
183 184 185 186

	for (i = 0; i < nentries; i++, pklm++) {
		pklm->bcount = cpu_to_be32(MLX5_IMR_MTT_SIZE);
		va = (offset + i) * MLX5_IMR_MTT_SIZE;
187
		if (odp && odp->umem.address == va) {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
			struct mlx5_ib_mr *mtt = odp->private;

			pklm->key = cpu_to_be32(mtt->ibmr.lkey);
			odp = odp_next(odp);
		} else {
			pklm->key = cpu_to_be32(dev->null_mkey);
		}
		mlx5_ib_dbg(dev, "[%d] va %lx key %x\n",
			    i, va, be32_to_cpu(pklm->key));
	}
}

static void mr_leaf_free_action(struct work_struct *work)
{
	struct ib_umem_odp *odp = container_of(work, struct ib_umem_odp, work);
203
	int idx = ib_umem_start(odp) >> MLX5_IMR_MTT_SHIFT;
204 205 206 207 208
	struct mlx5_ib_mr *mr = odp->private, *imr = mr->parent;

	mr->parent = NULL;
	synchronize_srcu(&mr->dev->mr_srcu);

209
	ib_umem_release(&odp->umem);
210 211 212 213 214 215 216 217 218 219
	if (imr->live)
		mlx5_ib_update_xlt(imr, idx, 1, 0,
				   MLX5_IB_UPD_XLT_INDIRECT |
				   MLX5_IB_UPD_XLT_ATOMIC);
	mlx5_mr_cache_free(mr->dev, mr);

	if (atomic_dec_and_test(&imr->num_leaf_free))
		wake_up(&imr->q_leaf_free);
}

220
void mlx5_ib_invalidate_range(struct ib_umem_odp *umem_odp, unsigned long start,
221 222 223
			      unsigned long end)
{
	struct mlx5_ib_mr *mr;
224 225
	const u64 umr_block_mask = (MLX5_UMR_MTT_ALIGNMENT /
				    sizeof(struct mlx5_mtt)) - 1;
226 227 228 229
	u64 idx = 0, blk_start_idx = 0;
	int in_block = 0;
	u64 addr;

230
	if (!umem_odp) {
231 232 233 234
		pr_err("invalidation called on NULL umem or non-ODP umem\n");
		return;
	}

235
	mr = umem_odp->private;
236 237 238 239

	if (!mr || !mr->ibmr.pd)
		return;

240 241
	start = max_t(u64, ib_umem_start(umem_odp), start);
	end = min_t(u64, ib_umem_end(umem_odp), end);
242 243 244 245 246 247 248

	/*
	 * Iteration one - zap the HW's MTTs. The notifiers_count ensures that
	 * while we are doing the invalidation, no page fault will attempt to
	 * overwrite the same MTTs.  Concurent invalidations might race us,
	 * but they will write 0s as well, so no difference in the end result.
	 */
249
	mutex_lock(&umem_odp->umem_mutex);
250 251
	for (addr = start; addr < end; addr += BIT(umem_odp->page_shift)) {
		idx = (addr - ib_umem_start(umem_odp)) >> umem_odp->page_shift;
252 253 254 255 256 257
		/*
		 * Strive to write the MTTs in chunks, but avoid overwriting
		 * non-existing MTTs. The huristic here can be improved to
		 * estimate the cost of another UMR vs. the cost of bigger
		 * UMR.
		 */
258
		if (umem_odp->dma_list[idx] &
259 260 261 262 263 264 265 266 267
		    (ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) {
			if (!in_block) {
				blk_start_idx = idx;
				in_block = 1;
			}
		} else {
			u64 umr_offset = idx & umr_block_mask;

			if (in_block && umr_offset == 0) {
268
				mlx5_ib_update_xlt(mr, blk_start_idx,
269
						   idx - blk_start_idx, 0,
270 271
						   MLX5_IB_UPD_XLT_ZAP |
						   MLX5_IB_UPD_XLT_ATOMIC);
272 273 274 275 276
				in_block = 0;
			}
		}
	}
	if (in_block)
277
		mlx5_ib_update_xlt(mr, blk_start_idx,
278
				   idx - blk_start_idx + 1, 0,
279 280
				   MLX5_IB_UPD_XLT_ZAP |
				   MLX5_IB_UPD_XLT_ATOMIC);
281
	mutex_unlock(&umem_odp->umem_mutex);
282 283 284 285 286 287
	/*
	 * We are now sure that the device will not access the
	 * memory. We can safely unmap it, and mark it as dirty if
	 * needed.
	 */

288
	ib_umem_odp_unmap_dma_pages(umem_odp, start, end);
289

290
	if (unlikely(!umem_odp->npages && mr->parent &&
291 292
		     !umem_odp->dying)) {
		WRITE_ONCE(umem_odp->dying, 1);
293
		atomic_inc(&mr->parent->num_leaf_free);
294
		schedule_work(&umem_odp->work);
295
	}
296 297
}

298
void mlx5_ib_internal_fill_odp_caps(struct mlx5_ib_dev *dev)
299 300 301 302 303
{
	struct ib_odp_caps *caps = &dev->odp_caps;

	memset(caps, 0, sizeof(*caps));

304 305
	if (!MLX5_CAP_GEN(dev->mdev, pg))
		return;
306

307
	caps->general_caps = IB_ODP_SUPPORT;
308

309 310 311 312 313
	if (MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset))
		dev->odp_max_size = U64_MAX;
	else
		dev->odp_max_size = BIT_ULL(MLX5_MAX_UMR_SHIFT + PAGE_SHIFT);

314 315 316
	if (MLX5_CAP_ODP(dev->mdev, ud_odp_caps.send))
		caps->per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SEND;

317 318 319
	if (MLX5_CAP_ODP(dev->mdev, ud_odp_caps.srq_receive))
		caps->per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV;

320 321 322 323 324 325 326 327 328 329 330 331
	if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.send))
		caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SEND;

	if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.receive))
		caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_RECV;

	if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.write))
		caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_WRITE;

	if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.read))
		caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_READ;

332 333 334
	if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.atomic))
		caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_ATOMIC;

335 336 337
	if (MLX5_CAP_ODP(dev->mdev, rc_odp_caps.srq_receive))
		caps->per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV;

M
Moni Shoua 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.send))
		caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_SEND;

	if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.receive))
		caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_RECV;

	if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.write))
		caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_WRITE;

	if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.read))
		caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_READ;

	if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.atomic))
		caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_ATOMIC;

	if (MLX5_CAP_ODP(dev->mdev, xrc_odp_caps.srq_receive))
		caps->per_transport_caps.xrc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV;

356 357 358 359 360
	if (MLX5_CAP_GEN(dev->mdev, fixed_buffer_size) &&
	    MLX5_CAP_GEN(dev->mdev, null_mkey) &&
	    MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset))
		caps->general_caps |= IB_ODP_SUPPORT_IMPLICIT;

361
	return;
362
}
363

364 365
static void mlx5_ib_page_fault_resume(struct mlx5_ib_dev *dev,
				      struct mlx5_pagefault *pfault,
366 367
				      int error)
{
368 369
	int wq_num = pfault->event_subtype == MLX5_PFAULT_SUBTYPE_WQE ?
		     pfault->wqe.wq_num : pfault->token;
370 371 372 373 374 375 376 377 378 379 380 381 382 383
	u32 out[MLX5_ST_SZ_DW(page_fault_resume_out)] = { };
	u32 in[MLX5_ST_SZ_DW(page_fault_resume_in)]   = { };
	int err;

	MLX5_SET(page_fault_resume_in, in, opcode, MLX5_CMD_OP_PAGE_FAULT_RESUME);
	MLX5_SET(page_fault_resume_in, in, page_fault_type, pfault->type);
	MLX5_SET(page_fault_resume_in, in, token, pfault->token);
	MLX5_SET(page_fault_resume_in, in, wq_number, wq_num);
	MLX5_SET(page_fault_resume_in, in, error, !!error);

	err = mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
	if (err)
		mlx5_ib_err(dev, "Failed to resolve the page fault on WQ 0x%x err %d\n",
			    wq_num, err);
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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static struct mlx5_ib_mr *implicit_mr_alloc(struct ib_pd *pd,
					    struct ib_umem *umem,
					    bool ksm, int access_flags)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct mlx5_ib_mr *mr;
	int err;

	mr = mlx5_mr_cache_alloc(dev, ksm ? MLX5_IMR_KSM_CACHE_ENTRY :
					    MLX5_IMR_MTT_CACHE_ENTRY);

	if (IS_ERR(mr))
		return mr;

	mr->ibmr.pd = pd;

	mr->dev = dev;
	mr->access_flags = access_flags;
	mr->mmkey.iova = 0;
	mr->umem = umem;

	if (ksm) {
		err = mlx5_ib_update_xlt(mr, 0,
					 mlx5_imr_ksm_entries,
					 MLX5_KSM_PAGE_SHIFT,
					 MLX5_IB_UPD_XLT_INDIRECT |
					 MLX5_IB_UPD_XLT_ZAP |
					 MLX5_IB_UPD_XLT_ENABLE);

	} else {
		err = mlx5_ib_update_xlt(mr, 0,
					 MLX5_IMR_MTT_ENTRIES,
					 PAGE_SHIFT,
					 MLX5_IB_UPD_XLT_ZAP |
					 MLX5_IB_UPD_XLT_ENABLE |
					 MLX5_IB_UPD_XLT_ATOMIC);
	}

	if (err)
		goto fail;

	mr->ibmr.lkey = mr->mmkey.key;
	mr->ibmr.rkey = mr->mmkey.key;

	mr->live = 1;

	mlx5_ib_dbg(dev, "key %x dev %p mr %p\n",
		    mr->mmkey.key, dev->mdev, mr);

	return mr;

fail:
	mlx5_ib_err(dev, "Failed to register MKEY %d\n", err);
	mlx5_mr_cache_free(dev, mr);

	return ERR_PTR(err);
}

static struct ib_umem_odp *implicit_mr_get_data(struct mlx5_ib_mr *mr,
						u64 io_virt, size_t bcnt)
{
	struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.pd->device);
	struct ib_umem_odp *odp, *result = NULL;
449
	struct ib_umem_odp *odp_mr = to_ib_umem_odp(mr->umem);
450 451 452 453
	u64 addr = io_virt & MLX5_IMR_MTT_MASK;
	int nentries = 0, start_idx = 0, ret;
	struct mlx5_ib_mr *mtt;

454
	mutex_lock(&odp_mr->umem_mutex);
455
	odp = odp_lookup(addr, 1, mr);
456 457 458 459 460 461 462 463 464

	mlx5_ib_dbg(dev, "io_virt:%llx bcnt:%zx addr:%llx odp:%p\n",
		    io_virt, bcnt, addr, odp);

next_mr:
	if (likely(odp)) {
		if (nentries)
			nentries++;
	} else {
465
		odp = ib_alloc_odp_umem(odp_mr, addr,
466
					MLX5_IMR_MTT_SIZE);
467
		if (IS_ERR(odp)) {
468
			mutex_unlock(&odp_mr->umem_mutex);
469
			return ERR_CAST(odp);
470 471
		}

472
		mtt = implicit_mr_alloc(mr->ibmr.pd, &odp->umem, 0,
473
					mr->access_flags);
474
		if (IS_ERR(mtt)) {
475
			mutex_unlock(&odp_mr->umem_mutex);
476
			ib_umem_release(&odp->umem);
477 478 479 480
			return ERR_CAST(mtt);
		}

		odp->private = mtt;
481
		mtt->umem = &odp->umem;
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		mtt->mmkey.iova = addr;
		mtt->parent = mr;
		INIT_WORK(&odp->work, mr_leaf_free_action);

		if (!nentries)
			start_idx = addr >> MLX5_IMR_MTT_SHIFT;
		nentries++;
	}

	/* Return first odp if region not covered by single one */
	if (likely(!result))
		result = odp;

	addr += MLX5_IMR_MTT_SIZE;
	if (unlikely(addr < io_virt + bcnt)) {
		odp = odp_next(odp);
498
		if (odp && odp->umem.address != addr)
499 500 501 502 503 504 505 506 507 508 509 510 511 512
			odp = NULL;
		goto next_mr;
	}

	if (unlikely(nentries)) {
		ret = mlx5_ib_update_xlt(mr, start_idx, nentries, 0,
					 MLX5_IB_UPD_XLT_INDIRECT |
					 MLX5_IB_UPD_XLT_ATOMIC);
		if (ret) {
			mlx5_ib_err(dev, "Failed to update PAS\n");
			result = ERR_PTR(ret);
		}
	}

513
	mutex_unlock(&odp_mr->umem_mutex);
514 515 516 517
	return result;
}

struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
518
					     struct ib_udata *udata,
519 520 521 522 523
					     int access_flags)
{
	struct mlx5_ib_mr *imr;
	struct ib_umem *umem;

524
	umem = ib_umem_get(udata, 0, 0, access_flags, 0);
525 526 527 528 529 530 531 532 533 534 535 536
	if (IS_ERR(umem))
		return ERR_CAST(umem);

	imr = implicit_mr_alloc(&pd->ibpd, umem, 1, access_flags);
	if (IS_ERR(imr)) {
		ib_umem_release(umem);
		return ERR_CAST(imr);
	}

	imr->umem = umem;
	init_waitqueue_head(&imr->q_leaf_free);
	atomic_set(&imr->num_leaf_free, 0);
537
	atomic_set(&imr->num_pending_prefetch, 0);
538 539 540 541

	return imr;
}

542 543
static int mr_leaf_free(struct ib_umem_odp *umem_odp, u64 start, u64 end,
			void *cookie)
544
{
545
	struct mlx5_ib_mr *mr = umem_odp->private, *imr = cookie;
546 547 548 549

	if (mr->parent != imr)
		return 0;

550 551
	ib_umem_odp_unmap_dma_pages(umem_odp, ib_umem_start(umem_odp),
				    ib_umem_end(umem_odp));
552

553
	if (umem_odp->dying)
554 555
		return 0;

556
	WRITE_ONCE(umem_odp->dying, 1);
557
	atomic_inc(&imr->num_leaf_free);
558
	schedule_work(&umem_odp->work);
559 560 561 562 563 564

	return 0;
}

void mlx5_ib_free_implicit_mr(struct mlx5_ib_mr *imr)
{
565
	struct ib_ucontext_per_mm *per_mm = mr_to_per_mm(imr);
566

567 568
	down_read(&per_mm->umem_rwsem);
	rbt_ib_umem_for_each_in_range(&per_mm->umem_tree, 0, ULLONG_MAX,
569
				      mr_leaf_free, true, imr);
570
	up_read(&per_mm->umem_rwsem);
571 572 573 574

	wait_event(imr->q_leaf_free, !atomic_read(&imr->num_leaf_free));
}

M
Moni Shoua 已提交
575 576
#define MLX5_PF_FLAGS_PREFETCH  BIT(0)
#define MLX5_PF_FLAGS_DOWNGRADE BIT(1)
577
static int pagefault_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr,
M
Moni Shoua 已提交
578 579
			u64 io_virt, size_t bcnt, u32 *bytes_mapped,
			u32 flags)
H
Haggai Eran 已提交
580
{
581 582
	int npages = 0, current_seq, page_shift, ret, np;
	bool implicit = false;
583
	struct ib_umem_odp *odp_mr = to_ib_umem_odp(mr->umem);
M
Moni Shoua 已提交
584 585
	bool downgrade = flags & MLX5_PF_FLAGS_DOWNGRADE;
	bool prefetch = flags & MLX5_PF_FLAGS_PREFETCH;
586
	u64 access_mask;
587
	u64 start_idx, page_mask;
588 589
	struct ib_umem_odp *odp;
	size_t size;
H
Haggai Eran 已提交
590

591
	if (!odp_mr->page_list) {
592 593
		odp = implicit_mr_get_data(mr, io_virt, bcnt);

594 595
		if (IS_ERR(odp))
			return PTR_ERR(odp);
596
		mr = odp->private;
597
		implicit = true;
598
	} else {
599
		odp = odp_mr;
600 601
	}

602
next_mr:
603
	size = min_t(size_t, bcnt, ib_umem_end(odp) - io_virt);
604

605
	page_shift = odp->page_shift;
606
	page_mask = ~(BIT(page_shift) - 1);
607
	start_idx = (io_virt - (mr->mmkey.iova & page_mask)) >> page_shift;
608
	access_mask = ODP_READ_ALLOWED_BIT;
609

M
Moni Shoua 已提交
610 611 612 613 614 615 616 617 618
	if (prefetch && !downgrade && !mr->umem->writable) {
		/* prefetch with write-access must
		 * be supported by the MR
		 */
		ret = -EINVAL;
		goto out;
	}

	if (mr->umem->writable && !downgrade)
619
		access_mask |= ODP_WRITE_ALLOWED_BIT;
620

621 622 623 624 625 626 627
	current_seq = READ_ONCE(odp->notifiers_seq);
	/*
	 * Ensure the sequence number is valid for some time before we call
	 * gup.
	 */
	smp_rmb();

628
	ret = ib_umem_odp_map_dma_pages(to_ib_umem_odp(mr->umem), io_virt, size,
629 630 631
					access_mask, current_seq);

	if (ret < 0)
632
		goto out;
H
Haggai Eran 已提交
633

634 635 636
	np = ret;

	mutex_lock(&odp->umem_mutex);
637 638
	if (!ib_umem_mmu_notifier_retry(to_ib_umem_odp(mr->umem),
					current_seq)) {
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		/*
		 * No need to check whether the MTTs really belong to
		 * this MR, since ib_umem_odp_map_dma_pages already
		 * checks this.
		 */
		ret = mlx5_ib_update_xlt(mr, start_idx, np,
					 page_shift, MLX5_IB_UPD_XLT_ATOMIC);
	} else {
		ret = -EAGAIN;
	}
	mutex_unlock(&odp->umem_mutex);

	if (ret < 0) {
		if (ret != -EAGAIN)
			mlx5_ib_err(dev, "Failed to update mkey page tables\n");
		goto out;
	}
656

657 658 659 660
	if (bytes_mapped) {
		u32 new_mappings = (np << page_shift) -
			(io_virt - round_down(io_virt, 1 << page_shift));
		*bytes_mapped += min_t(u32, new_mappings, size);
661 662
	}

663
	npages += np << (page_shift - PAGE_SHIFT);
664
	bcnt -= size;
665

666 667 668 669 670
	if (unlikely(bcnt)) {
		struct ib_umem_odp *next;

		io_virt += size;
		next = odp_next(odp);
671
		if (unlikely(!next || next->umem.address != io_virt)) {
672 673
			mlx5_ib_dbg(dev, "next implicit leaf removed at 0x%llx. got %p\n",
				    io_virt, next);
674
			return -EAGAIN;
675 676 677 678
		}
		odp = next;
		mr = odp->private;
		goto next_mr;
H
Haggai Eran 已提交
679 680
	}

681 682 683
	return npages;

out:
684
	if (ret == -EAGAIN) {
685
		if (implicit || !odp->dying) {
686 687 688 689
			unsigned long timeout =
				msecs_to_jiffies(MMU_NOTIFIER_TIMEOUT);

			if (!wait_for_completion_timeout(
690
					&odp->notifier_completion,
691
					timeout)) {
692 693
				mlx5_ib_warn(dev, "timeout waiting for mmu notifier. seq %d against %d. notifiers_count=%d\n",
					     current_seq, odp->notifiers_seq, odp->notifiers_count);
694 695 696 697 698 699
			}
		} else {
			/* The MR is being killed, kill the QP as well. */
			ret = -EFAULT;
		}
	}
700

701 702 703
	return ret;
}

A
Artemy Kovalyov 已提交
704 705 706 707 708 709 710 711
struct pf_frame {
	struct pf_frame *next;
	u32 key;
	u64 io_virt;
	size_t bcnt;
	int depth;
};

712 713 714 715 716 717 718 719 720
static bool mkey_is_eq(struct mlx5_core_mkey *mmkey, u32 key)
{
	if (!mmkey)
		return false;
	if (mmkey->type == MLX5_MKEY_MW)
		return mlx5_base_mkey(mmkey->key) == mlx5_base_mkey(key);
	return mmkey->key == key;
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
static int get_indirect_num_descs(struct mlx5_core_mkey *mmkey)
{
	struct mlx5_ib_mw *mw;
	struct mlx5_ib_devx_mr *devx_mr;

	if (mmkey->type == MLX5_MKEY_MW) {
		mw = container_of(mmkey, struct mlx5_ib_mw, mmkey);
		return mw->ndescs;
	}

	devx_mr = container_of(mmkey, struct mlx5_ib_devx_mr,
			       mmkey);
	return devx_mr->ndescs;
}

736 737 738 739 740 741 742 743 744 745 746
/*
 * Handle a single data segment in a page-fault WQE or RDMA region.
 *
 * Returns number of OS pages retrieved on success. The caller may continue to
 * the next data segment.
 * Can return the following error codes:
 * -EAGAIN to designate a temporary error. The caller will abort handling the
 *  page fault and resolve it.
 * -EFAULT when there's an error mapping the requested pages. The caller will
 *  abort the page fault handling.
 */
747 748
static int pagefault_single_data_segment(struct mlx5_ib_dev *dev,
					 struct ib_pd *pd, u32 key,
M
Moni Shoua 已提交
749
					 u64 io_virt, size_t bcnt,
750
					 u32 *bytes_committed,
M
Moni Shoua 已提交
751
					 u32 *bytes_mapped, u32 flags)
752
{
A
Artemy Kovalyov 已提交
753
	int npages = 0, srcu_key, ret, i, outlen, cur_outlen = 0, depth = 0;
M
Moni Shoua 已提交
754
	bool prefetch = flags & MLX5_PF_FLAGS_PREFETCH;
A
Artemy Kovalyov 已提交
755 756
	struct pf_frame *head = NULL, *frame;
	struct mlx5_core_mkey *mmkey;
757
	struct mlx5_ib_mr *mr;
A
Artemy Kovalyov 已提交
758 759 760
	struct mlx5_klm *pklm;
	u32 *out = NULL;
	size_t offset;
761
	int ndescs;
762 763

	srcu_key = srcu_read_lock(&dev->mr_srcu);
A
Artemy Kovalyov 已提交
764 765 766 767 768

	io_virt += *bytes_committed;
	bcnt -= *bytes_committed;

next_mr:
769
	mmkey = xa_load(&dev->mdev->priv.mkey_table, mlx5_base_mkey(key));
770
	if (!mkey_is_eq(mmkey, key)) {
A
Artemy Kovalyov 已提交
771
		mlx5_ib_dbg(dev, "failed to find mkey %x\n", key);
772 773 774
		ret = -EFAULT;
		goto srcu_unlock;
	}
A
Artemy Kovalyov 已提交
775

M
Moni Shoua 已提交
776 777 778 779 780 781
	if (prefetch && mmkey->type != MLX5_MKEY_MR) {
		mlx5_ib_dbg(dev, "prefetch is allowed only for MR\n");
		ret = -EINVAL;
		goto srcu_unlock;
	}

A
Artemy Kovalyov 已提交
782 783 784 785 786 787 788 789 790
	switch (mmkey->type) {
	case MLX5_MKEY_MR:
		mr = container_of(mmkey, struct mlx5_ib_mr, mmkey);
		if (!mr->live || !mr->ibmr.pd) {
			mlx5_ib_dbg(dev, "got dead MR\n");
			ret = -EFAULT;
			goto srcu_unlock;
		}

791 792 793 794 795 796 797 798 799
		if (prefetch) {
			if (!is_odp_mr(mr) ||
			    mr->ibmr.pd != pd) {
				mlx5_ib_dbg(dev, "Invalid prefetch request: %s\n",
					    is_odp_mr(mr) ?  "MR is not ODP" :
					    "PD is not of the MR");
				ret = -EINVAL;
				goto srcu_unlock;
			}
M
Moni Shoua 已提交
800 801
		}

802
		if (!is_odp_mr(mr)) {
803 804 805 806
			mlx5_ib_dbg(dev, "skipping non ODP MR (lkey=0x%06x) in page fault handler.\n",
				    key);
			if (bytes_mapped)
				*bytes_mapped += bcnt;
807
			ret = 0;
808 809 810
			goto srcu_unlock;
		}

M
Moni Shoua 已提交
811
		ret = pagefault_mr(dev, mr, io_virt, bcnt, bytes_mapped, flags);
A
Artemy Kovalyov 已提交
812 813 814 815 816 817 818 819
		if (ret < 0)
			goto srcu_unlock;

		npages += ret;
		ret = 0;
		break;

	case MLX5_MKEY_MW:
820 821
	case MLX5_MKEY_INDIRECT_DEVX:
		ndescs = get_indirect_num_descs(mmkey);
A
Artemy Kovalyov 已提交
822 823 824 825 826 827 828 829

		if (depth >= MLX5_CAP_GEN(dev->mdev, max_indirection)) {
			mlx5_ib_dbg(dev, "indirection level exceeded\n");
			ret = -EFAULT;
			goto srcu_unlock;
		}

		outlen = MLX5_ST_SZ_BYTES(query_mkey_out) +
830
			sizeof(*pklm) * (ndescs - 2);
A
Artemy Kovalyov 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844

		if (outlen > cur_outlen) {
			kfree(out);
			out = kzalloc(outlen, GFP_KERNEL);
			if (!out) {
				ret = -ENOMEM;
				goto srcu_unlock;
			}
			cur_outlen = outlen;
		}

		pklm = (struct mlx5_klm *)MLX5_ADDR_OF(query_mkey_out, out,
						       bsf0_klm0_pas_mtt0_1);

845
		ret = mlx5_core_query_mkey(dev->mdev, mmkey, out, outlen);
A
Artemy Kovalyov 已提交
846 847 848 849 850 851
		if (ret)
			goto srcu_unlock;

		offset = io_virt - MLX5_GET64(query_mkey_out, out,
					      memory_key_mkey_entry.start_addr);

852
		for (i = 0; bcnt && i < ndescs; i++, pklm++) {
A
Artemy Kovalyov 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
			if (offset >= be32_to_cpu(pklm->bcount)) {
				offset -= be32_to_cpu(pklm->bcount);
				continue;
			}

			frame = kzalloc(sizeof(*frame), GFP_KERNEL);
			if (!frame) {
				ret = -ENOMEM;
				goto srcu_unlock;
			}

			frame->key = be32_to_cpu(pklm->key);
			frame->io_virt = be64_to_cpu(pklm->va) + offset;
			frame->bcnt = min_t(size_t, bcnt,
					    be32_to_cpu(pklm->bcount) - offset);
			frame->depth = depth + 1;
			frame->next = head;
			head = frame;

			bcnt -= frame->bcnt;
873
			offset = 0;
A
Artemy Kovalyov 已提交
874 875 876 877 878 879
		}
		break;

	default:
		mlx5_ib_dbg(dev, "wrong mkey type %d\n", mmkey->type);
		ret = -EFAULT;
880 881 882
		goto srcu_unlock;
	}

A
Artemy Kovalyov 已提交
883 884 885 886 887 888 889 890 891
	if (head) {
		frame = head;
		head = frame->next;

		key = frame->key;
		io_virt = frame->io_virt;
		bcnt = frame->bcnt;
		depth = frame->depth;
		kfree(frame);
892

A
Artemy Kovalyov 已提交
893 894
		goto next_mr;
	}
895 896

srcu_unlock:
A
Artemy Kovalyov 已提交
897 898 899 900 901 902 903
	while (head) {
		frame = head;
		head = frame->next;
		kfree(frame);
	}
	kfree(out);

904
	srcu_read_unlock(&dev->mr_srcu, srcu_key);
905
	*bytes_committed = 0;
H
Haggai Eran 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
	return ret ? ret : npages;
}

/**
 * Parse a series of data segments for page fault handling.
 *
 * @pfault contains page fault information.
 * @wqe points at the first data segment in the WQE.
 * @wqe_end points after the end of the WQE.
 * @bytes_mapped receives the number of bytes that the function was able to
 *               map. This allows the caller to decide intelligently whether
 *               enough memory was mapped to resolve the page fault
 *               successfully (e.g. enough for the next MTU, or the entire
 *               WQE).
 * @total_wqe_bytes receives the total data size of this WQE in bytes (minus
 *                  the committed bytes).
 *
 * Returns the number of pages loaded if positive, zero for an empty WQE, or a
 * negative error code.
 */
926 927
static int pagefault_data_segments(struct mlx5_ib_dev *dev,
				   struct mlx5_pagefault *pfault,
928
				   void *wqe,
H
Haggai Eran 已提交
929
				   void *wqe_end, u32 *bytes_mapped,
930
				   u32 *total_wqe_bytes, bool receive_queue)
H
Haggai Eran 已提交
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
{
	int ret = 0, npages = 0;
	u64 io_virt;
	u32 key;
	u32 byte_count;
	size_t bcnt;
	int inline_segment;

	if (bytes_mapped)
		*bytes_mapped = 0;
	if (total_wqe_bytes)
		*total_wqe_bytes = 0;

	while (wqe < wqe_end) {
		struct mlx5_wqe_data_seg *dseg = wqe;

		io_virt = be64_to_cpu(dseg->addr);
		key = be32_to_cpu(dseg->lkey);
		byte_count = be32_to_cpu(dseg->byte_count);
		inline_segment = !!(byte_count &  MLX5_INLINE_SEG);
		bcnt	       = byte_count & ~MLX5_INLINE_SEG;

		if (inline_segment) {
			bcnt = bcnt & MLX5_WQE_INLINE_SEG_BYTE_COUNT_MASK;
			wqe += ALIGN(sizeof(struct mlx5_wqe_inline_seg) + bcnt,
				     16);
		} else {
			wqe += sizeof(*dseg);
		}

		/* receive WQE end of sg list. */
		if (receive_queue && bcnt == 0 && key == MLX5_INVALID_LKEY &&
		    io_virt == 0)
			break;

		if (!inline_segment && total_wqe_bytes) {
			*total_wqe_bytes += bcnt - min_t(size_t, bcnt,
968
					pfault->bytes_committed);
H
Haggai Eran 已提交
969 970 971 972 973 974
		}

		/* A zero length data segment designates a length of 2GB. */
		if (bcnt == 0)
			bcnt = 1U << 31;

975 976
		if (inline_segment || bcnt <= pfault->bytes_committed) {
			pfault->bytes_committed -=
H
Haggai Eran 已提交
977
				min_t(size_t, bcnt,
978
				      pfault->bytes_committed);
H
Haggai Eran 已提交
979 980 981
			continue;
		}

982 983
		ret = pagefault_single_data_segment(dev, NULL, key,
						    io_virt, bcnt,
984
						    &pfault->bytes_committed,
M
Moni Shoua 已提交
985
						    bytes_mapped, 0);
H
Haggai Eran 已提交
986 987 988 989 990 991 992 993
		if (ret < 0)
			break;
		npages += ret;
	}

	return ret < 0 ? ret : npages;
}

994 995 996 997 998 999 1000 1001 1002 1003 1004
static const u32 mlx5_ib_odp_opcode_cap[] = {
	[MLX5_OPCODE_SEND]	       = IB_ODP_SUPPORT_SEND,
	[MLX5_OPCODE_SEND_IMM]	       = IB_ODP_SUPPORT_SEND,
	[MLX5_OPCODE_SEND_INVAL]       = IB_ODP_SUPPORT_SEND,
	[MLX5_OPCODE_RDMA_WRITE]       = IB_ODP_SUPPORT_WRITE,
	[MLX5_OPCODE_RDMA_WRITE_IMM]   = IB_ODP_SUPPORT_WRITE,
	[MLX5_OPCODE_RDMA_READ]	       = IB_ODP_SUPPORT_READ,
	[MLX5_OPCODE_ATOMIC_CS]	       = IB_ODP_SUPPORT_ATOMIC,
	[MLX5_OPCODE_ATOMIC_FA]	       = IB_ODP_SUPPORT_ATOMIC,
};

H
Haggai Eran 已提交
1005 1006 1007 1008 1009
/*
 * Parse initiator WQE. Advances the wqe pointer to point at the
 * scatter-gather list, and set wqe_end to the end of the WQE.
 */
static int mlx5_ib_mr_initiator_pfault_handler(
1010 1011
	struct mlx5_ib_dev *dev, struct mlx5_pagefault *pfault,
	struct mlx5_ib_qp *qp, void **wqe, void **wqe_end, int wqe_length)
H
Haggai Eran 已提交
1012 1013
{
	struct mlx5_wqe_ctrl_seg *ctrl = *wqe;
1014
	u16 wqe_index = pfault->wqe.wqe_index;
1015 1016
	u32 transport_caps;
	struct mlx5_base_av *av;
H
Haggai Eran 已提交
1017 1018 1019 1020
	unsigned ds, opcode;
#if defined(DEBUG)
	u32 ctrl_wqe_index, ctrl_qpn;
#endif
1021
	u32 qpn = qp->trans_qp.base.mqp.qpn;
H
Haggai Eran 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

	ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
	if (ds * MLX5_WQE_DS_UNITS > wqe_length) {
		mlx5_ib_err(dev, "Unable to read the complete WQE. ds = 0x%x, ret = 0x%x\n",
			    ds, wqe_length);
		return -EFAULT;
	}

	if (ds == 0) {
		mlx5_ib_err(dev, "Got WQE with zero DS. wqe_index=%x, qpn=%x\n",
1032
			    wqe_index, qpn);
H
Haggai Eran 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041
		return -EFAULT;
	}

#if defined(DEBUG)
	ctrl_wqe_index = (be32_to_cpu(ctrl->opmod_idx_opcode) &
			MLX5_WQE_CTRL_WQE_INDEX_MASK) >>
			MLX5_WQE_CTRL_WQE_INDEX_SHIFT;
	if (wqe_index != ctrl_wqe_index) {
		mlx5_ib_err(dev, "Got WQE with invalid wqe_index. wqe_index=0x%x, qpn=0x%x ctrl->wqe_index=0x%x\n",
1042
			    wqe_index, qpn,
H
Haggai Eran 已提交
1043 1044 1045 1046 1047 1048
			    ctrl_wqe_index);
		return -EFAULT;
	}

	ctrl_qpn = (be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_QPN_MASK) >>
		MLX5_WQE_CTRL_QPN_SHIFT;
1049
	if (qpn != ctrl_qpn) {
H
Haggai Eran 已提交
1050
		mlx5_ib_err(dev, "Got WQE with incorrect QP number. wqe_index=0x%x, qpn=0x%x ctrl->qpn=0x%x\n",
1051
			    wqe_index, qpn,
H
Haggai Eran 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
			    ctrl_qpn);
		return -EFAULT;
	}
#endif /* DEBUG */

	*wqe_end = *wqe + ds * MLX5_WQE_DS_UNITS;
	*wqe += sizeof(*ctrl);

	opcode = be32_to_cpu(ctrl->opmod_idx_opcode) &
		 MLX5_WQE_CTRL_OPCODE_MASK;
1062

H
Haggai Eran 已提交
1063
	switch (qp->ibqp.qp_type) {
1064 1065 1066 1067
	case IB_QPT_XRC_INI:
		*wqe += sizeof(struct mlx5_wqe_xrc_seg);
		transport_caps = dev->odp_caps.per_transport_caps.xrc_odp_caps;
		break;
H
Haggai Eran 已提交
1068
	case IB_QPT_RC:
1069
		transport_caps = dev->odp_caps.per_transport_caps.rc_odp_caps;
H
Haggai Eran 已提交
1070 1071
		break;
	case IB_QPT_UD:
1072
		transport_caps = dev->odp_caps.per_transport_caps.ud_odp_caps;
H
Haggai Eran 已提交
1073 1074
		break;
	default:
1075 1076
		mlx5_ib_err(dev, "ODP fault on QP of an unsupported transport 0x%x\n",
			    qp->ibqp.qp_type);
H
Haggai Eran 已提交
1077 1078 1079
		return -EFAULT;
	}

J
Jérémy Lefaure 已提交
1080 1081
	if (unlikely(opcode >= ARRAY_SIZE(mlx5_ib_odp_opcode_cap) ||
		     !(transport_caps & mlx5_ib_odp_opcode_cap[opcode]))) {
1082 1083 1084 1085 1086
		mlx5_ib_err(dev, "ODP fault on QP of an unsupported opcode 0x%x\n",
			    opcode);
		return -EFAULT;
	}

1087
	if (qp->ibqp.qp_type == IB_QPT_UD) {
1088
		av = *wqe;
1089
		if (av->dqp_dct & cpu_to_be32(MLX5_EXTENDED_UD_AV))
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
			*wqe += sizeof(struct mlx5_av);
		else
			*wqe += sizeof(struct mlx5_base_av);
	}

	switch (opcode) {
	case MLX5_OPCODE_RDMA_WRITE:
	case MLX5_OPCODE_RDMA_WRITE_IMM:
	case MLX5_OPCODE_RDMA_READ:
		*wqe += sizeof(struct mlx5_wqe_raddr_seg);
		break;
	case MLX5_OPCODE_ATOMIC_CS:
	case MLX5_OPCODE_ATOMIC_FA:
		*wqe += sizeof(struct mlx5_wqe_raddr_seg);
		*wqe += sizeof(struct mlx5_wqe_atomic_seg);
		break;
	}

H
Haggai Eran 已提交
1108 1109 1110 1111
	return 0;
}

/*
1112
 * Parse responder WQE and set wqe_end to the end of the WQE.
H
Haggai Eran 已提交
1113
 */
M
Moni Shoua 已提交
1114 1115 1116 1117
static int mlx5_ib_mr_responder_pfault_handler_srq(struct mlx5_ib_dev *dev,
						   struct mlx5_ib_srq *srq,
						   void **wqe, void **wqe_end,
						   int wqe_length)
H
Haggai Eran 已提交
1118
{
M
Moni Shoua 已提交
1119
	int wqe_size = 1 << srq->msrq.wqe_shift;
H
Haggai Eran 已提交
1120

M
Moni Shoua 已提交
1121 1122
	if (wqe_size > wqe_length) {
		mlx5_ib_err(dev, "Couldn't read all of the receive WQE's content\n");
H
Haggai Eran 已提交
1123 1124 1125
		return -EFAULT;
	}

M
Moni Shoua 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	*wqe_end = *wqe + wqe_size;
	*wqe += sizeof(struct mlx5_wqe_srq_next_seg);

	return 0;
}

static int mlx5_ib_mr_responder_pfault_handler_rq(struct mlx5_ib_dev *dev,
						  struct mlx5_ib_qp *qp,
						  void *wqe, void **wqe_end,
						  int wqe_length)
{
	struct mlx5_ib_wq *wq = &qp->rq;
	int wqe_size = 1 << wq->wqe_shift;

H
Haggai Eran 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
	if (qp->wq_sig) {
		mlx5_ib_err(dev, "ODP fault with WQE signatures is not supported\n");
		return -EFAULT;
	}

	if (wqe_size > wqe_length) {
		mlx5_ib_err(dev, "Couldn't read all of the receive WQE's content\n");
		return -EFAULT;
	}

	switch (qp->ibqp.qp_type) {
	case IB_QPT_RC:
		if (!(dev->odp_caps.per_transport_caps.rc_odp_caps &
		      IB_ODP_SUPPORT_RECV))
			goto invalid_transport_or_opcode;
		break;
	default:
invalid_transport_or_opcode:
		mlx5_ib_err(dev, "ODP fault on QP of an unsupported transport. transport: 0x%x\n",
			    qp->ibqp.qp_type);
		return -EFAULT;
	}

1163
	*wqe_end = wqe + wqe_size;
H
Haggai Eran 已提交
1164 1165 1166 1167

	return 0;
}

1168 1169
static inline struct mlx5_core_rsc_common *odp_get_rsc(struct mlx5_ib_dev *dev,
						       u32 wq_num, int pf_type)
1170
{
1171 1172
	struct mlx5_core_rsc_common *common = NULL;
	struct mlx5_core_srq *srq;
1173

1174 1175
	switch (pf_type) {
	case MLX5_WQE_PF_TYPE_RMP:
1176 1177 1178
		srq = mlx5_cmd_get_srq(dev, wq_num);
		if (srq)
			common = &srq->common;
1179 1180 1181 1182
		break;
	case MLX5_WQE_PF_TYPE_REQ_SEND_OR_WRITE:
	case MLX5_WQE_PF_TYPE_RESP:
	case MLX5_WQE_PF_TYPE_REQ_READ_OR_ATOMIC:
1183
		common = mlx5_core_res_hold(dev->mdev, wq_num, MLX5_RES_QP);
1184 1185
		break;
	default:
1186
		break;
1187 1188
	}

1189
	return common;
1190 1191 1192 1193 1194 1195
}

static inline struct mlx5_ib_qp *res_to_qp(struct mlx5_core_rsc_common *res)
{
	struct mlx5_core_qp *mqp = (struct mlx5_core_qp *)res;

1196 1197 1198
	return to_mibqp(mqp);
}

M
Moni Shoua 已提交
1199 1200 1201 1202 1203 1204 1205 1206
static inline struct mlx5_ib_srq *res_to_srq(struct mlx5_core_rsc_common *res)
{
	struct mlx5_core_srq *msrq =
		container_of(res, struct mlx5_core_srq, common);

	return to_mibsrq(msrq);
}

1207 1208
static void mlx5_ib_mr_wqe_pfault_handler(struct mlx5_ib_dev *dev,
					  struct mlx5_pagefault *pfault)
H
Haggai Eran 已提交
1209
{
1210 1211 1212
	bool sq = pfault->type & MLX5_PFAULT_REQUESTOR;
	u16 wqe_index = pfault->wqe.wqe_index;
	void *wqe = NULL, *wqe_end = NULL;
H
Haggai Eran 已提交
1213
	u32 bytes_mapped, total_wqe_bytes;
1214
	struct mlx5_core_rsc_common *res;
1215
	int resume_with_error = 1;
1216
	struct mlx5_ib_qp *qp;
1217
	size_t bytes_copied;
1218
	int ret = 0;
H
Haggai Eran 已提交
1219

1220 1221 1222 1223 1224 1225
	res = odp_get_rsc(dev, pfault->wqe.wq_num, pfault->type);
	if (!res) {
		mlx5_ib_dbg(dev, "wqe page fault for missing resource %d\n", pfault->wqe.wq_num);
		return;
	}

1226 1227 1228 1229
	if (res->res != MLX5_RES_QP && res->res != MLX5_RES_SRQ &&
	    res->res != MLX5_RES_XSRQ) {
		mlx5_ib_err(dev, "wqe page fault for unsupported type %d\n",
			    pfault->type);
1230 1231 1232
		goto resolve_page_fault;
	}

1233 1234
	wqe = (void *)__get_free_page(GFP_KERNEL);
	if (!wqe) {
H
Haggai Eran 已提交
1235 1236 1237 1238
		mlx5_ib_err(dev, "Error allocating memory for IO page fault handling.\n");
		goto resolve_page_fault;
	}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	qp = (res->res == MLX5_RES_QP) ? res_to_qp(res) : NULL;
	if (qp && sq) {
		ret = mlx5_ib_read_user_wqe_sq(qp, wqe_index, wqe, PAGE_SIZE,
					       &bytes_copied);
		if (ret)
			goto read_user;
		ret = mlx5_ib_mr_initiator_pfault_handler(
			dev, pfault, qp, &wqe, &wqe_end, bytes_copied);
	} else if (qp && !sq) {
		ret = mlx5_ib_read_user_wqe_rq(qp, wqe_index, wqe, PAGE_SIZE,
					       &bytes_copied);
		if (ret)
			goto read_user;
		ret = mlx5_ib_mr_responder_pfault_handler_rq(
			dev, qp, wqe, &wqe_end, bytes_copied);
	} else if (!qp) {
		struct mlx5_ib_srq *srq = res_to_srq(res);

		ret = mlx5_ib_read_user_wqe_srq(srq, wqe_index, wqe, PAGE_SIZE,
M
Moni Shoua 已提交
1258
						&bytes_copied);
1259 1260 1261 1262
		if (ret)
			goto read_user;
		ret = mlx5_ib_mr_responder_pfault_handler_srq(
			dev, srq, &wqe, &wqe_end, bytes_copied);
M
Moni Shoua 已提交
1263
	}
1264

1265
	if (ret < 0 || wqe >= wqe_end)
H
Haggai Eran 已提交
1266 1267
		goto resolve_page_fault;

1268 1269 1270 1271
	ret = pagefault_data_segments(dev, pfault, wqe, wqe_end, &bytes_mapped,
				      &total_wqe_bytes, !sq);
	if (ret == -EAGAIN)
		goto out;
M
Moni Shoua 已提交
1272

1273
	if (ret < 0 || total_wqe_bytes > bytes_mapped)
H
Haggai Eran 已提交
1274 1275
		goto resolve_page_fault;

1276 1277 1278
out:
	ret = 0;
	resume_with_error = 0;
H
Haggai Eran 已提交
1279

1280 1281 1282 1283 1284 1285
read_user:
	if (ret)
		mlx5_ib_err(
			dev,
			"Failed reading a WQE following page fault, error %d, wqe_index %x, qpn %x\n",
			ret, wqe_index, pfault->token);
H
Haggai Eran 已提交
1286 1287

resolve_page_fault:
1288 1289
	mlx5_ib_page_fault_resume(dev, pfault, resume_with_error);
	mlx5_ib_dbg(dev, "PAGE FAULT completed. QP 0x%x resume_with_error=%d, type: 0x%x\n",
1290
		    pfault->wqe.wq_num, resume_with_error,
1291
		    pfault->type);
1292
	mlx5_core_res_put(res);
1293
	free_page((unsigned long)wqe);
H
Haggai Eran 已提交
1294 1295
}

1296 1297 1298 1299 1300 1301
static int pages_in_range(u64 address, u32 length)
{
	return (ALIGN(address + length, PAGE_SIZE) -
		(address & PAGE_MASK)) >> PAGE_SHIFT;
}

1302 1303
static void mlx5_ib_mr_rdma_pfault_handler(struct mlx5_ib_dev *dev,
					   struct mlx5_pagefault *pfault)
1304 1305 1306
{
	u64 address;
	u32 length;
1307
	u32 prefetch_len = pfault->bytes_committed;
1308
	int prefetch_activated = 0;
1309
	u32 rkey = pfault->rdma.r_key;
1310 1311 1312 1313 1314 1315 1316 1317
	int ret;

	/* The RDMA responder handler handles the page fault in two parts.
	 * First it brings the necessary pages for the current packet
	 * (and uses the pfault context), and then (after resuming the QP)
	 * prefetches more pages. The second operation cannot use the pfault
	 * context and therefore uses the dummy_pfault context allocated on
	 * the stack */
1318 1319 1320 1321
	pfault->rdma.rdma_va += pfault->bytes_committed;
	pfault->rdma.rdma_op_len -= min(pfault->bytes_committed,
					 pfault->rdma.rdma_op_len);
	pfault->bytes_committed = 0;
1322

1323 1324
	address = pfault->rdma.rdma_va;
	length  = pfault->rdma.rdma_op_len;
1325 1326 1327 1328 1329 1330

	/* For some operations, the hardware cannot tell the exact message
	 * length, and in those cases it reports zero. Use prefetch
	 * logic. */
	if (length == 0) {
		prefetch_activated = 1;
1331
		length = pfault->rdma.packet_size;
1332 1333 1334
		prefetch_len = min(MAX_PREFETCH_LEN, prefetch_len);
	}

1335
	ret = pagefault_single_data_segment(dev, NULL, rkey, address, length,
M
Moni Shoua 已提交
1336 1337
					    &pfault->bytes_committed, NULL,
					    0);
1338 1339 1340 1341
	if (ret == -EAGAIN) {
		/* We're racing with an invalidation, don't prefetch */
		prefetch_activated = 0;
	} else if (ret < 0 || pages_in_range(address, length) > ret) {
1342 1343
		mlx5_ib_page_fault_resume(dev, pfault, 1);
		if (ret != -ENOENT)
1344 1345
			mlx5_ib_dbg(dev, "PAGE FAULT error %d. QP 0x%x, type: 0x%x\n",
				    ret, pfault->token, pfault->type);
1346 1347 1348
		return;
	}

1349 1350 1351 1352
	mlx5_ib_page_fault_resume(dev, pfault, 0);
	mlx5_ib_dbg(dev, "PAGE FAULT completed. QP 0x%x, type: 0x%x, prefetch_activated: %d\n",
		    pfault->token, pfault->type,
		    prefetch_activated);
1353 1354 1355 1356 1357 1358 1359

	/* At this point, there might be a new pagefault already arriving in
	 * the eq, switch to the dummy pagefault for the rest of the
	 * processing. We're still OK with the objects being alive as the
	 * work-queue is being fenced. */

	if (prefetch_activated) {
1360 1361
		u32 bytes_committed = 0;

1362
		ret = pagefault_single_data_segment(dev, NULL, rkey, address,
1363
						    prefetch_len,
M
Moni Shoua 已提交
1364 1365
						    &bytes_committed, NULL,
						    0);
1366
		if (ret < 0 && ret != -EAGAIN) {
1367 1368
			mlx5_ib_dbg(dev, "Prefetch failed. ret: %d, QP 0x%x, address: 0x%.16llx, length = 0x%.16x\n",
				    ret, pfault->token, address, prefetch_len);
1369 1370 1371 1372
		}
	}
}

1373
static void mlx5_ib_pfault(struct mlx5_ib_dev *dev, struct mlx5_pagefault *pfault)
1374
{
1375
	u8 event_subtype = pfault->event_subtype;
1376 1377

	switch (event_subtype) {
H
Haggai Eran 已提交
1378
	case MLX5_PFAULT_SUBTYPE_WQE:
1379
		mlx5_ib_mr_wqe_pfault_handler(dev, pfault);
H
Haggai Eran 已提交
1380
		break;
1381
	case MLX5_PFAULT_SUBTYPE_RDMA:
1382
		mlx5_ib_mr_rdma_pfault_handler(dev, pfault);
1383
		break;
1384
	default:
1385 1386 1387
		mlx5_ib_err(dev, "Invalid page fault event subtype: 0x%x\n",
			    event_subtype);
		mlx5_ib_page_fault_resume(dev, pfault, 1);
1388 1389 1390
	}
}

1391 1392 1393 1394 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 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
static void mlx5_ib_eqe_pf_action(struct work_struct *work)
{
	struct mlx5_pagefault *pfault = container_of(work,
						     struct mlx5_pagefault,
						     work);
	struct mlx5_ib_pf_eq *eq = pfault->eq;

	mlx5_ib_pfault(eq->dev, pfault);
	mempool_free(pfault, eq->pool);
}

static void mlx5_ib_eq_pf_process(struct mlx5_ib_pf_eq *eq)
{
	struct mlx5_eqe_page_fault *pf_eqe;
	struct mlx5_pagefault *pfault;
	struct mlx5_eqe *eqe;
	int cc = 0;

	while ((eqe = mlx5_eq_get_eqe(eq->core, cc))) {
		pfault = mempool_alloc(eq->pool, GFP_ATOMIC);
		if (!pfault) {
			schedule_work(&eq->work);
			break;
		}

		pf_eqe = &eqe->data.page_fault;
		pfault->event_subtype = eqe->sub_type;
		pfault->bytes_committed = be32_to_cpu(pf_eqe->bytes_committed);

		mlx5_ib_dbg(eq->dev,
			    "PAGE_FAULT: subtype: 0x%02x, bytes_committed: 0x%06x\n",
			    eqe->sub_type, pfault->bytes_committed);

		switch (eqe->sub_type) {
		case MLX5_PFAULT_SUBTYPE_RDMA:
			/* RDMA based event */
			pfault->type =
				be32_to_cpu(pf_eqe->rdma.pftype_token) >> 24;
			pfault->token =
				be32_to_cpu(pf_eqe->rdma.pftype_token) &
				MLX5_24BIT_MASK;
			pfault->rdma.r_key =
				be32_to_cpu(pf_eqe->rdma.r_key);
			pfault->rdma.packet_size =
				be16_to_cpu(pf_eqe->rdma.packet_length);
			pfault->rdma.rdma_op_len =
				be32_to_cpu(pf_eqe->rdma.rdma_op_len);
			pfault->rdma.rdma_va =
				be64_to_cpu(pf_eqe->rdma.rdma_va);
			mlx5_ib_dbg(eq->dev,
				    "PAGE_FAULT: type:0x%x, token: 0x%06x, r_key: 0x%08x\n",
				    pfault->type, pfault->token,
				    pfault->rdma.r_key);
			mlx5_ib_dbg(eq->dev,
				    "PAGE_FAULT: rdma_op_len: 0x%08x, rdma_va: 0x%016llx\n",
				    pfault->rdma.rdma_op_len,
				    pfault->rdma.rdma_va);
			break;

		case MLX5_PFAULT_SUBTYPE_WQE:
			/* WQE based event */
			pfault->type =
				(be32_to_cpu(pf_eqe->wqe.pftype_wq) >> 24) & 0x7;
			pfault->token =
				be32_to_cpu(pf_eqe->wqe.token);
			pfault->wqe.wq_num =
				be32_to_cpu(pf_eqe->wqe.pftype_wq) &
				MLX5_24BIT_MASK;
			pfault->wqe.wqe_index =
				be16_to_cpu(pf_eqe->wqe.wqe_index);
			pfault->wqe.packet_size =
				be16_to_cpu(pf_eqe->wqe.packet_length);
			mlx5_ib_dbg(eq->dev,
				    "PAGE_FAULT: type:0x%x, token: 0x%06x, wq_num: 0x%06x, wqe_index: 0x%04x\n",
				    pfault->type, pfault->token,
				    pfault->wqe.wq_num,
				    pfault->wqe.wqe_index);
			break;

		default:
			mlx5_ib_warn(eq->dev,
				     "Unsupported page fault event sub-type: 0x%02hhx\n",
				     eqe->sub_type);
			/* Unsupported page faults should still be
			 * resolved by the page fault handler
			 */
		}

		pfault->eq = eq;
		INIT_WORK(&pfault->work, mlx5_ib_eqe_pf_action);
		queue_work(eq->wq, &pfault->work);

		cc = mlx5_eq_update_cc(eq->core, ++cc);
	}

	mlx5_eq_update_ci(eq->core, cc, 1);
}

1489 1490
static int mlx5_ib_eq_pf_int(struct notifier_block *nb, unsigned long type,
			     void *data)
1491
{
1492 1493
	struct mlx5_ib_pf_eq *eq =
		container_of(nb, struct mlx5_ib_pf_eq, irq_nb);
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 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 1547 1548 1549 1550 1551 1552 1553 1554 1555
	unsigned long flags;

	if (spin_trylock_irqsave(&eq->lock, flags)) {
		mlx5_ib_eq_pf_process(eq);
		spin_unlock_irqrestore(&eq->lock, flags);
	} else {
		schedule_work(&eq->work);
	}

	return IRQ_HANDLED;
}

/* mempool_refill() was proposed but unfortunately wasn't accepted
 * http://lkml.iu.edu/hypermail/linux/kernel/1512.1/05073.html
 * Cheap workaround.
 */
static void mempool_refill(mempool_t *pool)
{
	while (pool->curr_nr < pool->min_nr)
		mempool_free(mempool_alloc(pool, GFP_KERNEL), pool);
}

static void mlx5_ib_eq_pf_action(struct work_struct *work)
{
	struct mlx5_ib_pf_eq *eq =
		container_of(work, struct mlx5_ib_pf_eq, work);

	mempool_refill(eq->pool);

	spin_lock_irq(&eq->lock);
	mlx5_ib_eq_pf_process(eq);
	spin_unlock_irq(&eq->lock);
}

enum {
	MLX5_IB_NUM_PF_EQE	= 0x1000,
	MLX5_IB_NUM_PF_DRAIN	= 64,
};

static int
mlx5_ib_create_pf_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq)
{
	struct mlx5_eq_param param = {};
	int err;

	INIT_WORK(&eq->work, mlx5_ib_eq_pf_action);
	spin_lock_init(&eq->lock);
	eq->dev = dev;

	eq->pool = mempool_create_kmalloc_pool(MLX5_IB_NUM_PF_DRAIN,
					       sizeof(struct mlx5_pagefault));
	if (!eq->pool)
		return -ENOMEM;

	eq->wq = alloc_workqueue("mlx5_ib_page_fault",
				 WQ_HIGHPRI | WQ_UNBOUND | WQ_MEM_RECLAIM,
				 MLX5_NUM_CMD_EQE);
	if (!eq->wq) {
		err = -ENOMEM;
		goto err_mempool;
	}

1556
	eq->irq_nb.notifier_call = mlx5_ib_eq_pf_int;
1557
	param = (struct mlx5_eq_param) {
1558
		.irq_index = 0,
1559 1560
		.nent = MLX5_IB_NUM_PF_EQE,
	};
1561
	param.mask[0] = 1ull << MLX5_EVENT_TYPE_PAGE_FAULT;
1562
	eq->core = mlx5_eq_create_generic(dev->mdev, &param);
1563 1564 1565 1566
	if (IS_ERR(eq->core)) {
		err = PTR_ERR(eq->core);
		goto err_wq;
	}
1567 1568 1569 1570 1571
	err = mlx5_eq_enable(dev->mdev, eq->core, &eq->irq_nb);
	if (err) {
		mlx5_ib_err(dev, "failed to enable odp EQ %d\n", err);
		goto err_eq;
	}
1572 1573

	return 0;
1574 1575
err_eq:
	mlx5_eq_destroy_generic(dev->mdev, eq->core);
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
err_wq:
	destroy_workqueue(eq->wq);
err_mempool:
	mempool_destroy(eq->pool);
	return err;
}

static int
mlx5_ib_destroy_pf_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq)
{
	int err;

1588
	mlx5_eq_disable(dev->mdev, eq->core, &eq->irq_nb);
1589 1590 1591 1592 1593 1594 1595 1596
	err = mlx5_eq_destroy_generic(dev->mdev, eq->core);
	cancel_work_sync(&eq->work);
	destroy_workqueue(eq->wq);
	mempool_destroy(eq->pool);

	return err;
}

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent)
{
	if (!(ent->dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT))
		return;

	switch (ent->order - 2) {
	case MLX5_IMR_MTT_CACHE_ENTRY:
		ent->page = PAGE_SHIFT;
		ent->xlt = MLX5_IMR_MTT_ENTRIES *
			   sizeof(struct mlx5_mtt) /
			   MLX5_IB_UMR_OCTOWORD;
		ent->access_mode = MLX5_MKC_ACCESS_MODE_MTT;
		ent->limit = 0;
		break;

	case MLX5_IMR_KSM_CACHE_ENTRY:
		ent->page = MLX5_KSM_PAGE_SHIFT;
		ent->xlt = mlx5_imr_ksm_entries *
			   sizeof(struct mlx5_klm) /
			   MLX5_IB_UMR_OCTOWORD;
		ent->access_mode = MLX5_MKC_ACCESS_MODE_KSM;
		ent->limit = 0;
		break;
	}
}

M
Moni Shoua 已提交
1623 1624 1625 1626
static const struct ib_device_ops mlx5_ib_dev_odp_ops = {
	.advise_mr = mlx5_ib_advise_mr,
};

1627
int mlx5_ib_odp_init_one(struct mlx5_ib_dev *dev)
1628
{
1629
	int ret = 0;
1630

M
Moni Shoua 已提交
1631 1632 1633
	if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_odp_ops);

1634 1635 1636 1637 1638 1639 1640 1641
	if (dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT) {
		ret = mlx5_cmd_null_mkey(dev->mdev, &dev->null_mkey);
		if (ret) {
			mlx5_ib_err(dev, "Error getting null_mkey %d\n", ret);
			return ret;
		}
	}

1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
	if (!MLX5_CAP_GEN(dev->mdev, pg))
		return ret;

	ret = mlx5_ib_create_pf_eq(dev, &dev->odp_pf_eq);

	return ret;
}

void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *dev)
{
	if (!MLX5_CAP_GEN(dev->mdev, pg))
		return;

	mlx5_ib_destroy_pf_eq(dev, &dev->odp_pf_eq);
1656 1657
}

1658
int mlx5_ib_odp_init(void)
1659
{
1660 1661 1662 1663
	mlx5_imr_ksm_entries = BIT_ULL(get_order(TASK_SIZE) -
				       MLX5_IMR_MTT_BITS);

	return 0;
1664
}
M
Moni Shoua 已提交
1665 1666 1667

struct prefetch_mr_work {
	struct work_struct work;
1668
	struct ib_pd *pd;
M
Moni Shoua 已提交
1669 1670 1671 1672 1673
	u32 pf_flags;
	u32 num_sge;
	struct ib_sge sg_list[0];
};

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
static void num_pending_prefetch_dec(struct mlx5_ib_dev *dev,
				     struct ib_sge *sg_list, u32 num_sge,
				     u32 from)
{
	u32 i;
	int srcu_key;

	srcu_key = srcu_read_lock(&dev->mr_srcu);

	for (i = from; i < num_sge; ++i) {
		struct mlx5_core_mkey *mmkey;
		struct mlx5_ib_mr *mr;

1687 1688
		mmkey = xa_load(&dev->mdev->priv.mkey_table,
				mlx5_base_mkey(sg_list[i].lkey));
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
		mr = container_of(mmkey, struct mlx5_ib_mr, mmkey);
		atomic_dec(&mr->num_pending_prefetch);
	}

	srcu_read_unlock(&dev->mr_srcu, srcu_key);
}

static bool num_pending_prefetch_inc(struct ib_pd *pd,
				     struct ib_sge *sg_list, u32 num_sge)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	bool ret = true;
	u32 i;

	for (i = 0; i < num_sge; ++i) {
		struct mlx5_core_mkey *mmkey;
		struct mlx5_ib_mr *mr;

1707 1708
		mmkey = xa_load(&dev->mdev->priv.mkey_table,
				mlx5_base_mkey(sg_list[i].lkey));
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
		if (!mmkey || mmkey->key != sg_list[i].lkey) {
			ret = false;
			break;
		}

		if (mmkey->type != MLX5_MKEY_MR) {
			ret = false;
			break;
		}

		mr = container_of(mmkey, struct mlx5_ib_mr, mmkey);

		if (mr->ibmr.pd != pd) {
			ret = false;
			break;
		}

		if (!mr->live) {
			ret = false;
			break;
		}

		atomic_inc(&mr->num_pending_prefetch);
	}

	if (!ret)
		num_pending_prefetch_dec(dev, sg_list, i, 0);

	return ret;
}

1740
static int mlx5_ib_prefetch_sg_list(struct ib_pd *pd, u32 pf_flags,
M
Moni Shoua 已提交
1741 1742
				    struct ib_sge *sg_list, u32 num_sge)
{
1743 1744
	u32 i;
	int ret = 0;
1745
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
M
Moni Shoua 已提交
1746 1747 1748 1749 1750

	for (i = 0; i < num_sge; ++i) {
		struct ib_sge *sg = &sg_list[i];
		int bytes_committed = 0;

1751
		ret = pagefault_single_data_segment(dev, pd, sg->lkey, sg->addr,
M
Moni Shoua 已提交
1752 1753 1754 1755
						    sg->length,
						    &bytes_committed, NULL,
						    pf_flags);
		if (ret < 0)
1756
			break;
M
Moni Shoua 已提交
1757
	}
1758 1759

	return ret < 0 ? ret : 0;
M
Moni Shoua 已提交
1760 1761 1762 1763 1764 1765 1766
}

static void mlx5_ib_prefetch_mr_work(struct work_struct *work)
{
	struct prefetch_mr_work *w =
		container_of(work, struct prefetch_mr_work, work);

1767 1768
	if (ib_device_try_get(w->pd->device)) {
		mlx5_ib_prefetch_sg_list(w->pd, w->pf_flags, w->sg_list,
M
Moni Shoua 已提交
1769
					 w->num_sge);
1770
		ib_device_put(w->pd->device);
1771
	}
1772

1773 1774
	num_pending_prefetch_dec(to_mdev(w->pd->device), w->sg_list,
				 w->num_sge, 0);
1775
	kvfree(w);
M
Moni Shoua 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784
}

int mlx5_ib_advise_mr_prefetch(struct ib_pd *pd,
			       enum ib_uverbs_advise_mr_advice advice,
			       u32 flags, struct ib_sge *sg_list, u32 num_sge)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	u32 pf_flags = MLX5_PF_FLAGS_PREFETCH;
	struct prefetch_mr_work *work;
1785 1786
	bool valid_req;
	int srcu_key;
M
Moni Shoua 已提交
1787 1788 1789 1790 1791

	if (advice == IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH)
		pf_flags |= MLX5_PF_FLAGS_DOWNGRADE;

	if (flags & IB_UVERBS_ADVISE_MR_FLAG_FLUSH)
1792
		return mlx5_ib_prefetch_sg_list(pd, pf_flags, sg_list,
M
Moni Shoua 已提交
1793 1794 1795 1796 1797 1798 1799 1800
						num_sge);

	work = kvzalloc(struct_size(work, sg_list, num_sge), GFP_KERNEL);
	if (!work)
		return -ENOMEM;

	memcpy(work->sg_list, sg_list, num_sge * sizeof(struct ib_sge));

1801 1802 1803 1804 1805
	/* It is guaranteed that the pd when work is executed is the pd when
	 * work was queued since pd can't be destroyed while it holds MRs and
	 * destroying a MR leads to flushing the workquque
	 */
	work->pd = pd;
M
Moni Shoua 已提交
1806 1807 1808 1809
	work->pf_flags = pf_flags;
	work->num_sge = num_sge;

	INIT_WORK(&work->work, mlx5_ib_prefetch_mr_work);
1810 1811 1812 1813 1814 1815 1816

	srcu_key = srcu_read_lock(&dev->mr_srcu);

	valid_req = num_pending_prefetch_inc(pd, sg_list, num_sge);
	if (valid_req)
		queue_work(system_unbound_wq, &work->work);
	else
1817
		kvfree(work);
1818 1819 1820 1821

	srcu_read_unlock(&dev->mr_srcu, srcu_key);

	return valid_req ? 0 : -EINVAL;
M
Moni Shoua 已提交
1822
}