ps3rom.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PS3 BD/DVD/CD-ROM Storage Driver
  4. *
  5. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  6. * Copyright 2007 Sony Corp.
  7. */
  8. #include <linux/cdrom.h>
  9. #include <linux/highmem.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <scsi/scsi.h>
  13. #include <scsi/scsi_cmnd.h>
  14. #include <scsi/scsi_dbg.h>
  15. #include <scsi/scsi_device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_eh.h>
  18. #include <asm/lv1call.h>
  19. #include <asm/ps3stor.h>
  20. #define DEVICE_NAME "ps3rom"
  21. #define BOUNCE_SIZE (64*1024)
  22. #define PS3ROM_MAX_SECTORS (BOUNCE_SIZE >> 9)
  23. struct ps3rom_private {
  24. struct ps3_storage_device *dev;
  25. struct scsi_cmnd *curr_cmd;
  26. };
  27. #define LV1_STORAGE_SEND_ATAPI_COMMAND (1)
  28. struct lv1_atapi_cmnd_block {
  29. u8 pkt[32]; /* packet command block */
  30. u32 pktlen; /* should be 12 for ATAPI 8020 */
  31. u32 blocks;
  32. u32 block_size;
  33. u32 proto; /* transfer mode */
  34. u32 in_out; /* transfer direction */
  35. u64 buffer; /* parameter except command block */
  36. u32 arglen; /* length above */
  37. };
  38. enum lv1_atapi_proto {
  39. NON_DATA_PROTO = 0,
  40. PIO_DATA_IN_PROTO = 1,
  41. PIO_DATA_OUT_PROTO = 2,
  42. DMA_PROTO = 3
  43. };
  44. enum lv1_atapi_in_out {
  45. DIR_WRITE = 0, /* memory -> device */
  46. DIR_READ = 1 /* device -> memory */
  47. };
  48. static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
  49. {
  50. struct ps3rom_private *priv = shost_priv(scsi_dev->host);
  51. struct ps3_storage_device *dev = priv->dev;
  52. dev_dbg(&dev->sbd.core, "%s:%u: id %u, lun %llu, channel %u\n", __func__,
  53. __LINE__, scsi_dev->id, scsi_dev->lun, scsi_dev->channel);
  54. /*
  55. * ATAPI SFF8020 devices use MODE_SENSE_10,
  56. * so we can prohibit MODE_SENSE_6
  57. */
  58. scsi_dev->use_10_for_ms = 1;
  59. /* we don't support {READ,WRITE}_6 */
  60. scsi_dev->use_10_for_rw = 1;
  61. return 0;
  62. }
  63. static int ps3rom_atapi_request(struct ps3_storage_device *dev,
  64. struct scsi_cmnd *cmd)
  65. {
  66. struct lv1_atapi_cmnd_block atapi_cmnd;
  67. unsigned char opcode = cmd->cmnd[0];
  68. int res;
  69. u64 lpar;
  70. dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
  71. __LINE__, opcode);
  72. memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
  73. memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
  74. atapi_cmnd.pktlen = 12;
  75. atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
  76. atapi_cmnd.blocks = atapi_cmnd.arglen = scsi_bufflen(cmd);
  77. atapi_cmnd.buffer = dev->bounce_lpar;
  78. switch (cmd->sc_data_direction) {
  79. case DMA_FROM_DEVICE:
  80. if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
  81. atapi_cmnd.proto = DMA_PROTO;
  82. else
  83. atapi_cmnd.proto = PIO_DATA_IN_PROTO;
  84. atapi_cmnd.in_out = DIR_READ;
  85. break;
  86. case DMA_TO_DEVICE:
  87. if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
  88. atapi_cmnd.proto = DMA_PROTO;
  89. else
  90. atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
  91. atapi_cmnd.in_out = DIR_WRITE;
  92. scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
  93. break;
  94. default:
  95. atapi_cmnd.proto = NON_DATA_PROTO;
  96. break;
  97. }
  98. lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
  99. res = lv1_storage_send_device_command(dev->sbd.dev_id,
  100. LV1_STORAGE_SEND_ATAPI_COMMAND,
  101. lpar, sizeof(atapi_cmnd),
  102. atapi_cmnd.buffer,
  103. atapi_cmnd.arglen, &dev->tag);
  104. if (res == LV1_DENIED_BY_POLICY) {
  105. dev_dbg(&dev->sbd.core,
  106. "%s:%u: ATAPI command 0x%02x denied by policy\n",
  107. __func__, __LINE__, opcode);
  108. return DID_ERROR << 16;
  109. }
  110. if (res) {
  111. dev_err(&dev->sbd.core,
  112. "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
  113. __LINE__, opcode, res);
  114. return DID_ERROR << 16;
  115. }
  116. return 0;
  117. }
  118. static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
  119. {
  120. return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
  121. cmd->cmnd[5];
  122. }
  123. static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
  124. {
  125. return cmd->cmnd[7] << 8 | cmd->cmnd[8];
  126. }
  127. static int ps3rom_read_request(struct ps3_storage_device *dev,
  128. struct scsi_cmnd *cmd, u32 start_sector,
  129. u32 sectors)
  130. {
  131. int res;
  132. dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
  133. __func__, __LINE__, sectors, start_sector);
  134. res = lv1_storage_read(dev->sbd.dev_id,
  135. dev->regions[dev->region_idx].id, start_sector,
  136. sectors, 0, dev->bounce_lpar, &dev->tag);
  137. if (res) {
  138. dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
  139. __LINE__, res);
  140. return DID_ERROR << 16;
  141. }
  142. return 0;
  143. }
  144. static int ps3rom_write_request(struct ps3_storage_device *dev,
  145. struct scsi_cmnd *cmd, u32 start_sector,
  146. u32 sectors)
  147. {
  148. int res;
  149. dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
  150. __func__, __LINE__, sectors, start_sector);
  151. scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
  152. res = lv1_storage_write(dev->sbd.dev_id,
  153. dev->regions[dev->region_idx].id, start_sector,
  154. sectors, 0, dev->bounce_lpar, &dev->tag);
  155. if (res) {
  156. dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
  157. __LINE__, res);
  158. return DID_ERROR << 16;
  159. }
  160. return 0;
  161. }
  162. static int ps3rom_queuecommand_lck(struct scsi_cmnd *cmd)
  163. {
  164. struct ps3rom_private *priv = shost_priv(cmd->device->host);
  165. struct ps3_storage_device *dev = priv->dev;
  166. unsigned char opcode;
  167. int res;
  168. priv->curr_cmd = cmd;
  169. opcode = cmd->cmnd[0];
  170. /*
  171. * While we can submit READ/WRITE SCSI commands as ATAPI commands,
  172. * it's recommended for various reasons (performance, error handling,
  173. * ...) to use lv1_storage_{read,write}() instead
  174. */
  175. switch (opcode) {
  176. case READ_10:
  177. res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
  178. srb10_len(cmd));
  179. break;
  180. case WRITE_10:
  181. res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
  182. srb10_len(cmd));
  183. break;
  184. default:
  185. res = ps3rom_atapi_request(dev, cmd);
  186. break;
  187. }
  188. if (res) {
  189. scsi_build_sense(cmd, 0, ILLEGAL_REQUEST, 0, 0);
  190. cmd->result = res;
  191. priv->curr_cmd = NULL;
  192. scsi_done(cmd);
  193. }
  194. return 0;
  195. }
  196. static DEF_SCSI_QCMD(ps3rom_queuecommand)
  197. static int decode_lv1_status(u64 status, unsigned char *sense_key,
  198. unsigned char *asc, unsigned char *ascq)
  199. {
  200. if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
  201. return -1;
  202. *sense_key = (status >> 16) & 0xff;
  203. *asc = (status >> 8) & 0xff;
  204. *ascq = status & 0xff;
  205. return 0;
  206. }
  207. static irqreturn_t ps3rom_interrupt(int irq, void *data)
  208. {
  209. struct ps3_storage_device *dev = data;
  210. struct Scsi_Host *host;
  211. struct ps3rom_private *priv;
  212. struct scsi_cmnd *cmd;
  213. int res;
  214. u64 tag, status;
  215. unsigned char sense_key, asc, ascq;
  216. res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
  217. /*
  218. * status = -1 may mean that ATAPI transport completed OK, but
  219. * ATAPI command itself resulted CHECK CONDITION
  220. * so, upper layer should issue REQUEST_SENSE to check the sense data
  221. */
  222. if (tag != dev->tag)
  223. dev_err(&dev->sbd.core,
  224. "%s:%u: tag mismatch, got %llx, expected %llx\n",
  225. __func__, __LINE__, tag, dev->tag);
  226. if (res) {
  227. dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
  228. __func__, __LINE__, res, status);
  229. return IRQ_HANDLED;
  230. }
  231. host = ps3_system_bus_get_drvdata(&dev->sbd);
  232. priv = shost_priv(host);
  233. cmd = priv->curr_cmd;
  234. if (!status) {
  235. /* OK, completed */
  236. if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  237. int len;
  238. len = scsi_sg_copy_from_buffer(cmd,
  239. dev->bounce_buf,
  240. dev->bounce_size);
  241. scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
  242. }
  243. cmd->result = DID_OK << 16;
  244. goto done;
  245. }
  246. if (cmd->cmnd[0] == REQUEST_SENSE) {
  247. /* SCSI spec says request sense should never get error */
  248. dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
  249. __func__, __LINE__);
  250. cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
  251. goto done;
  252. }
  253. if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
  254. cmd->result = DID_ERROR << 16;
  255. goto done;
  256. }
  257. scsi_build_sense(cmd, 0, sense_key, asc, ascq);
  258. done:
  259. priv->curr_cmd = NULL;
  260. scsi_done(cmd);
  261. return IRQ_HANDLED;
  262. }
  263. static struct scsi_host_template ps3rom_host_template = {
  264. .name = DEVICE_NAME,
  265. .slave_configure = ps3rom_slave_configure,
  266. .queuecommand = ps3rom_queuecommand,
  267. .can_queue = 1,
  268. .this_id = 7,
  269. .sg_tablesize = SG_ALL,
  270. .emulated = 1, /* only sg driver uses this */
  271. .max_sectors = PS3ROM_MAX_SECTORS,
  272. .module = THIS_MODULE,
  273. };
  274. static int ps3rom_probe(struct ps3_system_bus_device *_dev)
  275. {
  276. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  277. int error;
  278. struct Scsi_Host *host;
  279. struct ps3rom_private *priv;
  280. if (dev->blk_size != CD_FRAMESIZE) {
  281. dev_err(&dev->sbd.core,
  282. "%s:%u: cannot handle block size %llu\n", __func__,
  283. __LINE__, dev->blk_size);
  284. return -EINVAL;
  285. }
  286. dev->bounce_size = BOUNCE_SIZE;
  287. dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
  288. if (!dev->bounce_buf)
  289. return -ENOMEM;
  290. error = ps3stor_setup(dev, ps3rom_interrupt);
  291. if (error)
  292. goto fail_free_bounce;
  293. host = scsi_host_alloc(&ps3rom_host_template,
  294. sizeof(struct ps3rom_private));
  295. if (!host) {
  296. dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
  297. __func__, __LINE__);
  298. error = -ENOMEM;
  299. goto fail_teardown;
  300. }
  301. priv = shost_priv(host);
  302. ps3_system_bus_set_drvdata(&dev->sbd, host);
  303. priv->dev = dev;
  304. /* One device/LUN per SCSI bus */
  305. host->max_id = 1;
  306. host->max_lun = 1;
  307. error = scsi_add_host(host, &dev->sbd.core);
  308. if (error) {
  309. dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
  310. __func__, __LINE__, error);
  311. error = -ENODEV;
  312. goto fail_host_put;
  313. }
  314. scsi_scan_host(host);
  315. return 0;
  316. fail_host_put:
  317. scsi_host_put(host);
  318. ps3_system_bus_set_drvdata(&dev->sbd, NULL);
  319. fail_teardown:
  320. ps3stor_teardown(dev);
  321. fail_free_bounce:
  322. kfree(dev->bounce_buf);
  323. return error;
  324. }
  325. static void ps3rom_remove(struct ps3_system_bus_device *_dev)
  326. {
  327. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  328. struct Scsi_Host *host = ps3_system_bus_get_drvdata(&dev->sbd);
  329. scsi_remove_host(host);
  330. ps3stor_teardown(dev);
  331. scsi_host_put(host);
  332. ps3_system_bus_set_drvdata(&dev->sbd, NULL);
  333. kfree(dev->bounce_buf);
  334. }
  335. static struct ps3_system_bus_driver ps3rom = {
  336. .match_id = PS3_MATCH_ID_STOR_ROM,
  337. .core.name = DEVICE_NAME,
  338. .core.owner = THIS_MODULE,
  339. .probe = ps3rom_probe,
  340. .remove = ps3rom_remove
  341. };
  342. static int __init ps3rom_init(void)
  343. {
  344. return ps3_system_bus_driver_register(&ps3rom);
  345. }
  346. static void __exit ps3rom_exit(void)
  347. {
  348. ps3_system_bus_driver_unregister(&ps3rom);
  349. }
  350. module_init(ps3rom_init);
  351. module_exit(ps3rom_exit);
  352. MODULE_LICENSE("GPL");
  353. MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
  354. MODULE_AUTHOR("Sony Corporation");
  355. MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);