xref: /linux/drivers/infiniband/hw/hns/hns_roce_pd.c (revision 55aa394a5ed871208eac11c5f4677cafd258c4dd)
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 "hns_roce_device.h"
34 
hns_roce_init_pd_table(struct hns_roce_dev * hr_dev)35 void hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
36 {
37 	struct hns_roce_ida *pd_ida = &hr_dev->pd_ida;
38 
39 	ida_init(&pd_ida->ida);
40 	pd_ida->max = hr_dev->caps.num_pds - 1;
41 	pd_ida->min = hr_dev->caps.reserved_pds;
42 }
43 
hns_roce_alloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)44 int hns_roce_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
45 {
46 	struct ib_device *ib_dev = ibpd->device;
47 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
48 	struct hns_roce_ida *pd_ida = &hr_dev->pd_ida;
49 	struct hns_roce_pd *pd = to_hr_pd(ibpd);
50 	int ret = 0;
51 	int id;
52 
53 	id = ida_alloc_range(&pd_ida->ida, pd_ida->min, pd_ida->max,
54 			     GFP_KERNEL);
55 	if (id < 0) {
56 		ibdev_err(ib_dev, "failed to alloc pd, id = %d.\n", id);
57 		return -ENOMEM;
58 	}
59 	pd->pdn = (unsigned long)id;
60 
61 	if (udata) {
62 		struct hns_roce_ib_alloc_pd_resp resp = {.pdn = pd->pdn};
63 
64 		ret = ib_copy_to_udata(udata, &resp,
65 				       min(udata->outlen, sizeof(resp)));
66 		if (ret) {
67 			ida_free(&pd_ida->ida, id);
68 			ibdev_err(ib_dev, "failed to copy to udata, ret = %d\n", ret);
69 		}
70 	}
71 
72 	return ret;
73 }
74 
hns_roce_dealloc_pd(struct ib_pd * pd,struct ib_udata * udata)75 int hns_roce_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
76 {
77 	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
78 
79 	ida_free(&hr_dev->pd_ida.ida, (int)to_hr_pd(pd)->pdn);
80 
81 	return 0;
82 }
83 
hns_roce_uar_alloc(struct hns_roce_dev * hr_dev,struct hns_roce_uar * uar)84 int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
85 {
86 	struct hns_roce_ida *uar_ida = &hr_dev->uar_ida;
87 	int id;
88 
89 	/* Using bitmap to manager UAR index */
90 	id = ida_alloc_range(&uar_ida->ida, uar_ida->min, uar_ida->max,
91 			     GFP_KERNEL);
92 	if (id < 0) {
93 		ibdev_err(&hr_dev->ib_dev, "failed to alloc uar id(%d).\n", id);
94 		return -ENOMEM;
95 	}
96 	uar->logic_idx = (unsigned long)id;
97 
98 	if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1)
99 		uar->index = (uar->logic_idx - 1) %
100 			     (hr_dev->caps.phy_num_uars - 1) + 1;
101 	else
102 		uar->index = 0;
103 
104 	uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2)) >> PAGE_SHIFT);
105 	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DIRECT_WQE)
106 		hr_dev->dwqe_page = pci_resource_start(hr_dev->pci_dev, 4);
107 
108 	return 0;
109 }
110 
hns_roce_init_uar_table(struct hns_roce_dev * hr_dev)111 void hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
112 {
113 	struct hns_roce_ida *uar_ida = &hr_dev->uar_ida;
114 
115 	ida_init(&uar_ida->ida);
116 	uar_ida->max = hr_dev->caps.num_uars - 1;
117 	uar_ida->min = hr_dev->caps.reserved_uars;
118 }
119 
hns_roce_xrcd_alloc(struct hns_roce_dev * hr_dev,u32 * xrcdn)120 static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn)
121 {
122 	struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida;
123 	int id;
124 
125 	id = ida_alloc_range(&xrcd_ida->ida, xrcd_ida->min, xrcd_ida->max,
126 			     GFP_KERNEL);
127 	if (id < 0) {
128 		ibdev_err(&hr_dev->ib_dev, "failed to alloc xrcdn(%d).\n", id);
129 		return -ENOMEM;
130 	}
131 	*xrcdn = (u32)id;
132 
133 	return 0;
134 }
135 
hns_roce_init_xrcd_table(struct hns_roce_dev * hr_dev)136 void hns_roce_init_xrcd_table(struct hns_roce_dev *hr_dev)
137 {
138 	struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida;
139 
140 	ida_init(&xrcd_ida->ida);
141 	xrcd_ida->max = hr_dev->caps.num_xrcds - 1;
142 	xrcd_ida->min = hr_dev->caps.reserved_xrcds;
143 }
144 
hns_roce_alloc_xrcd(struct ib_xrcd * ib_xrcd,struct ib_udata * udata)145 int hns_roce_alloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
146 {
147 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
148 	struct hns_roce_xrcd *xrcd = to_hr_xrcd(ib_xrcd);
149 	int ret;
150 
151 	if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)) {
152 		ret = -EOPNOTSUPP;
153 		goto err_out;
154 	}
155 
156 	ret = hns_roce_xrcd_alloc(hr_dev, &xrcd->xrcdn);
157 
158 err_out:
159 	if (ret)
160 		atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_XRCD_ALLOC_ERR_CNT]);
161 
162 	return ret;
163 }
164 
hns_roce_dealloc_xrcd(struct ib_xrcd * ib_xrcd,struct ib_udata * udata)165 int hns_roce_dealloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
166 {
167 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
168 	u32 xrcdn = to_hr_xrcd(ib_xrcd)->xrcdn;
169 
170 	ida_free(&hr_dev->xrcd_ida.ida, (int)xrcdn);
171 
172 	return 0;
173 }
174