pmem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2016 - 2018 Intel Corporation. All rights reserved. */
  3. #include <linux/memremap.h>
  4. #include <linux/module.h>
  5. #include <linux/pfn_t.h>
  6. #include "../nvdimm/pfn.h"
  7. #include "../nvdimm/nd.h"
  8. #include "bus.h"
  9. static struct dev_dax *__dax_pmem_probe(struct device *dev)
  10. {
  11. struct range range;
  12. int rc, id, region_id;
  13. resource_size_t offset;
  14. struct nd_pfn_sb *pfn_sb;
  15. struct dev_dax *dev_dax;
  16. struct dev_dax_data data;
  17. struct nd_namespace_io *nsio;
  18. struct dax_region *dax_region;
  19. struct dev_pagemap pgmap = { };
  20. struct nd_namespace_common *ndns;
  21. struct nd_dax *nd_dax = to_nd_dax(dev);
  22. struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
  23. struct nd_region *nd_region = to_nd_region(dev->parent);
  24. ndns = nvdimm_namespace_common_probe(dev);
  25. if (IS_ERR(ndns))
  26. return ERR_CAST(ndns);
  27. /* parse the 'pfn' info block via ->rw_bytes */
  28. rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
  29. if (rc)
  30. return ERR_PTR(rc);
  31. rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
  32. if (rc)
  33. return ERR_PTR(rc);
  34. devm_namespace_disable(dev, ndns);
  35. /* reserve the metadata area, device-dax will reserve the data */
  36. pfn_sb = nd_pfn->pfn_sb;
  37. offset = le64_to_cpu(pfn_sb->dataoff);
  38. nsio = to_nd_namespace_io(&ndns->dev);
  39. if (!devm_request_mem_region(dev, nsio->res.start, offset,
  40. dev_name(&ndns->dev))) {
  41. dev_warn(dev, "could not reserve metadata\n");
  42. return ERR_PTR(-EBUSY);
  43. }
  44. rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
  45. if (rc != 2)
  46. return ERR_PTR(-EINVAL);
  47. /* adjust the dax_region range to the start of data */
  48. range = pgmap.range;
  49. range.start += offset;
  50. dax_region = alloc_dax_region(dev, region_id, &range,
  51. nd_region->target_node, le32_to_cpu(pfn_sb->align),
  52. IORESOURCE_DAX_STATIC);
  53. if (!dax_region)
  54. return ERR_PTR(-ENOMEM);
  55. data = (struct dev_dax_data) {
  56. .dax_region = dax_region,
  57. .id = id,
  58. .pgmap = &pgmap,
  59. .size = range_len(&range),
  60. };
  61. dev_dax = devm_create_dev_dax(&data);
  62. /* child dev_dax instances now own the lifetime of the dax_region */
  63. dax_region_put(dax_region);
  64. return dev_dax;
  65. }
  66. static int dax_pmem_probe(struct device *dev)
  67. {
  68. return PTR_ERR_OR_ZERO(__dax_pmem_probe(dev));
  69. }
  70. static struct nd_device_driver dax_pmem_driver = {
  71. .probe = dax_pmem_probe,
  72. .drv = {
  73. .name = "dax_pmem",
  74. },
  75. .type = ND_DRIVER_DAX_PMEM,
  76. };
  77. static int __init dax_pmem_init(void)
  78. {
  79. return nd_driver_register(&dax_pmem_driver);
  80. }
  81. module_init(dax_pmem_init);
  82. static void __exit dax_pmem_exit(void)
  83. {
  84. driver_unregister(&dax_pmem_driver.drv);
  85. }
  86. module_exit(dax_pmem_exit);
  87. MODULE_LICENSE("GPL v2");
  88. MODULE_AUTHOR("Intel Corporation");
  89. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);