blob: 031c689035837e453736837c9f289088a9a5ce6c [file] [log] [blame]
Abhay Salunke6c54c282005-09-06 15:17:14 -07001/*
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 */
Abhay Salunke6c54c282005-09-06 15:17:14 -070037#include <linux/init.h>
38#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Abhay Salunke6c54c282005-09-06 15:17:14 -070040#include <linux/string.h>
41#include <linux/errno.h>
42#include <linux/blkdev.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010043#include <linux/platform_device.h>
Abhay Salunke6c54c282005-09-06 15:17:14 -070044#include <linux/spinlock.h>
45#include <linux/moduleparam.h>
46#include <linux/firmware.h>
47#include <linux/dma-mapping.h>
Stuart Hayes6aecee62018-09-26 16:50:17 -050048#include <asm/set_memory.h>
Abhay Salunke6c54c282005-09-06 15:17:14 -070049
50MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
51MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
52MODULE_LICENSE("GPL");
Abhay Salunke2c560842006-01-14 13:21:14 -080053MODULE_VERSION("3.2");
Abhay Salunke6c54c282005-09-06 15:17:14 -070054
55#define BIOS_SCAN_LIMIT 0xffffffff
56#define MAX_IMAGE_LENGTH 16
57static 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;
Abhay Salunke6c54c282005-09-06 15:17:14 -070062 spinlock_t lock;
63 unsigned long packet_read_count;
Abhay Salunke6c54c282005-09-06 15:17:14 -070064 unsigned long num_packets;
65 unsigned long packetsize;
Abhay Salunkead6ce872005-10-11 08:29:02 -070066 unsigned long imagesize;
Abhay Salunkee61c0e32005-09-16 19:28:04 -070067 int entry_created;
Abhay Salunke6c54c282005-09-06 15:17:14 -070068} rbu_data;
69
Abhay Salunkee61c0e32005-09-16 19:28:04 -070070static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";
71module_param_string(image_type, image_type, sizeof (image_type), 0);
Abhay Salunkead6ce872005-10-11 08:29:02 -070072MODULE_PARM_DESC(image_type,
73 "BIOS image type. choose- mono or packet or init");
Abhay Salunke6c54c282005-09-06 15:17:14 -070074
Abhay Salunke274b6932005-11-07 00:59:26 -080075static unsigned long allocation_floor = 0x100000;
76module_param(allocation_floor, ulong, 0644);
77MODULE_PARM_DESC(allocation_floor,
78 "Minimum address for allocations when using Packet mode");
79
Abhay Salunke6c54c282005-09-06 15:17:14 -070080struct packet_data {
81 struct list_head list;
82 size_t length;
83 void *data;
84 int ordernum;
85};
86
87static struct packet_data packet_data_head;
88
89static struct platform_device *rbu_device;
90static int context;
Abhay Salunke6c54c282005-09-06 15:17:14 -070091
Andrew Mortondda85772005-09-16 19:28:05 -070092static void init_packet_head(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -070093{
94 INIT_LIST_HEAD(&packet_data_head.list);
Abhay Salunke6c54c282005-09-06 15:17:14 -070095 rbu_data.packet_read_count = 0;
96 rbu_data.num_packets = 0;
97 rbu_data.packetsize = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -070098 rbu_data.imagesize = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -070099}
100
Abhay Salunkead6ce872005-10-11 08:29:02 -0700101static int create_packet(void *data, size_t length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700102{
103 struct packet_data *newpacket;
104 int ordernum = 0;
Abhay Salunke274b6932005-11-07 00:59:26 -0800105 int retval = 0;
106 unsigned int packet_array_size = 0;
Al Viro5ad92012005-12-15 09:18:00 +0000107 void **invalid_addr_packet_array = NULL;
108 void *packet_data_temp_buf = NULL;
Abhay Salunke274b6932005-11-07 00:59:26 -0800109 unsigned int idx = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700110
111 pr_debug("create_packet: entry \n");
112
113 if (!rbu_data.packetsize) {
114 pr_debug("create_packet: packetsize not specified\n");
Abhay Salunke274b6932005-11-07 00:59:26 -0800115 retval = -EINVAL;
116 goto out_noalloc;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700117 }
Abhay Salunke274b6932005-11-07 00:59:26 -0800118
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700119 spin_unlock(&rbu_data.lock);
Abhay Salunke274b6932005-11-07 00:59:26 -0800120
121 newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700122
Abhay Salunke6c54c282005-09-06 15:17:14 -0700123 if (!newpacket) {
124 printk(KERN_WARNING
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700125 "dell_rbu:%s: failed to allocate new "
Harvey Harrisoneecd5852008-04-29 00:59:19 -0700126 "packet\n", __func__);
Abhay Salunke274b6932005-11-07 00:59:26 -0800127 retval = -ENOMEM;
128 spin_lock(&rbu_data.lock);
129 goto out_noalloc;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700130 }
131
132 ordernum = get_order(length);
Abhay Salunke274b6932005-11-07 00:59:26 -0800133
Abhay Salunke6c54c282005-09-06 15:17:14 -0700134 /*
Abhay Salunke274b6932005-11-07 00:59:26 -0800135 * BIOS errata mean we cannot allocate packets below 1MB or they will
136 * be overwritten by BIOS.
137 *
138 * array to temporarily hold packets
139 * that are below the allocation floor
140 *
141 * NOTE: very simplistic because we only need the floor to be at 1MB
142 * due to BIOS errata. This shouldn't be used for higher floors
143 * or you will run out of mem trying to allocate the array.
Abhay Salunke6c54c282005-09-06 15:17:14 -0700144 */
Abhay Salunke274b6932005-11-07 00:59:26 -0800145 packet_array_size = max(
146 (unsigned int)(allocation_floor / rbu_data.packetsize),
147 (unsigned int)1);
Kees Cook6396bb22018-06-12 14:03:40 -0700148 invalid_addr_packet_array = kcalloc(packet_array_size, sizeof(void *),
Abhay Salunke274b6932005-11-07 00:59:26 -0800149 GFP_KERNEL);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700150
Abhay Salunke274b6932005-11-07 00:59:26 -0800151 if (!invalid_addr_packet_array) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700152 printk(KERN_WARNING
Abhay Salunke274b6932005-11-07 00:59:26 -0800153 "dell_rbu:%s: failed to allocate "
154 "invalid_addr_packet_array \n",
Harvey Harrisoneecd5852008-04-29 00:59:19 -0700155 __func__);
Abhay Salunke274b6932005-11-07 00:59:26 -0800156 retval = -ENOMEM;
157 spin_lock(&rbu_data.lock);
158 goto out_alloc_packet;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700159 }
160
Abhay Salunke274b6932005-11-07 00:59:26 -0800161 while (!packet_data_temp_buf) {
162 packet_data_temp_buf = (unsigned char *)
163 __get_free_pages(GFP_KERNEL, ordernum);
164 if (!packet_data_temp_buf) {
165 printk(KERN_WARNING
166 "dell_rbu:%s: failed to allocate new "
Harvey Harrisoneecd5852008-04-29 00:59:19 -0700167 "packet\n", __func__);
Abhay Salunke274b6932005-11-07 00:59:26 -0800168 retval = -ENOMEM;
169 spin_lock(&rbu_data.lock);
170 goto out_alloc_packet_array;
171 }
172
173 if ((unsigned long)virt_to_phys(packet_data_temp_buf)
174 < allocation_floor) {
175 pr_debug("packet 0x%lx below floor at 0x%lx.\n",
176 (unsigned long)virt_to_phys(
177 packet_data_temp_buf),
178 allocation_floor);
179 invalid_addr_packet_array[idx++] = packet_data_temp_buf;
Al Viro5ad92012005-12-15 09:18:00 +0000180 packet_data_temp_buf = NULL;
Abhay Salunke274b6932005-11-07 00:59:26 -0800181 }
182 }
Stuart Hayes6aecee62018-09-26 16:50:17 -0500183 /*
184 * set to uncachable or it may never get written back before reboot
185 */
186 set_memory_uc((unsigned long)packet_data_temp_buf, 1 << ordernum);
187
Abhay Salunke274b6932005-11-07 00:59:26 -0800188 spin_lock(&rbu_data.lock);
189
190 newpacket->data = packet_data_temp_buf;
191
192 pr_debug("create_packet: newpacket at physical addr %lx\n",
193 (unsigned long)virt_to_phys(newpacket->data));
194
195 /* packets may not have fixed size */
196 newpacket->length = length;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700197 newpacket->ordernum = ordernum;
198 ++rbu_data.num_packets;
Abhay Salunke274b6932005-11-07 00:59:26 -0800199
200 /* initialize the newly created packet headers */
Abhay Salunke6c54c282005-09-06 15:17:14 -0700201 INIT_LIST_HEAD(&newpacket->list);
202 list_add_tail(&newpacket->list, &packet_data_head.list);
Abhay Salunkead6ce872005-10-11 08:29:02 -0700203
204 memcpy(newpacket->data, data, length);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700205
206 pr_debug("create_packet: exit \n");
207
Abhay Salunke274b6932005-11-07 00:59:26 -0800208out_alloc_packet_array:
209 /* always free packet array */
210 for (;idx>0;idx--) {
211 pr_debug("freeing unused packet below floor 0x%lx.\n",
212 (unsigned long)virt_to_phys(
213 invalid_addr_packet_array[idx-1]));
214 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
215 ordernum);
216 }
217 kfree(invalid_addr_packet_array);
218
219out_alloc_packet:
220 /* if error, free data */
221 if (retval)
222 kfree(newpacket);
223
224out_noalloc:
225 return retval;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700226}
227
Greg Kroah-Hartmanc6c1c942008-05-29 10:05:08 -0700228static int packetize_data(const u8 *data, size_t length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700229{
230 int rc = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700231 int done = 0;
232 int packet_length;
233 u8 *temp;
234 u8 *end = (u8 *) data + length;
Zach Browncb7cf572006-10-03 01:16:09 -0700235 pr_debug("packetize_data: data length %zd\n", length);
Abhay Salunkead6ce872005-10-11 08:29:02 -0700236 if (!rbu_data.packetsize) {
237 printk(KERN_WARNING
238 "dell_rbu: packetsize not specified\n");
239 return -EIO;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700240 }
Abhay Salunkead6ce872005-10-11 08:29:02 -0700241
242 temp = (u8 *) data;
243
244 /* packetize the hunk */
245 while (!done) {
246 if ((temp + rbu_data.packetsize) < end)
247 packet_length = rbu_data.packetsize;
248 else {
249 /* this is the last packet */
250 packet_length = end - temp;
251 done = 1;
252 }
253
254 if ((rc = create_packet(temp, packet_length)))
255 return rc;
256
Andrew Morton9e42ef72006-10-11 01:22:13 -0700257 pr_debug("%p:%td\n", temp, (end - temp));
Abhay Salunkead6ce872005-10-11 08:29:02 -0700258 temp += packet_length;
259 }
260
261 rbu_data.imagesize = length;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700262
263 return rc;
264}
265
Andrew Mortondda85772005-09-16 19:28:05 -0700266static int do_packet_read(char *data, struct list_head *ptemp_list,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700267 int length, int bytes_read, int *list_read_count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700268{
269 void *ptemp_buf;
270 struct packet_data *newpacket = NULL;
271 int bytes_copied = 0;
272 int j = 0;
273
274 newpacket = list_entry(ptemp_list, struct packet_data, list);
275 *list_read_count += newpacket->length;
276
277 if (*list_read_count > bytes_read) {
278 /* point to the start of unread data */
279 j = newpacket->length - (*list_read_count - bytes_read);
280 /* point to the offset in the packet buffer */
281 ptemp_buf = (u8 *) newpacket->data + j;
282 /*
283 * check if there is enough room in
284 * * the incoming buffer
285 */
286 if (length > (*list_read_count - bytes_read))
287 /*
288 * copy what ever is there in this
289 * packet and move on
290 */
291 bytes_copied = (*list_read_count - bytes_read);
292 else
293 /* copy the remaining */
294 bytes_copied = length;
295 memcpy(data, ptemp_buf, bytes_copied);
296 }
297 return bytes_copied;
298}
299
Abhay Salunkead6ce872005-10-11 08:29:02 -0700300static int packet_read_list(char *data, size_t * pread_length)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700301{
302 struct list_head *ptemp_list;
303 int temp_count = 0;
304 int bytes_copied = 0;
305 int bytes_read = 0;
306 int remaining_bytes = 0;
307 char *pdest = data;
308
309 /* check if we have any packets */
310 if (0 == rbu_data.num_packets)
311 return -ENOMEM;
312
313 remaining_bytes = *pread_length;
314 bytes_read = rbu_data.packet_read_count;
315
316 ptemp_list = (&packet_data_head.list)->next;
317 while (!list_empty(ptemp_list)) {
318 bytes_copied = do_packet_read(pdest, ptemp_list,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700319 remaining_bytes, bytes_read, &temp_count);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700320 remaining_bytes -= bytes_copied;
321 bytes_read += bytes_copied;
322 pdest += bytes_copied;
323 /*
324 * check if we reached end of buffer before reaching the
325 * last packet
326 */
327 if (remaining_bytes == 0)
328 break;
329
330 ptemp_list = ptemp_list->next;
331 }
332 /*finally set the bytes read */
333 *pread_length = bytes_read - rbu_data.packet_read_count;
334 rbu_data.packet_read_count = bytes_read;
335 return 0;
336}
337
Andrew Mortondda85772005-09-16 19:28:05 -0700338static void packet_empty_list(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700339{
340 struct list_head *ptemp_list;
341 struct list_head *pnext_list;
342 struct packet_data *newpacket;
343
344 ptemp_list = (&packet_data_head.list)->next;
345 while (!list_empty(ptemp_list)) {
346 newpacket =
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700347 list_entry(ptemp_list, struct packet_data, list);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700348 pnext_list = ptemp_list->next;
349 list_del(ptemp_list);
350 ptemp_list = pnext_list;
351 /*
352 * zero out the RBU packet memory before freeing
353 * to make sure there are no stale RBU packets left in memory
354 */
355 memset(newpacket->data, 0, rbu_data.packetsize);
Stuart Hayes6aecee62018-09-26 16:50:17 -0500356 set_memory_wb((unsigned long)newpacket->data,
357 1 << newpacket->ordernum);
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700358 free_pages((unsigned long) newpacket->data,
359 newpacket->ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700360 kfree(newpacket);
361 }
Abhay Salunke6c54c282005-09-06 15:17:14 -0700362 rbu_data.packet_read_count = 0;
363 rbu_data.num_packets = 0;
Abhay Salunkead6ce872005-10-11 08:29:02 -0700364 rbu_data.imagesize = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700365}
366
367/*
368 * img_update_free: Frees the buffer allocated for storing BIOS image
369 * Always called with lock held and returned with lock held
370 */
Andrew Mortondda85772005-09-16 19:28:05 -0700371static void img_update_free(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700372{
373 if (!rbu_data.image_update_buffer)
374 return;
375 /*
376 * zero out this buffer before freeing it to get rid of any stale
377 * BIOS image copied in memory.
378 */
379 memset(rbu_data.image_update_buffer, 0,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700380 rbu_data.image_update_buffer_size);
Christoph Hellwigfd47a362019-01-29 08:34:09 +0100381 free_pages((unsigned long) rbu_data.image_update_buffer,
382 rbu_data.image_update_ordernum);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700383
384 /*
385 * Re-initialize the rbu_data variables after a free
386 */
387 rbu_data.image_update_ordernum = -1;
388 rbu_data.image_update_buffer = NULL;
389 rbu_data.image_update_buffer_size = 0;
390 rbu_data.bios_image_size = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700391}
392
393/*
394 * img_update_realloc: This function allocates the contiguous pages to
395 * accommodate the requested size of data. The memory address and size
396 * values are stored globally and on every call to this function the new
397 * size is checked to see if more data is required than the existing size.
398 * If true the previous memory is freed and new allocation is done to
399 * accommodate the new size. If the incoming size is less then than the
400 * already allocated size, then that memory is reused. This function is
401 * called with lock held and returns with lock held.
402 */
Andrew Mortondda85772005-09-16 19:28:05 -0700403static int img_update_realloc(unsigned long size)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700404{
405 unsigned char *image_update_buffer = NULL;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700406 unsigned long img_buf_phys_addr;
407 int ordernum;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700408
409 /*
410 * check if the buffer of sufficient size has been
411 * already allocated
412 */
413 if (rbu_data.image_update_buffer_size >= size) {
414 /*
415 * check for corruption
416 */
417 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
418 printk(KERN_ERR "dell_rbu:%s: corruption "
Harvey Harrisoneecd5852008-04-29 00:59:19 -0700419 "check failed\n", __func__);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700420 return -EINVAL;
421 }
422 /*
423 * we have a valid pre-allocated buffer with
424 * sufficient size
425 */
426 return 0;
427 }
428
429 /*
430 * free any previously allocated buffer
431 */
432 img_update_free();
433
434 spin_unlock(&rbu_data.lock);
435
436 ordernum = get_order(size);
437 image_update_buffer =
Christoph Hellwigfd47a362019-01-29 08:34:09 +0100438 (unsigned char *)__get_free_pages(GFP_DMA32, ordernum);
Christoph Hellwigf27e1d12019-02-11 14:09:40 +0100439 spin_lock(&rbu_data.lock);
Christoph Hellwigfd47a362019-01-29 08:34:09 +0100440 if (!image_update_buffer) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700441 pr_debug("Not enough memory for image update:"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700442 "size = %ld\n", size);
Christoph Hellwigfd47a362019-01-29 08:34:09 +0100443 return -ENOMEM;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700444 }
445
Christoph Hellwigfd47a362019-01-29 08:34:09 +0100446 img_buf_phys_addr = (unsigned long)virt_to_phys(image_update_buffer);
447 if (WARN_ON_ONCE(img_buf_phys_addr > BIOS_SCAN_LIMIT))
448 return -EINVAL; /* can't happen per definition */
449
Christoph Hellwigfd47a362019-01-29 08:34:09 +0100450 rbu_data.image_update_buffer = image_update_buffer;
451 rbu_data.image_update_buffer_size = size;
452 rbu_data.bios_image_size = rbu_data.image_update_buffer_size;
453 rbu_data.image_update_ordernum = ordernum;
454 return 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700455}
456
Andrew Mortondda85772005-09-16 19:28:05 -0700457static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700458{
459 int retval;
460 size_t bytes_left;
461 size_t data_length;
462 char *ptempBuf = buffer;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700463
464 /* check to see if we have something to return */
465 if (rbu_data.num_packets == 0) {
466 pr_debug("read_packet_data: no packets written\n");
467 retval = -ENOMEM;
468 goto read_rbu_data_exit;
469 }
470
Abhay Salunkead6ce872005-10-11 08:29:02 -0700471 if (pos > rbu_data.imagesize) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700472 retval = 0;
473 printk(KERN_WARNING "dell_rbu:read_packet_data: "
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700474 "data underrun\n");
Abhay Salunke6c54c282005-09-06 15:17:14 -0700475 goto read_rbu_data_exit;
476 }
477
Abhay Salunkead6ce872005-10-11 08:29:02 -0700478 bytes_left = rbu_data.imagesize - pos;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700479 data_length = min(bytes_left, count);
480
481 if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
482 goto read_rbu_data_exit;
483
Abhay Salunkead6ce872005-10-11 08:29:02 -0700484 if ((pos + count) > rbu_data.imagesize) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700485 rbu_data.packet_read_count = 0;
486 /* this was the last copy */
487 retval = bytes_left;
488 } else
489 retval = count;
490
491 read_rbu_data_exit:
492 return retval;
493}
494
Andrew Mortondda85772005-09-16 19:28:05 -0700495static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700496{
Abhay Salunke6c54c282005-09-06 15:17:14 -0700497 /* check to see if we have something to return */
498 if ((rbu_data.image_update_buffer == NULL) ||
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700499 (rbu_data.bios_image_size == 0)) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700500 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700501 "bios_image_size %lu\n",
502 rbu_data.image_update_buffer,
503 rbu_data.bios_image_size);
Akinobu Mita25377472008-07-25 01:48:27 -0700504 return -ENOMEM;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700505 }
506
Akinobu Mita25377472008-07-25 01:48:27 -0700507 return memory_read_from_buffer(buffer, count, &pos,
508 rbu_data.image_update_buffer, rbu_data.bios_image_size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700509}
510
Chris Wright2c3c8be2010-05-12 18:28:57 -0700511static ssize_t read_rbu_data(struct file *filp, struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +0800512 struct bin_attribute *bin_attr,
513 char *buffer, loff_t pos, size_t count)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700514{
515 ssize_t ret_count = 0;
516
517 spin_lock(&rbu_data.lock);
518
519 if (!strcmp(image_type, "mono"))
520 ret_count = read_rbu_mono_data(buffer, pos, count);
521 else if (!strcmp(image_type, "packet"))
522 ret_count = read_packet_data(buffer, pos, count);
523 else
524 pr_debug("read_rbu_data: invalid image type specified\n");
525
526 spin_unlock(&rbu_data.lock);
527 return ret_count;
528}
529
Andrew Mortondda85772005-09-16 19:28:05 -0700530static void callbackfn_rbu(const struct firmware *fw, void *context)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700531{
Abhay Salunke2c560842006-01-14 13:21:14 -0800532 rbu_data.entry_created = 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700533
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100534 if (!fw)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700535 return;
536
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100537 if (!fw->size)
538 goto out;
539
Abhay Salunke6c54c282005-09-06 15:17:14 -0700540 spin_lock(&rbu_data.lock);
541 if (!strcmp(image_type, "mono")) {
542 if (!img_update_realloc(fw->size))
543 memcpy(rbu_data.image_update_buffer,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700544 fw->data, fw->size);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700545 } else if (!strcmp(image_type, "packet")) {
Abhay Salunkead6ce872005-10-11 08:29:02 -0700546 /*
547 * we need to free previous packets if a
548 * new hunk of packets needs to be downloaded
549 */
550 packet_empty_list();
551 if (packetize_data(fw->data, fw->size))
552 /* Incase something goes wrong when we are
553 * in middle of packetizing the data, we
554 * need to free up whatever packets might
555 * have been created before we quit.
556 */
Abhay Salunke6c54c282005-09-06 15:17:14 -0700557 packet_empty_list();
Abhay Salunke6c54c282005-09-06 15:17:14 -0700558 } else
559 pr_debug("invalid image type specified.\n");
560 spin_unlock(&rbu_data.lock);
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100561 out:
562 release_firmware(fw);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700563}
564
Chris Wright2c3c8be2010-05-12 18:28:57 -0700565static ssize_t read_rbu_image_type(struct file *filp, struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +0800566 struct bin_attribute *bin_attr,
567 char *buffer, loff_t pos, size_t count)
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700568{
569 int size = 0;
570 if (!pos)
Pavel Roskin81156922009-01-17 13:33:03 -0500571 size = scnprintf(buffer, count, "%s\n", image_type);
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700572 return size;
573}
574
Chris Wright2c3c8be2010-05-12 18:28:57 -0700575static ssize_t write_rbu_image_type(struct file *filp, struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +0800576 struct bin_attribute *bin_attr,
577 char *buffer, loff_t pos, size_t count)
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700578{
579 int rc = count;
580 int req_firm_rc = 0;
581 int i;
582 spin_lock(&rbu_data.lock);
583 /*
584 * Find the first newline or space
585 */
586 for (i = 0; i < count; ++i)
587 if (buffer[i] == '\n' || buffer[i] == ' ') {
588 buffer[i] = '\0';
589 break;
590 }
591 if (i == count)
592 buffer[count] = '\0';
593
594 if (strstr(buffer, "mono"))
595 strcpy(image_type, "mono");
596 else if (strstr(buffer, "packet"))
597 strcpy(image_type, "packet");
598 else if (strstr(buffer, "init")) {
599 /*
600 * If due to the user error the driver gets in a bad
601 * state where even though it is loaded , the
602 * /sys/class/firmware/dell_rbu entries are missing.
603 * to cover this situation the user can recreate entries
604 * by writing init to image_type.
605 */
606 if (!rbu_data.entry_created) {
607 spin_unlock(&rbu_data.lock);
608 req_firm_rc = request_firmware_nowait(THIS_MODULE,
609 FW_ACTION_NOHOTPLUG, "dell_rbu",
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100610 &rbu_device->dev, GFP_KERNEL, &context,
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700611 callbackfn_rbu);
612 if (req_firm_rc) {
613 printk(KERN_ERR
614 "dell_rbu:%s request_firmware_nowait"
Harvey Harrisoneecd5852008-04-29 00:59:19 -0700615 " failed %d\n", __func__, rc);
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700616 rc = -EIO;
617 } else
618 rbu_data.entry_created = 1;
619
620 spin_lock(&rbu_data.lock);
621 }
622 } else {
623 printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
624 spin_unlock(&rbu_data.lock);
625 return -EINVAL;
626 }
627
628 /* we must free all previous allocations */
629 packet_empty_list();
630 img_update_free();
631 spin_unlock(&rbu_data.lock);
632
633 return rc;
634}
635
Chris Wright2c3c8be2010-05-12 18:28:57 -0700636static ssize_t read_rbu_packet_size(struct file *filp, struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +0800637 struct bin_attribute *bin_attr,
638 char *buffer, loff_t pos, size_t count)
Abhay Salunkead6ce872005-10-11 08:29:02 -0700639{
640 int size = 0;
641 if (!pos) {
642 spin_lock(&rbu_data.lock);
Pavel Roskin81156922009-01-17 13:33:03 -0500643 size = scnprintf(buffer, count, "%lu\n", rbu_data.packetsize);
Abhay Salunkead6ce872005-10-11 08:29:02 -0700644 spin_unlock(&rbu_data.lock);
645 }
646 return size;
647}
648
Chris Wright2c3c8be2010-05-12 18:28:57 -0700649static ssize_t write_rbu_packet_size(struct file *filp, struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +0800650 struct bin_attribute *bin_attr,
651 char *buffer, loff_t pos, size_t count)
Abhay Salunkead6ce872005-10-11 08:29:02 -0700652{
653 unsigned long temp;
654 spin_lock(&rbu_data.lock);
655 packet_empty_list();
656 sscanf(buffer, "%lu", &temp);
657 if (temp < 0xffffffff)
658 rbu_data.packetsize = temp;
659
660 spin_unlock(&rbu_data.lock);
661 return count;
662}
663
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700664static struct bin_attribute rbu_data_attr = {
Tejun Heo7b595752007-06-14 03:45:17 +0900665 .attr = {.name = "data", .mode = 0444},
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700666 .read = read_rbu_data,
667};
668
669static struct bin_attribute rbu_image_type_attr = {
Tejun Heo7b595752007-06-14 03:45:17 +0900670 .attr = {.name = "image_type", .mode = 0644},
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700671 .read = read_rbu_image_type,
672 .write = write_rbu_image_type,
673};
674
Abhay Salunkead6ce872005-10-11 08:29:02 -0700675static struct bin_attribute rbu_packet_size_attr = {
Tejun Heo7b595752007-06-14 03:45:17 +0900676 .attr = {.name = "packet_size", .mode = 0644},
Abhay Salunkead6ce872005-10-11 08:29:02 -0700677 .read = read_rbu_packet_size,
678 .write = write_rbu_packet_size,
679};
680
Andrew Mortondda85772005-09-16 19:28:05 -0700681static int __init dcdrbu_init(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700682{
Akinobu Mita68970832006-11-16 01:19:25 -0800683 int rc;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700684 spin_lock_init(&rbu_data.lock);
685
686 init_packet_head();
Akinobu Mita68970832006-11-16 01:19:25 -0800687 rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0);
688 if (IS_ERR(rbu_device)) {
Abhay Salunke6c54c282005-09-06 15:17:14 -0700689 printk(KERN_ERR
Abhay Salunkee61c0e32005-09-16 19:28:04 -0700690 "dell_rbu:%s:platform_device_register_simple "
Harvey Harrisoneecd5852008-04-29 00:59:19 -0700691 "failed\n", __func__);
Akinobu Mita68970832006-11-16 01:19:25 -0800692 return PTR_ERR(rbu_device);
Abhay Salunke6c54c282005-09-06 15:17:14 -0700693 }
694
Jeff Garzik41bfcfd2006-10-11 01:22:20 -0700695 rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
696 if (rc)
697 goto out_devreg;
698 rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
699 if (rc)
700 goto out_data;
701 rc = sysfs_create_bin_file(&rbu_device->dev.kobj,
Abhay Salunkead6ce872005-10-11 08:29:02 -0700702 &rbu_packet_size_attr);
Jeff Garzik41bfcfd2006-10-11 01:22:20 -0700703 if (rc)
704 goto out_imtype;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700705
Abhay Salunke2c560842006-01-14 13:21:14 -0800706 rbu_data.entry_created = 0;
Jeff Garzik41bfcfd2006-10-11 01:22:20 -0700707 return 0;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700708
Jeff Garzik41bfcfd2006-10-11 01:22:20 -0700709out_imtype:
710 sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
711out_data:
712 sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
713out_devreg:
714 platform_device_unregister(rbu_device);
715 return rc;
Abhay Salunke6c54c282005-09-06 15:17:14 -0700716}
717
Andrew Mortondda85772005-09-16 19:28:05 -0700718static __exit void dcdrbu_exit(void)
Abhay Salunke6c54c282005-09-06 15:17:14 -0700719{
720 spin_lock(&rbu_data.lock);
721 packet_empty_list();
722 img_update_free();
723 spin_unlock(&rbu_data.lock);
724 platform_device_unregister(rbu_device);
725}
726
727module_exit(dcdrbu_exit);
728module_init(dcdrbu_init);
Abhay Salunke274b6932005-11-07 00:59:26 -0800729
730/* vim:noet:ts=8:sw=8
731*/