12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c7e64b9cSStewart Smith /*
3c7e64b9cSStewart Smith  * PowerNV OPAL Dump Interface
4c7e64b9cSStewart Smith  *
5c7e64b9cSStewart Smith  * Copyright 2013,2014 IBM Corp.
6c7e64b9cSStewart Smith  */
7c7e64b9cSStewart Smith 
8c7e64b9cSStewart Smith #include <linux/kobject.h>
9c7e64b9cSStewart Smith #include <linux/mm.h>
10c7e64b9cSStewart Smith #include <linux/slab.h>
11c7e64b9cSStewart Smith #include <linux/vmalloc.h>
12c7e64b9cSStewart Smith #include <linux/pagemap.h>
13c7e64b9cSStewart Smith #include <linux/delay.h>
148034f715SAlistair Popple #include <linux/interrupt.h>
15c7e64b9cSStewart Smith 
16c7e64b9cSStewart Smith #include <asm/opal.h>
17c7e64b9cSStewart Smith 
18c7e64b9cSStewart Smith #define DUMP_TYPE_FSP	0x01
19c7e64b9cSStewart Smith 
20c7e64b9cSStewart Smith struct dump_obj {
21c7e64b9cSStewart Smith 	struct kobject  kobj;
22c7e64b9cSStewart Smith 	struct bin_attribute dump_attr;
23c7e64b9cSStewart Smith 	uint32_t	id;  /* becomes object name */
24c7e64b9cSStewart Smith 	uint32_t	type;
25c7e64b9cSStewart Smith 	uint32_t	size;
26c7e64b9cSStewart Smith 	char		*buffer;
27c7e64b9cSStewart Smith };
28c7e64b9cSStewart Smith #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
29c7e64b9cSStewart Smith 
30c7e64b9cSStewart Smith struct dump_attribute {
31c7e64b9cSStewart Smith 	struct attribute attr;
32c7e64b9cSStewart Smith 	ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
33c7e64b9cSStewart Smith 			char *buf);
34c7e64b9cSStewart Smith 	ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
35c7e64b9cSStewart Smith 			 const char *buf, size_t count);
36c7e64b9cSStewart Smith };
37c7e64b9cSStewart Smith #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
38c7e64b9cSStewart Smith 
dump_id_show(struct dump_obj * dump_obj,struct dump_attribute * attr,char * buf)39c7e64b9cSStewart Smith static ssize_t dump_id_show(struct dump_obj *dump_obj,
40c7e64b9cSStewart Smith 			    struct dump_attribute *attr,
41c7e64b9cSStewart Smith 			    char *buf)
42c7e64b9cSStewart Smith {
43c7e64b9cSStewart Smith 	return sprintf(buf, "0x%x\n", dump_obj->id);
44c7e64b9cSStewart Smith }
45c7e64b9cSStewart Smith 
dump_type_to_string(uint32_t type)46c7e64b9cSStewart Smith static const char* dump_type_to_string(uint32_t type)
47c7e64b9cSStewart Smith {
48c7e64b9cSStewart Smith 	switch (type) {
49c7e64b9cSStewart Smith 	case 0x01: return "SP Dump";
50c7e64b9cSStewart Smith 	case 0x02: return "System/Platform Dump";
51c7e64b9cSStewart Smith 	case 0x03: return "SMA Dump";
52c7e64b9cSStewart Smith 	default: return "unknown";
53c7e64b9cSStewart Smith 	}
54c7e64b9cSStewart Smith }
55c7e64b9cSStewart Smith 
dump_type_show(struct dump_obj * dump_obj,struct dump_attribute * attr,char * buf)56c7e64b9cSStewart Smith static ssize_t dump_type_show(struct dump_obj *dump_obj,
57c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
58c7e64b9cSStewart Smith 			      char *buf)
59c7e64b9cSStewart Smith {
60c7e64b9cSStewart Smith 
61c7e64b9cSStewart Smith 	return sprintf(buf, "0x%x %s\n", dump_obj->type,
62c7e64b9cSStewart Smith 		       dump_type_to_string(dump_obj->type));
63c7e64b9cSStewart Smith }
64c7e64b9cSStewart Smith 
dump_ack_show(struct dump_obj * dump_obj,struct dump_attribute * attr,char * buf)65c7e64b9cSStewart Smith static ssize_t dump_ack_show(struct dump_obj *dump_obj,
66c7e64b9cSStewart Smith 			     struct dump_attribute *attr,
67c7e64b9cSStewart Smith 			     char *buf)
68c7e64b9cSStewart Smith {
69c7e64b9cSStewart Smith 	return sprintf(buf, "ack - acknowledge dump\n");
70c7e64b9cSStewart Smith }
71c7e64b9cSStewart Smith 
72c7e64b9cSStewart Smith /*
73c7e64b9cSStewart Smith  * Send acknowledgement to OPAL
74c7e64b9cSStewart Smith  */
dump_send_ack(uint32_t dump_id)75c7e64b9cSStewart Smith static int64_t dump_send_ack(uint32_t dump_id)
76c7e64b9cSStewart Smith {
77c7e64b9cSStewart Smith 	int rc;
78c7e64b9cSStewart Smith 
79c7e64b9cSStewart Smith 	rc = opal_dump_ack(dump_id);
80c7e64b9cSStewart Smith 	if (rc)
81c7e64b9cSStewart Smith 		pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
82c7e64b9cSStewart Smith 			__func__, dump_id, rc);
83c7e64b9cSStewart Smith 	return rc;
84c7e64b9cSStewart Smith }
85c7e64b9cSStewart Smith 
dump_ack_store(struct dump_obj * dump_obj,struct dump_attribute * attr,const char * buf,size_t count)86c7e64b9cSStewart Smith static ssize_t dump_ack_store(struct dump_obj *dump_obj,
87c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
88c7e64b9cSStewart Smith 			      const char *buf,
89c7e64b9cSStewart Smith 			      size_t count)
90c7e64b9cSStewart Smith {
91358ab796SVasant Hegde 	/*
92358ab796SVasant Hegde 	 * Try to self remove this attribute. If we are successful,
93358ab796SVasant Hegde 	 * delete the kobject itself.
94358ab796SVasant Hegde 	 */
95358ab796SVasant Hegde 	if (sysfs_remove_file_self(&dump_obj->kobj, &attr->attr)) {
96c7e64b9cSStewart Smith 		dump_send_ack(dump_obj->id);
97cc4f265aSStewart Smith 		kobject_put(&dump_obj->kobj);
98358ab796SVasant Hegde 	}
99c7e64b9cSStewart Smith 	return count;
100c7e64b9cSStewart Smith }
101c7e64b9cSStewart Smith 
102c7e64b9cSStewart Smith /* Attributes of a dump
103c7e64b9cSStewart Smith  * The binary attribute of the dump itself is dynamic
104c7e64b9cSStewart Smith  * due to the dynamic size of the dump
105c7e64b9cSStewart Smith  */
106c7e64b9cSStewart Smith static struct dump_attribute id_attribute =
10757ad583fSRussell Currey 	__ATTR(id, 0444, dump_id_show, NULL);
108c7e64b9cSStewart Smith static struct dump_attribute type_attribute =
10957ad583fSRussell Currey 	__ATTR(type, 0444, dump_type_show, NULL);
110c7e64b9cSStewart Smith static struct dump_attribute ack_attribute =
111c7e64b9cSStewart Smith 	__ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
112c7e64b9cSStewart Smith 
init_dump_show(struct dump_obj * dump_obj,struct dump_attribute * attr,char * buf)113c7e64b9cSStewart Smith static ssize_t init_dump_show(struct dump_obj *dump_obj,
114c7e64b9cSStewart Smith 			      struct dump_attribute *attr,
115c7e64b9cSStewart Smith 			      char *buf)
116c7e64b9cSStewart Smith {
117cdd91b89SVasant Hegde 	return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
118c7e64b9cSStewart Smith }
119c7e64b9cSStewart Smith 
dump_fips_init(uint8_t type)120c7e64b9cSStewart Smith static int64_t dump_fips_init(uint8_t type)
121c7e64b9cSStewart Smith {
122c7e64b9cSStewart Smith 	int rc;
123c7e64b9cSStewart Smith 
124c7e64b9cSStewart Smith 	rc = opal_dump_init(type);
125c7e64b9cSStewart Smith 	if (rc)
126cdd91b89SVasant Hegde 		pr_warn("%s: Failed to initiate FSP dump (%d)\n",
127c7e64b9cSStewart Smith 			__func__, rc);
128c7e64b9cSStewart Smith 	return rc;
129c7e64b9cSStewart Smith }
130c7e64b9cSStewart Smith 
init_dump_store(struct dump_obj * dump_obj,struct dump_attribute * attr,const char * buf,size_t count)131c7e64b9cSStewart Smith static ssize_t init_dump_store(struct dump_obj *dump_obj,
132c7e64b9cSStewart Smith 			       struct dump_attribute *attr,
133c7e64b9cSStewart Smith 			       const char *buf,
134c7e64b9cSStewart Smith 			       size_t count)
135c7e64b9cSStewart Smith {
136cdd91b89SVasant Hegde 	int rc;
137cdd91b89SVasant Hegde 
138cdd91b89SVasant Hegde 	rc = dump_fips_init(DUMP_TYPE_FSP);
139cdd91b89SVasant Hegde 	if (rc == OPAL_SUCCESS)
140c7e64b9cSStewart Smith 		pr_info("%s: Initiated FSP dump\n", __func__);
141cdd91b89SVasant Hegde 
142c7e64b9cSStewart Smith 	return count;
143c7e64b9cSStewart Smith }
144c7e64b9cSStewart Smith 
145c7e64b9cSStewart Smith static struct dump_attribute initiate_attribute =
146c7e64b9cSStewart Smith 	__ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
147c7e64b9cSStewart Smith 
148c7e64b9cSStewart Smith static struct attribute *initiate_attrs[] = {
149c7e64b9cSStewart Smith 	&initiate_attribute.attr,
150c7e64b9cSStewart Smith 	NULL,
151c7e64b9cSStewart Smith };
152c7e64b9cSStewart Smith 
153*6b3a3e12SRohan McLure static const struct attribute_group initiate_attr_group = {
154c7e64b9cSStewart Smith 	.attrs = initiate_attrs,
155c7e64b9cSStewart Smith };
156c7e64b9cSStewart Smith 
157c7e64b9cSStewart Smith static struct kset *dump_kset;
158c7e64b9cSStewart Smith 
dump_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)159c7e64b9cSStewart Smith static ssize_t dump_attr_show(struct kobject *kobj,
160c7e64b9cSStewart Smith 			      struct attribute *attr,
161c7e64b9cSStewart Smith 			      char *buf)
162c7e64b9cSStewart Smith {
163c7e64b9cSStewart Smith 	struct dump_attribute *attribute;
164c7e64b9cSStewart Smith 	struct dump_obj *dump;
165c7e64b9cSStewart Smith 
166c7e64b9cSStewart Smith 	attribute = to_dump_attr(attr);
167c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
168c7e64b9cSStewart Smith 
169c7e64b9cSStewart Smith 	if (!attribute->show)
170c7e64b9cSStewart Smith 		return -EIO;
171c7e64b9cSStewart Smith 
172c7e64b9cSStewart Smith 	return attribute->show(dump, attribute, buf);
173c7e64b9cSStewart Smith }
174c7e64b9cSStewart Smith 
dump_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)175c7e64b9cSStewart Smith static ssize_t dump_attr_store(struct kobject *kobj,
176c7e64b9cSStewart Smith 			       struct attribute *attr,
177c7e64b9cSStewart Smith 			       const char *buf, size_t len)
178c7e64b9cSStewart Smith {
179c7e64b9cSStewart Smith 	struct dump_attribute *attribute;
180c7e64b9cSStewart Smith 	struct dump_obj *dump;
181c7e64b9cSStewart Smith 
182c7e64b9cSStewart Smith 	attribute = to_dump_attr(attr);
183c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
184c7e64b9cSStewart Smith 
185c7e64b9cSStewart Smith 	if (!attribute->store)
186c7e64b9cSStewart Smith 		return -EIO;
187c7e64b9cSStewart Smith 
188c7e64b9cSStewart Smith 	return attribute->store(dump, attribute, buf, len);
189c7e64b9cSStewart Smith }
190c7e64b9cSStewart Smith 
191c7e64b9cSStewart Smith static const struct sysfs_ops dump_sysfs_ops = {
192c7e64b9cSStewart Smith 	.show = dump_attr_show,
193c7e64b9cSStewart Smith 	.store = dump_attr_store,
194c7e64b9cSStewart Smith };
195c7e64b9cSStewart Smith 
dump_release(struct kobject * kobj)196c7e64b9cSStewart Smith static void dump_release(struct kobject *kobj)
197c7e64b9cSStewart Smith {
198c7e64b9cSStewart Smith 	struct dump_obj *dump;
199c7e64b9cSStewart Smith 
200c7e64b9cSStewart Smith 	dump = to_dump_obj(kobj);
201c7e64b9cSStewart Smith 	vfree(dump->buffer);
202c7e64b9cSStewart Smith 	kfree(dump);
203c7e64b9cSStewart Smith }
204c7e64b9cSStewart Smith 
205c7e64b9cSStewart Smith static struct attribute *dump_default_attrs[] = {
206c7e64b9cSStewart Smith 	&id_attribute.attr,
207c7e64b9cSStewart Smith 	&type_attribute.attr,
208c7e64b9cSStewart Smith 	&ack_attribute.attr,
209c7e64b9cSStewart Smith 	NULL,
210c7e64b9cSStewart Smith };
21132a1bda4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(dump_default);
212c7e64b9cSStewart Smith 
213c7e64b9cSStewart Smith static struct kobj_type dump_ktype = {
214c7e64b9cSStewart Smith 	.sysfs_ops = &dump_sysfs_ops,
215c7e64b9cSStewart Smith 	.release = &dump_release,
21632a1bda4SGreg Kroah-Hartman 	.default_groups = dump_default_groups,
217c7e64b9cSStewart Smith };
218c7e64b9cSStewart Smith 
dump_read_info(uint32_t * dump_id,uint32_t * dump_size,uint32_t * dump_type)2192d6b63bbSAnton Blanchard static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
220c7e64b9cSStewart Smith {
2212d6b63bbSAnton Blanchard 	__be32 id, size, type;
222c7e64b9cSStewart Smith 	int rc;
223c7e64b9cSStewart Smith 
2242d6b63bbSAnton Blanchard 	type = cpu_to_be32(0xffffffff);
225c7e64b9cSStewart Smith 
2262d6b63bbSAnton Blanchard 	rc = opal_dump_info2(&id, &size, &type);
227c7e64b9cSStewart Smith 	if (rc == OPAL_PARAMETER)
2282d6b63bbSAnton Blanchard 		rc = opal_dump_info(&id, &size);
2292d6b63bbSAnton Blanchard 
230a5bbe8fdSMukesh Ojha 	if (rc) {
231a5bbe8fdSMukesh Ojha 		pr_warn("%s: Failed to get dump info (%d)\n",
232a5bbe8fdSMukesh Ojha 			__func__, rc);
233a5bbe8fdSMukesh Ojha 		return rc;
234a5bbe8fdSMukesh Ojha 	}
235a5bbe8fdSMukesh Ojha 
2362d6b63bbSAnton Blanchard 	*dump_id = be32_to_cpu(id);
2372d6b63bbSAnton Blanchard 	*dump_size = be32_to_cpu(size);
2382d6b63bbSAnton Blanchard 	*dump_type = be32_to_cpu(type);
239c7e64b9cSStewart Smith 
240c7e64b9cSStewart Smith 	return rc;
241c7e64b9cSStewart Smith }
242c7e64b9cSStewart Smith 
dump_read_data(struct dump_obj * dump)243c7e64b9cSStewart Smith static int64_t dump_read_data(struct dump_obj *dump)
244c7e64b9cSStewart Smith {
245c7e64b9cSStewart Smith 	struct opal_sg_list *list;
246c7e64b9cSStewart Smith 	uint64_t addr;
247c7e64b9cSStewart Smith 	int64_t rc;
248c7e64b9cSStewart Smith 
249c7e64b9cSStewart Smith 	/* Allocate memory */
250c7e64b9cSStewart Smith 	dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
251c7e64b9cSStewart Smith 	if (!dump->buffer) {
252c7e64b9cSStewart Smith 		pr_err("%s : Failed to allocate memory\n", __func__);
253c7e64b9cSStewart Smith 		rc = -ENOMEM;
254c7e64b9cSStewart Smith 		goto out;
255c7e64b9cSStewart Smith 	}
256c7e64b9cSStewart Smith 
257c7e64b9cSStewart Smith 	/* Generate SG list */
2583441f04bSAnton Blanchard 	list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
259c7e64b9cSStewart Smith 	if (!list) {
260c7e64b9cSStewart Smith 		rc = -ENOMEM;
261c7e64b9cSStewart Smith 		goto out;
262c7e64b9cSStewart Smith 	}
263c7e64b9cSStewart Smith 
264c7e64b9cSStewart Smith 	/* First entry address */
265c7e64b9cSStewart Smith 	addr = __pa(list);
266c7e64b9cSStewart Smith 
267c7e64b9cSStewart Smith 	/* Fetch data */
268c7e64b9cSStewart Smith 	rc = OPAL_BUSY_EVENT;
269c7e64b9cSStewart Smith 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
270c7e64b9cSStewart Smith 		rc = opal_dump_read(dump->id, addr);
271c7e64b9cSStewart Smith 		if (rc == OPAL_BUSY_EVENT) {
272c7e64b9cSStewart Smith 			opal_poll_events(NULL);
273c7e64b9cSStewart Smith 			msleep(20);
274c7e64b9cSStewart Smith 		}
275c7e64b9cSStewart Smith 	}
276c7e64b9cSStewart Smith 
277c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
278c7e64b9cSStewart Smith 		pr_warn("%s: Extract dump failed for ID 0x%x\n",
279c7e64b9cSStewart Smith 			__func__, dump->id);
280c7e64b9cSStewart Smith 
281c7e64b9cSStewart Smith 	/* Free SG list */
2823441f04bSAnton Blanchard 	opal_free_sg_list(list);
283c7e64b9cSStewart Smith 
284c7e64b9cSStewart Smith out:
285c7e64b9cSStewart Smith 	return rc;
286c7e64b9cSStewart Smith }
287c7e64b9cSStewart Smith 
dump_attr_read(struct file * filep,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t pos,size_t count)288c7e64b9cSStewart Smith static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
289c7e64b9cSStewart Smith 			      struct bin_attribute *bin_attr,
290c7e64b9cSStewart Smith 			      char *buffer, loff_t pos, size_t count)
291c7e64b9cSStewart Smith {
292c7e64b9cSStewart Smith 	ssize_t rc;
293c7e64b9cSStewart Smith 
294c7e64b9cSStewart Smith 	struct dump_obj *dump = to_dump_obj(kobj);
295c7e64b9cSStewart Smith 
296c7e64b9cSStewart Smith 	if (!dump->buffer) {
297c7e64b9cSStewart Smith 		rc = dump_read_data(dump);
298c7e64b9cSStewart Smith 
299c7e64b9cSStewart Smith 		if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
300c7e64b9cSStewart Smith 			vfree(dump->buffer);
301c7e64b9cSStewart Smith 			dump->buffer = NULL;
302c7e64b9cSStewart Smith 
303c7e64b9cSStewart Smith 			return -EIO;
304c7e64b9cSStewart Smith 		}
305c7e64b9cSStewart Smith 		if (rc == OPAL_PARTIAL) {
306c7e64b9cSStewart Smith 			/* On a partial read, we just return EIO
307c7e64b9cSStewart Smith 			 * and rely on userspace to ask us to try
308c7e64b9cSStewart Smith 			 * again.
309c7e64b9cSStewart Smith 			 */
310c7e64b9cSStewart Smith 			pr_info("%s: Platform dump partially read. ID = 0x%x\n",
311c7e64b9cSStewart Smith 				__func__, dump->id);
312c7e64b9cSStewart Smith 			return -EIO;
313c7e64b9cSStewart Smith 		}
314c7e64b9cSStewart Smith 	}
315c7e64b9cSStewart Smith 
316c7e64b9cSStewart Smith 	memcpy(buffer, dump->buffer + pos, count);
317c7e64b9cSStewart Smith 
318c7e64b9cSStewart Smith 	/* You may think we could free the dump buffer now and retrieve
319c7e64b9cSStewart Smith 	 * it again later if needed, but due to current firmware limitation,
320c7e64b9cSStewart Smith 	 * that's not the case. So, once read into userspace once,
321c7e64b9cSStewart Smith 	 * we keep the dump around until it's acknowledged by userspace.
322c7e64b9cSStewart Smith 	 */
323c7e64b9cSStewart Smith 
324c7e64b9cSStewart Smith 	return count;
325c7e64b9cSStewart Smith }
326c7e64b9cSStewart Smith 
create_dump_obj(uint32_t id,size_t size,uint32_t type)3270a43ae3eSVasant Hegde static void create_dump_obj(uint32_t id, size_t size, uint32_t type)
328c7e64b9cSStewart Smith {
329c7e64b9cSStewart Smith 	struct dump_obj *dump;
330c7e64b9cSStewart Smith 	int rc;
331c7e64b9cSStewart Smith 
332c7e64b9cSStewart Smith 	dump = kzalloc(sizeof(*dump), GFP_KERNEL);
333c7e64b9cSStewart Smith 	if (!dump)
3340a43ae3eSVasant Hegde 		return;
335c7e64b9cSStewart Smith 
336c7e64b9cSStewart Smith 	dump->kobj.kset = dump_kset;
337c7e64b9cSStewart Smith 
338c7e64b9cSStewart Smith 	kobject_init(&dump->kobj, &dump_ktype);
339c7e64b9cSStewart Smith 
340c7e64b9cSStewart Smith 	sysfs_bin_attr_init(&dump->dump_attr);
341c7e64b9cSStewart Smith 
342c7e64b9cSStewart Smith 	dump->dump_attr.attr.name = "dump";
343c7e64b9cSStewart Smith 	dump->dump_attr.attr.mode = 0400;
344c7e64b9cSStewart Smith 	dump->dump_attr.size = size;
345c7e64b9cSStewart Smith 	dump->dump_attr.read = dump_attr_read;
346c7e64b9cSStewart Smith 
347c7e64b9cSStewart Smith 	dump->id = id;
348c7e64b9cSStewart Smith 	dump->size = size;
349c7e64b9cSStewart Smith 	dump->type = type;
350c7e64b9cSStewart Smith 
351c7e64b9cSStewart Smith 	rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
352c7e64b9cSStewart Smith 	if (rc) {
353c7e64b9cSStewart Smith 		kobject_put(&dump->kobj);
3540a43ae3eSVasant Hegde 		return;
355c7e64b9cSStewart Smith 	}
356c7e64b9cSStewart Smith 
3570a43ae3eSVasant Hegde 	/*
3580a43ae3eSVasant Hegde 	 * As soon as the sysfs file for this dump is created/activated there is
3590a43ae3eSVasant Hegde 	 * a chance the opal_errd daemon (or any userspace) might read and
3600a43ae3eSVasant Hegde 	 * acknowledge the dump before kobject_uevent() is called. If that
3610a43ae3eSVasant Hegde 	 * happens then there is a potential race between
3620a43ae3eSVasant Hegde 	 * dump_ack_store->kobject_put() and kobject_uevent() which leads to a
3630a43ae3eSVasant Hegde 	 * use-after-free of a kernfs object resulting in a kernel crash.
3640a43ae3eSVasant Hegde 	 *
3650a43ae3eSVasant Hegde 	 * To avoid that, we need to take a reference on behalf of the bin file,
3660a43ae3eSVasant Hegde 	 * so that our reference remains valid while we call kobject_uevent().
3670a43ae3eSVasant Hegde 	 * We then drop our reference before exiting the function, leaving the
3680a43ae3eSVasant Hegde 	 * bin file to drop the last reference (if it hasn't already).
3690a43ae3eSVasant Hegde 	 */
3700a43ae3eSVasant Hegde 
3710a43ae3eSVasant Hegde 	/* Take a reference for the bin file */
3720a43ae3eSVasant Hegde 	kobject_get(&dump->kobj);
373c7e64b9cSStewart Smith 	rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
3740a43ae3eSVasant Hegde 	if (rc == 0) {
3750a43ae3eSVasant Hegde 		kobject_uevent(&dump->kobj, KOBJ_ADD);
376c7e64b9cSStewart Smith 
377c7e64b9cSStewart Smith 		pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
378c7e64b9cSStewart Smith 			__func__, dump->id, dump->size);
3790a43ae3eSVasant Hegde 	} else {
3800a43ae3eSVasant Hegde 		/* Drop reference count taken for bin file */
3810a43ae3eSVasant Hegde 		kobject_put(&dump->kobj);
3820a43ae3eSVasant Hegde 	}
383c7e64b9cSStewart Smith 
3840a43ae3eSVasant Hegde 	/* Drop our reference */
3850a43ae3eSVasant Hegde 	kobject_put(&dump->kobj);
3860a43ae3eSVasant Hegde 	return;
387c7e64b9cSStewart Smith }
388c7e64b9cSStewart Smith 
process_dump(int irq,void * data)3898034f715SAlistair Popple static irqreturn_t process_dump(int irq, void *data)
390c7e64b9cSStewart Smith {
391c7e64b9cSStewart Smith 	int rc;
392c7e64b9cSStewart Smith 	uint32_t dump_id, dump_size, dump_type;
393c7e64b9cSStewart Smith 	char name[22];
394a9cbf0b2SMukesh Ojha 	struct kobject *kobj;
395c7e64b9cSStewart Smith 
396c7e64b9cSStewart Smith 	rc = dump_read_info(&dump_id, &dump_size, &dump_type);
397c7e64b9cSStewart Smith 	if (rc != OPAL_SUCCESS)
398b29336c0SMukesh Ojha 		return IRQ_HANDLED;
399c7e64b9cSStewart Smith 
400c7e64b9cSStewart Smith 	sprintf(name, "0x%x-0x%x", dump_type, dump_id);
401c7e64b9cSStewart Smith 
402c7e64b9cSStewart Smith 	/* we may get notified twice, let's handle
403c7e64b9cSStewart Smith 	 * that gracefully and not create two conflicting
404c7e64b9cSStewart Smith 	 * entries.
405c7e64b9cSStewart Smith 	 */
406a9cbf0b2SMukesh Ojha 	kobj = kset_find_obj(dump_kset, name);
407a9cbf0b2SMukesh Ojha 	if (kobj) {
408a9cbf0b2SMukesh Ojha 		/* Drop reference added by kset_find_obj() */
409a9cbf0b2SMukesh Ojha 		kobject_put(kobj);
410b29336c0SMukesh Ojha 		return IRQ_HANDLED;
411a9cbf0b2SMukesh Ojha 	}
412c7e64b9cSStewart Smith 
413b29336c0SMukesh Ojha 	create_dump_obj(dump_id, dump_size, dump_type);
414c7e64b9cSStewart Smith 
4158034f715SAlistair Popple 	return IRQ_HANDLED;
416c7e64b9cSStewart Smith }
417c7e64b9cSStewart Smith 
opal_platform_dump_init(void)418c7e64b9cSStewart Smith void __init opal_platform_dump_init(void)
419c7e64b9cSStewart Smith {
420c7e64b9cSStewart Smith 	int rc;
4218034f715SAlistair Popple 	int dump_irq;
422c7e64b9cSStewart Smith 
423ee878437SVasant Hegde 	/* Dump not supported by firmware */
424831cf65bSMichael Neuling 	if (!opal_check_token(OPAL_DUMP_READ))
425831cf65bSMichael Neuling 		return;
426831cf65bSMichael Neuling 
427c7e64b9cSStewart Smith 	dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
428c7e64b9cSStewart Smith 	if (!dump_kset) {
429c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create dump kset\n", __func__);
430c7e64b9cSStewart Smith 		return;
431c7e64b9cSStewart Smith 	}
432c7e64b9cSStewart Smith 
433c7e64b9cSStewart Smith 	rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
434c7e64b9cSStewart Smith 	if (rc) {
435c7e64b9cSStewart Smith 		pr_warn("%s: Failed to create initiate dump attr group\n",
436c7e64b9cSStewart Smith 			__func__);
437c7e64b9cSStewart Smith 		kobject_put(&dump_kset->kobj);
438c7e64b9cSStewart Smith 		return;
439c7e64b9cSStewart Smith 	}
440c7e64b9cSStewart Smith 
4418034f715SAlistair Popple 	dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
4428034f715SAlistair Popple 	if (!dump_irq) {
4438034f715SAlistair Popple 		pr_err("%s: Can't register OPAL event irq (%d)\n",
4448034f715SAlistair Popple 		       __func__, dump_irq);
4458034f715SAlistair Popple 		return;
4468034f715SAlistair Popple 	}
4478034f715SAlistair Popple 
4488034f715SAlistair Popple 	rc = request_threaded_irq(dump_irq, NULL, process_dump,
4498034f715SAlistair Popple 				IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
4508034f715SAlistair Popple 				"opal-dump", NULL);
451c7e64b9cSStewart Smith 	if (rc) {
4528034f715SAlistair Popple 		pr_err("%s: Can't request OPAL event irq (%d)\n",
453c7e64b9cSStewart Smith 		       __func__, rc);
454c7e64b9cSStewart Smith 		return;
455c7e64b9cSStewart Smith 	}
456c7e64b9cSStewart Smith 
4577e73a3b7SStewart Smith 	if (opal_check_token(OPAL_DUMP_RESEND))
458c7e64b9cSStewart Smith 		opal_dump_resend_notification();
459c7e64b9cSStewart Smith }
460