mr.c 57.7 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 33 34 35 36 37
 *
 * 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.
 */


#include <linux/kref.h>
#include <linux/random.h>
#include <linux/debugfs.h>
#include <linux/export.h>
E
Eli Cohen 已提交
38
#include <linux/delay.h>
39
#include <rdma/ib_umem.h>
40
#include <rdma/ib_umem_odp.h>
41
#include <rdma/ib_verbs.h>
42 43 44
#include "mlx5_ib.h"

enum {
E
Eli Cohen 已提交
45
	MAX_PENDING_REG_MR = 8,
46 47
};

48
#define MLX5_UMR_ALIGN 2048
49

50 51
static void clean_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr);
static void dereg_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr);
52
static int mr_cache_max_order(struct mlx5_ib_dev *dev);
53 54 55 56 57 58

static bool umr_can_use_indirect_mkey(struct mlx5_ib_dev *dev)
{
	return !MLX5_CAP_GEN(dev->mdev, umr_indirect_mkey_disabled);
}

59 60
static int destroy_mkey(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
{
61
	WARN_ON(xa_load(&dev->odp_mkeys, mlx5_base_mkey(mr->mmkey.key)));
62

63
	return mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
64 65
}

66 67 68 69 70 71 72 73 74 75
static int order2idx(struct mlx5_ib_dev *dev, int order)
{
	struct mlx5_mr_cache *cache = &dev->cache;

	if (order < cache->ent[0].order)
		return 0;
	else
		return order - cache->ent[0].order;
}

76 77 78 79 80 81
static bool use_umr_mtt_update(struct mlx5_ib_mr *mr, u64 start, u64 length)
{
	return ((u64)1 << mr->order) * MLX5_ADAPTER_PAGE_SIZE >=
		length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
}

82
static void reg_mr_callback(int status, struct mlx5_async_work *context)
E
Eli Cohen 已提交
83
{
84 85
	struct mlx5_ib_mr *mr =
		container_of(context, struct mlx5_ib_mr, cb_work);
E
Eli Cohen 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	struct mlx5_ib_dev *dev = mr->dev;
	struct mlx5_mr_cache *cache = &dev->cache;
	int c = order2idx(dev, mr->order);
	struct mlx5_cache_ent *ent = &cache->ent[c];
	u8 key;
	unsigned long flags;

	spin_lock_irqsave(&ent->lock, flags);
	ent->pending--;
	spin_unlock_irqrestore(&ent->lock, flags);
	if (status) {
		mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
		kfree(mr);
		dev->fill_delay = 1;
		mod_timer(&dev->delay_timer, jiffies + HZ);
		return;
	}

A
Artemy Kovalyov 已提交
104
	mr->mmkey.type = MLX5_MKEY_MR;
105 106 107
	spin_lock_irqsave(&dev->mdev->priv.mkey_lock, flags);
	key = dev->mdev->priv.mkey_key++;
	spin_unlock_irqrestore(&dev->mdev->priv.mkey_lock, flags);
108
	mr->mmkey.key = mlx5_idx_to_mkey(MLX5_GET(create_mkey_out, mr->out, mkey_index)) | key;
E
Eli Cohen 已提交
109 110 111 112 113 114 115 116

	cache->last_add = jiffies;

	spin_lock_irqsave(&ent->lock, flags);
	list_add_tail(&mr->list, &ent->head);
	ent->cur++;
	ent->size++;
	spin_unlock_irqrestore(&ent->lock, flags);
117

118 119
	if (!completion_done(&ent->compl))
		complete(&ent->compl);
E
Eli Cohen 已提交
120 121
}

122 123 124 125
static int add_keys(struct mlx5_ib_dev *dev, int c, int num)
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent = &cache->ent[c];
126
	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
127
	struct mlx5_ib_mr *mr;
128 129
	void *mkc;
	u32 *in;
130 131 132
	int err = 0;
	int i;

133
	in = kzalloc(inlen, GFP_KERNEL);
134 135 136
	if (!in)
		return -ENOMEM;

137
	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
138
	for (i = 0; i < num; i++) {
E
Eli Cohen 已提交
139 140 141 142 143
		if (ent->pending >= MAX_PENDING_REG_MR) {
			err = -EAGAIN;
			break;
		}

144 145 146
		mr = kzalloc(sizeof(*mr), GFP_KERNEL);
		if (!mr) {
			err = -ENOMEM;
E
Eli Cohen 已提交
147
			break;
148 149
		}
		mr->order = ent->order;
150
		mr->allocated_from_cache = true;
E
Eli Cohen 已提交
151
		mr->dev = dev;
152 153 154

		MLX5_SET(mkc, mkc, free, 1);
		MLX5_SET(mkc, mkc, umr_en, 1);
155 156 157
		MLX5_SET(mkc, mkc, access_mode_1_0, ent->access_mode & 0x3);
		MLX5_SET(mkc, mkc, access_mode_4_2,
			 (ent->access_mode >> 2) & 0x7);
158 159

		MLX5_SET(mkc, mkc, qpn, 0xffffff);
160 161
		MLX5_SET(mkc, mkc, translations_octword_size, ent->xlt);
		MLX5_SET(mkc, mkc, log_page_size, ent->page);
162

E
Eli Cohen 已提交
163 164 165
		spin_lock_irq(&ent->lock);
		ent->pending++;
		spin_unlock_irq(&ent->lock);
166
		err = mlx5_core_create_mkey_cb(dev->mdev, &mr->mmkey,
167
					       &dev->async_ctx, in, inlen,
168
					       mr->out, sizeof(mr->out),
169
					       reg_mr_callback, &mr->cb_work);
170
		if (err) {
E
Eli Cohen 已提交
171 172 173
			spin_lock_irq(&ent->lock);
			ent->pending--;
			spin_unlock_irq(&ent->lock);
174 175
			mlx5_ib_warn(dev, "create mkey failed %d\n", err);
			kfree(mr);
E
Eli Cohen 已提交
176
			break;
177 178 179 180 181 182 183 184 185 186 187
		}
	}

	kfree(in);
	return err;
}

static void remove_keys(struct mlx5_ib_dev *dev, int c, int num)
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent = &cache->ent[c];
188
	struct mlx5_ib_mr *tmp_mr;
189
	struct mlx5_ib_mr *mr;
190
	LIST_HEAD(del_list);
191 192 193
	int i;

	for (i = 0; i < num; i++) {
E
Eli Cohen 已提交
194
		spin_lock_irq(&ent->lock);
195
		if (list_empty(&ent->head)) {
E
Eli Cohen 已提交
196
			spin_unlock_irq(&ent->lock);
197
			break;
198 199
		}
		mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
200
		list_move(&mr->list, &del_list);
201 202
		ent->cur--;
		ent->size--;
E
Eli Cohen 已提交
203
		spin_unlock_irq(&ent->lock);
204 205 206 207 208 209
		mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
	}

	list_for_each_entry_safe(mr, tmp_mr, &del_list, list) {
		list_del(&mr->list);
		kfree(mr);
210 211 212 213 214 215 216 217
	}
}

static ssize_t size_write(struct file *filp, const char __user *buf,
			  size_t count, loff_t *pos)
{
	struct mlx5_cache_ent *ent = filp->private_data;
	struct mlx5_ib_dev *dev = ent->dev;
218
	char lbuf[20] = {0};
219 220 221 222
	u32 var;
	int err;
	int c;

223 224
	count = min(count, sizeof(lbuf) - 1);
	if (copy_from_user(lbuf, buf, count))
225
		return -EFAULT;
226 227 228 229 230 231 232 233 234 235

	c = order2idx(dev, ent->order);

	if (sscanf(lbuf, "%u", &var) != 1)
		return -EINVAL;

	if (var < ent->limit)
		return -EINVAL;

	if (var > ent->size) {
E
Eli Cohen 已提交
236 237 238 239 240 241 242
		do {
			err = add_keys(dev, c, var - ent->size);
			if (err && err != -EAGAIN)
				return err;

			usleep_range(3000, 5000);
		} while (err);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	} else if (var < ent->size) {
		remove_keys(dev, c, ent->size - var);
	}

	return count;
}

static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
			 loff_t *pos)
{
	struct mlx5_cache_ent *ent = filp->private_data;
	char lbuf[20];
	int err;

	err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->size);
	if (err < 0)
		return err;

261
	return simple_read_from_buffer(buf, count, pos, lbuf, err);
262 263 264 265 266 267 268 269 270 271 272 273 274 275
}

static const struct file_operations size_fops = {
	.owner	= THIS_MODULE,
	.open	= simple_open,
	.write	= size_write,
	.read	= size_read,
};

static ssize_t limit_write(struct file *filp, const char __user *buf,
			   size_t count, loff_t *pos)
{
	struct mlx5_cache_ent *ent = filp->private_data;
	struct mlx5_ib_dev *dev = ent->dev;
276
	char lbuf[20] = {0};
277 278 279 280
	u32 var;
	int err;
	int c;

281 282
	count = min(count, sizeof(lbuf) - 1);
	if (copy_from_user(lbuf, buf, count))
283
		return -EFAULT;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

	c = order2idx(dev, ent->order);

	if (sscanf(lbuf, "%u", &var) != 1)
		return -EINVAL;

	if (var > ent->size)
		return -EINVAL;

	ent->limit = var;

	if (ent->cur < ent->limit) {
		err = add_keys(dev, c, 2 * ent->limit - ent->cur);
		if (err)
			return err;
	}

	return count;
}

static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
			  loff_t *pos)
{
	struct mlx5_cache_ent *ent = filp->private_data;
	char lbuf[20];
	int err;

	err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
	if (err < 0)
		return err;

315
	return simple_read_from_buffer(buf, count, pos, lbuf, err);
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
}

static const struct file_operations limit_fops = {
	.owner	= THIS_MODULE,
	.open	= simple_open,
	.write	= limit_write,
	.read	= limit_read,
};

static int someone_adding(struct mlx5_mr_cache *cache)
{
	int i;

	for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
		if (cache->ent[i].cur < cache->ent[i].limit)
			return 1;
	}

	return 0;
}

