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 "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. percpu_ref_exit(ref);
  41. }
  42. static void dax_pmem_percpu_kill(void *data)
  43. {
  44. struct percpu_ref *ref = data;
  45. struct dax_pmem *dax_pmem = to_dax_pmem(ref);
  46. dev_dbg(dax_pmem->dev, "%s\n", __func__);
  47. percpu_ref_kill(ref);
  48. wait_for_completion(&dax_pmem->cmp);
  49. }
  50. static int dax_pmem_probe(struct device *dev)
  51. {
  52. void *addr;
  53. struct resource res;
  54. struct dax_dev *dax_dev;
  55. int rc, id, region_id;
  56. struct nd_pfn_sb *pfn_sb;
  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), dev_name(dev))) {
  79. dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
  80. return -EBUSY;
  81. }
  82. dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
  83. if (!dax_pmem)
  84. return -ENOMEM;
  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_or_reset(dev, dax_pmem_percpu_exit,
  92. &dax_pmem->ref);
  93. if (rc)
  94. return rc;
  95. addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
  96. if (IS_ERR(addr))
  97. return PTR_ERR(addr);
  98. rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
  99. &dax_pmem->ref);
  100. if (rc)
  101. return rc;
  102. /* adjust the dax_region resource to the start of data */
  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. dax_dev = devm_create_dax_dev(dax_region, id, &res, 1);
  113. /* child dax_dev instances now own the lifetime of the dax_region */
  114. dax_region_put(dax_region);
  115. return PTR_ERR_OR_ZERO(dax_dev);
  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. static int __init dax_pmem_init(void)
  125. {
  126. return nd_driver_register(&dax_pmem_driver);
  127. }
  128. module_init(dax_pmem_init);
  129. static void __exit dax_pmem_exit(void)
  130. {
  131. driver_unregister(&dax_pmem_driver.drv);
  132. }
  133. module_exit(dax_pmem_exit);
  134. MODULE_LICENSE("GPL v2");
  135. MODULE_AUTHOR("Intel Corporation");
  136. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);