pmem.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 completion cmp;
  24. };
  25. static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
  26. {
  27. return container_of(ref, struct dax_pmem, ref);
  28. }
  29. static void dax_pmem_percpu_release(struct percpu_ref *ref)
  30. {
  31. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  32. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  33. complete(&dax_pmem->cmp);
  34. }
  35. static void dax_pmem_percpu_exit(void *data)
  36. {
  37. struct percpu_ref *ref = data;
  38. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  39. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  40. wait_for_completion(&dax_pmem->cmp);
  41. percpu_ref_exit(ref);
  42. }
  43. static void dax_pmem_percpu_kill(void *data)
  44. {
  45. struct percpu_ref *ref = data;
  46. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  47. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  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. struct vmem_altmap __altmap, *altmap = NULL;
  64. ndns = nvdimm_namespace_common_probe(dev);
  65. if (IS_ERR(ndns))
  66. return PTR_ERR(ndns);
  67. nsio = to_nd_namespace_io(&ndns->dev);
  68. /* parse the 'pfn' info block via ->rw_bytes */
  69. rc = devm_nsio_enable(dev, nsio);
  70. if (rc)
  71. return rc;
  72. altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
  73. if (IS_ERR(altmap))
  74. return PTR_ERR(altmap);
  75. devm_nsio_disable(dev, nsio);
  76. pfn_sb = nd_pfn->pfn_sb;
  77. if (!devm_request_mem_region(dev, nsio->res.start,
  78. resource_size(&nsio->res),
  79. dev_name(&ndns->dev))) {
  80. dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
  81. return -EBUSY;
  82. }
  83. dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
  84. if (!dax_pmem)
  85. return -ENOMEM;
  86. dax_pmem->dev = dev;
  87. init_completion(&dax_pmem->cmp);
  88. rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
  89. GFP_KERNEL);
  90. if (rc)
  91. return rc;
  92. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
  93. &dax_pmem->ref);
  94. if (rc)
  95. return rc;
  96. addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
  97. if (IS_ERR(addr))
  98. return PTR_ERR(addr);
  99. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
  100. &dax_pmem->ref);
  101. if (rc)
  102. return rc;
  103. /* adjust the dax_region resource to the start of data */
  104. res.start += le64_to_cpu(pfn_sb->dataoff);
  105. rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
  106. if (rc != 2)
  107. return -EINVAL;
  108. dax_region = alloc_dax_region(dev, region_id, &res,
  109. le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
  110. if (!dax_region)
  111. return -ENOMEM;
  112. /* TODO: support for subdividing a dax region... */
  113. dev_dax = devm_create_dev_dax(dax_region, id, &res, 1);
  114. /* child dev_dax instances now own the lifetime of the dax_region */
  115. dax_region_put(dax_region);
  116. return PTR_ERR_OR_ZERO(dev_dax);
  117. }
  118. static struct nd_device_driver dax_pmem_driver = {
  119. .probe = dax_pmem_probe,
  120. .drv = {
  121. .name = "dax_pmem",
  122. },
  123. .type = ND_DRIVER_DAX_PMEM,
  124. };
  125. static int __init dax_pmem_init(void)
  126. {
  127. return nd_driver_register(&dax_pmem_driver);
  128. }
  129. module_init(dax_pmem_init);
  130. static void __exit dax_pmem_exit(void)
  131. {
  132. driver_unregister(&dax_pmem_driver.drv);
  133. }
  134. module_exit(dax_pmem_exit);
  135. MODULE_LICENSE("GPL v2");
  136. MODULE_AUTHOR("Intel Corporation");
  137. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);