rtasd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001 Anton Blanchard <[email protected]>, IBM
  4. *
  5. * Communication to userspace based on kernel/printk.c
  6. */
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/poll.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/cpu.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/slab.h>
  19. #include <linux/topology.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/io.h>
  22. #include <asm/rtas.h>
  23. #include <asm/nvram.h>
  24. #include <linux/atomic.h>
  25. #include <asm/machdep.h>
  26. #include <asm/topology.h>
  27. static DEFINE_SPINLOCK(rtasd_log_lock);
  28. static DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
  29. static char *rtas_log_buf;
  30. static unsigned long rtas_log_start;
  31. static unsigned long rtas_log_size;
  32. static int surveillance_timeout = -1;
  33. static unsigned int rtas_error_log_max;
  34. static unsigned int rtas_error_log_buffer_max;
  35. /* RTAS service tokens */
  36. static unsigned int event_scan;
  37. static unsigned int rtas_event_scan_rate;
  38. static bool full_rtas_msgs;
  39. /* Stop logging to nvram after first fatal error */
  40. static int logging_enabled; /* Until we initialize everything,
  41. * make sure we don't try logging
  42. * anything */
  43. static int error_log_cnt;
  44. /*
  45. * Since we use 32 bit RTAS, the physical address of this must be below
  46. * 4G or else bad things happen. Allocate this in the kernel data and
  47. * make it big enough.
  48. */
  49. static unsigned char logdata[RTAS_ERROR_LOG_MAX];
  50. static char *rtas_type[] = {
  51. "Unknown", "Retry", "TCE Error", "Internal Device Failure",
  52. "Timeout", "Data Parity", "Address Parity", "Cache Parity",
  53. "Address Invalid", "ECC Uncorrected", "ECC Corrupted",
  54. };
  55. static char *rtas_event_type(int type)
  56. {
  57. if ((type > 0) && (type < 11))
  58. return rtas_type[type];
  59. switch (type) {
  60. case RTAS_TYPE_EPOW:
  61. return "EPOW";
  62. case RTAS_TYPE_PLATFORM:
  63. return "Platform Error";
  64. case RTAS_TYPE_IO:
  65. return "I/O Event";
  66. case RTAS_TYPE_INFO:
  67. return "Platform Information Event";
  68. case RTAS_TYPE_DEALLOC:
  69. return "Resource Deallocation Event";
  70. case RTAS_TYPE_DUMP:
  71. return "Dump Notification Event";
  72. case RTAS_TYPE_PRRN:
  73. return "Platform Resource Reassignment Event";
  74. case RTAS_TYPE_HOTPLUG:
  75. return "Hotplug Event";
  76. }
  77. return rtas_type[0];
  78. }
  79. /* To see this info, grep RTAS /var/log/messages and each entry
  80. * will be collected together with obvious begin/end.
  81. * There will be a unique identifier on the begin and end lines.
  82. * This will persist across reboots.
  83. *
  84. * format of error logs returned from RTAS:
  85. * bytes (size) : contents
  86. * --------------------------------------------------------
  87. * 0-7 (8) : rtas_error_log
  88. * 8-47 (40) : extended info
  89. * 48-51 (4) : vendor id
  90. * 52-1023 (vendor specific) : location code and debug data
  91. */
  92. static void printk_log_rtas(char *buf, int len)
  93. {
  94. int i,j,n = 0;
  95. int perline = 16;
  96. char buffer[64];
  97. char * str = "RTAS event";
  98. if (full_rtas_msgs) {
  99. printk(RTAS_DEBUG "%d -------- %s begin --------\n",
  100. error_log_cnt, str);
  101. /*
  102. * Print perline bytes on each line, each line will start
  103. * with RTAS and a changing number, so syslogd will
  104. * print lines that are otherwise the same. Separate every
  105. * 4 bytes with a space.
  106. */
  107. for (i = 0; i < len; i++) {
  108. j = i % perline;
  109. if (j == 0) {
  110. memset(buffer, 0, sizeof(buffer));
  111. n = sprintf(buffer, "RTAS %d:", i/perline);
  112. }
  113. if ((i % 4) == 0)
  114. n += sprintf(buffer+n, " ");
  115. n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
  116. if (j == (perline-1))
  117. printk(KERN_DEBUG "%s\n", buffer);
  118. }
  119. if ((i % perline) != 0)
  120. printk(KERN_DEBUG "%s\n", buffer);
  121. printk(RTAS_DEBUG "%d -------- %s end ----------\n",
  122. error_log_cnt, str);
  123. } else {
  124. struct rtas_error_log *errlog = (struct rtas_error_log *)buf;
  125. printk(RTAS_DEBUG "event: %d, Type: %s (%d), Severity: %d\n",
  126. error_log_cnt,
  127. rtas_event_type(rtas_error_type(errlog)),
  128. rtas_error_type(errlog),
  129. rtas_error_severity(errlog));
  130. }
  131. }
  132. static int log_rtas_len(char * buf)
  133. {
  134. int len;
  135. struct rtas_error_log *err;
  136. uint32_t extended_log_length;
  137. /* rtas fixed header */
  138. len = 8;
  139. err = (struct rtas_error_log *)buf;
  140. extended_log_length = rtas_error_extended_log_length(err);
  141. if (rtas_error_extended(err) && extended_log_length) {
  142. /* extended header */
  143. len += extended_log_length;
  144. }
  145. if (rtas_error_log_max == 0)
  146. rtas_error_log_max = rtas_get_error_log_max();
  147. if (len > rtas_error_log_max)
  148. len = rtas_error_log_max;
  149. return len;
  150. }
  151. /*
  152. * First write to nvram, if fatal error, that is the only
  153. * place we log the info. The error will be picked up
  154. * on the next reboot by rtasd. If not fatal, run the
  155. * method for the type of error. Currently, only RTAS
  156. * errors have methods implemented, but in the future
  157. * there might be a need to store data in nvram before a
  158. * call to panic().
  159. *
  160. * XXX We write to nvram periodically, to indicate error has
  161. * been written and sync'd, but there is a possibility
  162. * that if we don't shutdown correctly, a duplicate error
  163. * record will be created on next reboot.
  164. */
  165. void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
  166. {
  167. unsigned long offset;
  168. unsigned long s;
  169. int len = 0;
  170. pr_debug("rtasd: logging event\n");
  171. if (buf == NULL)
  172. return;
  173. spin_lock_irqsave(&rtasd_log_lock, s);
  174. /* get length and increase count */
  175. switch (err_type & ERR_TYPE_MASK) {
  176. case ERR_TYPE_RTAS_LOG:
  177. len = log_rtas_len(buf);
  178. if (!(err_type & ERR_FLAG_BOOT))
  179. error_log_cnt++;
  180. break;
  181. case ERR_TYPE_KERNEL_PANIC:
  182. default:
  183. WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
  184. spin_unlock_irqrestore(&rtasd_log_lock, s);
  185. return;
  186. }
  187. #ifdef CONFIG_PPC64
  188. /* Write error to NVRAM */
  189. if (logging_enabled && !(err_type & ERR_FLAG_BOOT))
  190. nvram_write_error_log(buf, len, err_type, error_log_cnt);
  191. #endif /* CONFIG_PPC64 */
  192. /*
  193. * rtas errors can occur during boot, and we do want to capture
  194. * those somewhere, even if nvram isn't ready (why not?), and even
  195. * if rtasd isn't ready. Put them into the boot log, at least.
  196. */
  197. if ((err_type & ERR_TYPE_MASK) == ERR_TYPE_RTAS_LOG)
  198. printk_log_rtas(buf, len);
  199. /* Check to see if we need to or have stopped logging */
  200. if (fatal || !logging_enabled) {
  201. logging_enabled = 0;
  202. WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
  203. spin_unlock_irqrestore(&rtasd_log_lock, s);
  204. return;
  205. }
  206. /* call type specific method for error */
  207. switch (err_type & ERR_TYPE_MASK) {
  208. case ERR_TYPE_RTAS_LOG:
  209. offset = rtas_error_log_buffer_max *
  210. ((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
  211. /* First copy over sequence number */
  212. memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));
  213. /* Second copy over error log data */
  214. offset += sizeof(int);
  215. memcpy(&rtas_log_buf[offset], buf, len);
  216. if (rtas_log_size < LOG_NUMBER)
  217. rtas_log_size += 1;
  218. else
  219. rtas_log_start += 1;
  220. WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
  221. spin_unlock_irqrestore(&rtasd_log_lock, s);
  222. wake_up_interruptible(&rtas_log_wait);
  223. break;
  224. case ERR_TYPE_KERNEL_PANIC:
  225. default:
  226. WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
  227. spin_unlock_irqrestore(&rtasd_log_lock, s);
  228. return;
  229. }
  230. }
  231. static void handle_rtas_event(const struct rtas_error_log *log)
  232. {
  233. if (!machine_is(pseries))
  234. return;
  235. if (rtas_error_type(log) == RTAS_TYPE_PRRN)
  236. pr_info_ratelimited("Platform resource reassignment ignored.\n");
  237. }
  238. static int rtas_log_open(struct inode * inode, struct file * file)
  239. {
  240. return 0;
  241. }
  242. static int rtas_log_release(struct inode * inode, struct file * file)
  243. {
  244. return 0;
  245. }
  246. /* This will check if all events are logged, if they are then, we
  247. * know that we can safely clear the events in NVRAM.
  248. * Next we'll sit and wait for something else to log.
  249. */
  250. static ssize_t rtas_log_read(struct file * file, char __user * buf,
  251. size_t count, loff_t *ppos)
  252. {
  253. int error;
  254. char *tmp;
  255. unsigned long s;
  256. unsigned long offset;
  257. if (!buf || count < rtas_error_log_buffer_max)
  258. return -EINVAL;
  259. count = rtas_error_log_buffer_max;
  260. if (!access_ok(buf, count))
  261. return -EFAULT;
  262. tmp = kmalloc(count, GFP_KERNEL);
  263. if (!tmp)
  264. return -ENOMEM;
  265. spin_lock_irqsave(&rtasd_log_lock, s);
  266. /* if it's 0, then we know we got the last one (the one in NVRAM) */
  267. while (rtas_log_size == 0) {
  268. if (file->f_flags & O_NONBLOCK) {
  269. spin_unlock_irqrestore(&rtasd_log_lock, s);
  270. error = -EAGAIN;
  271. goto out;
  272. }
  273. if (!logging_enabled) {
  274. spin_unlock_irqrestore(&rtasd_log_lock, s);
  275. error = -ENODATA;
  276. goto out;
  277. }
  278. #ifdef CONFIG_PPC64
  279. nvram_clear_error_log();
  280. #endif /* CONFIG_PPC64 */
  281. spin_unlock_irqrestore(&rtasd_log_lock, s);
  282. error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
  283. if (error)
  284. goto out;
  285. spin_lock_irqsave(&rtasd_log_lock, s);
  286. }
  287. offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
  288. memcpy(tmp, &rtas_log_buf[offset], count);
  289. rtas_log_start += 1;
  290. rtas_log_size -= 1;
  291. spin_unlock_irqrestore(&rtasd_log_lock, s);
  292. error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
  293. out:
  294. kfree(tmp);
  295. return error;
  296. }
  297. static __poll_t rtas_log_poll(struct file *file, poll_table * wait)
  298. {
  299. poll_wait(file, &rtas_log_wait, wait);
  300. if (rtas_log_size)
  301. return EPOLLIN | EPOLLRDNORM;
  302. return 0;
  303. }
  304. static const struct proc_ops rtas_log_proc_ops = {
  305. .proc_read = rtas_log_read,
  306. .proc_poll = rtas_log_poll,
  307. .proc_open = rtas_log_open,
  308. .proc_release = rtas_log_release,
  309. .proc_lseek = noop_llseek,
  310. };
  311. static int enable_surveillance(int timeout)
  312. {
  313. int error;
  314. error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
  315. if (error == 0)
  316. return 0;
  317. if (error == -EINVAL) {
  318. printk(KERN_DEBUG "rtasd: surveillance not supported\n");
  319. return 0;
  320. }
  321. printk(KERN_ERR "rtasd: could not update surveillance\n");
  322. return -1;
  323. }
  324. static void do_event_scan(void)
  325. {
  326. int error;
  327. do {
  328. memset(logdata, 0, rtas_error_log_max);
  329. error = rtas_call(event_scan, 4, 1, NULL,
  330. RTAS_EVENT_SCAN_ALL_EVENTS, 0,
  331. __pa(logdata), rtas_error_log_max);
  332. if (error == -1) {
  333. printk(KERN_ERR "event-scan failed\n");
  334. break;
  335. }
  336. if (error == 0) {
  337. if (rtas_error_type((struct rtas_error_log *)logdata) !=
  338. RTAS_TYPE_PRRN)
  339. pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG,
  340. 0);
  341. handle_rtas_event((struct rtas_error_log *)logdata);
  342. }
  343. } while(error == 0);
  344. }
  345. static void rtas_event_scan(struct work_struct *w);
  346. static DECLARE_DELAYED_WORK(event_scan_work, rtas_event_scan);
  347. /*
  348. * Delay should be at least one second since some machines have problems if
  349. * we call event-scan too quickly.
  350. */
  351. static unsigned long event_scan_delay = 1*HZ;
  352. static int first_pass = 1;
  353. static void rtas_event_scan(struct work_struct *w)
  354. {
  355. unsigned int cpu;
  356. do_event_scan();
  357. cpus_read_lock();
  358. /* raw_ OK because just using CPU as starting point. */
  359. cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
  360. if (cpu >= nr_cpu_ids) {
  361. cpu = cpumask_first(cpu_online_mask);
  362. if (first_pass) {
  363. first_pass = 0;
  364. event_scan_delay = 30*HZ/rtas_event_scan_rate;
  365. if (surveillance_timeout != -1) {
  366. pr_debug("rtasd: enabling surveillance\n");
  367. enable_surveillance(surveillance_timeout);
  368. pr_debug("rtasd: surveillance enabled\n");
  369. }
  370. }
  371. }
  372. schedule_delayed_work_on(cpu, &event_scan_work,
  373. __round_jiffies_relative(event_scan_delay, cpu));
  374. cpus_read_unlock();
  375. }
  376. #ifdef CONFIG_PPC64
  377. static void __init retrieve_nvram_error_log(void)
  378. {
  379. unsigned int err_type ;
  380. int rc ;
  381. /* See if we have any error stored in NVRAM */
  382. memset(logdata, 0, rtas_error_log_max);
  383. rc = nvram_read_error_log(logdata, rtas_error_log_max,
  384. &err_type, &error_log_cnt);
  385. /* We can use rtas_log_buf now */
  386. logging_enabled = 1;
  387. if (!rc) {
  388. if (err_type != ERR_FLAG_ALREADY_LOGGED) {
  389. pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
  390. }
  391. }
  392. }
  393. #else /* CONFIG_PPC64 */
  394. static void __init retrieve_nvram_error_log(void)
  395. {
  396. }
  397. #endif /* CONFIG_PPC64 */
  398. static void __init start_event_scan(void)
  399. {
  400. printk(KERN_DEBUG "RTAS daemon started\n");
  401. pr_debug("rtasd: will sleep for %d milliseconds\n",
  402. (30000 / rtas_event_scan_rate));
  403. /* Retrieve errors from nvram if any */
  404. retrieve_nvram_error_log();
  405. schedule_delayed_work_on(cpumask_first(cpu_online_mask),
  406. &event_scan_work, event_scan_delay);
  407. }
  408. /* Cancel the rtas event scan work */
  409. void rtas_cancel_event_scan(void)
  410. {
  411. cancel_delayed_work_sync(&event_scan_work);
  412. }
  413. EXPORT_SYMBOL_GPL(rtas_cancel_event_scan);
  414. static int __init rtas_event_scan_init(void)
  415. {
  416. if (!machine_is(pseries) && !machine_is(chrp))
  417. return 0;
  418. /* No RTAS */
  419. event_scan = rtas_token("event-scan");
  420. if (event_scan == RTAS_UNKNOWN_SERVICE) {
  421. printk(KERN_INFO "rtasd: No event-scan on system\n");
  422. return -ENODEV;
  423. }
  424. rtas_event_scan_rate = rtas_token("rtas-event-scan-rate");
  425. if (rtas_event_scan_rate == RTAS_UNKNOWN_SERVICE) {
  426. printk(KERN_ERR "rtasd: no rtas-event-scan-rate on system\n");
  427. return -ENODEV;
  428. }
  429. if (!rtas_event_scan_rate) {
  430. /* Broken firmware: take a rate of zero to mean don't scan */
  431. printk(KERN_DEBUG "rtasd: scan rate is 0, not scanning\n");
  432. return 0;
  433. }
  434. /* Make room for the sequence number */
  435. rtas_error_log_max = rtas_get_error_log_max();
  436. rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
  437. rtas_log_buf = vmalloc(array_size(LOG_NUMBER,
  438. rtas_error_log_buffer_max));
  439. if (!rtas_log_buf) {
  440. printk(KERN_ERR "rtasd: no memory\n");
  441. return -ENOMEM;
  442. }
  443. start_event_scan();
  444. return 0;
  445. }
  446. arch_initcall(rtas_event_scan_init);
  447. static int __init rtas_init(void)
  448. {
  449. struct proc_dir_entry *entry;
  450. if (!machine_is(pseries) && !machine_is(chrp))
  451. return 0;
  452. if (!rtas_log_buf)
  453. return -ENODEV;
  454. entry = proc_create("powerpc/rtas/error_log", 0400, NULL,
  455. &rtas_log_proc_ops);
  456. if (!entry)
  457. printk(KERN_ERR "Failed to create error_log proc entry\n");
  458. return 0;
  459. }
  460. __initcall(rtas_init);
  461. static int __init surveillance_setup(char *str)
  462. {
  463. int i;
  464. /* We only do surveillance on pseries */
  465. if (!machine_is(pseries))
  466. return 0;
  467. if (get_option(&str,&i)) {
  468. if (i >= 0 && i <= 255)
  469. surveillance_timeout = i;
  470. }
  471. return 1;
  472. }
  473. __setup("surveillance=", surveillance_setup);
  474. static int __init rtasmsgs_setup(char *str)
  475. {
  476. return (kstrtobool(str, &full_rtas_msgs) == 0);
  477. }
  478. __setup("rtasmsgs=", rtasmsgs_setup);