erst.c 29 KB

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