ps3stor_lib.c 9.0 KB

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