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