ps3flash.c 11 KB

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