blob: 85c004a518ee1d9196aebb28a75e5464fce6354b [file] [log] [blame]
Geert Uytterhoevenf9652632007-07-21 04:37:48 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070024#include <linux/uaccess.h>
25
26#include <asm/lv1call.h>
27#include <asm/ps3stor.h>
28
29
30#define DEVICE_NAME "ps3flash"
31
32#define FLASH_BLOCK_SIZE (256*1024)
33
34
35struct ps3flash_private {
36 struct mutex mutex; /* Bounce buffer mutex */
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000037 u64 chunk_sectors;
38 int tag; /* Start sector of buffer, -1 if invalid */
39 bool dirty;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070040};
41
42static struct ps3_storage_device *ps3flash_dev;
43
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000044static int ps3flash_read_write_sectors(struct ps3_storage_device *dev,
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000045 u64 start_sector, int write)
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070046{
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000047 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
48 u64 res = ps3stor_read_write_sectors(dev, dev->bounce_lpar,
49 start_sector, priv->chunk_sectors,
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070050 write);
51 if (res) {
Stephen Rothwell4c33d2d2009-01-13 20:06:02 +000052 dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070053 __LINE__, write ? "write" : "read", res);
54 return -EIO;
55 }
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000056 return 0;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070057}
58
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000059static int ps3flash_writeback(struct ps3_storage_device *dev)
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070060{
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000061 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
62 int res;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070063
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000064 if (!priv->dirty || priv->tag < 0)
65 return 0;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070066
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000067 res = ps3flash_read_write_sectors(dev, priv->tag, 1);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000068 if (res)
69 return res;
70
71 priv->dirty = false;
72 return 0;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070073}
74
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000075static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector)
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070076{
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000077 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000078 int res;
79
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000080 if (start_sector == priv->tag)
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000081 return 0;
82
83 res = ps3flash_writeback(dev);
84 if (res)
85 return res;
86
87 priv->tag = -1;
88
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000089 res = ps3flash_read_write_sectors(dev, start_sector, 0);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000090 if (res)
91 return res;
92
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +000093 priv->tag = start_sector;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +000094 return 0;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -070095}
96
97static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin)
98{
99 struct ps3_storage_device *dev = ps3flash_dev;
100 loff_t res;
101
102 mutex_lock(&file->f_mapping->host->i_mutex);
103 switch (origin) {
104 case 1:
105 offset += file->f_pos;
106 break;
107 case 2:
108 offset += dev->regions[dev->region_idx].size*dev->blk_size;
109 break;
110 }
111 if (offset < 0) {
112 res = -EINVAL;
113 goto out;
114 }
115
116 file->f_pos = offset;
117 res = file->f_pos;
118
119out:
120 mutex_unlock(&file->f_mapping->host->i_mutex);
121 return res;
122}
123
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000124static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf,
125 size_t count, loff_t *pos)
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700126{
127 struct ps3_storage_device *dev = ps3flash_dev;
Geert Uytterhoeven559dc872009-06-10 04:38:55 +0000128 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000129 u64 size, sector, offset;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000130 int res;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700131 size_t remaining, n;
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000132 const void *src;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700133
134 dev_dbg(&dev->sbd.core,
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000135 "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
136 __func__, __LINE__, count, *pos, userbuf, kernelbuf);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700137
138 size = dev->regions[dev->region_idx].size*dev->blk_size;
139 if (*pos >= size || !count)
140 return 0;
141
142 if (*pos + count > size) {
143 dev_dbg(&dev->sbd.core,
144 "%s:%u Truncating count from %zu to %llu\n", __func__,
145 __LINE__, count, size - *pos);
146 count = size - *pos;
147 }
148
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000149 sector = *pos / dev->bounce_size * priv->chunk_sectors;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000150 offset = *pos % dev->bounce_size;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700151
152 remaining = count;
153 do {
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000154 n = min_t(u64, remaining, dev->bounce_size - offset);
155 src = dev->bounce_buf + offset;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000156
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700157 mutex_lock(&priv->mutex);
158
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000159 res = ps3flash_fetch(dev, sector);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000160 if (res)
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700161 goto fail;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700162
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700163 dev_dbg(&dev->sbd.core,
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000164 "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
165 __func__, __LINE__, n, src, userbuf, kernelbuf);
166 if (userbuf) {
167 if (copy_to_user(userbuf, src, n)) {
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000168 res = -EFAULT;
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000169 goto fail;
170 }
171 userbuf += n;
172 }
173 if (kernelbuf) {
174 memcpy(kernelbuf, src, n);
175 kernelbuf += n;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700176 }
177
178 mutex_unlock(&priv->mutex);
179
180 *pos += n;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700181 remaining -= n;
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000182 sector += priv->chunk_sectors;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700183 offset = 0;
184 } while (remaining > 0);
185
186 return count;
187
188fail:
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000189 mutex_unlock(&priv->mutex);
190 return res;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700191}
192
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000193static ssize_t ps3flash_write(const char __user *userbuf,
194 const void *kernelbuf, size_t count, loff_t *pos)
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700195{
196 struct ps3_storage_device *dev = ps3flash_dev;
Geert Uytterhoeven559dc872009-06-10 04:38:55 +0000197 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000198 u64 size, sector, offset;
199 int res = 0;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700200 size_t remaining, n;
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000201 void *dst;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700202
203 dev_dbg(&dev->sbd.core,
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000204 "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
205 __func__, __LINE__, count, *pos, userbuf, kernelbuf);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700206
207 size = dev->regions[dev->region_idx].size*dev->blk_size;
208 if (*pos >= size || !count)
209 return 0;
210
211 if (*pos + count > size) {
212 dev_dbg(&dev->sbd.core,
213 "%s:%u Truncating count from %zu to %llu\n", __func__,
214 __LINE__, count, size - *pos);
215 count = size - *pos;
216 }
217
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000218 sector = *pos / dev->bounce_size * priv->chunk_sectors;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700219 offset = *pos % dev->bounce_size;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700220
221 remaining = count;
222 do {
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000223 n = min_t(u64, remaining, dev->bounce_size - offset);
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000224 dst = dev->bounce_buf + offset;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000225
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700226 mutex_lock(&priv->mutex);
227
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000228 if (n != dev->bounce_size)
Geert Uytterhoeven42e27bf2009-06-10 04:39:08 +0000229 res = ps3flash_fetch(dev, sector);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000230 else if (sector != priv->tag)
231 res = ps3flash_writeback(dev);
232 if (res)
233 goto fail;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700234
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700235 dev_dbg(&dev->sbd.core,
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000236 "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
237 __func__, __LINE__, n, userbuf, kernelbuf, dst);
238 if (userbuf) {
239 if (copy_from_user(dst, userbuf, n)) {
240 res = -EFAULT;
241 goto fail;
242 }
243 userbuf += n;
244 }
245 if (kernelbuf) {
246 memcpy(dst, kernelbuf, n);
247 kernelbuf += n;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700248 }
249
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000250 priv->tag = sector;
251 priv->dirty = true;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700252
253 mutex_unlock(&priv->mutex);
254
255 *pos += n;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700256 remaining -= n;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000257 sector += priv->chunk_sectors;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700258 offset = 0;
259 } while (remaining > 0);
260
261 return count;
262
263fail:
264 mutex_unlock(&priv->mutex);
265 return res;
266}
267
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000268static ssize_t ps3flash_user_read(struct file *file, char __user *buf,
269 size_t count, loff_t *pos)
270{
271 return ps3flash_read(buf, NULL, count, pos);
272}
273
274static ssize_t ps3flash_user_write(struct file *file, const char __user *buf,
275 size_t count, loff_t *pos)
276{
277 return ps3flash_write(buf, NULL, count, pos);
278}
279
280static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos)
281{
282 return ps3flash_read(NULL, buf, count, &pos);
283}
284
285static ssize_t ps3flash_kernel_write(const void *buf, size_t count,
286 loff_t pos)
287{
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000288 ssize_t res;
289 int wb;
290
291 res = ps3flash_write(NULL, buf, count, &pos);
292 if (res < 0)
293 return res;
294
295 /* Make kernel writes synchronous */
296 wb = ps3flash_writeback(ps3flash_dev);
297 if (wb)
298 return wb;
299
300 return res;
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000301}
302
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000303static int ps3flash_flush(struct file *file, fl_owner_t id)
304{
305 return ps3flash_writeback(ps3flash_dev);
306}
307
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200308static int ps3flash_fsync(struct file *file, int datasync)
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000309{
310 return ps3flash_writeback(ps3flash_dev);
311}
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700312
313static irqreturn_t ps3flash_interrupt(int irq, void *data)
314{
315 struct ps3_storage_device *dev = data;
316 int res;
317 u64 tag, status;
318
319 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
320
321 if (tag != dev->tag)
322 dev_err(&dev->sbd.core,
Stephen Rothwell4c33d2d2009-01-13 20:06:02 +0000323 "%s:%u: tag mismatch, got %llx, expected %llx\n",
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700324 __func__, __LINE__, tag, dev->tag);
325
326 if (res) {
Stephen Rothwell4c33d2d2009-01-13 20:06:02 +0000327 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700328 __func__, __LINE__, res, status);
329 } else {
330 dev->lv1_status = status;
331 complete(&dev->done);
332 }
333 return IRQ_HANDLED;
334}
335
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700336static const struct file_operations ps3flash_fops = {
337 .owner = THIS_MODULE,
338 .llseek = ps3flash_llseek,
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000339 .read = ps3flash_user_read,
340 .write = ps3flash_user_write,
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000341 .flush = ps3flash_flush,
342 .fsync = ps3flash_fsync,
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000343};
344
345static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = {
346 .read = ps3flash_kernel_read,
347 .write = ps3flash_kernel_write,
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700348};
349
350static struct miscdevice ps3flash_misc = {
351 .minor = MISC_DYNAMIC_MINOR,
352 .name = DEVICE_NAME,
353 .fops = &ps3flash_fops,
354};
355
356static int __devinit ps3flash_probe(struct ps3_system_bus_device *_dev)
357{
358 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
359 struct ps3flash_private *priv;
360 int error;
361 unsigned long tmp;
362
363 tmp = dev->regions[dev->region_idx].start*dev->blk_size;
364 if (tmp % FLASH_BLOCK_SIZE) {
365 dev_err(&dev->sbd.core,
366 "%s:%u region start %lu is not aligned\n", __func__,
367 __LINE__, tmp);
368 return -EINVAL;
369 }
370 tmp = dev->regions[dev->region_idx].size*dev->blk_size;
371 if (tmp % FLASH_BLOCK_SIZE) {
372 dev_err(&dev->sbd.core,
373 "%s:%u region size %lu is not aligned\n", __func__,
374 __LINE__, tmp);
375 return -EINVAL;
376 }
377
378 /* use static buffer, kmalloc cannot allocate 256 KiB */
379 if (!ps3flash_bounce_buffer.address)
380 return -ENODEV;
381
382 if (ps3flash_dev) {
383 dev_err(&dev->sbd.core,
384 "Only one FLASH device is supported\n");
385 return -EBUSY;
386 }
387
388 ps3flash_dev = dev;
389
390 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
391 if (!priv) {
392 error = -ENOMEM;
393 goto fail;
394 }
395
Geert Uytterhoeven559dc872009-06-10 04:38:55 +0000396 ps3_system_bus_set_drvdata(&dev->sbd, priv);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700397 mutex_init(&priv->mutex);
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000398 priv->tag = -1;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700399
400 dev->bounce_size = ps3flash_bounce_buffer.size;
401 dev->bounce_buf = ps3flash_bounce_buffer.address;
Geert Uytterhoeven6bd57f22009-06-10 04:39:07 +0000402 priv->chunk_sectors = dev->bounce_size / dev->blk_size;
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700403
404 error = ps3stor_setup(dev, ps3flash_interrupt);
405 if (error)
406 goto fail_free_priv;
407
408 ps3flash_misc.parent = &dev->sbd.core;
409 error = misc_register(&ps3flash_misc);
410 if (error) {
411 dev_err(&dev->sbd.core, "%s:%u: misc_register failed %d\n",
412 __func__, __LINE__, error);
413 goto fail_teardown;
414 }
415
416 dev_info(&dev->sbd.core, "%s:%u: registered misc device %d\n",
417 __func__, __LINE__, ps3flash_misc.minor);
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000418
419 ps3_os_area_flash_register(&ps3flash_kernel_ops);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700420 return 0;
421
422fail_teardown:
423 ps3stor_teardown(dev);
424fail_free_priv:
425 kfree(priv);
Geert Uytterhoeven559dc872009-06-10 04:38:55 +0000426 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700427fail:
428 ps3flash_dev = NULL;
429 return error;
430}
431
432static int ps3flash_remove(struct ps3_system_bus_device *_dev)
433{
434 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
435
Geert Uytterhoevena4e623f2009-06-10 04:39:06 +0000436 ps3_os_area_flash_register(NULL);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700437 misc_deregister(&ps3flash_misc);
438 ps3stor_teardown(dev);
Geert Uytterhoeven559dc872009-06-10 04:38:55 +0000439 kfree(ps3_system_bus_get_drvdata(&dev->sbd));
440 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
Geert Uytterhoevenf9652632007-07-21 04:37:48 -0700441 ps3flash_dev = NULL;
442 return 0;
443}
444
445
446static struct ps3_system_bus_driver ps3flash = {
447 .match_id = PS3_MATCH_ID_STOR_FLASH,
448 .core.name = DEVICE_NAME,
449 .core.owner = THIS_MODULE,
450 .probe = ps3flash_probe,
451 .remove = ps3flash_remove,
452 .shutdown = ps3flash_remove,
453};
454
455
456static int __init ps3flash_init(void)
457{
458 return ps3_system_bus_driver_register(&ps3flash);
459}
460
461static void __exit ps3flash_exit(void)
462{
463 ps3_system_bus_driver_unregister(&ps3flash);
464}
465
466module_init(ps3flash_init);
467module_exit(ps3flash_exit);
468
469MODULE_LICENSE("GPL");
470MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
471MODULE_AUTHOR("Sony Corporation");
472MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH);