hns_roce_mr.c 26.5 KB
Newer Older
1 2 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
/*
 * Copyright (c) 2016 Hisilicon Limited.
 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
 *
 * 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/platform_device.h>
35
#include <linux/vmalloc.h>
36 37 38 39 40 41 42 43 44 45
#include <rdma/ib_umem.h>
#include "hns_roce_device.h"
#include "hns_roce_cmd.h"
#include "hns_roce_hem.h"

static u32 hw_index_to_key(unsigned long ind)
{
	return (u32)(ind >> 24) | (ind << 8);
}

S
Shaobo Xu 已提交
46
unsigned long key_to_hw_index(u32 key)
47 48 49 50
{
	return (key << 24) | (key >> 8);
}

51 52 53
static int hns_roce_hw_create_mpt(struct hns_roce_dev *hr_dev,
				  struct hns_roce_cmd_mailbox *mailbox,
				  unsigned long mpt_index)
54 55
{
	return hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0, mpt_index, 0,
56
				 HNS_ROCE_CMD_CREATE_MPT,
57
				 HNS_ROCE_CMD_TIMEOUT_MSECS);
58 59
}

60 61 62
int hns_roce_hw_destroy_mpt(struct hns_roce_dev *hr_dev,
			    struct hns_roce_cmd_mailbox *mailbox,
			    unsigned long mpt_index)
63 64
{
	return hns_roce_cmd_mbox(hr_dev, 0, mailbox ? mailbox->dma : 0,
65
				 mpt_index, !mailbox, HNS_ROCE_CMD_DESTROY_MPT,
66
				 HNS_ROCE_CMD_TIMEOUT_MSECS);
67 68
}

69 70
static int alloc_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr,
			u32 pd, u64 iova, u64 size, u32 access)
71
{
72 73 74
	struct ib_device *ibdev = &hr_dev->ib_dev;
	unsigned long obj = 0;
	int err;
75

76 77 78 79 80 81
	/* Allocate a key for mr from mr_table */
	err = hns_roce_bitmap_alloc(&hr_dev->mr_table.mtpt_bitmap, &obj);
	if (err) {
		ibdev_err(ibdev,
			  "failed to alloc bitmap for MR key, ret = %d.\n",
			  err);
82
		return -ENOMEM;
83 84
	}

85 86 87 88 89 90
	mr->iova = iova;			/* MR va starting addr */
	mr->size = size;			/* MR addr range */
	mr->pd = pd;				/* MR num */
	mr->access = access;			/* MR access permit */
	mr->enabled = 0;			/* MR active status */
	mr->key = hw_index_to_key(obj);		/* MR key */
91

92 93 94 95
	err = hns_roce_table_get(hr_dev, &hr_dev->mr_table.mtpt_table, obj);
	if (err) {
		ibdev_err(ibdev, "failed to alloc mtpt, ret = %d.\n", err);
		goto err_free_bitmap;
96 97 98
	}

	return 0;
99 100 101
err_free_bitmap:
	hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap, obj, BITMAP_NO_RR);
	return err;
102 103
}

104
static void free_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
105
{
106
	unsigned long obj = key_to_hw_index(mr->key);
107

108 109
	hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table, obj);
	hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap, obj, BITMAP_NO_RR);
110 111
}

112 113 114
static int alloc_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr,
			size_t length, struct ib_udata *udata, u64 start,
			int access)
115
{
116 117 118 119
	struct ib_device *ibdev = &hr_dev->ib_dev;
	bool is_fast = mr->type == MR_TYPE_FRMR;
	struct hns_roce_buf_attr buf_attr = {};
	int err;
120

121 122
	mr->pbl_hop_num = is_fast ? 1 : hr_dev->caps.pbl_hop_num;
	buf_attr.page_shift = is_fast ? PAGE_SHIFT :
123
			      hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT;
124 125 126 127 128 129
	buf_attr.region[0].size = length;
	buf_attr.region[0].hopnum = mr->pbl_hop_num;
	buf_attr.region_count = 1;
	buf_attr.user_access = access;
	/* fast MR's buffer is alloced before mapping, not at creation */
	buf_attr.mtt_only = is_fast;
130

131
	err = hns_roce_mtr_create(hr_dev, &mr->pbl_mtr, &buf_attr,
132
				  hr_dev->caps.pbl_ba_pg_sz + HNS_HW_PAGE_SHIFT,
133 134 135 136 137
				  udata, start);
	if (err)
		ibdev_err(ibdev, "failed to alloc pbl mtr, ret = %d.\n", err);
	else
		mr->npages = mr->pbl_mtr.hem_cfg.buf_pg_count;
138

139
	return err;
140 141
}

142
static void free_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
143
{
144
	hns_roce_mtr_destroy(hr_dev, &mr->pbl_mtr);
145 146 147 148 149
}

