2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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"
41 static u32 hw_index_to_key(int ind)
43 return ((u32)ind >> 24) | ((u32)ind << 8);
46 unsigned long key_to_hw_index(u32 key)
48 return (key << 24) | (key >> 8);
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)
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);
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)
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);
69 static int alloc_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
71 struct hns_roce_ida *mtpt_ida = &hr_dev->mr_table.mtpt_ida;
72 struct ib_device *ibdev = &hr_dev->ib_dev;
76 /* Allocate a key for mr from mr_table */
77 id = ida_alloc_range(&mtpt_ida->ida, mtpt_ida->min, mtpt_ida->max,
80 ibdev_err(ibdev, "failed to alloc id for MR key, id(%d)\n", id);
84 mr->key = hw_index_to_key(id); /* MR key */
86 err = hns_roce_table_get(hr_dev, &hr_dev->mr_table.mtpt_table,
89 ibdev_err(ibdev, "failed to alloc mtpt, ret = %d.\n", err);
95 ida_free(&mtpt_ida->ida, id);
99 static void free_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
101 unsigned long obj = key_to_hw_index(mr->key);
103 hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table, obj);
104 ida_free(&hr_dev->mr_table.mtpt_ida.ida, (int)obj);
107 static int alloc_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr,
108 struct ib_udata *udata, u64 start)
110 struct ib_device *ibdev = &hr_dev->ib_dev;
111 bool is_fast = mr->type == MR_TYPE_FRMR;
112 struct hns_roce_buf_attr buf_attr = {};
115 mr->pbl_hop_num = is_fast ? 1 : hr_dev->caps.pbl_hop_num;
116 buf_attr.page_shift = is_fast ? PAGE_SHIFT :
117 hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT;
118 buf_attr.region[0].size = mr->size;
119 buf_attr.region[0].hopnum = mr->pbl_hop_num;
120 buf_attr.region_count = 1;
121 buf_attr.user_access = mr->access;
122 /* fast MR's buffer is alloced before mapping, not at creation */
123 buf_attr.mtt_only = is_fast;
125 err = hns_roce_mtr_create(hr_dev, &mr->pbl_mtr, &buf_attr,
126 hr_dev->caps.pbl_ba_pg_sz + PAGE_SHIFT,
129 ibdev_err(ibdev, "failed to alloc pbl mtr, ret = %d.\n", err);
131 mr->npages = mr->pbl_mtr.hem_cfg.buf_pg_count;
136 static void free_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
138 hns_roce_mtr_destroy(hr_dev, &mr->pbl_mtr);
141 static void hns_roce_mr_free(struct hns_roce_dev *hr_dev,
142 struct hns_roce_mr *mr)
144 struct ib_device *ibdev = &hr_dev->ib_dev;
148 ret = hns_roce_hw_destroy_mpt(hr_dev, NULL,
149 key_to_hw_index(mr->key) &
150 (hr_dev->caps.num_mtpts - 1));
152 ibdev_warn(ibdev, "failed to destroy mpt, ret = %d.\n",
156 free_mr_pbl(hr_dev, mr);
157 free_mr_key(hr_dev, mr);
160 static int hns_roce_mr_enable(struct hns_roce_dev *hr_dev,
161 struct hns_roce_mr *mr)
163 unsigned long mtpt_idx = key_to_hw_index(mr->key);
164 struct hns_roce_cmd_mailbox *mailbox;
165 struct device *dev = hr_dev->dev;
168 /* Allocate mailbox memory */
169 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
170 if (IS_ERR(mailbox)) {
171 ret = PTR_ERR(mailbox);
175 if (mr->type != MR_TYPE_FRMR)
176 ret = hr_dev->hw->write_mtpt(hr_dev, mailbox->buf, mr,
179 ret = hr_dev->hw->frmr_write_mtpt(hr_dev, mailbox->buf, mr);
181 dev_err(dev, "failed to write mtpt, ret = %d.\n", ret);
185 ret = hns_roce_hw_create_mpt(hr_dev, mailbox,
186 mtpt_idx & (hr_dev->caps.num_mtpts - 1));
188 dev_err(dev, "failed to create mpt, ret = %d.\n", ret);
195 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
200 void hns_roce_init_mr_table(struct hns_roce_dev *hr_dev)
202 struct hns_roce_ida *mtpt_ida = &hr_dev->mr_table.mtpt_ida;
204 ida_init(&mtpt_ida->ida);
205 mtpt_ida->max = hr_dev->caps.num_mtpts - 1;
206 mtpt_ida->min = hr_dev->caps.reserved_mrws;
209 struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc)
211 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
212 struct hns_roce_mr *mr;
215 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
217 return ERR_PTR(-ENOMEM);
219 mr->type = MR_TYPE_DMA;
220 mr->pd = to_hr_pd(pd)->pdn;
223 /* Allocate memory region key */
224 hns_roce_hem_list_init(&mr->pbl_mtr.hem_list);
225 ret = alloc_mr_key(hr_dev, mr);
229 ret = hns_roce_mr_enable(hr_dev, mr);
233 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
237 free_mr_key(hr_dev, mr);
244 struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
245 u64 virt_addr, int access_flags,
246 struct ib_udata *udata)
248 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
249 struct hns_roce_mr *mr;
252 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
254 return ERR_PTR(-ENOMEM);
256 mr->iova = virt_addr;
258 mr->pd = to_hr_pd(pd)->pdn;
259 mr->access = access_flags;
260 mr->type = MR_TYPE_MR;
262 ret = alloc_mr_key(hr_dev, mr);
266 ret = alloc_mr_pbl(hr_dev, mr, udata, start);
270 ret = hns_roce_mr_enable(hr_dev, mr);
274 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
275 mr->ibmr.length = length;
280 free_mr_pbl(hr_dev, mr);
282 free_mr_key(hr_dev, mr);
288 struct ib_mr *hns_roce_rereg_user_mr(struct ib_mr *ibmr, int flags, u64 start,
289 u64 length, u64 virt_addr,
290 int mr_access_flags, struct ib_pd *pd,
291 struct ib_udata *udata)
293 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
294 struct ib_device *ib_dev = &hr_dev->ib_dev;
295 struct hns_roce_mr *mr = to_hr_mr(ibmr);
296 struct hns_roce_cmd_mailbox *mailbox;
297 unsigned long mtpt_idx;
301 return ERR_PTR(-EINVAL);
303 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
305 return ERR_CAST(mailbox);
307 mtpt_idx = key_to_hw_index(mr->key) & (hr_dev->caps.num_mtpts - 1);
308 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, mtpt_idx, 0,
309 HNS_ROCE_CMD_QUERY_MPT,
310 HNS_ROCE_CMD_TIMEOUT_MSECS);
314 ret = hns_roce_hw_destroy_mpt(hr_dev, NULL, mtpt_idx);
316 ibdev_warn(ib_dev, "failed to destroy MPT, ret = %d.\n", ret);
319 mr->iova = virt_addr;
322 if (flags & IB_MR_REREG_PD)
323 mr->pd = to_hr_pd(pd)->pdn;
325 if (flags & IB_MR_REREG_ACCESS)
326 mr->access = mr_access_flags;
328 if (flags & IB_MR_REREG_TRANS) {
329 free_mr_pbl(hr_dev, mr);
330 ret = alloc_mr_pbl(hr_dev, mr, udata, start);
332 ibdev_err(ib_dev, "failed to alloc mr PBL, ret = %d.\n",
338 ret = hr_dev->hw->rereg_write_mtpt(hr_dev, mr, flags, mailbox->buf);
340 ibdev_err(ib_dev, "failed to write mtpt, ret = %d.\n", ret);
344 ret = hns_roce_hw_create_mpt(hr_dev, mailbox, mtpt_idx);
346 ibdev_err(ib_dev, "failed to create MPT, ret = %d.\n", ret);
353 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
358 int hns_roce_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
360 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
361 struct hns_roce_mr *mr = to_hr_mr(ibmr);
364 if (hr_dev->hw->dereg_mr) {
365 ret = hr_dev->hw->dereg_mr(hr_dev, mr, udata);
367 hns_roce_mr_free(hr_dev, mr);
374 struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
377 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
378 struct device *dev = hr_dev->dev;
379 struct hns_roce_mr *mr;
382 if (mr_type != IB_MR_TYPE_MEM_REG)
383 return ERR_PTR(-EINVAL);
385 if (max_num_sg > HNS_ROCE_FRMR_MAX_PA) {
386 dev_err(dev, "max_num_sg larger than %d\n",
387 HNS_ROCE_FRMR_MAX_PA);
388 return ERR_PTR(-EINVAL);
391 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
393 return ERR_PTR(-ENOMEM);
395 mr->type = MR_TYPE_FRMR;
396 mr->pd = to_hr_pd(pd)->pdn;
397 mr->size = max_num_sg * (1 << PAGE_SHIFT);
399 /* Allocate memory region key */
400 ret = alloc_mr_key(hr_dev, mr);
404 ret = alloc_mr_pbl(hr_dev, mr, NULL, 0);
408 ret = hns_roce_mr_enable(hr_dev, mr);
412 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
413 mr->ibmr.length = mr->size;
418 free_mr_key(hr_dev, mr);
420 free_mr_pbl(hr_dev, mr);
426 static int hns_roce_set_page(struct ib_mr *ibmr, u64 addr)
428 struct hns_roce_mr *mr = to_hr_mr(ibmr);
430 if (likely(mr->npages < mr->pbl_mtr.hem_cfg.buf_pg_count)) {
431 mr->page_list[mr->npages++] = addr;
438 int hns_roce_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
439 unsigned int *sg_offset)
441 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
442 struct ib_device *ibdev = &hr_dev->ib_dev;
443 struct hns_roce_mr *mr = to_hr_mr(ibmr);
444 struct hns_roce_mtr *mtr = &mr->pbl_mtr;
448 mr->page_list = kvcalloc(mr->pbl_mtr.hem_cfg.buf_pg_count,
449 sizeof(dma_addr_t), GFP_KERNEL);
453 ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, hns_roce_set_page);
455 ibdev_err(ibdev, "failed to store sg pages %u %u, cnt = %d.\n",
456 mr->npages, mr->pbl_mtr.hem_cfg.buf_pg_count, ret);
460 mtr->hem_cfg.region[0].offset = 0;
461 mtr->hem_cfg.region[0].count = mr->npages;
462 mtr->hem_cfg.region[0].hopnum = mr->pbl_hop_num;
463 mtr->hem_cfg.region_count = 1;
464 ret = hns_roce_mtr_map(hr_dev, mtr, mr->page_list, mr->npages);
466 ibdev_err(ibdev, "failed to map sg mtr, ret = %d.\n", ret);
469 mr->pbl_mtr.hem_cfg.buf_pg_shift = (u32)ilog2(ibmr->page_size);
474 kvfree(mr->page_list);
475 mr->page_list = NULL;
480 static void hns_roce_mw_free(struct hns_roce_dev *hr_dev,
481 struct hns_roce_mw *mw)
483 struct device *dev = hr_dev->dev;
487 ret = hns_roce_hw_destroy_mpt(hr_dev, NULL,
488 key_to_hw_index(mw->rkey) &
489 (hr_dev->caps.num_mtpts - 1));
491 dev_warn(dev, "MW DESTROY_MPT failed (%d)\n", ret);
493 hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table,
494 key_to_hw_index(mw->rkey));
497 ida_free(&hr_dev->mr_table.mtpt_ida.ida,
498 (int)key_to_hw_index(mw->rkey));
501 static int hns_roce_mw_enable(struct hns_roce_dev *hr_dev,
502 struct hns_roce_mw *mw)
504 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
505 struct hns_roce_cmd_mailbox *mailbox;
506 struct device *dev = hr_dev->dev;
507 unsigned long mtpt_idx = key_to_hw_index(mw->rkey);
510 /* prepare HEM entry memory */
511 ret = hns_roce_table_get(hr_dev, &mr_table->mtpt_table, mtpt_idx);
515 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
516 if (IS_ERR(mailbox)) {
517 ret = PTR_ERR(mailbox);
521 ret = hr_dev->hw->mw_write_mtpt(mailbox->buf, mw);
523 dev_err(dev, "MW write mtpt fail!\n");
527 ret = hns_roce_hw_create_mpt(hr_dev, mailbox,
528 mtpt_idx & (hr_dev->caps.num_mtpts - 1));
530 dev_err(dev, "MW CREATE_MPT failed (%d)\n", ret);
536 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
541 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
544 hns_roce_table_put(hr_dev, &mr_table->mtpt_table, mtpt_idx);
549 int hns_roce_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
551 struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
552 struct hns_roce_ida *mtpt_ida = &hr_dev->mr_table.mtpt_ida;
553 struct ib_device *ibdev = &hr_dev->ib_dev;
554 struct hns_roce_mw *mw = to_hr_mw(ibmw);
558 /* Allocate a key for mw from mr_table */
559 id = ida_alloc_range(&mtpt_ida->ida, mtpt_ida->min, mtpt_ida->max,
562 ibdev_err(ibdev, "failed to alloc id for MW key, id(%d)\n", id);
566 mw->rkey = hw_index_to_key(id);
568 ibmw->rkey = mw->rkey;
569 mw->pdn = to_hr_pd(ibmw->pd)->pdn;
570 mw->pbl_hop_num = hr_dev->caps.pbl_hop_num;
571 mw->pbl_ba_pg_sz = hr_dev->caps.pbl_ba_pg_sz;
572 mw->pbl_buf_pg_sz = hr_dev->caps.pbl_buf_pg_sz;
574 ret = hns_roce_mw_enable(hr_dev, mw);
581 hns_roce_mw_free(hr_dev, mw);
585 int hns_roce_dealloc_mw(struct ib_mw *ibmw)
587 struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
588 struct hns_roce_mw *mw = to_hr_mw(ibmw);
590 hns_roce_mw_free(hr_dev, mw);
594 static int mtr_map_region(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
595 struct hns_roce_buf_region *region, dma_addr_t *pages,
604 offset = region->offset;
605 end = offset + region->count;
607 while (offset < end && npage < max_count) {
609 mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
610 offset, &count, NULL);
614 for (i = 0; i < count && npage < max_count; i++) {
615 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
616 addr = to_hr_hw_page_addr(pages[npage]);
620 mtts[i] = cpu_to_le64(addr);
629 static inline bool mtr_has_mtt(struct hns_roce_buf_attr *attr)
633 for (i = 0; i < attr->region_count; i++)
634 if (attr->region[i].hopnum != HNS_ROCE_HOP_NUM_0 &&
635 attr->region[i].hopnum > 0)
638 /* because the mtr only one root base address, when hopnum is 0 means
639 * root base address equals the first buffer address, thus all alloced
640 * memory must in a continuous space accessed by direct mode.
645 static inline size_t mtr_bufs_size(struct hns_roce_buf_attr *attr)
650 for (i = 0; i < attr->region_count; i++)
651 size += attr->region[i].size;
657 * check the given pages in continuous address space
658 * Returns 0 on success, or the error page num.
660 static inline int mtr_check_direct_pages(dma_addr_t *pages, int page_count,
661 unsigned int page_shift)
663 size_t page_size = 1 << page_shift;
666 for (i = 1; i < page_count; i++)
667 if (pages[i] - pages[i - 1] != page_size)
673 static void mtr_free_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
675 /* release user buffers */
677 ib_umem_release(mtr->umem);
681 /* release kernel buffers */
683 hns_roce_buf_free(hr_dev, mtr->kmem);
688 static int mtr_alloc_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
689 struct hns_roce_buf_attr *buf_attr,
690 struct ib_udata *udata, unsigned long user_addr)
692 struct ib_device *ibdev = &hr_dev->ib_dev;
695 total_size = mtr_bufs_size(buf_attr);
699 mtr->umem = ib_umem_get(ibdev, user_addr, total_size,
700 buf_attr->user_access);
701 if (IS_ERR_OR_NULL(mtr->umem)) {
702 ibdev_err(ibdev, "failed to get umem, ret = %ld.\n",
708 mtr->kmem = hns_roce_buf_alloc(hr_dev, total_size,
709 buf_attr->page_shift,
710 mtr->hem_cfg.is_direct ?
711 HNS_ROCE_BUF_DIRECT : 0);
712 if (IS_ERR(mtr->kmem)) {
713 ibdev_err(ibdev, "failed to alloc kmem, ret = %ld.\n",
715 return PTR_ERR(mtr->kmem);
722 static int mtr_map_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
723 int page_count, unsigned int page_shift)
725 struct ib_device *ibdev = &hr_dev->ib_dev;
730 /* alloc a tmp array to store buffer's dma address */
731 pages = kvcalloc(page_count, sizeof(dma_addr_t), GFP_KERNEL);
736 npage = hns_roce_get_umem_bufs(hr_dev, pages, page_count,
737 mtr->umem, page_shift);
739 npage = hns_roce_get_kmem_bufs(hr_dev, pages, page_count,
740 mtr->kmem, page_shift);
742 if (npage != page_count) {
743 ibdev_err(ibdev, "failed to get mtr page %d != %d.\n", npage,
749 if (mtr->hem_cfg.is_direct && npage > 1) {
750 ret = mtr_check_direct_pages(pages, npage, page_shift);
752 ibdev_err(ibdev, "failed to check %s page: %d / %d.\n",
753 mtr->umem ? "umtr" : "kmtr", ret, npage);
759 ret = hns_roce_mtr_map(hr_dev, mtr, pages, page_count);
761 ibdev_err(ibdev, "failed to map mtr pages, ret = %d.\n", ret);
769 int hns_roce_mtr_map(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
770 dma_addr_t *pages, unsigned int page_cnt)
772 struct ib_device *ibdev = &hr_dev->ib_dev;
773 struct hns_roce_buf_region *r;
774 unsigned int i, mapped_cnt;
778 * Only use the first page address as root ba when hopnum is 0, this
779 * is because the addresses of all pages are consecutive in this case.
781 if (mtr->hem_cfg.is_direct) {
782 mtr->hem_cfg.root_ba = pages[0];
786 for (i = 0, mapped_cnt = 0; i < mtr->hem_cfg.region_count &&
787 mapped_cnt < page_cnt; i++) {
788 r = &mtr->hem_cfg.region[i];
789 /* if hopnum is 0, no need to map pages in this region */
791 mapped_cnt += r->count;
795 if (r->offset + r->count > page_cnt) {
798 "failed to check mtr%u count %u + %u > %u.\n",
799 i, r->offset, r->count, page_cnt);
803 ret = mtr_map_region(hr_dev, mtr, r, &pages[r->offset],
804 page_cnt - mapped_cnt);
807 "failed to map mtr%u offset %u, ret = %d.\n",
815 if (mapped_cnt < page_cnt) {
817 ibdev_err(ibdev, "failed to map mtr pages count: %u < %u.\n",
818 mapped_cnt, page_cnt);
824 int hns_roce_mtr_find(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
825 int offset, u64 *mtt_buf, int mtt_max, u64 *base_addr)
827 struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
835 if (!mtt_buf || mtt_max < 1)
838 /* no mtt memory in direct mode, so just return the buffer address */
839 if (cfg->is_direct) {
840 start_index = offset >> HNS_HW_PAGE_SHIFT;
841 for (mtt_count = 0; mtt_count < cfg->region_count &&
842 total < mtt_max; mtt_count++) {
843 npage = cfg->region[mtt_count].offset;
844 if (npage < start_index)
847 addr = cfg->root_ba + (npage << HNS_HW_PAGE_SHIFT);
848 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
849 mtt_buf[total] = to_hr_hw_page_addr(addr);
851 mtt_buf[total] = addr;
859 start_index = offset >> cfg->buf_pg_shift;
863 mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
866 if (!mtts || !mtt_count)
869 npage = min(mtt_count, left);
871 for (mtt_count = 0; mtt_count < npage; mtt_count++)
872 mtt_buf[total++] = le64_to_cpu(mtts[mtt_count]);
877 *base_addr = cfg->root_ba;
882 static int mtr_init_buf_cfg(struct hns_roce_dev *hr_dev,
883 struct hns_roce_buf_attr *attr,
884 struct hns_roce_hem_cfg *cfg,
885 unsigned int *buf_page_shift, int unalinged_size)
887 struct hns_roce_buf_region *r;
888 int first_region_padding;
889 int page_cnt, region_cnt;
890 unsigned int page_shift;
893 /* If mtt is disabled, all pages must be within a continuous range */
894 cfg->is_direct = !mtr_has_mtt(attr);
895 buf_size = mtr_bufs_size(attr);
896 if (cfg->is_direct) {
897 /* When HEM buffer uses 0-level addressing, the page size is
898 * equal to the whole buffer size, and we split the buffer into
899 * small pages which is used to check whether the adjacent
900 * units are in the continuous space and its size is fixed to
901 * 4K based on hns ROCEE's requirement.
903 page_shift = HNS_HW_PAGE_SHIFT;
905 /* The ROCEE requires the page size to be 4K * 2 ^ N. */
906 cfg->buf_pg_count = 1;
907 cfg->buf_pg_shift = HNS_HW_PAGE_SHIFT +
908 order_base_2(DIV_ROUND_UP(buf_size, HNS_HW_PAGE_SIZE));
909 first_region_padding = 0;
911 page_shift = attr->page_shift;
912 cfg->buf_pg_count = DIV_ROUND_UP(buf_size + unalinged_size,
914 cfg->buf_pg_shift = page_shift;
915 first_region_padding = unalinged_size;
918 /* Convert buffer size to page index and page count for each region and
919 * the buffer's offset needs to be appended to the first region.
921 for (page_cnt = 0, region_cnt = 0; region_cnt < attr->region_count &&
922 region_cnt < ARRAY_SIZE(cfg->region); region_cnt++) {
923 r = &cfg->region[region_cnt];
924 r->offset = page_cnt;
925 buf_size = hr_hw_page_align(attr->region[region_cnt].size +
926 first_region_padding);
927 r->count = DIV_ROUND_UP(buf_size, 1 << page_shift);
928 first_region_padding = 0;
929 page_cnt += r->count;
930 r->hopnum = to_hr_hem_hopnum(attr->region[region_cnt].hopnum,
934 cfg->region_count = region_cnt;
935 *buf_page_shift = page_shift;
940 static int mtr_alloc_mtt(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
941 unsigned int ba_page_shift)
943 struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
946 hns_roce_hem_list_init(&mtr->hem_list);
947 if (!cfg->is_direct) {
948 ret = hns_roce_hem_list_request(hr_dev, &mtr->hem_list,
949 cfg->region, cfg->region_count,
953 cfg->root_ba = mtr->hem_list.root_ba;
954 cfg->ba_pg_shift = ba_page_shift;
956 cfg->ba_pg_shift = cfg->buf_pg_shift;
962 static void mtr_free_mtt(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
964 hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
968 * hns_roce_mtr_create - Create hns memory translate region.
970 * @hr_dev: RoCE device struct pointer
971 * @mtr: memory translate region
972 * @buf_attr: buffer attribute for creating mtr
973 * @ba_page_shift: page shift for multi-hop base address table
974 * @udata: user space context, if it's NULL, means kernel space
975 * @user_addr: userspace virtual address to start at
977 int hns_roce_mtr_create(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
978 struct hns_roce_buf_attr *buf_attr,
979 unsigned int ba_page_shift, struct ib_udata *udata,
980 unsigned long user_addr)
982 struct ib_device *ibdev = &hr_dev->ib_dev;
983 unsigned int buf_page_shift = 0;
987 buf_page_cnt = mtr_init_buf_cfg(hr_dev, buf_attr, &mtr->hem_cfg,
989 udata ? user_addr & ~PAGE_MASK : 0);
990 if (buf_page_cnt < 1 || buf_page_shift < HNS_HW_PAGE_SHIFT) {
991 ibdev_err(ibdev, "failed to init mtr cfg, count %d shift %d.\n",
992 buf_page_cnt, buf_page_shift);
996 ret = mtr_alloc_mtt(hr_dev, mtr, ba_page_shift);
998 ibdev_err(ibdev, "failed to alloc mtr mtt, ret = %d.\n", ret);
1002 /* The caller has its own buffer list and invokes the hns_roce_mtr_map()
1003 * to finish the MTT configuration.
1005 if (buf_attr->mtt_only) {
1011 ret = mtr_alloc_bufs(hr_dev, mtr, buf_attr, udata, user_addr);
1013 ibdev_err(ibdev, "failed to alloc mtr bufs, ret = %d.\n", ret);
1017 /* Write buffer's dma address to MTT */
1018 ret = mtr_map_bufs(hr_dev, mtr, buf_page_cnt, buf_page_shift);
1020 ibdev_err(ibdev, "failed to map mtr bufs, ret = %d.\n", ret);
1024 mtr_free_bufs(hr_dev, mtr);
1026 mtr_free_mtt(hr_dev, mtr);
1030 void hns_roce_mtr_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
1032 /* release multi-hop addressing resource */
1033 hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
1036 mtr_free_bufs(hr_dev, mtr);