diff mbox series

[rdma-next,2/8] RDMA/core: Introduce and use ib_setup_port_attrs()

Message ID 20190213172310.1681-3-leon@kernel.org (mailing list archive)
State Superseded
Delegated to: Jason Gunthorpe
Headers show
Series Register infiniband class as net namespace aware class | expand

Commit Message

Leon Romanovsky Feb. 13, 2019, 5:23 p.m. UTC
From: Parav Pandit <parav@mellanox.com>

Refactor code for device and port sysfs attributes for reuse.

while at it, rename counter part free function to
ib_free_port_attrs.

Also attribute setup sequence is:
(a) port specific init.
(b) device stats alloc/init.

So for cleanup, follow reverse sequence:
(a) device stats dealloc
(b) port specific cleanup

Signed-off-by: Parav Pandit <parav@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
---
 drivers/infiniband/core/sysfs.c | 64 ++++++++++++++++++---------------
 1 file changed, 35 insertions(+), 29 deletions(-)

Comments

Jason Gunthorpe Feb. 16, 2019, 4:58 a.m. UTC | #1
On Wed, Feb 13, 2019 at 07:23:04PM +0200, Leon Romanovsky wrote:
> From: Parav Pandit <parav@mellanox.com>
> 
> Refactor code for device and port sysfs attributes for reuse.
> 
> while at it, rename counter part free function to
> ib_free_port_attrs.
> 
> Also attribute setup sequence is:
> (a) port specific init.
> (b) device stats alloc/init.
> 
> So for cleanup, follow reverse sequence:
> (a) device stats dealloc
> (b) port specific cleanup
> 
> Signed-off-by: Parav Pandit <parav@mellanox.com>
> Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
> ---
>  drivers/infiniband/core/sysfs.c | 64 ++++++++++++++++++---------------
>  1 file changed, 35 insertions(+), 29 deletions(-)

Applied to for-next

Thanks,
Jason
diff mbox series

Patch

diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
index 824bd2c283f2..f32de6f77349 100644
--- a/drivers/infiniband/core/sysfs.c
+++ b/drivers/infiniband/core/sysfs.c
@@ -1279,17 +1279,17 @@  static const struct attribute_group dev_attr_group = {
 	.attrs = ib_dev_attrs,
 };
 
-static void free_port_list_attributes(struct ib_device *device)
+static void ib_free_port_attrs(struct ib_device *device)
 {
 	struct kobject *p, *t;
 
 	list_for_each_entry_safe(p, t, &device->port_list, entry) {
 		struct ib_port *port = container_of(p, struct ib_port, kobj);
+
 		list_del(&p->entry);
-		if (port->hw_stats) {
-			kfree(port->hw_stats);
+		if (port->hw_stats_ag)
 			free_hsag(&port->kobj, port->hw_stats_ag);
-		}
+		kfree(port->hw_stats);
 
 		if (port->pma_table)
 			sysfs_remove_group(p, port->pma_table);
@@ -1306,24 +1306,14 @@  static void free_port_list_attributes(struct ib_device *device)
 	kobject_put(device->ports_kobj);
 }
 
-int ib_device_register_sysfs(struct ib_device *device)
+static int ib_setup_port_attrs(struct ib_device *device)
 {
-	struct device *class_dev = &device->dev;
 	int ret;
 	int i;
 
-	device->groups[0] = &dev_attr_group;
-	class_dev->groups = device->groups;
-
-	ret = device_add(class_dev);
-	if (ret)
-		goto err;
-
-	device->ports_kobj = kobject_create_and_add("ports", &class_dev->kobj);
-	if (!device->ports_kobj) {
-		ret = -ENOMEM;
-		goto err_put;
-	}
+	device->ports_kobj = kobject_create_and_add("ports", &device->dev.kobj);
+	if (!device->ports_kobj)
+		return -ENOMEM;
 
 	if (rdma_cap_ib_switch(device)) {
 		ret = add_port(device, 0);
@@ -1337,26 +1327,42 @@  int ib_device_register_sysfs(struct ib_device *device)
 		}
 	}
 
-	if (device->ops.alloc_hw_stats)
-		setup_hw_stats(device, NULL, 0);
-
 	return 0;
 
 err_put:
-	free_port_list_attributes(device);
-	device_del(class_dev);
-err:
+	ib_free_port_attrs(device);
 	return ret;
 }
 
-void ib_device_unregister_sysfs(struct ib_device *device)
+int ib_device_register_sysfs(struct ib_device *device)
 {
-	free_port_list_attributes(device);
+	int ret;
 
-	if (device->hw_stats) {
-		kfree(device->hw_stats);
-		free_hsag(&device->dev.kobj, device->hw_stats_ag);
+	device->groups[0] = &dev_attr_group;
+	device->dev.groups = device->groups;
+
+	ret = device_add(&device->dev);
+	if (ret)
+		return ret;
+
+	ret = ib_setup_port_attrs(device);
+	if (ret) {
+		device_del(&device->dev);
+		return ret;
 	}
+	if (device->ops.alloc_hw_stats)
+		setup_hw_stats(device, NULL, 0);
+
+	return 0;
+}
+
+void ib_device_unregister_sysfs(struct ib_device *device)
+{
+	if (device->hw_stats_ag)
+		free_hsag(&device->dev.kobj, device->hw_stats_ag);
+	kfree(device->hw_stats);
+
+	ib_free_port_attrs(device);
 	/* Balance with device_add */
 	device_del(&device->dev);
 }