]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/powerpc/kernel/nvram_64.c
powerpc/nvram: Move generic code for nvram and pstore
[linux.git] / arch / powerpc / kernel / nvram_64.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  * TODO: Split the /dev/nvram part (that one can use
14  *       drivers/char/generic_nvram.c) from the arch & partition
15  *       parsing code.
16  */
17
18 #include <linux/module.h>
19
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/fs.h>
23 #include <linux/miscdevice.h>
24 #include <linux/fcntl.h>
25 #include <linux/nvram.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/kmsg_dump.h>
30 #include <linux/pstore.h>
31 #include <linux/zlib.h>
32 #include <asm/uaccess.h>
33 #include <asm/nvram.h>
34 #include <asm/rtas.h>
35 #include <asm/prom.h>
36 #include <asm/machdep.h>
37
38 #undef DEBUG_NVRAM
39
40 #define NVRAM_HEADER_LEN        sizeof(struct nvram_header)
41 #define NVRAM_BLOCK_LEN         NVRAM_HEADER_LEN
42
43 /* If change this size, then change the size of NVNAME_LEN */
44 struct nvram_header {
45         unsigned char signature;
46         unsigned char checksum;
47         unsigned short length;
48         /* Terminating null required only for names < 12 chars. */
49         char name[12];
50 };
51
52 struct nvram_partition {
53         struct list_head partition;
54         struct nvram_header header;
55         unsigned int index;
56 };
57
58 static LIST_HEAD(nvram_partitions);
59
60 #ifdef CONFIG_PPC_PSERIES
61 struct nvram_os_partition rtas_log_partition = {
62         .name = "ibm,rtas-log",
63         .req_size = 2079,
64         .min_size = 1055,
65         .index = -1,
66         .os_partition = true
67 };
68 #endif
69
70 struct nvram_os_partition oops_log_partition = {
71         .name = "lnx,oops-log",
72         .req_size = 4000,
73         .min_size = 2000,
74         .index = -1,
75         .os_partition = true
76 };
77
78 static const char *nvram_os_partitions[] = {
79 #ifdef CONFIG_PPC_PSERIES
80         "ibm,rtas-log",
81 #endif
82         "lnx,oops-log",
83         NULL
84 };
85
86 static void oops_to_nvram(struct kmsg_dumper *dumper,
87                           enum kmsg_dump_reason reason);
88
89 static struct kmsg_dumper nvram_kmsg_dumper = {
90         .dump = oops_to_nvram
91 };
92
93 /*
94  * For capturing and compressing an oops or panic report...
95
96  * big_oops_buf[] holds the uncompressed text we're capturing.
97  *
98  * oops_buf[] holds the compressed text, preceded by a oops header.
99  * oops header has u16 holding the version of oops header (to differentiate
100  * between old and new format header) followed by u16 holding the length of
101  * the compressed* text (*Or uncompressed, if compression fails.) and u64
102  * holding the timestamp. oops_buf[] gets written to NVRAM.
103  *
104  * oops_log_info points to the header. oops_data points to the compressed text.
105  *
106  * +- oops_buf
107  * |                                   +- oops_data
108  * v                                   v
109  * +-----------+-----------+-----------+------------------------+
110  * | version   | length    | timestamp | text                   |
111  * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes)   |
112  * +-----------+-----------+-----------+------------------------+
113  * ^
114  * +- oops_log_info
115  *
116  * We preallocate these buffers during init to avoid kmalloc during oops/panic.
117  */
118 static size_t big_oops_buf_sz;
119 static char *big_oops_buf, *oops_buf;
120 static char *oops_data;
121 static size_t oops_data_sz;
122
123 /* Compression parameters */
124 #define COMPR_LEVEL 6
125 #define WINDOW_BITS 12
126 #define MEM_LEVEL 4
127 static struct z_stream_s stream;
128
129 #ifdef CONFIG_PSTORE
130 #ifdef CONFIG_PPC_PSERIES
131 static struct nvram_os_partition of_config_partition = {
132         .name = "of-config",
133         .index = -1,
134         .os_partition = false
135 };
136 #endif
137
138 static struct nvram_os_partition common_partition = {
139         .name = "common",
140         .index = -1,
141         .os_partition = false
142 };
143
144 static enum pstore_type_id nvram_type_ids[] = {
145         PSTORE_TYPE_DMESG,
146         PSTORE_TYPE_PPC_COMMON,
147         -1,
148         -1,
149         -1
150 };
151 static int read_type;
152 #endif
153
154 /* nvram_write_os_partition
155  *
156  * We need to buffer the error logs into nvram to ensure that we have
157  * the failure information to decode.  If we have a severe error there
158  * is no way to guarantee that the OS or the machine is in a state to
159  * get back to user land and write the error to disk.  For example if
160  * the SCSI device driver causes a Machine Check by writing to a bad
161  * IO address, there is no way of guaranteeing that the device driver
162  * is in any state that is would also be able to write the error data
163  * captured to disk, thus we buffer it in NVRAM for analysis on the
164  * next boot.
165  *
166  * In NVRAM the partition containing the error log buffer will looks like:
167  * Header (in bytes):
168  * +-----------+----------+--------+------------+------------------+
169  * | signature | checksum | length | name       | data             |
170  * |0          |1         |2      3|4         15|16        length-1|
171  * +-----------+----------+--------+------------+------------------+
172  *
173  * The 'data' section would look like (in bytes):
174  * +--------------+------------+-----------------------------------+
175  * | event_logged | sequence # | error log                         |
176  * |0            3|4          7|8                  error_log_size-1|
177  * +--------------+------------+-----------------------------------+
178  *
179  * event_logged: 0 if event has not been logged to syslog, 1 if it has
180  * sequence #: The unique sequence # for each event. (until it wraps)
181  * error log: The error log from event_scan
182  */
183 int nvram_write_os_partition(struct nvram_os_partition *part,
184                              char *buff, int length,
185                              unsigned int err_type,
186                              unsigned int error_log_cnt)
187 {
188         int rc;
189         loff_t tmp_index;
190         struct err_log_info info;
191
192         if (part->index == -1)
193                 return -ESPIPE;
194
195         if (length > part->size)
196                 length = part->size;
197
198         info.error_type = cpu_to_be32(err_type);
199         info.seq_num = cpu_to_be32(error_log_cnt);
200
201         tmp_index = part->index;
202
203         rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info),
204                                 &tmp_index);
205         if (rc <= 0) {
206                 pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
207                 return rc;
208         }
209
210         rc = ppc_md.nvram_write(buff, length, &tmp_index);
211         if (rc <= 0) {
212                 pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
213                 return rc;
214         }
215
216         return 0;
217 }
218
219 /* nvram_read_partition
220  *
221  * Reads nvram partition for at most 'length'
222  */
223 int nvram_read_partition(struct nvram_os_partition *part, char *buff,
224                          int length, unsigned int *err_type,
225                          unsigned int *error_log_cnt)
226 {
227         int rc;
228         loff_t tmp_index;
229         struct err_log_info info;
230
231         if (part->index == -1)
232                 return -1;
233
234         if (length > part->size)
235                 length = part->size;
236
237         tmp_index = part->index;
238
239         if (part->os_partition) {
240                 rc = ppc_md.nvram_read((char *)&info,
241                                         sizeof(struct err_log_info),
242                                         &tmp_index);
243                 if (rc <= 0) {
244                         pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
245                         return rc;
246                 }
247         }
248
249         rc = ppc_md.nvram_read(buff, length, &tmp_index);
250         if (rc <= 0) {
251                 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
252                 return rc;
253         }
254
255         if (part->os_partition) {
256                 *error_log_cnt = be32_to_cpu(info.seq_num);
257                 *err_type = be32_to_cpu(info.error_type);
258         }
259
260         return 0;
261 }
262
263 /* nvram_init_os_partition
264  *
265  * This sets up a partition with an "OS" signature.
266  *
267  * The general strategy is the following:
268  * 1.) If a partition with the indicated name already exists...
269  *      - If it's large enough, use it.
270  *      - Otherwise, recycle it and keep going.
271  * 2.) Search for a free partition that is large enough.
272  * 3.) If there's not a free partition large enough, recycle any obsolete
273  * OS partitions and try again.
274  * 4.) Will first try getting a chunk that will satisfy the requested size.
275  * 5.) If a chunk of the requested size cannot be allocated, then try finding
276  * a chunk that will satisfy the minum needed.
277  *
278  * Returns 0 on success, else -1.
279  */
280 int __init nvram_init_os_partition(struct nvram_os_partition *part)
281 {
282         loff_t p;
283         int size;
284
285         /* Look for ours */
286         p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
287
288         /* Found one but too small, remove it */
289         if (p && size < part->min_size) {
290                 pr_info("nvram: Found too small %s partition,"
291                                         " removing it...\n", part->name);
292                 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
293                 p = 0;
294         }
295
296         /* Create one if we didn't find */
297         if (!p) {
298                 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
299                                         part->req_size, part->min_size);
300                 if (p == -ENOSPC) {
301                         pr_info("nvram: No room to create %s partition, "
302                                 "deleting any obsolete OS partitions...\n",
303                                 part->name);
304                         nvram_remove_partition(NULL, NVRAM_SIG_OS,
305                                         nvram_os_partitions);
306                         p = nvram_create_partition(part->name, NVRAM_SIG_OS,
307                                         part->req_size, part->min_size);
308                 }
309         }
310
311         if (p <= 0) {
312                 pr_err("nvram: Failed to find or create %s"
313                        " partition, err %d\n", part->name, (int)p);
314                 return -1;
315         }
316
317         part->index = p;
318         part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
319
320         return 0;
321 }
322
323 /* Derived from logfs_compress() */
324 static int nvram_compress(const void *in, void *out, size_t inlen,
325                                                         size_t outlen)
326 {
327         int err, ret;
328
329         ret = -EIO;
330         err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
331                                                 MEM_LEVEL, Z_DEFAULT_STRATEGY);
332         if (err != Z_OK)
333                 goto error;
334
335         stream.next_in = in;
336         stream.avail_in = inlen;
337         stream.total_in = 0;
338         stream.next_out = out;
339         stream.avail_out = outlen;
340         stream.total_out = 0;
341
342         err = zlib_deflate(&stream, Z_FINISH);
343         if (err != Z_STREAM_END)
344                 goto error;
345
346         err = zlib_deflateEnd(&stream);
347         if (err != Z_OK)
348                 goto error;
349
350         if (stream.total_out >= stream.total_in)
351                 goto error;
352
353         ret = stream.total_out;
354 error:
355         return ret;
356 }
357
358 /* Compress the text from big_oops_buf into oops_buf. */
359 static int zip_oops(size_t text_len)
360 {
361         struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
362         int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
363                                                                 oops_data_sz);
364         if (zipped_len < 0) {
365                 pr_err("nvram: compression failed; returned %d\n", zipped_len);
366                 pr_err("nvram: logging uncompressed oops/panic report\n");
367                 return -1;
368         }
369         oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
370         oops_hdr->report_length = cpu_to_be16(zipped_len);
371         oops_hdr->timestamp = cpu_to_be64(get_seconds());
372         return 0;
373 }
374
375 #ifdef CONFIG_PSTORE
376 static int nvram_pstore_open(struct pstore_info *psi)
377 {
378         /* Reset the iterator to start reading partitions again */
379         read_type = -1;
380         return 0;
381 }
382
383 /**
384  * nvram_pstore_write - pstore write callback for nvram
385  * @type:               Type of message logged
386  * @reason:             reason behind dump (oops/panic)
387  * @id:                 identifier to indicate the write performed
388  * @part:               pstore writes data to registered buffer in parts,
389  *                      part number will indicate the same.
390  * @count:              Indicates oops count
391  * @compressed:         Flag to indicate the log is compressed
392  * @size:               number of bytes written to the registered buffer
393  * @psi:                registered pstore_info structure
394  *
395  * Called by pstore_dump() when an oops or panic report is logged in the
396  * printk buffer.
397  * Returns 0 on successful write.
398  */
399 static int nvram_pstore_write(enum pstore_type_id type,
400                                 enum kmsg_dump_reason reason,
401                                 u64 *id, unsigned int part, int count,
402                                 bool compressed, size_t size,
403                                 struct pstore_info *psi)
404 {
405         int rc;
406         unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
407         struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
408
409         /* part 1 has the recent messages from printk buffer */
410         if (part > 1 || (type != PSTORE_TYPE_DMESG))
411                 return -1;
412
413         if (clobbering_unread_rtas_event())
414                 return -1;
415
416         oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
417         oops_hdr->report_length = cpu_to_be16(size);
418         oops_hdr->timestamp = cpu_to_be64(get_seconds());
419
420         if (compressed)
421                 err_type = ERR_TYPE_KERNEL_PANIC_GZ;
422
423         rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
424                 (int) (sizeof(*oops_hdr) + size), err_type, count);
425
426         if (rc != 0)
427                 return rc;
428
429         *id = part;
430         return 0;
431 }
432
433 /*
434  * Reads the oops/panic report, rtas, of-config and common partition.
435  * Returns the length of the data we read from each partition.
436  * Returns 0 if we've been called before.
437  */
438 static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
439                                 int *count, struct timespec *time, char **buf,
440                                 bool *compressed, struct pstore_info *psi)
441 {
442         struct oops_log_info *oops_hdr;
443         unsigned int err_type, id_no, size = 0;
444         struct nvram_os_partition *part = NULL;
445         char *buff = NULL;
446         int sig = 0;
447         loff_t p;
448
449         read_type++;
450
451         switch (nvram_type_ids[read_type]) {
452         case PSTORE_TYPE_DMESG:
453                 part = &oops_log_partition;
454                 *type = PSTORE_TYPE_DMESG;
455                 break;
456         case PSTORE_TYPE_PPC_COMMON:
457                 sig = NVRAM_SIG_SYS;
458                 part = &common_partition;
459                 *type = PSTORE_TYPE_PPC_COMMON;
460                 *id = PSTORE_TYPE_PPC_COMMON;
461                 time->tv_sec = 0;
462                 time->tv_nsec = 0;
463                 break;
464 #ifdef CONFIG_PPC_PSERIES
465         case PSTORE_TYPE_PPC_RTAS:
466                 part = &rtas_log_partition;
467                 *type = PSTORE_TYPE_PPC_RTAS;
468                 time->tv_sec = last_rtas_event;
469                 time->tv_nsec = 0;
470                 break;
471         case PSTORE_TYPE_PPC_OF:
472                 sig = NVRAM_SIG_OF;
473                 part = &of_config_partition;
474                 *type = PSTORE_TYPE_PPC_OF;
475                 *id = PSTORE_TYPE_PPC_OF;
476                 time->tv_sec = 0;
477                 time->tv_nsec = 0;
478                 break;
479 #endif
480         default:
481                 return 0;
482         }
483
484         if (!part->os_partition) {
485                 p = nvram_find_partition(part->name, sig, &size);
486                 if (p <= 0) {
487                         pr_err("nvram: Failed to find partition %s, "
488                                 "err %d\n", part->name, (int)p);
489                         return 0;
490                 }
491                 part->index = p;
492                 part->size = size;
493         }
494
495         buff = kmalloc(part->size, GFP_KERNEL);
496
497         if (!buff)
498                 return -ENOMEM;
499
500         if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
501                 kfree(buff);
502                 return 0;
503         }
504
505         *count = 0;
506
507         if (part->os_partition)
508                 *id = id_no;
509
510         if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
511                 size_t length, hdr_size;
512
513                 oops_hdr = (struct oops_log_info *)buff;
514                 if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
515                         /* Old format oops header had 2-byte record size */
516                         hdr_size = sizeof(u16);
517                         length = be16_to_cpu(oops_hdr->version);
518                         time->tv_sec = 0;
519                         time->tv_nsec = 0;
520                 } else {
521                         hdr_size = sizeof(*oops_hdr);
522                         length = be16_to_cpu(oops_hdr->report_length);
523                         time->tv_sec = be64_to_cpu(oops_hdr->timestamp);
524                         time->tv_nsec = 0;
525                 }
526                 *buf = kmalloc(length, GFP_KERNEL);
527                 if (*buf == NULL)
528                         return -ENOMEM;
529                 memcpy(*buf, buff + hdr_size, length);
530                 kfree(buff);
531
532                 if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
533                         *compressed = true;
534                 else
535                         *compressed = false;
536                 return length;
537         }
538
539         *buf = buff;
540         return part->size;
541 }
542
543 static struct pstore_info nvram_pstore_info = {
544         .owner = THIS_MODULE,
545         .name = "nvram",
546         .open = nvram_pstore_open,
547         .read = nvram_pstore_read,
548         .write = nvram_pstore_write,
549 };
550
551 static int nvram_pstore_init(void)
552 {
553         int rc = 0;
554
555         nvram_type_ids[2] = PSTORE_TYPE_PPC_RTAS;
556         nvram_type_ids[3] = PSTORE_TYPE_PPC_OF;
557
558         nvram_pstore_info.buf = oops_data;
559         nvram_pstore_info.bufsize = oops_data_sz;
560
561         spin_lock_init(&nvram_pstore_info.buf_lock);
562
563         rc = pstore_register(&nvram_pstore_info);
564         if (rc != 0)
565                 pr_err("nvram: pstore_register() failed, defaults to "
566                                 "kmsg_dump; returned %d\n", rc);
567
568         return rc;
569 }
570 #else
571 static int nvram_pstore_init(void)
572 {
573         return -1;
574 }
575 #endif
576
577 void __init nvram_init_oops_partition(int rtas_partition_exists)
578 {
579         int rc;
580
581         rc = nvram_init_os_partition(&oops_log_partition);
582         if (rc != 0) {
583 #ifdef CONFIG_PPC_PSERIES
584                 if (!rtas_partition_exists) {
585                         pr_err("nvram: Failed to initialize oops partition!");
586                         return;
587                 }
588                 pr_notice("nvram: Using %s partition to log both"
589                         " RTAS errors and oops/panic reports\n",
590                         rtas_log_partition.name);
591                 memcpy(&oops_log_partition, &rtas_log_partition,
592                                                 sizeof(rtas_log_partition));
593 #else
594                 pr_err("nvram: Failed to initialize oops partition!");
595                 return;
596 #endif
597         }
598         oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
599         if (!oops_buf) {
600                 pr_err("nvram: No memory for %s partition\n",
601                                                 oops_log_partition.name);
602                 return;
603         }
604         oops_data = oops_buf + sizeof(struct oops_log_info);
605         oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
606
607         rc = nvram_pstore_init();
608
609         if (!rc)
610                 return;
611
612         /*
613          * Figure compression (preceded by elimination of each line's <n>
614          * severity prefix) will reduce the oops/panic report to at most
615          * 45% of its original size.
616          */
617         big_oops_buf_sz = (oops_data_sz * 100) / 45;
618         big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
619         if (big_oops_buf) {
620                 stream.workspace =  kmalloc(zlib_deflate_workspacesize(
621                                         WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
622                 if (!stream.workspace) {
623                         pr_err("nvram: No memory for compression workspace; "
624                                 "skipping compression of %s partition data\n",
625                                 oops_log_partition.name);
626                         kfree(big_oops_buf);
627                         big_oops_buf = NULL;
628                 }
629         } else {
630                 pr_err("No memory for uncompressed %s data; "
631                         "skipping compression\n", oops_log_partition.name);
632                 stream.workspace = NULL;
633         }
634
635         rc = kmsg_dump_register(&nvram_kmsg_dumper);
636         if (rc != 0) {
637                 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
638                 kfree(oops_buf);
639                 kfree(big_oops_buf);
640                 kfree(stream.workspace);
641         }
642 }
643
644 /*
645  * This is our kmsg_dump callback, called after an oops or panic report
646  * has been written to the printk buffer.  We want to capture as much
647  * of the printk buffer as possible.  First, capture as much as we can
648  * that we think will compress sufficiently to fit in the lnx,oops-log
649  * partition.  If that's too much, go back and capture uncompressed text.
650  */
651 static void oops_to_nvram(struct kmsg_dumper *dumper,
652                           enum kmsg_dump_reason reason)
653 {
654         struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
655         static unsigned int oops_count = 0;
656         static bool panicking = false;
657         static DEFINE_SPINLOCK(lock);
658         unsigned long flags;
659         size_t text_len;
660         unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
661         int rc = -1;
662
663         switch (reason) {
664         case KMSG_DUMP_RESTART:
665         case KMSG_DUMP_HALT:
666         case KMSG_DUMP_POWEROFF:
667                 /* These are almost always orderly shutdowns. */
668                 return;
669         case KMSG_DUMP_OOPS:
670                 break;
671         case KMSG_DUMP_PANIC:
672                 panicking = true;
673                 break;
674         case KMSG_DUMP_EMERG:
675                 if (panicking)
676                         /* Panic report already captured. */
677                         return;
678                 break;
679         default:
680                 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
681                        __func__, (int) reason);
682                 return;
683         }
684
685         if (clobbering_unread_rtas_event())
686                 return;
687
688         if (!spin_trylock_irqsave(&lock, flags))
689                 return;
690
691         if (big_oops_buf) {
692                 kmsg_dump_get_buffer(dumper, false,
693                                      big_oops_buf, big_oops_buf_sz, &text_len);
694                 rc = zip_oops(text_len);
695         }
696         if (rc != 0) {
697                 kmsg_dump_rewind(dumper);
698                 kmsg_dump_get_buffer(dumper, false,
699                                      oops_data, oops_data_sz, &text_len);
700                 err_type = ERR_TYPE_KERNEL_PANIC;
701                 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
702                 oops_hdr->report_length = cpu_to_be16(text_len);
703                 oops_hdr->timestamp = cpu_to_be64(get_seconds());
704         }
705
706         (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
707                 (int) (sizeof(*oops_hdr) + text_len), err_type,
708                 ++oops_count);
709
710         spin_unlock_irqrestore(&lock, flags);
711 }
712
713 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
714 {
715         int size;
716
717         if (ppc_md.nvram_size == NULL)
718                 return -ENODEV;
719         size = ppc_md.nvram_size();
720
721         switch (origin) {
722         case 1:
723                 offset += file->f_pos;
724                 break;
725         case 2:
726                 offset += size;
727                 break;
728         }
729         if (offset < 0)
730                 return -EINVAL;
731         file->f_pos = offset;
732         return file->f_pos;
733 }
734
735
736 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
737                           size_t count, loff_t *ppos)
738 {
739         ssize_t ret;
740         char *tmp = NULL;
741         ssize_t size;
742
743         if (!ppc_md.nvram_size) {
744                 ret = -ENODEV;
745                 goto out;
746         }
747
748         size = ppc_md.nvram_size();
749         if (size < 0) {
750                 ret = size;
751                 goto out;
752         }
753
754         if (*ppos >= size) {
755                 ret = 0;
756                 goto out;
757         }
758
759         count = min_t(size_t, count, size - *ppos);
760         count = min(count, PAGE_SIZE);
761
762         tmp = kmalloc(count, GFP_KERNEL);
763         if (!tmp) {
764                 ret = -ENOMEM;
765                 goto out;
766         }
767
768         ret = ppc_md.nvram_read(tmp, count, ppos);
769         if (ret <= 0)
770                 goto out;
771
772         if (copy_to_user(buf, tmp, ret))
773                 ret = -EFAULT;
774
775 out:
776         kfree(tmp);
777         return ret;
778
779 }
780
781 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
782                           size_t count, loff_t *ppos)
783 {
784         ssize_t ret;
785         char *tmp = NULL;
786         ssize_t size;
787
788         ret = -ENODEV;
789         if (!ppc_md.nvram_size)
790                 goto out;
791
792         ret = 0;
793         size = ppc_md.nvram_size();
794         if (*ppos >= size || size < 0)
795                 goto out;
796
797         count = min_t(size_t, count, size - *ppos);
798         count = min(count, PAGE_SIZE);
799
800         ret = -ENOMEM;
801         tmp = kmalloc(count, GFP_KERNEL);
802         if (!tmp)
803                 goto out;
804
805         ret = -EFAULT;
806         if (copy_from_user(tmp, buf, count))
807                 goto out;
808
809         ret = ppc_md.nvram_write(tmp, count, ppos);
810
811 out:
812         kfree(tmp);
813         return ret;
814
815 }
816
817 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
818                             unsigned long arg)
819 {
820         switch(cmd) {
821 #ifdef CONFIG_PPC_PMAC
822         case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
823                 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
824         case IOC_NVRAM_GET_OFFSET: {
825                 int part, offset;
826
827                 if (!machine_is(powermac))
828                         return -EINVAL;
829                 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
830                         return -EFAULT;
831                 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
832                         return -EINVAL;
833                 offset = pmac_get_partition(part);
834                 if (offset < 0)
835                         return offset;
836                 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
837                         return -EFAULT;
838                 return 0;
839         }
840 #endif /* CONFIG_PPC_PMAC */
841         default:
842                 return -EINVAL;
843         }
844 }
845
846 const struct file_operations nvram_fops = {
847         .owner          = THIS_MODULE,
848         .llseek         = dev_nvram_llseek,
849         .read           = dev_nvram_read,
850         .write          = dev_nvram_write,
851         .unlocked_ioctl = dev_nvram_ioctl,
852 };
853
854 static struct miscdevice nvram_dev = {
855         NVRAM_MINOR,
856         "nvram",
857         &nvram_fops
858 };
859
860
861 #ifdef DEBUG_NVRAM
862 static void __init nvram_print_partitions(char * label)
863 {
864         struct nvram_partition * tmp_part;
865         
866         printk(KERN_WARNING "--------%s---------\n", label);
867         printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
868         list_for_each_entry(tmp_part, &nvram_partitions, partition) {
869                 printk(KERN_WARNING "%4d    \t%02x\t%02x\t%d\t%12.12s\n",
870                        tmp_part->index, tmp_part->header.signature,
871                        tmp_part->header.checksum, tmp_part->header.length,
872                        tmp_part->header.name);
873         }
874 }
875 #endif
876
877
878 static int __init nvram_write_header(struct nvram_partition * part)
879 {
880         loff_t tmp_index;
881         int rc;
882         struct nvram_header phead;
883
884         memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
885         phead.length = cpu_to_be16(phead.length);
886
887         tmp_index = part->index;
888         rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
889
890         return rc;
891 }
892
893
894 static unsigned char __init nvram_checksum(struct nvram_header *p)
895 {
896         unsigned int c_sum, c_sum2;
897         unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
898         c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
899
900         /* The sum may have spilled into the 3rd byte.  Fold it back. */
901         c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
902         /* The sum cannot exceed 2 bytes.  Fold it into a checksum */
903         c_sum2 = (c_sum >> 8) + (c_sum << 8);
904         c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
905         return c_sum;
906 }
907
908 /*
909  * Per the criteria passed via nvram_remove_partition(), should this
910  * partition be removed?  1=remove, 0=keep
911  */
912 static int nvram_can_remove_partition(struct nvram_partition *part,
913                 const char *name, int sig, const char *exceptions[])
914 {
915         if (part->header.signature != sig)
916                 return 0;
917         if (name) {
918                 if (strncmp(name, part->header.name, 12))
919                         return 0;
920         } else if (exceptions) {
921                 const char **except;
922                 for (except = exceptions; *except; except++) {
923                         if (!strncmp(*except, part->header.name, 12))
924                                 return 0;
925                 }
926         }
927         return 1;
928 }
929
930 /**
931  * nvram_remove_partition - Remove one or more partitions in nvram
932  * @name: name of the partition to remove, or NULL for a
933  *        signature only match
934  * @sig: signature of the partition(s) to remove
935  * @exceptions: When removing all partitions with a matching signature,
936  *        leave these alone.
937  */
938
939 int __init nvram_remove_partition(const char *name, int sig,
940                                                 const char *exceptions[])
941 {
942         struct nvram_partition *part, *prev, *tmp;
943         int rc;
944
945         list_for_each_entry(part, &nvram_partitions, partition) {
946                 if (!nvram_can_remove_partition(part, name, sig, exceptions))
947                         continue;
948
949                 /* Make partition a free partition */
950                 part->header.signature = NVRAM_SIG_FREE;
951                 strncpy(part->header.name, "wwwwwwwwwwww", 12);
952                 part->header.checksum = nvram_checksum(&part->header);
953                 rc = nvram_write_header(part);
954                 if (rc <= 0) {
955                         printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
956                         return rc;
957                 }
958         }
959
960         /* Merge contiguous ones */
961         prev = NULL;
962         list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
963                 if (part->header.signature != NVRAM_SIG_FREE) {
964                         prev = NULL;
965                         continue;
966                 }
967                 if (prev) {
968                         prev->header.length += part->header.length;
969                         prev->header.checksum = nvram_checksum(&part->header);
970                         rc = nvram_write_header(part);
971                         if (rc <= 0) {
972                                 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
973                                 return rc;
974                         }
975                         list_del(&part->partition);
976                         kfree(part);
977                 } else
978                         prev = part;
979         }
980         
981         return 0;
982 }
983
984 /**
985  * nvram_create_partition - Create a partition in nvram
986  * @name: name of the partition to create
987  * @sig: signature of the partition to create
988  * @req_size: size of data to allocate in bytes
989  * @min_size: minimum acceptable size (0 means req_size)
990  *
991  * Returns a negative error code or a positive nvram index
992  * of the beginning of the data area of the newly created
993  * partition. If you provided a min_size smaller than req_size
994  * you need to query for the actual size yourself after the
995  * call using nvram_partition_get_size().
996  */
997 loff_t __init nvram_create_partition(const char *name, int sig,
998                                      int req_size, int min_size)
999 {
1000         struct nvram_partition *part;
1001         struct nvram_partition *new_part;
1002         struct nvram_partition *free_part = NULL;
1003         static char nv_init_vals[16];
1004         loff_t tmp_index;
1005         long size = 0;
1006         int rc;
1007
1008         /* Convert sizes from bytes to blocks */
1009         req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
1010         min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
1011
1012         /* If no minimum size specified, make it the same as the
1013          * requested size
1014          */
1015         if (min_size == 0)
1016                 min_size = req_size;
1017         if (min_size > req_size)
1018                 return -EINVAL;
1019
1020         /* Now add one block to each for the header */
1021         req_size += 1;
1022         min_size += 1;
1023
1024         /* Find a free partition that will give us the maximum needed size 
1025            If can't find one that will give us the minimum size needed */
1026         list_for_each_entry(part, &nvram_partitions, partition) {
1027                 if (part->header.signature != NVRAM_SIG_FREE)
1028                         continue;
1029
1030                 if (part->header.length >= req_size) {
1031                         size = req_size;
1032                         free_part = part;
1033                         break;
1034                 }
1035                 if (part->header.length > size &&
1036                     part->header.length >= min_size) {
1037                         size = part->header.length;
1038                         free_part = part;
1039                 }
1040         }
1041         if (!size)
1042                 return -ENOSPC;
1043         
1044         /* Create our OS partition */
1045         new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
1046         if (!new_part) {
1047                 pr_err("nvram_create_os_partition: kmalloc failed\n");
1048                 return -ENOMEM;
1049         }
1050
1051         new_part->index = free_part->index;
1052         new_part->header.signature = sig;
1053         new_part->header.length = size;
1054         strncpy(new_part->header.name, name, 12);
1055         new_part->header.checksum = nvram_checksum(&new_part->header);
1056
1057         rc = nvram_write_header(new_part);
1058         if (rc <= 0) {
1059                 pr_err("nvram_create_os_partition: nvram_write_header "
1060                        "failed (%d)\n", rc);
1061                 return rc;
1062         }
1063         list_add_tail(&new_part->partition, &free_part->partition);
1064
1065         /* Adjust or remove the partition we stole the space from */
1066         if (free_part->header.length > size) {
1067                 free_part->index += size * NVRAM_BLOCK_LEN;
1068                 free_part->header.length -= size;
1069                 free_part->header.checksum = nvram_checksum(&free_part->header);
1070                 rc = nvram_write_header(free_part);
1071                 if (rc <= 0) {
1072                         pr_err("nvram_create_os_partition: nvram_write_header "
1073                                "failed (%d)\n", rc);
1074                         return rc;
1075                 }
1076         } else {
1077                 list_del(&free_part->partition);
1078                 kfree(free_part);
1079         } 
1080
1081         /* Clear the new partition */
1082         for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
1083              tmp_index <  ((size - 1) * NVRAM_BLOCK_LEN);
1084              tmp_index += NVRAM_BLOCK_LEN) {
1085                 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
1086                 if (rc <= 0) {
1087                         pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
1088                         return rc;
1089                 }
1090         }
1091         
1092         return new_part->index + NVRAM_HEADER_LEN;
1093 }
1094
1095 /**
1096  * nvram_get_partition_size - Get the data size of an nvram partition
1097  * @data_index: This is the offset of the start of the data of
1098  *              the partition. The same value that is returned by
1099  *              nvram_create_partition().
1100  */
1101 int nvram_get_partition_size(loff_t data_index)
1102 {
1103         struct nvram_partition *part;
1104         
1105         list_for_each_entry(part, &nvram_partitions, partition) {
1106                 if (part->index + NVRAM_HEADER_LEN == data_index)
1107                         return (part->header.length - 1) * NVRAM_BLOCK_LEN;
1108         }
1109         return -1;
1110 }
1111
1112
1113 /**
1114  * nvram_find_partition - Find an nvram partition by signature and name
1115  * @name: Name of the partition or NULL for any name
1116  * @sig: Signature to test against
1117  * @out_size: if non-NULL, returns the size of the data part of the partition
1118  */
1119 loff_t nvram_find_partition(const char *name, int sig, int *out_size)
1120 {
1121         struct nvram_partition *p;
1122
1123         list_for_each_entry(p, &nvram_partitions, partition) {
1124                 if (p->header.signature == sig &&
1125                     (!name || !strncmp(p->header.name, name, 12))) {
1126                         if (out_size)
1127                                 *out_size = (p->header.length - 1) *
1128                                         NVRAM_BLOCK_LEN;
1129                         return p->index + NVRAM_HEADER_LEN;
1130                 }
1131         }
1132         return 0;
1133 }
1134
1135 int __init nvram_scan_partitions(void)
1136 {
1137         loff_t cur_index = 0;
1138         struct nvram_header phead;
1139         struct nvram_partition * tmp_part;
1140         unsigned char c_sum;
1141         char * header;
1142         int total_size;
1143         int err;
1144
1145         if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
1146                 return -ENODEV;
1147         total_size = ppc_md.nvram_size();
1148         
1149         header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
1150         if (!header) {
1151                 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
1152                 return -ENOMEM;
1153         }
1154
1155         while (cur_index < total_size) {
1156
1157                 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
1158                 if (err != NVRAM_HEADER_LEN) {
1159                         printk(KERN_ERR "nvram_scan_partitions: Error parsing "
1160                                "nvram partitions\n");
1161                         goto out;
1162                 }
1163
1164                 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
1165
1166                 memcpy(&phead, header, NVRAM_HEADER_LEN);
1167
1168                 phead.length = be16_to_cpu(phead.length);
1169
1170                 err = 0;
1171                 c_sum = nvram_checksum(&phead);
1172                 if (c_sum != phead.checksum) {
1173                         printk(KERN_WARNING "WARNING: nvram partition checksum"
1174                                " was %02x, should be %02x!\n",
1175                                phead.checksum, c_sum);
1176                         printk(KERN_WARNING "Terminating nvram partition scan\n");
1177                         goto out;
1178                 }
1179                 if (!phead.length) {
1180                         printk(KERN_WARNING "WARNING: nvram corruption "
1181                                "detected: 0-length partition\n");
1182                         goto out;
1183                 }
1184                 tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
1185                 err = -ENOMEM;
1186                 if (!tmp_part) {
1187                         printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
1188                         goto out;
1189                 }
1190                 
1191                 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
1192                 tmp_part->index = cur_index;
1193                 list_add_tail(&tmp_part->partition, &nvram_partitions);
1194                 
1195                 cur_index += phead.length * NVRAM_BLOCK_LEN;
1196         }
1197         err = 0;
1198
1199 #ifdef DEBUG_NVRAM
1200         nvram_print_partitions("NVRAM Partitions");
1201 #endif
1202
1203  out:
1204         kfree(header);
1205         return err;
1206 }
1207
1208 static int __init nvram_init(void)
1209 {
1210         int rc;
1211         
1212         BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
1213
1214         if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
1215                 return  -ENODEV;
1216
1217         rc = misc_register(&nvram_dev);
1218         if (rc != 0) {
1219                 printk(KERN_ERR "nvram_init: failed to register device\n");
1220                 return rc;
1221         }
1222         
1223         return rc;
1224 }
1225
1226 static void __exit nvram_cleanup(void)
1227 {
1228         misc_deregister( &nvram_dev );
1229 }
1230
1231 module_init(nvram_init);
1232 module_exit(nvram_cleanup);
1233 MODULE_LICENSE("GPL");