ps3rom.c 13 KB

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