static void hns_roce_mr_free(struct hns_roce_dev *hr_dev,
			     struct hns_roce_mr *mr)
{
150
	struct ib_device *ibdev = &hr_dev->ib_dev;
151 152 153
	int ret;

	if (mr->enabled) {
154 155 156
		ret = hns_roce_hw_destroy_mpt(hr_dev, NULL,
					      key_to_hw_index(mr->key) &
					      (hr_dev->caps.num_mtpts - 1));
157
		if (ret)
158 159
			ibdev_warn(ibdev, "failed to destroy mpt, ret = %d.\n",
				   ret);
160 161
	}

162 163
	free_mr_pbl(hr_dev, mr);
	free_mr_key(hr_dev, mr);
164 165 166 167 168 169 170
}

static int hns_roce_mr_enable(struct hns_roce_dev *hr_dev,
			      struct hns_roce_mr *mr)
{
	unsigned long mtpt_idx = key_to_hw_index(mr->key);
	struct hns_roce_cmd_mailbox *mailbox;
L
Lang Cheng 已提交
171 172
	struct device *dev = hr_dev->dev;
	int ret;
173 174 175 176 177

	/* Allocate mailbox memory */
	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
	if (IS_ERR(mailbox)) {
		ret = PTR_ERR(mailbox);
178
		return ret;
179 180
	}

Y
Yixian Liu 已提交
181
	if (mr->type != MR_TYPE_FRMR)
182 183
		ret = hr_dev->hw->write_mtpt(hr_dev, mailbox->buf, mr,
					     mtpt_idx);
Y
Yixian Liu 已提交
184
	else
185
		ret = hr_dev->hw->frmr_write_mtpt(hr_dev, mailbox->buf, mr);
186
	if (ret) {
Y
Yixing Liu 已提交
187
		dev_err(dev, "failed to write mtpt, ret = %d.\n", ret);
188 189 190
		goto err_page;
	}

191 192
	ret = hns_roce_hw_create_mpt(hr_dev, mailbox,
				     mtpt_idx & (hr_dev->caps.num_mtpts - 1));
193
	if (ret) {
Y
Yixing Liu 已提交
194
		dev_err(dev, "failed to create mpt, ret = %d.\n", ret);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		goto err_page;
	}

	mr->enabled = 1;
	hns_roce_free_cmd_mailbox(hr_dev, mailbox);

	return 0;

err_page:
	hns_roce_free_cmd_mailbox(hr_dev, mailbox);

	return ret;
}

int hns_roce_init_mr_table(struct hns_roce_dev *hr_dev)
{
	struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
212
	int ret;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	ret = hns_roce_bitmap_init(&mr_table->mtpt_bitmap,
				   hr_dev->caps.num_mtpts,
				   hr_dev->caps.num_mtpts - 1,
				   hr_dev->caps.reserved_mrws, 0);
	return ret;
}

void hns_roce_cleanup_mr_table(struct hns_roce_dev *hr_dev)
{
	struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;

	hns_roce_bitmap_cleanup(&mr_table->mtpt_bitmap);
}

struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc)
{
230
	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
231 232
	struct hns_roce_mr *mr;
	int ret;
233

234
	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
235 236 237
	if (mr == NULL)
		return  ERR_PTR(-ENOMEM);

Y
Yixian Liu 已提交
238 239
	mr->type = MR_TYPE_DMA;

240
	/* Allocate memory region key */
241 242
	hns_roce_hem_list_init(&mr->pbl_mtr.hem_list);
	ret = alloc_mr_key(hr_dev, mr, to_hr_pd(pd)->pdn, 0, 0, acc);
243 244 245 246 247 248 249 250 251 252 253
	if (ret)
		goto err_free;

	ret = hns_roce_mr_enable(to_hr_dev(pd->device), mr);
	if (ret)
		goto err_mr;

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

	return &mr->ibmr;
err_mr:
254
	free_mr_key(hr_dev, mr);
255 256 257 258 259 260 261 262 263 264 265

err_free:
	kfree(mr);
	return ERR_PTR(ret);
}

struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
				   u64 virt_addr, int access_flags,
				   struct ib_udata *udata)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
266 267
	struct hns_roce_mr *mr;
	int ret;
268

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

Y
Yixian Liu 已提交
273
	mr->type = MR_TYPE_MR;
274 275
	ret = alloc_mr_key(hr_dev, mr, to_hr_pd(pd)->pdn, virt_addr, length,
			   access_flags);
276
	if (ret)
277
		goto err_alloc_mr;
278

279
	ret = alloc_mr_pbl(hr_dev, mr, length, udata, start, access_flags);
280
	if (ret)
281
		goto err_alloc_key;
282 283 284

	ret = hns_roce_mr_enable(hr_dev, mr);
	if (ret)
285
		goto err_alloc_pbl;
286 287

	mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
288
	mr->ibmr.length = length;
