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 <linux/platform_device.h>
34 #include <linux/pci.h>
35 #include "hns_roce_device.h"
36 
37 static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn)
38 {
39 	return hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, pdn) ? -ENOMEM : 0;
40 }
41 
42 static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn)
43 {
44 	hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn, BITMAP_NO_RR);
45 }
46 
47 int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
48 {
49 	return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds,
50 				    hr_dev->caps.num_pds - 1,
51 				    hr_dev->caps.reserved_pds, 0);
52 }
53 
54 void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev)
55 {
56 	hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap);
57 }
58 
59 int hns_roce_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
60 {
61 	struct ib_device *ib_dev = ibpd->device;
62 	struct hns_roce_pd *pd = to_hr_pd(ibpd);
63 	int ret;
64 
65 	ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn);
66 	if (ret) {
67 		ibdev_err(ib_dev, "failed to alloc pd, ret = %d.\n", ret);
68 		return ret;
69 	}
70 
71 	if (udata) {
72 		struct hns_roce_ib_alloc_pd_resp resp = {.pdn = pd->pdn};
73 
74 		ret = ib_copy_to_udata(udata, &resp,
75 				       min(udata->outlen, sizeof(resp)));
76 		if (ret) {
77 			hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn);
78 			ibdev_err(ib_dev, "failed to copy to udata, ret = %d\n", ret);
79 		}
80 	}
81 
82 	return ret;
83 }
84 
85 int hns_roce_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
86 {
87 	hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn);
88 	return 0;
89 }
90 
91 int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
92 {
93 	struct resource *res;
94 	int ret;
95 
96 	/* Using bitmap to manager UAR index */
97 	ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->logic_idx);
98 	if (ret)
99 		return -ENOMEM;
100 
101 	if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1)
102 		uar->index = (uar->logic_idx - 1) %
103 			     (hr_dev->caps.phy_num_uars - 1) + 1;
104 	else
105 		uar->index = 0;
106 
107 	if (!dev_is_pci(hr_dev->dev)) {
108 		res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
109 		if (!res) {
110 			dev_err(&hr_dev->pdev->dev, "memory resource not found!\n");
111 			return -EINVAL;
112 		}
113 		uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
114 	} else {
115 		uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2))
116 			   >> PAGE_SHIFT);
117 	}
118 
119 	return 0;
120 }
121 
122 void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
123 {
124 	hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->logic_idx,
125 			     BITMAP_NO_RR);
126 }
127 
128 int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
129 {
130 	return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap,
131 				    hr_dev->caps.num_uars,
132 				    hr_dev->caps.num_uars - 1,
133 				    hr_dev->caps.reserved_uars, 0);
134 }
135 
136 void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
137 {
138 	hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap);
139 }
140 
141 static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn)
142 {
143 	unsigned long obj;
144 	int ret;
145 
146 	ret = hns_roce_bitmap_alloc(&hr_dev->xrcd_bitmap, &obj);
147 	if (ret)
148 		return ret;
149 
150 	*xrcdn = obj;
151 
152 	return 0;
153 }
154 
155 static void hns_roce_xrcd_free(struct hns_roce_dev *hr_dev,
156 			       u32 xrcdn)
157 {
158 	hns_roce_bitmap_free(&hr_dev->xrcd_bitmap, xrcdn, BITMAP_NO_RR);
159 }
160 
161 int hns_roce_init_xrcd_table(struct hns_roce_dev *hr_dev)
162 {
163 	return hns_roce_bitmap_init(&hr_dev->xrcd_bitmap,
164 				    hr_dev->caps.num_xrcds,
165 				    hr_dev->caps.num_xrcds - 1,
166 				    hr_dev->caps.reserved_xrcds, 0);
167 }
168 
169 void hns_roce_cleanup_xrcd_table(struct hns_roce_dev *hr_dev)
170 {
171 	hns_roce_bitmap_cleanup(&hr_dev->xrcd_bitmap);
172 }
173 
174 int hns_roce_alloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
175 {
176 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
177 	struct hns_roce_xrcd *xrcd = to_hr_xrcd(ib_xrcd);
178 	int ret;
179 
180 	if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC))
181 		return -EINVAL;
182 
183 	ret = hns_roce_xrcd_alloc(hr_dev, &xrcd->xrcdn);
184 	if (ret) {
185 		dev_err(hr_dev->dev, "failed to alloc xrcdn, ret = %d.\n", ret);
186 		return ret;
187 	}
188 
189 	return 0;
190 }
191 
192 int hns_roce_dealloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
193 {
194 	hns_roce_xrcd_free(to_hr_dev(ib_xrcd->device),
195 			   to_hr_xrcd(ib_xrcd)->xrcdn);
196 
197 	return 0;
198 }
199