ps3rom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 %u, 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. #ifdef DEBUG
  182. scsi_print_command(cmd);
  183. #endif
  184. priv->curr_cmd = cmd;
  185. cmd->scsi_done = done;
  186. opcode = cmd->cmnd[0];
  187. /*
  188. * While we can submit READ/WRITE SCSI commands as ATAPI commands,
  189. * it's recommended for various reasons (performance, error handling,
  190. * ...) to use lv1_storage_{read,write}() instead
  191. */
  192. switch (opcode) {
  193. case READ_10:
  194. res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
  195. srb10_len(cmd));
  196. break;
  197. case WRITE_10:
  198. res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
  199. srb10_len(cmd));
  200. break;
  201. default:
  202. res = ps3rom_atapi_request(dev, cmd);
  203. break;
  204. }
  205. if (res) {
  206. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  207. cmd->result = res;
  208. cmd->sense_buffer[0] = 0x70;
  209. cmd->sense_buffer[2] = ILLEGAL_REQUEST;
  210. priv->curr_cmd = NULL;
  211. cmd->scsi_done(cmd);
  212. }
  213. return 0;
  214. }
  215. static DEF_SCSI_QCMD(ps3rom_queuecommand)
  216. static int decode_lv1_status(u64 status, unsigned char *sense_key,
  217. unsigned char *asc, unsigned char *ascq)
  218. {
  219. if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
  220. return -1;
  221. *sense_key = (status >> 16) & 0xff;
  222. *asc = (status >> 8) & 0xff;
  223. *ascq = status & 0xff;
  224. return 0;
  225. }
  226. static irqreturn_t ps3rom_interrupt(int irq, void *data)
  227. {
  228. struct ps3_storage_device *dev = data;
  229. struct Scsi_Host *host;
  230. struct ps3rom_private *priv;
  231. struct scsi_cmnd *cmd;
  232. int res;
  233. u64 tag, status;
  234. unsigned char sense_key, asc, ascq;
  235. res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
  236. /*
  237. * status = -1 may mean that ATAPI transport completed OK, but
  238. * ATAPI command itself resulted CHECK CONDITION
  239. * so, upper layer should issue REQUEST_SENSE to check the sense data
  240. */
  241. if (tag != dev->tag)
  242. dev_err(&dev->sbd.core,
  243. "%s:%u: tag mismatch, got %llx, expected %llx\n",
  244. __func__, __LINE__, tag, dev->tag);
  245. if (res) {
  246. dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
  247. __func__, __LINE__, res, status);
  248. return IRQ_HANDLED;
  249. }
  250. host = ps3_system_bus_get_drvdata(&dev->sbd);
  251. priv = shost_priv(host);
  252. cmd = priv->curr_cmd;
  253. if (!status) {
  254. /* OK, completed */
  255. if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  256. int len;
  257. len = scsi_sg_copy_from_buffer(cmd,
  258. dev->bounce_buf,
  259. dev->bounce_size);
  260. scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
  261. }
  262. cmd->result = DID_OK << 16;
  263. goto done;
  264. }
  265. if (cmd->cmnd[0] == REQUEST_SENSE) {
  266. /* SCSI spec says request sense should never get error */
  267. dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
  268. __func__, __LINE__);
  269. cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
  270. goto done;
  271. }
  272. if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
  273. cmd->result = DID_ERROR << 16;
  274. goto done;
  275. }
  276. scsi_build_sense_buffer(0, cmd->sense_buffer, sense_key, asc, ascq);
  277. cmd->result = SAM_STAT_CHECK_CONDITION;
  278. done:
  279. priv->curr_cmd = NULL;
  280. cmd->scsi_done(cmd);
  281. return IRQ_HANDLED;
  282. }
  283. static struct scsi_host_template ps3rom_host_template = {
  284. .name = DEVICE_NAME,
  285. .slave_configure = ps3rom_slave_configure,
  286. .queuecommand = ps3rom_queuecommand,
  287. .can_queue = 1,
  288. .this_id = 7,
  289. .sg_tablesize = SG_ALL,
  290. .cmd_per_lun = 1,
  291. .emulated = 1, /* only sg driver uses this */
  292. .max_sectors = PS3ROM_MAX_SECTORS,
  293. .use_clustering = ENABLE_CLUSTERING,
  294. .module = THIS_MODULE,
  295. };
  296. static int __devinit ps3rom_probe(struct ps3_system_bus_device *_dev)
  297. {
  298. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  299. int error;
  300. struct Scsi_Host *host;
  301. struct ps3rom_private *priv;
  302. if (dev->blk_size != CD_FRAMESIZE) {
  303. dev_err(&dev->sbd.core,
  304. "%s:%u: cannot handle block size %llu\n", __func__,
  305. __LINE__, dev->blk_size);
  306. return -EINVAL;
  307. }
  308. dev->bounce_size = BOUNCE_SIZE;
  309. dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
  310. if (!dev->bounce_buf)
  311. return -ENOMEM;
  312. error = ps3stor_setup(dev, ps3rom_interrupt);
  313. if (error)
  314. goto fail_free_bounce;
  315. host = scsi_host_alloc(&ps3rom_host_template,
  316. sizeof(struct ps3rom_private));
  317. if (!host) {
  318. dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
  319. __func__, __LINE__);
  320. goto fail_teardown;
  321. }
  322. priv = shost_priv(host);
  323. ps3_system_bus_set_drvdata(&dev->sbd, host);
  324. priv->dev = dev;
  325. /* One device/LUN per SCSI bus */
  326. host->max_id = 1;
  327. host->max_lun = 1;
  328. error = scsi_add_host(host, &dev->sbd.core);
  329. if (error) {
  330. dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
  331. __func__, __LINE__, error);
  332. error = -ENODEV;
  333. goto fail_host_put;
  334. }
  335. scsi_scan_host(host);
  336. return 0;
  337. fail_host_put:
  338. scsi_host_put(host);
  339. ps3_system_bus_set_drvdata(&dev->sbd, NULL);
  340. fail_teardown:
  341. ps3stor_teardown(dev);
  342. fail_free_bounce:
  343. kfree(dev->bounce_buf);
  344. return error;
  345. }
  346. static int ps3rom_remove(struct ps3_system_bus_device *_dev)
  347. {
  348. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  349. struct Scsi_Host *host = ps3_system_bus_get_drvdata(&dev->sbd);
  350. scsi_remove_host(host);
  351. ps3stor_teardown(dev);
  352. scsi_host_put(host);
  353. ps3_system_bus_set_drvdata(&dev->sbd, NULL);
  354. kfree(dev->bounce_buf);
  355. return 0;
  356. }
  357. static struct ps3_system_bus_driver ps3rom = {
  358. .match_id = PS3_MATCH_ID_STOR_ROM,
  359. .core.name = DEVICE_NAME,
  360. .core.owner = THIS_MODULE,
  361. .probe = ps3rom_probe,
  362. .remove = ps3rom_remove
  363. };
  364. static int __init ps3rom_init(void)
  365. {
  366. return ps3_system_bus_driver_register(&ps3rom);
  367. }
  368. static void __exit ps3rom_exit(void)
  369. {
  370. ps3_system_bus_driver_unregister(&ps3rom);
  371. }
  372. module_init(ps3rom_init);
  373. module_exit(ps3rom_exit);
  374. MODULE_LICENSE("GPL");
  375. MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
  376. MODULE_AUTHOR("Sony Corporation");
  377. MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);