289 290 291

	return &mr->ibmr;

292 293 294 295 296
err_alloc_pbl:
	free_mr_pbl(hr_dev, mr);
err_alloc_key:
	free_mr_key(hr_dev, mr);
err_alloc_mr:
297 298 299 300
	kfree(mr);
	return ERR_PTR(ret);
}

301 302 303 304 305 306 307
static int rereg_mr_trans(struct ib_mr *ibmr, int flags,
			  u64 start, u64 length,
			  u64 virt_addr, int mr_access_flags,
			  struct hns_roce_cmd_mailbox *mailbox,
			  u32 pdn, struct ib_udata *udata)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
308
	struct ib_device *ibdev = &hr_dev->ib_dev;
309 310 311
	struct hns_roce_mr *mr = to_hr_mr(ibmr);
	int ret;

312 313 314 315 316
	free_mr_pbl(hr_dev, mr);
	ret = alloc_mr_pbl(hr_dev, mr, length, udata, start, mr_access_flags);
	if (ret) {
		ibdev_err(ibdev, "failed to create mr PBL, ret = %d.\n", ret);
		return ret;
317 318 319 320 321 322
	}

	ret = hr_dev->hw->rereg_write_mtpt(hr_dev, mr, flags, pdn,
					   mr_access_flags, virt_addr,
					   length, mailbox->buf);
	if (ret) {
323 324
		ibdev_err(ibdev, "failed to write mtpt, ret = %d.\n", ret);
		free_mr_pbl(hr_dev, mr);
325 326 327 328 329
	}

	return ret;
}

330 331 332 333
struct ib_mr *hns_roce_rereg_user_mr(struct ib_mr *ibmr, int flags, u64 start,
				     u64 length, u64 virt_addr,
				     int mr_access_flags, struct ib_pd *pd,
				     struct ib_udata *udata)
334 335
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
336
	struct ib_device *ib_dev = &hr_dev->ib_dev;
337 338 339 340 341 342 343
	struct hns_roce_mr *mr = to_hr_mr(ibmr);
	struct hns_roce_cmd_mailbox *mailbox;
	unsigned long mtpt_idx;
	u32 pdn = 0;
	int ret;

	if (!mr->enabled)
344
		return ERR_PTR(-EINVAL);
345 346 347

	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
	if (IS_ERR(mailbox))
348
		return ERR_CAST(mailbox);
349 350 351 352 353 354 355 356

	mtpt_idx = key_to_hw_index(mr->key) & (hr_dev->caps.num_mtpts - 1);
	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, mtpt_idx, 0,
				HNS_ROCE_CMD_QUERY_MPT,
				HNS_ROCE_CMD_TIMEOUT_MSECS);
	if (ret)
		goto free_cmd_mbox;

357
	ret = hns_roce_hw_destroy_mpt(hr_dev, NULL, mtpt_idx);
358
	if (ret)
359
		ibdev_warn(ib_dev, "failed to destroy MPT, ret = %d.\n", ret);
360 361 362 363 364 365 366

	mr->enabled = 0;

	if (flags & IB_MR_REREG_PD)
		pdn = to_hr_pd(pd)->pdn;

	if (flags & IB_MR_REREG_TRANS) {
367 368 369 370 371
		ret = rereg_mr_trans(ibmr, flags,
				     start, length,
				     virt_addr, mr_access_flags,
				     mailbox, pdn, udata);
		if (ret)
372
			goto free_cmd_mbox;
373 374 375 376 377
	} else {
		ret = hr_dev->hw->rereg_write_mtpt(hr_dev, mr, flags, pdn,
						   mr_access_flags, virt_addr,
						   length, mailbox->buf);
		if (ret)
378 379 380
			goto free_cmd_mbox;
	}

381
	ret = hns_roce_hw_create_mpt(hr_dev, mailbox, mtpt_idx);
382
	if (ret) {
383
		ibdev_err(ib_dev, "failed to create MPT, ret = %d.\n", ret);
384
		goto free_cmd_mbox;
385 386 387 388 389 390 391 392
	}

	mr->enabled = 1;
	if (flags & IB_MR_REREG_ACCESS)
		mr->access = mr_access_flags;

	hns_roce_free_cmd_mailbox(hr_dev, mailbox);

393
	return NULL;
394 395 396 397

free_cmd_mbox:
	hns_roce_free_cmd_mailbox(hr_dev, mailbox);

398
	return ERR_PTR(ret);
399 400
}

401
int hns_roce_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
402
{
S
Shaobo Xu 已提交
403
	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
404
	struct hns_roce_mr *mr = to_hr_mr(ibmr);
S
Shaobo Xu 已提交
405
	int ret = 0;
406

S
Shaobo Xu 已提交
407
	if (hr_dev->hw->dereg_mr) {
408
		ret = hr_dev->hw->dereg_mr(hr_dev, mr, udata);
S
Shaobo Xu 已提交
409 410 411 412 413 414
	} else {
		hns_roce_mr_free(hr_dev, mr);
		kfree(mr);
	}

	return ret;
415
}
Y
Yixian Liu 已提交
416