static void __cache_work_func(struct mlx5_cache_ent *ent)
{
	struct mlx5_ib_dev *dev = ent->dev;
	struct mlx5_mr_cache *cache = &dev->cache;
	int i = order2idx(dev, ent->order);
E
Eli Cohen 已提交
342
	int err;
343 344 345 346 347

	if (cache->stopped)
		return;

	ent = &dev->cache.ent[i];
E
Eli Cohen 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	if (ent->cur < 2 * ent->limit && !dev->fill_delay) {
		err = add_keys(dev, i, 1);
		if (ent->cur < 2 * ent->limit) {
			if (err == -EAGAIN) {
				mlx5_ib_dbg(dev, "returned eagain, order %d\n",
					    i + 2);
				queue_delayed_work(cache->wq, &ent->dwork,
						   msecs_to_jiffies(3));
			} else if (err) {
				mlx5_ib_warn(dev, "command failed order %d, err %d\n",
					     i + 2, err);
				queue_delayed_work(cache->wq, &ent->dwork,
						   msecs_to_jiffies(1000));
			} else {
				queue_work(cache->wq, &ent->work);
			}
		}
365
	} else if (ent->cur > 2 * ent->limit) {
366 367 368 369 370 371 372 373 374 375 376 377 378
		/*
		 * The remove_keys() logic is performed as garbage collection
		 * task. Such task is intended to be run when no other active
		 * processes are running.
		 *
		 * The need_resched() will return TRUE if there are user tasks
		 * to be activated in near future.
		 *
		 * In such case, we don't execute remove_keys() and postpone
		 * the garbage collection work to try to run in next cycle,
		 * in order to free CPU resources to other tasks.
		 */
		if (!need_resched() && !someone_adding(cache) &&
E
Eli Cohen 已提交
379
		    time_after(jiffies, cache->last_add + 300 * HZ)) {
380 381 382 383
			remove_keys(dev, i, 1);
			if (ent->cur > ent->limit)
				queue_work(cache->wq, &ent->work);
		} else {
E
Eli Cohen 已提交
384
			queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		}
	}
}

static void delayed_cache_work_func(struct work_struct *work)
{
	struct mlx5_cache_ent *ent;

	ent = container_of(work, struct mlx5_cache_ent, dwork.work);
	__cache_work_func(ent);
}

static void cache_work_func(struct work_struct *work)
{
	struct mlx5_cache_ent *ent;

	ent = container_of(work, struct mlx5_cache_ent, work);
	__cache_work_func(ent);
}

405 406 407 408 409 410 411 412 413
struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int entry)
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent;
	struct mlx5_ib_mr *mr;
	int err;

	if (entry < 0 || entry >= MAX_MR_CACHE_ENTRIES) {
		mlx5_ib_err(dev, "cache entry %d is out of range\n", entry);
414
		return ERR_PTR(-EINVAL);
415 416 417 418 419 420 421 422 423
	}

	ent = &cache->ent[entry];
	while (1) {
		spin_lock_irq(&ent->lock);
		if (list_empty(&ent->head)) {
			spin_unlock_irq(&ent->lock);

			err = add_keys(dev, entry, 1);
424
			if (err && err != -EAGAIN)
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
				return ERR_PTR(err);

			wait_for_completion(&ent->compl);
		} else {
			mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
					      list);
			list_del(&mr->list);
			ent->cur--;
			spin_unlock_irq(&ent->lock);
			if (ent->cur < ent->limit)
				queue_work(cache->wq, &ent->work);
			return mr;
		}
	}
}

441 442 443 444 445
static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_ib_dev *dev, int order)
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_ib_mr *mr = NULL;
	struct mlx5_cache_ent *ent;
446
	int last_umr_cache_entry;
447 448 449 450
	int c;
	int i;

	c = order2idx(dev, order);
451
	last_umr_cache_entry = order2idx(dev, mr_cache_max_order(dev));
452
	if (c < 0 || c > last_umr_cache_entry) {
453 454 455 456
		mlx5_ib_warn(dev, "order %d, cache index %d\n", order, c);
		return NULL;
	}

457
	for (i = c; i <= last_umr_cache_entry; i++) {
458 459 460 461
		ent = &cache->ent[i];

		mlx5_ib_dbg(dev, "order %d, cache index %d\n", ent->order, i);

E
Eli Cohen 已提交
462
		spin_lock_irq(&ent->lock);
463 464 465 466 467
		if (!list_empty(&ent->head)) {
			mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
					      list);
			list_del(&mr->list);
			ent->cur--;
E
Eli Cohen 已提交
468
			spin_unlock_irq(&ent->lock);
469 470 471 472
			if (ent->cur < ent->limit)
				queue_work(cache->wq, &ent->work);
			break;
		}
E
Eli Cohen 已提交
473
		spin_unlock_irq(&ent->lock);
474 475 476 477 478 479 480 481 482 483

		queue_work(cache->wq, &ent->work);
	}

	if (!mr)
		cache->ent[c].miss++;

	return mr;
}

484
void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
485 486 487 488 489 490
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent;
	int shrink = 0;
	int c;

491 492 493
	if (!mr->allocated_from_cache)
		return;

494
	c = order2idx(dev, mr->order);
495 496
	WARN_ON(c < 0 || c >= MAX_MR_CACHE_ENTRIES);

497
	if (mlx5_mr_cache_invalidate(mr)) {
498 499 500 501 502
		mr->allocated_from_cache = false;
		destroy_mkey(dev, mr);
		ent = &cache->ent[c];
		if (ent->cur < ent->limit)
			queue_work(cache->wq, &ent->work);
503 504
		return;
	}
505

506
	ent = &cache->ent[c];
E
Eli Cohen 已提交
507
	spin_lock_irq(&ent->lock);
508 509 510 511
	list_add_tail(&mr->list, &ent->head);
	ent->cur++;
	if (ent->cur > 2 * ent->limit)
		shrink = 1;
E
Eli Cohen 已提交
512
	spin_unlock_irq(&ent->lock);
513 514 515 516 517 518 519 520 521

	if (shrink)
		queue_work(cache->wq, &ent->work);
}

static void clean_keys(struct mlx5_ib_dev *dev, int c)
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent = &cache->ent[c];
522
	struct mlx5_ib_mr *tmp_mr;
523
	struct mlx5_ib_mr *mr;
524
	LIST_HEAD(del_list);
525

526
	cancel_delayed_work(&ent->dwork);
527
	while (1) {
E
Eli Cohen 已提交
528
		spin_lock_irq(&ent->lock);
529
		if (list_empty(&ent->head)) {
E
Eli Cohen 已提交
530
			spin_unlock_irq(&ent->lock);
531
			break;
532 533
		}
		mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
534
		list_move(&mr->list, &del_list);
535 536
		ent->cur--;
		ent->size--;
E
Eli Cohen 已提交
537
		spin_unlock_irq(&ent->lock);
538 539 540 541 542 543
		mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
	}

	list_for_each_entry_safe(mr, tmp_mr, &del_list, list) {
		list_del(&mr->list);
		kfree(mr);
544 545 546
	}
}

547 548
static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev *dev)
{
549
	if (!mlx5_debugfs_root || dev->is_rep)
550 551 552 553 554 555
		return;

	debugfs_remove_recursive(dev->cache.root);
	dev->cache.root = NULL;
}

556
static void mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev *dev)
557 558 559
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent;
560
	struct dentry *dir;
561 562
	int i;

563
	if (!mlx5_debugfs_root || dev->is_rep)
564
		return;
565

566
	cache->root = debugfs_create_dir("mr_cache", dev->mdev->priv.dbg_root);
567 568 569 570

	for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
		ent = &cache->ent[i];
		sprintf(ent->name, "%d", ent->order);
571 572 573 574 575
		dir = debugfs_create_dir(ent->name, cache->root);
		debugfs_create_file("size", 0600, dir, ent, &size_fops);
		debugfs_create_file("limit", 0600, dir, ent, &limit_fops);
		debugfs_create_u32("cur", 0400, dir, &ent->cur);
		debugfs_create_u32("miss", 0600, dir, &ent->miss);
576 577 578
	}
}

579
static void delay_time_func(struct timer_list *t)
E
Eli Cohen 已提交
580
{
581
	struct mlx5_ib_dev *dev = from_timer(dev, t, delay_timer);
E
Eli Cohen 已提交
582 583 584 585

	dev->fill_delay = 0;
}

586 587 588 589 590 591
int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
{
	struct mlx5_mr_cache *cache = &dev->cache;
	struct mlx5_cache_ent *ent;
	int i;

592
	mutex_init(&dev->slow_path_mutex);
593
	cache->wq = alloc_ordered_workqueue("mkey_cache", WQ_MEM_RECLAIM);
594 595 596 597 598
	if (!cache->wq) {
		mlx5_ib_warn(dev, "failed to create work queue\n");
		return -ENOMEM;
	}

599
	mlx5_cmd_init_async_ctx(dev->mdev, &dev->async_ctx);
600
	timer_setup(&dev->delay_timer, delay_time_func, 0);
601 602 603 604 605 606
	for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
		ent = &cache->ent[i];
		INIT_LIST_HEAD(&ent->head);
		spin_lock_init(&ent->lock);
		ent->order = i + 2;
		ent->dev = dev;
607
		ent->limit = 0;
608

609
		init_completion(&ent->compl);
610 611
		INIT_WORK(&ent->work, cache_work_func);
		INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);
612

613
		if (i > MR_CACHE_LAST_STD_ENTRY) {
614
			mlx5_odp_init_mr_cache_entry(ent);
615
			continue;
616
		}
617

618
		if (ent->order > mr_cache_max_order(dev))
619 620 621 622 623 624 625
			continue;

		ent->page = PAGE_SHIFT;
		ent->xlt = (1 << ent->order) * sizeof(struct mlx5_mtt) /
			   MLX5_IB_UMR_OCTOWORD;
		ent->access_mode = MLX5_MKC_ACCESS_MODE_MTT;
		if ((dev->mdev->profile->mask & MLX5_PROF_MASK_MR_CACHE) &&
626
		    !dev->is_rep &&
627 628 629 630
		    mlx5_core_is_pf(dev->mdev))
			ent->limit = dev->mdev->profile->mr_cache[i].limit;
		else
			ent->limit = 0;
631
		queue_work(cache->wq, &ent->work);
632 633
	}

634
	mlx5_mr_cache_debugfs_init(dev);
635

636 637 638 639 640 641 642
	return 0;
}

