erst.c 24 KB

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