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