int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev)
{
	int i;

643 644 645
	if (!dev->cache.wq)
		return 0;

646
	dev->cache.stopped = 1;
647
	flush_workqueue(dev->cache.wq);
648 649

	mlx5_mr_cache_debugfs_cleanup(dev);
650
	mlx5_cmd_cleanup_async_ctx(&dev->async_ctx);
651 652 653 654

	for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++)
		clean_keys(dev, i);

655
	destroy_workqueue(dev->cache.wq);
E
Eli Cohen 已提交
656
	del_timer_sync(&dev->delay_timer);
657

658 659 660
	return 0;
}

661 662 663
static void set_mkc_access_pd_addr_fields(void *mkc, int acc, u64 start_addr,
					  struct ib_pd *pd)
{
664 665
	struct mlx5_ib_dev *dev = to_mdev(pd->device);

666 667 668 669 670 671
	MLX5_SET(mkc, mkc, a, !!(acc & IB_ACCESS_REMOTE_ATOMIC));
	MLX5_SET(mkc, mkc, rw, !!(acc & IB_ACCESS_REMOTE_WRITE));
	MLX5_SET(mkc, mkc, rr, !!(acc & IB_ACCESS_REMOTE_READ));
	MLX5_SET(mkc, mkc, lw, !!(acc & IB_ACCESS_LOCAL_WRITE));
	MLX5_SET(mkc, mkc, lr, 1);

672 673 674 675 676 677 678
	if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_write))
		MLX5_SET(mkc, mkc, relaxed_ordering_write,
			 !!(acc & IB_ACCESS_RELAXED_ORDERING));
	if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_read))
		MLX5_SET(mkc, mkc, relaxed_ordering_read,
			 !!(acc & IB_ACCESS_RELAXED_ORDERING));

679 680 681 682 683
	MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
	MLX5_SET(mkc, mkc, qpn, 0xffffff);
	MLX5_SET64(mkc, mkc, start_addr, start_addr);
}

684 685 686
struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
687
	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
688
	struct mlx5_core_dev *mdev = dev->mdev;
689
	struct mlx5_ib_mr *mr;
690 691
	void *mkc;
	u32 *in;
692 693 694 695 696 697
	int err;

	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

698
	in = kzalloc(inlen, GFP_KERNEL);
699 700 701 702 703
	if (!in) {
		err = -ENOMEM;
		goto err_free;
	}

704 705
	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);

706
	MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_PA);
707
	MLX5_SET(mkc, mkc, length64, 1);
708
	set_mkc_access_pd_addr_fields(mkc, acc, 0, pd);
709 710

	err = mlx5_core_create_mkey(mdev, &mr->mmkey, in, inlen);
711 712 713 714
	if (err)
		goto err_in;

	kfree(in);
A
Artemy Kovalyov 已提交
715
	mr->mmkey.type = MLX5_MKEY_MR;
716 717
	mr->ibmr.lkey = mr->mmkey.key;
	mr->ibmr.rkey = mr->mmkey.key;
718 719 720 721 722 723 724 725 726 727 728 729 730
	mr->umem = NULL;

	return &mr->ibmr;

err_in:
	kfree(in);

err_free:
	kfree(mr);

	return ERR_PTR(err);
}

731
static int get_octo_len(u64 addr, u64 len, int page_shift)
732
{
733
	u64 page_size = 1ULL << page_shift;
734 735 736 737
	u64 offset;
	int npages;

	offset = addr & (page_size - 1);
738
	npages = ALIGN(len + offset, page_size) >> page_shift;
739 740 741
	return (npages + 1) / 2;
}

742
static int mr_cache_max_order(struct mlx5_ib_dev *dev)
743
{
744
	if (MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset))
745
		return MR_CACHE_LAST_STD_ENTRY + 2;
746 747 748
	return MLX5_MAX_UMR_SHIFT;
}

749 750 751
static int mr_umem_get(struct mlx5_ib_dev *dev, u64 start, u64 length,
		       int access_flags, struct ib_umem **umem, int *npages,
		       int *page_shift, int *ncont, int *order)
752
{
753
	struct ib_umem *u;
754

755 756
	*umem = NULL;

757 758 759
	if (access_flags & IB_ACCESS_ON_DEMAND) {
		struct ib_umem_odp *odp;

760
		odp = ib_umem_odp_get(&dev->ib_dev, start, length, access_flags,
761
				      &mlx5_mn_ops);
762 763 764 765 766 767 768 769 770 771 772 773 774 775
		if (IS_ERR(odp)) {
			mlx5_ib_dbg(dev, "umem get failed (%ld)\n",
				    PTR_ERR(odp));
			return PTR_ERR(odp);
		}

		u = &odp->umem;

		*page_shift = odp->page_shift;
		*ncont = ib_umem_odp_num_pages(odp);
		*npages = *ncont << (*page_shift - PAGE_SHIFT);
		if (order)
			*order = ilog2(roundup_pow_of_two(*ncont));
	} else {
776
		u = ib_umem_get(&dev->ib_dev, start, length, access_flags);
777 778 779 780 781 782 783
		if (IS_ERR(u)) {
			mlx5_ib_dbg(dev, "umem get failed (%ld)\n", PTR_ERR(u));
			return PTR_ERR(u);
		}

		mlx5_ib_cont_pages(u, start, MLX5_MKEY_PAGE_SHIFT_MASK, npages,
				   page_shift, ncont, order);
784 785 786 787
	}

	if (!*npages) {
		mlx5_ib_warn(dev, "avoid zero region\n");
788
		ib_umem_release(u);
789
		return -EINVAL;
790 791
	}

792 793
	*umem = u;

794 795 796
	mlx5_ib_dbg(dev, "npages %d, ncont %d, order %d, page_shift %d\n",
		    *npages, *ncont, *order, *page_shift);

797
	return 0;
798 799
}

800
static void mlx5_ib_umr_done(struct ib_cq *cq, struct ib_wc *wc)
801
{
802 803
	struct mlx5_ib_umr_context *context =
		container_of(wc->wr_cqe, struct mlx5_ib_umr_context, cqe);
804

805 806 807
	context->status = wc->status;
	complete(&context->done);
}
808

809 810 811 812 813
static inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context *context)
{
	context->cqe.done = mlx5_ib_umr_done;
	context->status = -1;
	init_completion(&context->done);
814 815
}

816 817 818 819
static int mlx5_ib_post_send_wait(struct mlx5_ib_dev *dev,
				  struct mlx5_umr_wr *umrwr)
{
	struct umr_common *umrc = &dev->umrc;
820
	const struct ib_send_wr *bad;
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	int err;
	struct mlx5_ib_umr_context umr_context;

	mlx5_ib_init_umr_context(&umr_context);
	umrwr->wr.wr_cqe = &umr_context.cqe;

	down(&umrc->sem);
	err = ib_post_send(umrc->qp, &umrwr->wr, &bad);
	if (err) {
		mlx5_ib_warn(dev, "UMR post send failed, err %d\n", err);
	} else {
		wait_for_completion(&umr_context.done);
		if (umr_context.status != IB_WC_SUCCESS) {
			mlx5_ib_warn(dev, "reg umr failed (%u)\n",
				     umr_context.status);
			err = -EFAULT;
		}
	}
	up(&umrc->sem);
	return err;
}

843 844
static struct mlx5_ib_mr *alloc_mr_from_cache(
				  struct ib_pd *pd, struct ib_umem *umem,
845 846 847 848 849
				  u64 virt_addr, u64 len, int npages,
				  int page_shift, int order, int access_flags)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct mlx5_ib_mr *mr;
850
	int err = 0;
851 852
	int i;

E
Eli Cohen 已提交
853
	for (i = 0; i < 1; i++) {
854 855 856 857 858
		mr = alloc_cached_mr(dev, order);
		if (mr)
			break;

		err = add_keys(dev, order2idx(dev, order), 1);
E
Eli Cohen 已提交
859 860
		if (err && err != -EAGAIN) {
			mlx5_ib_warn(dev, "add_keys failed, err %d\n", err);
861 862 863 864 865 866 867
			break;
		}
	}

	if (!mr)
		return ERR_PTR(-EAGAIN);

868 869 870 871
	mr->ibmr.pd = pd;
	mr->umem = umem;
	mr->access_flags = access_flags;
	mr->desc_size = sizeof(struct mlx5_mtt);
872 873 874
	mr->mmkey.iova = virt_addr;
	mr->mmkey.size = len;
	mr->mmkey.pd = to_mpd(pd)->pdn;
875

876 877 878
	return mr;
}

879 880 881 882 883 884 885 886
#define MLX5_MAX_UMR_CHUNK ((1 << (MLX5_MAX_UMR_SHIFT + 4)) - \
			    MLX5_UMR_MTT_ALIGNMENT)
#define MLX5_SPARE_UMR_CHUNK 0x10000

