ps3rom.c 10 KB

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