From 45808361d4491217de11cdf0661d657081f8f422 Mon Sep 17 00:00:00 2001 From: Joe Perches Date: Wed, 7 Oct 2020 19:36:26 -0700 Subject: RDMA: Manual changes for sysfs_emit and neatening Make changes to use sysfs_emit in the RDMA code as cocci scripts can not be written to handle _all_ the possible variants of various sprintf family uses in sysfs show functions. While there, make the code more legible and update its style to be more like the typical kernel styles. Miscellanea: o Use intermediate pointers for dereferences o Add and use string lookup functions o return early when any intermediate call fails so normal return is at the bottom of the function o mlx4/mcg.c:sysfs_show_group: use scnprintf to format intermediate strings Link: https://lore.kernel.org/r/f5c9e4c9d8dafca1b7b70bd597ee7f8f219c31c8.1602122880.git.joe@perches.com Signed-off-by: Joe Perches Acked-by: Jack Wang Signed-off-by: Jason Gunthorpe --- drivers/infiniband/core/sysfs.c | 65 +++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 29 deletions(-) (limited to 'drivers/infiniband/core') diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c index 4e335fc4d016..4dd803f23aaa 100644 --- a/drivers/infiniband/core/sysfs.c +++ b/drivers/infiniband/core/sysfs.c @@ -1224,29 +1224,34 @@ err_put: return ret; } -static ssize_t node_type_show(struct device *device, - struct device_attribute *attr, char *buf) +static const char *node_type_string(int node_type) { - struct ib_device *dev = rdma_device_to_ibdev(device); - - switch (dev->node_type) { + switch (node_type) { case RDMA_NODE_IB_CA: - return sysfs_emit(buf, "%d: CA\n", dev->node_type); + return "CA"; + case RDMA_NODE_IB_SWITCH: + return "switch"; + case RDMA_NODE_IB_ROUTER: + return "router"; case RDMA_NODE_RNIC: - return sysfs_emit(buf, "%d: RNIC\n", dev->node_type); + return "RNIC"; case RDMA_NODE_USNIC: - return sysfs_emit(buf, "%d: usNIC\n", dev->node_type); + return "usNIC"; case RDMA_NODE_USNIC_UDP: - return sysfs_emit(buf, "%d: usNIC UDP\n", dev->node_type); + return "usNIC UDP"; case RDMA_NODE_UNSPECIFIED: - return sysfs_emit(buf, "%d: unspecified\n", dev->node_type); - case RDMA_NODE_IB_SWITCH: - return sysfs_emit(buf, "%d: switch\n", dev->node_type); - case RDMA_NODE_IB_ROUTER: - return sysfs_emit(buf, "%d: router\n", dev->node_type); - default: - return sysfs_emit(buf, "%d: \n", dev->node_type); + return "unspecified"; } + return ""; +} + +static ssize_t node_type_show(struct device *device, + struct device_attribute *attr, char *buf) +{ + struct ib_device *dev = rdma_device_to_ibdev(device); + + return sysfs_emit(buf, "%d: %s\n", dev->node_type, + node_type_string(dev->node_type)); } static DEVICE_ATTR_RO(node_type); @@ -1254,13 +1259,13 @@ static ssize_t sys_image_guid_show(struct device *device, struct device_attribute *dev_attr, char *buf) { struct ib_device *dev = rdma_device_to_ibdev(device); + __be16 *guid = (__be16 *)&dev->attrs.sys_image_guid; - return sysfs_emit( - buf, "%04x:%04x:%04x:%04x\n", - be16_to_cpu(((__be16 *)&dev->attrs.sys_image_guid)[0]), - be16_to_cpu(((__be16 *)&dev->attrs.sys_image_guid)[1]), - be16_to_cpu(((__be16 *)&dev->attrs.sys_image_guid)[2]), - be16_to_cpu(((__be16 *)&dev->attrs.sys_image_guid)[3])); + return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n", + be16_to_cpu(guid[0]), + be16_to_cpu(guid[1]), + be16_to_cpu(guid[2]), + be16_to_cpu(guid[3])); } static DEVICE_ATTR_RO(sys_image_guid); @@ -1268,12 +1273,13 @@ static ssize_t node_guid_show(struct device *device, struct device_attribute *attr, char *buf) { struct ib_device *dev = rdma_device_to_ibdev(device); + __be16 *node_guid = (__be16 *)&dev->node_guid; return sysfs_emit(buf, "%04x:%04x:%04x:%04x\n", - be16_to_cpu(((__be16 *)&dev->node_guid)[0]), - be16_to_cpu(((__be16 *)&dev->node_guid)[1]), - be16_to_cpu(((__be16 *)&dev->node_guid)[2]), - be16_to_cpu(((__be16 *)&dev->node_guid)[3])); + be16_to_cpu(node_guid[0]), + be16_to_cpu(node_guid[1]), + be16_to_cpu(node_guid[2]), + be16_to_cpu(node_guid[3])); } static DEVICE_ATTR_RO(node_guid); @@ -1309,10 +1315,11 @@ static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr, char *buf) { struct ib_device *dev = rdma_device_to_ibdev(device); + char version[IB_FW_VERSION_NAME_MAX] = {}; + + ib_get_device_fw_str(dev, version); - ib_get_device_fw_str(dev, buf); - strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX); - return strlen(buf); + return sysfs_emit(buf, "%s\n", version); } static DEVICE_ATTR_RO(fw_ver); -- cgit