162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2016 Hisilicon Limited.
362306a36Sopenharmony_ci * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This software is available to you under a choice of one of two
662306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
962306a36Sopenharmony_ci * OpenIB.org BSD license below:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1262306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1362306a36Sopenharmony_ci *     conditions are met:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1662306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1762306a36Sopenharmony_ci *        disclaimer.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2062306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2262306a36Sopenharmony_ci *        provided with the distribution.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3162306a36Sopenharmony_ci * SOFTWARE.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include <linux/vmalloc.h>
3562306a36Sopenharmony_ci#include <rdma/ib_umem.h>
3662306a36Sopenharmony_ci#include "hns_roce_device.h"
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_civoid hns_roce_buf_free(struct hns_roce_dev *hr_dev, struct hns_roce_buf *buf)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	struct hns_roce_buf_list *trunks;
4162306a36Sopenharmony_ci	u32 i;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	if (!buf)
4462306a36Sopenharmony_ci		return;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	trunks = buf->trunk_list;
4762306a36Sopenharmony_ci	if (trunks) {
4862306a36Sopenharmony_ci		buf->trunk_list = NULL;
4962306a36Sopenharmony_ci		for (i = 0; i < buf->ntrunks; i++)
5062306a36Sopenharmony_ci			dma_free_coherent(hr_dev->dev, 1 << buf->trunk_shift,
5162306a36Sopenharmony_ci					  trunks[i].buf, trunks[i].map);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci		kfree(trunks);
5462306a36Sopenharmony_ci	}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	kfree(buf);
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * Allocate the dma buffer for storing ROCEE table entries
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * @size: required size
6362306a36Sopenharmony_ci * @page_shift: the unit size in a continuous dma address range
6462306a36Sopenharmony_ci * @flags: HNS_ROCE_BUF_ flags to control the allocation flow.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_cistruct hns_roce_buf *hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size,
6762306a36Sopenharmony_ci					u32 page_shift, u32 flags)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	u32 trunk_size, page_size, alloced_size;
7062306a36Sopenharmony_ci	struct hns_roce_buf_list *trunks;
7162306a36Sopenharmony_ci	struct hns_roce_buf *buf;
7262306a36Sopenharmony_ci	gfp_t gfp_flags;
7362306a36Sopenharmony_ci	u32 ntrunk, i;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	/* The minimum shift of the page accessed by hw is HNS_HW_PAGE_SHIFT */
7662306a36Sopenharmony_ci	if (WARN_ON(page_shift < HNS_HW_PAGE_SHIFT))
7762306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	gfp_flags = (flags & HNS_ROCE_BUF_NOSLEEP) ? GFP_ATOMIC : GFP_KERNEL;
8062306a36Sopenharmony_ci	buf = kzalloc(sizeof(*buf), gfp_flags);
8162306a36Sopenharmony_ci	if (!buf)
8262306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	buf->page_shift = page_shift;
8562306a36Sopenharmony_ci	page_size = 1 << buf->page_shift;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* Calc the trunk size and num by required size and page_shift */
8862306a36Sopenharmony_ci	if (flags & HNS_ROCE_BUF_DIRECT) {
8962306a36Sopenharmony_ci		buf->trunk_shift = order_base_2(ALIGN(size, PAGE_SIZE));
9062306a36Sopenharmony_ci		ntrunk = 1;
9162306a36Sopenharmony_ci	} else {
9262306a36Sopenharmony_ci		buf->trunk_shift = order_base_2(ALIGN(page_size, PAGE_SIZE));
9362306a36Sopenharmony_ci		ntrunk = DIV_ROUND_UP(size, 1 << buf->trunk_shift);
9462306a36Sopenharmony_ci	}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	trunks = kcalloc(ntrunk, sizeof(*trunks), gfp_flags);
9762306a36Sopenharmony_ci	if (!trunks) {
9862306a36Sopenharmony_ci		kfree(buf);
9962306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
10062306a36Sopenharmony_ci	}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	trunk_size = 1 << buf->trunk_shift;
10362306a36Sopenharmony_ci	alloced_size = 0;
10462306a36Sopenharmony_ci	for (i = 0; i < ntrunk; i++) {
10562306a36Sopenharmony_ci		trunks[i].buf = dma_alloc_coherent(hr_dev->dev, trunk_size,
10662306a36Sopenharmony_ci						   &trunks[i].map, gfp_flags);
10762306a36Sopenharmony_ci		if (!trunks[i].buf)
10862306a36Sopenharmony_ci			break;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci		alloced_size += trunk_size;
11162306a36Sopenharmony_ci	}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	buf->ntrunks = i;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* In nofail mode, it's only failed when the alloced size is 0 */
11662306a36Sopenharmony_ci	if ((flags & HNS_ROCE_BUF_NOFAIL) ? i == 0 : i != ntrunk) {
11762306a36Sopenharmony_ci		for (i = 0; i < buf->ntrunks; i++)
11862306a36Sopenharmony_ci			dma_free_coherent(hr_dev->dev, trunk_size,
11962306a36Sopenharmony_ci					  trunks[i].buf, trunks[i].map);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci		kfree(trunks);
12262306a36Sopenharmony_ci		kfree(buf);
12362306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
12462306a36Sopenharmony_ci	}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	buf->npages = DIV_ROUND_UP(alloced_size, page_size);
12762306a36Sopenharmony_ci	buf->trunk_list = trunks;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	return buf;
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciint hns_roce_get_kmem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
13362306a36Sopenharmony_ci			   int buf_cnt, struct hns_roce_buf *buf,
13462306a36Sopenharmony_ci			   unsigned int page_shift)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	unsigned int offset, max_size;
13762306a36Sopenharmony_ci	int total = 0;
13862306a36Sopenharmony_ci	int i;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	if (page_shift > buf->trunk_shift) {
14162306a36Sopenharmony_ci		dev_err(hr_dev->dev, "failed to check kmem buf shift %u > %u\n",
14262306a36Sopenharmony_ci			page_shift, buf->trunk_shift);
14362306a36Sopenharmony_ci		return -EINVAL;
14462306a36Sopenharmony_ci	}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	offset = 0;
14762306a36Sopenharmony_ci	max_size = buf->ntrunks << buf->trunk_shift;
14862306a36Sopenharmony_ci	for (i = 0; i < buf_cnt && offset < max_size; i++) {
14962306a36Sopenharmony_ci		bufs[total++] = hns_roce_buf_dma_addr(buf, offset);
15062306a36Sopenharmony_ci		offset += (1 << page_shift);
15162306a36Sopenharmony_ci	}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	return total;
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciint hns_roce_get_umem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
15762306a36Sopenharmony_ci			   int buf_cnt, struct ib_umem *umem,
15862306a36Sopenharmony_ci			   unsigned int page_shift)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	struct ib_block_iter biter;
16162306a36Sopenharmony_ci	int total = 0;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	/* convert system page cnt to hw page cnt */
16462306a36Sopenharmony_ci	rdma_umem_for_each_dma_block(umem, &biter, 1 << page_shift) {
16562306a36Sopenharmony_ci		bufs[total++] = rdma_block_iter_dma_address(&biter);
16662306a36Sopenharmony_ci		if (total >= buf_cnt)
16762306a36Sopenharmony_ci			goto done;
16862306a36Sopenharmony_ci	}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cidone:
17162306a36Sopenharmony_ci	return total;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_civoid hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
17762306a36Sopenharmony_ci		ida_destroy(&hr_dev->xrcd_ida.ida);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
18062306a36Sopenharmony_ci		ida_destroy(&hr_dev->srq_table.srq_ida.ida);
18162306a36Sopenharmony_ci	hns_roce_cleanup_qp_table(hr_dev);
18262306a36Sopenharmony_ci	hns_roce_cleanup_cq_table(hr_dev);
18362306a36Sopenharmony_ci	ida_destroy(&hr_dev->mr_table.mtpt_ida.ida);
18462306a36Sopenharmony_ci	ida_destroy(&hr_dev->pd_ida.ida);
18562306a36Sopenharmony_ci	ida_destroy(&hr_dev->uar_ida.ida);
18662306a36Sopenharmony_ci}
187