Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file for managing the edac_device class of devices for EDAC |
| 3 | * |
| 4 | * (C) 2007 SoftwareBitMaker(http://www.softwarebitmaker.com) |
| 5 | * This file may be distributed under the terms of the |
| 6 | * GNU General Public License. |
| 7 | * |
| 8 | * Written Doug Thompson <norsk5@xmission.com> |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/sysdev.h> |
| 14 | #include <linux/ctype.h> |
| 15 | |
| 16 | #include "edac_core.h" |
| 17 | #include "edac_module.h" |
| 18 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 19 | #define EDAC_DEVICE_SYMLINK "device" |
| 20 | |
| 21 | #define to_edacdev(k) container_of(k, struct edac_device_ctl_info, kobj) |
| 22 | #define to_edacdev_attr(a) container_of(a, struct edacdev_attribute, attr) |
| 23 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 24 | /************************** edac_device sysfs code and data **************/ |
| 25 | |
| 26 | /* |
| 27 | * Set of edac_device_ctl_info attribute store/show functions |
| 28 | */ |
| 29 | |
| 30 | /* 'log_ue' */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 31 | static ssize_t edac_device_ctl_log_ue_show(struct edac_device_ctl_info |
| 32 | *ctl_info, char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 33 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 34 | return sprintf(data, "%u\n", ctl_info->log_ue); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 37 | static ssize_t edac_device_ctl_log_ue_store(struct edac_device_ctl_info |
| 38 | *ctl_info, const char *data, |
| 39 | size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 40 | { |
| 41 | /* if parameter is zero, turn off flag, if non-zero turn on flag */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 42 | ctl_info->log_ue = (simple_strtoul(data, NULL, 0) != 0); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 43 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 44 | return count; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* 'log_ce' */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 48 | static ssize_t edac_device_ctl_log_ce_show(struct edac_device_ctl_info |
| 49 | *ctl_info, char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 50 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 51 | return sprintf(data, "%u\n", ctl_info->log_ce); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 54 | static ssize_t edac_device_ctl_log_ce_store(struct edac_device_ctl_info |
| 55 | *ctl_info, const char *data, |
| 56 | size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 57 | { |
| 58 | /* if parameter is zero, turn off flag, if non-zero turn on flag */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 59 | ctl_info->log_ce = (simple_strtoul(data, NULL, 0) != 0); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 60 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 61 | return count; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 64 | /* 'panic_on_ue' */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 65 | static ssize_t edac_device_ctl_panic_on_ue_show(struct edac_device_ctl_info |
| 66 | *ctl_info, char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 67 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 68 | return sprintf(data, "%u\n", ctl_info->panic_on_ue); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 71 | static ssize_t edac_device_ctl_panic_on_ue_store(struct edac_device_ctl_info |
| 72 | *ctl_info, const char *data, |
| 73 | size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 74 | { |
| 75 | /* if parameter is zero, turn off flag, if non-zero turn on flag */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 76 | ctl_info->panic_on_ue = (simple_strtoul(data, NULL, 0) != 0); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 77 | |
| 78 | return count; |
| 79 | } |
| 80 | |
| 81 | /* 'poll_msec' show and store functions*/ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 82 | static ssize_t edac_device_ctl_poll_msec_show(struct edac_device_ctl_info |
| 83 | *ctl_info, char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 84 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 85 | return sprintf(data, "%u\n", ctl_info->poll_msec); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 88 | static ssize_t edac_device_ctl_poll_msec_store(struct edac_device_ctl_info |
| 89 | *ctl_info, const char *data, |
| 90 | size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 91 | { |
| 92 | unsigned long value; |
| 93 | |
| 94 | /* get the value and enforce that it is non-zero, must be at least |
| 95 | * one millisecond for the delay period, between scans |
| 96 | * Then cancel last outstanding delay for the work request |
| 97 | * and set a new one. |
| 98 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 99 | value = simple_strtoul(data, NULL, 0); |
| 100 | edac_device_reset_delay_period(ctl_info, value); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 101 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 102 | return count; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 105 | /* edac_device_ctl_info specific attribute structure */ |
| 106 | struct ctl_info_attribute { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 107 | struct attribute attr; |
| 108 | ssize_t(*show) (struct edac_device_ctl_info *, char *); |
| 109 | ssize_t(*store) (struct edac_device_ctl_info *, const char *, size_t); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | #define to_ctl_info(k) container_of(k, struct edac_device_ctl_info, kobj) |
| 113 | #define to_ctl_info_attr(a) container_of(a,struct ctl_info_attribute,attr) |
| 114 | |
| 115 | /* Function to 'show' fields from the edac_dev 'ctl_info' structure */ |
| 116 | static ssize_t edac_dev_ctl_info_show(struct kobject *kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 117 | struct attribute *attr, char *buffer) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 118 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 119 | struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj); |
| 120 | struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 121 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 122 | if (ctl_info_attr->show) |
| 123 | return ctl_info_attr->show(edac_dev, buffer); |
| 124 | return -EIO; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* Function to 'store' fields into the edac_dev 'ctl_info' structure */ |
| 128 | static ssize_t edac_dev_ctl_info_store(struct kobject *kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 129 | struct attribute *attr, |
| 130 | const char *buffer, size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 131 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 132 | struct edac_device_ctl_info *edac_dev = to_ctl_info(kobj); |
| 133 | struct ctl_info_attribute *ctl_info_attr = to_ctl_info_attr(attr); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 134 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 135 | if (ctl_info_attr->store) |
| 136 | return ctl_info_attr->store(edac_dev, buffer, count); |
| 137 | return -EIO; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* edac_dev file operations for an 'ctl_info' */ |
| 141 | static struct sysfs_ops device_ctl_info_ops = { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 142 | .show = edac_dev_ctl_info_show, |
| 143 | .store = edac_dev_ctl_info_store |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | #define CTL_INFO_ATTR(_name,_mode,_show,_store) \ |
| 147 | static struct ctl_info_attribute attr_ctl_info_##_name = { \ |
| 148 | .attr = {.name = __stringify(_name), .mode = _mode }, \ |
| 149 | .show = _show, \ |
| 150 | .store = _store, \ |
| 151 | }; |
| 152 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 153 | /* Declare the various ctl_info attributes here and their respective ops */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 154 | CTL_INFO_ATTR(log_ue, S_IRUGO | S_IWUSR, |
| 155 | edac_device_ctl_log_ue_show, edac_device_ctl_log_ue_store); |
| 156 | CTL_INFO_ATTR(log_ce, S_IRUGO | S_IWUSR, |
| 157 | edac_device_ctl_log_ce_show, edac_device_ctl_log_ce_store); |
| 158 | CTL_INFO_ATTR(panic_on_ue, S_IRUGO | S_IWUSR, |
| 159 | edac_device_ctl_panic_on_ue_show, |
| 160 | edac_device_ctl_panic_on_ue_store); |
| 161 | CTL_INFO_ATTR(poll_msec, S_IRUGO | S_IWUSR, |
| 162 | edac_device_ctl_poll_msec_show, edac_device_ctl_poll_msec_store); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 163 | |
| 164 | /* Base Attributes of the EDAC_DEVICE ECC object */ |
| 165 | static struct ctl_info_attribute *device_ctrl_attr[] = { |
| 166 | &attr_ctl_info_panic_on_ue, |
| 167 | &attr_ctl_info_log_ue, |
| 168 | &attr_ctl_info_log_ce, |
| 169 | &attr_ctl_info_poll_msec, |
| 170 | NULL, |
| 171 | }; |
| 172 | |
| 173 | /* Main DEVICE kobject release() function */ |
| 174 | static void edac_device_ctrl_master_release(struct kobject *kobj) |
| 175 | { |
| 176 | struct edac_device_ctl_info *edac_dev; |
| 177 | |
| 178 | edac_dev = to_edacdev(kobj); |
| 179 | |
| 180 | debugf1("%s()\n", __func__); |
| 181 | complete(&edac_dev->kobj_complete); |
| 182 | } |
| 183 | |
| 184 | static struct kobj_type ktype_device_ctrl = { |
| 185 | .release = edac_device_ctrl_master_release, |
| 186 | .sysfs_ops = &device_ctl_info_ops, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 187 | .default_attrs = (struct attribute **)device_ctrl_attr, |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 190 | /**************** edac_device main kobj ctor/dtor code *********************/ |
| 191 | |
| 192 | /* |
| 193 | * edac_device_register_main_kobj |
| 194 | * |
| 195 | * perform the high level setup for the new edac_device instance |
| 196 | * |
| 197 | * Return: 0 SUCCESS |
| 198 | * !0 FAILURE |
| 199 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 200 | static int edac_device_register_main_kobj(struct edac_device_ctl_info *edac_dev) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 201 | { |
| 202 | int err = 0; |
| 203 | struct sysdev_class *edac_class; |
| 204 | |
| 205 | debugf1("%s()\n", __func__); |
| 206 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 207 | /* get the /sys/devices/system/edac reference */ |
| 208 | edac_class = edac_get_edac_class(); |
| 209 | if (edac_class == NULL) { |
| 210 | debugf1("%s() no edac_class error=%d\n", __func__, err); |
| 211 | return err; |
| 212 | } |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 213 | |
| 214 | /* Point to the 'edac_class' this instance 'reports' to */ |
| 215 | edac_dev->edac_class = edac_class; |
| 216 | |
| 217 | /* Init the devices's kobject */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 218 | memset(&edac_dev->kobj, 0, sizeof(struct kobject)); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 219 | edac_dev->kobj.ktype = &ktype_device_ctrl; |
| 220 | |
| 221 | /* set this new device under the edac_class kobject */ |
| 222 | edac_dev->kobj.parent = &edac_class->kset.kobj; |
| 223 | |
| 224 | /* generate sysfs "..../edac/<name>" */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 225 | debugf1("%s() set name of kobject to: %s\n", __func__, edac_dev->name); |
| 226 | err = kobject_set_name(&edac_dev->kobj, "%s", edac_dev->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 227 | if (err) |
| 228 | return err; |
| 229 | err = kobject_register(&edac_dev->kobj); |
| 230 | if (err) { |
| 231 | debugf1("%s()Failed to register '.../edac/%s'\n", |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 232 | __func__, edac_dev->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 233 | return err; |
| 234 | } |
| 235 | |
| 236 | debugf1("%s() Registered '.../edac/%s' kobject\n", |
| 237 | __func__, edac_dev->name); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * edac_device_unregister_main_kobj: |
| 244 | * the '..../edac/<name>' kobject |
| 245 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 246 | static void edac_device_unregister_main_kobj(struct edac_device_ctl_info |
| 247 | *edac_dev) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 248 | { |
| 249 | debugf0("%s()\n", __func__); |
| 250 | debugf1("%s() name of kobject is: %s\n", |
| 251 | __func__, kobject_name(&edac_dev->kobj)); |
| 252 | |
| 253 | init_completion(&edac_dev->kobj_complete); |
| 254 | |
| 255 | /* |
| 256 | * Unregister the edac device's kobject and |
| 257 | * wait for reference count to reach 0. |
| 258 | */ |
| 259 | kobject_unregister(&edac_dev->kobj); |
| 260 | wait_for_completion(&edac_dev->kobj_complete); |
| 261 | } |
| 262 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 263 | /*************** edac_dev -> instance information ***********/ |
| 264 | |
| 265 | /* |
| 266 | * Set of low-level instance attribute show functions |
| 267 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 268 | static ssize_t instance_ue_count_show(struct edac_device_instance *instance, |
| 269 | char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 270 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 271 | return sprintf(data, "%u\n", instance->counters.ue_count); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 274 | static ssize_t instance_ce_count_show(struct edac_device_instance *instance, |
| 275 | char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 276 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 277 | return sprintf(data, "%u\n", instance->counters.ce_count); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 280 | #define to_instance(k) container_of(k, struct edac_device_instance, kobj) |
| 281 | #define to_instance_attr(a) container_of(a,struct instance_attribute,attr) |
| 282 | |
| 283 | /* DEVICE instance kobject release() function */ |
| 284 | static void edac_device_ctrl_instance_release(struct kobject *kobj) |
| 285 | { |
| 286 | struct edac_device_instance *instance; |
| 287 | |
| 288 | debugf1("%s()\n", __func__); |
| 289 | |
| 290 | instance = to_instance(kobj); |
| 291 | complete(&instance->kobj_complete); |
| 292 | } |
| 293 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 294 | /* instance specific attribute structure */ |
| 295 | struct instance_attribute { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 296 | struct attribute attr; |
| 297 | ssize_t(*show) (struct edac_device_instance *, char *); |
| 298 | ssize_t(*store) (struct edac_device_instance *, const char *, size_t); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 299 | }; |
| 300 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 301 | /* Function to 'show' fields from the edac_dev 'instance' structure */ |
| 302 | static ssize_t edac_dev_instance_show(struct kobject *kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 303 | struct attribute *attr, char *buffer) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 304 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 305 | struct edac_device_instance *instance = to_instance(kobj); |
| 306 | struct instance_attribute *instance_attr = to_instance_attr(attr); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 307 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 308 | if (instance_attr->show) |
| 309 | return instance_attr->show(instance, buffer); |
| 310 | return -EIO; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 313 | /* Function to 'store' fields into the edac_dev 'instance' structure */ |
| 314 | static ssize_t edac_dev_instance_store(struct kobject *kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 315 | struct attribute *attr, |
| 316 | const char *buffer, size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 317 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 318 | struct edac_device_instance *instance = to_instance(kobj); |
| 319 | struct instance_attribute *instance_attr = to_instance_attr(attr); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 320 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 321 | if (instance_attr->store) |
| 322 | return instance_attr->store(instance, buffer, count); |
| 323 | return -EIO; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 326 | /* edac_dev file operations for an 'instance' */ |
| 327 | static struct sysfs_ops device_instance_ops = { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 328 | .show = edac_dev_instance_show, |
| 329 | .store = edac_dev_instance_store |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | #define INSTANCE_ATTR(_name,_mode,_show,_store) \ |
| 333 | static struct instance_attribute attr_instance_##_name = { \ |
| 334 | .attr = {.name = __stringify(_name), .mode = _mode }, \ |
| 335 | .show = _show, \ |
| 336 | .store = _store, \ |
| 337 | }; |
| 338 | |
| 339 | /* |
| 340 | * Define attributes visible for the edac_device instance object |
| 341 | * Each contains a pointer to a show and an optional set |
| 342 | * function pointer that does the low level output/input |
| 343 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 344 | INSTANCE_ATTR(ce_count, S_IRUGO, instance_ce_count_show, NULL); |
| 345 | INSTANCE_ATTR(ue_count, S_IRUGO, instance_ue_count_show, NULL); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 346 | |
| 347 | /* list of edac_dev 'instance' attributes */ |
| 348 | static struct instance_attribute *device_instance_attr[] = { |
| 349 | &attr_instance_ce_count, |
| 350 | &attr_instance_ue_count, |
| 351 | NULL, |
| 352 | }; |
| 353 | |
| 354 | /* The 'ktype' for each edac_dev 'instance' */ |
| 355 | static struct kobj_type ktype_instance_ctrl = { |
| 356 | .release = edac_device_ctrl_instance_release, |
| 357 | .sysfs_ops = &device_instance_ops, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 358 | .default_attrs = (struct attribute **)device_instance_attr, |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 361 | /*************** edac_dev -> instance -> block information *********/ |
| 362 | |
| 363 | /* |
| 364 | * Set of low-level block attribute show functions |
| 365 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 366 | static ssize_t block_ue_count_show(struct edac_device_block *block, char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 367 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 368 | return sprintf(data, "%u\n", block->counters.ue_count); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 371 | static ssize_t block_ce_count_show(struct edac_device_block *block, char *data) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 372 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 373 | return sprintf(data, "%u\n", block->counters.ce_count); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 376 | #define to_block(k) container_of(k, struct edac_device_block, kobj) |
| 377 | #define to_block_attr(a) container_of(a,struct block_attribute,attr) |
| 378 | |
| 379 | /* DEVICE block kobject release() function */ |
| 380 | static void edac_device_ctrl_block_release(struct kobject *kobj) |
| 381 | { |
| 382 | struct edac_device_block *block; |
| 383 | |
| 384 | debugf1("%s()\n", __func__); |
| 385 | |
| 386 | block = to_block(kobj); |
| 387 | complete(&block->kobj_complete); |
| 388 | } |
| 389 | |
| 390 | /* block specific attribute structure */ |
| 391 | struct block_attribute { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 392 | struct attribute attr; |
| 393 | ssize_t(*show) (struct edac_device_block *, char *); |
| 394 | ssize_t(*store) (struct edac_device_block *, const char *, size_t); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | /* Function to 'show' fields from the edac_dev 'block' structure */ |
| 398 | static ssize_t edac_dev_block_show(struct kobject *kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 399 | struct attribute *attr, char *buffer) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 400 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 401 | struct edac_device_block *block = to_block(kobj); |
| 402 | struct block_attribute *block_attr = to_block_attr(attr); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 403 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 404 | if (block_attr->show) |
| 405 | return block_attr->show(block, buffer); |
| 406 | return -EIO; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 409 | /* Function to 'store' fields into the edac_dev 'block' structure */ |
| 410 | static ssize_t edac_dev_block_store(struct kobject *kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 411 | struct attribute *attr, |
| 412 | const char *buffer, size_t count) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 413 | { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 414 | struct edac_device_block *block = to_block(kobj); |
| 415 | struct block_attribute *block_attr = to_block_attr(attr); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 416 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 417 | if (block_attr->store) |
| 418 | return block_attr->store(block, buffer, count); |
| 419 | return -EIO; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 422 | /* edac_dev file operations for a 'block' */ |
| 423 | static struct sysfs_ops device_block_ops = { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 424 | .show = edac_dev_block_show, |
| 425 | .store = edac_dev_block_store |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 426 | }; |
| 427 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 428 | #define BLOCK_ATTR(_name,_mode,_show,_store) \ |
| 429 | static struct block_attribute attr_block_##_name = { \ |
| 430 | .attr = {.name = __stringify(_name), .mode = _mode }, \ |
| 431 | .show = _show, \ |
| 432 | .store = _store, \ |
| 433 | }; |
| 434 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 435 | BLOCK_ATTR(ce_count, S_IRUGO, block_ce_count_show, NULL); |
| 436 | BLOCK_ATTR(ue_count, S_IRUGO, block_ue_count_show, NULL); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* list of edac_dev 'block' attributes */ |
| 439 | static struct block_attribute *device_block_attr[] = { |
| 440 | &attr_block_ce_count, |
| 441 | &attr_block_ue_count, |
| 442 | NULL, |
| 443 | }; |
| 444 | |
| 445 | /* The 'ktype' for each edac_dev 'block' */ |
| 446 | static struct kobj_type ktype_block_ctrl = { |
| 447 | .release = edac_device_ctrl_block_release, |
| 448 | .sysfs_ops = &device_block_ops, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 449 | .default_attrs = (struct attribute **)device_block_attr, |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 452 | /************** block ctor/dtor code ************/ |
| 453 | |
| 454 | /* |
| 455 | * edac_device_create_block |
| 456 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 457 | static int edac_device_create_block(struct edac_device_ctl_info *edac_dev, |
| 458 | struct edac_device_instance *instance, |
| 459 | int idx) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 460 | { |
| 461 | int err; |
| 462 | struct edac_device_block *block; |
| 463 | |
| 464 | block = &instance->blocks[idx]; |
| 465 | |
| 466 | debugf1("%s() Instance '%s' block[%d] '%s'\n", |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 467 | __func__, instance->name, idx, block->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 468 | |
| 469 | /* init this block's kobject */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 470 | memset(&block->kobj, 0, sizeof(struct kobject)); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 471 | block->kobj.parent = &instance->kobj; |
| 472 | block->kobj.ktype = &ktype_block_ctrl; |
| 473 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 474 | err = kobject_set_name(&block->kobj, "%s", block->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 475 | if (err) |
| 476 | return err; |
| 477 | |
| 478 | err = kobject_register(&block->kobj); |
| 479 | if (err) { |
| 480 | debugf1("%s()Failed to register instance '%s'\n", |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 481 | __func__, block->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 482 | return err; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * edac_device_delete_block(edac_dev,j); |
| 490 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 491 | static void edac_device_delete_block(struct edac_device_ctl_info *edac_dev, |
| 492 | struct edac_device_instance *instance, |
| 493 | int idx) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct edac_device_block *block; |
| 496 | |
| 497 | block = &instance->blocks[idx]; |
| 498 | |
| 499 | /* unregister this block's kobject */ |
| 500 | init_completion(&block->kobj_complete); |
| 501 | kobject_unregister(&block->kobj); |
| 502 | wait_for_completion(&block->kobj_complete); |
| 503 | } |
| 504 | |
| 505 | /************** instance ctor/dtor code ************/ |
| 506 | |
| 507 | /* |
| 508 | * edac_device_create_instance |
| 509 | * create just one instance of an edac_device 'instance' |
| 510 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 511 | static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev, |
| 512 | int idx) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 513 | { |
| 514 | int i, j; |
| 515 | int err; |
| 516 | struct edac_device_instance *instance; |
| 517 | |
| 518 | instance = &edac_dev->instances[idx]; |
| 519 | |
| 520 | /* Init the instance's kobject */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 521 | memset(&instance->kobj, 0, sizeof(struct kobject)); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* set this new device under the edac_device main kobject */ |
| 524 | instance->kobj.parent = &edac_dev->kobj; |
| 525 | instance->kobj.ktype = &ktype_instance_ctrl; |
| 526 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 527 | err = kobject_set_name(&instance->kobj, "%s", instance->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 528 | if (err) |
| 529 | return err; |
| 530 | |
| 531 | err = kobject_register(&instance->kobj); |
| 532 | if (err != 0) { |
| 533 | debugf2("%s() Failed to register instance '%s'\n", |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 534 | __func__, instance->name); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 535 | return err; |
| 536 | } |
| 537 | |
| 538 | debugf1("%s() now register '%d' blocks for instance %d\n", |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 539 | __func__, instance->nr_blocks, idx); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 540 | |
| 541 | /* register all blocks of this instance */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 542 | for (i = 0; i < instance->nr_blocks; i++) { |
| 543 | err = edac_device_create_block(edac_dev, instance, i); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 544 | if (err) { |
| 545 | for (j = 0; j < i; j++) { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 546 | edac_device_delete_block(edac_dev, instance, j); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 547 | } |
| 548 | return err; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | debugf1("%s() Registered instance %d '%s' kobject\n", |
| 553 | __func__, idx, instance->name); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * edac_device_remove_instance |
| 560 | * remove an edac_device instance |
| 561 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 562 | static void edac_device_delete_instance(struct edac_device_ctl_info *edac_dev, |
| 563 | int idx) |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 564 | { |
| 565 | int i; |
| 566 | struct edac_device_instance *instance; |
| 567 | |
| 568 | instance = &edac_dev->instances[idx]; |
| 569 | |
| 570 | /* unregister all blocks in this instance */ |
| 571 | for (i = 0; i < instance->nr_blocks; i++) { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 572 | edac_device_delete_block(edac_dev, instance, i); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* unregister this instance's kobject */ |
| 576 | init_completion(&instance->kobj_complete); |
| 577 | kobject_unregister(&instance->kobj); |
| 578 | wait_for_completion(&instance->kobj_complete); |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * edac_device_create_instances |
| 583 | * create the first level of 'instances' for this device |
| 584 | * (ie 'cache' might have 'cache0', 'cache1', 'cache2', etc |
| 585 | */ |
| 586 | static int edac_device_create_instances(struct edac_device_ctl_info *edac_dev) |
| 587 | { |
| 588 | int i, j; |
| 589 | int err; |
| 590 | |
| 591 | debugf0("%s()\n", __func__); |
| 592 | |
| 593 | /* iterate over creation of the instances */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 594 | for (i = 0; i < edac_dev->nr_instances; i++) { |
| 595 | err = edac_device_create_instance(edac_dev, i); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 596 | if (err) { |
| 597 | /* unwind previous instances on error */ |
| 598 | for (j = 0; j < i; j++) { |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 599 | edac_device_delete_instance(edac_dev, j); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 600 | } |
| 601 | return err; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * edac_device_delete_instances(edac_dev); |
| 610 | * unregister all the kobjects of the instances |
| 611 | */ |
| 612 | static void edac_device_delete_instances(struct edac_device_ctl_info *edac_dev) |
| 613 | { |
| 614 | int i; |
| 615 | |
| 616 | /* iterate over creation of the instances */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 617 | for (i = 0; i < edac_dev->nr_instances; i++) { |
| 618 | edac_device_delete_instance(edac_dev, i); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | /******************* edac_dev sysfs ctor/dtor code *************/ |
| 623 | |
| 624 | /* |
| 625 | * edac_device_create_sysfs() Constructor |
| 626 | * |
| 627 | * Create a new edac_device kobject instance, |
| 628 | * |
| 629 | * Return: |
| 630 | * 0 Success |
| 631 | * !0 Failure |
| 632 | */ |
| 633 | int edac_device_create_sysfs(struct edac_device_ctl_info *edac_dev) |
| 634 | { |
| 635 | int err; |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 636 | struct kobject *edac_kobj = &edac_dev->kobj; |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 637 | |
| 638 | /* register this instance's main kobj with the edac class kobj */ |
| 639 | err = edac_device_register_main_kobj(edac_dev); |
| 640 | if (err) |
| 641 | return err; |
| 642 | |
| 643 | debugf0("%s() idx=%d\n", __func__, edac_dev->dev_idx); |
| 644 | |
| 645 | /* create a symlink from the edac device |
| 646 | * to the platform 'device' being used for this |
| 647 | */ |
| 648 | err = sysfs_create_link(edac_kobj, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 649 | &edac_dev->dev->kobj, EDAC_DEVICE_SYMLINK); |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 650 | if (err) { |
| 651 | debugf0("%s() sysfs_create_link() returned err= %d\n", |
| 652 | __func__, err); |
| 653 | return err; |
| 654 | } |
| 655 | |
| 656 | debugf0("%s() calling create-instances, idx=%d\n", |
| 657 | __func__, edac_dev->dev_idx); |
| 658 | |
| 659 | /* Create the first level instance directories */ |
| 660 | err = edac_device_create_instances(edac_dev); |
| 661 | if (err) { |
| 662 | goto error0; |
| 663 | } |
| 664 | |
| 665 | return 0; |
| 666 | |
| 667 | /* Error unwind stack */ |
| 668 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 669 | error0: |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 670 | edac_device_unregister_main_kobj(edac_dev); |
| 671 | |
| 672 | return err; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * edac_device_remove_sysfs() destructor |
| 677 | * |
| 678 | * remove a edac_device instance |
| 679 | */ |
| 680 | void edac_device_remove_sysfs(struct edac_device_ctl_info *edac_dev) |
| 681 | { |
| 682 | debugf0("%s()\n", __func__); |
| 683 | |
| 684 | edac_device_delete_instances(edac_dev); |
| 685 | |
| 686 | /* remove the sym link */ |
| 687 | sysfs_remove_link(&edac_dev->kobj, EDAC_DEVICE_SYMLINK); |
| 688 | |
| 689 | /* unregister the instance's main kobj */ |
| 690 | edac_device_unregister_main_kobj(edac_dev); |
| 691 | } |