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