1e3cf00d0SUpinder Malhi /*
2e3cf00d0SUpinder Malhi * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3e3cf00d0SUpinder Malhi *
43805eadeSJeff Squyres * This software is available to you under a choice of one of two
53805eadeSJeff Squyres * licenses. You may choose to be licensed under the terms of the GNU
63805eadeSJeff Squyres * General Public License (GPL) Version 2, available from the file
73805eadeSJeff Squyres * COPYING in the main directory of this source tree, or the
83805eadeSJeff Squyres * BSD license below:
93805eadeSJeff Squyres *
103805eadeSJeff Squyres * Redistribution and use in source and binary forms, with or
113805eadeSJeff Squyres * without modification, are permitted provided that the following
123805eadeSJeff Squyres * conditions are met:
133805eadeSJeff Squyres *
143805eadeSJeff Squyres * - Redistributions of source code must retain the above
153805eadeSJeff Squyres * copyright notice, this list of conditions and the following
163805eadeSJeff Squyres * disclaimer.
173805eadeSJeff Squyres *
183805eadeSJeff Squyres * - Redistributions in binary form must reproduce the above
193805eadeSJeff Squyres * copyright notice, this list of conditions and the following
203805eadeSJeff Squyres * disclaimer in the documentation and/or other materials
213805eadeSJeff Squyres * provided with the distribution.
22e3cf00d0SUpinder Malhi *
23e3cf00d0SUpinder Malhi * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e3cf00d0SUpinder Malhi * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e3cf00d0SUpinder Malhi * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e3cf00d0SUpinder Malhi * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e3cf00d0SUpinder Malhi * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e3cf00d0SUpinder Malhi * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e3cf00d0SUpinder Malhi * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e3cf00d0SUpinder Malhi * SOFTWARE.
31e3cf00d0SUpinder Malhi *
32e3cf00d0SUpinder Malhi */
33e3cf00d0SUpinder Malhi
34e3cf00d0SUpinder Malhi #include <linux/init.h>
35e3cf00d0SUpinder Malhi #include <linux/errno.h>
36e3cf00d0SUpinder Malhi
37e3cf00d0SUpinder Malhi #include <rdma/ib_user_verbs.h>
38e3cf00d0SUpinder Malhi #include <rdma/ib_addr.h>
39e3cf00d0SUpinder Malhi
40e3cf00d0SUpinder Malhi #include "usnic_common_util.h"
41e3cf00d0SUpinder Malhi #include "usnic_ib.h"
42e3cf00d0SUpinder Malhi #include "usnic_ib_qp_grp.h"
43e3cf00d0SUpinder Malhi #include "usnic_vnic.h"
44e3cf00d0SUpinder Malhi #include "usnic_ib_verbs.h"
4519188436SBart Van Assche #include "usnic_ib_sysfs.h"
46e3cf00d0SUpinder Malhi #include "usnic_log.h"
47e3cf00d0SUpinder Malhi
board_id_show(struct device * device,struct device_attribute * attr,char * buf)48508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
49508a523fSParav Pandit struct device_attribute *attr, char *buf)
50e3cf00d0SUpinder Malhi {
51e3cf00d0SUpinder Malhi struct usnic_ib_dev *us_ibdev =
5254747231SParav Pandit rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
53e3cf00d0SUpinder Malhi unsigned short subsystem_device_id;
54e3cf00d0SUpinder Malhi
55e3cf00d0SUpinder Malhi mutex_lock(&us_ibdev->usdev_lock);
56e3cf00d0SUpinder Malhi subsystem_device_id = us_ibdev->pdev->subsystem_device;
57e3cf00d0SUpinder Malhi mutex_unlock(&us_ibdev->usdev_lock);
58e3cf00d0SUpinder Malhi
5945808361SJoe Perches return sysfs_emit(buf, "%u\n", subsystem_device_id);
60e3cf00d0SUpinder Malhi }
61508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
62e3cf00d0SUpinder Malhi
63e3cf00d0SUpinder Malhi /*
64e3cf00d0SUpinder Malhi * Report the configuration for this PF
65e3cf00d0SUpinder Malhi */
66e3cf00d0SUpinder Malhi static ssize_t
config_show(struct device * device,struct device_attribute * attr,char * buf)67508a523fSParav Pandit config_show(struct device *device, struct device_attribute *attr, char *buf)
68e3cf00d0SUpinder Malhi {
6954747231SParav Pandit struct usnic_ib_dev *us_ibdev =
7054747231SParav Pandit rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
71e3cf00d0SUpinder Malhi enum usnic_vnic_res_type res_type;
7245808361SJoe Perches int len;
73e3cf00d0SUpinder Malhi
74e3cf00d0SUpinder Malhi mutex_lock(&us_ibdev->usdev_lock);
752c935bc5SPeter Zijlstra if (kref_read(&us_ibdev->vf_cnt) > 0) {
76e3cf00d0SUpinder Malhi char *busname;
7745808361SJoe Perches char *sep = "";
78e3cf00d0SUpinder Malhi /*
79e3cf00d0SUpinder Malhi * bus name seems to come with annoying prefix.
80e3cf00d0SUpinder Malhi * Remove it if it is predictable
81e3cf00d0SUpinder Malhi */
82e3cf00d0SUpinder Malhi busname = us_ibdev->pdev->bus->name;
83e3cf00d0SUpinder Malhi if (strncmp(busname, "PCI Bus ", 8) == 0)
84e3cf00d0SUpinder Malhi busname += 8;
85e3cf00d0SUpinder Malhi
8645808361SJoe Perches len = sysfs_emit(buf, "%s: %s:%d.%d, %s, %pM, %u VFs\n",
879de69861SJason Gunthorpe dev_name(&us_ibdev->ib_dev.dev),
88e3cf00d0SUpinder Malhi busname,
89e3cf00d0SUpinder Malhi PCI_SLOT(us_ibdev->pdev->devfn),
90e3cf00d0SUpinder Malhi PCI_FUNC(us_ibdev->pdev->devfn),
91e3cf00d0SUpinder Malhi netdev_name(us_ibdev->netdev),
9260b215e8SUpinder Malhi us_ibdev->ufdev->mac,
932c935bc5SPeter Zijlstra kref_read(&us_ibdev->vf_cnt));
94e3cf00d0SUpinder Malhi
9545808361SJoe Perches len += sysfs_emit_at(buf, len, " Per VF:");
96e3cf00d0SUpinder Malhi for (res_type = USNIC_VNIC_RES_TYPE_EOL;
9745808361SJoe Perches res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++) {
98e3cf00d0SUpinder Malhi if (us_ibdev->vf_res_cnt[res_type] == 0)
99e3cf00d0SUpinder Malhi continue;
10045808361SJoe Perches len += sysfs_emit_at(buf, len, "%s %d %s",
10145808361SJoe Perches sep,
102e3cf00d0SUpinder Malhi us_ibdev->vf_res_cnt[res_type],
10345808361SJoe Perches usnic_vnic_res_type_to_str(res_type));
10445808361SJoe Perches sep = ",";
105e3cf00d0SUpinder Malhi }
10645808361SJoe Perches len += sysfs_emit_at(buf, len, "\n");
107e3cf00d0SUpinder Malhi } else {
10845808361SJoe Perches len = sysfs_emit(buf, "%s: no VFs\n",
1099de69861SJason Gunthorpe dev_name(&us_ibdev->ib_dev.dev));
110e3cf00d0SUpinder Malhi }
11145808361SJoe Perches
112e3cf00d0SUpinder Malhi mutex_unlock(&us_ibdev->usdev_lock);
113e3cf00d0SUpinder Malhi
11445808361SJoe Perches return len;
115e3cf00d0SUpinder Malhi }
116508a523fSParav Pandit static DEVICE_ATTR_RO(config);
117e3cf00d0SUpinder Malhi
118e3cf00d0SUpinder Malhi static ssize_t
iface_show(struct device * device,struct device_attribute * attr,char * buf)119508a523fSParav Pandit iface_show(struct device *device, struct device_attribute *attr, char *buf)
120e3cf00d0SUpinder Malhi {
12154747231SParav Pandit struct usnic_ib_dev *us_ibdev =
12254747231SParav Pandit rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
123e3cf00d0SUpinder Malhi
1241c7fd726SJoe Perches return sysfs_emit(buf, "%s\n", netdev_name(us_ibdev->netdev));
125e3cf00d0SUpinder Malhi }
126508a523fSParav Pandit static DEVICE_ATTR_RO(iface);
127e3cf00d0SUpinder Malhi
128e3cf00d0SUpinder Malhi static ssize_t
max_vf_show(struct device * device,struct device_attribute * attr,char * buf)129508a523fSParav Pandit max_vf_show(struct device *device, struct device_attribute *attr, char *buf)
130e3cf00d0SUpinder Malhi {
13154747231SParav Pandit struct usnic_ib_dev *us_ibdev =
13254747231SParav Pandit rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
133e3cf00d0SUpinder Malhi
1341c7fd726SJoe Perches return sysfs_emit(buf, "%u\n", kref_read(&us_ibdev->vf_cnt));
135e3cf00d0SUpinder Malhi }
136508a523fSParav Pandit static DEVICE_ATTR_RO(max_vf);
137e3cf00d0SUpinder Malhi
138e3cf00d0SUpinder Malhi static ssize_t
qp_per_vf_show(struct device * device,struct device_attribute * attr,char * buf)139508a523fSParav Pandit qp_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
140e3cf00d0SUpinder Malhi {
14154747231SParav Pandit struct usnic_ib_dev *us_ibdev =
14254747231SParav Pandit rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
143e3cf00d0SUpinder Malhi int qp_per_vf;
144e3cf00d0SUpinder Malhi
145e3cf00d0SUpinder Malhi qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
146e3cf00d0SUpinder Malhi us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
147e3cf00d0SUpinder Malhi
1481c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", qp_per_vf);
149e3cf00d0SUpinder Malhi }
150508a523fSParav Pandit static DEVICE_ATTR_RO(qp_per_vf);
151e3cf00d0SUpinder Malhi
152e3cf00d0SUpinder Malhi static ssize_t
cq_per_vf_show(struct device * device,struct device_attribute * attr,char * buf)153508a523fSParav Pandit cq_per_vf_show(struct device *device, struct device_attribute *attr, char *buf)
154e3cf00d0SUpinder Malhi {
15554747231SParav Pandit struct usnic_ib_dev *us_ibdev =
15654747231SParav Pandit rdma_device_to_drv_device(device, struct usnic_ib_dev, ib_dev);
157e3cf00d0SUpinder Malhi
1581c7fd726SJoe Perches return sysfs_emit(buf, "%d\n",
159e3cf00d0SUpinder Malhi us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ]);
160e3cf00d0SUpinder Malhi }
161508a523fSParav Pandit static DEVICE_ATTR_RO(cq_per_vf);
162e3cf00d0SUpinder Malhi
163508a523fSParav Pandit static struct attribute *usnic_class_attributes[] = {
164508a523fSParav Pandit &dev_attr_board_id.attr,
165508a523fSParav Pandit &dev_attr_config.attr,
166508a523fSParav Pandit &dev_attr_iface.attr,
167508a523fSParav Pandit &dev_attr_max_vf.attr,
168508a523fSParav Pandit &dev_attr_qp_per_vf.attr,
169508a523fSParav Pandit &dev_attr_cq_per_vf.attr,
170508a523fSParav Pandit NULL
171508a523fSParav Pandit };
172e3cf00d0SUpinder Malhi
173508a523fSParav Pandit const struct attribute_group usnic_attr_group = {
174508a523fSParav Pandit .attrs = usnic_class_attributes,
175e3cf00d0SUpinder Malhi };
176e3cf00d0SUpinder Malhi
177e3cf00d0SUpinder Malhi struct qpn_attribute {
178e3cf00d0SUpinder Malhi struct attribute attr;
179e3cf00d0SUpinder Malhi ssize_t (*show)(struct usnic_ib_qp_grp *, char *buf);
180e3cf00d0SUpinder Malhi };
181e3cf00d0SUpinder Malhi
182e3cf00d0SUpinder Malhi /*
183e3cf00d0SUpinder Malhi * Definitions for supporting QPN entries in sysfs
184e3cf00d0SUpinder Malhi */
185e3cf00d0SUpinder Malhi static ssize_t
usnic_ib_qpn_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)186e3cf00d0SUpinder Malhi usnic_ib_qpn_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
187e3cf00d0SUpinder Malhi {
188e3cf00d0SUpinder Malhi struct usnic_ib_qp_grp *qp_grp;
189e3cf00d0SUpinder Malhi struct qpn_attribute *qpn_attr;
190e3cf00d0SUpinder Malhi
191e3cf00d0SUpinder Malhi qp_grp = container_of(kobj, struct usnic_ib_qp_grp, kobj);
192e3cf00d0SUpinder Malhi qpn_attr = container_of(attr, struct qpn_attribute, attr);
193e3cf00d0SUpinder Malhi
194e3cf00d0SUpinder Malhi return qpn_attr->show(qp_grp, buf);
195e3cf00d0SUpinder Malhi }
196e3cf00d0SUpinder Malhi
197e3cf00d0SUpinder Malhi static const struct sysfs_ops usnic_ib_qpn_sysfs_ops = {
198e3cf00d0SUpinder Malhi .show = usnic_ib_qpn_attr_show
199e3cf00d0SUpinder Malhi };
200e3cf00d0SUpinder Malhi
201e3cf00d0SUpinder Malhi #define QPN_ATTR_RO(NAME) \
202e3cf00d0SUpinder Malhi struct qpn_attribute qpn_attr_##NAME = __ATTR_RO(NAME)
203e3cf00d0SUpinder Malhi
context_show(struct usnic_ib_qp_grp * qp_grp,char * buf)204e3cf00d0SUpinder Malhi static ssize_t context_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
205e3cf00d0SUpinder Malhi {
206e28bf1f0SJoe Perches return sysfs_emit(buf, "0x%p\n", qp_grp->ctx);
207e3cf00d0SUpinder Malhi }
208e3cf00d0SUpinder Malhi
summary_show(struct usnic_ib_qp_grp * qp_grp,char * buf)209e3cf00d0SUpinder Malhi static ssize_t summary_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
210e3cf00d0SUpinder Malhi {
211e28bf1f0SJoe Perches int i, j;
212e3cf00d0SUpinder Malhi struct usnic_vnic_res_chunk *res_chunk;
213e3cf00d0SUpinder Malhi struct usnic_vnic_res *vnic_res;
214e28bf1f0SJoe Perches int len;
215e3cf00d0SUpinder Malhi
216e28bf1f0SJoe Perches len = sysfs_emit(buf, "QPN: %d State: (%s) PID: %u VF Idx: %hu",
217e3cf00d0SUpinder Malhi qp_grp->ibqp.qp_num,
218e3cf00d0SUpinder Malhi usnic_ib_qp_grp_state_to_string(qp_grp->state),
219e3cf00d0SUpinder Malhi qp_grp->owner_pid,
22060b215e8SUpinder Malhi usnic_vnic_get_index(qp_grp->vf->vnic));
221e3cf00d0SUpinder Malhi
222e3cf00d0SUpinder Malhi for (i = 0; qp_grp->res_chunk_list[i]; i++) {
223e3cf00d0SUpinder Malhi res_chunk = qp_grp->res_chunk_list[i];
224e3cf00d0SUpinder Malhi for (j = 0; j < res_chunk->cnt; j++) {
225e3cf00d0SUpinder Malhi vnic_res = res_chunk->res[j];
226f068cb1dSJoe Perches len += sysfs_emit_at(buf, len, " %s[%d]",
227e3cf00d0SUpinder Malhi usnic_vnic_res_type_to_str(vnic_res->type),
228e3cf00d0SUpinder Malhi vnic_res->vnic_idx);
229e3cf00d0SUpinder Malhi }
230e3cf00d0SUpinder Malhi }
231e3cf00d0SUpinder Malhi
232f068cb1dSJoe Perches len += sysfs_emit_at(buf, len, "\n");
233e3cf00d0SUpinder Malhi
234e28bf1f0SJoe Perches return len;
235e3cf00d0SUpinder Malhi }
236e3cf00d0SUpinder Malhi
237e3cf00d0SUpinder Malhi static QPN_ATTR_RO(context);
238e3cf00d0SUpinder Malhi static QPN_ATTR_RO(summary);
239e3cf00d0SUpinder Malhi
240e3cf00d0SUpinder Malhi static struct attribute *usnic_ib_qpn_default_attrs[] = {
241e3cf00d0SUpinder Malhi &qpn_attr_context.attr,
242e3cf00d0SUpinder Malhi &qpn_attr_summary.attr,
243e3cf00d0SUpinder Malhi NULL
244e3cf00d0SUpinder Malhi };
245*01097139SGreg Kroah-Hartman ATTRIBUTE_GROUPS(usnic_ib_qpn_default);
246e3cf00d0SUpinder Malhi
247e3cf00d0SUpinder Malhi static struct kobj_type usnic_ib_qpn_type = {
248e3cf00d0SUpinder Malhi .sysfs_ops = &usnic_ib_qpn_sysfs_ops,
249*01097139SGreg Kroah-Hartman .default_groups = usnic_ib_qpn_default_groups,
250e3cf00d0SUpinder Malhi };
251e3cf00d0SUpinder Malhi
usnic_ib_sysfs_register_usdev(struct usnic_ib_dev * us_ibdev)252e3cf00d0SUpinder Malhi int usnic_ib_sysfs_register_usdev(struct usnic_ib_dev *us_ibdev)
253e3cf00d0SUpinder Malhi {
254e3cf00d0SUpinder Malhi /* create kernel object for looking at individual QPs */
255e3cf00d0SUpinder Malhi kobject_get(&us_ibdev->ib_dev.dev.kobj);
256e3cf00d0SUpinder Malhi us_ibdev->qpn_kobj = kobject_create_and_add("qpn",
257e3cf00d0SUpinder Malhi &us_ibdev->ib_dev.dev.kobj);
258e3cf00d0SUpinder Malhi if (us_ibdev->qpn_kobj == NULL) {
259e3cf00d0SUpinder Malhi kobject_put(&us_ibdev->ib_dev.dev.kobj);
260e3cf00d0SUpinder Malhi return -ENOMEM;
261e3cf00d0SUpinder Malhi }
262e3cf00d0SUpinder Malhi
263e3cf00d0SUpinder Malhi return 0;
264e3cf00d0SUpinder Malhi }
265e3cf00d0SUpinder Malhi
usnic_ib_sysfs_unregister_usdev(struct usnic_ib_dev * us_ibdev)266e3cf00d0SUpinder Malhi void usnic_ib_sysfs_unregister_usdev(struct usnic_ib_dev *us_ibdev)
267e3cf00d0SUpinder Malhi {
268e3cf00d0SUpinder Malhi kobject_put(us_ibdev->qpn_kobj);
269e3cf00d0SUpinder Malhi }
270e3cf00d0SUpinder Malhi
usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp * qp_grp)271e3cf00d0SUpinder Malhi void usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp *qp_grp)
272e3cf00d0SUpinder Malhi {
273e3cf00d0SUpinder Malhi struct usnic_ib_dev *us_ibdev;
274e3cf00d0SUpinder Malhi int err;
275e3cf00d0SUpinder Malhi
276e3cf00d0SUpinder Malhi us_ibdev = qp_grp->vf->pf;
277e3cf00d0SUpinder Malhi
278e3cf00d0SUpinder Malhi err = kobject_init_and_add(&qp_grp->kobj, &usnic_ib_qpn_type,
279e3cf00d0SUpinder Malhi kobject_get(us_ibdev->qpn_kobj),
280e3cf00d0SUpinder Malhi "%d", qp_grp->grp_id);
281e3cf00d0SUpinder Malhi if (err) {
282e3cf00d0SUpinder Malhi kobject_put(us_ibdev->qpn_kobj);
283e3cf00d0SUpinder Malhi return;
284e3cf00d0SUpinder Malhi }
285e3cf00d0SUpinder Malhi }
286e3cf00d0SUpinder Malhi
usnic_ib_sysfs_qpn_remove(struct usnic_ib_qp_grp * qp_grp)287e3cf00d0SUpinder Malhi void usnic_ib_sysfs_qpn_remove(struct usnic_ib_qp_grp *qp_grp)
288e3cf00d0SUpinder Malhi {
289e3cf00d0SUpinder Malhi struct usnic_ib_dev *us_ibdev;
290e3cf00d0SUpinder Malhi
291e3cf00d0SUpinder Malhi us_ibdev = qp_grp->vf->pf;
292e3cf00d0SUpinder Malhi
293e3cf00d0SUpinder Malhi kobject_put(&qp_grp->kobj);
294e3cf00d0SUpinder Malhi kobject_put(us_ibdev->qpn_kobj);
295e3cf00d0SUpinder Malhi }
296