erst.c 29 KB

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