1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. All rights reserved. */
3 #include <linux/libnvdimm.h>
4 #include <linux/ndctl.h>
5 #include <linux/acpi.h>
10 static enum nvdimm_security_state intel_security_state(struct nvdimm *nvdimm,
11 enum nvdimm_passphrase_type ptype)
13 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
15 struct nd_cmd_pkg pkg;
16 struct nd_intel_get_security_state cmd;
19 .nd_command = NVDIMM_INTEL_GET_SECURITY_STATE,
20 .nd_family = NVDIMM_FAMILY_INTEL,
22 sizeof(struct nd_intel_get_security_state),
24 sizeof(struct nd_intel_get_security_state),
29 if (!test_bit(NVDIMM_INTEL_GET_SECURITY_STATE, &nfit_mem->dsm_mask))
33 * Short circuit the state retrieval while we are doing overwrite.
34 * The DSM spec states that the security state is indeterminate
35 * until the overwrite DSM completes.
37 if (nvdimm_in_overwrite(nvdimm) && ptype == NVDIMM_USER)
38 return NVDIMM_SECURITY_OVERWRITE;
40 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
43 if (nd_cmd.cmd.status)
46 /* check and see if security is enabled and locked */
47 if (ptype == NVDIMM_MASTER) {
48 if (nd_cmd.cmd.extended_state & ND_INTEL_SEC_ESTATE_ENABLED)
49 return NVDIMM_SECURITY_UNLOCKED;
50 else if (nd_cmd.cmd.extended_state &
51 ND_INTEL_SEC_ESTATE_PLIMIT)
52 return NVDIMM_SECURITY_FROZEN;
54 if (nd_cmd.cmd.state & ND_INTEL_SEC_STATE_UNSUPPORTED)
56 else if (nd_cmd.cmd.state & ND_INTEL_SEC_STATE_ENABLED) {
57 if (nd_cmd.cmd.state & ND_INTEL_SEC_STATE_LOCKED)
58 return NVDIMM_SECURITY_LOCKED;
59 else if (nd_cmd.cmd.state & ND_INTEL_SEC_STATE_FROZEN
61 ND_INTEL_SEC_STATE_PLIMIT)
62 return NVDIMM_SECURITY_FROZEN;
64 return NVDIMM_SECURITY_UNLOCKED;
68 /* this should cover master security disabled as well */
69 return NVDIMM_SECURITY_DISABLED;
72 static int intel_security_freeze(struct nvdimm *nvdimm)
74 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
76 struct nd_cmd_pkg pkg;
77 struct nd_intel_freeze_lock cmd;
80 .nd_command = NVDIMM_INTEL_FREEZE_LOCK,
81 .nd_family = NVDIMM_FAMILY_INTEL,
82 .nd_size_out = ND_INTEL_STATUS_SIZE,
83 .nd_fw_size = ND_INTEL_STATUS_SIZE,
88 if (!test_bit(NVDIMM_INTEL_FREEZE_LOCK, &nfit_mem->dsm_mask))
91 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
94 if (nd_cmd.cmd.status)
99 static int intel_security_change_key(struct nvdimm *nvdimm,
100 const struct nvdimm_key_data *old_data,
101 const struct nvdimm_key_data *new_data,
102 enum nvdimm_passphrase_type ptype)
104 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
105 unsigned int cmd = ptype == NVDIMM_MASTER ?
106 NVDIMM_INTEL_SET_MASTER_PASSPHRASE :
107 NVDIMM_INTEL_SET_PASSPHRASE;
109 struct nd_cmd_pkg pkg;
110 struct nd_intel_set_passphrase cmd;
113 .nd_family = NVDIMM_FAMILY_INTEL,
114 .nd_size_in = ND_INTEL_PASSPHRASE_SIZE * 2,
115 .nd_size_out = ND_INTEL_STATUS_SIZE,
116 .nd_fw_size = ND_INTEL_STATUS_SIZE,
122 if (!test_bit(cmd, &nfit_mem->dsm_mask))
125 memcpy(nd_cmd.cmd.old_pass, old_data->data,
126 sizeof(nd_cmd.cmd.old_pass));
127 memcpy(nd_cmd.cmd.new_pass, new_data->data,
128 sizeof(nd_cmd.cmd.new_pass));
129 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
133 switch (nd_cmd.cmd.status) {
136 case ND_INTEL_STATUS_INVALID_PASS:
138 case ND_INTEL_STATUS_NOT_SUPPORTED:
140 case ND_INTEL_STATUS_INVALID_STATE:
146 static void nvdimm_invalidate_cache(void);
148 static int __maybe_unused intel_security_unlock(struct nvdimm *nvdimm,
149 const struct nvdimm_key_data *key_data)
151 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
153 struct nd_cmd_pkg pkg;
154 struct nd_intel_unlock_unit cmd;
157 .nd_command = NVDIMM_INTEL_UNLOCK_UNIT,
158 .nd_family = NVDIMM_FAMILY_INTEL,
159 .nd_size_in = ND_INTEL_PASSPHRASE_SIZE,
160 .nd_size_out = ND_INTEL_STATUS_SIZE,
161 .nd_fw_size = ND_INTEL_STATUS_SIZE,
166 if (!test_bit(NVDIMM_INTEL_UNLOCK_UNIT, &nfit_mem->dsm_mask))
169 memcpy(nd_cmd.cmd.passphrase, key_data->data,
170 sizeof(nd_cmd.cmd.passphrase));
171 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
174 switch (nd_cmd.cmd.status) {
177 case ND_INTEL_STATUS_INVALID_PASS:
183 /* DIMM unlocked, invalidate all CPU caches before we read it */
184 nvdimm_invalidate_cache();
189 static int intel_security_disable(struct nvdimm *nvdimm,
190 const struct nvdimm_key_data *key_data)
193 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
195 struct nd_cmd_pkg pkg;
196 struct nd_intel_disable_passphrase cmd;
199 .nd_command = NVDIMM_INTEL_DISABLE_PASSPHRASE,
200 .nd_family = NVDIMM_FAMILY_INTEL,
201 .nd_size_in = ND_INTEL_PASSPHRASE_SIZE,
202 .nd_size_out = ND_INTEL_STATUS_SIZE,
203 .nd_fw_size = ND_INTEL_STATUS_SIZE,
207 if (!test_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE, &nfit_mem->dsm_mask))
210 memcpy(nd_cmd.cmd.passphrase, key_data->data,
211 sizeof(nd_cmd.cmd.passphrase));
212 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
216 switch (nd_cmd.cmd.status) {
219 case ND_INTEL_STATUS_INVALID_PASS:
221 case ND_INTEL_STATUS_INVALID_STATE:
229 static int __maybe_unused intel_security_erase(struct nvdimm *nvdimm,
230 const struct nvdimm_key_data *key,
231 enum nvdimm_passphrase_type ptype)
234 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
235 unsigned int cmd = ptype == NVDIMM_MASTER ?
236 NVDIMM_INTEL_MASTER_SECURE_ERASE : NVDIMM_INTEL_SECURE_ERASE;
238 struct nd_cmd_pkg pkg;
239 struct nd_intel_secure_erase cmd;
242 .nd_family = NVDIMM_FAMILY_INTEL,
243 .nd_size_in = ND_INTEL_PASSPHRASE_SIZE,
244 .nd_size_out = ND_INTEL_STATUS_SIZE,
245 .nd_fw_size = ND_INTEL_STATUS_SIZE,
250 if (!test_bit(cmd, &nfit_mem->dsm_mask))
253 /* flush all cache before we erase DIMM */
254 nvdimm_invalidate_cache();
255 memcpy(nd_cmd.cmd.passphrase, key->data,
256 sizeof(nd_cmd.cmd.passphrase));
257 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
261 switch (nd_cmd.cmd.status) {
264 case ND_INTEL_STATUS_NOT_SUPPORTED:
266 case ND_INTEL_STATUS_INVALID_PASS:
268 case ND_INTEL_STATUS_INVALID_STATE:
273 /* DIMM erased, invalidate all CPU caches before we read it */
274 nvdimm_invalidate_cache();
278 static int __maybe_unused intel_security_query_overwrite(struct nvdimm *nvdimm)
281 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
283 struct nd_cmd_pkg pkg;
284 struct nd_intel_query_overwrite cmd;
287 .nd_command = NVDIMM_INTEL_QUERY_OVERWRITE,
288 .nd_family = NVDIMM_FAMILY_INTEL,
289 .nd_size_out = ND_INTEL_STATUS_SIZE,
290 .nd_fw_size = ND_INTEL_STATUS_SIZE,
294 if (!test_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &nfit_mem->dsm_mask))
297 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
301 switch (nd_cmd.cmd.status) {
304 case ND_INTEL_STATUS_OQUERY_INPROGRESS:
310 /* flush all cache before we make the nvdimms available */
311 nvdimm_invalidate_cache();
315 static int __maybe_unused intel_security_overwrite(struct nvdimm *nvdimm,
316 const struct nvdimm_key_data *nkey)
319 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
321 struct nd_cmd_pkg pkg;
322 struct nd_intel_overwrite cmd;
325 .nd_command = NVDIMM_INTEL_OVERWRITE,
326 .nd_family = NVDIMM_FAMILY_INTEL,
327 .nd_size_in = ND_INTEL_PASSPHRASE_SIZE,
328 .nd_size_out = ND_INTEL_STATUS_SIZE,
329 .nd_fw_size = ND_INTEL_STATUS_SIZE,
333 if (!test_bit(NVDIMM_INTEL_OVERWRITE, &nfit_mem->dsm_mask))
336 /* flush all cache before we erase DIMM */
337 nvdimm_invalidate_cache();
338 memcpy(nd_cmd.cmd.passphrase, nkey->data,
339 sizeof(nd_cmd.cmd.passphrase));
340 rc = nvdimm_ctl(nvdimm, ND_CMD_CALL, &nd_cmd, sizeof(nd_cmd), NULL);
344 switch (nd_cmd.cmd.status) {
347 case ND_INTEL_STATUS_OVERWRITE_UNSUPPORTED:
349 case ND_INTEL_STATUS_INVALID_PASS:
351 case ND_INTEL_STATUS_INVALID_STATE:
358 * TODO: define a cross arch wbinvd equivalent when/if
359 * NVDIMM_FAMILY_INTEL command support arrives on another arch.
362 static void nvdimm_invalidate_cache(void)
364 wbinvd_on_all_cpus();
367 static void nvdimm_invalidate_cache(void)
369 WARN_ON_ONCE("cache invalidation required after unlock\n");
373 static const struct nvdimm_security_ops __intel_security_ops = {
374 .state = intel_security_state,
375 .freeze = intel_security_freeze,
376 .change_key = intel_security_change_key,
377 .disable = intel_security_disable,
379 .unlock = intel_security_unlock,
380 .erase = intel_security_erase,
381 .overwrite = intel_security_overwrite,
382 .query_overwrite = intel_security_query_overwrite,
386 const struct nvdimm_security_ops *intel_security_ops = &__intel_security_ops;