region.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_namespaces *num_ns;
  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_blk_region_init(nd_region);
  34. if (rc)
  35. return rc;
  36. rc = nd_region_register_namespaces(nd_region, &err);
  37. num_ns = devm_kzalloc(dev, sizeof(*num_ns), GFP_KERNEL);
  38. if (!num_ns)
  39. return -ENOMEM;
  40. if (rc < 0)
  41. return rc;
  42. num_ns->active = rc;
  43. num_ns->count = rc + err;
  44. dev_set_drvdata(dev, num_ns);
  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. if (err == 0)
  50. return 0;
  51. /*
  52. * Given multiple namespaces per region, we do not want to
  53. * disable all the successfully registered peer namespaces upon
  54. * a single registration failure. If userspace is missing a
  55. * namespace that it expects it can disable/re-enable the region
  56. * to retry discovery after correcting the failure.
  57. * <regionX>/namespaces returns the current
  58. * "<async-registered>/<total>" namespace count.
  59. */
  60. dev_err(dev, "failed to register %d namespace%s, continuing...\n",
  61. err, err == 1 ? "" : "s");
  62. return 0;
  63. }
  64. static int child_unregister(struct device *dev, void *data)
  65. {
  66. nd_device_unregister(dev, ND_SYNC);
  67. return 0;
  68. }
  69. static int nd_region_remove(struct device *dev)
  70. {
  71. struct nd_region *nd_region = to_nd_region(dev);
  72. /* flush attribute readers and disable */
  73. nvdimm_bus_lock(dev);
  74. nd_region->ns_seed = NULL;
  75. nd_region->btt_seed = NULL;
  76. nd_region->pfn_seed = NULL;
  77. dev_set_drvdata(dev, NULL);
  78. nvdimm_bus_unlock(dev);
  79. device_for_each_child(dev, NULL, child_unregister);
  80. return 0;
  81. }
  82. static struct nd_device_driver nd_region_driver = {
  83. .probe = nd_region_probe,
  84. .remove = nd_region_remove,
  85. .drv = {
  86. .name = "nd_region",
  87. },
  88. .type = ND_DRIVER_REGION_BLK | ND_DRIVER_REGION_PMEM,
  89. };
  90. int __init nd_region_init(void)
  91. {
  92. return nd_driver_register(&nd_region_driver);
  93. }
  94. void nd_region_exit(void)
  95. {
  96. driver_unregister(&nd_region_driver.drv);
  97. }
  98. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_PMEM);
  99. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_REGION_BLK);