Skip to content

Commit

Permalink
RDMA/core: Depend on device_add() to add device attributes
Browse files Browse the repository at this point in the history
Instead of adding/removing device attribute files, depend on device_add()
which considers adding these device files based on NULL terminated
attributes group array.

Signed-off-by: Parav Pandit <parav@mellanox.com>
Reviewed-by: Daniel Jurgens <danielj@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
  • Loading branch information
Parav Pandit authored and Jason Gunthorpe committed Sep 5, 2018
1 parent 08e74be commit adee9f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
61 changes: 27 additions & 34 deletions drivers/infiniband/core/sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ static int add_port(struct ib_device *device, int port_num,
return ret;
}

static ssize_t show_node_type(struct device *device,
static ssize_t node_type_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
Expand All @@ -1198,8 +1198,9 @@ static ssize_t show_node_type(struct device *device,
default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
}
}
static DEVICE_ATTR_RO(node_type);

static ssize_t show_sys_image_guid(struct device *device,
static ssize_t sys_image_guid_show(struct device *device,
struct device_attribute *dev_attr, char *buf)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
Expand All @@ -1210,8 +1211,9 @@ static ssize_t show_sys_image_guid(struct device *device,
be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
}
static DEVICE_ATTR_RO(sys_image_guid);

static ssize_t show_node_guid(struct device *device,
static ssize_t node_guid_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
Expand All @@ -1222,18 +1224,19 @@ static ssize_t show_node_guid(struct device *device,
be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
}
static DEVICE_ATTR_RO(node_guid);

static ssize_t show_node_desc(struct device *device,
static ssize_t node_desc_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);

return sprintf(buf, "%.64s\n", dev->node_desc);
}

static ssize_t set_node_desc(struct device *device,
struct device_attribute *attr,
const char *buf, size_t count)
static ssize_t node_desc_store(struct device *device,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
struct ib_device_modify desc = {};
Expand All @@ -1249,8 +1252,9 @@ static ssize_t set_node_desc(struct device *device,

return count;
}
static DEVICE_ATTR_RW(node_desc);

static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
char *buf)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
Expand All @@ -1259,19 +1263,19 @@ static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
return strlen(buf);
}
static DEVICE_ATTR_RO(fw_ver);

static struct attribute *ib_dev_attrs[] = {
&dev_attr_node_type.attr,
&dev_attr_node_guid.attr,
&dev_attr_sys_image_guid.attr,
&dev_attr_fw_ver.attr,
&dev_attr_node_desc.attr,
NULL,
};

static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);

static struct device_attribute *ib_class_attributes[] = {
&dev_attr_node_type,
&dev_attr_sys_image_guid,
&dev_attr_node_guid,
&dev_attr_node_desc,
&dev_attr_fw_ver,
static const struct attribute_group dev_attr_group = {
.attrs = ib_dev_attrs,
};

static void free_port_list_attributes(struct ib_device *device)
Expand Down Expand Up @@ -1311,16 +1315,13 @@ int ib_device_register_sysfs(struct ib_device *device,
if (ret)
return ret;

device->groups[0] = &dev_attr_group;
class_dev->groups = device->groups;

ret = device_add(class_dev);
if (ret)
goto err;

for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
ret = device_create_file(class_dev, ib_class_attributes[i]);
if (ret)
goto err_unregister;
}

device->ports_parent = kobject_create_and_add("ports",
&class_dev->kobj);
if (!device->ports_parent) {
Expand All @@ -1347,18 +1348,13 @@ int ib_device_register_sysfs(struct ib_device *device,

err_put:
free_port_list_attributes(device);

err_unregister:
device_del(class_dev);

err:
return ret;
}

void ib_device_unregister_sysfs(struct ib_device *device)
{
int i;

/* Hold kobject until ib_dealloc_device() */
kobject_get(&device->dev.kobj);

Expand All @@ -1369,8 +1365,5 @@ void ib_device_unregister_sysfs(struct ib_device *device)
free_hsag(&device->dev.kobj, device->hw_stats_ag);
}

for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
device_remove_file(&device->dev, ib_class_attributes[i]);

device_unregister(&device->dev);
}
3 changes: 3 additions & 0 deletions include/rdma/ib_verbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,9 @@ struct ib_device {

struct module *owner;
struct device dev;
/* First group for device attributes, NULL terminated array */
const struct attribute_group *groups[2];

struct kobject *ports_parent;
struct list_head port_list;

Expand Down

0 comments on commit adee9f3

Please sign in to comment.