erst.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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. return -ENOMEM;
  252. memmove(dst, src, ctx->var2);
  253. iounmap(src);
  254. iounmap(dst);
  255. return 0;
  256. }
  257. static struct apei_exec_ins_type erst_ins_type[] = {
  258. [ACPI_ERST_READ_REGISTER] = {
  259. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  260. .run = apei_exec_read_register,
  261. },
  262. [ACPI_ERST_READ_REGISTER_VALUE] = {
  263. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  264. .run = apei_exec_read_register_value,
  265. },
  266. [ACPI_ERST_WRITE_REGISTER] = {
  267. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  268. .run = apei_exec_write_register,
  269. },
  270. [ACPI_ERST_WRITE_REGISTER_VALUE] = {
  271. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  272. .run = apei_exec_write_register_value,
  273. },
  274. [ACPI_ERST_NOOP] = {
  275. .flags = 0,
  276. .run = apei_exec_noop,
  277. },
  278. [ACPI_ERST_LOAD_VAR1] = {
  279. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  280. .run = erst_exec_load_var1,
  281. },
  282. [ACPI_ERST_LOAD_VAR2] = {
  283. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  284. .run = erst_exec_load_var2,
  285. },
  286. [ACPI_ERST_STORE_VAR1] = {
  287. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  288. .run = erst_exec_store_var1,
  289. },
  290. [ACPI_ERST_ADD] = {
  291. .flags = 0,
  292. .run = erst_exec_add,
  293. },
  294. [ACPI_ERST_SUBTRACT] = {
  295. .flags = 0,
  296. .run = erst_exec_subtract,
  297. },
  298. [ACPI_ERST_ADD_VALUE] = {
  299. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  300. .run = erst_exec_add_value,
  301. },
  302. [ACPI_ERST_SUBTRACT_VALUE] = {
  303. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  304. .run = erst_exec_subtract_value,
  305. },
  306. [ACPI_ERST_STALL] = {
  307. .flags = 0,
  308. .run = erst_exec_stall,
  309. },
  310. [ACPI_ERST_STALL_WHILE_TRUE] = {
  311. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  312. .run = erst_exec_stall_while_true,
  313. },
  314. [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
  315. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  316. .run = erst_exec_skip_next_instruction_if_true,
  317. },
  318. [ACPI_ERST_GOTO] = {
  319. .flags = 0,
  320. .run = erst_exec_goto,
  321. },
  322. [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
  323. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  324. .run = erst_exec_set_src_address_base,
  325. },
  326. [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
  327. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  328. .run = erst_exec_set_dst_address_base,
  329. },
  330. [ACPI_ERST_MOVE_DATA] = {
  331. .flags = APEI_EXEC_INS_ACCESS_REGISTER,
  332. .run = erst_exec_move_data,
  333. },
  334. };
  335. static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
  336. {
  337. apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
  338. ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
  339. }
  340. static int erst_get_erange(struct erst_erange *range)
  341. {
  342. struct apei_exec_context ctx;
  343. int rc;
  344. erst_exec_ctx_init(&ctx);
  345. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
  346. if (rc)
  347. return rc;
  348. range->base = apei_exec_ctx_get_output(&ctx);
  349. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
  350. if (rc)
  351. return rc;
  352. range->size = apei_exec_ctx_get_output(&ctx);
  353. rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
  354. if (rc)
  355. return rc;
  356. range->attr = apei_exec_ctx_get_output(&ctx);
  357. return 0;
  358. }
  359. static ssize_t __erst_get_record_count(void)
  360. {
  361. struct apei_exec_context ctx;
  362. int rc;
  363. erst_exec_ctx_init(&ctx);
  364. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
  365. if (rc)
  366. return rc;
  367. return apei_exec_ctx_get_output(&ctx);
  368. }
  369. ssize_t erst_get_record_count(void)
  370. {
  371. ssize_t count;
  372. unsigned long flags;
  373. if (erst_disable)
  374. return -ENODEV;
  375. raw_spin_lock_irqsave(&erst_lock, flags);
  376. count = __erst_get_record_count();
  377. raw_spin_unlock_irqrestore(&erst_lock, flags);
  378. return count;
  379. }
  380. EXPORT_SYMBOL_GPL(erst_get_record_count);
  381. #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
  382. #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
  383. struct erst_record_id_cache {
  384. struct mutex lock;
  385. u64 *entries;
  386. int len;
  387. int size;
  388. int refcount;
  389. };
  390. static struct erst_record_id_cache erst_record_id_cache = {
  391. .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
  392. .refcount = 0,
  393. };
  394. static int __erst_get_next_record_id(u64 *record_id)
  395. {
  396. struct apei_exec_context ctx;
  397. int rc;
  398. erst_exec_ctx_init(&ctx);
  399. rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
  400. if (rc)
  401. return rc;
  402. *record_id = apei_exec_ctx_get_output(&ctx);
  403. return 0;
  404. }
  405. int erst_get_record_id_begin(int *pos)
  406. {
  407. int rc;
  408. if (erst_disable)
  409. return -ENODEV;
  410. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  411. if (rc)
  412. return rc;
  413. erst_record_id_cache.refcount++;
  414. mutex_unlock(&erst_record_id_cache.lock);
  415. *pos = 0;
  416. return 0;
  417. }
  418. EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
  419. /* erst_record_id_cache.lock must be held by caller */
  420. static int __erst_record_id_cache_add_one(void)
  421. {
  422. u64 id, prev_id, first_id;
  423. int i, rc;
  424. u64 *entries;
  425. unsigned long flags;
  426. id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
  427. retry:
  428. raw_spin_lock_irqsave(&erst_lock, flags);
  429. rc = __erst_get_next_record_id(&id);
  430. raw_spin_unlock_irqrestore(&erst_lock, flags);
  431. if (rc == -ENOENT)
  432. return 0;
  433. if (rc)
  434. return rc;
  435. if (id == APEI_ERST_INVALID_RECORD_ID)
  436. return 0;
  437. /* can not skip current ID, or loop back to first ID */
  438. if (id == prev_id || id == first_id)
  439. return 0;
  440. if (first_id == APEI_ERST_INVALID_RECORD_ID)
  441. first_id = id;
  442. prev_id = id;
  443. entries = erst_record_id_cache.entries;
  444. for (i = 0; i < erst_record_id_cache.len; i++) {
  445. if (entries[i] == id)
  446. break;
  447. }
  448. /* record id already in cache, try next */
  449. if (i < erst_record_id_cache.len)
  450. goto retry;
  451. if (erst_record_id_cache.len >= erst_record_id_cache.size) {
  452. int new_size, alloc_size;
  453. u64 *new_entries;
  454. new_size = erst_record_id_cache.size * 2;
  455. new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
  456. ERST_RECORD_ID_CACHE_SIZE_MAX);
  457. if (new_size <= erst_record_id_cache.size) {
  458. if (printk_ratelimit())
  459. pr_warning(FW_WARN ERST_PFX
  460. "too many record ID!\n");
  461. return 0;
  462. }
  463. alloc_size = new_size * sizeof(entries[0]);
  464. if (alloc_size < PAGE_SIZE)
  465. new_entries = kmalloc(alloc_size, GFP_KERNEL);
  466. else
  467. new_entries = vmalloc(alloc_size);
  468. if (!new_entries)
  469. return -ENOMEM;
  470. memcpy(new_entries, entries,
  471. erst_record_id_cache.len * sizeof(entries[0]));
  472. if (erst_record_id_cache.size < PAGE_SIZE)
  473. kfree(entries);
  474. else
  475. vfree(entries);
  476. erst_record_id_cache.entries = entries = new_entries;
  477. erst_record_id_cache.size = new_size;
  478. }
  479. entries[i] = id;
  480. erst_record_id_cache.len++;
  481. return 1;
  482. }
  483. /*
  484. * Get the record ID of an existing error record on the persistent
  485. * storage. If there is no error record on the persistent storage, the
  486. * returned record_id is APEI_ERST_INVALID_RECORD_ID.
  487. */
  488. int erst_get_record_id_next(int *pos, u64 *record_id)
  489. {
  490. int rc = 0;
  491. u64 *entries;
  492. if (erst_disable)
  493. return -ENODEV;
  494. /* must be enclosed by erst_get_record_id_begin/end */
  495. BUG_ON(!erst_record_id_cache.refcount);
  496. BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
  497. mutex_lock(&erst_record_id_cache.lock);
  498. entries = erst_record_id_cache.entries;
  499. for (; *pos < erst_record_id_cache.len; (*pos)++)
  500. if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
  501. break;
  502. /* found next record id in cache */
  503. if (*pos < erst_record_id_cache.len) {
  504. *record_id = entries[*pos];
  505. (*pos)++;
  506. goto out_unlock;
  507. }
  508. /* Try to add one more record ID to cache */
  509. rc = __erst_record_id_cache_add_one();
  510. if (rc < 0)
  511. goto out_unlock;
  512. /* successfully add one new ID */
  513. if (rc == 1) {
  514. *record_id = erst_record_id_cache.entries[*pos];
  515. (*pos)++;
  516. rc = 0;
  517. } else {
  518. *pos = -1;
  519. *record_id = APEI_ERST_INVALID_RECORD_ID;
  520. }
  521. out_unlock:
  522. mutex_unlock(&erst_record_id_cache.lock);
  523. return rc;
  524. }
  525. EXPORT_SYMBOL_GPL(erst_get_record_id_next);
  526. /* erst_record_id_cache.lock must be held by caller */
  527. static void __erst_record_id_cache_compact(void)
  528. {
  529. int i, wpos = 0;
  530. u64 *entries;
  531. if (erst_record_id_cache.refcount)
  532. return;
  533. entries = erst_record_id_cache.entries;
  534. for (i = 0; i < erst_record_id_cache.len; i++) {
  535. if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
  536. continue;
  537. if (wpos != i)
  538. memcpy(&entries[wpos], &entries[i], sizeof(entries[i]));
  539. wpos++;
  540. }
  541. erst_record_id_cache.len = wpos;
  542. }
  543. void erst_get_record_id_end(void)
  544. {
  545. /*
  546. * erst_disable != 0 should be detected by invoker via the
  547. * return value of erst_get_record_id_begin/next, so this
  548. * function should not be called for erst_disable != 0.
  549. */
  550. BUG_ON(erst_disable);
  551. mutex_lock(&erst_record_id_cache.lock);
  552. erst_record_id_cache.refcount--;
  553. BUG_ON(erst_record_id_cache.refcount < 0);
  554. __erst_record_id_cache_compact();
  555. mutex_unlock(&erst_record_id_cache.lock);
  556. }
  557. EXPORT_SYMBOL_GPL(erst_get_record_id_end);
  558. static int __erst_write_to_storage(u64 offset)
  559. {
  560. struct apei_exec_context ctx;
  561. u64 timeout = FIRMWARE_TIMEOUT;
  562. u64 val;
  563. int rc;
  564. erst_exec_ctx_init(&ctx);
  565. rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
  566. if (rc)
  567. return rc;
  568. apei_exec_ctx_set_input(&ctx, offset);
  569. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  570. if (rc)
  571. return rc;
  572. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  573. if (rc)
  574. return rc;
  575. for (;;) {
  576. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  577. if (rc)
  578. return rc;
  579. val = apei_exec_ctx_get_output(&ctx);
  580. if (!val)
  581. break;
  582. if (erst_timedout(&timeout, SPIN_UNIT))
  583. return -EIO;
  584. }
  585. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  586. if (rc)
  587. return rc;
  588. val = apei_exec_ctx_get_output(&ctx);
  589. rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
  590. if (rc)
  591. return rc;
  592. return erst_errno(val);
  593. }
  594. static int __erst_read_from_storage(u64 record_id, u64 offset)
  595. {
  596. struct apei_exec_context ctx;
  597. u64 timeout = FIRMWARE_TIMEOUT;
  598. u64 val;
  599. int rc;
  600. erst_exec_ctx_init(&ctx);
  601. rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
  602. if (rc)
  603. return rc;
  604. apei_exec_ctx_set_input(&ctx, offset);
  605. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
  606. if (rc)
  607. return rc;
  608. apei_exec_ctx_set_input(&ctx, record_id);
  609. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  610. if (rc)
  611. return rc;
  612. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  613. if (rc)
  614. return rc;
  615. for (;;) {
  616. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  617. if (rc)
  618. return rc;
  619. val = apei_exec_ctx_get_output(&ctx);
  620. if (!val)
  621. break;
  622. if (erst_timedout(&timeout, SPIN_UNIT))
  623. return -EIO;
  624. };
  625. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  626. if (rc)
  627. return rc;
  628. val = apei_exec_ctx_get_output(&ctx);
  629. rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
  630. if (rc)
  631. return rc;
  632. return erst_errno(val);
  633. }
  634. static int __erst_clear_from_storage(u64 record_id)
  635. {
  636. struct apei_exec_context ctx;
  637. u64 timeout = FIRMWARE_TIMEOUT;
  638. u64 val;
  639. int rc;
  640. erst_exec_ctx_init(&ctx);
  641. rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
  642. if (rc)
  643. return rc;
  644. apei_exec_ctx_set_input(&ctx, record_id);
  645. rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
  646. if (rc)
  647. return rc;
  648. rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
  649. if (rc)
  650. return rc;
  651. for (;;) {
  652. rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
  653. if (rc)
  654. return rc;
  655. val = apei_exec_ctx_get_output(&ctx);
  656. if (!val)
  657. break;
  658. if (erst_timedout(&timeout, SPIN_UNIT))
  659. return -EIO;
  660. }
  661. rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
  662. if (rc)
  663. return rc;
  664. val = apei_exec_ctx_get_output(&ctx);
  665. rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
  666. if (rc)
  667. return rc;
  668. return erst_errno(val);
  669. }
  670. /* NVRAM ERST Error Log Address Range is not supported yet */
  671. static void pr_unimpl_nvram(void)
  672. {
  673. if (printk_ratelimit())
  674. pr_warning(ERST_PFX
  675. "NVRAM ERST Log Address Range is not implemented yet\n");
  676. }
  677. static int __erst_write_to_nvram(const struct cper_record_header *record)
  678. {
  679. /* do not print message, because printk is not safe for NMI */
  680. return -ENOSYS;
  681. }
  682. static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
  683. {
  684. pr_unimpl_nvram();
  685. return -ENOSYS;
  686. }
  687. static int __erst_clear_from_nvram(u64 record_id)
  688. {
  689. pr_unimpl_nvram();
  690. return -ENOSYS;
  691. }
  692. int erst_write(const struct cper_record_header *record)
  693. {
  694. int rc;
  695. unsigned long flags;
  696. struct cper_record_header *rcd_erange;
  697. if (erst_disable)
  698. return -ENODEV;
  699. if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
  700. return -EINVAL;
  701. if (erst_erange.attr & ERST_RANGE_NVRAM) {
  702. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  703. return -EBUSY;
  704. rc = __erst_write_to_nvram(record);
  705. raw_spin_unlock_irqrestore(&erst_lock, flags);
  706. return rc;
  707. }
  708. if (record->record_length > erst_erange.size)
  709. return -EINVAL;
  710. if (!raw_spin_trylock_irqsave(&erst_lock, flags))
  711. return -EBUSY;
  712. memcpy(erst_erange.vaddr, record, record->record_length);
  713. rcd_erange = erst_erange.vaddr;
  714. /* signature for serialization system */
  715. memcpy(&rcd_erange->persistence_information, "ER", 2);
  716. rc = __erst_write_to_storage(0);
  717. raw_spin_unlock_irqrestore(&erst_lock, flags);
  718. return rc;
  719. }
  720. EXPORT_SYMBOL_GPL(erst_write);
  721. static int __erst_read_to_erange(u64 record_id, u64 *offset)
  722. {
  723. int rc;
  724. if (erst_erange.attr & ERST_RANGE_NVRAM)
  725. return __erst_read_to_erange_from_nvram(
  726. record_id, offset);
  727. rc = __erst_read_from_storage(record_id, 0);
  728. if (rc)
  729. return rc;
  730. *offset = 0;
  731. return 0;
  732. }
  733. static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
  734. size_t buflen)
  735. {
  736. int rc;
  737. u64 offset, len = 0;
  738. struct cper_record_header *rcd_tmp;
  739. rc = __erst_read_to_erange(record_id, &offset);
  740. if (rc)
  741. return rc;
  742. rcd_tmp = erst_erange.vaddr + offset;
  743. len = rcd_tmp->record_length;
  744. if (len <= buflen)
  745. memcpy(record, rcd_tmp, len);
  746. return len;
  747. }
  748. /*
  749. * If return value > buflen, the buffer size is not big enough,
  750. * else if return value < 0, something goes wrong,
  751. * else everything is OK, and return value is record length
  752. */
  753. ssize_t erst_read(u64 record_id, struct cper_record_header *record,
  754. size_t buflen)
  755. {
  756. ssize_t len;
  757. unsigned long flags;
  758. if (erst_disable)
  759. return -ENODEV;
  760. raw_spin_lock_irqsave(&erst_lock, flags);
  761. len = __erst_read(record_id, record, buflen);
  762. raw_spin_unlock_irqrestore(&erst_lock, flags);
  763. return len;
  764. }
  765. EXPORT_SYMBOL_GPL(erst_read);
  766. int erst_clear(u64 record_id)
  767. {
  768. int rc, i;
  769. unsigned long flags;
  770. u64 *entries;
  771. if (erst_disable)
  772. return -ENODEV;
  773. rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
  774. if (rc)
  775. return rc;
  776. raw_spin_lock_irqsave(&erst_lock, flags);
  777. if (erst_erange.attr & ERST_RANGE_NVRAM)
  778. rc = __erst_clear_from_nvram(record_id);
  779. else
  780. rc = __erst_clear_from_storage(record_id);
  781. raw_spin_unlock_irqrestore(&erst_lock, flags);
  782. if (rc)
  783. goto out;
  784. entries = erst_record_id_cache.entries;
  785. for (i = 0; i < erst_record_id_cache.len; i++) {
  786. if (entries[i] == record_id)
  787. entries[i] = APEI_ERST_INVALID_RECORD_ID;
  788. }
  789. __erst_record_id_cache_compact();
  790. out:
  791. mutex_unlock(&erst_record_id_cache.lock);
  792. return rc;
  793. }
  794. EXPORT_SYMBOL_GPL(erst_clear);
  795. static int __init setup_erst_disable(char *str)
  796. {
  797. erst_disable = 1;
  798. return 0;
  799. }
  800. __setup("erst_disable", setup_erst_disable);
  801. static int erst_check_table(struct acpi_table_erst *erst_tab)
  802. {
  803. if ((erst_tab->header_length !=
  804. (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
  805. && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
  806. return -EINVAL;
  807. if (erst_tab->header.length < sizeof(struct acpi_table_erst))
  808. return -EINVAL;
  809. if (erst_tab->entries !=
  810. (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
  811. sizeof(struct acpi_erst_entry))
  812. return -EINVAL;
  813. return 0;
  814. }
  815. static int erst_open_pstore(struct pstore_info *psi);
  816. static int erst_close_pstore(struct pstore_info *psi);
  817. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
  818. struct timespec *time, char **buf,
  819. struct pstore_info *psi);
  820. static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
  821. u64 *id, unsigned int part,
  822. size_t size, struct pstore_info *psi);
  823. static int erst_clearer(enum pstore_type_id type, u64 id,
  824. struct pstore_info *psi);
  825. static struct pstore_info erst_info = {
  826. .owner = THIS_MODULE,
  827. .name = "erst",
  828. .open = erst_open_pstore,
  829. .close = erst_close_pstore,
  830. .read = erst_reader,
  831. .write = erst_writer,
  832. .erase = erst_clearer
  833. };
  834. #define CPER_CREATOR_PSTORE \
  835. UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
  836. 0x64, 0x90, 0xb8, 0x9d)
  837. #define CPER_SECTION_TYPE_DMESG \
  838. UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
  839. 0x94, 0x19, 0xeb, 0x12)
  840. #define CPER_SECTION_TYPE_MCE \
  841. UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
  842. 0x04, 0x4a, 0x38, 0xfc)
  843. struct cper_pstore_record {
  844. struct cper_record_header hdr;
  845. struct cper_section_descriptor sec_hdr;
  846. char data[];
  847. } __packed;
  848. static int reader_pos;
  849. static int erst_open_pstore(struct pstore_info *psi)
  850. {
  851. int rc;
  852. if (erst_disable)
  853. return -ENODEV;
  854. rc = erst_get_record_id_begin(&reader_pos);
  855. return rc;
  856. }
  857. static int erst_close_pstore(struct pstore_info *psi)
  858. {
  859. erst_get_record_id_end();
  860. return 0;
  861. }
  862. static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
  863. struct timespec *time, char **buf,
  864. struct pstore_info *psi)
  865. {
  866. int rc;
  867. ssize_t len = 0;
  868. u64 record_id;
  869. struct cper_pstore_record *rcd;
  870. size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
  871. if (erst_disable)
  872. return -ENODEV;
  873. rcd = kmalloc(rcd_len, GFP_KERNEL);
  874. if (!rcd) {
  875. rc = -ENOMEM;
  876. goto out;
  877. }
  878. skip:
  879. rc = erst_get_record_id_next(&reader_pos, &record_id);
  880. if (rc)
  881. goto out;
  882. /* no more record */
  883. if (record_id == APEI_ERST_INVALID_RECORD_ID) {
  884. rc = -EINVAL;
  885. goto out;
  886. }
  887. len = erst_read(record_id, &rcd->hdr, rcd_len);
  888. /* The record may be cleared by others, try read next record */
  889. if (len == -ENOENT)
  890. goto skip;
  891. else if (len < sizeof(*rcd)) {
  892. rc = -EIO;
  893. goto out;
  894. }
  895. if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
  896. goto skip;
  897. *buf = kmalloc(len, GFP_KERNEL);
  898. if (*buf == NULL) {
  899. rc = -ENOMEM;
  900. goto out;
  901. }
  902. memcpy(*buf, rcd->data, len - sizeof(*rcd));
  903. *id = record_id;
  904. if (uuid_le_cmp(rcd->sec_hdr.section_type,
  905. CPER_SECTION_TYPE_DMESG) == 0)
  906. *type = PSTORE_TYPE_DMESG;
  907. else if (uuid_le_cmp(rcd->sec_hdr.section_type,
  908. CPER_SECTION_TYPE_MCE) == 0)
  909. *type = PSTORE_TYPE_MCE;
  910. else
  911. *type = PSTORE_TYPE_UNKNOWN;
  912. if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
  913. time->tv_sec = rcd->hdr.timestamp;
  914. else
  915. time->tv_sec = 0;
  916. time->tv_nsec = 0;
  917. out:
  918. kfree(rcd);
  919. return (rc < 0) ? rc : (len - sizeof(*rcd));
  920. }
  921. static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
  922. u64 *id, unsigned int part,
  923. size_t size, struct pstore_info *psi)
  924. {
  925. struct cper_pstore_record *rcd = (struct cper_pstore_record *)
  926. (erst_info.buf - sizeof(*rcd));
  927. int ret;
  928. memset(rcd, 0, sizeof(*rcd));
  929. memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
  930. rcd->hdr.revision = CPER_RECORD_REV;
  931. rcd->hdr.signature_end = CPER_SIG_END;
  932. rcd->hdr.section_count = 1;
  933. rcd->hdr.error_severity = CPER_SEV_FATAL;
  934. /* timestamp valid. platform_id, partition_id are invalid */
  935. rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
  936. rcd->hdr.timestamp = get_seconds();
  937. rcd->hdr.record_length = sizeof(*rcd) + size;
  938. rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
  939. rcd->hdr.notification_type = CPER_NOTIFY_MCE;
  940. rcd->hdr.record_id = cper_next_record_id();
  941. rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
  942. rcd->sec_hdr.section_offset = sizeof(*rcd);
  943. rcd->sec_hdr.section_length = size;
  944. rcd->sec_hdr.revision = CPER_SEC_REV;
  945. /* fru_id and fru_text is invalid */
  946. rcd->sec_hdr.validation_bits = 0;
  947. rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
  948. switch (type) {
  949. case PSTORE_TYPE_DMESG:
  950. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
  951. break;
  952. case PSTORE_TYPE_MCE:
  953. rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
  954. break;
  955. default:
  956. return -EINVAL;
  957. }
  958. rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
  959. ret = erst_write(&rcd->hdr);
  960. *id = rcd->hdr.record_id;
  961. return ret;
  962. }
  963. static int erst_clearer(enum pstore_type_id type, u64 id,
  964. struct pstore_info *psi)
  965. {
  966. return erst_clear(id);
  967. }
  968. static int __init erst_init(void)
  969. {
  970. int rc = 0;
  971. acpi_status status;
  972. struct apei_exec_context ctx;
  973. struct apei_resources erst_resources;
  974. struct resource *r;
  975. char *buf;
  976. if (acpi_disabled)
  977. goto err;
  978. if (erst_disable) {
  979. pr_info(ERST_PFX
  980. "Error Record Serialization Table (ERST) support is disabled.\n");
  981. goto err;
  982. }
  983. status = acpi_get_table(ACPI_SIG_ERST, 0,
  984. (struct acpi_table_header **)&erst_tab);
  985. if (status == AE_NOT_FOUND)
  986. goto err;
  987. else if (ACPI_FAILURE(status)) {
  988. const char *msg = acpi_format_exception(status);
  989. pr_err(ERST_PFX "Failed to get table, %s\n", msg);
  990. rc = -EINVAL;
  991. goto err;
  992. }
  993. rc = erst_check_table(erst_tab);
  994. if (rc) {
  995. pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
  996. goto err;
  997. }
  998. apei_resources_init(&erst_resources);
  999. erst_exec_ctx_init(&ctx);
  1000. rc = apei_exec_collect_resources(&ctx, &erst_resources);
  1001. if (rc)
  1002. goto err_fini;
  1003. rc = apei_resources_request(&erst_resources, "APEI ERST");
  1004. if (rc)
  1005. goto err_fini;
  1006. rc = apei_exec_pre_map_gars(&ctx);
  1007. if (rc)
  1008. goto err_release;
  1009. rc = erst_get_erange(&erst_erange);
  1010. if (rc) {
  1011. if (rc == -ENODEV)
  1012. pr_info(ERST_PFX
  1013. "The corresponding hardware device or firmware implementation "
  1014. "is not available.\n");
  1015. else
  1016. pr_err(ERST_PFX
  1017. "Failed to get Error Log Address Range.\n");
  1018. goto err_unmap_reg;
  1019. }
  1020. r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
  1021. if (!r) {
  1022. pr_err(ERST_PFX
  1023. "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
  1024. (unsigned long long)erst_erange.base,
  1025. (unsigned long long)erst_erange.base + erst_erange.size);
  1026. rc = -EIO;
  1027. goto err_unmap_reg;
  1028. }
  1029. rc = -ENOMEM;
  1030. erst_erange.vaddr = ioremap_cache(erst_erange.base,
  1031. erst_erange.size);
  1032. if (!erst_erange.vaddr)
  1033. goto err_release_erange;
  1034. buf = kmalloc(erst_erange.size, GFP_KERNEL);
  1035. spin_lock_init(&erst_info.buf_lock);
  1036. if (buf) {
  1037. erst_info.buf = buf + sizeof(struct cper_pstore_record);
  1038. erst_info.bufsize = erst_erange.size -
  1039. sizeof(struct cper_pstore_record);
  1040. if (pstore_register(&erst_info)) {
  1041. pr_info(ERST_PFX "Could not register with persistent store\n");
  1042. kfree(buf);
  1043. }
  1044. }
  1045. pr_info(ERST_PFX
  1046. "Error Record Serialization Table (ERST) support is initialized.\n");
  1047. return 0;
  1048. err_release_erange:
  1049. release_mem_region(erst_erange.base, erst_erange.size);
  1050. err_unmap_reg:
  1051. apei_exec_post_unmap_gars(&ctx);
  1052. err_release:
  1053. apei_resources_release(&erst_resources);
  1054. err_fini:
  1055. apei_resources_fini(&erst_resources);
  1056. err:
  1057. erst_disable = 1;
  1058. return rc;
  1059. }
  1060. device_initcall(erst_init);