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