Comments concerning Enhanced Flash Patch

Mark DeWandel mdewand at redhat.com
Thu Jan 22 00:30:37 EST 2004


John, I have just one complaint about the memory leak fix in the
attached patch that you sent me yesterday.  Calling remove_flash_pde()
more than once for a given proc_dir_entry can occur if the kmalloc() in
initialize_flash_pde_data() fails.  If this happens, remove_flash_pde()
will try to free dp->data and remove the proc_dir_entry more than
once.  One solution is to remove the call to remove_flash_pde() in
initialize_flash_pde_data() since it is guaranteed to be called in
rtas_flash_init() for the error path.

The consensus among Red Hat engineers is that the following issues still
must be resolved:

[1] In manage_flash() and validate_flash(), a bug in firmware could
    effectively become a busy wait if the return code from rtas_call() is
    consistently RTAS_RC_BUSY for a prolonged period of time.  The check
    for pending signals in rtas_do_extended_delay() provides a bail-out
    of sorts in the blocking case but doesn't guarantee termination
    of the loop if there's a bug in firmware and no signal is posted.
    Even if this never becomes a real problem, providing a way out of
    this loop after some threshold certainly doesn't hurt anything.
    It's just good defensive programming.

[2] The need for mutual exclusion in the read/write paths is still a
    sticking point as well.  The introduction of a semaphore to guard
    these paths is all that is being requested.  Can we get a patch
    which includes this?

--
Mark DeWandel <mdewand at redhat.com>
Red Hat, Inc.
(978) 692-3113 ext. 23252
-------------- next part --------------
diff -X /home/johnrose/tmp/diffignore.txt -urpN /usr/src/linux-2.4.21-6.EL/arch/ppc64/kernel/ppc_ksyms.c ./EL_ef/arch/ppc64/kernel/ppc_ksyms.c
--- linux-2.4.21-6.EL/arch/ppc64/kernel/ppc_ksyms.c	2003-12-09 13:42:04.000000000 -0600
+++ ./EL_ef/arch/ppc64/kernel/ppc_ksyms.c	2004-01-15 15:31:47.000000000 -0600
@@ -266,6 +266,7 @@ EXPORT_SYMBOL(rtas_token);
 EXPORT_SYMBOL(rtas_call);
 EXPORT_SYMBOL(rtas_data_buf);
 EXPORT_SYMBOL(rtas_data_buf_lock);
+EXPORT_SYMBOL(rtas_do_extended_delay);
 #endif

 #ifndef CONFIG_PPC_ISERIES
diff -X /home/johnrose/tmp/diffignore.txt -urpN /usr/src/linux-2.4.21-6.EL/arch/ppc64/kernel/rtas.c ./EL_ef/arch/ppc64/kernel/rtas.c
--- linux-2.4.21-6.EL/arch/ppc64/kernel/rtas.c	2003-12-09 13:41:30.000000000 -0600
+++ ./EL_ef/arch/ppc64/kernel/rtas.c	2004-01-19 16:13:36.000000000 -0600
@@ -184,6 +184,35 @@ rtas_call(int token, int nargs, int nret
 	return (ulong)((nret > 0) ? rtas_args->rets[0] : 0);
 }

+/* Given an RTAS status code of 990n perform the hinted delay of 10^n
+ * (last digit) milliseconds.  For now we bound at n=5 (100 secs).
+ */
+int
+rtas_do_extended_delay(int status)
+{
+	int order = status - 9900;
+	unsigned long ms;
+	unsigned long jiffies;
+
+	if (order < 0)
+		order = 0;	/* RTC depends on this for -2 clock busy */
+	else if (order > 5)
+		order = 5;	/* bound */
+
+	/* Use microseconds for reasonable accuracy */
+	for (ms=1; order > 0; order--)
+		ms *= 10;
+
+	jiffies = (ms * HZ) / 1000;
+
+	set_current_state(TASK_INTERRUPTIBLE);
+	schedule_timeout(jiffies);
+	if (signal_pending(current))
+		return RTAS_DELAY_INTR;
+
+	return 0;
+}
+
 #define FLASH_BLOCK_LIST_VERSION (1UL)
 static void
 rtas_flash_firmware(void)
