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