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