ps3rom.c 11 KB

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