region.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_dbg(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_dbg(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. if (is_memory(&nd_region->dev)) {
  41. struct resource ndr_res;
  42. if (devm_init_badblocks(dev, &nd_region->bb))
  43. return -ENODEV;
  44. nd_region->bb_state = sysfs_get_dirent(nd_region->dev.kobj.sd,
  45. "badblocks");
  46. if (!nd_region->bb_state)
  47. dev_warn(&nd_region->dev,
  48. "'badblocks' notification disabled\n");
  49. ndr_res.start = nd_region->ndr_start;
  50. ndr_res.end = nd_region->ndr_start + nd_region->ndr_size - 1;
  51. nvdimm_badblocks_populate(nd_region, &nd_region->bb, &ndr_res);
  52. }
  53. rc = nd_region_register_namespaces(nd_region, &err);
  54. if (rc < 0)
  55. return rc;
  56. ndrd = dev_get_drvdata(dev);
  57. ndrd->ns_active = rc;
  58. ndrd->ns_count = rc + err;
  59. if (rc && err && rc == err)
  60. return -ENODEV;
  61. nd_region->btt_seed = nd_btt_create(nd_region);
  62. nd_region->pfn_seed = nd_pfn_create(nd_region);
  63. nd_region->dax_seed = nd_dax_create(nd_region);
  64. if (err == 0)
  65. return 0;
  66. /*
  67. * Given multiple namespaces per region, we do not want to
  68. * disable all the successfully registered peer namespaces upon
  69. * a single registration failure. If userspace is missing a
  70. * namespace that it expects it can disable/re-enable the region
  71. * to retry discovery after correcting the failure.
  72. * <regionX>/namespaces returns the current
  73. * "<async-registered>/<total>" namespace count.
  74. */
  75. dev_err(dev, "failed to register %d namespace%s, continuing...\n",
  76. err, err == 1 ? "" : "s");
  77. return 0;
  78. }
  79. static int child_unregister(struct device *dev, void *data)
  80. {
  81. nd_device_unregister(dev, ND_SYNC);
  82. return 0;
  83. }
  84. static int nd_region_remove(struct device *dev)
  85. {
  86. struct nd_region *nd_region = to_nd_region(dev);
  87. device_for_each_child(dev, NULL, child_unregister);
  88. /* flush attribute readers and disable */
  89. nvdimm_bus_lock(dev);
  90. nd_region->ns_seed = NULL;
  91. nd_region->btt_seed = NULL;
  92. nd_region->pfn_seed = NULL;
  93. nd_region->dax_seed = NULL;
  94. dev_set_drvdata(dev, NULL);
  95. nvdimm_bus_unlock(dev);
  96. /*
  97. * Note, this assumes device_lock() context to not race
  98. * nd_region_notify()
  99. */
  100. sysfs_put(nd_region->bb_state);
  101. nd_region->bb_state = NULL;
  102. return 0;
  103. }
  104. static int child_notify(struct device *dev, void *data)
  105. {
  106. nd_device_notify(dev, *(enum nvdimm_event *) data);
  107. return 0;
  108. }
  109. static void nd_region_notify(struct device *dev, enum nvdimm_event event)
  110. {
  111. if (event == NVDIMM_REVALIDATE_POISON) {
  112. struct nd_region *nd_region = to_nd_region(dev);
  113. struct resource res;
  114. if (is_memory(&nd_region->dev)) {
  115. res.start = nd_region->ndr_start;
  116. res.end = nd_region->ndr_start +
  117. nd_region->ndr_size - 1;
  118. nvdimm_badblocks_populate(nd_region,
  119. &nd_region->bb, &res);
  120. if (nd_region->bb_state)
  121. sysfs_notify_dirent(nd_region->bb_state);
  122. }
  123. }
  124. device_for_each_child(dev, &event, child_notify);
  125. }
  126. static struct nd_device_driver nd_region_driver = {
  127. .probe = nd_region_probe,
  128. .remove = nd_region_remove,
  129. .notify = nd_region_notify,
  130. .drv = {
  131. .name = "nd_region",
  132. },
  133. .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
  134. };
  135. int __init nd_region_init(void)
  136. {
  137. return nd_driver_register(&nd_region_driver);
  138. }
  139. void nd_region_exit(void)
  140. {
  141. driver_unregister(&nd_region_driver.drv);
  142. }
  143. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
  144. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);