ps3disk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * PS3 Disk 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/ata.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/slab.h>
  23. #include <asm/lv1call.h>
  24. #include <asm/ps3stor.h>
  25. #include <asm/firmware.h>
  26. #define DEVICE_NAME "ps3disk"
  27. #define BOUNCE_SIZE (64*1024)
  28. #define PS3DISK_MAX_DISKS 16
  29. #define PS3DISK_MINORS 16
  30. #define PS3DISK_NAME "ps3d%c"
  31. struct ps3disk_private {
  32. spinlock_t lock; /* Request queue spinlock */
  33. struct request_queue *queue;
  34. struct gendisk *gendisk;
  35. unsigned int blocking_factor;
  36. struct request *req;
  37. u64 raw_capacity;
  38. unsigned char model[ATA_ID_PROD_LEN+1];
  39. };
  40. #define LV1_STORAGE_SEND_ATA_COMMAND (2)
  41. #define LV1_STORAGE_ATA_HDDOUT (0x23)
  42. struct lv1_ata_cmnd_block {
  43. u16 features;
  44. u16 sector_count;
  45. u16 LBA_low;
  46. u16 LBA_mid;
  47. u16 LBA_high;
  48. u8 device;
  49. u8 command;
  50. u32 is_ext;
  51. u32 proto;
  52. u32 in_out;
  53. u32 size;
  54. u64 buffer;
  55. u32 arglen;
  56. };
  57. enum lv1_ata_proto {
  58. NON_DATA_PROTO = 0,
  59. PIO_DATA_IN_PROTO = 1,
  60. PIO_DATA_OUT_PROTO = 2,
  61. DMA_PROTO = 3
  62. };
  63. enum lv1_ata_in_out {
  64. DIR_WRITE = 0, /* memory -> device */
  65. DIR_READ = 1 /* device -> memory */
  66. };
  67. static int ps3disk_major;
  68. static const struct block_device_operations ps3disk_fops = {
  69. .owner = THIS_MODULE,
  70. };
  71. static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
  72. struct request *req, int gather)
  73. {
  74. unsigned int offset = 0;
  75. struct req_iterator iter;
  76. struct bio_vec *bvec;
  77. unsigned int i = 0;
  78. size_t size;
  79. void *buf;
  80. rq_for_each_segment(bvec, req, iter) {
  81. unsigned long flags;
  82. dev_dbg(&dev->sbd.core,
  83. "%s:%u: bio %u: %u segs %u sectors from %lu\n",
  84. __func__, __LINE__, i, bio_segments(iter.bio),
  85. bio_sectors(iter.bio), iter.bio->bi_sector);
  86. size = bvec->bv_len;
  87. buf = bvec_kmap_irq(bvec, &flags);
  88. if (gather)
  89. memcpy(dev->bounce_buf+offset, buf, size);
  90. else
  91. memcpy(buf, dev->bounce_buf+offset, size);
  92. offset += size;
  93. flush_kernel_dcache_page(bvec->bv_page);
  94. bvec_kunmap_irq(bvec, &flags);
  95. i++;
  96. }
  97. }
  98. static int ps3disk_submit_request_sg(struct ps3_storage_device *dev,
  99. struct request *req)
  100. {
  101. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  102. int write = rq_data_dir(req), res;
  103. const char *op = write ? "write" : "read";
  104. u64 start_sector, sectors;
  105. unsigned int region_id = dev->regions[dev->region_idx].id;
  106. #ifdef DEBUG
  107. unsigned int n = 0;
  108. struct bio_vec *bv;
  109. struct req_iterator iter;
  110. rq_for_each_segment(bv, req, iter)
  111. n++;
  112. dev_dbg(&dev->sbd.core,
  113. "%s:%u: %s req has %u bvecs for %u sectors\n",
  114. __func__, __LINE__, op, n, blk_rq_sectors(req));
  115. #endif
  116. start_sector = blk_rq_pos(req) * priv->blocking_factor;
  117. sectors = blk_rq_sectors(req) * priv->blocking_factor;
  118. dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
  119. __func__, __LINE__, op, sectors, start_sector);
  120. if (write) {
  121. ps3disk_scatter_gather(dev, req, 1);
  122. res = lv1_storage_write(dev->sbd.dev_id, region_id,
  123. start_sector, sectors, 0,
  124. dev->bounce_lpar, &dev->tag);
  125. } else {
  126. res = lv1_storage_read(dev->sbd.dev_id, region_id,
  127. start_sector, sectors, 0,
  128. dev->bounce_lpar, &dev->tag);
  129. }
  130. if (res) {
  131. dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
  132. __LINE__, op, res);
  133. __blk_end_request_all(req, -EIO);
  134. return 0;
  135. }
  136. priv->req = req;
  137. return 1;
  138. }
  139. static int ps3disk_submit_flush_request(struct ps3_storage_device *dev,
  140. struct request *req)
  141. {
  142. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  143. u64 res;
  144. dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
  145. res = lv1_storage_send_device_command(dev->sbd.dev_id,
  146. LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
  147. 0, &dev->tag);
  148. if (res) {
  149. dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
  150. __func__, __LINE__, res);
  151. __blk_end_request_all(req, -EIO);
  152. return 0;
  153. }
  154. priv->req = req;
  155. return 1;
  156. }
  157. static void ps3disk_do_request(struct ps3_storage_device *dev,
  158. struct request_queue *q)
  159. {
  160. struct request *req;
  161. dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
  162. while ((req = blk_fetch_request(q))) {
  163. if (blk_fs_request(req)) {
  164. if (ps3disk_submit_request_sg(dev, req))
  165. break;
  166. } else if (req->cmd_type == REQ_TYPE_LINUX_BLOCK &&
  167. req->cmd[0] == REQ_LB_OP_FLUSH) {
  168. if (ps3disk_submit_flush_request(dev, req))
  169. break;
  170. } else {
  171. blk_dump_rq_flags(req, DEVICE_NAME " bad request");
  172. __blk_end_request_all(req, -EIO);
  173. continue;
  174. }
  175. }
  176. }
  177. static void ps3disk_request(struct request_queue *q)
  178. {
  179. struct ps3_storage_device *dev = q->queuedata;
  180. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  181. if (priv->req) {
  182. dev_dbg(&dev->sbd.core, "%s:%u busy\n", __func__, __LINE__);
  183. return;
  184. }
  185. ps3disk_do_request(dev, q);
  186. }
  187. static irqreturn_t ps3disk_interrupt(int irq, void *data)
  188. {
  189. struct ps3_storage_device *dev = data;
  190. struct ps3disk_private *priv;
  191. struct request *req;
  192. int res, read, error;
  193. u64 tag, status;
  194. const char *op;
  195. res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
  196. if (tag != dev->tag)
  197. dev_err(&dev->sbd.core,
  198. "%s:%u: tag mismatch, got %llx, expected %llx\n",
  199. __func__, __LINE__, tag, dev->tag);
  200. if (res) {
  201. dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
  202. __func__, __LINE__, res, status);
  203. return IRQ_HANDLED;
  204. }
  205. priv = ps3_system_bus_get_drvdata(&dev->sbd);
  206. req = priv->req;
  207. if (!req) {
  208. dev_dbg(&dev->sbd.core,
  209. "%s:%u non-block layer request completed\n", __func__,
  210. __LINE__);
  211. dev->lv1_status = status;
  212. complete(&dev->done);
  213. return IRQ_HANDLED;
  214. }
  215. if (req->cmd_type == REQ_TYPE_LINUX_BLOCK &&
  216. req->cmd[0] == REQ_LB_OP_FLUSH) {
  217. read = 0;
  218. op = "flush";
  219. } else {
  220. read = !rq_data_dir(req);
  221. op = read ? "read" : "write";
  222. }
  223. if (status) {
  224. dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
  225. __LINE__, op, status);
  226. error = -EIO;
  227. } else {
  228. dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
  229. __LINE__, op);
  230. error = 0;
  231. if (read)
  232. ps3disk_scatter_gather(dev, req, 0);
  233. }
  234. spin_lock(&priv->lock);
  235. __blk_end_request_all(req, error);
  236. priv->req = NULL;
  237. ps3disk_do_request(dev, priv->queue);
  238. spin_unlock(&priv->lock);
  239. return IRQ_HANDLED;
  240. }
  241. static int ps3disk_sync_cache(struct ps3_storage_device *dev)
  242. {
  243. u64 res;
  244. dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
  245. res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
  246. if (res) {
  247. dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
  248. __func__, __LINE__, res);
  249. return -EIO;
  250. }
  251. return 0;
  252. }
  253. /* ATA helpers copied from drivers/ata/libata-core.c */
  254. static void swap_buf_le16(u16 *buf, unsigned int buf_words)
  255. {
  256. #ifdef __BIG_ENDIAN
  257. unsigned int i;
  258. for (i = 0; i < buf_words; i++)
  259. buf[i] = le16_to_cpu(buf[i]);
  260. #endif /* __BIG_ENDIAN */
  261. }
  262. static u64 ata_id_n_sectors(const u16 *id)
  263. {
  264. if (ata_id_has_lba(id)) {
  265. if (ata_id_has_lba48(id))
  266. return ata_id_u64(id, 100);
  267. else
  268. return ata_id_u32(id, 60);
  269. } else {
  270. if (ata_id_current_chs_valid(id))
  271. return ata_id_u32(id, 57);
  272. else
  273. return id[1] * id[3] * id[6];
  274. }
  275. }
  276. static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
  277. unsigned int len)
  278. {
  279. unsigned int c;
  280. while (len > 0) {
  281. c = id[ofs] >> 8;
  282. *s = c;
  283. s++;
  284. c = id[ofs] & 0xff;
  285. *s = c;
  286. s++;
  287. ofs++;
  288. len -= 2;
  289. }
  290. }
  291. static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
  292. unsigned int len)
  293. {
  294. unsigned char *p;
  295. WARN_ON(!(len & 1));
  296. ata_id_string(id, s, ofs, len - 1);
  297. p = s + strnlen(s, len - 1);
  298. while (p > s && p[-1] == ' ')
  299. p--;
  300. *p = '\0';
  301. }
  302. static int ps3disk_identify(struct ps3_storage_device *dev)
  303. {
  304. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  305. struct lv1_ata_cmnd_block ata_cmnd;
  306. u16 *id = dev->bounce_buf;
  307. u64 res;
  308. dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
  309. memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
  310. ata_cmnd.command = ATA_CMD_ID_ATA;
  311. ata_cmnd.sector_count = 1;
  312. ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
  313. ata_cmnd.buffer = dev->bounce_lpar;
  314. ata_cmnd.proto = PIO_DATA_IN_PROTO;
  315. ata_cmnd.in_out = DIR_READ;
  316. res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
  317. ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
  318. sizeof(ata_cmnd), ata_cmnd.buffer,
  319. ata_cmnd.arglen);
  320. if (res) {
  321. dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
  322. __func__, __LINE__, res);
  323. return -EIO;
  324. }
  325. swap_buf_le16(id, ATA_ID_WORDS);
  326. /* All we're interested in are raw capacity and model name */
  327. priv->raw_capacity = ata_id_n_sectors(id);
  328. ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
  329. return 0;
  330. }
  331. static void ps3disk_prepare_flush(struct request_queue *q, struct request *req)
  332. {
  333. struct ps3_storage_device *dev = q->queuedata;
  334. dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
  335. req->cmd_type = REQ_TYPE_LINUX_BLOCK;
  336. req->cmd[0] = REQ_LB_OP_FLUSH;
  337. }
  338. static unsigned long ps3disk_mask;
  339. static DEFINE_MUTEX(ps3disk_mask_mutex);
  340. static int __devinit ps3disk_probe(struct ps3_system_bus_device *_dev)
  341. {
  342. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  343. struct ps3disk_private *priv;
  344. int error;
  345. unsigned int devidx;
  346. struct request_queue *queue;
  347. struct gendisk *gendisk;
  348. if (dev->blk_size < 512) {
  349. dev_err(&dev->sbd.core,
  350. "%s:%u: cannot handle block size %llu\n", __func__,
  351. __LINE__, dev->blk_size);
  352. return -EINVAL;
  353. }
  354. BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
  355. mutex_lock(&ps3disk_mask_mutex);
  356. devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
  357. if (devidx >= PS3DISK_MAX_DISKS) {
  358. dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
  359. __LINE__);
  360. mutex_unlock(&ps3disk_mask_mutex);
  361. return -ENOSPC;
  362. }
  363. __set_bit(devidx, &ps3disk_mask);
  364. mutex_unlock(&ps3disk_mask_mutex);
  365. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  366. if (!priv) {
  367. error = -ENOMEM;
  368. goto fail;
  369. }
  370. ps3_system_bus_set_drvdata(_dev, priv);
  371. spin_lock_init(&priv->lock);
  372. dev->bounce_size = BOUNCE_SIZE;
  373. dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
  374. if (!dev->bounce_buf) {
  375. error = -ENOMEM;
  376. goto fail_free_priv;
  377. }
  378. error = ps3stor_setup(dev, ps3disk_interrupt);
  379. if (error)
  380. goto fail_free_bounce;
  381. ps3disk_identify(dev);
  382. queue = blk_init_queue(ps3disk_request, &priv->lock);
  383. if (!queue) {
  384. dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
  385. __func__, __LINE__);
  386. error = -ENOMEM;
  387. goto fail_teardown;
  388. }
  389. priv->queue = queue;
  390. queue->queuedata = dev;
  391. blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
  392. blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9);
  393. blk_queue_segment_boundary(queue, -1UL);
  394. blk_queue_dma_alignment(queue, dev->blk_size-1);
  395. blk_queue_logical_block_size(queue, dev->blk_size);
  396. blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH,
  397. ps3disk_prepare_flush);
  398. blk_queue_max_segments(queue, -1);
  399. blk_queue_max_segment_size(queue, dev->bounce_size);
  400. gendisk = alloc_disk(PS3DISK_MINORS);
  401. if (!gendisk) {
  402. dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
  403. __LINE__);
  404. error = -ENOMEM;
  405. goto fail_cleanup_queue;
  406. }
  407. priv->gendisk = gendisk;
  408. gendisk->major = ps3disk_major;
  409. gendisk->first_minor = devidx * PS3DISK_MINORS;
  410. gendisk->fops = &ps3disk_fops;
  411. gendisk->queue = queue;
  412. gendisk->private_data = dev;
  413. gendisk->driverfs_dev = &dev->sbd.core;
  414. snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
  415. devidx+'a');
  416. priv->blocking_factor = dev->blk_size >> 9;
  417. set_capacity(gendisk,
  418. dev->regions[dev->region_idx].size*priv->blocking_factor);
  419. dev_info(&dev->sbd.core,
  420. "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n",
  421. gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
  422. get_capacity(gendisk) >> 11);
  423. add_disk(gendisk);
  424. return 0;
  425. fail_cleanup_queue:
  426. blk_cleanup_queue(queue);
  427. fail_teardown:
  428. ps3stor_teardown(dev);
  429. fail_free_bounce:
  430. kfree(dev->bounce_buf);
  431. fail_free_priv:
  432. kfree(priv);
  433. ps3_system_bus_set_drvdata(_dev, NULL);
  434. fail:
  435. mutex_lock(&ps3disk_mask_mutex);
  436. __clear_bit(devidx, &ps3disk_mask);
  437. mutex_unlock(&ps3disk_mask_mutex);
  438. return error;
  439. }
  440. static int ps3disk_remove(struct ps3_system_bus_device *_dev)
  441. {
  442. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  443. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  444. mutex_lock(&ps3disk_mask_mutex);
  445. __clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
  446. &ps3disk_mask);
  447. mutex_unlock(&ps3disk_mask_mutex);
  448. del_gendisk(priv->gendisk);
  449. blk_cleanup_queue(priv->queue);
  450. put_disk(priv->gendisk);
  451. dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
  452. ps3disk_sync_cache(dev);
  453. ps3stor_teardown(dev);
  454. kfree(dev->bounce_buf);
  455. kfree(priv);
  456. ps3_system_bus_set_drvdata(_dev, NULL);
  457. return 0;
  458. }
  459. static struct ps3_system_bus_driver ps3disk = {
  460. .match_id = PS3_MATCH_ID_STOR_DISK,
  461. .core.name = DEVICE_NAME,
  462. .core.owner = THIS_MODULE,
  463. .probe = ps3disk_probe,
  464. .remove = ps3disk_remove,
  465. .shutdown = ps3disk_remove,
  466. };
  467. static int __init ps3disk_init(void)
  468. {
  469. int error;
  470. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  471. return -ENODEV;
  472. error = register_blkdev(0, DEVICE_NAME);
  473. if (error <= 0) {
  474. printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
  475. __LINE__, error);
  476. return error;
  477. }
  478. ps3disk_major = error;
  479. pr_info("%s:%u: registered block device major %d\n", __func__,
  480. __LINE__, ps3disk_major);
  481. error = ps3_system_bus_driver_register(&ps3disk);
  482. if (error)
  483. unregister_blkdev(ps3disk_major, DEVICE_NAME);
  484. return error;
  485. }
  486. static void __exit ps3disk_exit(void)
  487. {
  488. ps3_system_bus_driver_unregister(&ps3disk);
  489. unregister_blkdev(ps3disk_major, DEVICE_NAME);
  490. }
  491. module_init(ps3disk_init);
  492. module_exit(ps3disk_exit);
  493. MODULE_LICENSE("GPL");
  494. MODULE_DESCRIPTION("PS3 Disk Storage Driver");
  495. MODULE_AUTHOR("Sony Corporation");
  496. MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);