ps3flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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/uaccess.h>
  23. #include <asm/lv1call.h>
  24. #include <asm/ps3stor.h>
  25. #define DEVICE_NAME "ps3flash"
  26. #define FLASH_BLOCK_SIZE (256*1024)
  27. struct ps3flash_private {
  28. struct mutex mutex; /* Bounce buffer mutex */
  29. u64 chunk_sectors;
  30. int tag; /* Start sector of buffer, -1 if invalid */
  31. bool dirty;
  32. };
  33. static struct ps3_storage_device *ps3flash_dev;
  34. static int ps3flash_read_write_sectors(struct ps3_storage_device *dev,
  35. u64 start_sector, int write)
  36. {
  37. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  38. u64 res = ps3stor_read_write_sectors(dev, dev->bounce_lpar,
  39. start_sector, priv->chunk_sectors,
  40. write);
  41. if (res) {
  42. dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
  43. __LINE__, write ? "write" : "read", res);
  44. return -EIO;
  45. }
  46. return 0;
  47. }
  48. static int ps3flash_writeback(struct ps3_storage_device *dev)
  49. {
  50. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  51. int res;
  52. if (!priv->dirty || priv->tag < 0)
  53. return 0;
  54. res = ps3flash_read_write_sectors(dev, priv->tag, 1);
  55. if (res)
  56. return res;
  57. priv->dirty = false;
  58. return 0;
  59. }
  60. static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector)
  61. {
  62. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  63. int res;
  64. if (start_sector == priv->tag)
  65. return 0;
  66. res = ps3flash_writeback(dev);
  67. if (res)
  68. return res;
  69. priv->tag = -1;
  70. res = ps3flash_read_write_sectors(dev, start_sector, 0);
  71. if (res)
  72. return res;
  73. priv->tag = start_sector;
  74. return 0;
  75. }
  76. static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin)
  77. {
  78. struct ps3_storage_device *dev = ps3flash_dev;
  79. loff_t res;
  80. mutex_lock(&file->f_mapping->host->i_mutex);
  81. switch (origin) {
  82. case 1:
  83. offset += file->f_pos;
  84. break;
  85. case 2:
  86. offset += dev->regions[dev->region_idx].size*dev->blk_size;
  87. break;
  88. }
  89. if (offset < 0) {
  90. res = -EINVAL;
  91. goto out;
  92. }
  93. file->f_pos = offset;
  94. res = file->f_pos;
  95. out:
  96. mutex_unlock(&file->f_mapping->host->i_mutex);
  97. return res;
  98. }
  99. static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf,
  100. size_t count, loff_t *pos)
  101. {
  102. struct ps3_storage_device *dev = ps3flash_dev;
  103. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  104. u64 size, sector, offset;
  105. int res;
  106. size_t remaining, n;
  107. const void *src;
  108. dev_dbg(&dev->sbd.core,
  109. "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
  110. __func__, __LINE__, count, *pos, userbuf, kernelbuf);
  111. size = dev->regions[dev->region_idx].size*dev->blk_size;
  112. if (*pos >= size || !count)
  113. return 0;
  114. if (*pos + count > size) {
  115. dev_dbg(&dev->sbd.core,
  116. "%s:%u Truncating count from %zu to %llu\n", __func__,
  117. __LINE__, count, size - *pos);
  118. count = size - *pos;
  119. }
  120. sector = *pos / dev->bounce_size * priv->chunk_sectors;
  121. offset = *pos % dev->bounce_size;
  122. remaining = count;
  123. do {
  124. n = min_t(u64, remaining, dev->bounce_size - offset);
  125. src = dev->bounce_buf + offset;
  126. mutex_lock(&priv->mutex);
  127. res = ps3flash_fetch(dev, sector);
  128. if (res)
  129. goto fail;
  130. dev_dbg(&dev->sbd.core,
  131. "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
  132. __func__, __LINE__, n, src, userbuf, kernelbuf);
  133. if (userbuf) {
  134. if (copy_to_user(userbuf, src, n)) {
  135. res = -EFAULT;
  136. goto fail;
  137. }
  138. userbuf += n;
  139. }
  140. if (kernelbuf) {
  141. memcpy(kernelbuf, src, n);
  142. kernelbuf += n;
  143. }
  144. mutex_unlock(&priv->mutex);
  145. *pos += n;
  146. remaining -= n;
  147. sector += priv->chunk_sectors;
  148. offset = 0;
  149. } while (remaining > 0);
  150. return count;
  151. fail:
  152. mutex_unlock(&priv->mutex);
  153. return res;
  154. }
  155. static ssize_t ps3flash_write(const char __user *userbuf,
  156. const void *kernelbuf, size_t count, loff_t *pos)
  157. {
  158. struct ps3_storage_device *dev = ps3flash_dev;
  159. struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  160. u64 size, sector, offset;
  161. int res = 0;
  162. size_t remaining, n;
  163. void *dst;
  164. dev_dbg(&dev->sbd.core,
  165. "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
  166. __func__, __LINE__, count, *pos, userbuf, kernelbuf);
  167. size = dev->regions[dev->region_idx].size*dev->blk_size;
  168. if (*pos >= size || !count)
  169. return 0;
  170. if (*pos + count > size) {
  171. dev_dbg(&dev->sbd.core,
  172. "%s:%u Truncating count from %zu to %llu\n", __func__,
  173. __LINE__, count, size - *pos);
  174. count = size - *pos;
  175. }
  176. sector = *pos / dev->bounce_size * priv->chunk_sectors;
  177. offset = *pos % dev->bounce_size;
  178. remaining = count;
  179. do {
  180. n = min_t(u64, remaining, dev->bounce_size - offset);
  181. dst = dev->bounce_buf + offset;
  182. mutex_lock(&priv->mutex);
  183. if (n != dev->bounce_size)
  184. res = ps3flash_fetch(dev, sector);
  185. else if (sector != priv->tag)
  186. res = ps3flash_writeback(dev);
  187. if (res)
  188. goto fail;
  189. dev_dbg(&dev->sbd.core,
  190. "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
  191. __func__, __LINE__, n, userbuf, kernelbuf, dst);
  192. if (userbuf) {
  193. if (copy_from_user(dst, userbuf, n)) {
  194. res = -EFAULT;
  195. goto fail;
  196. }
  197. userbuf += n;
  198. }
  199. if (kernelbuf) {
  200. memcpy(dst, kernelbuf, n);
  201. kernelbuf += n;
  202. }
  203. priv->tag = sector;
  204. priv->dirty = true;
  205. mutex_unlock(&priv->mutex);
  206. *pos += n;
  207. remaining -= n;
  208. sector += priv->chunk_sectors;
  209. offset = 0;
  210. } while (remaining > 0);
  211. return count;
  212. fail:
  213. mutex_unlock(&priv->mutex);
  214. return res;
  215. }
  216. static ssize_t ps3flash_user_read(struct file *file, char __user *buf,
  217. size_t count, loff_t *pos)
  218. {
  219. return ps3flash_read(buf, NULL, count, pos);
  220. }
  221. static ssize_t ps3flash_user_write(struct file *file, const char __user *buf,
  222. size_t count, loff_t *pos)
  223. {
  224. return ps3flash_write(buf, NULL, count, pos);
  225. }
  226. static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos)
  227. {
  228. return ps3flash_read(NULL, buf, count, &pos);
  229. }
  230. static ssize_t ps3flash_kernel_write(const void *buf, size_t count,
  231. loff_t pos)
  232. {
  233. ssize_t res;
  234. int wb;
  235. res = ps3flash_write(NULL, buf, count, &pos);
  236. if (res < 0)
  237. return res;
  238. /* Make kernel writes synchronous */
  239. wb = ps3flash_writeback(ps3flash_dev);
  240. if (wb)
  241. return wb;
  242. return res;
  243. }
  244. static int ps3flash_flush(struct file *file, fl_owner_t id)
  245. {
  246. return ps3flash_writeback(ps3flash_dev);
  247. }
  248. static int ps3flash_fsync(struct file *file, struct dentry *dentry,
  249. 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);