int mlx5_ib_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,
		       int page_shift, int flags)
{
	struct mlx5_ib_dev *dev = mr->dev;
887
	struct device *ddev = dev->ib_dev.dev.parent;
888
	int size;
889
	void *xlt;
890
	dma_addr_t dma;
C
Christoph Hellwig 已提交
891
	struct mlx5_umr_wr wr;
892 893
	struct ib_sge sg;
	int err = 0;
894 895 896
	int desc_size = (flags & MLX5_IB_UPD_XLT_INDIRECT)
			       ? sizeof(struct mlx5_klm)
			       : sizeof(struct mlx5_mtt);
897 898
	const int page_align = MLX5_UMR_MTT_ALIGNMENT / desc_size;
	const int page_mask = page_align - 1;
899 900 901
	size_t pages_mapped = 0;
	size_t pages_to_map = 0;
	size_t pages_iter = 0;
902
	size_t size_to_map = 0;
903
	gfp_t gfp;
904
	bool use_emergency_page = false;
905

906 907 908
	if ((flags & MLX5_IB_UPD_XLT_INDIRECT) &&
	    !umr_can_use_indirect_mkey(dev))
		return -EPERM;
909 910

	/* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
911 912 913 914 915
	 * so we need to align the offset and length accordingly
	 */
	if (idx & page_mask) {
		npages += idx & page_mask;
		idx &= ~page_mask;
916 917
	}

918 919
	gfp = flags & MLX5_IB_UPD_XLT_ATOMIC ? GFP_ATOMIC : GFP_KERNEL;
	gfp |= __GFP_ZERO | __GFP_NOWARN;
920

921 922 923
	pages_to_map = ALIGN(npages, page_align);
	size = desc_size * pages_to_map;
	size = min_t(int, size, MLX5_MAX_UMR_CHUNK);
924

925 926 927 928 929 930 931
	xlt = (void *)__get_free_pages(gfp, get_order(size));
	if (!xlt && size > MLX5_SPARE_UMR_CHUNK) {
		mlx5_ib_dbg(dev, "Failed to allocate %d bytes of order %d. fallback to spare UMR allocation od %d bytes\n",
			    size, get_order(size), MLX5_SPARE_UMR_CHUNK);

		size = MLX5_SPARE_UMR_CHUNK;
		xlt = (void *)__get_free_pages(gfp, get_order(size));
932
	}
933 934 935

	if (!xlt) {
		mlx5_ib_warn(dev, "Using XLT emergency buffer\n");
936
		xlt = (void *)mlx5_ib_get_xlt_emergency_page();
937 938
		size = PAGE_SIZE;
		memset(xlt, 0, size);
939
		use_emergency_page = true;
940 941 942
	}
	pages_iter = size / desc_size;
	dma = dma_map_single(ddev, xlt, size, DMA_TO_DEVICE);
943
	if (dma_mapping_error(ddev, dma)) {
944
		mlx5_ib_err(dev, "unable to map DMA during XLT update.\n");
945
		err = -ENOMEM;
946
		goto free_xlt;
947 948
	}

949 950 951 952 953 954 955 956 957
	if (mr->umem->is_odp) {
		if (!(flags & MLX5_IB_UPD_XLT_INDIRECT)) {
			struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
			size_t max_pages = ib_umem_odp_num_pages(odp) - idx;

			pages_to_map = min_t(size_t, pages_to_map, max_pages);
		}
	}

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
	sg.addr = dma;
	sg.lkey = dev->umrc.pd->local_dma_lkey;

	memset(&wr, 0, sizeof(wr));
	wr.wr.send_flags = MLX5_IB_SEND_UMR_UPDATE_XLT;
	if (!(flags & MLX5_IB_UPD_XLT_ENABLE))
		wr.wr.send_flags |= MLX5_IB_SEND_UMR_FAIL_IF_FREE;
	wr.wr.sg_list = &sg;
	wr.wr.num_sge = 1;
	wr.wr.opcode = MLX5_IB_WR_UMR;

	wr.pd = mr->ibmr.pd;
	wr.mkey = mr->mmkey.key;
	wr.length = mr->mmkey.size;
	wr.virt_addr = mr->mmkey.iova;
	wr.access_flags = mr->access_flags;
	wr.page_shift = page_shift;

976 977
	for (pages_mapped = 0;
	     pages_mapped < pages_to_map && !err;
978
	     pages_mapped += pages_iter, idx += pages_iter) {
979
		npages = min_t(int, pages_iter, pages_to_map - pages_mapped);
980
		size_to_map = npages * desc_size;
981
		dma_sync_single_for_cpu(ddev, dma, size, DMA_TO_DEVICE);
982 983 984 985 986 987 988 989 990 991 992
		if (mr->umem->is_odp) {
			mlx5_odp_populate_xlt(xlt, idx, npages, mr, flags);
		} else {
			__mlx5_ib_populate_pas(dev, mr->umem, page_shift, idx,
					       npages, xlt,
					       MLX5_IB_MTT_PRESENT);
			/* Clear padding after the pages
			 * brought from the umem.
			 */
			memset(xlt + size_to_map, 0, size - size_to_map);
		}
993 994
		dma_sync_single_for_device(ddev, dma, size, DMA_TO_DEVICE);

995
		sg.length = ALIGN(size_to_map, MLX5_UMR_MTT_ALIGNMENT);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

		if (pages_mapped + pages_iter >= pages_to_map) {
			if (flags & MLX5_IB_UPD_XLT_ENABLE)
				wr.wr.send_flags |=
					MLX5_IB_SEND_UMR_ENABLE_MR |
					MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS |
					MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
			if (flags & MLX5_IB_UPD_XLT_PD ||
			    flags & MLX5_IB_UPD_XLT_ACCESS)
				wr.wr.send_flags |=
					MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
			if (flags & MLX5_IB_UPD_XLT_ADDR)
				wr.wr.send_flags |=
					MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
		}
1011

1012
		wr.offset = idx * desc_size;
1013
		wr.xlt_size = sg.length;
1014

1015
		err = mlx5_ib_post_send_wait(dev, &wr);
1016 1017 1018
	}
	dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);

1019
free_xlt:
1020 1021
	if (use_emergency_page)
		mlx5_ib_put_xlt_emergency_page();
1022
	else
1023
		free_pages((unsigned long)xlt, get_order(size));
1024 1025 1026 1027

	return err;
}

1028 1029 1030 1031 1032 1033 1034
/*
 * If ibmr is NULL it will be allocated by reg_create.
 * Else, the given ibmr will be used.
 */
static struct mlx5_ib_mr *reg_create(struct ib_mr *ibmr, struct ib_pd *pd,
				     u64 virt_addr, u64 length,
				     struct ib_umem *umem, int npages,
1035 1036
				     int page_shift, int access_flags,
				     bool populate)
1037 1038 1039
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct mlx5_ib_mr *mr;
1040 1041
	__be64 *pas;
	void *mkc;
1042
	int inlen;
1043
	u32 *in;
1044
	int err;
1045
	bool pg_cap = !!(MLX5_CAP_GEN(dev->mdev, pg));
1046

1047
	mr = ibmr ? to_mmr(ibmr) : kzalloc(sizeof(*mr), GFP_KERNEL);
1048 1049 1050
	if (!mr)
		return ERR_PTR(-ENOMEM);

1051 1052 1053 1054 1055 1056
	mr->ibmr.pd = pd;
	mr->access_flags = access_flags;

	inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
	if (populate)
		inlen += sizeof(*pas) * roundup(npages, 2);
1057
	in = kvzalloc(inlen, GFP_KERNEL);
1058 1059 1060 1061
	if (!in) {
		err = -ENOMEM;
		goto err_1;
	}
1062
	pas = (__be64 *)MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
1063
	if (populate && !(access_flags & IB_ACCESS_ON_DEMAND))
1064 1065
		mlx5_ib_populate_pas(dev, umem, page_shift, pas,
				     pg_cap ? MLX5_IB_MTT_PRESENT : 0);
1066

1067
	/* The pg_access bit allows setting the access flags
1068
	 * in the page list submitted with the command. */
1069 1070 1071
	MLX5_SET(create_mkey_in, in, pg_access, !!(pg_cap));

	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
1072
	MLX5_SET(mkc, mkc, free, !populate);
1073
	MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
1074 1075 1076 1077 1078 1079
	if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_write))
		MLX5_SET(mkc, mkc, relaxed_ordering_write,
			 !!(access_flags & IB_ACCESS_RELAXED_ORDERING));
	if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_read))
		MLX5_SET(mkc, mkc, relaxed_ordering_read,
			 !!(access_flags & IB_ACCESS_RELAXED_ORDERING));
1080 1081 1082 1083 1084
	MLX5_SET(mkc, mkc, a, !!(access_flags & IB_ACCESS_REMOTE_ATOMIC));
	MLX5_SET(mkc, mkc, rw, !!(access_flags & IB_ACCESS_REMOTE_WRITE));
	MLX5_SET(mkc, mkc, rr, !!(access_flags & IB_ACCESS_REMOTE_READ));
	MLX5_SET(mkc, mkc, lw, !!(access_flags & IB_ACCESS_LOCAL_WRITE));
	MLX5_SET(mkc, mkc, lr, 1);
1085
	MLX5_SET(mkc, mkc, umr_en, 1);
1086 1087 1088 1089 1090 1091

	MLX5_SET64(mkc, mkc, start_addr, virt_addr);
	MLX5_SET64(mkc, mkc, len, length);
	MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
	MLX5_SET(mkc, mkc, bsf_octword_size, 0);
	MLX5_SET(mkc, mkc, translations_octword_size,
1092
		 get_octo_len(virt_addr, length, page_shift));
1093 1094
	MLX5_SET(mkc, mkc, log_page_size, page_shift);
	MLX5_SET(mkc, mkc, qpn, 0xffffff);
1095 1096
	if (populate) {
		MLX5_SET(create_mkey_in, in, translations_octword_actual_size,
1097
			 get_octo_len(virt_addr, length, page_shift));
1098
	}
1099 1100

	err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, inlen);
1101 1102 1103 1104
	if (err) {
		mlx5_ib_warn(dev, "create mkey failed\n");
		goto err_2;
	}
A
Artemy Kovalyov 已提交
1105
	mr->mmkey.type = MLX5_MKEY_MR;
1106
	mr->desc_size = sizeof(struct mlx5_mtt);
1107
	mr->dev = dev;
A
Al Viro 已提交
1108
	kvfree(in);
1109

1110
	mlx5_ib_dbg(dev, "mkey = 0x%x\n", mr->mmkey.key);
1111 1112 1113 1114

	return mr;

err_2:
A
Al Viro 已提交
1115
	kvfree(in);
1116 1117

err_1:
1118 1119
	if (!ibmr)
		kfree(mr);
1120 1121 1122 1123

	return ERR_PTR(err);
}

1124
static void set_mr_fields(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr,
1125 1126 1127 1128
			  int npages, u64 length, int access_flags)
{
	mr->npages = npages;
	atomic_add(npages, &dev->mdev->priv.reg_pages);
1129 1130
	mr->ibmr.lkey = mr->mmkey.key;
	mr->ibmr.rkey = mr->mmkey.key;
1131
	mr->ibmr.length = length;
1132
	mr->access_flags = access_flags;
1133 1134
}

1135 1136
static struct ib_mr *mlx5_ib_get_dm_mr(struct ib_pd *pd, u64 start_addr,
				       u64 length, int acc, int mode)
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
	struct mlx5_core_dev *mdev = dev->mdev;
	struct mlx5_ib_mr *mr;
	void *mkc;
	u32 *in;
	int err;

	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

	in = kzalloc(inlen, GFP_KERNEL);
	if (!in) {
		err = -ENOMEM;
		goto err_free;
	}

	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);

1158 1159
	MLX5_SET(mkc, mkc, access_mode_1_0, mode & 0x3);
	MLX5_SET(mkc, mkc, access_mode_4_2, (mode >> 2) & 0x7);
1160
	MLX5_SET64(mkc, mkc, len, length);
