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