sysfs.c (2ca1cca4352ed50a363159aa9b8eeb6182446580) sysfs.c (b7066b32a14fd21480efd93cb0c24807d6b28484)
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file

--- 93 unchanged lines hidden (view full) ---

102 unsigned int index, unsigned int port_num, char *buf);
103 ssize_t (*store)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
104 unsigned int index, unsigned int port_num,
105 const char *buf, size_t count);
106};
107
108struct hw_stats_device_data {
109 struct attribute_group group;
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file

--- 93 unchanged lines hidden (view full) ---

102 unsigned int index, unsigned int port_num, char *buf);
103 ssize_t (*store)(struct ib_device *ibdev, struct rdma_hw_stats *stats,
104 unsigned int index, unsigned int port_num,
105 const char *buf, size_t count);
106};
107
108struct hw_stats_device_data {
109 struct attribute_group group;
110 const struct attribute_group *groups[2];
111 struct rdma_hw_stats *stats;
112 struct hw_stats_device_attribute attrs[];
113};
114
115struct hw_stats_port_data {
116 struct rdma_hw_stats *stats;
117 struct hw_stats_port_attribute attrs[];
118};

--- 791 unchanged lines hidden (view full) ---

910 data->group.attrs = kcalloc(stats->num_counters + 2,
911 sizeof(*data->group.attrs), GFP_KERNEL);
912 if (!data->group.attrs)
913 goto err_free_data;
914
915 mutex_init(&stats->lock);
916 data->group.name = "hw_counters";
917 data->stats = stats;
110 struct rdma_hw_stats *stats;
111 struct hw_stats_device_attribute attrs[];
112};
113
114struct hw_stats_port_data {
115 struct rdma_hw_stats *stats;
116 struct hw_stats_port_attribute attrs[];
117};

--- 791 unchanged lines hidden (view full) ---

909 data->group.attrs = kcalloc(stats->num_counters + 2,
910 sizeof(*data->group.attrs), GFP_KERNEL);
911 if (!data->group.attrs)
912 goto err_free_data;
913
914 mutex_init(&stats->lock);
915 data->group.name = "hw_counters";
916 data->stats = stats;
918 data->groups[0] = &data->group;
919 return data;
920
921err_free_data:
922 kfree(data);
923err_free_stats:
924 kfree(stats);
925 return ERR_PTR(-ENOMEM);
926}
927
917 return data;
918
919err_free_data:
920 kfree(data);
921err_free_stats:
922 kfree(stats);
923 return ERR_PTR(-ENOMEM);
924}
925
928static void free_hw_stats_device(struct hw_stats_device_data *data)
926void ib_device_release_hw_stats(struct hw_stats_device_data *data)
929{
930 kfree(data->group.attrs);
931 kfree(data->stats);
932 kfree(data);
933}
934
927{
928 kfree(data->group.attrs);
929 kfree(data->stats);
930 kfree(data);
931}
932
935static int setup_hw_device_stats(struct ib_device *ibdev)
933int ib_setup_device_attrs(struct ib_device *ibdev)
936{
937 struct hw_stats_device_attribute *attr;
938 struct hw_stats_device_data *data;
939 int i, ret;
940
941 data = alloc_hw_stats_device(ibdev);
934{
935 struct hw_stats_device_attribute *attr;
936 struct hw_stats_device_data *data;
937 int i, ret;
938
939 data = alloc_hw_stats_device(ibdev);
942 if (IS_ERR(data))
940 if (IS_ERR(data)) {
941 if (PTR_ERR(data) == -EOPNOTSUPP)
942 return 0;
943 return PTR_ERR(data);
943 return PTR_ERR(data);
944 }
945 ibdev->hw_stats_data = data;
944
945 ret = ibdev->ops.get_hw_stats(ibdev, data->stats, 0,
946 data->stats->num_counters);
947 if (ret != data->stats->num_counters) {
948 if (WARN_ON(ret >= 0))
946
947 ret = ibdev->ops.get_hw_stats(ibdev, data->stats, 0,
948 data->stats->num_counters);
949 if (ret != data->stats->num_counters) {
950 if (WARN_ON(ret >= 0))
949 ret = -EINVAL;
950 goto err_free;
951 return -EINVAL;
952 return ret;
951 }
952
953 data->stats->timestamp = jiffies;
954
955 for (i = 0; i < data->stats->num_counters; i++) {
956 attr = &data->attrs[i];
957 sysfs_attr_init(&attr->attr.attr);
958 attr->attr.attr.name = data->stats->names[i];

--- 7 unchanged lines hidden (view full) ---

966 sysfs_attr_init(&attr->attr.attr);
967 attr->attr.attr.name = "lifespan";
968 attr->attr.attr.mode = 0644;
969 attr->attr.show = hw_stat_device_show;
970 attr->show = show_stats_lifespan;
971 attr->attr.store = hw_stat_device_store;
972 attr->store = set_stats_lifespan;
973 data->group.attrs[i] = &attr->attr.attr;
953 }
954
955 data->stats->timestamp = jiffies;
956
957 for (i = 0; i < data->stats->num_counters; i++) {
958 attr = &data->attrs[i];
959 sysfs_attr_init(&attr->attr.attr);
960 attr->attr.attr.name = data->stats->names[i];

--- 7 unchanged lines hidden (view full) ---

968 sysfs_attr_init(&attr->attr.attr);
969 attr->attr.attr.name = "lifespan";
970 attr->attr.attr.mode = 0644;
971 attr->attr.show = hw_stat_device_show;
972 attr->show = show_stats_lifespan;
973 attr->attr.store = hw_stat_device_store;
974 attr->store = set_stats_lifespan;
975 data->group.attrs[i] = &attr->attr.attr;
974
975 ibdev->hw_stats_data = data;
976 ret = device_add_groups(&ibdev->dev, data->groups);
977 if (ret)
978 goto err_free;
979 return 0;
980
981err_free:
982 free_hw_stats_device(data);
983 ibdev->hw_stats_data = NULL;
984 return ret;
976 for (i = 0; i != ARRAY_SIZE(ibdev->groups); i++)
977 if (!ibdev->groups[i]) {
978 ibdev->groups[i] = &data->group;
979 return 0;
980 }
981 WARN(true, "struct ib_device->groups is too small");
982 return -EINVAL;
985}
986
983}
984
987static void destroy_hw_device_stats(struct ib_device *ibdev)
988{
989 if (!ibdev->hw_stats_data)
990 return;
991 device_remove_groups(&ibdev->dev, ibdev->hw_stats_data->groups);
992 free_hw_stats_device(ibdev->hw_stats_data);
993 ibdev->hw_stats_data = NULL;
994}
995
996static struct hw_stats_port_data *
997alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
998{
999 struct ib_device *ibdev = port->ibdev;
1000 struct hw_stats_port_data *data;
1001 struct rdma_hw_stats *stats;
1002
1003 if (!ibdev->ops.alloc_hw_port_stats)

--- 434 unchanged lines hidden (view full) ---

1438 }
1439 return 0;
1440
1441err_put:
1442 ib_free_port_attrs(coredev);
1443 return ret;
1444}
1445
985static struct hw_stats_port_data *
986alloc_hw_stats_port(struct ib_port *port, struct attribute_group *group)
987{
988 struct ib_device *ibdev = port->ibdev;
989 struct hw_stats_port_data *data;
990 struct rdma_hw_stats *stats;
991
992 if (!ibdev->ops.alloc_hw_port_stats)

--- 434 unchanged lines hidden (view full) ---

1427 }
1428 return 0;
1429
1430err_put:
1431 ib_free_port_attrs(coredev);
1432 return ret;
1433}
1434
1446int ib_device_register_sysfs(struct ib_device *device)
1447{
1448 int ret;
1449
1450 ret = ib_setup_port_attrs(&device->coredev);
1451 if (ret)
1452 return ret;
1453
1454 ret = setup_hw_device_stats(device);
1455 if (ret && ret != -EOPNOTSUPP) {
1456 ib_free_port_attrs(&device->coredev);
1457 return ret;
1458 }
1459
1460 return 0;
1461}
1462
1463void ib_device_unregister_sysfs(struct ib_device *device)
1464{
1465 destroy_hw_device_stats(device);
1466 ib_free_port_attrs(&device->coredev);
1467}
1468
1469/**
1470 * ib_port_register_module_stat - add module counters under relevant port
1471 * of IB device.
1472 *
1473 * @device: IB device to add counters
1474 * @port_num: valid port number
1475 * @kobj: pointer to the kobject to initialize
1476 * @ktype: pointer to the ktype for this kobject.

--- 36 unchanged lines hidden ---
1435/**
1436 * ib_port_register_module_stat - add module counters under relevant port
1437 * of IB device.
1438 *
1439 * @device: IB device to add counters
1440 * @port_num: valid port number
1441 * @kobj: pointer to the kobject to initialize
1442 * @ktype: pointer to the ktype for this kobject.

--- 36 unchanged lines hidden ---