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 
212c7e64b9cSStewart Smith static int64_t dump_read_info(uint32_t *id, uint32_t *size, uint32_t *type)
213c7e64b9cSStewart Smith {
214c7e64b9cSStewart Smith 	int rc;
215c7e64b9cSStewart Smith 	*type = 0xffffffff;
216c7e64b9cSStewart Smith 
217c7e64b9cSStewart Smith 	rc = opal_dump_info2(id, size, type);
218c7e64b9cSStewart Smith 
219c7e64b9cSStewart Smith 	if (rc == OPAL_PARAMETER)
220c7e64b9cSStewart Smith 		rc = opal_dump_info(id, size);
221c7e64b9cSStewart Smith 
222c7e64b9cSStewart Smith 	if (rc)
223c7e64b9cSStewart Smith 		pr_warn("%s: Failed to get dump info (%d)\n",
224c7e64b9cSStewart Smith 			__func__, rc);
225c7e64b9cSStewart Smith 	return rc;
226c7e64b9cSStewart Smith }
227c7e64b9cSStewart Smith 
228c7e64b9cSStewart Smith static int64_t dump_read_data(struct dump_obj *dump)
229c7e64b9cSStewart Smith {
230c7e64b9cSStewart Smith 	struct opal_sg_list *list;
231c7e64b9cSStewart Smith 	uint64_t addr;
232c7e64b9cSStewart Smith 	int64_t rc;
233c7e64b9cSStewart Smith 
234c7e64b9cSStewart Smith 	/* Allocate memory */
235c7e64b9cSStewart Smith 	dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
236c7e64b9cSStewart Smith 	if (!dump->buffer) {
237c7e64b9cSStewart Smith 		pr_err("%s : Failed to allocate memory\n", __func__);
238c7e64b9cSStewart Smith 		rc = -ENOMEM;
239c7e64b9cSStewart Smith 		goto out;
240c7e64b9cSStewart Smith 	}
241c7e64b9cSStewart Smith 
242c7e64b9cSStewart Smith 	/* Generate SG list */
2433441f04bSAnton Blanchard 	list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
244c7e64b9cSStewart Smith 	if (!list) {
245c7e64b9cSStewart Smith 		rc = -ENOMEM;
246c7e64b9cSStewart Smith 		goto out;
247c7e64b9cSStewart Smith 	}
248c7e64b9cSStewart Smith 
249c7e64b9cSStewart Smith 	/* First entry address */
250c7e64b9cSStewart Smith 	addr = __pa(list);
251c7e64b9cSStewart Smith 
252c7e64b9cSStewart Smith 	/* Fetch data */
253c7e64b9cSStewart Smith 	rc = OPAL_BUSY_EVENT;
254c7e64b9cSStewart Smith 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
255c7e64b9cSStewart Smith 		rc = opal_dump_read(dump->id, addr);
256c7e64b9cSStewart Smith 		if (rc == OPAL_BUSY_EVENT) {
257c7e64b9cSStewart Smith 			opal_poll_events(NULL);
258c7e64b9cSStewart Smith 			msleep(20);
259c7e64b9cSStewart Smith 		}
260c7e64b9cSStewart Smith 	}
261c7e64b9cSStewart Smith 
262c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
263c7e64b9cSStewart Smith 		pr_warn("%s: Extract dump failed for ID 0x%x\n",
264c7e64b9cSStewart Smith 			__func__, dump->id);
265c7e64b9cSStewart Smith 
266c7e64b9cSStewart Smith 	/* Free SG list */
2673441f04bSAnton Blanchard 	opal_free_sg_list(list);
268c7e64b9cSStewart Smith 
269c7e64b9cSStewart Smith out:
270c7e64b9cSStewart Smith 	return rc;
271c7e64b9cSStewart Smith }
272c7e64b9cSStewart Smith 
273c7e64b9cSStewart Smith static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
274c7e64b9cSStewart Smith 			      struct bin_attribute *bin_attr,
275c7e64b9cSStewart Smith 			      char *buffer, loff_t pos, size_t count)
276c7e64b9cSStewart Smith {
277c7e64b9cSStewart Smith 	ssize_t rc;
278c7e64b9cSStewart Smith 
279c7e64b9cSStewart Smith 	struct dump_obj *dump = to_dump_obj(kobj);
280c7e64b9cSStewart Smith 
281c7e64b9cSStewart Smith 	if (!dump->buffer) {
282c7e64b9cSStewart Smith 		rc = dump_read_data(dump);
283c7e64b9cSStewart Smith 
284c7e64b9cSStewart Smith 		if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
285c7e64b9cSStewart Smith 			vfree(dump->buffer);
286c7e64b9cSStewart Smith 			dump->buffer = NULL;
287c7e64b9cSStewart Smith 
288c7e64b9cSStewart Smith 			return -EIO;
289c7e64b9cSStewart Smith 		}
290c7e64b9cSStewart Smith 		if (rc == OPAL_PARTIAL) {
291c7e64b9cSStewart Smith 			/* On a partial read, we just return EIO
292c7e64b9cSStewart Smith 			 * and rely on userspace to ask us to try
293c7e64b9cSStewart Smith 			 * again.
294c7e64b9cSStewart Smith 			 */
295c7e64b9cSStewart Smith 			pr_info("%s: Platform dump partially read.ID = 0x%x\n",
296c7e64b9cSStewart Smith 				__func__, dump->id);
297c7e64b9cSStewart Smith 			return -EIO;
298c7e64b9cSStewart Smith 		}
299c7e64b9cSStewart Smith 	}
300c7e64b9cSStewart Smith 
301c7e64b9cSStewart Smith 	memcpy(buffer, dump->buffer + pos, count);
302c7e64b9cSStewart Smith 
303c7e64b9cSStewart Smith 	/* You may think we could free the dump buffer now and retrieve
304c7e64b9cSStewart Smith 	 * it again later if needed, but due to current firmware limitation,
305c7e64b9cSStewart Smith 	 * that's not the case. So, once read into userspace once,
306c7e64b9cSStewart Smith 	 * we keep the dump around until it's acknowledged by userspace.
307c7e64b9cSStewart Smith 	 */
308c7e64b9cSStewart Smith 
309c7e64b9cSStewart Smith 	return count;
310c7e64b9cSStewart Smith }
311c7e64b9cSStewart Smith 
312c7e64b9cSStewart Smith static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
313c7e64b9cSStewart Smith 					uint32_t type)
314c7e64b9cSStewart Smith {
315c7e64b9cSStewart Smith 	struct dump_obj *dump;
316c7e64b9cSStewart Smith 	int rc;
317c7e64b9cSStewart Smith 
318c7e64b9cSStewart Smith 	dump = kzalloc(sizeof(*dump), GFP_KERNEL);
319c7e64b9cSStewart Smith 	if (!dump)
320c7e64b9cSStewart Smith 		return NULL;
321c7e64b9cSStewart Smith 
322c7e64b9cSStewart Smith 	dump->kobj.kset = dump_kset;
323c7e64b9cSStewart Smith 
324c7e64b9cSStewart Smith 	kobject_init(&dump->kobj, &dump_ktype);
325c7e64b9cSStewart Smith 
326c7e64b9cSStewart Smith 	sysfs_bin_attr_init(&dump->dump_attr);
327c7e64b9cSStewart Smith 
328c7e64b9cSStewart Smith 	dump->dump_attr.attr.name = "dump";
329c7e64b9cSStewart Smith 	dump->dump_attr.attr.mode = 0400;
330c7e64b9cSStewart Smith 	dump->dump_attr.size = size;
331c7e64b9cSStewart Smith 	dump->dump_attr.read = dump_attr_read;
332c7e64b9cSStewart Smith 
333c7e64b9cSStewart Smith 	dump->id = id;
334c7e64b9cSStewart Smith 	dump->size = size;
335c7e64b9cSStewart Smith 	dump->type = type;
336c7e64b9cSStewart Smith 
337c7e64b9cSStewart Smith 	rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
338c7e64b9cSStewart Smith 	if (rc) {
339c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
340c7e64b9cSStewart Smith 		return NULL;
341c7e64b9cSStewart Smith 	}
342c7e64b9cSStewart Smith 
343c7e64b9cSStewart Smith 	rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
344c7e64b9cSStewart Smith 	if (rc) {
345c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
346c7e64b9cSStewart Smith 		return NULL;
347c7e64b9cSStewart Smith 	}
348c7e64b9cSStewart Smith 
349c7e64b9cSStewart Smith 	pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
350c7e64b9cSStewart Smith 		__func__, dump->id, dump->size);
351c7e64b9cSStewart Smith 
352c7e64b9cSStewart Smith 	kobject_uevent(&dump->kobj, KOBJ_ADD);
353c7e64b9cSStewart Smith 
354c7e64b9cSStewart Smith 	return dump;
355c7e64b9cSStewart Smith }
356c7e64b9cSStewart Smith 
357c7e64b9cSStewart Smith static int process_dump(void)
358c7e64b9cSStewart Smith {
359c7e64b9cSStewart Smith 	int rc;
360c7e64b9cSStewart Smith 	uint32_t dump_id, dump_size, dump_type;
361c7e64b9cSStewart Smith 	struct dump_obj *dump;
362c7e64b9cSStewart Smith 	char name[22];
363c7e64b9cSStewart Smith 
364c7e64b9cSStewart Smith 	rc = dump_read_info(&dump_id, &dump_size, &dump_type);
365c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS)
366c7e64b9cSStewart Smith 		return rc;
367c7e64b9cSStewart Smith 
368c7e64b9cSStewart Smith 	sprintf(name, "0x%x-0x%x", dump_type, dump_id);
369c7e64b9cSStewart Smith 
370c7e64b9cSStewart Smith 	/* we may get notified twice, let's handle
371c7e64b9cSStewart Smith 	 * that gracefully and not create two conflicting
372c7e64b9cSStewart Smith 	 * entries.
373c7e64b9cSStewart Smith 	 */
374c7e64b9cSStewart Smith 	if (kset_find_obj(dump_kset, name))
375c7e64b9cSStewart Smith 		return 0;
376c7e64b9cSStewart Smith 
377c7e64b9cSStewart Smith 	dump = create_dump_obj(dump_id, dump_size, dump_type);
378c7e64b9cSStewart Smith 	if (!dump)
379c7e64b9cSStewart Smith 		return -1;
380c7e64b9cSStewart Smith 
381c7e64b9cSStewart Smith 	return 0;
382c7e64b9cSStewart Smith }
383c7e64b9cSStewart Smith 
384c7e64b9cSStewart Smith static void dump_work_fn(struct work_struct *work)
385c7e64b9cSStewart Smith {
386c7e64b9cSStewart Smith 	process_dump();
387c7e64b9cSStewart Smith }
388c7e64b9cSStewart Smith 
389c7e64b9cSStewart Smith static DECLARE_WORK(dump_work, dump_work_fn);
390c7e64b9cSStewart Smith 
391c7e64b9cSStewart Smith static void schedule_process_dump(void)
392c7e64b9cSStewart Smith {
393c7e64b9cSStewart Smith 	schedule_work(&dump_work);
394c7e64b9cSStewart Smith }
395c7e64b9cSStewart Smith 
396c7e64b9cSStewart Smith /*
397c7e64b9cSStewart Smith  * New dump available notification
398c7e64b9cSStewart Smith  *
399c7e64b9cSStewart Smith  * Once we get notification, we add sysfs entries for it.
400c7e64b9cSStewart Smith  * We only fetch the dump on demand, and create sysfs asynchronously.
401c7e64b9cSStewart Smith  */
402c7e64b9cSStewart Smith static int dump_event(struct notifier_block *nb,
403c7e64b9cSStewart Smith 		      unsigned long events, void *change)
404c7e64b9cSStewart Smith {
405c7e64b9cSStewart Smith 	if (events & OPAL_EVENT_DUMP_AVAIL)
406c7e64b9cSStewart Smith 		schedule_process_dump();
407c7e64b9cSStewart Smith 
408c7e64b9cSStewart Smith 	return 0;
409c7e64b9cSStewart Smith }
410c7e64b9cSStewart Smith 
411c7e64b9cSStewart Smith static struct notifier_block dump_nb = {
412c7e64b9cSStewart Smith 	.notifier_call  = dump_event,
413c7e64b9cSStewart Smith 	.next           = NULL,
414c7e64b9cSStewart Smith 	.priority       = 0
415c7e64b9cSStewart Smith };
416c7e64b9cSStewart Smith 
417c7e64b9cSStewart Smith void __init opal_platform_dump_init(void)
418c7e64b9cSStewart Smith {
419c7e64b9cSStewart Smith 	int rc;
420c7e64b9cSStewart Smith 
421c7e64b9cSStewart Smith 	dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
422c7e64b9cSStewart Smith 	if (!dump_kset) {
423c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create dump kset\n", __func__);
424c7e64b9cSStewart Smith 		return;
425c7e64b9cSStewart Smith 	}
426c7e64b9cSStewart Smith 
427c7e64b9cSStewart Smith 	rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
428c7e64b9cSStewart Smith 	if (rc) {
429c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create initiate dump attr group\n",
430c7e64b9cSStewart Smith 			__func__);
431c7e64b9cSStewart Smith 		kobject_put(&dump_kset->kobj);
432c7e64b9cSStewart Smith 		return;
433c7e64b9cSStewart Smith 	}
434c7e64b9cSStewart Smith 
435c7e64b9cSStewart Smith 	rc = opal_notifier_register(&dump_nb);
436c7e64b9cSStewart Smith 	if (rc) {
437c7e64b9cSStewart Smith 		pr_warn("%s: Can't register OPAL event notifier (%d)\n",
438c7e64b9cSStewart Smith 			__func__, rc);
439c7e64b9cSStewart Smith 		return;
440c7e64b9cSStewart Smith 	}
441c7e64b9cSStewart Smith 
442c7e64b9cSStewart Smith 	opal_dump_resend_notification();
443c7e64b9cSStewart Smith }
444