ps3stor_lib.c 9.0 KB

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