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>
18 #include <linux/interrupt.h>
22 #define DUMP_TYPE_FSP 0x01
26 struct bin_attribute dump_attr;
27 uint32_t id; /* becomes object name */
32 #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
34 struct dump_attribute {
35 struct attribute attr;
36 ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
38 ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
39 const char *buf, size_t count);
41 #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
43 static ssize_t dump_id_show(struct dump_obj *dump_obj,
44 struct dump_attribute *attr,
47 return sprintf(buf, "0x%x\n", dump_obj->id);
50 static const char* dump_type_to_string(uint32_t type)
53 case 0x01: return "SP Dump";
54 case 0x02: return "System/Platform Dump";
55 case 0x03: return "SMA Dump";
56 default: return "unknown";
60 static ssize_t dump_type_show(struct dump_obj *dump_obj,
61 struct dump_attribute *attr,
65 return sprintf(buf, "0x%x %s\n", dump_obj->type,
66 dump_type_to_string(dump_obj->type));
69 static ssize_t dump_ack_show(struct dump_obj *dump_obj,
70 struct dump_attribute *attr,
73 return sprintf(buf, "ack - acknowledge dump\n");
77 * Send acknowledgement to OPAL
79 static int64_t dump_send_ack(uint32_t dump_id)
83 rc = opal_dump_ack(dump_id);
85 pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
86 __func__, dump_id, rc);
90 static ssize_t dump_ack_store(struct dump_obj *dump_obj,
91 struct dump_attribute *attr,
95 dump_send_ack(dump_obj->id);
96 sysfs_remove_file_self(&dump_obj->kobj, &attr->attr);
97 kobject_put(&dump_obj->kobj);
101 /* Attributes of a dump
102 * The binary attribute of the dump itself is dynamic
103 * due to the dynamic size of the dump
105 static struct dump_attribute id_attribute =
106 __ATTR(id, S_IRUGO, dump_id_show, NULL);
107 static struct dump_attribute type_attribute =
108 __ATTR(type, S_IRUGO, dump_type_show, NULL);
109 static struct dump_attribute ack_attribute =
110 __ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
112 static ssize_t init_dump_show(struct dump_obj *dump_obj,
113 struct dump_attribute *attr,
116 return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
119 static int64_t dump_fips_init(uint8_t type)
123 rc = opal_dump_init(type);
125 pr_warn("%s: Failed to initiate FSP dump (%d)\n",
130 static ssize_t init_dump_store(struct dump_obj *dump_obj,
131 struct dump_attribute *attr,
137 rc = dump_fips_init(DUMP_TYPE_FSP);
138 if (rc == OPAL_SUCCESS)
139 pr_info("%s: Initiated FSP dump\n", __func__);
144 static struct dump_attribute initiate_attribute =
145 __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
147 static struct attribute *initiate_attrs[] = {
148 &initiate_attribute.attr,
152 static struct attribute_group initiate_attr_group = {
153 .attrs = initiate_attrs,
156 static struct kset *dump_kset;
158 static ssize_t dump_attr_show(struct kobject *kobj,
159 struct attribute *attr,
162 struct dump_attribute *attribute;
163 struct dump_obj *dump;
165 attribute = to_dump_attr(attr);
166 dump = to_dump_obj(kobj);
168 if (!attribute->show)
171 return attribute->show(dump, attribute, buf);
174 static ssize_t dump_attr_store(struct kobject *kobj,
175 struct attribute *attr,
176 const char *buf, size_t len)
178 struct dump_attribute *attribute;
179 struct dump_obj *dump;
181 attribute = to_dump_attr(attr);
182 dump = to_dump_obj(kobj);
184 if (!attribute->store)
187 return attribute->store(dump, attribute, buf, len);
190 static const struct sysfs_ops dump_sysfs_ops = {
191 .show = dump_attr_show,
192 .store = dump_attr_store,
195 static void dump_release(struct kobject *kobj)
197 struct dump_obj *dump;
199 dump = to_dump_obj(kobj);
204 static struct attribute *dump_default_attrs[] = {
206 &type_attribute.attr,
211 static struct kobj_type dump_ktype = {
212 .sysfs_ops = &dump_sysfs_ops,
213 .release = &dump_release,
214 .default_attrs = dump_default_attrs,
217 static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
219 __be32 id, size, type;
222 type = cpu_to_be32(0xffffffff);
224 rc = opal_dump_info2(&id, &size, &type);
225 if (rc == OPAL_PARAMETER)
226 rc = opal_dump_info(&id, &size);
228 *dump_id = be32_to_cpu(id);
229 *dump_size = be32_to_cpu(size);
230 *dump_type = be32_to_cpu(type);
233 pr_warn("%s: Failed to get dump info (%d)\n",
238 static int64_t dump_read_data(struct dump_obj *dump)
240 struct opal_sg_list *list;
244 /* Allocate memory */
245 dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
247 pr_err("%s : Failed to allocate memory\n", __func__);
252 /* Generate SG list */
253 list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
259 /* First entry address */
263 rc = OPAL_BUSY_EVENT;
264 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
265 rc = opal_dump_read(dump->id, addr);
266 if (rc == OPAL_BUSY_EVENT) {
267 opal_poll_events(NULL);
272 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
273 pr_warn("%s: Extract dump failed for ID 0x%x\n",
277 opal_free_sg_list(list);
283 static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
284 struct bin_attribute *bin_attr,
285 char *buffer, loff_t pos, size_t count)
289 struct dump_obj *dump = to_dump_obj(kobj);
292 rc = dump_read_data(dump);
294 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
300 if (rc == OPAL_PARTIAL) {
301 /* On a partial read, we just return EIO
302 * and rely on userspace to ask us to try
305 pr_info("%s: Platform dump partially read. ID = 0x%x\n",
311 memcpy(buffer, dump->buffer + pos, count);
313 /* You may think we could free the dump buffer now and retrieve
314 * it again later if needed, but due to current firmware limitation,
315 * that's not the case. So, once read into userspace once,
316 * we keep the dump around until it's acknowledged by userspace.
322 static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
325 struct dump_obj *dump;
328 dump = kzalloc(sizeof(*dump), GFP_KERNEL);
332 dump->kobj.kset = dump_kset;
334 kobject_init(&dump->kobj, &dump_ktype);
336 sysfs_bin_attr_init(&dump->dump_attr);
338 dump->dump_attr.attr.name = "dump";
339 dump->dump_attr.attr.mode = 0400;
340 dump->dump_attr.size = size;
341 dump->dump_attr.read = dump_attr_read;
347 rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
349 kobject_put(&dump->kobj);
353 rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
355 kobject_put(&dump->kobj);
359 pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
360 __func__, dump->id, dump->size);
362 kobject_uevent(&dump->kobj, KOBJ_ADD);
367 static irqreturn_t process_dump(int irq, void *data)
370 uint32_t dump_id, dump_size, dump_type;
371 struct dump_obj *dump;
373 struct kobject *kobj;
375 rc = dump_read_info(&dump_id, &dump_size, &dump_type);
376 if (rc != OPAL_SUCCESS)
379 sprintf(name, "0x%x-0x%x", dump_type, dump_id);
381 /* we may get notified twice, let's handle
382 * that gracefully and not create two conflicting
385 kobj = kset_find_obj(dump_kset, name);
387 /* Drop reference added by kset_find_obj() */
392 dump = create_dump_obj(dump_id, dump_size, dump_type);
399 void __init opal_platform_dump_init(void)
404 /* ELOG not supported by firmware */
405 if (!opal_check_token(OPAL_DUMP_READ))
408 dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
410 pr_warn("%s: Failed to create dump kset\n", __func__);
414 rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
416 pr_warn("%s: Failed to create initiate dump attr group\n",
418 kobject_put(&dump_kset->kobj);
422 dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
424 pr_err("%s: Can't register OPAL event irq (%d)\n",
429 rc = request_threaded_irq(dump_irq, NULL, process_dump,
430 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
433 pr_err("%s: Can't request OPAL event irq (%d)\n",
438 if (opal_check_token(OPAL_DUMP_RESEND))
439 opal_dump_resend_notification();