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