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