ps3flash.c 11 KB

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