2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /dev/nvram driver for PPC64
11 * This perhaps should live in drivers/char
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>
23 #include <asm/machdep.h>
25 /* Max bytes to read/write in one go */
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);
33 static long nvram_error_log_index = -1;
34 static long nvram_error_log_size = 0;
40 #define NVRAM_MAX_REQ 2079
41 #define NVRAM_MIN_REQ 1055
43 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
52 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
55 if (*index >= nvram_size)
59 if (i + count > nvram_size)
60 count = nvram_size - i;
62 spin_lock_irqsave(&nvram_lock, flags);
64 for (; count != 0; count -= len) {
69 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
70 len) != 0) || len != done) {
71 spin_unlock_irqrestore(&nvram_lock, flags);
75 memcpy(p, nvram_buf, len);
81 spin_unlock_irqrestore(&nvram_lock, flags);
87 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
95 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
98 if (*index >= nvram_size)
102 if (i + count > nvram_size)
103 count = nvram_size - i;
105 spin_lock_irqsave(&nvram_lock, flags);
107 for (; count != 0; count -= len) {
112 memcpy(nvram_buf, p, len);
114 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
115 len) != 0) || len != done) {
116 spin_unlock_irqrestore(&nvram_lock, flags);
123 spin_unlock_irqrestore(&nvram_lock, flags);
129 static ssize_t pSeries_nvram_get_size(void)
131 return nvram_size ? nvram_size : -ENODEV;
135 /* nvram_write_error_log
137 * We need to buffer the error logs into nvram to ensure that we have
138 * the failure information to decode. If we have a severe error there
139 * is no way to guarantee that the OS or the machine is in a state to
140 * get back to user land and write the error to disk. For example if
141 * the SCSI device driver causes a Machine Check by writing to a bad
142 * IO address, there is no way of guaranteeing that the device driver
143 * is in any state that is would also be able to write the error data
144 * captured to disk, thus we buffer it in NVRAM for analysis on the
147 * In NVRAM the partition containing the error log buffer will looks like:
149 * +-----------+----------+--------+------------+------------------+
150 * | signature | checksum | length | name | data |
151 * |0 |1 |2 3|4 15|16 length-1|
152 * +-----------+----------+--------+------------+------------------+
154 * The 'data' section would look like (in bytes):
155 * +--------------+------------+-----------------------------------+
156 * | event_logged | sequence # | error log |
157 * |0 3|4 7|8 nvram_error_log_size-1|
158 * +--------------+------------+-----------------------------------+
160 * event_logged: 0 if event has not been logged to syslog, 1 if it has
161 * sequence #: The unique sequence # for each event. (until it wraps)
162 * error log: The error log from event_scan
164 int nvram_write_error_log(char * buff, int length,
165 unsigned int err_type, unsigned int error_log_cnt)
169 struct err_log_info info;
171 if (nvram_error_log_index == -1) {
175 if (length > nvram_error_log_size) {
176 length = nvram_error_log_size;
179 info.error_type = err_type;
180 info.seq_num = error_log_cnt;
182 tmp_index = nvram_error_log_index;
184 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
186 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
190 rc = ppc_md.nvram_write(buff, length, &tmp_index);
192 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
199 /* nvram_read_error_log
201 * Reads nvram for error log for at most 'length'
203 int nvram_read_error_log(char * buff, int length,
204 unsigned int * err_type, unsigned int * error_log_cnt)
208 struct err_log_info info;
210 if (nvram_error_log_index == -1)
213 if (length > nvram_error_log_size)
214 length = nvram_error_log_size;
216 tmp_index = nvram_error_log_index;
218 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
220 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
224 rc = ppc_md.nvram_read(buff, length, &tmp_index);
226 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
230 *error_log_cnt = info.seq_num;
231 *err_type = info.error_type;
236 /* This doesn't actually zero anything, but it sets the event_logged
237 * word to tell that this event is safely in syslog.
239 int nvram_clear_error_log(void)
242 int clear_word = ERR_FLAG_ALREADY_LOGGED;
245 if (nvram_error_log_index == -1)
248 tmp_index = nvram_error_log_index;
250 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
252 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
259 /* pseries_nvram_init_log_partition
261 * This will setup the partition we need for buffering the
262 * error logs and cleanup partitions if needed.
264 * The general strategy is the following:
265 * 1.) If there is ppc64,linux partition large enough then use it.
266 * 2.) If there is not a ppc64,linux partition large enough, search
267 * for a free partition that is large enough.
268 * 3.) If there is not a free partition large enough remove
269 * _all_ OS partitions and consolidate the space.
270 * 4.) Will first try getting a chunk that will satisfy the maximum
271 * error log size (NVRAM_MAX_REQ).
272 * 5.) If the max chunk cannot be allocated then try finding a chunk
273 * that will satisfy the minum needed (NVRAM_MIN_REQ).
275 static int __init pseries_nvram_init_log_partition(void)
280 /* Scan nvram for partitions */
281 nvram_scan_partitions();
284 p = nvram_find_partition("ppc64,linux", NVRAM_SIG_OS, &size);
286 /* Found one but too small, remove it */
287 if (p && size < NVRAM_MIN_REQ) {
288 pr_info("nvram: Found too small ppc64,linux partition"
290 nvram_remove_partition("ppc64,linux", NVRAM_SIG_OS);
294 /* Create one if we didn't find */
296 p = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
297 NVRAM_MAX_REQ, NVRAM_MIN_REQ);
298 /* No room for it, try to get rid of any OS partition
302 pr_info("nvram: No room to create ppc64,linux"
303 " partition, deleting all OS partitions...");
304 nvram_remove_partition(NULL, NVRAM_SIG_OS);
305 p = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
306 NVRAM_MAX_REQ, NVRAM_MIN_REQ);
311 pr_err("nvram: Failed to find or create ppc64,linux"
312 " partition, err %d\n", (int)p);
316 nvram_error_log_index = p;
317 nvram_error_log_size = nvram_get_partition_size(p) -
318 sizeof(struct err_log_info);
322 machine_arch_initcall(pseries, pseries_nvram_init_log_partition);
324 int __init pSeries_nvram_init(void)
326 struct device_node *nvram;
327 const unsigned int *nbytes_p;
328 unsigned int proplen;
330 nvram = of_find_node_by_type(NULL, "nvram");
334 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
335 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
340 nvram_size = *nbytes_p;
342 nvram_fetch = rtas_token("nvram-fetch");
343 nvram_store = rtas_token("nvram-store");
344 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
347 ppc_md.nvram_read = pSeries_nvram_read;
348 ppc_md.nvram_write = pSeries_nvram_write;
349 ppc_md.nvram_size = pSeries_nvram_get_size;