ps3flash.c 11 KB

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