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(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, struct ib_udata *udata)
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 	for (i = 0; i < mtr->hem_cfg.region_count; i++) {
875 		r = &mtr->hem_cfg.region[i];
876 		if (r->offset + r->count > page_cnt) {
877 			err = -EINVAL;
878 			ibdev_err(ibdev,
879 				  "Failed to check mtr%d end %d + %d, max %d\n",
880 				  i, r->offset, r->count, page_cnt);
881 			return err;
882 		}
883 
884 		err = mtr_map_region(hr_dev, mtr, &pages[r->offset], r);
885 		if (err) {
886 			ibdev_err(ibdev,
887 				  "Failed to map mtr%d offset %d, err %d\n",
888 				  i, r->offset, err);
889 			return err;
890 		}
891 	}
892 
893 	return 0;
894 }
895 
896 int hns_roce_mtr_find(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
897 		      int offset, u64 *mtt_buf, int mtt_max, u64 *base_addr)
898 {
899 	int mtt_count;
900 	int total = 0;
901 	__le64 *mtts;
902 	int npage;
903 	u64 addr;
904 	int left;
905 
906 	if (!mtt_buf || mtt_max < 1)
907 		goto done;
908 
909 	/* no mtt memory in direct mode, so just return the buffer address */
910 	if (mtr->hem_cfg.is_direct) {
911 		npage = offset;
912 		for (total = 0; total < mtt_max; total++, npage++) {
913 			addr = mtr->hem_cfg.root_ba +
914 			       (npage << mtr->hem_cfg.buf_pg_shift);
915 
916 			if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
917 				mtt_buf[total] = to_hr_hw_page_addr(addr);
918 			else
919 				mtt_buf[total] = addr;
920 		}
921 
922 		goto done;
923 	}
924 
925 	left = mtt_max;
926 	while (left > 0) {
927 		mtt_count = 0;
928 		mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
929 						  offset + total,
930 						  &mtt_count, NULL);
931 		if (!mtts || !mtt_count)
932 			goto done;
933 
934 		npage = min(mtt_count, left);
935 		left -= npage;
936 		for (mtt_count = 0; mtt_count < npage; mtt_count++)
937 			mtt_buf[total++] = le64_to_cpu(mtts[mtt_count]);
938 	}
939 
940 done:
941 	if (base_addr)
942 		*base_addr = mtr->hem_cfg.root_ba;
943 
944 	return total;
945 }
946 
947 /* convert buffer size to page index and page count */
948 static unsigned int mtr_init_region(struct hns_roce_buf_attr *attr,
949 				    int page_cnt,
950 				    struct hns_roce_buf_region *regions,
951 				    int region_cnt, unsigned int page_shift)
952 {
953 	unsigned int page_size = 1 << page_shift;
954 	int max_region = attr->region_count;
955 	struct hns_roce_buf_region *r;
956 	unsigned int i = 0;
957 	int page_idx = 0;
958 
959 	for (; i < region_cnt && i < max_region && page_idx < page_cnt; i++) {
960 		r = &regions[i];
961 		r->hopnum = attr->region[i].hopnum == HNS_ROCE_HOP_NUM_0 ?
962 			    0 : attr->region[i].hopnum;
963 		r->offset = page_idx;
964 		r->count = DIV_ROUND_UP(attr->region[i].size, page_size);
965 		page_idx += r->count;
966 	}
967 
968 	return i;
969 }
970 
971 /**
972  * hns_roce_mtr_create - Create hns memory translate region.
973  *
974  * @mtr: memory translate region
975  * @init_attr: init attribute for creating mtr
976  * @page_shift: page shift for multi-hop base address table
977  * @udata: user space context, if it's NULL, means kernel space
978  * @user_addr: userspace virtual address to start at
979  * @buf_alloced: mtr has private buffer, true means need to alloc
980  */
981 int hns_roce_mtr_create(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
982 			struct hns_roce_buf_attr *buf_attr,
983 			unsigned int page_shift, struct ib_udata *udata,
984 			unsigned long user_addr)
985 {
986 	struct ib_device *ibdev = &hr_dev->ib_dev;
987 	dma_addr_t *pages = NULL;
988 	int region_cnt = 0;
989 	int all_pg_cnt;
990 	int get_pg_cnt;
991 	bool has_mtt;
992 	int err = 0;
993 
994 	has_mtt = mtr_has_mtt(buf_attr);
995 	/* if buffer only need mtt, just init the hem cfg */
996 	if (buf_attr->mtt_only) {
997 		mtr->hem_cfg.buf_pg_shift = buf_attr->page_shift;
998 		mtr->hem_cfg.buf_pg_count = mtr_bufs_size(buf_attr) >>
999 					    buf_attr->page_shift;
1000 		mtr->umem = NULL;
1001 		mtr->kmem = NULL;
1002 	} else {
1003 		err = mtr_alloc_bufs(hr_dev, mtr, buf_attr, !has_mtt, udata,
1004 				     user_addr);
1005 		if (err) {
1006 			ibdev_err(ibdev, "Failed to alloc mtr bufs, err %d\n",
1007 				  err);
1008 			return err;
1009 		}
1010 	}
1011 
1012 	/* alloc mtt memory */
1013 	all_pg_cnt = mtr->hem_cfg.buf_pg_count;
1014 	hns_roce_hem_list_init(&mtr->hem_list);
1015 	mtr->hem_cfg.is_direct = !has_mtt;
1016 	mtr->hem_cfg.ba_pg_shift = page_shift;
1017 	mtr->hem_cfg.region_count = 0;
1018 	region_cnt = mtr_init_region(buf_attr, all_pg_cnt,
1019 				     mtr->hem_cfg.region,
1020 				     ARRAY_SIZE(mtr->hem_cfg.region),
1021 				     mtr->hem_cfg.buf_pg_shift);
1022 	if (region_cnt < 1) {
1023 		err = -ENOBUFS;
1024 		ibdev_err(ibdev, "failed to init mtr region %d\n", region_cnt);
1025 		goto err_alloc_bufs;
1026 	}
1027 
1028 	mtr->hem_cfg.region_count = region_cnt;
1029 
1030 	if (has_mtt) {
1031 		err = hns_roce_hem_list_request(hr_dev, &mtr->hem_list,
1032 						mtr->hem_cfg.region, region_cnt,
1033 						page_shift);
1034 		if (err) {
1035 			ibdev_err(ibdev, "Failed to request mtr hem, err %d\n",
1036 				  err);
1037 			goto err_alloc_bufs;
1038 		}
1039 		mtr->hem_cfg.root_ba = mtr->hem_list.root_ba;
1040 	}
1041 
1042 	/* no buffer to map */
1043 	if (buf_attr->mtt_only)
1044 		return 0;
1045 
1046 	/* alloc a tmp array to store buffer's dma address */
1047 	pages = kvcalloc(all_pg_cnt, sizeof(dma_addr_t), GFP_KERNEL);
1048 	if (!pages) {
1049 		err = -ENOMEM;
1050 		ibdev_err(ibdev, "Failed to alloc mtr page list %d\n",
1051 			  all_pg_cnt);
1052 		goto err_alloc_hem_list;
1053 	}
1054 
1055 	get_pg_cnt = mtr_get_pages(hr_dev, mtr, pages, all_pg_cnt,
1056 				   mtr->hem_cfg.buf_pg_shift);
1057 	if (get_pg_cnt != all_pg_cnt) {
1058 		ibdev_err(ibdev, "Failed to get mtr page %d != %d\n",
1059 			  get_pg_cnt, all_pg_cnt);
1060 		err = -ENOBUFS;
1061 		goto err_alloc_page_list;
1062 	}
1063 
1064 	if (!has_mtt) {
1065 		mtr->hem_cfg.root_ba = pages[0];
1066 	} else {
1067 		/* write buffer's dma address to BA table */
1068 		err = hns_roce_mtr_map(hr_dev, mtr, pages, all_pg_cnt);
1069 		if (err) {
1070 			ibdev_err(ibdev, "Failed to map mtr pages, err %d\n",
1071 				  err);
1072 			goto err_alloc_page_list;
1073 		}
1074 	}
1075 
1076 	/* drop tmp array */
1077 	kvfree(pages);
1078 	return 0;
1079 err_alloc_page_list:
1080 	kvfree(pages);
1081 err_alloc_hem_list:
1082 	hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
1083 err_alloc_bufs:
1084 	mtr_free_bufs(hr_dev, mtr);
1085 	return err;
1086 }
1087 
1088 void hns_roce_mtr_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
1089 {
1090 	/* release multi-hop addressing resource */
1091 	hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
1092 
1093 	/* free buffers */
1094 	mtr_free_bufs(hr_dev, mtr);
1095 }
1096