dimm.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/vmalloc.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/sizes.h>
  17. #include <linux/ndctl.h>
  18. #include <linux/slab.h>
  19. #include <linux/mm.h>
  20. #include <linux/nd.h>
  21. #include "label.h"
  22. #include "nd.h"
  23. static int nvdimm_probe(struct device *dev)
  24. {
  25. struct nvdimm_drvdata *ndd;
  26. int rc;
  27. rc = nvdimm_check_config_data(dev);
  28. if (rc) {
  29. /* not required for non-aliased nvdimm, ex. NVDIMM-N */
  30. if (rc == -ENOTTY)
  31. rc = 0;
  32. return rc;
  33. }
  34. /* reset locked, to be validated below... */
  35. nvdimm_clear_locked(dev);
  36. ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
  37. if (!ndd)
  38. return -ENOMEM;
  39. dev_set_drvdata(dev, ndd);
  40. ndd->dpa.name = dev_name(dev);
  41. ndd->ns_current = -1;
  42. ndd->ns_next = -1;
  43. ndd->dpa.start = 0;
  44. ndd->dpa.end = -1;
  45. ndd->dev = dev;
  46. get_device(dev);
  47. kref_init(&ndd->kref);
  48. /*
  49. * EACCES failures reading the namespace label-area-properties
  50. * are interpreted as the DIMM capacity being locked but the
  51. * namespace labels themselves being accessible.
  52. */
  53. rc = nvdimm_init_nsarea(ndd);
  54. if (rc == -EACCES) {
  55. /*
  56. * See nvdimm_namespace_common_probe() where we fail to
  57. * allow namespaces to probe while the DIMM is locked,
  58. * but we do allow for namespace enumeration.
  59. */
  60. nvdimm_set_locked(dev);
  61. rc = 0;
  62. }
  63. if (rc)
  64. goto err;
  65. /*
  66. * EACCES failures reading the namespace label-data are
  67. * interpreted as the label area being locked in addition to the
  68. * DIMM capacity. We fail the dimm probe to prevent regions from
  69. * attempting to parse the label area.
  70. */
  71. rc = nvdimm_init_config_data(ndd);
  72. if (rc == -EACCES)
  73. nvdimm_set_locked(dev);
  74. if (rc)
  75. goto err;
  76. dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
  77. nvdimm_bus_lock(dev);
  78. ndd->ns_current = nd_label_validate(ndd);
  79. ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
  80. nd_label_copy(ndd, to_next_namespace_index(ndd),
  81. to_current_namespace_index(ndd));
  82. if (ndd->ns_current >= 0) {
  83. rc = nd_label_reserve_dpa(ndd);
  84. if (rc == 0)
  85. nvdimm_set_aliasing(dev);
  86. }
  87. nvdimm_bus_unlock(dev);
  88. if (rc)
  89. goto err;
  90. return 0;
  91. err:
  92. put_ndd(ndd);
  93. return rc;
  94. }
  95. static int nvdimm_remove(struct device *dev)
  96. {
  97. struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
  98. if (!ndd)
  99. return 0;
  100. nvdimm_bus_lock(dev);
  101. dev_set_drvdata(dev, NULL);
  102. nvdimm_bus_unlock(dev);
  103. put_ndd(ndd);
  104. return 0;
  105. }
  106. static struct nd_device_driver nvdimm_driver = {
  107. .probe = nvdimm_probe,
  108. .remove = nvdimm_remove,
  109. .drv = {
  110. .name = "nvdimm",
  111. },
  112. .type = ND_DRIVER_DIMM,
  113. };
  114. int __init nvdimm_init(void)
  115. {
  116. return nd_driver_register(&nvdimm_driver);
  117. }
  118. void nvdimm_exit(void)
  119. {
  120. driver_unregister(&nvdimm_driver.drv);
  121. }
  122. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);