xref: /linux/drivers/infiniband/hw/hns/hns_roce_pd.c (revision a5e9f0ae620f58b6f6fa4ee00accaac523cb1a72)
1 /*
2  * Copyright (c) 2016 Hisilicon Limited.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <rdma/uverbs_ioctl.h>
34 #include "hns_roce_device.h"
35 
36 void hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
37 {
38 	struct hns_roce_ida *pd_ida = &hr_dev->pd_ida;
39 
40 	ida_init(&pd_ida->ida);
41 	pd_ida->max = hr_dev->caps.num_pds - 1;
42 	pd_ida->min = hr_dev->caps.reserved_pds;
43 }
44 
45 int hns_roce_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
46 {
47 	struct ib_device *ib_dev = ibpd->device;
48 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
49 	struct hns_roce_ida *pd_ida = &hr_dev->pd_ida;
50 	struct hns_roce_pd *pd = to_hr_pd(ibpd);
51 	int ret = 0;
52 	int id;
53 
54 	id = ida_alloc_range(&pd_ida->ida, pd_ida->min, pd_ida->max,
55 			     GFP_KERNEL);
56 	if (id < 0) {
57 		ibdev_err(ib_dev, "failed to alloc pd, id = %d.\n", id);
58 		return -ENOMEM;
59 	}
60 	pd->pdn = (unsigned long)id;
61 
62 	if (udata) {
63 		struct hns_roce_ib_alloc_pd_resp resp = {.pdn = pd->pdn};
64 
65 		ret = ib_respond_udata(udata, resp);
66 		if (ret)
67 			ida_free(&pd_ida->ida, id);
68 	}
69 
70 	return ret;
71 }
72 
73 int hns_roce_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
74 {
75 	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
76 
77 	ida_free(&hr_dev->pd_ida.ida, (int)to_hr_pd(pd)->pdn);
78 
79 	return 0;
80 }
81 
82 int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
83 {
84 	struct hns_roce_ida *uar_ida = &hr_dev->uar_ida;
85 	int id;
86 
87 	/* Using bitmap to manager UAR index */
88 	id = ida_alloc_range(&uar_ida->ida, uar_ida->min, uar_ida->max,
89 			     GFP_KERNEL);
90 	if (id < 0) {
91 		ibdev_err(&hr_dev->ib_dev, "failed to alloc uar id(%d).\n", id);
92 		return -ENOMEM;
93 	}
94 	uar->logic_idx = (unsigned long)id;
95 
96 	if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1)
97 		uar->index = (uar->logic_idx - 1) %
98 			     (hr_dev->caps.phy_num_uars - 1) + 1;
99 	else
100 		uar->index = 0;
101 
102 	uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2)) >> PAGE_SHIFT);
103 	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DIRECT_WQE)
104 		hr_dev->dwqe_page = pci_resource_start(hr_dev->pci_dev, 4);
105 
106 	return 0;
107 }
108 
109 void hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
110 {
111 	struct hns_roce_ida *uar_ida = &hr_dev->uar_ida;
112 
113 	ida_init(&uar_ida->ida);
114 	uar_ida->max = hr_dev->caps.num_uars - 1;
115 	uar_ida->min = hr_dev->caps.reserved_uars;
116 }
117 
118 static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn)
119 {
120 	struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida;
121 	int id;
122 
123 	id = ida_alloc_range(&xrcd_ida->ida, xrcd_ida->min, xrcd_ida->max,
124 			     GFP_KERNEL);
125 	if (id < 0) {
126 		ibdev_err(&hr_dev->ib_dev, "failed to alloc xrcdn(%d).\n", id);
127 		return -ENOMEM;
128 	}
129 	*xrcdn = (u32)id;
130 
131 	return 0;
132 }
133 
134 void hns_roce_init_xrcd_table(struct hns_roce_dev *hr_dev)
135 {
136 	struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida;
137 
138 	ida_init(&xrcd_ida->ida);
139 	xrcd_ida->max = hr_dev->caps.num_xrcds - 1;
140 	xrcd_ida->min = hr_dev->caps.reserved_xrcds;
141 }
142 
143 int hns_roce_alloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
144 {
145 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
146 	struct hns_roce_xrcd *xrcd = to_hr_xrcd(ib_xrcd);
147 	int ret;
148 
149 	if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)) {
150 		ret = -EOPNOTSUPP;
151 		goto err_out;
152 	}
153 
154 	ret = hns_roce_xrcd_alloc(hr_dev, &xrcd->xrcdn);
155 
156 err_out:
157 	if (ret)
158 		atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_XRCD_ALLOC_ERR_CNT]);
159 
160 	return ret;
161 }
162 
163 int hns_roce_dealloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
164 {
165 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
166 	u32 xrcdn = to_hr_xrcd(ib_xrcd)->xrcdn;
167 
168 	ida_free(&hr_dev->xrcd_ida.ida, (int)xrcdn);
169 
170 	return 0;
171 }
172