[NETLINK]: w1_int.c: fix default netlink group
[linux-2.6/verdex.git] / arch / ppc64 / kernel / rtas_flash.c
blob923e2e201a70669620f1834adf3b9aec321380b0
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 * /proc/ppc64/rtas/firmware_flash interface
11 * This file implements a firmware_flash interface to pump a firmware
12 * image into the kernel. At reboot time rtas_restart() will see the
13 * firmware image and flash it as it reboots (see rtas.c).
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/proc_fs.h>
19 #include <asm/delay.h>
20 #include <asm/uaccess.h>
21 #include <asm/rtas.h>
23 #define MODULE_VERS "1.0"
24 #define MODULE_NAME "rtas_flash"
26 #define FIRMWARE_FLASH_NAME "firmware_flash"
27 #define FIRMWARE_UPDATE_NAME "firmware_update"
28 #define MANAGE_FLASH_NAME "manage_flash"
29 #define VALIDATE_FLASH_NAME "validate_flash"
31 /* General RTAS Status Codes */
32 #define RTAS_RC_SUCCESS 0
33 #define RTAS_RC_HW_ERR -1
34 #define RTAS_RC_BUSY -2
36 /* Flash image status values */
37 #define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */
38 #define FLASH_NO_OP -1099 /* No operation initiated by user */
39 #define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */
40 #define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */
41 #define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */
42 #define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */
44 /* Manage image status values */
45 #define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */
46 #define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */
47 #define MANAGE_NO_OP -1099 /* No operation initiated by user */
48 #define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */
49 #define MANAGE_HW_ERR -1 /* RTAS Hardware Error */
51 /* Validate image status values */
52 #define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */
53 #define VALIDATE_NO_OP -1099 /* No operation initiated by the user */
54 #define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
55 #define VALIDATE_READY -1001 /* Firmware image ready for validation */
56 #define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */
57 #define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */
58 #define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */
59 #define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */
60 #define VALIDATE_INVALID_IMG 2 /* Validate Return Status */
61 #define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */
62 #define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */
63 #define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */
64 #define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */
66 /* ibm,manage-flash-image operation tokens */
67 #define RTAS_REJECT_TMP_IMG 0
68 #define RTAS_COMMIT_TMP_IMG 1
70 /* Array sizes */
71 #define VALIDATE_BUF_SIZE 4096
72 #define RTAS_MSG_MAXLEN 64
74 /* Local copy of the flash block list.
75 * We only allow one open of the flash proc file and create this
76 * list as we go. This list will be put in the kernel's
77 * rtas_firmware_flash_list global var once it is fully read.
79 * For convenience as we build the list we use virtual addrs,
80 * we do not fill in the version number, and the length field
81 * is treated as the number of entries currently in the block
82 * (i.e. not a byte count). This is all fixed on release.
85 /* Status int must be first member of struct */
86 struct rtas_update_flash_t
88 int status; /* Flash update status */
89 struct flash_block_list *flist; /* Local copy of flash block list */
92 /* Status int must be first member of struct */
93 struct rtas_manage_flash_t
95 int status; /* Returned status */
96 unsigned int op; /* Reject or commit image */
99 /* Status int must be first member of struct */
100 struct rtas_validate_flash_t
102 int status; /* Returned status */
103 char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
104 unsigned int buf_size; /* Size of image buf */
105 unsigned int update_results; /* Update results token */
108 static DEFINE_SPINLOCK(flash_file_open_lock);
109 static struct proc_dir_entry *firmware_flash_pde;
110 static struct proc_dir_entry *firmware_update_pde;
111 static struct proc_dir_entry *validate_pde;
112 static struct proc_dir_entry *manage_pde;
114 /* Do simple sanity checks on the flash image. */
115 static int flash_list_valid(struct flash_block_list *flist)
117 struct flash_block_list *f;
118 int i;
119 unsigned long block_size, image_size;
121 /* Paranoid self test here. We also collect the image size. */
122 image_size = 0;
123 for (f = flist; f; f = f->next) {
124 for (i = 0; i < f->num_blocks; i++) {
125 if (f->blocks[i].data == NULL) {
126 return FLASH_IMG_NULL_DATA;
128 block_size = f->blocks[i].length;
129 if (block_size <= 0 || block_size > PAGE_SIZE) {
130 return FLASH_IMG_BAD_LEN;
132 image_size += block_size;
136 if (image_size < (256 << 10)) {
137 if (image_size < 2)
138 return FLASH_NO_OP;
141 printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
143 return FLASH_IMG_READY;
146 static void free_flash_list(struct flash_block_list *f)
148 struct flash_block_list *next;
149 int i;
151 while (f) {
152 for (i = 0; i < f->num_blocks; i++)
153 free_page((unsigned long)(f->blocks[i].data));
154 next = f->next;
155 free_page((unsigned long)f);
156 f = next;
160 static int rtas_flash_release(struct inode *inode, struct file *file)
162 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
163 struct rtas_update_flash_t *uf;
165 uf = (struct rtas_update_flash_t *) dp->data;
166 if (uf->flist) {
167 /* File was opened in write mode for a new flash attempt */
168 /* Clear saved list */
169 if (rtas_firmware_flash_list.next) {
170 free_flash_list(rtas_firmware_flash_list.next);
171 rtas_firmware_flash_list.next = NULL;
174 if (uf->status != FLASH_AUTH)
175 uf->status = flash_list_valid(uf->flist);
177 if (uf->status == FLASH_IMG_READY)
178 rtas_firmware_flash_list.next = uf->flist;
179 else
180 free_flash_list(uf->flist);
182 uf->flist = NULL;
185 atomic_dec(&dp->count);
186 return 0;
189 static void get_flash_status_msg(int status, char *buf)
191 char *msg;
193 switch (status) {
194 case FLASH_AUTH:
195 msg = "error: this partition does not have service authority\n";
196 break;
197 case FLASH_NO_OP:
198 msg = "info: no firmware image for flash\n";
199 break;
200 case FLASH_IMG_SHORT:
201 msg = "error: flash image short\n";
202 break;
203 case FLASH_IMG_BAD_LEN:
204 msg = "error: internal error bad length\n";
205 break;
206 case FLASH_IMG_NULL_DATA:
207 msg = "error: internal error null data\n";
208 break;
209 case FLASH_IMG_READY:
210 msg = "ready: firmware image ready for flash on reboot\n";
211 break;
212 default:
213 sprintf(buf, "error: unexpected status value %d\n", status);
214 return;
217 strcpy(buf, msg);
220 /* Reading the proc file will show status (not the firmware contents) */
221 static ssize_t rtas_flash_read(struct file *file, char __user *buf,
222 size_t count, loff_t *ppos)
224 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
225 struct rtas_update_flash_t *uf;
226 char msg[RTAS_MSG_MAXLEN];
227 int msglen;
229 uf = (struct rtas_update_flash_t *) dp->data;
231 if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
232 get_flash_status_msg(uf->status, msg);
233 } else { /* FIRMWARE_UPDATE_NAME */
234 sprintf(msg, "%d\n", uf->status);
236 msglen = strlen(msg);
237 if (msglen > count)
238 msglen = count;
240 if (ppos && *ppos != 0)
241 return 0; /* be cheap */
243 if (!access_ok(VERIFY_WRITE, buf, msglen))
244 return -EINVAL;
246 if (copy_to_user(buf, msg, msglen))
247 return -EFAULT;
249 if (ppos)
250 *ppos = msglen;
251 return msglen;
254 /* We could be much more efficient here. But to keep this function
255 * simple we allocate a page to the block list no matter how small the
256 * count is. If the system is low on memory it will be just as well
257 * that we fail....
259 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
260 size_t count, loff_t *off)
262 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
263 struct rtas_update_flash_t *uf;
264 char *p;
265 int next_free;
266 struct flash_block_list *fl;
268 uf = (struct rtas_update_flash_t *) dp->data;
270 if (uf->status == FLASH_AUTH || count == 0)
271 return count; /* discard data */
273 /* In the case that the image is not ready for flashing, the memory
274 * allocated for the block list will be freed upon the release of the
275 * proc file
277 if (uf->flist == NULL) {
278 uf->flist = (struct flash_block_list *) get_zeroed_page(GFP_KERNEL);
279 if (!uf->flist)
280 return -ENOMEM;
283 fl = uf->flist;
284 while (fl->next)
285 fl = fl->next; /* seek to last block_list for append */
286 next_free = fl->num_blocks;
287 if (next_free == FLASH_BLOCKS_PER_NODE) {
288 /* Need to allocate another block_list */
289 fl->next = (struct flash_block_list *)get_zeroed_page(GFP_KERNEL);
290 if (!fl->next)
291 return -ENOMEM;
292 fl = fl->next;
293 next_free = 0;
296 if (count > PAGE_SIZE)
297 count = PAGE_SIZE;
298 p = (char *)get_zeroed_page(GFP_KERNEL);
299 if (!p)
300 return -ENOMEM;
302 if(copy_from_user(p, buffer, count)) {
303 free_page((unsigned long)p);
304 return -EFAULT;
306 fl->blocks[next_free].data = p;
307 fl->blocks[next_free].length = count;
308 fl->num_blocks++;
310 return count;
313 static int rtas_excl_open(struct inode *inode, struct file *file)
315 struct proc_dir_entry *dp = PDE(inode);
317 /* Enforce exclusive open with use count of PDE */
318 spin_lock(&flash_file_open_lock);
319 if (atomic_read(&dp->count) > 1) {
320 spin_unlock(&flash_file_open_lock);
321 return -EBUSY;
324 atomic_inc(&dp->count);
325 spin_unlock(&flash_file_open_lock);
327 return 0;
330 static int rtas_excl_release(struct inode *inode, struct file *file)
332 struct proc_dir_entry *dp = PDE(inode);
334 atomic_dec(&dp->count);
336 return 0;
339 static void manage_flash(struct rtas_manage_flash_t *args_buf)
341 unsigned int wait_time;
342 s32 rc;
344 while (1) {
345 rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
346 1, NULL, args_buf->op);
347 if (rc == RTAS_RC_BUSY)
348 udelay(1);
349 else if (rtas_is_extended_busy(rc)) {
350 wait_time = rtas_extended_busy_delay_time(rc);
351 udelay(wait_time * 1000);
352 } else
353 break;
356 args_buf->status = rc;
359 static ssize_t manage_flash_read(struct file *file, char __user *buf,
360 size_t count, loff_t *ppos)
362 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
363 struct rtas_manage_flash_t *args_buf;
364 char msg[RTAS_MSG_MAXLEN];
365 int msglen;
367 args_buf = (struct rtas_manage_flash_t *) dp->data;
368 if (args_buf == NULL)
369 return 0;
371 msglen = sprintf(msg, "%d\n", args_buf->status);
372 if (msglen > count)
373 msglen = count;
375 if (ppos && *ppos != 0)
376 return 0; /* be cheap */
378 if (!access_ok(VERIFY_WRITE, buf, msglen))
379 return -EINVAL;
381 if (copy_to_user(buf, msg, msglen))
382 return -EFAULT;
384 if (ppos)
385 *ppos = msglen;
386 return msglen;
389 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
390 size_t count, loff_t *off)
392 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
393 struct rtas_manage_flash_t *args_buf;
394 const char reject_str[] = "0";
395 const char commit_str[] = "1";
396 char stkbuf[10];
397 int op;
399 args_buf = (struct rtas_manage_flash_t *) dp->data;
400 if ((args_buf->status == MANAGE_AUTH) || (count == 0))
401 return count;
403 op = -1;
404 if (buf) {
405 if (count > 9) count = 9;
406 if (copy_from_user (stkbuf, buf, count)) {
407 return -EFAULT;
409 if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
410 op = RTAS_REJECT_TMP_IMG;
411 else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
412 op = RTAS_COMMIT_TMP_IMG;
415 if (op == -1) /* buf is empty, or contains invalid string */
416 return -EINVAL;
418 args_buf->op = op;
419 manage_flash(args_buf);
421 return count;
424 static void validate_flash(struct rtas_validate_flash_t *args_buf)
426 int token = rtas_token("ibm,validate-flash-image");
427 unsigned int wait_time;
428 int update_results;
429 s32 rc;
431 rc = 0;
432 while(1) {
433 spin_lock(&rtas_data_buf_lock);
434 memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
435 rc = rtas_call(token, 2, 2, &update_results,
436 (u32) __pa(rtas_data_buf), args_buf->buf_size);
437 memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
438 spin_unlock(&rtas_data_buf_lock);
440 if (rc == RTAS_RC_BUSY)
441 udelay(1);
442 else if (rtas_is_extended_busy(rc)) {
443 wait_time = rtas_extended_busy_delay_time(rc);
444 udelay(wait_time * 1000);
445 } else
446 break;
449 args_buf->status = rc;
450 args_buf->update_results = update_results;
453 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
454 char *msg)
456 int n;
458 if (args_buf->status >= VALIDATE_TMP_UPDATE) {
459 n = sprintf(msg, "%d\n", args_buf->update_results);
460 if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
461 (args_buf->update_results == VALIDATE_TMP_UPDATE))
462 n += sprintf(msg + n, "%s\n", args_buf->buf);
463 } else {
464 n = sprintf(msg, "%d\n", args_buf->status);
466 return n;
469 static ssize_t validate_flash_read(struct file *file, char __user *buf,
470 size_t count, loff_t *ppos)
472 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
473 struct rtas_validate_flash_t *args_buf;
474 char msg[RTAS_MSG_MAXLEN];
475 int msglen;
477 args_buf = (struct rtas_validate_flash_t *) dp->data;
479 if (ppos && *ppos != 0)
480 return 0; /* be cheap */
482 msglen = get_validate_flash_msg(args_buf, msg);
483 if (msglen > count)
484 msglen = count;
486 if (!access_ok(VERIFY_WRITE, buf, msglen))
487 return -EINVAL;
489 if (copy_to_user(buf, msg, msglen))
490 return -EFAULT;
492 if (ppos)
493 *ppos = msglen;
494 return msglen;
497 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
498 size_t count, loff_t *off)
500 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
501 struct rtas_validate_flash_t *args_buf;
502 int rc;
504 args_buf = (struct rtas_validate_flash_t *) dp->data;
506 if (dp->data == NULL) {
507 dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
508 GFP_KERNEL);
509 if (dp->data == NULL)
510 return -ENOMEM;
513 /* We are only interested in the first 4K of the
514 * candidate image */
515 if ((*off >= VALIDATE_BUF_SIZE) ||
516 (args_buf->status == VALIDATE_AUTH)) {
517 *off += count;
518 return count;
521 if (*off + count >= VALIDATE_BUF_SIZE) {
522 count = VALIDATE_BUF_SIZE - *off;
523 args_buf->status = VALIDATE_READY;
524 } else {
525 args_buf->status = VALIDATE_INCOMPLETE;
528 if (!access_ok(VERIFY_READ, buf, count)) {
529 rc = -EFAULT;
530 goto done;
532 if (copy_from_user(args_buf->buf + *off, buf, count)) {
533 rc = -EFAULT;
534 goto done;
537 *off += count;
538 rc = count;
539 done:
540 if (rc < 0) {
541 kfree(dp->data);
542 dp->data = NULL;
544 return rc;
547 static int validate_flash_release(struct inode *inode, struct file *file)
549 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
550 struct rtas_validate_flash_t *args_buf;
552 args_buf = (struct rtas_validate_flash_t *) dp->data;
554 if (args_buf->status == VALIDATE_READY) {
555 args_buf->buf_size = VALIDATE_BUF_SIZE;
556 validate_flash(args_buf);
559 /* The matching atomic_inc was in rtas_excl_open() */
560 atomic_dec(&dp->count);
562 return 0;
565 static void remove_flash_pde(struct proc_dir_entry *dp)
567 if (dp) {
568 if (dp->data != NULL)
569 kfree(dp->data);
570 dp->owner = NULL;
571 remove_proc_entry(dp->name, dp->parent);
575 static int initialize_flash_pde_data(const char *rtas_call_name,
576 size_t buf_size,
577 struct proc_dir_entry *dp)
579 int *status;
580 int token;
582 dp->data = kmalloc(buf_size, GFP_KERNEL);
583 if (dp->data == NULL) {
584 remove_flash_pde(dp);
585 return -ENOMEM;
588 memset(dp->data, 0, buf_size);
591 * This code assumes that the status int is the first member of the
592 * struct
594 status = (int *) dp->data;
595 token = rtas_token(rtas_call_name);
596 if (token == RTAS_UNKNOWN_SERVICE)
597 *status = FLASH_AUTH;
598 else
599 *status = FLASH_NO_OP;
601 return 0;
604 static struct proc_dir_entry *create_flash_pde(const char *filename,
605 struct file_operations *fops)
607 struct proc_dir_entry *ent = NULL;
609 ent = create_proc_entry(filename, S_IRUSR | S_IWUSR, NULL);
610 if (ent != NULL) {
611 ent->nlink = 1;
612 ent->proc_fops = fops;
613 ent->owner = THIS_MODULE;
616 return ent;
619 static struct file_operations rtas_flash_operations = {
620 .read = rtas_flash_read,
621 .write = rtas_flash_write,
622 .open = rtas_excl_open,
623 .release = rtas_flash_release,
626 static struct file_operations manage_flash_operations = {
627 .read = manage_flash_read,
628 .write = manage_flash_write,
629 .open = rtas_excl_open,
630 .release = rtas_excl_release,
633 static struct file_operations validate_flash_operations = {
634 .read = validate_flash_read,
635 .write = validate_flash_write,
636 .open = rtas_excl_open,
637 .release = validate_flash_release,
640 int __init rtas_flash_init(void)
642 int rc;
644 if (rtas_token("ibm,update-flash-64-and-reboot") ==
645 RTAS_UNKNOWN_SERVICE) {
646 printk(KERN_ERR "rtas_flash: no firmware flash support\n");
647 return 1;
650 firmware_flash_pde = create_flash_pde("ppc64/rtas/"
651 FIRMWARE_FLASH_NAME,
652 &rtas_flash_operations);
653 if (firmware_flash_pde == NULL) {
654 rc = -ENOMEM;
655 goto cleanup;
658 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
659 sizeof(struct rtas_update_flash_t),
660 firmware_flash_pde);
661 if (rc != 0)
662 goto cleanup;
664 firmware_update_pde = create_flash_pde("ppc64/rtas/"
665 FIRMWARE_UPDATE_NAME,
666 &rtas_flash_operations);
667 if (firmware_update_pde == NULL) {
668 rc = -ENOMEM;
669 goto cleanup;
672 rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
673 sizeof(struct rtas_update_flash_t),
674 firmware_update_pde);
675 if (rc != 0)
676 goto cleanup;
678 validate_pde = create_flash_pde("ppc64/rtas/" VALIDATE_FLASH_NAME,
679 &validate_flash_operations);
680 if (validate_pde == NULL) {
681 rc = -ENOMEM;
682 goto cleanup;
685 rc = initialize_flash_pde_data("ibm,validate-flash-image",
686 sizeof(struct rtas_validate_flash_t),
687 validate_pde);
688 if (rc != 0)
689 goto cleanup;
691 manage_pde = create_flash_pde("ppc64/rtas/" MANAGE_FLASH_NAME,
692 &manage_flash_operations);
693 if (manage_pde == NULL) {
694 rc = -ENOMEM;
695 goto cleanup;
698 rc = initialize_flash_pde_data("ibm,manage-flash-image",
699 sizeof(struct rtas_manage_flash_t),
700 manage_pde);
701 if (rc != 0)
702 goto cleanup;
704 return 0;
706 cleanup:
707 remove_flash_pde(firmware_flash_pde);
708 remove_flash_pde(firmware_update_pde);
709 remove_flash_pde(validate_pde);
710 remove_flash_pde(manage_pde);
712 return rc;
715 void __exit rtas_flash_cleanup(void)
717 remove_flash_pde(firmware_flash_pde);
718 remove_flash_pde(firmware_update_pde);
719 remove_flash_pde(validate_pde);
720 remove_flash_pde(manage_pde);
723 module_init(rtas_flash_init);
724 module_exit(rtas_flash_cleanup);
725 MODULE_LICENSE("GPL");