erst.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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_MCE \
  843. UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  844. 0x04, 0x4a, 0x38, 0xfc)
  845. struct cper_pstore_record {
  846. struct cper_record_header hdr;
  847. struct cper_section_descriptor sec_hdr;
  848. char data[];
  849. } __packed;
  850. static int reader_pos;
  851. static int erst_open_pstore(struct pstore_info *psi)
  852. {
  853. int rc;
  854. if (erst_disable)
  855. return -ENODEV;
  856. rc = erst_get_record_id_begin(&reader_pos);
  857. return rc;
  858. }
  859. static int erst_close_pstore(struct pstore_info *psi)
  860. {
  861. erst_get_record_id_end();
  862. return 0;
  863. }
  864. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
  865. struct timespec *time, char **buf,
  866. bool *compressed, struct pstore_info *psi)
  867. {
  868. int rc;
  869. ssize_t len = 0;
  870. u64 record_id;
  871. struct cper_pstore_record *rcd;
  872. size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
  873. if (erst_disable)
  874. return -ENODEV;
  875. rcd = kmalloc(rcd_len, GFP_KERNEL);
  876. if (!rcd) {
  877. rc = -ENOMEM;
  878. goto out;
  879. }
  880. skip:
  881. rc = erst_get_record_id_next(&reader_pos, &record_id);
  882. if (rc)
  883. goto out;
  884. /* no more record */
  885. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  886. rc = -EINVAL;
  887. goto out;
  888. }
  889. len = erst_read(record_id, &rcd->hdr, rcd_len);
  890. /* The record may be cleared by others, try read next record */
  891. if (len == -ENOENT)
  892. goto skip;
  893. else if (len < sizeof(*rcd)) {
  894. rc = -EIO;
  895. goto out;
  896. }
  897. if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
  898. goto skip;
  899. *buf = kmalloc(len, GFP_KERNEL);
  900. if (*buf == NULL) {
  901. rc = -ENOMEM;
  902. goto out;
  903. }
  904. memcpy(*buf, rcd->data, len - sizeof(*rcd));
  905. *id = record_id;
  906. if (uuid_le_cmp(rcd->sec_hdr.section_type,
  907. CPER_SECTION_TYPE_DMESG) == 0)
  908. *type = PSTORE_TYPE_DMESG;
  909. else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  910. CPER_SECTION_TYPE_MCE) == 0)
  911. *type = PSTORE_TYPE_MCE;
  912. else
  913. *type = PSTORE_TYPE_UNKNOWN;
  914. if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
  915. time->tv_sec = rcd->hdr.timestamp;
  916. else
  917. time->tv_sec = 0;
  918. time->tv_nsec = 0;
  919. out:
  920. kfree(rcd);
  921. return (rc < 0) ? rc : (len - sizeof(*rcd));
  922. }
  923. static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
  924. u64 *id, unsigned int part, int count, bool compressed,
  925. size_t size, struct pstore_info *psi)
  926. {
  927. struct cper_pstore_record *rcd = (struct cper_pstore_record *)
  928. (erst_info.buf - sizeof(*rcd));
  929. int ret;
  930. memset(rcd, 0, sizeof(*rcd));
  931. memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  932. rcd->hdr.revision = CPER_RECORD_REV;
  933. rcd->hdr.signature_end = CPER_SIG_END;
  934. rcd->hdr.section_count = 1;
  935. rcd->hdr.error_severity = CPER_SEV_FATAL;
  936. /* timestamp valid. platform_id, partition_id are invalid */
  937. rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
  938. rcd->hdr.timestamp = get_seconds();
  939. rcd->hdr.record_length = sizeof(*rcd) + size;
  940. rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
  941. rcd->hdr.notification_type = CPER_NOTIFY_MCE;
  942. rcd->hdr.record_id = cper_next_record_id();
  943. rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  944. rcd->sec_hdr.section_offset = sizeof(*rcd);
  945. rcd->sec_hdr.section_length = size;
  946. rcd->sec_hdr.revision = CPER_SEC_REV;
  947. /* fru_id and fru_text is invalid */
  948. rcd->sec_hdr.validation_bits = 0;
  949. rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
  950. switch (type) {
  951. case PSTORE_TYPE_DMESG:
  952. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
  953. break;
  954. case PSTORE_TYPE_MCE:
  955. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  956. break;
  957. default:
  958. return -EINVAL;
  959. }
  960. rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
  961. ret = erst_write(&rcd->hdr);
  962. *id = rcd->hdr.record_id;
  963. return ret;
  964. }
  965. static int erst_clearer(enum pstore_type_id type, u64 id, int count,
  966. struct timespec time, struct pstore_info *psi)
  967. {
  968. return erst_clear(id);
  969. }
  970. static int __init erst_init(void)
  971. {
  972. int rc = 0;
  973. acpi_status status;
  974. struct apei_exec_context ctx;
  975. struct apei_resources erst_resources;
  976. struct resource *r;
  977. char *buf;
  978. if (acpi_disabled)
  979. goto err;
  980. if (erst_disable) {
  981. pr_info(ERST_PFX
  982. "Error Record Serialization Table (ERST) support is disabled.\n");
  983. goto err;
  984. }
  985. status = acpi_get_table(ACPI_SIG_ERST, 0,
  986. (struct acpi_table_header **)&erst_tab);
  987. if (status == AE_NOT_FOUND)
  988. goto err;
  989. else if (ACPI_FAILURE(status)) {
  990. const char *msg = acpi_format_exception(status);
  991. pr_err(ERST_PFX "Failed to get table, %s\n", msg);
  992. rc = -EINVAL;
  993. goto err;
  994. }
  995. rc = erst_check_table(erst_tab);
  996. if (rc) {
  997. pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
  998. goto err;
  999. }
  1000. apei_resources_init(&erst_resources);
  1001. erst_exec_ctx_init(&ctx);
  1002. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  1003. if (rc)
  1004. goto err_fini;
  1005. rc = apei_resources_request(&erst_resources, "APEI ERST");
  1006. if (rc)
  1007. goto err_fini;
  1008. rc = apei_exec_pre_map_gars(&ctx);
  1009. if (rc)
  1010. goto err_release;
  1011. rc = erst_get_erange(&erst_erange);
  1012. if (rc) {
  1013. if (rc == -ENODEV)
  1014. pr_info(ERST_PFX
  1015. "The corresponding hardware device or firmware implementation "
  1016. "is not available.\n");
  1017. else
  1018. pr_err(ERST_PFX
  1019. "Failed to get Error Log Address Range.\n");
  1020. goto err_unmap_reg;
  1021. }
  1022. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  1023. if (!r) {
  1024. pr_err(ERST_PFX
  1025. "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
  1026. (unsigned long long)erst_erange.base,
  1027. (unsigned long long)erst_erange.base + erst_erange.size);
  1028. rc = -EIO;
  1029. goto err_unmap_reg;
  1030. }
  1031. rc = -ENOMEM;
  1032. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  1033. erst_erange.size);
  1034. if (!erst_erange.vaddr)
  1035. goto err_release_erange;
  1036. pr_info(ERST_PFX
  1037. "Error Record Serialization Table (ERST) support is initialized.\n");
  1038. buf = kmalloc(erst_erange.size, GFP_KERNEL);
  1039. spin_lock_init(&erst_info.buf_lock);
  1040. if (buf) {
  1041. erst_info.buf = buf + sizeof(struct cper_pstore_record);
  1042. erst_info.bufsize = erst_erange.size -
  1043. sizeof(struct cper_pstore_record);
  1044. rc = pstore_register(&erst_info);
  1045. if (rc) {
  1046. if (rc != -EPERM)
  1047. pr_info(ERST_PFX
  1048. "Could not register with persistent store\n");
  1049. erst_info.buf = NULL;
  1050. erst_info.bufsize = 0;
  1051. kfree(buf);
  1052. }
  1053. } else
  1054. pr_err(ERST_PFX
  1055. "Failed to allocate %lld bytes for persistent store error log\n",
  1056. erst_erange.size);
  1057. return 0;
  1058. err_release_erange:
  1059. release_mem_region(erst_erange.base, erst_erange.size);
  1060. err_unmap_reg:
  1061. apei_exec_post_unmap_gars(&ctx);
  1062. err_release:
  1063. apei_resources_release(&erst_resources);
  1064. err_fini:
  1065. apei_resources_fini(&erst_resources);
  1066. err:
  1067. erst_disable = 1;
  1068. return rc;
  1069. }
  1070. device_initcall(erst_init);