scanlog.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/ppc64/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 <asm/uaccess.h>
  29. #include <asm/rtas.h>
  30. #include <asm/prom.h>
  31. #define MODULE_VERS "1.0"
  32. #define MODULE_NAME "scanlog"
  33. /* Status returns from ibm,scan-log-dump */
  34. #define SCANLOG_COMPLETE 0
  35. #define SCANLOG_HWERROR -1
  36. #define SCANLOG_CONTINUE 1
  37. #define DEBUG(A...) do { if (scanlog_debug) printk(KERN_ERR "scanlog: " A); } while (0)
  38. static int scanlog_debug;
  39. static unsigned int ibm_scan_log_dump; /* RTAS token */
  40. static struct proc_dir_entry *proc_ppc64_scan_log_dump; /* The proc file */
  41. static ssize_t scanlog_read(struct file *file, char __user *buf,
  42. size_t count, loff_t *ppos)
  43. {
  44. struct inode * inode = file->f_dentry->d_inode;
  45. struct proc_dir_entry *dp;
  46. unsigned int *data;
  47. int status;
  48. unsigned long len, off;
  49. unsigned int wait_time;
  50. dp = PDE(inode);
  51. data = (unsigned int *)dp->data;
  52. if (!data) {
  53. printk(KERN_ERR "scanlog: read failed no data\n");
  54. return -EIO;
  55. }
  56. if (count > RTAS_DATA_BUF_SIZE)
  57. count = RTAS_DATA_BUF_SIZE;
  58. if (count < 1024) {
  59. /* This is the min supported by this RTAS call. Rather
  60. * than do all the buffering we insist the user code handle
  61. * larger reads. As long as cp works... :)
  62. */
  63. printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
  64. return -EINVAL;
  65. }
  66. if (!access_ok(VERIFY_WRITE, buf, count))
  67. return -EFAULT;
  68. for (;;) {
  69. wait_time = 500; /* default wait if no data */
  70. spin_lock(&rtas_data_buf_lock);
  71. memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
  72. status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
  73. (u32) __pa(rtas_data_buf), (u32) count);
  74. memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  75. spin_unlock(&rtas_data_buf_lock);
  76. DEBUG("status=%d, data[0]=%x, data[1]=%x, data[2]=%x\n",
  77. status, data[0], data[1], data[2]);
  78. switch (status) {
  79. case SCANLOG_COMPLETE:
  80. DEBUG("hit eof\n");
  81. return 0;
  82. case SCANLOG_HWERROR:
  83. DEBUG("hardware error reading scan log data\n");
  84. return -EIO;
  85. case SCANLOG_CONTINUE:
  86. /* We may or may not have data yet */
  87. len = data[1];
  88. off = data[2];
  89. if (len > 0) {
  90. if (copy_to_user(buf, ((char *)data)+off, len))
  91. return -EFAULT;
  92. return len;
  93. }
  94. /* Break to sleep default time */
  95. break;
  96. default:
  97. if (status > 9900 && status <= 9905) {
  98. wait_time = rtas_extended_busy_delay_time(status);
  99. } else {
  100. printk(KERN_ERR "scanlog: unknown error from rtas: %d\n", status);
  101. return -EIO;
  102. }
  103. }
  104. /* Apparently no data yet. Wait and try again. */
  105. msleep_interruptible(wait_time);
  106. }
  107. /*NOTREACHED*/
  108. }
  109. static ssize_t scanlog_write(struct file * file, const char __user * buf,
  110. size_t count, loff_t *ppos)
  111. {
  112. char stkbuf[20];
  113. int status;
  114. if (count > 19) count = 19;
  115. if (copy_from_user (stkbuf, buf, count)) {
  116. return -EFAULT;
  117. }
  118. stkbuf[count] = 0;
  119. if (buf) {
  120. if (strncmp(stkbuf, "reset", 5) == 0) {
  121. DEBUG("reset scanlog\n");
  122. status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
  123. DEBUG("rtas returns %d\n", status);
  124. } else if (strncmp(stkbuf, "debugon", 7) == 0) {
  125. printk(KERN_ERR "scanlog: debug on\n");
  126. scanlog_debug = 1;
  127. } else if (strncmp(stkbuf, "debugoff", 8) == 0) {
  128. printk(KERN_ERR "scanlog: debug off\n");
  129. scanlog_debug = 0;
  130. }
  131. }
  132. return count;
  133. }
  134. static int scanlog_open(struct inode * inode, struct file * file)
  135. {
  136. struct proc_dir_entry *dp = PDE(inode);
  137. unsigned int *data = (unsigned int *)dp->data;
  138. if (!data) {
  139. printk(KERN_ERR "scanlog: open failed no data\n");
  140. return -EIO;
  141. }
  142. if (data[0] != 0) {
  143. /* This imperfect test stops a second copy of the
  144. * data (or a reset while data is being copied)
  145. */
  146. return -EBUSY;
  147. }
  148. data[0] = 0; /* re-init so we restart the scan */
  149. return 0;
  150. }
  151. static int scanlog_release(struct inode * inode, struct file * file)
  152. {
  153. struct proc_dir_entry *dp = PDE(inode);
  154. unsigned int *data = (unsigned int *)dp->data;
  155. if (!data) {
  156. printk(KERN_ERR "scanlog: release failed no data\n");
  157. return -EIO;
  158. }
  159. data[0] = 0;
  160. return 0;
  161. }
  162. struct file_operations scanlog_fops = {
  163. .owner = THIS_MODULE,
  164. .read = scanlog_read,
  165. .write = scanlog_write,
  166. .open = scanlog_open,
  167. .release = scanlog_release,
  168. };
  169. static int __init scanlog_init(void)
  170. {
  171. struct proc_dir_entry *ent;
  172. ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
  173. if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE) {
  174. printk(KERN_ERR "scan-log-dump not implemented on this system\n");
  175. return -EIO;
  176. }
  177. ent = create_proc_entry("ppc64/rtas/scan-log-dump", S_IRUSR, NULL);
  178. if (ent) {
  179. ent->proc_fops = &scanlog_fops;
  180. /* Ideally we could allocate a buffer < 4G */
  181. ent->data = kmalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  182. if (!ent->data) {
  183. printk(KERN_ERR "Failed to allocate a buffer\n");
  184. remove_proc_entry("scan-log-dump", ent->parent);
  185. return -ENOMEM;
  186. }
  187. ((unsigned int *)ent->data)[0] = 0;
  188. } else {
  189. printk(KERN_ERR "Failed to create ppc64/scan-log-dump proc entry\n");
  190. return -EIO;
  191. }
  192. proc_ppc64_scan_log_dump = ent;
  193. return 0;
  194. }
  195. static void __exit scanlog_cleanup(void)
  196. {
  197. if (proc_ppc64_scan_log_dump) {
  198. kfree(proc_ppc64_scan_log_dump->data);
  199. remove_proc_entry("scan-log-dump", proc_ppc64_scan_log_dump->parent);
  200. }
  201. }
  202. module_init(scanlog_init);
  203. module_exit(scanlog_cleanup);
  204. MODULE_LICENSE("GPL");