xref: /openbmc/linux/arch/powerpc/kernel/rtas_flash.c (revision f51005d7)
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  * /proc/powerpc/rtas/firmware_flash interface
10  *
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).
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/proc_fs.h>
20 #include <linux/reboot.h>
21 #include <asm/delay.h>
22 #include <asm/uaccess.h>
23 #include <asm/rtas.h>
24 
25 #define MODULE_VERS "1.0"
26 #define MODULE_NAME "rtas_flash"
27 
28 #define FIRMWARE_FLASH_NAME "firmware_flash"
29 #define FIRMWARE_UPDATE_NAME "firmware_update"
30 #define MANAGE_FLASH_NAME "manage_flash"
31 #define VALIDATE_FLASH_NAME "validate_flash"
32 
33 /* General RTAS Status Codes */
34 #define RTAS_RC_SUCCESS  0
35 #define RTAS_RC_HW_ERR	-1
36 #define RTAS_RC_BUSY	-2
37 
38 /* Flash image status values */
39 #define FLASH_AUTH           -9002 /* RTAS Not Service Authority Partition */
40 #define FLASH_NO_OP          -1099 /* No operation initiated by user */
41 #define FLASH_IMG_SHORT	     -1005 /* Flash image shorter than expected */
42 #define FLASH_IMG_BAD_LEN    -1004 /* Bad length value in flash list block */
43 #define FLASH_IMG_NULL_DATA  -1003 /* Bad data value in flash list block */
44 #define FLASH_IMG_READY      0     /* Firmware img ready for flash on reboot */
45 
46 /* Manage image status values */
47 #define MANAGE_AUTH          -9002 /* RTAS Not Service Authority Partition */
48 #define MANAGE_ACTIVE_ERR    -9001 /* RTAS Cannot Overwrite Active Img */
49 #define MANAGE_NO_OP         -1099 /* No operation initiated by user */
50 #define MANAGE_PARAM_ERR     -3    /* RTAS Parameter Error */
51 #define MANAGE_HW_ERR        -1    /* RTAS Hardware Error */
52 
53 /* Validate image status values */
54 #define VALIDATE_AUTH          -9002 /* RTAS Not Service Authority Partition */
55 #define VALIDATE_NO_OP         -1099 /* No operation initiated by the user */
56 #define VALIDATE_INCOMPLETE    -1002 /* User copied < VALIDATE_BUF_SIZE */
57 #define VALIDATE_READY	       -1001 /* Firmware image ready for validation */
58 #define VALIDATE_PARAM_ERR     -3    /* RTAS Parameter Error */
59 #define VALIDATE_HW_ERR        -1    /* RTAS Hardware Error */
60 
61 /* ibm,validate-flash-image update result tokens */
62 #define VALIDATE_TMP_UPDATE    0     /* T side will be updated */
63 #define VALIDATE_FLASH_AUTH    1     /* Partition does not have authority */
64 #define VALIDATE_INVALID_IMG   2     /* Candidate image is not valid */
65 #define VALIDATE_CUR_UNKNOWN   3     /* Current fixpack level is unknown */
66 /*
67  * Current T side will be committed to P side before being replace with new
68  * image, and the new image is downlevel from current image
69  */
70 #define VALIDATE_TMP_COMMIT_DL 4
71 /*
72  * Current T side will be committed to P side before being replaced with new
73  * image
74  */
75 #define VALIDATE_TMP_COMMIT    5
76 /*
77  * T side will be updated with a downlevel image
78  */
79 #define VALIDATE_TMP_UPDATE_DL 6
80 
81 /* ibm,manage-flash-image operation tokens */
82 #define RTAS_REJECT_TMP_IMG   0
83 #define RTAS_COMMIT_TMP_IMG   1
84 
85 /* Array sizes */
86 #define VALIDATE_BUF_SIZE 4096
87 #define RTAS_MSG_MAXLEN   64
88 
89 /* Quirk - RTAS requires 4k list length and block size */
90 #define RTAS_BLKLIST_LENGTH 4096
91 #define RTAS_BLK_SIZE 4096
92 
93 struct flash_block {
94 	char *data;
95 	unsigned long length;
96 };
97 
98 /* This struct is very similar but not identical to
99  * that needed by the rtas flash update.
100  * All we need to do for rtas is rewrite num_blocks
101  * into a version/length and translate the pointers
102  * to absolute.
103  */
104 #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
105 struct flash_block_list {
106 	unsigned long num_blocks;
107 	struct flash_block_list *next;
108 	struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
109 };
110 
111 static struct flash_block_list *rtas_firmware_flash_list;
112 
113 /* Use slab cache to guarantee 4k alignment */
114 static struct kmem_cache *flash_block_cache = NULL;
115 
116 #define FLASH_BLOCK_LIST_VERSION (1UL)
117 
118 /* Local copy of the flash block list.
119  * We only allow one open of the flash proc file and create this
120  * list as we go.  The rtas_firmware_flash_list varable will be
121  * set once the data is fully read.
122  *
123  * For convenience as we build the list we use virtual addrs,
124  * we do not fill in the version number, and the length field
125  * is treated as the number of entries currently in the block
126  * (i.e. not a byte count).  This is all fixed when calling
127  * the flash routine.
128  */
129 
130 /* Status int must be first member of struct */
131 struct rtas_update_flash_t
132 {
133 	int status;			/* Flash update status */
134 	struct flash_block_list *flist; /* Local copy of flash block list */
135 };
136 
137 /* Status int must be first member of struct */
138 struct rtas_manage_flash_t
139 {
140 	int status;			/* Returned status */
141 	unsigned int op;		/* Reject or commit image */
142 };
143 
144 /* Status int must be first member of struct */
145 struct rtas_validate_flash_t
146 {
147 	int status;		 	/* Returned status */
148 	char buf[VALIDATE_BUF_SIZE]; 	/* Candidate image buffer */
149 	unsigned int buf_size;		/* Size of image buf */
150 	unsigned int update_results;	/* Update results token */
151 };
152 
153 static DEFINE_SPINLOCK(flash_file_open_lock);
154 static struct proc_dir_entry *firmware_flash_pde;
155 static struct proc_dir_entry *firmware_update_pde;
156 static struct proc_dir_entry *validate_pde;
157 static struct proc_dir_entry *manage_pde;
158 
159 /* Do simple sanity checks on the flash image. */
160 static int flash_list_valid(struct flash_block_list *flist)
161 {
162 	struct flash_block_list *f;
163 	int i;
164 	unsigned long block_size, image_size;
165 
166 	/* Paranoid self test here.  We also collect the image size. */
167 	image_size = 0;
168 	for (f = flist; f; f = f->next) {
169 		for (i = 0; i < f->num_blocks; i++) {
170 			if (f->blocks[i].data == NULL) {
171 				return FLASH_IMG_NULL_DATA;
172 			}
173 			block_size = f->blocks[i].length;
174 			if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
175 				return FLASH_IMG_BAD_LEN;
176 			}
177 			image_size += block_size;
178 		}
179 	}
180 
181 	if (image_size < (256 << 10)) {
182 		if (image_size < 2)
183 			return FLASH_NO_OP;
184 	}
185 
186 	printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
187 
188 	return FLASH_IMG_READY;
189 }
190 
191 static void free_flash_list(struct flash_block_list *f)
192 {
193 	struct flash_block_list *next;
194 	int i;
195 
196 	while (f) {
197 		for (i = 0; i < f->num_blocks; i++)
198 			kmem_cache_free(flash_block_cache, f->blocks[i].data);
199 		next = f->next;
200 		kmem_cache_free(flash_block_cache, f);
201 		f = next;
202 	}
203 }
204 
205 static int rtas_flash_release(struct inode *inode, struct file *file)
206 {
207 	struct proc_dir_entry *dp = PDE(file_inode(file));
208 	struct rtas_update_flash_t *uf;
209 
210 	uf = (struct rtas_update_flash_t *) dp->data;
211 	if (uf->flist) {
212 		/* File was opened in write mode for a new flash attempt */
213 		/* Clear saved list */
214 		if (rtas_firmware_flash_list) {
215 			free_flash_list(rtas_firmware_flash_list);
216 			rtas_firmware_flash_list = NULL;
217 		}
218 
219 		if (uf->status != FLASH_AUTH)
220 			uf->status = flash_list_valid(uf->flist);
221 
222 		if (uf->status == FLASH_IMG_READY)
223 			rtas_firmware_flash_list = uf->flist;
224 		else
225 			free_flash_list(uf->flist);
226 
227 		uf->flist = NULL;
228 	}
229 
230 	atomic_dec(&dp->count);
231 	return 0;
232 }
233 
234 static void get_flash_status_msg(int status, char *buf)
235 {
236 	char *msg;
237 
238 	switch (status) {
239 	case FLASH_AUTH:
240 		msg = "error: this partition does not have service authority\n";
241 		break;
242 	case FLASH_NO_OP:
243 		msg = "info: no firmware image for flash\n";
244 		break;
245 	case FLASH_IMG_SHORT:
246 		msg = "error: flash image short\n";
247 		break;
248 	case FLASH_IMG_BAD_LEN:
249 		msg = "error: internal error bad length\n";
250 		break;
251 	case FLASH_IMG_NULL_DATA:
252 		msg = "error: internal error null data\n";
253 		break;
254 	case FLASH_IMG_READY:
255 		msg = "ready: firmware image ready for flash on reboot\n";
256 		break;
257 	default:
258 		sprintf(buf, "error: unexpected status value %d\n", status);
259 		return;
260 	}
261 
262 	strcpy(buf, msg);
263 }
264 
265 /* Reading the proc file will show status (not the firmware contents) */
266 static ssize_t rtas_flash_read(struct file *file, char __user *buf,
267 			       size_t count, loff_t *ppos)
268 {
269 	struct proc_dir_entry *dp = PDE(file_inode(file));
270 	struct rtas_update_flash_t *uf;
271 	char msg[RTAS_MSG_MAXLEN];
272 
273 	uf = dp->data;
274 
275 	if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
276 		get_flash_status_msg(uf->status, msg);
277 	} else {	   /* FIRMWARE_UPDATE_NAME */
278 		sprintf(msg, "%d\n", uf->status);
279 	}
280 
281 	return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
282 }
283 
284 /* constructor for flash_block_cache */
285 void rtas_block_ctor(void *ptr)
286 {
287 	memset(ptr, 0, RTAS_BLK_SIZE);
288 }
289 
290 /* We could be much more efficient here.  But to keep this function
291  * simple we allocate a page to the block list no matter how small the
292  * count is.  If the system is low on memory it will be just as well
293  * that we fail....
294  */
295 static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
296 				size_t count, loff_t *off)
297 {
298 	struct proc_dir_entry *dp = PDE(file_inode(file));
299 	struct rtas_update_flash_t *uf;
300 	char *p;
301 	int next_free;
302 	struct flash_block_list *fl;
303 
304 	uf = (struct rtas_update_flash_t *) dp->data;
305 
306 	if (uf->status == FLASH_AUTH || count == 0)
307 		return count;	/* discard data */
308 
309 	/* In the case that the image is not ready for flashing, the memory
310 	 * allocated for the block list will be freed upon the release of the
311 	 * proc file
312 	 */
313 	if (uf->flist == NULL) {
314 		uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
315 		if (!uf->flist)
316 			return -ENOMEM;
317 	}
318 
319 	fl = uf->flist;
320 	while (fl->next)
321 		fl = fl->next; /* seek to last block_list for append */
322 	next_free = fl->num_blocks;
323 	if (next_free == FLASH_BLOCKS_PER_NODE) {
324 		/* Need to allocate another block_list */
325 		fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
326 		if (!fl->next)
327 			return -ENOMEM;
328 		fl = fl->next;
329 		next_free = 0;
330 	}
331 
332 	if (count > RTAS_BLK_SIZE)
333 		count = RTAS_BLK_SIZE;
334 	p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
335 	if (!p)
336 		return -ENOMEM;
337 
338 	if(copy_from_user(p, buffer, count)) {
339 		kmem_cache_free(flash_block_cache, p);
340 		return -EFAULT;
341 	}
342 	fl->blocks[next_free].data = p;
343 	fl->blocks[next_free].length = count;
344 	fl->num_blocks++;
345 
346 	return count;
347 }
348 
349 static int rtas_excl_open(struct inode *inode, struct file *file)
350 {
351 	struct proc_dir_entry *dp = PDE(inode);
352 
353 	/* Enforce exclusive open with use count of PDE */
354 	spin_lock(&flash_file_open_lock);
355 	if (atomic_read(&dp->count) > 2) {
356 		spin_unlock(&flash_file_open_lock);
357 		return -EBUSY;
358 	}
359 
360 	atomic_inc(&dp->count);
361 	spin_unlock(&flash_file_open_lock);
362 
363 	return 0;
364 }
365 
366 static int rtas_excl_release(struct inode *inode, struct file *file)
367 {
368 	struct proc_dir_entry *dp = PDE(inode);
369 
370 	atomic_dec(&dp->count);
371 
372 	return 0;
373 }
374 
375 static void manage_flash(struct rtas_manage_flash_t *args_buf)
376 {
377 	s32 rc;
378 
379 	do {
380 		rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
381 			       1, NULL, args_buf->op);
382 	} while (rtas_busy_delay(rc));
383 
384 	args_buf->status = rc;
385 }
386 
387 static ssize_t manage_flash_read(struct file *file, char __user *buf,
388 			       size_t count, loff_t *ppos)
389 {
390 	struct proc_dir_entry *dp = PDE(file_inode(file));
391 	struct rtas_manage_flash_t *args_buf;
392 	char msg[RTAS_MSG_MAXLEN];
393 	int msglen;
394 
395 	args_buf = dp->data;
396 	if (args_buf == NULL)
397 		return 0;
398 
399 	msglen = sprintf(msg, "%d\n", args_buf->status);
400 
401 	return simple_read_from_buffer(buf, count, ppos, msg, msglen);
402 }
403 
404 static ssize_t manage_flash_write(struct file *file, const char __user *buf,
405 				size_t count, loff_t *off)
406 {
407 	struct proc_dir_entry *dp = PDE(file_inode(file));
408 	struct rtas_manage_flash_t *args_buf;
409 	const char reject_str[] = "0";
410 	const char commit_str[] = "1";
411 	char stkbuf[10];
412 	int op;
413 
414 	args_buf = (struct rtas_manage_flash_t *) dp->data;
415 	if ((args_buf->status == MANAGE_AUTH) || (count == 0))
416 		return count;
417 
418 	op = -1;
419 	if (buf) {
420 		if (count > 9) count = 9;
421 		if (copy_from_user (stkbuf, buf, count)) {
422 			return -EFAULT;
423 		}
424 		if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
425 			op = RTAS_REJECT_TMP_IMG;
426 		else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
427 			op = RTAS_COMMIT_TMP_IMG;
428 	}
429 
430 	if (op == -1)   /* buf is empty, or contains invalid string */
431 		return -EINVAL;
432 
433 	args_buf->op = op;
434 	manage_flash(args_buf);
435 
436 	return count;
437 }
438 
439 static void validate_flash(struct rtas_validate_flash_t *args_buf)
440 {
441 	int token = rtas_token("ibm,validate-flash-image");
442 	int update_results;
443 	s32 rc;
444 
445 	rc = 0;
446 	do {
447 		spin_lock(&rtas_data_buf_lock);
448 		memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
449 		rc = rtas_call(token, 2, 2, &update_results,
450 			       (u32) __pa(rtas_data_buf), args_buf->buf_size);
451 		memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
452 		spin_unlock(&rtas_data_buf_lock);
453 	} while (rtas_busy_delay(rc));
454 
455 	args_buf->status = rc;
456 	args_buf->update_results = update_results;
457 }
458 
459 static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
460 		                   char *msg)
461 {
462 	int n;
463 
464 	if (args_buf->status >= VALIDATE_TMP_UPDATE) {
465 		n = sprintf(msg, "%d\n", args_buf->update_results);
466 		if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
467 		    (args_buf->update_results == VALIDATE_TMP_UPDATE))
468 			n += sprintf(msg + n, "%s\n", args_buf->buf);
469 	} else {
470 		n = sprintf(msg, "%d\n", args_buf->status);
471 	}
472 	return n;
473 }
474 
475 static ssize_t validate_flash_read(struct file *file, char __user *buf,
476 			       size_t count, loff_t *ppos)
477 {
478 	struct proc_dir_entry *dp = PDE(file_inode(file));
479 	struct rtas_validate_flash_t *args_buf;
480 	char msg[RTAS_MSG_MAXLEN];
481 	int msglen;
482 
483 	args_buf = dp->data;
484 
485 	msglen = get_validate_flash_msg(args_buf, msg);
486 
487 	return simple_read_from_buffer(buf, count, ppos, msg, msglen);
488 }
489 
490 static ssize_t validate_flash_write(struct file *file, const char __user *buf,
491 				    size_t count, loff_t *off)
492 {
493 	struct proc_dir_entry *dp = PDE(file_inode(file));
494 	struct rtas_validate_flash_t *args_buf;
495 	int rc;
496 
497 	args_buf = (struct rtas_validate_flash_t *) dp->data;
498 
499 	if (dp->data == NULL) {
500 		dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
501 				GFP_KERNEL);
502 		if (dp->data == NULL)
503 			return -ENOMEM;
504 	}
505 
506 	/* We are only interested in the first 4K of the
507 	 * candidate image */
508 	if ((*off >= VALIDATE_BUF_SIZE) ||
509 		(args_buf->status == VALIDATE_AUTH)) {
510 		*off += count;
511 		return count;
512 	}
513 
514 	if (*off + count >= VALIDATE_BUF_SIZE)  {
515 		count = VALIDATE_BUF_SIZE - *off;
516 		args_buf->status = VALIDATE_READY;
517 	} else {
518 		args_buf->status = VALIDATE_INCOMPLETE;
519 	}
520 
521 	if (!access_ok(VERIFY_READ, buf, count)) {
522 		rc = -EFAULT;
523 		goto done;
524 	}
525 	if (copy_from_user(args_buf->buf + *off, buf, count)) {
526 		rc = -EFAULT;
527 		goto done;
528 	}
529 
530 	*off += count;
531 	rc = count;
532 done:
533 	if (rc < 0) {
534 		kfree(dp->data);
535 		dp->data = NULL;
536 	}
537 	return rc;
538 }
539 
540 static int validate_flash_release(struct inode *inode, struct file *file)
541 {
542 	struct proc_dir_entry *dp = PDE(file_inode(file));
543 	struct rtas_validate_flash_t *args_buf;
544 
545 	args_buf = (struct rtas_validate_flash_t *) dp->data;
546 
547 	if (args_buf->status == VALIDATE_READY) {
548 		args_buf->buf_size = VALIDATE_BUF_SIZE;
549 		validate_flash(args_buf);
550 	}
551 
552 	/* The matching atomic_inc was in rtas_excl_open() */
553 	atomic_dec(&dp->count);
554 
555 	return 0;
556 }
557 
558 static void rtas_flash_firmware(int reboot_type)
559 {
560 	unsigned long image_size;
561 	struct flash_block_list *f, *next, *flist;
562 	unsigned long rtas_block_list;
563 	int i, status, update_token;
564 
565 	if (rtas_firmware_flash_list == NULL)
566 		return;		/* nothing to do */
567 
568 	if (reboot_type != SYS_RESTART) {
569 		printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
570 		printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
571 		return;
572 	}
573 
574 	update_token = rtas_token("ibm,update-flash-64-and-reboot");
575 	if (update_token == RTAS_UNKNOWN_SERVICE) {
576 		printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
577 		       "is not available -- not a service partition?\n");
578 		printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
579 		return;
580 	}
581 
582 	/*
583 	 * Just before starting the firmware flash, cancel the event scan work
584 	 * to avoid any soft lockup issues.
585 	 */
586 	rtas_cancel_event_scan();
587 
588 	/*
589 	 * NOTE: the "first" block must be under 4GB, so we create
590 	 * an entry with no data blocks in the reserved buffer in
591 	 * the kernel data segment.
592 	 */
593 	spin_lock(&rtas_data_buf_lock);
594 	flist = (struct flash_block_list *)&rtas_data_buf[0];
595 	flist->num_blocks = 0;
596 	flist->next = rtas_firmware_flash_list;
597 	rtas_block_list = __pa(flist);
598 	if (rtas_block_list >= 4UL*1024*1024*1024) {
599 		printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
600 		spin_unlock(&rtas_data_buf_lock);
601 		return;
602 	}
603 
604 	printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
605 	/* Update the block_list in place. */
606 	rtas_firmware_flash_list = NULL; /* too hard to backout on error */
607 	image_size = 0;
608 	for (f = flist; f; f = next) {
609 		/* Translate data addrs to absolute */
610 		for (i = 0; i < f->num_blocks; i++) {
611 			f->blocks[i].data = (char *)__pa(f->blocks[i].data);
612 			image_size += f->blocks[i].length;
613 		}
614 		next = f->next;
615 		/* Don't translate NULL pointer for last entry */
616 		if (f->next)
617 			f->next = (struct flash_block_list *)__pa(f->next);
618 		else
619 			f->next = NULL;
620 		/* make num_blocks into the version/length field */
621 		f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
622 	}
623 
624 	printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
625 	printk(KERN_ALERT "FLASH: performing flash and reboot\n");
626 	rtas_progress("Flashing        \n", 0x0);
627 	rtas_progress("Please Wait...  ", 0x0);
628 	printk(KERN_ALERT "FLASH: this will take several minutes.  Do not power off!\n");
629 	status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
630 	switch (status) {	/* should only get "bad" status */
631 	    case 0:
632 		printk(KERN_ALERT "FLASH: success\n");
633 		break;
634 	    case -1:
635 		printk(KERN_ALERT "FLASH: hardware error.  Firmware may not be not flashed\n");
636 		break;
637 	    case -3:
638 		printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform.  Firmware not flashed\n");
639 		break;
640 	    case -4:
641 		printk(KERN_ALERT "FLASH: flash failed when partially complete.  System may not reboot\n");
642 		break;
643 	    default:
644 		printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
645 		break;
646 	}
647 	spin_unlock(&rtas_data_buf_lock);
648 }
649 
650 static void remove_flash_pde(struct proc_dir_entry *dp)
651 {
652 	if (dp) {
653 		kfree(dp->data);
654 		remove_proc_entry(dp->name, dp->parent);
655 	}
656 }
657 
658 static int initialize_flash_pde_data(const char *rtas_call_name,
659 				     size_t buf_size,
660 				     struct proc_dir_entry *dp)
661 {
662 	int *status;
663 	int token;
664 
665 	dp->data = kzalloc(buf_size, GFP_KERNEL);
666 	if (dp->data == NULL)
667 		return -ENOMEM;
668 
669 	/*
670 	 * This code assumes that the status int is the first member of the
671 	 * struct
672 	 */
673 	status = (int *) dp->data;
674 	token = rtas_token(rtas_call_name);
675 	if (token == RTAS_UNKNOWN_SERVICE)
676 		*status = FLASH_AUTH;
677 	else
678 		*status = FLASH_NO_OP;
679 
680 	return 0;
681 }
682 
683 static struct proc_dir_entry *create_flash_pde(const char *filename,
684 					       const struct file_operations *fops)
685 {
686 	return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
687 }
688 
689 static const struct file_operations rtas_flash_operations = {
690 	.owner		= THIS_MODULE,
691 	.read		= rtas_flash_read,
692 	.write		= rtas_flash_write,
693 	.open		= rtas_excl_open,
694 	.release	= rtas_flash_release,
695 	.llseek		= default_llseek,
696 };
697 
698 static const struct file_operations manage_flash_operations = {
699 	.owner		= THIS_MODULE,
700 	.read		= manage_flash_read,
701 	.write		= manage_flash_write,
702 	.open		= rtas_excl_open,
703 	.release	= rtas_excl_release,
704 	.llseek		= default_llseek,
705 };
706 
707 static const struct file_operations validate_flash_operations = {
708 	.owner		= THIS_MODULE,
709 	.read		= validate_flash_read,
710 	.write		= validate_flash_write,
711 	.open		= rtas_excl_open,
712 	.release	= validate_flash_release,
713 	.llseek		= default_llseek,
714 };
715 
716 static int __init rtas_flash_init(void)
717 {
718 	int rc;
719 
720 	if (rtas_token("ibm,update-flash-64-and-reboot") ==
721 		       RTAS_UNKNOWN_SERVICE) {
722 		pr_info("rtas_flash: no firmware flash support\n");
723 		return 1;
724 	}
725 
726 	firmware_flash_pde = create_flash_pde("powerpc/rtas/"
727 					      FIRMWARE_FLASH_NAME,
728 					      &rtas_flash_operations);
729 	if (firmware_flash_pde == NULL) {
730 		rc = -ENOMEM;
731 		goto cleanup;
732 	}
733 
734 	rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
735 			 	       sizeof(struct rtas_update_flash_t),
736 				       firmware_flash_pde);
737 	if (rc != 0)
738 		goto cleanup;
739 
740 	firmware_update_pde = create_flash_pde("powerpc/rtas/"
741 					       FIRMWARE_UPDATE_NAME,
742 					       &rtas_flash_operations);
743 	if (firmware_update_pde == NULL) {
744 		rc = -ENOMEM;
745 		goto cleanup;
746 	}
747 
748 	rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
749 			 	       sizeof(struct rtas_update_flash_t),
750 				       firmware_update_pde);
751 	if (rc != 0)
752 		goto cleanup;
753 
754 	validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
755 			      		&validate_flash_operations);
756 	if (validate_pde == NULL) {
757 		rc = -ENOMEM;
758 		goto cleanup;
759 	}
760 
761 	rc = initialize_flash_pde_data("ibm,validate-flash-image",
762 		                       sizeof(struct rtas_validate_flash_t),
763 				       validate_pde);
764 	if (rc != 0)
765 		goto cleanup;
766 
767 	manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
768 				      &manage_flash_operations);
769 	if (manage_pde == NULL) {
770 		rc = -ENOMEM;
771 		goto cleanup;
772 	}
773 
774 	rc = initialize_flash_pde_data("ibm,manage-flash-image",
775 			               sizeof(struct rtas_manage_flash_t),
776 				       manage_pde);
777 	if (rc != 0)
778 		goto cleanup;
779 
780 	rtas_flash_term_hook = rtas_flash_firmware;
781 
782 	flash_block_cache = kmem_cache_create("rtas_flash_cache",
783 				RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
784 				rtas_block_ctor);
785 	if (!flash_block_cache) {
786 		printk(KERN_ERR "%s: failed to create block cache\n",
787 				__func__);
788 		rc = -ENOMEM;
789 		goto cleanup;
790 	}
791 	return 0;
792 
793 cleanup:
794 	remove_flash_pde(firmware_flash_pde);
795 	remove_flash_pde(firmware_update_pde);
796 	remove_flash_pde(validate_pde);
797 	remove_flash_pde(manage_pde);
798 
799 	return rc;
800 }
801 
802 static void __exit rtas_flash_cleanup(void)
803 {
804 	rtas_flash_term_hook = NULL;
805 
806 	if (rtas_firmware_flash_list) {
807 		free_flash_list(rtas_firmware_flash_list);
808 		rtas_firmware_flash_list = NULL;
809 	}
810 
811 	if (flash_block_cache)
812 		kmem_cache_destroy(flash_block_cache);
813 
814 	remove_flash_pde(firmware_flash_pde);
815 	remove_flash_pde(firmware_update_pde);
816 	remove_flash_pde(validate_pde);
817 	remove_flash_pde(manage_pde);
818 }
819 
820 module_init(rtas_flash_init);
821 module_exit(rtas_flash_cleanup);
822 MODULE_LICENSE("GPL");
823