1 /*
2  * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
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  * 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 
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 
38 #include <rdma/ib_user_verbs.h>
39 #include <rdma/ib_addr.h>
40 
41 #include "usnic_common_util.h"
42 #include "usnic_ib.h"
43 #include "usnic_ib_qp_grp.h"
44 #include "usnic_vnic.h"
45 #include "usnic_ib_verbs.h"
46 #include "usnic_ib_sysfs.h"
47 #include "usnic_log.h"
48 
49 static ssize_t board_id_show(struct device *device,
50 			     struct device_attribute *attr, char *buf)
51 {
52 	struct usnic_ib_dev *us_ibdev =
53 		container_of(device, struct usnic_ib_dev, ib_dev.dev);
54 	unsigned short subsystem_device_id;
55 
56 	mutex_lock(&us_ibdev->usdev_lock);
57 	subsystem_device_id = us_ibdev->pdev->subsystem_device;
58 	mutex_unlock(&us_ibdev->usdev_lock);
59 
60 	return scnprintf(buf, PAGE_SIZE, "%hu\n", subsystem_device_id);
61 }
62 static DEVICE_ATTR_RO(board_id);
63 
64 /*
65  * Report the configuration for this PF
66  */
67 static ssize_t
68 config_show(struct device *device, struct device_attribute *attr, char *buf)
69 {
70 	struct usnic_ib_dev *us_ibdev;
71 	char *ptr;
72 	unsigned left;
73 	unsigned n;
74 	enum usnic_vnic_res_type res_type;
75 
76 	us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
77 
78 	/* Buffer space limit is 1 page */
79 	ptr = buf;
80 	left = PAGE_SIZE;
81 
82 	mutex_lock(&us_ibdev->usdev_lock);
83 	if (kref_read(&us_ibdev->vf_cnt) > 0) {
84 		char *busname;
85 
86 		/*
87 		 * bus name seems to come with annoying prefix.
88 		 * Remove it if it is predictable
89 		 */
90 		busname = us_ibdev->pdev->bus->name;
91 		if (strncmp(busname, "PCI Bus ", 8) == 0)
92 			busname += 8;
93 
94 		n = scnprintf(ptr, left,
95 			"%s: %s:%d.%d, %s, %pM, %u VFs\n Per VF:",
96 			dev_name(&us_ibdev->ib_dev.dev),
97 			busname,
98 			PCI_SLOT(us_ibdev->pdev->devfn),
99 			PCI_FUNC(us_ibdev->pdev->devfn),
100 			netdev_name(us_ibdev->netdev),
101 			us_ibdev->ufdev->mac,
102 			kref_read(&us_ibdev->vf_cnt));
103 		UPDATE_PTR_LEFT(n, ptr, left);
104 
105 		for (res_type = USNIC_VNIC_RES_TYPE_EOL;
106 				res_type < USNIC_VNIC_RES_TYPE_MAX;
107 				res_type++) {
108 			if (us_ibdev->vf_res_cnt[res_type] == 0)
109 				continue;
110 			n = scnprintf(ptr, left, " %d %s%s",
111 				us_ibdev->vf_res_cnt[res_type],
112 				usnic_vnic_res_type_to_str(res_type),
113 				(res_type < (USNIC_VNIC_RES_TYPE_MAX - 1)) ?
114 				 "," : "");
115 			UPDATE_PTR_LEFT(n, ptr, left);
116 		}
117 		n = scnprintf(ptr, left, "\n");
118 		UPDATE_PTR_LEFT(n, ptr, left);
119 	} else {
120 		n = scnprintf(ptr, left, "%s: no VFs\n",
121 				dev_name(&us_ibdev->ib_dev.dev));
122 		UPDATE_PTR_LEFT(n, ptr, left);
123 	}
124 	mutex_unlock(&us_ibdev->usdev_lock);
125 
126 	return ptr - buf;
127 }
128 static DEVICE_ATTR_RO(config);
129 
130 static ssize_t
131 iface_show(struct device *device, struct device_attribute *attr, char *buf)
132 {
133 	struct usnic_ib_dev *us_ibdev;
134 
135 	us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
136 
137 	return scnprintf(buf, PAGE_SIZE, "%s\n",
138 			netdev_name(us_ibdev->netdev));
139 }
140 static DEVICE_ATTR_RO(iface);
141 
142 static ssize_t
143 max_vf_show(struct device *device, struct device_attribute *attr, char *buf)
144 {
145 	struct usnic_ib_dev *us_ibdev;
146 
147 	us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
148 
149 	return scnprintf(buf, PAGE_SIZE, "%u\n",
150 			kref_read(&us_ibdev->vf_cnt));
151 }
152 static DEVICE_ATTR_RO(max_vf);
153 
154 static ssize_t
155 qp_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
156 {
157 	struct usnic_ib_dev *us_ibdev;
158 	int qp_per_vf;
159 
160 	us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
161 	qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
162 			us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
163 
164 	return scnprintf(buf, PAGE_SIZE,
165 				"%d\n", qp_per_vf);
166 }
167 static DEVICE_ATTR_RO(qp_per_vf);
168 
169 static ssize_t
170 cq_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
171 {
172 	struct usnic_ib_dev *us_ibdev;
173 
174 	us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
175 
176 	return scnprintf(buf, PAGE_SIZE, "%d\n",
177 			us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ]);
178 }
179 static DEVICE_ATTR_RO(cq_per_vf);
180 
181 static struct attribute *usnic_class_attributes[] = {
182 	&dev_attr_board_id.attr,
183 	&dev_attr_config.attr,
184 	&dev_attr_iface.attr,
185 	&dev_attr_max_vf.attr,
186 	&dev_attr_qp_per_vf.attr,
187 	&dev_attr_cq_per_vf.attr,
188 	NULL
189 };
190 
191 const struct attribute_group usnic_attr_group = {
192 	.attrs = usnic_class_attributes,
193 };
194 
195 struct qpn_attribute {
196 	struct attribute attr;
197 	ssize_t (*show)(struct usnic_ib_qp_grp *, char *buf);
198 };
199 
200 /*
201  * Definitions for supporting QPN entries in sysfs
202  */
203 static ssize_t
204 usnic_ib_qpn_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
205 {
206 	struct usnic_ib_qp_grp *qp_grp;
207 	struct qpn_attribute *qpn_attr;
208 
209 	qp_grp = container_of(kobj, struct usnic_ib_qp_grp, kobj);
210 	qpn_attr = container_of(attr, struct qpn_attribute, attr);
211 
212 	return qpn_attr->show(qp_grp, buf);
213 }
214 
215 static const struct sysfs_ops usnic_ib_qpn_sysfs_ops = {
216 	.show = usnic_ib_qpn_attr_show
217 };
218 
219 #define QPN_ATTR_RO(NAME) \
220 struct qpn_attribute qpn_attr_##NAME = __ATTR_RO(NAME)
221 
222 static ssize_t context_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
223 {
224 	return scnprintf(buf, PAGE_SIZE, "0x%p\n", qp_grp->ctx);
225 }
226 
227 static ssize_t summary_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
228 {
229 	int i, j, n;
230 	int left;
231 	char *ptr;
232 	struct usnic_vnic_res_chunk *res_chunk;
233 	struct usnic_vnic_res *vnic_res;
234 
235 	left = PAGE_SIZE;
236 	ptr = buf;
237 
238 	n = scnprintf(ptr, left,
239 			"QPN: %d State: (%s) PID: %u VF Idx: %hu ",
240 			qp_grp->ibqp.qp_num,
241 			usnic_ib_qp_grp_state_to_string(qp_grp->state),
242 			qp_grp->owner_pid,
243 			usnic_vnic_get_index(qp_grp->vf->vnic));
244 	UPDATE_PTR_LEFT(n, ptr, left);
245 
246 	for (i = 0; qp_grp->res_chunk_list[i]; i++) {
247 		res_chunk = qp_grp->res_chunk_list[i];
248 		for (j = 0; j < res_chunk->cnt; j++) {
249 			vnic_res = res_chunk->res[j];
250 			n = scnprintf(ptr, left, "%s[%d] ",
251 				usnic_vnic_res_type_to_str(vnic_res->type),
252 				vnic_res->vnic_idx);
253 			UPDATE_PTR_LEFT(n, ptr, left);
254 		}
255 	}
256 
257 	n = scnprintf(ptr, left, "\n");
258 	UPDATE_PTR_LEFT(n, ptr, left);
259 
260 	return ptr - buf;
261 }
262 
263 static QPN_ATTR_RO(context);
264 static QPN_ATTR_RO(summary);
265 
266 static struct attribute *usnic_ib_qpn_default_attrs[] = {
267 	&qpn_attr_context.attr,
268 	&qpn_attr_summary.attr,
269 	NULL
270 };
271 
272 static struct kobj_type usnic_ib_qpn_type = {
273 	.sysfs_ops = &usnic_ib_qpn_sysfs_ops,
274 	.default_attrs = usnic_ib_qpn_default_attrs
275 };
276 
277 int usnic_ib_sysfs_register_usdev(struct usnic_ib_dev *us_ibdev)
278 {
279 	/* create kernel object for looking at individual QPs */
280 	kobject_get(&us_ibdev->ib_dev.dev.kobj);
281 	us_ibdev->qpn_kobj = kobject_create_and_add("qpn",
282 			&us_ibdev->ib_dev.dev.kobj);
283 	if (us_ibdev->qpn_kobj == NULL) {
284 		kobject_put(&us_ibdev->ib_dev.dev.kobj);
285 		return -ENOMEM;
286 	}
287 
288 	return 0;
289 }
290 
291 void usnic_ib_sysfs_unregister_usdev(struct usnic_ib_dev *us_ibdev)
292 {
293 	kobject_put(us_ibdev->qpn_kobj);
294 }
295 
296 void usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp *qp_grp)
297 {
298 	struct usnic_ib_dev *us_ibdev;
299 	int err;
300 
301 	us_ibdev = qp_grp->vf->pf;
302 
303 	err = kobject_init_and_add(&qp_grp->kobj, &usnic_ib_qpn_type,
304 			kobject_get(us_ibdev->qpn_kobj),
305 			"%d", qp_grp->grp_id);
306 	if (err) {
307 		kobject_put(us_ibdev->qpn_kobj);
308 		return;
309 	}
310 }
311 
312 void usnic_ib_sysfs_qpn_remove(struct usnic_ib_qp_grp *qp_grp)
313 {
314 	struct usnic_ib_dev *us_ibdev;
315 
316 	us_ibdev = qp_grp->vf->pf;
317 
318 	kobject_put(&qp_grp->kobj);
319 	kobject_put(us_ibdev->qpn_kobj);
320 }
321