include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / firmware / dell_rbu.c
index b667823..3a44602 100644 (file)
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */
-#include <linux/version.h>
-#include <linux/config.h>
 #include <linux/init.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/errno.h>
 #include <linux/blkdev.h>
-#include <linux/device.h>
+#include <linux/platform_device.h>
 #include <linux/spinlock.h>
 #include <linux/moduleparam.h>
 #include <linux/firmware.h>
@@ -50,7 +49,7 @@
 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
 MODULE_LICENSE("GPL");
-MODULE_VERSION("2.0");
+MODULE_VERSION("3.2");
 
 #define BIOS_SCAN_LIMIT 0xffffffff
 #define MAX_IMAGE_LENGTH 16
@@ -62,15 +61,21 @@ 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 + 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");
+MODULE_PARM_DESC(image_type,
+       "BIOS image type. choose- mono or packet or init");
+
+static unsigned long allocation_floor = 0x100000;
+module_param(allocation_floor, ulong, 0644);
+MODULE_PARM_DESC(allocation_floor,
+    "Minimum address for allocations when using Packet mode");
 
 struct packet_data {
        struct list_head list;
@@ -88,123 +93,168 @@ 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;
+       int retval = 0;
+       unsigned int packet_array_size = 0;
+       void **invalid_addr_packet_array = NULL;
+       void *packet_data_temp_buf = NULL;
+       unsigned int idx = 0;
 
        pr_debug("create_packet: entry \n");
 
        if (!rbu_data.packetsize) {
                pr_debug("create_packet: packetsize not specified\n");
-               return -EINVAL;
+               retval = -EINVAL;
+               goto out_noalloc;
        }
+
        spin_unlock(&rbu_data.lock);
-       newpacket = kmalloc(sizeof (struct packet_data), GFP_KERNEL);
-       spin_lock(&rbu_data.lock);
+
+       newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
 
        if (!newpacket) {
                printk(KERN_WARNING
                        "dell_rbu:%s: failed to allocate new "
-                       "packet\n", __FUNCTION__);
-               return -ENOMEM;
+                       "packet\n", __func__);
+               retval = -ENOMEM;
+               spin_lock(&rbu_data.lock);
+               goto out_noalloc;
        }
 
        ordernum = get_order(length);
+
        /*
-        * there is no upper limit on memory
-        * address for packetized mechanism
+        * BIOS errata mean we cannot allocate packets below 1MB or they will
+        * be overwritten by BIOS.
+        *
+        * array to temporarily hold packets
+        * that are below the allocation floor
+        *
+        * NOTE: very simplistic because we only need the floor to be at 1MB
+        *       due to BIOS errata. This shouldn't be used for higher floors
+        *       or you will run out of mem trying to allocate the array.
         */
-       spin_unlock(&rbu_data.lock);
-       newpacket->data = (unsigned char *) __get_free_pages(GFP_KERNEL,
-               ordernum);
-       spin_lock(&rbu_data.lock);
+       packet_array_size = max(
+                       (unsigned int)(allocation_floor / rbu_data.packetsize),
+                       (unsigned int)1);
+       invalid_addr_packet_array = kzalloc(packet_array_size * sizeof(void*),
+                                               GFP_KERNEL);
 
-       pr_debug("create_packet: newpacket %p\n", newpacket->data);
-
-       if (!newpacket->data) {
+       if (!invalid_addr_packet_array) {
                printk(KERN_WARNING
-                       "dell_rbu:%s: failed to allocate new "
-                       "packet\n", __FUNCTION__);
-               kfree(newpacket);
-               return -ENOMEM;
+                       "dell_rbu:%s: failed to allocate "
+                       "invalid_addr_packet_array \n",
+                       __func__);
+               retval = -ENOMEM;
+               spin_lock(&rbu_data.lock);
+               goto out_alloc_packet;
+       }
+
+       while (!packet_data_temp_buf) {
+               packet_data_temp_buf = (unsigned char *)
+                       __get_free_pages(GFP_KERNEL, ordernum);
+               if (!packet_data_temp_buf) {
+                       printk(KERN_WARNING
+                               "dell_rbu:%s: failed to allocate new "
+                               "packet\n", __func__);
+                       retval = -ENOMEM;
+                       spin_lock(&rbu_data.lock);
+                       goto out_alloc_packet_array;
+               }
+
+               if ((unsigned long)virt_to_phys(packet_data_temp_buf)
+                               < allocation_floor) {
+                       pr_debug("packet 0x%lx below floor at 0x%lx.\n",
+                                       (unsigned long)virt_to_phys(
+                                               packet_data_temp_buf),
+                                       allocation_floor);
+                       invalid_addr_packet_array[idx++] = packet_data_temp_buf;
+                       packet_data_temp_buf = NULL;
+               }
        }
