1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Error log support on PowerNV.
4 *
5 * Copyright 2013,2014 IBM Corp.
6 */
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/of.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/fs.h>
14 #include <linux/vmalloc.h>
15 #include <linux/fcntl.h>
16 #include <linux/kobject.h>
17 #include <linux/uaccess.h>
18 #include <asm/opal.h>
19
20 struct elog_obj {
21 struct kobject kobj;
22 struct bin_attribute raw_attr;
23 uint64_t id;
24 uint64_t type;
25 size_t size;
26 char *buffer;
27 };
28 #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
29
30 struct elog_attribute {
31 struct attribute attr;
32 ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
33 char *buf);
34 ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
35 const char *buf, size_t count);
36 };
37 #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
38
elog_id_show(struct elog_obj * elog_obj,struct elog_attribute * attr,char * buf)39 static ssize_t elog_id_show(struct elog_obj *elog_obj,
40 struct elog_attribute *attr,
41 char *buf)
42 {
43 return sprintf(buf, "0x%llx\n", elog_obj->id);
44 }
45
elog_type_to_string(uint64_t type)46 static const char *elog_type_to_string(uint64_t type)
47 {
48 switch (type) {
49 case 0: return "PEL";
50 default: return "unknown";
51 }
52 }
53
elog_type_show(struct elog_obj * elog_obj,struct elog_attribute * attr,char * buf)54 static ssize_t elog_type_show(struct elog_obj *elog_obj,
55 struct elog_attribute *attr,
56 char *buf)
57 {
58 return sprintf(buf, "0x%llx %s\n",
59 elog_obj->type,
60 elog_type_to_string(elog_obj->type));
61 }
62
elog_ack_show(struct elog_obj * elog_obj,struct elog_attribute * attr,char * buf)63 static ssize_t elog_ack_show(struct elog_obj *elog_obj,
64 struct elog_attribute *attr,
65 char *buf)
66 {
67 return sprintf(buf, "ack - acknowledge log message\n");
68 }
69
elog_ack_store(struct elog_obj * elog_obj,struct elog_attribute * attr,const char * buf,size_t count)70 static ssize_t elog_ack_store(struct elog_obj *elog_obj,
71 struct elog_attribute *attr,
72 const char *buf,
73 size_t count)
74 {
75 opal_send_ack_elog(elog_obj->id);
76 sysfs_remove_file_self(&elog_obj->kobj, &attr->attr);
77 kobject_put(&elog_obj->kobj);
78 return count;
79 }
80
81 static struct elog_attribute id_attribute =
82 __ATTR(id, 0444, elog_id_show, NULL);
83 static struct elog_attribute type_attribute =
84 __ATTR(type, 0444, elog_type_show, NULL);
85 static struct elog_attribute ack_attribute =
86 __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
87
88 static struct kset *elog_kset;
89
elog_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)90 static ssize_t elog_attr_show(struct kobject *kobj,
91 struct attribute *attr,
92 char *buf)
93 {
94 struct elog_attribute *attribute;
95 struct elog_obj *elog;
96
97 attribute = to_elog_attr(attr);
98 elog = to_elog_obj(kobj);
99
100 if (!attribute->show)
101 return -EIO;
102
103 return attribute->show(elog, attribute, buf);
104 }
105
elog_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)106 static ssize_t elog_attr_store(struct kobject *kobj,
107 struct attribute *attr,
108 const char *buf, size_t len)
109 {
110 struct elog_attribute *attribute;
111 struct elog_obj *elog;
112
113 attribute = to_elog_attr(attr);
114 elog = to_elog_obj(kobj);
115
116 if (!attribute->store)
117 return -EIO;
118
119 return attribute->store(elog, attribute, buf, len);
120 }
121
122 static const struct sysfs_ops elog_sysfs_ops = {
123 .show = elog_attr_show,
124 .store = elog_attr_store,
125 };
126
elog_release(struct kobject * kobj)127 static void elog_release(struct kobject *kobj)
128 {
129 struct elog_obj *elog;
130
131 elog = to_elog_obj(kobj);
132 kfree(elog->buffer);
133 kfree(elog);
134 }
135
136 static struct attribute *elog_default_attrs[] = {
137 &id_attribute.attr,
138 &type_attribute.attr,
139 &ack_attribute.attr,
140 NULL,
141 };
142
143 static struct kobj_type elog_ktype = {
144 .sysfs_ops = &elog_sysfs_ops,
145 .release = &elog_release,
146 .default_attrs = elog_default_attrs,
147 };
148
149 /* Maximum size of a single log on FSP is 16KB */
150 #define OPAL_MAX_ERRLOG_SIZE 16384
151
raw_attr_read(struct file * filep,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t pos,size_t count)152 static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
153 struct bin_attribute *bin_attr,
154 char *buffer, loff_t pos, size_t count)
155 {
156 int opal_rc;
157
158 struct elog_obj *elog = to_elog_obj(kobj);
159
160 /* We may have had an error reading before, so let's retry */
161 if (!elog->buffer) {
162 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
163 if (!elog->buffer)
164 return -EIO;
165
166 opal_rc = opal_read_elog(__pa(elog->buffer),
167 elog->size, elog->id);
168 if (opal_rc != OPAL_SUCCESS) {
169 pr_err("ELOG: log read failed for log-id=%llx\n",
170 elog->id);
171 kfree(elog->buffer);
172 elog->buffer = NULL;
173 return -EIO;
174 }
175 }
176
177 memcpy(buffer, elog->buffer + pos, count);
178
179 return count;
180 }
181
create_elog_obj(uint64_t id,size_t size,uint64_t type)182 static void create_elog_obj(uint64_t id, size_t size, uint64_t type)
183 {
184 struct elog_obj *elog;
185 int rc;
186
187 elog = kzalloc(sizeof(*elog), GFP_KERNEL);
188 if (!elog)
189 return;
190
191 elog->kobj.kset = elog_kset;
192
193 kobject_init(&elog->kobj, &elog_ktype);
194
195 sysfs_bin_attr_init(&elog->raw_attr);
196
197 elog->raw_attr.attr.name = "raw";
198 elog->raw_attr.attr.mode = 0400;
199 elog->raw_attr.size = size;
200 elog->raw_attr.read = raw_attr_read;
201
202 elog->id = id;
203 elog->size = size;
204 elog->type = type;
205
206 elog->buffer = kzalloc(elog->size, GFP_KERNEL);
207
208 if (elog->buffer) {
209 rc = opal_read_elog(__pa(elog->buffer),
210 elog->size, elog->id);
211 if (rc != OPAL_SUCCESS) {
212 pr_err("ELOG: log read failed for log-id=%llx\n",
213 elog->id);
214 kfree(elog->buffer);
215 elog->buffer = NULL;
216 }
217 }
218
219 rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
220 if (rc) {
221 kobject_put(&elog->kobj);
222 return;
223 }
224
225 /*
226 * As soon as the sysfs file for this elog is created/activated there is
227 * a chance the opal_errd daemon (or any userspace) might read and
228 * acknowledge the elog before kobject_uevent() is called. If that
229 * happens then there is a potential race between
230 * elog_ack_store->kobject_put() and kobject_uevent() which leads to a
231 * use-after-free of a kernfs object resulting in a kernel crash.
232 *
233 * To avoid that, we need to take a reference on behalf of the bin file,
234 * so that our reference remains valid while we call kobject_uevent().
235 * We then drop our reference before exiting the function, leaving the
236 * bin file to drop the last reference (if it hasn't already).
237 */
238
239 /* Take a reference for the bin file */
240 kobject_get(&elog->kobj);
241 rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
242 if (rc == 0) {
243 kobject_uevent(&elog->kobj, KOBJ_ADD);
244 } else {
245 /* Drop the reference taken for the bin file */
246 kobject_put(&elog->kobj);
247 }
248
249 /* Drop our reference */
250 kobject_put(&elog->kobj);
251
252 return;
253 }
254
elog_event(int irq,void * data)255 static irqreturn_t elog_event(int irq, void *data)
256 {
257 __be64 size;
258 __be64 id;
259 __be64 type;
260 uint64_t elog_size;
261 uint64_t log_id;
262 uint64_t elog_type;
263 int rc;
264 char name[2+16+1];
265 struct kobject *kobj;
266
267 rc = opal_get_elog_size(&id, &size, &type);
268 if (rc != OPAL_SUCCESS) {
269 pr_err("ELOG: OPAL log info read failed\n");
270 return IRQ_HANDLED;
271 }
272
273 elog_size = be64_to_cpu(size);
274 log_id = be64_to_cpu(id);
275 elog_type = be64_to_cpu(type);
276
277 WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
278
279 if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
280 elog_size = OPAL_MAX_ERRLOG_SIZE;
281
282 sprintf(name, "0x%llx", log_id);
283
284 /* we may get notified twice, let's handle
285 * that gracefully and not create two conflicting
286 * entries.
287 */
288 kobj = kset_find_obj(elog_kset, name);
289 if (kobj) {
290 /* Drop reference added by kset_find_obj() */
291 kobject_put(kobj);
292 return IRQ_HANDLED;
293 }
294
295 create_elog_obj(log_id, elog_size, elog_type);
296
297 return IRQ_HANDLED;
298 }
299
opal_elog_init(void)300 int __init opal_elog_init(void)
301 {
302 int rc = 0, irq;
303
304 /* ELOG not supported by firmware */
305 if (!opal_check_token(OPAL_ELOG_READ))
306 return -1;
307
308 elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
309 if (!elog_kset) {
310 pr_warn("%s: failed to create elog kset\n", __func__);
311 return -1;
312 }
313
314 irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
315 if (!irq) {
316 pr_err("%s: Can't register OPAL event irq (%d)\n",
317 __func__, irq);
318 return irq;
319 }
320
321 rc = request_threaded_irq(irq, NULL, elog_event,
322 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
323 if (rc) {
324 pr_err("%s: Can't request OPAL event irq (%d)\n",
325 __func__, rc);
326 return rc;
327 }
328
329 /* We are now ready to pull error logs from opal. */
330 if (opal_check_token(OPAL_ELOG_RESEND))
331 opal_resend_pending_logs();
332
333 return 0;
334 }
335