xref: /linux/drivers/infiniband/hw/hns/hns_roce_mr.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 /*
2  * Copyright (c) 2016 Hisilicon Limited.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/vmalloc.h>
35 #include <rdma/ib_umem.h>
36 #include <linux/math.h>
37 #include "hns_roce_device.h"
38 #include "hns_roce_cmd.h"
39 #include "hns_roce_hem.h"
40 
41 static u32 hw_index_to_key(int ind)
42 {
43 	return ((u32)ind >> 24) | ((u32)ind << 8);
44 }
45 
46 unsigned long key_to_hw_index(u32 key)
47 {
48 	return (key << 24) | (key >> 8);
49 }
50 
51 static int alloc_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
52 {
53 	struct hns_roce_ida *mtpt_ida = &hr_dev->mr_table.mtpt_ida;
54 	struct ib_device *ibdev = &hr_dev->ib_dev;
55 	int err;
56 	int id;
57 
58 	/* Allocate a key for mr from mr_table */
59 	id = ida_alloc_range(&mtpt_ida->ida, mtpt_ida->min, mtpt_ida->max,
60 			     GFP_KERNEL);
61 	if (id < 0) {
62 		ibdev_err(ibdev, "failed to alloc id for MR key, id(%d)\n", id);
63 		return -ENOMEM;
64 	}
65 
66 	mr->key = hw_index_to_key(id); /* MR key */
67 
68 	err = hns_roce_table_get(hr_dev, &hr_dev->mr_table.mtpt_table,
69 				 (unsigned long)id);
70 	if (err) {
71 		ibdev_err(ibdev, "failed to alloc mtpt, ret = %d.\n", err);
72 		goto err_free_bitmap;
73 	}
74 
75 	return 0;
76 err_free_bitmap:
77 	ida_free(&mtpt_ida->ida, id);
78 	return err;
79 }
80 
81 static void free_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
82 {
83 	unsigned long obj = key_to_hw_index(mr->key);
84 
85 	hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table, obj);
86 	ida_free(&hr_dev->mr_table.mtpt_ida.ida, (int)obj);
87 }
88 
89 static int alloc_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr,
90 			struct ib_udata *udata, u64 start)
91 {
92 	struct ib_device *ibdev = &hr_dev->ib_dev;
93 	bool is_fast = mr->type == MR_TYPE_FRMR;
94 	struct hns_roce_buf_attr buf_attr = {};
95 	int err;
96 
97 	mr->pbl_hop_num = is_fast ? 1 : hr_dev->caps.pbl_hop_num;
98 	buf_attr.page_shift = is_fast ? PAGE_SHIFT :
99 			      hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT;
100 	buf_attr.region[0].size = mr->size;
101 	buf_attr.region[0].hopnum = mr->pbl_hop_num;
102 	buf_attr.region_count = 1;
103 	buf_attr.user_access = mr->access;
104 	/* fast MR's buffer is alloced before mapping, not at creation */
105 	buf_attr.mtt_only = is_fast;
106 
107 	err = hns_roce_mtr_create(hr_dev, &mr->pbl_mtr, &buf_attr,
108 				  hr_dev->caps.pbl_ba_pg_sz + PAGE_SHIFT,
109 				  udata, start);
110 	if (err)
111 		ibdev_err(ibdev, "failed to alloc pbl mtr, ret = %d.\n", err);
112 	else
113 		mr->npages = mr->pbl_mtr.hem_cfg.buf_pg_count;
114 
115 	return err;
116 }
117 
118 static void free_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
119 {
120 	hns_roce_mtr_destroy(hr_dev, &mr->pbl_mtr);
121 }
122 
123 static void hns_roce_mr_free(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
124 {
125 	struct ib_device *ibdev = &hr_dev->ib_dev;
126 	int ret;
127 
128 	if (mr->enabled) {
129 		ret = hns_roce_destroy_hw_ctx(hr_dev, HNS_ROCE_CMD_DESTROY_MPT,
130 					      key_to_hw_index(mr->key) &
131 					      (hr_dev->caps.num_mtpts - 1));
132 		if (ret)
133 			ibdev_warn(ibdev, "failed to destroy mpt, ret = %d.\n",
134 				   ret);
135 	}
136 
137 	free_mr_pbl(hr_dev, mr);
138 	free_mr_key(hr_dev, mr);
139 }
140 
141 static int hns_roce_mr_enable(struct hns_roce_dev *hr_dev,
142 			      struct hns_roce_mr *mr)
143 {
144 	unsigned long mtpt_idx = key_to_hw_index(mr->key);
145 	struct hns_roce_cmd_mailbox *mailbox;
146 	struct device *dev = hr_dev->dev;
147 	int ret;
148 
149 	/* Allocate mailbox memory */
150 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
151 	if (IS_ERR(mailbox))
152 		return PTR_ERR(mailbox);
153 
154 	if (mr->type != MR_TYPE_FRMR)
155 		ret = hr_dev->hw->write_mtpt(hr_dev, mailbox->buf, mr);
156 	else
157 		ret = hr_dev->hw->frmr_write_mtpt(hr_dev, mailbox->buf, mr);
158 	if (ret) {
159 		dev_err(dev, "failed to write mtpt, ret = %d.\n", ret);
160 		goto err_page;
161 	}
162 
163 	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_MPT,
164 				     mtpt_idx & (hr_dev->caps.num_mtpts - 1));
165 	if (ret) {
166 		dev_err(dev, "failed to create mpt, ret = %d.\n", ret);
167 		goto err_page;
168 	}
169 
170 	mr->enabled = 1;
171 
172 err_page:
173 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
174 
175 	return ret;
176 }
177 
178 void hns_roce_init_mr_table(struct hns_roce_dev *hr_dev)
179 {
180 	struct hns_roce_ida *mtpt_ida = &hr_dev->mr_table.mtpt_ida;
181 
182 	ida_init(&mtpt_ida->ida);
183 	mtpt_ida->max = hr_dev->caps.num_mtpts - 1;
184 	mtpt_ida->min = hr_dev->caps.reserved_mrws;
185 }
186 
187 struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc)
188 {
189 	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
190 	struct hns_roce_mr *mr;
191 	int ret;
192 
193 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
194 	if (!mr)
195 		return  ERR_PTR(-ENOMEM);
196 
197 	mr->type = MR_TYPE_DMA;
198 	mr->pd = to_hr_pd(pd)->pdn;
199 	mr->access = acc;
200 
201 	/* Allocate memory region key */
202 	hns_roce_hem_list_init(&mr->pbl_mtr.hem_list);
203 	ret = alloc_mr_key(hr_dev, mr);
204 	if (ret)
205 		goto err_free;
206 
207 	ret = hns_roce_mr_enable(hr_dev, mr);
208 	if (ret)
209 		goto err_mr;
210 
211 	mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
212 
213 	return &mr->ibmr;
214 err_mr:
215 	free_mr_key(hr_dev, mr);
216 
217 err_free:
218 	kfree(mr);
219 	return ERR_PTR(ret);
220 }
221 
222 struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
223 				   u64 virt_addr, int access_flags,
224 				   struct ib_udata *udata)
225 {
226 	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
227 	struct hns_roce_mr *mr;
228 	int ret;
229 
230 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
231 	if (!mr) {
232 		ret = -ENOMEM;
233 		goto err_out;
234 	}
235 
236 	mr->iova = virt_addr;
237 	mr->size = length;
238 	mr->pd = to_hr_pd(pd)->pdn;
239 	mr->access = access_flags;
240 	mr->type = MR_TYPE_MR;
241 
242 	ret = alloc_mr_key(hr_dev, mr);
243 	if (ret)
244 		goto err_alloc_mr;
245 
246 	ret = alloc_mr_pbl(hr_dev, mr, udata, start);
247 	if (ret)
248 		goto err_alloc_key;
249 
250 	ret = hns_roce_mr_enable(hr_dev, mr);
251 	if (ret)
252 		goto err_alloc_pbl;
253 
254 	mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
255 
256 	return &mr->ibmr;
257 
258 err_alloc_pbl:
259 	free_mr_pbl(hr_dev, mr);
260 err_alloc_key:
261 	free_mr_key(hr_dev, mr);
262 err_alloc_mr:
263 	kfree(mr);
264 err_out:
265 	atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_MR_REG_ERR_CNT]);
266 
267 	return ERR_PTR(ret);
268 }
269 
270 struct ib_mr *hns_roce_rereg_user_mr(struct ib_mr *ibmr, int flags, u64 start,
271 				     u64 length, u64 virt_addr,
272 				     int mr_access_flags, struct ib_pd *pd,
273 				     struct ib_udata *udata)
274 {
275 	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
276 	struct ib_device *ib_dev = &hr_dev->ib_dev;
277 	struct hns_roce_mr *mr = to_hr_mr(ibmr);
278 	struct hns_roce_cmd_mailbox *mailbox;
279 	unsigned long mtpt_idx;
280 	int ret;
281 
282 	if (!mr->enabled) {
283 		ret = -EINVAL;
284 		goto err_out;
285 	}
286 
287 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
288 	ret = PTR_ERR_OR_ZERO(mailbox);
289 	if (ret)
290 		goto err_out;
291 
292 	mtpt_idx = key_to_hw_index(mr->key) & (hr_dev->caps.num_mtpts - 1);
293 
294 	ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, HNS_ROCE_CMD_QUERY_MPT,
295 				mtpt_idx);
296 	if (ret)
297 		goto free_cmd_mbox;
298 
299 	ret = hns_roce_destroy_hw_ctx(hr_dev, HNS_ROCE_CMD_DESTROY_MPT,
300 				      mtpt_idx);
301 	if (ret)
302 		ibdev_warn(ib_dev, "failed to destroy MPT, ret = %d.\n", ret);
303 
304 	mr->enabled = 0;
305 	mr->iova = virt_addr;
306 	mr->size = length;
307 
308 	if (flags & IB_MR_REREG_PD)
309 		mr->pd = to_hr_pd(pd)->pdn;
310 
311 	if (flags & IB_MR_REREG_ACCESS)
312 		mr->access = mr_access_flags;
313 
314 	if (flags & IB_MR_REREG_TRANS) {
315 		free_mr_pbl(hr_dev, mr);
316 		ret = alloc_mr_pbl(hr_dev, mr, udata, start);
317 		if (ret) {
318 			ibdev_err(ib_dev, "failed to alloc mr PBL, ret = %d.\n",
319 				  ret);
320 			goto free_cmd_mbox;
321 		}
322 	}
323 
324 	ret = hr_dev->hw->rereg_write_mtpt(hr_dev, mr, flags, mailbox->buf);
325 	if (ret) {
326 		ibdev_err(ib_dev, "failed to write mtpt, ret = %d.\n", ret);
327 		goto free_cmd_mbox;
328 	}
329 
330 	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_MPT,
331 				     mtpt_idx);
332 	if (ret) {
333 		ibdev_err(ib_dev, "failed to create MPT, ret = %d.\n", ret);
334 		goto free_cmd_mbox;
335 	}
336 
337 	mr->enabled = 1;
338 
339 free_cmd_mbox:
340 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
341 
342 err_out:
343 	if (ret) {
344 		atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_MR_REREG_ERR_CNT]);
345 		return ERR_PTR(ret);
346 	}
347 
348 	return NULL;
349 }
350 
351 int hns_roce_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
352 {
353 	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
354 	struct hns_roce_mr *mr = to_hr_mr(ibmr);
355 
356 	if (hr_dev->hw->dereg_mr)
357 		hr_dev->hw->dereg_mr(hr_dev);
358 
359 	hns_roce_mr_free(hr_dev, mr);
360 	kfree(mr);
361 
362 	return 0;
363 }
364 
365 struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
366 				u32 max_num_sg)
367 {
368 	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
369 	struct device *dev = hr_dev->dev;
370 	struct hns_roce_mr *mr;
371 	int ret;
372 
373 	if (mr_type != IB_MR_TYPE_MEM_REG)
374 		return ERR_PTR(-EINVAL);
375 
376 	if (max_num_sg > HNS_ROCE_FRMR_MAX_PA) {
377 		dev_err(dev, "max_num_sg larger than %d\n",
378 			HNS_ROCE_FRMR_MAX_PA);
379 		return ERR_PTR(-EINVAL);
380 	}
381 
382 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
383 	if (!mr)
384 		return ERR_PTR(-ENOMEM);
385 
386 	mr->type = MR_TYPE_FRMR;
387 	mr->pd = to_hr_pd(pd)->pdn;
388 	mr->size = max_num_sg * (1 << PAGE_SHIFT);
389 
390 	/* Allocate memory region key */
391 	ret = alloc_mr_key(hr_dev, mr);
392 	if (ret)
393 		goto err_free;
394 
395 	ret = alloc_mr_pbl(hr_dev, mr, NULL, 0);
396 	if (ret)
397 		goto err_key;
398 
399 	ret = hns_roce_mr_enable(hr_dev, mr);
400 	if (ret)
401 		goto err_pbl;
402 
403 	mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
404 	mr->ibmr.length = mr->size;
405 
406 	return &mr->ibmr;
407 
408 err_pbl:
409 	free_mr_pbl(hr_dev, mr);
410 err_key:
411 	free_mr_key(hr_dev, mr);
412 err_free:
413 	kfree(mr);
414 	return ERR_PTR(ret);
415 }
416 
417 static int hns_roce_set_page(struct ib_mr *ibmr, u64 addr)
418 {
419 	struct hns_roce_mr *mr = to_hr_mr(ibmr);
420 
421 	if (likely(mr->npages < mr->pbl_mtr.hem_cfg.buf_pg_count)) {
422 		mr->page_list[mr->npages++] = addr;
423 		return 0;
424 	}
425 
426 	return -ENOBUFS;
427 }
428 
429 int hns_roce_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
430 		       unsigned int *sg_offset)
431 {
432 	struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
433 	struct ib_device *ibdev = &hr_dev->ib_dev;
434 	struct hns_roce_mr *mr = to_hr_mr(ibmr);
435 	struct hns_roce_mtr *mtr = &mr->pbl_mtr;
436 	int ret = 0;
437 
438 	mr->npages = 0;
439 	mr->page_list = kvcalloc(mr->pbl_mtr.hem_cfg.buf_pg_count,
440 				 sizeof(dma_addr_t), GFP_KERNEL);
441 	if (!mr->page_list)
442 		return ret;
443 
444 	ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, hns_roce_set_page);
445 	if (ret < 1) {
446 		ibdev_err(ibdev, "failed to store sg pages %u %u, cnt = %d.\n",
447 			  mr->npages, mr->pbl_mtr.hem_cfg.buf_pg_count, ret);
448 		goto err_page_list;
449 	}
450 
451 	mtr->hem_cfg.region[0].offset = 0;
452 	mtr->hem_cfg.region[0].count = mr->npages;
453 	mtr->hem_cfg.region[0].hopnum = mr->pbl_hop_num;
454 	mtr->hem_cfg.region_count = 1;
455 	ret = hns_roce_mtr_map(hr_dev, mtr, mr->page_list, mr->npages);
456 	if (ret) {
457 		ibdev_err(ibdev, "failed to map sg mtr, ret = %d.\n", ret);
458 		ret = 0;
459 	} else {
460 		mr->pbl_mtr.hem_cfg.buf_pg_shift = (u32)ilog2(ibmr->page_size);
461 		ret = mr->npages;
462 	}
463 
464 err_page_list:
465 	kvfree(mr->page_list);
466 	mr->page_list = NULL;
467 
468 	return ret;
469 }
470 
471 static void hns_roce_mw_free(struct hns_roce_dev *hr_dev,
472 			     struct hns_roce_mw *mw)
473 {
474 	struct device *dev = hr_dev->dev;
475 	int ret;
476 
477 	if (mw->enabled) {
478 		ret = hns_roce_destroy_hw_ctx(hr_dev, HNS_ROCE_CMD_DESTROY_MPT,
479 					      key_to_hw_index(mw->rkey) &
480 					      (hr_dev->caps.num_mtpts - 1));
481 		if (ret)
482 			dev_warn(dev, "MW DESTROY_MPT failed (%d)\n", ret);
483 
484 		hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table,
485 				   key_to_hw_index(mw->rkey));
486 	}
487 
488 	ida_free(&hr_dev->mr_table.mtpt_ida.ida,
489 		 (int)key_to_hw_index(mw->rkey));
490 }
491 
492 static int hns_roce_mw_enable(struct hns_roce_dev *hr_dev,
493 			      struct hns_roce_mw *mw)
494 {
495 	struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
496 	struct hns_roce_cmd_mailbox *mailbox;
497 	struct device *dev = hr_dev->dev;
498 	unsigned long mtpt_idx = key_to_hw_index(mw->rkey);
499 	int ret;
500 
501 	/* prepare HEM entry memory */
502 	ret = hns_roce_table_get(hr_dev, &mr_table->mtpt_table, mtpt_idx);
503 	if (ret)
504 		return ret;
505 
506 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
507 	if (IS_ERR(mailbox)) {
508 		ret = PTR_ERR(mailbox);
509 		goto err_table;
510 	}
511 
512 	ret = hr_dev->hw->mw_write_mtpt(mailbox->buf, mw);
513 	if (ret) {
514 		dev_err(dev, "MW write mtpt fail!\n");
515 		goto err_page;
516 	}
517 
518 	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_MPT,
519 				     mtpt_idx & (hr_dev->caps.num_mtpts - 1));
520 	if (ret) {
521 		dev_err(dev, "MW CREATE_MPT failed (%d)\n", ret);
522 		goto err_page;
523 	}
524 
525 	mw->enabled = 1;
526 
527 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
528 
529 	return 0;
530 
531 err_page:
532 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
533 
534 err_table:
535 	hns_roce_table_put(hr_dev, &mr_table->mtpt_table, mtpt_idx);
536 
537 	return ret;
538 }
539 
540 int hns_roce_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
541 {
542 	struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
543 	struct hns_roce_ida *mtpt_ida = &hr_dev->mr_table.mtpt_ida;
544 	struct ib_device *ibdev = &hr_dev->ib_dev;
545 	struct hns_roce_mw *mw = to_hr_mw(ibmw);
546 	int ret;
547 	int id;
548 
549 	/* Allocate a key for mw from mr_table */
550 	id = ida_alloc_range(&mtpt_ida->ida, mtpt_ida->min, mtpt_ida->max,
551 			     GFP_KERNEL);
552 	if (id < 0) {
553 		ibdev_err(ibdev, "failed to alloc id for MW key, id(%d)\n", id);
554 		return -ENOMEM;
555 	}
556 
557 	mw->rkey = hw_index_to_key(id);
558 
559 	ibmw->rkey = mw->rkey;
560 	mw->pdn = to_hr_pd(ibmw->pd)->pdn;
561 	mw->pbl_hop_num = hr_dev->caps.pbl_hop_num;
562 	mw->pbl_ba_pg_sz = hr_dev->caps.pbl_ba_pg_sz;
563 	mw->pbl_buf_pg_sz = hr_dev->caps.pbl_buf_pg_sz;
564 
565 	ret = hns_roce_mw_enable(hr_dev, mw);
566 	if (ret)
567 		goto err_mw;
568 
569 	return 0;
570 
571 err_mw:
572 	hns_roce_mw_free(hr_dev, mw);
573 	return ret;
574 }
575 
576 int hns_roce_dealloc_mw(struct ib_mw *ibmw)
577 {
578 	struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
579 	struct hns_roce_mw *mw = to_hr_mw(ibmw);
580 
581 	hns_roce_mw_free(hr_dev, mw);
582 	return 0;
583 }
584 
585 static int mtr_map_region(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
586 			  struct hns_roce_buf_region *region, dma_addr_t *pages,
587 			  int max_count)
588 {
589 	int count, npage;
590 	int offset, end;
591 	__le64 *mtts;
592 	u64 addr;
593 	int i;
594 
595 	offset = region->offset;
596 	end = offset + region->count;
597 	npage = 0;
598 	while (offset < end && npage < max_count) {
599 		count = 0;
600 		mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
601 						  offset, &count);
602 		if (!mtts)
603 			return -ENOBUFS;
604 
605 		for (i = 0; i < count && npage < max_count; i++) {
606 			addr = pages[npage];
607 
608 			mtts[i] = cpu_to_le64(addr);
609 			npage++;
610 		}
611 		offset += count;
612 	}
613 
614 	return npage;
615 }
616 
617 static inline bool mtr_has_mtt(struct hns_roce_buf_attr *attr)
618 {
619 	int i;
620 
621 	for (i = 0; i < attr->region_count; i++)
622 		if (attr->region[i].hopnum != HNS_ROCE_HOP_NUM_0 &&
623 		    attr->region[i].hopnum > 0)
624 			return true;
625 
626 	/* because the mtr only one root base address, when hopnum is 0 means
627 	 * root base address equals the first buffer address, thus all alloced
628 	 * memory must in a continuous space accessed by direct mode.
629 	 */
630 	return false;
631 }
632 
633 static inline size_t mtr_bufs_size(struct hns_roce_buf_attr *attr)
634 {
635 	size_t size = 0;
636 	int i;
637 
638 	for (i = 0; i < attr->region_count; i++)
639 		size += attr->region[i].size;
640 
641 	return size;
642 }
643 
644 /*
645  * check the given pages in continuous address space
646  * Returns 0 on success, or the error page num.
647  */
648 static inline int mtr_check_direct_pages(dma_addr_t *pages, int page_count,
649 					 unsigned int page_shift)
650 {
651 	size_t page_size = 1 << page_shift;
652 	int i;
653 
654 	for (i = 1; i < page_count; i++)
655 		if (pages[i] - pages[i - 1] != page_size)
656 			return i;
657 
658 	return 0;
659 }
660 
661 static void mtr_free_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
662 {
663 	/* release user buffers */
664 	if (mtr->umem) {
665 		ib_umem_release(mtr->umem);
666 		mtr->umem = NULL;
667 	}
668 
669 	/* release kernel buffers */
670 	if (mtr->kmem) {
671 		hns_roce_buf_free(hr_dev, mtr->kmem);
672 		mtr->kmem = NULL;
673 	}
674 }
675 
676 static int mtr_alloc_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
677 			  struct hns_roce_buf_attr *buf_attr,
678 			  struct ib_udata *udata, unsigned long user_addr)
679 {
680 	struct ib_device *ibdev = &hr_dev->ib_dev;
681 	size_t total_size;
682 
683 	total_size = mtr_bufs_size(buf_attr);
684 
685 	if (udata) {
686 		mtr->kmem = NULL;
687 		mtr->umem = ib_umem_get(ibdev, user_addr, total_size,
688 					buf_attr->user_access);
689 		if (IS_ERR(mtr->umem)) {
690 			ibdev_err(ibdev, "failed to get umem, ret = %ld.\n",
691 				  PTR_ERR(mtr->umem));
692 			return -ENOMEM;
693 		}
694 	} else {
695 		mtr->umem = NULL;
696 		mtr->kmem = hns_roce_buf_alloc(hr_dev, total_size,
697 					       buf_attr->page_shift,
698 					       mtr->hem_cfg.is_direct ?
699 					       HNS_ROCE_BUF_DIRECT : 0);
700 		if (IS_ERR(mtr->kmem)) {
701 			ibdev_err(ibdev, "failed to alloc kmem, ret = %ld.\n",
702 				  PTR_ERR(mtr->kmem));
703 			return PTR_ERR(mtr->kmem);
704 		}
705 	}
706 
707 	return 0;
708 }
709 
710 static int mtr_map_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
711 			int page_count, unsigned int page_shift)
712 {
713 	struct ib_device *ibdev = &hr_dev->ib_dev;
714 	dma_addr_t *pages;
715 	int npage;
716 	int ret;
717 
718 	/* alloc a tmp array to store buffer's dma address */
719 	pages = kvcalloc(page_count, sizeof(dma_addr_t), GFP_KERNEL);
720 	if (!pages)
721 		return -ENOMEM;
722 
723 	if (mtr->umem)
724 		npage = hns_roce_get_umem_bufs(hr_dev, pages, page_count,
725 					       mtr->umem, page_shift);
726 	else
727 		npage = hns_roce_get_kmem_bufs(hr_dev, pages, page_count,
728 					       mtr->kmem, page_shift);
729 
730 	if (npage != page_count) {
731 		ibdev_err(ibdev, "failed to get mtr page %d != %d.\n", npage,
732 			  page_count);
733 		ret = -ENOBUFS;
734 		goto err_alloc_list;
735 	}
736 
737 	if (mtr->hem_cfg.is_direct && npage > 1) {
738 		ret = mtr_check_direct_pages(pages, npage, page_shift);
739 		if (ret) {
740 			ibdev_err(ibdev, "failed to check %s page: %d / %d.\n",
741 				  mtr->umem ? "umtr" : "kmtr", ret, npage);
742 			ret = -ENOBUFS;
743 			goto err_alloc_list;
744 		}
745 	}
746 
747 	ret = hns_roce_mtr_map(hr_dev, mtr, pages, page_count);
748 	if (ret)
749 		ibdev_err(ibdev, "failed to map mtr pages, ret = %d.\n", ret);
750 
751 err_alloc_list:
752 	kvfree(pages);
753 
754 	return ret;
755 }
756 
757 int hns_roce_mtr_map(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
758 		     dma_addr_t *pages, unsigned int page_cnt)
759 {
760 	struct ib_device *ibdev = &hr_dev->ib_dev;
761 	struct hns_roce_buf_region *r;
762 	unsigned int i, mapped_cnt;
763 	int ret = 0;
764 
765 	/*
766 	 * Only use the first page address as root ba when hopnum is 0, this
767 	 * is because the addresses of all pages are consecutive in this case.
768 	 */
769 	if (mtr->hem_cfg.is_direct) {
770 		mtr->hem_cfg.root_ba = pages[0];
771 		return 0;
772 	}
773 
774 	for (i = 0, mapped_cnt = 0; i < mtr->hem_cfg.region_count &&
775 	     mapped_cnt < page_cnt; i++) {
776 		r = &mtr->hem_cfg.region[i];
777 		/* if hopnum is 0, no need to map pages in this region */
778 		if (!r->hopnum) {
779 			mapped_cnt += r->count;
780 			continue;
781 		}
782 
783 		if (r->offset + r->count > page_cnt) {
784 			ret = -EINVAL;
785 			ibdev_err(ibdev,
786 				  "failed to check mtr%u count %u + %u > %u.\n",
787 				  i, r->offset, r->count, page_cnt);
788 			return ret;
789 		}
790 
791 		ret = mtr_map_region(hr_dev, mtr, r, &pages[r->offset],
792 				     page_cnt - mapped_cnt);
793 		if (ret < 0) {
794 			ibdev_err(ibdev,
795 				  "failed to map mtr%u offset %u, ret = %d.\n",
796 				  i, r->offset, ret);
797 			return ret;
798 		}
799 		mapped_cnt += ret;
800 		ret = 0;
801 	}
802 
803 	if (mapped_cnt < page_cnt) {
804 		ret = -ENOBUFS;
805 		ibdev_err(ibdev, "failed to map mtr pages count: %u < %u.\n",
806 			  mapped_cnt, page_cnt);
807 	}
808 
809 	return ret;
810 }
811 
812 int hns_roce_mtr_find(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
813 		      u32 offset, u64 *mtt_buf, int mtt_max, u64 *base_addr)
814 {
815 	struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
816 	int mtt_count, left;
817 	u32 start_index;
818 	int total = 0;
819 	__le64 *mtts;
820 	u32 npage;
821 	u64 addr;
822 
823 	if (!mtt_buf || mtt_max < 1)
824 		goto done;
825 
826 	/* no mtt memory in direct mode, so just return the buffer address */
827 	if (cfg->is_direct) {
828 		start_index = offset >> HNS_HW_PAGE_SHIFT;
829 		for (mtt_count = 0; mtt_count < cfg->region_count &&
830 		     total < mtt_max; mtt_count++) {
831 			npage = cfg->region[mtt_count].offset;
832 			if (npage < start_index)
833 				continue;
834 
835 			addr = cfg->root_ba + (npage << HNS_HW_PAGE_SHIFT);
836 			mtt_buf[total] = addr;
837 
838 			total++;
839 		}
840 
841 		goto done;
842 	}
843 
844 	start_index = offset >> cfg->buf_pg_shift;
845 	left = mtt_max;
846 	while (left > 0) {
847 		mtt_count = 0;
848 		mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
849 						  start_index + total,
850 						  &mtt_count);
851 		if (!mtts || !mtt_count)
852 			goto done;
853 
854 		npage = min(mtt_count, left);
855 		left -= npage;
856 		for (mtt_count = 0; mtt_count < npage; mtt_count++)
857 			mtt_buf[total++] = le64_to_cpu(mtts[mtt_count]);
858 	}
859 
860 done:
861 	if (base_addr)
862 		*base_addr = cfg->root_ba;
863 
864 	return total;
865 }
866 
867 static int mtr_init_buf_cfg(struct hns_roce_dev *hr_dev,
868 			    struct hns_roce_buf_attr *attr,
869 			    struct hns_roce_hem_cfg *cfg,
870 			    unsigned int *buf_page_shift, u64 unalinged_size)
871 {
872 	struct hns_roce_buf_region *r;
873 	u64 first_region_padding;
874 	int page_cnt, region_cnt;
875 	unsigned int page_shift;
876 	size_t buf_size;
877 
878 	/* If mtt is disabled, all pages must be within a continuous range */
879 	cfg->is_direct = !mtr_has_mtt(attr);
880 	buf_size = mtr_bufs_size(attr);
881 	if (cfg->is_direct) {
882 		/* When HEM buffer uses 0-level addressing, the page size is
883 		 * equal to the whole buffer size, and we split the buffer into
884 		 * small pages which is used to check whether the adjacent
885 		 * units are in the continuous space and its size is fixed to
886 		 * 4K based on hns ROCEE's requirement.
887 		 */
888 		page_shift = HNS_HW_PAGE_SHIFT;
889 
890 		/* The ROCEE requires the page size to be 4K * 2 ^ N. */
891 		cfg->buf_pg_count = 1;
892 		cfg->buf_pg_shift = HNS_HW_PAGE_SHIFT +
893 			order_base_2(DIV_ROUND_UP(buf_size, HNS_HW_PAGE_SIZE));
894 		first_region_padding = 0;
895 	} else {
896 		page_shift = attr->page_shift;
897 		cfg->buf_pg_count = DIV_ROUND_UP(buf_size + unalinged_size,
898 						 1 << page_shift);
899 		cfg->buf_pg_shift = page_shift;
900 		first_region_padding = unalinged_size;
901 	}
902 
903 	/* Convert buffer size to page index and page count for each region and
904 	 * the buffer's offset needs to be appended to the first region.
905 	 */
906 	for (page_cnt = 0, region_cnt = 0; region_cnt < attr->region_count &&
907 	     region_cnt < ARRAY_SIZE(cfg->region); region_cnt++) {
908 		r = &cfg->region[region_cnt];
909 		r->offset = page_cnt;
910 		buf_size = hr_hw_page_align(attr->region[region_cnt].size +
911 					    first_region_padding);
912 		r->count = DIV_ROUND_UP(buf_size, 1 << page_shift);
913 		first_region_padding = 0;
914 		page_cnt += r->count;
915 		r->hopnum = to_hr_hem_hopnum(attr->region[region_cnt].hopnum,
916 					     r->count);
917 	}
918 
919 	cfg->region_count = region_cnt;
920 	*buf_page_shift = page_shift;
921 
922 	return page_cnt;
923 }
924 
925 static u64 cal_pages_per_l1ba(unsigned int ba_per_bt, unsigned int hopnum)
926 {
927 	return int_pow(ba_per_bt, hopnum - 1);
928 }
929 
930 static unsigned int cal_best_bt_pg_sz(struct hns_roce_dev *hr_dev,
931 				      struct hns_roce_mtr *mtr,
932 				      unsigned int pg_shift)
933 {
934 	unsigned long cap = hr_dev->caps.page_size_cap;
935 	struct hns_roce_buf_region *re;
936 	unsigned int pgs_per_l1ba;
937 	unsigned int ba_per_bt;
938 	unsigned int ba_num;
939 	int i;
940 
941 	for_each_set_bit_from(pg_shift, &cap, sizeof(cap) * BITS_PER_BYTE) {
942 		if (!(BIT(pg_shift) & cap))
943 			continue;
944 
945 		ba_per_bt = BIT(pg_shift) / BA_BYTE_LEN;
946 		ba_num = 0;
947 		for (i = 0; i < mtr->hem_cfg.region_count; i++) {
948 			re = &mtr->hem_cfg.region[i];
949 			if (re->hopnum == 0)
950 				continue;
951 
952 			pgs_per_l1ba = cal_pages_per_l1ba(ba_per_bt, re->hopnum);
953 			ba_num += DIV_ROUND_UP(re->count, pgs_per_l1ba);
954 		}
955 
956 		if (ba_num <= ba_per_bt)
957 			return pg_shift;
958 	}
959 
960 	return 0;
961 }
962 
963 static int mtr_alloc_mtt(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
964 			 unsigned int ba_page_shift)
965 {
966 	struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
967 	int ret;
968 
969 	hns_roce_hem_list_init(&mtr->hem_list);
970 	if (!cfg->is_direct) {
971 		ba_page_shift = cal_best_bt_pg_sz(hr_dev, mtr, ba_page_shift);
972 		if (!ba_page_shift)
973 			return -ERANGE;
974 
975 		ret = hns_roce_hem_list_request(hr_dev, &mtr->hem_list,
976 						cfg->region, cfg->region_count,
977 						ba_page_shift);
978 		if (ret)
979 			return ret;
980 		cfg->root_ba = mtr->hem_list.root_ba;
981 		cfg->ba_pg_shift = ba_page_shift;
982 	} else {
983 		cfg->ba_pg_shift = cfg->buf_pg_shift;
984 	}
985 
986 	return 0;
987 }
988 
989 static void mtr_free_mtt(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
990 {
991 	hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
992 }
993 
994 /**
995  * hns_roce_mtr_create - Create hns memory translate region.
996  *
997  * @hr_dev: RoCE device struct pointer
998  * @mtr: memory translate region
999  * @buf_attr: buffer attribute for creating mtr
1000  * @ba_page_shift: page shift for multi-hop base address table
1001  * @udata: user space context, if it's NULL, means kernel space
1002  * @user_addr: userspace virtual address to start at
1003  */
1004 int hns_roce_mtr_create(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
1005 			struct hns_roce_buf_attr *buf_attr,
1006 			unsigned int ba_page_shift, struct ib_udata *udata,
1007 			unsigned long user_addr)
1008 {
1009 	struct ib_device *ibdev = &hr_dev->ib_dev;
1010 	unsigned int buf_page_shift = 0;
1011 	int buf_page_cnt;
1012 	int ret;
1013 
1014 	buf_page_cnt = mtr_init_buf_cfg(hr_dev, buf_attr, &mtr->hem_cfg,
1015 					&buf_page_shift,
1016 					udata ? user_addr & ~PAGE_MASK : 0);
1017 	if (buf_page_cnt < 1 || buf_page_shift < HNS_HW_PAGE_SHIFT) {
1018 		ibdev_err(ibdev, "failed to init mtr cfg, count %d shift %u.\n",
1019 			  buf_page_cnt, buf_page_shift);
1020 		return -EINVAL;
1021 	}
1022 
1023 	ret = mtr_alloc_mtt(hr_dev, mtr, ba_page_shift);
1024 	if (ret) {
1025 		ibdev_err(ibdev, "failed to alloc mtr mtt, ret = %d.\n", ret);
1026 		return ret;
1027 	}
1028 
1029 	/* The caller has its own buffer list and invokes the hns_roce_mtr_map()
1030 	 * to finish the MTT configuration.
1031 	 */
1032 	if (buf_attr->mtt_only) {
1033 		mtr->umem = NULL;
1034 		mtr->kmem = NULL;
1035 		return 0;
1036 	}
1037 
1038 	ret = mtr_alloc_bufs(hr_dev, mtr, buf_attr, udata, user_addr);
1039 	if (ret) {
1040 		ibdev_err(ibdev, "failed to alloc mtr bufs, ret = %d.\n", ret);
1041 		goto err_alloc_mtt;
1042 	}
1043 
1044 	/* Write buffer's dma address to MTT */
1045 	ret = mtr_map_bufs(hr_dev, mtr, buf_page_cnt, buf_page_shift);
1046 	if (ret)
1047 		ibdev_err(ibdev, "failed to map mtr bufs, ret = %d.\n", ret);
1048 	else
1049 		return 0;
1050 
1051 	mtr_free_bufs(hr_dev, mtr);
1052 err_alloc_mtt:
1053 	mtr_free_mtt(hr_dev, mtr);
1054 	return ret;
1055 }
1056 
1057 void hns_roce_mtr_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
1058 {
1059 	/* release multi-hop addressing resource */
1060 	hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
1061 
1062 	/* free buffers */
1063 	mtr_free_bufs(hr_dev, mtr);
1064 }
1065