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