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