ps3flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/lv1call.h>
  25. #include <asm/ps3stor.h>
  26. #define DEVICE_NAME "ps3flash"
  27. #define FLASH_BLOCK_SIZE (256*1024)
  28. struct ps3flash_private {
  29. struct mutex mutex; /* Bounce buffer mutex */
  30. u64 chunk_sectors;
  31. int tag; /* Start sector of buffer, -1 if invalid */
  32. bool dirty;
  33. };
  34. static struct ps3_storage_device *ps3flash_dev;
  35. static int ps3flash_read_write_sectors(struct ps3_storage_device *dev,
  36. u64 start_sector, int write)
  37. {
  38. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  39. u64 res = ps3stor_read_write_sectors(dev, dev->bounce_lpar,
  40. start_sector, priv->chunk_sectors,
  41. write);
  42. if (res) {
  43. dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
  44. __LINE__, write ? "write" : "read", res);
  45. return -EIO;
  46. }
  47. return 0;
  48. }
  49. static int ps3flash_writeback(struct ps3_storage_device *dev)
  50. {
  51. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  52. int res;
  53. if (!priv->dirty || priv->tag < 0)
  54. return 0;
  55. res = ps3flash_read_write_sectors(dev, priv->tag, 1);
  56. if (res)
  57. return res;
  58. priv->dirty = false;
  59. return 0;
  60. }
  61. static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector)
  62. {
  63. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  64. int res;
  65. if (start_sector == priv->tag)
  66. return 0;
  67. res = ps3flash_writeback(dev);
  68. if (res)
  69. return res;
  70. priv->tag = -1;
  71. res = ps3flash_read_write_sectors(dev, start_sector, 0);
  72. if (res)
  73. return res;
  74. priv->tag = start_sector;
  75. return 0;
  76. }
  77. static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin)
  78. {
  79. struct ps3_storage_device *dev = ps3flash_dev;
  80. loff_t res;
  81. mutex_lock(&file->f_mapping->host->i_mutex);
  82. switch (origin) {
  83. case 1:
  84. offset += file->f_pos;
  85. break;
  86. case 2:
  87. offset += dev->regions[dev->region_idx].size*dev->blk_size;
  88. break;
  89. }
  90. if (offset < 0) {
  91. res = -EINVAL;
  92. goto out;
  93. }
  94. file->f_pos = offset;
  95. res = file->f_pos;
  96. out:
  97. mutex_unlock(&file->f_mapping->host->i_mutex);
  98. return res;
  99. }
  100. static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf,
  101. size_t count, loff_t *pos)
  102. {
  103. struct ps3_storage_device *dev = ps3flash_dev;
  104. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  105. u64 size, sector, offset;
  106. int res;
  107. size_t remaining, n;
  108. const void *src;
  109. dev_dbg(&dev->sbd.core,
  110. "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
  111. __func__, __LINE__, count, *pos, userbuf, kernelbuf);
  112. size = dev->regions[dev->region_idx].size*dev->blk_size;
  113. if (*pos >= size || !count)
  114. return 0;
  115. if (*pos + count > size) {
  116. dev_dbg(&dev->sbd.core,
  117. "%s:%u Truncating count from %zu to %llu\n", __func__,
  118. __LINE__, count, size - *pos);
  119. count = size - *pos;
  120. }
  121. sector = *pos / dev->bounce_size * priv->chunk_sectors;
  122. offset = *pos % dev->bounce_size;
  123. remaining = count;
  124. do {
  125. n = min_t(u64, remaining, dev->bounce_size - offset);
  126. src = dev->bounce_buf + offset;
  127. mutex_lock(&priv->mutex);
  128. res = ps3flash_fetch(dev, sector);
  129. if (res)
  130. goto fail;
  131. dev_dbg(&dev->sbd.core,
  132. "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
  133. __func__, __LINE__, n, src, userbuf, kernelbuf);
  134. if (userbuf) {
  135. if (copy_to_user(userbuf, src, n)) {
  136. res = -EFAULT;
  137. goto fail;
  138. }
  139. userbuf += n;
  140. }
  141. if (kernelbuf) {
  142. memcpy(kernelbuf, src, n);
  143. kernelbuf += n;
  144. }
  145. mutex_unlock(&priv->mutex);
  146. *pos += n;
  147. remaining -= n;
  148. sector += priv->chunk_sectors;
  149. offset = 0;
  150. } while (remaining > 0);
  151. return count;
  152. fail:
  153. mutex_unlock(&priv->mutex);
  154. return res;
  155. }
  156. static ssize_t ps3flash_write(const char __user *userbuf,
  157. const void *kernelbuf, size_t count, loff_t *pos)
  158. {
  159. struct ps3_storage_device *dev = ps3flash_dev;
  160. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  161. u64 size, sector, offset;
  162. int res = 0;
  163. size_t remaining, n;
  164. void *dst;
  165. dev_dbg(&dev->sbd.core,
  166. "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
  167. __func__, __LINE__, count, *pos, userbuf, kernelbuf);
  168. size = dev->regions[dev->region_idx].size*dev->blk_size;
  169. if (*pos >= size || !count)
  170. return 0;
  171. if (*pos + count > size) {
  172. dev_dbg(&dev->sbd.core,
  173. "%s:%u Truncating count from %zu to %llu\n", __func__,
  174. __LINE__, count, size - *pos);
  175. count = size - *pos;
  176. }
  177. sector = *pos / dev->bounce_size * priv->chunk_sectors;
  178. offset = *pos % dev->bounce_size;
  179. remaining = count;
  180. do {
  181. n = min_t(u64, remaining, dev->bounce_size - offset);
  182. dst = dev->bounce_buf + offset;
  183. mutex_lock(&priv->mutex);
  184. if (n != dev->bounce_size)
  185. res = ps3flash_fetch(dev, sector);
  186. else if (sector != priv->tag)
  187. res = ps3flash_writeback(dev);
  188. if (res)
  189. goto fail;
  190. dev_dbg(&dev->sbd.core,
  191. "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
  192. __func__, __LINE__, n, userbuf, kernelbuf, dst);
  193. if (userbuf) {
  194. if (copy_from_user(dst, userbuf, n)) {
  195. res = -EFAULT;
  196. goto fail;
  197. }
  198. userbuf += n;
  199. }
  200. if (kernelbuf) {
  201. memcpy(dst, kernelbuf, n);
  202. kernelbuf += n;
  203. }
  204. priv->tag = sector;
  205. priv->dirty = true;
  206. mutex_unlock(&priv->mutex);
  207. *pos += n;
  208. remaining -= n;
  209. sector += priv->chunk_sectors;
  210. offset = 0;
  211. } while (remaining > 0);
  212. return count;
  213. fail:
  214. mutex_unlock(&priv->mutex);
  215. return res;
  216. }
  217. static ssize_t ps3flash_user_read(struct file *file, char __user *buf,
  218. size_t count, loff_t *pos)
  219. {
  220. return ps3flash_read(buf, NULL, count, pos);
  221. }
  222. static ssize_t ps3flash_user_write(struct file *file, const char __user *buf,
  223. size_t count, loff_t *pos)
  224. {
  225. return ps3flash_write(buf, NULL, count, pos);
  226. }
  227. static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos)
  228. {
  229. return ps3flash_read(NULL, buf, count, &pos);
  230. }
  231. static ssize_t ps3flash_kernel_write(const void *buf, size_t count,
  232. loff_t pos)
  233. {
  234. ssize_t res;
  235. int wb;
  236. res = ps3flash_write(NULL, buf, count, &pos);
  237. if (res < 0)
  238. return res;
  239. /* Make kernel writes synchronous */
  240. wb = ps3flash_writeback(ps3flash_dev);
  241. if (wb)
  242. return wb;
  243. return res;
  244. }
  245. static int ps3flash_flush(struct file *file, fl_owner_t id)
  246. {
  247. return ps3flash_writeback(ps3flash_dev);
  248. }
  249. static int ps3flash_fsync(struct file *file, int datasync)
  250. {
  251. return ps3flash_writeback(ps3flash_dev);
  252. }
  253. static irqreturn_t ps3flash_interrupt(int irq, void *data)
  254. {
  255. struct ps3_storage_device *dev = data;
  256. int res;
  257. u64 tag, status;
  258. res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
  259. if (tag != dev->tag)
  260. dev_err(&dev->sbd.core,
  261. "%s:%u: tag mismatch, got %llx, expected %llx\n",
  262. __func__, __LINE__, tag, dev->tag);
  263. if (res) {
  264. dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
  265. __func__, __LINE__, res, status);
  266. } else {
  267. dev->lv1_status = status;
  268. complete(&dev->done);
  269. }
  270. return IRQ_HANDLED;
  271. }
  272. static const struct file_operations ps3flash_fops = {
  273. .owner = THIS_MODULE,
  274. .llseek = ps3flash_llseek,
  275. .read = ps3flash_user_read,
  276. .write = ps3flash_user_write,
  277. .flush = ps3flash_flush,
  278. .fsync = ps3flash_fsync,
  279. };
  280. static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = {
  281. .read = ps3flash_kernel_read,
  282. .write = ps3flash_kernel_write,
  283. };
  284. static struct miscdevice ps3flash_misc = {
  285. .minor = MISC_DYNAMIC_MINOR,
  286. .name = DEVICE_NAME,
  287. .fops = &ps3flash_fops,
  288. };
  289. static int __devinit ps3flash_probe(struct ps3_system_bus_device *_dev)
  290. {
  291. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  292. struct ps3flash_private *priv;
  293. int error;
  294. unsigned long tmp;
  295. tmp = dev->regions[dev->region_idx].start*dev->blk_size;
  296. if (tmp % FLASH_BLOCK_SIZE) {
  297. dev_err(&dev->sbd.core,
  298. "%s:%u region start %lu is not aligned\n", __func__,
  299. __LINE__, tmp);
  300. return -EINVAL;
  301. }
  302. tmp = dev->regions[dev->region_idx].size*dev->blk_size;
  303. if (tmp % FLASH_BLOCK_SIZE) {
  304. dev_err(&dev->sbd.core,
  305. "%s:%u region size %lu is not aligned\n", __func__,
  306. __LINE__, tmp);
  307. return -EINVAL;
  308. }
  309. /* use static buffer, kmalloc cannot allocate 256 KiB */
  310. if (!ps3flash_bounce_buffer.address)
  311. return -ENODEV;
  312. if (ps3flash_dev) {
  313. dev_err(&dev->sbd.core,
  314. "Only one FLASH device is supported\n");
  315. return -EBUSY;
  316. }
  317. ps3flash_dev = dev;
  318. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  319. if (!priv) {
  320. error = -ENOMEM;
  321. goto fail;
  322. }
  323. ps3_system_bus_set_drvdata(&dev->sbd, priv);
  324. mutex_init(&priv->mutex);
  325. priv->tag = -1;
  326. dev->bounce_size = ps3flash_bounce_buffer.size;
  327. dev->bounce_buf = ps3flash_bounce_buffer.address;
  328. priv->chunk_sectors = dev->bounce_size / dev->blk_size;
  329. error = ps3stor_setup(dev, ps3flash_interrupt);
  330. if (error)
  331. goto fail_free_priv;
  332. ps3flash_misc.parent = &dev->sbd.core;
  333. error = misc_register(&ps3flash_misc);
  334. if (error) {
  335. dev_err(&dev->sbd.core, "%s:%u: misc_register failed %d\n",
  336. __func__, __LINE__, error);
  337. goto fail_teardown;
  338. }
  339. dev_info(&dev->sbd.core, "%s:%u: registered misc device %d\n",
  340. __func__, __LINE__, ps3flash_misc.minor);
  341. ps3_os_area_flash_register(&ps3flash_kernel_ops);
  342. return 0;
  343. fail_teardown:
  344. ps3stor_teardown(dev);
  345. fail_free_priv:
  346. kfree(priv);
  347. ps3_system_bus_set_drvdata(&dev->sbd, NULL);
  348. fail:
  349. ps3flash_dev = NULL;
  350. return error;
  351. }
  352. static int ps3flash_remove(struct ps3_system_bus_device *_dev)
  353. {
  354. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  355. ps3_os_area_flash_register(NULL);
  356. misc_deregister(&ps3flash_misc);
  357. ps3stor_teardown(dev);
  358. kfree(ps3_system_bus_get_drvdata(&dev->sbd));
  359. ps3_system_bus_set_drvdata(&dev->sbd, NULL);
  360. ps3flash_dev = NULL;
  361. return 0;
  362. }
  363. static struct ps3_system_bus_driver ps3flash = {
  364. .match_id = PS3_MATCH_ID_STOR_FLASH,
  365. .core.name = DEVICE_NAME,
  366. .core.owner = THIS_MODULE,
  367. .probe = ps3flash_probe,
  368. .remove = ps3flash_remove,
  369. .shutdown = ps3flash_remove,
  370. };
  371. static int __init ps3flash_init(void)
  372. {
  373. return ps3_system_bus_driver_register(&ps3flash);
  374. }
  375. static void __exit ps3flash_exit(void)
  376. {
  377. ps3_system_bus_driver_unregister(&ps3flash);
  378. }
  379. module_init(ps3flash_init);
  380. module_exit(ps3flash_exit);
  381. MODULE_LICENSE("GPL");
  382. MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
  383. MODULE_AUTHOR("Sony Corporation");
  384. MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH);