[PATCH] briq_panel: read() and write() get __user pointers, damnit
[linux-2.6/verdex.git] / arch / powerpc / kernel / nvram_64.c
blob6960f090991ed2fcabc318bd1743a04c6d9550d4
1 /*
2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /dev/nvram driver for PPC64
11 * This perhaps should live in drivers/char
13 * TODO: Split the /dev/nvram part (that one can use
14 * drivers/char/generic_nvram.c) from the arch & partition
15 * parsing code.
18 #include <linux/module.h>
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>
35 #undef DEBUG_NVRAM
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);
42 static struct nvram_partition * nvram_part;
43 static long nvram_error_log_index = -1;
44 static long nvram_error_log_size = 0;
46 int no_logging = 1; /* Until we initialize everything,
47 * make sure we don't try logging
48 * anything */
50 extern volatile int error_log_cnt;
52 struct err_log_info {
53 int error_type;
54 unsigned int seq_num;
57 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
59 int size;
61 if (ppc_md.nvram_size == NULL)
62 return -ENODEV;
63 size = ppc_md.nvram_size();
65 switch (origin) {
66 case 1:
67 offset += file->f_pos;
68 break;
69 case 2:
70 offset += size;
71 break;
73 if (offset < 0)
74 return -EINVAL;
75 file->f_pos = offset;
76 return file->f_pos;
80 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
81 size_t count, loff_t *ppos)
83 ssize_t ret;
84 char *tmp = NULL;
85 ssize_t size;
87 ret = -ENODEV;
88 if (!ppc_md.nvram_size)
89 goto out;
91 ret = 0;
92 size = ppc_md.nvram_size();
93 if (*ppos >= size || size < 0)
94 goto out;
96 count = min_t(size_t, count, size - *ppos);
97 count = min(count, PAGE_SIZE);
99 ret = -ENOMEM;
100 tmp = kmalloc(count, GFP_KERNEL);
101 if (!tmp)
102 goto out;
104 ret = ppc_md.nvram_read(tmp, count, ppos);
105 if (ret <= 0)
106 goto out;
108 if (copy_to_user(buf, tmp, ret))
109 ret = -EFAULT;
111 out:
112 kfree(tmp);
113 return ret;
117 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
118 size_t count, loff_t *ppos)
120 ssize_t ret;
121 char *tmp = NULL;
122 ssize_t size;
124 ret = -ENODEV;
125 if (!ppc_md.nvram_size)
126 goto out;
128 ret = 0;
129 size = ppc_md.nvram_size();
130 if (*ppos >= size || size < 0)
131 goto out;
133 count = min_t(size_t, count, size - *ppos);
134 count = min(count, PAGE_SIZE);
136 ret = -ENOMEM;
137 tmp = kmalloc(count, GFP_KERNEL);
138 if (!tmp)
139 goto out;
141 ret = -EFAULT;
142 if (copy_from_user(tmp, buf, count))
143 goto out;
145 ret = ppc_md.nvram_write(tmp, count, ppos);
147 out:
148 kfree(tmp);
149 return ret;
153 static int dev_nvram_ioctl(struct inode *inode, struct file *file,
154 unsigned int cmd, unsigned long arg)
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;
163 if (!machine_is(powermac))
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;
176 #endif /* CONFIG_PPC_PMAC */
177 default:
178 return -EINVAL;
182 struct 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,
190 static struct miscdevice nvram_dev = {
191 NVRAM_MINOR,
192 "nvram",
193 &nvram_fops
197 #ifdef DEBUG_NVRAM
198 static void nvram_print_partitions(char * label)
200 struct list_head * p;
201 struct nvram_partition * tmp_part;
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 "%4d \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);
213 #endif
216 static int nvram_write_header(struct nvram_partition * part)
218 loff_t tmp_index;
219 int rc;
221 tmp_index = part->index;
222 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
224 return rc;
228 static unsigned char nvram_checksum(struct nvram_header *p)
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];
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;
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
248 struct nvram_partition *nvram_find_partition(int sig, const char *name)
250 struct nvram_partition * part;
251 struct list_head * p;
253 list_for_each(p, &nvram_part->partition) {
254 part = list_entry(p, struct nvram_partition, partition);
256 if (sig && part->header.signature != sig)
257 continue;
258 if (name && 0 != strncmp(name, part->header.name, 12))
259 continue;
260 return part;
262 return NULL;
264 EXPORT_SYMBOL(nvram_find_partition);
267 static int nvram_remove_os_partition(void)
269 struct list_head *i;
270 struct list_head *j;
271 struct nvram_partition * part;
272 struct nvram_partition * cur_part;
273 int rc;
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;
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);
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;
292 part->header.length += cur_part->header.length;
293 part->header.checksum = nvram_checksum(&part->header);
294 part->index = cur_part->index;
296 list_del(&cur_part->partition);
297 kfree(cur_part);
298 j = &part->partition; /* fixup our loop */
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;
308 part->header.length += cur_part->header.length;
309 part->header.checksum = nvram_checksum(&part->header);
311 list_del(&cur_part->partition);
312 kfree(cur_part);
313 j = &part->partition; /* fixup our loop */
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;
324 return 0;
327 /* nvram_create_os_partition
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.
333 static int nvram_create_os_partition(void)
335 struct nvram_partition *part;
336 struct nvram_partition *new_part;
337 struct nvram_partition *free_part = NULL;
338 int seq_init[2] = { 0, 0 };
339 loff_t tmp_index;
340 long size = 0;
341 int rc;
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 */
345 list_for_each_entry(part, &nvram_part->partition, partition) {
346 if (part->header.signature != NVRAM_SIG_FREE)
347 continue;
349 if (part->header.length >= NVRAM_MAX_REQ) {
350 size = NVRAM_MAX_REQ;
351 free_part = part;
352 break;
354 if (!size && part->header.length >= NVRAM_MIN_REQ) {
355 size = NVRAM_MIN_REQ;
356 free_part = part;
359 if (!size)
360 return -ENOSPC;
362 /* Create our OS partition */
363 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
364 if (!new_part) {
365 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
366 return -ENOMEM;
369 new_part->index = free_part->index;
370 new_part->header.signature = NVRAM_SIG_OS;
371 new_part->header.length = size;
372 strcpy(new_part->header.name, "ppc64,linux");
373 new_part->header.checksum = nvram_checksum(&new_part->header);
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;
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) {
387 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
388 "failed (%d)\n", rc);
389 return rc;
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);
396 list_add_tail(&new_part->partition, &free_part->partition);
398 if (free_part->header.length <= size) {
399 list_del(&free_part->partition);
400 kfree(free_part);
401 return 0;
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);
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;
416 return 0;
420 /* nvram_setup_partition
422 * This will setup the partition we need for buffering the
423 * error logs and cleanup partitions if needed.
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).
436 static int nvram_setup_partition(void)
438 struct list_head * p;
439 struct nvram_partition * part;
440 int rc;
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
447 if (machine_is(powermac))
448 return -ENOSPC;
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;
458 if (strcmp(part->header.name, "ppc64,linux"))
459 continue;
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;
470 /* try creating a partition with the free space we have */
471 rc = nvram_create_os_partition();
472 if (!rc) {
473 return 0;
476 /* need to free up some space */
477 rc = nvram_remove_os_partition();
478 if (rc) {
479 return rc;
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;
490 return 0;
494 static int nvram_scan_partitions(void)
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;
504 if (ppc_md.nvram_size == NULL)
505 return -ENODEV;
506 total_size = ppc_md.nvram_size();
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;
514 while (cur_index < total_size) {
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;
523 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
525 memcpy(&phead, header, NVRAM_HEADER_LEN);
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;
536 if (!phead.length) {
537 printk(KERN_WARNING "WARNING: nvram corruption "
538 "detected: 0-length partition\n");
539 goto out;
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;
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);
553 cur_index += phead.length * NVRAM_BLOCK_LEN;
555 err = 0;
557 out:
558 kfree(header);
559 return err;
562 static int __init nvram_init(void)
564 int error;
565 int rc;
567 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
568 return -ENODEV;
570 rc = misc_register(&nvram_dev);
571 if (rc != 0) {
572 printk(KERN_ERR "nvram_init: failed to register device\n");
573 return rc;
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;
582 INIT_LIST_HEAD(&nvram_part->partition);
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;
591 if(nvram_setup_partition())
592 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
593 " for nvram buffered error logging.\n");
595 #ifdef DEBUG_NVRAM
596 nvram_print_partitions("NVRAM Partitions");
597 #endif
599 return rc;
602 void __exit nvram_cleanup(void)
604 misc_deregister( &nvram_dev );
608 #ifdef CONFIG_PPC_PSERIES
610 /* nvram_write_error_log
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.
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 * +-----------+----------+--------+------------+------------------+
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 * +--------------+------------+-----------------------------------+
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
639 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
641 int rc;
642 loff_t tmp_index;
643 struct err_log_info info;
645 if (no_logging) {
646 return -EPERM;
649 if (nvram_error_log_index == -1) {
650 return -ESPIPE;
653 if (length > nvram_error_log_size) {
654 length = nvram_error_log_size;
657 info.error_type = err_type;
658 info.seq_num = error_log_cnt;
660 tmp_index = nvram_error_log_index;
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;
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;
674 return 0;
677 /* nvram_read_error_log
679 * Reads nvram for error log for at most 'length'
681 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
683 int rc;
684 loff_t tmp_index;
685 struct err_log_info info;
687 if (nvram_error_log_index == -1)
688 return -1;
690 if (length > nvram_error_log_size)
691 length = nvram_error_log_size;
693 tmp_index = nvram_error_log_index;
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;
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;
707 error_log_cnt = info.seq_num;
708 *err_type = info.error_type;
710 return 0;
713 /* This doesn't actually zero anything, but it sets the event_logged
714 * word to tell that this event is safely in syslog.
716 int nvram_clear_error_log(void)
718 loff_t tmp_index;
719 int clear_word = ERR_FLAG_ALREADY_LOGGED;
720 int rc;
722 tmp_index = nvram_error_log_index;
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;
730 return 0;
733 #endif /* CONFIG_PPC_PSERIES */
735 module_init(nvram_init);
736 module_exit(nvram_cleanup);
737 MODULE_LICENSE("GPL");