einj.c 20 KB

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