Y
Yixian Liu 已提交
417
struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
418
				u32 max_num_sg)
Y
Yixian Liu 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
{
	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
	struct device *dev = hr_dev->dev;
	struct hns_roce_mr *mr;
	u64 length;
	int ret;

	if (mr_type != IB_MR_TYPE_MEM_REG)
		return ERR_PTR(-EINVAL);

	if (max_num_sg > HNS_ROCE_FRMR_MAX_PA) {
		dev_err(dev, "max_num_sg larger than %d\n",
			HNS_ROCE_FRMR_MAX_PA);
		return ERR_PTR(-EINVAL);
	}

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

	mr->type = MR_TYPE_FRMR;

	/* Allocate memory region key */
442 443
	length = max_num_sg * (1 << PAGE_SHIFT);
	ret = alloc_mr_key(hr_dev, mr, to_hr_pd(pd)->pdn, 0, length, 0);
Y
Yixian Liu 已提交
444 445 446
	if (ret)
		goto err_free;

447 448 449 450
	ret = alloc_mr_pbl(hr_dev, mr, length, NULL, 0, 0);
	if (ret)
		goto err_key;

Y
Yixian Liu 已提交
451 452
	ret = hns_roce_mr_enable(hr_dev, mr);
	if (ret)
453
		goto err_pbl;
Y
Yixian Liu 已提交
454 455

	mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
456
	mr->ibmr.length = length;
Y
Yixian Liu 已提交
457 458 459

	return &mr->ibmr;

460 461 462 463
err_key:
	free_mr_key(hr_dev, mr);
err_pbl:
	free_mr_pbl(hr_dev, mr);
Y
Yixian Liu 已提交
464 465 466 467 468 469 470 471 472
err_free:
	kfree(mr);
	return ERR_PTR(ret);
}

static int hns_roce_set_page(struct ib_mr *ibmr, u64 addr)
{
	struct hns_roce_mr *mr = to_hr_mr(ibmr);

473 474 475 476
	if (likely(mr->npages < mr->pbl_mtr.hem_cfg.buf_pg_count)) {
		mr->page_list[mr->npages++] = addr;
		return 0;
	}
Y
Yixian Liu 已提交
477

478
	return -ENOBUFS;
Y
Yixian Liu 已提交
479 480 481 482 483
}

int hns_roce_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
		       unsigned int *sg_offset)
{
484 485
	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
	struct ib_device *ibdev = &hr_dev->ib_dev;
Y
Yixian Liu 已提交
486
	struct hns_roce_mr *mr = to_hr_mr(ibmr);
X
Xi Wang 已提交
487
	struct hns_roce_mtr *mtr = &mr->pbl_mtr;
488
	int ret = 0;
Y
Yixian Liu 已提交
489 490

	mr->npages = 0;
491 492 493 494 495 496 497
	mr->page_list = kvcalloc(mr->pbl_mtr.hem_cfg.buf_pg_count,
				 sizeof(dma_addr_t), GFP_KERNEL);
	if (!mr->page_list)
		return ret;

	ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, hns_roce_set_page);
	if (ret < 1) {
Y
Yixing Liu 已提交
498
		ibdev_err(ibdev, "failed to store sg pages %u %u, cnt = %d.\n",
499 500 501
			  mr->npages, mr->pbl_mtr.hem_cfg.buf_pg_count, ret);
		goto err_page_list;
	}
Y
Yixian Liu 已提交
502

X
Xi Wang 已提交
503 504 505 506 507
	mtr->hem_cfg.region[0].offset = 0;
	mtr->hem_cfg.region[0].count = mr->npages;
	mtr->hem_cfg.region[0].hopnum = mr->pbl_hop_num;
	mtr->hem_cfg.region_count = 1;
	ret = hns_roce_mtr_map(hr_dev, mtr, mr->page_list, mr->npages);
508 509 510 511
	if (ret) {
		ibdev_err(ibdev, "failed to map sg mtr, ret = %d.\n", ret);
		ret = 0;
	} else {
512
		mr->pbl_mtr.hem_cfg.buf_pg_shift = (u32)ilog2(ibmr->page_size);
513 514 515 516 517 518 519 520
		ret = mr->npages;
	}

err_page_list:
	kvfree(mr->page_list);
	mr->page_list = NULL;

	return ret;
Y
Yixian Liu 已提交
521 522
}

Y
Yixian Liu 已提交
523 524 525 526 527 528 529
static void hns_roce_mw_free(struct hns_roce_dev *hr_dev,
			     struct hns_roce_mw *mw)
{
	struct device *dev = hr_dev->dev;
	int ret;

