xref: /openbmc/linux/drivers/acpi/apei/einj.c (revision b5266ea6)
1 /*
2  * APEI Error INJection support
3  *
4  * EINJ provides a hardware error injection mechanism, this is useful
5  * for debugging and testing of other APEI and RAS features.
6  *
7  * For more information about EINJ, please refer to ACPI Specification
8  * version 4.0, section 17.5.
9  *
10  * Copyright 2009-2010 Intel Corp.
11  *   Author: Huang Ying <ying.huang@intel.com>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License version
15  * 2 as published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/io.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/nmi.h>
34 #include <linux/delay.h>
35 #include <acpi/acpi.h>
36 
37 #include "apei-internal.h"
38 
39 #define EINJ_PFX "EINJ: "
40 
41 #define SPIN_UNIT		100			/* 100ns */
42 /* Firmware should respond within 1 milliseconds */
43 #define FIRMWARE_TIMEOUT	(1 * NSEC_PER_MSEC)
44 
45 /*
46  * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
47  */
48 static int acpi5;
49 
50 struct set_error_type_with_address {
51 	u32	type;
52 	u32	vendor_extension;
53 	u32	flags;
54 	u32	apicid;
55 	u64	memory_address;
56 	u64	memory_address_range;
57 	u32	pcie_sbdf;
58 };
59 enum {
60 	SETWA_FLAGS_APICID = 1,
61 	SETWA_FLAGS_MEM = 2,
62 	SETWA_FLAGS_PCIE_SBDF = 4,
63 };
64 
65 /*
66  * Vendor extensions for platform specific operations
67  */
68 struct vendor_error_type_extension {
69 	u32	length;
70 	u32	pcie_sbdf;
71 	u16	vendor_id;
72 	u16	device_id;
73 	u8	rev_id;
74 	u8	reserved[3];
75 };
76 
77 static u32 vendor_flags;
78 static struct debugfs_blob_wrapper vendor_blob;
79 static char vendor_dev[64];
80 
81 /*
82  * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
83  * EINJ table through an unpublished extension. Use with caution as
84  * most will ignore the parameter and make their own choice of address
85  * for error injection.  This extension is used only if
86  * param_extension module parameter is specified.
87  */
88 struct einj_parameter {
89 	u64 type;
90 	u64 reserved1;
91 	u64 reserved2;
92 	u64 param1;
93 	u64 param2;
94 };
95 
96 #define EINJ_OP_BUSY			0x1
97 #define EINJ_STATUS_SUCCESS		0x0
98 #define EINJ_STATUS_FAIL		0x1
99 #define EINJ_STATUS_INVAL		0x2
100 
101 #define EINJ_TAB_ENTRY(tab)						\
102 	((struct acpi_whea_header *)((char *)(tab) +			\
103 				    sizeof(struct acpi_table_einj)))
104 
105 static bool param_extension;
106 module_param(param_extension, bool, 0);
107 
108 static struct acpi_table_einj *einj_tab;
109 
110 static struct apei_resources einj_resources;
111 
112 static struct apei_exec_ins_type einj_ins_type[] = {
113 	[ACPI_EINJ_READ_REGISTER] = {
114 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
115 		.run   = apei_exec_read_register,
116 	},
117 	[ACPI_EINJ_READ_REGISTER_VALUE] = {
118 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
119 		.run   = apei_exec_read_register_value,
120 	},
121 	[ACPI_EINJ_WRITE_REGISTER] = {
122 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
123 		.run   = apei_exec_write_register,
124 	},
125 	[ACPI_EINJ_WRITE_REGISTER_VALUE] = {
126 		.flags = APEI_EXEC_INS_ACCESS_REGISTER,
127 		.run   = apei_exec_write_register_value,
128 	},
129 	[ACPI_EINJ_NOOP] = {
130 		.flags = 0,
131 		.run   = apei_exec_noop,
132 	},
133 };
134 
135 /*
136  * Prevent EINJ interpreter to run simultaneously, because the
137  * corresponding firmware implementation may not work properly when
138  * invoked simultaneously.
139  */
140 static DEFINE_MUTEX(einj_mutex);
141 
142 static void *einj_param;
143 
144 #ifndef readq
145 static inline __u64 readq(volatile void __iomem *addr)
146 {
147 	return ((__u64)readl(addr+4) << 32) + readl(addr);
148 }
149 #endif
150 
151 #ifndef writeq
152 static inline void writeq(__u64 val, volatile void __iomem *addr)
153 {
154 	writel(val, addr);
155 	writel(val >> 32, addr+4);
156 }
157 #endif
158 
159 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
160 {
161 	apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
162 			   EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
163 }
164 
165 static int __einj_get_available_error_type(u32 *type)
166 {
167 	struct apei_exec_context ctx;
168 	int rc;
169 
170 	einj_exec_ctx_init(&ctx);
171 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
172 	if (rc)
173 		return rc;
174 	*type = apei_exec_ctx_get_output(&ctx);
175 
176 	return 0;
177 }
178 
179 /* Get error injection capabilities of the platform */
180 static int einj_get_available_error_type(u32 *type)
181 {
182 	int rc;
183 
184 	mutex_lock(&einj_mutex);
185 	rc = __einj_get_available_error_type(type);
186 	mutex_unlock(&einj_mutex);
187 
188 	return rc;
189 }
190 
191 static int einj_timedout(u64 *t)
192 {
193 	if ((s64)*t < SPIN_UNIT) {
194 		pr_warning(FW_WARN EINJ_PFX
195 			   "Firmware does not respond in time\n");
196 		return 1;
197 	}
198 	*t -= SPIN_UNIT;
199 	ndelay(SPIN_UNIT);
200 	touch_nmi_watchdog();
201 	return 0;
202 }
203 
204 static void check_vendor_extension(u64 paddr,
205 				   struct set_error_type_with_address *v5param)
206 {
207 	int	offset = readl(&v5param->vendor_extension);
208 	struct	vendor_error_type_extension *v;
209 	u32	sbdf;
210 
211 	if (!offset)
212 		return;
213 	v = ioremap(paddr + offset, sizeof(*v));
214 	if (!v)
215 		return;
216 	sbdf = readl(&v->pcie_sbdf);
217 	sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
218 		sbdf >> 24, (sbdf >> 16) & 0xff,
219 		(sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
220 		 readw(&v->vendor_id), readw(&v->device_id),
221 		readb(&v->rev_id));
222 	iounmap(v);
223 }
224 
225 static void *einj_get_parameter_address(void)
226 {
227 	int i;
228 	u64 paddrv4 = 0, paddrv5 = 0;
229 	struct acpi_whea_header *entry;
230 
231 	entry = EINJ_TAB_ENTRY(einj_tab);
232 	for (i = 0; i < einj_tab->entries; i++) {
233 		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
234 		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
235 		    entry->register_region.space_id ==
236 		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
237 			memcpy(&paddrv4, &entry->register_region.address,
238 			       sizeof(paddrv4));
239 		if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
240 		    entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
241 		    entry->register_region.space_id ==
242 		    ACPI_ADR_SPACE_SYSTEM_MEMORY)
243 			memcpy(&paddrv5, &entry->register_region.address,
244 			       sizeof(paddrv5));
245 		entry++;
246 	}
247 	if (paddrv5) {
248 		struct set_error_type_with_address *v5param;
249 
250 		v5param = ioremap(paddrv5, sizeof(*v5param));
251 		if (v5param) {
252 			acpi5 = 1;
253 			check_vendor_extension(paddrv5, v5param);
254 			return v5param;
255 		}
256 	}
257 	if (paddrv4) {
258 		struct einj_parameter *v4param;
259 
260 		v4param = ioremap(paddrv4, sizeof(*v4param));
261 		if (!v4param)
262 			return 0;
263 		if (readq(&v4param->reserved1) || readq(&v4param->reserved2)) {
264 			iounmap(v4param);
265 			return 0;
266 		}
267 		return v4param;
268 	}
269 
270 	return 0;
271 }
272 
273 /* do sanity check to trigger table */
274 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
275 {
276 	if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
277 		return -EINVAL;
278 	if (trigger_tab->table_size > PAGE_SIZE ||
279 	    trigger_tab->table_size <= trigger_tab->header_size)
280 		return -EINVAL;
281 	if (trigger_tab->entry_count !=
282 	    (trigger_tab->table_size - trigger_tab->header_size) /
283 	    sizeof(struct acpi_einj_entry))
284 		return -EINVAL;
285 
286 	return 0;
287 }
288 
289 static struct acpi_generic_address *einj_get_trigger_parameter_region(
290 	struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
291 {
292 	int i;
293 	struct acpi_whea_header *entry;
294 
295 	entry = (struct acpi_whea_header *)
296 		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
297 	for (i = 0; i < trigger_tab->entry_count; i++) {
298 		if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
299 		entry->instruction == ACPI_EINJ_WRITE_REGISTER_VALUE &&
300 		entry->register_region.space_id ==
301 			ACPI_ADR_SPACE_SYSTEM_MEMORY &&
302 		(entry->register_region.address & param2) == (param1 & param2))
303 			return &entry->register_region;
304 		entry++;
305 	}
306 
307 	return NULL;
308 }
309 /* Execute instructions in trigger error action table */
310 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
311 				u64 param1, u64 param2)
312 {
313 	struct acpi_einj_trigger *trigger_tab = NULL;
314 	struct apei_exec_context trigger_ctx;
315 	struct apei_resources trigger_resources;
316 	struct acpi_whea_header *trigger_entry;
317 	struct resource *r;
318 	u32 table_size;
319 	int rc = -EIO;
320 	struct acpi_generic_address *trigger_param_region = NULL;
321 
322 	r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
323 			       "APEI EINJ Trigger Table");
324 	if (!r) {
325 		pr_err(EINJ_PFX
326 	"Can not request [mem %#010llx-%#010llx] for Trigger table\n",
327 		       (unsigned long long)trigger_paddr,
328 		       (unsigned long long)trigger_paddr +
329 			    sizeof(*trigger_tab) - 1);
330 		goto out;
331 	}
332 	trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
333 	if (!trigger_tab) {
334 		pr_err(EINJ_PFX "Failed to map trigger table!\n");
335 		goto out_rel_header;
336 	}
337 	rc = einj_check_trigger_header(trigger_tab);
338 	if (rc) {
339 		pr_warning(FW_BUG EINJ_PFX
340 			   "The trigger error action table is invalid\n");
341 		goto out_rel_header;
342 	}
343 	rc = -EIO;
344 	table_size = trigger_tab->table_size;
345 	r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
346 			       table_size - sizeof(*trigger_tab),
347 			       "APEI EINJ Trigger Table");
348 	if (!r) {
349 		pr_err(EINJ_PFX
350 "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
351 		       (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
352 		       (unsigned long long)trigger_paddr + table_size - 1);
353 		goto out_rel_header;
354 	}
355 	iounmap(trigger_tab);
356 	trigger_tab = ioremap_cache(trigger_paddr, table_size);
357 	if (!trigger_tab) {
358 		pr_err(EINJ_PFX "Failed to map trigger table!\n");
359 		goto out_rel_entry;
360 	}
361 	trigger_entry = (struct acpi_whea_header *)
362 		((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
363 	apei_resources_init(&trigger_resources);
364 	apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
365 			   ARRAY_SIZE(einj_ins_type),
366 			   trigger_entry, trigger_tab->entry_count);
367 	rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
368 	if (rc)
369 		goto out_fini;
370 	rc = apei_resources_sub(&trigger_resources, &einj_resources);
371 	if (rc)
372 		goto out_fini;
373 	/*
374 	 * Some firmware will access target address specified in
375 	 * param1 to trigger the error when injecting memory error.
376 	 * This will cause resource conflict with regular memory.  So
377 	 * remove it from trigger table resources.
378 	 */
379 	if (param_extension && (type & 0x0038) && param2) {
380 		struct apei_resources addr_resources;
381 		apei_resources_init(&addr_resources);
382 		trigger_param_region = einj_get_trigger_parameter_region(
383 			trigger_tab, param1, param2);
384 		if (trigger_param_region) {
385 			rc = apei_resources_add(&addr_resources,
386 				trigger_param_region->address,
387 				trigger_param_region->bit_width/8, true);
388 			if (rc)
389 				goto out_fini;
390 			rc = apei_resources_sub(&trigger_resources,
391 					&addr_resources);
392 		}
393 		apei_resources_fini(&addr_resources);
394 		if (rc)
395 			goto out_fini;
396 	}
397 	rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
398 	if (rc)
399 		goto out_fini;
400 	rc = apei_exec_pre_map_gars(&trigger_ctx);
401 	if (rc)
402 		goto out_release;
403 
404 	rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
405 
406 	apei_exec_post_unmap_gars(&trigger_ctx);
407 out_release:
408 	apei_resources_release(&trigger_resources);
409 out_fini:
410 	apei_resources_fini(&trigger_resources);
411 out_rel_entry:
412 	release_mem_region(trigger_paddr + sizeof(*trigger_tab),
413 			   table_size - sizeof(*trigger_tab));
414 out_rel_header:
415 	release_mem_region(trigger_paddr, sizeof(*trigger_tab));
416 out:
417 	if (trigger_tab)
418 		iounmap(trigger_tab);
419 
420 	return rc;
421 }
422 
423 static int __einj_error_inject(u32 type, u64 param1, u64 param2)
424 {
425 	struct apei_exec_context ctx;
426 	u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
427 	int rc;
428 
429 	einj_exec_ctx_init(&ctx);
430 
431 	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
432 	if (rc)
433 		return rc;
434 	apei_exec_ctx_set_input(&ctx, type);
435 	if (acpi5) {
436 		struct set_error_type_with_address *v5param = einj_param;
437 
438 		writel(type, &v5param->type);
439 		if (type & 0x80000000) {
440 			switch (vendor_flags) {
441 			case SETWA_FLAGS_APICID:
442 				writel(param1, &v5param->apicid);
443 				break;
444 			case SETWA_FLAGS_MEM:
445 				writeq(param1, &v5param->memory_address);
446 				writeq(param2, &v5param->memory_address_range);
447 				break;
448 			case SETWA_FLAGS_PCIE_SBDF:
449 				writel(param1, &v5param->pcie_sbdf);
450 				break;
451 			}
452 			writel(vendor_flags, &v5param->flags);
453 		} else {
454 			switch (type) {
455 			case ACPI_EINJ_PROCESSOR_CORRECTABLE:
456 			case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
457 			case ACPI_EINJ_PROCESSOR_FATAL:
458 				writel(param1, &v5param->apicid);
459 				writel(SETWA_FLAGS_APICID, &v5param->flags);
460 				break;
461 			case ACPI_EINJ_MEMORY_CORRECTABLE:
462 			case ACPI_EINJ_MEMORY_UNCORRECTABLE:
463 			case ACPI_EINJ_MEMORY_FATAL:
464 				writeq(param1, &v5param->memory_address);
465 				writeq(param2, &v5param->memory_address_range);
466 				writel(SETWA_FLAGS_MEM, &v5param->flags);
467 				break;
468 			case ACPI_EINJ_PCIX_CORRECTABLE:
469 			case ACPI_EINJ_PCIX_UNCORRECTABLE:
470 			case ACPI_EINJ_PCIX_FATAL:
471 				writel(param1, &v5param->pcie_sbdf);
472 				writel(SETWA_FLAGS_PCIE_SBDF, &v5param->flags);
473 				break;
474 			}
475 		}
476 	} else {
477 		rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
478 		if (rc)
479 			return rc;
480 		if (einj_param) {
481 			struct einj_parameter *v4param = einj_param;
482 			writeq(param1, &v4param->param1);
483 			writeq(param2, &v4param->param2);
484 		}
485 	}
486 	rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
487 	if (rc)
488 		return rc;
489 	for (;;) {
490 		rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
491 		if (rc)
492 			return rc;
493 		val = apei_exec_ctx_get_output(&ctx);
494 		if (!(val & EINJ_OP_BUSY))
495 			break;
496 		if (einj_timedout(&timeout))
497 			return -EIO;
498 	}
499 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
500 	if (rc)
501 		return rc;
502 	val = apei_exec_ctx_get_output(&ctx);
503 	if (val != EINJ_STATUS_SUCCESS)
504 		return -EBUSY;
505 
506 	rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
507 	if (rc)
508 		return rc;
509 	trigger_paddr = apei_exec_ctx_get_output(&ctx);
510 	rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
511 	if (rc)
512 		return rc;
513 	rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
514 
515 	return rc;
516 }
517 
518 /* Inject the specified hardware error */
519 static int einj_error_inject(u32 type, u64 param1, u64 param2)
520 {
521 	int rc;
522 
523 	mutex_lock(&einj_mutex);
524 	rc = __einj_error_inject(type, param1, param2);
525 	mutex_unlock(&einj_mutex);
526 
527 	return rc;
528 }
529 
530 static u32 error_type;
531 static u64 error_param1;
532 static u64 error_param2;
533 static struct dentry *einj_debug_dir;
534 
535 static int available_error_type_show(struct seq_file *m, void *v)
536 {
537 	int rc;
538 	u32 available_error_type = 0;
539 
540 	rc = einj_get_available_error_type(&available_error_type);
541 	if (rc)
542 		return rc;
543 	if (available_error_type & 0x0001)
544 		seq_printf(m, "0x00000001\tProcessor Correctable\n");
545 	if (available_error_type & 0x0002)
546 		seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
547 	if (available_error_type & 0x0004)
548 		seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
549 	if (available_error_type & 0x0008)
550 		seq_printf(m, "0x00000008\tMemory Correctable\n");
551 	if (available_error_type & 0x0010)
552 		seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
553 	if (available_error_type & 0x0020)
554 		seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
555 	if (available_error_type & 0x0040)
556 		seq_printf(m, "0x00000040\tPCI Express Correctable\n");
557 	if (available_error_type & 0x0080)
558 		seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
559 	if (available_error_type & 0x0100)
560 		seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
561 	if (available_error_type & 0x0200)
562 		seq_printf(m, "0x00000200\tPlatform Correctable\n");
563 	if (available_error_type & 0x0400)
564 		seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
565 	if (available_error_type & 0x0800)
566 		seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
567 
568 	return 0;
569 }
570 
571 static int available_error_type_open(struct inode *inode, struct file *file)
572 {
573 	return single_open(file, available_error_type_show, NULL);
574 }
575 
576 static const struct file_operations available_error_type_fops = {
577 	.open		= available_error_type_open,
578 	.read		= seq_read,
579 	.llseek		= seq_lseek,
580 	.release	= single_release,
581 };
582 
583 static int error_type_get(void *data, u64 *val)
584 {
585 	*val = error_type;
586 
587 	return 0;
588 }
589 
590 static int error_type_set(void *data, u64 val)
591 {
592 	int rc;
593 	u32 available_error_type = 0;
594 	u32 tval, vendor;
595 
596 	/*
597 	 * Vendor defined types have 0x80000000 bit set, and
598 	 * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
599 	 */
600 	vendor = val & 0x80000000;
601 	tval = val & 0x7fffffff;
602 
603 	/* Only one error type can be specified */
604 	if (tval & (tval - 1))
605 		return -EINVAL;
606 	if (!vendor) {
607 		rc = einj_get_available_error_type(&available_error_type);
608 		if (rc)
609 			return rc;
610 		if (!(val & available_error_type))
611 			return -EINVAL;
612 	}
613 	error_type = val;
614 
615 	return 0;
616 }
617 
618 DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get,
619 			error_type_set, "0x%llx\n");
620 
621 static int error_inject_set(void *data, u64 val)
622 {
623 	if (!error_type)
624 		return -EINVAL;
625 
626 	return einj_error_inject(error_type, error_param1, error_param2);
627 }
628 
629 DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL,
630 			error_inject_set, "%llu\n");
631 
632 static int einj_check_table(struct acpi_table_einj *einj_tab)
633 {
634 	if ((einj_tab->header_length !=
635 	     (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
636 	    && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
637 		return -EINVAL;
638 	if (einj_tab->header.length < sizeof(struct acpi_table_einj))
639 		return -EINVAL;
640 	if (einj_tab->entries !=
641 	    (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
642 	    sizeof(struct acpi_einj_entry))
643 		return -EINVAL;
644 
645 	return 0;
646 }
647 
648 static int __init einj_init(void)
649 {
650 	int rc;
651 	acpi_status status;
652 	struct dentry *fentry;
653 	struct apei_exec_context ctx;
654 
655 	if (acpi_disabled)
656 		return -ENODEV;
657 
658 	status = acpi_get_table(ACPI_SIG_EINJ, 0,
659 				(struct acpi_table_header **)&einj_tab);
660 	if (status == AE_NOT_FOUND)
661 		return -ENODEV;
662 	else if (ACPI_FAILURE(status)) {
663 		const char *msg = acpi_format_exception(status);
664 		pr_err(EINJ_PFX "Failed to get table, %s\n", msg);
665 		return -EINVAL;
666 	}
667 
668 	rc = einj_check_table(einj_tab);
669 	if (rc) {
670 		pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n");
671 		return -EINVAL;
672 	}
673 
674 	rc = -ENOMEM;
675 	einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
676 	if (!einj_debug_dir)
677 		goto err_cleanup;
678 	fentry = debugfs_create_file("available_error_type", S_IRUSR,
679 				     einj_debug_dir, NULL,
680 				     &available_error_type_fops);
681 	if (!fentry)
682 		goto err_cleanup;
683 	fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR,
684 				     einj_debug_dir, NULL, &error_type_fops);
685 	if (!fentry)
686 		goto err_cleanup;
687 	fentry = debugfs_create_file("error_inject", S_IWUSR,
688 				     einj_debug_dir, NULL, &error_inject_fops);
689 	if (!fentry)
690 		goto err_cleanup;
691 
692 	apei_resources_init(&einj_resources);
693 	einj_exec_ctx_init(&ctx);
694 	rc = apei_exec_collect_resources(&ctx, &einj_resources);
695 	if (rc)
696 		goto err_fini;
697 	rc = apei_resources_request(&einj_resources, "APEI EINJ");
698 	if (rc)
699 		goto err_fini;
700 	rc = apei_exec_pre_map_gars(&ctx);
701 	if (rc)
702 		goto err_release;
703 
704 	einj_param = einj_get_parameter_address();
705 	if ((param_extension || acpi5) && einj_param) {
706 		fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR,
707 					    einj_debug_dir, &error_param1);
708 		if (!fentry)
709 			goto err_unmap;
710 		fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR,
711 					    einj_debug_dir, &error_param2);
712 		if (!fentry)
713 			goto err_unmap;
714 	}
715 
716 	if (vendor_dev[0]) {
717 		vendor_blob.data = vendor_dev;
718 		vendor_blob.size = strlen(vendor_dev);
719 		fentry = debugfs_create_blob("vendor", S_IRUSR,
720 					     einj_debug_dir, &vendor_blob);
721 		if (!fentry)
722 			goto err_unmap;
723 		fentry = debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
724 					    einj_debug_dir, &vendor_flags);
725 		if (!fentry)
726 			goto err_unmap;
727 	}
728 
729 	pr_info(EINJ_PFX "Error INJection is initialized.\n");
730 
731 	return 0;
732 
733 err_unmap:
734 	if (einj_param)
735 		iounmap(einj_param);
736 	apei_exec_post_unmap_gars(&ctx);
737 err_release:
738 	apei_resources_release(&einj_resources);
739 err_fini:
740 	apei_resources_fini(&einj_resources);
741 err_cleanup:
742 	debugfs_remove_recursive(einj_debug_dir);
743 
744 	return rc;
745 }
746 
747 static void __exit einj_exit(void)
748 {
749 	struct apei_exec_context ctx;
750 
751 	if (einj_param)
752 		iounmap(einj_param);
753 	einj_exec_ctx_init(&ctx);
754 	apei_exec_post_unmap_gars(&ctx);
755 	apei_resources_release(&einj_resources);
756 	apei_resources_fini(&einj_resources);
757 	debugfs_remove_recursive(einj_debug_dir);
758 }
759 
760 module_init(einj_init);
761 module_exit(einj_exit);
762 
763 MODULE_AUTHOR("Huang Ying");
764 MODULE_DESCRIPTION("APEI Error INJection support");
765 MODULE_LICENSE("GPL");
766