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