ps3flash.c 11 KB

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