Merge branch 'connlimit' of git://dev.medozas.de/linux
[pandora-kernel.git] / arch / powerpc / platforms / pseries / nvram.c
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  * /dev/nvram driver for PPC64
10  *
11  * This perhaps should live in drivers/char
12  */
13
14
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <asm/uaccess.h>
20 #include <asm/nvram.h>
21 #include <asm/rtas.h>
22 #include <asm/prom.h>
23 #include <asm/machdep.h>
24
25 /* Max bytes to read/write in one go */
26 #define NVRW_CNT 0x20
27
28 static unsigned int nvram_size;
29 static int nvram_fetch, nvram_store;
30 static char nvram_buf[NVRW_CNT];        /* assume this is in the first 4GB */
31 static DEFINE_SPINLOCK(nvram_lock);
32
33 static long nvram_error_log_index = -1;
34 static long nvram_error_log_size = 0;
35
36 struct err_log_info {
37         int error_type;
38         unsigned int seq_num;
39 };
40 #define NVRAM_MAX_REQ           2079
41 #define NVRAM_MIN_REQ           1055
42
43 #define NVRAM_LOG_PART_NAME     "ibm,rtas-log"
44
45 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
46 {
47         unsigned int i;
48         unsigned long len;
49         int done;
50         unsigned long flags;
51         char *p = buf;
52
53
54         if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
55                 return -ENODEV;
56
57         if (*index >= nvram_size)
58                 return 0;
59
60         i = *index;
61         if (i + count > nvram_size)
62                 count = nvram_size - i;
63
64         spin_lock_irqsave(&nvram_lock, flags);
65
66         for (; count != 0; count -= len) {
67                 len = count;
68                 if (len > NVRW_CNT)
69                         len = NVRW_CNT;
70                 
71                 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
72                                len) != 0) || len != done) {
73                         spin_unlock_irqrestore(&nvram_lock, flags);
74                         return -EIO;
75                 }
76                 
77                 memcpy(p, nvram_buf, len);
78
79                 p += len;
80                 i += len;
81         }
82
83         spin_unlock_irqrestore(&nvram_lock, flags);
84         
85         *index = i;
86         return p - buf;
87 }
88
89 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
90 {
91         unsigned int i;
92         unsigned long len;
93         int done;
94         unsigned long flags;
95         const char *p = buf;
96
97         if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
98                 return -ENODEV;
99
100         if (*index >= nvram_size)
101                 return 0;
102
103         i = *index;
104         if (i + count > nvram_size)
105                 count = nvram_size - i;
106
107         spin_lock_irqsave(&nvram_lock, flags);
108
109         for (; count != 0; count -= len) {
110                 len = count;
111                 if (len > NVRW_CNT)
112                         len = NVRW_CNT;
113
114                 memcpy(nvram_buf, p, len);
115
116                 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
117                                len) != 0) || len != done) {
118                         spin_unlock_irqrestore(&nvram_lock, flags);
119                         return -EIO;
120                 }
121                 
122                 p += len;
123                 i += len;
124         }
125         spin_unlock_irqrestore(&nvram_lock, flags);
126         
127         *index = i;
128         return p - buf;
129 }
130
131 static ssize_t pSeries_nvram_get_size(void)
132 {
133         return nvram_size ? nvram_size : -ENODEV;
134 }
135
136
137 /* nvram_write_error_log
138  *
139  * We need to buffer the error logs into nvram to ensure that we have
140  * the failure information to decode.  If we have a severe error there
141  * is no way to guarantee that the OS or the machine is in a state to
142  * get back to user land and write the error to disk.  For example if
143  * the SCSI device driver causes a Machine Check by writing to a bad
144  * IO address, there is no way of guaranteeing that the device driver
145  * is in any state that is would also be able to write the error data
146  * captured to disk, thus we buffer it in NVRAM for analysis on the
147  * next boot.
148  *
149  * In NVRAM the partition containing the error log buffer will looks like:
150  * Header (in bytes):
151  * +-----------+----------+--------+------------+------------------+
152  * | signature | checksum | length | name       | data             |
153  * |0          |1         |2      3|4         15|16        length-1|
154  * +-----------+----------+--------+------------+------------------+
155  *
156  * The 'data' section would look like (in bytes):
157  * +--------------+------------+-----------------------------------+
158  * | event_logged | sequence # | error log                         |
159  * |0            3|4          7|8            nvram_error_log_size-1|
160  * +--------------+------------+-----------------------------------+
161  *
162  * event_logged: 0 if event has not been logged to syslog, 1 if it has
163  * sequence #: The unique sequence # for each event. (until it wraps)
164  * error log: The error log from event_scan
165  */
166 int nvram_write_error_log(char * buff, int length,
167                           unsigned int err_type, unsigned int error_log_cnt)
168 {
169         int rc;
170         loff_t tmp_index;
171         struct err_log_info info;
172         
173         if (nvram_error_log_index == -1) {
174                 return -ESPIPE;
175         }
176
177         if (length > nvram_error_log_size) {
178                 length = nvram_error_log_size;
179         }
180
181         info.error_type = err_type;
182         info.seq_num = error_log_cnt;
183
184         tmp_index = nvram_error_log_index;
185
186         rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
187         if (rc <= 0) {
188                 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
189                 return rc;
190         }
191
192         rc = ppc_md.nvram_write(buff, length, &tmp_index);
193         if (rc <= 0) {
194                 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
195                 return rc;
196         }
197         
198         return 0;
199 }
200
201 /* nvram_read_error_log
202  *
203  * Reads nvram for error log for at most 'length'
204  */
205 int nvram_read_error_log(char * buff, int length,
206                          unsigned int * err_type, unsigned int * error_log_cnt)
207 {
208         int rc;
209         loff_t tmp_index;
210         struct err_log_info info;
211         
212         if (nvram_error_log_index == -1)
213                 return -1;
214
215         if (length > nvram_error_log_size)
216                 length = nvram_error_log_size;
217
218         tmp_index = nvram_error_log_index;
219
220         rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
221         if (rc <= 0) {
222                 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
223                 return rc;
224         }
225
226         rc = ppc_md.nvram_read(buff, length, &tmp_index);
227         if (rc <= 0) {
228                 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
229                 return rc;
230         }
231
232         *error_log_cnt = info.seq_num;
233         *err_type = info.error_type;
234
235         return 0;
236 }
237
238 /* This doesn't actually zero anything, but it sets the event_logged
239  * word to tell that this event is safely in syslog.
240  */
241 int nvram_clear_error_log(void)
242 {
243         loff_t tmp_index;
244         int clear_word = ERR_FLAG_ALREADY_LOGGED;
245         int rc;
246
247         if (nvram_error_log_index == -1)
248                 return -1;
249
250         tmp_index = nvram_error_log_index;
251         
252         rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
253         if (rc <= 0) {
254                 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
255                 return rc;
256         }
257
258         return 0;
259 }
260
261 /* pseries_nvram_init_log_partition
262  *
263  * This will setup the partition we need for buffering the
264  * error logs and cleanup partitions if needed.
265  *
266  * The general strategy is the following:
267  * 1.) If there is log partition large enough then use it.
268  * 2.) If there is none large enough, search
269  * for a free partition that is large enough.
270  * 3.) If there is not a free partition large enough remove 
271  * _all_ OS partitions and consolidate the space.
272  * 4.) Will first try getting a chunk that will satisfy the maximum
273  * error log size (NVRAM_MAX_REQ).
274  * 5.) If the max chunk cannot be allocated then try finding a chunk
275  * that will satisfy the minum needed (NVRAM_MIN_REQ).
276  */
277 static int __init pseries_nvram_init_log_partition(void)
278 {
279         loff_t p;
280         int size;
281
282         /* Scan nvram for partitions */
283         nvram_scan_partitions();
284
285         /* Lookg for ours */
286         p = nvram_find_partition(NVRAM_LOG_PART_NAME, NVRAM_SIG_OS, &size);
287
288         /* Found one but too small, remove it */
289         if (p && size < NVRAM_MIN_REQ) {
290                 pr_info("nvram: Found too small "NVRAM_LOG_PART_NAME" partition"
291                         ",removing it...");
292                 nvram_remove_partition(NVRAM_LOG_PART_NAME, NVRAM_SIG_OS);
293                 p = 0;
294         }
295
296         /* Create one if we didn't find */
297         if (!p) {
298                 p = nvram_create_partition(NVRAM_LOG_PART_NAME, NVRAM_SIG_OS,
299                                            NVRAM_MAX_REQ, NVRAM_MIN_REQ);
300                 /* No room for it, try to get rid of any OS partition
301                  * and try again
302                  */
303                 if (p == -ENOSPC) {
304                         pr_info("nvram: No room to create "NVRAM_LOG_PART_NAME
305                                 " partition, deleting all OS partitions...");
306                         nvram_remove_partition(NULL, NVRAM_SIG_OS);
307                         p = nvram_create_partition(NVRAM_LOG_PART_NAME,
308                                                    NVRAM_SIG_OS, NVRAM_MAX_REQ,
309                                                    NVRAM_MIN_REQ);
310                 }
311         }
312
313         if (p <= 0) {
314                 pr_err("nvram: Failed to find or create "NVRAM_LOG_PART_NAME
315                        " partition, err %d\n", (int)p);
316                 return 0;
317         }
318
319         nvram_error_log_index = p;
320         nvram_error_log_size = nvram_get_partition_size(p) -
321                 sizeof(struct err_log_info);
322         
323         return 0;
324 }
325 machine_arch_initcall(pseries, pseries_nvram_init_log_partition);
326
327 int __init pSeries_nvram_init(void)
328 {
329         struct device_node *nvram;
330         const unsigned int *nbytes_p;
331         unsigned int proplen;
332
333         nvram = of_find_node_by_type(NULL, "nvram");
334         if (nvram == NULL)
335                 return -ENODEV;
336
337         nbytes_p = of_get_property(nvram, "#bytes", &proplen);
338         if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
339                 of_node_put(nvram);
340                 return -EIO;
341         }
342
343         nvram_size = *nbytes_p;
344
345         nvram_fetch = rtas_token("nvram-fetch");
346         nvram_store = rtas_token("nvram-store");
347         printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
348         of_node_put(nvram);
349
350         ppc_md.nvram_read       = pSeries_nvram_read;
351         ppc_md.nvram_write      = pSeries_nvram_write;
352         ppc_md.nvram_size       = pSeries_nvram_get_size;
353
354         return 0;
355 }