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