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