Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Error log support on PowerNV. |
| 3 | * |
| 4 | * Copyright 2013,2014 IBM Corp. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
Alistair Popple | 74159a7 | 2015-05-15 14:06:42 +1000 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 14 | #include <linux/of.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/sysfs.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/fcntl.h> |
| 20 | #include <linux/kobject.h> |
| 21 | #include <asm/uaccess.h> |
| 22 | #include <asm/opal.h> |
| 23 | |
| 24 | struct elog_obj { |
| 25 | struct kobject kobj; |
| 26 | struct bin_attribute raw_attr; |
| 27 | uint64_t id; |
| 28 | uint64_t type; |
| 29 | size_t size; |
| 30 | char *buffer; |
| 31 | }; |
| 32 | #define to_elog_obj(x) container_of(x, struct elog_obj, kobj) |
| 33 | |
| 34 | struct elog_attribute { |
| 35 | struct attribute attr; |
| 36 | ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr, |
| 37 | char *buf); |
| 38 | ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr, |
| 39 | const char *buf, size_t count); |
| 40 | }; |
| 41 | #define to_elog_attr(x) container_of(x, struct elog_attribute, attr) |
| 42 | |
| 43 | static ssize_t elog_id_show(struct elog_obj *elog_obj, |
| 44 | struct elog_attribute *attr, |
| 45 | char *buf) |
| 46 | { |
| 47 | return sprintf(buf, "0x%llx\n", elog_obj->id); |
| 48 | } |
| 49 | |
| 50 | static const char *elog_type_to_string(uint64_t type) |
| 51 | { |
| 52 | switch (type) { |
| 53 | case 0: return "PEL"; |
| 54 | default: return "unknown"; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static ssize_t elog_type_show(struct elog_obj *elog_obj, |
| 59 | struct elog_attribute *attr, |
| 60 | char *buf) |
| 61 | { |
| 62 | return sprintf(buf, "0x%llx %s\n", |
| 63 | elog_obj->type, |
| 64 | elog_type_to_string(elog_obj->type)); |
| 65 | } |
| 66 | |
| 67 | static ssize_t elog_ack_show(struct elog_obj *elog_obj, |
| 68 | struct elog_attribute *attr, |
| 69 | char *buf) |
| 70 | { |
| 71 | return sprintf(buf, "ack - acknowledge log message\n"); |
| 72 | } |
| 73 | |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 74 | static ssize_t elog_ack_store(struct elog_obj *elog_obj, |
| 75 | struct elog_attribute *attr, |
| 76 | const char *buf, |
| 77 | size_t count) |
| 78 | { |
| 79 | opal_send_ack_elog(elog_obj->id); |
Stewart Smith | cc4f265 | 2014-04-09 13:47:37 +1000 | [diff] [blame] | 80 | sysfs_remove_file_self(&elog_obj->kobj, &attr->attr); |
| 81 | kobject_put(&elog_obj->kobj); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 82 | return count; |
| 83 | } |
| 84 | |
| 85 | static struct elog_attribute id_attribute = |
Rusty Russell | 6656c21 | 2014-08-06 10:15:36 +0930 | [diff] [blame] | 86 | __ATTR(id, S_IRUGO, elog_id_show, NULL); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 87 | static struct elog_attribute type_attribute = |
Rusty Russell | 6656c21 | 2014-08-06 10:15:36 +0930 | [diff] [blame] | 88 | __ATTR(type, S_IRUGO, elog_type_show, NULL); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 89 | static struct elog_attribute ack_attribute = |
| 90 | __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store); |
| 91 | |
| 92 | static struct kset *elog_kset; |
| 93 | |
| 94 | static ssize_t elog_attr_show(struct kobject *kobj, |
| 95 | struct attribute *attr, |
| 96 | char *buf) |
| 97 | { |
| 98 | struct elog_attribute *attribute; |
| 99 | struct elog_obj *elog; |
| 100 | |
| 101 | attribute = to_elog_attr(attr); |
| 102 | elog = to_elog_obj(kobj); |
| 103 | |
| 104 | if (!attribute->show) |
| 105 | return -EIO; |
| 106 | |
| 107 | return attribute->show(elog, attribute, buf); |
| 108 | } |
| 109 | |
| 110 | static ssize_t elog_attr_store(struct kobject *kobj, |
| 111 | struct attribute *attr, |
| 112 | const char *buf, size_t len) |
| 113 | { |
| 114 | struct elog_attribute *attribute; |
| 115 | struct elog_obj *elog; |
| 116 | |
| 117 | attribute = to_elog_attr(attr); |
| 118 | elog = to_elog_obj(kobj); |
| 119 | |
| 120 | if (!attribute->store) |
| 121 | return -EIO; |
| 122 | |
| 123 | return attribute->store(elog, attribute, buf, len); |
| 124 | } |
| 125 | |
| 126 | static const struct sysfs_ops elog_sysfs_ops = { |
| 127 | .show = elog_attr_show, |
| 128 | .store = elog_attr_store, |
| 129 | }; |
| 130 | |
| 131 | static void elog_release(struct kobject *kobj) |
| 132 | { |
| 133 | struct elog_obj *elog; |
| 134 | |
| 135 | elog = to_elog_obj(kobj); |
| 136 | kfree(elog->buffer); |
| 137 | kfree(elog); |
| 138 | } |
| 139 | |
| 140 | static struct attribute *elog_default_attrs[] = { |
| 141 | &id_attribute.attr, |
| 142 | &type_attribute.attr, |
| 143 | &ack_attribute.attr, |
| 144 | NULL, |
| 145 | }; |
| 146 | |
| 147 | static struct kobj_type elog_ktype = { |
| 148 | .sysfs_ops = &elog_sysfs_ops, |
| 149 | .release = &elog_release, |
| 150 | .default_attrs = elog_default_attrs, |
| 151 | }; |
| 152 | |
| 153 | /* Maximum size of a single log on FSP is 16KB */ |
| 154 | #define OPAL_MAX_ERRLOG_SIZE 16384 |
| 155 | |
| 156 | static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj, |
| 157 | struct bin_attribute *bin_attr, |
| 158 | char *buffer, loff_t pos, size_t count) |
| 159 | { |
| 160 | int opal_rc; |
| 161 | |
| 162 | struct elog_obj *elog = to_elog_obj(kobj); |
| 163 | |
| 164 | /* We may have had an error reading before, so let's retry */ |
| 165 | if (!elog->buffer) { |
| 166 | elog->buffer = kzalloc(elog->size, GFP_KERNEL); |
| 167 | if (!elog->buffer) |
| 168 | return -EIO; |
| 169 | |
| 170 | opal_rc = opal_read_elog(__pa(elog->buffer), |
| 171 | elog->size, elog->id); |
| 172 | if (opal_rc != OPAL_SUCCESS) { |
| 173 | pr_err("ELOG: log read failed for log-id=%llx\n", |
| 174 | elog->id); |
| 175 | kfree(elog->buffer); |
| 176 | elog->buffer = NULL; |
| 177 | return -EIO; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | memcpy(buffer, elog->buffer + pos, count); |
| 182 | |
| 183 | return count; |
| 184 | } |
| 185 | |
| 186 | static struct elog_obj *create_elog_obj(uint64_t id, size_t size, uint64_t type) |
| 187 | { |
| 188 | struct elog_obj *elog; |
| 189 | int rc; |
| 190 | |
| 191 | elog = kzalloc(sizeof(*elog), GFP_KERNEL); |
| 192 | if (!elog) |
| 193 | return NULL; |
| 194 | |
| 195 | elog->kobj.kset = elog_kset; |
| 196 | |
| 197 | kobject_init(&elog->kobj, &elog_ktype); |
| 198 | |
| 199 | sysfs_bin_attr_init(&elog->raw_attr); |
| 200 | |
| 201 | elog->raw_attr.attr.name = "raw"; |
| 202 | elog->raw_attr.attr.mode = 0400; |
| 203 | elog->raw_attr.size = size; |
| 204 | elog->raw_attr.read = raw_attr_read; |
| 205 | |
| 206 | elog->id = id; |
| 207 | elog->size = size; |
| 208 | elog->type = type; |
| 209 | |
| 210 | elog->buffer = kzalloc(elog->size, GFP_KERNEL); |
| 211 | |
| 212 | if (elog->buffer) { |
| 213 | rc = opal_read_elog(__pa(elog->buffer), |
| 214 | elog->size, elog->id); |
| 215 | if (rc != OPAL_SUCCESS) { |
| 216 | pr_err("ELOG: log read failed for log-id=%llx\n", |
| 217 | elog->id); |
| 218 | kfree(elog->buffer); |
| 219 | elog->buffer = NULL; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | rc = kobject_add(&elog->kobj, NULL, "0x%llx", id); |
| 224 | if (rc) { |
| 225 | kobject_put(&elog->kobj); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr); |
| 230 | if (rc) { |
| 231 | kobject_put(&elog->kobj); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | kobject_uevent(&elog->kobj, KOBJ_ADD); |
| 236 | |
| 237 | return elog; |
| 238 | } |
| 239 | |
Alistair Popple | a8956a7 | 2015-07-03 17:39:12 +1000 | [diff] [blame^] | 240 | static irqreturn_t elog_event(int irq, void *data) |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 241 | { |
Anton Blanchard | 14ad0c5 | 2014-04-22 15:01:25 +1000 | [diff] [blame] | 242 | __be64 size; |
| 243 | __be64 id; |
| 244 | __be64 type; |
Anton Blanchard | 2bad742 | 2014-04-22 15:01:22 +1000 | [diff] [blame] | 245 | uint64_t elog_size; |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 246 | uint64_t log_id; |
| 247 | uint64_t elog_type; |
| 248 | int rc; |
| 249 | char name[2+16+1]; |
| 250 | |
Anton Blanchard | 14ad0c5 | 2014-04-22 15:01:25 +1000 | [diff] [blame] | 251 | rc = opal_get_elog_size(&id, &size, &type); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 252 | if (rc != OPAL_SUCCESS) { |
Vasant Hegde | fa952c5 | 2014-07-23 14:52:39 +0530 | [diff] [blame] | 253 | pr_err("ELOG: OPAL log info read failed\n"); |
Alistair Popple | a8956a7 | 2015-07-03 17:39:12 +1000 | [diff] [blame^] | 254 | return IRQ_HANDLED; |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 255 | } |
| 256 | |
Anton Blanchard | 14ad0c5 | 2014-04-22 15:01:25 +1000 | [diff] [blame] | 257 | elog_size = be64_to_cpu(size); |
| 258 | log_id = be64_to_cpu(id); |
| 259 | elog_type = be64_to_cpu(type); |
| 260 | |
Vasant Hegde | fa952c5 | 2014-07-23 14:52:39 +0530 | [diff] [blame] | 261 | WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 262 | |
| 263 | if (elog_size >= OPAL_MAX_ERRLOG_SIZE) |
| 264 | elog_size = OPAL_MAX_ERRLOG_SIZE; |
| 265 | |
| 266 | sprintf(name, "0x%llx", log_id); |
| 267 | |
| 268 | /* we may get notified twice, let's handle |
| 269 | * that gracefully and not create two conflicting |
| 270 | * entries. |
| 271 | */ |
| 272 | if (kset_find_obj(elog_kset, name)) |
Alistair Popple | a8956a7 | 2015-07-03 17:39:12 +1000 | [diff] [blame^] | 273 | return IRQ_HANDLED; |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 274 | |
| 275 | create_elog_obj(log_id, elog_size, elog_type); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 276 | |
Alistair Popple | 74159a7 | 2015-05-15 14:06:42 +1000 | [diff] [blame] | 277 | return IRQ_HANDLED; |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 278 | } |
| 279 | |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 280 | int __init opal_elog_init(void) |
| 281 | { |
Alistair Popple | 74159a7 | 2015-05-15 14:06:42 +1000 | [diff] [blame] | 282 | int rc = 0, irq; |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 283 | |
Michael Neuling | 7dc992e | 2014-08-19 14:48:01 +1000 | [diff] [blame] | 284 | /* ELOG not supported by firmware */ |
| 285 | if (!opal_check_token(OPAL_ELOG_READ)) |
| 286 | return -1; |
| 287 | |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 288 | elog_kset = kset_create_and_add("elog", NULL, opal_kobj); |
| 289 | if (!elog_kset) { |
| 290 | pr_warn("%s: failed to create elog kset\n", __func__); |
| 291 | return -1; |
| 292 | } |
| 293 | |
Alistair Popple | 74159a7 | 2015-05-15 14:06:42 +1000 | [diff] [blame] | 294 | irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL)); |
| 295 | if (!irq) { |
| 296 | pr_err("%s: Can't register OPAL event irq (%d)\n", |
| 297 | __func__, irq); |
| 298 | return irq; |
| 299 | } |
| 300 | |
Alistair Popple | a8956a7 | 2015-07-03 17:39:12 +1000 | [diff] [blame^] | 301 | rc = request_threaded_irq(irq, NULL, elog_event, |
| 302 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 303 | if (rc) { |
Alistair Popple | 74159a7 | 2015-05-15 14:06:42 +1000 | [diff] [blame] | 304 | pr_err("%s: Can't request OPAL event irq (%d)\n", |
| 305 | __func__, rc); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 306 | return rc; |
| 307 | } |
| 308 | |
| 309 | /* We are now ready to pull error logs from opal. */ |
Stewart Smith | fc81de6 | 2015-02-12 16:25:28 +1100 | [diff] [blame] | 310 | if (opal_check_token(OPAL_ELOG_RESEND)) |
| 311 | opal_resend_pending_logs(); |
Stewart Smith | 774fea1 | 2014-02-28 11:58:32 +1100 | [diff] [blame] | 312 | |
| 313 | return 0; |
| 314 | } |