OSDN Git Service

powerpc/nvram: Change nvram_setup_partition() to use new helper
[sagit-ice-cold/kernel_xiaomi_msm8998.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 <asm/uaccess.h>
30 #include <asm/nvram.h>
31 #include <asm/rtas.h>
32 #include <asm/prom.h>
33 #include <asm/machdep.h>
34
35 #undef DEBUG_NVRAM
36
37 #define NVRAM_HEADER_LEN        sizeof(struct nvram_header)
38 #define NVRAM_BLOCK_LEN         NVRAM_HEADER_LEN
39 #define NVRAM_MAX_REQ           2079
40 #define NVRAM_MIN_REQ           1055
41
42 /* If change this size, then change the size of NVNAME_LEN */
43 struct nvram_header {
44         unsigned char signature;
45         unsigned char checksum;
46         unsigned short length;
47         char name[12];
48 };
49
50 struct nvram_partition {
51         struct list_head partition;
52         struct nvram_header header;
53         unsigned int index;
54 };
55
56 static struct nvram_partition * nvram_part;
57 static long nvram_error_log_index = -1;
58 static long nvram_error_log_size = 0;
59
60 struct err_log_info {
61         int error_type;
62         unsigned int seq_num;
63 };
64
65 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
66 {
67         int size;
68
69         if (ppc_md.nvram_size == NULL)
70                 return -ENODEV;
71         size = ppc_md.nvram_size();
72
73         switch (origin) {
74         case 1:
75                 offset += file->f_pos;
76                 break;
77         case 2:
78                 offset += size;
79                 break;
80         }
81         if (offset < 0)
82                 return -EINVAL;
83         file->f_pos = offset;
84         return file->f_pos;
85 }
86
87
88 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
89                           size_t count, loff_t *ppos)
90 {
91         ssize_t ret;
92         char *tmp = NULL;
93         ssize_t size;
94
95         ret = -ENODEV;
96         if (!ppc_md.nvram_size)
97                 goto out;
98
99         ret = 0;
100         size = ppc_md.nvram_size();
101         if (*ppos >= size || size < 0)
102                 goto out;
103
104         count = min_t(size_t, count, size - *ppos);
105         count = min(count, PAGE_SIZE);
106
107         ret = -ENOMEM;
108         tmp = kmalloc(count, GFP_KERNEL);
109         if (!tmp)
110                 goto out;
111
112         ret = ppc_md.nvram_read(tmp, count, ppos);
113         if (ret <= 0)
114                 goto out;
115
116         if (copy_to_user(buf, tmp, ret))
117                 ret = -EFAULT;
118
119 out:
120         kfree(tmp);
121         return ret;
122
123 }
124
125 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
126                           size_t count, loff_t *ppos)
127 {
128         ssize_t ret;
129         char *tmp = NULL;
130         ssize_t size;
131
132         ret = -ENODEV;
133         if (!ppc_md.nvram_size)
134                 goto out;
135
136         ret = 0;
137         size = ppc_md.nvram_size();
138         if (*ppos >= size || size < 0)
139                 goto out;
140
141         count = min_t(size_t, count, size - *ppos);
142         count = min(count, PAGE_SIZE);
143
144         ret = -ENOMEM;
145         tmp = kmalloc(count, GFP_KERNEL);
146         if (!tmp)
147                 goto out;
148
149         ret = -EFAULT;
150         if (copy_from_user(tmp, buf, count))
151                 goto out;
152
153         ret = ppc_md.nvram_write(tmp, count, ppos);
154
155 out:
156         kfree(tmp);
157         return ret;
158
159 }
160
161 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
162                             unsigned long arg)
163 {
164         switch(cmd) {
165 #ifdef CONFIG_PPC_PMAC
166         case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
167                 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
168         case IOC_NVRAM_GET_OFFSET: {
169                 int part, offset;
170
171                 if (!machine_is(powermac))
172                         return -EINVAL;
173                 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
174                         return -EFAULT;
175                 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
176                         return -EINVAL;
177                 offset = pmac_get_partition(part);
178                 if (offset < 0)
179                         return offset;
180                 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
181                         return -EFAULT;
182                 return 0;
183         }
184 #endif /* CONFIG_PPC_PMAC */
185         default:
186                 return -EINVAL;
187         }
188 }
189
190 const struct file_operations nvram_fops = {
191         .owner          = THIS_MODULE,
192         .llseek         = dev_nvram_llseek,
193         .read           = dev_nvram_read,
194         .write          = dev_nvram_write,
195         .unlocked_ioctl = dev_nvram_ioctl,
196 };
197
198 static struct miscdevice nvram_dev = {
199         NVRAM_MINOR,
200         "nvram",
201         &nvram_fops
202 };
203
204
205 #ifdef DEBUG_NVRAM
206 static void __init nvram_print_partitions(char * label)
207 {
208         struct list_head * p;
209         struct nvram_partition * tmp_part;
210         
211         printk(KERN_WARNING "--------%s---------\n", label);
212         printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
213         list_for_each(p, &nvram_part->partition) {
214                 tmp_part = list_entry(p, struct nvram_partition, partition);
215                 printk(KERN_WARNING "%4d    \t%02x\t%02x\t%d\t%s\n",
216                        tmp_part->index, tmp_part->header.signature,
217                        tmp_part->header.checksum, tmp_part->header.length,
218                        tmp_part->header.name);
219         }
220 }
221 #endif
222
223
224 static int __init nvram_write_header(struct nvram_partition * part)
225 {
226         loff_t tmp_index;
227         int rc;
228         
229         tmp_index = part->index;
230         rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index); 
231
232         return rc;
233 }
234
235
236 static unsigned char __init nvram_checksum(struct nvram_header *p)
237 {
238         unsigned int c_sum, c_sum2;
239         unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
240         c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
241
242         /* The sum may have spilled into the 3rd byte.  Fold it back. */
243         c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
244         /* The sum cannot exceed 2 bytes.  Fold it into a checksum */
245         c_sum2 = (c_sum >> 8) + (c_sum << 8);
246         c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
247         return c_sum;
248 }
249
250 /**
251  * nvram_remove_partition - Remove one or more partitions in nvram
252  * @name: name of the partition to remove, or NULL for a
253  *        signature only match
254  * @sig: signature of the partition(s) to remove
255  */
256
257 static int __init nvram_remove_partition(const char *name, int sig)
258 {
259         struct nvram_partition *part, *prev, *tmp;
260         int rc;
261
262         list_for_each_entry(part, &nvram_part->partition, partition) {
263                 if (part->header.signature != sig)
264                         continue;
265                 if (name && strncmp(name, part->header.name, 12))
266                         continue;
267
268                 /* Make partition a free partition */
269                 part->header.signature = NVRAM_SIG_FREE;
270                 sprintf(part->header.name, "wwwwwwwwwwww");
271                 part->header.checksum = nvram_checksum(&part->header);
272                 rc = nvram_write_header(part);
273                 if (rc <= 0) {
274                         printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
275                         return rc;
276                 }
277         }
278
279         /* Merge contiguous ones */
280         prev = NULL;
281         list_for_each_entry_safe(part, tmp, &nvram_part->partition, partition) {
282                 if (part->header.signature != NVRAM_SIG_FREE) {
283                         prev = NULL;
284                         continue;
285                 }
286                 if (prev) {
287                         prev->header.length += part->header.length;
288                         prev->header.checksum = nvram_checksum(&part->header);
289                         rc = nvram_write_header(part);
290                         if (rc <= 0) {
291                                 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
292                                 return rc;
293                         }
294                         list_del(&part->partition);
295                         kfree(part);
296                 } else
297                         prev = part;
298         }
299         
300         return 0;
301 }
302
303 /**
304  * nvram_create_partition - Create a partition in nvram
305  * @name: name of the partition to create
306  * @sig: signature of the partition to create
307  * @req_size: size of data to allocate in bytes
308  * @min_size: minimum acceptable size (0 means req_size)
309  *
310  * Returns a negative error code or a positive nvram index
311  * of the beginning of the data area of the newly created
312  * partition. If you provided a min_size smaller than req_size
313  * you need to query for the actual size yourself after the
314  * call using nvram_partition_get_size().
315  */
316 static loff_t __init nvram_create_partition(const char *name, int sig,
317                                             int req_size, int min_size)
318 {
319         struct nvram_partition *part;
320         struct nvram_partition *new_part;
321         struct nvram_partition *free_part = NULL;
322         static char nv_init_vals[16];
323         loff_t tmp_index;
324         long size = 0;
325         int rc;
326
327         /* Convert sizes from bytes to blocks */
328         req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
329         min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
330
331         /* If no minimum size specified, make it the same as the
332          * requested size
333          */
334         if (min_size == 0)
335                 min_size = req_size;
336         if (min_size > req_size)
337                 return -EINVAL;
338
339         /* Now add one block to each for the header */
340         req_size += 1;
341         min_size += 1;
342
343         /* Find a free partition that will give us the maximum needed size 
344            If can't find one that will give us the minimum size needed */
345         list_for_each_entry(part, &nvram_part->partition, partition) {
346                 if (part->header.signature != NVRAM_SIG_FREE)
347                         continue;
348
349                 if (part->header.length >= req_size) {
350                         size = req_size;
351                         free_part = part;
352                         break;
353                 }
354                 if (part->header.length > size &&
355                     part->header.length >= min_size) {
356                         size = part->header.length;
357                         free_part = part;
358                 }
359         }
360         if (!size)
361                 return -ENOSPC;
362         
363         /* Create our OS partition */
364         new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
365         if (!new_part) {
366                 pr_err("nvram_create_os_partition: kmalloc failed\n");
367                 return -ENOMEM;
368         }
369
370         new_part->index = free_part->index;
371         new_part->header.signature = sig;
372         new_part->header.length = size;
373         strncpy(new_part->header.name, name, 12);
374         new_part->header.checksum = nvram_checksum(&new_part->header);
375
376         rc = nvram_write_header(new_part);
377         if (rc <= 0) {
378                 pr_err("nvram_create_os_partition: nvram_write_header "
379                        "failed (%d)\n", rc);
380                 return rc;
381         }
382         list_add_tail(&new_part->partition, &free_part->partition);
383
384         /* Adjust or remove the partition we stole the space from */
385         if (free_part->header.length > size) {
386                 free_part->index += size * NVRAM_BLOCK_LEN;
387                 free_part->header.length -= size;
388                 free_part->header.checksum = nvram_checksum(&free_part->header);
389                 rc = nvram_write_header(free_part);
390                 if (rc <= 0) {
391                         pr_err("nvram_create_os_partition: nvram_write_header "
392                                "failed (%d)\n", rc);
393                         return rc;
394                 }
395         } else {
396                 list_del(&free_part->partition);
397                 kfree(free_part);
398         } 
399
400         /* Clear the new partition */
401         for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
402              tmp_index <  ((size - 1) * NVRAM_BLOCK_LEN);
403              tmp_index += NVRAM_BLOCK_LEN) {
404                 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
405                 if (rc <= 0) {
406                         pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
407                         return rc;
408                 }
409         }
410         
411         return new_part->index + NVRAM_HEADER_LEN;
412 }
413
414 /**
415  * nvram_get_partition_size - Get the data size of an nvram partition
416  * @data_index: This is the offset of the start of the data of
417  *              the partition. The same value that is returned by
418  *              nvram_create_partition().
419  */
420 static int nvram_get_partition_size(loff_t data_index)
421 {
422         struct nvram_partition *part;
423         
424         list_for_each_entry(part, &nvram_part->partition, partition) {
425                 if (part->index + NVRAM_HEADER_LEN == data_index)
426                         return (part->header.length - 1) * NVRAM_BLOCK_LEN;
427         }
428         return -1;
429 }
430
431
432 /**
433  * nvram_find_partition - Find an nvram partition by signature and name
434  * @name: Name of the partition or NULL for any name
435  * @sig: Signature to test against
436  * @out_size: if non-NULL, returns the size of the data part of the partition
437  */
438 loff_t nvram_find_partition(const char *name, int sig, int *out_size)
439 {
440         struct nvram_partition *p;
441
442         list_for_each_entry(p, &nvram_part->partition, partition) {
443                 if (p->header.signature == sig &&
444                     (!name || !strncmp(p->header.name, name, 12))) {
445                         if (out_size)
446                                 *out_size = (p->header.length - 1) *
447                                         NVRAM_BLOCK_LEN;
448                         return p->index + NVRAM_HEADER_LEN;
449                 }
450         }
451         return 0;
452 }
453
454 /* nvram_setup_partition
455  *
456  * This will setup the partition we need for buffering the
457  * error logs and cleanup partitions if needed.
458  *
459  * The general strategy is the following:
460  * 1.) If there is ppc64,linux partition large enough then use it.
461  * 2.) If there is not a ppc64,linux partition large enough, search
462  * for a free partition that is large enough.
463  * 3.) If there is not a free partition large enough remove 
464  * _all_ OS partitions and consolidate the space.
465  * 4.) Will first try getting a chunk that will satisfy the maximum
466  * error log size (NVRAM_MAX_REQ).
467  * 5.) If the max chunk cannot be allocated then try finding a chunk
468  * that will satisfy the minum needed (NVRAM_MIN_REQ).
469  */
470 static int __init nvram_setup_partition(void)
471 {
472         loff_t p;
473         int size;
474
475         /* For now, we don't do any of this on pmac, until I
476          * have figured out if it's worth killing some unused stuffs
477          * in our nvram, as Apple defined partitions use pretty much
478          * all of the space
479          */
480         if (machine_is(powermac))
481                 return -ENOSPC;
482
483         p = nvram_find_partition("ppc64,linux", NVRAM_SIG_OS, &size);
484
485         /* Found one but too small, remove it */
486         if (p && size < NVRAM_MIN_REQ) {
487                 pr_info("nvram: Found too small ppc64,linux partition"
488                         ",removing it...");
489                 nvram_remove_partition("ppc64,linux", NVRAM_SIG_OS);
490                 p = 0;
491         }
492
493         /* Create one if we didn't find */
494         if (!p) {
495                 p = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
496                                            NVRAM_MAX_REQ, NVRAM_MIN_REQ);
497                 /* No room for it, try to get rid of any OS partition
498                  * and try again
499                  */
500                 if (p == -ENOSPC) {
501                         pr_info("nvram: No room to create ppc64,linux"
502                                 " partition, deleting all OS partitions...");
503                         nvram_remove_partition(NULL, NVRAM_SIG_OS);
504                         p = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
505                                                    NVRAM_MAX_REQ, NVRAM_MIN_REQ);
506                 }
507         }
508
509         if (p <= 0) {
510                 pr_err("nvram: Failed to find or create ppc64,linux"
511                        " partition, err %d\n", (int)p);
512                 return 0;
513         }
514
515         nvram_error_log_index = p;
516         nvram_error_log_size = nvram_get_partition_size(p) -
517                 sizeof(struct err_log_info);
518         
519         return 0;
520 }
521
522 static int __init nvram_scan_partitions(void)
523 {
524         loff_t cur_index = 0;
525         struct nvram_header phead;
526         struct nvram_partition * tmp_part;
527         unsigned char c_sum;
528         char * header;
529         int total_size;
530         int err;
531
532         if (ppc_md.nvram_size == NULL)
533                 return -ENODEV;
534         total_size = ppc_md.nvram_size();
535         
536         header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
537         if (!header) {
538                 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
539                 return -ENOMEM;
540         }
541
542         while (cur_index < total_size) {
543
544                 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
545                 if (err != NVRAM_HEADER_LEN) {
546                         printk(KERN_ERR "nvram_scan_partitions: Error parsing "
547                                "nvram partitions\n");
548                         goto out;
549                 }
550
551                 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
552
553                 memcpy(&phead, header, NVRAM_HEADER_LEN);
554
555                 err = 0;
556                 c_sum = nvram_checksum(&phead);
557                 if (c_sum != phead.checksum) {
558                         printk(KERN_WARNING "WARNING: nvram partition checksum"
559                                " was %02x, should be %02x!\n",
560                                phead.checksum, c_sum);
561                         printk(KERN_WARNING "Terminating nvram partition scan\n");
562                         goto out;
563                 }
564                 if (!phead.length) {
565                         printk(KERN_WARNING "WARNING: nvram corruption "
566                                "detected: 0-length partition\n");
567                         goto out;
568                 }
569                 tmp_part = (struct nvram_partition *)
570                         kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
571                 err = -ENOMEM;
572                 if (!tmp_part) {
573                         printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
574                         goto out;
575                 }
576                 
577                 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
578                 tmp_part->index = cur_index;
579                 list_add_tail(&tmp_part->partition, &nvram_part->partition);
580                 
581                 cur_index += phead.length * NVRAM_BLOCK_LEN;
582         }
583         err = 0;
584
585  out:
586         kfree(header);
587         return err;
588 }
589
590 static int __init nvram_init(void)
591 {
592         int error;
593         int rc;
594         
595         BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
596
597         if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
598                 return  -ENODEV;
599
600         rc = misc_register(&nvram_dev);
601         if (rc != 0) {
602                 printk(KERN_ERR "nvram_init: failed to register device\n");
603                 return rc;
604         }
605         
606         /* initialize our anchor for the nvram partition list */
607         nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
608         if (!nvram_part) {
609                 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
610                 return -ENOMEM;
611         }
612         INIT_LIST_HEAD(&nvram_part->partition);
613   
614         /* Get all the NVRAM partitions */
615         error = nvram_scan_partitions();
616         if (error) {
617                 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
618                 return error;
619         }
620                 
621         if(nvram_setup_partition()) 
622                 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
623                        " for nvram buffered error logging.\n");
624   
625 #ifdef DEBUG_NVRAM
626         nvram_print_partitions("NVRAM Partitions");
627 #endif
628
629         return rc;
630 }
631
632 void __exit nvram_cleanup(void)
633 {
634         misc_deregister( &nvram_dev );
635 }
636
637
638 #ifdef CONFIG_PPC_PSERIES
639
640 /* nvram_write_error_log
641  *
642  * We need to buffer the error logs into nvram to ensure that we have
643  * the failure information to decode.  If we have a severe error there
644  * is no way to guarantee that the OS or the machine is in a state to
645  * get back to user land and write the error to disk.  For example if
646  * the SCSI device driver causes a Machine Check by writing to a bad
647  * IO address, there is no way of guaranteeing that the device driver
648  * is in any state that is would also be able to write the error data
649  * captured to disk, thus we buffer it in NVRAM for analysis on the
650  * next boot.
651  *
652  * In NVRAM the partition containing the error log buffer will looks like:
653  * Header (in bytes):
654  * +-----------+----------+--------+------------+------------------+
655  * | signature | checksum | length | name       | data             |
656  * |0          |1         |2      3|4         15|16        length-1|
657  * +-----------+----------+--------+------------+------------------+
658  *
659  * The 'data' section would look like (in bytes):
660  * +--------------+------------+-----------------------------------+
661  * | event_logged | sequence # | error log                         |
662  * |0            3|4          7|8            nvram_error_log_size-1|
663  * +--------------+------------+-----------------------------------+
664  *
665  * event_logged: 0 if event has not been logged to syslog, 1 if it has
666  * sequence #: The unique sequence # for each event. (until it wraps)
667  * error log: The error log from event_scan
668  */
669 int nvram_write_error_log(char * buff, int length,
670                           unsigned int err_type, unsigned int error_log_cnt)
671 {
672         int rc;
673         loff_t tmp_index;
674         struct err_log_info info;
675         
676         if (nvram_error_log_index == -1) {
677                 return -ESPIPE;
678         }
679
680         if (length > nvram_error_log_size) {
681                 length = nvram_error_log_size;
682         }
683
684         info.error_type = err_type;
685         info.seq_num = error_log_cnt;
686
687         tmp_index = nvram_error_log_index;
688
689         rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
690         if (rc <= 0) {
691                 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
692                 return rc;
693         }
694
695         rc = ppc_md.nvram_write(buff, length, &tmp_index);
696         if (rc <= 0) {
697                 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
698                 return rc;
699         }
700         
701         return 0;
702 }
703
704 /* nvram_read_error_log
705  *
706  * Reads nvram for error log for at most 'length'
707  */
708 int nvram_read_error_log(char * buff, int length,
709                          unsigned int * err_type, unsigned int * error_log_cnt)
710 {
711         int rc;
712         loff_t tmp_index;
713         struct err_log_info info;
714         
715         if (nvram_error_log_index == -1)
716                 return -1;
717
718         if (length > nvram_error_log_size)
719                 length = nvram_error_log_size;
720
721         tmp_index = nvram_error_log_index;
722
723         rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
724         if (rc <= 0) {
725                 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
726                 return rc;
727         }
728
729         rc = ppc_md.nvram_read(buff, length, &tmp_index);
730         if (rc <= 0) {
731                 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
732                 return rc;
733         }
734
735         *error_log_cnt = info.seq_num;
736         *err_type = info.error_type;
737
738         return 0;
739 }
740
741 /* This doesn't actually zero anything, but it sets the event_logged
742  * word to tell that this event is safely in syslog.
743  */
744 int nvram_clear_error_log(void)
745 {
746         loff_t tmp_index;
747         int clear_word = ERR_FLAG_ALREADY_LOGGED;
748         int rc;
749
750         if (nvram_error_log_index == -1)
751                 return -1;
752
753         tmp_index = nvram_error_log_index;
754         
755         rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
756         if (rc <= 0) {
757                 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
758                 return rc;
759         }
760
761         return 0;
762 }
763
764 #endif /* CONFIG_PPC_PSERIES */
765
766 module_init(nvram_init);
767 module_exit(nvram_cleanup);
768 MODULE_LICENSE("GPL");