diff options
Diffstat (limited to 'drivers/firmware/dell_rbu.c')
-rw-r--r-- | drivers/firmware/dell_rbu.c | 361 |
1 files changed, 211 insertions, 150 deletions
diff --git a/drivers/firmware/dell_rbu.c b/drivers/firmware/dell_rbu.c index 3b865f34a095..4f4ba9b6d182 100644 --- a/drivers/firmware/dell_rbu.c +++ b/drivers/firmware/dell_rbu.c @@ -50,7 +50,7 @@ MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>"); MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems"); MODULE_LICENSE("GPL"); -MODULE_VERSION("1.0"); +MODULE_VERSION("3.0"); #define BIOS_SCAN_LIMIT 0xffffffff #define MAX_IMAGE_LENGTH 16 @@ -62,14 +62,16 @@ static struct _rbu_data { int dma_alloc; spinlock_t lock; unsigned long packet_read_count; - unsigned long packet_write_count; unsigned long num_packets; unsigned long packetsize; + unsigned long imagesize; + int entry_created; } rbu_data; -static char image_type[MAX_IMAGE_LENGTH] = "mono"; -module_param_string(image_type, image_type, sizeof(image_type), 0); -MODULE_PARM_DESC(image_type, "BIOS image type. choose- mono or packet"); +static char image_type[MAX_IMAGE_LENGTH + 1] = "mono"; +module_param_string(image_type, image_type, sizeof (image_type), 0); +MODULE_PARM_DESC(image_type, + "BIOS image type. choose- mono or packet or init"); struct packet_data { struct list_head list; @@ -87,55 +89,13 @@ static dma_addr_t dell_rbu_dmaaddr; static void init_packet_head(void) { INIT_LIST_HEAD(&packet_data_head.list); - rbu_data.packet_write_count = 0; rbu_data.packet_read_count = 0; rbu_data.num_packets = 0; rbu_data.packetsize = 0; + rbu_data.imagesize = 0; } -static int fill_last_packet(void *data, size_t length) -{ - struct list_head *ptemp_list; - struct packet_data *packet = NULL; - int packet_count = 0; - - pr_debug("fill_last_packet: entry \n"); - - if (!rbu_data.num_packets) { - pr_debug("fill_last_packet: num_packets=0\n"); - return -ENOMEM; - } - - packet_count = rbu_data.num_packets; - - ptemp_list = (&packet_data_head.list)->prev; - - packet = list_entry(ptemp_list, struct packet_data, list); - - if ((rbu_data.packet_write_count + length) > rbu_data.packetsize) { - pr_debug("dell_rbu:%s: packet size data " - "overrun\n", __FUNCTION__); - return -EINVAL; - } - - pr_debug("fill_last_packet : buffer = %p\n", packet->data); - - memcpy((packet->data + rbu_data.packet_write_count), data, length); - - if ((rbu_data.packet_write_count + length) == rbu_data.packetsize) { - /* - * this was the last data chunk in the packet - * so reinitialize the packet data counter to zero - */ - rbu_data.packet_write_count = 0; - } else - rbu_data.packet_write_count += length; - - pr_debug("fill_last_packet: exit \n"); - return 0; -} - -static int create_packet(size_t length) +static int create_packet(void *data, size_t length) { struct packet_data *newpacket; int ordernum = 0; @@ -146,12 +106,14 @@ static int create_packet(size_t length) pr_debug("create_packet: packetsize not specified\n"); return -EINVAL; } + spin_unlock(&rbu_data.lock); + newpacket = kmalloc(sizeof (struct packet_data), GFP_KERNEL); + spin_lock(&rbu_data.lock); - newpacket = kmalloc(sizeof(struct packet_data), GFP_KERNEL); if (!newpacket) { printk(KERN_WARNING - "dell_rbu:%s: failed to allocate new " - "packet\n", __FUNCTION__); + "dell_rbu:%s: failed to allocate new " + "packet\n", __FUNCTION__); return -ENOMEM; } @@ -160,15 +122,17 @@ static int create_packet(size_t length) * there is no upper limit on memory * address for packetized mechanism */ - newpacket->data = (unsigned char *)__get_free_pages(GFP_KERNEL, - ordernum); + spin_unlock(&rbu_data.lock); + newpacket->data = (unsigned char *) __get_free_pages(GFP_KERNEL, + ordernum); + spin_lock(&rbu_data.lock); pr_debug("create_packet: newpacket %p\n", newpacket->data); if (!newpacket->data) { printk(KERN_WARNING - "dell_rbu:%s: failed to allocate new " - "packet\n", __FUNCTION__); + "dell_rbu:%s: failed to allocate new " + "packet\n", __FUNCTION__); kfree(newpacket); return -ENOMEM; } @@ -181,9 +145,11 @@ static int create_packet(size_t length) INIT_LIST_HEAD(&newpacket->list); list_add_tail(&newpacket->list, &packet_data_head.list); /* - * packets have fixed size + * packets may not have fixed size */ - newpacket->length = rbu_data.packetsize; + newpacket->length = length; + + memcpy(newpacket->data, data, length); pr_debug("create_packet: exit \n"); @@ -193,20 +159,43 @@ static int create_packet(size_t length) static int packetize_data(void *data, size_t length) { int rc = 0; + int done = 0; + int packet_length; + u8 *temp; + u8 *end = (u8 *) data + length; + pr_debug("packetize_data: data length %d\n", length); + if (!rbu_data.packetsize) { + printk(KERN_WARNING + "dell_rbu: packetsize not specified\n"); + return -EIO; + } + + temp = (u8 *) data; - if (!rbu_data.packet_write_count) { - if ((rc = create_packet(length))) + /* packetize the hunk */ + while (!done) { + if ((temp + rbu_data.packetsize) < end) + packet_length = rbu_data.packetsize; + else { + /* this is the last packet */ + packet_length = end - temp; + done = 1; + } + + if ((rc = create_packet(temp, packet_length))) return rc; + + pr_debug("%lu:%lu\n", temp, (end - temp)); + temp += packet_length; } - if ((rc = fill_last_packet(data, length))) - return rc; + + rbu_data.imagesize = length; return rc; } -static int -do_packet_read(char *data, struct list_head *ptemp_list, - int length, int bytes_read, int *list_read_count) +static int do_packet_read(char *data, struct list_head *ptemp_list, + int length, int bytes_read, int *list_read_count) { void *ptemp_buf; struct packet_data *newpacket = NULL; @@ -258,8 +247,7 @@ static int packet_read_list(char *data, size_t * pread_length) ptemp_list = (&packet_data_head.list)->next; while (!list_empty(ptemp_list)) { bytes_copied = do_packet_read(pdest, ptemp_list, - remaining_bytes, bytes_read, - &temp_count); + remaining_bytes, bytes_read, &temp_count); remaining_bytes -= bytes_copied; bytes_read += bytes_copied; pdest += bytes_copied; @@ -287,7 +275,7 @@ static void packet_empty_list(void) ptemp_list = (&packet_data_head.list)->next; while (!list_empty(ptemp_list)) { newpacket = - list_entry(ptemp_list, struct packet_data, list); + list_entry(ptemp_list, struct packet_data, list); pnext_list = ptemp_list->next; list_del(ptemp_list); ptemp_list = pnext_list; @@ -296,14 +284,13 @@ static void packet_empty_list(void) * to make sure there are no stale RBU packets left in memory */ memset(newpacket->data, 0, rbu_data.packetsize); - free_pages((unsigned long)newpacket->data, - newpacket->ordernum); + free_pages((unsigned long) newpacket->data, + newpacket->ordernum); kfree(newpacket); } - rbu_data.packet_write_count = 0; rbu_data.packet_read_count = 0; rbu_data.num_packets = 0; - rbu_data.packetsize = 0; + rbu_data.imagesize = 0; } /* @@ -319,14 +306,13 @@ static void img_update_free(void) * BIOS image copied in memory. */ memset(rbu_data.image_update_buffer, 0, - rbu_data.image_update_buffer_size); + rbu_data.image_update_buffer_size); if (rbu_data.dma_alloc == 1) dma_free_coherent(NULL, rbu_data.bios_image_size, - rbu_data.image_update_buffer, - dell_rbu_dmaaddr); + rbu_data.image_update_buffer, dell_rbu_dmaaddr); else - free_pages((unsigned long)rbu_data.image_update_buffer, - rbu_data.image_update_ordernum); + free_pages((unsigned long) rbu_data.image_update_buffer, + rbu_data.image_update_ordernum); /* * Re-initialize the rbu_data variables after a free @@ -366,7 +352,7 @@ static int img_update_realloc(unsigned long size) */ if ((size != 0) && (rbu_data.image_update_buffer == NULL)) { printk(KERN_ERR "dell_rbu:%s: corruption " - "check failed\n", __FUNCTION__); + "check failed\n", __FUNCTION__); return -EINVAL; } /* @@ -385,17 +371,16 @@ static int img_update_realloc(unsigned long size) ordernum = get_order(size); image_update_buffer = - (unsigned char *)__get_free_pages(GFP_KERNEL, ordernum); + (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum); img_buf_phys_addr = - (unsigned long)virt_to_phys(image_update_buffer); + (unsigned long) virt_to_phys(image_update_buffer); if (img_buf_phys_addr > BIOS_SCAN_LIMIT) { - free_pages((unsigned long)image_update_buffer, ordernum); + free_pages((unsigned long) image_update_buffer, ordernum); ordernum = -1; image_update_buffer = dma_alloc_coherent(NULL, size, - &dell_rbu_dmaaddr, - GFP_KERNEL); + &dell_rbu_dmaaddr, GFP_KERNEL); dma_alloc = 1; } @@ -405,13 +390,13 @@ static int img_update_realloc(unsigned long size) rbu_data.image_update_buffer = image_update_buffer; rbu_data.image_update_buffer_size = size; rbu_data.bios_image_size = - rbu_data.image_update_buffer_size; + rbu_data.image_update_buffer_size; rbu_data.image_update_ordernum = ordernum; rbu_data.dma_alloc = dma_alloc; rc = 0; } else { pr_debug("Not enough memory for image update:" - "size = %ld\n", size); + "size = %ld\n", size); rc = -ENOMEM; } @@ -424,7 +409,6 @@ static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count) size_t bytes_left; size_t data_length; char *ptempBuf = buffer; - unsigned long imagesize; /* check to see if we have something to return */ if (rbu_data.num_packets == 0) { @@ -433,22 +417,20 @@ static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count) goto read_rbu_data_exit; } - imagesize = rbu_data.num_packets * rbu_data.packetsize; - - if (pos > imagesize) { + if (pos > rbu_data.imagesize) { retval = 0; printk(KERN_WARNING "dell_rbu:read_packet_data: " - "data underrun\n"); + "data underrun\n"); goto read_rbu_data_exit; } - bytes_left = imagesize - pos; + bytes_left = rbu_data.imagesize - pos; data_length = min(bytes_left, count); if ((retval = packet_read_list(ptempBuf, &data_length)) < 0) goto read_rbu_data_exit; - if ((pos + count) > imagesize) { + if ((pos + count) > rbu_data.imagesize) { rbu_data.packet_read_count = 0; /* this was the last copy */ retval = bytes_left; @@ -468,11 +450,11 @@ static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count) /* check to see if we have something to return */ if ((rbu_data.image_update_buffer == NULL) || - (rbu_data.bios_image_size == 0)) { + (rbu_data.bios_image_size == 0)) { pr_debug("read_rbu_data_mono: image_update_buffer %p ," - "bios_image_size %lu\n", - rbu_data.image_update_buffer, - rbu_data.bios_image_size); + "bios_image_size %lu\n", + rbu_data.image_update_buffer, + rbu_data.bios_image_size); ret_count = -ENOMEM; goto read_rbu_data_exit; } @@ -497,8 +479,8 @@ static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count) return ret_count; } -static ssize_t -read_rbu_data(struct kobject *kobj, char *buffer, loff_t pos, size_t count) +static ssize_t read_rbu_data(struct kobject *kobj, char *buffer, + loff_t pos, size_t count) { ssize_t ret_count = 0; @@ -515,9 +497,49 @@ read_rbu_data(struct kobject *kobj, char *buffer, loff_t pos, size_t count) return ret_count; } -static ssize_t -read_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos, - size_t count) +static void callbackfn_rbu(const struct firmware *fw, void *context) +{ + int rc = 0; + + if (!fw || !fw->size) { + rbu_data.entry_created = 0; + return; + } + + spin_lock(&rbu_data.lock); + if (!strcmp(image_type, "mono")) { + if (!img_update_realloc(fw->size)) + memcpy(rbu_data.image_update_buffer, + fw->data, fw->size); + } else if (!strcmp(image_type, "packet")) { + /* + * we need to free previous packets if a + * new hunk of packets needs to be downloaded + */ + packet_empty_list(); + if (packetize_data(fw->data, fw->size)) + /* Incase something goes wrong when we are + * in middle of packetizing the data, we + * need to free up whatever packets might + * have been created before we quit. + */ + packet_empty_list(); + } else + pr_debug("invalid image type specified.\n"); + spin_unlock(&rbu_data.lock); + + rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, + "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu); + if (rc) + printk(KERN_ERR + "dell_rbu:%s request_firmware_nowait failed" + " %d\n", __FUNCTION__, rc); + else + rbu_data.entry_created = 1; +} + +static ssize_t read_rbu_image_type(struct kobject *kobj, char *buffer, + loff_t pos, size_t count) { int size = 0; if (!pos) @@ -525,27 +547,90 @@ read_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos, return size; } -static ssize_t -write_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos, - size_t count) +static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer, + loff_t pos, size_t count) { int rc = count; + int req_firm_rc = 0; + int i; spin_lock(&rbu_data.lock); - - if (strlen(buffer) < MAX_IMAGE_LENGTH) - sscanf(buffer, "%s", image_type); - else - printk(KERN_WARNING "dell_rbu: image_type is invalid" - "max chars = %d, \n incoming str--%s-- \n", - MAX_IMAGE_LENGTH, buffer); + /* + * Find the first newline or space + */ + for (i = 0; i < count; ++i) + if (buffer[i] == '\n' || buffer[i] == ' ') { + buffer[i] = '\0'; + break; + } + if (i == count) + buffer[count] = '\0'; + + if (strstr(buffer, "mono")) + strcpy(image_type, "mono"); + else if (strstr(buffer, "packet")) + strcpy(image_type, "packet"); + else if (strstr(buffer, "init")) { + /* + * If due to the user error the driver gets in a bad + * state where even though it is loaded , the + * /sys/class/firmware/dell_rbu entries are missing. + * to cover this situation the user can recreate entries + * by writing init to image_type. + */ + if (!rbu_data.entry_created) { + spin_unlock(&rbu_data.lock); + req_firm_rc = request_firmware_nowait(THIS_MODULE, + FW_ACTION_NOHOTPLUG, "dell_rbu", + &rbu_device->dev, &context, + callbackfn_rbu); + if (req_firm_rc) { + printk(KERN_ERR + "dell_rbu:%s request_firmware_nowait" + " failed %d\n", __FUNCTION__, rc); + rc = -EIO; + } else + rbu_data.entry_created = 1; + + spin_lock(&rbu_data.lock); + } + } else { + printk(KERN_WARNING "dell_rbu: image_type is invalid\n"); + spin_unlock(&rbu_data.lock); + return -EINVAL; + } /* we must free all previous allocations */ packet_empty_list(); img_update_free(); - spin_unlock(&rbu_data.lock); + return rc; +} +static ssize_t read_rbu_packet_size(struct kobject *kobj, char *buffer, + loff_t pos, size_t count) +{ + int size = 0; + if (!pos) { + spin_lock(&rbu_data.lock); + size = sprintf(buffer, "%lu\n", rbu_data.packetsize); + spin_unlock(&rbu_data.lock); + } + return size; +} + +static ssize_t write_rbu_packet_size(struct kobject *kobj, char *buffer, + loff_t pos, size_t count) +{ + unsigned long temp; + spin_lock(&rbu_data.lock); + packet_empty_list(); + sscanf(buffer, "%lu", &temp); + if (temp < 0xffffffff) + rbu_data.packetsize = temp; + + spin_unlock(&rbu_data.lock); + return count; } static struct bin_attribute rbu_data_attr = { @@ -559,38 +644,11 @@ static struct bin_attribute rbu_image_type_attr = { .write = write_rbu_image_type, }; -static void callbackfn_rbu(const struct firmware *fw, void *context) -{ - int rc = 0; - - if (!fw || !fw->size) - return; - - spin_lock(&rbu_data.lock); - if (!strcmp(image_type, "mono")) { - if (!img_update_realloc(fw->size)) - memcpy(rbu_data.image_update_buffer, - fw->data, fw->size); - } else if (!strcmp(image_type, "packet")) { - if (!rbu_data.packetsize) - rbu_data.packetsize = fw->size; - else if (rbu_data.packetsize != fw->size) { - packet_empty_list(); - rbu_data.packetsize = fw->size; - } - packetize_data(fw->data, fw->size); - } else - pr_debug("invalid image type specified.\n"); - spin_unlock(&rbu_data.lock); - - rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, - "dell_rbu", &rbu_device->dev, - &context, callbackfn_rbu); - if (rc) - printk(KERN_ERR - "dell_rbu:%s request_firmware_nowait failed" - " %d\n", __FUNCTION__, rc); -} +static struct bin_attribute rbu_packet_size_attr = { + .attr = {.name = "packet_size",.owner = THIS_MODULE,.mode = 0644}, + .read = read_rbu_packet_size, + .write = write_rbu_packet_size, +}; static int __init dcdrbu_init(void) { @@ -599,23 +657,26 @@ static int __init dcdrbu_init(void) init_packet_head(); rbu_device = - platform_device_register_simple("dell_rbu", -1, NULL, 0); + platform_device_register_simple("dell_rbu", -1, NULL, 0); if (!rbu_device) { printk(KERN_ERR - "dell_rbu:%s:platform_device_register_simple " - "failed\n", __FUNCTION__); + "dell_rbu:%s:platform_device_register_simple " + "failed\n", __FUNCTION__); return -EIO; } sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr); sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr); + sysfs_create_bin_file(&rbu_device->dev.kobj, + &rbu_packet_size_attr); rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, - "dell_rbu", &rbu_device->dev, - &context, callbackfn_rbu); + "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu); if (rc) printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait" - " failed %d\n", __FUNCTION__, rc); + " failed %d\n", __FUNCTION__, rc); + else + rbu_data.entry_created = 1; return rc; |