blob: 278867c68682f70b5a051cbe65e6752d72c782f7 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williamse6dfb2d2015-04-25 03:56:17 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williamse6dfb2d2015-04-25 03:56:17 -04004 */
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Dan Williamsd5d30d52019-02-02 16:35:26 -08006#include <linux/moduleparam.h>
Dan Williams4d88a972015-05-31 14:41:48 -04007#include <linux/vmalloc.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -04008#include <linux/device.h>
Dan Williams62232e452015-06-08 14:27:06 -04009#include <linux/ndctl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040010#include <linux/slab.h>
11#include <linux/io.h>
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include "nd-core.h"
Dan Williams0ba1c632015-05-30 12:35:36 -040015#include "label.h"
Dan Williamsca6a4652017-01-13 20:36:58 -080016#include "pmem.h"
Dan Williams4d88a972015-05-31 14:41:48 -040017#include "nd.h"
Dan Williamse6dfb2d2015-04-25 03:56:17 -040018
19static DEFINE_IDA(dimm_ida);
20
Dan Williamsd5d30d52019-02-02 16:35:26 -080021static bool noblk;
22module_param(noblk, bool, 0444);
23MODULE_PARM_DESC(noblk, "force disable BLK / local alias support");
24
Dan Williams4d88a972015-05-31 14:41:48 -040025/*
26 * Retrieve bus and dimm handle and return if this bus supports
27 * get_config_data commands
28 */
Toshi Kaniaee65982016-08-16 13:08:40 -060029int nvdimm_check_config_data(struct device *dev)
Dan Williams4d88a972015-05-31 14:41:48 -040030{
Toshi Kaniaee65982016-08-16 13:08:40 -060031 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williams4d88a972015-05-31 14:41:48 -040032
Toshi Kaniaee65982016-08-16 13:08:40 -060033 if (!nvdimm->cmd_mask ||
34 !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) {
Dan Williams8f078b32017-05-04 14:01:24 -070035 if (test_bit(NDD_ALIASING, &nvdimm->flags))
Toshi Kaniaee65982016-08-16 13:08:40 -060036 return -ENXIO;
37 else
38 return -ENOTTY;
39 }
Dan Williams4d88a972015-05-31 14:41:48 -040040
41 return 0;
42}
43
44static int validate_dimm(struct nvdimm_drvdata *ndd)
45{
Toshi Kaniaee65982016-08-16 13:08:40 -060046 int rc;
Dan Williams4d88a972015-05-31 14:41:48 -040047
Toshi Kaniaee65982016-08-16 13:08:40 -060048 if (!ndd)
49 return -EINVAL;
50
51 rc = nvdimm_check_config_data(ndd->dev);
52 if (rc)
Sakari Ailusd75f7732019-03-25 21:32:28 +020053 dev_dbg(ndd->dev, "%ps: %s error: %d\n",
Dan Williams4d88a972015-05-31 14:41:48 -040054 __builtin_return_address(0), __func__, rc);
55 return rc;
56}
57
58/**
59 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
60 * @nvdimm: dimm to initialize
61 */
62int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
63{
64 struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
65 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
66 struct nvdimm_bus_descriptor *nd_desc;
67 int rc = validate_dimm(ndd);
Dan Williams9d62ed92017-05-04 11:47:22 -070068 int cmd_rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -040069
70 if (rc)
71 return rc;
72
73 if (cmd->config_size)
74 return 0; /* already valid */
75
76 memset(cmd, 0, sizeof(*cmd));
77 nd_desc = nvdimm_bus->nd_desc;
Dan Williams9d62ed92017-05-04 11:47:22 -070078 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
79 ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd), &cmd_rc);
80 if (rc < 0)
81 return rc;
82 return cmd_rc;
Dan Williams4d88a972015-05-31 14:41:48 -040083}
84
Alexander Duyck2d657d12018-10-10 16:39:20 -070085int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
86 size_t offset, size_t len)
Dan Williams4d88a972015-05-31 14:41:48 -040087{
88 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
Alexander Duyck2d657d12018-10-10 16:39:20 -070089 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
Dan Williamse7c5a572018-04-09 12:34:24 -070090 int rc = validate_dimm(ndd), cmd_rc = 0;
Dan Williams4d88a972015-05-31 14:41:48 -040091 struct nd_cmd_get_config_data_hdr *cmd;
Alexander Duyck2d657d12018-10-10 16:39:20 -070092 size_t max_cmd_size, buf_offset;
Dan Williams4d88a972015-05-31 14:41:48 -040093
94 if (rc)
95 return rc;
96
Alexander Duyck2d657d12018-10-10 16:39:20 -070097 if (offset + len > ndd->nsarea.config_size)
Dan Williams4d88a972015-05-31 14:41:48 -040098 return -ENXIO;
99
Alexander Duyck2d657d12018-10-10 16:39:20 -0700100 max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
Dan Williamsd11cf4a2018-10-10 16:38:24 -0700101 cmd = kvzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
Dan Williams4d88a972015-05-31 14:41:48 -0400102 if (!cmd)
103 return -ENOMEM;
104
Alexander Duyck2d657d12018-10-10 16:39:20 -0700105 for (buf_offset = 0; len;
106 len -= cmd->in_length, buf_offset += cmd->in_length) {
107 size_t cmd_size;
108
109 cmd->in_offset = offset + buf_offset;
110 cmd->in_length = min(max_cmd_size, len);
111
112 cmd_size = sizeof(*cmd) + cmd->in_length;
113
Dan Williams4d88a972015-05-31 14:41:48 -0400114 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
Alexander Duyck2d657d12018-10-10 16:39:20 -0700115 ND_CMD_GET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
Dan Williamse7c5a572018-04-09 12:34:24 -0700116 if (rc < 0)
117 break;
118 if (cmd_rc < 0) {
119 rc = cmd_rc;
Dan Williams4d88a972015-05-31 14:41:48 -0400120 break;
121 }
Alexander Duyck2d657d12018-10-10 16:39:20 -0700122
123 /* out_buf should be valid, copy it into our output buffer */
124 memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length);
Dan Williams4d88a972015-05-31 14:41:48 -0400125 }
Dan Williamsd11cf4a2018-10-10 16:38:24 -0700126 kvfree(cmd);
Dan Williams4d88a972015-05-31 14:41:48 -0400127
128 return rc;
129}
130
Dan Williamsf524bf22015-05-30 12:36:02 -0400131int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
132 void *buf, size_t len)
133{
Dan Williamsf524bf22015-05-30 12:36:02 -0400134 size_t max_cmd_size, buf_offset;
135 struct nd_cmd_set_config_hdr *cmd;
Dan Williamse7c5a572018-04-09 12:34:24 -0700136 int rc = validate_dimm(ndd), cmd_rc = 0;
Dan Williamsf524bf22015-05-30 12:36:02 -0400137 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
138 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
139
140 if (rc)
141 return rc;
142
Dan Williamsf524bf22015-05-30 12:36:02 -0400143 if (offset + len > ndd->nsarea.config_size)
144 return -ENXIO;
145
Dan Williamsd11cf4a2018-10-10 16:38:24 -0700146 max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
147 cmd = kvzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
Dan Williamsf524bf22015-05-30 12:36:02 -0400148 if (!cmd)
149 return -ENOMEM;
150
151 for (buf_offset = 0; len; len -= cmd->in_length,
152 buf_offset += cmd->in_length) {
153 size_t cmd_size;
Dan Williamsf524bf22015-05-30 12:36:02 -0400154
155 cmd->in_offset = offset + buf_offset;
156 cmd->in_length = min(max_cmd_size, len);
157 memcpy(cmd->in_buf, buf + buf_offset, cmd->in_length);
158
159 /* status is output in the last 4-bytes of the command buffer */
160 cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
Dan Williamsf524bf22015-05-30 12:36:02 -0400161
162 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
Dan Williamse7c5a572018-04-09 12:34:24 -0700163 ND_CMD_SET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
164 if (rc < 0)
165 break;
166 if (cmd_rc < 0) {
167 rc = cmd_rc;
Dan Williamsf524bf22015-05-30 12:36:02 -0400168 break;
169 }
170 }
Dan Williamsd11cf4a2018-10-10 16:38:24 -0700171 kvfree(cmd);
Dan Williamsf524bf22015-05-30 12:36:02 -0400172
173 return rc;
174}
175
Dan Williams42237e32016-10-15 15:33:52 -0700176void nvdimm_set_aliasing(struct device *dev)
177{
178 struct nvdimm *nvdimm = to_nvdimm(dev);
179
Dan Williams8f078b32017-05-04 14:01:24 -0700180 set_bit(NDD_ALIASING, &nvdimm->flags);
181}
182
183void nvdimm_set_locked(struct device *dev)
184{
185 struct nvdimm *nvdimm = to_nvdimm(dev);
186
187 set_bit(NDD_LOCKED, &nvdimm->flags);
Dan Williams42237e32016-10-15 15:33:52 -0700188}
189
Dan Williamsd34cb802017-09-25 11:01:31 -0700190void nvdimm_clear_locked(struct device *dev)
191{
192 struct nvdimm *nvdimm = to_nvdimm(dev);
193
194 clear_bit(NDD_LOCKED, &nvdimm->flags);
195}
196
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400197static void nvdimm_release(struct device *dev)
198{
199 struct nvdimm *nvdimm = to_nvdimm(dev);
200
201 ida_simple_remove(&dimm_ida, nvdimm->id);
202 kfree(nvdimm);
203}
204
Dan Williamsadbb6822019-11-12 17:00:24 -0800205static const struct attribute_group *nvdimm_attribute_groups[] = {
206 &nd_device_attribute_group,
207 NULL,
208};
209
210static const struct device_type nvdimm_device_type = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400211 .name = "nvdimm",
212 .release = nvdimm_release,
Dan Williamsadbb6822019-11-12 17:00:24 -0800213 .groups = nvdimm_attribute_groups,
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400214};
215
Dan Williams62232e452015-06-08 14:27:06 -0400216bool is_nvdimm(struct device *dev)
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400217{
218 return dev->type == &nvdimm_device_type;
219}
220
221struct nvdimm *to_nvdimm(struct device *dev)
222{
223 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
224
225 WARN_ON(!is_nvdimm(dev));
226 return nvdimm;
227}
228EXPORT_SYMBOL_GPL(to_nvdimm);
229
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400230struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
231{
232 struct nd_region *nd_region = &ndbr->nd_region;
233 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
234
235 return nd_mapping->nvdimm;
236}
237EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
238
Dan Williamsca6a4652017-01-13 20:36:58 -0800239unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr)
240{
241 /* pmem mapping properties are private to libnvdimm */
242 return ARCH_MEMREMAP_PMEM;
243}
244EXPORT_SYMBOL_GPL(nd_blk_memremap_flags);
245
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400246struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
247{
248 struct nvdimm *nvdimm = nd_mapping->nvdimm;
249
250 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
251
252 return dev_get_drvdata(&nvdimm->dev);
253}
254EXPORT_SYMBOL(to_ndd);
255
256void nvdimm_drvdata_release(struct kref *kref)
257{
258 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
259 struct device *dev = ndd->dev;
260 struct resource *res, *_r;
261
Dan Williams426824d2018-03-05 16:39:31 -0800262 dev_dbg(dev, "trace\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400263 nvdimm_bus_lock(dev);
264 for_each_dpa_resource_safe(ndd, res, _r)
265 nvdimm_free_dpa(ndd, res);
266 nvdimm_bus_unlock(dev);
267
yalin wanga06a7572015-08-27 19:35:48 -0400268 kvfree(ndd->data);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400269 kfree(ndd);
270 put_device(dev);
271}
272
273void get_ndd(struct nvdimm_drvdata *ndd)
274{
275 kref_get(&ndd->kref);
276}
277
278void put_ndd(struct nvdimm_drvdata *ndd)
279{
280 if (ndd)
281 kref_put(&ndd->kref, nvdimm_drvdata_release);
282}
283
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400284const char *nvdimm_name(struct nvdimm *nvdimm)
285{
286 return dev_name(&nvdimm->dev);
287}
288EXPORT_SYMBOL_GPL(nvdimm_name);
289
Dan Williamsba9c8dd2016-08-22 19:28:37 -0700290struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
291{
292 return &nvdimm->dev.kobj;
293}
294EXPORT_SYMBOL_GPL(nvdimm_kobj);
295
Dan Williamse3654ec2016-04-28 16:17:07 -0700296unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
297{
298 return nvdimm->cmd_mask;
299}
300EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
301
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400302void *nvdimm_provider_data(struct nvdimm *nvdimm)
303{
Dan Williams62232e452015-06-08 14:27:06 -0400304 if (nvdimm)
305 return nvdimm->provider_data;
306 return NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400307}
308EXPORT_SYMBOL_GPL(nvdimm_provider_data);
309
Dan Williams62232e452015-06-08 14:27:06 -0400310static ssize_t commands_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
312{
313 struct nvdimm *nvdimm = to_nvdimm(dev);
314 int cmd, len = 0;
315
Dan Williamse3654ec2016-04-28 16:17:07 -0700316 if (!nvdimm->cmd_mask)
Dan Williams62232e452015-06-08 14:27:06 -0400317 return sprintf(buf, "\n");
318
Dan Williamse3654ec2016-04-28 16:17:07 -0700319 for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -0400320 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
321 len += sprintf(buf + len, "\n");
322 return len;
323}
324static DEVICE_ATTR_RO(commands);
325
Dan Williamsefbf6f52017-09-25 10:24:26 -0700326static ssize_t flags_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct nvdimm *nvdimm = to_nvdimm(dev);
330
331 return sprintf(buf, "%s%s\n",
332 test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "",
333 test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
334}
335static DEVICE_ATTR_RO(flags);
336
Dan Williamseaf96152015-05-01 13:11:27 -0400337static ssize_t state_show(struct device *dev, struct device_attribute *attr,
338 char *buf)
339{
340 struct nvdimm *nvdimm = to_nvdimm(dev);
341
342 /*
343 * The state may be in the process of changing, userspace should
344 * quiesce probing if it wants a static answer
345 */
346 nvdimm_bus_lock(dev);
347 nvdimm_bus_unlock(dev);
348 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
349 ? "active" : "idle");
350}
351static DEVICE_ATTR_RO(state);
352
Dan Williams0ba1c632015-05-30 12:35:36 -0400353static ssize_t available_slots_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
356 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
357 ssize_t rc;
358 u32 nfree;
359
360 if (!ndd)
361 return -ENXIO;
362
363 nvdimm_bus_lock(dev);
364 nfree = nd_label_nfree(ndd);
365 if (nfree - 1 > nfree) {
366 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
367 nfree = 0;
368 } else
369 nfree--;
370 rc = sprintf(buf, "%d\n", nfree);
371 nvdimm_bus_unlock(dev);
372 return rc;
373}
374static DEVICE_ATTR_RO(available_slots);
375
Dave Jiang3c13e2a2018-12-10 13:20:42 -0700376__weak ssize_t security_show(struct device *dev,
Dave Jiangf2989392018-12-05 23:39:29 -0800377 struct device_attribute *attr, char *buf)
378{
379 struct nvdimm *nvdimm = to_nvdimm(dev);
380
Dan Williamsd78c6202019-08-26 17:54:54 -0700381 if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800382 return sprintf(buf, "disabled\n");
Dan Williamsd78c6202019-08-26 17:54:54 -0700383 if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800384 return sprintf(buf, "unlocked\n");
Dan Williamsd78c6202019-08-26 17:54:54 -0700385 if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800386 return sprintf(buf, "locked\n");
Dan Williamsd78c6202019-08-26 17:54:54 -0700387 if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800388 return sprintf(buf, "overwrite\n");
Dave Jiangf2989392018-12-05 23:39:29 -0800389 return -ENOTTY;
390}
Dave Jiang37833fb2018-12-06 09:14:08 -0800391
Dan Williamsd78c6202019-08-26 17:54:54 -0700392static ssize_t frozen_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
395 struct nvdimm *nvdimm = to_nvdimm(dev);
396
397 return sprintf(buf, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN,
398 &nvdimm->sec.flags));
399}
400static DEVICE_ATTR_RO(frozen);
401
Dave Jiang37833fb2018-12-06 09:14:08 -0800402static ssize_t security_store(struct device *dev,
403 struct device_attribute *attr, const char *buf, size_t len)
404
405{
406 ssize_t rc;
407
408 /*
409 * Require all userspace triggered security management to be
410 * done while probing is idle and the DIMM is not in active use
411 * in any region.
412 */
Dan Williams87a30e12019-07-17 18:08:26 -0700413 nd_device_lock(dev);
Dave Jiang37833fb2018-12-06 09:14:08 -0800414 nvdimm_bus_lock(dev);
415 wait_nvdimm_bus_probe_idle(dev);
Dan Williams7b604222019-08-26 17:55:05 -0700416 rc = nvdimm_security_store(dev, buf, len);
Dave Jiang37833fb2018-12-06 09:14:08 -0800417 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700418 nd_device_unlock(dev);
Dave Jiang37833fb2018-12-06 09:14:08 -0800419
420 return rc;
421}
422static DEVICE_ATTR_RW(security);
Dave Jiangf2989392018-12-05 23:39:29 -0800423
Dan Williams62232e452015-06-08 14:27:06 -0400424static struct attribute *nvdimm_attributes[] = {
Dan Williamseaf96152015-05-01 13:11:27 -0400425 &dev_attr_state.attr,
Dan Williamsefbf6f52017-09-25 10:24:26 -0700426 &dev_attr_flags.attr,
Dan Williams62232e452015-06-08 14:27:06 -0400427 &dev_attr_commands.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -0400428 &dev_attr_available_slots.attr,
Dave Jiangf2989392018-12-05 23:39:29 -0800429 &dev_attr_security.attr,
Dan Williamsd78c6202019-08-26 17:54:54 -0700430 &dev_attr_frozen.attr,
Dan Williams62232e452015-06-08 14:27:06 -0400431 NULL,
432};
433
Dave Jiangf2989392018-12-05 23:39:29 -0800434static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
435{
436 struct device *dev = container_of(kobj, typeof(*dev), kobj);
437 struct nvdimm *nvdimm = to_nvdimm(dev);
438
Dan Williamsd78c6202019-08-26 17:54:54 -0700439 if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
Dave Jiangf2989392018-12-05 23:39:29 -0800440 return a->mode;
Dan Williamsd78c6202019-08-26 17:54:54 -0700441 if (!nvdimm->sec.flags)
Dave Jiangf2989392018-12-05 23:39:29 -0800442 return 0;
Dan Williamsd78c6202019-08-26 17:54:54 -0700443
444 if (a == &dev_attr_security.attr) {
445 /* Are there any state mutation ops (make writable)? */
446 if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
447 || nvdimm->sec.ops->change_key
448 || nvdimm->sec.ops->erase
449 || nvdimm->sec.ops->overwrite)
450 return a->mode;
451 return 0444;
452 }
453
454 if (nvdimm->sec.ops->freeze)
Dave Jiang37833fb2018-12-06 09:14:08 -0800455 return a->mode;
Dan Williamsd78c6202019-08-26 17:54:54 -0700456 return 0;
Dave Jiangf2989392018-12-05 23:39:29 -0800457}
458
Dan Williams62232e452015-06-08 14:27:06 -0400459struct attribute_group nvdimm_attribute_group = {
460 .attrs = nvdimm_attributes,
Dave Jiangf2989392018-12-05 23:39:29 -0800461 .is_visible = nvdimm_visible,
Dan Williams62232e452015-06-08 14:27:06 -0400462};
463EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
464
Dave Jiangd6548ae2018-12-04 10:31:20 -0800465struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
466 void *provider_data, const struct attribute_group **groups,
467 unsigned long flags, unsigned long cmd_mask, int num_flush,
Dave Jiangf2989392018-12-05 23:39:29 -0800468 struct resource *flush_wpq, const char *dimm_id,
469 const struct nvdimm_security_ops *sec_ops)
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400470{
471 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
472 struct device *dev;
473
474 if (!nvdimm)
475 return NULL;
476
477 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
478 if (nvdimm->id < 0) {
479 kfree(nvdimm);
480 return NULL;
481 }
Dave Jiangd6548ae2018-12-04 10:31:20 -0800482
483 nvdimm->dimm_id = dimm_id;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400484 nvdimm->provider_data = provider_data;
Dan Williamsd5d30d52019-02-02 16:35:26 -0800485 if (noblk)
486 flags |= 1 << NDD_NOBLK;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400487 nvdimm->flags = flags;
Dan Williamse3654ec2016-04-28 16:17:07 -0700488 nvdimm->cmd_mask = cmd_mask;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700489 nvdimm->num_flush = num_flush;
490 nvdimm->flush_wpq = flush_wpq;
Dan Williamseaf96152015-05-01 13:11:27 -0400491 atomic_set(&nvdimm->busy, 0);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400492 dev = &nvdimm->dev;
493 dev_set_name(dev, "nmem%d", nvdimm->id);
494 dev->parent = &nvdimm_bus->dev;
495 dev->type = &nvdimm_device_type;
Dan Williams62232e452015-06-08 14:27:06 -0400496 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400497 dev->groups = groups;
Dave Jiangf2989392018-12-05 23:39:29 -0800498 nvdimm->sec.ops = sec_ops;
Dave Jiang7d988092018-12-13 15:36:18 -0700499 nvdimm->sec.overwrite_tmo = 0;
500 INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
Dave Jiangf2989392018-12-05 23:39:29 -0800501 /*
502 * Security state must be initialized before device_add() for
503 * attribute visibility.
504 */
Dave Jiang89fa9d82018-12-10 10:53:22 -0700505 /* get security state and extended (master) state */
Dan Williamsd78c6202019-08-26 17:54:54 -0700506 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
507 nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
Dan Williams4d88a972015-05-31 14:41:48 -0400508 nd_device_register(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400509
510 return nvdimm;
511}
Dave Jiangd6548ae2018-12-04 10:31:20 -0800512EXPORT_SYMBOL_GPL(__nvdimm_create);
Dan Williams4d88a972015-05-31 14:41:48 -0400513
Dan Williams1cd73862019-01-19 08:45:56 -0800514static void shutdown_security_notify(void *data)
Dave Jiang7d988092018-12-13 15:36:18 -0700515{
Dan Williams1cd73862019-01-19 08:45:56 -0800516 struct nvdimm *nvdimm = data;
517
518 sysfs_put(nvdimm->sec.overwrite_state);
519}
520
521int nvdimm_security_setup_events(struct device *dev)
522{
523 struct nvdimm *nvdimm = to_nvdimm(dev);
524
Dan Williamsd78c6202019-08-26 17:54:54 -0700525 if (!nvdimm->sec.flags || !nvdimm->sec.ops
Dan Williams1cd73862019-01-19 08:45:56 -0800526 || !nvdimm->sec.ops->overwrite)
527 return 0;
528 nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
Dave Jiang7d988092018-12-13 15:36:18 -0700529 if (!nvdimm->sec.overwrite_state)
Dan Williams1cd73862019-01-19 08:45:56 -0800530 return -ENOMEM;
531
532 return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
Dave Jiang7d988092018-12-13 15:36:18 -0700533}
534EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
535
536int nvdimm_in_overwrite(struct nvdimm *nvdimm)
537{
538 return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
539}
540EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
541
Dave Jiang37833fb2018-12-06 09:14:08 -0800542int nvdimm_security_freeze(struct nvdimm *nvdimm)
543{
544 int rc;
545
546 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
547
548 if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
549 return -EOPNOTSUPP;
550
Dan Williamsd78c6202019-08-26 17:54:54 -0700551 if (!nvdimm->sec.flags)
Dave Jiang37833fb2018-12-06 09:14:08 -0800552 return -EIO;
553
Dave Jiang7d988092018-12-13 15:36:18 -0700554 if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
555 dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n");
556 return -EBUSY;
557 }
558
Dave Jiang37833fb2018-12-06 09:14:08 -0800559 rc = nvdimm->sec.ops->freeze(nvdimm);
Dan Williamsd78c6202019-08-26 17:54:54 -0700560 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
Dave Jiang37833fb2018-12-06 09:14:08 -0800561
562 return rc;
563}
564
Dan Williams762d0672016-10-04 16:09:59 -0700565int alias_dpa_busy(struct device *dev, void *data)
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700566{
Dan Williamsfe514732017-04-04 15:08:36 -0700567 resource_size_t map_end, blk_start, new;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700568 struct blk_alloc_info *info = data;
569 struct nd_mapping *nd_mapping;
570 struct nd_region *nd_region;
571 struct nvdimm_drvdata *ndd;
572 struct resource *res;
573 int i;
574
Dan Williamsc9e582a2017-05-29 23:12:19 -0700575 if (!is_memory(dev))
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700576 return 0;
577
578 nd_region = to_nd_region(dev);
579 for (i = 0; i < nd_region->ndr_mappings; i++) {
580 nd_mapping = &nd_region->mapping[i];
581 if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
582 break;
583 }
584
585 if (i >= nd_region->ndr_mappings)
586 return 0;
587
588 ndd = to_ndd(nd_mapping);
589 map_end = nd_mapping->start + nd_mapping->size - 1;
590 blk_start = nd_mapping->start;
Dan Williams762d0672016-10-04 16:09:59 -0700591
592 /*
593 * In the allocation case ->res is set to free space that we are
594 * looking to validate against PMEM aliasing collision rules
595 * (i.e. BLK is allocated after all aliased PMEM).
596 */
597 if (info->res) {
598 if (info->res->start >= nd_mapping->start
599 && info->res->start < map_end)
600 /* pass */;
601 else
602 return 0;
603 }
604
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700605 retry:
606 /*
607 * Find the free dpa from the end of the last pmem allocation to
Dan Williamsfe514732017-04-04 15:08:36 -0700608 * the end of the interleave-set mapping.
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700609 */
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700610 for_each_dpa_resource(ndd, res) {
Dan Williamsfe514732017-04-04 15:08:36 -0700611 if (strncmp(res->name, "pmem", 4) != 0)
612 continue;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700613 if ((res->start >= blk_start && res->start < map_end)
614 || (res->end >= blk_start
615 && res->end <= map_end)) {
Dan Williamsfe514732017-04-04 15:08:36 -0700616 new = max(blk_start, min(map_end + 1, res->end + 1));
617 if (new != blk_start) {
618 blk_start = new;
619 goto retry;
620 }
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700621 }
622 }
623
Dan Williams762d0672016-10-04 16:09:59 -0700624 /* update the free space range with the probed blk_start */
625 if (info->res && blk_start > info->res->start) {
626 info->res->start = max(info->res->start, blk_start);
627 if (info->res->start > info->res->end)
628 info->res->end = info->res->start - 1;
629 return 1;
630 }
631
Dan Williamsfe514732017-04-04 15:08:36 -0700632 info->available -= blk_start - nd_mapping->start;
Dan Williams762d0672016-10-04 16:09:59 -0700633
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700634 return 0;
635}
636
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400637/**
Dan Williams1b40e092015-05-01 13:34:01 -0400638 * nd_blk_available_dpa - account the unused dpa of BLK region
639 * @nd_mapping: container of dpa-resource-root + labels
640 *
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700641 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
642 * we arrange for them to never start at an lower dpa than the last
643 * PMEM allocation in an aliased region.
Dan Williams1b40e092015-05-01 13:34:01 -0400644 */
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700645resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -0400646{
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700647 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
648 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williams1b40e092015-05-01 13:34:01 -0400649 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700650 struct blk_alloc_info info = {
651 .nd_mapping = nd_mapping,
652 .available = nd_mapping->size,
Dan Williams762d0672016-10-04 16:09:59 -0700653 .res = NULL,
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700654 };
Dan Williams1b40e092015-05-01 13:34:01 -0400655 struct resource *res;
656
657 if (!ndd)
658 return 0;
659
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700660 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
Dan Williams1b40e092015-05-01 13:34:01 -0400661
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700662 /* now account for busy blk allocations in unaliased dpa */
663 for_each_dpa_resource(ndd, res) {
664 if (strncmp(res->name, "blk", 3) != 0)
665 continue;
Dan Williamsfe514732017-04-04 15:08:36 -0700666 info.available -= resource_size(res);
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700667 }
668
669 return info.available;
Dan Williams1b40e092015-05-01 13:34:01 -0400670}
671
672/**
Keith Busch12e31292018-07-24 15:07:57 -0600673 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
674 * contiguous unallocated dpa range.
675 * @nd_region: constrain available space check to this reference region
676 * @nd_mapping: container of dpa-resource-root + labels
677 */
678resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
679 struct nd_mapping *nd_mapping)
680{
681 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
682 struct nvdimm_bus *nvdimm_bus;
683 resource_size_t max = 0;
684 struct resource *res;
685
686 /* if a dimm is disabled the available capacity is zero */
687 if (!ndd)
688 return 0;
689
690 nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
691 if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm))
692 return 0;
693 for_each_dpa_resource(ndd, res) {
694 if (strcmp(res->name, "pmem-reserve") != 0)
695 continue;
696 if (resource_size(res) > max)
697 max = resource_size(res);
698 }
699 release_free_pmem(nvdimm_bus, nd_mapping);
700 return max;
701}
702
703/**
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400704 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
705 * @nd_mapping: container of dpa-resource-root + labels
706 * @nd_region: constrain available space check to this reference region
707 * @overlap: calculate available space assuming this level of overlap
708 *
709 * Validate that a PMEM label, if present, aligns with the start of an
710 * interleave set and truncate the available size at the lowest BLK
711 * overlap point.
712 *
713 * The expectation is that this routine is called multiple times as it
714 * probes for the largest BLK encroachment for any single member DIMM of
715 * the interleave set. Once that value is determined the PMEM-limit for
716 * the set can be established.
717 */
718resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
719 struct nd_mapping *nd_mapping, resource_size_t *overlap)
720{
721 resource_size_t map_start, map_end, busy = 0, available, blk_start;
722 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
723 struct resource *res;
724 const char *reason;
725
726 if (!ndd)
727 return 0;
728
729 map_start = nd_mapping->start;
730 map_end = map_start + nd_mapping->size - 1;
731 blk_start = max(map_start, map_end + 1 - *overlap);
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700732 for_each_dpa_resource(ndd, res) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400733 if (res->start >= map_start && res->start < map_end) {
734 if (strncmp(res->name, "blk", 3) == 0)
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700735 blk_start = min(blk_start,
736 max(map_start, res->start));
737 else if (res->end > map_end) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400738 reason = "misaligned to iset";
739 goto err;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700740 } else
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400741 busy += resource_size(res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400742 } else if (res->end >= map_start && res->end <= map_end) {
743 if (strncmp(res->name, "blk", 3) == 0) {
744 /*
745 * If a BLK allocation overlaps the start of
746 * PMEM the entire interleave set may now only
747 * be used for BLK.
748 */
749 blk_start = map_start;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700750 } else
751 busy += resource_size(res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400752 } else if (map_start > res->start && map_start < res->end) {
753 /* total eclipse of the mapping */
754 busy += nd_mapping->size;
755 blk_start = map_start;
756 }
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700757 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400758
759 *overlap = map_end + 1 - blk_start;
760 available = blk_start - map_start;
761 if (busy < available)
762 return available - busy;
763 return 0;
764
765 err:
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400766 nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
767 return 0;
768}
769
Dan Williams4a826c82015-06-09 16:09:36 -0400770void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
771{
772 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
773 kfree(res->name);
774 __release_region(&ndd->dpa, res->start, resource_size(res));
775}
776
777struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
778 struct nd_label_id *label_id, resource_size_t start,
779 resource_size_t n)
780{
781 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
782 struct resource *res;
783
784 if (!name)
785 return NULL;
786
787 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
788 res = __request_region(&ndd->dpa, start, n, name, 0);
789 if (!res)
790 kfree(name);
791 return res;
792}
793
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400794/**
795 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
796 * @nvdimm: container of dpa-resource-root + labels
797 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
798 */
799resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
800 struct nd_label_id *label_id)
801{
802 resource_size_t allocated = 0;
803 struct resource *res;
804
805 for_each_dpa_resource(ndd, res)
806 if (strcmp(res->name, label_id->id) == 0)
807 allocated += resource_size(res);
808
809 return allocated;
810}
811
Dan Williams4d88a972015-05-31 14:41:48 -0400812static int count_dimms(struct device *dev, void *c)
813{
814 int *count = c;
815
816 if (is_nvdimm(dev))
817 (*count)++;
818 return 0;
819}
820
821int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
822{
823 int count = 0;
824 /* Flush any possible dimm registration failures */
825 nd_synchronize();
826
827 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
Dan Williams426824d2018-03-05 16:39:31 -0800828 dev_dbg(&nvdimm_bus->dev, "count: %d\n", count);
Dan Williams4d88a972015-05-31 14:41:48 -0400829 if (count != dimm_count)
830 return -ENXIO;
831 return 0;
832}
833EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
Dan Williamsb354aba2016-05-17 20:24:16 -0700834
835void __exit nvdimm_devs_exit(void)
836{
837 ida_destroy(&dimm_ida);
838}