2 * Error log support on PowerNV.
4 * Copyright 2013,2014 IBM Corp.
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.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
17 #include <linux/vmalloc.h>
18 #include <linux/fcntl.h>
19 #include <linux/kobject.h>
20 #include <asm/uaccess.h>
25 struct bin_attribute raw_attr;
31 #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
33 struct elog_attribute {
34 struct attribute attr;
35 ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
37 ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
38 const char *buf, size_t count);
40 #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
42 static ssize_t elog_id_show(struct elog_obj *elog_obj,
43 struct elog_attribute *attr,
46 return sprintf(buf, "0x%llx\n", elog_obj->id);
49 static const char *elog_type_to_string(uint64_t type)
53 default: return "unknown";
57 static ssize_t elog_type_show(struct elog_obj *elog_obj,
58 struct elog_attribute *attr,
61 return sprintf(buf, "0x%llx %s\n",
63 elog_type_to_string(elog_obj->type));
66 static ssize_t elog_ack_show(struct elog_obj *elog_obj,
67 struct elog_attribute *attr,
70 return sprintf(buf, "ack - acknowledge log message\n");
73 static ssize_t elog_ack_store(struct elog_obj *elog_obj,
74 struct elog_attribute *attr,
78 opal_send_ack_elog(elog_obj->id);
79 sysfs_remove_file_self(&elog_obj->kobj, &attr->attr);
80 kobject_put(&elog_obj->kobj);
84 static struct elog_attribute id_attribute =
85 __ATTR(id, 0666, elog_id_show, NULL);
86 static struct elog_attribute type_attribute =
87 __ATTR(type, 0666, elog_type_show, NULL);
88 static struct elog_attribute ack_attribute =
89 __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
91 static struct kset *elog_kset;
93 static ssize_t elog_attr_show(struct kobject *kobj,
94 struct attribute *attr,
97 struct elog_attribute *attribute;
98 struct elog_obj *elog;
100 attribute = to_elog_attr(attr);
101 elog = to_elog_obj(kobj);
103 if (!attribute->show)
106 return attribute->show(elog, attribute, buf);
109 static ssize_t elog_attr_store(struct kobject *kobj,
110 struct attribute *attr,
111 const char *buf, size_t len)
113 struct elog_attribute *attribute;
114 struct elog_obj *elog;
116 attribute = to_elog_attr(attr);
117 elog = to_elog_obj(kobj);
119 if (!attribute->store)
122 return attribute->store(elog, attribute, buf, len);
125 static const struct sysfs_ops elog_sysfs_ops = {
126 .show = elog_attr_show,
127 .store = elog_attr_store,
130 static void elog_release(struct kobject *kobj)
132 struct elog_obj *elog;
134 elog = to_elog_obj(kobj);
139 static struct attribute *elog_default_attrs[] = {
141 &type_attribute.attr,
146 static struct kobj_type elog_ktype = {
147 .sysfs_ops = &elog_sysfs_ops,
148 .release = &elog_release,
149 .default_attrs = elog_default_attrs,
152 /* Maximum size of a single log on FSP is 16KB */
153 #define OPAL_MAX_ERRLOG_SIZE 16384
155 static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
156 struct bin_attribute *bin_attr,
157 char *buffer, loff_t pos, size_t count)
161 struct elog_obj *elog = to_elog_obj(kobj);
163 /* We may have had an error reading before, so let's retry */
165 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
169 opal_rc = opal_read_elog(__pa(elog->buffer),
170 elog->size, elog->id);
171 if (opal_rc != OPAL_SUCCESS) {
172 pr_err("ELOG: log read failed for log-id=%llx\n",
180 memcpy(buffer, elog->buffer + pos, count);
185 static struct elog_obj *create_elog_obj(uint64_t id, size_t size, uint64_t type)
187 struct elog_obj *elog;
190 elog = kzalloc(sizeof(*elog), GFP_KERNEL);
194 elog->kobj.kset = elog_kset;
196 kobject_init(&elog->kobj, &elog_ktype);
198 sysfs_bin_attr_init(&elog->raw_attr);
200 elog->raw_attr.attr.name = "raw";
201 elog->raw_attr.attr.mode = 0400;
202 elog->raw_attr.size = size;
203 elog->raw_attr.read = raw_attr_read;
209 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
212 rc = opal_read_elog(__pa(elog->buffer),
213 elog->size, elog->id);
214 if (rc != OPAL_SUCCESS) {
215 pr_err("ELOG: log read failed for log-id=%llx\n",
222 rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
224 kobject_put(&elog->kobj);
228 rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
230 kobject_put(&elog->kobj);
234 kobject_uevent(&elog->kobj, KOBJ_ADD);
239 static void elog_work_fn(struct work_struct *work)
250 rc = opal_get_elog_size(&id, &size, &type);
251 if (rc != OPAL_SUCCESS) {
252 pr_err("ELOG: Opal log read failed\n");
256 elog_size = be64_to_cpu(size);
257 log_id = be64_to_cpu(id);
258 elog_type = be64_to_cpu(type);
260 BUG_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
262 if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
263 elog_size = OPAL_MAX_ERRLOG_SIZE;
265 sprintf(name, "0x%llx", log_id);
267 /* we may get notified twice, let's handle
268 * that gracefully and not create two conflicting
271 if (kset_find_obj(elog_kset, name))
274 create_elog_obj(log_id, elog_size, elog_type);
277 static DECLARE_WORK(elog_work, elog_work_fn);
279 static int elog_event(struct notifier_block *nb,
280 unsigned long events, void *change)
282 /* check for error log event */
283 if (events & OPAL_EVENT_ERROR_LOG_AVAIL)
284 schedule_work(&elog_work);
288 static struct notifier_block elog_nb = {
289 .notifier_call = elog_event,
294 int __init opal_elog_init(void)
298 elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
300 pr_warn("%s: failed to create elog kset\n", __func__);
304 rc = opal_notifier_register(&elog_nb);
306 pr_err("%s: Can't register OPAL event notifier (%d)\n",
311 /* We are now ready to pull error logs from opal. */
312 opal_resend_pending_logs();