erst.c 29 KB

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