erst.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. #undef pr_fmt
  40. #define pr_fmt(fmt) "ERST: " fmt
  41. /* ERST command status */
  42. #define ERST_STATUS_SUCCESS 0x0
  43. #define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
  44. #define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
  45. #define ERST_STATUS_FAILED 0x3
  46. #define ERST_STATUS_RECORD_STORE_EMPTY 0x4
  47. #define ERST_STATUS_RECORD_NOT_FOUND 0x5
  48. #define ERST_TAB_ENTRY(tab) \
  49. ((struct acpi_whea_header *)((char *)(tab) + \
  50. sizeof(struct acpi_table_erst)))
  51. #define SPIN_UNIT 100 /* 100ns */
  52. /* Firmware should respond within 1 milliseconds */
  53. #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
  54. #define FIRMWARE_MAX_STALL 50 /* 50us */
  55. int erst_disable;
  56. EXPORT_SYMBOL_GPL(erst_disable);
  57. static struct acpi_table_erst *erst_tab;
  58. /* ERST Error Log Address Range atrributes */
  59. #define ERST_RANGE_RESERVED 0x0001
  60. #define ERST_RANGE_NVRAM 0x0002
  61. #define ERST_RANGE_SLOW 0x0004
  62. /*
  63. * ERST Error Log Address Range, used as buffer for reading/writing
  64. * error records.
  65. */
  66. static struct erst_erange {
  67. u64 base;
  68. u64 size;
  69. void __iomem *vaddr;
  70. u32 attr;
  71. } erst_erange;
  72. /*
  73. * Prevent ERST interpreter to run simultaneously, because the
  74. * corresponding firmware implementation may not work properly when
  75. * invoked simultaneously.
  76. *
  77. * It is used to provide exclusive accessing for ERST Error Log
  78. * Address Range too.
  79. */
  80. static DEFINE_RAW_SPINLOCK(erst_lock);
  81. static inline int erst_errno(int command_status)
  82. {
  83. switch (command_status) {
  84. case ERST_STATUS_SUCCESS:
  85. return 0;
  86. case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
  87. return -ENODEV;
  88. case ERST_STATUS_NOT_ENOUGH_SPACE:
  89. return -ENOSPC;
  90. case ERST_STATUS_RECORD_STORE_EMPTY:
  91. case ERST_STATUS_RECORD_NOT_FOUND:
  92. return -ENOENT;
  93. default:
  94. return -EINVAL;
  95. }
  96. }
  97. static int erst_timedout(u64 *t, u64 spin_unit)
  98. {
  99. if ((s64)*t < spin_unit) {
  100. pr_warn(FW_WARN "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_warn(FW_WARN
  166. "Too long stall time for stall instruction: 0x%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_warn(FW_WARN
  184. "Too long stall time for stall while true instruction: 0x%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_warn("MOVE_DATA can not be used in interrupt context.\n");
  240. return -EBUSY;
  241. }
  242. rc = __apei_exec_read_register(entry, &offset);
  243. if (rc)
  244. return rc;
  245. src = ioremap(ctx->src_base + offset, ctx->var2);
  246. if (!src)
  247. return -ENOMEM;
  248. dst = ioremap(ctx->dst_base + offset, ctx->var2);
  249. if (!dst) {
  250. iounmap(src);
  251. return -ENOMEM;
  252. }
  253. memmove(dst, src, ctx->var2);
  254. iounmap(src);
  255. iounmap(dst);
  256. return 0;
  257. }
  258. static struct apei_exec_ins_type erst_ins_type[] = {
  259. [ACPI_ERST_READ_REGISTER] = {
  260. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  261. .run = apei_exec_read_register,
  262. },
  263. [ACPI_ERST_READ_REGISTER_VALUE] = {
  264. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  265. .run = apei_exec_read_register_value,
  266. },
  267. [ACPI_ERST_WRITE_REGISTER] = {
  268. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  269. .run = apei_exec_write_register,
  270. },
  271. [ACPI_ERST_WRITE_REGISTER_VALUE] = {
  272. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  273. .run = apei_exec_write_register_value,
  274. },
  275. [ACPI_ERST_NOOP] = {
  276. .flags = 0,
  277. .run = apei_exec_noop,
  278. },
  279. [ACPI_ERST_LOAD_VAR1] = {
  280. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  281. .run = erst_exec_load_var1,
  282. },
  283. [ACPI_ERST_LOAD_VAR2] = {
  284. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  285. .run = erst_exec_load_var2,
  286. },
  287. [ACPI_ERST_STORE_VAR1] = {
  288. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  289. .run = erst_exec_store_var1,
  290. },
  291. [ACPI_ERST_ADD] = {
  292. .flags = 0,
  293. .run = erst_exec_add,
  294. },
  295. [ACPI_ERST_SUBTRACT] = {
  296. .flags = 0,
  297. .run = erst_exec_subtract,
  298. },
  299. [ACPI_ERST_ADD_VALUE] = {
  300. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  301. .run = erst_exec_add_value,
  302. },
  303. [ACPI_ERST_SUBTRACT_VALUE] = {
  304. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  305. .run = erst_exec_subtract_value,
  306. },
  307. [ACPI_ERST_STALL] = {
  308. .flags = 0,
  309. .run = erst_exec_stall,
  310. },
  311. [ACPI_ERST_STALL_WHILE_TRUE] = {
  312. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  313. .run = erst_exec_stall_while_true,
  314. },
  315. [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
  316. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  317. .run = erst_exec_skip_next_instruction_if_true,
  318. },
  319. [ACPI_ERST_GOTO] = {
  320. .flags = 0,
  321. .run = erst_exec_goto,
  322. },
  323. [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
  324. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  325. .run = erst_exec_set_src_address_base,
  326. },
  327. [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
  328. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  329. .run = erst_exec_set_dst_address_base,
  330. },
  331. [ACPI_ERST_MOVE_DATA] = {
  332. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  333. .run = erst_exec_move_data,
  334. },
  335. };
  336. static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
  337. {
  338. apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
  339. ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
  340. }
  341. static int erst_get_erange(struct erst_erange *range)
  342. {
  343. struct apei_exec_context ctx;
  344. int rc;
  345. erst_exec_ctx_init(&ctx);
  346. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
  347. if (rc)
  348. return rc;
  349. range->base = apei_exec_ctx_get_output(&ctx);
  350. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
  351. if (rc)
  352. return rc;
  353. range->size = apei_exec_ctx_get_output(&ctx);
  354. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
  355. if (rc)
  356. return rc;
  357. range->attr = apei_exec_ctx_get_output(&ctx);
  358. return 0;
  359. }
  360. static ssize_t __erst_get_record_count(void)
  361. {
  362. struct apei_exec_context ctx;
  363. int rc;
  364. erst_exec_ctx_init(&ctx);
  365. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
  366. if (rc)
  367. return rc;
  368. return apei_exec_ctx_get_output(&ctx);
  369. }
  370. ssize_t erst_get_record_count(void)
  371. {
  372. ssize_t count;
  373. unsigned long flags;
  374. if (erst_disable)
  375. return -ENODEV;
  376. raw_spin_lock_irqsave(&erst_lock, flags);
  377. count = __erst_get_record_count();
  378. raw_spin_unlock_irqrestore(&erst_lock, flags);
  379. return count;
  380. }
  381. EXPORT_SYMBOL_GPL(erst_get_record_count);
  382. #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
  383. #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
  384. struct erst_record_id_cache {
  385. struct mutex lock;
  386. u64 *entries;
  387. int len;
  388. int size;
  389. int refcount;
  390. };
  391. static struct erst_record_id_cache erst_record_id_cache = {
  392. .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
  393. .refcount = 0,
  394. };
  395. static int __erst_get_next_record_id(u64 *record_id)
  396. {
  397. struct apei_exec_context ctx;
  398. int rc;
  399. erst_exec_ctx_init(&ctx);
  400. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
  401. if (rc)
  402. return rc;
  403. *record_id = apei_exec_ctx_get_output(&ctx);
  404. return 0;
  405. }
  406. int erst_get_record_id_begin(int *pos)
  407. {
  408. int rc;
  409. if (erst_disable)
  410. return -ENODEV;
  411. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  412. if (rc)
  413. return rc;
  414. erst_record_id_cache.refcount++;
  415. mutex_unlock(&erst_record_id_cache.lock);
  416. *pos = 0;
  417. return 0;
  418. }
  419. EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
  420. /* erst_record_id_cache.lock must be held by caller */
  421. static int __erst_record_id_cache_add_one(void)
  422. {
  423. u64 id, prev_id, first_id;
  424. int i, rc;
  425. u64 *entries;
  426. unsigned long flags;
  427. id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
  428. retry:
  429. raw_spin_lock_irqsave(&erst_lock, flags);
  430. rc = __erst_get_next_record_id(&id);
  431. raw_spin_unlock_irqrestore(&erst_lock, flags);
  432. if (rc == -ENOENT)
  433. return 0;
  434. if (rc)
  435. return rc;
  436. if (id == APEI_ERST_INVALID_RECORD_ID)
  437. return 0;
  438. /* can not skip current ID, or loop back to first ID */
  439. if (id == prev_id || id == first_id)
  440. return 0;
  441. if (first_id == APEI_ERST_INVALID_RECORD_ID)
  442. first_id = id;
  443. prev_id = id;
  444. entries = erst_record_id_cache.entries;
  445. for (i = 0; i < erst_record_id_cache.len; i++) {
  446. if (entries[i] == id)
  447. break;
  448. }
  449. /* record id already in cache, try next */
  450. if (i < erst_record_id_cache.len)
  451. goto retry;
  452. if (erst_record_id_cache.len >= erst_record_id_cache.size) {
  453. int new_size, alloc_size;
  454. u64 *new_entries;
  455. new_size = erst_record_id_cache.size * 2;
  456. new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
  457. ERST_RECORD_ID_CACHE_SIZE_MAX);
  458. if (new_size <= erst_record_id_cache.size) {
  459. if (printk_ratelimit())
  460. pr_warn(FW_WARN "too many record IDs!\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. entries[wpos] = 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_optional(&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_optional(&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_optional(&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_optional(&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_optional(&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_optional(&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_warn("NVRAM ERST Log Address Range not implemented yet.\n");
  675. }
  676. static int __erst_write_to_nvram(const struct cper_record_header *record)
  677. {
  678. /* do not print message, because printk is not safe for NMI */
  679. return -ENOSYS;
  680. }
  681. static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
  682. {
  683. pr_unimpl_nvram();
  684. return -ENOSYS;
  685. }
  686. static int __erst_clear_from_nvram(u64 record_id)
  687. {
  688. pr_unimpl_nvram();
  689. return -ENOSYS;
  690. }
  691. int erst_write(const struct cper_record_header *record)
  692. {
  693. int rc;
  694. unsigned long flags;
  695. struct cper_record_header *rcd_erange;
  696. if (erst_disable)
  697. return -ENODEV;
  698. if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
  699. return -EINVAL;
  700. if (erst_erange.attr & ERST_RANGE_NVRAM) {
  701. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  702. return -EBUSY;
  703. rc = __erst_write_to_nvram(record);
  704. raw_spin_unlock_irqrestore(&erst_lock, flags);
  705. return rc;
  706. }
  707. if (record->record_length > erst_erange.size)
  708. return -EINVAL;
  709. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  710. return -EBUSY;
  711. memcpy(erst_erange.vaddr, record, record->record_length);
  712. rcd_erange = erst_erange.vaddr;
  713. /* signature for serialization system */
  714. memcpy(&rcd_erange->persistence_information, "ER", 2);
  715. rc = __erst_write_to_storage(0);
  716. raw_spin_unlock_irqrestore(&erst_lock, flags);
  717. return rc;
  718. }
  719. EXPORT_SYMBOL_GPL(erst_write);
  720. static int __erst_read_to_erange(u64 record_id, u64 *offset)
  721. {
  722. int rc;
  723. if (erst_erange.attr & ERST_RANGE_NVRAM)
  724. return __erst_read_to_erange_from_nvram(
  725. record_id, offset);
  726. rc = __erst_read_from_storage(record_id, 0);
  727. if (rc)
  728. return rc;
  729. *offset = 0;
  730. return 0;
  731. }
  732. static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
  733. size_t buflen)
  734. {
  735. int rc;
  736. u64 offset, len = 0;
  737. struct cper_record_header *rcd_tmp;
  738. rc = __erst_read_to_erange(record_id, &offset);
  739. if (rc)
  740. return rc;
  741. rcd_tmp = erst_erange.vaddr + offset;
  742. len = rcd_tmp->record_length;
  743. if (len <= buflen)
  744. memcpy(record, rcd_tmp, len);
  745. return len;
  746. }
  747. /*
  748. * If return value > buflen, the buffer size is not big enough,
  749. * else if return value < 0, something goes wrong,
  750. * else everything is OK, and return value is record length
  751. */
  752. ssize_t erst_read(u64 record_id, struct cper_record_header *record,
  753. size_t buflen)
  754. {
  755. ssize_t len;
  756. unsigned long flags;
  757. if (erst_disable)
  758. return -ENODEV;
  759. raw_spin_lock_irqsave(&erst_lock, flags);
  760. len = __erst_read(record_id, record, buflen);
  761. raw_spin_unlock_irqrestore(&erst_lock, flags);
  762. return len;
  763. }
  764. EXPORT_SYMBOL_GPL(erst_read);
  765. int erst_clear(u64 record_id)
  766. {
  767. int rc, i;
  768. unsigned long flags;
  769. u64 *entries;
  770. if (erst_disable)
  771. return -ENODEV;
  772. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  773. if (rc)
  774. return rc;
  775. raw_spin_lock_irqsave(&erst_lock, flags);
  776. if (erst_erange.attr & ERST_RANGE_NVRAM)
  777. rc = __erst_clear_from_nvram(record_id);
  778. else
  779. rc = __erst_clear_from_storage(record_id);
  780. raw_spin_unlock_irqrestore(&erst_lock, flags);
  781. if (rc)
  782. goto out;
  783. entries = erst_record_id_cache.entries;
  784. for (i = 0; i < erst_record_id_cache.len; i++) {
  785. if (entries[i] == record_id)
  786. entries[i] = APEI_ERST_INVALID_RECORD_ID;
  787. }
  788. __erst_record_id_cache_compact();
  789. out:
  790. mutex_unlock(&erst_record_id_cache.lock);
  791. return rc;
  792. }
  793. EXPORT_SYMBOL_GPL(erst_clear);
  794. static int __init setup_erst_disable(char *str)
  795. {
  796. erst_disable = 1;
  797. return 0;
  798. }
  799. __setup("erst_disable", setup_erst_disable);
  800. static int erst_check_table(struct acpi_table_erst *erst_tab)
  801. {
  802. if ((erst_tab->header_length !=
  803. (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
  804. && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
  805. return -EINVAL;
  806. if (erst_tab->header.length < sizeof(struct acpi_table_erst))
  807. return -EINVAL;
  808. if (erst_tab->entries !=
  809. (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
  810. sizeof(struct acpi_erst_entry))
  811. return -EINVAL;
  812. return 0;
  813. }
  814. static int erst_open_pstore(struct pstore_info *psi);
  815. static int erst_close_pstore(struct pstore_info *psi);
  816. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
  817. struct timespec *time, char **buf,
  818. bool *compressed, struct pstore_info *psi);
  819. static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
  820. u64 *id, unsigned int part, int count, bool compressed,
  821. size_t size, struct pstore_info *psi);
  822. static int erst_clearer(enum pstore_type_id type, u64 id, int count,
  823. struct timespec time, struct pstore_info *psi);
  824. static struct pstore_info erst_info = {
  825. .owner = THIS_MODULE,
  826. .name = "erst",
  827. .flags = PSTORE_FLAGS_FRAGILE,
  828. .open = erst_open_pstore,
  829. .close = erst_close_pstore,
  830. .read = erst_reader,
  831. .write = erst_writer,
  832. .erase = erst_clearer
  833. };
  834. #define CPER_CREATOR_PSTORE \
  835. UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
  836. 0x64, 0x90, 0xb8, 0x9d)
  837. #define CPER_SECTION_TYPE_DMESG \
  838. UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
  839. 0x94, 0x19, 0xeb, 0x12)
  840. #define CPER_SECTION_TYPE_DMESG_Z \
  841. UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
  842. 0x34, 0xdd, 0xfa, 0xc6)
  843. #define CPER_SECTION_TYPE_MCE \
  844. UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  845. 0x04, 0x4a, 0x38, 0xfc)
  846. struct cper_pstore_record {
  847. struct cper_record_header hdr;
  848. struct cper_section_descriptor sec_hdr;
  849. char data[];
  850. } __packed;
  851. static int reader_pos;
  852. static int erst_open_pstore(struct pstore_info *psi)
  853. {
  854. int rc;
  855. if (erst_disable)
  856. return -ENODEV;
  857. rc = erst_get_record_id_begin(&reader_pos);
  858. return rc;
  859. }
  860. static int erst_close_pstore(struct pstore_info *psi)
  861. {
  862. erst_get_record_id_end();
  863. return 0;
  864. }
  865. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
  866. struct timespec *time, char **buf,
  867. bool *compressed, struct pstore_info *psi)
  868. {
  869. int rc;
  870. ssize_t len = 0;
  871. u64 record_id;
  872. struct cper_pstore_record *rcd;
  873. size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
  874. if (erst_disable)
  875. return -ENODEV;
  876. rcd = kmalloc(rcd_len, GFP_KERNEL);
  877. if (!rcd) {
  878. rc = -ENOMEM;
  879. goto out;
  880. }
  881. skip:
  882. rc = erst_get_record_id_next(&reader_pos, &record_id);
  883. if (rc)
  884. goto out;
  885. /* no more record */
  886. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  887. rc = -EINVAL;
  888. goto out;
  889. }
  890. len = erst_read(record_id, &rcd->hdr, rcd_len);
  891. /* The record may be cleared by others, try read next record */
  892. if (len == -ENOENT)
  893. goto skip;
  894. else if (len < sizeof(*rcd)) {
  895. rc = -EIO;
  896. goto out;
  897. }
  898. if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
  899. goto skip;
  900. *buf = kmalloc(len, GFP_KERNEL);
  901. if (*buf == NULL) {
  902. rc = -ENOMEM;
  903. goto out;
  904. }
  905. memcpy(*buf, rcd->data, len - sizeof(*rcd));
  906. *id = record_id;
  907. *compressed = false;
  908. if (uuid_le_cmp(rcd->sec_hdr.section_type,
  909. CPER_SECTION_TYPE_DMESG_Z) == 0) {
  910. *type = PSTORE_TYPE_DMESG;
  911. *compressed = true;
  912. } else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  913. CPER_SECTION_TYPE_DMESG) == 0)
  914. *type = PSTORE_TYPE_DMESG;
  915. else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  916. CPER_SECTION_TYPE_MCE) == 0)
  917. *type = PSTORE_TYPE_MCE;
  918. else
  919. *type = PSTORE_TYPE_UNKNOWN;
  920. if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
  921. time->tv_sec = rcd->hdr.timestamp;
  922. else
  923. time->tv_sec = 0;
  924. time->tv_nsec = 0;
  925. out:
  926. kfree(rcd);
  927. return (rc < 0) ? rc : (len - sizeof(*rcd));
  928. }
  929. static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
  930. u64 *id, unsigned int part, int count, bool compressed,
  931. size_t size, struct pstore_info *psi)
  932. {
  933. struct cper_pstore_record *rcd = (struct cper_pstore_record *)
  934. (erst_info.buf - sizeof(*rcd));
  935. int ret;
  936. memset(rcd, 0, sizeof(*rcd));
  937. memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  938. rcd->hdr.revision = CPER_RECORD_REV;
  939. rcd->hdr.signature_end = CPER_SIG_END;
  940. rcd->hdr.section_count = 1;
  941. rcd->hdr.error_severity = CPER_SEV_FATAL;
  942. /* timestamp valid. platform_id, partition_id are invalid */
  943. rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
  944. rcd->hdr.timestamp = get_seconds();
  945. rcd->hdr.record_length = sizeof(*rcd) + size;
  946. rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
  947. rcd->hdr.notification_type = CPER_NOTIFY_MCE;
  948. rcd->hdr.record_id = cper_next_record_id();
  949. rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  950. rcd->sec_hdr.section_offset = sizeof(*rcd);
  951. rcd->sec_hdr.section_length = size;
  952. rcd->sec_hdr.revision = CPER_SEC_REV;
  953. /* fru_id and fru_text is invalid */
  954. rcd->sec_hdr.validation_bits = 0;
  955. rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
  956. switch (type) {
  957. case PSTORE_TYPE_DMESG:
  958. if (compressed)
  959. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
  960. else
  961. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
  962. break;
  963. case PSTORE_TYPE_MCE:
  964. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  965. break;
  966. default:
  967. return -EINVAL;
  968. }
  969. rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
  970. ret = erst_write(&rcd->hdr);
  971. *id = rcd->hdr.record_id;
  972. return ret;
  973. }
  974. static int erst_clearer(enum pstore_type_id type, u64 id, int count,
  975. struct timespec time, struct pstore_info *psi)
  976. {
  977. return erst_clear(id);
  978. }
  979. static int __init erst_init(void)
  980. {
  981. int rc = 0;
  982. acpi_status status;
  983. struct apei_exec_context ctx;
  984. struct apei_resources erst_resources;
  985. struct resource *r;
  986. char *buf;
  987. if (acpi_disabled)
  988. goto err;
  989. if (erst_disable) {
  990. pr_info(
  991. "Error Record Serialization Table (ERST) support is disabled.\n");
  992. goto err;
  993. }
  994. status = acpi_get_table(ACPI_SIG_ERST, 0,
  995. (struct acpi_table_header **)&erst_tab);
  996. if (status == AE_NOT_FOUND)
  997. goto err;
  998. else if (ACPI_FAILURE(status)) {
  999. const char *msg = acpi_format_exception(status);
  1000. pr_err("Failed to get table, %s\n", msg);
  1001. rc = -EINVAL;
  1002. goto err;
  1003. }
  1004. rc = erst_check_table(erst_tab);
  1005. if (rc) {
  1006. pr_err(FW_BUG "ERST table is invalid.\n");
  1007. goto err;
  1008. }
  1009. apei_resources_init(&erst_resources);
  1010. erst_exec_ctx_init(&ctx);
  1011. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  1012. if (rc)
  1013. goto err_fini;
  1014. rc = apei_resources_request(&erst_resources, "APEI ERST");
  1015. if (rc)
  1016. goto err_fini;
  1017. rc = apei_exec_pre_map_gars(&ctx);
  1018. if (rc)
  1019. goto err_release;
  1020. rc = erst_get_erange(&erst_erange);
  1021. if (rc) {
  1022. if (rc == -ENODEV)
  1023. pr_info(
  1024. "The corresponding hardware device or firmware implementation "
  1025. "is not available.\n");
  1026. else
  1027. pr_err("Failed to get Error Log Address Range.\n");
  1028. goto err_unmap_reg;
  1029. }
  1030. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  1031. if (!r) {
  1032. pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
  1033. (unsigned long long)erst_erange.base,
  1034. (unsigned long long)erst_erange.base + erst_erange.size - 1);
  1035. rc = -EIO;
  1036. goto err_unmap_reg;
  1037. }
  1038. rc = -ENOMEM;
  1039. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  1040. erst_erange.size);
  1041. if (!erst_erange.vaddr)
  1042. goto err_release_erange;
  1043. pr_info(
  1044. "Error Record Serialization Table (ERST) support is initialized.\n");
  1045. buf = kmalloc(erst_erange.size, GFP_KERNEL);
  1046. spin_lock_init(&erst_info.buf_lock);
  1047. if (buf) {
  1048. erst_info.buf = buf + sizeof(struct cper_pstore_record);
  1049. erst_info.bufsize = erst_erange.size -
  1050. sizeof(struct cper_pstore_record);
  1051. rc = pstore_register(&erst_info);
  1052. if (rc) {
  1053. if (rc != -EPERM)
  1054. pr_info(
  1055. "Could not register with persistent store.\n");
  1056. erst_info.buf = NULL;
  1057. erst_info.bufsize = 0;
  1058. kfree(buf);
  1059. }
  1060. } else
  1061. pr_err(
  1062. "Failed to allocate %lld bytes for persistent store error log.\n",
  1063. erst_erange.size);
  1064. return 0;
  1065. err_release_erange:
  1066. release_mem_region(erst_erange.base, erst_erange.size);
  1067. err_unmap_reg:
  1068. apei_exec_post_unmap_gars(&ctx);
  1069. err_release:
  1070. apei_resources_release(&erst_resources);
  1071. err_fini:
  1072. apei_resources_fini(&erst_resources);
  1073. err:
  1074. erst_disable = 1;
  1075. return rc;
  1076. }
  1077. device_initcall(erst_init);