diff -X /home/johnrose/tmp/diffignore.txt -urpN /usr/src/linux-2.4.21-6.EL/arch/ppc64/kernel/rtas_flash.c ./EL_ef/arch/ppc64/kernel/rtas_flash.c
--- linux-2.4.21-6.EL/arch/ppc64/kernel/rtas_flash.c	2002-11-28 17:53:11.000000000 -0600
+++ ./EL_ef/arch/ppc64/kernel/rtas_flash.c	2004-01-19 17:28:16.000000000 -0600
@@ -24,7 +24,56 @@
 #define MODULE_VERSION "1.0"
 #define MODULE_NAME "rtas_flash"

-#define FIRMWARE_FLASH_NAME "firmware_flash"
+#define FIRMWARE_FLASH_NAME "firmware_flash"
+#define FIRMWARE_UPDATE_NAME "firmware_update"
+#define MANAGE_FLASH_NAME "manage_flash"
+#define VALIDATE_FLASH_NAME "validate_flash"
+
+/* General RTAS Status Codes */
+#define RTAS_RC_SUCCESS  0
+#define RTAS_RC_HW_ERR	-1
+#define RTAS_RC_BUSY	-2
+
+/* Interrupted RTAS operation */
+#define RTAS_INTR -1098
+
+/* Flash image status values */
+#define FLASH_AUTH           -9002 /* RTAS Not Service Authority Partition */
+#define FLASH_NO_OP          -1099 /* No operation initiated by user */
+#define FLASH_IMG_SHORT	     -1005 /* Flash image shorter than expected */
+#define FLASH_IMG_BAD_LEN    -1004 /* Bad length value in flash list block */
+#define FLASH_IMG_NULL_DATA  -1003 /* Bad data value in flash list block */
+#define FLASH_IMG_READY      0     /* Firmware img ready for flash on reboot */
+
+/* Manage image status values */
+#define MANAGE_AUTH          -9002 /* RTAS Not Service Authority Partition */
+#define MANAGE_ACTIVE_ERR    -9001 /* RTAS Cannot Overwrite Active Img */
+#define MANAGE_NO_OP         -1099 /* No operation initiated by user */
+#define MANAGE_PARAM_ERR     -3    /* RTAS Parameter Error */
+#define MANAGE_HW_ERR        -1    /* RTAS Hardware Error */
+
+/* Validate image status values */
+#define VALIDATE_AUTH          -9002 /* RTAS Not Service Authority Partition */
+#define VALIDATE_NO_OP         -1099 /* No operation initiated by the user */
+#define VALIDATE_INCOMPLETE    -1002 /* User copied < VALIDATE_BUF_SIZE */
+#define VALIDATE_READY	       -1001 /* Firmware image ready for validation */
+#define VALIDATE_PARAM_ERR     -3    /* RTAS Parameter Error */
+#define VALIDATE_HW_ERR        -1    /* RTAS Hardware Error */
+#define VALIDATE_TMP_UPDATE    0     /* Validate Return Status */
+#define VALIDATE_FLASH_AUTH    1     /* Validate Return Status */
+#define VALIDATE_INVALID_IMG   2     /* Validate Return Status */
+#define VALIDATE_CUR_UNKNOWN   3     /* Validate Return Status */
+#define VALIDATE_TMP_COMMIT_DL 4     /* Validate Return Status */
+#define VALIDATE_TMP_COMMIT    5     /* Validate Return Status */
+#define VALIDATE_TMP_UPDATE_DL 6     /* Validate Return Status */
+
+/* ibm,manage-flash-image operation tokens */
+#define RTAS_REJECT_TMP_IMG   0
+#define RTAS_COMMIT_TMP_IMG   1
+
+/* Array sizes */
+#define VALIDATE_BUF_SIZE 4096
+#define RTAS_MSG_MAXLEN   64

 /* Local copy of the flash block list.
  * We only allow one open of the flash proc file and create this
@@ -36,21 +85,35 @@
  * is treated as the number of entries currently in the block
  * (i.e. not a byte count).  This is all fixed on release.
  */
