ps3flash.c 10 KB

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