dax_devs.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/sizes.h>
  7. #include <linux/slab.h>
  8. #include <linux/mm.h>
  9. #include "nd-core.h"
  10. #include "pfn.h"
  11. #include "nd.h"
  12. static void nd_dax_release(struct device *dev)
  13. {
  14. struct nd_region *nd_region = to_nd_region(dev->parent);
  15. struct nd_dax *nd_dax = to_nd_dax(dev);
  16. struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
  17. dev_dbg(dev, "trace\n");
  18. nd_detach_ndns(dev, &nd_pfn->ndns);
  19. ida_simple_remove(&nd_region->dax_ida, nd_pfn->id);
  20. kfree(nd_pfn->uuid);
  21. kfree(nd_dax);
  22. }
  23. struct nd_dax *to_nd_dax(struct device *dev)
  24. {
  25. struct nd_dax *nd_dax = container_of(dev, struct nd_dax, nd_pfn.dev);
  26. WARN_ON(!is_nd_dax(dev));
  27. return nd_dax;
  28. }
  29. EXPORT_SYMBOL(to_nd_dax);
  30. static const struct device_type nd_dax_device_type = {
  31. .name = "nd_dax",
  32. .release = nd_dax_release,
  33. .groups = nd_pfn_attribute_groups,
  34. };
  35. bool is_nd_dax(struct device *dev)
  36. {
  37. return dev ? dev->type == &nd_dax_device_type : false;
  38. }
  39. EXPORT_SYMBOL(is_nd_dax);
  40. static struct nd_dax *nd_dax_alloc(struct nd_region *nd_region)
  41. {
  42. struct nd_pfn *nd_pfn;
  43. struct nd_dax *nd_dax;
  44. struct device *dev;
  45. nd_dax = kzalloc(sizeof(*nd_dax), GFP_KERNEL);
  46. if (!nd_dax)
  47. return NULL;
  48. nd_pfn = &nd_dax->nd_pfn;
  49. nd_pfn->id = ida_simple_get(&nd_region->dax_ida, 0, 0, GFP_KERNEL);
  50. if (nd_pfn->id < 0) {
  51. kfree(nd_dax);
  52. return NULL;
  53. }
  54. dev = &nd_pfn->dev;
  55. dev_set_name(dev, "dax%d.%d", nd_region->id, nd_pfn->id);
  56. dev->type = &nd_dax_device_type;
  57. dev->parent = &nd_region->dev;
  58. return nd_dax;
  59. }
  60. struct device *nd_dax_create(struct nd_region *nd_region)
  61. {
  62. struct device *dev = NULL;
  63. struct nd_dax *nd_dax;
  64. if (!is_memory(&nd_region->dev))
  65. return NULL;
  66. nd_dax = nd_dax_alloc(nd_region);
  67. if (nd_dax)
  68. dev = nd_pfn_devinit(&nd_dax->nd_pfn, NULL);
  69. __nd_device_register(dev);
  70. return dev;
  71. }
  72. int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
  73. {
  74. int rc;
  75. struct nd_dax *nd_dax;
  76. struct device *dax_dev;
  77. struct nd_pfn *nd_pfn;
  78. struct nd_pfn_sb *pfn_sb;
  79. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  80. if (ndns->force_raw)
  81. return -ENODEV;
  82. switch (ndns->claim_class) {
  83. case NVDIMM_CCLASS_NONE:
  84. case NVDIMM_CCLASS_DAX:
  85. break;
  86. default:
  87. return -ENODEV;
  88. }
  89. nvdimm_bus_lock(&ndns->dev);
  90. nd_dax = nd_dax_alloc(nd_region);
  91. nd_pfn = &nd_dax->nd_pfn;
  92. dax_dev = nd_pfn_devinit(nd_pfn, ndns);
  93. nvdimm_bus_unlock(&ndns->dev);
  94. if (!dax_dev)
  95. return -ENOMEM;
  96. pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
  97. nd_pfn->pfn_sb = pfn_sb;
  98. rc = nd_pfn_validate(nd_pfn, DAX_SIG);
  99. dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
  100. if (rc < 0) {
  101. nd_detach_ndns(dax_dev, &nd_pfn->ndns);
  102. put_device(dax_dev);
  103. } else
  104. __nd_device_register(dax_dev);
  105. return rc;
  106. }
  107. EXPORT_SYMBOL(nd_dax_probe);