scanlog.c 6.5 KB

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