erst.c 29 KB

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