erst.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
  382. #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
  383. struct erst_record_id_cache {
  384. struct mutex lock;
  385. u64 *entries;
  386. int len;
  387. int size;
  388. int refcount;
  389. };
  390. static struct erst_record_id_cache erst_record_id_cache = {
  391. .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
  392. .refcount = 0,
  393. };
  394. static int __erst_get_next_record_id(u64 *record_id)
  395. {
  396. struct apei_exec_context ctx;
  397. int rc;
  398. erst_exec_ctx_init(&ctx);
  399. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
  400. if (rc)
  401. return rc;
  402. *record_id = apei_exec_ctx_get_output(&ctx);
  403. return 0;
  404. }
  405. int erst_get_record_id_begin(int *pos)
  406. {
  407. int rc;
  408. if (erst_disable)
  409. return -ENODEV;
  410. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  411. if (rc)
  412. return rc;
  413. erst_record_id_cache.refcount++;
  414. mutex_unlock(&erst_record_id_cache.lock);
  415. *pos = 0;
  416. return 0;
  417. }
  418. EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
  419. /* erst_record_id_cache.lock must be held by caller */
  420. static int __erst_record_id_cache_add_one(void)
  421. {
  422. u64 id, prev_id, first_id;
  423. int i, rc;
  424. u64 *entries;
  425. unsigned long flags;
  426. id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
  427. retry:
  428. raw_spin_lock_irqsave(&erst_lock, flags);
  429. rc = __erst_get_next_record_id(&id);
  430. raw_spin_unlock_irqrestore(&erst_lock, flags);
  431. if (rc == -ENOENT)
  432. return 0;
  433. if (rc)
  434. return rc;
  435. if (id == APEI_ERST_INVALID_RECORD_ID)
  436. return 0;
  437. /* can not skip current ID, or loop back to first ID */
  438. if (id == prev_id || id == first_id)
  439. return 0;
  440. if (first_id == APEI_ERST_INVALID_RECORD_ID)
  441. first_id = id;
  442. prev_id = id;
  443. entries = erst_record_id_cache.entries;
  444. for (i = 0; i < erst_record_id_cache.len; i++) {
  445. if (entries[i] == id)
  446. break;
  447. }
  448. /* record id already in cache, try next */
  449. if (i < erst_record_id_cache.len)
  450. goto retry;
  451. if (erst_record_id_cache.len >= erst_record_id_cache.size) {
  452. int new_size, alloc_size;
  453. u64 *new_entries;
  454. new_size = erst_record_id_cache.size * 2;
  455. new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
  456. ERST_RECORD_ID_CACHE_SIZE_MAX);
  457. if (new_size <= erst_record_id_cache.size) {
  458. if (printk_ratelimit())
  459. pr_warning(FW_WARN ERST_PFX
  460. "too many record ID!\n");
  461. return 0;
  462. }
  463. alloc_size = new_size * sizeof(entries[0]);
  464. if (alloc_size < PAGE_SIZE)
  465. new_entries = kmalloc(alloc_size, GFP_KERNEL);
  466. else
  467. new_entries = vmalloc(alloc_size);
  468. if (!new_entries)
  469. return -ENOMEM;
  470. memcpy(new_entries, entries,
  471. erst_record_id_cache.len * sizeof(entries[0]));
  472. if (erst_record_id_cache.size < PAGE_SIZE)
  473. kfree(entries);
  474. else
  475. vfree(entries);
  476. erst_record_id_cache.entries = entries = new_entries;
  477. erst_record_id_cache.size = new_size;
  478. }
  479. entries[i] = id;
  480. erst_record_id_cache.len++;
  481. return 1;
  482. }
  483. /*
  484. * Get the record ID of an existing error record on the persistent
  485. * storage. If there is no error record on the persistent storage, the
  486. * returned record_id is APEI_ERST_INVALID_RECORD_ID.
  487. */
  488. int erst_get_record_id_next(int *pos, u64 *record_id)
  489. {
  490. int rc = 0;
  491. u64 *entries;
  492. if (erst_disable)
  493. return -ENODEV;
  494. /* must be enclosed by erst_get_record_id_begin/end */
  495. BUG_ON(!erst_record_id_cache.refcount);
  496. BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
  497. mutex_lock(&erst_record_id_cache.lock);
  498. entries = erst_record_id_cache.entries;
  499. for (; *pos < erst_record_id_cache.len; (*pos)++)
  500. if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
  501. break;
  502. /* found next record id in cache */
  503. if (*pos < erst_record_id_cache.len) {
  504. *record_id = entries[*pos];
  505. (*pos)++;
  506. goto out_unlock;
  507. }
  508. /* Try to add one more record ID to cache */
  509. rc = __erst_record_id_cache_add_one();
  510. if (rc < 0)
  511. goto out_unlock;
  512. /* successfully add one new ID */
  513. if (rc == 1) {
  514. *record_id = erst_record_id_cache.entries[*pos];
  515. (*pos)++;
  516. rc = 0;
  517. } else {
  518. *pos = -1;
  519. *record_id = APEI_ERST_INVALID_RECORD_ID;
  520. }
  521. out_unlock:
  522. mutex_unlock(&erst_record_id_cache.lock);
  523. return rc;
  524. }
  525. EXPORT_SYMBOL_GPL(erst_get_record_id_next);
  526. /* erst_record_id_cache.lock must be held by caller */
  527. static void __erst_record_id_cache_compact(void)
  528. {
  529. int i, wpos = 0;
  530. u64 *entries;
  531. if (erst_record_id_cache.refcount)
  532. return;
  533. entries = erst_record_id_cache.entries;
  534. for (i = 0; i < erst_record_id_cache.len; i++) {
  535. if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
  536. continue;
  537. if (wpos != i)
  538. memcpy(&entries[wpos], &entries[i], sizeof(entries[i]));
  539. wpos++;
  540. }
  541. erst_record_id_cache.len = wpos;
  542. }
  543. void erst_get_record_id_end(void)
  544. {
  545. /*
  546. * erst_disable != 0 should be detected by invoker via the
  547. * return value of erst_get_record_id_begin/next, so this
  548. * function should not be called for erst_disable != 0.
  549. */
  550. BUG_ON(erst_disable);
  551. mutex_lock(&erst_record_id_cache.lock);
  552. erst_record_id_cache.refcount--;
  553. BUG_ON(erst_record_id_cache.refcount < 0);
  554. __erst_record_id_cache_compact();
  555. mutex_unlock(&erst_record_id_cache.lock);
  556. }
  557. EXPORT_SYMBOL_GPL(erst_get_record_id_end);
  558. static int __erst_write_to_storage(u64 offset)
  559. {
  560. struct apei_exec_context ctx;
  561. u64 timeout = FIRMWARE_TIMEOUT;
  562. u64 val;
  563. int rc;
  564. erst_exec_ctx_init(&ctx);
  565. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_WRITE);
  566. if (rc)
  567. return rc;
  568. apei_exec_ctx_set_input(&ctx, offset);
  569. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  570. if (rc)
  571. return rc;
  572. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  573. if (rc)
  574. return rc;
  575. for (;;) {
  576. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  577. if (rc)
  578. return rc;
  579. val = apei_exec_ctx_get_output(&ctx);
  580. if (!val)
  581. break;
  582. if (erst_timedout(&timeout, SPIN_UNIT))
  583. return -EIO;
  584. }
  585. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  586. if (rc)
  587. return rc;
  588. val = apei_exec_ctx_get_output(&ctx);
  589. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  590. if (rc)
  591. return rc;
  592. return erst_errno(val);
  593. }
  594. static int __erst_read_from_storage(u64 record_id, u64 offset)
  595. {
  596. struct apei_exec_context ctx;
  597. u64 timeout = FIRMWARE_TIMEOUT;
  598. u64 val;
  599. int rc;
  600. erst_exec_ctx_init(&ctx);
  601. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_READ);
  602. if (rc)
  603. return rc;
  604. apei_exec_ctx_set_input(&ctx, offset);
  605. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  606. if (rc)
  607. return rc;
  608. apei_exec_ctx_set_input(&ctx, record_id);
  609. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  610. if (rc)
  611. return rc;
  612. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  613. if (rc)
  614. return rc;
  615. for (;;) {
  616. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  617. if (rc)
  618. return rc;
  619. val = apei_exec_ctx_get_output(&ctx);
  620. if (!val)
  621. break;
  622. if (erst_timedout(&timeout, SPIN_UNIT))
  623. return -EIO;
  624. };
  625. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  626. if (rc)
  627. return rc;
  628. val = apei_exec_ctx_get_output(&ctx);
  629. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  630. if (rc)
  631. return rc;
  632. return erst_errno(val);
  633. }
  634. static int __erst_clear_from_storage(u64 record_id)
  635. {
  636. struct apei_exec_context ctx;
  637. u64 timeout = FIRMWARE_TIMEOUT;
  638. u64 val;
  639. int rc;
  640. erst_exec_ctx_init(&ctx);
  641. rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_CLEAR);
  642. if (rc)
  643. return rc;
  644. apei_exec_ctx_set_input(&ctx, record_id);
  645. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  646. if (rc)
  647. return rc;
  648. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  649. if (rc)
  650. return rc;
  651. for (;;) {
  652. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  653. if (rc)
  654. return rc;
  655. val = apei_exec_ctx_get_output(&ctx);
  656. if (!val)
  657. break;
  658. if (erst_timedout(&timeout, SPIN_UNIT))
  659. return -EIO;
  660. }
  661. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  662. if (rc)
  663. return rc;
  664. val = apei_exec_ctx_get_output(&ctx);
  665. rc = apei_exec_run(&ctx, ACPI_ERST_END);
  666. if (rc)
  667. return rc;
  668. return erst_errno(val);
  669. }
  670. /* NVRAM ERST Error Log Address Range is not supported yet */
  671. static void pr_unimpl_nvram(void)
  672. {
  673. if (printk_ratelimit())
  674. pr_warning(ERST_PFX
  675. "NVRAM ERST Log Address Range is not implemented yet\n");
  676. }
  677. static int __erst_write_to_nvram(const struct cper_record_header *record)
  678. {
  679. /* do not print message, because printk is not safe for NMI */
  680. return -ENOSYS;
  681. }
  682. static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
  683. {
  684. pr_unimpl_nvram();
  685. return -ENOSYS;
  686. }
  687. static int __erst_clear_from_nvram(u64 record_id)
  688. {
  689. pr_unimpl_nvram();
  690. return -ENOSYS;
  691. }
  692. int erst_write(const struct cper_record_header *record)
  693. {
  694. int rc;
  695. unsigned long flags;
  696. struct cper_record_header *rcd_erange;
  697. if (erst_disable)
  698. return -ENODEV;
  699. if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
  700. return -EINVAL;
  701. if (erst_erange.attr & ERST_RANGE_NVRAM) {
  702. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  703. return -EBUSY;
  704. rc = __erst_write_to_nvram(record);
  705. raw_spin_unlock_irqrestore(&erst_lock, flags);
  706. return rc;
  707. }
  708. if (record->record_length > erst_erange.size)
  709. return -EINVAL;
  710. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  711. return -EBUSY;
  712. memcpy(erst_erange.vaddr, record, record->record_length);
  713. rcd_erange = erst_erange.vaddr;
  714. /* signature for serialization system */
  715. memcpy(&rcd_erange->persistence_information, "ER", 2);
  716. rc = __erst_write_to_storage(0);
  717. raw_spin_unlock_irqrestore(&erst_lock, flags);
  718. return rc;
  719. }
  720. EXPORT_SYMBOL_GPL(erst_write);
  721. static int __erst_read_to_erange(u64 record_id, u64 *offset)
  722. {
  723. int rc;
  724. if (erst_erange.attr & ERST_RANGE_NVRAM)
  725. return __erst_read_to_erange_from_nvram(
  726. record_id, offset);
  727. rc = __erst_read_from_storage(record_id, 0);
  728. if (rc)
  729. return rc;
  730. *offset = 0;
  731. return 0;
  732. }
  733. static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
  734. size_t buflen)
  735. {
  736. int rc;
  737. u64 offset, len = 0;
  738. struct cper_record_header *rcd_tmp;
  739. rc = __erst_read_to_erange(record_id, &offset);
  740. if (rc)
  741. return rc;
  742. rcd_tmp = erst_erange.vaddr + offset;
  743. len = rcd_tmp->record_length;
  744. if (len <= buflen)
  745. memcpy(record, rcd_tmp, len);
  746. return len;
  747. }
  748. /*
  749. * If return value > buflen, the buffer size is not big enough,
  750. * else if return value < 0, something goes wrong,
  751. * else everything is OK, and return value is record length
  752. */
  753. ssize_t erst_read(u64 record_id, struct cper_record_header *record,
  754. size_t buflen)
  755. {
  756. ssize_t len;
  757. unsigned long flags;
  758. if (erst_disable)
  759. return -ENODEV;
  760. raw_spin_lock_irqsave(&erst_lock, flags);
  761. len = __erst_read(record_id, record, buflen);
  762. raw_spin_unlock_irqrestore(&erst_lock, flags);
  763. return len;
  764. }
  765. EXPORT_SYMBOL_GPL(erst_read);
  766. int erst_clear(u64 record_id)
  767. {
  768. int rc, i;
  769. unsigned long flags;
  770. u64 *entries;
  771. if (erst_disable)
  772. return -ENODEV;
  773. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  774. if (rc)
  775. return rc;
  776. raw_spin_lock_irqsave(&erst_lock, flags);
  777. if (erst_erange.attr & ERST_RANGE_NVRAM)
  778. rc = __erst_clear_from_nvram(record_id);
  779. else
  780. rc = __erst_clear_from_storage(record_id);
  781. raw_spin_unlock_irqrestore(&erst_lock, flags);
  782. if (rc)
  783. goto out;
  784. entries = erst_record_id_cache.entries;
  785. for (i = 0; i < erst_record_id_cache.len; i++) {
  786. if (entries[i] == record_id)
  787. entries[i] = APEI_ERST_INVALID_RECORD_ID;
  788. }
  789. __erst_record_id_cache_compact();
  790. out:
  791. mutex_unlock(&erst_record_id_cache.lock);
  792. return rc;
  793. }
  794. EXPORT_SYMBOL_GPL(erst_clear);
  795. static int __init setup_erst_disable(char *str)
  796. {
  797. erst_disable = 1;
  798. return 0;
  799. }
  800. __setup("erst_disable", setup_erst_disable);
  801. static int erst_check_table(struct acpi_table_erst *erst_tab)
  802. {
  803. if ((erst_tab->header_length !=
  804. (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
  805. && (erst_tab->header_length != sizeof(struct acpi_table_einj)))
  806. return -EINVAL;
  807. if (erst_tab->header.length < sizeof(struct acpi_table_erst))
  808. return -EINVAL;
  809. if (erst_tab->entries !=
  810. (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
  811. sizeof(struct acpi_erst_entry))
  812. return -EINVAL;
  813. return 0;
  814. }
  815. static int erst_open_pstore(struct pstore_info *psi);
  816. static int erst_close_pstore(struct pstore_info *psi);
  817. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
  818. struct timespec *time);
  819. static u64 erst_writer(enum pstore_type_id type, size_t size);
  820. static struct pstore_info erst_info = {
  821. .owner = THIS_MODULE,
  822. .name = "erst",
  823. .open = erst_open_pstore,
  824. .close = erst_close_pstore,
  825. .read = erst_reader,
  826. .write = erst_writer,
  827. .erase = erst_clear
  828. };
  829. #define CPER_CREATOR_PSTORE \
  830. UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
  831. 0x64, 0x90, 0xb8, 0x9d)
  832. #define CPER_SECTION_TYPE_DMESG \
  833. UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
  834. 0x94, 0x19, 0xeb, 0x12)
  835. #define CPER_SECTION_TYPE_MCE \
  836. UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  837. 0x04, 0x4a, 0x38, 0xfc)
  838. struct cper_pstore_record {
  839. struct cper_record_header hdr;
  840. struct cper_section_descriptor sec_hdr;
  841. char data[];
  842. } __packed;
  843. static int reader_pos;
  844. static int erst_open_pstore(struct pstore_info *psi)
  845. {
  846. int rc;
  847. if (erst_disable)
  848. return -ENODEV;
  849. rc = erst_get_record_id_begin(&reader_pos);
  850. return rc;
  851. }
  852. static int erst_close_pstore(struct pstore_info *psi)
  853. {
  854. erst_get_record_id_end();
  855. return 0;
  856. }
  857. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
  858. struct timespec *time)
  859. {
  860. int rc;
  861. ssize_t len = 0;
  862. u64 record_id;
  863. struct cper_pstore_record *rcd = (struct cper_pstore_record *)
  864. (erst_info.buf - sizeof(*rcd));
  865. if (erst_disable)
  866. return -ENODEV;
  867. skip:
  868. rc = erst_get_record_id_next(&reader_pos, &record_id);
  869. if (rc)
  870. goto out;
  871. /* no more record */
  872. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  873. rc = -1;
  874. goto out;
  875. }
  876. len = erst_read(record_id, &rcd->hdr, sizeof(*rcd) +
  877. erst_info.bufsize);
  878. /* The record may be cleared by others, try read next record */
  879. if (len == -ENOENT)
  880. goto skip;
  881. else if (len < 0) {
  882. rc = -1;
  883. goto out;
  884. }
  885. if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
  886. goto skip;
  887. *id = record_id;
  888. if (uuid_le_cmp(rcd->sec_hdr.section_type,
  889. CPER_SECTION_TYPE_DMESG) == 0)
  890. *type = PSTORE_TYPE_DMESG;
  891. else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  892. CPER_SECTION_TYPE_MCE) == 0)
  893. *type = PSTORE_TYPE_MCE;
  894. else
  895. *type = PSTORE_TYPE_UNKNOWN;
  896. if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
  897. time->tv_sec = rcd->hdr.timestamp;
  898. else
  899. time->tv_sec = 0;
  900. time->tv_nsec = 0;
  901. out:
  902. return (rc < 0) ? rc : (len - sizeof(*rcd));
  903. }
  904. static u64 erst_writer(enum pstore_type_id type, size_t size)
  905. {
  906. struct cper_pstore_record *rcd = (struct cper_pstore_record *)
  907. (erst_info.buf - sizeof(*rcd));
  908. memset(rcd, 0, sizeof(*rcd));
  909. memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  910. rcd->hdr.revision = CPER_RECORD_REV;
  911. rcd->hdr.signature_end = CPER_SIG_END;
  912. rcd->hdr.section_count = 1;
  913. rcd->hdr.error_severity = CPER_SEV_FATAL;
  914. /* timestamp valid. platform_id, partition_id are invalid */
  915. rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
  916. rcd->hdr.timestamp = get_seconds();
  917. rcd->hdr.record_length = sizeof(*rcd) + size;
  918. rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
  919. rcd->hdr.notification_type = CPER_NOTIFY_MCE;
  920. rcd->hdr.record_id = cper_next_record_id();
  921. rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  922. rcd->sec_hdr.section_offset = sizeof(*rcd);
  923. rcd->sec_hdr.section_length = size;
  924. rcd->sec_hdr.revision = CPER_SEC_REV;
  925. /* fru_id and fru_text is invalid */
  926. rcd->sec_hdr.validation_bits = 0;
  927. rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
  928. switch (type) {
  929. case PSTORE_TYPE_DMESG:
  930. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
  931. break;
  932. case PSTORE_TYPE_MCE:
  933. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  934. break;
  935. default:
  936. return -EINVAL;
  937. }
  938. rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
  939. erst_write(&rcd->hdr);
  940. return rcd->hdr.record_id;
  941. }
  942. static int __init erst_init(void)
  943. {
  944. int rc = 0;
  945. acpi_status status;
  946. struct apei_exec_context ctx;
  947. struct apei_resources erst_resources;
  948. struct resource *r;
  949. char *buf;
  950. if (acpi_disabled)
  951. goto err;
  952. if (erst_disable) {
  953. pr_info(ERST_PFX
  954. "Error Record Serialization Table (ERST) support is disabled.\n");
  955. goto err;
  956. }
  957. status = acpi_get_table(ACPI_SIG_ERST, 0,
  958. (struct acpi_table_header **)&erst_tab);
  959. if (status == AE_NOT_FOUND) {
  960. pr_info(ERST_PFX "Table is not found!\n");
  961. goto err;
  962. } else if (ACPI_FAILURE(status)) {
  963. const char *msg = acpi_format_exception(status);
  964. pr_err(ERST_PFX "Failed to get table, %s\n", msg);
  965. rc = -EINVAL;
  966. goto err;
  967. }
  968. rc = erst_check_table(erst_tab);
  969. if (rc) {
  970. pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
  971. goto err;
  972. }
  973. apei_resources_init(&erst_resources);
  974. erst_exec_ctx_init(&ctx);
  975. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  976. if (rc)
  977. goto err_fini;
  978. rc = apei_resources_request(&erst_resources, "APEI ERST");
  979. if (rc)
  980. goto err_fini;
  981. rc = apei_exec_pre_map_gars(&ctx);
  982. if (rc)
  983. goto err_release;
  984. rc = erst_get_erange(&erst_erange);
  985. if (rc) {
  986. if (rc == -ENODEV)
  987. pr_info(ERST_PFX
  988. "The corresponding hardware device or firmware implementation "
  989. "is not available.\n");
  990. else
  991. pr_err(ERST_PFX
  992. "Failed to get Error Log Address Range.\n");
  993. goto err_unmap_reg;
  994. }
  995. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  996. if (!r) {
  997. pr_err(ERST_PFX
  998. "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
  999. (unsigned long long)erst_erange.base,
  1000. (unsigned long long)erst_erange.base + erst_erange.size);
  1001. rc = -EIO;
  1002. goto err_unmap_reg;
  1003. }
  1004. rc = -ENOMEM;
  1005. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  1006. erst_erange.size);
  1007. if (!erst_erange.vaddr)
  1008. goto err_release_erange;
  1009. buf = kmalloc(erst_erange.size, GFP_KERNEL);
  1010. mutex_init(&erst_info.buf_mutex);
  1011. if (buf) {
  1012. erst_info.buf = buf + sizeof(struct cper_pstore_record);
  1013. erst_info.bufsize = erst_erange.size -
  1014. sizeof(struct cper_pstore_record);
  1015. if (pstore_register(&erst_info)) {
  1016. pr_info(ERST_PFX "Could not register with persistent store\n");
  1017. kfree(buf);
  1018. }
  1019. }
  1020. pr_info(ERST_PFX
  1021. "Error Record Serialization Table (ERST) support is initialized.\n");
  1022. return 0;
  1023. err_release_erange:
  1024. release_mem_region(erst_erange.base, erst_erange.size);
  1025. err_unmap_reg:
  1026. apei_exec_post_unmap_gars(&ctx);
  1027. err_release:
  1028. apei_resources_release(&erst_resources);
  1029. err_fini:
  1030. apei_resources_fini(&erst_resources);
  1031. err:
  1032. erst_disable = 1;
  1033. return rc;
  1034. }
  1035. device_initcall(erst_init);