1c7e64b9cSStewart Smith /*
2c7e64b9cSStewart Smith  * PowerNV OPAL Dump Interface
3c7e64b9cSStewart Smith  *
4c7e64b9cSStewart Smith  * Copyright 2013,2014 IBM Corp.
5c7e64b9cSStewart Smith  *
6c7e64b9cSStewart Smith  * This program is free software; you can redistribute it and/or
7c7e64b9cSStewart Smith  * modify it under the terms of the GNU General Public License
8c7e64b9cSStewart Smith  * as published by the Free Software Foundation; either version
9c7e64b9cSStewart Smith  * 2 of the License, or (at your option) any later version.
10c7e64b9cSStewart Smith  */
11c7e64b9cSStewart Smith 
12c7e64b9cSStewart Smith #include <linux/kobject.h>
13c7e64b9cSStewart Smith #include <linux/mm.h>
14c7e64b9cSStewart Smith #include <linux/slab.h>
15c7e64b9cSStewart Smith #include <linux/vmalloc.h>
16c7e64b9cSStewart Smith #include <linux/pagemap.h>
17c7e64b9cSStewart Smith #include <linux/delay.h>
18c7e64b9cSStewart Smith 
19c7e64b9cSStewart Smith #include <asm/opal.h>
20c7e64b9cSStewart Smith 
21c7e64b9cSStewart Smith #define DUMP_TYPE_FSP	0x01
22c7e64b9cSStewart Smith 
23c7e64b9cSStewart Smith struct dump_obj {
24c7e64b9cSStewart Smith 	struct kobject  kobj;
25c7e64b9cSStewart Smith 	struct bin_attribute dump_attr;
26c7e64b9cSStewart Smith 	uint32_t	id;  /* becomes object name */
27c7e64b9cSStewart Smith 	uint32_t	type;
28c7e64b9cSStewart Smith 	uint32_t	size;
29c7e64b9cSStewart Smith 	char		*buffer;
30c7e64b9cSStewart Smith };
31c7e64b9cSStewart Smith #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
32c7e64b9cSStewart Smith 
33c7e64b9cSStewart Smith struct dump_attribute {
34c7e64b9cSStewart Smith 	struct attribute attr;
35c7e64b9cSStewart Smith 	ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
36c7e64b9cSStewart Smith 			char *buf);
37c7e64b9cSStewart Smith 	ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
38c7e64b9cSStewart Smith 			 const char *buf, size_t count);
39c7e64b9cSStewart Smith };
40c7e64b9cSStewart Smith #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
41c7e64b9cSStewart Smith 
42c7e64b9cSStewart Smith static ssize_t dump_id_show(struct dump_obj *dump_obj,
43c7e64b9cSStewart Smith 			    struct dump_attribute *attr,
44c7e64b9cSStewart Smith 			    char *buf)
45c7e64b9cSStewart Smith {
46c7e64b9cSStewart Smith 	return sprintf(buf, "0x%x\n", dump_obj->id);
47c7e64b9cSStewart Smith }
48c7e64b9cSStewart Smith 
49c7e64b9cSStewart Smith static const char* dump_type_to_string(uint32_t type)
50c7e64b9cSStewart Smith {
51c7e64b9cSStewart Smith 	switch (type) {
52c7e64b9cSStewart Smith 	case 0x01: return "SP Dump";
53c7e64b9cSStewart Smith 	case 0x02: return "System/Platform Dump";
54c7e64b9cSStewart Smith 	case 0x03: return "SMA Dump";
55c7e64b9cSStewart Smith 	default: return "unknown";
56c7e64b9cSStewart Smith 	}
57c7e64b9cSStewart Smith }
58c7e64b9cSStewart Smith 
59c7e64b9cSStewart Smith static ssize_t dump_type_show(struct dump_obj *dump_obj,
60c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
61c7e64b9cSStewart Smith 			      char *buf)
62c7e64b9cSStewart Smith {
63c7e64b9cSStewart Smith 
64c7e64b9cSStewart Smith 	return sprintf(buf, "0x%x %s\n", dump_obj->type,
65c7e64b9cSStewart Smith 		       dump_type_to_string(dump_obj->type));
66c7e64b9cSStewart Smith }
67c7e64b9cSStewart Smith 
68c7e64b9cSStewart Smith static ssize_t dump_ack_show(struct dump_obj *dump_obj,
69c7e64b9cSStewart Smith 			     struct dump_attribute *attr,
70c7e64b9cSStewart Smith 			     char *buf)
71c7e64b9cSStewart Smith {
72c7e64b9cSStewart Smith 	return sprintf(buf, "ack - acknowledge dump\n");
73c7e64b9cSStewart Smith }
74c7e64b9cSStewart Smith 
75c7e64b9cSStewart Smith /*
76c7e64b9cSStewart Smith  * Send acknowledgement to OPAL
77c7e64b9cSStewart Smith  */
78c7e64b9cSStewart Smith static int64_t dump_send_ack(uint32_t dump_id)
79c7e64b9cSStewart Smith {
80c7e64b9cSStewart Smith 	int rc;
81c7e64b9cSStewart Smith 
82c7e64b9cSStewart Smith 	rc = opal_dump_ack(dump_id);
83c7e64b9cSStewart Smith 	if (rc)
84c7e64b9cSStewart Smith 		pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
85c7e64b9cSStewart Smith 			__func__, dump_id, rc);
86c7e64b9cSStewart Smith 	return rc;
87c7e64b9cSStewart Smith }
88c7e64b9cSStewart Smith 
89c7e64b9cSStewart Smith static ssize_t dump_ack_store(struct dump_obj *dump_obj,
90c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
91c7e64b9cSStewart Smith 			      const char *buf,
92c7e64b9cSStewart Smith 			      size_t count)
93c7e64b9cSStewart Smith {
94c7e64b9cSStewart Smith 	dump_send_ack(dump_obj->id);
95cc4f265aSStewart Smith 	sysfs_remove_file_self(&dump_obj->kobj, &attr->attr);
96cc4f265aSStewart Smith 	kobject_put(&dump_obj->kobj);
97c7e64b9cSStewart Smith 	return count;
98c7e64b9cSStewart Smith }
99c7e64b9cSStewart Smith 
100c7e64b9cSStewart Smith /* Attributes of a dump
101c7e64b9cSStewart Smith  * The binary attribute of the dump itself is dynamic
102c7e64b9cSStewart Smith  * due to the dynamic size of the dump
103c7e64b9cSStewart Smith  */
104c7e64b9cSStewart Smith static struct dump_attribute id_attribute =
105c7e64b9cSStewart Smith 	__ATTR(id, 0666, dump_id_show, NULL);
106c7e64b9cSStewart Smith static struct dump_attribute type_attribute =
107c7e64b9cSStewart Smith 	__ATTR(type, 0666, dump_type_show, NULL);
108c7e64b9cSStewart Smith static struct dump_attribute ack_attribute =
109c7e64b9cSStewart Smith 	__ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
110c7e64b9cSStewart Smith 
111c7e64b9cSStewart Smith static ssize_t init_dump_show(struct dump_obj *dump_obj,
112c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
113c7e64b9cSStewart Smith 			      char *buf)
114c7e64b9cSStewart Smith {
115c7e64b9cSStewart Smith 	return sprintf(buf, "1 - initiate dump\n");
116c7e64b9cSStewart Smith }
117c7e64b9cSStewart Smith 
118c7e64b9cSStewart Smith static int64_t dump_fips_init(uint8_t type)
119c7e64b9cSStewart Smith {
120c7e64b9cSStewart Smith 	int rc;
121c7e64b9cSStewart Smith 
122c7e64b9cSStewart Smith 	rc = opal_dump_init(type);
123c7e64b9cSStewart Smith 	if (rc)
124c7e64b9cSStewart Smith 		pr_warn("%s: Failed to initiate FipS dump (%d)\n",
125c7e64b9cSStewart Smith 			__func__, rc);
126c7e64b9cSStewart Smith 	return rc;
127c7e64b9cSStewart Smith }
128c7e64b9cSStewart Smith 
129c7e64b9cSStewart Smith static ssize_t init_dump_store(struct dump_obj *dump_obj,
130c7e64b9cSStewart Smith 			       struct dump_attribute *attr,
131c7e64b9cSStewart Smith 			       const char *buf,
132c7e64b9cSStewart Smith 			       size_t count)
133c7e64b9cSStewart Smith {
134c7e64b9cSStewart Smith 	dump_fips_init(DUMP_TYPE_FSP);
135c7e64b9cSStewart Smith 	pr_info("%s: Initiated FSP dump\n", __func__);
136c7e64b9cSStewart Smith 	return count;
137c7e64b9cSStewart Smith }
138c7e64b9cSStewart Smith 
139c7e64b9cSStewart Smith static struct dump_attribute initiate_attribute =
140c7e64b9cSStewart Smith 	__ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
141c7e64b9cSStewart Smith 
142c7e64b9cSStewart Smith static struct attribute *initiate_attrs[] = {
143c7e64b9cSStewart Smith 	&initiate_attribute.attr,
144c7e64b9cSStewart Smith 	NULL,
145c7e64b9cSStewart Smith };
146c7e64b9cSStewart Smith 
147c7e64b9cSStewart Smith static struct attribute_group initiate_attr_group = {
148c7e64b9cSStewart Smith 	.attrs = initiate_attrs,
149c7e64b9cSStewart Smith };
150c7e64b9cSStewart Smith 
151c7e64b9cSStewart Smith static struct kset *dump_kset;
152c7e64b9cSStewart Smith 
153c7e64b9cSStewart Smith static ssize_t dump_attr_show(struct kobject *kobj,
154c7e64b9cSStewart Smith 			      struct attribute *attr,
155c7e64b9cSStewart Smith 			      char *buf)
156c7e64b9cSStewart Smith {
157c7e64b9cSStewart Smith 	struct dump_attribute *attribute;
158c7e64b9cSStewart Smith 	struct dump_obj *dump;
159c7e64b9cSStewart Smith 
160c7e64b9cSStewart Smith 	attribute = to_dump_attr(attr);
161c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
162c7e64b9cSStewart Smith 
163c7e64b9cSStewart Smith 	if (!attribute->show)
164c7e64b9cSStewart Smith 		return -EIO;
165c7e64b9cSStewart Smith 
166c7e64b9cSStewart Smith 	return attribute->show(dump, attribute, buf);
167c7e64b9cSStewart Smith }
168c7e64b9cSStewart Smith 
169c7e64b9cSStewart Smith static ssize_t dump_attr_store(struct kobject *kobj,
170c7e64b9cSStewart Smith 			       struct attribute *attr,
171c7e64b9cSStewart Smith 			       const char *buf, size_t len)
172c7e64b9cSStewart Smith {
173c7e64b9cSStewart Smith 	struct dump_attribute *attribute;
174c7e64b9cSStewart Smith 	struct dump_obj *dump;
175c7e64b9cSStewart Smith 
176c7e64b9cSStewart Smith 	attribute = to_dump_attr(attr);
177c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
178c7e64b9cSStewart Smith 
179c7e64b9cSStewart Smith 	if (!attribute->store)
180c7e64b9cSStewart Smith 		return -EIO;
181c7e64b9cSStewart Smith 
182c7e64b9cSStewart Smith 	return attribute->store(dump, attribute, buf, len);
183c7e64b9cSStewart Smith }
184c7e64b9cSStewart Smith 
185c7e64b9cSStewart Smith static const struct sysfs_ops dump_sysfs_ops = {
186c7e64b9cSStewart Smith 	.show = dump_attr_show,
187c7e64b9cSStewart Smith 	.store = dump_attr_store,
188c7e64b9cSStewart Smith };
189c7e64b9cSStewart Smith 
190c7e64b9cSStewart Smith static void dump_release(struct kobject *kobj)
191c7e64b9cSStewart Smith {
192c7e64b9cSStewart Smith 	struct dump_obj *dump;
193c7e64b9cSStewart Smith 
194c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
195c7e64b9cSStewart Smith 	vfree(dump->buffer);
196c7e64b9cSStewart Smith 	kfree(dump);
197c7e64b9cSStewart Smith }
198c7e64b9cSStewart Smith 
199c7e64b9cSStewart Smith static struct attribute *dump_default_attrs[] = {
200c7e64b9cSStewart Smith 	&id_attribute.attr,
201c7e64b9cSStewart Smith 	&type_attribute.attr,
202c7e64b9cSStewart Smith 	&ack_attribute.attr,
203c7e64b9cSStewart Smith 	NULL,
204c7e64b9cSStewart Smith };
205c7e64b9cSStewart Smith 
206c7e64b9cSStewart Smith static struct kobj_type dump_ktype = {
207c7e64b9cSStewart Smith 	.sysfs_ops = &dump_sysfs_ops,
208c7e64b9cSStewart Smith 	.release = &dump_release,
209c7e64b9cSStewart Smith 	.default_attrs = dump_default_attrs,
210c7e64b9cSStewart Smith };
211c7e64b9cSStewart Smith 
2122d6b63bbSAnton Blanchard static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
213c7e64b9cSStewart Smith {
2142d6b63bbSAnton Blanchard 	__be32 id, size, type;
215c7e64b9cSStewart Smith 	int rc;
216c7e64b9cSStewart Smith 
2172d6b63bbSAnton Blanchard 	type = cpu_to_be32(0xffffffff);
218c7e64b9cSStewart Smith 
2192d6b63bbSAnton Blanchard 	rc = opal_dump_info2(&id, &size, &type);
220c7e64b9cSStewart Smith 	if (rc == OPAL_PARAMETER)
2212d6b63bbSAnton Blanchard 		rc = opal_dump_info(&id, &size);
2222d6b63bbSAnton Blanchard 
2232d6b63bbSAnton Blanchard 	*dump_id = be32_to_cpu(id);
2242d6b63bbSAnton Blanchard 	*dump_size = be32_to_cpu(size);
2252d6b63bbSAnton Blanchard 	*dump_type = be32_to_cpu(type);
226c7e64b9cSStewart Smith 
227c7e64b9cSStewart Smith 	if (rc)
228c7e64b9cSStewart Smith 		pr_warn("%s: Failed to get dump info (%d)\n",
229c7e64b9cSStewart Smith 			__func__, rc);
230c7e64b9cSStewart Smith 	return rc;
231c7e64b9cSStewart Smith }
232c7e64b9cSStewart Smith 
233c7e64b9cSStewart Smith static int64_t dump_read_data(struct dump_obj *dump)
234c7e64b9cSStewart Smith {
235c7e64b9cSStewart Smith 	struct opal_sg_list *list;
236c7e64b9cSStewart Smith 	uint64_t addr;
237c7e64b9cSStewart Smith 	int64_t rc;
238c7e64b9cSStewart Smith 
239c7e64b9cSStewart Smith 	/* Allocate memory */
240c7e64b9cSStewart Smith 	dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
241c7e64b9cSStewart Smith 	if (!dump->buffer) {
242c7e64b9cSStewart Smith 		pr_err("%s : Failed to allocate memory\n", __func__);
243c7e64b9cSStewart Smith 		rc = -ENOMEM;
244c7e64b9cSStewart Smith 		goto out;
245c7e64b9cSStewart Smith 	}
246c7e64b9cSStewart Smith 
247c7e64b9cSStewart Smith 	/* Generate SG list */
2483441f04bSAnton Blanchard 	list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
249c7e64b9cSStewart Smith 	if (!list) {
250c7e64b9cSStewart Smith 		rc = -ENOMEM;
251c7e64b9cSStewart Smith 		goto out;
252c7e64b9cSStewart Smith 	}
253c7e64b9cSStewart Smith 
254c7e64b9cSStewart Smith 	/* First entry address */
255c7e64b9cSStewart Smith 	addr = __pa(list);
256c7e64b9cSStewart Smith 
257c7e64b9cSStewart Smith 	/* Fetch data */
258c7e64b9cSStewart Smith 	rc = OPAL_BUSY_EVENT;
259c7e64b9cSStewart Smith 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
260c7e64b9cSStewart Smith 		rc = opal_dump_read(dump->id, addr);
261c7e64b9cSStewart Smith 		if (rc == OPAL_BUSY_EVENT) {
262c7e64b9cSStewart Smith 			opal_poll_events(NULL);
263c7e64b9cSStewart Smith 			msleep(20);
264c7e64b9cSStewart Smith 		}
265c7e64b9cSStewart Smith 	}
266c7e64b9cSStewart Smith 
267c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
268c7e64b9cSStewart Smith 		pr_warn("%s: Extract dump failed for ID 0x%x\n",
269c7e64b9cSStewart Smith 			__func__, dump->id);
270c7e64b9cSStewart Smith 
271c7e64b9cSStewart Smith 	/* Free SG list */
2723441f04bSAnton Blanchard 	opal_free_sg_list(list);
273c7e64b9cSStewart Smith 
274c7e64b9cSStewart Smith out:
275c7e64b9cSStewart Smith 	return rc;
276c7e64b9cSStewart Smith }
277c7e64b9cSStewart Smith 
278c7e64b9cSStewart Smith static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
279c7e64b9cSStewart Smith 			      struct bin_attribute *bin_attr,
280c7e64b9cSStewart Smith 			      char *buffer, loff_t pos, size_t count)
281c7e64b9cSStewart Smith {
282c7e64b9cSStewart Smith 	ssize_t rc;
283c7e64b9cSStewart Smith 
284c7e64b9cSStewart Smith 	struct dump_obj *dump = to_dump_obj(kobj);
285c7e64b9cSStewart Smith 
286c7e64b9cSStewart Smith 	if (!dump->buffer) {
287c7e64b9cSStewart Smith 		rc = dump_read_data(dump);
288c7e64b9cSStewart Smith 
289c7e64b9cSStewart Smith 		if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
290c7e64b9cSStewart Smith 			vfree(dump->buffer);
291c7e64b9cSStewart Smith 			dump->buffer = NULL;
292c7e64b9cSStewart Smith 
293c7e64b9cSStewart Smith 			return -EIO;
294c7e64b9cSStewart Smith 		}
295c7e64b9cSStewart Smith 		if (rc == OPAL_PARTIAL) {
296c7e64b9cSStewart Smith 			/* On a partial read, we just return EIO
297c7e64b9cSStewart Smith 			 * and rely on userspace to ask us to try
298c7e64b9cSStewart Smith 			 * again.
299c7e64b9cSStewart Smith 			 */
300c7e64b9cSStewart Smith 			pr_info("%s: Platform dump partially read.ID = 0x%x\n",
301c7e64b9cSStewart Smith 				__func__, dump->id);
302c7e64b9cSStewart Smith 			return -EIO;
303c7e64b9cSStewart Smith 		}
304c7e64b9cSStewart Smith 	}
305c7e64b9cSStewart Smith 
306c7e64b9cSStewart Smith 	memcpy(buffer, dump->buffer + pos, count);
307c7e64b9cSStewart Smith 
308c7e64b9cSStewart Smith 	/* You may think we could free the dump buffer now and retrieve
309c7e64b9cSStewart Smith 	 * it again later if needed, but due to current firmware limitation,
310c7e64b9cSStewart Smith 	 * that's not the case. So, once read into userspace once,
311c7e64b9cSStewart Smith 	 * we keep the dump around until it's acknowledged by userspace.
312c7e64b9cSStewart Smith 	 */
313c7e64b9cSStewart Smith 
314c7e64b9cSStewart Smith 	return count;
315c7e64b9cSStewart Smith }
316c7e64b9cSStewart Smith 
317c7e64b9cSStewart Smith static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
318c7e64b9cSStewart Smith 					uint32_t type)
319c7e64b9cSStewart Smith {
320c7e64b9cSStewart Smith 	struct dump_obj *dump;
321c7e64b9cSStewart Smith 	int rc;
322c7e64b9cSStewart Smith 
323c7e64b9cSStewart Smith 	dump = kzalloc(sizeof(*dump), GFP_KERNEL);
324c7e64b9cSStewart Smith 	if (!dump)
325c7e64b9cSStewart Smith 		return NULL;
326c7e64b9cSStewart Smith 
327c7e64b9cSStewart Smith 	dump->kobj.kset = dump_kset;
328c7e64b9cSStewart Smith 
329c7e64b9cSStewart Smith 	kobject_init(&dump->kobj, &dump_ktype);
330c7e64b9cSStewart Smith 
331c7e64b9cSStewart Smith 	sysfs_bin_attr_init(&dump->dump_attr);
332c7e64b9cSStewart Smith 
333c7e64b9cSStewart Smith 	dump->dump_attr.attr.name = "dump";
334c7e64b9cSStewart Smith 	dump->dump_attr.attr.mode = 0400;
335c7e64b9cSStewart Smith 	dump->dump_attr.size = size;
336c7e64b9cSStewart Smith 	dump->dump_attr.read = dump_attr_read;
337c7e64b9cSStewart Smith 
338c7e64b9cSStewart Smith 	dump->id = id;
339c7e64b9cSStewart Smith 	dump->size = size;
340c7e64b9cSStewart Smith 	dump->type = type;
341c7e64b9cSStewart Smith 
342c7e64b9cSStewart Smith 	rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
343c7e64b9cSStewart Smith 	if (rc) {
344c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
345c7e64b9cSStewart Smith 		return NULL;
346c7e64b9cSStewart Smith 	}
347c7e64b9cSStewart Smith 
348c7e64b9cSStewart Smith 	rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
349c7e64b9cSStewart Smith 	if (rc) {
350c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
351c7e64b9cSStewart Smith 		return NULL;
352c7e64b9cSStewart Smith 	}
353c7e64b9cSStewart Smith 
354c7e64b9cSStewart Smith 	pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
355c7e64b9cSStewart Smith 		__func__, dump->id, dump->size);
356c7e64b9cSStewart Smith 
357c7e64b9cSStewart Smith 	kobject_uevent(&dump->kobj, KOBJ_ADD);
358c7e64b9cSStewart Smith 
359c7e64b9cSStewart Smith 	return dump;
360c7e64b9cSStewart Smith }
361c7e64b9cSStewart Smith 
362c7e64b9cSStewart Smith static int process_dump(void)
363c7e64b9cSStewart Smith {
364c7e64b9cSStewart Smith 	int rc;
365c7e64b9cSStewart Smith 	uint32_t dump_id, dump_size, dump_type;
366c7e64b9cSStewart Smith 	struct dump_obj *dump;
367c7e64b9cSStewart Smith 	char name[22];
368c7e64b9cSStewart Smith 
369c7e64b9cSStewart Smith 	rc = dump_read_info(&dump_id, &dump_size, &dump_type);
370c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS)
371c7e64b9cSStewart Smith 		return rc;
372c7e64b9cSStewart Smith 
373c7e64b9cSStewart Smith 	sprintf(name, "0x%x-0x%x", dump_type, dump_id);
374c7e64b9cSStewart Smith 
375c7e64b9cSStewart Smith 	/* we may get notified twice, let's handle
376c7e64b9cSStewart Smith 	 * that gracefully and not create two conflicting
377c7e64b9cSStewart Smith 	 * entries.
378c7e64b9cSStewart Smith 	 */
379c7e64b9cSStewart Smith 	if (kset_find_obj(dump_kset, name))
380c7e64b9cSStewart Smith 		return 0;
381c7e64b9cSStewart Smith 
382c7e64b9cSStewart Smith 	dump = create_dump_obj(dump_id, dump_size, dump_type);
383c7e64b9cSStewart Smith 	if (!dump)
384c7e64b9cSStewart Smith 		return -1;
385c7e64b9cSStewart Smith 
386c7e64b9cSStewart Smith 	return 0;
387c7e64b9cSStewart Smith }
388c7e64b9cSStewart Smith 
389c7e64b9cSStewart Smith static void dump_work_fn(struct work_struct *work)
390c7e64b9cSStewart Smith {
391c7e64b9cSStewart Smith 	process_dump();
392c7e64b9cSStewart Smith }
393c7e64b9cSStewart Smith 
394c7e64b9cSStewart Smith static DECLARE_WORK(dump_work, dump_work_fn);
395c7e64b9cSStewart Smith 
396c7e64b9cSStewart Smith static void schedule_process_dump(void)
397c7e64b9cSStewart Smith {
398c7e64b9cSStewart Smith 	schedule_work(&dump_work);
399c7e64b9cSStewart Smith }
400c7e64b9cSStewart Smith 
401c7e64b9cSStewart Smith /*
402c7e64b9cSStewart Smith  * New dump available notification
403c7e64b9cSStewart Smith  *
404c7e64b9cSStewart Smith  * Once we get notification, we add sysfs entries for it.
405c7e64b9cSStewart Smith  * We only fetch the dump on demand, and create sysfs asynchronously.
406c7e64b9cSStewart Smith  */
407c7e64b9cSStewart Smith static int dump_event(struct notifier_block *nb,
408c7e64b9cSStewart Smith 		      unsigned long events, void *change)
409c7e64b9cSStewart Smith {
410c7e64b9cSStewart Smith 	if (events & OPAL_EVENT_DUMP_AVAIL)
411c7e64b9cSStewart Smith 		schedule_process_dump();
412c7e64b9cSStewart Smith 
413c7e64b9cSStewart Smith 	return 0;
414c7e64b9cSStewart Smith }
415c7e64b9cSStewart Smith 
416c7e64b9cSStewart Smith static struct notifier_block dump_nb = {
417c7e64b9cSStewart Smith 	.notifier_call  = dump_event,
418c7e64b9cSStewart Smith 	.next           = NULL,
419c7e64b9cSStewart Smith 	.priority       = 0
420c7e64b9cSStewart Smith };
421c7e64b9cSStewart Smith 
422c7e64b9cSStewart Smith void __init opal_platform_dump_init(void)
423c7e64b9cSStewart Smith {
424c7e64b9cSStewart Smith 	int rc;
425c7e64b9cSStewart Smith 
426c7e64b9cSStewart Smith 	dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
427c7e64b9cSStewart Smith 	if (!dump_kset) {
428c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create dump kset\n", __func__);
429c7e64b9cSStewart Smith 		return;
430c7e64b9cSStewart Smith 	}
431c7e64b9cSStewart Smith 
432c7e64b9cSStewart Smith 	rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
433c7e64b9cSStewart Smith 	if (rc) {
434c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create initiate dump attr group\n",
435c7e64b9cSStewart Smith 			__func__);
436c7e64b9cSStewart Smith 		kobject_put(&dump_kset->kobj);
437c7e64b9cSStewart Smith 		return;
438c7e64b9cSStewart Smith 	}
439c7e64b9cSStewart Smith 
440c7e64b9cSStewart Smith 	rc = opal_notifier_register(&dump_nb);
441c7e64b9cSStewart Smith 	if (rc) {
442c7e64b9cSStewart Smith 		pr_warn("%s: Can't register OPAL event notifier (%d)\n",
443c7e64b9cSStewart Smith 			__func__, rc);
444c7e64b9cSStewart Smith 		return;
445c7e64b9cSStewart Smith 	}
446c7e64b9cSStewart Smith 
447c7e64b9cSStewart Smith 	opal_dump_resend_notification();
448c7e64b9cSStewart Smith }
449