xref: /openbmc/linux/drivers/infiniband/hw/hns/hns_roce_alloc.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
19a443537Soulijun /*
29a443537Soulijun  * Copyright (c) 2016 Hisilicon Limited.
39a443537Soulijun  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
49a443537Soulijun  *
59a443537Soulijun  * This software is available to you under a choice of one of two
69a443537Soulijun  * licenses.  You may choose to be licensed under the terms of the GNU
79a443537Soulijun  * General Public License (GPL) Version 2, available from the file
89a443537Soulijun  * COPYING in the main directory of this source tree, or the
99a443537Soulijun  * OpenIB.org BSD license below:
109a443537Soulijun  *
119a443537Soulijun  *     Redistribution and use in source and binary forms, with or
129a443537Soulijun  *     without modification, are permitted provided that the following
139a443537Soulijun  *     conditions are met:
149a443537Soulijun  *
159a443537Soulijun  *      - Redistributions of source code must retain the above
169a443537Soulijun  *        copyright notice, this list of conditions and the following
179a443537Soulijun  *        disclaimer.
189a443537Soulijun  *
199a443537Soulijun  *      - Redistributions in binary form must reproduce the above
209a443537Soulijun  *        copyright notice, this list of conditions and the following
219a443537Soulijun  *        disclaimer in the documentation and/or other materials
229a443537Soulijun  *        provided with the distribution.
239a443537Soulijun  *
249a443537Soulijun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
259a443537Soulijun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
269a443537Soulijun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
279a443537Soulijun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
289a443537Soulijun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
299a443537Soulijun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
309a443537Soulijun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
319a443537Soulijun  * SOFTWARE.
329a443537Soulijun  */
339a443537Soulijun 
34e89bf462SMatan Barak #include <linux/vmalloc.h>
352ac0bc5eSLijun Ou #include <rdma/ib_umem.h>
36*38d22088SChengchang Tang #include "hns_roce_device.h"
379a443537Soulijun 
hns_roce_buf_free(struct hns_roce_dev * hr_dev,struct hns_roce_buf * buf)38cc23267aSXi Wang void hns_roce_buf_free(struct hns_roce_dev *hr_dev, struct hns_roce_buf *buf)
399a443537Soulijun {
406f6e2dcbSXi Wang 	struct hns_roce_buf_list *trunks;
416f6e2dcbSXi Wang 	u32 i;
429a443537Soulijun 
436f6e2dcbSXi Wang 	if (!buf)
44cc23267aSXi Wang 		return;
45cc23267aSXi Wang 
466f6e2dcbSXi Wang 	trunks = buf->trunk_list;
476f6e2dcbSXi Wang 	if (trunks) {
486f6e2dcbSXi Wang 		buf->trunk_list = NULL;
496f6e2dcbSXi Wang 		for (i = 0; i < buf->ntrunks; i++)
506f6e2dcbSXi Wang 			dma_free_coherent(hr_dev->dev, 1 << buf->trunk_shift,
516f6e2dcbSXi Wang 					  trunks[i].buf, trunks[i].map);
52cc23267aSXi Wang 
536f6e2dcbSXi Wang 		kfree(trunks);
549a443537Soulijun 	}
559a443537Soulijun 
566f6e2dcbSXi Wang 	kfree(buf);
576f6e2dcbSXi Wang }
586f6e2dcbSXi Wang 
596f6e2dcbSXi Wang /*
606f6e2dcbSXi Wang  * Allocate the dma buffer for storing ROCEE table entries
616f6e2dcbSXi Wang  *
626f6e2dcbSXi Wang  * @size: required size
636f6e2dcbSXi Wang  * @page_shift: the unit size in a continuous dma address range
646f6e2dcbSXi Wang  * @flags: HNS_ROCE_BUF_ flags to control the allocation flow.
656f6e2dcbSXi Wang  */
hns_roce_buf_alloc(struct hns_roce_dev * hr_dev,u32 size,u32 page_shift,u32 flags)666f6e2dcbSXi Wang struct hns_roce_buf *hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size,
676f6e2dcbSXi Wang 					u32 page_shift, u32 flags)
689a443537Soulijun {
696f6e2dcbSXi Wang 	u32 trunk_size, page_size, alloced_size;
706f6e2dcbSXi Wang 	struct hns_roce_buf_list *trunks;
716f6e2dcbSXi Wang 	struct hns_roce_buf *buf;
726f6e2dcbSXi Wang 	gfp_t gfp_flags;
736f6e2dcbSXi Wang 	u32 ntrunk, i;
749a443537Soulijun 
759581a356SXi Wang 	/* The minimum shift of the page accessed by hw is HNS_HW_PAGE_SHIFT */
766f6e2dcbSXi Wang 	if (WARN_ON(page_shift < HNS_HW_PAGE_SHIFT))
776f6e2dcbSXi Wang 		return ERR_PTR(-EINVAL);
78cc23267aSXi Wang 
796f6e2dcbSXi Wang 	gfp_flags = (flags & HNS_ROCE_BUF_NOSLEEP) ? GFP_ATOMIC : GFP_KERNEL;
806f6e2dcbSXi Wang 	buf = kzalloc(sizeof(*buf), gfp_flags);
816f6e2dcbSXi Wang 	if (!buf)
826f6e2dcbSXi Wang 		return ERR_PTR(-ENOMEM);
836f6e2dcbSXi Wang 
846f6e2dcbSXi Wang 	buf->page_shift = page_shift;
85cc23267aSXi Wang 	page_size = 1 << buf->page_shift;
86cc23267aSXi Wang 
876f6e2dcbSXi Wang 	/* Calc the trunk size and num by required size and page_shift */
886f6e2dcbSXi Wang 	if (flags & HNS_ROCE_BUF_DIRECT) {
897b0006dbSXi Wang 		buf->trunk_shift = order_base_2(ALIGN(size, PAGE_SIZE));
906f6e2dcbSXi Wang 		ntrunk = 1;
919a443537Soulijun 	} else {
927b0006dbSXi Wang 		buf->trunk_shift = order_base_2(ALIGN(page_size, PAGE_SIZE));
936f6e2dcbSXi Wang 		ntrunk = DIV_ROUND_UP(size, 1 << buf->trunk_shift);
946f6e2dcbSXi Wang 	}
959a443537Soulijun 
966f6e2dcbSXi Wang 	trunks = kcalloc(ntrunk, sizeof(*trunks), gfp_flags);
976f6e2dcbSXi Wang 	if (!trunks) {
986f6e2dcbSXi Wang 		kfree(buf);
996f6e2dcbSXi Wang 		return ERR_PTR(-ENOMEM);
1006f6e2dcbSXi Wang 	}
1016f6e2dcbSXi Wang 
1026f6e2dcbSXi Wang 	trunk_size = 1 << buf->trunk_shift;
1036f6e2dcbSXi Wang 	alloced_size = 0;
1046f6e2dcbSXi Wang 	for (i = 0; i < ntrunk; i++) {
1056f6e2dcbSXi Wang 		trunks[i].buf = dma_alloc_coherent(hr_dev->dev, trunk_size,
1066f6e2dcbSXi Wang 						   &trunks[i].map, gfp_flags);
1076f6e2dcbSXi Wang 		if (!trunks[i].buf)
108cc23267aSXi Wang 			break;
1096f6e2dcbSXi Wang 
1106f6e2dcbSXi Wang 		alloced_size += trunk_size;
1119a443537Soulijun 	}
112cc23267aSXi Wang 
1136f6e2dcbSXi Wang 	buf->ntrunks = i;
1149a443537Soulijun 
1156f6e2dcbSXi Wang 	/* In nofail mode, it's only failed when the alloced size is 0 */
1166f6e2dcbSXi Wang 	if ((flags & HNS_ROCE_BUF_NOFAIL) ? i == 0 : i != ntrunk) {
1176f6e2dcbSXi Wang 		for (i = 0; i < buf->ntrunks; i++)
1186f6e2dcbSXi Wang 			dma_free_coherent(hr_dev->dev, trunk_size,
1196f6e2dcbSXi Wang 					  trunks[i].buf, trunks[i].map);
1206f6e2dcbSXi Wang 
1216f6e2dcbSXi Wang 		kfree(trunks);
1226f6e2dcbSXi Wang 		kfree(buf);
1236f6e2dcbSXi Wang 		return ERR_PTR(-ENOMEM);
1246f6e2dcbSXi Wang 	}
1256f6e2dcbSXi Wang 
1266f6e2dcbSXi Wang 	buf->npages = DIV_ROUND_UP(alloced_size, page_size);
1276f6e2dcbSXi Wang 	buf->trunk_list = trunks;
1286f6e2dcbSXi Wang 
1296f6e2dcbSXi Wang 	return buf;
1309a443537Soulijun }
1319a443537Soulijun 
hns_roce_get_kmem_bufs(struct hns_roce_dev * hr_dev,dma_addr_t * bufs,int buf_cnt,struct hns_roce_buf * buf,unsigned int page_shift)1322ac0bc5eSLijun Ou int hns_roce_get_kmem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
1337b0006dbSXi Wang 			   int buf_cnt, struct hns_roce_buf *buf,
1347b0006dbSXi Wang 			   unsigned int page_shift)
1352ac0bc5eSLijun Ou {
1367b0006dbSXi Wang 	unsigned int offset, max_size;
1377b0006dbSXi Wang 	int total = 0;
1387b0006dbSXi Wang 	int i;
1392ac0bc5eSLijun Ou 
1407b0006dbSXi Wang 	if (page_shift > buf->trunk_shift) {
1417b0006dbSXi Wang 		dev_err(hr_dev->dev, "failed to check kmem buf shift %u > %u\n",
1427b0006dbSXi Wang 			page_shift, buf->trunk_shift);
1432ac0bc5eSLijun Ou 		return -EINVAL;
1442ac0bc5eSLijun Ou 	}
1452ac0bc5eSLijun Ou 
1467b0006dbSXi Wang 	offset = 0;
1477b0006dbSXi Wang 	max_size = buf->ntrunks << buf->trunk_shift;
1487b0006dbSXi Wang 	for (i = 0; i < buf_cnt && offset < max_size; i++) {
1497b0006dbSXi Wang 		bufs[total++] = hns_roce_buf_dma_addr(buf, offset);
1507b0006dbSXi Wang 		offset += (1 << page_shift);
1517b0006dbSXi Wang 	}
1522ac0bc5eSLijun Ou 
1532ac0bc5eSLijun Ou 	return total;
1542ac0bc5eSLijun Ou }
1552ac0bc5eSLijun Ou 
hns_roce_get_umem_bufs(struct hns_roce_dev * hr_dev,dma_addr_t * bufs,int buf_cnt,struct ib_umem * umem,unsigned int page_shift)1562ac0bc5eSLijun Ou int hns_roce_get_umem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
1577b0006dbSXi Wang 			   int buf_cnt, struct ib_umem *umem,
15882d07a4eSWeihang Li 			   unsigned int page_shift)
1592ac0bc5eSLijun Ou {
1602ac0bc5eSLijun Ou 	struct ib_block_iter biter;
1612ac0bc5eSLijun Ou 	int total = 0;
1622ac0bc5eSLijun Ou 
1632ac0bc5eSLijun Ou 	/* convert system page cnt to hw page cnt */
164ebc24096SJason Gunthorpe 	rdma_umem_for_each_dma_block(umem, &biter, 1 << page_shift) {
1657b0006dbSXi Wang 		bufs[total++] = rdma_block_iter_dma_address(&biter);
1662ac0bc5eSLijun Ou 		if (total >= buf_cnt)
1672ac0bc5eSLijun Ou 			goto done;
1682ac0bc5eSLijun Ou 	}
1692ac0bc5eSLijun Ou 
1702ac0bc5eSLijun Ou done:
1712ac0bc5eSLijun Ou 	return total;
1722ac0bc5eSLijun Ou }
1732ac0bc5eSLijun Ou 
hns_roce_cleanup_bitmap(struct hns_roce_dev * hr_dev)1749a443537Soulijun void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
1759a443537Soulijun {
17632548870SWenpeng Liang 	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
177da43b7beSYangyang Li 		ida_destroy(&hr_dev->xrcd_ida.ida);
17832548870SWenpeng Liang 
1795c1f167aSLijun Ou 	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
180c4f11b36SYangyang Li 		ida_destroy(&hr_dev->srq_table.srq_ida.ida);
1819a443537Soulijun 	hns_roce_cleanup_qp_table(hr_dev);
1829a443537Soulijun 	hns_roce_cleanup_cq_table(hr_dev);
183d38936f0SYangyang Li 	ida_destroy(&hr_dev->mr_table.mtpt_ida.ida);
184645f0593SYangyang Li 	ida_destroy(&hr_dev->pd_ida.ida);
1858feafd90SYangyang Li 	ida_destroy(&hr_dev->uar_ida.ida);
1869a443537Soulijun }
187