]>
Commit | Line | Data |
---|---|---|
4d88a97a DW |
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> | |
4a826c83 | 21 | #include "label.h" |
4d88a97a DW |
22 | #include "nd.h" |
23 | ||
4d88a97a DW |
24 | static int nvdimm_probe(struct device *dev) |
25 | { | |
26 | struct nvdimm_drvdata *ndd; | |
27 | int rc; | |
28 | ||
aee65987 TK |
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; | |
35 | } | |
36 | ||
4c6926a2 DJ |
37 | /* |
38 | * The locked status bit reflects explicit status codes from the | |
39 | * label reading commands, revalidate it each time the driver is | |
40 | * activated and re-reads the label area. | |
41 | */ | |
08e6b3c6 DW |
42 | nvdimm_clear_locked(dev); |
43 | ||
4d88a97a DW |
44 | ndd = kzalloc(sizeof(*ndd), GFP_KERNEL); |
45 | if (!ndd) | |
46 | return -ENOMEM; | |
47 | ||
48 | dev_set_drvdata(dev, ndd); | |
4a826c83 DW |
49 | ndd->dpa.name = dev_name(dev); |
50 | ndd->ns_current = -1; | |
51 | ndd->ns_next = -1; | |
52 | ndd->dpa.start = 0; | |
53 | ndd->dpa.end = -1; | |
4d88a97a | 54 | ndd->dev = dev; |
bf9bccc1 DW |
55 | get_device(dev); |
56 | kref_init(&ndd->kref); | |
4d88a97a | 57 | |
4c6926a2 DJ |
58 | /* |
59 | * Attempt to unlock, if the DIMM supports security commands, | |
60 | * otherwise the locked indication is determined by explicit | |
61 | * status codes from the label reading commands. | |
62 | */ | |
63 | rc = nvdimm_security_unlock(dev); | |
64 | if (rc < 0) | |
37379cfc | 65 | dev_dbg(dev, "failed to unlock dimm: %d\n", rc); |
4c6926a2 DJ |
66 | |
67 | ||
08e6b3c6 DW |
68 | /* |
69 | * EACCES failures reading the namespace label-area-properties | |
70 | * are interpreted as the DIMM capacity being locked but the | |
71 | * namespace labels themselves being accessible. | |
72 | */ | |
4d88a97a | 73 | rc = nvdimm_init_nsarea(ndd); |
08e6b3c6 DW |
74 | if (rc == -EACCES) { |
75 | /* | |
76 | * See nvdimm_namespace_common_probe() where we fail to | |
77 | * allow namespaces to probe while the DIMM is locked, | |
78 | * but we do allow for namespace enumeration. | |
79 | */ | |
9d62ed96 | 80 | nvdimm_set_locked(dev); |
08e6b3c6 DW |
81 | rc = 0; |
82 | } | |
4d88a97a DW |
83 | if (rc) |
84 | goto err; | |
85 | ||
08e6b3c6 DW |
86 | /* |
87 | * EACCES failures reading the namespace label-data are | |
88 | * interpreted as the label area being locked in addition to the | |
89 | * DIMM capacity. We fail the dimm probe to prevent regions from | |
90 | * attempting to parse the label area. | |
91 | */ | |
2d657d17 | 92 | rc = nd_label_data_init(ndd); |
4b27db7e DW |
93 | if (rc == -EACCES) |
94 | nvdimm_set_locked(dev); | |
4d88a97a DW |
95 | if (rc) |
96 | goto err; | |
97 | ||
98 | dev_dbg(dev, "config data size: %d\n", ndd->nsarea.config_size); | |
99 | ||
4a826c83 | 100 | nvdimm_bus_lock(dev); |
c31898c8 DW |
101 | if (ndd->ns_current >= 0) { |
102 | rc = nd_label_reserve_dpa(ndd); | |
103 | if (rc == 0) | |
104 | nvdimm_set_aliasing(dev); | |
105 | } | |
4a826c83 DW |
106 | nvdimm_bus_unlock(dev); |
107 | ||
108 | if (rc) | |
109 | goto err; | |
110 | ||
4d88a97a DW |
111 | return 0; |
112 | ||
113 | err: | |
bf9bccc1 | 114 | put_ndd(ndd); |
4d88a97a DW |
115 | return rc; |
116 | } | |
117 | ||
118 | static int nvdimm_remove(struct device *dev) | |
119 | { | |
120 | struct nvdimm_drvdata *ndd = dev_get_drvdata(dev); | |
121 | ||
aee65987 TK |
122 | if (!ndd) |
123 | return 0; | |
124 | ||
4a826c83 DW |
125 | nvdimm_bus_lock(dev); |
126 | dev_set_drvdata(dev, NULL); | |
4a826c83 | 127 | nvdimm_bus_unlock(dev); |
bf9bccc1 | 128 | put_ndd(ndd); |
4d88a97a DW |
129 | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static struct nd_device_driver nvdimm_driver = { | |
134 | .probe = nvdimm_probe, | |
135 | .remove = nvdimm_remove, | |
136 | .drv = { | |
137 | .name = "nvdimm", | |
138 | }, | |
139 | .type = ND_DRIVER_DIMM, | |
140 | }; | |
141 | ||
142 | int __init nvdimm_init(void) | |
143 | { | |
144 | return nd_driver_register(&nvdimm_driver); | |
145 | } | |
146 | ||
3d88002e | 147 | void nvdimm_exit(void) |
4d88a97a DW |
148 | { |
149 | driver_unregister(&nvdimm_driver.drv); | |
150 | } | |
151 | ||
152 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DIMM); |