erst.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * APEI Error Record Serialization Table support
  3. *
  4. * ERST is a way provided by APEI to save and retrieve hardware error
  5. * infomation 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_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. rc = __apei_exec_read_register(entry, &offset);
  236. if (rc)
  237. return rc;
  238. memmove((void *)ctx->dst_base + offset,
  239. (void *)ctx->src_base + offset,
  240. ctx->var2);
  241. return 0;
  242. }
  243. static struct apei_exec_ins_type erst_ins_type[] = {
  244. [ACPI_ERST_READ_REGISTER] = {
  245. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  246. .run = apei_exec_read_register,
  247. },
  248. [ACPI_ERST_READ_REGISTER_VALUE] = {
  249. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  250. .run = apei_exec_read_register_value,
  251. },
  252. [ACPI_ERST_WRITE_REGISTER] = {
  253. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  254. .run = apei_exec_write_register,
  255. },
  256. [ACPI_ERST_WRITE_REGISTER_VALUE] = {
  257. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  258. .run = apei_exec_write_register_value,
  259. },
  260. [ACPI_ERST_NOOP] = {
  261. .flags = 0,
  262. .run = apei_exec_noop,
  263. },
  264. [ACPI_ERST_LOAD_VAR1] = {
  265. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  266. .run = erst_exec_load_var1,
  267. },
  268. [ACPI_ERST_LOAD_VAR2] = {
  269. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  270. .run = erst_exec_load_var2,
  271. },
  272. [ACPI_ERST_STORE_VAR1] = {
  273. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  274. .run = erst_exec_store_var1,
  275. },
  276. [ACPI_ERST_ADD] = {
  277. .flags = 0,
  278. .run = erst_exec_add,
  279. },
  280. [ACPI_ERST_SUBTRACT] = {
  281. .flags = 0,
  282. .run = erst_exec_subtract,
  283. },
  284. [ACPI_ERST_ADD_VALUE] = {
  285. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  286. .run = erst_exec_add_value,
  287. },
  288. [ACPI_ERST_SUBTRACT_VALUE] = {
  289. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  290. .run = erst_exec_subtract_value,
  291. },
  292. [ACPI_ERST_STALL] = {
  293. .flags = 0,
  294. .run = erst_exec_stall,
  295. },
  296. [ACPI_ERST_STALL_WHILE_TRUE] = {
  297. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  298. .run = erst_exec_stall_while_true,
  299. },
  300. [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
  301. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  302. .run = erst_exec_skip_next_instruction_if_true,
  303. },
  304. [ACPI_ERST_GOTO] = {
  305. .flags = 0,
  306. .run = erst_exec_goto,
  307. },
  308. [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
  309. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  310. .run = erst_exec_set_src_address_base,
  311. },
  312. [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
  313. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  314. .run = erst_exec_set_dst_address_base,
  315. },
  316. [ACPI_ERST_MOVE_DATA] = {
  317. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  318. .run = erst_exec_move_data,
  319. },
  320. };
  321. static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
  322. {
  323. apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
  324. ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
  325. }
  326. static int erst_get_erange(struct erst_erange *range)
  327. {
  328. struct apei_exec_context ctx;
  329. int rc;
  330. erst_exec_ctx_init(&ctx);
  331. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
  332. if (rc)
  333. return rc;
  334. range->base = apei_exec_ctx_get_output(&ctx);
  335. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
  336. if (rc)
  337. return rc;
  338. range->size = apei_exec_ctx_get_output(&ctx);
  339. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
  340. if (rc)
  341. return rc;
  342. range->attr = apei_exec_ctx_get_output(&ctx);
  343. return 0;
  344. }
  345. static ssize_t __erst_get_record_count(void)
  346. {
  347. struct apei_exec_context ctx;
  348. int rc;
  349. erst_exec_ctx_init(&ctx);
  350. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
  351. if (rc)
  352. return rc;
  353. return apei_exec_ctx_get_output(&ctx);
  354. }
  355. ssize_t erst_get_record_count(void)
  356. {
  357. ssize_t count;
  358. unsigned long flags;
  359. if (erst_disable)
  360. return -ENODEV;
  361. spin_lock_irqsave(&erst_lock, flags);
  362. count = __erst_get_record_count();
  363. spin_unlock_irqrestore(&erst_lock, flags);
  364. return count;
  365. }
  366. EXPORT_SYMBOL_GPL(erst_get_record_count);
  367. static int __erst_get_next_record_id(u64 *record_id)
  368. {
  369. struct apei_exec_context ctx;
  370. int rc;
  371. erst_exec_ctx_init(&ctx);
  372. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
  373. if (rc)
  374. return rc;
  375. *record_id = apei_exec_ctx_get_output(&ctx);
  376. return 0;
  377. }
  378. /*
  379. * Get the record ID of an existing error record on the persistent
  380. * storage. If there is no error record on the persistent storage, the
  381. * returned record_id is APEI_ERST_INVALID_RECORD_ID.
  382. */
  383. int erst_get_next_record_id(u64 *record_id)
  384. {
  385. int rc;
  386. unsigned long flags;
  387. if (erst_disable)
  388. return -ENODEV;
  389. spin_lock_irqsave(&erst_lock, flags);
  390. rc = __erst_get_next_record_id(record_id);
  391. spin_unlock_irqrestore(&erst_lock, flags);
  392. return rc;
  393. }
  394. EXPORT_SYMBOL_GPL(erst_get_next_record_id);
  395. static int __erst_write_to_storage(u64 offset)
  396. {
  397. struct apei_exec_context ctx;
  398. u64 timeout = FIRMWARE_TIMEOUT;
  399. u64 val;
  400. int rc;
  401. erst_exec_ctx_init(&ctx);
  402. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_WRITE);
  403. if (rc)
  404. return rc;
  405. apei_exec_ctx_set_input(&ctx, offset);
  406. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  407. if (rc)
  408. return rc;
  409. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  410. if (rc)
  411. return rc;
  412. for (;;) {
  413. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  414. if (rc)
  415. return rc;
  416. val = apei_exec_ctx_get_output(&ctx);
  417. if (!val)
  418. break;
  419. if (erst_timedout(&timeout, SPIN_UNIT))
  420. return -EIO;
  421. }
  422. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  423. if (rc)
  424. return rc;
  425. val = apei_exec_ctx_get_output(&ctx);
  426. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  427. if (rc)
  428. return rc;
  429. return erst_errno(val);
  430. }
  431. static int __erst_read_from_storage(u64 record_id, u64 offset)
  432. {
  433. struct apei_exec_context ctx;
  434. u64 timeout = FIRMWARE_TIMEOUT;
  435. u64 val;
  436. int rc;
  437. erst_exec_ctx_init(&ctx);
  438. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_READ);
  439. if (rc)
  440. return rc;
  441. apei_exec_ctx_set_input(&ctx, offset);
  442. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  443. if (rc)
  444. return rc;
  445. apei_exec_ctx_set_input(&ctx, record_id);
  446. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  447. if (rc)
  448. return rc;
  449. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  450. if (rc)
  451. return rc;
  452. for (;;) {
  453. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  454. if (rc)
  455. return rc;
  456. val = apei_exec_ctx_get_output(&ctx);
  457. if (!val)
  458. break;
  459. if (erst_timedout(&timeout, SPIN_UNIT))
  460. return -EIO;
  461. };
  462. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  463. if (rc)
  464. return rc;
  465. val = apei_exec_ctx_get_output(&ctx);
  466. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  467. if (rc)
  468. return rc;
  469. return erst_errno(val);
  470. }
  471. static int __erst_clear_from_storage(u64 record_id)
  472. {
  473. struct apei_exec_context ctx;
  474. u64 timeout = FIRMWARE_TIMEOUT;
  475. u64 val;
  476. int rc;
  477. erst_exec_ctx_init(&ctx);
  478. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_CLEAR);
  479. if (rc)
  480. return rc;
  481. apei_exec_ctx_set_input(&ctx, record_id);
  482. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  483. if (rc)
  484. return rc;
  485. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  486. if (rc)
  487. return rc;
  488. for (;;) {
  489. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  490. if (rc)
  491. return rc;
  492. val = apei_exec_ctx_get_output(&ctx);
  493. if (!val)
  494. break;
  495. if (erst_timedout(&timeout, SPIN_UNIT))
  496. return -EIO;
  497. }
  498. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  499. if (rc)
  500. return rc;
  501. val = apei_exec_ctx_get_output(&ctx);
  502. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  503. if (rc)
  504. return rc;
  505. return erst_errno(val);
  506. }
  507. /* NVRAM ERST Error Log Address Range is not supported yet */
  508. static void pr_unimpl_nvram(void)
  509. {
  510. if (printk_ratelimit())
  511. pr_warning(ERST_PFX
  512. "NVRAM ERST Log Address Range is not implemented yet\n");
  513. }
  514. static int __erst_write_to_nvram(const struct cper_record_header *record)
  515. {
  516. /* do not print message, because printk is not safe for NMI */
  517. return -ENOSYS;
  518. }
  519. static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
  520. {
  521. pr_unimpl_nvram();
  522. return -ENOSYS;
  523. }
  524. static int __erst_clear_from_nvram(u64 record_id)
  525. {
  526. pr_unimpl_nvram();
  527. return -ENOSYS;
  528. }
  529. int erst_write(const struct cper_record_header *record)
  530. {
  531. int rc;
  532. unsigned long flags;
  533. struct cper_record_header *rcd_erange;
  534. if (erst_disable)
  535. return -ENODEV;
  536. if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
  537. return -EINVAL;
  538. if (erst_erange.attr & ERST_RANGE_NVRAM) {
  539. if (!spin_trylock_irqsave(&erst_lock, flags))
  540. return -EBUSY;
  541. rc = __erst_write_to_nvram(record);
  542. spin_unlock_irqrestore(&erst_lock, flags);
  543. return rc;
  544. }
  545. if (record->record_length > erst_erange.size)
  546. return -EINVAL;
  547. if (!spin_trylock_irqsave(&erst_lock, flags))
  548. return -EBUSY;
  549. memcpy(erst_erange.vaddr, record, record->record_length);
  550. rcd_erange = erst_erange.vaddr;
  551. /* signature for serialization system */
  552. memcpy(&rcd_erange->persistence_information, "ER", 2);
  553. rc = __erst_write_to_storage(0);
  554. spin_unlock_irqrestore(&erst_lock, flags);
  555. return rc;
  556. }
  557. EXPORT_SYMBOL_GPL(erst_write);
  558. static int __erst_read_to_erange(u64 record_id, u64 *offset)
  559. {
  560. int rc;
  561. if (erst_erange.attr & ERST_RANGE_NVRAM)
  562. return __erst_read_to_erange_from_nvram(
  563. record_id, offset);
  564. rc = __erst_read_from_storage(record_id, 0);
  565. if (rc)
  566. return rc;
  567. *offset = 0;
  568. return 0;
  569. }
  570. static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
  571. size_t buflen)
  572. {
  573. int rc;
  574. u64 offset, len = 0;
  575. struct cper_record_header *rcd_tmp;
  576. rc = __erst_read_to_erange(record_id, &offset);
  577. if (rc)
  578. return rc;
  579. rcd_tmp = erst_erange.vaddr + offset;
  580. len = rcd_tmp->record_length;
  581. if (len <= buflen)
  582. memcpy(record, rcd_tmp, len);
  583. return len;
  584. }
  585. /*
  586. * If return value > buflen, the buffer size is not big enough,
  587. * else if return value < 0, something goes wrong,
  588. * else everything is OK, and return value is record length
  589. */
  590. ssize_t erst_read(u64 record_id, struct cper_record_header *record,
  591. size_t buflen)
  592. {
  593. ssize_t len;
  594. unsigned long flags;
  595. if (erst_disable)
  596. return -ENODEV;
  597. spin_lock_irqsave(&erst_lock, flags);
  598. len = __erst_read(record_id, record, buflen);
  599. spin_unlock_irqrestore(&erst_lock, flags);
  600. return len;
  601. }
  602. EXPORT_SYMBOL_GPL(erst_read);
  603. /*
  604. * If return value > buflen, the buffer size is not big enough,
  605. * else if return value = 0, there is no more record to read,
  606. * else if return value < 0, something goes wrong,
  607. * else everything is OK, and return value is record length
  608. */
  609. ssize_t erst_read_next(struct cper_record_header *record, size_t buflen)
  610. {
  611. int rc;
  612. ssize_t len;
  613. unsigned long flags;
  614. u64 record_id;
  615. if (erst_disable)
  616. return -ENODEV;
  617. spin_lock_irqsave(&erst_lock, flags);
  618. rc = __erst_get_next_record_id(&record_id);
  619. if (rc) {
  620. spin_unlock_irqrestore(&erst_lock, flags);
  621. return rc;
  622. }
  623. /* no more record */
  624. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  625. spin_unlock_irqrestore(&erst_lock, flags);
  626. return 0;
  627. }
  628. len = __erst_read(record_id, record, buflen);
  629. spin_unlock_irqrestore(&erst_lock, flags);
  630. return len;
  631. }
  632. EXPORT_SYMBOL_GPL(erst_read_next);
  633. int erst_clear(u64 record_id)
  634. {
  635. int rc;
  636. unsigned long flags;
  637. if (erst_disable)
  638. return -ENODEV;
  639. spin_lock_irqsave(&erst_lock, flags);
  640. if (erst_erange.attr & ERST_RANGE_NVRAM)
  641. rc = __erst_clear_from_nvram(record_id);
  642. else
  643. rc = __erst_clear_from_storage(record_id);
  644. spin_unlock_irqrestore(&erst_lock, flags);
  645. return rc;
  646. }
  647. EXPORT_SYMBOL_GPL(erst_clear);
  648. static int __init setup_erst_disable(char *str)
  649. {
  650. erst_disable = 1;
  651. return 0;
  652. }
  653. __setup("erst_disable", setup_erst_disable);
  654. static int erst_check_table(struct acpi_table_erst *erst_tab)
  655. {
  656. if (erst_tab->header_length != sizeof(struct acpi_table_erst))
  657. return -EINVAL;
  658. if (erst_tab->header.length < sizeof(struct acpi_table_erst))
  659. return -EINVAL;
  660. if (erst_tab->entries !=
  661. (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
  662. sizeof(struct acpi_erst_entry))
  663. return -EINVAL;
  664. return 0;
  665. }
  666. static int __init erst_init(void)
  667. {
  668. int rc = 0;
  669. acpi_status status;
  670. struct apei_exec_context ctx;
  671. struct apei_resources erst_resources;
  672. struct resource *r;
  673. if (acpi_disabled)
  674. goto err;
  675. if (erst_disable) {
  676. pr_info(ERST_PFX
  677. "Error Record Serialization Table (ERST) support is disabled.\n");
  678. goto err;
  679. }
  680. status = acpi_get_table(ACPI_SIG_ERST, 0,
  681. (struct acpi_table_header **)&erst_tab);
  682. if (status == AE_NOT_FOUND) {
  683. pr_info(ERST_PFX "Table is not found!\n");
  684. goto err;
  685. } else if (ACPI_FAILURE(status)) {
  686. const char *msg = acpi_format_exception(status);
  687. pr_err(ERST_PFX "Failed to get table, %s\n", msg);
  688. rc = -EINVAL;
  689. goto err;
  690. }
  691. rc = erst_check_table(erst_tab);
  692. if (rc) {
  693. pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
  694. goto err;
  695. }
  696. apei_resources_init(&erst_resources);
  697. erst_exec_ctx_init(&ctx);
  698. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  699. if (rc)
  700. goto err_fini;
  701. rc = apei_resources_request(&erst_resources, "APEI ERST");
  702. if (rc)
  703. goto err_fini;
  704. rc = apei_exec_pre_map_gars(&ctx);
  705. if (rc)
  706. goto err_release;
  707. rc = erst_get_erange(&erst_erange);
  708. if (rc) {
  709. if (rc == -ENODEV)
  710. pr_info(ERST_PFX
  711. "The corresponding hardware device or firmware implementation "
  712. "is not available.\n");
  713. else
  714. pr_err(ERST_PFX
  715. "Failed to get Error Log Address Range.\n");
  716. goto err_unmap_reg;
  717. }
  718. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  719. if (!r) {
  720. pr_err(ERST_PFX
  721. "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
  722. (unsigned long long)erst_erange.base,
  723. (unsigned long long)erst_erange.base + erst_erange.size);
  724. rc = -EIO;
  725. goto err_unmap_reg;
  726. }
  727. rc = -ENOMEM;
  728. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  729. erst_erange.size);
  730. if (!erst_erange.vaddr)
  731. goto err_release_erange;
  732. pr_info(ERST_PFX
  733. "Error Record Serialization Table (ERST) support is initialized.\n");
  734. return 0;
  735. err_release_erange:
  736. release_mem_region(erst_erange.base, erst_erange.size);
  737. err_unmap_reg:
  738. apei_exec_post_unmap_gars(&ctx);
  739. err_release:
  740. apei_resources_release(&erst_resources);
  741. err_fini:
  742. apei_resources_fini(&erst_resources);
  743. err:
  744. erst_disable = 1;
  745. return rc;
  746. }
  747. device_initcall(erst_init);