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