RDMA/core: Depend on device_add() to add device attributes

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>
This commit is contained in:
Parav Pandit 2018-09-05 09:47:58 +03:00 committed by Jason Gunthorpe
parent 08e74be103
commit adee9f3f3b
2 changed files with 29 additions and 33 deletions

View File

@ -1183,7 +1183,7 @@ err_put:
return ret; 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 device_attribute *attr, char *buf)
{ {
struct ib_device *dev = container_of(device, struct ib_device, dev); struct ib_device *dev = container_of(device, struct ib_device, dev);
@ -1198,8 +1198,9 @@ static ssize_t show_node_type(struct device *device,
default: return sprintf(buf, "%d: <unknown>\n", dev->node_type); 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 device_attribute *dev_attr, char *buf)
{ {
struct ib_device *dev = container_of(device, struct ib_device, dev); struct ib_device *dev = container_of(device, struct ib_device, dev);
@ -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)[2]),
be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3])); 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 device_attribute *attr, char *buf)
{ {
struct ib_device *dev = container_of(device, struct ib_device, dev); struct ib_device *dev = container_of(device, struct ib_device, dev);
@ -1222,8 +1224,9 @@ static ssize_t show_node_guid(struct device *device,
be16_to_cpu(((__be16 *) &dev->node_guid)[2]), be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
be16_to_cpu(((__be16 *) &dev->node_guid)[3])); 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 device_attribute *attr, char *buf)
{ {
struct ib_device *dev = container_of(device, struct ib_device, dev); struct ib_device *dev = container_of(device, struct ib_device, dev);
@ -1231,9 +1234,9 @@ static ssize_t show_node_desc(struct device *device,
return sprintf(buf, "%.64s\n", dev->node_desc); return sprintf(buf, "%.64s\n", dev->node_desc);
} }
static ssize_t set_node_desc(struct device *device, static ssize_t node_desc_store(struct device *device,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct ib_device *dev = container_of(device, struct ib_device, dev); struct ib_device *dev = container_of(device, struct ib_device, dev);
struct ib_device_modify desc = {}; struct ib_device_modify desc = {};
@ -1249,8 +1252,9 @@ static ssize_t set_node_desc(struct device *device,
return count; 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) char *buf)
{ {
struct ib_device *dev = container_of(device, struct ib_device, dev); struct ib_device *dev = container_of(device, struct ib_device, dev);
@ -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); strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
return strlen(buf); return strlen(buf);
} }
static DEVICE_ATTR_RO(fw_ver);
static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL); static struct attribute *ib_dev_attrs[] = {
static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL); &dev_attr_node_type.attr,
static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL); &dev_attr_node_guid.attr,
static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc); &dev_attr_sys_image_guid.attr,
static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL); &dev_attr_fw_ver.attr,
&dev_attr_node_desc.attr,
NULL,
};
static struct device_attribute *ib_class_attributes[] = { static const struct attribute_group dev_attr_group = {
&dev_attr_node_type, .attrs = ib_dev_attrs,
&dev_attr_sys_image_guid,
&dev_attr_node_guid,
&dev_attr_node_desc,
&dev_attr_fw_ver,
}; };
static void free_port_list_attributes(struct ib_device *device) static void free_port_list_attributes(struct ib_device *device)
@ -1311,16 +1315,13 @@ int ib_device_register_sysfs(struct ib_device *device,
if (ret) if (ret)
return ret; return ret;
device->groups[0] = &dev_attr_group;
class_dev->groups = device->groups;
ret = device_add(class_dev); ret = device_add(class_dev);
if (ret) if (ret)
goto err; 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", device->ports_parent = kobject_create_and_add("ports",
&class_dev->kobj); &class_dev->kobj);
if (!device->ports_parent) { if (!device->ports_parent) {
@ -1347,18 +1348,13 @@ int ib_device_register_sysfs(struct ib_device *device,
err_put: err_put:
free_port_list_attributes(device); free_port_list_attributes(device);
err_unregister:
device_del(class_dev); device_del(class_dev);
err: err:
return ret; return ret;
} }
void ib_device_unregister_sysfs(struct ib_device *device) void ib_device_unregister_sysfs(struct ib_device *device)
{ {
int i;
/* Hold kobject until ib_dealloc_device() */ /* Hold kobject until ib_dealloc_device() */
kobject_get(&device->dev.kobj); kobject_get(&device->dev.kobj);
@ -1369,8 +1365,5 @@ void ib_device_unregister_sysfs(struct ib_device *device)
free_hsag(&device->dev.kobj, device->hw_stats_ag); 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); device_unregister(&device->dev);
} }

View File

@ -2536,6 +2536,9 @@ struct ib_device {
struct module *owner; struct module *owner;
struct device dev; struct device dev;
/* First group for device attributes, NULL terminated array */
const struct attribute_group *groups[2];
struct kobject *ports_parent; struct kobject *ports_parent;
struct list_head port_list; struct list_head port_list;