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