erst.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * APEI Error Record Serialization Table support
  3. *
  4. * ERST is a way provided by APEI to save and retrieve hardware error
  5. * information to and from a persistent store.
  6. *
  7. * For more information about ERST, please refer to ACPI Specification
  8. * version 4.0, section 17.4.
  9. *
  10. * Copyright 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/delay.h>
  30. #include <linux/io.h>
  31. #include <linux/acpi.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/cper.h>
  34. #include <linux/nmi.h>
  35. #include <linux/hardirq.h>
  36. #include <acpi/apei.h>
  37. #include "apei-internal.h"
  38. #define ERST_PFX "ERST: "
  39. /* ERST command status */
  40. #define ERST_STATUS_SUCCESS 0x0
  41. #define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
  42. #define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
  43. #define ERST_STATUS_FAILED 0x3
  44. #define ERST_STATUS_RECORD_STORE_EMPTY 0x4
  45. #define ERST_STATUS_RECORD_NOT_FOUND 0x5
  46. #define ERST_TAB_ENTRY(tab) \
  47. ((struct acpi_whea_header *)((char *)(tab) + \
  48. sizeof(struct acpi_table_erst)))
  49. #define SPIN_UNIT 100 /* 100ns */
  50. /* Firmware should respond within 1 miliseconds */
  51. #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
  52. #define FIRMWARE_MAX_STALL 50 /* 50us */
  53. int erst_disable;
  54. EXPORT_SYMBOL_GPL(erst_disable);
  55. static struct acpi_table_erst *erst_tab;
  56. /* ERST Error Log Address Range atrributes */
  57. #define ERST_RANGE_RESERVED 0x0001
  58. #define ERST_RANGE_NVRAM 0x0002
  59. #define ERST_RANGE_SLOW 0x0004
  60. /*
  61. * ERST Error Log Address Range, used as buffer for reading/writing
  62. * error records.
  63. */
  64. static struct erst_erange {
  65. u64 base;
  66. u64 size;
  67. void __iomem *vaddr;
  68. u32 attr;
  69. } erst_erange;
  70. /*
  71. * Prevent ERST interpreter to run simultaneously, because the
  72. * corresponding firmware implementation may not work properly when
  73. * invoked simultaneously.
  74. *
  75. * It is used to provide exclusive accessing for ERST Error Log
  76. * Address Range too.
  77. */
  78. static DEFINE_RAW_SPINLOCK(erst_lock);
  79. static inline int erst_errno(int command_status)
  80. {
  81. switch (command_status) {
  82. case ERST_STATUS_SUCCESS:
  83. return 0;
  84. case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
  85. return -ENODEV;
  86. case ERST_STATUS_NOT_ENOUGH_SPACE:
  87. return -ENOSPC;
  88. case ERST_STATUS_RECORD_STORE_EMPTY:
  89. case ERST_STATUS_RECORD_NOT_FOUND:
  90. return -ENOENT;
  91. default:
  92. return -EINVAL;
  93. }
  94. }
  95. static int erst_timedout(u64 *t, u64 spin_unit)
  96. {
  97. if ((s64)*t < spin_unit) {
  98. pr_warning(FW_WARN ERST_PFX
  99. "Firmware does not respond in time\n");
  100. return 1;
  101. }
  102. *t -= spin_unit;
  103. ndelay(spin_unit);
  104. touch_nmi_watchdog();
  105. return 0;
  106. }
  107. static int erst_exec_load_var1(struct apei_exec_context *ctx,
  108. struct acpi_whea_header *entry)
  109. {
  110. return __apei_exec_read_register(entry, &ctx->var1);
  111. }
  112. static int erst_exec_load_var2(struct apei_exec_context *ctx,
  113. struct acpi_whea_header *entry)
  114. {
  115. return __apei_exec_read_register(entry, &ctx->var2);
  116. }
  117. static int erst_exec_store_var1(struct apei_exec_context *ctx,
  118. struct acpi_whea_header *entry)
  119. {
  120. return __apei_exec_write_register(entry, ctx->var1);
  121. }
  122. static int erst_exec_add(struct apei_exec_context *ctx,
  123. struct acpi_whea_header *entry)
  124. {
  125. ctx->var1 += ctx->var2;
  126. return 0;
  127. }
  128. static int erst_exec_subtract(struct apei_exec_context *ctx,
  129. struct acpi_whea_header *entry)
  130. {
  131. ctx->var1 -= ctx->var2;
  132. return 0;
  133. }
  134. static int erst_exec_add_value(struct apei_exec_context *ctx,
  135. struct acpi_whea_header *entry)
  136. {
  137. int rc;
  138. u64 val;
  139. rc = __apei_exec_read_register(entry, &val);
  140. if (rc)
  141. return rc;
  142. val += ctx->value;
  143. rc = __apei_exec_write_register(entry, val);
  144. return rc;
  145. }
  146. static int erst_exec_subtract_value(struct apei_exec_context *ctx,
  147. struct acpi_whea_header *entry)
  148. {
  149. int rc;
  150. u64 val;
  151. rc = __apei_exec_read_register(entry, &val);
  152. if (rc)
  153. return rc;
  154. val -= ctx->value;
  155. rc = __apei_exec_write_register(entry, val);
  156. return rc;
  157. }
  158. static int erst_exec_stall(struct apei_exec_context *ctx,
  159. struct acpi_whea_header *entry)
  160. {
  161. u64 stall_time;
  162. if (ctx->value > FIRMWARE_MAX_STALL) {
  163. if (!in_nmi())
  164. pr_warning(FW_WARN ERST_PFX
  165. "Too long stall time for stall instruction: %llx.\n",
  166. ctx->value);
  167. stall_time = FIRMWARE_MAX_STALL;
  168. } else
  169. stall_time = ctx->value;
  170. udelay(stall_time);
  171. return 0;
  172. }
  173. static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
  174. struct acpi_whea_header *entry)
  175. {
  176. int rc;
  177. u64 val;
  178. u64 timeout = FIRMWARE_TIMEOUT;
  179. u64 stall_time;
  180. if (ctx->var1 > FIRMWARE_MAX_STALL) {
  181. if (!in_nmi())
  182. pr_warning(FW_WARN ERST_PFX
  183. "Too long stall time for stall while true instruction: %llx.\n",
  184. ctx->var1);
  185. stall_time = FIRMWARE_MAX_STALL;
  186. } else
  187. stall_time = ctx->var1;
  188. for (;;) {
  189. rc = __apei_exec_read_register(entry, &val);
  190. if (rc)
  191. return rc;
  192. if (val != ctx->value)
  193. break;
  194. if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
  195. return -EIO;
  196. }
  197. return 0;
  198. }
  199. static int erst_exec_skip_next_instruction_if_true(
  200. struct apei_exec_context *ctx,
  201. struct acpi_whea_header *entry)
  202. {
  203. int rc;
  204. u64 val;
  205. rc = __apei_exec_read_register(entry, &val);
  206. if (rc)
  207. return rc;
  208. if (val == ctx->value) {
  209. ctx->ip += 2;
  210. return APEI_EXEC_SET_IP;
  211. }
  212. return 0;
  213. }
  214. static int erst_exec_goto(struct apei_exec_context *ctx,
  215. struct acpi_whea_header *entry)
  216. {
  217. ctx->ip = ctx->value;
  218. return APEI_EXEC_SET_IP;
  219. }
  220. static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
  221. struct acpi_whea_header *entry)
  222. {
  223. return __apei_exec_read_register(entry, &ctx->src_base);
  224. }
  225. static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
  226. struct acpi_whea_header *entry)
  227. {
  228. return __apei_exec_read_register(entry, &ctx->dst_base);
  229. }
  230. static int erst_exec_move_data(struct apei_exec_context *ctx,
  231. struct acpi_whea_header *entry)
  232. {
  233. int rc;
  234. u64 offset;
  235. void *src, *dst;
  236. /* ioremap does not work in interrupt context */
  237. if (in_interrupt()) {
  238. pr_warning(ERST_PFX
  239. "MOVE_DATA can not be used in interrupt context");
  240. return -EBUSY;
  241. }
  242. rc = __apei_exec_read_register(entry, &offset);
  243. if (rc)
  244. return rc;
  245. src = ioremap(ctx->src_base + offset, ctx->var2);
  246. if (!src)
  247. return -ENOMEM;
  248. dst = ioremap(ctx->dst_base + offset, ctx->var2);
  249. if (!dst)
  250. return -ENOMEM;
  251. memmove(dst, src, ctx->var2);
  252. iounmap(src);
  253. iounmap(dst);
  254. return 0;
  255. }
  256. static struct apei_exec_ins_type erst_ins_type[] = {
  257. [ACPI_ERST_READ_REGISTER] = {
  258. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  259. .run = apei_exec_read_register,
  260. },
  261. [ACPI_ERST_READ_REGISTER_VALUE] = {
  262. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  263. .run = apei_exec_read_register_value,
  264. },
  265. [ACPI_ERST_WRITE_REGISTER] = {
  266. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  267. .run = apei_exec_write_register,
  268. },
  269. [ACPI_ERST_WRITE_REGISTER_VALUE] = {
  270. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  271. .run = apei_exec_write_register_value,
  272. },
  273. [ACPI_ERST_NOOP] = {
  274. .flags = 0,
  275. .run = apei_exec_noop,
  276. },
  277. [ACPI_ERST_LOAD_VAR1] = {
  278. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  279. .run = erst_exec_load_var1,
  280. },
  281. [ACPI_ERST_LOAD_VAR2] = {
  282. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  283. .run = erst_exec_load_var2,
  284. },
  285. [ACPI_ERST_STORE_VAR1] = {
  286. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  287. .run = erst_exec_store_var1,
  288. },
  289. [ACPI_ERST_ADD] = {
  290. .flags = 0,
  291. .run = erst_exec_add,
  292. },
  293. [ACPI_ERST_SUBTRACT] = {
  294. .flags = 0,
  295. .run = erst_exec_subtract,
  296. },
  297. [ACPI_ERST_ADD_VALUE] = {
  298. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  299. .run = erst_exec_add_value,
  300. },
  301. [ACPI_ERST_SUBTRACT_VALUE] = {
  302. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  303. .run = erst_exec_subtract_value,
  304. },
  305. [ACPI_ERST_STALL] = {
  306. .flags = 0,
  307. .run = erst_exec_stall,
  308. },
  309. [ACPI_ERST_STALL_WHILE_TRUE] = {
  310. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  311. .run = erst_exec_stall_while_true,
  312. },
  313. [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
  314. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  315. .run = erst_exec_skip_next_instruction_if_true,
  316. },
  317. [ACPI_ERST_GOTO] = {
  318. .flags = 0,
  319. .run = erst_exec_goto,
  320. },
  321. [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
  322. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  323. .run = erst_exec_set_src_address_base,
  324. },
  325. [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
  326. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  327. .run = erst_exec_set_dst_address_base,
  328. },
  329. [ACPI_ERST_MOVE_DATA] = {
  330. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  331. .run = erst_exec_move_data,
  332. },
  333. };
  334. static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
  335. {
  336. apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
  337. ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
  338. }
  339. static int erst_get_erange(struct erst_erange *range)
  340. {
  341. struct apei_exec_context ctx;
  342. int rc;
  343. erst_exec_ctx_init(&ctx);
  344. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
  345. if (rc)
  346. return rc;
  347. range->base = apei_exec_ctx_get_output(&ctx);
  348. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
  349. if (rc)
  350. return rc;
  351. range->size = apei_exec_ctx_get_output(&ctx);
  352. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
  353. if (rc)
  354. return rc;
  355. range->attr = apei_exec_ctx_get_output(&ctx);
  356. return 0;
  357. }
  358. static ssize_t __erst_get_record_count(void)
  359. {
  360. struct apei_exec_context ctx;
  361. int rc;
  362. erst_exec_ctx_init(&ctx);
  363. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
  364. if (rc)
  365. return rc;
  366. return apei_exec_ctx_get_output(&ctx);
  367. }
  368. ssize_t erst_get_record_count(void)
  369. {
  370. ssize_t count;
  371. unsigned long flags;
  372. if (erst_disable)
  373. return -ENODEV;
  374. raw_spin_lock_irqsave(&erst_lock, flags);
  375. count = __erst_get_record_count();
  376. raw_spin_unlock_irqrestore(&erst_lock, flags);
  377. return count;
  378. }
  379. EXPORT_SYMBOL_GPL(erst_get_record_count);
  380. static int __erst_get_next_record_id(u64 *record_id)
  381. {
  382. struct apei_exec_context ctx;
  383. int rc;
  384. erst_exec_ctx_init(&ctx);
  385. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
  386. if (rc)
  387. return rc;
  388. *record_id = apei_exec_ctx_get_output(&ctx);
  389. return 0;
  390. }
  391. /*
  392. * Get the record ID of an existing error record on the persistent
  393. * storage. If there is no error record on the persistent storage, the
  394. * returned record_id is APEI_ERST_INVALID_RECORD_ID.
  395. */
  396. int erst_get_next_record_id(u64 *record_id)
  397. {
  398. int rc;
  399. unsigned long flags;
  400. if (erst_disable)
  401. return -ENODEV;
  402. raw_spin_lock_irqsave(&erst_lock, flags);
  403. rc = __erst_get_next_record_id(record_id);
  404. raw_spin_unlock_irqrestore(&erst_lock, flags);
  405. return rc;
  406. }
  407. EXPORT_SYMBOL_GPL(erst_get_next_record_id);
  408. static int __erst_write_to_storage(u64 offset)
  409. {
  410. struct apei_exec_context ctx;
  411. u64 timeout = FIRMWARE_TIMEOUT;
  412. u64 val;
  413. int rc;
  414. erst_exec_ctx_init(&ctx);
  415. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_WRITE);
  416. if (rc)
  417. return rc;
  418. apei_exec_ctx_set_input(&ctx, offset);
  419. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  420. if (rc)
  421. return rc;
  422. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  423. if (rc)
  424. return rc;
  425. for (;;) {
  426. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  427. if (rc)
  428. return rc;
  429. val = apei_exec_ctx_get_output(&ctx);
  430. if (!val)
  431. break;
  432. if (erst_timedout(&timeout, SPIN_UNIT))
  433. return -EIO;
  434. }
  435. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  436. if (rc)
  437. return rc;
  438. val = apei_exec_ctx_get_output(&ctx);
  439. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  440. if (rc)
  441. return rc;
  442. return erst_errno(val);
  443. }
  444. static int __erst_read_from_storage(u64 record_id, u64 offset)
  445. {
  446. struct apei_exec_context ctx;
  447. u64 timeout = FIRMWARE_TIMEOUT;
  448. u64 val;
  449. int rc;
  450. erst_exec_ctx_init(&ctx);
  451. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_READ);
  452. if (rc)
  453. return rc;
  454. apei_exec_ctx_set_input(&ctx, offset);
  455. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  456. if (rc)
  457. return rc;
  458. apei_exec_ctx_set_input(&ctx, record_id);
  459. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  460. if (rc)
  461. return rc;
  462. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  463. if (rc)
  464. return rc;
  465. for (;;) {
  466. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  467. if (rc)
  468. return rc;
  469. val = apei_exec_ctx_get_output(&ctx);
  470. if (!val)
  471. break;
  472. if (erst_timedout(&timeout, SPIN_UNIT))
  473. return -EIO;
  474. };
  475. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  476. if (rc)
  477. return rc;
  478. val = apei_exec_ctx_get_output(&ctx);
  479. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  480. if (rc)
  481. return rc;
  482. return erst_errno(val);
  483. }
  484. static int __erst_clear_from_storage(u64 record_id)
  485. {
  486. struct apei_exec_context ctx;
  487. u64 timeout = FIRMWARE_TIMEOUT;
  488. u64 val;
  489. int rc;
  490. erst_exec_ctx_init(&ctx);
  491. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_CLEAR);
  492. if (rc)
  493. return rc;
  494. apei_exec_ctx_set_input(&ctx, record_id);
  495. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  496. if (rc)
  497. return rc;
  498. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  499. if (rc)
  500. return rc;
  501. for (;;) {
  502. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  503. if (rc)
  504. return rc;
  505. val = apei_exec_ctx_get_output(&ctx);
  506. if (!val)
  507. break;
  508. if (erst_timedout(&timeout, SPIN_UNIT))
  509. return -EIO;
  510. }
  511. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  512. if (rc)
  513. return rc;
  514. val = apei_exec_ctx_get_output(&ctx);
  515. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  516. if (rc)
  517. return rc;
  518. return erst_errno(val);
  519. }
  520. /* NVRAM ERST Error Log Address Range is not supported yet */
  521. static void pr_unimpl_nvram(void)
  522. {
  523. if (printk_ratelimit())
  524. pr_warning(ERST_PFX
  525. "NVRAM ERST Log Address Range is not implemented yet\n");
  526. }
  527. static int __erst_write_to_nvram(const struct cper_record_header *record)
  528. {
  529. /* do not print message, because printk is not safe for NMI */
  530. return -ENOSYS;
  531. }
  532. static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
  533. {
  534. pr_unimpl_nvram();
  535. return -ENOSYS;
  536. }
  537. static int __erst_clear_from_nvram(u64 record_id)
  538. {
  539. pr_unimpl_nvram();
  540. return -ENOSYS;
  541. }
  542. int erst_write(const struct cper_record_header *record)
  543. {
  544. int rc;
  545. unsigned long flags;
  546. struct cper_record_header *rcd_erange;
  547. if (erst_disable)
  548. return -ENODEV;
  549. if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
  550. return -EINVAL;
  551. if (erst_erange.attr & ERST_RANGE_NVRAM) {
  552. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  553. return -EBUSY;
  554. rc = __erst_write_to_nvram(record);
  555. raw_spin_unlock_irqrestore(&erst_lock, flags);
  556. return rc;
  557. }
  558. if (record->record_length > erst_erange.size)
  559. return -EINVAL;
  560. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  561. return -EBUSY;
  562. memcpy(erst_erange.vaddr, record, record->record_length);
  563. rcd_erange = erst_erange.vaddr;
  564. /* signature for serialization system */
  565. memcpy(&rcd_erange->persistence_information, "ER", 2);
  566. rc = __erst_write_to_storage(0);
  567. raw_spin_unlock_irqrestore(&erst_lock, flags);
  568. return rc;
  569. }
  570. EXPORT_SYMBOL_GPL(erst_write);
  571. static int __erst_read_to_erange(u64 record_id, u64 *offset)
  572. {
  573. int rc;
  574. if (erst_erange.attr & ERST_RANGE_NVRAM)
  575. return __erst_read_to_erange_from_nvram(
  576. record_id, offset);
  577. rc = __erst_read_from_storage(record_id, 0);
  578. if (rc)
  579. return rc;
  580. *offset = 0;
  581. return 0;
  582. }
  583. static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
  584. size_t buflen)
  585. {
  586. int rc;
  587. u64 offset, len = 0;
  588. struct cper_record_header *rcd_tmp;
  589. rc = __erst_read_to_erange(record_id, &offset);
  590. if (rc)
  591. return rc;
  592. rcd_tmp = erst_erange.vaddr + offset;
  593. len = rcd_tmp->record_length;
  594. if (len <= buflen)
  595. memcpy(record, rcd_tmp, len);
  596. return len;
  597. }
  598. /*
  599. * If return value > buflen, the buffer size is not big enough,
  600. * else if return value < 0, something goes wrong,
  601. * else everything is OK, and return value is record length
  602. */
  603. ssize_t erst_read(u64 record_id, struct cper_record_header *record,
  604. size_t buflen)
  605. {
  606. ssize_t len;
  607. unsigned long flags;
  608. if (erst_disable)
  609. return -ENODEV;
  610. raw_spin_lock_irqsave(&erst_lock, flags);
  611. len = __erst_read(record_id, record, buflen);
  612. raw_spin_unlock_irqrestore(&erst_lock, flags);
  613. return len;
  614. }
  615. EXPORT_SYMBOL_GPL(erst_read);
  616. /*
  617. * If return value > buflen, the buffer size is not big enough,
  618. * else if return value = 0, there is no more record to read,
  619. * else if return value < 0, something goes wrong,
  620. * else everything is OK, and return value is record length
  621. */
  622. ssize_t erst_read_next(struct cper_record_header *record, size_t buflen)
  623. {
  624. int rc;
  625. ssize_t len;
  626. unsigned long flags;
  627. u64 record_id;
  628. if (erst_disable)
  629. return -ENODEV;
  630. raw_spin_lock_irqsave(&erst_lock, flags);
  631. rc = __erst_get_next_record_id(&record_id);
  632. if (rc) {
  633. raw_spin_unlock_irqrestore(&erst_lock, flags);
  634. return rc;
  635. }
  636. /* no more record */
  637. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  638. raw_spin_unlock_irqrestore(&erst_lock, flags);
  639. return 0;
  640. }
  641. len = __erst_read(record_id, record, buflen);
  642. raw_spin_unlock_irqrestore(&erst_lock, flags);
  643. return len;
  644. }
  645. EXPORT_SYMBOL_GPL(erst_read_next);
  646. int erst_clear(u64 record_id)
  647. {
  648. int rc;
  649. unsigned long flags;
  650. if (erst_disable)
  651. return -ENODEV;
  652. raw_spin_lock_irqsave(&erst_lock, flags);
  653. if (erst_erange.attr & ERST_RANGE_NVRAM)
  654. rc = __erst_clear_from_nvram(record_id);
  655. else
  656. rc = __erst_clear_from_storage(record_id);
  657. raw_spin_unlock_irqrestore(&erst_lock, flags);
  658. return rc;
  659. }
  660. EXPORT_SYMBOL_GPL(erst_clear);
  661. static int __init setup_erst_disable(char *str)
  662. {
  663. erst_disable = 1;
  664. return 0;
  665. }
  666. __setup("erst_disable", setup_erst_disable);
  667. static int erst_check_table(struct acpi_table_erst *erst_tab)
  668. {
  669. if ((erst_tab->header_length !=
  670. (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
  671. && (erst_tab->header_length != sizeof(struct acpi_table_einj)))
  672. return -EINVAL;
  673. if (erst_tab->header.length < sizeof(struct acpi_table_erst))
  674. return -EINVAL;
  675. if (erst_tab->entries !=
  676. (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
  677. sizeof(struct acpi_erst_entry))
  678. return -EINVAL;
  679. return 0;
  680. }
  681. static int __init erst_init(void)
  682. {
  683. int rc = 0;
  684. acpi_status status;
  685. struct apei_exec_context ctx;
  686. struct apei_resources erst_resources;
  687. struct resource *r;
  688. if (acpi_disabled)
  689. goto err;
  690. if (erst_disable) {
  691. pr_info(ERST_PFX
  692. "Error Record Serialization Table (ERST) support is disabled.\n");
  693. goto err;
  694. }
  695. status = acpi_get_table(ACPI_SIG_ERST, 0,
  696. (struct acpi_table_header **)&erst_tab);
  697. if (status == AE_NOT_FOUND) {
  698. pr_info(ERST_PFX "Table is not found!\n");
  699. goto err;
  700. } else if (ACPI_FAILURE(status)) {
  701. const char *msg = acpi_format_exception(status);
  702. pr_err(ERST_PFX "Failed to get table, %s\n", msg);
  703. rc = -EINVAL;
  704. goto err;
  705. }
  706. rc = erst_check_table(erst_tab);
  707. if (rc) {
  708. pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
  709. goto err;
  710. }
  711. apei_resources_init(&erst_resources);
  712. erst_exec_ctx_init(&ctx);
  713. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  714. if (rc)
  715. goto err_fini;
  716. rc = apei_resources_request(&erst_resources, "APEI ERST");
  717. if (rc)
  718. goto err_fini;
  719. rc = apei_exec_pre_map_gars(&ctx);
  720. if (rc)
  721. goto err_release;
  722. rc = erst_get_erange(&erst_erange);
  723. if (rc) {
  724. if (rc == -ENODEV)
  725. pr_info(ERST_PFX
  726. "The corresponding hardware device or firmware implementation "
  727. "is not available.\n");
  728. else
  729. pr_err(ERST_PFX
  730. "Failed to get Error Log Address Range.\n");
  731. goto err_unmap_reg;
  732. }
  733. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  734. if (!r) {
  735. pr_err(ERST_PFX
  736. "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
  737. (unsigned long long)erst_erange.base,
  738. (unsigned long long)erst_erange.base + erst_erange.size);
  739. rc = -EIO;
  740. goto err_unmap_reg;
  741. }
  742. rc = -ENOMEM;
  743. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  744. erst_erange.size);
  745. if (!erst_erange.vaddr)
  746. goto err_release_erange;
  747. pr_info(ERST_PFX
  748. "Error Record Serialization Table (ERST) support is initialized.\n");
  749. return 0;
  750. err_release_erange:
  751. release_mem_region(erst_erange.base, erst_erange.size);
  752. err_unmap_reg:
  753. apei_exec_post_unmap_gars(&ctx);
  754. err_release:
  755. apei_resources_release(&erst_resources);
  756. err_fini:
  757. apei_resources_fini(&erst_resources);
  758. err:
  759. erst_disable = 1;
  760. return rc;
  761. }
  762. device_initcall(erst_init);