1161
	set_mkc_access_pd_addr_fields(mkc, acc, start_addr, pd);
1162 1163 1164 1165 1166 1167 1168 1169

	err = mlx5_core_create_mkey(mdev, &mr->mmkey, in, inlen);
	if (err)
		goto err_in;

	kfree(in);

	mr->umem = NULL;
1170
	set_mr_fields(dev, mr, 0, length, acc);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

	return &mr->ibmr;

err_in:
	kfree(in);

err_free:
	kfree(mr);

	return ERR_PTR(err);
}

M
Moni Shoua 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
int mlx5_ib_advise_mr(struct ib_pd *pd,
		      enum ib_uverbs_advise_mr_advice advice,
		      u32 flags,
		      struct ib_sge *sg_list,
		      u32 num_sge,
		      struct uverbs_attr_bundle *attrs)
{
	if (advice != IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH &&
	    advice != IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH_WRITE)
		return -EOPNOTSUPP;

	return mlx5_ib_advise_mr_prefetch(pd, advice, flags,
					 sg_list, num_sge);
}

1198 1199 1200 1201 1202
struct ib_mr *mlx5_ib_reg_dm_mr(struct ib_pd *pd, struct ib_dm *dm,
				struct ib_dm_mr_attr *attr,
				struct uverbs_attr_bundle *attrs)
{
	struct mlx5_ib_dm *mdm = to_mdm(dm);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	struct mlx5_core_dev *dev = to_mdev(dm->device)->mdev;
	u64 start_addr = mdm->dev_addr + attr->offset;
	int mode;

	switch (mdm->type) {
	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
		if (attr->access_flags & ~MLX5_IB_DM_MEMIC_ALLOWED_ACCESS)
			return ERR_PTR(-EINVAL);

		mode = MLX5_MKC_ACCESS_MODE_MEMIC;
		start_addr -= pci_resource_start(dev->pdev, 0);
		break;
1215 1216 1217 1218 1219 1220 1221
	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
		if (attr->access_flags & ~MLX5_IB_DM_SW_ICM_ALLOWED_ACCESS)
			return ERR_PTR(-EINVAL);

		mode = MLX5_MKC_ACCESS_MODE_SW_ICM;
		break;
1222
	default:
1223
		return ERR_PTR(-EINVAL);
1224
	}
1225

1226 1227
	return mlx5_ib_get_dm_mr(pd, start_addr, attr->length,
				 attr->access_flags, mode);
1228 1229
}

1230 1231 1232 1233 1234 1235
struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
				  u64 virt_addr, int access_flags,
				  struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct mlx5_ib_mr *mr = NULL;
1236
	bool use_umr;
1237 1238 1239 1240 1241 1242 1243
	struct ib_umem *umem;
	int page_shift;
	int npages;
	int ncont;
	int order;
	int err;

1244
	if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM))
1245
		return ERR_PTR(-EOPNOTSUPP);
1246

1247 1248
	mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
		    start, virt_addr, length, access_flags);
1249

1250 1251
	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) && !start &&
	    length == U64_MAX) {
1252 1253
		if (virt_addr != start)
			return ERR_PTR(-EINVAL);
1254 1255 1256 1257
		if (!(access_flags & IB_ACCESS_ON_DEMAND) ||
		    !(dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT))
			return ERR_PTR(-EINVAL);

1258
		mr = mlx5_ib_alloc_implicit_mr(to_mpd(pd), udata, access_flags);
1259 1260
		if (IS_ERR(mr))
			return ERR_CAST(mr);
1261 1262 1263
		return &mr->ibmr;
	}

1264
	err = mr_umem_get(dev, start, length, access_flags, &umem,
1265
			  &npages, &page_shift, &ncont, &order);
1266

1267
	if (err < 0)
1268
		return ERR_PTR(err);
1269

1270
	use_umr = mlx5_ib_can_use_umr(dev, true, access_flags);
1271 1272

	if (order <= mr_cache_max_order(dev) && use_umr) {
1273 1274
		mr = alloc_mr_from_cache(pd, umem, virt_addr, length, ncont,
					 page_shift, order, access_flags);
1275
		if (PTR_ERR(mr) == -EAGAIN) {
1276
			mlx5_ib_dbg(dev, "cache empty for order %d\n", order);
1277 1278
			mr = NULL;
		}
1279 1280 1281
	} else if (!MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset)) {
		if (access_flags & IB_ACCESS_ON_DEMAND) {
			err = -EINVAL;
1282
			pr_err("Got MR registration for ODP MR > 512MB, not supported for Connect-IB\n");
1283 1284
			goto error;
		}
1285
		use_umr = false;
1286 1287
	}

1288 1289
	if (!mr) {
		mutex_lock(&dev->slow_path_mutex);
1290
		mr = reg_create(NULL, pd, virt_addr, length, umem, ncont,
1291
				page_shift, access_flags, !use_umr);
1292 1293
		mutex_unlock(&dev->slow_path_mutex);
	}
1294 1295 1296 1297 1298 1299

	if (IS_ERR(mr)) {
		err = PTR_ERR(mr);
		goto error;
	}

1300
	mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmkey.key);
1301 1302

	mr->umem = umem;
1303
	set_mr_fields(dev, mr, npages, length, access_flags);
1304

1305
	if (use_umr) {
1306 1307 1308 1309
		int update_xlt_flags = MLX5_IB_UPD_XLT_ENABLE;

		if (access_flags & IB_ACCESS_ON_DEMAND)
			update_xlt_flags |= MLX5_IB_UPD_XLT_ZAP;
1310

1311 1312
		err = mlx5_ib_update_xlt(mr, 0, ncont, page_shift,
					 update_xlt_flags);
1313

1314
		if (err) {
1315
			dereg_mr(dev, mr);
1316 1317 1318 1319
			return ERR_PTR(err);
		}
	}

1320 1321
	if (is_odp_mr(mr)) {
		to_ib_umem_odp(mr->umem)->private = mr;
1322
		atomic_set(&mr->num_deferred_work, 0);
1323 1324 1325 1326 1327 1328 1329
		err = xa_err(xa_store(&dev->odp_mkeys,
				      mlx5_base_mkey(mr->mmkey.key), &mr->mmkey,
				      GFP_KERNEL));
		if (err) {
			dereg_mr(dev, mr);
			return ERR_PTR(err);
		}
1330
	}
1331

1332
	return &mr->ibmr;
1333 1334 1335 1336 1337
error:
	ib_umem_release(umem);
	return ERR_PTR(err);
}

1338 1339 1340 1341 1342 1343 1344 1345 1346
/**
 * mlx5_mr_cache_invalidate - Fence all DMA on the MR
 * @mr: The MR to fence
 *
 * Upon return the NIC will not be doing any DMA to the pages under the MR,
 * and any DMA inprogress will be completed. Failure of this function
 * indicates the HW has failed catastrophically.
 */
int mlx5_mr_cache_invalidate(struct mlx5_ib_mr *mr)
1347
{
1348
	struct mlx5_umr_wr umrwr = {};
1349

1350
	if (mr->dev->mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR)
1351 1352
		return 0;

1353 1354
	umrwr.wr.send_flags = MLX5_IB_SEND_UMR_DISABLE_MR |
			      MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
1355
	umrwr.wr.opcode = MLX5_IB_WR_UMR;
1356
	umrwr.pd = mr->dev->umrc.pd;
1357
	umrwr.mkey = mr->mmkey.key;
1358
	umrwr.ignore_free_state = 1;
1359

1360
	return mlx5_ib_post_send_wait(mr->dev, &umrwr);
1361 1362
}

1363
static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr,
1364 1365 1366 1367 1368 1369 1370 1371
		     int access_flags, int flags)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct mlx5_umr_wr umrwr = {};
	int err;

	umrwr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE;

1372 1373
	umrwr.wr.opcode = MLX5_IB_WR_UMR;
	umrwr.mkey = mr->mmkey.key;
1374

1375
	if (flags & IB_MR_REREG_PD || flags & IB_MR_REREG_ACCESS) {
1376 1377
		umrwr.pd = pd;
		umrwr.access_flags = access_flags;
1378
		umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
1379 1380
	}

1381
	err = mlx5_ib_post_send_wait(dev, &umrwr);
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396

	return err;
}

int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
			  u64 length, u64 virt_addr, int new_access_flags,
			  struct ib_pd *new_pd, struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev = to_mdev(ib_mr->device);
	struct mlx5_ib_mr *mr = to_mmr(ib_mr);
	struct ib_pd *pd = (flags & IB_MR_REREG_PD) ? new_pd : ib_mr->pd;
	int access_flags = flags & IB_MR_REREG_ACCESS ?
			    new_access_flags :
			    mr->access_flags;
	int page_shift = 0;
1397
	int upd_flags = 0;
1398 1399 1400
	int npages = 0;
	int ncont = 0;
	int order = 0;
1401
	u64 addr, len;
1402 1403 1404 1405 1406
	int err;

	mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
		    start, virt_addr, length, access_flags);

1407 1408
	atomic_sub(mr->npages, &dev->mdev->priv.reg_pages);

1409 1410 1411
	if (!mr->umem)
		return -EINVAL;

1412 1413 1414
	if (is_odp_mr(mr))
		return -EOPNOTSUPP;

1415 1416 1417 1418 1419 1420 1421 1422
	if (flags & IB_MR_REREG_TRANS) {
		addr = virt_addr;
		len = length;
	} else {
		addr = mr->umem->address;
		len = mr->umem->length;
	}

1423 1424 1425 1426 1427 1428 1429
	if (flags != IB_MR_REREG_PD) {
		/*
		 * Replace umem. This needs to be done whether or not UMR is
		 * used.
		 */
		flags |= IB_MR_REREG_TRANS;
		ib_umem_release(mr->umem);
1430
		mr->umem = NULL;
1431 1432
		err = mr_umem_get(dev, addr, len, access_flags, &mr->umem,
				  &npages, &page_shift, &ncont, &order);
1433 1434
		if (err)
			goto err;
1435 1436
	}

