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