libnvdimm: namespace indices: read and validate
[deliverable/linux.git] / drivers / nvdimm / dimm.c
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
24 static void free_data(struct nvdimm_drvdata *ndd)
25 {
26 if (!ndd)
27 return;
28
29 if (ndd->data && is_vmalloc_addr(ndd->data))
30 vfree(ndd->data);
31 else
32 kfree(ndd->data);
33 kfree(ndd);
34 }
35
36 static int nvdimm_probe(struct device *dev)
37 {
38 struct nvdimm_drvdata *ndd;
39 int rc;
40
41 ndd = kzalloc(sizeof(*ndd), GFP_KERNEL);
42 if (!ndd)
43 return -ENOMEM;
44
45 dev_set_drvdata(dev, ndd);
46 ndd->dpa.name = dev_name(dev);
47 ndd->ns_current = -1;
48 ndd->ns_next = -1;
49 ndd->dpa.start = 0;
50 ndd->dpa.end = -1;
51 ndd->dev = dev;
52
53 rc = nvdimm_init_nsarea(ndd);
54 if (rc)
55 goto err;
56
57 rc = nvdimm_init_config_data(ndd);
58 if (rc)
59 goto err;
60
61 dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size);
62
63 nvdimm_bus_lock(dev);
64 ndd->ns_current = nd_label_validate(ndd);
65 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
66 nd_label_copy(ndd, to_next_namespace_index(ndd),
67 to_current_namespace_index(ndd));
68 rc = nd_label_reserve_dpa(ndd);
69 nvdimm_bus_unlock(dev);
70
71 if (rc)
72 goto err;
73
74 return 0;
75
76 err:
77 free_data(ndd);
78 return rc;
79 }
80
81 static int nvdimm_remove(struct device *dev)
82 {
83 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
84 struct resource *res, *_r;
85
86 nvdimm_bus_lock(dev);
87 dev_set_drvdata(dev, NULL);
88 for_each_dpa_resource_safe(ndd, res, _r)
89 nvdimm_free_dpa(ndd, res);
90 nvdimm_bus_unlock(dev);
91 free_data(ndd);
92
93 return 0;
94 }
95
96 static struct nd_device_driver nvdimm_driver = {
97 .probe = nvdimm_probe,
98 .remove = nvdimm_remove,
99 .drv = {
100 .name = "nvdimm",
101 },
102 .type = ND_DRIVER_DIMM,
103 };
104
105 int __init nvdimm_init(void)
106 {
107 return nd_driver_register(&nvdimm_driver);
108 }
109
110 void nvdimm_exit(void)
111 {
112 driver_unregister(&nvdimm_driver.drv);
113 }
114
115 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM);
This page took 0.039151 seconds and 6 git commands to generate.