1437
	if (!mlx5_ib_can_use_umr(dev, true, access_flags) ||
1438
	    (flags & IB_MR_REREG_TRANS && !use_umr_mtt_update(mr, addr, len))) {
1439 1440 1441
		/*
		 * UMR can't be used - MKey needs to be replaced.
		 */
1442
		if (mr->allocated_from_cache)
1443
			err = mlx5_mr_cache_invalidate(mr);
1444
		else
1445 1446
			err = destroy_mkey(dev, mr);
		if (err)
1447
			goto err;
1448 1449

		mr = reg_create(ib_mr, pd, addr, len, mr->umem, ncont,
1450
				page_shift, access_flags, true);
1451

1452 1453 1454 1455 1456
		if (IS_ERR(mr)) {
			err = PTR_ERR(mr);
			mr = to_mmr(ib_mr);
			goto err;
		}
1457

1458
		mr->allocated_from_cache = false;
1459 1460 1461 1462
	} else {
		/*
		 * Send a UMR WQE
		 */
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
		mr->ibmr.pd = pd;
		mr->access_flags = access_flags;
		mr->mmkey.iova = addr;
		mr->mmkey.size = len;
		mr->mmkey.pd = to_mpd(pd)->pdn;

		if (flags & IB_MR_REREG_TRANS) {
			upd_flags = MLX5_IB_UPD_XLT_ADDR;
			if (flags & IB_MR_REREG_PD)
				upd_flags |= MLX5_IB_UPD_XLT_PD;
			if (flags & IB_MR_REREG_ACCESS)
				upd_flags |= MLX5_IB_UPD_XLT_ACCESS;
			err = mlx5_ib_update_xlt(mr, 0, npages, page_shift,
						 upd_flags);
		} else {
			err = rereg_umr(pd, mr, access_flags, flags);
		}

1481 1482
		if (err)
			goto err;
1483 1484
	}

1485
	set_mr_fields(dev, mr, npages, len, access_flags);
1486 1487

	return 0;
1488 1489

err:
1490 1491 1492
	ib_umem_release(mr->umem);
	mr->umem = NULL;

1493 1494
	clean_mr(dev, mr);
	return err;
1495 1496
}

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
static int
mlx5_alloc_priv_descs(struct ib_device *device,
		      struct mlx5_ib_mr *mr,
		      int ndescs,
		      int desc_size)
{
	int size = ndescs * desc_size;
	int add_size;
	int ret;

	add_size = max_t(int, MLX5_UMR_ALIGN - ARCH_KMALLOC_MINALIGN, 0);

	mr->descs_alloc = kzalloc(size + add_size, GFP_KERNEL);
	if (!mr->descs_alloc)
		return -ENOMEM;

	mr->descs = PTR_ALIGN(mr->descs_alloc, MLX5_UMR_ALIGN);

1515
	mr->desc_map = dma_map_single(device->dev.parent, mr->descs,
1516
				      size, DMA_TO_DEVICE);
1517
	if (dma_mapping_error(device->dev.parent, mr->desc_map)) {
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
		ret = -ENOMEM;
		goto err;
	}

	return 0;
err:
	kfree(mr->descs_alloc);

	return ret;
}

static void
mlx5_free_priv_descs(struct mlx5_ib_mr *mr)
{
	if (mr->descs) {
		struct ib_device *device = mr->ibmr.device;
		int size = mr->max_descs * mr->desc_size;

1536
		dma_unmap_single(device->dev.parent, mr->desc_map,
1537 1538 1539 1540 1541 1542
				 size, DMA_TO_DEVICE);
		kfree(mr->descs_alloc);
		mr->descs = NULL;
	}
}

1543
static void clean_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
1544
{
1545
	int allocated_from_cache = mr->allocated_from_cache;
1546

1547 1548 1549 1550 1551 1552 1553 1554 1555
	if (mr->sig) {
		if (mlx5_core_destroy_psv(dev->mdev,
					  mr->sig->psv_memory.psv_idx))
			mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
				     mr->sig->psv_memory.psv_idx);
		if (mlx5_core_destroy_psv(dev->mdev,
					  mr->sig->psv_wire.psv_idx))
			mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
				     mr->sig->psv_wire.psv_idx);
1556
		xa_erase(&dev->sig_mrs, mlx5_base_mkey(mr->mmkey.key));
1557 1558 1559 1560
		kfree(mr->sig);
		mr->sig = NULL;
	}

1561
	if (!allocated_from_cache) {
1562
		destroy_mkey(dev, mr);
1563 1564
		mlx5_free_priv_descs(mr);
	}
1565 1566
}

1567
static void dereg_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
1568 1569 1570 1571
{
	int npages = mr->npages;
	struct ib_umem *umem = mr->umem;

1572 1573 1574 1575 1576
	/* Stop all DMA */
	if (is_odp_mr(mr))
		mlx5_ib_fence_odp_mr(mr);
	else
		clean_mr(dev, mr);
1577

1578 1579 1580 1581
	if (mr->allocated_from_cache)
		mlx5_mr_cache_free(dev, mr);
	else
		kfree(mr);
1582

1583
	ib_umem_release(umem);
1584
	atomic_sub(npages, &dev->mdev->priv.reg_pages);
1585

1586 1587
}

1588
int mlx5_ib_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
1589
{
1590 1591
	struct mlx5_ib_mr *mmr = to_mmr(ibmr);

1592 1593 1594 1595
	if (ibmr->type == IB_MR_TYPE_INTEGRITY) {
		dereg_mr(to_mdev(mmr->mtt_mr->ibmr.device), mmr->mtt_mr);
		dereg_mr(to_mdev(mmr->klm_mr->ibmr.device), mmr->klm_mr);
	}
1596

1597 1598 1599 1600 1601
	if (is_odp_mr(mmr) && to_ib_umem_odp(mmr->umem)->is_implicit_odp) {
		mlx5_ib_free_implicit_mr(mmr);
		return 0;
	}

1602 1603
	dereg_mr(to_mdev(ibmr->device), mmr);

1604
	return 0;
1605 1606
}

1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
static void mlx5_set_umr_free_mkey(struct ib_pd *pd, u32 *in, int ndescs,
				   int access_mode, int page_shift)
{
	void *mkc;

	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);

	MLX5_SET(mkc, mkc, free, 1);
	MLX5_SET(mkc, mkc, qpn, 0xffffff);
	MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
	MLX5_SET(mkc, mkc, translations_octword_size, ndescs);
	MLX5_SET(mkc, mkc, access_mode_1_0, access_mode & 0x3);
	MLX5_SET(mkc, mkc, access_mode_4_2, (access_mode >> 2) & 0x7);
	MLX5_SET(mkc, mkc, umr_en, 1);
	MLX5_SET(mkc, mkc, log_page_size, page_shift);
}

static int _mlx5_alloc_mkey_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
				  int ndescs, int desc_size, int page_shift,
				  int access_mode, u32 *in, int inlen)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	int err;

	mr->access_mode = access_mode;
	mr->desc_size = desc_size;
	mr->max_descs = ndescs;

	err = mlx5_alloc_priv_descs(pd->device, mr, ndescs, desc_size);
	if (err)
		return err;

	mlx5_set_umr_free_mkey(pd, in, ndescs, access_mode, page_shift);

	err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, inlen);
	if (err)
		goto err_free_descs;

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

	return 0;

err_free_descs:
	mlx5_free_priv_descs(mr);
	return err;
}

1656
static struct mlx5_ib_mr *mlx5_ib_alloc_pi_mr(struct ib_pd *pd,
1657 1658
				u32 max_num_sg, u32 max_num_meta_sg,
				int desc_size, int access_mode)
1659
{
1660
	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
1661
	int ndescs = ALIGN(max_num_sg + max_num_meta_sg, 4);
1662
	int page_shift = 0;
1663 1664
	struct mlx5_ib_mr *mr;
	u32 *in;
1665
	int err;
1666 1667 1668 1669 1670

	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

1671 1672 1673
	mr->ibmr.pd = pd;
	mr->ibmr.device = pd->device;

1674
	in = kzalloc(inlen, GFP_KERNEL);
1675 1676 1677 1678 1679
	if (!in) {
		err = -ENOMEM;
		goto err_free;
	}

1680
	if (access_mode == MLX5_MKC_ACCESS_MODE_MTT)
1681
		page_shift = PAGE_SHIFT;
1682

1683 1684
	err = _mlx5_alloc_mkey_descs(pd, mr, ndescs, desc_size, page_shift,
				     access_mode, in, inlen);
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
	if (err)
		goto err_free_in;

	mr->umem = NULL;
	kfree(in);

	return mr;

err_free_in:
	kfree(in);
err_free:
	kfree(mr);
	return ERR_PTR(err);
}

1700 1701 1702 1703 1704 1705 1706 1707 1708 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 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
static int mlx5_alloc_mem_reg_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
				    int ndescs, u32 *in, int inlen)
{
	return _mlx5_alloc_mkey_descs(pd, mr, ndescs, sizeof(struct mlx5_mtt),
				      PAGE_SHIFT, MLX5_MKC_ACCESS_MODE_MTT, in,
				      inlen);
}

static int mlx5_alloc_sg_gaps_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
				    int ndescs, u32 *in, int inlen)
{
	return _mlx5_alloc_mkey_descs(pd, mr, ndescs, sizeof(struct mlx5_klm),
				      0, MLX5_MKC_ACCESS_MODE_KLMS, in, inlen);
}

static int mlx5_alloc_integrity_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
				      int max_num_sg, int max_num_meta_sg,
				      u32 *in, int inlen)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	u32 psv_index[2];
	void *mkc;
	int err;

	mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
	if (!mr->sig)
		return -ENOMEM;

	/* create mem & wire PSVs */
	err = mlx5_core_create_psv(dev->mdev, to_mpd(pd)->pdn, 2, psv_index);
	if (err)
		goto err_free_sig;

	mr->sig->psv_memory.psv_idx = psv_index[0];
	mr->sig->psv_wire.psv_idx = psv_index[1];

	mr->sig->sig_status_checked = true;
	mr->sig->sig_err_exists = false;
	/* Next UMR, Arm SIGERR */
	++mr->sig->sigerr_count;
	mr->klm_mr = mlx5_ib_alloc_pi_mr(pd, max_num_sg, max_num_meta_sg,
					 sizeof(struct mlx5_klm),
					 MLX5_MKC_ACCESS_MODE_KLMS);
	if (IS_ERR(mr->klm_mr)) {
		err = PTR_ERR(mr->klm_mr);
		goto err_destroy_psv;
	}
	mr->mtt_mr = mlx5_ib_alloc_pi_mr(pd, max_num_sg, max_num_meta_sg,
					 sizeof(struct mlx5_mtt),
					 MLX5_MKC_ACCESS_MODE_MTT);
	if (IS_ERR(mr->mtt_mr)) {
		err = PTR_ERR(mr->mtt_mr);
		goto err_free_klm_mr;
	}

	/* Set bsf descriptors for mkey */
	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
	MLX5_SET(mkc, mkc, bsf_en, 1);
	MLX5_SET(mkc, mkc, bsf_octword_size, MLX5_MKEY_BSF_OCTO_SIZE);

	err = _mlx5_alloc_mkey_descs(pd, mr, 4, sizeof(struct mlx5_klm), 0,
				     MLX5_MKC_ACCESS_MODE_KLMS, in, inlen);
	if (err)
		goto err_free_mtt_mr;

