ps3stor_lib.c 8.3 KB

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