ps3flash: Cache the last accessed FLASH chunk
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / char / ps3flash.c
1 /*
2 * PS3 FLASH ROM Storage Driver
3 *
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <linux/fs.h>
22 #include <linux/miscdevice.h>
23 #include <linux/uaccess.h>
24
25 #include <asm/lv1call.h>
26 #include <asm/ps3stor.h>
27
28
29 #define DEVICE_NAME "ps3flash"
30
31 #define FLASH_BLOCK_SIZE (256*1024)
32
33
34 struct ps3flash_private {
35 struct mutex mutex; /* Bounce buffer mutex */
36 u64 chunk_sectors;
37 int tag; /* Start sector of buffer, -1 if invalid */
38 bool dirty;
39 };
40
41 static struct ps3_storage_device *ps3flash_dev;
42
43 static int ps3flash_read_write_sectors(struct ps3_storage_device *dev,
44 u64 lpar, u64 start_sector, u64 sectors,
45 int write)
46 {
47 u64 res = ps3stor_read_write_sectors(dev, lpar, start_sector, sectors,
48 write);
49 if (res) {
50 dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
51 __LINE__, write ? "write" : "read", res);
52 return -EIO;
53 }
54 return 0;
55 }
56
57 static int ps3flash_writeback(struct ps3_storage_device *dev)
58 {
59 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
60 int res;
61
62 if (!priv->dirty || priv->tag < 0)
63 return 0;
64
65 res = ps3flash_read_write_sectors(dev, dev->bounce_lpar, priv->tag,
66 priv->chunk_sectors, 1);
67 if (res)
68 return res;
69
70 priv->dirty = false;
71 return 0;
72 }
73
74 static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector,
75 u64 sectors)
76 {
77 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
78 unsigned int tag, offset;
79 u64 lpar;
80 int res;
81
82 offset = start_sector % priv->chunk_sectors;
83 tag = start_sector - offset;
84 if (tag == priv->tag)
85 return 0;
86
87 res = ps3flash_writeback(dev);
88 if (res)
89 return res;
90
91 priv->tag = -1;
92
93 lpar = dev->bounce_lpar + offset * dev->blk_size;
94 res = ps3flash_read_write_sectors(dev, lpar, start_sector, sectors, 0);
95 if (res)
96 return res;
97
98 /* We don't bother caching reads smaller than the chunk size */
99 if (sectors == priv->chunk_sectors)
100 priv->tag = tag;
101
102 return 0;
103 }
104
105 static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin)
106 {
107 struct ps3_storage_device *dev = ps3flash_dev;
108 loff_t res;
109
110 mutex_lock(&file->f_mapping->host->i_mutex);
111 switch (origin) {
112 case 1:
113 offset += file->f_pos;
114 break;
115 case 2:
116 offset += dev->regions[dev->region_idx].size*dev->blk_size;
117 break;
118 }
119 if (offset < 0) {
120 res = -EINVAL;
121 goto out;
122 }
123
124 file->f_pos = offset;
125 res = file->f_pos;
126
127 out:
128 mutex_unlock(&file->f_mapping->host->i_mutex);
129 return res;
130 }
131
132 static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf,
133 size_t count, loff_t *pos)
134 {
135 struct ps3_storage_device *dev = ps3flash_dev;
136 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
137 u64 size, start_sector, end_sector, offset, sectors;
138 int res;
139 size_t remaining, n;
140 const void *src;
141
142 dev_dbg(&dev->sbd.core,
143 "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
144 __func__, __LINE__, count, *pos, userbuf, kernelbuf);
145
146 size = dev->regions[dev->region_idx].size*dev->blk_size;
147 if (*pos >= size || !count)
148 return 0;
149
150 if (*pos + count > size) {
151 dev_dbg(&dev->sbd.core,
152 "%s:%u Truncating count from %zu to %llu\n", __func__,
153 __LINE__, count, size - *pos);
154 count = size - *pos;
155 }
156
157 start_sector = *pos / dev->blk_size;
158 offset = *pos % dev->bounce_size;
159 end_sector = DIV_ROUND_UP(*pos + count, dev->blk_size);
160
161 remaining = count;
162 do {
163 sectors = min(end_sector - start_sector,
164 priv->chunk_sectors -
165 start_sector % priv->chunk_sectors);
166
167 mutex_lock(&priv->mutex);
168
169 res = ps3flash_fetch(dev, start_sector, sectors);
170 if (res)
171 goto fail;
172
173 n = min_t(u64, remaining, dev->bounce_size - offset);
174 src = dev->bounce_buf + offset;
175 dev_dbg(&dev->sbd.core,
176 "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
177 __func__, __LINE__, n, src, userbuf, kernelbuf);
178 if (userbuf) {
179 if (copy_to_user(userbuf, src, n)) {
180 res = -EFAULT;
181 goto fail;
182 }
183 userbuf += n;
184 }
185 if (kernelbuf) {
186 memcpy(kernelbuf, src, n);
187 kernelbuf += n;
188 }
189
190 mutex_unlock(&priv->mutex);
191
192 *pos += n;
193 remaining -= n;
194 start_sector += sectors;
195 offset = 0;
196 } while (remaining > 0);
197
198 return count;
199
200 fail:
201 mutex_unlock(&priv->mutex);
202 return res;
203 }
204
205 static ssize_t ps3flash_write(const char __user *userbuf,
206 const void *kernelbuf, size_t count, loff_t *pos)
207 {
208 struct ps3_storage_device *dev = ps3flash_dev;
209 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
210 u64 size, sector, offset;
211 int res = 0;
212 size_t remaining, n;
213 void *dst;
214
215 dev_dbg(&dev->sbd.core,
216 "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
217 __func__, __LINE__, count, *pos, userbuf, kernelbuf);
218
219 size = dev->regions[dev->region_idx].size*dev->blk_size;
220 if (*pos >= size || !count)
221 return 0;
222
223 if (*pos + count > size) {
224 dev_dbg(&dev->sbd.core,
225 "%s:%u Truncating count from %zu to %llu\n", __func__,
226 __LINE__, count, size - *pos);
227 count = size - *pos;
228 }
229
230 sector = *pos / dev->bounce_size * priv->chunk_sectors;
231 offset = *pos % dev->bounce_size;
232
233 remaining = count;
234 do {
235 n = min_t(u64, remaining, dev->bounce_size - offset);
236
237 mutex_lock(&priv->mutex);
238
239 if (n != dev->bounce_size)
240 res = ps3flash_fetch(dev, sector, priv->chunk_sectors);
241 else if (sector != priv->tag)
242 res = ps3flash_writeback(dev);
243 if (res)
244 goto fail;
245
246 dst = dev->bounce_buf + offset;
247 dev_dbg(&dev->sbd.core,
248 "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
249 __func__, __LINE__, n, userbuf, kernelbuf, dst);
250 if (userbuf) {
251 if (copy_from_user(dst, userbuf, n)) {
252 res = -EFAULT;
253 goto fail;
254 }
255 userbuf += n;
256 }
257 if (kernelbuf) {
258 memcpy(dst, kernelbuf, n);
259 kernelbuf += n;
260 }
261
262 priv->tag = sector;
263 priv->dirty = true;
264
265 mutex_unlock(&priv->mutex);
266
267 *pos += n;
268 remaining -= n;
269 sector += priv->chunk_sectors;
270 offset = 0;
271 } while (remaining > 0);
272
273 return count;
274
275 fail:
276 mutex_unlock(&priv->mutex);
277 return res;
278 }
279
280 static ssize_t ps3flash_user_read(struct file *file, char __user *buf,
281 size_t count, loff_t *pos)
282 {
283 return ps3flash_read(buf, NULL, count, pos);
284 }
285
286 static ssize_t ps3flash_user_write(struct file *file, const char __user *buf,
287 size_t count, loff_t *pos)
288 {
289 return ps3flash_write(buf, NULL, count, pos);
290 }
291
292 static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos)
293 {
294 return ps3flash_read(NULL, buf, count, &pos);
295 }
296
297 static ssize_t ps3flash_kernel_write(const void *buf, size_t count,
298 loff_t pos)
299 {
300 ssize_t res;
301 int wb;
302
303 res = ps3flash_write(NULL, buf, count, &pos);
304 if (res < 0)
305 return res;
306
307 /* Make kernel writes synchronous */
308 wb = ps3flash_writeback(ps3flash_dev);
309 if (wb)
310 return wb;
311
312 return res;
313 }
314
315 static int ps3flash_flush(struct file *file, fl_owner_t id)
316 {
317 return ps3flash_writeback(ps3flash_dev);
318 }
319
320 static int ps3flash_fsync(struct file *file, struct dentry *dentry,
321 int datasync)
322 {
323 return ps3flash_writeback(ps3flash_dev);
324 }
325
326 static irqreturn_t ps3flash_interrupt(int irq, void *data)
327 {
328 struct ps3_storage_device *dev = data;
329 int res;
330 u64 tag, status;
331
332 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
333
334 if (tag != dev->tag)
335 dev_err(&dev->sbd.core,
336 "%s:%u: tag mismatch, got %llx, expected %llx\n",
337 __func__, __LINE__, tag, dev->tag);
338
339 if (res) {
340 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
341 __func__, __LINE__, res, status);
342 } else {
343 dev->lv1_status = status;
344 complete(&dev->done);
345 }
346 return IRQ_HANDLED;
347 }
348
349 static const struct file_operations ps3flash_fops = {
350 .owner = THIS_MODULE,
351 .llseek = ps3flash_llseek,
352 .read = ps3flash_user_read,
353 .write = ps3flash_user_write,
354 .flush = ps3flash_flush,
355 .fsync = ps3flash_fsync,
356 };
357
358 static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = {
359 .read = ps3flash_kernel_read,
360 .write = ps3flash_kernel_write,
361 };
362
363 static struct miscdevice ps3flash_misc = {
364 .minor = MISC_DYNAMIC_MINOR,
365 .name = DEVICE_NAME,
366 .fops = &ps3flash_fops,
367 };
368
369 static int __devinit ps3flash_probe(struct ps3_system_bus_device *_dev)
370 {
371 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
372 struct ps3flash_private *priv;
373 int error;
374 unsigned long tmp;
375
376 tmp = dev->regions[dev->region_idx].start*dev->blk_size;
377 if (tmp % FLASH_BLOCK_SIZE) {
378 dev_err(&dev->sbd.core,
379 "%s:%u region start %lu is not aligned\n", __func__,
380 __LINE__, tmp);
381 return -EINVAL;
382 }
383 tmp = dev->regions[dev->region_idx].size*dev->blk_size;
384 if (tmp % FLASH_BLOCK_SIZE) {
385 dev_err(&dev->sbd.core,
386 "%s:%u region size %lu is not aligned\n", __func__,
387 __LINE__, tmp);
388 return -EINVAL;
389 }
390
391 /* use static buffer, kmalloc cannot allocate 256 KiB */
392 if (!ps3flash_bounce_buffer.address)
393 return -ENODEV;
394
395 if (ps3flash_dev) {
396 dev_err(&dev->sbd.core,
397 "Only one FLASH device is supported\n");
398 return -EBUSY;
399 }
400
401 ps3flash_dev = dev;
402
403 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
404 if (!priv) {
405 error = -ENOMEM;
406 goto fail;
407 }
408
409 ps3_system_bus_set_drvdata(&dev->sbd, priv);
410 mutex_init(&priv->mutex);
411 priv->tag = -1;
412
413 dev->bounce_size = ps3flash_bounce_buffer.size;
414 dev->bounce_buf = ps3flash_bounce_buffer.address;
415 priv->chunk_sectors = dev->bounce_size / dev->blk_size;
416
417 error = ps3stor_setup(dev, ps3flash_interrupt);
418 if (error)
419 goto fail_free_priv;
420
421 ps3flash_misc.parent = &dev->sbd.core;
422 error = misc_register(&ps3flash_misc);
423 if (error) {
424 dev_err(&dev->sbd.core, "%s:%u: misc_register failed %d\n",
425 __func__, __LINE__, error);
426 goto fail_teardown;
427 }
428
429 dev_info(&dev->sbd.core, "%s:%u: registered misc device %d\n",
430 __func__, __LINE__, ps3flash_misc.minor);
431
432 ps3_os_area_flash_register(&ps3flash_kernel_ops);
433 return 0;
434
435 fail_teardown:
436 ps3stor_teardown(dev);
437 fail_free_priv:
438 kfree(priv);
439 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
440 fail:
441 ps3flash_dev = NULL;
442 return error;
443 }
444
445 static int ps3flash_remove(struct ps3_system_bus_device *_dev)
446 {
447 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
448
449 ps3_os_area_flash_register(NULL);
450 misc_deregister(&ps3flash_misc);
451 ps3stor_teardown(dev);
452 kfree(ps3_system_bus_get_drvdata(&dev->sbd));
453 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
454 ps3flash_dev = NULL;
455 return 0;
456 }
457
458
459 static struct ps3_system_bus_driver ps3flash = {
460 .match_id = PS3_MATCH_ID_STOR_FLASH,
461 .core.name = DEVICE_NAME,
462 .core.owner = THIS_MODULE,
463 .probe = ps3flash_probe,
464 .remove = ps3flash_remove,
465 .shutdown = ps3flash_remove,
466 };
467
468
469 static int __init ps3flash_init(void)
470 {
471 return ps3_system_bus_driver_register(&ps3flash);
472 }
473
474 static void __exit ps3flash_exit(void)
475 {
476 ps3_system_bus_driver_unregister(&ps3flash);
477 }
478
479 module_init(ps3flash_init);
480 module_exit(ps3flash_exit);
481
482 MODULE_LICENSE("GPL");
483 MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
484 MODULE_AUTHOR("Sony Corporation");
485 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH);