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