ps3stor_lib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * PS3 Storage Library
  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/dma-mapping.h>
  21. #include <linux/module.h>
  22. #include <asm/lv1call.h>
  23. #include <asm/ps3stor.h>
  24. /*
  25. * A workaround for flash memory I/O errors when the internal hard disk
  26. * has not been formatted for OtherOS use. Delay disk close until flash
  27. * memory is closed.
  28. */
  29. static struct ps3_flash_workaround {
  30. int flash_open;
  31. int disk_open;
  32. struct ps3_system_bus_device *disk_sbd;
  33. } ps3_flash_workaround;
  34. static int ps3stor_open_hv_device(struct ps3_system_bus_device *sbd)
  35. {
  36. int error = ps3_open_hv_device(sbd);
  37. if (error)
  38. return error;
  39. if (sbd->match_id == PS3_MATCH_ID_STOR_FLASH)
  40. ps3_flash_workaround.flash_open = 1;
  41. if (sbd->match_id == PS3_MATCH_ID_STOR_DISK)
  42. ps3_flash_workaround.disk_open = 1;
  43. return 0;
  44. }
  45. static int ps3stor_close_hv_device(struct ps3_system_bus_device *sbd)
  46. {
  47. int error;
  48. if (sbd->match_id == PS3_MATCH_ID_STOR_DISK
  49. && ps3_flash_workaround.disk_open
  50. && ps3_flash_workaround.flash_open) {
  51. ps3_flash_workaround.disk_sbd = sbd;
  52. return 0;
  53. }
  54. error = ps3_close_hv_device(sbd);
  55. if (error)
  56. return error;
  57. if (sbd->match_id == PS3_MATCH_ID_STOR_DISK)
  58. ps3_flash_workaround.disk_open = 0;
  59. if (sbd->match_id == PS3_MATCH_ID_STOR_FLASH) {
  60. ps3_flash_workaround.flash_open = 0;
  61. if (ps3_flash_workaround.disk_sbd) {
  62. ps3_close_hv_device(ps3_flash_workaround.disk_sbd);
  63. ps3_flash_workaround.disk_open = 0;
  64. ps3_flash_workaround.disk_sbd = NULL;
  65. }
  66. }
  67. return 0;
  68. }
  69. static int ps3stor_probe_access(struct ps3_storage_device *dev)
  70. {
  71. int res, error;
  72. unsigned int i;
  73. unsigned long n;
  74. if (dev->sbd.match_id == PS3_MATCH_ID_STOR_ROM) {
  75. /* special case: CD-ROM is assumed always accessible */
  76. dev->accessible_regions = 1;
  77. return 0;
  78. }
  79. error = -EPERM;
  80. for (i = 0; i < dev->num_regions; i++) {
  81. dev_dbg(&dev->sbd.core,
  82. "%s:%u: checking accessibility of region %u\n",
  83. __func__, __LINE__, i);
  84. dev->region_idx = i;
  85. res = ps3stor_read_write_sectors(dev, dev->bounce_lpar, 0, 1,
  86. 0);
  87. if (res) {
  88. dev_dbg(&dev->sbd.core, "%s:%u: read failed, "
  89. "region %u is not accessible\n", __func__,
  90. __LINE__, i);
  91. continue;
  92. }
  93. dev_dbg(&dev->sbd.core, "%s:%u: region %u is accessible\n",
  94. __func__, __LINE__, i);
  95. set_bit(i, &dev->accessible_regions);
  96. /* We can access at least one region */
  97. error = 0;
  98. }
  99. if (error)
  100. return error;
  101. n = hweight_long(dev->accessible_regions);
  102. if (n > 1)
  103. dev_info(&dev->sbd.core,
  104. "%s:%u: %lu accessible regions found. Only the first "
  105. "one will be used\n",
  106. __func__, __LINE__, n);
  107. dev->region_idx = __ffs(dev->accessible_regions);
  108. dev_info(&dev->sbd.core,
  109. "First accessible region has index %u start %llu size %llu\n",
  110. dev->region_idx, dev->regions[dev->region_idx].start,
  111. dev->regions[dev->region_idx].size);
  112. return 0;
  113. }
  114. /**
  115. * ps3stor_setup - Setup a storage device before use
  116. * @dev: Pointer to a struct ps3_storage_device
  117. * @handler: Pointer to an interrupt handler
  118. *
  119. * Returns 0 for success, or an error code
  120. */
  121. int ps3stor_setup(struct ps3_storage_device *dev, irq_handler_t handler)
  122. {
  123. int error, res, alignment;
  124. enum ps3_dma_page_size page_size;
  125. error = ps3stor_open_hv_device(&dev->sbd);
  126. if (error) {
  127. dev_err(&dev->sbd.core,
  128. "%s:%u: ps3_open_hv_device failed %d\n", __func__,
  129. __LINE__, error);
  130. goto fail;
  131. }
  132. error = ps3_sb_event_receive_port_setup(&dev->sbd, PS3_BINDING_CPU_ANY,
  133. &dev->irq);
  134. if (error) {
  135. dev_err(&dev->sbd.core,
  136. "%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
  137. __func__, __LINE__, error);
  138. goto fail_close_device;
  139. }
  140. error = request_irq(dev->irq, handler, 0,
  141. dev->sbd.core.driver->name, dev);
  142. if (error) {
  143. dev_err(&dev->sbd.core, "%s:%u: request_irq failed %d\n",
  144. __func__, __LINE__, error);
  145. goto fail_sb_event_receive_port_destroy;
  146. }
  147. alignment = min(__ffs(dev->bounce_size),
  148. __ffs((unsigned long)dev->bounce_buf));
  149. if (alignment < 12) {
  150. dev_err(&dev->sbd.core,
  151. "%s:%u: bounce buffer not aligned (%lx at 0x%p)\n",
  152. __func__, __LINE__, dev->bounce_size, dev->bounce_buf);
  153. error = -EINVAL;
  154. goto fail_free_irq;
  155. } else if (alignment < 16)
  156. page_size = PS3_DMA_4K;
  157. else
  158. page_size = PS3_DMA_64K;
  159. dev->sbd.d_region = &dev->dma_region;
  160. ps3_dma_region_init(&dev->sbd, &dev->dma_region, page_size,
  161. PS3_DMA_OTHER, dev->bounce_buf, dev->bounce_size);
  162. res = ps3_dma_region_create(&dev->dma_region);
  163. if (res) {
  164. dev_err(&dev->sbd.core, "%s:%u: cannot create DMA region\n",
  165. __func__, __LINE__);
  166. error = -ENOMEM;
  167. goto fail_free_irq;
  168. }
  169. dev->bounce_lpar = ps3_mm_phys_to_lpar(__pa(dev->bounce_buf));
  170. dev->bounce_dma = dma_map_single(&dev->sbd.core, dev->bounce_buf,
  171. dev->bounce_size, DMA_BIDIRECTIONAL);
  172. if (!dev->bounce_dma) {
  173. dev_err(&dev->sbd.core, "%s:%u: map DMA region failed\n",
  174. __func__, __LINE__);
  175. error = -ENODEV;
  176. goto fail_free_dma;
  177. }
  178. error = ps3stor_probe_access(dev);
  179. if (error) {
  180. dev_err(&dev->sbd.core, "%s:%u: No accessible regions found\n",
  181. __func__, __LINE__);
  182. goto fail_unmap_dma;
  183. }
  184. return 0;
  185. fail_unmap_dma:
  186. dma_unmap_single(&dev->sbd.core, dev->bounce_dma, dev->bounce_size,
  187. DMA_BIDIRECTIONAL);
  188. fail_free_dma:
  189. ps3_dma_region_free(&dev->dma_region);
  190. fail_free_irq:
  191. free_irq(dev->irq, dev);
  192. fail_sb_event_receive_port_destroy:
  193. ps3_sb_event_receive_port_destroy(&dev->sbd, dev->irq);
  194. fail_close_device:
  195. ps3stor_close_hv_device(&dev->sbd);
  196. fail:
  197. return error;
  198. }
  199. EXPORT_SYMBOL_GPL(ps3stor_setup);
  200. /**
  201. * ps3stor_teardown - Tear down a storage device after use
  202. * @dev: Pointer to a struct ps3_storage_device
  203. */
  204. void ps3stor_teardown(struct ps3_storage_device *dev)
  205. {
  206. int error;
  207. dma_unmap_single(&dev->sbd.core, dev->bounce_dma, dev->bounce_size,
  208. DMA_BIDIRECTIONAL);
  209. ps3_dma_region_free(&dev->dma_region);
  210. free_irq(dev->irq, dev);
  211. error = ps3_sb_event_receive_port_destroy(&dev->sbd, dev->irq);
  212. if (error)
  213. dev_err(&dev->sbd.core,
  214. "%s:%u: destroy event receive port failed %d\n",
  215. __func__, __LINE__, error);
  216. error = ps3stor_close_hv_device(&dev->sbd);
  217. if (error)
  218. dev_err(&dev->sbd.core,
  219. "%s:%u: ps3_close_hv_device failed %d\n", __func__,
  220. __LINE__, error);
  221. }
  222. EXPORT_SYMBOL_GPL(ps3stor_teardown);
  223. /**
  224. * ps3stor_read_write_sectors - read/write from/to a storage device
  225. * @dev: Pointer to a struct ps3_storage_device
  226. * @lpar: HV logical partition address
  227. * @start_sector: First sector to read/write
  228. * @sectors: Number of sectors to read/write
  229. * @write: Flag indicating write (non-zero) or read (zero)
  230. *
  231. * Returns 0 for success, -1 in case of failure to submit the command, or
  232. * an LV1 status value in case of other errors
  233. */
  234. u64 ps3stor_read_write_sectors(struct ps3_storage_device *dev, u64 lpar,
  235. u64 start_sector, u64 sectors, int write)
  236. {
  237. unsigned int region_id = dev->regions[dev->region_idx].id;
  238. const char *op = write ? "write" : "read";
  239. int res;
  240. dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
  241. __func__, __LINE__, op, sectors, start_sector);
  242. init_completion(&dev->done);
  243. res = write ? lv1_storage_write(dev->sbd.dev_id, region_id,
  244. start_sector, sectors, 0, lpar,
  245. &dev->tag)
  246. : lv1_storage_read(dev->sbd.dev_id, region_id,
  247. start_sector, sectors, 0, lpar,
  248. &dev->tag);
  249. if (res) {
  250. dev_dbg(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
  251. __LINE__, op, res);
  252. return -1;
  253. }
  254. wait_for_completion(&dev->done);
  255. if (dev->lv1_status) {
  256. dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
  257. __LINE__, op, dev->lv1_status);
  258. return dev->lv1_status;
  259. }
  260. dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__, __LINE__,
  261. op);
  262. return 0;
  263. }
  264. EXPORT_SYMBOL_GPL(ps3stor_read_write_sectors);
  265. /**
  266. * ps3stor_send_command - send a device command to a storage device
  267. * @dev: Pointer to a struct ps3_storage_device
  268. * @cmd: Command number
  269. * @arg1: First command argument
  270. * @arg2: Second command argument
  271. * @arg3: Third command argument
  272. * @arg4: Fourth command argument
  273. *
  274. * Returns 0 for success, -1 in case of failure to submit the command, or
  275. * an LV1 status value in case of other errors
  276. */
  277. u64 ps3stor_send_command(struct ps3_storage_device *dev, u64 cmd, u64 arg1,
  278. u64 arg2, u64 arg3, u64 arg4)
  279. {
  280. int res;
  281. dev_dbg(&dev->sbd.core, "%s:%u: send device command 0x%llx\n", __func__,
  282. __LINE__, cmd);
  283. init_completion(&dev->done);
  284. res = lv1_storage_send_device_command(dev->sbd.dev_id, cmd, arg1,
  285. arg2, arg3, arg4, &dev->tag);
  286. if (res) {
  287. dev_err(&dev->sbd.core,
  288. "%s:%u: send_device_command 0x%llx failed %d\n",
  289. __func__, __LINE__, cmd, res);
  290. return -1;
  291. }
  292. wait_for_completion(&dev->done);
  293. if (dev->lv1_status) {
  294. dev_dbg(&dev->sbd.core, "%s:%u: command 0x%llx failed 0x%llx\n",
  295. __func__, __LINE__, cmd, dev->lv1_status);
  296. return dev->lv1_status;
  297. }
  298. dev_dbg(&dev->sbd.core, "%s:%u: command 0x%llx completed\n", __func__,
  299. __LINE__, cmd);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(ps3stor_send_command);
  303. MODULE_LICENSE("GPL");
  304. MODULE_DESCRIPTION("PS3 Storage Bus Library");
  305. MODULE_AUTHOR("Sony Corporation");