GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / powerpc / platforms / powernv / opal-dump.c
1 /*
2  * PowerNV OPAL Dump Interface
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
12 #include <linux/kobject.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19
20 #include <asm/opal.h>
21
22 #define DUMP_TYPE_FSP   0x01
23
24 struct dump_obj {
25         struct kobject  kobj;
26         struct bin_attribute dump_attr;
27         uint32_t        id;  /* becomes object name */
28         uint32_t        type;
29         uint32_t        size;
30         char            *buffer;
31 };
32 #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
33
34 struct dump_attribute {
35         struct attribute attr;
36         ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
37                         char *buf);
38         ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
39                          const char *buf, size_t count);
40 };
41 #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
42
43 static ssize_t dump_id_show(struct dump_obj *dump_obj,
44                             struct dump_attribute *attr,
45                             char *buf)
46 {
47         return sprintf(buf, "0x%x\n", dump_obj->id);
48 }
49
50 static const char* dump_type_to_string(uint32_t type)
51 {
52         switch (type) {
53         case 0x01: return "SP Dump";
54         case 0x02: return "System/Platform Dump";
55         case 0x03: return "SMA Dump";
56         default: return "unknown";
57         }
58 }
59
60 static ssize_t dump_type_show(struct dump_obj *dump_obj,
61                               struct dump_attribute *attr,
62                               char *buf)
63 {
64
65         return sprintf(buf, "0x%x %s\n", dump_obj->type,
66                        dump_type_to_string(dump_obj->type));
67 }
68
69 static ssize_t dump_ack_show(struct dump_obj *dump_obj,
70                              struct dump_attribute *attr,
71                              char *buf)
72 {
73         return sprintf(buf, "ack - acknowledge dump\n");
74 }
75
76 /*
77  * Send acknowledgement to OPAL
78  */
79 static int64_t dump_send_ack(uint32_t dump_id)
80 {
81         int rc;
82
83         rc = opal_dump_ack(dump_id);
84         if (rc)
85                 pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
86                         __func__, dump_id, rc);
87         return rc;
88 }
89
90 static ssize_t dump_ack_store(struct dump_obj *dump_obj,
91                               struct dump_attribute *attr,
92                               const char *buf,
93                               size_t count)
94 {
95         dump_send_ack(dump_obj->id);
96         sysfs_remove_file_self(&dump_obj->kobj, &attr->attr);
97         kobject_put(&dump_obj->kobj);
98         return count;
99 }
100
101 /* Attributes of a dump
102  * The binary attribute of the dump itself is dynamic
103  * due to the dynamic size of the dump
104  */
105 static struct dump_attribute id_attribute =
106         __ATTR(id, 0444, dump_id_show, NULL);
107 static struct dump_attribute type_attribute =
108         __ATTR(type, 0444, dump_type_show, NULL);
109 static struct dump_attribute ack_attribute =
110         __ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
111
112 static ssize_t init_dump_show(struct dump_obj *dump_obj,
113                               struct dump_attribute *attr,
114                               char *buf)
115 {
116         return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
117 }
118
119 static int64_t dump_fips_init(uint8_t type)
120 {
121         int rc;
122
123         rc = opal_dump_init(type);
124         if (rc)
125                 pr_warn("%s: Failed to initiate FSP dump (%d)\n",
126                         __func__, rc);
127         return rc;
128 }
129
130 static ssize_t init_dump_store(struct dump_obj *dump_obj,
131                                struct dump_attribute *attr,
132                                const char *buf,
133                                size_t count)
134 {
135         int rc;
136
137         rc = dump_fips_init(DUMP_TYPE_FSP);
138         if (rc == OPAL_SUCCESS)
139                 pr_info("%s: Initiated FSP dump\n", __func__);
140
141         return count;
142 }
143
144 static struct dump_attribute initiate_attribute =
145         __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
146
147 static struct attribute *initiate_attrs[] = {
148         &initiate_attribute.attr,
149         NULL,
150 };
151
152 static struct attribute_group initiate_attr_group = {
153         .attrs = initiate_attrs,
154 };
155
156 static struct kset *dump_kset;
157
158 static ssize_t dump_attr_show(struct kobject *kobj,
159                               struct attribute *attr,
160                               char *buf)
161 {
162         struct dump_attribute *attribute;
163         struct dump_obj *dump;
164
165         attribute = to_dump_attr(attr);
166         dump = to_dump_obj(kobj);
167
168         if (!attribute->show)
169                 return -EIO;
170
171         return attribute->show(dump, attribute, buf);
172 }
173
174 static ssize_t dump_attr_store(struct kobject *kobj,
175                                struct attribute *attr,
176                                const char *buf, size_t len)
177 {
178         struct dump_attribute *attribute;
179         struct dump_obj *dump;
180
181         attribute = to_dump_attr(attr);
182         dump = to_dump_obj(kobj);
183
184         if (!attribute->store)
185                 return -EIO;
186
187         return attribute->store(dump, attribute, buf, len);
188 }
189
190 static const struct sysfs_ops dump_sysfs_ops = {
191         .show = dump_attr_show,
192         .store = dump_attr_store,
193 };
194
195 static void dump_release(struct kobject *kobj)
196 {
197         struct dump_obj *dump;
198
199         dump = to_dump_obj(kobj);
200         vfree(dump->buffer);
201         kfree(dump);
202 }
203
204 static struct attribute *dump_default_attrs[] = {
205         &id_attribute.attr,
206         &type_attribute.attr,
207         &ack_attribute.attr,
208         NULL,
209 };
210
211 static struct kobj_type dump_ktype = {
212         .sysfs_ops = &dump_sysfs_ops,
213         .release = &dump_release,
214         .default_attrs = dump_default_attrs,
215 };
216
217 static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
218 {
219         __be32 id, size, type;
220         int rc;
221
222         type = cpu_to_be32(0xffffffff);
223
224         rc = opal_dump_info2(&id, &size, &type);
225         if (rc == OPAL_PARAMETER)
226                 rc = opal_dump_info(&id, &size);
227
228         if (rc) {
229                 pr_warn("%s: Failed to get dump info (%d)\n",
230                         __func__, rc);
231                 return rc;
232         }
233
234         *dump_id = be32_to_cpu(id);
235         *dump_size = be32_to_cpu(size);
236         *dump_type = be32_to_cpu(type);
237
238         return rc;
239 }
240
241 static int64_t dump_read_data(struct dump_obj *dump)
242 {
243         struct opal_sg_list *list;
244         uint64_t addr;
245         int64_t rc;
246
247         /* Allocate memory */
248         dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
249         if (!dump->buffer) {
250                 pr_err("%s : Failed to allocate memory\n", __func__);
251                 rc = -ENOMEM;
252                 goto out;
253         }
254
255         /* Generate SG list */
256         list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
257         if (!list) {
258                 rc = -ENOMEM;
259                 goto out;
260         }
261
262         /* First entry address */
263         addr = __pa(list);
264
265         /* Fetch data */
266         rc = OPAL_BUSY_EVENT;
267         while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
268                 rc = opal_dump_read(dump->id, addr);
269                 if (rc == OPAL_BUSY_EVENT) {
270                         opal_poll_events(NULL);
271                         msleep(20);
272                 }
273         }
274
275         if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
276                 pr_warn("%s: Extract dump failed for ID 0x%x\n",
277                         __func__, dump->id);
278
279         /* Free SG list */
280         opal_free_sg_list(list);
281
282 out:
283         return rc;
284 }
285
286 static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
287                               struct bin_attribute *bin_attr,
288                               char *buffer, loff_t pos, size_t count)
289 {
290         ssize_t rc;
291
292         struct dump_obj *dump = to_dump_obj(kobj);
293
294         if (!dump->buffer) {
295                 rc = dump_read_data(dump);
296
297                 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
298                         vfree(dump->buffer);
299                         dump->buffer = NULL;
300
301                         return -EIO;
302                 }
303                 if (rc == OPAL_PARTIAL) {
304                         /* On a partial read, we just return EIO
305                          * and rely on userspace to ask us to try
306                          * again.
307                          */
308                         pr_info("%s: Platform dump partially read. ID = 0x%x\n",
309                                 __func__, dump->id);
310                         return -EIO;
311                 }
312         }
313
314         memcpy(buffer, dump->buffer + pos, count);
315
316         /* You may think we could free the dump buffer now and retrieve
317          * it again later if needed, but due to current firmware limitation,
318          * that's not the case. So, once read into userspace once,
319          * we keep the dump around until it's acknowledged by userspace.
320          */
321
322         return count;
323 }
324
325 static void create_dump_obj(uint32_t id, size_t size, uint32_t type)
326 {
327         struct dump_obj *dump;
328         int rc;
329
330         dump = kzalloc(sizeof(*dump), GFP_KERNEL);
331         if (!dump)
332                 return;
333
334         dump->kobj.kset = dump_kset;
335
336         kobject_init(&dump->kobj, &dump_ktype);
337
338         sysfs_bin_attr_init(&dump->dump_attr);
339
340         dump->dump_attr.attr.name = "dump";
341         dump->dump_attr.attr.mode = 0400;
342         dump->dump_attr.size = size;
343         dump->dump_attr.read = dump_attr_read;
344
345         dump->id = id;
346         dump->size = size;
347         dump->type = type;
348
349         rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
350         if (rc) {
351                 kobject_put(&dump->kobj);
352                 return;
353         }
354
355         /*
356          * As soon as the sysfs file for this dump is created/activated there is
357          * a chance the opal_errd daemon (or any userspace) might read and
358          * acknowledge the dump before kobject_uevent() is called. If that
359          * happens then there is a potential race between
360          * dump_ack_store->kobject_put() and kobject_uevent() which leads to a
361          * use-after-free of a kernfs object resulting in a kernel crash.
362          *
363          * To avoid that, we need to take a reference on behalf of the bin file,
364          * so that our reference remains valid while we call kobject_uevent().
365          * We then drop our reference before exiting the function, leaving the
366          * bin file to drop the last reference (if it hasn't already).
367          */
368
369         /* Take a reference for the bin file */
370         kobject_get(&dump->kobj);
371         rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
372         if (rc == 0) {
373                 kobject_uevent(&dump->kobj, KOBJ_ADD);
374
375                 pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
376                         __func__, dump->id, dump->size);
377         } else {
378                 /* Drop reference count taken for bin file */
379                 kobject_put(&dump->kobj);
380         }
381
382         /* Drop our reference */
383         kobject_put(&dump->kobj);
384         return;
385 }
386
387 static irqreturn_t process_dump(int irq, void *data)
388 {
389         int rc;
390         uint32_t dump_id, dump_size, dump_type;
391         char name[22];
392         struct kobject *kobj;
393
394         rc = dump_read_info(&dump_id, &dump_size, &dump_type);
395         if (rc != OPAL_SUCCESS)
396                 return IRQ_HANDLED;
397
398         sprintf(name, "0x%x-0x%x", dump_type, dump_id);
399
400         /* we may get notified twice, let's handle
401          * that gracefully and not create two conflicting
402          * entries.
403          */
404         kobj = kset_find_obj(dump_kset, name);
405         if (kobj) {
406                 /* Drop reference added by kset_find_obj() */
407                 kobject_put(kobj);
408                 return IRQ_HANDLED;
409         }
410
411         create_dump_obj(dump_id, dump_size, dump_type);
412
413         return IRQ_HANDLED;
414 }
415
416 void __init opal_platform_dump_init(void)
417 {
418         int rc;
419         int dump_irq;
420
421         /* ELOG not supported by firmware */
422         if (!opal_check_token(OPAL_DUMP_READ))
423                 return;
424
425         dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
426         if (!dump_kset) {
427                 pr_warn("%s: Failed to create dump kset\n", __func__);
428                 return;
429         }
430
431         rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
432         if (rc) {
433                 pr_warn("%s: Failed to create initiate dump attr group\n",
434                         __func__);
435                 kobject_put(&dump_kset->kobj);
436                 return;
437         }
438
439         dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
440         if (!dump_irq) {
441                 pr_err("%s: Can't register OPAL event irq (%d)\n",
442                        __func__, dump_irq);
443                 return;
444         }
445
446         rc = request_threaded_irq(dump_irq, NULL, process_dump,
447                                 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
448                                 "opal-dump", NULL);
449         if (rc) {
450                 pr_err("%s: Can't request OPAL event irq (%d)\n",
451                        __func__, rc);
452                 return;
453         }
454
455         if (opal_check_token(OPAL_DUMP_RESEND))
456                 opal_dump_resend_notification();
457 }