	if (mw->enabled) {
530 531 532
		ret = hns_roce_hw_destroy_mpt(hr_dev, NULL,
					      key_to_hw_index(mw->rkey) &
					      (hr_dev->caps.num_mtpts - 1));
Y
Yixian Liu 已提交
533
		if (ret)
534
			dev_warn(dev, "MW DESTROY_MPT failed (%d)\n", ret);
Y
Yixian Liu 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

		hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table,
				   key_to_hw_index(mw->rkey));
	}

	hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap,
			     key_to_hw_index(mw->rkey), BITMAP_NO_RR);
}

static int hns_roce_mw_enable(struct hns_roce_dev *hr_dev,
			      struct hns_roce_mw *mw)
{
	struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
	struct hns_roce_cmd_mailbox *mailbox;
	struct device *dev = hr_dev->dev;
	unsigned long mtpt_idx = key_to_hw_index(mw->rkey);
	int ret;

	/* prepare HEM entry memory */
	ret = hns_roce_table_get(hr_dev, &mr_table->mtpt_table, mtpt_idx);
	if (ret)
		return ret;

	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
	if (IS_ERR(mailbox)) {
		ret = PTR_ERR(mailbox);
		goto err_table;
	}

	ret = hr_dev->hw->mw_write_mtpt(mailbox->buf, mw);
	if (ret) {
		dev_err(dev, "MW write mtpt fail!\n");
		goto err_page;
	}

570 571
	ret = hns_roce_hw_create_mpt(hr_dev, mailbox,
				     mtpt_idx & (hr_dev->caps.num_mtpts - 1));
Y
Yixian Liu 已提交
572
	if (ret) {
573
		dev_err(dev, "MW CREATE_MPT failed (%d)\n", ret);
Y
Yixian Liu 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
		goto err_page;
	}

	mw->enabled = 1;

	hns_roce_free_cmd_mailbox(hr_dev, mailbox);

	return 0;

err_page:
	hns_roce_free_cmd_mailbox(hr_dev, mailbox);

err_table:
	hns_roce_table_put(hr_dev, &mr_table->mtpt_table, mtpt_idx);

	return ret;
}

592
int hns_roce_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
Y
Yixian Liu 已提交
593
{
594 595
	struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
	struct hns_roce_mw *mw = to_hr_mw(ibmw);
Y
Yixian Liu 已提交
596 597 598 599 600 601
	unsigned long index = 0;
	int ret;

	/* Allocate a key for mw from bitmap */
	ret = hns_roce_bitmap_alloc(&hr_dev->mr_table.mtpt_bitmap, &index);
	if (ret)
602
		return ret;
Y
Yixian Liu 已提交
603 604 605

	mw->rkey = hw_index_to_key(index);

606 607
	ibmw->rkey = mw->rkey;
	mw->pdn = to_hr_pd(ibmw->pd)->pdn;
Y
Yixian Liu 已提交
608 609 610 611 612 613 614 615
	mw->pbl_hop_num = hr_dev->caps.pbl_hop_num;
	mw->pbl_ba_pg_sz = hr_dev->caps.pbl_ba_pg_sz;
	mw->pbl_buf_pg_sz = hr_dev->caps.pbl_buf_pg_sz;

	ret = hns_roce_mw_enable(hr_dev, mw);
	if (ret)
		goto err_mw;

616
	return 0;
Y
Yixian Liu 已提交
617 618 619

err_mw:
	hns_roce_mw_free(hr_dev, mw);
620
	return ret;
Y
Yixian Liu 已提交
621 622 623 624 625 626 627 628 629 630
}

int hns_roce_dealloc_mw(struct ib_mw *ibmw)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
	struct hns_roce_mw *mw = to_hr_mw(ibmw);

	hns_roce_mw_free(hr_dev, mw);
	return 0;
}
631

632 633
static int mtr_map_region(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
			  dma_addr_t *pages, struct hns_roce_buf_region *region)
634
{
635
	__le64 *mtts;
636 637 638
	int offset;
	int count;
	int npage;
639
	u64 addr;
640 641 642
	int end;
	int i;

643 644 645 646 647 648
	/* if hopnum is 0, buffer cannot store BAs, so skip write mtt */
	if (!region->hopnum)
		return 0;

	offset = region->offset;
	end = offset + region->count;
649 650 651 652 653 654 655 656 657
	npage = 0;
	while (offset < end) {
		mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
						  offset, &count, NULL);
		if (!mtts)
			return -ENOBUFS;

		for (i = 0; i < count; i++) {
			if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
658
				addr = to_hr_hw_page_addr(pages[npage]);
659
			else
660
				addr = pages[npage];
661

662
			mtts[i] = cpu_to_le64(addr);
663 664 665 666 667 668 669 670
			npage++;
		}
		offset += count;
	}

	return 0;
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
static inline bool mtr_has_mtt(struct hns_roce_buf_attr *attr)
{
	int i;

	for (i = 0; i < attr->region_count; i++)
		if (attr->region[i].hopnum != HNS_ROCE_HOP_NUM_0 &&
		    attr->region[i].hopnum > 0)
			return true;

	/* because the mtr only one root base address, when hopnum is 0 means
	 * root base address equals the first buffer address, thus all alloced
	 * memory must in a continuous space accessed by direct mode.
	 */
	return false;
}

