dimm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
  35. if (!ndd)
  36. return -ENOMEM;
  37. dev_set_drvdata(dev, ndd);
  38. ndd->dpa.name = dev_name(dev);
  39. ndd->ns_current = -1;
  40. ndd->ns_next = -1;
  41. ndd->dpa.start = 0;
  42. ndd->dpa.end = -1;
  43. ndd->dev = dev;
  44. get_device(dev);
  45. kref_init(&ndd->kref);
  46. rc = nvdimm_init_nsarea(ndd);
  47. if (rc == -EACCES)
  48. nvdimm_set_locked(dev);
  49. if (rc)
  50. goto err;
  51. rc = nvdimm_init_config_data(ndd);
  52. if (rc == -EACCES)
  53. nvdimm_set_locked(dev);
  54. if (rc)
  55. goto err;
  56. dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
  57. nvdimm_bus_lock(dev);
  58. ndd->ns_current = nd_label_validate(ndd);
  59. ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
  60. nd_label_copy(ndd, to_next_namespace_index(ndd),
  61. to_current_namespace_index(ndd));
  62. if (ndd->ns_current >= 0) {
  63. rc = nd_label_reserve_dpa(ndd);
  64. if (rc == 0)
  65. nvdimm_set_aliasing(dev);
  66. }
  67. nvdimm_clear_locked(dev);
  68. nvdimm_bus_unlock(dev);
  69. if (rc)
  70. goto err;
  71. return 0;
  72. err:
  73. put_ndd(ndd);
  74. return rc;
  75. }
  76. static int nvdimm_remove(struct device *dev)
  77. {
  78. struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
  79. if (!ndd)
  80. return 0;
  81. nvdimm_bus_lock(dev);
  82. dev_set_drvdata(dev, NULL);
  83. nvdimm_bus_unlock(dev);
  84. put_ndd(ndd);
  85. return 0;
  86. }
  87. static struct nd_device_driver nvdimm_driver = {
  88. .probe = nvdimm_probe,
  89. .remove = nvdimm_remove,
  90. .drv = {
  91. .name = "nvdimm",
  92. },
  93. .type = ND_DRIVER_DIMM,
  94. };
  95. int __init nvdimm_init(void)
  96. {
  97. return nd_driver_register(&nvdimm_driver);
  98. }
  99. void nvdimm_exit(void)
  100. {
  101. driver_unregister(&nvdimm_driver.drv);
  102. }
  103. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);