Commit | Line | Data |
---|---|---|
f9652635 GU |
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 */ | |
6bd57f2e GU |
36 | u64 chunk_sectors; |
37 | int tag; /* Start sector of buffer, -1 if invalid */ | |
38 | bool dirty; | |
f9652635 GU |
39 | }; |
40 | ||
41 | static struct ps3_storage_device *ps3flash_dev; | |
42 | ||
6bd57f2e GU |
43 | static int ps3flash_read_write_sectors(struct ps3_storage_device *dev, |
44 | u64 lpar, u64 start_sector, u64 sectors, | |
45 | int write) | |
f9652635 GU |
46 | { |
47 | u64 res = ps3stor_read_write_sectors(dev, lpar, start_sector, sectors, | |
48 | write); | |
49 | if (res) { | |
4c33d2dc | 50 | dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__, |
f9652635 GU |
51 | __LINE__, write ? "write" : "read", res); |
52 | return -EIO; | |
53 | } | |
6bd57f2e | 54 | return 0; |
f9652635 GU |
55 | } |
56 | ||
6bd57f2e | 57 | static int ps3flash_writeback(struct ps3_storage_device *dev) |
f9652635 | 58 | { |
6bd57f2e GU |
59 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
60 | int res; | |
f9652635 | 61 | |
6bd57f2e GU |
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; | |
f9652635 | 69 | |
6bd57f2e GU |
70 | priv->dirty = false; |
71 | return 0; | |
f9652635 GU |
72 | } |
73 | ||
6bd57f2e GU |
74 | static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector, |
75 | u64 sectors) | |
f9652635 | 76 | { |
6bd57f2e GU |
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; | |
f9652635 GU |
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 | ||
a4e623fb GU |
132 | static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf, |
133 | size_t count, loff_t *pos) | |
f9652635 GU |
134 | { |
135 | struct ps3_storage_device *dev = ps3flash_dev; | |
559dc87f | 136 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
6bd57f2e GU |
137 | u64 size, start_sector, end_sector, offset, sectors; |
138 | int res; | |
f9652635 | 139 | size_t remaining, n; |
a4e623fb | 140 | const void *src; |
f9652635 GU |
141 | |
142 | dev_dbg(&dev->sbd.core, | |
a4e623fb GU |
143 | "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n", |
144 | __func__, __LINE__, count, *pos, userbuf, kernelbuf); | |
f9652635 GU |
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; | |
6bd57f2e | 158 | offset = *pos % dev->bounce_size; |
f9652635 GU |
159 | end_sector = DIV_ROUND_UP(*pos + count, dev->blk_size); |
160 | ||
161 | remaining = count; | |
162 | do { | |
6bd57f2e GU |
163 | sectors = min(end_sector - start_sector, |
164 | priv->chunk_sectors - | |
165 | start_sector % priv->chunk_sectors); | |
166 | ||
f9652635 GU |
167 | mutex_lock(&priv->mutex); |
168 | ||
6bd57f2e GU |
169 | res = ps3flash_fetch(dev, start_sector, sectors); |
170 | if (res) | |
f9652635 | 171 | goto fail; |
f9652635 | 172 | |
6bd57f2e GU |
173 | n = min_t(u64, remaining, dev->bounce_size - offset); |
174 | src = dev->bounce_buf + offset; | |
f9652635 | 175 | dev_dbg(&dev->sbd.core, |
a4e623fb GU |
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)) { | |
6bd57f2e | 180 | res = -EFAULT; |
a4e623fb GU |
181 | goto fail; |
182 | } | |
183 | userbuf += n; | |
184 | } | |
185 | if (kernelbuf) { | |
186 | memcpy(kernelbuf, src, n); | |
187 | kernelbuf += n; | |
f9652635 GU |
188 | } |
189 | ||
190 | mutex_unlock(&priv->mutex); | |
191 | ||
192 | *pos += n; | |
f9652635 | 193 | remaining -= n; |
6bd57f2e | 194 | start_sector += sectors; |
f9652635 GU |
195 | offset = 0; |
196 | } while (remaining > 0); | |
197 | ||
198 | return count; | |
199 | ||
200 | fail: | |
6bd57f2e GU |
201 | mutex_unlock(&priv->mutex); |
202 | return res; | |
f9652635 GU |
203 | } |
204 | ||
a4e623fb GU |
205 | static ssize_t ps3flash_write(const char __user *userbuf, |
206 | const void *kernelbuf, size_t count, loff_t *pos) | |
f9652635 GU |
207 | { |
208 | struct ps3_storage_device *dev = ps3flash_dev; | |
559dc87f | 209 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
6bd57f2e GU |
210 | u64 size, sector, offset; |
211 | int res = 0; | |
f9652635 | 212 | size_t remaining, n; |
a4e623fb | 213 | void *dst; |
f9652635 GU |
214 | |
215 | dev_dbg(&dev->sbd.core, | |
a4e623fb GU |
216 | "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n", |
217 | __func__, __LINE__, count, *pos, userbuf, kernelbuf); | |
f9652635 GU |
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 | ||
6bd57f2e | 230 | sector = *pos / dev->bounce_size * priv->chunk_sectors; |
f9652635 | 231 | offset = *pos % dev->bounce_size; |
f9652635 GU |
232 | |
233 | remaining = count; | |
234 | do { | |
6bd57f2e GU |
235 | n = min_t(u64, remaining, dev->bounce_size - offset); |
236 | ||
f9652635 GU |
237 | mutex_lock(&priv->mutex); |
238 | ||
6bd57f2e GU |
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; | |
f9652635 | 245 | |
6bd57f2e | 246 | dst = dev->bounce_buf + offset; |
f9652635 | 247 | dev_dbg(&dev->sbd.core, |
a4e623fb GU |
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; | |
f9652635 GU |
260 | } |
261 | ||
6bd57f2e GU |
262 | priv->tag = sector; |
263 | priv->dirty = true; | |
f9652635 GU |
264 | |
265 | mutex_unlock(&priv->mutex); | |
266 | ||
267 | *pos += n; | |
f9652635 | 268 | remaining -= n; |
6bd57f2e | 269 | sector += priv->chunk_sectors; |
f9652635 GU |
270 | offset = 0; |
271 | } while (remaining > 0); | |
272 | ||
273 | return count; | |
274 | ||
275 | fail: | |
276 | mutex_unlock(&priv->mutex); | |
277 | return res; | |
278 | } | |
279 | ||
a4e623fb GU |
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 | { | |
6bd57f2e GU |
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; | |
a4e623fb GU |
313 | } |
314 | ||
6bd57f2e GU |
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 | } | |
f9652635 GU |
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, | |
4c33d2dc | 336 | "%s:%u: tag mismatch, got %llx, expected %llx\n", |
f9652635 GU |
337 | __func__, __LINE__, tag, dev->tag); |
338 | ||
339 | if (res) { | |
4c33d2dc | 340 | dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n", |
f9652635 GU |
341 | __func__, __LINE__, res, status); |
342 | } else { | |
343 | dev->lv1_status = status; | |
344 | complete(&dev->done); | |
345 | } | |
346 | return IRQ_HANDLED; | |
347 | } | |
348 | ||
f9652635 GU |
349 | static const struct file_operations ps3flash_fops = { |
350 | .owner = THIS_MODULE, | |
351 | .llseek = ps3flash_llseek, | |
a4e623fb GU |
352 | .read = ps3flash_user_read, |
353 | .write = ps3flash_user_write, | |
6bd57f2e GU |
354 | .flush = ps3flash_flush, |
355 | .fsync = ps3flash_fsync, | |
a4e623fb GU |
356 | }; |
357 | ||
358 | static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = { | |
359 | .read = ps3flash_kernel_read, | |
360 | .write = ps3flash_kernel_write, | |
f9652635 GU |
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 | ||
559dc87f | 409 | ps3_system_bus_set_drvdata(&dev->sbd, priv); |
f9652635 | 410 | mutex_init(&priv->mutex); |
6bd57f2e | 411 | priv->tag = -1; |
f9652635 GU |
412 | |
413 | dev->bounce_size = ps3flash_bounce_buffer.size; | |
414 | dev->bounce_buf = ps3flash_bounce_buffer.address; | |
6bd57f2e | 415 | priv->chunk_sectors = dev->bounce_size / dev->blk_size; |
f9652635 GU |
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); | |
a4e623fb GU |
431 | |
432 | ps3_os_area_flash_register(&ps3flash_kernel_ops); | |
f9652635 GU |
433 | return 0; |
434 | ||
435 | fail_teardown: | |
436 | ps3stor_teardown(dev); | |
437 | fail_free_priv: | |
438 | kfree(priv); | |
559dc87f | 439 | ps3_system_bus_set_drvdata(&dev->sbd, NULL); |
f9652635 GU |
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 | ||
a4e623fb | 449 | ps3_os_area_flash_register(NULL); |
f9652635 GU |
450 | misc_deregister(&ps3flash_misc); |
451 | ps3stor_teardown(dev); | |
559dc87f GU |
452 | kfree(ps3_system_bus_get_drvdata(&dev->sbd)); |
453 | ps3_system_bus_set_drvdata(&dev->sbd, NULL); | |
f9652635 GU |
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); |