erst.c 29 KB

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