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