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_board(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 	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 
63 /*
64  * Report the configuration for this PF
65  */
66 static ssize_t
67 usnic_ib_show_config(struct device *device, struct device_attribute *attr,
68 			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 (atomic_read(&us_ibdev->vf_cnt.refcount) > 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 			us_ibdev->ib_dev.name,
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 			atomic_read(&us_ibdev->vf_cnt.refcount));
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 				us_ibdev->ib_dev.name);
122 		UPDATE_PTR_LEFT(n, ptr, left);
123 	}
124 	mutex_unlock(&us_ibdev->usdev_lock);
125 
126 	return ptr - buf;
127 }
128 
129 static ssize_t
130 usnic_ib_show_iface(struct device *device, struct device_attribute *attr,
131 			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 
141 static ssize_t
142 usnic_ib_show_max_vf(struct device *device, struct device_attribute *attr,
143 			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 			atomic_read(&us_ibdev->vf_cnt.refcount));
151 }
152 
153 static ssize_t
154 usnic_ib_show_qp_per_vf(struct device *device, struct device_attribute *attr,
155 			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 
168 static ssize_t
169 usnic_ib_show_cq_per_vf(struct device *device, struct device_attribute *attr,
170 			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 
180 static DEVICE_ATTR(board_id, S_IRUGO, usnic_ib_show_board, NULL);
181 static DEVICE_ATTR(config, S_IRUGO, usnic_ib_show_config, NULL);
182 static DEVICE_ATTR(iface, S_IRUGO, usnic_ib_show_iface, NULL);
183 static DEVICE_ATTR(max_vf, S_IRUGO, usnic_ib_show_max_vf, NULL);
184 static DEVICE_ATTR(qp_per_vf, S_IRUGO, usnic_ib_show_qp_per_vf, NULL);
185 static DEVICE_ATTR(cq_per_vf, S_IRUGO, usnic_ib_show_cq_per_vf, NULL);
186 
187 static struct device_attribute *usnic_class_attributes[] = {
188 	&dev_attr_board_id,
189 	&dev_attr_config,
190 	&dev_attr_iface,
191 	&dev_attr_max_vf,
192 	&dev_attr_qp_per_vf,
193 	&dev_attr_cq_per_vf,
194 };
195 
196 struct qpn_attribute {
197 	struct attribute attr;
198 	ssize_t (*show)(struct usnic_ib_qp_grp *, char *buf);
199 };
200 
201 /*
202  * Definitions for supporting QPN entries in sysfs
203  */
204 static ssize_t
205 usnic_ib_qpn_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
206 {
207 	struct usnic_ib_qp_grp *qp_grp;
208 	struct qpn_attribute *qpn_attr;
209 
210 	qp_grp = container_of(kobj, struct usnic_ib_qp_grp, kobj);
211 	qpn_attr = container_of(attr, struct qpn_attribute, attr);
212 
213 	return qpn_attr->show(qp_grp, buf);
214 }
215 
216 static const struct sysfs_ops usnic_ib_qpn_sysfs_ops = {
217 	.show = usnic_ib_qpn_attr_show
218 };
219 
220 #define QPN_ATTR_RO(NAME) \
221 struct qpn_attribute qpn_attr_##NAME = __ATTR_RO(NAME)
222 
223 static ssize_t context_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
224 {
225 	return scnprintf(buf, PAGE_SIZE, "0x%p\n", qp_grp->ctx);
226 }
227 
228 static ssize_t summary_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
229 {
230 	int i, j, n;
231 	int left;
232 	char *ptr;
233 	struct usnic_vnic_res_chunk *res_chunk;
234 	struct usnic_vnic_res *vnic_res;
235 
236 	left = PAGE_SIZE;
237 	ptr = buf;
238 
239 	n = scnprintf(ptr, left,
240 			"QPN: %d State: (%s) PID: %u VF Idx: %hu ",
241 			qp_grp->ibqp.qp_num,
242 			usnic_ib_qp_grp_state_to_string(qp_grp->state),
243 			qp_grp->owner_pid,
244 			usnic_vnic_get_index(qp_grp->vf->vnic));
245 	UPDATE_PTR_LEFT(n, ptr, left);
246 
247 	for (i = 0; qp_grp->res_chunk_list[i]; i++) {
248 		res_chunk = qp_grp->res_chunk_list[i];
249 		for (j = 0; j < res_chunk->cnt; j++) {
250 			vnic_res = res_chunk->res[j];
251 			n = scnprintf(ptr, left, "%s[%d] ",
252 				usnic_vnic_res_type_to_str(vnic_res->type),
253 				vnic_res->vnic_idx);
254 			UPDATE_PTR_LEFT(n, ptr, left);
255 		}
256 	}
257 
258 	n = scnprintf(ptr, left, "\n");
259 	UPDATE_PTR_LEFT(n, ptr, left);
260 
261 	return ptr - buf;
262 }
263 
264 static QPN_ATTR_RO(context);
265 static QPN_ATTR_RO(summary);
266 
267 static struct attribute *usnic_ib_qpn_default_attrs[] = {
268 	&qpn_attr_context.attr,
269 	&qpn_attr_summary.attr,
270 	NULL
271 };
272 
273 static struct kobj_type usnic_ib_qpn_type = {
274 	.sysfs_ops = &usnic_ib_qpn_sysfs_ops,
275 	.default_attrs = usnic_ib_qpn_default_attrs
276 };
277 
278 int usnic_ib_sysfs_register_usdev(struct usnic_ib_dev *us_ibdev)
279 {
280 	int i;
281 	int err;
282 	for (i = 0; i < ARRAY_SIZE(usnic_class_attributes); ++i) {
283 		err = device_create_file(&us_ibdev->ib_dev.dev,
284 						usnic_class_attributes[i]);
285 		if (err) {
286 			usnic_err("Failed to create device file %d for %s eith err %d",
287 				i, us_ibdev->ib_dev.name, err);
288 			return -EINVAL;
289 		}
290 	}
291 
292 	/* create kernel object for looking at individual QPs */
293 	kobject_get(&us_ibdev->ib_dev.dev.kobj);
294 	us_ibdev->qpn_kobj = kobject_create_and_add("qpn",
295 			&us_ibdev->ib_dev.dev.kobj);
296 	if (us_ibdev->qpn_kobj == NULL) {
297 		kobject_put(&us_ibdev->ib_dev.dev.kobj);
298 		return -ENOMEM;
299 	}
300 
301 	return 0;
302 }
303 
304 void usnic_ib_sysfs_unregister_usdev(struct usnic_ib_dev *us_ibdev)
305 {
306 	int i;
307 	for (i = 0; i < ARRAY_SIZE(usnic_class_attributes); ++i) {
308 		device_remove_file(&us_ibdev->ib_dev.dev,
309 					usnic_class_attributes[i]);
310 	}
311 
312 	kobject_put(us_ibdev->qpn_kobj);
313 }
314 
315 void usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp *qp_grp)
316 {
317 	struct usnic_ib_dev *us_ibdev;
318 	int err;
319 
320 	us_ibdev = qp_grp->vf->pf;
321 
322 	err = kobject_init_and_add(&qp_grp->kobj, &usnic_ib_qpn_type,
323 			kobject_get(us_ibdev->qpn_kobj),
324 			"%d", qp_grp->grp_id);
325 	if (err) {
326 		kobject_put(us_ibdev->qpn_kobj);
327 		return;
328 	}
329 }
330 
331 void usnic_ib_sysfs_qpn_remove(struct usnic_ib_qp_grp *qp_grp)
332 {
333 	struct usnic_ib_dev *us_ibdev;
334 
335 	us_ibdev = qp_grp->vf->pf;
336 
337 	kobject_put(&qp_grp->kobj);
338 	kobject_put(us_ibdev->qpn_kobj);
339 }
340