static inline size_t mtr_bufs_size(struct hns_roce_buf_attr *attr)
{
	size_t size = 0;
	int i;

	for (i = 0; i < attr->region_count; i++)
		size += attr->region[i].size;

	return size;
}

/*
 * check the given pages in continuous address space
 * Returns 0 on success, or the error page num.
 */
static inline int mtr_check_direct_pages(dma_addr_t *pages, int page_count,
703
					 unsigned int page_shift)
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
{
	size_t page_size = 1 << page_shift;
	int i;

	for (i = 1; i < page_count; i++)
		if (pages[i] - pages[i - 1] != page_size)
			return i;

	return 0;
}

static void mtr_free_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
{
	/* release user buffers */
	if (mtr->umem) {
		ib_umem_release(mtr->umem);
		mtr->umem = NULL;
	}

	/* release kernel buffers */
	if (mtr->kmem) {
		hns_roce_buf_free(hr_dev, mtr->kmem);
		mtr->kmem = NULL;
	}
}

static int mtr_alloc_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
731
			  struct hns_roce_buf_attr *buf_attr,
732 733 734 735 736 737 738 739 740 741 742 743
			  struct ib_udata *udata, unsigned long user_addr)
{
	struct ib_device *ibdev = &hr_dev->ib_dev;
	size_t total_size;

	total_size = mtr_bufs_size(buf_attr);

	if (udata) {
		mtr->kmem = NULL;
		mtr->umem = ib_umem_get(ibdev, user_addr, total_size,
					buf_attr->user_access);
		if (IS_ERR_OR_NULL(mtr->umem)) {
744
			ibdev_err(ibdev, "failed to get umem, ret = %ld.\n",
745 746 747 748 749
				  PTR_ERR(mtr->umem));
			return -ENOMEM;
		}
	} else {
		mtr->umem = NULL;
750 751 752 753
		mtr->kmem = hns_roce_buf_alloc(hr_dev, total_size,
					       buf_attr->page_shift,
					       mtr->hem_cfg.is_direct ?
					       HNS_ROCE_BUF_DIRECT : 0);
754 755 756 757
		if (IS_ERR(mtr->kmem)) {
			ibdev_err(ibdev, "failed to alloc kmem, ret = %ld.\n",
				  PTR_ERR(mtr->kmem));
			return PTR_ERR(mtr->kmem);
758 759 760 761 762 763
		}
	}

	return 0;
}

764 765
static int mtr_map_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
			int page_count, unsigned int page_shift)
766 767
{
	struct ib_device *ibdev = &hr_dev->ib_dev;
768
	dma_addr_t *pages;
769
	int npage;
770 771 772 773 774 775
	int ret;

	/* alloc a tmp array to store buffer's dma address */
	pages = kvcalloc(page_count, sizeof(dma_addr_t), GFP_KERNEL);
	if (!pages)
		return -ENOMEM;
776 777

	if (mtr->umem)
778
		npage = hns_roce_get_umem_bufs(hr_dev, pages, page_count, 0,
779 780
					       mtr->umem, page_shift);
	else
781
		npage = hns_roce_get_kmem_bufs(hr_dev, pages, page_count, 0,
782 783
					       mtr->kmem);

784 785 786 787 788 789 790
	if (npage != page_count) {
		ibdev_err(ibdev, "failed to get mtr page %d != %d.\n", npage,
			  page_count);
		ret = -ENOBUFS;
		goto err_alloc_list;
	}

791
	if (mtr->hem_cfg.is_direct && npage > 1) {
792 793 794 795 796 797
		ret = mtr_check_direct_pages(pages, npage, page_shift);
		if (ret) {
			ibdev_err(ibdev, "failed to check %s mtr, idx = %d.\n",
				  mtr->umem ? "user" : "kernel", ret);
			ret = -ENOBUFS;
			goto err_alloc_list;
798 799 800
		}
	}

801 802 803 804 805 806 807 808
	ret = hns_roce_mtr_map(hr_dev, mtr, pages, page_count);
	if (ret)
		ibdev_err(ibdev, "failed to map mtr pages, ret = %d.\n", ret);

err_alloc_list:
	kvfree(pages);

	return ret;
809 810 811
}

int hns_roce_mtr_map(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
812
		     dma_addr_t *pages, unsigned int page_cnt)
