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