dimm.c 2.1 KB

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