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