-static struct flash_block_list *flist;
-static char *flash_msg;
-static int flash_possible;
-
-static int rtas_flash_open(struct inode *inode, struct file *file)
-{
-	if ((file->f_mode & FMODE_WRITE) && flash_possible) {
-		if (flist)
-			return -EBUSY;
-		flist = (struct flash_block_list *)get_free_page(GFP_KERNEL);
-		if (!flist)
-			return -ENOMEM;
-	}
-	return 0;
-}
+
+/* Status int must be first member of struct */
+struct rtas_update_flash_t
+{
+	int status;			/* Flash update status */
+	struct flash_block_list *flist; /* Local copy of flash block list */
+};
+
+/* Status int must be first member of struct */
+struct rtas_manage_flash_t
+{
+	int status;			/* Returned status */
+	unsigned int op;		/* Reject or commit image */
+};
+
+/* Status int must be first member of struct */
+struct rtas_validate_flash_t
+{
+	int status;		 	/* Returned status */
+	char buf[VALIDATE_BUF_SIZE]; 	/* Candidate image buffer */
+	unsigned int buf_size;		/* Size of image buf */
+	unsigned int update_results;	/* Update results token */
+};
+
+static spinlock_t flash_file_open_lock = SPIN_LOCK_UNLOCKED;
+static struct proc_dir_entry *firmware_flash_pde = NULL;
+static struct proc_dir_entry *firmware_update_pde = NULL;
+static struct proc_dir_entry *validate_pde = NULL;
+static struct proc_dir_entry *manage_pde = NULL;

 /* Do simple sanity checks on the flash image. */
 static int flash_list_valid(struct flash_block_list *flist)
@@ -59,32 +122,27 @@ static int flash_list_valid(struct flash
 	int i;
 	unsigned long block_size, image_size;

-	flash_msg = NULL;
 	/* Paranoid self test here.  We also collect the image size. */
 	image_size = 0;
 	for (f = flist; f; f = f->next) {
 		for (i = 0; i < f->num_blocks; i++) {
 			if (f->blocks[i].data == NULL) {
-				flash_msg = "error: internal error null data\n";
-				return 0;
+				return FLASH_IMG_NULL_DATA;
 			}
 			block_size = f->blocks[i].length;
 			if (block_size <= 0 || block_size > PAGE_SIZE) {
-				flash_msg = "error: internal error bad length\n";
-				return 0;
+				return FLASH_IMG_BAD_LEN;
 			}
 			image_size += block_size;
 		}
 	}
-	if (image_size < (256 << 10)) {
-		if (image_size < 2)
-			flash_msg = NULL;	/* allow "clear" of image */
-		else
-			flash_msg = "error: flash image short\n";
-		return 0;
-	}
+
+	if (image_size < 2)
+		return FLASH_NO_OP;
+
 	printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
-	return 1;
+
+	return FLASH_IMG_READY;
 }

 static void free_flash_list(struct flash_block_list *f)
