Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 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 | * |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 10 | * Copyright 2009-2010 Intel Corp. |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 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 */ |
Stefan Weil | e8a8b25 | 2011-01-02 15:12:42 +0100 | [diff] [blame] | 42 | /* Firmware should respond within 1 milliseconds */ |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 43 | #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC) |
| 44 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 45 | /* |
| 46 | * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the |
| 47 | * EINJ table through an unpublished extension. Use with caution as |
| 48 | * most will ignore the parameter and make their own choice of address |
Huang Ying | c3e6088 | 2011-07-20 16:09:29 +0800 | [diff] [blame] | 49 | * for error injection. This extension is used only if |
| 50 | * param_extension module parameter is specified. |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 51 | */ |
| 52 | struct einj_parameter { |
| 53 | u64 type; |
| 54 | u64 reserved1; |
| 55 | u64 reserved2; |
| 56 | u64 param1; |
| 57 | u64 param2; |
| 58 | }; |
| 59 | |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 60 | #define EINJ_OP_BUSY 0x1 |
| 61 | #define EINJ_STATUS_SUCCESS 0x0 |
| 62 | #define EINJ_STATUS_FAIL 0x1 |
| 63 | #define EINJ_STATUS_INVAL 0x2 |
| 64 | |
| 65 | #define EINJ_TAB_ENTRY(tab) \ |
| 66 | ((struct acpi_whea_header *)((char *)(tab) + \ |
| 67 | sizeof(struct acpi_table_einj))) |
| 68 | |
Huang Ying | c3e6088 | 2011-07-20 16:09:29 +0800 | [diff] [blame] | 69 | static bool param_extension; |
| 70 | module_param(param_extension, bool, 0); |
| 71 | |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 72 | static struct acpi_table_einj *einj_tab; |
| 73 | |
| 74 | static struct apei_resources einj_resources; |
| 75 | |
| 76 | static struct apei_exec_ins_type einj_ins_type[] = { |
| 77 | [ACPI_EINJ_READ_REGISTER] = { |
| 78 | .flags = APEI_EXEC_INS_ACCESS_REGISTER, |
| 79 | .run = apei_exec_read_register, |
| 80 | }, |
| 81 | [ACPI_EINJ_READ_REGISTER_VALUE] = { |
| 82 | .flags = APEI_EXEC_INS_ACCESS_REGISTER, |
| 83 | .run = apei_exec_read_register_value, |
| 84 | }, |
| 85 | [ACPI_EINJ_WRITE_REGISTER] = { |
| 86 | .flags = APEI_EXEC_INS_ACCESS_REGISTER, |
| 87 | .run = apei_exec_write_register, |
| 88 | }, |
| 89 | [ACPI_EINJ_WRITE_REGISTER_VALUE] = { |
| 90 | .flags = APEI_EXEC_INS_ACCESS_REGISTER, |
| 91 | .run = apei_exec_write_register_value, |
| 92 | }, |
| 93 | [ACPI_EINJ_NOOP] = { |
| 94 | .flags = 0, |
| 95 | .run = apei_exec_noop, |
| 96 | }, |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Prevent EINJ interpreter to run simultaneously, because the |
| 101 | * corresponding firmware implementation may not work properly when |
| 102 | * invoked simultaneously. |
| 103 | */ |
| 104 | static DEFINE_MUTEX(einj_mutex); |
| 105 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 106 | static struct einj_parameter *einj_param; |
| 107 | |
Roland Dreier | dbee8a0 | 2011-05-24 17:13:09 -0700 | [diff] [blame] | 108 | #ifndef writeq |
| 109 | static inline void writeq(__u64 val, volatile void __iomem *addr) |
| 110 | { |
| 111 | writel(val, addr); |
| 112 | writel(val >> 32, addr+4); |
| 113 | } |
| 114 | #endif |
| 115 | |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 116 | static void einj_exec_ctx_init(struct apei_exec_context *ctx) |
| 117 | { |
| 118 | apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type), |
| 119 | EINJ_TAB_ENTRY(einj_tab), einj_tab->entries); |
| 120 | } |
| 121 | |
| 122 | static int __einj_get_available_error_type(u32 *type) |
| 123 | { |
| 124 | struct apei_exec_context ctx; |
| 125 | int rc; |
| 126 | |
| 127 | einj_exec_ctx_init(&ctx); |
| 128 | rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE); |
| 129 | if (rc) |
| 130 | return rc; |
| 131 | *type = apei_exec_ctx_get_output(&ctx); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* Get error injection capabilities of the platform */ |
| 137 | static int einj_get_available_error_type(u32 *type) |
| 138 | { |
| 139 | int rc; |
| 140 | |
| 141 | mutex_lock(&einj_mutex); |
| 142 | rc = __einj_get_available_error_type(type); |
| 143 | mutex_unlock(&einj_mutex); |
| 144 | |
| 145 | return rc; |
| 146 | } |
| 147 | |
| 148 | static int einj_timedout(u64 *t) |
| 149 | { |
| 150 | if ((s64)*t < SPIN_UNIT) { |
| 151 | pr_warning(FW_WARN EINJ_PFX |
| 152 | "Firmware does not respond in time\n"); |
| 153 | return 1; |
| 154 | } |
| 155 | *t -= SPIN_UNIT; |
| 156 | ndelay(SPIN_UNIT); |
| 157 | touch_nmi_watchdog(); |
| 158 | return 0; |
| 159 | } |
| 160 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 161 | static u64 einj_get_parameter_address(void) |
| 162 | { |
| 163 | int i; |
| 164 | u64 paddr = 0; |
| 165 | struct acpi_whea_header *entry; |
| 166 | |
| 167 | entry = EINJ_TAB_ENTRY(einj_tab); |
| 168 | for (i = 0; i < einj_tab->entries; i++) { |
| 169 | if (entry->action == ACPI_EINJ_SET_ERROR_TYPE && |
| 170 | entry->instruction == ACPI_EINJ_WRITE_REGISTER && |
| 171 | entry->register_region.space_id == |
| 172 | ACPI_ADR_SPACE_SYSTEM_MEMORY) |
| 173 | memcpy(&paddr, &entry->register_region.address, |
| 174 | sizeof(paddr)); |
| 175 | entry++; |
| 176 | } |
| 177 | |
| 178 | return paddr; |
| 179 | } |
| 180 | |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 181 | /* do sanity check to trigger table */ |
| 182 | static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab) |
| 183 | { |
| 184 | if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger)) |
| 185 | return -EINVAL; |
| 186 | if (trigger_tab->table_size > PAGE_SIZE || |
| 187 | trigger_tab->table_size <= trigger_tab->header_size) |
| 188 | return -EINVAL; |
| 189 | if (trigger_tab->entry_count != |
| 190 | (trigger_tab->table_size - trigger_tab->header_size) / |
| 191 | sizeof(struct acpi_einj_entry)) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* Execute instructions in trigger error action table */ |
| 198 | static int __einj_error_trigger(u64 trigger_paddr) |
| 199 | { |
| 200 | struct acpi_einj_trigger *trigger_tab = NULL; |
| 201 | struct apei_exec_context trigger_ctx; |
| 202 | struct apei_resources trigger_resources; |
| 203 | struct acpi_whea_header *trigger_entry; |
| 204 | struct resource *r; |
| 205 | u32 table_size; |
| 206 | int rc = -EIO; |
| 207 | |
| 208 | r = request_mem_region(trigger_paddr, sizeof(*trigger_tab), |
| 209 | "APEI EINJ Trigger Table"); |
| 210 | if (!r) { |
| 211 | pr_err(EINJ_PFX |
Bjorn Helgaas | 46b91e3 | 2011-12-08 11:25:42 +0800 | [diff] [blame^] | 212 | "Can not request [mem %#010llx-%#010llx] for Trigger table\n", |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 213 | (unsigned long long)trigger_paddr, |
Bjorn Helgaas | 46b91e3 | 2011-12-08 11:25:42 +0800 | [diff] [blame^] | 214 | (unsigned long long)trigger_paddr + |
| 215 | sizeof(*trigger_tab) - 1); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 216 | goto out; |
| 217 | } |
| 218 | trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab)); |
| 219 | if (!trigger_tab) { |
| 220 | pr_err(EINJ_PFX "Failed to map trigger table!\n"); |
| 221 | goto out_rel_header; |
| 222 | } |
| 223 | rc = einj_check_trigger_header(trigger_tab); |
| 224 | if (rc) { |
| 225 | pr_warning(FW_BUG EINJ_PFX |
| 226 | "The trigger error action table is invalid\n"); |
| 227 | goto out_rel_header; |
| 228 | } |
| 229 | rc = -EIO; |
| 230 | table_size = trigger_tab->table_size; |
| 231 | r = request_mem_region(trigger_paddr + sizeof(*trigger_tab), |
| 232 | table_size - sizeof(*trigger_tab), |
| 233 | "APEI EINJ Trigger Table"); |
| 234 | if (!r) { |
| 235 | pr_err(EINJ_PFX |
Bjorn Helgaas | 46b91e3 | 2011-12-08 11:25:42 +0800 | [diff] [blame^] | 236 | "Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n", |
| 237 | (unsigned long long)trigger_paddr + sizeof(*trigger_tab), |
| 238 | (unsigned long long)trigger_paddr + table_size - 1); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 239 | goto out_rel_header; |
| 240 | } |
| 241 | iounmap(trigger_tab); |
| 242 | trigger_tab = ioremap_cache(trigger_paddr, table_size); |
| 243 | if (!trigger_tab) { |
| 244 | pr_err(EINJ_PFX "Failed to map trigger table!\n"); |
| 245 | goto out_rel_entry; |
| 246 | } |
| 247 | trigger_entry = (struct acpi_whea_header *) |
| 248 | ((char *)trigger_tab + sizeof(struct acpi_einj_trigger)); |
| 249 | apei_resources_init(&trigger_resources); |
| 250 | apei_exec_ctx_init(&trigger_ctx, einj_ins_type, |
| 251 | ARRAY_SIZE(einj_ins_type), |
| 252 | trigger_entry, trigger_tab->entry_count); |
| 253 | rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources); |
| 254 | if (rc) |
| 255 | goto out_fini; |
| 256 | rc = apei_resources_sub(&trigger_resources, &einj_resources); |
| 257 | if (rc) |
| 258 | goto out_fini; |
| 259 | rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger"); |
| 260 | if (rc) |
| 261 | goto out_fini; |
| 262 | rc = apei_exec_pre_map_gars(&trigger_ctx); |
| 263 | if (rc) |
| 264 | goto out_release; |
| 265 | |
| 266 | rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR); |
| 267 | |
| 268 | apei_exec_post_unmap_gars(&trigger_ctx); |
| 269 | out_release: |
| 270 | apei_resources_release(&trigger_resources); |
| 271 | out_fini: |
| 272 | apei_resources_fini(&trigger_resources); |
| 273 | out_rel_entry: |
| 274 | release_mem_region(trigger_paddr + sizeof(*trigger_tab), |
| 275 | table_size - sizeof(*trigger_tab)); |
| 276 | out_rel_header: |
| 277 | release_mem_region(trigger_paddr, sizeof(*trigger_tab)); |
| 278 | out: |
| 279 | if (trigger_tab) |
| 280 | iounmap(trigger_tab); |
| 281 | |
| 282 | return rc; |
| 283 | } |
| 284 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 285 | static int __einj_error_inject(u32 type, u64 param1, u64 param2) |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 286 | { |
| 287 | struct apei_exec_context ctx; |
| 288 | u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT; |
| 289 | int rc; |
| 290 | |
| 291 | einj_exec_ctx_init(&ctx); |
| 292 | |
Huang Ying | 392913d | 2011-07-13 13:14:17 +0800 | [diff] [blame] | 293 | rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 294 | if (rc) |
| 295 | return rc; |
| 296 | apei_exec_ctx_set_input(&ctx, type); |
| 297 | rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE); |
| 298 | if (rc) |
| 299 | return rc; |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 300 | if (einj_param) { |
| 301 | writeq(param1, &einj_param->param1); |
| 302 | writeq(param2, &einj_param->param2); |
| 303 | } |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 304 | rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION); |
| 305 | if (rc) |
| 306 | return rc; |
| 307 | for (;;) { |
| 308 | rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS); |
| 309 | if (rc) |
| 310 | return rc; |
| 311 | val = apei_exec_ctx_get_output(&ctx); |
| 312 | if (!(val & EINJ_OP_BUSY)) |
| 313 | break; |
| 314 | if (einj_timedout(&timeout)) |
| 315 | return -EIO; |
| 316 | } |
| 317 | rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS); |
| 318 | if (rc) |
| 319 | return rc; |
| 320 | val = apei_exec_ctx_get_output(&ctx); |
| 321 | if (val != EINJ_STATUS_SUCCESS) |
| 322 | return -EBUSY; |
| 323 | |
| 324 | rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE); |
| 325 | if (rc) |
| 326 | return rc; |
| 327 | trigger_paddr = apei_exec_ctx_get_output(&ctx); |
| 328 | rc = __einj_error_trigger(trigger_paddr); |
| 329 | if (rc) |
| 330 | return rc; |
Huang Ying | 392913d | 2011-07-13 13:14:17 +0800 | [diff] [blame] | 331 | rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 332 | |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | /* Inject the specified hardware error */ |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 337 | static int einj_error_inject(u32 type, u64 param1, u64 param2) |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 338 | { |
| 339 | int rc; |
| 340 | |
| 341 | mutex_lock(&einj_mutex); |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 342 | rc = __einj_error_inject(type, param1, param2); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 343 | mutex_unlock(&einj_mutex); |
| 344 | |
| 345 | return rc; |
| 346 | } |
| 347 | |
| 348 | static u32 error_type; |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 349 | static u64 error_param1; |
| 350 | static u64 error_param2; |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 351 | static struct dentry *einj_debug_dir; |
| 352 | |
| 353 | static int available_error_type_show(struct seq_file *m, void *v) |
| 354 | { |
| 355 | int rc; |
| 356 | u32 available_error_type = 0; |
| 357 | |
| 358 | rc = einj_get_available_error_type(&available_error_type); |
| 359 | if (rc) |
| 360 | return rc; |
| 361 | if (available_error_type & 0x0001) |
| 362 | seq_printf(m, "0x00000001\tProcessor Correctable\n"); |
| 363 | if (available_error_type & 0x0002) |
| 364 | seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n"); |
| 365 | if (available_error_type & 0x0004) |
| 366 | seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n"); |
| 367 | if (available_error_type & 0x0008) |
| 368 | seq_printf(m, "0x00000008\tMemory Correctable\n"); |
| 369 | if (available_error_type & 0x0010) |
| 370 | seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n"); |
| 371 | if (available_error_type & 0x0020) |
| 372 | seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n"); |
| 373 | if (available_error_type & 0x0040) |
| 374 | seq_printf(m, "0x00000040\tPCI Express Correctable\n"); |
| 375 | if (available_error_type & 0x0080) |
| 376 | seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n"); |
| 377 | if (available_error_type & 0x0100) |
| 378 | seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n"); |
| 379 | if (available_error_type & 0x0200) |
| 380 | seq_printf(m, "0x00000200\tPlatform Correctable\n"); |
| 381 | if (available_error_type & 0x0400) |
| 382 | seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n"); |
| 383 | if (available_error_type & 0x0800) |
| 384 | seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n"); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int available_error_type_open(struct inode *inode, struct file *file) |
| 390 | { |
| 391 | return single_open(file, available_error_type_show, NULL); |
| 392 | } |
| 393 | |
| 394 | static const struct file_operations available_error_type_fops = { |
| 395 | .open = available_error_type_open, |
| 396 | .read = seq_read, |
| 397 | .llseek = seq_lseek, |
| 398 | .release = single_release, |
| 399 | }; |
| 400 | |
| 401 | static int error_type_get(void *data, u64 *val) |
| 402 | { |
| 403 | *val = error_type; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int error_type_set(void *data, u64 val) |
| 409 | { |
| 410 | int rc; |
| 411 | u32 available_error_type = 0; |
| 412 | |
| 413 | /* Only one error type can be specified */ |
| 414 | if (val & (val - 1)) |
| 415 | return -EINVAL; |
| 416 | rc = einj_get_available_error_type(&available_error_type); |
| 417 | if (rc) |
| 418 | return rc; |
| 419 | if (!(val & available_error_type)) |
| 420 | return -EINVAL; |
| 421 | error_type = val; |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | DEFINE_SIMPLE_ATTRIBUTE(error_type_fops, error_type_get, |
| 427 | error_type_set, "0x%llx\n"); |
| 428 | |
| 429 | static int error_inject_set(void *data, u64 val) |
| 430 | { |
| 431 | if (!error_type) |
| 432 | return -EINVAL; |
| 433 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 434 | return einj_error_inject(error_type, error_param1, error_param2); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | DEFINE_SIMPLE_ATTRIBUTE(error_inject_fops, NULL, |
| 438 | error_inject_set, "%llu\n"); |
| 439 | |
| 440 | static int einj_check_table(struct acpi_table_einj *einj_tab) |
| 441 | { |
Huang Ying | 3a78f96 | 2010-09-29 19:53:51 +0800 | [diff] [blame] | 442 | if ((einj_tab->header_length != |
| 443 | (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header))) |
| 444 | && (einj_tab->header_length != sizeof(struct acpi_table_einj))) |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 445 | return -EINVAL; |
| 446 | if (einj_tab->header.length < sizeof(struct acpi_table_einj)) |
| 447 | return -EINVAL; |
| 448 | if (einj_tab->entries != |
| 449 | (einj_tab->header.length - sizeof(struct acpi_table_einj)) / |
| 450 | sizeof(struct acpi_einj_entry)) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int __init einj_init(void) |
| 457 | { |
| 458 | int rc; |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 459 | u64 param_paddr; |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 460 | acpi_status status; |
| 461 | struct dentry *fentry; |
| 462 | struct apei_exec_context ctx; |
| 463 | |
| 464 | if (acpi_disabled) |
| 465 | return -ENODEV; |
| 466 | |
| 467 | status = acpi_get_table(ACPI_SIG_EINJ, 0, |
| 468 | (struct acpi_table_header **)&einj_tab); |
| 469 | if (status == AE_NOT_FOUND) { |
| 470 | pr_info(EINJ_PFX "Table is not found!\n"); |
| 471 | return -ENODEV; |
| 472 | } else if (ACPI_FAILURE(status)) { |
| 473 | const char *msg = acpi_format_exception(status); |
| 474 | pr_err(EINJ_PFX "Failed to get table, %s\n", msg); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | |
| 478 | rc = einj_check_table(einj_tab); |
| 479 | if (rc) { |
| 480 | pr_warning(FW_BUG EINJ_PFX "EINJ table is invalid\n"); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
| 484 | rc = -ENOMEM; |
| 485 | einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir()); |
| 486 | if (!einj_debug_dir) |
| 487 | goto err_cleanup; |
| 488 | fentry = debugfs_create_file("available_error_type", S_IRUSR, |
| 489 | einj_debug_dir, NULL, |
| 490 | &available_error_type_fops); |
| 491 | if (!fentry) |
| 492 | goto err_cleanup; |
| 493 | fentry = debugfs_create_file("error_type", S_IRUSR | S_IWUSR, |
| 494 | einj_debug_dir, NULL, &error_type_fops); |
| 495 | if (!fentry) |
| 496 | goto err_cleanup; |
| 497 | fentry = debugfs_create_file("error_inject", S_IWUSR, |
| 498 | einj_debug_dir, NULL, &error_inject_fops); |
| 499 | if (!fentry) |
| 500 | goto err_cleanup; |
| 501 | |
| 502 | apei_resources_init(&einj_resources); |
| 503 | einj_exec_ctx_init(&ctx); |
| 504 | rc = apei_exec_collect_resources(&ctx, &einj_resources); |
| 505 | if (rc) |
| 506 | goto err_fini; |
| 507 | rc = apei_resources_request(&einj_resources, "APEI EINJ"); |
| 508 | if (rc) |
| 509 | goto err_fini; |
| 510 | rc = apei_exec_pre_map_gars(&ctx); |
| 511 | if (rc) |
| 512 | goto err_release; |
Huang Ying | c3e6088 | 2011-07-20 16:09:29 +0800 | [diff] [blame] | 513 | if (param_extension) { |
| 514 | param_paddr = einj_get_parameter_address(); |
| 515 | if (param_paddr) { |
| 516 | einj_param = ioremap(param_paddr, sizeof(*einj_param)); |
| 517 | rc = -ENOMEM; |
| 518 | if (!einj_param) |
| 519 | goto err_unmap; |
| 520 | fentry = debugfs_create_x64("param1", S_IRUSR | S_IWUSR, |
| 521 | einj_debug_dir, &error_param1); |
| 522 | if (!fentry) |
| 523 | goto err_unmap; |
| 524 | fentry = debugfs_create_x64("param2", S_IRUSR | S_IWUSR, |
| 525 | einj_debug_dir, &error_param2); |
| 526 | if (!fentry) |
| 527 | goto err_unmap; |
| 528 | } else |
| 529 | pr_warn(EINJ_PFX "Parameter extension is not supported.\n"); |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 530 | } |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 531 | |
| 532 | pr_info(EINJ_PFX "Error INJection is initialized.\n"); |
| 533 | |
| 534 | return 0; |
| 535 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 536 | err_unmap: |
Huang Ying | c3e6088 | 2011-07-20 16:09:29 +0800 | [diff] [blame] | 537 | if (einj_param) |
| 538 | iounmap(einj_param); |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 539 | apei_exec_post_unmap_gars(&ctx); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 540 | err_release: |
| 541 | apei_resources_release(&einj_resources); |
| 542 | err_fini: |
| 543 | apei_resources_fini(&einj_resources); |
| 544 | err_cleanup: |
| 545 | debugfs_remove_recursive(einj_debug_dir); |
| 546 | |
| 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | static void __exit einj_exit(void) |
| 551 | { |
| 552 | struct apei_exec_context ctx; |
| 553 | |
Huang Ying | 6e320ec | 2010-05-18 14:35:24 +0800 | [diff] [blame] | 554 | if (einj_param) |
| 555 | iounmap(einj_param); |
Huang Ying | e402134 | 2010-05-18 14:35:14 +0800 | [diff] [blame] | 556 | einj_exec_ctx_init(&ctx); |
| 557 | apei_exec_post_unmap_gars(&ctx); |
| 558 | apei_resources_release(&einj_resources); |
| 559 | apei_resources_fini(&einj_resources); |
| 560 | debugfs_remove_recursive(einj_debug_dir); |
| 561 | } |
| 562 | |
| 563 | module_init(einj_init); |
| 564 | module_exit(einj_exit); |
| 565 | |
| 566 | MODULE_AUTHOR("Huang Ying"); |
| 567 | MODULE_DESCRIPTION("APEI Error INJection support"); |
| 568 | MODULE_LICENSE("GPL"); |