PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/block/ps3disk.c

https://bitbucket.org/danhamilt1/linux
C | 590 lines | 466 code | 103 blank | 21 comment | 44 complexity | bde2a80d51290827dcac11c038bd1132 MD5 | raw file
  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,
  84. "%s:%u: bio %u: %u segs %u sectors from %lu\n",
  85. __func__, __LINE__, i, bio_segments(iter.bio),
  86. bio_sectors(iter.bio), iter.bio->bi_sector);
  87. size = bvec->bv_len;
  88. buf = bvec_kmap_irq(bvec, &flags);
  89. if (gather)
  90. memcpy(dev->bounce_buf+offset, buf, size);
  91. else
  92. memcpy(buf, dev->bounce_buf+offset, size);
  93. offset += size;
  94. flush_kernel_dcache_page(bvec->bv_page);
  95. bvec_kunmap_irq(buf, &flags);
  96. i++;
  97. }
  98. }
  99. static int ps3disk_submit_request_sg(struct ps3_storage_device *dev,
  100. struct request *req)
  101. {
  102. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  103. int write = rq_data_dir(req), res;
  104. const char *op = write ? "write" : "read";
  105. u64 start_sector, sectors;
  106. unsigned int region_id = dev->regions[dev->region_idx].id;
  107. #ifdef DEBUG
  108. unsigned int n = 0;
  109. struct bio_vec *bv;
  110. struct req_iterator iter;
  111. rq_for_each_segment(bv, req, iter)
  112. n++;
  113. dev_dbg(&dev->sbd.core,
  114. "%s:%u: %s req has %u bvecs for %u sectors\n",
  115. __func__, __LINE__, op, n, blk_rq_sectors(req));
  116. #endif
  117. start_sector = blk_rq_pos(req) * priv->blocking_factor;
  118. sectors = blk_rq_sectors(req) * priv->blocking_factor;
  119. dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
  120. __func__, __LINE__, op, sectors, start_sector);
  121. if (write) {
  122. ps3disk_scatter_gather(dev, req, 1);
  123. res = lv1_storage_write(dev->sbd.dev_id, region_id,
  124. start_sector, sectors, 0,
  125. dev->bounce_lpar, &dev->tag);
  126. } else {
  127. res = lv1_storage_read(dev->sbd.dev_id, region_id,
  128. start_sector, sectors, 0,
  129. dev->bounce_lpar, &dev->tag);
  130. }
  131. if (res) {
  132. dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
  133. __LINE__, op, res);
  134. __blk_end_request_all(req, -EIO);
  135. return 0;
  136. }
  137. priv->req = req;
  138. return 1;
  139. }
  140. static int ps3disk_submit_flush_request(struct ps3_storage_device *dev,
  141. struct request *req)
  142. {
  143. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  144. u64 res;
  145. dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
  146. res = lv1_storage_send_device_command(dev->sbd.dev_id,
  147. LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
  148. 0, &dev->tag);
  149. if (res) {
  150. dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
  151. __func__, __LINE__, res);
  152. __blk_end_request_all(req, -EIO);
  153. return 0;
  154. }
  155. priv->req = req;
  156. return 1;
  157. }
  158. static void ps3disk_do_request(struct ps3_storage_device *dev,
  159. struct request_queue *q)
  160. {
  161. struct request *req;
  162. dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
  163. while ((req = blk_fetch_request(q))) {
  164. if (req->cmd_flags & REQ_FLUSH) {
  165. if (ps3disk_submit_flush_request(dev, req))
  166. break;
  167. } else if (req->cmd_type == REQ_TYPE_FS) {
  168. if (ps3disk_submit_request_sg(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_flags & REQ_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 unsigned long ps3disk_mask;
  331. static DEFINE_MUTEX(ps3disk_mask_mutex);
  332. static int ps3disk_probe(struct ps3_system_bus_device *_dev)
  333. {
  334. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  335. struct ps3disk_private *priv;
  336. int error;
  337. unsigned int devidx;
  338. struct request_queue *queue;
  339. struct gendisk *gendisk;
  340. if (dev->blk_size < 512) {
  341. dev_err(&dev->sbd.core,
  342. "%s:%u: cannot handle block size %llu\n", __func__,
  343. __LINE__, dev->blk_size);
  344. return -EINVAL;
  345. }
  346. BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
  347. mutex_lock(&ps3disk_mask_mutex);
  348. devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
  349. if (devidx >= PS3DISK_MAX_DISKS) {
  350. dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
  351. __LINE__);
  352. mutex_unlock(&ps3disk_mask_mutex);
  353. return -ENOSPC;
  354. }
  355. __set_bit(devidx, &ps3disk_mask);
  356. mutex_unlock(&ps3disk_mask_mutex);
  357. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  358. if (!priv) {
  359. error = -ENOMEM;
  360. goto fail;
  361. }
  362. ps3_system_bus_set_drvdata(_dev, priv);
  363. spin_lock_init(&priv->lock);
  364. dev->bounce_size = BOUNCE_SIZE;
  365. dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
  366. if (!dev->bounce_buf) {
  367. error = -ENOMEM;
  368. goto fail_free_priv;
  369. }
  370. error = ps3stor_setup(dev, ps3disk_interrupt);
  371. if (error)
  372. goto fail_free_bounce;
  373. ps3disk_identify(dev);
  374. queue = blk_init_queue(ps3disk_request, &priv->lock);
  375. if (!queue) {
  376. dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
  377. __func__, __LINE__);
  378. error = -ENOMEM;
  379. goto fail_teardown;
  380. }
  381. priv->queue = queue;
  382. queue->queuedata = dev;
  383. blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
  384. blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9);
  385. blk_queue_segment_boundary(queue, -1UL);
  386. blk_queue_dma_alignment(queue, dev->blk_size-1);
  387. blk_queue_logical_block_size(queue, dev->blk_size);
  388. blk_queue_flush(queue, REQ_FLUSH);
  389. blk_queue_max_segments(queue, -1);
  390. blk_queue_max_segment_size(queue, dev->bounce_size);
  391. gendisk = alloc_disk(PS3DISK_MINORS);
  392. if (!gendisk) {
  393. dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
  394. __LINE__);
  395. error = -ENOMEM;
  396. goto fail_cleanup_queue;
  397. }
  398. priv->gendisk = gendisk;
  399. gendisk->major = ps3disk_major;
  400. gendisk->first_minor = devidx * PS3DISK_MINORS;
  401. gendisk->fops = &ps3disk_fops;
  402. gendisk->queue = queue;
  403. gendisk->private_data = dev;
  404. gendisk->driverfs_dev = &dev->sbd.core;
  405. snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
  406. devidx+'a');
  407. priv->blocking_factor = dev->blk_size >> 9;
  408. set_capacity(gendisk,
  409. dev->regions[dev->region_idx].size*priv->blocking_factor);
  410. dev_info(&dev->sbd.core,
  411. "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n",
  412. gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
  413. get_capacity(gendisk) >> 11);
  414. add_disk(gendisk);
  415. return 0;
  416. fail_cleanup_queue:
  417. blk_cleanup_queue(queue);
  418. fail_teardown:
  419. ps3stor_teardown(dev);
  420. fail_free_bounce:
  421. kfree(dev->bounce_buf);
  422. fail_free_priv:
  423. kfree(priv);
  424. ps3_system_bus_set_drvdata(_dev, NULL);
  425. fail:
  426. mutex_lock(&ps3disk_mask_mutex);
  427. __clear_bit(devidx, &ps3disk_mask);
  428. mutex_unlock(&ps3disk_mask_mutex);
  429. return error;
  430. }
  431. static int ps3disk_remove(struct ps3_system_bus_device *_dev)
  432. {
  433. struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
  434. struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
  435. mutex_lock(&ps3disk_mask_mutex);
  436. __clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
  437. &ps3disk_mask);
  438. mutex_unlock(&ps3disk_mask_mutex);
  439. del_gendisk(priv->gendisk);
  440. blk_cleanup_queue(priv->queue);
  441. put_disk(priv->gendisk);
  442. dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
  443. ps3disk_sync_cache(dev);
  444. ps3stor_teardown(dev);
  445. kfree(dev->bounce_buf);
  446. kfree(priv);
  447. ps3_system_bus_set_drvdata(_dev, NULL);
  448. return 0;
  449. }
  450. static struct ps3_system_bus_driver ps3disk = {
  451. .match_id = PS3_MATCH_ID_STOR_DISK,
  452. .core.name = DEVICE_NAME,
  453. .core.owner = THIS_MODULE,
  454. .probe = ps3disk_probe,
  455. .remove = ps3disk_remove,
  456. .shutdown = ps3disk_remove,
  457. };
  458. static int __init ps3disk_init(void)
  459. {
  460. int error;
  461. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  462. return -ENODEV;
  463. error = register_blkdev(0, DEVICE_NAME);
  464. if (error <= 0) {
  465. printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
  466. __LINE__, error);
  467. return error;
  468. }
  469. ps3disk_major = error;
  470. pr_info("%s:%u: registered block device major %d\n", __func__,
  471. __LINE__, ps3disk_major);
  472. error = ps3_system_bus_driver_register(&ps3disk);
  473. if (error)
  474. unregister_blkdev(ps3disk_major, DEVICE_NAME);
  475. return error;
  476. }
  477. static void __exit ps3disk_exit(void)
  478. {
  479. ps3_system_bus_driver_unregister(&ps3disk);
  480. unregister_blkdev(ps3disk_major, DEVICE_NAME);
  481. }
  482. module_init(ps3disk_init);
  483. module_exit(ps3disk_exit);
  484. MODULE_LICENSE("GPL");
  485. MODULE_DESCRIPTION("PS3 Disk Storage Driver");
  486. MODULE_AUTHOR("Sony Corporation");
  487. MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);