@@ -103,56 +161,91 @@ static void free_flash_list(struct flash

 static int rtas_flash_release(struct inode *inode, struct file *file)
 {
-	if (flist) {
-		/* Always clear saved list on a new attempt. */
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_update_flash_t *uf;
+
+	uf = (struct rtas_update_flash_t *) dp->data;
+	if (uf->flist) {
+		/* File was opened in write mode for a new flash attempt */
+		/* Clear saved list */
 		if (rtas_firmware_flash_list.next) {
 			free_flash_list(rtas_firmware_flash_list.next);
 			rtas_firmware_flash_list.next = NULL;
 		}

-		if (flash_list_valid(flist))
-			rtas_firmware_flash_list.next = flist;
+		if (uf->status != FLASH_AUTH)
+			uf->status = flash_list_valid(uf->flist);
+
+		if (uf->status == FLASH_IMG_READY)
+			rtas_firmware_flash_list.next = uf->flist;
 		else
-			free_flash_list(flist);
-		flist = NULL;
+			free_flash_list(uf->flist);
+
+		uf->flist = NULL;
 	}
+
+	atomic_dec(&dp->count);
 	return 0;
 }

+static int get_flash_status_msg(int status, char *buf, int size)
+{
+	int len;
+
+	switch (status) {
+	case FLASH_AUTH:
+		len = snprintf(buf, size, "error: this partition does not have service authority\n");
+		break;
+	case FLASH_NO_OP:
+		len = snprintf(buf, size, "info: no firmware image for flash\n");
+		break;
+	case FLASH_IMG_SHORT:
+		len = snprintf(buf, size, "error: flash image short\n");
+		break;
+	case FLASH_IMG_BAD_LEN:
+		len = snprintf(buf, size, "error: internal error bad length\n");
+		break;
+	case FLASH_IMG_NULL_DATA:
+		len = snprintf(buf, size, "error: internal error null data\n");
+		break;
+	case FLASH_IMG_READY:
+		len = snprintf(buf, size, "ready: firmware image ready for flash on reboot\n");
+		break;
+	default:
+		len = snprintf(buf, size, "error: unexpected status value %d\n", status);
+		break;
+	}
+
+	return len >= size ? size-1 : len;
+}
+
 /* Reading the proc file will show status (not the firmware contents) */
 static ssize_t rtas_flash_read(struct file *file, char *buf,
 			       size_t count, loff_t *ppos)
 {
-	int error;
-	char *msg;
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_update_flash_t *uf;
+	char msg[RTAS_MSG_MAXLEN];
 	int msglen;

-	if (!flash_possible) {
-		msg = "error: this partition does not have service authority\n";
-	} else if (flist) {
-		msg = "info: this file is busy for write by some process\n";
-	} else if (flash_msg) {
-		msg = flash_msg;	/* message from last flash attempt */
-	} else if (rtas_firmware_flash_list.next) {
-		msg = "ready: firmware image ready for flash on reboot\n";
-	} else {
-		msg = "info: no firmware image for flash\n";
+	uf = (struct rtas_update_flash_t *) dp->data;
+
+	if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
+		msglen = get_flash_status_msg(uf->status, msg, RTAS_MSG_MAXLEN);
+	} else {	   /* FIRMWARE_UPDATE_NAME */
+		msglen = sprintf(msg, "%d\n", uf->status);
 	}
-	msglen = strlen(msg);
+
+	if (*ppos >= msglen)
+		return 0;
+	msglen -= *ppos;
 	if (msglen > count)
 		msglen = count;

-	if (ppos && *ppos != 0)
-		return 0;	/* be cheap */
-
-	error = verify_area(VERIFY_WRITE, buf, msglen);
-	if (error)
-		return -EINVAL;
-
-	copy_to_user(buf, msg, msglen);
+	if (copy_to_user(buf, msg + (*ppos), msglen))
+		return -EFAULT;
+	*ppos += msglen;

-	if (ppos)
-		*ppos = msglen;
 	return msglen;
 }

@@ -164,14 +257,28 @@ static ssize_t rtas_flash_read(struct fi
 static ssize_t rtas_flash_write(struct file *file, const char *buffer,
 				size_t count, loff_t *off)
 {
-	size_t len = count;
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_update_flash_t *uf;
 	char *p;
 	int next_free;
-	struct flash_block_list *fl = flist;
+	struct flash_block_list *fl;
+
+	uf = (struct rtas_update_flash_t *) dp->data;
+
+	if (uf->status == FLASH_AUTH || count == 0)
+		return count;	/* discard data */

-	if (!flash_possible || len == 0)
-		return len;	/* discard data */
+	/* In the case that the image is not ready for flashing, the memory
+	 * allocated for the block list will be freed upon the release of the
+	 * proc file
+	 */
+	if (uf->flist == NULL) {
+		uf->flist = (struct flash_block_list *) get_free_page(GFP_KERNEL);
+		if (!uf->flist)
+			return -ENOMEM;
+	}

+	fl = uf->flist;
 	while (fl->next)
 		fl = fl->next; /* seek to last block_list for append */
 	next_free = fl->num_blocks;
@@ -184,55 +291,409 @@ static ssize_t rtas_flash_write(struct f
 		next_free = 0;
 	}

-	if (len > PAGE_SIZE)
-		len = PAGE_SIZE;
+	if (count > PAGE_SIZE)
+		count = PAGE_SIZE;
 	p = (char *)get_free_page(GFP_KERNEL);
 	if (!p)
 		return -ENOMEM;
-	if(copy_from_user(p, buffer, len)) {
+
+	if(copy_from_user(p, buffer, count)) {
 		free_page((unsigned long)p);
 		return -EFAULT;
 	}
 	fl->blocks[next_free].data = p;
-	fl->blocks[next_free].length = len;
+	fl->blocks[next_free].length = count;
 	fl->num_blocks++;

-	return len;
+	return count;
+}
+
+static int rtas_excl_open(struct inode *inode, struct file *file)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+
+	/* Enforce exclusive open with use count of PDE */
+	spin_lock(&flash_file_open_lock);
+	if (atomic_read(&dp->count) > 1) {
+		spin_unlock(&flash_file_open_lock);
+		return -EBUSY;
+	}
+
+	atomic_inc(&dp->count);
+	spin_unlock(&flash_file_open_lock);
+
+	return 0;
+}
+
+static int rtas_excl_release(struct inode *inode, struct file *file)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+
+	atomic_dec(&dp->count);
+
+	return 0;
+}
+
+static void manage_flash(struct rtas_manage_flash_t *args_buf)
+{
+	s32 delay_rc;
+	s32 rc;
+
+	while (1) {
+		rc = (s32) rtas_call(rtas_token("ibm,manage-flash-image"), 1,
+				1, NULL, (long) args_buf->op);
+		if (rc == RTAS_RC_BUSY)
+			udelay(1);
+		else if (rtas_is_extended_busy(rc)) {
+			if ((delay_rc = rtas_do_extended_delay(rc))) {
+				/* Delay interrupted */
+				args_buf->status = delay_rc;
+				break;
+			}
+		} else {
+			args_buf->status = rc;
+			break;
+		}
+	}
+}
+
+static ssize_t manage_flash_read(struct file *file, char *buf,
+			       size_t count, loff_t *ppos)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_manage_flash_t *args_buf;
+	char msg[RTAS_MSG_MAXLEN];
+	int msglen;
+
+	args_buf = (struct rtas_manage_flash_t *) dp->data;
+	if (args_buf == NULL)
+		return 0;
+
+	msglen = sprintf(msg, "%d\n", args_buf->status);
+	if (*ppos >= msglen)
+		return 0;
+
+	msglen -= *ppos;
+	if (msglen > count)
+		msglen = count;
+
+	if (copy_to_user(buf, msg + (*ppos), msglen))
+		return -EFAULT;
+	*ppos += msglen;
+
+	return msglen;
+}
+
+static ssize_t manage_flash_write(struct file *file, const char *buf,
+				size_t count, loff_t *off)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_manage_flash_t *args_buf;
+	const char reject_str[] = "0";
+	const char commit_str[] = "1";
+	char msg[RTAS_MSG_MAXLEN];
+	int op;
+
+	args_buf = (struct rtas_manage_flash_t *) dp->data;
+	if ((args_buf->status == MANAGE_AUTH) || (count == 0))
+		return count;
+
+	if (count > RTAS_MSG_MAXLEN)
+		count = RTAS_MSG_MAXLEN;
+	if (copy_from_user(msg, buf, count))
+		return -EFAULT;
+
+	if (strncmp(buf, reject_str, strlen(reject_str)) == 0)
+		op = RTAS_REJECT_TMP_IMG;
+	else if (strncmp(buf, commit_str, strlen(commit_str)) == 0)
+		op = RTAS_COMMIT_TMP_IMG;
+	else
+		return -EINVAL;
+
+	args_buf->op = op;
+	manage_flash(args_buf);
+	*off += count;
+
+	return count;
+}
+
+static void validate_flash(struct rtas_validate_flash_t *args_buf)
+{
+	int token = rtas_token("ibm,validate-flash-image");
+	unsigned int wait_time;
+	long update_results;
+	s32 delay_rc;
+	s32 rc;
+
+	rc = 0;
+	while(1) {
+		spin_lock(&rtas_data_buf_lock);
+		memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
+		rc = (s32) rtas_call(token, 2, 2, &update_results,
+				     __pa(rtas_data_buf), args_buf->buf_size);
+		memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
+		spin_unlock(&rtas_data_buf_lock);
+
+		if (rc == RTAS_RC_BUSY)
+			udelay(1);
+		else if (rtas_is_extended_busy(rc)) {
+			if ((delay_rc = rtas_do_extended_delay(rc))) {
+				/* Delay interrupted */
+				args_buf->status = delay_rc;
+				break;
+			}
+		} else {
+			args_buf->status = rc;
+			args_buf->update_results = (u32) update_results;
+			break;
+		}
+	}
+}
+
+static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
+		                  char *msg, int size)
+{
+	int n;
+
+	if (args_buf->status >= VALIDATE_TMP_UPDATE) {
+		n = snprintf(msg, size, "%u\n", args_buf->update_results);
+		if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
+		    (args_buf->update_results == VALIDATE_TMP_UPDATE))
+			n += snprintf(msg + n, size - n, "%s\n", args_buf->buf);
+	} else {
+		n = snprintf(msg, size, "%d\n", args_buf->status);
+	}
+
+	return n >= size ? size - 1 : n;
+}
+
+static ssize_t validate_flash_read(struct file *file, char *buf,
+			       size_t count, loff_t *ppos)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_validate_flash_t *args_buf;
+	char msg[RTAS_MSG_MAXLEN];
+	int msglen;
+
+	args_buf = (struct rtas_validate_flash_t *) dp->data;
+
+	msglen = get_validate_flash_msg(args_buf, msg, RTAS_MSG_MAXLEN);
+
+	if (*ppos >= msglen)
+		return 0;
+
+	msglen -= *ppos;
+	if (msglen > count)
+		msglen = count;
+
+	if (copy_to_user(buf, msg + (*ppos), msglen))
+		return -EFAULT;
+	*ppos += msglen;
+
+	return msglen;
+}
+
+static ssize_t validate_flash_write(struct file *file, const char *buf,
+				size_t count, loff_t *off)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_validate_flash_t *args_buf;
+
+	args_buf = (struct rtas_validate_flash_t *) dp->data;
+
+	if (dp->data == NULL) {
+		dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
+				GFP_KERNEL);
+		if (dp->data == NULL)
+			return -ENOMEM;
+	}
+
+	/* We are only interested in the first 4K of the
+	 * candidate image */
+	if ((*off >= VALIDATE_BUF_SIZE) ||
+		(args_buf->status == VALIDATE_AUTH)) {
+		*off += count;
+		return count;
+	}
+
+	if (*off + count >= VALIDATE_BUF_SIZE)  {
+		count = VALIDATE_BUF_SIZE - *off;
+		args_buf->status = VALIDATE_READY;
+	} else {
+		args_buf->status = VALIDATE_INCOMPLETE;
+	}
+
+	if (copy_from_user(args_buf->buf + *off, buf, count))
+		return -EFAULT;
+	*off += count;
+
+	return count;
+}
+
+static int validate_flash_release(struct inode *inode, struct file *file)
+{
+	struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
+	struct rtas_validate_flash_t *args_buf;
+
+	args_buf = (struct rtas_validate_flash_t *) dp->data;
+
+	if (args_buf->status == VALIDATE_READY) {
+		args_buf->buf_size = VALIDATE_BUF_SIZE;
+		validate_flash(args_buf);
+	}
+
+	atomic_dec(&dp->count);
+
+	return 0;
+}
+
+static inline void remove_flash_pde(struct proc_dir_entry *dp)
+{
+	if (dp) {
+		if (dp->data != NULL)
+			kfree(dp->data);
+		remove_proc_entry(dp->name, rtas_proc_dir);
+	}
+}
+
+static inline int initialize_flash_pde_data(const char *rtas_call_name,
+		                            size_t buf_size,
+					    struct proc_dir_entry *dp)
+{
+	int *status;
+	int token;
+
+	dp->data = kmalloc(buf_size, GFP_KERNEL);
+	if (dp->data == NULL) {
+		remove_flash_pde(dp);
+		return -ENOMEM;
+	}
+
+	memset(dp->data, 0, buf_size);
+
+	/* This code assumes that the status int is the first member of the
+	 * struct
+	 */
+	status = (int *) dp->data;
+	token = rtas_token(rtas_call_name);
+	if (token == RTAS_UNKNOWN_SERVICE)
+		*status = FLASH_AUTH;
+	else
+		*status = FLASH_NO_OP;
+
+	return 0;
+}
+
+static inline struct proc_dir_entry * create_flash_pde(const char *filename,
+					struct file_operations *fops)
+{
+	struct proc_dir_entry *ent = NULL;
+
+	ent = create_proc_entry(filename, S_IRUSR | S_IWUSR, rtas_proc_dir);
+	if (ent != NULL) {
+		ent->nlink = 1;
+		ent->proc_fops = fops;
+		ent->owner = THIS_MODULE;
+	}
+
+	return ent;
 }

 static struct file_operations rtas_flash_operations = {
 	read:		rtas_flash_read,
 	write:		rtas_flash_write,
-	open:		rtas_flash_open,
+	open:		rtas_excl_open,
 	release:	rtas_flash_release,
 };

