162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include "core_priv.h"
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include <linux/slab.h>
3862306a36Sopenharmony_ci#include <linux/stat.h>
3962306a36Sopenharmony_ci#include <linux/string.h>
4062306a36Sopenharmony_ci#include <linux/netdevice.h>
4162306a36Sopenharmony_ci#include <linux/ethtool.h>
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#include <rdma/ib_mad.h>
4462306a36Sopenharmony_ci#include <rdma/ib_pma.h>
4562306a36Sopenharmony_ci#include <rdma/ib_cache.h>
4662306a36Sopenharmony_ci#include <rdma/rdma_counter.h>
4762306a36Sopenharmony_ci#include <rdma/ib_sysfs.h>
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistruct port_table_attribute {
5062306a36Sopenharmony_ci	struct ib_port_attribute attr;
5162306a36Sopenharmony_ci	char			name[8];
5262306a36Sopenharmony_ci	int			index;
5362306a36Sopenharmony_ci	__be16			attr_id;
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct gid_attr_group {
5762306a36Sopenharmony_ci	struct ib_port *port;
5862306a36Sopenharmony_ci	struct kobject kobj;
5962306a36Sopenharmony_ci	struct attribute_group groups[2];
6062306a36Sopenharmony_ci	const struct attribute_group *groups_list[3];
6162306a36Sopenharmony_ci	struct port_table_attribute attrs_list[];
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistruct ib_port {
6562306a36Sopenharmony_ci	struct kobject kobj;
6662306a36Sopenharmony_ci	struct ib_device *ibdev;
6762306a36Sopenharmony_ci	struct gid_attr_group *gid_attr_group;
6862306a36Sopenharmony_ci	struct hw_stats_port_data *hw_stats_data;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	struct attribute_group groups[3];
7162306a36Sopenharmony_ci	const struct attribute_group *groups_list[5];
7262306a36Sopenharmony_ci	u32 port_num;
7362306a36Sopenharmony_ci	struct port_table_attribute attrs_list[];
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistruct hw_stats_device_attribute {
7762306a36Sopenharmony_ci	struct device_attribute attr;
7862306a36Sopenharmony_ci	ssize_t (*show)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
7962306a36Sopenharmony_ci			unsigned int index, unsigned int port_num, char *buf);
8062306a36Sopenharmony_ci	ssize_t (*store)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
8162306a36Sopenharmony_ci			 unsigned int index, unsigned int port_num,
8262306a36Sopenharmony_ci			 const char *buf, size_t count);
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistruct hw_stats_port_attribute {
8662306a36Sopenharmony_ci	struct ib_port_attribute attr;
8762306a36Sopenharmony_ci	ssize_t (*show)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
8862306a36Sopenharmony_ci			unsigned int index, unsigned int port_num, char *buf);
8962306a36Sopenharmony_ci	ssize_t (*store)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
9062306a36Sopenharmony_ci			 unsigned int index, unsigned int port_num,
9162306a36Sopenharmony_ci			 const char *buf, size_t count);
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistruct hw_stats_device_data {
9562306a36Sopenharmony_ci	struct attribute_group group;
9662306a36Sopenharmony_ci	struct rdma_hw_stats *stats;
9762306a36Sopenharmony_ci	struct hw_stats_device_attribute attrs[];
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cistruct hw_stats_port_data {
10162306a36Sopenharmony_ci	struct rdma_hw_stats *stats;
10262306a36Sopenharmony_ci	struct hw_stats_port_attribute attrs[];
10362306a36Sopenharmony_ci};
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic ssize_t port_attr_show(struct kobject *kobj,
10662306a36Sopenharmony_ci			      struct attribute *attr, char *buf)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct ib_port_attribute *port_attr =
10962306a36Sopenharmony_ci		container_of(attr, struct ib_port_attribute, attr);
11062306a36Sopenharmony_ci	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	if (!port_attr->show)
11362306a36Sopenharmony_ci		return -EIO;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	return port_attr->show(p->ibdev, p->port_num, port_attr, buf);
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic ssize_t port_attr_store(struct kobject *kobj,
11962306a36Sopenharmony_ci			       struct attribute *attr,
12062306a36Sopenharmony_ci			       const char *buf, size_t count)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct ib_port_attribute *port_attr =
12362306a36Sopenharmony_ci		container_of(attr, struct ib_port_attribute, attr);
12462306a36Sopenharmony_ci	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	if (!port_attr->store)
12762306a36Sopenharmony_ci		return -EIO;
12862306a36Sopenharmony_ci	return port_attr->store(p->ibdev, p->port_num, port_attr, buf, count);
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistruct ib_device *ib_port_sysfs_get_ibdev_kobj(struct kobject *kobj,
13262306a36Sopenharmony_ci					       u32 *port_num)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	struct ib_port *port = container_of(kobj, struct ib_port, kobj);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	*port_num = port->port_num;
13762306a36Sopenharmony_ci	return port->ibdev;
13862306a36Sopenharmony_ci}
13962306a36Sopenharmony_ciEXPORT_SYMBOL(ib_port_sysfs_get_ibdev_kobj);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_cistatic const struct sysfs_ops port_sysfs_ops = {
14262306a36Sopenharmony_ci	.show	= port_attr_show,
14362306a36Sopenharmony_ci	.store	= port_attr_store
14462306a36Sopenharmony_ci};
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cistatic ssize_t hw_stat_device_show(struct device *dev,
14762306a36Sopenharmony_ci				   struct device_attribute *attr, char *buf)
14862306a36Sopenharmony_ci{
14962306a36Sopenharmony_ci	struct hw_stats_device_attribute *stat_attr =
15062306a36Sopenharmony_ci		container_of(attr, struct hw_stats_device_attribute, attr);
15162306a36Sopenharmony_ci	struct ib_device *ibdev = container_of(dev, struct ib_device, dev);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	return stat_attr->show(ibdev, ibdev->hw_stats_data->stats,
15462306a36Sopenharmony_ci			       stat_attr - ibdev->hw_stats_data->attrs, 0, buf);
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cistatic ssize_t hw_stat_device_store(struct device *dev,
15862306a36Sopenharmony_ci				    struct device_attribute *attr,
15962306a36Sopenharmony_ci				    const char *buf, size_t count)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	struct hw_stats_device_attribute *stat_attr =
16262306a36Sopenharmony_ci		container_of(attr, struct hw_stats_device_attribute, attr);
16362306a36Sopenharmony_ci	struct ib_device *ibdev = container_of(dev, struct ib_device, dev);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	return stat_attr->store(ibdev, ibdev->hw_stats_data->stats,
16662306a36Sopenharmony_ci				stat_attr - ibdev->hw_stats_data->attrs, 0, buf,
16762306a36Sopenharmony_ci				count);
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic ssize_t hw_stat_port_show(struct ib_device *ibdev, u32 port_num,
17162306a36Sopenharmony_ci				 struct ib_port_attribute *attr, char *buf)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	struct hw_stats_port_attribute *stat_attr =
17462306a36Sopenharmony_ci		container_of(attr, struct hw_stats_port_attribute, attr);
17562306a36Sopenharmony_ci	struct ib_port *port = ibdev->port_data[port_num].sysfs;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	return stat_attr->show(ibdev, port->hw_stats_data->stats,
17862306a36Sopenharmony_ci			       stat_attr - port->hw_stats_data->attrs,
17962306a36Sopenharmony_ci			       port->port_num, buf);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic ssize_t hw_stat_port_store(struct ib_device *ibdev, u32 port_num,
18362306a36Sopenharmony_ci				  struct ib_port_attribute *attr,
18462306a36Sopenharmony_ci				  const char *buf, size_t count)
18562306a36Sopenharmony_ci{
18662306a36Sopenharmony_ci	struct hw_stats_port_attribute *stat_attr =
18762306a36Sopenharmony_ci		container_of(attr, struct hw_stats_port_attribute, attr);
18862306a36Sopenharmony_ci	struct ib_port *port = ibdev->port_data[port_num].sysfs;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	return stat_attr->store(ibdev, port->hw_stats_data->stats,
19162306a36Sopenharmony_ci				stat_attr - port->hw_stats_data->attrs,
19262306a36Sopenharmony_ci				port->port_num, buf, count);
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic ssize_t gid_attr_show(struct kobject *kobj,
19662306a36Sopenharmony_ci			     struct attribute *attr, char *buf)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	struct ib_port_attribute *port_attr =
19962306a36Sopenharmony_ci		container_of(attr, struct ib_port_attribute, attr);
20062306a36Sopenharmony_ci	struct ib_port *p = container_of(kobj, struct gid_attr_group,
20162306a36Sopenharmony_ci					 kobj)->port;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	if (!port_attr->show)
20462306a36Sopenharmony_ci		return -EIO;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	return port_attr->show(p->ibdev, p->port_num, port_attr, buf);
20762306a36Sopenharmony_ci}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_cistatic const struct sysfs_ops gid_attr_sysfs_ops = {
21062306a36Sopenharmony_ci	.show = gid_attr_show
21162306a36Sopenharmony_ci};
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic ssize_t state_show(struct ib_device *ibdev, u32 port_num,
21462306a36Sopenharmony_ci			  struct ib_port_attribute *unused, char *buf)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	struct ib_port_attr attr;
21762306a36Sopenharmony_ci	ssize_t ret;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	static const char *state_name[] = {
22062306a36Sopenharmony_ci		[IB_PORT_NOP]		= "NOP",
22162306a36Sopenharmony_ci		[IB_PORT_DOWN]		= "DOWN",
22262306a36Sopenharmony_ci		[IB_PORT_INIT]		= "INIT",
22362306a36Sopenharmony_ci		[IB_PORT_ARMED]		= "ARMED",
22462306a36Sopenharmony_ci		[IB_PORT_ACTIVE]	= "ACTIVE",
22562306a36Sopenharmony_ci		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
22662306a36Sopenharmony_ci	};
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
22962306a36Sopenharmony_ci	if (ret)
23062306a36Sopenharmony_ci		return ret;
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	return sysfs_emit(buf, "%d: %s\n", attr.state,
23362306a36Sopenharmony_ci			  attr.state >= 0 &&
23462306a36Sopenharmony_ci					  attr.state < ARRAY_SIZE(state_name) ?
23562306a36Sopenharmony_ci				  state_name[attr.state] :
23662306a36Sopenharmony_ci				  "UNKNOWN");
23762306a36Sopenharmony_ci}
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_cistatic ssize_t lid_show(struct ib_device *ibdev, u32 port_num,
24062306a36Sopenharmony_ci			struct ib_port_attribute *unused, char *buf)
24162306a36Sopenharmony_ci{
24262306a36Sopenharmony_ci	struct ib_port_attr attr;
24362306a36Sopenharmony_ci	ssize_t ret;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
24662306a36Sopenharmony_ci	if (ret)
24762306a36Sopenharmony_ci		return ret;
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	return sysfs_emit(buf, "0x%x\n", attr.lid);
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_cistatic ssize_t lid_mask_count_show(struct ib_device *ibdev, u32 port_num,
25362306a36Sopenharmony_ci				   struct ib_port_attribute *unused, char *buf)
25462306a36Sopenharmony_ci{
25562306a36Sopenharmony_ci	struct ib_port_attr attr;
25662306a36Sopenharmony_ci	ssize_t ret;
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
25962306a36Sopenharmony_ci	if (ret)
26062306a36Sopenharmony_ci		return ret;
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	return sysfs_emit(buf, "%u\n", attr.lmc);
26362306a36Sopenharmony_ci}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_cistatic ssize_t sm_lid_show(struct ib_device *ibdev, u32 port_num,
26662306a36Sopenharmony_ci			   struct ib_port_attribute *unused, char *buf)
26762306a36Sopenharmony_ci{
26862306a36Sopenharmony_ci	struct ib_port_attr attr;
26962306a36Sopenharmony_ci	ssize_t ret;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
27262306a36Sopenharmony_ci	if (ret)
27362306a36Sopenharmony_ci		return ret;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	return sysfs_emit(buf, "0x%x\n", attr.sm_lid);
27662306a36Sopenharmony_ci}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_cistatic ssize_t sm_sl_show(struct ib_device *ibdev, u32 port_num,
27962306a36Sopenharmony_ci			  struct ib_port_attribute *unused, char *buf)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	struct ib_port_attr attr;
28262306a36Sopenharmony_ci	ssize_t ret;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
28562306a36Sopenharmony_ci	if (ret)
28662306a36Sopenharmony_ci		return ret;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	return sysfs_emit(buf, "%u\n", attr.sm_sl);
28962306a36Sopenharmony_ci}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistatic ssize_t cap_mask_show(struct ib_device *ibdev, u32 port_num,
29262306a36Sopenharmony_ci			     struct ib_port_attribute *unused, char *buf)
29362306a36Sopenharmony_ci{
29462306a36Sopenharmony_ci	struct ib_port_attr attr;
29562306a36Sopenharmony_ci	ssize_t ret;
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
29862306a36Sopenharmony_ci	if (ret)
29962306a36Sopenharmony_ci		return ret;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	return sysfs_emit(buf, "0x%08x\n", attr.port_cap_flags);
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_cistatic ssize_t rate_show(struct ib_device *ibdev, u32 port_num,
30562306a36Sopenharmony_ci			 struct ib_port_attribute *unused, char *buf)
30662306a36Sopenharmony_ci{
30762306a36Sopenharmony_ci	struct ib_port_attr attr;
30862306a36Sopenharmony_ci	char *speed = "";
30962306a36Sopenharmony_ci	int rate;		/* in deci-Gb/sec */
31062306a36Sopenharmony_ci	ssize_t ret;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
31362306a36Sopenharmony_ci	if (ret)
31462306a36Sopenharmony_ci		return ret;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	switch (attr.active_speed) {
31762306a36Sopenharmony_ci	case IB_SPEED_DDR:
31862306a36Sopenharmony_ci		speed = " DDR";
31962306a36Sopenharmony_ci		rate = 50;
32062306a36Sopenharmony_ci		break;
32162306a36Sopenharmony_ci	case IB_SPEED_QDR:
32262306a36Sopenharmony_ci		speed = " QDR";
32362306a36Sopenharmony_ci		rate = 100;
32462306a36Sopenharmony_ci		break;
32562306a36Sopenharmony_ci	case IB_SPEED_FDR10:
32662306a36Sopenharmony_ci		speed = " FDR10";
32762306a36Sopenharmony_ci		rate = 100;
32862306a36Sopenharmony_ci		break;
32962306a36Sopenharmony_ci	case IB_SPEED_FDR:
33062306a36Sopenharmony_ci		speed = " FDR";
33162306a36Sopenharmony_ci		rate = 140;
33262306a36Sopenharmony_ci		break;
33362306a36Sopenharmony_ci	case IB_SPEED_EDR:
33462306a36Sopenharmony_ci		speed = " EDR";
33562306a36Sopenharmony_ci		rate = 250;
33662306a36Sopenharmony_ci		break;
33762306a36Sopenharmony_ci	case IB_SPEED_HDR:
33862306a36Sopenharmony_ci		speed = " HDR";
33962306a36Sopenharmony_ci		rate = 500;
34062306a36Sopenharmony_ci		break;
34162306a36Sopenharmony_ci	case IB_SPEED_NDR:
34262306a36Sopenharmony_ci		speed = " NDR";
34362306a36Sopenharmony_ci		rate = 1000;
34462306a36Sopenharmony_ci		break;
34562306a36Sopenharmony_ci	case IB_SPEED_SDR:
34662306a36Sopenharmony_ci	default:		/* default to SDR for invalid rates */
34762306a36Sopenharmony_ci		speed = " SDR";
34862306a36Sopenharmony_ci		rate = 25;
34962306a36Sopenharmony_ci		break;
35062306a36Sopenharmony_ci	}
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	rate *= ib_width_enum_to_int(attr.active_width);
35362306a36Sopenharmony_ci	if (rate < 0)
35462306a36Sopenharmony_ci		return -EINVAL;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	return sysfs_emit(buf, "%d%s Gb/sec (%dX%s)\n", rate / 10,
35762306a36Sopenharmony_ci			  rate % 10 ? ".5" : "",
35862306a36Sopenharmony_ci			  ib_width_enum_to_int(attr.active_width), speed);
35962306a36Sopenharmony_ci}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_cistatic const char *phys_state_to_str(enum ib_port_phys_state phys_state)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	static const char *phys_state_str[] = {
36462306a36Sopenharmony_ci		"<unknown>",
36562306a36Sopenharmony_ci		"Sleep",
36662306a36Sopenharmony_ci		"Polling",
36762306a36Sopenharmony_ci		"Disabled",
36862306a36Sopenharmony_ci		"PortConfigurationTraining",
36962306a36Sopenharmony_ci		"LinkUp",
37062306a36Sopenharmony_ci		"LinkErrorRecovery",
37162306a36Sopenharmony_ci		"Phy Test",
37262306a36Sopenharmony_ci	};
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	if (phys_state < ARRAY_SIZE(phys_state_str))
37562306a36Sopenharmony_ci		return phys_state_str[phys_state];
37662306a36Sopenharmony_ci	return "<unknown>";
37762306a36Sopenharmony_ci}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_cistatic ssize_t phys_state_show(struct ib_device *ibdev, u32 port_num,
38062306a36Sopenharmony_ci			       struct ib_port_attribute *unused, char *buf)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	struct ib_port_attr attr;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	ssize_t ret;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	ret = ib_query_port(ibdev, port_num, &attr);
38762306a36Sopenharmony_ci	if (ret)
38862306a36Sopenharmony_ci		return ret;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	return sysfs_emit(buf, "%u: %s\n", attr.phys_state,
39162306a36Sopenharmony_ci			  phys_state_to_str(attr.phys_state));
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_cistatic ssize_t link_layer_show(struct ib_device *ibdev, u32 port_num,
39562306a36Sopenharmony_ci			       struct ib_port_attribute *unused, char *buf)
39662306a36Sopenharmony_ci{
39762306a36Sopenharmony_ci	const char *output;
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	switch (rdma_port_get_link_layer(ibdev, port_num)) {
40062306a36Sopenharmony_ci	case IB_LINK_LAYER_INFINIBAND:
40162306a36Sopenharmony_ci		output = "InfiniBand";
40262306a36Sopenharmony_ci		break;
40362306a36Sopenharmony_ci	case IB_LINK_LAYER_ETHERNET:
40462306a36Sopenharmony_ci		output = "Ethernet";
40562306a36Sopenharmony_ci		break;
40662306a36Sopenharmony_ci	default:
40762306a36Sopenharmony_ci		output = "Unknown";
40862306a36Sopenharmony_ci		break;
40962306a36Sopenharmony_ci	}
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	return sysfs_emit(buf, "%s\n", output);
41262306a36Sopenharmony_ci}
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(state);
41562306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(lid);
41662306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(lid_mask_count);
41762306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(sm_lid);
41862306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(sm_sl);
41962306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(cap_mask);
42062306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(rate);
42162306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(phys_state);
42262306a36Sopenharmony_cistatic IB_PORT_ATTR_RO(link_layer);
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_cistatic struct attribute *port_default_attrs[] = {
42562306a36Sopenharmony_ci	&ib_port_attr_state.attr,
42662306a36Sopenharmony_ci	&ib_port_attr_lid.attr,
42762306a36Sopenharmony_ci	&ib_port_attr_lid_mask_count.attr,
42862306a36Sopenharmony_ci	&ib_port_attr_sm_lid.attr,
42962306a36Sopenharmony_ci	&ib_port_attr_sm_sl.attr,
43062306a36Sopenharmony_ci	&ib_port_attr_cap_mask.attr,
43162306a36Sopenharmony_ci	&ib_port_attr_rate.attr,
43262306a36Sopenharmony_ci	&ib_port_attr_phys_state.attr,
43362306a36Sopenharmony_ci	&ib_port_attr_link_layer.attr,
43462306a36Sopenharmony_ci	NULL
43562306a36Sopenharmony_ci};
43662306a36Sopenharmony_ciATTRIBUTE_GROUPS(port_default);
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_cistatic ssize_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
43962306a36Sopenharmony_ci{
44062306a36Sopenharmony_ci	struct net_device *ndev;
44162306a36Sopenharmony_ci	int ret = -EINVAL;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	rcu_read_lock();
44462306a36Sopenharmony_ci	ndev = rcu_dereference(gid_attr->ndev);
44562306a36Sopenharmony_ci	if (ndev)
44662306a36Sopenharmony_ci		ret = sysfs_emit(buf, "%s\n", ndev->name);
44762306a36Sopenharmony_ci	rcu_read_unlock();
44862306a36Sopenharmony_ci	return ret;
44962306a36Sopenharmony_ci}
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_cistatic ssize_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
45262306a36Sopenharmony_ci{
45362306a36Sopenharmony_ci	return sysfs_emit(buf, "%s\n",
45462306a36Sopenharmony_ci			  ib_cache_gid_type_str(gid_attr->gid_type));
45562306a36Sopenharmony_ci}
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_cistatic ssize_t _show_port_gid_attr(
45862306a36Sopenharmony_ci	struct ib_device *ibdev, u32 port_num, struct ib_port_attribute *attr,
45962306a36Sopenharmony_ci	char *buf,
46062306a36Sopenharmony_ci	ssize_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
46162306a36Sopenharmony_ci{
46262306a36Sopenharmony_ci	struct port_table_attribute *tab_attr =
46362306a36Sopenharmony_ci		container_of(attr, struct port_table_attribute, attr);
46462306a36Sopenharmony_ci	const struct ib_gid_attr *gid_attr;
46562306a36Sopenharmony_ci	ssize_t ret;
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	gid_attr = rdma_get_gid_attr(ibdev, port_num, tab_attr->index);
46862306a36Sopenharmony_ci	if (IS_ERR(gid_attr))
46962306a36Sopenharmony_ci		/* -EINVAL is returned for user space compatibility reasons. */
47062306a36Sopenharmony_ci		return -EINVAL;
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci	ret = print(gid_attr, buf);
47362306a36Sopenharmony_ci	rdma_put_gid_attr(gid_attr);
47462306a36Sopenharmony_ci	return ret;
47562306a36Sopenharmony_ci}
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_cistatic ssize_t show_port_gid(struct ib_device *ibdev, u32 port_num,
47862306a36Sopenharmony_ci			     struct ib_port_attribute *attr, char *buf)
47962306a36Sopenharmony_ci{
48062306a36Sopenharmony_ci	struct port_table_attribute *tab_attr =
48162306a36Sopenharmony_ci		container_of(attr, struct port_table_attribute, attr);
48262306a36Sopenharmony_ci	const struct ib_gid_attr *gid_attr;
48362306a36Sopenharmony_ci	int len;
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	gid_attr = rdma_get_gid_attr(ibdev, port_num, tab_attr->index);
48662306a36Sopenharmony_ci	if (IS_ERR(gid_attr)) {
48762306a36Sopenharmony_ci		const union ib_gid zgid = {};
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci		/* If reading GID fails, it is likely due to GID entry being
49062306a36Sopenharmony_ci		 * empty (invalid) or reserved GID in the table.  User space
49162306a36Sopenharmony_ci		 * expects to read GID table entries as long as it given index
49262306a36Sopenharmony_ci		 * is within GID table size.  Administrative/debugging tool
49362306a36Sopenharmony_ci		 * fails to query rest of the GID entries if it hits error
49462306a36Sopenharmony_ci		 * while querying a GID of the given index.  To avoid user
49562306a36Sopenharmony_ci		 * space throwing such error on fail to read gid, return zero
49662306a36Sopenharmony_ci		 * GID as before. This maintains backward compatibility.
49762306a36Sopenharmony_ci		 */
49862306a36Sopenharmony_ci		return sysfs_emit(buf, "%pI6\n", zgid.raw);
49962306a36Sopenharmony_ci	}
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	len = sysfs_emit(buf, "%pI6\n", gid_attr->gid.raw);
50262306a36Sopenharmony_ci	rdma_put_gid_attr(gid_attr);
50362306a36Sopenharmony_ci	return len;
50462306a36Sopenharmony_ci}
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_cistatic ssize_t show_port_gid_attr_ndev(struct ib_device *ibdev, u32 port_num,
50762306a36Sopenharmony_ci				       struct ib_port_attribute *attr,
50862306a36Sopenharmony_ci				       char *buf)
50962306a36Sopenharmony_ci{
51062306a36Sopenharmony_ci	return _show_port_gid_attr(ibdev, port_num, attr, buf, print_ndev);
51162306a36Sopenharmony_ci}
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_cistatic ssize_t show_port_gid_attr_gid_type(struct ib_device *ibdev,
51462306a36Sopenharmony_ci					   u32 port_num,
51562306a36Sopenharmony_ci					   struct ib_port_attribute *attr,
51662306a36Sopenharmony_ci					   char *buf)
51762306a36Sopenharmony_ci{
51862306a36Sopenharmony_ci	return _show_port_gid_attr(ibdev, port_num, attr, buf, print_gid_type);
51962306a36Sopenharmony_ci}
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_cistatic ssize_t show_port_pkey(struct ib_device *ibdev, u32 port_num,
52262306a36Sopenharmony_ci			      struct ib_port_attribute *attr, char *buf)
52362306a36Sopenharmony_ci{
52462306a36Sopenharmony_ci	struct port_table_attribute *tab_attr =
52562306a36Sopenharmony_ci		container_of(attr, struct port_table_attribute, attr);
52662306a36Sopenharmony_ci	u16 pkey;
52762306a36Sopenharmony_ci	int ret;
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci	ret = ib_query_pkey(ibdev, port_num, tab_attr->index, &pkey);
53062306a36Sopenharmony_ci	if (ret)
53162306a36Sopenharmony_ci		return ret;
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	return sysfs_emit(buf, "0x%04x\n", pkey);
53462306a36Sopenharmony_ci}
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci#define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
53762306a36Sopenharmony_cistruct port_table_attribute port_pma_attr_##_name = {			\
53862306a36Sopenharmony_ci	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
53962306a36Sopenharmony_ci	.index = (_offset) | ((_width) << 16) | ((_counter) << 24),	\
54062306a36Sopenharmony_ci	.attr_id = IB_PMA_PORT_COUNTERS,				\
54162306a36Sopenharmony_ci}
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci#define PORT_PMA_ATTR_EXT(_name, _width, _offset)			\
54462306a36Sopenharmony_cistruct port_table_attribute port_pma_attr_ext_##_name = {		\
54562306a36Sopenharmony_ci	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
54662306a36Sopenharmony_ci	.index = (_offset) | ((_width) << 16),				\
54762306a36Sopenharmony_ci	.attr_id = IB_PMA_PORT_COUNTERS_EXT,				\
54862306a36Sopenharmony_ci}
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci/*
55162306a36Sopenharmony_ci * Get a Perfmgmt MAD block of data.
55262306a36Sopenharmony_ci * Returns error code or the number of bytes retrieved.
55362306a36Sopenharmony_ci */
55462306a36Sopenharmony_cistatic int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
55562306a36Sopenharmony_ci		void *data, int offset, size_t size)
55662306a36Sopenharmony_ci{
55762306a36Sopenharmony_ci	struct ib_mad *in_mad;
55862306a36Sopenharmony_ci	struct ib_mad *out_mad;
55962306a36Sopenharmony_ci	size_t mad_size = sizeof(*out_mad);
56062306a36Sopenharmony_ci	u16 out_mad_pkey_index = 0;
56162306a36Sopenharmony_ci	ssize_t ret;
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci	if (!dev->ops.process_mad)
56462306a36Sopenharmony_ci		return -ENOSYS;
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci	in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
56762306a36Sopenharmony_ci	out_mad = kzalloc(sizeof(*out_mad), GFP_KERNEL);
56862306a36Sopenharmony_ci	if (!in_mad || !out_mad) {
56962306a36Sopenharmony_ci		ret = -ENOMEM;
57062306a36Sopenharmony_ci		goto out;
57162306a36Sopenharmony_ci	}
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	in_mad->mad_hdr.base_version  = 1;
57462306a36Sopenharmony_ci	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
57562306a36Sopenharmony_ci	in_mad->mad_hdr.class_version = 1;
57662306a36Sopenharmony_ci	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
57762306a36Sopenharmony_ci	in_mad->mad_hdr.attr_id       = attr;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci	if (attr != IB_PMA_CLASS_PORT_INFO)
58062306a36Sopenharmony_ci		in_mad->data[41] = port_num;	/* PortSelect field */
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci	if ((dev->ops.process_mad(dev, IB_MAD_IGNORE_MKEY, port_num, NULL, NULL,
58362306a36Sopenharmony_ci				  in_mad, out_mad, &mad_size,
58462306a36Sopenharmony_ci				  &out_mad_pkey_index) &
58562306a36Sopenharmony_ci	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
58662306a36Sopenharmony_ci	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
58762306a36Sopenharmony_ci		ret = -EINVAL;
58862306a36Sopenharmony_ci		goto out;
58962306a36Sopenharmony_ci	}
59062306a36Sopenharmony_ci	memcpy(data, out_mad->data + offset, size);
59162306a36Sopenharmony_ci	ret = size;
59262306a36Sopenharmony_ciout:
59362306a36Sopenharmony_ci	kfree(in_mad);
59462306a36Sopenharmony_ci	kfree(out_mad);
59562306a36Sopenharmony_ci	return ret;
59662306a36Sopenharmony_ci}
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_cistatic ssize_t show_pma_counter(struct ib_device *ibdev, u32 port_num,
59962306a36Sopenharmony_ci				struct ib_port_attribute *attr, char *buf)
60062306a36Sopenharmony_ci{
60162306a36Sopenharmony_ci	struct port_table_attribute *tab_attr =
60262306a36Sopenharmony_ci		container_of(attr, struct port_table_attribute, attr);
60362306a36Sopenharmony_ci	int offset = tab_attr->index & 0xffff;
60462306a36Sopenharmony_ci	int width  = (tab_attr->index >> 16) & 0xff;
60562306a36Sopenharmony_ci	int ret;
60662306a36Sopenharmony_ci	u8 data[8];
60762306a36Sopenharmony_ci	int len;
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	ret = get_perf_mad(ibdev, port_num, tab_attr->attr_id, &data,
61062306a36Sopenharmony_ci			40 + offset / 8, sizeof(data));
61162306a36Sopenharmony_ci	if (ret < 0)
61262306a36Sopenharmony_ci		return ret;
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	switch (width) {
61562306a36Sopenharmony_ci	case 4:
61662306a36Sopenharmony_ci		len = sysfs_emit(buf, "%d\n",
61762306a36Sopenharmony_ci				 (*data >> (4 - (offset % 8))) & 0xf);
61862306a36Sopenharmony_ci		break;
61962306a36Sopenharmony_ci	case 8:
62062306a36Sopenharmony_ci		len = sysfs_emit(buf, "%u\n", *data);
62162306a36Sopenharmony_ci		break;
62262306a36Sopenharmony_ci	case 16:
62362306a36Sopenharmony_ci		len = sysfs_emit(buf, "%u\n", be16_to_cpup((__be16 *)data));
62462306a36Sopenharmony_ci		break;
62562306a36Sopenharmony_ci	case 32:
62662306a36Sopenharmony_ci		len = sysfs_emit(buf, "%u\n", be32_to_cpup((__be32 *)data));
62762306a36Sopenharmony_ci		break;
62862306a36Sopenharmony_ci	case 64:
62962306a36Sopenharmony_ci		len = sysfs_emit(buf, "%llu\n", be64_to_cpup((__be64 *)data));
63062306a36Sopenharmony_ci		break;
63162306a36Sopenharmony_ci	default:
63262306a36Sopenharmony_ci		len = 0;
63362306a36Sopenharmony_ci		break;
63462306a36Sopenharmony_ci	}
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci	return len;
63762306a36Sopenharmony_ci}
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_cistatic PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
64062306a36Sopenharmony_cistatic PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
64162306a36Sopenharmony_cistatic PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
64262306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
64362306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
64462306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
64562306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
64662306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
64762306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
64862306a36Sopenharmony_cistatic PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
64962306a36Sopenharmony_cistatic PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
65062306a36Sopenharmony_cistatic PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
65162306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
65262306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
65362306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
65462306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
65562306a36Sopenharmony_cistatic PORT_PMA_ATTR(port_xmit_wait		    ,  0, 32, 320);
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci/*
65862306a36Sopenharmony_ci * Counters added by extended set
65962306a36Sopenharmony_ci */
66062306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(port_xmit_data		    , 64,  64);
66162306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(port_rcv_data		    , 64, 128);
66262306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(port_xmit_packets	    , 64, 192);
66362306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(port_rcv_packets	    , 64, 256);
66462306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(unicast_xmit_packets	    , 64, 320);
66562306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(unicast_rcv_packets	    , 64, 384);
66662306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(multicast_xmit_packets	    , 64, 448);
66762306a36Sopenharmony_cistatic PORT_PMA_ATTR_EXT(multicast_rcv_packets	    , 64, 512);
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_cistatic struct attribute *pma_attrs[] = {
67062306a36Sopenharmony_ci	&port_pma_attr_symbol_error.attr.attr,
67162306a36Sopenharmony_ci	&port_pma_attr_link_error_recovery.attr.attr,
67262306a36Sopenharmony_ci	&port_pma_attr_link_downed.attr.attr,
67362306a36Sopenharmony_ci	&port_pma_attr_port_rcv_errors.attr.attr,
67462306a36Sopenharmony_ci	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
67562306a36Sopenharmony_ci	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
67662306a36Sopenharmony_ci	&port_pma_attr_port_xmit_discards.attr.attr,
67762306a36Sopenharmony_ci	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
67862306a36Sopenharmony_ci	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
67962306a36Sopenharmony_ci	&port_pma_attr_local_link_integrity_errors.attr.attr,
68062306a36Sopenharmony_ci	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
68162306a36Sopenharmony_ci	&port_pma_attr_VL15_dropped.attr.attr,
68262306a36Sopenharmony_ci	&port_pma_attr_port_xmit_data.attr.attr,
68362306a36Sopenharmony_ci	&port_pma_attr_port_rcv_data.attr.attr,
68462306a36Sopenharmony_ci	&port_pma_attr_port_xmit_packets.attr.attr,
68562306a36Sopenharmony_ci	&port_pma_attr_port_rcv_packets.attr.attr,
68662306a36Sopenharmony_ci	&port_pma_attr_port_xmit_wait.attr.attr,
68762306a36Sopenharmony_ci	NULL
68862306a36Sopenharmony_ci};
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_cistatic struct attribute *pma_attrs_ext[] = {
69162306a36Sopenharmony_ci	&port_pma_attr_symbol_error.attr.attr,
69262306a36Sopenharmony_ci	&port_pma_attr_link_error_recovery.attr.attr,
69362306a36Sopenharmony_ci	&port_pma_attr_link_downed.attr.attr,
69462306a36Sopenharmony_ci	&port_pma_attr_port_rcv_errors.attr.attr,
69562306a36Sopenharmony_ci	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
69662306a36Sopenharmony_ci	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
69762306a36Sopenharmony_ci	&port_pma_attr_port_xmit_discards.attr.attr,
69862306a36Sopenharmony_ci	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
69962306a36Sopenharmony_ci	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
70062306a36Sopenharmony_ci	&port_pma_attr_local_link_integrity_errors.attr.attr,
70162306a36Sopenharmony_ci	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
70262306a36Sopenharmony_ci	&port_pma_attr_VL15_dropped.attr.attr,
70362306a36Sopenharmony_ci	&port_pma_attr_ext_port_xmit_data.attr.attr,
70462306a36Sopenharmony_ci	&port_pma_attr_ext_port_rcv_data.attr.attr,
70562306a36Sopenharmony_ci	&port_pma_attr_ext_port_xmit_packets.attr.attr,
70662306a36Sopenharmony_ci	&port_pma_attr_port_xmit_wait.attr.attr,
70762306a36Sopenharmony_ci	&port_pma_attr_ext_port_rcv_packets.attr.attr,
70862306a36Sopenharmony_ci	&port_pma_attr_ext_unicast_rcv_packets.attr.attr,
70962306a36Sopenharmony_ci	&port_pma_attr_ext_unicast_xmit_packets.attr.attr,
71062306a36Sopenharmony_ci	&port_pma_attr_ext_multicast_rcv_packets.attr.attr,
71162306a36Sopenharmony_ci	&port_pma_attr_ext_multicast_xmit_packets.attr.attr,
71262306a36Sopenharmony_ci	NULL
71362306a36Sopenharmony_ci};
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_cistatic struct attribute *pma_attrs_noietf[] = {
71662306a36Sopenharmony_ci	&port_pma_attr_symbol_error.attr.attr,
71762306a36Sopenharmony_ci	&port_pma_attr_link_error_recovery.attr.attr,
71862306a36Sopenharmony_ci	&port_pma_attr_link_downed.attr.attr,
71962306a36Sopenharmony_ci	&port_pma_attr_port_rcv_errors.attr.attr,
72062306a36Sopenharmony_ci	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
72162306a36Sopenharmony_ci	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
72262306a36Sopenharmony_ci	&port_pma_attr_port_xmit_discards.attr.attr,
72362306a36Sopenharmony_ci	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
72462306a36Sopenharmony_ci	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
72562306a36Sopenharmony_ci	&port_pma_attr_local_link_integrity_errors.attr.attr,
72662306a36Sopenharmony_ci	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
72762306a36Sopenharmony_ci	&port_pma_attr_VL15_dropped.attr.attr,
72862306a36Sopenharmony_ci	&port_pma_attr_ext_port_xmit_data.attr.attr,
72962306a36Sopenharmony_ci	&port_pma_attr_ext_port_rcv_data.attr.attr,
73062306a36Sopenharmony_ci	&port_pma_attr_ext_port_xmit_packets.attr.attr,
73162306a36Sopenharmony_ci	&port_pma_attr_ext_port_rcv_packets.attr.attr,
73262306a36Sopenharmony_ci	&port_pma_attr_port_xmit_wait.attr.attr,
73362306a36Sopenharmony_ci	NULL
73462306a36Sopenharmony_ci};
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_cistatic const struct attribute_group pma_group = {
73762306a36Sopenharmony_ci	.name  = "counters",
73862306a36Sopenharmony_ci	.attrs  = pma_attrs
73962306a36Sopenharmony_ci};
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_cistatic const struct attribute_group pma_group_ext = {
74262306a36Sopenharmony_ci	.name  = "counters",
74362306a36Sopenharmony_ci	.attrs  = pma_attrs_ext
74462306a36Sopenharmony_ci};
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_cistatic const struct attribute_group pma_group_noietf = {
74762306a36Sopenharmony_ci	.name  = "counters",
74862306a36Sopenharmony_ci	.attrs  = pma_attrs_noietf
74962306a36Sopenharmony_ci};
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_cistatic void ib_port_release(struct kobject *kobj)
75262306a36Sopenharmony_ci{
75362306a36Sopenharmony_ci	struct ib_port *port = container_of(kobj, struct ib_port, kobj);
75462306a36Sopenharmony_ci	int i;
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_ci	for (i = 0; i != ARRAY_SIZE(port->groups); i++)
75762306a36Sopenharmony_ci		kfree(port->groups[i].attrs);
75862306a36Sopenharmony_ci	if (port->hw_stats_data)
75962306a36Sopenharmony_ci		rdma_free_hw_stats_struct(port->hw_stats_data->stats);
76062306a36Sopenharmony_ci	kfree(port->hw_stats_data);
76162306a36Sopenharmony_ci	kvfree(port);
76262306a36Sopenharmony_ci}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_cistatic void ib_port_gid_attr_release(struct kobject *kobj)
76562306a36Sopenharmony_ci{
76662306a36Sopenharmony_ci	struct gid_attr_group *gid_attr_group =
76762306a36Sopenharmony_ci		container_of(kobj, struct gid_attr_group, kobj);
76862306a36Sopenharmony_ci	int i;
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci	for (i = 0; i != ARRAY_SIZE(gid_attr_group->groups); i++)
77162306a36Sopenharmony_ci		kfree(gid_attr_group->groups[i].attrs);
77262306a36Sopenharmony_ci	kfree(gid_attr_group);
77362306a36Sopenharmony_ci}
77462306a36Sopenharmony_ci
77562306a36Sopenharmony_cistatic struct kobj_type port_type = {
77662306a36Sopenharmony_ci	.release       = ib_port_release,
77762306a36Sopenharmony_ci	.sysfs_ops     = &port_sysfs_ops,
77862306a36Sopenharmony_ci	.default_groups = port_default_groups,
77962306a36Sopenharmony_ci};
78062306a36Sopenharmony_ci
78162306a36Sopenharmony_cistatic struct kobj_type gid_attr_type = {
78262306a36Sopenharmony_ci	.sysfs_ops      = &gid_attr_sysfs_ops,
78362306a36Sopenharmony_ci	.release        = ib_port_gid_attr_release
78462306a36Sopenharmony_ci};
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci/*
78762306a36Sopenharmony_ci * Figure out which counter table to use depending on
78862306a36Sopenharmony_ci * the device capabilities.
78962306a36Sopenharmony_ci */
79062306a36Sopenharmony_cistatic const struct attribute_group *get_counter_table(struct ib_device *dev,
79162306a36Sopenharmony_ci						       int port_num)
79262306a36Sopenharmony_ci{
79362306a36Sopenharmony_ci	struct ib_class_port_info cpi;
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
79662306a36Sopenharmony_ci				&cpi, 40, sizeof(cpi)) >= 0) {
79762306a36Sopenharmony_ci		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
79862306a36Sopenharmony_ci			/* We have extended counters */
79962306a36Sopenharmony_ci			return &pma_group_ext;
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
80262306a36Sopenharmony_ci			/* But not the IETF ones */
80362306a36Sopenharmony_ci			return &pma_group_noietf;
80462306a36Sopenharmony_ci	}
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci	/* Fall back to normal counters */
80762306a36Sopenharmony_ci	return &pma_group;
80862306a36Sopenharmony_ci}
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_cistatic int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
81162306a36Sopenharmony_ci			   u32 port_num, int index)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	int ret;
81462306a36Sopenharmony_ci
81562306a36Sopenharmony_ci	if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
81662306a36Sopenharmony_ci		return 0;
81762306a36Sopenharmony_ci	ret = dev->ops.get_hw_stats(dev, stats, port_num, index);
81862306a36Sopenharmony_ci	if (ret < 0)
81962306a36Sopenharmony_ci		return ret;
82062306a36Sopenharmony_ci	if (ret == stats->num_counters)
82162306a36Sopenharmony_ci		stats->timestamp = jiffies;
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	return 0;
82462306a36Sopenharmony_ci}
82562306a36Sopenharmony_ci
82662306a36Sopenharmony_cistatic int print_hw_stat(struct ib_device *dev, int port_num,
82762306a36Sopenharmony_ci			 struct rdma_hw_stats *stats, int index, char *buf)
82862306a36Sopenharmony_ci{
82962306a36Sopenharmony_ci	u64 v = rdma_counter_get_hwstat_value(dev, port_num, index);
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	return sysfs_emit(buf, "%llu\n", stats->value[index] + v);
83262306a36Sopenharmony_ci}
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_cistatic ssize_t show_hw_stats(struct ib_device *ibdev,
83562306a36Sopenharmony_ci			     struct rdma_hw_stats *stats, unsigned int index,
83662306a36Sopenharmony_ci			     unsigned int port_num, char *buf)
83762306a36Sopenharmony_ci{
83862306a36Sopenharmony_ci	int ret;
83962306a36Sopenharmony_ci
84062306a36Sopenharmony_ci	mutex_lock(&stats->lock);
84162306a36Sopenharmony_ci	ret = update_hw_stats(ibdev, stats, port_num, index);
84262306a36Sopenharmony_ci	if (ret)
84362306a36Sopenharmony_ci		goto unlock;
84462306a36Sopenharmony_ci	ret = print_hw_stat(ibdev, port_num, stats, index, buf);
84562306a36Sopenharmony_ciunlock:
84662306a36Sopenharmony_ci	mutex_unlock(&stats->lock);
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci	return ret;
84962306a36Sopenharmony_ci}
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_cistatic ssize_t show_stats_lifespan(struct ib_device *ibdev,
85262306a36Sopenharmony_ci				   struct rdma_hw_stats *stats,
85362306a36Sopenharmony_ci				   unsigned int index, unsigned int port_num,
85462306a36Sopenharmony_ci				   char *buf)
85562306a36Sopenharmony_ci{
85662306a36Sopenharmony_ci	int msecs;
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci	mutex_lock(&stats->lock);
85962306a36Sopenharmony_ci	msecs = jiffies_to_msecs(stats->lifespan);
86062306a36Sopenharmony_ci	mutex_unlock(&stats->lock);
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", msecs);
86362306a36Sopenharmony_ci}
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_cistatic ssize_t set_stats_lifespan(struct ib_device *ibdev,
86662306a36Sopenharmony_ci				   struct rdma_hw_stats *stats,
86762306a36Sopenharmony_ci				   unsigned int index, unsigned int port_num,
86862306a36Sopenharmony_ci				   const char *buf, size_t count)
86962306a36Sopenharmony_ci{
87062306a36Sopenharmony_ci	int msecs;
87162306a36Sopenharmony_ci	int jiffies;
87262306a36Sopenharmony_ci	int ret;
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci	ret = kstrtoint(buf, 10, &msecs);
87562306a36Sopenharmony_ci	if (ret)
87662306a36Sopenharmony_ci		return ret;
87762306a36Sopenharmony_ci	if (msecs < 0 || msecs > 10000)
87862306a36Sopenharmony_ci		return -EINVAL;
87962306a36Sopenharmony_ci	jiffies = msecs_to_jiffies(msecs);
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_ci	mutex_lock(&stats->lock);
88262306a36Sopenharmony_ci	stats->lifespan = jiffies;
88362306a36Sopenharmony_ci	mutex_unlock(&stats->lock);
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_ci	return count;
88662306a36Sopenharmony_ci}
88762306a36Sopenharmony_ci
88862306a36Sopenharmony_cistatic struct hw_stats_device_data *
88962306a36Sopenharmony_cialloc_hw_stats_device(struct ib_device *ibdev)
89062306a36Sopenharmony_ci{
89162306a36Sopenharmony_ci	struct hw_stats_device_data *data;
89262306a36Sopenharmony_ci	struct rdma_hw_stats *stats;
89362306a36Sopenharmony_ci
89462306a36Sopenharmony_ci	if (!ibdev->ops.alloc_hw_device_stats)
89562306a36Sopenharmony_ci		return ERR_PTR(-EOPNOTSUPP);
89662306a36Sopenharmony_ci	stats = ibdev->ops.alloc_hw_device_stats(ibdev);
89762306a36Sopenharmony_ci	if (!stats)
89862306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
89962306a36Sopenharmony_ci	if (!stats->descs || stats->num_counters <= 0)
90062306a36Sopenharmony_ci		goto err_free_stats;
90162306a36Sopenharmony_ci
90262306a36Sopenharmony_ci	/*
90362306a36Sopenharmony_ci	 * Two extra attribue elements here, one for the lifespan entry and
90462306a36Sopenharmony_ci	 * one to NULL terminate the list for the sysfs core code
90562306a36Sopenharmony_ci	 */
90662306a36Sopenharmony_ci	data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)),
90762306a36Sopenharmony_ci		       GFP_KERNEL);
90862306a36Sopenharmony_ci	if (!data)
90962306a36Sopenharmony_ci		goto err_free_stats;
91062306a36Sopenharmony_ci	data->group.attrs = kcalloc(stats->num_counters + 2,
91162306a36Sopenharmony_ci				    sizeof(*data->group.attrs), GFP_KERNEL);
91262306a36Sopenharmony_ci	if (!data->group.attrs)
91362306a36Sopenharmony_ci		goto err_free_data;
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci	data->group.name = "hw_counters";
91662306a36Sopenharmony_ci	data->stats = stats;
91762306a36Sopenharmony_ci	return data;
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_cierr_free_data:
92062306a36Sopenharmony_ci	kfree(data);
92162306a36Sopenharmony_cierr_free_stats:
92262306a36Sopenharmony_ci	rdma_free_hw_stats_struct(stats);
92362306a36Sopenharmony_ci	return ERR_PTR(-ENOMEM);
92462306a36Sopenharmony_ci}
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_civoid ib_device_release_hw_stats(struct hw_stats_device_data *data)
92762306a36Sopenharmony_ci{
92862306a36Sopenharmony_ci	kfree(data->group.attrs);
92962306a36Sopenharmony_ci	rdma_free_hw_stats_struct(data->stats);
93062306a36Sopenharmony_ci	kfree(data);
93162306a36Sopenharmony_ci}
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ciint ib_setup_device_attrs(struct ib_device *ibdev)
93462306a36Sopenharmony_ci{
93562306a36Sopenharmony_ci	struct hw_stats_device_attribute *attr;
93662306a36Sopenharmony_ci	struct hw_stats_device_data *data;
93762306a36Sopenharmony_ci	bool opstat_skipped = false;
93862306a36Sopenharmony_ci	int i, ret, pos = 0;
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	data = alloc_hw_stats_device(ibdev);
94162306a36Sopenharmony_ci	if (IS_ERR(data)) {
94262306a36Sopenharmony_ci		if (PTR_ERR(data) == -EOPNOTSUPP)
94362306a36Sopenharmony_ci			return 0;
94462306a36Sopenharmony_ci		return PTR_ERR(data);
94562306a36Sopenharmony_ci	}
94662306a36Sopenharmony_ci	ibdev->hw_stats_data = data;
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci	ret = ibdev->ops.get_hw_stats(ibdev, data->stats, 0,
94962306a36Sopenharmony_ci				      data->stats->num_counters);
95062306a36Sopenharmony_ci	if (ret != data->stats->num_counters) {
95162306a36Sopenharmony_ci		if (WARN_ON(ret >= 0))
95262306a36Sopenharmony_ci			return -EINVAL;
95362306a36Sopenharmony_ci		return ret;
95462306a36Sopenharmony_ci	}
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci	data->stats->timestamp = jiffies;
95762306a36Sopenharmony_ci
95862306a36Sopenharmony_ci	for (i = 0; i < data->stats->num_counters; i++) {
95962306a36Sopenharmony_ci		if (data->stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) {
96062306a36Sopenharmony_ci			opstat_skipped = true;
96162306a36Sopenharmony_ci			continue;
96262306a36Sopenharmony_ci		}
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci		WARN_ON(opstat_skipped);
96562306a36Sopenharmony_ci		attr = &data->attrs[pos];
96662306a36Sopenharmony_ci		sysfs_attr_init(&attr->attr.attr);
96762306a36Sopenharmony_ci		attr->attr.attr.name = data->stats->descs[i].name;
96862306a36Sopenharmony_ci		attr->attr.attr.mode = 0444;
96962306a36Sopenharmony_ci		attr->attr.show = hw_stat_device_show;
97062306a36Sopenharmony_ci		attr->show = show_hw_stats;
97162306a36Sopenharmony_ci		data->group.attrs[pos] = &attr->attr.attr;
97262306a36Sopenharmony_ci		pos++;
97362306a36Sopenharmony_ci	}
97462306a36Sopenharmony_ci
97562306a36Sopenharmony_ci	attr = &data->attrs[pos];
97662306a36Sopenharmony_ci	sysfs_attr_init(&attr->attr.attr);
97762306a36Sopenharmony_ci	attr->attr.attr.name = "lifespan";
97862306a36Sopenharmony_ci	attr->attr.attr.mode = 0644;
97962306a36Sopenharmony_ci	attr->attr.show = hw_stat_device_show;
98062306a36Sopenharmony_ci	attr->show = show_stats_lifespan;
98162306a36Sopenharmony_ci	attr->attr.store = hw_stat_device_store;
98262306a36Sopenharmony_ci	attr->store = set_stats_lifespan;
98362306a36Sopenharmony_ci	data->group.attrs[pos] = &attr->attr.attr;
98462306a36Sopenharmony_ci	for (i = 0; i != ARRAY_SIZE(ibdev->groups); i++)
98562306a36Sopenharmony_ci		if (!ibdev->groups[i]) {
98662306a36Sopenharmony_ci			ibdev->groups[i] = &data->group;
98762306a36Sopenharmony_ci			return 0;
98862306a36Sopenharmony_ci		}
98962306a36Sopenharmony_ci	WARN(true, "struct ib_device->groups is too small");
99062306a36Sopenharmony_ci	return -EINVAL;
99162306a36Sopenharmony_ci}
99262306a36Sopenharmony_ci
99362306a36Sopenharmony_cistatic struct hw_stats_port_data *
99462306a36Sopenharmony_cialloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
99562306a36Sopenharmony_ci{
99662306a36Sopenharmony_ci	struct ib_device *ibdev = port->ibdev;
99762306a36Sopenharmony_ci	struct hw_stats_port_data *data;
99862306a36Sopenharmony_ci	struct rdma_hw_stats *stats;
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ci	if (!ibdev->ops.alloc_hw_port_stats)
100162306a36Sopenharmony_ci		return ERR_PTR(-EOPNOTSUPP);
100262306a36Sopenharmony_ci	stats = ibdev->ops.alloc_hw_port_stats(port->ibdev, port->port_num);
100362306a36Sopenharmony_ci	if (!stats)
100462306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
100562306a36Sopenharmony_ci	if (!stats->descs || stats->num_counters <= 0)
100662306a36Sopenharmony_ci		goto err_free_stats;
100762306a36Sopenharmony_ci
100862306a36Sopenharmony_ci	/*
100962306a36Sopenharmony_ci	 * Two extra attribue elements here, one for the lifespan entry and
101062306a36Sopenharmony_ci	 * one to NULL terminate the list for the sysfs core code
101162306a36Sopenharmony_ci	 */
101262306a36Sopenharmony_ci	data = kzalloc(struct_size(data, attrs, size_add(stats->num_counters, 1)),
101362306a36Sopenharmony_ci		       GFP_KERNEL);
101462306a36Sopenharmony_ci	if (!data)
101562306a36Sopenharmony_ci		goto err_free_stats;
101662306a36Sopenharmony_ci	group->attrs = kcalloc(stats->num_counters + 2,
101762306a36Sopenharmony_ci				    sizeof(*group->attrs), GFP_KERNEL);
101862306a36Sopenharmony_ci	if (!group->attrs)
101962306a36Sopenharmony_ci		goto err_free_data;
102062306a36Sopenharmony_ci
102162306a36Sopenharmony_ci	group->name = "hw_counters";
102262306a36Sopenharmony_ci	data->stats = stats;
102362306a36Sopenharmony_ci	return data;
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_cierr_free_data:
102662306a36Sopenharmony_ci	kfree(data);
102762306a36Sopenharmony_cierr_free_stats:
102862306a36Sopenharmony_ci	rdma_free_hw_stats_struct(stats);
102962306a36Sopenharmony_ci	return ERR_PTR(-ENOMEM);
103062306a36Sopenharmony_ci}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_cistatic int setup_hw_port_stats(struct ib_port *port,
103362306a36Sopenharmony_ci			       struct attribute_group *group)
103462306a36Sopenharmony_ci{
103562306a36Sopenharmony_ci	struct hw_stats_port_attribute *attr;
103662306a36Sopenharmony_ci	struct hw_stats_port_data *data;
103762306a36Sopenharmony_ci	bool opstat_skipped = false;
103862306a36Sopenharmony_ci	int i, ret, pos = 0;
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_ci	data = alloc_hw_stats_port(port, group);
104162306a36Sopenharmony_ci	if (IS_ERR(data))
104262306a36Sopenharmony_ci		return PTR_ERR(data);
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	ret = port->ibdev->ops.get_hw_stats(port->ibdev, data->stats,
104562306a36Sopenharmony_ci					    port->port_num,
104662306a36Sopenharmony_ci					    data->stats->num_counters);
104762306a36Sopenharmony_ci	if (ret != data->stats->num_counters) {
104862306a36Sopenharmony_ci		if (WARN_ON(ret >= 0))
104962306a36Sopenharmony_ci			return -EINVAL;
105062306a36Sopenharmony_ci		return ret;
105162306a36Sopenharmony_ci	}
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	data->stats->timestamp = jiffies;
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci	for (i = 0; i < data->stats->num_counters; i++) {
105662306a36Sopenharmony_ci		if (data->stats->descs[i].flags & IB_STAT_FLAG_OPTIONAL) {
105762306a36Sopenharmony_ci			opstat_skipped = true;
105862306a36Sopenharmony_ci			continue;
105962306a36Sopenharmony_ci		}
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_ci		WARN_ON(opstat_skipped);
106262306a36Sopenharmony_ci		attr = &data->attrs[pos];
106362306a36Sopenharmony_ci		sysfs_attr_init(&attr->attr.attr);
106462306a36Sopenharmony_ci		attr->attr.attr.name = data->stats->descs[i].name;
106562306a36Sopenharmony_ci		attr->attr.attr.mode = 0444;
106662306a36Sopenharmony_ci		attr->attr.show = hw_stat_port_show;
106762306a36Sopenharmony_ci		attr->show = show_hw_stats;
106862306a36Sopenharmony_ci		group->attrs[pos] = &attr->attr.attr;
106962306a36Sopenharmony_ci		pos++;
107062306a36Sopenharmony_ci	}
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_ci	attr = &data->attrs[pos];
107362306a36Sopenharmony_ci	sysfs_attr_init(&attr->attr.attr);
107462306a36Sopenharmony_ci	attr->attr.attr.name = "lifespan";
107562306a36Sopenharmony_ci	attr->attr.attr.mode = 0644;
107662306a36Sopenharmony_ci	attr->attr.show = hw_stat_port_show;
107762306a36Sopenharmony_ci	attr->show = show_stats_lifespan;
107862306a36Sopenharmony_ci	attr->attr.store = hw_stat_port_store;
107962306a36Sopenharmony_ci	attr->store = set_stats_lifespan;
108062306a36Sopenharmony_ci	group->attrs[pos] = &attr->attr.attr;
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	port->hw_stats_data = data;
108362306a36Sopenharmony_ci	return 0;
108462306a36Sopenharmony_ci}
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_cistruct rdma_hw_stats *ib_get_hw_stats_port(struct ib_device *ibdev,
108762306a36Sopenharmony_ci					   u32 port_num)
108862306a36Sopenharmony_ci{
108962306a36Sopenharmony_ci	if (!ibdev->port_data || !rdma_is_port_valid(ibdev, port_num) ||
109062306a36Sopenharmony_ci	    !ibdev->port_data[port_num].sysfs->hw_stats_data)
109162306a36Sopenharmony_ci		return NULL;
109262306a36Sopenharmony_ci	return ibdev->port_data[port_num].sysfs->hw_stats_data->stats;
109362306a36Sopenharmony_ci}
109462306a36Sopenharmony_ci
109562306a36Sopenharmony_cistatic int
109662306a36Sopenharmony_cialloc_port_table_group(const char *name, struct attribute_group *group,
109762306a36Sopenharmony_ci		       struct port_table_attribute *attrs, size_t num,
109862306a36Sopenharmony_ci		       ssize_t (*show)(struct ib_device *ibdev, u32 port_num,
109962306a36Sopenharmony_ci				       struct ib_port_attribute *, char *buf))
110062306a36Sopenharmony_ci{
110162306a36Sopenharmony_ci	struct attribute **attr_list;
110262306a36Sopenharmony_ci	int i;
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci	attr_list = kcalloc(num + 1, sizeof(*attr_list), GFP_KERNEL);
110562306a36Sopenharmony_ci	if (!attr_list)
110662306a36Sopenharmony_ci		return -ENOMEM;
110762306a36Sopenharmony_ci
110862306a36Sopenharmony_ci	for (i = 0; i < num; i++) {
110962306a36Sopenharmony_ci		struct port_table_attribute *element = &attrs[i];
111062306a36Sopenharmony_ci
111162306a36Sopenharmony_ci		if (snprintf(element->name, sizeof(element->name), "%d", i) >=
111262306a36Sopenharmony_ci		    sizeof(element->name))
111362306a36Sopenharmony_ci			goto err;
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci		sysfs_attr_init(&element->attr.attr);
111662306a36Sopenharmony_ci		element->attr.attr.name = element->name;
111762306a36Sopenharmony_ci		element->attr.attr.mode = 0444;
111862306a36Sopenharmony_ci		element->attr.show = show;
111962306a36Sopenharmony_ci		element->index = i;
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci		attr_list[i] = &element->attr.attr;
112262306a36Sopenharmony_ci	}
112362306a36Sopenharmony_ci	group->name = name;
112462306a36Sopenharmony_ci	group->attrs = attr_list;
112562306a36Sopenharmony_ci	return 0;
112662306a36Sopenharmony_cierr:
112762306a36Sopenharmony_ci	kfree(attr_list);
112862306a36Sopenharmony_ci	return -EINVAL;
112962306a36Sopenharmony_ci}
113062306a36Sopenharmony_ci
113162306a36Sopenharmony_ci/*
113262306a36Sopenharmony_ci * Create the sysfs:
113362306a36Sopenharmony_ci *  ibp0s9/ports/XX/gid_attrs/{ndevs,types}/YYY
113462306a36Sopenharmony_ci * YYY is the gid table index in decimal
113562306a36Sopenharmony_ci */
113662306a36Sopenharmony_cistatic int setup_gid_attrs(struct ib_port *port,
113762306a36Sopenharmony_ci			   const struct ib_port_attr *attr)
113862306a36Sopenharmony_ci{
113962306a36Sopenharmony_ci	struct gid_attr_group *gid_attr_group;
114062306a36Sopenharmony_ci	int ret;
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci	gid_attr_group = kzalloc(struct_size(gid_attr_group, attrs_list,
114362306a36Sopenharmony_ci					     size_mul(attr->gid_tbl_len, 2)),
114462306a36Sopenharmony_ci				 GFP_KERNEL);
114562306a36Sopenharmony_ci	if (!gid_attr_group)
114662306a36Sopenharmony_ci		return -ENOMEM;
114762306a36Sopenharmony_ci	gid_attr_group->port = port;
114862306a36Sopenharmony_ci	kobject_init(&gid_attr_group->kobj, &gid_attr_type);
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci	ret = alloc_port_table_group("ndevs", &gid_attr_group->groups[0],
115162306a36Sopenharmony_ci				     gid_attr_group->attrs_list,
115262306a36Sopenharmony_ci				     attr->gid_tbl_len,
115362306a36Sopenharmony_ci				     show_port_gid_attr_ndev);
115462306a36Sopenharmony_ci	if (ret)
115562306a36Sopenharmony_ci		goto err_put;
115662306a36Sopenharmony_ci	gid_attr_group->groups_list[0] = &gid_attr_group->groups[0];
115762306a36Sopenharmony_ci
115862306a36Sopenharmony_ci	ret = alloc_port_table_group(
115962306a36Sopenharmony_ci		"types", &gid_attr_group->groups[1],
116062306a36Sopenharmony_ci		gid_attr_group->attrs_list + attr->gid_tbl_len,
116162306a36Sopenharmony_ci		attr->gid_tbl_len, show_port_gid_attr_gid_type);
116262306a36Sopenharmony_ci	if (ret)
116362306a36Sopenharmony_ci		goto err_put;
116462306a36Sopenharmony_ci	gid_attr_group->groups_list[1] = &gid_attr_group->groups[1];
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci	ret = kobject_add(&gid_attr_group->kobj, &port->kobj, "gid_attrs");
116762306a36Sopenharmony_ci	if (ret)
116862306a36Sopenharmony_ci		goto err_put;
116962306a36Sopenharmony_ci	ret = sysfs_create_groups(&gid_attr_group->kobj,
117062306a36Sopenharmony_ci				  gid_attr_group->groups_list);
117162306a36Sopenharmony_ci	if (ret)
117262306a36Sopenharmony_ci		goto err_del;
117362306a36Sopenharmony_ci	port->gid_attr_group = gid_attr_group;
117462306a36Sopenharmony_ci	return 0;
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_cierr_del:
117762306a36Sopenharmony_ci	kobject_del(&gid_attr_group->kobj);
117862306a36Sopenharmony_cierr_put:
117962306a36Sopenharmony_ci	kobject_put(&gid_attr_group->kobj);
118062306a36Sopenharmony_ci	return ret;
118162306a36Sopenharmony_ci}
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_cistatic void destroy_gid_attrs(struct ib_port *port)
118462306a36Sopenharmony_ci{
118562306a36Sopenharmony_ci	struct gid_attr_group *gid_attr_group = port->gid_attr_group;
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_ci	if (!gid_attr_group)
118862306a36Sopenharmony_ci		return;
118962306a36Sopenharmony_ci	sysfs_remove_groups(&gid_attr_group->kobj, gid_attr_group->groups_list);
119062306a36Sopenharmony_ci	kobject_del(&gid_attr_group->kobj);
119162306a36Sopenharmony_ci	kobject_put(&gid_attr_group->kobj);
119262306a36Sopenharmony_ci}
119362306a36Sopenharmony_ci
119462306a36Sopenharmony_ci/*
119562306a36Sopenharmony_ci * Create the sysfs:
119662306a36Sopenharmony_ci *  ibp0s9/ports/XX/{gids,pkeys,counters}/YYY
119762306a36Sopenharmony_ci */
119862306a36Sopenharmony_cistatic struct ib_port *setup_port(struct ib_core_device *coredev, int port_num,
119962306a36Sopenharmony_ci				  const struct ib_port_attr *attr)
120062306a36Sopenharmony_ci{
120162306a36Sopenharmony_ci	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
120262306a36Sopenharmony_ci	bool is_full_dev = &device->coredev == coredev;
120362306a36Sopenharmony_ci	const struct attribute_group **cur_group;
120462306a36Sopenharmony_ci	struct ib_port *p;
120562306a36Sopenharmony_ci	int ret;
120662306a36Sopenharmony_ci
120762306a36Sopenharmony_ci	p = kvzalloc(struct_size(p, attrs_list,
120862306a36Sopenharmony_ci				size_add(attr->gid_tbl_len, attr->pkey_tbl_len)),
120962306a36Sopenharmony_ci		     GFP_KERNEL);
121062306a36Sopenharmony_ci	if (!p)
121162306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
121262306a36Sopenharmony_ci	p->ibdev = device;
121362306a36Sopenharmony_ci	p->port_num = port_num;
121462306a36Sopenharmony_ci	kobject_init(&p->kobj, &port_type);
121562306a36Sopenharmony_ci
121662306a36Sopenharmony_ci	if (device->port_data && is_full_dev)
121762306a36Sopenharmony_ci		device->port_data[port_num].sysfs = p;
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ci	cur_group = p->groups_list;
122062306a36Sopenharmony_ci	ret = alloc_port_table_group("gids", &p->groups[0], p->attrs_list,
122162306a36Sopenharmony_ci				     attr->gid_tbl_len, show_port_gid);
122262306a36Sopenharmony_ci	if (ret)
122362306a36Sopenharmony_ci		goto err_put;
122462306a36Sopenharmony_ci	*cur_group++ = &p->groups[0];
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	if (attr->pkey_tbl_len) {
122762306a36Sopenharmony_ci		ret = alloc_port_table_group("pkeys", &p->groups[1],
122862306a36Sopenharmony_ci					     p->attrs_list + attr->gid_tbl_len,
122962306a36Sopenharmony_ci					     attr->pkey_tbl_len, show_port_pkey);
123062306a36Sopenharmony_ci		if (ret)
123162306a36Sopenharmony_ci			goto err_put;
123262306a36Sopenharmony_ci		*cur_group++ = &p->groups[1];
123362306a36Sopenharmony_ci	}
123462306a36Sopenharmony_ci
123562306a36Sopenharmony_ci	/*
123662306a36Sopenharmony_ci	 * If port == 0, it means hw_counters are per device and not per
123762306a36Sopenharmony_ci	 * port, so holder should be device. Therefore skip per port
123862306a36Sopenharmony_ci	 * counter initialization.
123962306a36Sopenharmony_ci	 */
124062306a36Sopenharmony_ci	if (port_num && is_full_dev) {
124162306a36Sopenharmony_ci		ret = setup_hw_port_stats(p, &p->groups[2]);
124262306a36Sopenharmony_ci		if (ret && ret != -EOPNOTSUPP)
124362306a36Sopenharmony_ci			goto err_put;
124462306a36Sopenharmony_ci		if (!ret)
124562306a36Sopenharmony_ci			*cur_group++ = &p->groups[2];
124662306a36Sopenharmony_ci	}
124762306a36Sopenharmony_ci
124862306a36Sopenharmony_ci	if (device->ops.process_mad && is_full_dev)
124962306a36Sopenharmony_ci		*cur_group++ = get_counter_table(device, port_num);
125062306a36Sopenharmony_ci
125162306a36Sopenharmony_ci	ret = kobject_add(&p->kobj, coredev->ports_kobj, "%d", port_num);
125262306a36Sopenharmony_ci	if (ret)
125362306a36Sopenharmony_ci		goto err_put;
125462306a36Sopenharmony_ci	ret = sysfs_create_groups(&p->kobj, p->groups_list);
125562306a36Sopenharmony_ci	if (ret)
125662306a36Sopenharmony_ci		goto err_del;
125762306a36Sopenharmony_ci	if (is_full_dev) {
125862306a36Sopenharmony_ci		ret = sysfs_create_groups(&p->kobj, device->ops.port_groups);
125962306a36Sopenharmony_ci		if (ret)
126062306a36Sopenharmony_ci			goto err_groups;
126162306a36Sopenharmony_ci	}
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_ci	list_add_tail(&p->kobj.entry, &coredev->port_list);
126462306a36Sopenharmony_ci	return p;
126562306a36Sopenharmony_ci
126662306a36Sopenharmony_cierr_groups:
126762306a36Sopenharmony_ci	sysfs_remove_groups(&p->kobj, p->groups_list);
126862306a36Sopenharmony_cierr_del:
126962306a36Sopenharmony_ci	kobject_del(&p->kobj);
127062306a36Sopenharmony_cierr_put:
127162306a36Sopenharmony_ci	if (device->port_data && is_full_dev)
127262306a36Sopenharmony_ci		device->port_data[port_num].sysfs = NULL;
127362306a36Sopenharmony_ci	kobject_put(&p->kobj);
127462306a36Sopenharmony_ci	return ERR_PTR(ret);
127562306a36Sopenharmony_ci}
127662306a36Sopenharmony_ci
127762306a36Sopenharmony_cistatic void destroy_port(struct ib_core_device *coredev, struct ib_port *port)
127862306a36Sopenharmony_ci{
127962306a36Sopenharmony_ci	bool is_full_dev = &port->ibdev->coredev == coredev;
128062306a36Sopenharmony_ci
128162306a36Sopenharmony_ci	list_del(&port->kobj.entry);
128262306a36Sopenharmony_ci	if (is_full_dev)
128362306a36Sopenharmony_ci		sysfs_remove_groups(&port->kobj, port->ibdev->ops.port_groups);
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_ci	sysfs_remove_groups(&port->kobj, port->groups_list);
128662306a36Sopenharmony_ci	kobject_del(&port->kobj);
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci	if (port->ibdev->port_data &&
128962306a36Sopenharmony_ci	    port->ibdev->port_data[port->port_num].sysfs == port)
129062306a36Sopenharmony_ci		port->ibdev->port_data[port->port_num].sysfs = NULL;
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci	kobject_put(&port->kobj);
129362306a36Sopenharmony_ci}
129462306a36Sopenharmony_ci
129562306a36Sopenharmony_cistatic const char *node_type_string(int node_type)
129662306a36Sopenharmony_ci{
129762306a36Sopenharmony_ci	switch (node_type) {
129862306a36Sopenharmony_ci	case RDMA_NODE_IB_CA:
129962306a36Sopenharmony_ci		return "CA";
130062306a36Sopenharmony_ci	case RDMA_NODE_IB_SWITCH:
130162306a36Sopenharmony_ci		return "switch";
130262306a36Sopenharmony_ci	case RDMA_NODE_IB_ROUTER:
130362306a36Sopenharmony_ci		return "router";
130462306a36Sopenharmony_ci	case RDMA_NODE_RNIC:
130562306a36Sopenharmony_ci		return "RNIC";
130662306a36Sopenharmony_ci	case RDMA_NODE_USNIC:
130762306a36Sopenharmony_ci		return "usNIC";
130862306a36Sopenharmony_ci	case RDMA_NODE_USNIC_UDP:
130962306a36Sopenharmony_ci		return "usNIC UDP";
131062306a36Sopenharmony_ci	case RDMA_NODE_UNSPECIFIED:
131162306a36Sopenharmony_ci		return "unspecified";
131262306a36Sopenharmony_ci	}
131362306a36Sopenharmony_ci	return "<unknown>";
131462306a36Sopenharmony_ci}
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_cistatic ssize_t node_type_show(struct device *device,
131762306a36Sopenharmony_ci			      struct device_attribute *attr, char *buf)
131862306a36Sopenharmony_ci{
131962306a36Sopenharmony_ci	struct ib_device *dev = rdma_device_to_ibdev(device);
132062306a36Sopenharmony_ci
132162306a36Sopenharmony_ci	return sysfs_emit(buf, "%u: %s\n", dev->node_type,
132262306a36Sopenharmony_ci			  node_type_string(dev->node_type));
132362306a36Sopenharmony_ci}
132462306a36Sopenharmony_cistatic DEVICE_ATTR_RO(node_type);
132562306a36Sopenharmony_ci
132662306a36Sopenharmony_cistatic ssize_t sys_image_guid_show(struct device *device,
132762306a36Sopenharmony_ci				   struct device_attribute *dev_attr, char *buf)
132862306a36Sopenharmony_ci{
132962306a36Sopenharmony_ci	struct ib_device *dev = rdma_device_to_ibdev(device);
133062306a36Sopenharmony_ci	__be16 *guid = (__be16 *)&dev->attrs.sys_image_guid;
133162306a36Sopenharmony_ci
133262306a36Sopenharmony_ci	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
133362306a36Sopenharmony_ci			  be16_to_cpu(guid[0]),
133462306a36Sopenharmony_ci			  be16_to_cpu(guid[1]),
133562306a36Sopenharmony_ci			  be16_to_cpu(guid[2]),
133662306a36Sopenharmony_ci			  be16_to_cpu(guid[3]));
133762306a36Sopenharmony_ci}
133862306a36Sopenharmony_cistatic DEVICE_ATTR_RO(sys_image_guid);
133962306a36Sopenharmony_ci
134062306a36Sopenharmony_cistatic ssize_t node_guid_show(struct device *device,
134162306a36Sopenharmony_ci			      struct device_attribute *attr, char *buf)
134262306a36Sopenharmony_ci{
134362306a36Sopenharmony_ci	struct ib_device *dev = rdma_device_to_ibdev(device);
134462306a36Sopenharmony_ci	__be16 *node_guid = (__be16 *)&dev->node_guid;
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci	return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n",
134762306a36Sopenharmony_ci			  be16_to_cpu(node_guid[0]),
134862306a36Sopenharmony_ci			  be16_to_cpu(node_guid[1]),
134962306a36Sopenharmony_ci			  be16_to_cpu(node_guid[2]),
135062306a36Sopenharmony_ci			  be16_to_cpu(node_guid[3]));
135162306a36Sopenharmony_ci}
135262306a36Sopenharmony_cistatic DEVICE_ATTR_RO(node_guid);
135362306a36Sopenharmony_ci
135462306a36Sopenharmony_cistatic ssize_t node_desc_show(struct device *device,
135562306a36Sopenharmony_ci			      struct device_attribute *attr, char *buf)
135662306a36Sopenharmony_ci{
135762306a36Sopenharmony_ci	struct ib_device *dev = rdma_device_to_ibdev(device);
135862306a36Sopenharmony_ci
135962306a36Sopenharmony_ci	return sysfs_emit(buf, "%.64s\n", dev->node_desc);
136062306a36Sopenharmony_ci}
136162306a36Sopenharmony_ci
136262306a36Sopenharmony_cistatic ssize_t node_desc_store(struct device *device,
136362306a36Sopenharmony_ci			       struct device_attribute *attr,
136462306a36Sopenharmony_ci			       const char *buf, size_t count)
136562306a36Sopenharmony_ci{
136662306a36Sopenharmony_ci	struct ib_device *dev = rdma_device_to_ibdev(device);
136762306a36Sopenharmony_ci	struct ib_device_modify desc = {};
136862306a36Sopenharmony_ci	int ret;
136962306a36Sopenharmony_ci
137062306a36Sopenharmony_ci	if (!dev->ops.modify_device)
137162306a36Sopenharmony_ci		return -EOPNOTSUPP;
137262306a36Sopenharmony_ci
137362306a36Sopenharmony_ci	memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
137462306a36Sopenharmony_ci	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
137562306a36Sopenharmony_ci	if (ret)
137662306a36Sopenharmony_ci		return ret;
137762306a36Sopenharmony_ci
137862306a36Sopenharmony_ci	return count;
137962306a36Sopenharmony_ci}
138062306a36Sopenharmony_cistatic DEVICE_ATTR_RW(node_desc);
138162306a36Sopenharmony_ci
138262306a36Sopenharmony_cistatic ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
138362306a36Sopenharmony_ci			   char *buf)
138462306a36Sopenharmony_ci{
138562306a36Sopenharmony_ci	struct ib_device *dev = rdma_device_to_ibdev(device);
138662306a36Sopenharmony_ci	char version[IB_FW_VERSION_NAME_MAX] = {};
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_ci	ib_get_device_fw_str(dev, version);
138962306a36Sopenharmony_ci
139062306a36Sopenharmony_ci	return sysfs_emit(buf, "%s\n", version);
139162306a36Sopenharmony_ci}
139262306a36Sopenharmony_cistatic DEVICE_ATTR_RO(fw_ver);
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_cistatic struct attribute *ib_dev_attrs[] = {
139562306a36Sopenharmony_ci	&dev_attr_node_type.attr,
139662306a36Sopenharmony_ci	&dev_attr_node_guid.attr,
139762306a36Sopenharmony_ci	&dev_attr_sys_image_guid.attr,
139862306a36Sopenharmony_ci	&dev_attr_fw_ver.attr,
139962306a36Sopenharmony_ci	&dev_attr_node_desc.attr,
140062306a36Sopenharmony_ci	NULL,
140162306a36Sopenharmony_ci};
140262306a36Sopenharmony_ci
140362306a36Sopenharmony_ciconst struct attribute_group ib_dev_attr_group = {
140462306a36Sopenharmony_ci	.attrs = ib_dev_attrs,
140562306a36Sopenharmony_ci};
140662306a36Sopenharmony_ci
140762306a36Sopenharmony_civoid ib_free_port_attrs(struct ib_core_device *coredev)
140862306a36Sopenharmony_ci{
140962306a36Sopenharmony_ci	struct kobject *p, *t;
141062306a36Sopenharmony_ci
141162306a36Sopenharmony_ci	list_for_each_entry_safe(p, t, &coredev->port_list, entry) {
141262306a36Sopenharmony_ci		struct ib_port *port = container_of(p, struct ib_port, kobj);
141362306a36Sopenharmony_ci
141462306a36Sopenharmony_ci		destroy_gid_attrs(port);
141562306a36Sopenharmony_ci		destroy_port(coredev, port);
141662306a36Sopenharmony_ci	}
141762306a36Sopenharmony_ci
141862306a36Sopenharmony_ci	kobject_put(coredev->ports_kobj);
141962306a36Sopenharmony_ci}
142062306a36Sopenharmony_ci
142162306a36Sopenharmony_ciint ib_setup_port_attrs(struct ib_core_device *coredev)
142262306a36Sopenharmony_ci{
142362306a36Sopenharmony_ci	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
142462306a36Sopenharmony_ci	u32 port_num;
142562306a36Sopenharmony_ci	int ret;
142662306a36Sopenharmony_ci
142762306a36Sopenharmony_ci	coredev->ports_kobj = kobject_create_and_add("ports",
142862306a36Sopenharmony_ci						     &coredev->dev.kobj);
142962306a36Sopenharmony_ci	if (!coredev->ports_kobj)
143062306a36Sopenharmony_ci		return -ENOMEM;
143162306a36Sopenharmony_ci
143262306a36Sopenharmony_ci	rdma_for_each_port (device, port_num) {
143362306a36Sopenharmony_ci		struct ib_port_attr attr;
143462306a36Sopenharmony_ci		struct ib_port *port;
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci		ret = ib_query_port(device, port_num, &attr);
143762306a36Sopenharmony_ci		if (ret)
143862306a36Sopenharmony_ci			goto err_put;
143962306a36Sopenharmony_ci
144062306a36Sopenharmony_ci		port = setup_port(coredev, port_num, &attr);
144162306a36Sopenharmony_ci		if (IS_ERR(port)) {
144262306a36Sopenharmony_ci			ret = PTR_ERR(port);
144362306a36Sopenharmony_ci			goto err_put;
144462306a36Sopenharmony_ci		}
144562306a36Sopenharmony_ci
144662306a36Sopenharmony_ci		ret = setup_gid_attrs(port, &attr);
144762306a36Sopenharmony_ci		if (ret)
144862306a36Sopenharmony_ci			goto err_put;
144962306a36Sopenharmony_ci	}
145062306a36Sopenharmony_ci	return 0;
145162306a36Sopenharmony_ci
145262306a36Sopenharmony_cierr_put:
145362306a36Sopenharmony_ci	ib_free_port_attrs(coredev);
145462306a36Sopenharmony_ci	return ret;
145562306a36Sopenharmony_ci}
145662306a36Sopenharmony_ci
145762306a36Sopenharmony_ci/**
145862306a36Sopenharmony_ci * ib_port_register_client_groups - Add an ib_client's attributes to the port
145962306a36Sopenharmony_ci *
146062306a36Sopenharmony_ci * @ibdev: IB device to add counters
146162306a36Sopenharmony_ci * @port_num: valid port number
146262306a36Sopenharmony_ci * @groups: Group list of attributes
146362306a36Sopenharmony_ci *
146462306a36Sopenharmony_ci * Do not use. Only for legacy sysfs compatibility.
146562306a36Sopenharmony_ci */
146662306a36Sopenharmony_ciint ib_port_register_client_groups(struct ib_device *ibdev, u32 port_num,
146762306a36Sopenharmony_ci				   const struct attribute_group **groups)
146862306a36Sopenharmony_ci{
146962306a36Sopenharmony_ci	return sysfs_create_groups(&ibdev->port_data[port_num].sysfs->kobj,
147062306a36Sopenharmony_ci				   groups);
147162306a36Sopenharmony_ci}
147262306a36Sopenharmony_ciEXPORT_SYMBOL(ib_port_register_client_groups);
147362306a36Sopenharmony_ci
147462306a36Sopenharmony_civoid ib_port_unregister_client_groups(struct ib_device *ibdev, u32 port_num,
147562306a36Sopenharmony_ci				      const struct attribute_group **groups)
147662306a36Sopenharmony_ci{
147762306a36Sopenharmony_ci	return sysfs_remove_groups(&ibdev->port_data[port_num].sysfs->kobj,
147862306a36Sopenharmony_ci				   groups);
147962306a36Sopenharmony_ci}
148062306a36Sopenharmony_ciEXPORT_SYMBOL(ib_port_unregister_client_groups);
1481