PageRenderTime 492ms CodeModel.GetById 24ms RepoModel.GetById 7ms app.codeStats 0ms

/drivers/scsi/ps3rom.c

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