2 * PowerNV OPAL Dump Interface
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.
12 #include <linux/kobject.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/delay.h>
21 #define DUMP_TYPE_FSP 0x01
25 struct bin_attribute dump_attr;
26 uint32_t id; /* becomes object name */
31 #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
33 struct dump_attribute {
34 struct attribute attr;
35 ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
37 ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
38 const char *buf, size_t count);
40 #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
42 static ssize_t dump_id_show(struct dump_obj *dump_obj,
43 struct dump_attribute *attr,
46 return sprintf(buf, "0x%x\n", dump_obj->id);
49 static const char* dump_type_to_string(uint32_t type)
52 case 0x01: return "SP Dump";
53 case 0x02: return "System/Platform Dump";
54 case 0x03: return "SMA Dump";
55 default: return "unknown";
59 static ssize_t dump_type_show(struct dump_obj *dump_obj,
60 struct dump_attribute *attr,
64 return sprintf(buf, "0x%x %s\n", dump_obj->type,
65 dump_type_to_string(dump_obj->type));
68 static ssize_t dump_ack_show(struct dump_obj *dump_obj,
69 struct dump_attribute *attr,
72 return sprintf(buf, "ack - acknowledge dump\n");
76 * Send acknowledgement to OPAL
78 static int64_t dump_send_ack(uint32_t dump_id)
82 rc = opal_dump_ack(dump_id);
84 pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
85 __func__, dump_id, rc);
89 static ssize_t dump_ack_store(struct dump_obj *dump_obj,
90 struct dump_attribute *attr,
94 dump_send_ack(dump_obj->id);
95 sysfs_remove_file_self(&dump_obj->kobj, &attr->attr);
96 kobject_put(&dump_obj->kobj);
100 /* Attributes of a dump
101 * The binary attribute of the dump itself is dynamic
102 * due to the dynamic size of the dump
104 static struct dump_attribute id_attribute =
105 __ATTR(id, 0666, dump_id_show, NULL);
106 static struct dump_attribute type_attribute =
107 __ATTR(type, 0666, dump_type_show, NULL);
108 static struct dump_attribute ack_attribute =
109 __ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
111 static ssize_t init_dump_show(struct dump_obj *dump_obj,
112 struct dump_attribute *attr,
115 return sprintf(buf, "1 - initiate dump\n");
118 static int64_t dump_fips_init(uint8_t type)
122 rc = opal_dump_init(type);
124 pr_warn("%s: Failed to initiate FipS dump (%d)\n",
129 static ssize_t init_dump_store(struct dump_obj *dump_obj,
130 struct dump_attribute *attr,
134 dump_fips_init(DUMP_TYPE_FSP);
135 pr_info("%s: Initiated FSP dump\n", __func__);
139 static struct dump_attribute initiate_attribute =
140 __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
142 static struct attribute *initiate_attrs[] = {
143 &initiate_attribute.attr,
147 static struct attribute_group initiate_attr_group = {
148 .attrs = initiate_attrs,
151 static struct kset *dump_kset;
153 static ssize_t dump_attr_show(struct kobject *kobj,
154 struct attribute *attr,
157 struct dump_attribute *attribute;
158 struct dump_obj *dump;
160 attribute = to_dump_attr(attr);
161 dump = to_dump_obj(kobj);
163 if (!attribute->show)
166 return attribute->show(dump, attribute, buf);
169 static ssize_t dump_attr_store(struct kobject *kobj,
170 struct attribute *attr,
171 const char *buf, size_t len)
173 struct dump_attribute *attribute;
174 struct dump_obj *dump;
176 attribute = to_dump_attr(attr);
177 dump = to_dump_obj(kobj);
179 if (!attribute->store)
182 return attribute->store(dump, attribute, buf, len);
185 static const struct sysfs_ops dump_sysfs_ops = {
186 .show = dump_attr_show,
187 .store = dump_attr_store,
190 static void dump_release(struct kobject *kobj)
192 struct dump_obj *dump;
194 dump = to_dump_obj(kobj);
199 static struct attribute *dump_default_attrs[] = {
201 &type_attribute.attr,
206 static struct kobj_type dump_ktype = {
207 .sysfs_ops = &dump_sysfs_ops,
208 .release = &dump_release,
209 .default_attrs = dump_default_attrs,
212 static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
214 __be32 id, size, type;
217 type = cpu_to_be32(0xffffffff);
219 rc = opal_dump_info2(&id, &size, &type);
220 if (rc == OPAL_PARAMETER)
221 rc = opal_dump_info(&id, &size);
223 *dump_id = be32_to_cpu(id);
224 *dump_size = be32_to_cpu(size);
225 *dump_type = be32_to_cpu(type);
228 pr_warn("%s: Failed to get dump info (%d)\n",
233 static int64_t dump_read_data(struct dump_obj *dump)
235 struct opal_sg_list *list;
239 /* Allocate memory */
240 dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
242 pr_err("%s : Failed to allocate memory\n", __func__);
247 /* Generate SG list */
248 list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
254 /* First entry address */
258 rc = OPAL_BUSY_EVENT;
259 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
260 rc = opal_dump_read(dump->id, addr);
261 if (rc == OPAL_BUSY_EVENT) {
262 opal_poll_events(NULL);
267 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
268 pr_warn("%s: Extract dump failed for ID 0x%x\n",
272 opal_free_sg_list(list);
278 static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
279 struct bin_attribute *bin_attr,
280 char *buffer, loff_t pos, size_t count)
284 struct dump_obj *dump = to_dump_obj(kobj);
287 rc = dump_read_data(dump);
289 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
295 if (rc == OPAL_PARTIAL) {
296 /* On a partial read, we just return EIO
297 * and rely on userspace to ask us to try
300 pr_info("%s: Platform dump partially read.ID = 0x%x\n",
306 memcpy(buffer, dump->buffer + pos, count);
308 /* You may think we could free the dump buffer now and retrieve
309 * it again later if needed, but due to current firmware limitation,
310 * that's not the case. So, once read into userspace once,
311 * we keep the dump around until it's acknowledged by userspace.
317 static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
320 struct dump_obj *dump;
323 dump = kzalloc(sizeof(*dump), GFP_KERNEL);
327 dump->kobj.kset = dump_kset;
329 kobject_init(&dump->kobj, &dump_ktype);
331 sysfs_bin_attr_init(&dump->dump_attr);
333 dump->dump_attr.attr.name = "dump";
334 dump->dump_attr.attr.mode = 0400;
335 dump->dump_attr.size = size;
336 dump->dump_attr.read = dump_attr_read;
342 rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
344 kobject_put(&dump->kobj);
348 rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
350 kobject_put(&dump->kobj);
354 pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
355 __func__, dump->id, dump->size);
357 kobject_uevent(&dump->kobj, KOBJ_ADD);
362 static int process_dump(void)
365 uint32_t dump_id, dump_size, dump_type;
366 struct dump_obj *dump;
369 rc = dump_read_info(&dump_id, &dump_size, &dump_type);
370 if (rc != OPAL_SUCCESS)
373 sprintf(name, "0x%x-0x%x", dump_type, dump_id);
375 /* we may get notified twice, let's handle
376 * that gracefully and not create two conflicting
379 if (kset_find_obj(dump_kset, name))
382 dump = create_dump_obj(dump_id, dump_size, dump_type);
389 static void dump_work_fn(struct work_struct *work)
394 static DECLARE_WORK(dump_work, dump_work_fn);
396 static void schedule_process_dump(void)
398 schedule_work(&dump_work);
402 * New dump available notification
404 * Once we get notification, we add sysfs entries for it.
405 * We only fetch the dump on demand, and create sysfs asynchronously.
407 static int dump_event(struct notifier_block *nb,
408 unsigned long events, void *change)
410 if (events & OPAL_EVENT_DUMP_AVAIL)
411 schedule_process_dump();
416 static struct notifier_block dump_nb = {
417 .notifier_call = dump_event,
422 void __init opal_platform_dump_init(void)
426 dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
428 pr_warn("%s: Failed to create dump kset\n", __func__);
432 rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
434 pr_warn("%s: Failed to create initiate dump attr group\n",
436 kobject_put(&dump_kset->kobj);
440 rc = opal_notifier_register(&dump_nb);
442 pr_warn("%s: Can't register OPAL event notifier (%d)\n",
447 opal_dump_resend_notification();