+       spin_lock(&rbu_data.lock);
+
+       newpacket->data = packet_data_temp_buf;
+
+       pr_debug("create_packet: newpacket at physical addr %lx\n",
+               (unsigned long)virt_to_phys(newpacket->data));
 
+       /* packets may not have fixed size */
+       newpacket->length = length;
        newpacket->ordernum = ordernum;
        ++rbu_data.num_packets;
-       /*
-        * initialize the newly created packet headers
-        */
+
+       /* initialize the newly created packet headers */
        INIT_LIST_HEAD(&newpacket->list);
        list_add_tail(&newpacket->list, &packet_data_head.list);
-       /*
-        * packets have fixed size
-        */
-       newpacket->length = rbu_data.packetsize;
+
+       memcpy(newpacket->data, data, length);
 
        pr_debug("create_packet: exit \n");
 
-       return 0;
+out_alloc_packet_array:
+       /* always free packet array */
+       for (;idx>0;idx--) {
+               pr_debug("freeing unused packet below floor 0x%lx.\n",
+                       (unsigned long)virt_to_phys(
+                               invalid_addr_packet_array[idx-1]));
+               free_pages((unsigned long)invalid_addr_packet_array[idx-1],
+                       ordernum);
+       }
+       kfree(invalid_addr_packet_array);
+
+out_alloc_packet:
+       /* if error, free data */
+       if (retval)
+               kfree(newpacket);
+
+out_noalloc:
+       return retval;
 }
 
-static int packetize_data(void *data, size_t length)
+static int packetize_data(const u8 *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 %zd\n", length);
+       if (!rbu_data.packetsize) {
+               printk(KERN_WARNING
+                       "dell_rbu: packetsize not specified\n");
+               return -EIO;
+       }
+
+       temp = (u8 *) data;
+
+       /* 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 (!rbu_data.packet_write_count) {
-               if ((rc = create_packet(length)))
+               if ((rc = create_packet(temp, packet_length)))
                        return rc;
+
+               pr_debug("%p:%td\n", temp, (end - temp));
+               temp += packet_length;
        }
-       if ((rc = fill_last_packet(data, length)))
-               return rc;
+
+       rbu_data.imagesize = length;
 
        return rc;
 }
@@ -243,7 +293,7 @@ static int do_packet_read(char *data, struct list_head *ptemp_list,
        return bytes_copied;
 }
 
-static int packet_read_list(char *data, size_t *pread_length)
+static int packet_read_list(char *data, size_t * pread_length)
 {
        struct list_head *ptemp_list;
        int temp_count = 0;
@@ -303,10 +353,9 @@ static void packet_empty_list(void)
                        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;
 }
 
 /*
@@ -368,7 +417,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", __func__);
                        return -EINVAL;
                }
                /*
@@ -425,7 +474,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) {
@@ -434,22 +482,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");
                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;
@@ -462,11 +508,6 @@ static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
 
 static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
 {
-       unsigned char *ptemp = NULL;
-       size_t bytes_left = 0;
-       size_t data_length = 0;
-       ssize_t ret_count = 0;
-
        /* check to see if we have something to return */
        if ((rbu_data.image_update_buffer == NULL) ||
                (rbu_data.bios_image_size == 0)) {
@@ -474,32 +515,16 @@ static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
                        "bios_image_size %lu\n",
                        rbu_data.image_update_buffer,
                        rbu_data.bios_image_size);
-               ret_count = -ENOMEM;
-               goto read_rbu_data_exit;
-       }
-
-       if (pos > rbu_data.bios_image_size) {
-               ret_count = 0;
-               goto read_rbu_data_exit;
+               return -ENOMEM;
        }
 
-       bytes_left = rbu_data.bios_image_size - pos;
-       data_length = min(bytes_left, count);
-
-       ptemp = rbu_data.image_update_buffer;
-       memcpy(buffer, (ptemp + pos), data_length);
-
-       if ((pos + count) > rbu_data.bios_image_size)
-               /* this was the last copy */
-               ret_count = bytes_left;
-       else
-               ret_count = count;
-      read_rbu_data_exit:
-       return ret_count;
+       return memory_read_from_buffer(buffer, count, &pos,
+                       rbu_data.image_update_buffer, rbu_data.bios_image_size);
 }
 
