ps3disk.c 14 KB

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