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