region.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/cpumask.h>
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include <linux/nd.h>
  9. #include "nd-core.h"
  10. #include "nd.h"
  11. static int nd_region_probe(struct device *dev)
  12. {
  13. int err, rc;
  14. static unsigned long once;
  15. struct nd_region_data *ndrd;
  16. struct nd_region *nd_region = to_nd_region(dev);
  17. struct range range = {
  18. .start = nd_region->ndr_start,
  19. .end = nd_region->ndr_start + nd_region->ndr_size - 1,
  20. };
  21. if (nd_region->num_lanes > num_online_cpus()
  22. && nd_region->num_lanes < num_possible_cpus()
  23. && !test_and_set_bit(0, &once)) {
  24. dev_dbg(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
  25. num_online_cpus(), nd_region->num_lanes,
  26. num_possible_cpus());
  27. dev_dbg(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
  28. nd_region->num_lanes);
  29. }
  30. rc = nd_region_activate(nd_region);
  31. if (rc)
  32. return rc;
  33. if (devm_init_badblocks(dev, &nd_region->bb))
  34. return -ENODEV;
  35. nd_region->bb_state =
  36. sysfs_get_dirent(nd_region->dev.kobj.sd, "badblocks");
  37. if (!nd_region->bb_state)
  38. dev_warn(dev, "'badblocks' notification disabled\n");
  39. nvdimm_badblocks_populate(nd_region, &nd_region->bb, &range);
  40. rc = nd_region_register_namespaces(nd_region, &err);
  41. if (rc < 0)
  42. return rc;
  43. ndrd = dev_get_drvdata(dev);
  44. ndrd->ns_active = rc;
  45. ndrd->ns_count = rc + err;
  46. if (rc && err && rc == err)
  47. return -ENODEV;
  48. nd_region->btt_seed = nd_btt_create(nd_region);
  49. nd_region->pfn_seed = nd_pfn_create(nd_region);
  50. nd_region->dax_seed = nd_dax_create(nd_region);
  51. if (err == 0)
  52. return 0;
  53. /*
  54. * Given multiple namespaces per region, we do not want to
  55. * disable all the successfully registered peer namespaces upon
  56. * a single registration failure. If userspace is missing a
  57. * namespace that it expects it can disable/re-enable the region
  58. * to retry discovery after correcting the failure.
  59. * <regionX>/namespaces returns the current
  60. * "<async-registered>/<total>" namespace count.
  61. */
  62. dev_err(dev, "failed to register %d namespace%s, continuing...\n",
  63. err, err == 1 ? "" : "s");
  64. return 0;
  65. }
  66. static int child_unregister(struct device *dev, void *data)
  67. {
  68. nd_device_unregister(dev, ND_SYNC);
  69. return 0;
  70. }
  71. static void nd_region_remove(struct device *dev)
  72. {
  73. struct nd_region *nd_region = to_nd_region(dev);
  74. device_for_each_child(dev, NULL, child_unregister);
  75. /* flush attribute readers and disable */
  76. nvdimm_bus_lock(dev);
  77. nd_region->ns_seed = NULL;
  78. nd_region->btt_seed = NULL;
  79. nd_region->pfn_seed = NULL;
  80. nd_region->dax_seed = NULL;
  81. dev_set_drvdata(dev, NULL);
  82. nvdimm_bus_unlock(dev);
  83. /*
  84. * Note, this assumes device_lock() context to not race
  85. * nd_region_notify()
  86. */
  87. sysfs_put(nd_region->bb_state);
  88. nd_region->bb_state = NULL;
  89. }
  90. static int child_notify(struct device *dev, void *data)
  91. {
  92. nd_device_notify(dev, *(enum nvdimm_event *) data);
  93. return 0;
  94. }
  95. static void nd_region_notify(struct device *dev, enum nvdimm_event event)
  96. {
  97. if (event == NVDIMM_REVALIDATE_POISON) {
  98. struct nd_region *nd_region = to_nd_region(dev);
  99. if (is_memory(&nd_region->dev)) {
  100. struct range range = {
  101. .start = nd_region->ndr_start,
  102. .end = nd_region->ndr_start +
  103. nd_region->ndr_size - 1,
  104. };
  105. nvdimm_badblocks_populate(nd_region,
  106. &nd_region->bb, &range);
  107. if (nd_region->bb_state)
  108. sysfs_notify_dirent(nd_region->bb_state);
  109. }
  110. }
  111. device_for_each_child(dev, &event, child_notify);
  112. }
  113. static struct nd_device_driver nd_region_driver = {
  114. .probe = nd_region_probe,
  115. .remove = nd_region_remove,
  116. .notify = nd_region_notify,
  117. .drv = {
  118. .name = "nd_region",
  119. },
  120. .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
  121. };
  122. int __init nd_region_init(void)
  123. {
  124. return nd_driver_register(&nd_region_driver);
  125. }
  126. void nd_region_exit(void)
  127. {
  128. driver_unregister(&nd_region_driver.drv);
  129. }
  130. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);