region.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.h"
  18. static int nd_region_probe(struct device *dev)
  19. {
  20. int err, rc;
  21. static unsigned long once;
  22. struct nd_region_data *ndrd;
  23. struct nd_region *nd_region = to_nd_region(dev);
  24. if (nd_region->num_lanes > num_online_cpus()
  25. && nd_region->num_lanes < num_possible_cpus()
  26. && !test_and_set_bit(0, &once)) {
  27. dev_info(dev, "online cpus (%d) < concurrent i/o lanes (%d) < possible cpus (%d)\n",
  28. num_online_cpus(), nd_region->num_lanes,
  29. num_possible_cpus());
  30. dev_info(dev, "setting nr_cpus=%d may yield better libnvdimm device performance\n",
  31. nd_region->num_lanes);
  32. }
  33. rc = nd_region_activate(nd_region);
  34. if (rc)
  35. return rc;
  36. rc = nd_blk_region_init(nd_region);
  37. if (rc)
  38. return rc;
  39. rc = nd_region_register_namespaces(nd_region, &err);
  40. if (rc < 0)
  41. return rc;
  42. ndrd = dev_get_drvdata(dev);
  43. ndrd->ns_active = rc;
  44. ndrd->ns_count = rc + err;
  45. if (rc && err && rc == err)
  46. return -ENODEV;
  47. nd_region->btt_seed = nd_btt_create(nd_region);
  48. nd_region->pfn_seed = nd_pfn_create(nd_region);
  49. nd_region->dax_seed = nd_dax_create(nd_region);
  50. if (err == 0)
  51. return 0;
  52. /*
  53. * Given multiple namespaces per region, we do not want to
  54. * disable all the successfully registered peer namespaces upon
  55. * a single registration failure. If userspace is missing a
  56. * namespace that it expects it can disable/re-enable the region
  57. * to retry discovery after correcting the failure.
  58. * <regionX>/namespaces returns the current
  59. * "<async-registered>/<total>" namespace count.
  60. */
  61. dev_err(dev, "failed to register %d namespace%s, continuing...\n",
  62. err, err == 1 ? "" : "s");
  63. return 0;
  64. }
  65. static int child_unregister(struct device *dev, void *data)
  66. {
  67. nd_device_unregister(dev, ND_SYNC);
  68. return 0;
  69. }
  70. static int nd_region_remove(struct device *dev)
  71. {
  72. struct nd_region *nd_region = to_nd_region(dev);
  73. device_for_each_child(dev, NULL, child_unregister);
  74. /* flush attribute readers and disable */
  75. nvdimm_bus_lock(dev);
  76. nd_region->ns_seed = NULL;
  77. nd_region->btt_seed = NULL;
  78. nd_region->pfn_seed = NULL;
  79. nd_region->dax_seed = NULL;
  80. dev_set_drvdata(dev, NULL);
  81. nvdimm_bus_unlock(dev);
  82. return 0;
  83. }
  84. static int child_notify(struct device *dev, void *data)
  85. {
  86. nd_device_notify(dev, *(enum nvdimm_event *) data);
  87. return 0;
  88. }
  89. static void nd_region_notify(struct device *dev, enum nvdimm_event event)
  90. {
  91. device_for_each_child(dev, &event, child_notify);
  92. }
  93. static struct nd_device_driver nd_region_driver = {
  94. .probe = nd_region_probe,
  95. .remove = nd_region_remove,
  96. .notify = nd_region_notify,
  97. .drv = {
  98. .name = "nd_region",
  99. },
  100. .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
  101. };
  102. int __init nd_region_init(void)
  103. {
  104. return nd_driver_register(&nd_region_driver);
  105. }
  106. void nd_region_exit(void)
  107. {
  108. driver_unregister(&nd_region_driver.drv);
  109. }
  110. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
  111. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);