pmem.c 12 KB

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