813 814 815
{
	struct ib_device *ibdev = &hr_dev->ib_dev;
	struct hns_roce_buf_region *r;
816
	unsigned int i;
817 818
	int err;

819 820 821 822 823 824 825 826 827
	/*
	 * Only use the first page address as root ba when hopnum is 0, this
	 * is because the addresses of all pages are consecutive in this case.
	 */
	if (mtr->hem_cfg.is_direct) {
		mtr->hem_cfg.root_ba = pages[0];
		return 0;
	}

X
Xi Wang 已提交
828 829
	for (i = 0; i < mtr->hem_cfg.region_count; i++) {
		r = &mtr->hem_cfg.region[i];
830 831 832
		if (r->offset + r->count > page_cnt) {
			err = -EINVAL;
			ibdev_err(ibdev,
Y
Yixing Liu 已提交
833
				  "failed to check mtr%u end %u + %u, max %u.\n",
834 835 836 837 838 839 840
				  i, r->offset, r->count, page_cnt);
			return err;
		}

		err = mtr_map_region(hr_dev, mtr, &pages[r->offset], r);
		if (err) {
			ibdev_err(ibdev,
Y
Yixing Liu 已提交
841
				  "failed to map mtr%u offset %u, ret = %d.\n",
842 843 844 845 846 847 848 849
				  i, r->offset, err);
			return err;
		}
	}

	return 0;
}

850 851 852
int hns_roce_mtr_find(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
		      int offset, u64 *mtt_buf, int mtt_max, u64 *base_addr)
{
853
	struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
854
	int mtt_count, left;
855
	int start_index;
856
	int total = 0;
857
	__le64 *mtts;
858
	u32 npage;
859
	u64 addr;
860

861
	if (!mtt_buf || mtt_max < 1)
862 863
		goto done;

864
	/* no mtt memory in direct mode, so just return the buffer address */
865 866 867 868 869 870 871 872 873
	if (cfg->is_direct) {
		start_index = offset >> HNS_HW_PAGE_SHIFT;
		for (mtt_count = 0; mtt_count < cfg->region_count &&
		     total < mtt_max; mtt_count++) {
			npage = cfg->region[mtt_count].offset;
			if (npage < start_index)
				continue;

			addr = cfg->root_ba + (npage << HNS_HW_PAGE_SHIFT);
874 875 876 877
			if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
				mtt_buf[total] = to_hr_hw_page_addr(addr);
			else
				mtt_buf[total] = addr;
878 879

			total++;
880 881 882 883 884
		}

		goto done;
	}

885
	start_index = offset >> cfg->buf_pg_shift;
886 887 888
	left = mtt_max;
	while (left > 0) {
		mtt_count = 0;
889
		mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
890
						  start_index + total,
891
						  &mtt_count, NULL);
892
		if (!mtts || !mtt_count)
893 894 895 896
			goto done;

		npage = min(mtt_count, left);
		left -= npage;
897 898
		for (mtt_count = 0; mtt_count < npage; mtt_count++)
			mtt_buf[total++] = le64_to_cpu(mtts[mtt_count]);
899 900 901 902
	}

done:
	if (base_addr)
903
		*base_addr = cfg->root_ba;
904 905 906

	return total;
}
907

908 909 910
static int mtr_init_buf_cfg(struct hns_roce_dev *hr_dev,
			    struct hns_roce_buf_attr *attr,
			    struct hns_roce_hem_cfg *cfg,
911
			    unsigned int *buf_page_shift, int unalinged_size)