+static struct file_operations manage_flash_operations = {
+	read:		manage_flash_read,
+	write:		manage_flash_write,
+	open:		rtas_excl_open,
+	release:	rtas_excl_release,
+};
+
+static struct file_operations validate_flash_operations = {
+	read:		validate_flash_read,
+	write:		validate_flash_write,
+	open:		rtas_excl_open,
+	release:	validate_flash_release,
+};
+
+#define CHECK_PDE_CREATE(_pdevar, _rcvar, _label)	\
+	if (!_pdevar) {					\
+		_rcvar = -ENOMEM;			\
+		goto _label;				\
+	}
+
+#define CHECK_RC(_rc, _label)	\
+	if (_rc != 0) 		\
+		goto _label;

 int __init rtas_flash_init(void)
 {
-	struct proc_dir_entry *ent = NULL;
+	int rc;

 	if (!rtas_proc_dir) {
-		printk(KERN_WARNING "rtas proc dir does not already exist");
+		printk(KERN_WARNING "%s: rtas proc dir does not already exist",
+				__FUNCTION__);
 		return -ENOENT;
 	}

-	if (rtas_token("ibm,update-flash-64-and-reboot") != RTAS_UNKNOWN_SERVICE)
-		flash_possible = 1;
-
-	if ((ent = create_proc_entry(FIRMWARE_FLASH_NAME, S_IRUSR | S_IWUSR, rtas_proc_dir)) != NULL) {
-		ent->nlink = 1;
-		ent->proc_fops = &rtas_flash_operations;
-		ent->owner = THIS_MODULE;
+	firmware_flash_pde = create_flash_pde(FIRMWARE_FLASH_NAME,
+					      &rtas_flash_operations);
+	CHECK_PDE_CREATE(firmware_flash_pde, rc, done);
+
+	rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
+			 	       sizeof(struct rtas_update_flash_t),
+				       firmware_flash_pde);
+	CHECK_RC(rc, done);
+
+	firmware_update_pde = create_flash_pde(FIRMWARE_UPDATE_NAME,
+					       &rtas_flash_operations);
+	CHECK_PDE_CREATE(firmware_update_pde, rc, done);
+
+	rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
+			 	       sizeof(struct rtas_update_flash_t),
+				       firmware_update_pde);
+	CHECK_RC(rc, done);
+
+	validate_pde = create_flash_pde(VALIDATE_FLASH_NAME,
+			      		&validate_flash_operations);
+	CHECK_PDE_CREATE(validate_pde, rc, done);
+
+	rc = initialize_flash_pde_data("ibm,validate-flash-image",
+		                       sizeof(struct rtas_validate_flash_t),
+				       validate_pde);
+	CHECK_RC(rc, done);
+
+	manage_pde = create_flash_pde(MANAGE_FLASH_NAME,
+				      &manage_flash_operations);
+	CHECK_PDE_CREATE(manage_pde, rc, done);
+
+	rc = initialize_flash_pde_data("ibm,manage-flash-image",
+			               sizeof(struct rtas_manage_flash_t),
+				       manage_pde);
+done:
+	if (rc != 0) {
+		remove_flash_pde(firmware_flash_pde);
+		remove_flash_pde(firmware_update_pde);
+		remove_flash_pde(validate_pde);
+		remove_flash_pde(manage_pde);
 	}
