Merge 4.18-rc7 into staging-next
[linux-2.6/btrfs-unstable.git] / drivers / nvdimm / dimm.c
blob233907889f96a36d6a3b87fc79cf82cabf602def
1 /*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
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"
24 static int nvdimm_probe(struct device *dev)
26 struct nvdimm_drvdata *ndd;
27 int rc;
29 rc = nvdimm_check_config_data(dev);
30 if (rc) {
31 /* not required for non-aliased nvdimm, ex. NVDIMM-N */
32 if (rc == -ENOTTY)
33 rc = 0;
34 return rc;
37 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
38 if (!ndd)
39 return -ENOMEM;
41 dev_set_drvdata(dev, ndd);
42 ndd->dpa.name = dev_name(dev);
43 ndd->ns_current = -1;
44 ndd->ns_next = -1;
45 ndd->dpa.start = 0;
46 ndd->dpa.end = -1;
47 ndd->dev = dev;
48 get_device(dev);
49 kref_init(&ndd->kref);
51 rc = nvdimm_init_nsarea(ndd);
52 if (rc == -EACCES)
53 nvdimm_set_locked(dev);
54 if (rc)
55 goto err;
57 rc = nvdimm_init_config_data(ndd);
58 if (rc == -EACCES)
59 nvdimm_set_locked(dev);
60 if (rc)
61 goto err;
63 dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
65 nvdimm_bus_lock(dev);
66 ndd->ns_current = nd_label_validate(ndd);
67 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
68 nd_label_copy(ndd, to_next_namespace_index(ndd),
69 to_current_namespace_index(ndd));
70 if (ndd->ns_current >= 0) {
71 rc = nd_label_reserve_dpa(ndd);
72 if (rc == 0)
73 nvdimm_set_aliasing(dev);
75 nvdimm_clear_locked(dev);
76 nvdimm_bus_unlock(dev);
78 if (rc)
79 goto err;
81 return 0;
83 err:
84 put_ndd(ndd);
85 return rc;
88 static int nvdimm_remove(struct device *dev)
90 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
92 if (!ndd)
93 return 0;
95 nvdimm_bus_lock(dev);
96 dev_set_drvdata(dev, NULL);
97 nvdimm_bus_unlock(dev);
98 put_ndd(ndd);
100 return 0;
103 static struct nd_device_driver nvdimm_driver = {
104 .probe = nvdimm_probe,
105 .remove = nvdimm_remove,
106 .drv = {
107 .name = "nvdimm",
109 .type = ND_DRIVER_DIMM,
112 int __init nvdimm_init(void)
114 return nd_driver_register(&nvdimm_driver);
117 void nvdimm_exit(void)
119 driver_unregister(&nvdimm_driver.drv);
122 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);