erst.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/acpi.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/cper.h>
  30. #include <linux/nmi.h>
  31. #include <linux/hardirq.h>
  32. #include <linux/pstore.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/mm.h> /* kvfree() */
  35. #include <acpi/apei.h>
  36. #include "apei-internal.h"
  37. #undef pr_fmt
  38. #define pr_fmt(fmt) "ERST: " fmt
  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 milliseconds */
  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_warn(FW_WARN "Firmware does not respond in time.\n");
  99. return 1;
  100. }
  101. *t -= spin_unit;
  102. ndelay(spin_unit);
  103. touch_nmi_watchdog();
  104. return 0;
  105. }
  106. static int erst_exec_load_var1(struct apei_exec_context *ctx,
  107. struct acpi_whea_header *entry)
  108. {
  109. return __apei_exec_read_register(entry, &ctx->var1);
  110. }
  111. static int erst_exec_load_var2(struct apei_exec_context *ctx,
  112. struct acpi_whea_header *entry)
  113. {
  114. return __apei_exec_read_register(entry, &ctx->var2);
  115. }
  116. static int erst_exec_store_var1(struct apei_exec_context *ctx,
  117. struct acpi_whea_header *entry)
  118. {
  119. return __apei_exec_write_register(entry, ctx->var1);
  120. }
  121. static int erst_exec_add(struct apei_exec_context *ctx,
  122. struct acpi_whea_header *entry)
  123. {
  124. ctx->var1 += ctx->var2;
  125. return 0;
  126. }
  127. static int erst_exec_subtract(struct apei_exec_context *ctx,
  128. struct acpi_whea_header *entry)
  129. {
  130. ctx->var1 -= ctx->var2;
  131. return 0;
  132. }
  133. static int erst_exec_add_value(struct apei_exec_context *ctx,
  134. struct acpi_whea_header *entry)
  135. {
  136. int rc;
  137. u64 val;
  138. rc = __apei_exec_read_register(entry, &val);
  139. if (rc)
  140. return rc;
  141. val += ctx->value;
  142. rc = __apei_exec_write_register(entry, val);
  143. return rc;
  144. }
  145. static int erst_exec_subtract_value(struct apei_exec_context *ctx,
  146. struct acpi_whea_header *entry)
  147. {
  148. int rc;
  149. u64 val;
  150. rc = __apei_exec_read_register(entry, &val);
  151. if (rc)
  152. return rc;
  153. val -= ctx->value;
  154. rc = __apei_exec_write_register(entry, val);
  155. return rc;
  156. }
  157. static int erst_exec_stall(struct apei_exec_context *ctx,
  158. struct acpi_whea_header *entry)
  159. {
  160. u64 stall_time;
  161. if (ctx->value > FIRMWARE_MAX_STALL) {
  162. if (!in_nmi())
  163. pr_warn(FW_WARN
  164. "Too long stall time for stall instruction: 0x%llx.\n",
  165. ctx->value);
  166. stall_time = FIRMWARE_MAX_STALL;
  167. } else
  168. stall_time = ctx->value;
  169. udelay(stall_time);
  170. return 0;
  171. }
  172. static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
  173. struct acpi_whea_header *entry)
  174. {
  175. int rc;
  176. u64 val;
  177. u64 timeout = FIRMWARE_TIMEOUT;
  178. u64 stall_time;
  179. if (ctx->var1 > FIRMWARE_MAX_STALL) {
  180. if (!in_nmi())
  181. pr_warn(FW_WARN
  182. "Too long stall time for stall while true instruction: 0x%llx.\n",
  183. ctx->var1);
  184. stall_time = FIRMWARE_MAX_STALL;
  185. } else
  186. stall_time = ctx->var1;
  187. for (;;) {
  188. rc = __apei_exec_read_register(entry, &val);
  189. if (rc)
  190. return rc;
  191. if (val != ctx->value)
  192. break;
  193. if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
  194. return -EIO;
  195. }
  196. return 0;
  197. }
  198. static int erst_exec_skip_next_instruction_if_true(
  199. struct apei_exec_context *ctx,
  200. struct acpi_whea_header *entry)
  201. {
  202. int rc;
  203. u64 val;
  204. rc = __apei_exec_read_register(entry, &val);
  205. if (rc)
  206. return rc;
  207. if (val == ctx->value) {
  208. ctx->ip += 2;
  209. return APEI_EXEC_SET_IP;
  210. }
  211. return 0;
  212. }
  213. static int erst_exec_goto(struct apei_exec_context *ctx,
  214. struct acpi_whea_header *entry)
  215. {
  216. ctx->ip = ctx->value;
  217. return APEI_EXEC_SET_IP;
  218. }
  219. static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
  220. struct acpi_whea_header *entry)
  221. {
  222. return __apei_exec_read_register(entry, &ctx->src_base);
  223. }
  224. static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
  225. struct acpi_whea_header *entry)
  226. {
  227. return __apei_exec_read_register(entry, &ctx->dst_base);
  228. }
  229. static int erst_exec_move_data(struct apei_exec_context *ctx,
  230. struct acpi_whea_header *entry)
  231. {
  232. int rc;
  233. u64 offset;
  234. void *src, *dst;
  235. /* ioremap does not work in interrupt context */
  236. if (in_interrupt()) {
  237. pr_warn("MOVE_DATA can not be used in interrupt context.\n");
  238. return -EBUSY;
  239. }
  240. rc = __apei_exec_read_register(entry, &offset);
  241. if (rc)
  242. return rc;
  243. src = ioremap(ctx->src_base + offset, ctx->var2);
  244. if (!src)
  245. return -ENOMEM;
  246. dst = ioremap(ctx->dst_base + offset, ctx->var2);
  247. if (!dst) {
  248. iounmap(src);
  249. return -ENOMEM;
  250. }
  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. #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
  381. #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
  382. struct erst_record_id_cache {
  383. struct mutex lock;
  384. u64 *entries;
  385. int len;
  386. int size;
  387. int refcount;
  388. };
  389. static struct erst_record_id_cache erst_record_id_cache = {
  390. .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
  391. .refcount = 0,
  392. };
  393. static int __erst_get_next_record_id(u64 *record_id)
  394. {
  395. struct apei_exec_context ctx;
  396. int rc;
  397. erst_exec_ctx_init(&ctx);
  398. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
  399. if (rc)
  400. return rc;
  401. *record_id = apei_exec_ctx_get_output(&ctx);
  402. return 0;
  403. }
  404. int erst_get_record_id_begin(int *pos)
  405. {
  406. int rc;
  407. if (erst_disable)
  408. return -ENODEV;
  409. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  410. if (rc)
  411. return rc;
  412. erst_record_id_cache.refcount++;
  413. mutex_unlock(&erst_record_id_cache.lock);
  414. *pos = 0;
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
  418. /* erst_record_id_cache.lock must be held by caller */
  419. static int __erst_record_id_cache_add_one(void)
  420. {
  421. u64 id, prev_id, first_id;
  422. int i, rc;
  423. u64 *entries;
  424. unsigned long flags;
  425. id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
  426. retry:
  427. raw_spin_lock_irqsave(&erst_lock, flags);
  428. rc = __erst_get_next_record_id(&id);
  429. raw_spin_unlock_irqrestore(&erst_lock, flags);
  430. if (rc == -ENOENT)
  431. return 0;
  432. if (rc)
  433. return rc;
  434. if (id == APEI_ERST_INVALID_RECORD_ID)
  435. return 0;
  436. /* can not skip current ID, or loop back to first ID */
  437. if (id == prev_id || id == first_id)
  438. return 0;
  439. if (first_id == APEI_ERST_INVALID_RECORD_ID)
  440. first_id = id;
  441. prev_id = id;
  442. entries = erst_record_id_cache.entries;
  443. for (i = 0; i < erst_record_id_cache.len; i++) {
  444. if (entries[i] == id)
  445. break;
  446. }
  447. /* record id already in cache, try next */
  448. if (i < erst_record_id_cache.len)
  449. goto retry;
  450. if (erst_record_id_cache.len >= erst_record_id_cache.size) {
  451. int new_size;
  452. u64 *new_entries;
  453. new_size = erst_record_id_cache.size * 2;
  454. new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
  455. ERST_RECORD_ID_CACHE_SIZE_MAX);
  456. if (new_size <= erst_record_id_cache.size) {
  457. if (printk_ratelimit())
  458. pr_warn(FW_WARN "too many record IDs!\n");
  459. return 0;
  460. }
  461. new_entries = kvmalloc_array(new_size, sizeof(entries[0]),
  462. GFP_KERNEL);
  463. if (!new_entries)
  464. return -ENOMEM;
  465. memcpy(new_entries, entries,
  466. erst_record_id_cache.len * sizeof(entries[0]));
  467. kvfree(entries);
  468. erst_record_id_cache.entries = entries = new_entries;
  469. erst_record_id_cache.size = new_size;
  470. }
  471. entries[i] = id;
  472. erst_record_id_cache.len++;
  473. return 1;
  474. }
  475. /*
  476. * Get the record ID of an existing error record on the persistent
  477. * storage. If there is no error record on the persistent storage, the
  478. * returned record_id is APEI_ERST_INVALID_RECORD_ID.
  479. */
  480. int erst_get_record_id_next(int *pos, u64 *record_id)
  481. {
  482. int rc = 0;
  483. u64 *entries;
  484. if (erst_disable)
  485. return -ENODEV;
  486. /* must be enclosed by erst_get_record_id_begin/end */
  487. BUG_ON(!erst_record_id_cache.refcount);
  488. BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
  489. mutex_lock(&erst_record_id_cache.lock);
  490. entries = erst_record_id_cache.entries;
  491. for (; *pos < erst_record_id_cache.len; (*pos)++)
  492. if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
  493. break;
  494. /* found next record id in cache */
  495. if (*pos < erst_record_id_cache.len) {
  496. *record_id = entries[*pos];
  497. (*pos)++;
  498. goto out_unlock;
  499. }
  500. /* Try to add one more record ID to cache */
  501. rc = __erst_record_id_cache_add_one();
  502. if (rc < 0)
  503. goto out_unlock;
  504. /* successfully add one new ID */
  505. if (rc == 1) {
  506. *record_id = erst_record_id_cache.entries[*pos];
  507. (*pos)++;
  508. rc = 0;
  509. } else {
  510. *pos = -1;
  511. *record_id = APEI_ERST_INVALID_RECORD_ID;
  512. }
  513. out_unlock:
  514. mutex_unlock(&erst_record_id_cache.lock);
  515. return rc;
  516. }
  517. EXPORT_SYMBOL_GPL(erst_get_record_id_next);
  518. /* erst_record_id_cache.lock must be held by caller */
  519. static void __erst_record_id_cache_compact(void)
  520. {
  521. int i, wpos = 0;
  522. u64 *entries;
  523. if (erst_record_id_cache.refcount)
  524. return;
  525. entries = erst_record_id_cache.entries;
  526. for (i = 0; i < erst_record_id_cache.len; i++) {
  527. if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
  528. continue;
  529. if (wpos != i)
  530. entries[wpos] = entries[i];
  531. wpos++;
  532. }
  533. erst_record_id_cache.len = wpos;
  534. }
  535. void erst_get_record_id_end(void)
  536. {
  537. /*
  538. * erst_disable != 0 should be detected by invoker via the
  539. * return value of erst_get_record_id_begin/next, so this
  540. * function should not be called for erst_disable != 0.
  541. */
  542. BUG_ON(erst_disable);
  543. mutex_lock(&erst_record_id_cache.lock);
  544. erst_record_id_cache.refcount--;
  545. BUG_ON(erst_record_id_cache.refcount < 0);
  546. __erst_record_id_cache_compact();
  547. mutex_unlock(&erst_record_id_cache.lock);
  548. }
  549. EXPORT_SYMBOL_GPL(erst_get_record_id_end);
  550. static int __erst_write_to_storage(u64 offset)
  551. {
  552. struct apei_exec_context ctx;
  553. u64 timeout = FIRMWARE_TIMEOUT;
  554. u64 val;
  555. int rc;
  556. erst_exec_ctx_init(&ctx);
  557. rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
  558. if (rc)
  559. return rc;
  560. apei_exec_ctx_set_input(&ctx, offset);
  561. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  562. if (rc)
  563. return rc;
  564. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  565. if (rc)
  566. return rc;
  567. for (;;) {
  568. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  569. if (rc)
  570. return rc;
  571. val = apei_exec_ctx_get_output(&ctx);
  572. if (!val)
  573. break;
  574. if (erst_timedout(&timeout, SPIN_UNIT))
  575. return -EIO;
  576. }
  577. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  578. if (rc)
  579. return rc;
  580. val = apei_exec_ctx_get_output(&ctx);
  581. rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
  582. if (rc)
  583. return rc;
  584. return erst_errno(val);
  585. }
  586. static int __erst_read_from_storage(u64 record_id, u64 offset)
  587. {
  588. struct apei_exec_context ctx;
  589. u64 timeout = FIRMWARE_TIMEOUT;
  590. u64 val;
  591. int rc;
  592. erst_exec_ctx_init(&ctx);
  593. rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
  594. if (rc)
  595. return rc;
  596. apei_exec_ctx_set_input(&ctx, offset);
  597. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  598. if (rc)
  599. return rc;
  600. apei_exec_ctx_set_input(&ctx, record_id);
  601. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  602. if (rc)
  603. return rc;
  604. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  605. if (rc)
  606. return rc;
  607. for (;;) {
  608. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  609. if (rc)
  610. return rc;
  611. val = apei_exec_ctx_get_output(&ctx);
  612. if (!val)
  613. break;
  614. if (erst_timedout(&timeout, SPIN_UNIT))
  615. return -EIO;
  616. };
  617. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  618. if (rc)
  619. return rc;
  620. val = apei_exec_ctx_get_output(&ctx);
  621. rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
  622. if (rc)
  623. return rc;
  624. return erst_errno(val);
  625. }
  626. static int __erst_clear_from_storage(u64 record_id)
  627. {
  628. struct apei_exec_context ctx;
  629. u64 timeout = FIRMWARE_TIMEOUT;
  630. u64 val;
  631. int rc;
  632. erst_exec_ctx_init(&ctx);
  633. rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
  634. if (rc)
  635. return rc;
  636. apei_exec_ctx_set_input(&ctx, record_id);
  637. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  638. if (rc)
  639. return rc;
  640. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  641. if (rc)
  642. return rc;
  643. for (;;) {
  644. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  645. if (rc)
  646. return rc;
  647. val = apei_exec_ctx_get_output(&ctx);
  648. if (!val)
  649. break;
  650. if (erst_timedout(&timeout, SPIN_UNIT))
  651. return -EIO;
  652. }
  653. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  654. if (rc)
  655. return rc;
  656. val = apei_exec_ctx_get_output(&ctx);
  657. rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
  658. if (rc)
  659. return rc;
  660. return erst_errno(val);
  661. }
  662. /* NVRAM ERST Error Log Address Range is not supported yet */
  663. static void pr_unimpl_nvram(void)
  664. {
  665. if (printk_ratelimit())
  666. pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
  667. }
  668. static int __erst_write_to_nvram(const struct cper_record_header *record)
  669. {
  670. /* do not print message, because printk is not safe for NMI */
  671. return -ENOSYS;
  672. }
  673. static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
  674. {
  675. pr_unimpl_nvram();
  676. return -ENOSYS;
  677. }
  678. static int __erst_clear_from_nvram(u64 record_id)
  679. {
  680. pr_unimpl_nvram();
  681. return -ENOSYS;
  682. }
  683. int erst_write(const struct cper_record_header *record)
  684. {
  685. int rc;
  686. unsigned long flags;
  687. struct cper_record_header *rcd_erange;
  688. if (erst_disable)
  689. return -ENODEV;
  690. if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
  691. return -EINVAL;
  692. if (erst_erange.attr & ERST_RANGE_NVRAM) {
  693. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  694. return -EBUSY;
  695. rc = __erst_write_to_nvram(record);
  696. raw_spin_unlock_irqrestore(&erst_lock, flags);
  697. return rc;
  698. }
  699. if (record->record_length > erst_erange.size)
  700. return -EINVAL;
  701. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  702. return -EBUSY;
  703. memcpy(erst_erange.vaddr, record, record->record_length);
  704. rcd_erange = erst_erange.vaddr;
  705. /* signature for serialization system */
  706. memcpy(&rcd_erange->persistence_information, "ER", 2);
  707. rc = __erst_write_to_storage(0);
  708. raw_spin_unlock_irqrestore(&erst_lock, flags);
  709. return rc;
  710. }
  711. EXPORT_SYMBOL_GPL(erst_write);
  712. static int __erst_read_to_erange(u64 record_id, u64 *offset)
  713. {
  714. int rc;
  715. if (erst_erange.attr & ERST_RANGE_NVRAM)
  716. return __erst_read_to_erange_from_nvram(
  717. record_id, offset);
  718. rc = __erst_read_from_storage(record_id, 0);
  719. if (rc)
  720. return rc;
  721. *offset = 0;
  722. return 0;
  723. }
  724. static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
  725. size_t buflen)
  726. {
  727. int rc;
  728. u64 offset, len = 0;
  729. struct cper_record_header *rcd_tmp;
  730. rc = __erst_read_to_erange(record_id, &offset);
  731. if (rc)
  732. return rc;
  733. rcd_tmp = erst_erange.vaddr + offset;
  734. len = rcd_tmp->record_length;
  735. if (len <= buflen)
  736. memcpy(record, rcd_tmp, len);
  737. return len;
  738. }
  739. /*
  740. * If return value > buflen, the buffer size is not big enough,
  741. * else if return value < 0, something goes wrong,
  742. * else everything is OK, and return value is record length
  743. */
  744. ssize_t erst_read(u64 record_id, struct cper_record_header *record,
  745. size_t buflen)
  746. {
  747. ssize_t len;
  748. unsigned long flags;
  749. if (erst_disable)
  750. return -ENODEV;
  751. raw_spin_lock_irqsave(&erst_lock, flags);
  752. len = __erst_read(record_id, record, buflen);
  753. raw_spin_unlock_irqrestore(&erst_lock, flags);
  754. return len;
  755. }
  756. EXPORT_SYMBOL_GPL(erst_read);
  757. int erst_clear(u64 record_id)
  758. {
  759. int rc, i;
  760. unsigned long flags;
  761. u64 *entries;
  762. if (erst_disable)
  763. return -ENODEV;
  764. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  765. if (rc)
  766. return rc;
  767. raw_spin_lock_irqsave(&erst_lock, flags);
  768. if (erst_erange.attr & ERST_RANGE_NVRAM)
  769. rc = __erst_clear_from_nvram(record_id);
  770. else
  771. rc = __erst_clear_from_storage(record_id);
  772. raw_spin_unlock_irqrestore(&erst_lock, flags);
  773. if (rc)
  774. goto out;
  775. entries = erst_record_id_cache.entries;
  776. for (i = 0; i < erst_record_id_cache.len; i++) {
  777. if (entries[i] == record_id)
  778. entries[i] = APEI_ERST_INVALID_RECORD_ID;
  779. }
  780. __erst_record_id_cache_compact();
  781. out:
  782. mutex_unlock(&erst_record_id_cache.lock);
  783. return rc;
  784. }
  785. EXPORT_SYMBOL_GPL(erst_clear);
  786. static int __init setup_erst_disable(char *str)
  787. {
  788. erst_disable = 1;
  789. return 0;
  790. }
  791. __setup("erst_disable", setup_erst_disable);
  792. static int erst_check_table(struct acpi_table_erst *erst_tab)
  793. {
  794. if ((erst_tab->header_length !=
  795. (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
  796. && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
  797. return -EINVAL;
  798. if (erst_tab->header.length < sizeof(struct acpi_table_erst))
  799. return -EINVAL;
  800. if (erst_tab->entries !=
  801. (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
  802. sizeof(struct acpi_erst_entry))
  803. return -EINVAL;
  804. return 0;
  805. }
  806. static int erst_open_pstore(struct pstore_info *psi);
  807. static int erst_close_pstore(struct pstore_info *psi);
  808. static ssize_t erst_reader(struct pstore_record *record);
  809. static int erst_writer(struct pstore_record *record);
  810. static int erst_clearer(struct pstore_record *record);
  811. static struct pstore_info erst_info = {
  812. .owner = THIS_MODULE,
  813. .name = "erst",
  814. .flags = PSTORE_FLAGS_DMESG,
  815. .open = erst_open_pstore,
  816. .close = erst_close_pstore,
  817. .read = erst_reader,
  818. .write = erst_writer,
  819. .erase = erst_clearer
  820. };
  821. #define CPER_CREATOR_PSTORE \
  822. UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
  823. 0x64, 0x90, 0xb8, 0x9d)
  824. #define CPER_SECTION_TYPE_DMESG \
  825. UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
  826. 0x94, 0x19, 0xeb, 0x12)
  827. #define CPER_SECTION_TYPE_DMESG_Z \
  828. UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
  829. 0x34, 0xdd, 0xfa, 0xc6)
  830. #define CPER_SECTION_TYPE_MCE \
  831. UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  832. 0x04, 0x4a, 0x38, 0xfc)
  833. struct cper_pstore_record {
  834. struct cper_record_header hdr;
  835. struct cper_section_descriptor sec_hdr;
  836. char data[];
  837. } __packed;
  838. static int reader_pos;
  839. static int erst_open_pstore(struct pstore_info *psi)
  840. {
  841. int rc;
  842. if (erst_disable)
  843. return -ENODEV;
  844. rc = erst_get_record_id_begin(&reader_pos);
  845. return rc;
  846. }
  847. static int erst_close_pstore(struct pstore_info *psi)
  848. {
  849. erst_get_record_id_end();
  850. return 0;
  851. }
  852. static ssize_t erst_reader(struct pstore_record *record)
  853. {
  854. int rc;
  855. ssize_t len = 0;
  856. u64 record_id;
  857. struct cper_pstore_record *rcd;
  858. size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
  859. if (erst_disable)
  860. return -ENODEV;
  861. rcd = kmalloc(rcd_len, GFP_KERNEL);
  862. if (!rcd) {
  863. rc = -ENOMEM;
  864. goto out;
  865. }
  866. skip:
  867. rc = erst_get_record_id_next(&reader_pos, &record_id);
  868. if (rc)
  869. goto out;
  870. /* no more record */
  871. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  872. rc = -EINVAL;
  873. goto out;
  874. }
  875. len = erst_read(record_id, &rcd->hdr, rcd_len);
  876. /* The record may be cleared by others, try read next record */
  877. if (len == -ENOENT)
  878. goto skip;
  879. else if (len < 0 || len < sizeof(*rcd)) {
  880. rc = -EIO;
  881. goto out;
  882. }
  883. if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
  884. goto skip;
  885. record->buf = kmalloc(len, GFP_KERNEL);
  886. if (record->buf == NULL) {
  887. rc = -ENOMEM;
  888. goto out;
  889. }
  890. memcpy(record->buf, rcd->data, len - sizeof(*rcd));
  891. record->id = record_id;
  892. record->compressed = false;
  893. record->ecc_notice_size = 0;
  894. if (uuid_le_cmp(rcd->sec_hdr.section_type,
  895. CPER_SECTION_TYPE_DMESG_Z) == 0) {
  896. record->type = PSTORE_TYPE_DMESG;
  897. record->compressed = true;
  898. } else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  899. CPER_SECTION_TYPE_DMESG) == 0)
  900. record->type = PSTORE_TYPE_DMESG;
  901. else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  902. CPER_SECTION_TYPE_MCE) == 0)
  903. record->type = PSTORE_TYPE_MCE;
  904. else
  905. record->type = PSTORE_TYPE_UNKNOWN;
  906. if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
  907. record->time.tv_sec = rcd->hdr.timestamp;
  908. else
  909. record->time.tv_sec = 0;
  910. record->time.tv_nsec = 0;
  911. out:
  912. kfree(rcd);
  913. return (rc < 0) ? rc : (len - sizeof(*rcd));
  914. }
  915. static int erst_writer(struct pstore_record *record)
  916. {
  917. struct cper_pstore_record *rcd = (struct cper_pstore_record *)
  918. (erst_info.buf - sizeof(*rcd));
  919. int ret;
  920. memset(rcd, 0, sizeof(*rcd));
  921. memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  922. rcd->hdr.revision = CPER_RECORD_REV;
  923. rcd->hdr.signature_end = CPER_SIG_END;
  924. rcd->hdr.section_count = 1;
  925. rcd->hdr.error_severity = CPER_SEV_FATAL;
  926. /* timestamp valid. platform_id, partition_id are invalid */
  927. rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
  928. rcd->hdr.timestamp = ktime_get_real_seconds();
  929. rcd->hdr.record_length = sizeof(*rcd) + record->size;
  930. rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
  931. rcd->hdr.notification_type = CPER_NOTIFY_MCE;
  932. rcd->hdr.record_id = cper_next_record_id();
  933. rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  934. rcd->sec_hdr.section_offset = sizeof(*rcd);
  935. rcd->sec_hdr.section_length = record->size;
  936. rcd->sec_hdr.revision = CPER_SEC_REV;
  937. /* fru_id and fru_text is invalid */
  938. rcd->sec_hdr.validation_bits = 0;
  939. rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
  940. switch (record->type) {
  941. case PSTORE_TYPE_DMESG:
  942. if (record->compressed)
  943. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
  944. else
  945. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
  946. break;
  947. case PSTORE_TYPE_MCE:
  948. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  949. break;
  950. default:
  951. return -EINVAL;
  952. }
  953. rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
  954. ret = erst_write(&rcd->hdr);
  955. record->id = rcd->hdr.record_id;
  956. return ret;
  957. }
  958. static int erst_clearer(struct pstore_record *record)
  959. {
  960. return erst_clear(record->id);
  961. }
  962. static int __init erst_init(void)
  963. {
  964. int rc = 0;
  965. acpi_status status;
  966. struct apei_exec_context ctx;
  967. struct apei_resources erst_resources;
  968. struct resource *r;
  969. char *buf;
  970. if (acpi_disabled)
  971. goto err;
  972. if (erst_disable) {
  973. pr_info(
  974. "Error Record Serialization Table (ERST) support is disabled.\n");
  975. goto err;
  976. }
  977. status = acpi_get_table(ACPI_SIG_ERST, 0,
  978. (struct acpi_table_header **)&erst_tab);
  979. if (status == AE_NOT_FOUND)
  980. goto err;
  981. else if (ACPI_FAILURE(status)) {
  982. const char *msg = acpi_format_exception(status);
  983. pr_err("Failed to get table, %s\n", msg);
  984. rc = -EINVAL;
  985. goto err;
  986. }
  987. rc = erst_check_table(erst_tab);
  988. if (rc) {
  989. pr_err(FW_BUG "ERST table is invalid.\n");
  990. goto err;
  991. }
  992. apei_resources_init(&erst_resources);
  993. erst_exec_ctx_init(&ctx);
  994. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  995. if (rc)
  996. goto err_fini;
  997. rc = apei_resources_request(&erst_resources, "APEI ERST");
  998. if (rc)
  999. goto err_fini;
  1000. rc = apei_exec_pre_map_gars(&ctx);
  1001. if (rc)
  1002. goto err_release;
  1003. rc = erst_get_erange(&erst_erange);
  1004. if (rc) {
  1005. if (rc == -ENODEV)
  1006. pr_info(
  1007. "The corresponding hardware device or firmware implementation "
  1008. "is not available.\n");
  1009. else
  1010. pr_err("Failed to get Error Log Address Range.\n");
  1011. goto err_unmap_reg;
  1012. }
  1013. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  1014. if (!r) {
  1015. pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
  1016. (unsigned long long)erst_erange.base,
  1017. (unsigned long long)erst_erange.base + erst_erange.size - 1);
  1018. rc = -EIO;
  1019. goto err_unmap_reg;
  1020. }
  1021. rc = -ENOMEM;
  1022. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  1023. erst_erange.size);
  1024. if (!erst_erange.vaddr)
  1025. goto err_release_erange;
  1026. pr_info(
  1027. "Error Record Serialization Table (ERST) support is initialized.\n");
  1028. buf = kmalloc(erst_erange.size, GFP_KERNEL);
  1029. if (buf) {
  1030. erst_info.buf = buf + sizeof(struct cper_pstore_record);
  1031. erst_info.bufsize = erst_erange.size -
  1032. sizeof(struct cper_pstore_record);
  1033. rc = pstore_register(&erst_info);
  1034. if (rc) {
  1035. if (rc != -EPERM)
  1036. pr_info(
  1037. "Could not register with persistent store.\n");
  1038. erst_info.buf = NULL;
  1039. erst_info.bufsize = 0;
  1040. kfree(buf);
  1041. }
  1042. } else
  1043. pr_err(
  1044. "Failed to allocate %lld bytes for persistent store error log.\n",
  1045. erst_erange.size);
  1046. /* Cleanup ERST Resources */
  1047. apei_resources_fini(&erst_resources);
  1048. return 0;
  1049. err_release_erange:
  1050. release_mem_region(erst_erange.base, erst_erange.size);
  1051. err_unmap_reg:
  1052. apei_exec_post_unmap_gars(&ctx);
  1053. err_release:
  1054. apei_resources_release(&erst_resources);
  1055. err_fini:
  1056. apei_resources_fini(&erst_resources);
  1057. err:
  1058. erst_disable = 1;
  1059. return rc;
  1060. }
  1061. device_initcall(erst_init);