xref: /openbmc/linux/drivers/infiniband/core/sysfs.c (revision 119d4350)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
32a1d9b7fSRoland Dreier  * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
42a1d9b7fSRoland Dreier  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This software is available to you under a choice of one of two
71da177e4SLinus Torvalds  * licenses.  You may choose to be licensed under the terms of the GNU
81da177e4SLinus Torvalds  * General Public License (GPL) Version 2, available from the file
91da177e4SLinus Torvalds  * COPYING in the main directory of this source tree, or the
101da177e4SLinus Torvalds  * OpenIB.org BSD license below:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *     Redistribution and use in source and binary forms, with or
131da177e4SLinus Torvalds  *     without modification, are permitted provided that the following
141da177e4SLinus Torvalds  *     conditions are met:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *      - Redistributions of source code must retain the above
171da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
181da177e4SLinus Torvalds  *        disclaimer.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *      - Redistributions in binary form must reproduce the above
211da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
221da177e4SLinus Torvalds  *        disclaimer in the documentation and/or other materials
231da177e4SLinus Torvalds  *        provided with the distribution.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
261da177e4SLinus Torvalds  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
271da177e4SLinus Torvalds  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
281da177e4SLinus Torvalds  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
291da177e4SLinus Torvalds  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
301da177e4SLinus Torvalds  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
311da177e4SLinus Torvalds  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
321da177e4SLinus Torvalds  * SOFTWARE.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "core_priv.h"
361da177e4SLinus Torvalds 
378c65b4a6STim Schmielau #include <linux/slab.h>
38fc87af74SPaul Gortmaker #include <linux/stat.h>
398c65b4a6STim Schmielau #include <linux/string.h>
40470be516SMatan Barak #include <linux/netdevice.h>
4141a6ae1eSIra Weiny #include <linux/ethtool.h>
428c65b4a6STim Schmielau 
43a4d61e84SRoland Dreier #include <rdma/ib_mad.h>
44b2788ce5SChristoph Lameter #include <rdma/ib_pma.h>
4577e786fcSParav Pandit #include <rdma/ib_cache.h>
46f34a55e4SMark Zhang #include <rdma/rdma_counter.h>
47054239f4SJason Gunthorpe #include <rdma/ib_sysfs.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds struct port_table_attribute {
50054239f4SJason Gunthorpe 	struct ib_port_attribute attr;
51d48593bfSDmitry Torokhov 	char			name[8];
521da177e4SLinus Torvalds 	int			index;
5365487fdcSIra Weiny 	__be16			attr_id;
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds 
56a4676388SJason Gunthorpe struct gid_attr_group {
57a4676388SJason Gunthorpe 	struct ib_port *port;
58a4676388SJason Gunthorpe 	struct kobject kobj;
59a4676388SJason Gunthorpe 	struct attribute_group groups[2];
60a4676388SJason Gunthorpe 	const struct attribute_group *groups_list[3];
61a4676388SJason Gunthorpe 	struct port_table_attribute attrs_list[];
62a4676388SJason Gunthorpe };
63a4676388SJason Gunthorpe 
64a4676388SJason Gunthorpe struct ib_port {
65a4676388SJason Gunthorpe 	struct kobject kobj;
66a4676388SJason Gunthorpe 	struct ib_device *ibdev;
67a4676388SJason Gunthorpe 	struct gid_attr_group *gid_attr_group;
68a4676388SJason Gunthorpe 	struct hw_stats_port_data *hw_stats_data;
692ca1cca4SJason Gunthorpe 
702ca1cca4SJason Gunthorpe 	struct attribute_group groups[3];
712ca1cca4SJason Gunthorpe 	const struct attribute_group *groups_list[5];
72a4676388SJason Gunthorpe 	u32 port_num;
732ca1cca4SJason Gunthorpe 	struct port_table_attribute attrs_list[];
74a4676388SJason Gunthorpe };
75a4676388SJason Gunthorpe 
76467f432aSJason Gunthorpe struct hw_stats_device_attribute {
77467f432aSJason Gunthorpe 	struct device_attribute attr;
78467f432aSJason Gunthorpe 	ssize_t (*show)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
79467f432aSJason Gunthorpe 			unsigned int index, unsigned int port_num, char *buf);
80467f432aSJason Gunthorpe 	ssize_t (*store)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
81467f432aSJason Gunthorpe 			 unsigned int index, unsigned int port_num,
82467f432aSJason Gunthorpe 			 const char *buf, size_t count);
83467f432aSJason Gunthorpe };
84467f432aSJason Gunthorpe 
85467f432aSJason Gunthorpe struct hw_stats_port_attribute {
86054239f4SJason Gunthorpe 	struct ib_port_attribute attr;
87467f432aSJason Gunthorpe 	ssize_t (*show)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
88467f432aSJason Gunthorpe 			unsigned int index, unsigned int port_num, char *buf);
89467f432aSJason Gunthorpe 	ssize_t (*store)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
90467f432aSJason Gunthorpe 			 unsigned int index, unsigned int port_num,
91467f432aSJason Gunthorpe 			 const char *buf, size_t count);
92467f432aSJason Gunthorpe };
93467f432aSJason Gunthorpe 
94467f432aSJason Gunthorpe struct hw_stats_device_data {
95467f432aSJason Gunthorpe 	struct attribute_group group;
96467f432aSJason Gunthorpe 	struct rdma_hw_stats *stats;
97467f432aSJason Gunthorpe 	struct hw_stats_device_attribute attrs[];
98467f432aSJason Gunthorpe };
99467f432aSJason Gunthorpe 
100467f432aSJason Gunthorpe struct hw_stats_port_data {
101467f432aSJason Gunthorpe 	struct rdma_hw_stats *stats;
102467f432aSJason Gunthorpe 	struct hw_stats_port_attribute attrs[];
103b40f4757SChristoph Lameter };
104b40f4757SChristoph Lameter 
port_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1051da177e4SLinus Torvalds static ssize_t port_attr_show(struct kobject *kobj,
1061da177e4SLinus Torvalds 			      struct attribute *attr, char *buf)
1071da177e4SLinus Torvalds {
108054239f4SJason Gunthorpe 	struct ib_port_attribute *port_attr =
109054239f4SJason Gunthorpe 		container_of(attr, struct ib_port_attribute, attr);
1101da177e4SLinus Torvalds 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	if (!port_attr->show)
11370f2817aSDmitry Torokhov 		return -EIO;
1141da177e4SLinus Torvalds 
115054239f4SJason Gunthorpe 	return port_attr->show(p->ibdev, p->port_num, port_attr, buf);
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
port_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)11879c4d80bSParav Pandit static ssize_t port_attr_store(struct kobject *kobj,
11979c4d80bSParav Pandit 			       struct attribute *attr,
12079c4d80bSParav Pandit 			       const char *buf, size_t count)
12179c4d80bSParav Pandit {
122054239f4SJason Gunthorpe 	struct ib_port_attribute *port_attr =
123054239f4SJason Gunthorpe 		container_of(attr, struct ib_port_attribute, attr);
12479c4d80bSParav Pandit 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
12579c4d80bSParav Pandit 
12679c4d80bSParav Pandit 	if (!port_attr->store)
12779c4d80bSParav Pandit 		return -EIO;
128054239f4SJason Gunthorpe 	return port_attr->store(p->ibdev, p->port_num, port_attr, buf, count);
12979c4d80bSParav Pandit }
13079c4d80bSParav Pandit 
ib_port_sysfs_get_ibdev_kobj(struct kobject * kobj,u32 * port_num)131054239f4SJason Gunthorpe struct ib_device *ib_port_sysfs_get_ibdev_kobj(struct kobject *kobj,
132054239f4SJason Gunthorpe 					       u32 *port_num)
133054239f4SJason Gunthorpe {
134054239f4SJason Gunthorpe 	struct ib_port *port = container_of(kobj, struct ib_port, kobj);
135054239f4SJason Gunthorpe 
136054239f4SJason Gunthorpe 	*port_num = port->port_num;
137054239f4SJason Gunthorpe 	return port->ibdev;
138054239f4SJason Gunthorpe }
139054239f4SJason Gunthorpe EXPORT_SYMBOL(ib_port_sysfs_get_ibdev_kobj);
140054239f4SJason Gunthorpe 
14152cf25d0SEmese Revfy static const struct sysfs_ops port_sysfs_ops = {
14279c4d80bSParav Pandit 	.show	= port_attr_show,
14379c4d80bSParav Pandit 	.store	= port_attr_store
1441da177e4SLinus Torvalds };
1451da177e4SLinus Torvalds 
hw_stat_device_show(struct device * dev,struct device_attribute * attr,char * buf)146467f432aSJason Gunthorpe static ssize_t hw_stat_device_show(struct device *dev,
147467f432aSJason Gunthorpe 				   struct device_attribute *attr, char *buf)
148467f432aSJason Gunthorpe {
149467f432aSJason Gunthorpe 	struct hw_stats_device_attribute *stat_attr =
150467f432aSJason Gunthorpe 		container_of(attr, struct hw_stats_device_attribute, attr);
151467f432aSJason Gunthorpe 	struct ib_device *ibdev = container_of(dev, struct ib_device, dev);
152467f432aSJason Gunthorpe 
153467f432aSJason Gunthorpe 	return stat_attr->show(ibdev, ibdev->hw_stats_data->stats,
154467f432aSJason Gunthorpe 			       stat_attr - ibdev->hw_stats_data->attrs, 0, buf);
155467f432aSJason Gunthorpe }
156467f432aSJason Gunthorpe 
hw_stat_device_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)157467f432aSJason Gunthorpe static ssize_t hw_stat_device_store(struct device *dev,
158467f432aSJason Gunthorpe 				    struct device_attribute *attr,
159467f432aSJason Gunthorpe 				    const char *buf, size_t count)
160467f432aSJason Gunthorpe {
161467f432aSJason Gunthorpe 	struct hw_stats_device_attribute *stat_attr =
162467f432aSJason Gunthorpe 		container_of(attr, struct hw_stats_device_attribute, attr);
163467f432aSJason Gunthorpe 	struct ib_device *ibdev = container_of(dev, struct ib_device, dev);
164467f432aSJason Gunthorpe 
165467f432aSJason Gunthorpe 	return stat_attr->store(ibdev, ibdev->hw_stats_data->stats,
166467f432aSJason Gunthorpe 				stat_attr - ibdev->hw_stats_data->attrs, 0, buf,
167467f432aSJason Gunthorpe 				count);
168467f432aSJason Gunthorpe }
169467f432aSJason Gunthorpe 
hw_stat_port_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf)170054239f4SJason Gunthorpe static ssize_t hw_stat_port_show(struct ib_device *ibdev, u32 port_num,
171054239f4SJason Gunthorpe 				 struct ib_port_attribute *attr, char *buf)
172467f432aSJason Gunthorpe {
173467f432aSJason Gunthorpe 	struct hw_stats_port_attribute *stat_attr =
174467f432aSJason Gunthorpe 		container_of(attr, struct hw_stats_port_attribute, attr);
175054239f4SJason Gunthorpe 	struct ib_port *port = ibdev->port_data[port_num].sysfs;
176467f432aSJason Gunthorpe 
177054239f4SJason Gunthorpe 	return stat_attr->show(ibdev, port->hw_stats_data->stats,
178467f432aSJason Gunthorpe 			       stat_attr - port->hw_stats_data->attrs,
179467f432aSJason Gunthorpe 			       port->port_num, buf);
180467f432aSJason Gunthorpe }
181467f432aSJason Gunthorpe 
hw_stat_port_store(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,const char * buf,size_t count)182054239f4SJason Gunthorpe static ssize_t hw_stat_port_store(struct ib_device *ibdev, u32 port_num,
183054239f4SJason Gunthorpe 				  struct ib_port_attribute *attr,
184054239f4SJason Gunthorpe 				  const char *buf, size_t count)
185467f432aSJason Gunthorpe {
186467f432aSJason Gunthorpe 	struct hw_stats_port_attribute *stat_attr =
187467f432aSJason Gunthorpe 		container_of(attr, struct hw_stats_port_attribute, attr);
188054239f4SJason Gunthorpe 	struct ib_port *port = ibdev->port_data[port_num].sysfs;
189467f432aSJason Gunthorpe 
190054239f4SJason Gunthorpe 	return stat_attr->store(ibdev, port->hw_stats_data->stats,
191467f432aSJason Gunthorpe 				stat_attr - port->hw_stats_data->attrs,
192467f432aSJason Gunthorpe 				port->port_num, buf, count);
193467f432aSJason Gunthorpe }
194467f432aSJason Gunthorpe 
gid_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)195470be516SMatan Barak static ssize_t gid_attr_show(struct kobject *kobj,
196470be516SMatan Barak 			     struct attribute *attr, char *buf)
197470be516SMatan Barak {
198054239f4SJason Gunthorpe 	struct ib_port_attribute *port_attr =
199054239f4SJason Gunthorpe 		container_of(attr, struct ib_port_attribute, attr);
200470be516SMatan Barak 	struct ib_port *p = container_of(kobj, struct gid_attr_group,
201470be516SMatan Barak 					 kobj)->port;
202470be516SMatan Barak 
203470be516SMatan Barak 	if (!port_attr->show)
204470be516SMatan Barak 		return -EIO;
205470be516SMatan Barak 
206054239f4SJason Gunthorpe 	return port_attr->show(p->ibdev, p->port_num, port_attr, buf);
207470be516SMatan Barak }
208470be516SMatan Barak 
209470be516SMatan Barak static const struct sysfs_ops gid_attr_sysfs_ops = {
210470be516SMatan Barak 	.show = gid_attr_show
211470be516SMatan Barak };
212470be516SMatan Barak 
state_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)213054239f4SJason Gunthorpe static ssize_t state_show(struct ib_device *ibdev, u32 port_num,
214054239f4SJason Gunthorpe 			  struct ib_port_attribute *unused, char *buf)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct ib_port_attr attr;
2171da177e4SLinus Torvalds 	ssize_t ret;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	static const char *state_name[] = {
2201da177e4SLinus Torvalds 		[IB_PORT_NOP]		= "NOP",
2211da177e4SLinus Torvalds 		[IB_PORT_DOWN]		= "DOWN",
2221da177e4SLinus Torvalds 		[IB_PORT_INIT]		= "INIT",
2231da177e4SLinus Torvalds 		[IB_PORT_ARMED]		= "ARMED",
2241da177e4SLinus Torvalds 		[IB_PORT_ACTIVE]	= "ACTIVE",
2251da177e4SLinus Torvalds 		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
2261da177e4SLinus Torvalds 	};
2271da177e4SLinus Torvalds 
228054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
2291da177e4SLinus Torvalds 	if (ret)
2301da177e4SLinus Torvalds 		return ret;
2311da177e4SLinus Torvalds 
232e28bf1f0SJoe Perches 	return sysfs_emit(buf, "%d: %s\n", attr.state,
233e28bf1f0SJoe Perches 			  attr.state >= 0 &&
234e28bf1f0SJoe Perches 					  attr.state < ARRAY_SIZE(state_name) ?
235e28bf1f0SJoe Perches 				  state_name[attr.state] :
236e28bf1f0SJoe Perches 				  "UNKNOWN");
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
lid_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)239054239f4SJason Gunthorpe static ssize_t lid_show(struct ib_device *ibdev, u32 port_num,
240054239f4SJason Gunthorpe 			struct ib_port_attribute *unused, char *buf)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct ib_port_attr attr;
2431da177e4SLinus Torvalds 	ssize_t ret;
2441da177e4SLinus Torvalds 
245054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
2461da177e4SLinus Torvalds 	if (ret)
2471da177e4SLinus Torvalds 		return ret;
2481da177e4SLinus Torvalds 
249e28bf1f0SJoe Perches 	return sysfs_emit(buf, "0x%x\n", attr.lid);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
lid_mask_count_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)252054239f4SJason Gunthorpe static ssize_t lid_mask_count_show(struct ib_device *ibdev, u32 port_num,
253054239f4SJason Gunthorpe 				   struct ib_port_attribute *unused, char *buf)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct ib_port_attr attr;
2561da177e4SLinus Torvalds 	ssize_t ret;
2571da177e4SLinus Torvalds 
258054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
2591da177e4SLinus Torvalds 	if (ret)
2601da177e4SLinus Torvalds 		return ret;
2611da177e4SLinus Torvalds 
2623cea7b4aSWenpeng Liang 	return sysfs_emit(buf, "%u\n", attr.lmc);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
sm_lid_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)265054239f4SJason Gunthorpe static ssize_t sm_lid_show(struct ib_device *ibdev, u32 port_num,
266054239f4SJason Gunthorpe 			   struct ib_port_attribute *unused, char *buf)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct ib_port_attr attr;
2691da177e4SLinus Torvalds 	ssize_t ret;
2701da177e4SLinus Torvalds 
271054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
2721da177e4SLinus Torvalds 	if (ret)
2731da177e4SLinus Torvalds 		return ret;
2741da177e4SLinus Torvalds 
275e28bf1f0SJoe Perches 	return sysfs_emit(buf, "0x%x\n", attr.sm_lid);
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
sm_sl_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)278054239f4SJason Gunthorpe static ssize_t sm_sl_show(struct ib_device *ibdev, u32 port_num,
279054239f4SJason Gunthorpe 			  struct ib_port_attribute *unused, char *buf)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	struct ib_port_attr attr;
2821da177e4SLinus Torvalds 	ssize_t ret;
2831da177e4SLinus Torvalds 
284054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
2851da177e4SLinus Torvalds 	if (ret)
2861da177e4SLinus Torvalds 		return ret;
2871da177e4SLinus Torvalds 
2883cea7b4aSWenpeng Liang 	return sysfs_emit(buf, "%u\n", attr.sm_sl);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
cap_mask_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)291054239f4SJason Gunthorpe static ssize_t cap_mask_show(struct ib_device *ibdev, u32 port_num,
292054239f4SJason Gunthorpe 			     struct ib_port_attribute *unused, char *buf)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct ib_port_attr attr;
2951da177e4SLinus Torvalds 	ssize_t ret;
2961da177e4SLinus Torvalds 
297054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
2981da177e4SLinus Torvalds 	if (ret)
2991da177e4SLinus Torvalds 		return ret;
3001da177e4SLinus Torvalds 
301e28bf1f0SJoe Perches 	return sysfs_emit(buf, "0x%08x\n", attr.port_cap_flags);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
rate_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)304054239f4SJason Gunthorpe static ssize_t rate_show(struct ib_device *ibdev, u32 port_num,
305054239f4SJason Gunthorpe 			 struct ib_port_attribute *unused, char *buf)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	struct ib_port_attr attr;
3081da177e4SLinus Torvalds 	char *speed = "";
3090559d8dcSRoland Dreier 	int rate;		/* in deci-Gb/sec */
3101da177e4SLinus Torvalds 	ssize_t ret;
3111da177e4SLinus Torvalds 
312054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
3131da177e4SLinus Torvalds 	if (ret)
3141da177e4SLinus Torvalds 		return ret;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	switch (attr.active_speed) {
3172e96691cSOr Gerlitz 	case IB_SPEED_DDR:
31871eeba16SMarcel Apfelbaum 		speed = " DDR";
319e9319b0cSRoland Dreier 		rate = 50;
32071eeba16SMarcel Apfelbaum 		break;
3212e96691cSOr Gerlitz 	case IB_SPEED_QDR:
32271eeba16SMarcel Apfelbaum 		speed = " QDR";
323e9319b0cSRoland Dreier 		rate = 100;
32471eeba16SMarcel Apfelbaum 		break;
3252e96691cSOr Gerlitz 	case IB_SPEED_FDR10:
32671eeba16SMarcel Apfelbaum 		speed = " FDR10";
327e9319b0cSRoland Dreier 		rate = 100;
32871eeba16SMarcel Apfelbaum 		break;
3292e96691cSOr Gerlitz 	case IB_SPEED_FDR:
33071eeba16SMarcel Apfelbaum 		speed = " FDR";
331e9319b0cSRoland Dreier 		rate = 140;
33271eeba16SMarcel Apfelbaum 		break;
3332e96691cSOr Gerlitz 	case IB_SPEED_EDR:
33471eeba16SMarcel Apfelbaum 		speed = " EDR";
335e9319b0cSRoland Dreier 		rate = 250;
33671eeba16SMarcel Apfelbaum 		break;
33712113a35SNoa Osherovich 	case IB_SPEED_HDR:
33812113a35SNoa Osherovich 		speed = " HDR";
33912113a35SNoa Osherovich 		rate = 500;
34012113a35SNoa Osherovich 		break;
341c7adf771SMeir Lichtinger 	case IB_SPEED_NDR:
342c7adf771SMeir Lichtinger 		speed = " NDR";
343c7adf771SMeir Lichtinger 		rate = 1000;
344c7adf771SMeir Lichtinger 		break;
3450559d8dcSRoland Dreier 	case IB_SPEED_SDR:
3460559d8dcSRoland Dreier 	default:		/* default to SDR for invalid rates */
347311d0da9SHonggang Li 		speed = " SDR";
3480559d8dcSRoland Dreier 		rate = 25;
3490559d8dcSRoland Dreier 		break;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 
35271eeba16SMarcel Apfelbaum 	rate *= ib_width_enum_to_int(attr.active_width);
3531da177e4SLinus Torvalds 	if (rate < 0)
3541da177e4SLinus Torvalds 		return -EINVAL;
3551da177e4SLinus Torvalds 
356e28bf1f0SJoe Perches 	return sysfs_emit(buf, "%d%s Gb/sec (%dX%s)\n", rate / 10,
357e28bf1f0SJoe Perches 			  rate % 10 ? ".5" : "",
3581da177e4SLinus Torvalds 			  ib_width_enum_to_int(attr.active_width), speed);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
phys_state_to_str(enum ib_port_phys_state phys_state)36172a7720fSKamal Heib static const char *phys_state_to_str(enum ib_port_phys_state phys_state)
36272a7720fSKamal Heib {
36372a7720fSKamal Heib 	static const char *phys_state_str[] = {
36472a7720fSKamal Heib 		"<unknown>",
36572a7720fSKamal Heib 		"Sleep",
36672a7720fSKamal Heib 		"Polling",
36772a7720fSKamal Heib 		"Disabled",
36872a7720fSKamal Heib 		"PortConfigurationTraining",
36972a7720fSKamal Heib 		"LinkUp",
37072a7720fSKamal Heib 		"LinkErrorRecovery",
37172a7720fSKamal Heib 		"Phy Test",
37272a7720fSKamal Heib 	};
37372a7720fSKamal Heib 
37472a7720fSKamal Heib 	if (phys_state < ARRAY_SIZE(phys_state_str))
37572a7720fSKamal Heib 		return phys_state_str[phys_state];
37672a7720fSKamal Heib 	return "<unknown>";
37772a7720fSKamal Heib }
37872a7720fSKamal Heib 
phys_state_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)379054239f4SJason Gunthorpe static ssize_t phys_state_show(struct ib_device *ibdev, u32 port_num,
380054239f4SJason Gunthorpe 			       struct ib_port_attribute *unused, char *buf)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	struct ib_port_attr attr;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	ssize_t ret;
3851da177e4SLinus Torvalds 
386054239f4SJason Gunthorpe 	ret = ib_query_port(ibdev, port_num, &attr);
3871da177e4SLinus Torvalds 	if (ret)
3881da177e4SLinus Torvalds 		return ret;
3891da177e4SLinus Torvalds 
3903cea7b4aSWenpeng Liang 	return sysfs_emit(buf, "%u: %s\n", attr.phys_state,
39172a7720fSKamal Heib 			  phys_state_to_str(attr.phys_state));
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
link_layer_show(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * unused,char * buf)394054239f4SJason Gunthorpe static ssize_t link_layer_show(struct ib_device *ibdev, u32 port_num,
395054239f4SJason Gunthorpe 			       struct ib_port_attribute *unused, char *buf)
3968ad330a0SEli Cohen {
397e28bf1f0SJoe Perches 	const char *output;
398e28bf1f0SJoe Perches 
399054239f4SJason Gunthorpe 	switch (rdma_port_get_link_layer(ibdev, port_num)) {
4008ad330a0SEli Cohen 	case IB_LINK_LAYER_INFINIBAND:
401e28bf1f0SJoe Perches 		output = "InfiniBand";
402e28bf1f0SJoe Perches 		break;
4038ad330a0SEli Cohen 	case IB_LINK_LAYER_ETHERNET:
404e28bf1f0SJoe Perches 		output = "Ethernet";
405e28bf1f0SJoe Perches 		break;
4068ad330a0SEli Cohen 	default:
407e28bf1f0SJoe Perches 		output = "Unknown";
408e28bf1f0SJoe Perches 		break;
4098ad330a0SEli Cohen 	}
410e28bf1f0SJoe Perches 
411e28bf1f0SJoe Perches 	return sysfs_emit(buf, "%s\n", output);
4128ad330a0SEli Cohen }
4138ad330a0SEli Cohen 
414054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(state);
415054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(lid);
416054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(lid_mask_count);
417054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(sm_lid);
418054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(sm_sl);
419054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(cap_mask);
420054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(rate);
421054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(phys_state);
422054239f4SJason Gunthorpe static IB_PORT_ATTR_RO(link_layer);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds static struct attribute *port_default_attrs[] = {
425054239f4SJason Gunthorpe 	&ib_port_attr_state.attr,
426054239f4SJason Gunthorpe 	&ib_port_attr_lid.attr,
427054239f4SJason Gunthorpe 	&ib_port_attr_lid_mask_count.attr,
428054239f4SJason Gunthorpe 	&ib_port_attr_sm_lid.attr,
429054239f4SJason Gunthorpe 	&ib_port_attr_sm_sl.attr,
430054239f4SJason Gunthorpe 	&ib_port_attr_cap_mask.attr,
431054239f4SJason Gunthorpe 	&ib_port_attr_rate.attr,
432054239f4SJason Gunthorpe 	&ib_port_attr_phys_state.attr,
433054239f4SJason Gunthorpe 	&ib_port_attr_link_layer.attr,
4341da177e4SLinus Torvalds 	NULL
4351da177e4SLinus Torvalds };
43601097139SGreg Kroah-Hartman ATTRIBUTE_GROUPS(port_default);
4371da177e4SLinus Torvalds 
print_ndev(const struct ib_gid_attr * gid_attr,char * buf)438e28bf1f0SJoe Perches static ssize_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
439470be516SMatan Barak {
440943bd984SParav Pandit 	struct net_device *ndev;
441e28bf1f0SJoe Perches 	int ret = -EINVAL;
442470be516SMatan Barak 
443943bd984SParav Pandit 	rcu_read_lock();
444943bd984SParav Pandit 	ndev = rcu_dereference(gid_attr->ndev);
445943bd984SParav Pandit 	if (ndev)
446e28bf1f0SJoe Perches 		ret = sysfs_emit(buf, "%s\n", ndev->name);
447943bd984SParav Pandit 	rcu_read_unlock();
448943bd984SParav Pandit 	return ret;
449470be516SMatan Barak }
450470be516SMatan Barak 
print_gid_type(const struct ib_gid_attr * gid_attr,char * buf)451e28bf1f0SJoe Perches static ssize_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
452470be516SMatan Barak {
453e28bf1f0SJoe Perches 	return sysfs_emit(buf, "%s\n",
454e28bf1f0SJoe Perches 			  ib_cache_gid_type_str(gid_attr->gid_type));
455470be516SMatan Barak }
456470be516SMatan Barak 
_show_port_gid_attr(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf,ssize_t (* print)(const struct ib_gid_attr * gid_attr,char * buf))45777e786fcSParav Pandit static ssize_t _show_port_gid_attr(
458054239f4SJason Gunthorpe 	struct ib_device *ibdev, u32 port_num, struct ib_port_attribute *attr,
459054239f4SJason Gunthorpe 	char *buf,
460e28bf1f0SJoe Perches 	ssize_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
461470be516SMatan Barak {
462470be516SMatan Barak 	struct port_table_attribute *tab_attr =
463470be516SMatan Barak 		container_of(attr, struct port_table_attribute, attr);
46477e786fcSParav Pandit 	const struct ib_gid_attr *gid_attr;
465470be516SMatan Barak 	ssize_t ret;
466470be516SMatan Barak 
467054239f4SJason Gunthorpe 	gid_attr = rdma_get_gid_attr(ibdev, port_num, tab_attr->index);
46877e786fcSParav Pandit 	if (IS_ERR(gid_attr))
4693ff4de8fSAvihai Horon 		/* -EINVAL is returned for user space compatibility reasons. */
4703ff4de8fSAvihai Horon 		return -EINVAL;
471470be516SMatan Barak 
47277e786fcSParav Pandit 	ret = print(gid_attr, buf);
47377e786fcSParav Pandit 	rdma_put_gid_attr(gid_attr);
474470be516SMatan Barak 	return ret;
475470be516SMatan Barak }
476470be516SMatan Barak 
show_port_gid(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf)477054239f4SJason Gunthorpe static ssize_t show_port_gid(struct ib_device *ibdev, u32 port_num,
478054239f4SJason Gunthorpe 			     struct ib_port_attribute *attr, char *buf)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	struct port_table_attribute *tab_attr =
4811da177e4SLinus Torvalds 		container_of(attr, struct port_table_attribute, attr);
48277e786fcSParav Pandit 	const struct ib_gid_attr *gid_attr;
483e28bf1f0SJoe Perches 	int len;
4841da177e4SLinus Torvalds 
485054239f4SJason Gunthorpe 	gid_attr = rdma_get_gid_attr(ibdev, port_num, tab_attr->index);
48677e786fcSParav Pandit 	if (IS_ERR(gid_attr)) {
48777e786fcSParav Pandit 		const union ib_gid zgid = {};
4881da177e4SLinus Torvalds 
48977e786fcSParav Pandit 		/* If reading GID fails, it is likely due to GID entry being
49077e786fcSParav Pandit 		 * empty (invalid) or reserved GID in the table.  User space
49177e786fcSParav Pandit 		 * expects to read GID table entries as long as it given index
49277e786fcSParav Pandit 		 * is within GID table size.  Administrative/debugging tool
49377e786fcSParav Pandit 		 * fails to query rest of the GID entries if it hits error
49477e786fcSParav Pandit 		 * while querying a GID of the given index.  To avoid user
49577e786fcSParav Pandit 		 * space throwing such error on fail to read gid, return zero
49677e786fcSParav Pandit 		 * GID as before. This maintains backward compatibility.
497598ff6baSParav Pandit 		 */
498e28bf1f0SJoe Perches 		return sysfs_emit(buf, "%pI6\n", zgid.raw);
49977e786fcSParav Pandit 	}
50077e786fcSParav Pandit 
501e28bf1f0SJoe Perches 	len = sysfs_emit(buf, "%pI6\n", gid_attr->gid.raw);
50277e786fcSParav Pandit 	rdma_put_gid_attr(gid_attr);
503e28bf1f0SJoe Perches 	return len;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
show_port_gid_attr_ndev(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf)506054239f4SJason Gunthorpe static ssize_t show_port_gid_attr_ndev(struct ib_device *ibdev, u32 port_num,
507054239f4SJason Gunthorpe 				       struct ib_port_attribute *attr,
508470be516SMatan Barak 				       char *buf)
509470be516SMatan Barak {
510054239f4SJason Gunthorpe 	return _show_port_gid_attr(ibdev, port_num, attr, buf, print_ndev);
511470be516SMatan Barak }
512470be516SMatan Barak 
show_port_gid_attr_gid_type(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf)513054239f4SJason Gunthorpe static ssize_t show_port_gid_attr_gid_type(struct ib_device *ibdev,
514054239f4SJason Gunthorpe 					   u32 port_num,
515054239f4SJason Gunthorpe 					   struct ib_port_attribute *attr,
5161da177e4SLinus Torvalds 					   char *buf)
5171da177e4SLinus Torvalds {
518054239f4SJason Gunthorpe 	return _show_port_gid_attr(ibdev, port_num, attr, buf, print_gid_type);
519054239f4SJason Gunthorpe }
520054239f4SJason Gunthorpe 
show_port_pkey(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf)521054239f4SJason Gunthorpe static ssize_t show_port_pkey(struct ib_device *ibdev, u32 port_num,
522054239f4SJason Gunthorpe 			      struct ib_port_attribute *attr, char *buf)
523054239f4SJason Gunthorpe {
5241da177e4SLinus Torvalds 	struct port_table_attribute *tab_attr =
5251da177e4SLinus Torvalds 		container_of(attr, struct port_table_attribute, attr);
5261da177e4SLinus Torvalds 	u16 pkey;
527e28bf1f0SJoe Perches 	int ret;
5281da177e4SLinus Torvalds 
529054239f4SJason Gunthorpe 	ret = ib_query_pkey(ibdev, port_num, tab_attr->index, &pkey);
5301da177e4SLinus Torvalds 	if (ret)
5311da177e4SLinus Torvalds 		return ret;
5321da177e4SLinus Torvalds 
533e28bf1f0SJoe Perches 	return sysfs_emit(buf, "0x%04x\n", pkey);
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds #define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
5371da177e4SLinus Torvalds struct port_table_attribute port_pma_attr_##_name = {			\
5381da177e4SLinus Torvalds 	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
539b2788ce5SChristoph Lameter 	.index = (_offset) | ((_width) << 16) | ((_counter) << 24),	\
540b2788ce5SChristoph Lameter 	.attr_id = IB_PMA_PORT_COUNTERS,				\
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543145d9c54SChristoph Lameter #define PORT_PMA_ATTR_EXT(_name, _width, _offset)			\
544145d9c54SChristoph Lameter struct port_table_attribute port_pma_attr_ext_##_name = {		\
545145d9c54SChristoph Lameter 	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
546145d9c54SChristoph Lameter 	.index = (_offset) | ((_width) << 16),				\
547145d9c54SChristoph Lameter 	.attr_id = IB_PMA_PORT_COUNTERS_EXT,				\
548145d9c54SChristoph Lameter }
549145d9c54SChristoph Lameter 
55035c4cbb1SChristoph Lameter /*
55135c4cbb1SChristoph Lameter  * Get a Perfmgmt MAD block of data.
55235c4cbb1SChristoph Lameter  * Returns error code or the number of bytes retrieved.
55335c4cbb1SChristoph Lameter  */
get_perf_mad(struct ib_device * dev,int port_num,__be16 attr,void * data,int offset,size_t size)55465487fdcSIra Weiny static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
55535c4cbb1SChristoph Lameter 		void *data, int offset, size_t size)
5561da177e4SLinus Torvalds {
55735c4cbb1SChristoph Lameter 	struct ib_mad *in_mad;
55835c4cbb1SChristoph Lameter 	struct ib_mad *out_mad;
5594cd7c947SIra Weiny 	size_t mad_size = sizeof(*out_mad);
5604cd7c947SIra Weiny 	u16 out_mad_pkey_index = 0;
5611da177e4SLinus Torvalds 	ssize_t ret;
5621da177e4SLinus Torvalds 
5633023a1e9SKamal Heib 	if (!dev->ops.process_mad)
56435c4cbb1SChristoph Lameter 		return -ENOSYS;
5651da177e4SLinus Torvalds 
566be4a8d46SLeon Romanovsky 	in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
567be4a8d46SLeon Romanovsky 	out_mad = kzalloc(sizeof(*out_mad), GFP_KERNEL);
5681da177e4SLinus Torvalds 	if (!in_mad || !out_mad) {
5691da177e4SLinus Torvalds 		ret = -ENOMEM;
5701da177e4SLinus Torvalds 		goto out;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	in_mad->mad_hdr.base_version  = 1;
5741da177e4SLinus Torvalds 	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
5751da177e4SLinus Torvalds 	in_mad->mad_hdr.class_version = 1;
5761da177e4SLinus Torvalds 	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
57735c4cbb1SChristoph Lameter 	in_mad->mad_hdr.attr_id       = attr;
5781da177e4SLinus Torvalds 
5796e2a51a0SHal Rosenstock 	if (attr != IB_PMA_CLASS_PORT_INFO)
58035c4cbb1SChristoph Lameter 		in_mad->data[41] = port_num;	/* PortSelect field */
5811da177e4SLinus Torvalds 
582e26e7b88SLeon Romanovsky 	if ((dev->ops.process_mad(dev, IB_MAD_IGNORE_MKEY, port_num, NULL, NULL,
583e26e7b88SLeon Romanovsky 				  in_mad, out_mad, &mad_size,
5844cd7c947SIra Weiny 				  &out_mad_pkey_index) &
5851da177e4SLinus Torvalds 	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
5861da177e4SLinus Torvalds 	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
5871da177e4SLinus Torvalds 		ret = -EINVAL;
5881da177e4SLinus Torvalds 		goto out;
5891da177e4SLinus Torvalds 	}
59035c4cbb1SChristoph Lameter 	memcpy(data, out_mad->data + offset, size);
59135c4cbb1SChristoph Lameter 	ret = size;
59235c4cbb1SChristoph Lameter out:
59335c4cbb1SChristoph Lameter 	kfree(in_mad);
59435c4cbb1SChristoph Lameter 	kfree(out_mad);
59535c4cbb1SChristoph Lameter 	return ret;
59635c4cbb1SChristoph Lameter }
59735c4cbb1SChristoph Lameter 
show_pma_counter(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute * attr,char * buf)598054239f4SJason Gunthorpe static ssize_t show_pma_counter(struct ib_device *ibdev, u32 port_num,
599054239f4SJason Gunthorpe 				struct ib_port_attribute *attr, char *buf)
60035c4cbb1SChristoph Lameter {
60135c4cbb1SChristoph Lameter 	struct port_table_attribute *tab_attr =
60235c4cbb1SChristoph Lameter 		container_of(attr, struct port_table_attribute, attr);
60335c4cbb1SChristoph Lameter 	int offset = tab_attr->index & 0xffff;
60435c4cbb1SChristoph Lameter 	int width  = (tab_attr->index >> 16) & 0xff;
605e28bf1f0SJoe Perches 	int ret;
60635c4cbb1SChristoph Lameter 	u8 data[8];
607e28bf1f0SJoe Perches 	int len;
60835c4cbb1SChristoph Lameter 
609054239f4SJason Gunthorpe 	ret = get_perf_mad(ibdev, port_num, tab_attr->attr_id, &data,
61035c4cbb1SChristoph Lameter 			40 + offset / 8, sizeof(data));
61135c4cbb1SChristoph Lameter 	if (ret < 0)
6120f6ef65dSParav Pandit 		return ret;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	switch (width) {
6151da177e4SLinus Torvalds 	case 4:
6163cea7b4aSWenpeng Liang 		len = sysfs_emit(buf, "%d\n",
617e28bf1f0SJoe Perches 				 (*data >> (4 - (offset % 8))) & 0xf);
6181da177e4SLinus Torvalds 		break;
6191da177e4SLinus Torvalds 	case 8:
620e28bf1f0SJoe Perches 		len = sysfs_emit(buf, "%u\n", *data);
6211da177e4SLinus Torvalds 		break;
6221da177e4SLinus Torvalds 	case 16:
623e28bf1f0SJoe Perches 		len = sysfs_emit(buf, "%u\n", be16_to_cpup((__be16 *)data));
6241da177e4SLinus Torvalds 		break;
6251da177e4SLinus Torvalds 	case 32:
626e28bf1f0SJoe Perches 		len = sysfs_emit(buf, "%u\n", be32_to_cpup((__be32 *)data));
6271da177e4SLinus Torvalds 		break;
628145d9c54SChristoph Lameter 	case 64:
629e28bf1f0SJoe Perches 		len = sysfs_emit(buf, "%llu\n", be64_to_cpup((__be64 *)data));
630145d9c54SChristoph Lameter 		break;
6311da177e4SLinus Torvalds 	default:
632e28bf1f0SJoe Perches 		len = 0;
633e28bf1f0SJoe Perches 		break;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
636e28bf1f0SJoe Perches 	return len;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
6401da177e4SLinus Torvalds static PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
6411da177e4SLinus Torvalds static PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
6421da177e4SLinus Torvalds static PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
6431da177e4SLinus Torvalds static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
6441da177e4SLinus Torvalds static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
6451da177e4SLinus Torvalds static PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
6461da177e4SLinus Torvalds static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
6471da177e4SLinus Torvalds static PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
6481da177e4SLinus Torvalds static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
6491da177e4SLinus Torvalds static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
6501da177e4SLinus Torvalds static PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
6511da177e4SLinus Torvalds static PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
6521da177e4SLinus Torvalds static PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
6531da177e4SLinus Torvalds static PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
6541da177e4SLinus Torvalds static PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
655c5a81d11SChristoph Lameter static PORT_PMA_ATTR(port_xmit_wait		    ,  0, 32, 320);
6561da177e4SLinus Torvalds 
657145d9c54SChristoph Lameter /*
658145d9c54SChristoph Lameter  * Counters added by extended set
659145d9c54SChristoph Lameter  */
660145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(port_xmit_data		    , 64,  64);
661145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(port_rcv_data		    , 64, 128);
662145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(port_xmit_packets	    , 64, 192);
663145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(port_rcv_packets	    , 64, 256);
664145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(unicast_xmit_packets	    , 64, 320);
665145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(unicast_rcv_packets	    , 64, 384);
666145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(multicast_xmit_packets	    , 64, 448);
667145d9c54SChristoph Lameter static PORT_PMA_ATTR_EXT(multicast_rcv_packets	    , 64, 512);
668145d9c54SChristoph Lameter 
6691da177e4SLinus Torvalds static struct attribute *pma_attrs[] = {
6701da177e4SLinus Torvalds 	&port_pma_attr_symbol_error.attr.attr,
6711da177e4SLinus Torvalds 	&port_pma_attr_link_error_recovery.attr.attr,
6721da177e4SLinus Torvalds 	&port_pma_attr_link_downed.attr.attr,
6731da177e4SLinus Torvalds 	&port_pma_attr_port_rcv_errors.attr.attr,
6741da177e4SLinus Torvalds 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
6751da177e4SLinus Torvalds 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
6761da177e4SLinus Torvalds 	&port_pma_attr_port_xmit_discards.attr.attr,
6771da177e4SLinus Torvalds 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
6781da177e4SLinus Torvalds 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
6791da177e4SLinus Torvalds 	&port_pma_attr_local_link_integrity_errors.attr.attr,
6801da177e4SLinus Torvalds 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
6811da177e4SLinus Torvalds 	&port_pma_attr_VL15_dropped.attr.attr,
6821da177e4SLinus Torvalds 	&port_pma_attr_port_xmit_data.attr.attr,
6831da177e4SLinus Torvalds 	&port_pma_attr_port_rcv_data.attr.attr,
6841da177e4SLinus Torvalds 	&port_pma_attr_port_xmit_packets.attr.attr,
6851da177e4SLinus Torvalds 	&port_pma_attr_port_rcv_packets.attr.attr,
686c5a81d11SChristoph Lameter 	&port_pma_attr_port_xmit_wait.attr.attr,
6871da177e4SLinus Torvalds 	NULL
6881da177e4SLinus Torvalds };
6891da177e4SLinus Torvalds 
690145d9c54SChristoph Lameter static struct attribute *pma_attrs_ext[] = {
691145d9c54SChristoph Lameter 	&port_pma_attr_symbol_error.attr.attr,
692145d9c54SChristoph Lameter 	&port_pma_attr_link_error_recovery.attr.attr,
693145d9c54SChristoph Lameter 	&port_pma_attr_link_downed.attr.attr,
694145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_errors.attr.attr,
695145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
696145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
697145d9c54SChristoph Lameter 	&port_pma_attr_port_xmit_discards.attr.attr,
698145d9c54SChristoph Lameter 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
699145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
700145d9c54SChristoph Lameter 	&port_pma_attr_local_link_integrity_errors.attr.attr,
701145d9c54SChristoph Lameter 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
702145d9c54SChristoph Lameter 	&port_pma_attr_VL15_dropped.attr.attr,
703145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_xmit_data.attr.attr,
704145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_rcv_data.attr.attr,
705145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_xmit_packets.attr.attr,
706c5a81d11SChristoph Lameter 	&port_pma_attr_port_xmit_wait.attr.attr,
707145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_rcv_packets.attr.attr,
708145d9c54SChristoph Lameter 	&port_pma_attr_ext_unicast_rcv_packets.attr.attr,
709145d9c54SChristoph Lameter 	&port_pma_attr_ext_unicast_xmit_packets.attr.attr,
710145d9c54SChristoph Lameter 	&port_pma_attr_ext_multicast_rcv_packets.attr.attr,
711145d9c54SChristoph Lameter 	&port_pma_attr_ext_multicast_xmit_packets.attr.attr,
712145d9c54SChristoph Lameter 	NULL
713145d9c54SChristoph Lameter };
714145d9c54SChristoph Lameter 
715145d9c54SChristoph Lameter static struct attribute *pma_attrs_noietf[] = {
716145d9c54SChristoph Lameter 	&port_pma_attr_symbol_error.attr.attr,
717145d9c54SChristoph Lameter 	&port_pma_attr_link_error_recovery.attr.attr,
718145d9c54SChristoph Lameter 	&port_pma_attr_link_downed.attr.attr,
719145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_errors.attr.attr,
720145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
721145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
722145d9c54SChristoph Lameter 	&port_pma_attr_port_xmit_discards.attr.attr,
723145d9c54SChristoph Lameter 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
724145d9c54SChristoph Lameter 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
725145d9c54SChristoph Lameter 	&port_pma_attr_local_link_integrity_errors.attr.attr,
726145d9c54SChristoph Lameter 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
727145d9c54SChristoph Lameter 	&port_pma_attr_VL15_dropped.attr.attr,
728145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_xmit_data.attr.attr,
729145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_rcv_data.attr.attr,
730145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_xmit_packets.attr.attr,
731145d9c54SChristoph Lameter 	&port_pma_attr_ext_port_rcv_packets.attr.attr,
732c5a81d11SChristoph Lameter 	&port_pma_attr_port_xmit_wait.attr.attr,
733145d9c54SChristoph Lameter 	NULL
734145d9c54SChristoph Lameter };
735145d9c54SChristoph Lameter 
73642d5179cSRikard Falkeborn static const struct attribute_group pma_group = {
7371da177e4SLinus Torvalds 	.name  = "counters",
7381da177e4SLinus Torvalds 	.attrs  = pma_attrs
7391da177e4SLinus Torvalds };
7401da177e4SLinus Torvalds 
74142d5179cSRikard Falkeborn static const struct attribute_group pma_group_ext = {
742145d9c54SChristoph Lameter 	.name  = "counters",
743145d9c54SChristoph Lameter 	.attrs  = pma_attrs_ext
744145d9c54SChristoph Lameter };
745145d9c54SChristoph Lameter 
74642d5179cSRikard Falkeborn static const struct attribute_group pma_group_noietf = {
747145d9c54SChristoph Lameter 	.name  = "counters",
748145d9c54SChristoph Lameter 	.attrs  = pma_attrs_noietf
749145d9c54SChristoph Lameter };
750145d9c54SChristoph Lameter 
ib_port_release(struct kobject * kobj)7511da177e4SLinus Torvalds static void ib_port_release(struct kobject *kobj)
7521da177e4SLinus Torvalds {
7532ca1cca4SJason Gunthorpe 	struct ib_port *port = container_of(kobj, struct ib_port, kobj);
7541da177e4SLinus Torvalds 	int i;
7551da177e4SLinus Torvalds 
7562ca1cca4SJason Gunthorpe 	for (i = 0; i != ARRAY_SIZE(port->groups); i++)
7572ca1cca4SJason Gunthorpe 		kfree(port->groups[i].attrs);
7582ca1cca4SJason Gunthorpe 	if (port->hw_stats_data)
7590a0800ceSMark Zhang 		rdma_free_hw_stats_struct(port->hw_stats_data->stats);
7602ca1cca4SJason Gunthorpe 	kfree(port->hw_stats_data);
761911a81c9Swangyugui 	kvfree(port);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
ib_port_gid_attr_release(struct kobject * kobj)764470be516SMatan Barak static void ib_port_gid_attr_release(struct kobject *kobj)
765470be516SMatan Barak {
766a4676388SJason Gunthorpe 	struct gid_attr_group *gid_attr_group =
767a4676388SJason Gunthorpe 		container_of(kobj, struct gid_attr_group, kobj);
768470be516SMatan Barak 	int i;
769470be516SMatan Barak 
770a4676388SJason Gunthorpe 	for (i = 0; i != ARRAY_SIZE(gid_attr_group->groups); i++)
771a4676388SJason Gunthorpe 		kfree(gid_attr_group->groups[i].attrs);
772a4676388SJason Gunthorpe 	kfree(gid_attr_group);
773470be516SMatan Barak }
774470be516SMatan Barak 
7751da177e4SLinus Torvalds static struct kobj_type port_type = {
7761da177e4SLinus Torvalds 	.release       = ib_port_release,
7771da177e4SLinus Torvalds 	.sysfs_ops     = &port_sysfs_ops,
77801097139SGreg Kroah-Hartman 	.default_groups = port_default_groups,
7791da177e4SLinus Torvalds };
7801da177e4SLinus Torvalds 
781470be516SMatan Barak static struct kobj_type gid_attr_type = {
782470be516SMatan Barak 	.sysfs_ops      = &gid_attr_sysfs_ops,
783470be516SMatan Barak 	.release        = ib_port_gid_attr_release
784470be516SMatan Barak };
785470be516SMatan Barak 
786145d9c54SChristoph Lameter /*
787145d9c54SChristoph Lameter  * Figure out which counter table to use depending on
788145d9c54SChristoph Lameter  * the device capabilities.
789145d9c54SChristoph Lameter  */
get_counter_table(struct ib_device * dev,int port_num)79042d5179cSRikard Falkeborn static const struct attribute_group *get_counter_table(struct ib_device *dev,
7916e2a51a0SHal Rosenstock 						       int port_num)
792145d9c54SChristoph Lameter {
793145d9c54SChristoph Lameter 	struct ib_class_port_info cpi;
794145d9c54SChristoph Lameter 
7956e2a51a0SHal Rosenstock 	if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
796145d9c54SChristoph Lameter 				&cpi, 40, sizeof(cpi)) >= 0) {
797ee50aeacSEran Ben Elisha 		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
798145d9c54SChristoph Lameter 			/* We have extended counters */
799145d9c54SChristoph Lameter 			return &pma_group_ext;
800145d9c54SChristoph Lameter 
801ee50aeacSEran Ben Elisha 		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
802145d9c54SChristoph Lameter 			/* But not the IETF ones */
803145d9c54SChristoph Lameter 			return &pma_group_noietf;
804145d9c54SChristoph Lameter 	}
805145d9c54SChristoph Lameter 
806145d9c54SChristoph Lameter 	/* Fall back to normal counters */
807145d9c54SChristoph Lameter 	return &pma_group;
808145d9c54SChristoph Lameter }
809145d9c54SChristoph Lameter 
update_hw_stats(struct ib_device * dev,struct rdma_hw_stats * stats,u32 port_num,int index)810b40f4757SChristoph Lameter static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
8111fb7f897SMark Bloch 			   u32 port_num, int index)
812b40f4757SChristoph Lameter {
813b40f4757SChristoph Lameter 	int ret;
814b40f4757SChristoph Lameter 
815b40f4757SChristoph Lameter 	if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
816b40f4757SChristoph Lameter 		return 0;
8173023a1e9SKamal Heib 	ret = dev->ops.get_hw_stats(dev, stats, port_num, index);
818b40f4757SChristoph Lameter 	if (ret < 0)
819b40f4757SChristoph Lameter 		return ret;
820b40f4757SChristoph Lameter 	if (ret == stats->num_counters)
821b40f4757SChristoph Lameter 		stats->timestamp = jiffies;
822b40f4757SChristoph Lameter 
823b40f4757SChristoph Lameter 	return 0;
824b40f4757SChristoph Lameter }
825b40f4757SChristoph Lameter 
print_hw_stat(struct ib_device * dev,int port_num,struct rdma_hw_stats * stats,int index,char * buf)826e28bf1f0SJoe Perches static int print_hw_stat(struct ib_device *dev, int port_num,
827f34a55e4SMark Zhang 			 struct rdma_hw_stats *stats, int index, char *buf)
828b40f4757SChristoph Lameter {
829f34a55e4SMark Zhang 	u64 v = rdma_counter_get_hwstat_value(dev, port_num, index);
830f34a55e4SMark Zhang 
831e28bf1f0SJoe Perches 	return sysfs_emit(buf, "%llu\n", stats->value[index] + v);
832b40f4757SChristoph Lameter }
833b40f4757SChristoph Lameter 
show_hw_stats(struct ib_device * ibdev,struct rdma_hw_stats * stats,unsigned int index,unsigned int port_num,char * buf)834467f432aSJason Gunthorpe static ssize_t show_hw_stats(struct ib_device *ibdev,
835467f432aSJason Gunthorpe 			     struct rdma_hw_stats *stats, unsigned int index,
836467f432aSJason Gunthorpe 			     unsigned int port_num, char *buf)
837b40f4757SChristoph Lameter {
838b40f4757SChristoph Lameter 	int ret;
839b40f4757SChristoph Lameter 
840e945130bSMark Bloch 	mutex_lock(&stats->lock);
841467f432aSJason Gunthorpe 	ret = update_hw_stats(ibdev, stats, port_num, index);
842b40f4757SChristoph Lameter 	if (ret)
843e945130bSMark Bloch 		goto unlock;
844467f432aSJason Gunthorpe 	ret = print_hw_stat(ibdev, port_num, stats, index, buf);
845e945130bSMark Bloch unlock:
846e945130bSMark Bloch 	mutex_unlock(&stats->lock);
847e945130bSMark Bloch 
848b40f4757SChristoph Lameter 	return ret;
849b40f4757SChristoph Lameter }
850b40f4757SChristoph Lameter 
show_stats_lifespan(struct ib_device * ibdev,struct rdma_hw_stats * stats,unsigned int index,unsigned int port_num,char * buf)851467f432aSJason Gunthorpe static ssize_t show_stats_lifespan(struct ib_device *ibdev,
852467f432aSJason Gunthorpe 				   struct rdma_hw_stats *stats,
853467f432aSJason Gunthorpe 				   unsigned int index, unsigned int port_num,
854b40f4757SChristoph Lameter 				   char *buf)
855b40f4757SChristoph Lameter {
856b40f4757SChristoph Lameter 	int msecs;
857b40f4757SChristoph Lameter 
858e945130bSMark Bloch 	mutex_lock(&stats->lock);
859e945130bSMark Bloch 	msecs = jiffies_to_msecs(stats->lifespan);
860e945130bSMark Bloch 	mutex_unlock(&stats->lock);
861e945130bSMark Bloch 
862e28bf1f0SJoe Perches 	return sysfs_emit(buf, "%d\n", msecs);
863b40f4757SChristoph Lameter }
864b40f4757SChristoph Lameter 
set_stats_lifespan(struct ib_device * ibdev,struct rdma_hw_stats * stats,unsigned int index,unsigned int port_num,const char * buf,size_t count)865467f432aSJason Gunthorpe static ssize_t set_stats_lifespan(struct ib_device *ibdev,
866467f432aSJason Gunthorpe 				   struct rdma_hw_stats *stats,
867467f432aSJason Gunthorpe 				   unsigned int index, unsigned int port_num,
868b40f4757SChristoph Lameter 				   const char *buf, size_t count)
869b40f4757SChristoph Lameter {
870b40f4757SChristoph Lameter 	int msecs;
871b40f4757SChristoph Lameter 	int jiffies;
872b40f4757SChristoph Lameter 	int ret;
873b40f4757SChristoph Lameter 
874b40f4757SChristoph Lameter 	ret = kstrtoint(buf, 10, &msecs);
875b40f4757SChristoph Lameter 	if (ret)
876b40f4757SChristoph Lameter 		return ret;
877b40f4757SChristoph Lameter 	if (msecs < 0 || msecs > 10000)
878b40f4757SChristoph Lameter 		return -EINVAL;
879b40f4757SChristoph Lameter 	jiffies = msecs_to_jiffies(msecs);
880e945130bSMark Bloch 
881e945130bSMark Bloch 	mutex_lock(&stats->lock);
882e945130bSMark Bloch 	stats->lifespan = jiffies;
883e945130bSMark Bloch 	mutex_unlock(&stats->lock);
884e945130bSMark Bloch 
885b40f4757SChristoph Lameter 	return count;
886b40f4757SChristoph Lameter }
887b40f4757SChristoph Lameter 
888467f432aSJason Gunthorpe static struct hw_stats_device_data *
alloc_hw_stats_device(struct ib_device * ibdev)889467f432aSJason Gunthorpe alloc_hw_stats_device(struct ib_device *ibdev)
890b40f4757SChristoph Lameter {
891467f432aSJason Gunthorpe 	struct hw_stats_device_data *data;
892b40f4757SChristoph Lameter 	struct rdma_hw_stats *stats;
893b40f4757SChristoph Lameter 
894467f432aSJason Gunthorpe 	if (!ibdev->ops.alloc_hw_device_stats)
895467f432aSJason Gunthorpe 		return ERR_PTR(-EOPNOTSUPP);
896467f432aSJason Gunthorpe 	stats = ibdev->ops.alloc_hw_device_stats(ibdev);
897b40f4757SChristoph Lameter 	if (!stats)
898467f432aSJason Gunthorpe 		return ERR_PTR(-ENOMEM);
89913f30b0fSAharon Landau 	if (!stats->descs || stats->num_counters <= 0)
9000147ebcfSColin Ian King 		goto err_free_stats;
901b40f4757SChristoph Lameter 
90241aaa99fSDoug Ledford 	/*
90341aaa99fSDoug Ledford 	 * Two extra attribue elements here, one for the lifespan entry and
90441aaa99fSDoug Ledford 	 * one to NULL terminate the list for the sysfs core code
90541aaa99fSDoug Ledford 	 */
906*119d4350SGustavo A. R. Silva 	data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)),
907b40f4757SChristoph Lameter 		       GFP_KERNEL);
908467f432aSJason Gunthorpe 	if (!data)
9090147ebcfSColin Ian King 		goto err_free_stats;
910467f432aSJason Gunthorpe 	data->group.attrs = kcalloc(stats->num_counters + 2,
911467f432aSJason Gunthorpe 				    sizeof(*data->group.attrs), GFP_KERNEL);
912467f432aSJason Gunthorpe 	if (!data->group.attrs)
913467f432aSJason Gunthorpe 		goto err_free_data;
914b40f4757SChristoph Lameter 
915467f432aSJason Gunthorpe 	data->group.name = "hw_counters";
916467f432aSJason Gunthorpe 	data->stats = stats;
917467f432aSJason Gunthorpe 	return data;
918b40f4757SChristoph Lameter 
919467f432aSJason Gunthorpe err_free_data:
920467f432aSJason Gunthorpe 	kfree(data);
9210147ebcfSColin Ian King err_free_stats:
9220a0800ceSMark Zhang 	rdma_free_hw_stats_struct(stats);
923467f432aSJason Gunthorpe 	return ERR_PTR(-ENOMEM);
924467f432aSJason Gunthorpe }
925467f432aSJason Gunthorpe 
ib_device_release_hw_stats(struct hw_stats_device_data * data)926b7066b32SJason Gunthorpe void ib_device_release_hw_stats(struct hw_stats_device_data *data)
927467f432aSJason Gunthorpe {
928467f432aSJason Gunthorpe 	kfree(data->group.attrs);
9290a0800ceSMark Zhang 	rdma_free_hw_stats_struct(data->stats);
930467f432aSJason Gunthorpe 	kfree(data);
931467f432aSJason Gunthorpe }
932467f432aSJason Gunthorpe 
ib_setup_device_attrs(struct ib_device * ibdev)933b7066b32SJason Gunthorpe int ib_setup_device_attrs(struct ib_device *ibdev)
934467f432aSJason Gunthorpe {
935467f432aSJason Gunthorpe 	struct hw_stats_device_attribute *attr;
936467f432aSJason Gunthorpe 	struct hw_stats_device_data *data;
9375e2ddd1eSAharon Landau 	bool opstat_skipped = false;
9385e2ddd1eSAharon Landau 	int i, ret, pos = 0;
939467f432aSJason Gunthorpe 
940467f432aSJason Gunthorpe 	data = alloc_hw_stats_device(ibdev);
941b7066b32SJason Gunthorpe 	if (IS_ERR(data)) {
942b7066b32SJason Gunthorpe 		if (PTR_ERR(data) == -EOPNOTSUPP)
943b7066b32SJason Gunthorpe 			return 0;
944467f432aSJason Gunthorpe 		return PTR_ERR(data);
945b7066b32SJason Gunthorpe 	}
946b7066b32SJason Gunthorpe 	ibdev->hw_stats_data = data;
947467f432aSJason Gunthorpe 
948467f432aSJason Gunthorpe 	ret = ibdev->ops.get_hw_stats(ibdev, data->stats, 0,
949467f432aSJason Gunthorpe 				      data->stats->num_counters);
950467f432aSJason Gunthorpe 	if (ret != data->stats->num_counters) {
951467f432aSJason Gunthorpe 		if (WARN_ON(ret >= 0))
952b7066b32SJason Gunthorpe 			return -EINVAL;
953b7066b32SJason Gunthorpe 		return ret;
954467f432aSJason Gunthorpe 	}
955467f432aSJason Gunthorpe 
956467f432aSJason Gunthorpe 	data->stats->timestamp = jiffies;
957467f432aSJason Gunthorpe 
958467f432aSJason Gunthorpe 	for (i = 0; i < data->stats->num_counters; i++) {
9595e2ddd1eSAharon Landau 		if (data->stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) {
9605e2ddd1eSAharon Landau 			opstat_skipped = true;
9615e2ddd1eSAharon Landau 			continue;
9625e2ddd1eSAharon Landau 		}
9635e2ddd1eSAharon Landau 
9645e2ddd1eSAharon Landau 		WARN_ON(opstat_skipped);
9655e2ddd1eSAharon Landau 		attr = &data->attrs[pos];
966467f432aSJason Gunthorpe 		sysfs_attr_init(&attr->attr.attr);
96713f30b0fSAharon Landau 		attr->attr.attr.name = data->stats->descs[i].name;
968467f432aSJason Gunthorpe 		attr->attr.attr.mode = 0444;
969467f432aSJason Gunthorpe 		attr->attr.show = hw_stat_device_show;
970467f432aSJason Gunthorpe 		attr->show = show_hw_stats;
9715e2ddd1eSAharon Landau 		data->group.attrs[pos] = &attr->attr.attr;
9725e2ddd1eSAharon Landau 		pos++;
973467f432aSJason Gunthorpe 	}
974467f432aSJason Gunthorpe 
9755e2ddd1eSAharon Landau 	attr = &data->attrs[pos];
976467f432aSJason Gunthorpe 	sysfs_attr_init(&attr->attr.attr);
977467f432aSJason Gunthorpe 	attr->attr.attr.name = "lifespan";
978467f432aSJason Gunthorpe 	attr->attr.attr.mode = 0644;
979467f432aSJason Gunthorpe 	attr->attr.show = hw_stat_device_show;
980467f432aSJason Gunthorpe 	attr->show = show_stats_lifespan;
981467f432aSJason Gunthorpe 	attr->attr.store = hw_stat_device_store;
982467f432aSJason Gunthorpe 	attr->store = set_stats_lifespan;
9835e2ddd1eSAharon Landau 	data->group.attrs[pos] = &attr->attr.attr;
984b7066b32SJason Gunthorpe 	for (i = 0; i != ARRAY_SIZE(ibdev->groups); i++)
985b7066b32SJason Gunthorpe 		if (!ibdev->groups[i]) {
986b7066b32SJason Gunthorpe 			ibdev->groups[i] = &data->group;
987467f432aSJason Gunthorpe 			return 0;
988467f432aSJason Gunthorpe 		}
989b7066b32SJason Gunthorpe 	WARN(true, "struct ib_device->groups is too small");
990b7066b32SJason Gunthorpe 	return -EINVAL;
991467f432aSJason Gunthorpe }
992467f432aSJason Gunthorpe 
9932ca1cca4SJason Gunthorpe static struct hw_stats_port_data *
alloc_hw_stats_port(struct ib_port * port,struct attribute_group * group)9942ca1cca4SJason Gunthorpe alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
995467f432aSJason Gunthorpe {
996467f432aSJason Gunthorpe 	struct ib_device *ibdev = port->ibdev;
997467f432aSJason Gunthorpe 	struct hw_stats_port_data *data;
998467f432aSJason Gunthorpe 	struct rdma_hw_stats *stats;
999467f432aSJason Gunthorpe 
1000467f432aSJason Gunthorpe 	if (!ibdev->ops.alloc_hw_port_stats)
1001467f432aSJason Gunthorpe 		return ERR_PTR(-EOPNOTSUPP);
1002467f432aSJason Gunthorpe 	stats = ibdev->ops.alloc_hw_port_stats(port->ibdev, port->port_num);
1003467f432aSJason Gunthorpe 	if (!stats)
1004467f432aSJason Gunthorpe 		return ERR_PTR(-ENOMEM);
100513f30b0fSAharon Landau 	if (!stats->descs || stats->num_counters <= 0)
1006467f432aSJason Gunthorpe 		goto err_free_stats;
1007467f432aSJason Gunthorpe 
1008467f432aSJason Gunthorpe 	/*
1009467f432aSJason Gunthorpe 	 * Two extra attribue elements here, one for the lifespan entry and
1010467f432aSJason Gunthorpe 	 * one to NULL terminate the list for the sysfs core code
1011467f432aSJason Gunthorpe 	 */
1012*119d4350SGustavo A. R. Silva 	data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)),
1013467f432aSJason Gunthorpe 		       GFP_KERNEL);
1014467f432aSJason Gunthorpe 	if (!data)
1015467f432aSJason Gunthorpe 		goto err_free_stats;
10162ca1cca4SJason Gunthorpe 	group->attrs = kcalloc(stats->num_counters + 2,
10172ca1cca4SJason Gunthorpe 				    sizeof(*group->attrs), GFP_KERNEL);
10182ca1cca4SJason Gunthorpe 	if (!group->attrs)
1019467f432aSJason Gunthorpe 		goto err_free_data;
1020467f432aSJason Gunthorpe 
10212ca1cca4SJason Gunthorpe 	group->name = "hw_counters";
1022467f432aSJason Gunthorpe 	data->stats = stats;
1023467f432aSJason Gunthorpe 	return data;
1024467f432aSJason Gunthorpe 
1025467f432aSJason Gunthorpe err_free_data:
1026467f432aSJason Gunthorpe 	kfree(data);
1027467f432aSJason Gunthorpe err_free_stats:
10280a0800ceSMark Zhang 	rdma_free_hw_stats_struct(stats);
1029467f432aSJason Gunthorpe 	return ERR_PTR(-ENOMEM);
1030467f432aSJason Gunthorpe }
1031467f432aSJason Gunthorpe 
setup_hw_port_stats(struct ib_port * port,struct attribute_group * group)10322ca1cca4SJason Gunthorpe static int setup_hw_port_stats(struct ib_port *port,
10332ca1cca4SJason Gunthorpe 			       struct attribute_group *group)
1034467f432aSJason Gunthorpe {
1035467f432aSJason Gunthorpe 	struct hw_stats_port_attribute *attr;
1036467f432aSJason Gunthorpe 	struct hw_stats_port_data *data;
10375e2ddd1eSAharon Landau 	bool opstat_skipped = false;
10385e2ddd1eSAharon Landau 	int i, ret, pos = 0;
1039467f432aSJason Gunthorpe 
10402ca1cca4SJason Gunthorpe 	data = alloc_hw_stats_port(port, group);
1041467f432aSJason Gunthorpe 	if (IS_ERR(data))
1042467f432aSJason Gunthorpe 		return PTR_ERR(data);
1043467f432aSJason Gunthorpe 
1044467f432aSJason Gunthorpe 	ret = port->ibdev->ops.get_hw_stats(port->ibdev, data->stats,
1045467f432aSJason Gunthorpe 					    port->port_num,
1046467f432aSJason Gunthorpe 					    data->stats->num_counters);
1047467f432aSJason Gunthorpe 	if (ret != data->stats->num_counters) {
1048467f432aSJason Gunthorpe 		if (WARN_ON(ret >= 0))
10492ca1cca4SJason Gunthorpe 			return -EINVAL;
10502ca1cca4SJason Gunthorpe 		return ret;
1051467f432aSJason Gunthorpe 	}
10522ca1cca4SJason Gunthorpe 
1053467f432aSJason Gunthorpe 	data->stats->timestamp = jiffies;
1054467f432aSJason Gunthorpe 
1055467f432aSJason Gunthorpe 	for (i = 0; i < data->stats->num_counters; i++) {
10565e2ddd1eSAharon Landau 		if (data->stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) {
10575e2ddd1eSAharon Landau 			opstat_skipped = true;
10585e2ddd1eSAharon Landau 			continue;
10595e2ddd1eSAharon Landau 		}
10605e2ddd1eSAharon Landau 
10615e2ddd1eSAharon Landau 		WARN_ON(opstat_skipped);
10625e2ddd1eSAharon Landau 		attr = &data->attrs[pos];
1063467f432aSJason Gunthorpe 		sysfs_attr_init(&attr->attr.attr);
106413f30b0fSAharon Landau 		attr->attr.attr.name = data->stats->descs[i].name;
1065467f432aSJason Gunthorpe 		attr->attr.attr.mode = 0444;
1066467f432aSJason Gunthorpe 		attr->attr.show = hw_stat_port_show;
1067467f432aSJason Gunthorpe 		attr->show = show_hw_stats;
10685e2ddd1eSAharon Landau 		group->attrs[pos] = &attr->attr.attr;
10695e2ddd1eSAharon Landau 		pos++;
1070467f432aSJason Gunthorpe 	}
1071467f432aSJason Gunthorpe 
10725e2ddd1eSAharon Landau 	attr = &data->attrs[pos];
1073467f432aSJason Gunthorpe 	sysfs_attr_init(&attr->attr.attr);
1074467f432aSJason Gunthorpe 	attr->attr.attr.name = "lifespan";
1075467f432aSJason Gunthorpe 	attr->attr.attr.mode = 0644;
1076467f432aSJason Gunthorpe 	attr->attr.show = hw_stat_port_show;
1077467f432aSJason Gunthorpe 	attr->show = show_stats_lifespan;
1078467f432aSJason Gunthorpe 	attr->attr.store = hw_stat_port_store;
1079467f432aSJason Gunthorpe 	attr->store = set_stats_lifespan;
10805e2ddd1eSAharon Landau 	group->attrs[pos] = &attr->attr.attr;
1081467f432aSJason Gunthorpe 
1082467f432aSJason Gunthorpe 	port->hw_stats_data = data;
1083467f432aSJason Gunthorpe 	return 0;
1084b40f4757SChristoph Lameter }
1085b40f4757SChristoph Lameter 
ib_get_hw_stats_port(struct ib_device * ibdev,u32 port_num)1086d8a58838SJason Gunthorpe struct rdma_hw_stats *ib_get_hw_stats_port(struct ib_device *ibdev,
1087d8a58838SJason Gunthorpe 					   u32 port_num)
1088d8a58838SJason Gunthorpe {
1089467f432aSJason Gunthorpe 	if (!ibdev->port_data || !rdma_is_port_valid(ibdev, port_num) ||
1090467f432aSJason Gunthorpe 	    !ibdev->port_data[port_num].sysfs->hw_stats_data)
1091d8a58838SJason Gunthorpe 		return NULL;
1092467f432aSJason Gunthorpe 	return ibdev->port_data[port_num].sysfs->hw_stats_data->stats;
1093d8a58838SJason Gunthorpe }
1094d8a58838SJason Gunthorpe 
1095054239f4SJason Gunthorpe static int
alloc_port_table_group(const char * name,struct attribute_group * group,struct port_table_attribute * attrs,size_t num,ssize_t (* show)(struct ib_device * ibdev,u32 port_num,struct ib_port_attribute *,char * buf))1096054239f4SJason Gunthorpe alloc_port_table_group(const char *name, struct attribute_group *group,
1097a4676388SJason Gunthorpe 		       struct port_table_attribute *attrs, size_t num,
1098054239f4SJason Gunthorpe 		       ssize_t (*show)(struct ib_device *ibdev, u32 port_num,
1099054239f4SJason Gunthorpe 				       struct ib_port_attribute *, char *buf))
1100a4676388SJason Gunthorpe {
1101a4676388SJason Gunthorpe 	struct attribute **attr_list;
1102a4676388SJason Gunthorpe 	int i;
1103a4676388SJason Gunthorpe 
1104a4676388SJason Gunthorpe 	attr_list = kcalloc(num + 1, sizeof(*attr_list), GFP_KERNEL);
1105a4676388SJason Gunthorpe 	if (!attr_list)
1106a4676388SJason Gunthorpe 		return -ENOMEM;
1107a4676388SJason Gunthorpe 
1108a4676388SJason Gunthorpe 	for (i = 0; i < num; i++) {
1109a4676388SJason Gunthorpe 		struct port_table_attribute *element = &attrs[i];
1110a4676388SJason Gunthorpe 
1111a4676388SJason Gunthorpe 		if (snprintf(element->name, sizeof(element->name), "%d", i) >=
1112a4676388SJason Gunthorpe 		    sizeof(element->name))
1113a4676388SJason Gunthorpe 			goto err;
1114a4676388SJason Gunthorpe 
1115a4676388SJason Gunthorpe 		sysfs_attr_init(&element->attr.attr);
1116a4676388SJason Gunthorpe 		element->attr.attr.name = element->name;
1117a4676388SJason Gunthorpe 		element->attr.attr.mode = 0444;
1118a4676388SJason Gunthorpe 		element->attr.show = show;
1119a4676388SJason Gunthorpe 		element->index = i;
1120a4676388SJason Gunthorpe 
1121a4676388SJason Gunthorpe 		attr_list[i] = &element->attr.attr;
1122a4676388SJason Gunthorpe 	}
1123a4676388SJason Gunthorpe 	group->name = name;
1124a4676388SJason Gunthorpe 	group->attrs = attr_list;
1125a4676388SJason Gunthorpe 	return 0;
1126a4676388SJason Gunthorpe err:
1127a4676388SJason Gunthorpe 	kfree(attr_list);
1128a4676388SJason Gunthorpe 	return -EINVAL;
1129a4676388SJason Gunthorpe }
1130a4676388SJason Gunthorpe 
1131a32f4335SJason Gunthorpe /*
1132a32f4335SJason Gunthorpe  * Create the sysfs:
1133a32f4335SJason Gunthorpe  *  ibp0s9/ports/XX/gid_attrs/{ndevs,types}/YYY
1134a32f4335SJason Gunthorpe  * YYY is the gid table index in decimal
1135a32f4335SJason Gunthorpe  */
setup_gid_attrs(struct ib_port * port,const struct ib_port_attr * attr)1136a32f4335SJason Gunthorpe static int setup_gid_attrs(struct ib_port *port,
1137a32f4335SJason Gunthorpe 			   const struct ib_port_attr *attr)
1138a32f4335SJason Gunthorpe {
1139a32f4335SJason Gunthorpe 	struct gid_attr_group *gid_attr_group;
1140a32f4335SJason Gunthorpe 	int ret;
1141a32f4335SJason Gunthorpe 
1142a4676388SJason Gunthorpe 	gid_attr_group = kzalloc(struct_size(gid_attr_group, attrs_list,
1143*119d4350SGustavo A. R. Silva 					     size_mul(attr->gid_tbl_len, 2)),
1144a4676388SJason Gunthorpe 				 GFP_KERNEL);
1145a32f4335SJason Gunthorpe 	if (!gid_attr_group)
1146a32f4335SJason Gunthorpe 		return -ENOMEM;
1147a32f4335SJason Gunthorpe 	gid_attr_group->port = port;
1148a4676388SJason Gunthorpe 	kobject_init(&gid_attr_group->kobj, &gid_attr_type);
1149a4676388SJason Gunthorpe 
1150a4676388SJason Gunthorpe 	ret = alloc_port_table_group("ndevs", &gid_attr_group->groups[0],
1151a4676388SJason Gunthorpe 				     gid_attr_group->attrs_list,
1152a4676388SJason Gunthorpe 				     attr->gid_tbl_len,
1153a4676388SJason Gunthorpe 				     show_port_gid_attr_ndev);
1154a32f4335SJason Gunthorpe 	if (ret)
1155a4676388SJason Gunthorpe 		goto err_put;
1156a4676388SJason Gunthorpe 	gid_attr_group->groups_list[0] = &gid_attr_group->groups[0];
1157a32f4335SJason Gunthorpe 
1158a4676388SJason Gunthorpe 	ret = alloc_port_table_group(
1159a4676388SJason Gunthorpe 		"types", &gid_attr_group->groups[1],
1160a4676388SJason Gunthorpe 		gid_attr_group->attrs_list + attr->gid_tbl_len,
1161a4676388SJason Gunthorpe 		attr->gid_tbl_len, show_port_gid_attr_gid_type);
1162a32f4335SJason Gunthorpe 	if (ret)
1163a4676388SJason Gunthorpe 		goto err_put;
1164a4676388SJason Gunthorpe 	gid_attr_group->groups_list[1] = &gid_attr_group->groups[1];
1165a32f4335SJason Gunthorpe 
1166a4676388SJason Gunthorpe 	ret = kobject_add(&gid_attr_group->kobj, &port->kobj, "gid_attrs");
1167a32f4335SJason Gunthorpe 	if (ret)
1168a4676388SJason Gunthorpe 		goto err_put;
1169a4676388SJason Gunthorpe 	ret = sysfs_create_groups(&gid_attr_group->kobj,
1170a4676388SJason Gunthorpe 				  gid_attr_group->groups_list);
1171a4676388SJason Gunthorpe 	if (ret)
1172a4676388SJason Gunthorpe 		goto err_del;
1173a32f4335SJason Gunthorpe 	port->gid_attr_group = gid_attr_group;
1174a32f4335SJason Gunthorpe 	return 0;
1175a32f4335SJason Gunthorpe 
1176a4676388SJason Gunthorpe err_del:
1177a4676388SJason Gunthorpe 	kobject_del(&gid_attr_group->kobj);
1178a4676388SJason Gunthorpe err_put:
1179a32f4335SJason Gunthorpe 	kobject_put(&gid_attr_group->kobj);
1180a32f4335SJason Gunthorpe 	return ret;
1181a32f4335SJason Gunthorpe }
1182a32f4335SJason Gunthorpe 
destroy_gid_attrs(struct ib_port * port)1183a32f4335SJason Gunthorpe static void destroy_gid_attrs(struct ib_port *port)
1184a32f4335SJason Gunthorpe {
1185a32f4335SJason Gunthorpe 	struct gid_attr_group *gid_attr_group = port->gid_attr_group;
1186a32f4335SJason Gunthorpe 
1187a4676388SJason Gunthorpe 	if (!gid_attr_group)
1188a4676388SJason Gunthorpe 		return;
1189a4676388SJason Gunthorpe 	sysfs_remove_groups(&gid_attr_group->kobj, gid_attr_group->groups_list);
1190a4676388SJason Gunthorpe 	kobject_del(&gid_attr_group->kobj);
1191a32f4335SJason Gunthorpe 	kobject_put(&gid_attr_group->kobj);
1192a32f4335SJason Gunthorpe }
1193a32f4335SJason Gunthorpe 
11942ca1cca4SJason Gunthorpe /*
11952ca1cca4SJason Gunthorpe  * Create the sysfs:
11962ca1cca4SJason Gunthorpe  *  ibp0s9/ports/XX/{gids,pkeys,counters}/YYY
11972ca1cca4SJason Gunthorpe  */
setup_port(struct ib_core_device * coredev,int port_num,const struct ib_port_attr * attr)11982ca1cca4SJason Gunthorpe static struct ib_port *setup_port(struct ib_core_device *coredev, int port_num,
11992ca1cca4SJason Gunthorpe 				  const struct ib_port_attr *attr)
12001da177e4SLinus Torvalds {
1201cebe556bSParav Pandit 	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
1202eb15c78bSParav Pandit 	bool is_full_dev = &device->coredev == coredev;
12032ca1cca4SJason Gunthorpe 	const struct attribute_group **cur_group;
12041da177e4SLinus Torvalds 	struct ib_port *p;
12051da177e4SLinus Torvalds 	int ret;
12061da177e4SLinus Torvalds 
1207911a81c9Swangyugui 	p = kvzalloc(struct_size(p, attrs_list,
1208*119d4350SGustavo A. R. Silva 				size_add(attr->gid_tbl_len, attr->pkey_tbl_len)),
12092ca1cca4SJason Gunthorpe 		     GFP_KERNEL);
12101da177e4SLinus Torvalds 	if (!p)
12112ca1cca4SJason Gunthorpe 		return ERR_PTR(-ENOMEM);
12121da177e4SLinus Torvalds 	p->ibdev = device;
12131da177e4SLinus Torvalds 	p->port_num = port_num;
12142ca1cca4SJason Gunthorpe 	kobject_init(&p->kobj, &port_type);
12151da177e4SLinus Torvalds 
12165e15ff29SMark Zhang 	if (device->port_data && is_full_dev)
12175e15ff29SMark Zhang 		device->port_data[port_num].sysfs = p;
12185e15ff29SMark Zhang 
12192ca1cca4SJason Gunthorpe 	cur_group = p->groups_list;
12202ca1cca4SJason Gunthorpe 	ret = alloc_port_table_group("gids", &p->groups[0], p->attrs_list,
12212ca1cca4SJason Gunthorpe 				     attr->gid_tbl_len, show_port_gid);
1222b6eb7011SWenpeng Liang 	if (ret)
12230b8e125eSQiushi Wu 		goto err_put;
12242ca1cca4SJason Gunthorpe 	*cur_group++ = &p->groups[0];
12251da177e4SLinus Torvalds 
12262ca1cca4SJason Gunthorpe 	if (attr->pkey_tbl_len) {
12272ca1cca4SJason Gunthorpe 		ret = alloc_port_table_group("pkeys", &p->groups[1],
12282ca1cca4SJason Gunthorpe 					     p->attrs_list + attr->gid_tbl_len,
12292ca1cca4SJason Gunthorpe 					     attr->pkey_tbl_len, show_port_pkey);
12301da177e4SLinus Torvalds 		if (ret)
1231a32f4335SJason Gunthorpe 			goto err_put;
12322ca1cca4SJason Gunthorpe 		*cur_group++ = &p->groups[1];
123390efc8b2SKamal Heib 	}
123490efc8b2SKamal Heib 
1235467f432aSJason Gunthorpe 	/*
1236467f432aSJason Gunthorpe 	 * If port == 0, it means hw_counters are per device and not per
1237467f432aSJason Gunthorpe 	 * port, so holder should be device. Therefore skip per port
1238467f432aSJason Gunthorpe 	 * counter initialization.
1239467f432aSJason Gunthorpe 	 */
1240467f432aSJason Gunthorpe 	if (port_num && is_full_dev) {
12412ca1cca4SJason Gunthorpe 		ret = setup_hw_port_stats(p, &p->groups[2]);
1242467f432aSJason Gunthorpe 		if (ret && ret != -EOPNOTSUPP)
12432ca1cca4SJason Gunthorpe 			goto err_put;
12442ca1cca4SJason Gunthorpe 		if (!ret)
12452ca1cca4SJason Gunthorpe 			*cur_group++ = &p->groups[2];
1246467f432aSJason Gunthorpe 	}
12471da177e4SLinus Torvalds 
12482ca1cca4SJason Gunthorpe 	if (device->ops.process_mad && is_full_dev)
12492ca1cca4SJason Gunthorpe 		*cur_group++ = get_counter_table(device, port_num);
12502ca1cca4SJason Gunthorpe 
12512ca1cca4SJason Gunthorpe 	ret = kobject_add(&p->kobj, coredev->ports_kobj, "%d", port_num);
12529a6edb60SRalph Campbell 	if (ret)
12532ca1cca4SJason Gunthorpe 		goto err_put;
12542ca1cca4SJason Gunthorpe 	ret = sysfs_create_groups(&p->kobj, p->groups_list);
12552ca1cca4SJason Gunthorpe 	if (ret)
12562ca1cca4SJason Gunthorpe 		goto err_del;
1257d7407d16SJason Gunthorpe 	if (is_full_dev) {
1258d7407d16SJason Gunthorpe 		ret = sysfs_create_groups(&p->kobj, device->ops.port_groups);
1259d7407d16SJason Gunthorpe 		if (ret)
1260d7407d16SJason Gunthorpe 			goto err_groups;
1261d7407d16SJason Gunthorpe 	}
12629a6edb60SRalph Campbell 
1263cebe556bSParav Pandit 	list_add_tail(&p->kobj.entry, &coredev->port_list);
12642ca1cca4SJason Gunthorpe 	return p;
12651da177e4SLinus Torvalds 
1266d7407d16SJason Gunthorpe err_groups:
1267d7407d16SJason Gunthorpe 	sysfs_remove_groups(&p->kobj, p->groups_list);
12682ca1cca4SJason Gunthorpe err_del:
12692ca1cca4SJason Gunthorpe 	kobject_del(&p->kobj);
12701da177e4SLinus Torvalds err_put:
12715e15ff29SMark Zhang 	if (device->port_data && is_full_dev)
12725e15ff29SMark Zhang 		device->port_data[port_num].sysfs = NULL;
1273cad6d02aSHaggai Eran 	kobject_put(&p->kobj);
12742ca1cca4SJason Gunthorpe 	return ERR_PTR(ret);
12752ca1cca4SJason Gunthorpe }
12762ca1cca4SJason Gunthorpe 
destroy_port(struct ib_core_device * coredev,struct ib_port * port)1277d7407d16SJason Gunthorpe static void destroy_port(struct ib_core_device *coredev, struct ib_port *port)
12782ca1cca4SJason Gunthorpe {
1279d7407d16SJason Gunthorpe 	bool is_full_dev = &port->ibdev->coredev == coredev;
1280d7407d16SJason Gunthorpe 
12812ca1cca4SJason Gunthorpe 	list_del(&port->kobj.entry);
1282d7407d16SJason Gunthorpe 	if (is_full_dev)
1283d7407d16SJason Gunthorpe 		sysfs_remove_groups(&port->kobj, port->ibdev->ops.port_groups);
12845e15ff29SMark Zhang 
12852ca1cca4SJason Gunthorpe 	sysfs_remove_groups(&port->kobj, port->groups_list);
12862ca1cca4SJason Gunthorpe 	kobject_del(&port->kobj);
12875e15ff29SMark Zhang 
12885e15ff29SMark Zhang 	if (port->ibdev->port_data &&
12895e15ff29SMark Zhang 	    port->ibdev->port_data[port->port_num].sysfs == port)
12905e15ff29SMark Zhang 		port->ibdev->port_data[port->port_num].sysfs = NULL;
12915e15ff29SMark Zhang 
12922ca1cca4SJason Gunthorpe 	kobject_put(&port->kobj);
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
node_type_string(int node_type)129545808361SJoe Perches static const char *node_type_string(int node_type)
129645808361SJoe Perches {
129745808361SJoe Perches 	switch (node_type) {
129845808361SJoe Perches 	case RDMA_NODE_IB_CA:
129945808361SJoe Perches 		return "CA";
130045808361SJoe Perches 	case RDMA_NODE_IB_SWITCH:
130145808361SJoe Perches 		return "switch";
130245808361SJoe Perches 	case RDMA_NODE_IB_ROUTER:
130345808361SJoe Perches 		return "router";
130445808361SJoe Perches 	case RDMA_NODE_RNIC:
130545808361SJoe Perches 		return "RNIC";
130645808361SJoe Perches 	case RDMA_NODE_USNIC:
130745808361SJoe Perches 		return "usNIC";
130845808361SJoe Perches 	case RDMA_NODE_USNIC_UDP:
130945808361SJoe Perches 		return "usNIC UDP";
131045808361SJoe Perches 	case RDMA_NODE_UNSPECIFIED:
131145808361SJoe Perches 		return "unspecified";
131245808361SJoe Perches 	}
131345808361SJoe Perches 	return "<unknown>";
131445808361SJoe Perches }
131545808361SJoe Perches 
node_type_show(struct device * device,struct device_attribute * attr,char * buf)1316adee9f3fSParav Pandit static ssize_t node_type_show(struct device *device,
1317f4e91eb4STony Jones 			      struct device_attribute *attr, char *buf)
13181da177e4SLinus Torvalds {
131954747231SParav Pandit 	struct ib_device *dev = rdma_device_to_ibdev(device);
13201da177e4SLinus Torvalds 
13213cea7b4aSWenpeng Liang 	return sysfs_emit(buf, "%u: %s\n", dev->node_type,
132245808361SJoe Perches 			  node_type_string(dev->node_type));
13231da177e4SLinus Torvalds }
1324adee9f3fSParav Pandit static DEVICE_ATTR_RO(node_type);
13251da177e4SLinus Torvalds 
sys_image_guid_show(struct device * device,struct device_attribute * dev_attr,char * buf)1326adee9f3fSParav Pandit static ssize_t sys_image_guid_show(struct device *device,
1327f4e91eb4STony Jones 				   struct device_attribute *dev_attr, char *buf)
13281da177e4SLinus Torvalds {
132954747231SParav Pandit 	struct ib_device *dev = rdma_device_to_ibdev(device);
133045808361SJoe Perches 	__be16 *guid = (__be16 *)&dev->attrs.sys_image_guid;
13311da177e4SLinus Torvalds 
133245808361SJoe Perches 	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
133345808361SJoe Perches 			  be16_to_cpu(guid[0]),
133445808361SJoe Perches 			  be16_to_cpu(guid[1]),
133545808361SJoe Perches 			  be16_to_cpu(guid[2]),
133645808361SJoe Perches 			  be16_to_cpu(guid[3]));
13371da177e4SLinus Torvalds }
1338adee9f3fSParav Pandit static DEVICE_ATTR_RO(sys_image_guid);
13391da177e4SLinus Torvalds 
node_guid_show(struct device * device,struct device_attribute * attr,char * buf)1340adee9f3fSParav Pandit static ssize_t node_guid_show(struct device *device,
1341f4e91eb4STony Jones 			      struct device_attribute *attr, char *buf)
13421da177e4SLinus Torvalds {
134354747231SParav Pandit 	struct ib_device *dev = rdma_device_to_ibdev(device);
134445808361SJoe Perches 	__be16 *node_guid = (__be16 *)&dev->node_guid;
13451da177e4SLinus Torvalds 
13461c7fd726SJoe Perches 	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
134745808361SJoe Perches 			  be16_to_cpu(node_guid[0]),
134845808361SJoe Perches 			  be16_to_cpu(node_guid[1]),
134945808361SJoe Perches 			  be16_to_cpu(node_guid[2]),
135045808361SJoe Perches 			  be16_to_cpu(node_guid[3]));
13511da177e4SLinus Torvalds }
1352adee9f3fSParav Pandit static DEVICE_ATTR_RO(node_guid);
13531da177e4SLinus Torvalds 
node_desc_show(struct device * device,struct device_attribute * attr,char * buf)1354adee9f3fSParav Pandit static ssize_t node_desc_show(struct device *device,
1355f4e91eb4STony Jones 			      struct device_attribute *attr, char *buf)
1356c5bcbbb9SRoland Dreier {
135754747231SParav Pandit 	struct ib_device *dev = rdma_device_to_ibdev(device);
1358c5bcbbb9SRoland Dreier 
13591c7fd726SJoe Perches 	return sysfs_emit(buf, "%.64s\n", dev->node_desc);
1360c5bcbbb9SRoland Dreier }
1361c5bcbbb9SRoland Dreier 
node_desc_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1362adee9f3fSParav Pandit static ssize_t node_desc_store(struct device *device,
1363f4e91eb4STony Jones 			       struct device_attribute *attr,
1364f4e91eb4STony Jones 			       const char *buf, size_t count)
1365c5bcbbb9SRoland Dreier {
136654747231SParav Pandit 	struct ib_device *dev = rdma_device_to_ibdev(device);
1367c5bcbbb9SRoland Dreier 	struct ib_device_modify desc = {};
1368c5bcbbb9SRoland Dreier 	int ret;
1369c5bcbbb9SRoland Dreier 
13703023a1e9SKamal Heib 	if (!dev->ops.modify_device)
1371d0f3ef36SKamal Heib 		return -EOPNOTSUPP;
1372c5bcbbb9SRoland Dreier 
1373bd99fdeaSYuval Shaia 	memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
1374c5bcbbb9SRoland Dreier 	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
1375c5bcbbb9SRoland Dreier 	if (ret)
1376c5bcbbb9SRoland Dreier 		return ret;
1377c5bcbbb9SRoland Dreier 
1378c5bcbbb9SRoland Dreier 	return count;
1379c5bcbbb9SRoland Dreier }
1380adee9f3fSParav Pandit static DEVICE_ATTR_RW(node_desc);
1381c5bcbbb9SRoland Dreier 
fw_ver_show(struct device * device,struct device_attribute * attr,char * buf)1382adee9f3fSParav Pandit static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
138341a6ae1eSIra Weiny 			   char *buf)
138441a6ae1eSIra Weiny {
138554747231SParav Pandit 	struct ib_device *dev = rdma_device_to_ibdev(device);
138645808361SJoe Perches 	char version[IB_FW_VERSION_NAME_MAX] = {};
138741a6ae1eSIra Weiny 
138845808361SJoe Perches 	ib_get_device_fw_str(dev, version);
138945808361SJoe Perches 
139045808361SJoe Perches 	return sysfs_emit(buf, "%s\n", version);
139141a6ae1eSIra Weiny }
1392adee9f3fSParav Pandit static DEVICE_ATTR_RO(fw_ver);
139341a6ae1eSIra Weiny 
1394adee9f3fSParav Pandit static struct attribute *ib_dev_attrs[] = {
1395adee9f3fSParav Pandit 	&dev_attr_node_type.attr,
1396adee9f3fSParav Pandit 	&dev_attr_node_guid.attr,
1397adee9f3fSParav Pandit 	&dev_attr_sys_image_guid.attr,
1398adee9f3fSParav Pandit 	&dev_attr_fw_ver.attr,
1399adee9f3fSParav Pandit 	&dev_attr_node_desc.attr,
1400adee9f3fSParav Pandit 	NULL,
1401adee9f3fSParav Pandit };
14021da177e4SLinus Torvalds 
14035f8f5499SParav Pandit const struct attribute_group ib_dev_attr_group = {
1404adee9f3fSParav Pandit 	.attrs = ib_dev_attrs,
14051da177e4SLinus Torvalds };
14061da177e4SLinus Torvalds 
ib_free_port_attrs(struct ib_core_device * coredev)14075417783eSParav Pandit void ib_free_port_attrs(struct ib_core_device *coredev)
1408584482acSHaggai Eran {
1409584482acSHaggai Eran 	struct kobject *p, *t;
1410584482acSHaggai Eran 
1411cebe556bSParav Pandit 	list_for_each_entry_safe(p, t, &coredev->port_list, entry) {
1412584482acSHaggai Eran 		struct ib_port *port = container_of(p, struct ib_port, kobj);
14135767198aSParav Pandit 
1414a32f4335SJason Gunthorpe 		destroy_gid_attrs(port);
1415d7407d16SJason Gunthorpe 		destroy_port(coredev, port);
1416584482acSHaggai Eran 	}
1417584482acSHaggai Eran 
1418cebe556bSParav Pandit 	kobject_put(coredev->ports_kobj);
1419584482acSHaggai Eran }
1420584482acSHaggai Eran 
ib_setup_port_attrs(struct ib_core_device * coredev)1421eb15c78bSParav Pandit int ib_setup_port_attrs(struct ib_core_device *coredev)
14221da177e4SLinus Torvalds {
1423cebe556bSParav Pandit 	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
14242ca1cca4SJason Gunthorpe 	u32 port_num;
14251da177e4SLinus Torvalds 	int ret;
14261da177e4SLinus Torvalds 
1427cebe556bSParav Pandit 	coredev->ports_kobj = kobject_create_and_add("ports",
1428cebe556bSParav Pandit 						     &coredev->dev.kobj);
1429cebe556bSParav Pandit 	if (!coredev->ports_kobj)
14305767198aSParav Pandit 		return -ENOMEM;
14311da177e4SLinus Torvalds 
14322ca1cca4SJason Gunthorpe 	rdma_for_each_port (device, port_num) {
14332ca1cca4SJason Gunthorpe 		struct ib_port_attr attr;
14342ca1cca4SJason Gunthorpe 		struct ib_port *port;
14352ca1cca4SJason Gunthorpe 
14362ca1cca4SJason Gunthorpe 		ret = ib_query_port(device, port_num, &attr);
14372ca1cca4SJason Gunthorpe 		if (ret)
14382ca1cca4SJason Gunthorpe 			goto err_put;
14392ca1cca4SJason Gunthorpe 
14402ca1cca4SJason Gunthorpe 		port = setup_port(coredev, port_num, &attr);
14412ca1cca4SJason Gunthorpe 		if (IS_ERR(port)) {
14422ca1cca4SJason Gunthorpe 			ret = PTR_ERR(port);
14432ca1cca4SJason Gunthorpe 			goto err_put;
14442ca1cca4SJason Gunthorpe 		}
14452ca1cca4SJason Gunthorpe 
14462ca1cca4SJason Gunthorpe 		ret = setup_gid_attrs(port, &attr);
14472ca1cca4SJason Gunthorpe 		if (ret)
14482ca1cca4SJason Gunthorpe 			goto err_put;
14492ca1cca4SJason Gunthorpe 	}
14505767198aSParav Pandit 	return 0;
14515767198aSParav Pandit 
14525767198aSParav Pandit err_put:
1453cebe556bSParav Pandit 	ib_free_port_attrs(coredev);
14545767198aSParav Pandit 	return ret;
14555767198aSParav Pandit }
14565767198aSParav Pandit 
1457c87e65cfSLeon Romanovsky /**
1458526a12c8SJason Gunthorpe  * ib_port_register_client_groups - Add an ib_client's attributes to the port
1459c87e65cfSLeon Romanovsky  *
1460526a12c8SJason Gunthorpe  * @ibdev: IB device to add counters
1461c87e65cfSLeon Romanovsky  * @port_num: valid port number
1462526a12c8SJason Gunthorpe  * @groups: Group list of attributes
1463526a12c8SJason Gunthorpe  *
1464526a12c8SJason Gunthorpe  * Do not use. Only for legacy sysfs compatibility.
1465c87e65cfSLeon Romanovsky  */
ib_port_register_client_groups(struct ib_device * ibdev,u32 port_num,const struct attribute_group ** groups)1466526a12c8SJason Gunthorpe int ib_port_register_client_groups(struct ib_device *ibdev, u32 port_num,
1467526a12c8SJason Gunthorpe 				   const struct attribute_group **groups)
1468c87e65cfSLeon Romanovsky {
1469526a12c8SJason Gunthorpe 	return sysfs_create_groups(&ibdev->port_data[port_num].sysfs->kobj,
1470526a12c8SJason Gunthorpe 				   groups);
1471c87e65cfSLeon Romanovsky }
1472526a12c8SJason Gunthorpe EXPORT_SYMBOL(ib_port_register_client_groups);
1473c87e65cfSLeon Romanovsky 
ib_port_unregister_client_groups(struct ib_device * ibdev,u32 port_num,const struct attribute_group ** groups)1474526a12c8SJason Gunthorpe void ib_port_unregister_client_groups(struct ib_device *ibdev, u32 port_num,
1475526a12c8SJason Gunthorpe 				      const struct attribute_group **groups)
1476c87e65cfSLeon Romanovsky {
1477526a12c8SJason Gunthorpe 	return sysfs_remove_groups(&ibdev->port_data[port_num].sysfs->kobj,
1478526a12c8SJason Gunthorpe 				   groups);
1479c87e65cfSLeon Romanovsky }
1480526a12c8SJason Gunthorpe EXPORT_SYMBOL(ib_port_unregister_client_groups);
1481