ps3disk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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, -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 (req_op(req) == REQ_OP_FLUSH) {
  164. if (ps3disk_submit_flush_request(dev, req))
  165. break;
  166. } else if (req->cmd_type == REQ_TYPE_FS) {
  167. if (ps3disk_submit_request_sg(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_op(req) == REQ_OP_FLUSH) {
  215. read = 0;
  216. op = "flush";
  217. } else {
  218. read = !rq_data_dir(req);
  219. op = read ? "read" : "write";
  220. }
  221. if (status) {
  222. dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
  223. __LINE__, op, status);
  224. error = -EIO;
  225. } else {
  226. dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
  227. __LINE__, op);
  228. error = 0;
  229. if (read)
  230. ps3disk_scatter_gather(dev, req, 0);
  231. }
  232. spin_lock(&priv->lock);
  233. __blk_end_request_all(req, error);
  234. priv->req = NULL;
  235. ps3disk_do_request(dev, priv->queue);
  236. spin_unlock(&priv->lock);
  237. return IRQ_HANDLED;
  238. }
  239. static int ps3disk_sync_cache(struct ps3_storage_device *dev)
  240. {
  241. u64 res;
  242. dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
  243. res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
  244. if (res) {
  245. dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
  246. __func__, __LINE__, res);
  247. return -EIO;
  248. }
  249. return 0;
  250. }
  251. /* ATA helpers copied from drivers/ata/libata-core.c */
  252. static void swap_buf_le16(u16 *buf, unsigned int buf_words)
  253. {
  254. #ifdef __BIG_ENDIAN
  255. unsigned int i;
  256. for (i = 0; i < buf_words; i++)
  257. buf[i] = le16_to_cpu(buf[i]);
  258. #endif /* __BIG_ENDIAN */
  259. }
  260. static u64 ata_id_n_sectors(const u16 *id)
  261. {
  262. if (ata_id_has_lba(id)) {
  263. if (ata_id_has_lba48(id))
  264. return ata_id_u64(id, 100);
  265. else
  266. return ata_id_u32(id, 60);
  267. } else {
  268. if (ata_id_current_chs_valid(id))
  269. return ata_id_u32(id, 57);
  270. else
  271. return id[1] * id[3] * id[6];
  272. }
  273. }
  274. static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
  275. unsigned int len)
  276. {
  277. unsigned int c;
  278. while (len > 0) {
  279. c = id[ofs] >> 8;
  280. *s = c;
  281. s++;
  282. c = id[ofs] & 0xff;
  283. *s = c;
  284. s++;
  285. ofs++;
  286. len -= 2;
  287. }
  288. }
  289. static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
  290. unsigned int len)
  291. {
  292. unsigned char *p;
  293. WARN_ON(!(len & 1));
  294. ata_id_string(id, s, ofs, len - 1);
  295. p = s + strnlen(s, len - 1);
  296. while (p > s && p[-1] == ' ')
  297. p--;
  298. *p = '\0';
  299. }
  300. static int ps3disk_identify(struct ps3_storage_device *dev)
  301. {
  302. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  303. struct lv1_ata_cmnd_block ata_cmnd;
  304. u16 *id = dev->bounce_buf;
  305. u64 res;
  306. dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
  307. memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
  308. ata_cmnd.command = ATA_CMD_ID_ATA;
  309. ata_cmnd.sector_count = 1;
  310. ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
  311. ata_cmnd.buffer = dev->bounce_lpar;
  312. ata_cmnd.proto = PIO_DATA_IN_PROTO;
  313. ata_cmnd.in_out = DIR_READ;
  314. res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
  315. ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
  316. sizeof(ata_cmnd), ata_cmnd.buffer,
  317. ata_cmnd.arglen);
  318. if (res) {
  319. dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
  320. __func__, __LINE__, res);
  321. return -EIO;
  322. }
  323. swap_buf_le16(id, ATA_ID_WORDS);
  324. /* All we're interested in are raw capacity and model name */
  325. priv->raw_capacity = ata_id_n_sectors(id);
  326. ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
  327. return 0;
  328. }
  329. static unsigned long ps3disk_mask;
  330. static DEFINE_MUTEX(ps3disk_mask_mutex);
  331. static int ps3disk_probe(struct ps3_system_bus_device *_dev)
  332. {
  333. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  334. struct ps3disk_private *priv;
  335. int error;
  336. unsigned int devidx;
  337. struct request_queue *queue;
  338. struct gendisk *gendisk;
  339. if (dev->blk_size < 512) {
  340. dev_err(&dev->sbd.core,
  341. "%s:%u: cannot handle block size %llu\n", __func__,
  342. __LINE__, dev->blk_size);
  343. return -EINVAL;
  344. }
  345. BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
  346. mutex_lock(&ps3disk_mask_mutex);
  347. devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
  348. if (devidx >= PS3DISK_MAX_DISKS) {
  349. dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
  350. __LINE__);
  351. mutex_unlock(&ps3disk_mask_mutex);
  352. return -ENOSPC;
  353. }
  354. __set_bit(devidx, &ps3disk_mask);
  355. mutex_unlock(&ps3disk_mask_mutex);
  356. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  357. if (!priv) {
  358. error = -ENOMEM;
  359. goto fail;
  360. }
  361. ps3_system_bus_set_drvdata(_dev, priv);
  362. spin_lock_init(&priv->lock);
  363. dev->bounce_size = BOUNCE_SIZE;
  364. dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
  365. if (!dev->bounce_buf) {
  366. error = -ENOMEM;
  367. goto fail_free_priv;
  368. }
  369. error = ps3stor_setup(dev, ps3disk_interrupt);
  370. if (error)
  371. goto fail_free_bounce;
  372. ps3disk_identify(dev);
  373. queue = blk_init_queue(ps3disk_request, &priv->lock);
  374. if (!queue) {
  375. dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
  376. __func__, __LINE__);
  377. error = -ENOMEM;
  378. goto fail_teardown;
  379. }
  380. priv->queue = queue;
  381. queue->queuedata = dev;
  382. blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
  383. blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9);
  384. blk_queue_segment_boundary(queue, -1UL);
  385. blk_queue_dma_alignment(queue, dev->blk_size-1);
  386. blk_queue_logical_block_size(queue, dev->blk_size);
  387. blk_queue_write_cache(queue, true, false);
  388. blk_queue_max_segments(queue, -1);
  389. blk_queue_max_segment_size(queue, dev->bounce_size);
  390. gendisk = alloc_disk(PS3DISK_MINORS);
  391. if (!gendisk) {
  392. dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
  393. __LINE__);
  394. error = -ENOMEM;
  395. goto fail_cleanup_queue;
  396. }
  397. priv->gendisk = gendisk;
  398. gendisk->major = ps3disk_major;
  399. gendisk->first_minor = devidx * PS3DISK_MINORS;
  400. gendisk->fops = &ps3disk_fops;
  401. gendisk->queue = queue;
  402. gendisk->private_data = dev;
  403. snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
  404. devidx+'a');
  405. priv->blocking_factor = dev->blk_size >> 9;
  406. set_capacity(gendisk,
  407. dev->regions[dev->region_idx].size*priv->blocking_factor);
  408. dev_info(&dev->sbd.core,
  409. "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n",
  410. gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
  411. get_capacity(gendisk) >> 11);
  412. device_add_disk(&dev->sbd.core, gendisk);
  413. return 0;
  414. fail_cleanup_queue:
  415. blk_cleanup_queue(queue);
  416. fail_teardown:
  417. ps3stor_teardown(dev);
  418. fail_free_bounce:
  419. kfree(dev->bounce_buf);
  420. fail_free_priv:
  421. kfree(priv);
  422. ps3_system_bus_set_drvdata(_dev, NULL);
  423. fail:
  424. mutex_lock(&ps3disk_mask_mutex);
  425. __clear_bit(devidx, &ps3disk_mask);
  426. mutex_unlock(&ps3disk_mask_mutex);
  427. return error;
  428. }
  429. static int ps3disk_remove(struct ps3_system_bus_device *_dev)
  430. {
  431. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  432. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  433. mutex_lock(&ps3disk_mask_mutex);
  434. __clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
  435. &ps3disk_mask);
  436. mutex_unlock(&ps3disk_mask_mutex);
  437. del_gendisk(priv->gendisk);
  438. blk_cleanup_queue(priv->queue);
  439. put_disk(priv->gendisk);
  440. dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
  441. ps3disk_sync_cache(dev);
  442. ps3stor_teardown(dev);
  443. kfree(dev->bounce_buf);
  444. kfree(priv);
  445. ps3_system_bus_set_drvdata(_dev, NULL);
  446. return 0;
  447. }
  448. static struct ps3_system_bus_driver ps3disk = {
  449. .match_id = PS3_MATCH_ID_STOR_DISK,
  450. .core.name = DEVICE_NAME,
  451. .core.owner = THIS_MODULE,
  452. .probe = ps3disk_probe,
  453. .remove = ps3disk_remove,
  454. .shutdown = ps3disk_remove,
  455. };
  456. static int __init ps3disk_init(void)
  457. {
  458. int error;
  459. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  460. return -ENODEV;
  461. error = register_blkdev(0, DEVICE_NAME);
  462. if (error <= 0) {
  463. printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
  464. __LINE__, error);
  465. return error;
  466. }
  467. ps3disk_major = error;
  468. pr_info("%s:%u: registered block device major %d\n", __func__,
  469. __LINE__, ps3disk_major);
  470. error = ps3_system_bus_driver_register(&ps3disk);
  471. if (error)
  472. unregister_blkdev(ps3disk_major, DEVICE_NAME);
  473. return error;
  474. }
  475. static void __exit ps3disk_exit(void)
  476. {
  477. ps3_system_bus_driver_unregister(&ps3disk);
  478. unregister_blkdev(ps3disk_major, DEVICE_NAME);
  479. }
  480. module_init(ps3disk_init);
  481. module_exit(ps3disk_exit);
  482. MODULE_LICENSE("GPL");
  483. MODULE_DESCRIPTION("PS3 Disk Storage Driver");
  484. MODULE_AUTHOR("Sony Corporation");
  485. MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);