-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,
+                            struct bin_attribute *bin_attr,
+                            char *buffer, loff_t pos, size_t count)
 {
        ssize_t ret_count = 0;
 
@@ -518,12 +543,13 @@ static ssize_t read_rbu_data(struct kobject *kobj, char *buffer,
 
 static void callbackfn_rbu(const struct firmware *fw, void *context)
 {
-       int rc = 0;
+       rbu_data.entry_created = 0;
 
-       if (!fw || !fw->size) {
-               rbu_data.entry_created = 0;
+       if (!fw)
                return;
-       }
+
+       if (!fw->size)
+               goto out;
 
        spin_lock(&rbu_data.lock);
        if (!strcmp(image_type, "mono")) {
@@ -531,38 +557,38 @@ static void callbackfn_rbu(const struct firmware *fw, void *context)
                        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) {
+               /*
+                * 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();
-                       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);
-       else
-               rbu_data.entry_created = 1;
+ out:
+       release_firmware(fw);
 }
 
-static ssize_t read_rbu_image_type(struct kobject *kobj, char *buffer,
-                       loff_t pos, size_t count)
+static ssize_t read_rbu_image_type(struct kobject *kobj,
+                                  struct bin_attribute *bin_attr,
+                                  char *buffer, loff_t pos, size_t count)
 {
        int size = 0;
        if (!pos)
-               size = sprintf(buffer, "%s\n", image_type);
+               size = scnprintf(buffer, count, "%s\n", image_type);
        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,
+                                   struct bin_attribute *bin_attr,
+                                   char *buffer, loff_t pos, size_t count)
 {
        int rc = count;
        int req_firm_rc = 0;
@@ -595,12 +621,12 @@ static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer,
                        spin_unlock(&rbu_data.lock);
                        req_firm_rc = request_firmware_nowait(THIS_MODULE,
                                FW_ACTION_NOHOTPLUG, "dell_rbu",
-                               &rbu_device->dev, &context,
+                               &rbu_device->dev, GFP_KERNEL, &context,
                                callbackfn_rbu);
                        if (req_firm_rc) {
                                printk(KERN_ERR
                                        "dell_rbu:%s request_firmware_nowait"
-                                       " failed %d\n", __FUNCTION__, rc);
+                                       " failed %d\n", __func__, rc);
                                rc = -EIO;
                        } else
                                rbu_data.entry_created = 1;
@@ -621,53 +647,86 @@ static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer,
        return rc;
 }
 
+static ssize_t read_rbu_packet_size(struct kobject *kobj,
+                                   struct bin_attribute *bin_attr,
+                                   char *buffer, loff_t pos, size_t count)
+{
+       int size = 0;
+       if (!pos) {
+               spin_lock(&rbu_data.lock);
+               size = scnprintf(buffer, count, "%lu\n", rbu_data.packetsize);
+               spin_unlock(&rbu_data.lock);
+       }
+       return size;
+}
+
+static ssize_t write_rbu_packet_size(struct kobject *kobj,
+                                    struct bin_attribute *bin_attr,
+                                    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 = {
-       .attr = {
-               .name = "data",
-               .owner = THIS_MODULE,
-               .mode = 0444,
-       },
+       .attr = {.name = "data", .mode = 0444},
        .read = read_rbu_data,
 };
 
 static struct bin_attribute rbu_image_type_attr = {
-       .attr = {
-               .name = "image_type",
-               .owner = THIS_MODULE,
-               .mode = 0644,
-       },
+       .attr = {.name = "image_type", .mode = 0644},
        .read = read_rbu_image_type,
        .write = write_rbu_image_type,
 };
 
+static struct bin_attribute rbu_packet_size_attr = {
+       .attr = {.name = "packet_size", .mode = 0644},
+       .read = read_rbu_packet_size,
+       .write = write_rbu_packet_size,
+};
+
 static int __init dcdrbu_init(void)
 {
-       int rc = 0;
+       int rc;
        spin_lock_init(&rbu_data.lock);
 
        init_packet_head();
-       rbu_device =
-               platform_device_register_simple("dell_rbu", -1, NULL, 0);
-       if (!rbu_device) {
+       rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0);
+       if (IS_ERR(rbu_device)) {
                printk(KERN_ERR
                        "dell_rbu:%s:platform_device_register_simple "
-                       "failed\n", __FUNCTION__);
-               return -EIO;
+                       "failed\n", __func__);
+               return PTR_ERR(rbu_device);
        }
 
-       sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
-       sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
-
-       rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
-               "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
+       rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
        if (rc)
-               printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait"
-                       " failed %d\n", __FUNCTION__, rc);
-       else
-               rbu_data.entry_created = 1;
+               goto out_devreg;
+       rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
+       if (rc)
+               goto out_data;
+       rc = sysfs_create_bin_file(&rbu_device->dev.kobj,
+               &rbu_packet_size_attr);
+       if (rc)
+               goto out_imtype;
 
-       return rc;
+       rbu_data.entry_created = 0;
+       return 0;
 
+out_imtype:
+       sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
+out_data:
+       sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
+out_devreg:
+       platform_device_unregister(rbu_device);
+       return rc;
 }
 
 static __exit void dcdrbu_exit(void)
@@ -681,3 +740,6 @@ static __exit void dcdrbu_exit(void)
 
 module_exit(dcdrbu_exit);
 module_init(dcdrbu_init);
+
+/* vim:noet:ts=8:sw=8
+*/