912 913
{
	struct hns_roce_buf_region *r;
914 915
	int first_region_padding;
	int page_cnt, region_cnt;
916
	unsigned int page_shift;
917 918
	size_t buf_size;

919 920 921
	/* If mtt is disabled, all pages must be within a continuous range */
	cfg->is_direct = !mtr_has_mtt(attr);
	buf_size = mtr_bufs_size(attr);
922
	if (cfg->is_direct) {
923 924 925 926 927
		/* When HEM buffer uses 0-level addressing, the page size is
		 * equal to the whole buffer size, and we split the buffer into
		 * small pages which is used to check whether the adjacent
		 * units are in the continuous space and its size is fixed to
		 * 4K based on hns ROCEE's requirement.
928
		 */
929 930 931 932 933 934 935
		page_shift = HNS_HW_PAGE_SHIFT;

		/* The ROCEE requires the page size to be 4K * 2 ^ N. */
		cfg->buf_pg_count = 1;
		cfg->buf_pg_shift = HNS_HW_PAGE_SHIFT +
			order_base_2(DIV_ROUND_UP(buf_size, HNS_HW_PAGE_SIZE));
		first_region_padding = 0;
936
	} else {
937 938 939 940 941
		page_shift = attr->page_shift;
		cfg->buf_pg_count = DIV_ROUND_UP(buf_size + unalinged_size,
						 1 << page_shift);
		cfg->buf_pg_shift = page_shift;
		first_region_padding = unalinged_size;
942 943
	}

944 945 946 947
	/* Convert buffer size to page index and page count for each region and
	 * the buffer's offset needs to be appended to the first region.
	 */
	for (page_cnt = 0, region_cnt = 0; region_cnt < attr->region_count &&
948 949 950
	     region_cnt < ARRAY_SIZE(cfg->region); region_cnt++) {
		r = &cfg->region[region_cnt];
		r->offset = page_cnt;
951 952
		buf_size = hr_hw_page_align(attr->region[region_cnt].size +
					    first_region_padding);
953
		r->count = DIV_ROUND_UP(buf_size, 1 << page_shift);
954
		first_region_padding = 0;
955 956 957 958 959 960 961 962 963
		page_cnt += r->count;
		r->hopnum = to_hr_hem_hopnum(attr->region[region_cnt].hopnum,
					     r->count);
	}

	cfg->region_count = region_cnt;
	*buf_page_shift = page_shift;

	return page_cnt;
964 965
}

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
static int mtr_alloc_mtt(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
			 unsigned int ba_page_shift)
{
	struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
	int ret;

	hns_roce_hem_list_init(&mtr->hem_list);
	if (!cfg->is_direct) {
		ret = hns_roce_hem_list_request(hr_dev, &mtr->hem_list,
						cfg->region, cfg->region_count,
						ba_page_shift);
		if (ret)
			return ret;
		cfg->root_ba = mtr->hem_list.root_ba;
		cfg->ba_pg_shift = ba_page_shift;
	} else {
		cfg->ba_pg_shift = cfg->buf_pg_shift;
	}

	return 0;
}

static void mtr_free_mtt(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
{
	hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
}

993 994 995
/**
 * hns_roce_mtr_create - Create hns memory translate region.
 *
996
 * @hr_dev: RoCE device struct pointer
997
 * @mtr: memory translate region
998 999
 * @buf_attr: buffer attribute for creating mtr
 * @ba_page_shift: page shift for multi-hop base address table
1000 1001 1002 1003
 * @udata: user space context, if it's NULL, means kernel space
 * @user_addr: userspace virtual address to start at
 */
int hns_roce_mtr_create(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
1004
			struct hns_roce_buf_attr *buf_attr,
1005
			unsigned int ba_page_shift, struct ib_udata *udata,
1006
			unsigned long user_addr)
1007 1008
{
	struct ib_device *ibdev = &hr_dev->ib_dev;
1009
	unsigned int buf_page_shift = 0;
1010 1011
	int buf_page_cnt;
	int ret;
1012

1013 1014 1015 1016 1017 1018 1019
	buf_page_cnt = mtr_init_buf_cfg(hr_dev, buf_attr, &mtr->hem_cfg,
					&buf_page_shift,
					udata ? user_addr & ~PAGE_MASK : 0);
	if (buf_page_cnt < 1 || buf_page_shift < HNS_HW_PAGE_SHIFT) {
		ibdev_err(ibdev, "failed to init mtr cfg, count %d shift %d.\n",
			  buf_page_cnt, buf_page_shift);
		return -EINVAL;
X
Xi Wang 已提交
1020 1021
	}

1022 1023 1024 1025
	ret = mtr_alloc_mtt(hr_dev, mtr, ba_page_shift);
	if (ret) {
		ibdev_err(ibdev, "failed to alloc mtr mtt, ret = %d.\n", ret);
		return ret;
1026 1027
	}

1028 1029 1030 1031 1032 1033
	/* The caller has its own buffer list and invokes the hns_roce_mtr_map()
	 * to finish the MTT configuration.
	 */
	if (buf_attr->mtt_only) {
		mtr->umem = NULL;
		mtr->kmem = NULL;
1034 1035 1036
		return 0;
	}

1037
	ret = mtr_alloc_bufs(hr_dev, mtr, buf_attr, udata, user_addr);
1038
	if (ret) {
1039 1040
		ibdev_err(ibdev, "failed to alloc mtr bufs, ret = %d.\n", ret);
		goto err_alloc_mtt;
1041 1042
	}

1043 1044 1045 1046 1047 1048 1049
	/* Write buffer's dma address to MTT */
	ret = mtr_map_bufs(hr_dev, mtr, buf_page_cnt, buf_page_shift);
	if (ret)
		ibdev_err(ibdev, "failed to map mtr bufs, ret = %d.\n", ret);
	else
		return 0;

1050
	mtr_free_bufs(hr_dev, mtr);
1051 1052
err_alloc_mtt:
	mtr_free_mtt(hr_dev, mtr);
1053
	return ret;
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
}

void hns_roce_mtr_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
{
	/* release multi-hop addressing resource */
	hns_roce_hem_list_release(hr_dev, &mtr->hem_list);

	/* free buffers */
	mtr_free_bufs(hr_dev, mtr);
}