-	return 0;
+
+	return rc;
 }

 void __exit rtas_flash_cleanup(void)
 {
 	if (!rtas_proc_dir)
 		return;
-	remove_proc_entry(FIRMWARE_FLASH_NAME, rtas_proc_dir);
+
+	remove_flash_pde(firmware_flash_pde);
+	remove_flash_pde(firmware_update_pde);
+	remove_flash_pde(validate_pde);
+	remove_flash_pde(manage_pde);
 }

 module_init(rtas_flash_init);
diff -X /home/johnrose/tmp/diffignore.txt -urpN /usr/src/linux-2.4.21-6.EL/include/asm-ppc64/rtas.h ./EL_ef/include/asm-ppc64/rtas.h
--- linux-2.4.21-6.EL/include/asm-ppc64/rtas.h	2003-12-09 13:41:33.000000000 -0600
+++ ./EL_ef/include/asm-ppc64/rtas.h	2004-01-19 17:28:45.000000000 -0600
@@ -24,6 +24,9 @@
 #define MAX_ERRINJCT_TOKENS 8 /* Max # tokens. */
 #define WORKSPACE_SIZE 1024

+/* Extended Delay Interrupted by Signal */
+#define RTAS_DELAY_INTR -1098
+
 /*
  * In general to call RTAS use rtas_token("string") to lookup
  * an RTAS token for the given string (e.g. "event-scan").
@@ -182,6 +185,13 @@ extern int rtas_errinjct_close(unsigned
 extern struct proc_dir_entry *rtas_proc_dir;
 extern struct errinjct_token ei_token_list[MAX_ERRINJCT_TOKENS];

+/* Given an RTAS status code of 9900..9905 compute the hinted delay */
+extern int rtas_do_extended_delay(int status);
+static inline int rtas_is_extended_busy(int status)
+{
+	return status >= 9900 && status <= 9905;
+}
+
 extern void pSeries_log_error(char *buf, unsigned int err_type, int fatal);

 /* Error types logged.  */


More information about the Linuxppc64-dev mailing list