pmem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Persistent Memory Driver
  3. *
  4. * Copyright (c) 2014-2015, Intel Corporation.
  5. * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
  6. * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <asm/cacheflush.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/badblocks.h>
  25. #include <linux/memremap.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/pfn_t.h>
  28. #include <linux/slab.h>
  29. #include <linux/pmem.h>
  30. #include <linux/nd.h>
  31. #include "pmem.h"
  32. #include "pfn.h"
  33. #include "nd.h"
  34. static struct device *to_dev(struct pmem_device *pmem)
  35. {
  36. /*
  37. * nvdimm bus services need a 'dev' parameter, and we record the device
  38. * at init in bb.dev.
  39. */
  40. return pmem->bb.dev;
  41. }
  42. static struct nd_region *to_region(struct pmem_device *pmem)
  43. {
  44. return to_nd_region(to_dev(pmem)->parent);
  45. }
  46. static int pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
  47. unsigned int len)
  48. {
  49. struct device *dev = to_dev(pmem);
  50. sector_t sector;
  51. long cleared;
  52. sector = (offset - pmem->data_offset) / 512;
  53. cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
  54. if (cleared > 0 && cleared / 512) {
  55. dev_dbg(dev, "%s: %#llx clear %ld sector%s\n",
  56. __func__, (unsigned long long) sector,
  57. cleared / 512, cleared / 512 > 1 ? "s" : "");
  58. badblocks_clear(&pmem->bb, sector, cleared / 512);
  59. } else {
  60. return -EIO;
  61. }
  62. invalidate_pmem(pmem->virt_addr + offset, len);
  63. return 0;
  64. }
  65. static void write_pmem(void *pmem_addr, struct page *page,
  66. unsigned int off, unsigned int len)
  67. {
  68. void *mem = kmap_atomic(page);
  69. memcpy_to_pmem(pmem_addr, mem + off, len);
  70. kunmap_atomic(mem);
  71. }
  72. static int read_pmem(struct page *page, unsigned int off,
  73. void *pmem_addr, unsigned int len)
  74. {
  75. int rc;
  76. void *mem = kmap_atomic(page);
  77. rc = memcpy_from_pmem(mem + off, pmem_addr, len);
  78. kunmap_atomic(mem);
  79. if (rc)
  80. return -EIO;
  81. return 0;
  82. }
  83. static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
  84. unsigned int len, unsigned int off, bool is_write,
  85. sector_t sector)
  86. {
  87. int rc = 0;
  88. bool bad_pmem = false;
  89. phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
  90. void *pmem_addr = pmem->virt_addr + pmem_off;
  91. if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
  92. bad_pmem = true;
  93. if (!is_write) {
  94. if (unlikely(bad_pmem))
  95. rc = -EIO;
  96. else {
  97. rc = read_pmem(page, off, pmem_addr, len);
  98. flush_dcache_page(page);
  99. }
  100. } else {
  101. /*
  102. * Note that we write the data both before and after
  103. * clearing poison. The write before clear poison
  104. * handles situations where the latest written data is
  105. * preserved and the clear poison operation simply marks
  106. * the address range as valid without changing the data.
  107. * In this case application software can assume that an
  108. * interrupted write will either return the new good
  109. * data or an error.
  110. *
  111. * However, if pmem_clear_poison() leaves the data in an
  112. * indeterminate state we need to perform the write
  113. * after clear poison.
  114. */
  115. flush_dcache_page(page);
  116. write_pmem(pmem_addr, page, off, len);
  117. if (unlikely(bad_pmem)) {
  118. rc = pmem_clear_poison(pmem, pmem_off, len);
  119. write_pmem(pmem_addr, page, off, len);
  120. }
  121. }
  122. return rc;
  123. }
  124. /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
  125. #ifndef REQ_FLUSH
  126. #define REQ_FLUSH REQ_PREFLUSH
  127. #endif
  128. static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
  129. {
  130. int rc = 0;
  131. bool do_acct;
  132. unsigned long start;
  133. struct bio_vec bvec;
  134. struct bvec_iter iter;
  135. struct pmem_device *pmem = q->queuedata;
  136. struct nd_region *nd_region = to_region(pmem);
  137. if (bio->bi_opf & REQ_FLUSH)
  138. nvdimm_flush(nd_region);
  139. do_acct = nd_iostat_start(bio, &start);
  140. bio_for_each_segment(bvec, bio, iter) {
  141. rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
  142. bvec.bv_offset, op_is_write(bio_op(bio)),
  143. iter.bi_sector);
  144. if (rc) {
  145. bio->bi_error = rc;
  146. break;
  147. }
  148. }
  149. if (do_acct)
  150. nd_iostat_end(bio, start);
  151. if (bio->bi_opf & REQ_FUA)
  152. nvdimm_flush(nd_region);
  153. bio_endio(bio);
  154. return BLK_QC_T_NONE;
  155. }
  156. static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  157. struct page *page, bool is_write)
  158. {
  159. struct pmem_device *pmem = bdev->bd_queue->queuedata;
  160. int rc;
  161. rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
  162. /*
  163. * The ->rw_page interface is subtle and tricky. The core
  164. * retries on any error, so we can only invoke page_endio() in
  165. * the successful completion case. Otherwise, we'll see crashes
  166. * caused by double completion.
  167. */
  168. if (rc == 0)
  169. page_endio(page, is_write, 0);
  170. return rc;
  171. }
  172. /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
  173. __weak long pmem_direct_access(struct block_device *bdev, sector_t sector,
  174. void **kaddr, pfn_t *pfn, long size)
  175. {
  176. struct pmem_device *pmem = bdev->bd_queue->queuedata;
  177. resource_size_t offset = sector * 512 + pmem->data_offset;
  178. if (unlikely(is_bad_pmem(&pmem->bb, sector, size)))
  179. return -EIO;
  180. *kaddr = pmem->virt_addr + offset;
  181. *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
  182. /*
  183. * If badblocks are present, limit known good range to the
  184. * requested range.
  185. */
  186. if (unlikely(pmem->bb.count))
  187. return size;
  188. return pmem->size - pmem->pfn_pad - offset;
  189. }
  190. static const struct block_device_operations pmem_fops = {
  191. .owner = THIS_MODULE,
  192. .rw_page = pmem_rw_page,
  193. .direct_access = pmem_direct_access,
  194. .revalidate_disk = nvdimm_revalidate_disk,
  195. };
  196. static void pmem_release_queue(void *q)
  197. {
  198. blk_cleanup_queue(q);
  199. }
  200. static void pmem_release_disk(void *disk)
  201. {
  202. del_gendisk(disk);
  203. put_disk(disk);
  204. }
  205. static int pmem_attach_disk(struct device *dev,
  206. struct nd_namespace_common *ndns)
  207. {
  208. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  209. struct nd_region *nd_region = to_nd_region(dev->parent);
  210. struct vmem_altmap __altmap, *altmap = NULL;
  211. struct resource *res = &nsio->res;
  212. struct nd_pfn *nd_pfn = NULL;
  213. int nid = dev_to_node(dev);
  214. struct nd_pfn_sb *pfn_sb;
  215. struct pmem_device *pmem;
  216. struct resource pfn_res;
  217. struct request_queue *q;
  218. struct gendisk *disk;
  219. void *addr;
  220. /* while nsio_rw_bytes is active, parse a pfn info block if present */
  221. if (is_nd_pfn(dev)) {
  222. nd_pfn = to_nd_pfn(dev);
  223. altmap = nvdimm_setup_pfn(nd_pfn, &pfn_res, &__altmap);
  224. if (IS_ERR(altmap))
  225. return PTR_ERR(altmap);
  226. }
  227. /* we're attaching a block device, disable raw namespace access */
  228. devm_nsio_disable(dev, nsio);
  229. pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
  230. if (!pmem)
  231. return -ENOMEM;
  232. dev_set_drvdata(dev, pmem);
  233. pmem->phys_addr = res->start;
  234. pmem->size = resource_size(res);
  235. if (nvdimm_has_flush(nd_region) < 0)
  236. dev_warn(dev, "unable to guarantee persistence of writes\n");
  237. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  238. dev_name(dev))) {
  239. dev_warn(dev, "could not reserve region %pR\n", res);
  240. return -EBUSY;
  241. }
  242. q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
  243. if (!q)
  244. return -ENOMEM;
  245. pmem->pfn_flags = PFN_DEV;
  246. if (is_nd_pfn(dev)) {
  247. addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
  248. altmap);
  249. pfn_sb = nd_pfn->pfn_sb;
  250. pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
  251. pmem->pfn_pad = resource_size(res) - resource_size(&pfn_res);
  252. pmem->pfn_flags |= PFN_MAP;
  253. res = &pfn_res; /* for badblocks populate */
  254. res->start += pmem->data_offset;
  255. } else if (pmem_should_map_pages(dev)) {
  256. addr = devm_memremap_pages(dev, &nsio->res,
  257. &q->q_usage_counter, NULL);
  258. pmem->pfn_flags |= PFN_MAP;
  259. } else
  260. addr = devm_memremap(dev, pmem->phys_addr,
  261. pmem->size, ARCH_MEMREMAP_PMEM);
  262. /*
  263. * At release time the queue must be dead before
  264. * devm_memremap_pages is unwound
  265. */
  266. if (devm_add_action_or_reset(dev, pmem_release_queue, q))
  267. return -ENOMEM;
  268. if (IS_ERR(addr))
  269. return PTR_ERR(addr);
  270. pmem->virt_addr = addr;
  271. blk_queue_write_cache(q, true, true);
  272. blk_queue_make_request(q, pmem_make_request);
  273. blk_queue_physical_block_size(q, PAGE_SIZE);
  274. blk_queue_max_hw_sectors(q, UINT_MAX);
  275. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  276. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  277. queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
  278. q->queuedata = pmem;
  279. disk = alloc_disk_node(0, nid);
  280. if (!disk)
  281. return -ENOMEM;
  282. disk->fops = &pmem_fops;
  283. disk->queue = q;
  284. disk->flags = GENHD_FL_EXT_DEVT;
  285. nvdimm_namespace_disk_name(ndns, disk->disk_name);
  286. set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
  287. / 512);
  288. if (devm_init_badblocks(dev, &pmem->bb))
  289. return -ENOMEM;
  290. nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
  291. disk->bb = &pmem->bb;
  292. device_add_disk(dev, disk);
  293. if (devm_add_action_or_reset(dev, pmem_release_disk, disk))
  294. return -ENOMEM;
  295. revalidate_disk(disk);
  296. return 0;
  297. }
  298. static int nd_pmem_probe(struct device *dev)
  299. {
  300. struct nd_namespace_common *ndns;
  301. ndns = nvdimm_namespace_common_probe(dev);
  302. if (IS_ERR(ndns))
  303. return PTR_ERR(ndns);
  304. if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
  305. return -ENXIO;
  306. if (is_nd_btt(dev))
  307. return nvdimm_namespace_attach_btt(ndns);
  308. if (is_nd_pfn(dev))
  309. return pmem_attach_disk(dev, ndns);
  310. /* if we find a valid info-block we'll come back as that personality */
  311. if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
  312. || nd_dax_probe(dev, ndns) == 0)
  313. return -ENXIO;
  314. /* ...otherwise we're just a raw pmem device */
  315. return pmem_attach_disk(dev, ndns);
  316. }
  317. static int nd_pmem_remove(struct device *dev)
  318. {
  319. if (is_nd_btt(dev))
  320. nvdimm_namespace_detach_btt(to_nd_btt(dev));
  321. nvdimm_flush(to_nd_region(dev->parent));
  322. return 0;
  323. }
  324. static void nd_pmem_shutdown(struct device *dev)
  325. {
  326. nvdimm_flush(to_nd_region(dev->parent));
  327. }
  328. static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
  329. {
  330. struct nd_region *nd_region;
  331. resource_size_t offset = 0, end_trunc = 0;
  332. struct nd_namespace_common *ndns;
  333. struct nd_namespace_io *nsio;
  334. struct resource res;
  335. struct badblocks *bb;
  336. if (event != NVDIMM_REVALIDATE_POISON)
  337. return;
  338. if (is_nd_btt(dev)) {
  339. struct nd_btt *nd_btt = to_nd_btt(dev);
  340. ndns = nd_btt->ndns;
  341. nd_region = to_nd_region(ndns->dev.parent);
  342. nsio = to_nd_namespace_io(&ndns->dev);
  343. bb = &nsio->bb;
  344. } else {
  345. struct pmem_device *pmem = dev_get_drvdata(dev);
  346. nd_region = to_region(pmem);
  347. bb = &pmem->bb;
  348. if (is_nd_pfn(dev)) {
  349. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  350. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  351. ndns = nd_pfn->ndns;
  352. offset = pmem->data_offset +
  353. __le32_to_cpu(pfn_sb->start_pad);
  354. end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  355. } else {
  356. ndns = to_ndns(dev);
  357. }
  358. nsio = to_nd_namespace_io(&ndns->dev);
  359. }
  360. res.start = nsio->res.start + offset;
  361. res.end = nsio->res.end - end_trunc;
  362. nvdimm_badblocks_populate(nd_region, bb, &res);
  363. }
  364. MODULE_ALIAS("pmem");
  365. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
  366. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
  367. static struct nd_device_driver nd_pmem_driver = {
  368. .probe = nd_pmem_probe,
  369. .remove = nd_pmem_remove,
  370. .notify = nd_pmem_notify,
  371. .shutdown = nd_pmem_shutdown,
  372. .drv = {
  373. .name = "nd_pmem",
  374. },
  375. .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
  376. };
  377. static int __init pmem_init(void)
  378. {
  379. return nd_driver_register(&nd_pmem_driver);
  380. }
  381. module_init(pmem_init);
  382. static void pmem_exit(void)
  383. {
  384. driver_unregister(&nd_pmem_driver.drv);
  385. }
  386. module_exit(pmem_exit);
  387. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  388. MODULE_LICENSE("GPL v2");