pmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 void 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. }
  60. invalidate_pmem(pmem->virt_addr + offset, len);
  61. }
  62. static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
  63. unsigned int len, unsigned int off, bool is_write,
  64. sector_t sector)
  65. {
  66. int rc = 0;
  67. bool bad_pmem = false;
  68. void *mem = kmap_atomic(page);
  69. phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
  70. void *pmem_addr = pmem->virt_addr + pmem_off;
  71. if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
  72. bad_pmem = true;
  73. if (!is_write) {
  74. if (unlikely(bad_pmem))
  75. rc = -EIO;
  76. else {
  77. rc = memcpy_from_pmem(mem + off, pmem_addr, len);
  78. flush_dcache_page(page);
  79. }
  80. } else {
  81. /*
  82. * Note that we write the data both before and after
  83. * clearing poison. The write before clear poison
  84. * handles situations where the latest written data is
  85. * preserved and the clear poison operation simply marks
  86. * the address range as valid without changing the data.
  87. * In this case application software can assume that an
  88. * interrupted write will either return the new good
  89. * data or an error.
  90. *
  91. * However, if pmem_clear_poison() leaves the data in an
  92. * indeterminate state we need to perform the write
  93. * after clear poison.
  94. */
  95. flush_dcache_page(page);
  96. memcpy_to_pmem(pmem_addr, mem + off, len);
  97. if (unlikely(bad_pmem)) {
  98. pmem_clear_poison(pmem, pmem_off, len);
  99. memcpy_to_pmem(pmem_addr, mem + off, len);
  100. }
  101. }
  102. kunmap_atomic(mem);
  103. return rc;
  104. }
  105. /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
  106. #ifndef REQ_FLUSH
  107. #define REQ_FLUSH REQ_PREFLUSH
  108. #endif
  109. static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
  110. {
  111. int rc = 0;
  112. bool do_acct;
  113. unsigned long start;
  114. struct bio_vec bvec;
  115. struct bvec_iter iter;
  116. struct pmem_device *pmem = q->queuedata;
  117. struct nd_region *nd_region = to_region(pmem);
  118. if (bio->bi_opf & REQ_FLUSH)
  119. nvdimm_flush(nd_region);
  120. do_acct = nd_iostat_start(bio, &start);
  121. bio_for_each_segment(bvec, bio, iter) {
  122. rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
  123. bvec.bv_offset, op_is_write(bio_op(bio)),
  124. iter.bi_sector);
  125. if (rc) {
  126. bio->bi_error = rc;
  127. break;
  128. }
  129. }
  130. if (do_acct)
  131. nd_iostat_end(bio, start);
  132. if (bio->bi_opf & REQ_FUA)
  133. nvdimm_flush(nd_region);
  134. bio_endio(bio);
  135. return BLK_QC_T_NONE;
  136. }
  137. static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  138. struct page *page, bool is_write)
  139. {
  140. struct pmem_device *pmem = bdev->bd_queue->queuedata;
  141. int rc;
  142. rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
  143. /*
  144. * The ->rw_page interface is subtle and tricky. The core
  145. * retries on any error, so we can only invoke page_endio() in
  146. * the successful completion case. Otherwise, we'll see crashes
  147. * caused by double completion.
  148. */
  149. if (rc == 0)
  150. page_endio(page, is_write, 0);
  151. return rc;
  152. }
  153. /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
  154. __weak long pmem_direct_access(struct block_device *bdev, sector_t sector,
  155. void **kaddr, pfn_t *pfn, long size)
  156. {
  157. struct pmem_device *pmem = bdev->bd_queue->queuedata;
  158. resource_size_t offset = sector * 512 + pmem->data_offset;
  159. if (unlikely(is_bad_pmem(&pmem->bb, sector, size)))
  160. return -EIO;
  161. *kaddr = pmem->virt_addr + offset;
  162. *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
  163. /*
  164. * If badblocks are present, limit known good range to the
  165. * requested range.
  166. */
  167. if (unlikely(pmem->bb.count))
  168. return size;
  169. return pmem->size - pmem->pfn_pad - offset;
  170. }
  171. static const struct block_device_operations pmem_fops = {
  172. .owner = THIS_MODULE,
  173. .rw_page = pmem_rw_page,
  174. .direct_access = pmem_direct_access,
  175. .revalidate_disk = nvdimm_revalidate_disk,
  176. };
  177. static void pmem_release_queue(void *q)
  178. {
  179. blk_cleanup_queue(q);
  180. }
  181. static void pmem_release_disk(void *disk)
  182. {
  183. del_gendisk(disk);
  184. put_disk(disk);
  185. }
  186. static int pmem_attach_disk(struct device *dev,
  187. struct nd_namespace_common *ndns)
  188. {
  189. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  190. struct nd_region *nd_region = to_nd_region(dev->parent);
  191. struct vmem_altmap __altmap, *altmap = NULL;
  192. struct resource *res = &nsio->res;
  193. struct nd_pfn *nd_pfn = NULL;
  194. int nid = dev_to_node(dev);
  195. struct nd_pfn_sb *pfn_sb;
  196. struct pmem_device *pmem;
  197. struct resource pfn_res;
  198. struct request_queue *q;
  199. struct gendisk *disk;
  200. void *addr;
  201. /* while nsio_rw_bytes is active, parse a pfn info block if present */
  202. if (is_nd_pfn(dev)) {
  203. nd_pfn = to_nd_pfn(dev);
  204. altmap = nvdimm_setup_pfn(nd_pfn, &pfn_res, &__altmap);
  205. if (IS_ERR(altmap))
  206. return PTR_ERR(altmap);
  207. }
  208. /* we're attaching a block device, disable raw namespace access */
  209. devm_nsio_disable(dev, nsio);
  210. pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
  211. if (!pmem)
  212. return -ENOMEM;
  213. dev_set_drvdata(dev, pmem);
  214. pmem->phys_addr = res->start;
  215. pmem->size = resource_size(res);
  216. if (nvdimm_has_flush(nd_region) < 0)
  217. dev_warn(dev, "unable to guarantee persistence of writes\n");
  218. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  219. dev_name(dev))) {
  220. dev_warn(dev, "could not reserve region %pR\n", res);
  221. return -EBUSY;
  222. }
  223. q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
  224. if (!q)
  225. return -ENOMEM;
  226. pmem->pfn_flags = PFN_DEV;
  227. if (is_nd_pfn(dev)) {
  228. addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
  229. altmap);
  230. pfn_sb = nd_pfn->pfn_sb;
  231. pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
  232. pmem->pfn_pad = resource_size(res) - resource_size(&pfn_res);
  233. pmem->pfn_flags |= PFN_MAP;
  234. res = &pfn_res; /* for badblocks populate */
  235. res->start += pmem->data_offset;
  236. } else if (pmem_should_map_pages(dev)) {
  237. addr = devm_memremap_pages(dev, &nsio->res,
  238. &q->q_usage_counter, NULL);
  239. pmem->pfn_flags |= PFN_MAP;
  240. } else
  241. addr = devm_memremap(dev, pmem->phys_addr,
  242. pmem->size, ARCH_MEMREMAP_PMEM);
  243. /*
  244. * At release time the queue must be dead before
  245. * devm_memremap_pages is unwound
  246. */
  247. if (devm_add_action_or_reset(dev, pmem_release_queue, q))
  248. return -ENOMEM;
  249. if (IS_ERR(addr))
  250. return PTR_ERR(addr);
  251. pmem->virt_addr = addr;
  252. blk_queue_write_cache(q, true, true);
  253. blk_queue_make_request(q, pmem_make_request);
  254. blk_queue_physical_block_size(q, PAGE_SIZE);
  255. blk_queue_max_hw_sectors(q, UINT_MAX);
  256. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  257. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  258. queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
  259. q->queuedata = pmem;
  260. disk = alloc_disk_node(0, nid);
  261. if (!disk)
  262. return -ENOMEM;
  263. disk->fops = &pmem_fops;
  264. disk->queue = q;
  265. disk->flags = GENHD_FL_EXT_DEVT;
  266. nvdimm_namespace_disk_name(ndns, disk->disk_name);
  267. set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
  268. / 512);
  269. if (devm_init_badblocks(dev, &pmem->bb))
  270. return -ENOMEM;
  271. nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
  272. disk->bb = &pmem->bb;
  273. device_add_disk(dev, disk);
  274. if (devm_add_action_or_reset(dev, pmem_release_disk, disk))
  275. return -ENOMEM;
  276. revalidate_disk(disk);
  277. return 0;
  278. }
  279. static int nd_pmem_probe(struct device *dev)
  280. {
  281. struct nd_namespace_common *ndns;
  282. ndns = nvdimm_namespace_common_probe(dev);
  283. if (IS_ERR(ndns))
  284. return PTR_ERR(ndns);
  285. if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
  286. return -ENXIO;
  287. if (is_nd_btt(dev))
  288. return nvdimm_namespace_attach_btt(ndns);
  289. if (is_nd_pfn(dev))
  290. return pmem_attach_disk(dev, ndns);
  291. /* if we find a valid info-block we'll come back as that personality */
  292. if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
  293. || nd_dax_probe(dev, ndns) == 0)
  294. return -ENXIO;
  295. /* ...otherwise we're just a raw pmem device */
  296. return pmem_attach_disk(dev, ndns);
  297. }
  298. static int nd_pmem_remove(struct device *dev)
  299. {
  300. if (is_nd_btt(dev))
  301. nvdimm_namespace_detach_btt(to_nd_btt(dev));
  302. nvdimm_flush(to_nd_region(dev->parent));
  303. return 0;
  304. }
  305. static void nd_pmem_shutdown(struct device *dev)
  306. {
  307. nvdimm_flush(to_nd_region(dev->parent));
  308. }
  309. static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
  310. {
  311. struct pmem_device *pmem = dev_get_drvdata(dev);
  312. struct nd_region *nd_region = to_region(pmem);
  313. resource_size_t offset = 0, end_trunc = 0;
  314. struct nd_namespace_common *ndns;
  315. struct nd_namespace_io *nsio;
  316. struct resource res;
  317. if (event != NVDIMM_REVALIDATE_POISON)
  318. return;
  319. if (is_nd_btt(dev)) {
  320. struct nd_btt *nd_btt = to_nd_btt(dev);
  321. ndns = nd_btt->ndns;
  322. } else if (is_nd_pfn(dev)) {
  323. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  324. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  325. ndns = nd_pfn->ndns;
  326. offset = pmem->data_offset + __le32_to_cpu(pfn_sb->start_pad);
  327. end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  328. } else
  329. ndns = to_ndns(dev);
  330. nsio = to_nd_namespace_io(&ndns->dev);
  331. res.start = nsio->res.start + offset;
  332. res.end = nsio->res.end - end_trunc;
  333. nvdimm_badblocks_populate(nd_region, &pmem->bb, &res);
  334. }
  335. MODULE_ALIAS("pmem");
  336. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
  337. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
  338. static struct nd_device_driver nd_pmem_driver = {
  339. .probe = nd_pmem_probe,
  340. .remove = nd_pmem_remove,
  341. .notify = nd_pmem_notify,
  342. .shutdown = nd_pmem_shutdown,
  343. .drv = {
  344. .name = "nd_pmem",
  345. },
  346. .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
  347. };
  348. static int __init pmem_init(void)
  349. {
  350. return nd_driver_register(&nd_pmem_driver);
  351. }
  352. module_init(pmem_init);
  353. static void pmem_exit(void)
  354. {
  355. driver_unregister(&nd_pmem_driver.drv);
  356. }
  357. module_exit(pmem_exit);
  358. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  359. MODULE_LICENSE("GPL v2");