1765 1766 1767 1768
	err = xa_err(xa_store(&dev->sig_mrs, mlx5_base_mkey(mr->mmkey.key),
			      mr->sig, GFP_KERNEL));
	if (err)
		goto err_free_descs;
1769 1770
	return 0;

1771 1772 1773
err_free_descs:
	destroy_mkey(dev, mr);
	mlx5_free_priv_descs(mr);
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
err_free_mtt_mr:
	dereg_mr(to_mdev(mr->mtt_mr->ibmr.device), mr->mtt_mr);
	mr->mtt_mr = NULL;
err_free_klm_mr:
	dereg_mr(to_mdev(mr->klm_mr->ibmr.device), mr->klm_mr);
	mr->klm_mr = NULL;
err_destroy_psv:
	if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_memory.psv_idx))
		mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
			     mr->sig->psv_memory.psv_idx);
	if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_wire.psv_idx))
		mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
			     mr->sig->psv_wire.psv_idx);
err_free_sig:
	kfree(mr->sig);

	return err;
}

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
static struct ib_mr *__mlx5_ib_alloc_mr(struct ib_pd *pd,
					enum ib_mr_type mr_type, u32 max_num_sg,
					u32 max_num_meta_sg)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
	int ndescs = ALIGN(max_num_sg, 4);
	struct mlx5_ib_mr *mr;
	u32 *in;
	int err;

	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
	if (!mr)
		return ERR_PTR(-ENOMEM);

	in = kzalloc(inlen, GFP_KERNEL);
	if (!in) {
		err = -ENOMEM;
		goto err_free;
	}

1814 1815
	mr->ibmr.device = pd->device;
	mr->umem = NULL;
1816

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
	switch (mr_type) {
	case IB_MR_TYPE_MEM_REG:
		err = mlx5_alloc_mem_reg_descs(pd, mr, ndescs, in, inlen);
		break;
	case IB_MR_TYPE_SG_GAPS:
		err = mlx5_alloc_sg_gaps_descs(pd, mr, ndescs, in, inlen);
		break;
	case IB_MR_TYPE_INTEGRITY:
		err = mlx5_alloc_integrity_descs(pd, mr, max_num_sg,
						 max_num_meta_sg, in, inlen);
		break;
	default:
S
Sagi Grimberg 已提交
1829 1830
		mlx5_ib_warn(dev, "Invalid mr type %d\n", mr_type);
		err = -EINVAL;
1831 1832 1833
	}

	if (err)
1834
		goto err_free_in;
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846

	kfree(in);

	return &mr->ibmr;

err_free_in:
	kfree(in);
err_free:
	kfree(mr);
	return ERR_PTR(err);
}

1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
			       u32 max_num_sg, struct ib_udata *udata)
{
	return __mlx5_ib_alloc_mr(pd, mr_type, max_num_sg, 0);
}

struct ib_mr *mlx5_ib_alloc_mr_integrity(struct ib_pd *pd,
					 u32 max_num_sg, u32 max_num_meta_sg)
{
	return __mlx5_ib_alloc_mr(pd, IB_MR_TYPE_INTEGRITY, max_num_sg,
				  max_num_meta_sg);
}

1860 1861 1862 1863
struct ib_mw *mlx5_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
			       struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
1864
	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
1865
	struct mlx5_ib_mw *mw = NULL;
1866 1867
	u32 *in = NULL;
	void *mkc;
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
	int ndescs;
	int err;
	struct mlx5_ib_alloc_mw req = {};
	struct {
		__u32	comp_mask;
		__u32	response_length;
	} resp = {};

	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
	if (err)
		return ERR_PTR(err);

	if (req.comp_mask || req.reserved1 || req.reserved2)
		return ERR_PTR(-EOPNOTSUPP);

	if (udata->inlen > sizeof(req) &&
	    !ib_is_udata_cleared(udata, sizeof(req),
				 udata->inlen - sizeof(req)))
		return ERR_PTR(-EOPNOTSUPP);

	ndescs = req.num_klms ? roundup(req.num_klms, 4) : roundup(1, 4);

	mw = kzalloc(sizeof(*mw), GFP_KERNEL);
1891
	in = kzalloc(inlen, GFP_KERNEL);
1892 1893 1894 1895 1896
	if (!mw || !in) {
		err = -ENOMEM;
		goto free;
	}

1897 1898 1899 1900 1901 1902 1903
	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);

	MLX5_SET(mkc, mkc, free, 1);
	MLX5_SET(mkc, mkc, translations_octword_size, ndescs);
	MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
	MLX5_SET(mkc, mkc, umr_en, 1);
	MLX5_SET(mkc, mkc, lr, 1);
1904
	MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_KLMS);
1905 1906 1907 1908
	MLX5_SET(mkc, mkc, en_rinval, !!((type == IB_MW_TYPE_2)));
	MLX5_SET(mkc, mkc, qpn, 0xffffff);

	err = mlx5_core_create_mkey(dev->mdev, &mw->mmkey, in, inlen);
1909 1910 1911
	if (err)
		goto free;

A
Artemy Kovalyov 已提交
1912
	mw->mmkey.type = MLX5_MKEY_MW;
1913
	mw->ibmw.rkey = mw->mmkey.key;
A
Artemy Kovalyov 已提交
1914
	mw->ndescs = ndescs;
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925

	resp.response_length = min(offsetof(typeof(resp), response_length) +
				   sizeof(resp.response_length), udata->outlen);
	if (resp.response_length) {
		err = ib_copy_to_udata(udata, &resp, resp.response_length);
		if (err) {
			mlx5_core_destroy_mkey(dev->mdev, &mw->mmkey);
			goto free;
		}
	}

1926 1927 1928 1929 1930 1931 1932 1933
	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
		err = xa_err(xa_store(&dev->odp_mkeys,
				      mlx5_base_mkey(mw->mmkey.key), &mw->mmkey,
				      GFP_KERNEL));
		if (err)
			goto free_mkey;
	}

1934 1935 1936
	kfree(in);
	return &mw->ibmw;

1937 1938
free_mkey:
	mlx5_core_destroy_mkey(dev->mdev, &mw->mmkey);
1939 1940 1941 1942 1943 1944 1945 1946
free:
	kfree(mw);
	kfree(in);
	return ERR_PTR(err);
}

int mlx5_ib_dealloc_mw(struct ib_mw *mw)
{
1947
	struct mlx5_ib_dev *dev = to_mdev(mw->device);
1948 1949 1950
	struct mlx5_ib_mw *mmw = to_mmw(mw);
	int err;

1951
	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1952
		xa_erase(&dev->odp_mkeys, mlx5_base_mkey(mmw->mmkey.key));
1953 1954 1955 1956
		/*
		 * pagefault_single_data_segment() may be accessing mmw under
		 * SRCU if the user bound an ODP MR to this MW.
		 */
1957
		synchronize_srcu(&dev->odp_srcu);
1958 1959 1960 1961 1962 1963 1964
	}

	err = mlx5_core_destroy_mkey(dev->mdev, &mmw->mmkey);
	if (err)
		return err;
	kfree(mmw);
	return 0;
1965 1966
}

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
			    struct ib_mr_status *mr_status)
{
	struct mlx5_ib_mr *mmr = to_mmr(ibmr);
	int ret = 0;

	if (check_mask & ~IB_MR_CHECK_SIG_STATUS) {
		pr_err("Invalid status check mask\n");
		ret = -EINVAL;
		goto done;
	}

	mr_status->fail_status = 0;
	if (check_mask & IB_MR_CHECK_SIG_STATUS) {
		if (!mmr->sig) {
			ret = -EINVAL;
			pr_err("signature status check requested on a non-signature enabled MR\n");
			goto done;
		}

		mmr->sig->sig_status_checked = true;
		if (!mmr->sig->sig_err_exists)
			goto done;

		if (ibmr->lkey == mmr->sig->err_item.key)
			memcpy(&mr_status->sig_err, &mmr->sig->err_item,
			       sizeof(mr_status->sig_err));
		else {
			mr_status->sig_err.err_type = IB_SIG_BAD_GUARD;
			mr_status->sig_err.sig_err_offset = 0;
			mr_status->sig_err.key = mmr->sig->err_item.key;
		}

		mmr->sig->sig_err_exists = false;
		mr_status->fail_status |= IB_MR_CHECK_SIG_STATUS;
	}

done:
	return ret;
}
2007

2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
static int
mlx5_ib_map_pa_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
			int data_sg_nents, unsigned int *data_sg_offset,
			struct scatterlist *meta_sg, int meta_sg_nents,
			unsigned int *meta_sg_offset)
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
	unsigned int sg_offset = 0;
	int n = 0;

	mr->meta_length = 0;
	if (data_sg_nents == 1) {
		n++;
		mr->ndescs = 1;
		if (data_sg_offset)
			sg_offset = *data_sg_offset;
		mr->data_length = sg_dma_len(data_sg) - sg_offset;
		mr->data_iova = sg_dma_address(data_sg) + sg_offset;
		if (meta_sg_nents == 1) {
			n++;
			mr->meta_ndescs = 1;
			if (meta_sg_offset)
				sg_offset = *meta_sg_offset;
			else
				sg_offset = 0;
			mr->meta_length = sg_dma_len(meta_sg) - sg_offset;
			mr->pi_iova = sg_dma_address(meta_sg) + sg_offset;
		}
		ibmr->length = mr->data_length + mr->meta_length;
	}

	return n;
}

2042 2043 2044
static int
mlx5_ib_sg_to_klms(struct mlx5_ib_mr *mr,
		   struct scatterlist *sgl,
2045
		   unsigned short sg_nents,
2046 2047 2048 2049
		   unsigned int *sg_offset_p,
		   struct scatterlist *meta_sgl,
		   unsigned short meta_sg_nents,
		   unsigned int *meta_sg_offset_p)
