region.c 4.1 KB

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