scanlog.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * scan-log-data driver for PPC64 Todd Inglett <tinglett@vnet.ibm.com>
  10. *
  11. * When ppc64 hardware fails the service processor dumps internal state
  12. * of the system. After a reboot the operating system can access a dump
  13. * of this data using this driver. A dump exists if the device-tree
  14. * /chosen/ibm,scan-log-data property exists.
  15. *
  16. * This driver exports /proc/powerpc/scan-log-dump which can be read.
  17. * The driver supports only sequential reads.
  18. *
  19. * The driver looks at a write to the driver for the single word "reset".
  20. * If given, the driver will reset the scanlog so the platform can free it.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/rtas.h>
  31. #include <asm/prom.h>
  32. #define MODULE_VERS "1.0"
  33. #define MODULE_NAME "scanlog"
  34. /* Status returns from ibm,scan-log-dump */
  35. #define SCANLOG_COMPLETE 0
  36. #define SCANLOG_HWERROR -1
  37. #define SCANLOG_CONTINUE 1
  38. static unsigned int ibm_scan_log_dump; /* RTAS token */
  39. static unsigned int *scanlog_buffer; /* The data buffer */
  40. static ssize_t scanlog_read(struct file *file, char __user *buf,
  41. size_t count, loff_t *ppos)
  42. {
  43. unsigned int *data = scanlog_buffer;
  44. int status;
  45. unsigned long len, off;
  46. unsigned int wait_time;
  47. if (count > RTAS_DATA_BUF_SIZE)
  48. count = RTAS_DATA_BUF_SIZE;
  49. if (count < 1024) {
  50. /* This is the min supported by this RTAS call. Rather
  51. * than do all the buffering we insist the user code handle
  52. * larger reads. As long as cp works... :)
  53. */
  54. printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
  55. return -EINVAL;
  56. }
  57. if (!access_ok(VERIFY_WRITE, buf, count))
  58. return -EFAULT;
  59. for (;;) {
  60. wait_time = 500; /* default wait if no data */
  61. spin_lock(&rtas_data_buf_lock);
  62. memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
  63. status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
  64. (u32) __pa(rtas_data_buf), (u32) count);
  65. memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  66. spin_unlock(&rtas_data_buf_lock);
  67. pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \
  68. "data[2]=%x\n", status, data[0], data[1], data[2]);
  69. switch (status) {
  70. case SCANLOG_COMPLETE:
  71. pr_debug("scanlog: hit eof\n");
  72. return 0;
  73. case SCANLOG_HWERROR:
  74. pr_debug("scanlog: hardware error reading data\n");
  75. return -EIO;
  76. case SCANLOG_CONTINUE:
  77. /* We may or may not have data yet */
  78. len = data[1];
  79. off = data[2];
  80. if (len > 0) {
  81. if (copy_to_user(buf, ((char *)data)+off, len))
  82. return -EFAULT;
  83. return len;
  84. }
  85. /* Break to sleep default time */
  86. break;
  87. default:
  88. /* Assume extended busy */
  89. wait_time = rtas_busy_delay_time(status);
  90. if (!wait_time) {
  91. printk(KERN_ERR "scanlog: unknown error " \
  92. "from rtas: %d\n", status);
  93. return -EIO;
  94. }
  95. }
  96. /* Apparently no data yet. Wait and try again. */
  97. msleep_interruptible(wait_time);
  98. }
  99. /*NOTREACHED*/
  100. }
  101. static ssize_t scanlog_write(struct file * file, const char __user * buf,
  102. size_t count, loff_t *ppos)
  103. {
  104. char stkbuf[20];
  105. int status;
  106. if (count > 19) count = 19;
  107. if (copy_from_user (stkbuf, buf, count)) {
  108. return -EFAULT;
  109. }
  110. stkbuf[count] = 0;
  111. if (buf) {
  112. if (strncmp(stkbuf, "reset", 5) == 0) {
  113. pr_debug("scanlog: reset scanlog\n");
  114. status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
  115. pr_debug("scanlog: rtas returns %d\n", status);
  116. }
  117. }
  118. return count;
  119. }
  120. static int scanlog_open(struct inode * inode, struct file * file)
  121. {
  122. unsigned int *data = scanlog_buffer;
  123. if (data[0] != 0) {
  124. /* This imperfect test stops a second copy of the
  125. * data (or a reset while data is being copied)
  126. */
  127. return -EBUSY;
  128. }
  129. data[0] = 0; /* re-init so we restart the scan */
  130. return 0;
  131. }
  132. static int scanlog_release(struct inode * inode, struct file * file)
  133. {
  134. unsigned int *data = scanlog_buffer;
  135. data[0] = 0;
  136. return 0;
  137. }
  138. static const struct file_operations scanlog_fops = {
  139. .owner = THIS_MODULE,
  140. .read = scanlog_read,
  141. .write = scanlog_write,
  142. .open = scanlog_open,
  143. .release = scanlog_release,
  144. .llseek = noop_llseek,
  145. };
  146. static int __init scanlog_init(void)
  147. {
  148. struct proc_dir_entry *ent;
  149. int err = -ENOMEM;
  150. ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
  151. if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE)
  152. return -ENODEV;
  153. /* Ideally we could allocate a buffer < 4G */
  154. scanlog_buffer = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  155. if (!scanlog_buffer)
  156. goto err;
  157. ent = proc_create("powerpc/rtas/scan-log-dump", S_IRUSR, NULL,
  158. &scanlog_fops);
  159. if (!ent)
  160. goto err;
  161. return 0;
  162. err:
  163. kfree(scanlog_buffer);
  164. return err;
  165. }
  166. static void __exit scanlog_cleanup(void)
  167. {
  168. remove_proc_entry("powerpc/rtas/scan-log-dump", NULL);
  169. kfree(scanlog_buffer);
  170. }
  171. module_init(scanlog_init);
  172. module_exit(scanlog_cleanup);
  173. MODULE_LICENSE("GPL");