pmem.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright(c) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/percpu-refcount.h>
  14. #include <linux/memremap.h>
  15. #include <linux/module.h>
  16. #include <linux/pfn_t.h>
  17. #include "../nvdimm/pfn.h"
  18. #include "../nvdimm/nd.h"
  19. #include "device-dax.h"
  20. struct dax_pmem {
  21. struct device *dev;
  22. struct percpu_ref ref;
  23. struct dev_pagemap pgmap;
  24. struct completion cmp;
  25. };
  26. static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
  27. {
  28. return container_of(ref, struct dax_pmem, ref);
  29. }
  30. static void dax_pmem_percpu_release(struct percpu_ref *ref)
  31. {
  32. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  33. dev_dbg(dax_pmem->dev, "trace\n");
  34. complete(&dax_pmem->cmp);
  35. }
  36. static void dax_pmem_percpu_exit(void *data)
  37. {
  38. struct percpu_ref *ref = data;
  39. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  40. dev_dbg(dax_pmem->dev, "trace\n");
  41. wait_for_completion(&dax_pmem->cmp);
  42. percpu_ref_exit(ref);
  43. }
  44. static void dax_pmem_percpu_kill(struct percpu_ref *ref)
  45. {
  46. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  47. dev_dbg(dax_pmem->dev, "trace\n");
  48. percpu_ref_kill(ref);
  49. }
  50. static int dax_pmem_probe(struct device *dev)
  51. {
  52. void *addr;
  53. struct resource res;
  54. int rc, id, region_id;
  55. struct nd_pfn_sb *pfn_sb;
  56. struct dev_dax *dev_dax;
  57. struct dax_pmem *dax_pmem;
  58. struct nd_namespace_io *nsio;
  59. struct dax_region *dax_region;
  60. struct nd_namespace_common *ndns;
  61. struct nd_dax *nd_dax = to_nd_dax(dev);
  62. struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
  63. ndns = nvdimm_namespace_common_probe(dev);
  64. if (IS_ERR(ndns))
  65. return PTR_ERR(ndns);
  66. nsio = to_nd_namespace_io(&ndns->dev);
  67. dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
  68. if (!dax_pmem)
  69. return -ENOMEM;
  70. /* parse the 'pfn' info block via ->rw_bytes */
  71. rc = devm_nsio_enable(dev, nsio);
  72. if (rc)
  73. return rc;
  74. rc = nvdimm_setup_pfn(nd_pfn, &dax_pmem->pgmap);
  75. if (rc)
  76. return rc;
  77. devm_nsio_disable(dev, nsio);
  78. pfn_sb = nd_pfn->pfn_sb;
  79. if (!devm_request_mem_region(dev, nsio->res.start,
  80. resource_size(&nsio->res),
  81. dev_name(&ndns->dev))) {
  82. dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
  83. return -EBUSY;
  84. }
  85. dax_pmem->dev = dev;
  86. init_completion(&dax_pmem->cmp);
  87. rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
  88. GFP_KERNEL);
  89. if (rc)
  90. return rc;
  91. rc = devm_add_action(dev, dax_pmem_percpu_exit, &dax_pmem->ref);
  92. if (rc) {
  93. percpu_ref_exit(&dax_pmem->ref);
  94. return rc;
  95. }
  96. dax_pmem->pgmap.ref = &dax_pmem->ref;
  97. dax_pmem->pgmap.kill = dax_pmem_percpu_kill;
  98. addr = devm_memremap_pages(dev, &dax_pmem->pgmap);
  99. if (IS_ERR(addr))
  100. return PTR_ERR(addr);
  101. /* adjust the dax_region resource to the start of data */
  102. memcpy(&res, &dax_pmem->pgmap.res, sizeof(res));
  103. res.start += le64_to_cpu(pfn_sb->dataoff);
  104. rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
  105. if (rc != 2)
  106. return -EINVAL;
  107. dax_region = alloc_dax_region(dev, region_id, &res,
  108. le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
  109. if (!dax_region)
  110. return -ENOMEM;
  111. /* TODO: support for subdividing a dax region... */
  112. dev_dax = devm_create_dev_dax(dax_region, id, &res, 1);
  113. /* child dev_dax instances now own the lifetime of the dax_region */
  114. dax_region_put(dax_region);
  115. return PTR_ERR_OR_ZERO(dev_dax);
  116. }
  117. static struct nd_device_driver dax_pmem_driver = {
  118. .probe = dax_pmem_probe,
  119. .drv = {
  120. .name = "dax_pmem",
  121. },
  122. .type = ND_DRIVER_DAX_PMEM,
  123. };
  124. module_nd_driver(dax_pmem_driver);
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_AUTHOR("Intel Corporation");
  127. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);