2050 2051 2052
{
	struct scatterlist *sg = sgl;
	struct mlx5_klm *klms = mr->descs;
2053
	unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2054
	u32 lkey = mr->ibmr.pd->local_dma_lkey;
2055
	int i, j = 0;
2056

2057
	mr->ibmr.iova = sg_dma_address(sg) + sg_offset;
2058 2059 2060
	mr->ibmr.length = 0;

	for_each_sg(sgl, sg, sg_nents, i) {
2061
		if (unlikely(i >= mr->max_descs))
2062
			break;
2063 2064
		klms[i].va = cpu_to_be64(sg_dma_address(sg) + sg_offset);
		klms[i].bcount = cpu_to_be32(sg_dma_len(sg) - sg_offset);
2065
		klms[i].key = cpu_to_be32(lkey);
2066
		mr->ibmr.length += sg_dma_len(sg) - sg_offset;
2067 2068

		sg_offset = 0;
2069 2070
	}

2071 2072 2073
	if (sg_offset_p)
		*sg_offset_p = sg_offset;

2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
	mr->ndescs = i;
	mr->data_length = mr->ibmr.length;

	if (meta_sg_nents) {
		sg = meta_sgl;
		sg_offset = meta_sg_offset_p ? *meta_sg_offset_p : 0;
		for_each_sg(meta_sgl, sg, meta_sg_nents, j) {
			if (unlikely(i + j >= mr->max_descs))
				break;
			klms[i + j].va = cpu_to_be64(sg_dma_address(sg) +
						     sg_offset);
			klms[i + j].bcount = cpu_to_be32(sg_dma_len(sg) -
							 sg_offset);
			klms[i + j].key = cpu_to_be32(lkey);
			mr->ibmr.length += sg_dma_len(sg) - sg_offset;

			sg_offset = 0;
		}
		if (meta_sg_offset_p)
			*meta_sg_offset_p = sg_offset;

		mr->meta_ndescs = j;
		mr->meta_length = mr->ibmr.length - mr->data_length;
	}

	return i + j;
2100 2101
}

2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
static int mlx5_set_page(struct ib_mr *ibmr, u64 addr)
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
	__be64 *descs;

	if (unlikely(mr->ndescs == mr->max_descs))
		return -ENOMEM;

	descs = mr->descs;
	descs[mr->ndescs++] = cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);

	return 0;
}

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
static int mlx5_set_page_pi(struct ib_mr *ibmr, u64 addr)
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
	__be64 *descs;

	if (unlikely(mr->ndescs + mr->meta_ndescs == mr->max_descs))
		return -ENOMEM;

	descs = mr->descs;
	descs[mr->ndescs + mr->meta_ndescs++] =
		cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);

	return 0;
}

static int
mlx5_ib_map_mtt_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
2133 2134 2135 2136 2137
			 int data_sg_nents, unsigned int *data_sg_offset,
			 struct scatterlist *meta_sg, int meta_sg_nents,
			 unsigned int *meta_sg_offset)
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
2138
	struct mlx5_ib_mr *pi_mr = mr->mtt_mr;
2139 2140
	int n;

2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
	pi_mr->ndescs = 0;
	pi_mr->meta_ndescs = 0;
	pi_mr->meta_length = 0;

	ib_dma_sync_single_for_cpu(ibmr->device, pi_mr->desc_map,
				   pi_mr->desc_size * pi_mr->max_descs,
				   DMA_TO_DEVICE);

	pi_mr->ibmr.page_size = ibmr->page_size;
	n = ib_sg_to_pages(&pi_mr->ibmr, data_sg, data_sg_nents, data_sg_offset,
			   mlx5_set_page);
	if (n != data_sg_nents)
		return n;

2155
	pi_mr->data_iova = pi_mr->ibmr.iova;
2156 2157 2158 2159 2160 2161
	pi_mr->data_length = pi_mr->ibmr.length;
	pi_mr->ibmr.length = pi_mr->data_length;
	ibmr->length = pi_mr->data_length;

	if (meta_sg_nents) {
		u64 page_mask = ~((u64)ibmr->page_size - 1);
2162
		u64 iova = pi_mr->data_iova;
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204

		n += ib_sg_to_pages(&pi_mr->ibmr, meta_sg, meta_sg_nents,
				    meta_sg_offset, mlx5_set_page_pi);

		pi_mr->meta_length = pi_mr->ibmr.length;
		/*
		 * PI address for the HW is the offset of the metadata address
		 * relative to the first data page address.
		 * It equals to first data page address + size of data pages +
		 * metadata offset at the first metadata page
		 */
		pi_mr->pi_iova = (iova & page_mask) +
				 pi_mr->ndescs * ibmr->page_size +
				 (pi_mr->ibmr.iova & ~page_mask);
		/*
		 * In order to use one MTT MR for data and metadata, we register
		 * also the gaps between the end of the data and the start of
		 * the metadata (the sig MR will verify that the HW will access
		 * to right addresses). This mapping is safe because we use
		 * internal mkey for the registration.
		 */
		pi_mr->ibmr.length = pi_mr->pi_iova + pi_mr->meta_length - iova;
		pi_mr->ibmr.iova = iova;
		ibmr->length += pi_mr->meta_length;
	}

	ib_dma_sync_single_for_device(ibmr->device, pi_mr->desc_map,
				      pi_mr->desc_size * pi_mr->max_descs,
				      DMA_TO_DEVICE);

	return n;
}

static int
mlx5_ib_map_klm_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
			 int data_sg_nents, unsigned int *data_sg_offset,
			 struct scatterlist *meta_sg, int meta_sg_nents,
			 unsigned int *meta_sg_offset)
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
	struct mlx5_ib_mr *pi_mr = mr->klm_mr;
	int n;
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216

	pi_mr->ndescs = 0;
	pi_mr->meta_ndescs = 0;
	pi_mr->meta_length = 0;

	ib_dma_sync_single_for_cpu(ibmr->device, pi_mr->desc_map,
				   pi_mr->desc_size * pi_mr->max_descs,
				   DMA_TO_DEVICE);

	n = mlx5_ib_sg_to_klms(pi_mr, data_sg, data_sg_nents, data_sg_offset,
			       meta_sg, meta_sg_nents, meta_sg_offset);

2217 2218 2219 2220
	ib_dma_sync_single_for_device(ibmr->device, pi_mr->desc_map,
				      pi_mr->desc_size * pi_mr->max_descs,
				      DMA_TO_DEVICE);

2221
	/* This is zero-based memory region */
2222
	pi_mr->data_iova = 0;
2223
	pi_mr->ibmr.iova = 0;
2224
	pi_mr->pi_iova = pi_mr->data_length;
2225 2226
	ibmr->length = pi_mr->ibmr.length;

2227 2228
	return n;
}
2229

2230 2231 2232 2233 2234 2235
int mlx5_ib_map_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
			 int data_sg_nents, unsigned int *data_sg_offset,
			 struct scatterlist *meta_sg, int meta_sg_nents,
			 unsigned int *meta_sg_offset)
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
2236
	struct mlx5_ib_mr *pi_mr = NULL;
2237 2238 2239 2240
	int n;

	WARN_ON(ibmr->type != IB_MR_TYPE_INTEGRITY);

2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
	mr->ndescs = 0;
	mr->data_length = 0;
	mr->data_iova = 0;
	mr->meta_ndescs = 0;
	mr->pi_iova = 0;
	/*
	 * As a performance optimization, if possible, there is no need to
	 * perform UMR operation to register the data/metadata buffers.
	 * First try to map the sg lists to PA descriptors with local_dma_lkey.
	 * Fallback to UMR only in case of a failure.
	 */
	n = mlx5_ib_map_pa_mr_sg_pi(ibmr, data_sg, data_sg_nents,
				    data_sg_offset, meta_sg, meta_sg_nents,
				    meta_sg_offset);
	if (n == data_sg_nents + meta_sg_nents)
		goto out;
2257 2258 2259 2260 2261 2262 2263 2264
	/*
	 * As a performance optimization, if possible, there is no need to map
	 * the sg lists to KLM descriptors. First try to map the sg lists to MTT
	 * descriptors and fallback to KLM only in case of a failure.
	 * It's more efficient for the HW to work with MTT descriptors
	 * (especially in high load).
	 * Use KLM (indirect access) only if it's mandatory.
	 */
2265
	pi_mr = mr->mtt_mr;
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
	n = mlx5_ib_map_mtt_mr_sg_pi(ibmr, data_sg, data_sg_nents,
				     data_sg_offset, meta_sg, meta_sg_nents,
				     meta_sg_offset);
	if (n == data_sg_nents + meta_sg_nents)
		goto out;

	pi_mr = mr->klm_mr;
	n = mlx5_ib_map_klm_mr_sg_pi(ibmr, data_sg, data_sg_nents,
				     data_sg_offset, meta_sg, meta_sg_nents,
				     meta_sg_offset);
2276 2277 2278
	if (unlikely(n != data_sg_nents + meta_sg_nents))
		return -ENOMEM;

2279 2280 2281 2282
out:
	/* This is zero-based memory region */
	ibmr->iova = 0;
	mr->pi_mr = pi_mr;
2283 2284 2285 2286
	if (pi_mr)
		ibmr->sig_attrs->meta_length = pi_mr->meta_length;
	else
		ibmr->sig_attrs->meta_length = mr->meta_length;
2287

2288 2289 2290
	return 0;
}

2291
int mlx5_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
2292
		      unsigned int *sg_offset)
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
{
	struct mlx5_ib_mr *mr = to_mmr(ibmr);
	int n;

	mr->ndescs = 0;

	ib_dma_sync_single_for_cpu(ibmr->device, mr->desc_map,
				   mr->desc_size * mr->max_descs,
				   DMA_TO_DEVICE);

2303
	if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
2304 2305
		n = mlx5_ib_sg_to_klms(mr, sg, sg_nents, sg_offset, NULL, 0,
				       NULL);
2306
	else
2307 2308
		n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset,
				mlx5_set_page);
2309 2310 2311 2312 2313 2314 2315

	ib_dma_sync_single_for_device(ibmr->device, mr->desc_map,
				      mr->desc_size * mr->max_descs,
				      DMA_TO_DEVICE);

	return n;
}