b667823982581db99206b15cd6a4fefdb8660ed2
[linux-2.6.git] / drivers / firmware / dell_rbu.c
1 /*
2  * dell_rbu.c
3  * Bios Update driver for Dell systems
4  * Author: Dell Inc
5  *         Abhay Salunke <abhay_salunke@dell.com>
6  *
7  * Copyright (C) 2005 Dell Inc.
8  *
9  * Remote BIOS Update (rbu) driver is used for updating DELL BIOS by
10  * creating entries in the /sys file systems on Linux 2.6 and higher
11  * kernels. The driver supports two mechanism to update the BIOS namely
12  * contiguous and packetized. Both these methods still require having some
13  * application to set the CMOS bit indicating the BIOS to update itself
14  * after a reboot.
15  *
16  * Contiguous method:
17  * This driver writes the incoming data in a monolithic image by allocating
18  * contiguous physical pages large enough to accommodate the incoming BIOS
19  * image size.
20  *
21  * Packetized method:
22  * The driver writes the incoming packet image by allocating a new packet
23  * on every time the packet data is written. This driver requires an
24  * application to break the BIOS image in to fixed sized packet chunks.
25  *
26  * See Documentation/dell_rbu.txt for more info.
27  *
28  * This program is free software; you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License v2.0 as published by
30  * the Free Software Foundation
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  */
37 #include <linux/version.h>
38 #include <linux/config.h>
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/blkdev.h>
44 #include <linux/device.h>
45 #include <linux/spinlock.h>
46 #include <linux/moduleparam.h>
47 #include <linux/firmware.h>
48 #include <linux/dma-mapping.h>
49
50 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
51 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
52 MODULE_LICENSE("GPL");
53 MODULE_VERSION("2.0");
54
55 #define BIOS_SCAN_LIMIT 0xffffffff
56 #define MAX_IMAGE_LENGTH 16
57 static struct _rbu_data {
58         void *image_update_buffer;
59         unsigned long image_update_buffer_size;
60         unsigned long bios_image_size;
61         int image_update_ordernum;
62         int dma_alloc;
63         spinlock_t lock;
64         unsigned long packet_read_count;
65         unsigned long packet_write_count;
66         unsigned long num_packets;
67         unsigned long packetsize;
68         int entry_created;
69 } rbu_data;
70
71 static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";
72 module_param_string(image_type, image_type, sizeof (image_type), 0);
73 MODULE_PARM_DESC(image_type, "BIOS image type. choose- mono or packet");
74
75 struct packet_data {
76         struct list_head list;
77         size_t length;
78         void *data;
79         int ordernum;
80 };
81
82 static struct packet_data packet_data_head;
83
84 static struct platform_device *rbu_device;
85 static int context;
86 static dma_addr_t dell_rbu_dmaaddr;
87
88 static void init_packet_head(void)
89 {
90         INIT_LIST_HEAD(&packet_data_head.list);
91         rbu_data.packet_write_count = 0;
92         rbu_data.packet_read_count = 0;
93         rbu_data.num_packets = 0;
94         rbu_data.packetsize = 0;
95 }
96
97 static int fill_last_packet(void *data, size_t length)
98 {
99         struct list_head *ptemp_list;
100         struct packet_data *packet = NULL;
101         int packet_count = 0;
102
103         pr_debug("fill_last_packet: entry \n");
104
105         if (!rbu_data.num_packets) {
106                 pr_debug("fill_last_packet: num_packets=0\n");
107                 return -ENOMEM;
108         }
109
110         packet_count = rbu_data.num_packets;
111
112         ptemp_list = (&packet_data_head.list)->prev;
113
114         packet = list_entry(ptemp_list, struct packet_data, list);
115
116         if ((rbu_data.packet_write_count + length) > rbu_data.packetsize) {
117                 pr_debug("dell_rbu:%s: packet size data "
118                         "overrun\n", __FUNCTION__);
119                 return -EINVAL;
120         }
121
122         pr_debug("fill_last_packet : buffer = %p\n", packet->data);
123
124         memcpy((packet->data + rbu_data.packet_write_count), data, length);
125
126         if ((rbu_data.packet_write_count + length) == rbu_data.packetsize) {
127                 /*
128                  * this was the last data chunk in the packet
129                  * so reinitialize the packet data counter to zero
130                  */
131                 rbu_data.packet_write_count = 0;
132         } else
133                 rbu_data.packet_write_count += length;
134
135         pr_debug("fill_last_packet: exit \n");
136         return 0;
137 }
138
139 static int create_packet(size_t length)
140 {
141         struct packet_data *newpacket;
142         int ordernum = 0;
143
144         pr_debug("create_packet: entry \n");
145
146         if (!rbu_data.packetsize) {
147                 pr_debug("create_packet: packetsize not specified\n");
148                 return -EINVAL;
149         }
150         spin_unlock(&rbu_data.lock);
151         newpacket = kmalloc(sizeof (struct packet_data), GFP_KERNEL);
152         spin_lock(&rbu_data.lock);
153
154         if (!newpacket) {
155                 printk(KERN_WARNING
156                         "dell_rbu:%s: failed to allocate new "
157                         "packet\n", __FUNCTION__);
158                 return -ENOMEM;
159         }
160
161         ordernum = get_order(length);
162         /*
163          * there is no upper limit on memory
164          * address for packetized mechanism
165          */
166         spin_unlock(&rbu_data.lock);
167         newpacket->data = (unsigned char *) __get_free_pages(GFP_KERNEL,
168                 ordernum);
169         spin_lock(&rbu_data.lock);
170
171         pr_debug("create_packet: newpacket %p\n", newpacket->data);
172
173         if (!newpacket->data) {
174                 printk(KERN_WARNING
175                         "dell_rbu:%s: failed to allocate new "
176                         "packet\n", __FUNCTION__);
177                 kfree(newpacket);
178                 return -ENOMEM;
179         }
180
181         newpacket->ordernum = ordernum;
182         ++rbu_data.num_packets;
183         /*
184          * initialize the newly created packet headers
185          */
186         INIT_LIST_HEAD(&newpacket->list);
187         list_add_tail(&newpacket->list, &packet_data_head.list);
188         /*
189          * packets have fixed size
190          */
191         newpacket->length = rbu_data.packetsize;
192
193         pr_debug("create_packet: exit \n");
194
195         return 0;
196 }
197
198 static int packetize_data(void *data, size_t length)
199 {
200         int rc = 0;
201
202         if (!rbu_data.packet_write_count) {
203                 if ((rc = create_packet(length)))
204                         return rc;
205         }
206         if ((rc = fill_last_packet(data, length)))
207                 return rc;
208
209         return rc;
210 }
211
212 static int do_packet_read(char *data, struct list_head *ptemp_list,
213         int length, int bytes_read, int *list_read_count)
214 {
215         void *ptemp_buf;
216         struct packet_data *newpacket = NULL;
217         int bytes_copied = 0;
218         int j = 0;
219
220         newpacket = list_entry(ptemp_list, struct packet_data, list);
221         *list_read_count += newpacket->length;
222
223         if (*list_read_count > bytes_read) {
224                 /* point to the start of unread data */
225                 j = newpacket->length - (*list_read_count - bytes_read);
226                 /* point to the offset in the packet buffer */
227                 ptemp_buf = (u8 *) newpacket->data + j;
228                 /*
229                  * check if there is enough room in
230                  * * the incoming buffer
231                  */
232                 if (length > (*list_read_count - bytes_read))
233                         /*
234                          * copy what ever is there in this
235                          * packet and move on
236                          */
237                         bytes_copied = (*list_read_count - bytes_read);
238                 else
239                         /* copy the remaining */
240                         bytes_copied = length;
241                 memcpy(data, ptemp_buf, bytes_copied);
242         }
243         return bytes_copied;
244 }
245
246 static int packet_read_list(char *data, size_t *pread_length)
247 {
248         struct list_head *ptemp_list;
249         int temp_count = 0;
250         int bytes_copied = 0;
251         int bytes_read = 0;
252         int remaining_bytes = 0;
253         char *pdest = data;
254
255         /* check if we have any packets */
256         if (0 == rbu_data.num_packets)
257                 return -ENOMEM;
258
259         remaining_bytes = *pread_length;
260         bytes_read = rbu_data.packet_read_count;
261
262         ptemp_list = (&packet_data_head.list)->next;
263         while (!list_empty(ptemp_list)) {
264                 bytes_copied = do_packet_read(pdest, ptemp_list,
265                         remaining_bytes, bytes_read, &temp_count);
266                 remaining_bytes -= bytes_copied;
267                 bytes_read += bytes_copied;
268                 pdest += bytes_copied;
269                 /*
270                  * check if we reached end of buffer before reaching the
271                  * last packet
272                  */
273                 if (remaining_bytes == 0)
274                         break;
275
276                 ptemp_list = ptemp_list->next;
277         }
278         /*finally set the bytes read */
279         *pread_length = bytes_read - rbu_data.packet_read_count;
280         rbu_data.packet_read_count = bytes_read;
281         return 0;
282 }
283
284 static void packet_empty_list(void)
285 {
286         struct list_head *ptemp_list;
287         struct list_head *pnext_list;
288         struct packet_data *newpacket;
289
290         ptemp_list = (&packet_data_head.list)->next;
291         while (!list_empty(ptemp_list)) {
292                 newpacket =
293                         list_entry(ptemp_list, struct packet_data, list);
294                 pnext_list = ptemp_list->next;
295                 list_del(ptemp_list);
296                 ptemp_list = pnext_list;
297                 /*
298                  * zero out the RBU packet memory before freeing
299                  * to make sure there are no stale RBU packets left in memory
300                  */
301                 memset(newpacket->data, 0, rbu_data.packetsize);
302                 free_pages((unsigned long) newpacket->data,
303                         newpacket->ordernum);
304                 kfree(newpacket);
305         }
306         rbu_data.packet_write_count = 0;
307         rbu_data.packet_read_count = 0;
308         rbu_data.num_packets = 0;
309         rbu_data.packetsize = 0;
310 }
311
312 /*
313  * img_update_free: Frees the buffer allocated for storing BIOS image
314  * Always called with lock held and returned with lock held
315  */
316 static void img_update_free(void)
317 {
318         if (!rbu_data.image_update_buffer)
319                 return;
320         /*
321          * zero out this buffer before freeing it to get rid of any stale
322          * BIOS image copied in memory.
323          */
324         memset(rbu_data.image_update_buffer, 0,
325                 rbu_data.image_update_buffer_size);
326         if (rbu_data.dma_alloc == 1)
327                 dma_free_coherent(NULL, rbu_data.bios_image_size,
328                         rbu_data.image_update_buffer, dell_rbu_dmaaddr);
329         else
330                 free_pages((unsigned long) rbu_data.image_update_buffer,
331                         rbu_data.image_update_ordernum);
332
333         /*
334          * Re-initialize the rbu_data variables after a free
335          */
336         rbu_data.image_update_ordernum = -1;
337         rbu_data.image_update_buffer = NULL;
338         rbu_data.image_update_buffer_size = 0;
339         rbu_data.bios_image_size = 0;
340         rbu_data.dma_alloc = 0;
341 }
342
343 /*
344  * img_update_realloc: This function allocates the contiguous pages to
345  * accommodate the requested size of data. The memory address and size
346  * values are stored globally and on every call to this function the new
347  * size is checked to see if more data is required than the existing size.
348  * If true the previous memory is freed and new allocation is done to
349  * accommodate the new size. If the incoming size is less then than the
350  * already allocated size, then that memory is reused. This function is
351  * called with lock held and returns with lock held.
352  */
353 static int img_update_realloc(unsigned long size)
354 {
355         unsigned char *image_update_buffer = NULL;
356         unsigned long rc;
357         unsigned long img_buf_phys_addr;
358         int ordernum;
359         int dma_alloc = 0;
360
361         /*
362          * check if the buffer of sufficient size has been
363          * already allocated
364          */
365         if (rbu_data.image_update_buffer_size >= size) {
366                 /*
367                  * check for corruption
368                  */
369                 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
370                         printk(KERN_ERR "dell_rbu:%s: corruption "
371                                 "check failed\n", __FUNCTION__);
372                         return -EINVAL;
373                 }
374                 /*
375                  * we have a valid pre-allocated buffer with
376                  * sufficient size
377                  */
378                 return 0;
379         }
380
381         /*
382          * free any previously allocated buffer
383          */
384         img_update_free();
385
386         spin_unlock(&rbu_data.lock);
387
388         ordernum = get_order(size);
389         image_update_buffer =
390                 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
391
392         img_buf_phys_addr =
393                 (unsigned long) virt_to_phys(image_update_buffer);
394
395         if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
396                 free_pages((unsigned long) image_update_buffer, ordernum);
397                 ordernum = -1;
398                 image_update_buffer = dma_alloc_coherent(NULL, size,
399                         &dell_rbu_dmaaddr, GFP_KERNEL);
400                 dma_alloc = 1;
401         }
402
403         spin_lock(&rbu_data.lock);
404
405         if (image_update_buffer != NULL) {
406                 rbu_data.image_update_buffer = image_update_buffer;
407                 rbu_data.image_update_buffer_size = size;
408                 rbu_data.bios_image_size =
409                         rbu_data.image_update_buffer_size;
410                 rbu_data.image_update_ordernum = ordernum;
411                 rbu_data.dma_alloc = dma_alloc;
412                 rc = 0;
413         } else {
414                 pr_debug("Not enough memory for image update:"
415                         "size = %ld\n", size);
416                 rc = -ENOMEM;
417         }
418
419         return rc;
420 }
421
422 static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
423 {
424         int retval;
425         size_t bytes_left;
426         size_t data_length;
427         char *ptempBuf = buffer;
428         unsigned long imagesize;
429
430         /* check to see if we have something to return */
431         if (rbu_data.num_packets == 0) {
432                 pr_debug("read_packet_data: no packets written\n");
433                 retval = -ENOMEM;
434                 goto read_rbu_data_exit;
435         }
436
437         imagesize = rbu_data.num_packets * rbu_data.packetsize;
438
439         if (pos > imagesize) {
440                 retval = 0;
441                 printk(KERN_WARNING "dell_rbu:read_packet_data: "
442                         "data underrun\n");
443                 goto read_rbu_data_exit;
444         }
445
446         bytes_left = imagesize - pos;
447         data_length = min(bytes_left, count);
448
449         if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
450                 goto read_rbu_data_exit;
451
452         if ((pos + count) > imagesize) {
453                 rbu_data.packet_read_count = 0;
454                 /* this was the last copy */
455                 retval = bytes_left;
456         } else
457                 retval = count;
458
459       read_rbu_data_exit:
460         return retval;
461 }
462
463 static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
464 {
465         unsigned char *ptemp = NULL;
466         size_t bytes_left = 0;
467         size_t data_length = 0;
468         ssize_t ret_count = 0;
469
470         /* check to see if we have something to return */
471         if ((rbu_data.image_update_buffer == NULL) ||
472                 (rbu_data.bios_image_size == 0)) {
473                 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
474                         "bios_image_size %lu\n",
475                         rbu_data.image_update_buffer,
476                         rbu_data.bios_image_size);
477                 ret_count = -ENOMEM;
478                 goto read_rbu_data_exit;
479         }
480
481         if (pos > rbu_data.bios_image_size) {
482                 ret_count = 0;
483                 goto read_rbu_data_exit;
484         }
485
486         bytes_left = rbu_data.bios_image_size - pos;
487         data_length = min(bytes_left, count);
488
489         ptemp = rbu_data.image_update_buffer;
490         memcpy(buffer, (ptemp + pos), data_length);
491
492         if ((pos + count) > rbu_data.bios_image_size)
493                 /* this was the last copy */
494                 ret_count = bytes_left;
495         else
496                 ret_count = count;
497       read_rbu_data_exit:
498         return ret_count;
499 }
500
501 static ssize_t read_rbu_data(struct kobject *kobj, char *buffer,
502                         loff_t pos, size_t count)
503 {
504         ssize_t ret_count = 0;
505
506         spin_lock(&rbu_data.lock);
507
508         if (!strcmp(image_type, "mono"))
509                 ret_count = read_rbu_mono_data(buffer, pos, count);
510         else if (!strcmp(image_type, "packet"))
511                 ret_count = read_packet_data(buffer, pos, count);
512         else
513                 pr_debug("read_rbu_data: invalid image type specified\n");
514
515         spin_unlock(&rbu_data.lock);
516         return ret_count;
517 }
518
519 static void callbackfn_rbu(const struct firmware *fw, void *context)
520 {
521         int rc = 0;
522
523         if (!fw || !fw->size) {
524                 rbu_data.entry_created = 0;
525                 return;
526         }
527
528         spin_lock(&rbu_data.lock);
529         if (!strcmp(image_type, "mono")) {
530                 if (!img_update_realloc(fw->size))
531                         memcpy(rbu_data.image_update_buffer,
532                                 fw->data, fw->size);
533         } else if (!strcmp(image_type, "packet")) {
534                 if (!rbu_data.packetsize)
535                         rbu_data.packetsize = fw->size;
536                 else if (rbu_data.packetsize != fw->size) {
537                         packet_empty_list();
538                         rbu_data.packetsize = fw->size;
539                 }
540                 packetize_data(fw->data, fw->size);
541         } else
542                 pr_debug("invalid image type specified.\n");
543         spin_unlock(&rbu_data.lock);
544
545         rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
546                 "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
547         if (rc)
548                 printk(KERN_ERR
549                         "dell_rbu:%s request_firmware_nowait failed"
550                         " %d\n", __FUNCTION__, rc);
551         else
552                 rbu_data.entry_created = 1;
553 }
554
555 static ssize_t read_rbu_image_type(struct kobject *kobj, char *buffer,
556                         loff_t pos, size_t count)
557 {
558         int size = 0;
559         if (!pos)
560                 size = sprintf(buffer, "%s\n", image_type);
561         return size;
562 }
563
564 static ssize_t write_rbu_image_type(struct kobject *kobj, char *buffer,
565                         loff_t pos, size_t count)
566 {
567         int rc = count;
568         int req_firm_rc = 0;
569         int i;
570         spin_lock(&rbu_data.lock);
571         /*
572          * Find the first newline or space
573          */
574         for (i = 0; i < count; ++i)
575                 if (buffer[i] == '\n' || buffer[i] == ' ') {
576                         buffer[i] = '\0';
577                         break;
578                 }
579         if (i == count)
580                 buffer[count] = '\0';
581
582         if (strstr(buffer, "mono"))
583                 strcpy(image_type, "mono");
584         else if (strstr(buffer, "packet"))
585                 strcpy(image_type, "packet");
586         else if (strstr(buffer, "init")) {
587                 /*
588                  * If due to the user error the driver gets in a bad
589                  * state where even though it is loaded , the
590                  * /sys/class/firmware/dell_rbu entries are missing.
591                  * to cover this situation the user can recreate entries
592                  * by writing init to image_type.
593                  */
594                 if (!rbu_data.entry_created) {
595                         spin_unlock(&rbu_data.lock);
596                         req_firm_rc = request_firmware_nowait(THIS_MODULE,
597                                 FW_ACTION_NOHOTPLUG, "dell_rbu",
598                                 &rbu_device->dev, &context,
599                                 callbackfn_rbu);
600                         if (req_firm_rc) {
601                                 printk(KERN_ERR
602                                         "dell_rbu:%s request_firmware_nowait"
603                                         " failed %d\n", __FUNCTION__, rc);
604                                 rc = -EIO;
605                         } else
606                                 rbu_data.entry_created = 1;
607
608                         spin_lock(&rbu_data.lock);
609                 }
610         } else {
611                 printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
612                 spin_unlock(&rbu_data.lock);
613                 return -EINVAL;
614         }
615
616         /* we must free all previous allocations */
617         packet_empty_list();
618         img_update_free();
619         spin_unlock(&rbu_data.lock);
620
621         return rc;
622 }
623
624 static struct bin_attribute rbu_data_attr = {
625         .attr = {
626                 .name = "data",
627                 .owner = THIS_MODULE,
628                 .mode = 0444,
629         },
630         .read = read_rbu_data,
631 };
632
633 static struct bin_attribute rbu_image_type_attr = {
634         .attr = {
635                 .name = "image_type",
636                 .owner = THIS_MODULE,
637                 .mode = 0644,
638         },
639         .read = read_rbu_image_type,
640         .write = write_rbu_image_type,
641 };
642
643 static int __init dcdrbu_init(void)
644 {
645         int rc = 0;
646         spin_lock_init(&rbu_data.lock);
647
648         init_packet_head();
649         rbu_device =
650                 platform_device_register_simple("dell_rbu", -1, NULL, 0);
651         if (!rbu_device) {
652                 printk(KERN_ERR
653                         "dell_rbu:%s:platform_device_register_simple "
654                         "failed\n", __FUNCTION__);
655                 return -EIO;
656         }
657
658         sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
659         sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
660
661         rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
662                 "dell_rbu", &rbu_device->dev, &context, callbackfn_rbu);
663         if (rc)
664                 printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait"
665                         " failed %d\n", __FUNCTION__, rc);
666         else
667                 rbu_data.entry_created = 1;
668
669         return rc;
670
671 }
672
673 static __exit void dcdrbu_exit(void)
674 {
675         spin_lock(&rbu_data.lock);
676         packet_empty_list();
677         img_update_free();
678         spin_unlock(&rbu_data.lock);
679         platform_device_unregister(rbu_device);
680 }
681
682 module_exit(dcdrbu_exit);
683 module_init(dcdrbu_init);