scanlog.c 5.6 KB

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