blob: dc7449a400037b273ff0b7b01e115eb3cad19c81 [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 Williamsa0e37452020-01-30 12:06:18 -080035 if (test_bit(NDD_LABELING, &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 Williamsa0e37452020-01-30 12:06:18 -0800176void nvdimm_set_labeling(struct device *dev)
Dan Williams42237e32016-10-15 15:33:52 -0700177{
178 struct nvdimm *nvdimm = to_nvdimm(dev);
179
Dan Williamsa0e37452020-01-30 12:06:18 -0800180 set_bit(NDD_LABELING, &nvdimm->flags);
Dan Williams8f078b32017-05-04 14:01:24 -0700181}
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 Williamse6dfb2d2015-04-25 03:56:17 -0400205struct nvdimm *to_nvdimm(struct device *dev)
206{
207 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
208
209 WARN_ON(!is_nvdimm(dev));
210 return nvdimm;
211}
212EXPORT_SYMBOL_GPL(to_nvdimm);
213
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400214struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
215{
216 struct nd_region *nd_region = &ndbr->nd_region;
217 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
218
219 return nd_mapping->nvdimm;
220}
221EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
222
Dan Williamsca6a4652017-01-13 20:36:58 -0800223unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr)
224{
225 /* pmem mapping properties are private to libnvdimm */
226 return ARCH_MEMREMAP_PMEM;
227}
228EXPORT_SYMBOL_GPL(nd_blk_memremap_flags);
229
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400230struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
231{
232 struct nvdimm *nvdimm = nd_mapping->nvdimm;
233
234 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
235
236 return dev_get_drvdata(&nvdimm->dev);
237}
238EXPORT_SYMBOL(to_ndd);
239
240void nvdimm_drvdata_release(struct kref *kref)
241{
242 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
243 struct device *dev = ndd->dev;
244 struct resource *res, *_r;
245
Dan Williams426824d2018-03-05 16:39:31 -0800246 dev_dbg(dev, "trace\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400247 nvdimm_bus_lock(dev);
248 for_each_dpa_resource_safe(ndd, res, _r)
249 nvdimm_free_dpa(ndd, res);
250 nvdimm_bus_unlock(dev);
251
yalin wanga06a7572015-08-27 19:35:48 -0400252 kvfree(ndd->data);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400253 kfree(ndd);
254 put_device(dev);
255}
256
257void get_ndd(struct nvdimm_drvdata *ndd)
258{
259 kref_get(&ndd->kref);
260}
261
262void put_ndd(struct nvdimm_drvdata *ndd)
263{
264 if (ndd)
265 kref_put(&ndd->kref, nvdimm_drvdata_release);
266}
267
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400268const char *nvdimm_name(struct nvdimm *nvdimm)
269{
270 return dev_name(&nvdimm->dev);
271}
272EXPORT_SYMBOL_GPL(nvdimm_name);
273
Dan Williamsba9c8dd2016-08-22 19:28:37 -0700274struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
275{
276 return &nvdimm->dev.kobj;
277}
278EXPORT_SYMBOL_GPL(nvdimm_kobj);
279
Dan Williamse3654ec2016-04-28 16:17:07 -0700280unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
281{
282 return nvdimm->cmd_mask;
283}
284EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
285
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400286void *nvdimm_provider_data(struct nvdimm *nvdimm)
287{
Dan Williams62232e452015-06-08 14:27:06 -0400288 if (nvdimm)
289 return nvdimm->provider_data;
290 return NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400291}
292EXPORT_SYMBOL_GPL(nvdimm_provider_data);
293
Dan Williams62232e452015-06-08 14:27:06 -0400294static ssize_t commands_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct nvdimm *nvdimm = to_nvdimm(dev);
298 int cmd, len = 0;
299
Dan Williamse3654ec2016-04-28 16:17:07 -0700300 if (!nvdimm->cmd_mask)
Dan Williams62232e452015-06-08 14:27:06 -0400301 return sprintf(buf, "\n");
302
Dan Williamse3654ec2016-04-28 16:17:07 -0700303 for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -0400304 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
305 len += sprintf(buf + len, "\n");
306 return len;
307}
308static DEVICE_ATTR_RO(commands);
309
Dan Williamsefbf6f52017-09-25 10:24:26 -0700310static ssize_t flags_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
312{
313 struct nvdimm *nvdimm = to_nvdimm(dev);
314
Dan Williamsa0e37452020-01-30 12:06:18 -0800315 return sprintf(buf, "%s%s%s\n",
Dan Williamsefbf6f52017-09-25 10:24:26 -0700316 test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "",
Dan Williamsa0e37452020-01-30 12:06:18 -0800317 test_bit(NDD_LABELING, &nvdimm->flags) ? "label " : "",
Dan Williamsefbf6f52017-09-25 10:24:26 -0700318 test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
319}
320static DEVICE_ATTR_RO(flags);
321
Dan Williamseaf96152015-05-01 13:11:27 -0400322static ssize_t state_show(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct nvdimm *nvdimm = to_nvdimm(dev);
326
327 /*
328 * The state may be in the process of changing, userspace should
329 * quiesce probing if it wants a static answer
330 */
331 nvdimm_bus_lock(dev);
332 nvdimm_bus_unlock(dev);
333 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
334 ? "active" : "idle");
335}
336static DEVICE_ATTR_RO(state);
337
Dan Williams7018c892021-02-01 16:20:40 -0800338static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf)
Dan Williams0ba1c632015-05-30 12:35:36 -0400339{
Dan Williams7018c892021-02-01 16:20:40 -0800340 struct device *dev;
Dan Williams0ba1c632015-05-30 12:35:36 -0400341 ssize_t rc;
342 u32 nfree;
343
344 if (!ndd)
345 return -ENXIO;
346
Dan Williams7018c892021-02-01 16:20:40 -0800347 dev = ndd->dev;
Dan Williams0ba1c632015-05-30 12:35:36 -0400348 nvdimm_bus_lock(dev);
349 nfree = nd_label_nfree(ndd);
350 if (nfree - 1 > nfree) {
351 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
352 nfree = 0;
353 } else
354 nfree--;
355 rc = sprintf(buf, "%d\n", nfree);
356 nvdimm_bus_unlock(dev);
357 return rc;
358}
Dan Williams7018c892021-02-01 16:20:40 -0800359
360static ssize_t available_slots_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
362{
363 ssize_t rc;
364
365 nd_device_lock(dev);
366 rc = __available_slots_show(dev_get_drvdata(dev), buf);
367 nd_device_unlock(dev);
368
369 return rc;
370}
Dan Williams0ba1c632015-05-30 12:35:36 -0400371static DEVICE_ATTR_RO(available_slots);
372
Dave Jiang3c13e2a2018-12-10 13:20:42 -0700373__weak ssize_t security_show(struct device *dev,
Dave Jiangf2989392018-12-05 23:39:29 -0800374 struct device_attribute *attr, char *buf)
375{
376 struct nvdimm *nvdimm = to_nvdimm(dev);
377
Jane Chu7c02d532020-08-03 16:41:38 -0600378 if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
379 return sprintf(buf, "overwrite\n");
Dan Williamsd78c6202019-08-26 17:54:54 -0700380 if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800381 return sprintf(buf, "disabled\n");
Dan Williamsd78c6202019-08-26 17:54:54 -0700382 if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800383 return sprintf(buf, "unlocked\n");
Dan Williamsd78c6202019-08-26 17:54:54 -0700384 if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
Dave Jiangf2989392018-12-05 23:39:29 -0800385 return sprintf(buf, "locked\n");
Dave Jiangf2989392018-12-05 23:39:29 -0800386 return -ENOTTY;
387}
Dave Jiang37833fb2018-12-06 09:14:08 -0800388
Dan Williamsd78c6202019-08-26 17:54:54 -0700389static ssize_t frozen_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 struct nvdimm *nvdimm = to_nvdimm(dev);
393
394 return sprintf(buf, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN,
395 &nvdimm->sec.flags));
396}
397static DEVICE_ATTR_RO(frozen);
398
Dave Jiang37833fb2018-12-06 09:14:08 -0800399static ssize_t security_store(struct device *dev,
400 struct device_attribute *attr, const char *buf, size_t len)
401
402{
403 ssize_t rc;
404
405 /*
406 * Require all userspace triggered security management to be
407 * done while probing is idle and the DIMM is not in active use
408 * in any region.
409 */
Dan Williams87a30e12019-07-17 18:08:26 -0700410 nd_device_lock(dev);
Dave Jiang37833fb2018-12-06 09:14:08 -0800411 nvdimm_bus_lock(dev);
412 wait_nvdimm_bus_probe_idle(dev);
Dan Williams7b604222019-08-26 17:55:05 -0700413 rc = nvdimm_security_store(dev, buf, len);
Dave Jiang37833fb2018-12-06 09:14:08 -0800414 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700415 nd_device_unlock(dev);
Dave Jiang37833fb2018-12-06 09:14:08 -0800416
417 return rc;
418}
419static DEVICE_ATTR_RW(security);
Dave Jiangf2989392018-12-05 23:39:29 -0800420
Dan Williams62232e452015-06-08 14:27:06 -0400421static struct attribute *nvdimm_attributes[] = {
Dan Williamseaf96152015-05-01 13:11:27 -0400422 &dev_attr_state.attr,
Dan Williamsefbf6f52017-09-25 10:24:26 -0700423 &dev_attr_flags.attr,
Dan Williams62232e452015-06-08 14:27:06 -0400424 &dev_attr_commands.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -0400425 &dev_attr_available_slots.attr,
Dave Jiangf2989392018-12-05 23:39:29 -0800426 &dev_attr_security.attr,
Dan Williamsd78c6202019-08-26 17:54:54 -0700427 &dev_attr_frozen.attr,
Dan Williams62232e452015-06-08 14:27:06 -0400428 NULL,
429};
430
Dave Jiangf2989392018-12-05 23:39:29 -0800431static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
432{
433 struct device *dev = container_of(kobj, typeof(*dev), kobj);
434 struct nvdimm *nvdimm = to_nvdimm(dev);
435
Dan Williamsd78c6202019-08-26 17:54:54 -0700436 if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
Dave Jiangf2989392018-12-05 23:39:29 -0800437 return a->mode;
Dan Williamsd78c6202019-08-26 17:54:54 -0700438 if (!nvdimm->sec.flags)
Dave Jiangf2989392018-12-05 23:39:29 -0800439 return 0;
Dan Williamsd78c6202019-08-26 17:54:54 -0700440
441 if (a == &dev_attr_security.attr) {
442 /* Are there any state mutation ops (make writable)? */
443 if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
444 || nvdimm->sec.ops->change_key
445 || nvdimm->sec.ops->erase
446 || nvdimm->sec.ops->overwrite)
447 return a->mode;
448 return 0444;
449 }
450
451 if (nvdimm->sec.ops->freeze)
Dave Jiang37833fb2018-12-06 09:14:08 -0800452 return a->mode;
Dan Williamsd78c6202019-08-26 17:54:54 -0700453 return 0;
Dave Jiangf2989392018-12-05 23:39:29 -0800454}
455
Dan Williams360eba72019-11-12 17:08:04 -0800456static const struct attribute_group nvdimm_attribute_group = {
Dan Williams62232e452015-06-08 14:27:06 -0400457 .attrs = nvdimm_attributes,
Dave Jiangf2989392018-12-05 23:39:29 -0800458 .is_visible = nvdimm_visible,
Dan Williams62232e452015-06-08 14:27:06 -0400459};
Dan Williams360eba72019-11-12 17:08:04 -0800460
Dan Williams48001ea2020-07-20 15:08:18 -0700461static ssize_t result_show(struct device *dev, struct device_attribute *attr, char *buf)
462{
463 struct nvdimm *nvdimm = to_nvdimm(dev);
464 enum nvdimm_fwa_result result;
465
466 if (!nvdimm->fw_ops)
467 return -EOPNOTSUPP;
468
469 nvdimm_bus_lock(dev);
470 result = nvdimm->fw_ops->activate_result(nvdimm);
471 nvdimm_bus_unlock(dev);
472
473 switch (result) {
474 case NVDIMM_FWA_RESULT_NONE:
475 return sprintf(buf, "none\n");
476 case NVDIMM_FWA_RESULT_SUCCESS:
477 return sprintf(buf, "success\n");
478 case NVDIMM_FWA_RESULT_FAIL:
479 return sprintf(buf, "fail\n");
480 case NVDIMM_FWA_RESULT_NOTSTAGED:
481 return sprintf(buf, "not_staged\n");
482 case NVDIMM_FWA_RESULT_NEEDRESET:
483 return sprintf(buf, "need_reset\n");
484 default:
485 return -ENXIO;
486 }
487}
488static DEVICE_ATTR_ADMIN_RO(result);
489
490static ssize_t activate_show(struct device *dev, struct device_attribute *attr, char *buf)
491{
492 struct nvdimm *nvdimm = to_nvdimm(dev);
493 enum nvdimm_fwa_state state;
494
495 if (!nvdimm->fw_ops)
496 return -EOPNOTSUPP;
497
498 nvdimm_bus_lock(dev);
499 state = nvdimm->fw_ops->activate_state(nvdimm);
500 nvdimm_bus_unlock(dev);
501
502 switch (state) {
503 case NVDIMM_FWA_IDLE:
504 return sprintf(buf, "idle\n");
505 case NVDIMM_FWA_BUSY:
506 return sprintf(buf, "busy\n");
507 case NVDIMM_FWA_ARMED:
508 return sprintf(buf, "armed\n");
509 default:
510 return -ENXIO;
511 }
512}
513
514static ssize_t activate_store(struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t len)
516{
517 struct nvdimm *nvdimm = to_nvdimm(dev);
518 enum nvdimm_fwa_trigger arg;
519 int rc;
520
521 if (!nvdimm->fw_ops)
522 return -EOPNOTSUPP;
523
524 if (sysfs_streq(buf, "arm"))
525 arg = NVDIMM_FWA_ARM;
526 else if (sysfs_streq(buf, "disarm"))
527 arg = NVDIMM_FWA_DISARM;
528 else
529 return -EINVAL;
530
531 nvdimm_bus_lock(dev);
532 rc = nvdimm->fw_ops->arm(nvdimm, arg);
533 nvdimm_bus_unlock(dev);
534
535 if (rc < 0)
536 return rc;
537 return len;
538}
539static DEVICE_ATTR_ADMIN_RW(activate);
540
541static struct attribute *nvdimm_firmware_attributes[] = {
542 &dev_attr_activate.attr,
543 &dev_attr_result.attr,
Zqiang62c78922020-08-12 16:55:01 +0800544 NULL,
Dan Williams48001ea2020-07-20 15:08:18 -0700545};
546
547static umode_t nvdimm_firmware_visible(struct kobject *kobj, struct attribute *a, int n)
548{
549 struct device *dev = container_of(kobj, typeof(*dev), kobj);
550 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
551 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
552 struct nvdimm *nvdimm = to_nvdimm(dev);
553 enum nvdimm_fwa_capability cap;
554
555 if (!nd_desc->fw_ops)
556 return 0;
557 if (!nvdimm->fw_ops)
558 return 0;
559
560 nvdimm_bus_lock(dev);
561 cap = nd_desc->fw_ops->capability(nd_desc);
562 nvdimm_bus_unlock(dev);
563
564 if (cap < NVDIMM_FWA_CAP_QUIESCE)
565 return 0;
566
567 return a->mode;
568}
569
570static const struct attribute_group nvdimm_firmware_attribute_group = {
571 .name = "firmware",
572 .attrs = nvdimm_firmware_attributes,
573 .is_visible = nvdimm_firmware_visible,
574};
575
Dan Williams360eba72019-11-12 17:08:04 -0800576static const struct attribute_group *nvdimm_attribute_groups[] = {
577 &nd_device_attribute_group,
578 &nvdimm_attribute_group,
Dan Williams48001ea2020-07-20 15:08:18 -0700579 &nvdimm_firmware_attribute_group,
Dan Williams360eba72019-11-12 17:08:04 -0800580 NULL,
581};
582
583static const struct device_type nvdimm_device_type = {
584 .name = "nvdimm",
585 .release = nvdimm_release,
586 .groups = nvdimm_attribute_groups,
587};
588
589bool is_nvdimm(struct device *dev)
590{
591 return dev->type == &nvdimm_device_type;
592}
Dan Williams62232e452015-06-08 14:27:06 -0400593
Dave Jiangd6548ae2018-12-04 10:31:20 -0800594struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
595 void *provider_data, const struct attribute_group **groups,
596 unsigned long flags, unsigned long cmd_mask, int num_flush,
Dave Jiangf2989392018-12-05 23:39:29 -0800597 struct resource *flush_wpq, const char *dimm_id,
Dan Williamsa1facc12020-07-20 15:08:24 -0700598 const struct nvdimm_security_ops *sec_ops,
599 const struct nvdimm_fw_ops *fw_ops)
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400600{
601 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
602 struct device *dev;
603
604 if (!nvdimm)
605 return NULL;
606
607 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
608 if (nvdimm->id < 0) {
609 kfree(nvdimm);
610 return NULL;
611 }
Dave Jiangd6548ae2018-12-04 10:31:20 -0800612
613 nvdimm->dimm_id = dimm_id;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400614 nvdimm->provider_data = provider_data;
Dan Williamsd5d30d52019-02-02 16:35:26 -0800615 if (noblk)
616 flags |= 1 << NDD_NOBLK;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400617 nvdimm->flags = flags;
Dan Williamse3654ec2016-04-28 16:17:07 -0700618 nvdimm->cmd_mask = cmd_mask;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700619 nvdimm->num_flush = num_flush;
620 nvdimm->flush_wpq = flush_wpq;
Dan Williamseaf96152015-05-01 13:11:27 -0400621 atomic_set(&nvdimm->busy, 0);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400622 dev = &nvdimm->dev;
623 dev_set_name(dev, "nmem%d", nvdimm->id);
624 dev->parent = &nvdimm_bus->dev;
625 dev->type = &nvdimm_device_type;
Dan Williams62232e452015-06-08 14:27:06 -0400626 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400627 dev->groups = groups;
Dave Jiangf2989392018-12-05 23:39:29 -0800628 nvdimm->sec.ops = sec_ops;
Dan Williamsa1facc12020-07-20 15:08:24 -0700629 nvdimm->fw_ops = fw_ops;
Dave Jiang7d988092018-12-13 15:36:18 -0700630 nvdimm->sec.overwrite_tmo = 0;
631 INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
Dave Jiangf2989392018-12-05 23:39:29 -0800632 /*
633 * Security state must be initialized before device_add() for
634 * attribute visibility.
635 */
Dave Jiang89fa9d82018-12-10 10:53:22 -0700636 /* get security state and extended (master) state */
Dan Williamsd78c6202019-08-26 17:54:54 -0700637 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
638 nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
Dan Williams4d88a972015-05-31 14:41:48 -0400639 nd_device_register(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400640
641 return nvdimm;
642}
Dave Jiangd6548ae2018-12-04 10:31:20 -0800643EXPORT_SYMBOL_GPL(__nvdimm_create);
Dan Williams4d88a972015-05-31 14:41:48 -0400644
Dan Williamsfd146022021-06-15 16:18:22 -0700645void nvdimm_delete(struct nvdimm *nvdimm)
646{
647 struct device *dev = &nvdimm->dev;
648 bool dev_put = false;
649
650 /* We are shutting down. Make state frozen artificially. */
651 nvdimm_bus_lock(dev);
652 set_bit(NVDIMM_SECURITY_FROZEN, &nvdimm->sec.flags);
653 if (test_and_clear_bit(NDD_WORK_PENDING, &nvdimm->flags))
654 dev_put = true;
655 nvdimm_bus_unlock(dev);
656 cancel_delayed_work_sync(&nvdimm->dwork);
657 if (dev_put)
658 put_device(dev);
659 nd_device_unregister(dev, ND_SYNC);
660}
661EXPORT_SYMBOL_GPL(nvdimm_delete);
662
Dan Williams1cd73862019-01-19 08:45:56 -0800663static void shutdown_security_notify(void *data)
Dave Jiang7d988092018-12-13 15:36:18 -0700664{
Dan Williams1cd73862019-01-19 08:45:56 -0800665 struct nvdimm *nvdimm = data;
666
667 sysfs_put(nvdimm->sec.overwrite_state);
668}
669
670int nvdimm_security_setup_events(struct device *dev)
671{
672 struct nvdimm *nvdimm = to_nvdimm(dev);
673
Dan Williamsd78c6202019-08-26 17:54:54 -0700674 if (!nvdimm->sec.flags || !nvdimm->sec.ops
Dan Williams1cd73862019-01-19 08:45:56 -0800675 || !nvdimm->sec.ops->overwrite)
676 return 0;
677 nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
Dave Jiang7d988092018-12-13 15:36:18 -0700678 if (!nvdimm->sec.overwrite_state)
Dan Williams1cd73862019-01-19 08:45:56 -0800679 return -ENOMEM;
680
681 return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
Dave Jiang7d988092018-12-13 15:36:18 -0700682}
683EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
684
685int nvdimm_in_overwrite(struct nvdimm *nvdimm)
686{
687 return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
688}
689EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
690
Dave Jiang37833fb2018-12-06 09:14:08 -0800691int nvdimm_security_freeze(struct nvdimm *nvdimm)
692{
693 int rc;
694
695 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
696
697 if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
698 return -EOPNOTSUPP;
699
Dan Williamsd78c6202019-08-26 17:54:54 -0700700 if (!nvdimm->sec.flags)
Dave Jiang37833fb2018-12-06 09:14:08 -0800701 return -EIO;
702
Dave Jiang7d988092018-12-13 15:36:18 -0700703 if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
704 dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n");
705 return -EBUSY;
706 }
707
Dave Jiang37833fb2018-12-06 09:14:08 -0800708 rc = nvdimm->sec.ops->freeze(nvdimm);
Dan Williamsd78c6202019-08-26 17:54:54 -0700709 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
Dave Jiang37833fb2018-12-06 09:14:08 -0800710
711 return rc;
712}
713
Dan Williams2522afb2020-01-30 12:06:23 -0800714static unsigned long dpa_align(struct nd_region *nd_region)
715{
716 struct device *dev = &nd_region->dev;
717
718 if (dev_WARN_ONCE(dev, !is_nvdimm_bus_locked(dev),
719 "bus lock required for capacity provision\n"))
720 return 0;
721 if (dev_WARN_ONCE(dev, !nd_region->ndr_mappings || nd_region->align
722 % nd_region->ndr_mappings,
723 "invalid region align %#lx mappings: %d\n",
724 nd_region->align, nd_region->ndr_mappings))
725 return 0;
726 return nd_region->align / nd_region->ndr_mappings;
727}
728
Dan Williams762d0672016-10-04 16:09:59 -0700729int alias_dpa_busy(struct device *dev, void *data)
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700730{
Dan Williamsfe514732017-04-04 15:08:36 -0700731 resource_size_t map_end, blk_start, new;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700732 struct blk_alloc_info *info = data;
733 struct nd_mapping *nd_mapping;
734 struct nd_region *nd_region;
735 struct nvdimm_drvdata *ndd;
736 struct resource *res;
Dan Williams2522afb2020-01-30 12:06:23 -0800737 unsigned long align;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700738 int i;
739
Dan Williamsc9e582a2017-05-29 23:12:19 -0700740 if (!is_memory(dev))
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700741 return 0;
742
743 nd_region = to_nd_region(dev);
744 for (i = 0; i < nd_region->ndr_mappings; i++) {
745 nd_mapping = &nd_region->mapping[i];
746 if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
747 break;
748 }
749
750 if (i >= nd_region->ndr_mappings)
751 return 0;
752
753 ndd = to_ndd(nd_mapping);
754 map_end = nd_mapping->start + nd_mapping->size - 1;
755 blk_start = nd_mapping->start;
Dan Williams762d0672016-10-04 16:09:59 -0700756
757 /*
758 * In the allocation case ->res is set to free space that we are
759 * looking to validate against PMEM aliasing collision rules
760 * (i.e. BLK is allocated after all aliased PMEM).
761 */
762 if (info->res) {
763 if (info->res->start >= nd_mapping->start
764 && info->res->start < map_end)
765 /* pass */;
766 else
767 return 0;
768 }
769
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700770 retry:
771 /*
772 * Find the free dpa from the end of the last pmem allocation to
Dan Williamsfe514732017-04-04 15:08:36 -0700773 * the end of the interleave-set mapping.
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700774 */
Dan Williams2522afb2020-01-30 12:06:23 -0800775 align = dpa_align(nd_region);
776 if (!align)
777 return 0;
778
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700779 for_each_dpa_resource(ndd, res) {
Dan Williams2522afb2020-01-30 12:06:23 -0800780 resource_size_t start, end;
781
Dan Williamsfe514732017-04-04 15:08:36 -0700782 if (strncmp(res->name, "pmem", 4) != 0)
783 continue;
Dan Williams2522afb2020-01-30 12:06:23 -0800784
785 start = ALIGN_DOWN(res->start, align);
786 end = ALIGN(res->end + 1, align) - 1;
787 if ((start >= blk_start && start < map_end)
788 || (end >= blk_start && end <= map_end)) {
789 new = max(blk_start, min(map_end, end) + 1);
Dan Williamsfe514732017-04-04 15:08:36 -0700790 if (new != blk_start) {
791 blk_start = new;
792 goto retry;
793 }
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700794 }
795 }
796
Dan Williams762d0672016-10-04 16:09:59 -0700797 /* update the free space range with the probed blk_start */
798 if (info->res && blk_start > info->res->start) {
799 info->res->start = max(info->res->start, blk_start);
800 if (info->res->start > info->res->end)
801 info->res->end = info->res->start - 1;
802 return 1;
803 }
804
Dan Williamsfe514732017-04-04 15:08:36 -0700805 info->available -= blk_start - nd_mapping->start;
Dan Williams762d0672016-10-04 16:09:59 -0700806
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700807 return 0;
808}
809
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400810/**
Dan Williams1b40e092015-05-01 13:34:01 -0400811 * nd_blk_available_dpa - account the unused dpa of BLK region
812 * @nd_mapping: container of dpa-resource-root + labels
813 *
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700814 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
815 * we arrange for them to never start at an lower dpa than the last
816 * PMEM allocation in an aliased region.
Dan Williams1b40e092015-05-01 13:34:01 -0400817 */
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700818resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -0400819{
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700820 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
821 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williams1b40e092015-05-01 13:34:01 -0400822 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700823 struct blk_alloc_info info = {
824 .nd_mapping = nd_mapping,
825 .available = nd_mapping->size,
Dan Williams762d0672016-10-04 16:09:59 -0700826 .res = NULL,
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700827 };
Dan Williams1b40e092015-05-01 13:34:01 -0400828 struct resource *res;
Dan Williams2522afb2020-01-30 12:06:23 -0800829 unsigned long align;
Dan Williams1b40e092015-05-01 13:34:01 -0400830
831 if (!ndd)
832 return 0;
833
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700834 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
Dan Williams1b40e092015-05-01 13:34:01 -0400835
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700836 /* now account for busy blk allocations in unaliased dpa */
Dan Williams2522afb2020-01-30 12:06:23 -0800837 align = dpa_align(nd_region);
838 if (!align)
839 return 0;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700840 for_each_dpa_resource(ndd, res) {
Dan Williams2522afb2020-01-30 12:06:23 -0800841 resource_size_t start, end, size;
842
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700843 if (strncmp(res->name, "blk", 3) != 0)
844 continue;
Dan Williams2522afb2020-01-30 12:06:23 -0800845 start = ALIGN_DOWN(res->start, align);
846 end = ALIGN(res->end + 1, align) - 1;
847 size = end - start + 1;
848 if (size >= info.available)
849 return 0;
850 info.available -= size;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700851 }
852
853 return info.available;
Dan Williams1b40e092015-05-01 13:34:01 -0400854}
855
856/**
Keith Busch12e31292018-07-24 15:07:57 -0600857 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
858 * contiguous unallocated dpa range.
859 * @nd_region: constrain available space check to this reference region
860 * @nd_mapping: container of dpa-resource-root + labels
861 */
862resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
863 struct nd_mapping *nd_mapping)
864{
865 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
866 struct nvdimm_bus *nvdimm_bus;
867 resource_size_t max = 0;
868 struct resource *res;
Dan Williams2522afb2020-01-30 12:06:23 -0800869 unsigned long align;
Keith Busch12e31292018-07-24 15:07:57 -0600870
871 /* if a dimm is disabled the available capacity is zero */
872 if (!ndd)
873 return 0;
874
Dan Williams2522afb2020-01-30 12:06:23 -0800875 align = dpa_align(nd_region);
876 if (!align)
877 return 0;
878
Keith Busch12e31292018-07-24 15:07:57 -0600879 nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
880 if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm))
881 return 0;
882 for_each_dpa_resource(ndd, res) {
Dan Williams2522afb2020-01-30 12:06:23 -0800883 resource_size_t start, end;
884
Keith Busch12e31292018-07-24 15:07:57 -0600885 if (strcmp(res->name, "pmem-reserve") != 0)
886 continue;
Dan Williams2522afb2020-01-30 12:06:23 -0800887 /* trim free space relative to current alignment setting */
888 start = ALIGN(res->start, align);
889 end = ALIGN_DOWN(res->end + 1, align) - 1;
890 if (end < start)
891 continue;
892 if (end - start + 1 > max)
893 max = end - start + 1;
Keith Busch12e31292018-07-24 15:07:57 -0600894 }
895 release_free_pmem(nvdimm_bus, nd_mapping);
896 return max;
897}
898
899/**
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400900 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
901 * @nd_mapping: container of dpa-resource-root + labels
902 * @nd_region: constrain available space check to this reference region
903 * @overlap: calculate available space assuming this level of overlap
904 *
905 * Validate that a PMEM label, if present, aligns with the start of an
906 * interleave set and truncate the available size at the lowest BLK
907 * overlap point.
908 *
909 * The expectation is that this routine is called multiple times as it
910 * probes for the largest BLK encroachment for any single member DIMM of
911 * the interleave set. Once that value is determined the PMEM-limit for
912 * the set can be established.
913 */
914resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
915 struct nd_mapping *nd_mapping, resource_size_t *overlap)
916{
917 resource_size_t map_start, map_end, busy = 0, available, blk_start;
918 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
919 struct resource *res;
920 const char *reason;
Dan Williams2522afb2020-01-30 12:06:23 -0800921 unsigned long align;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400922
923 if (!ndd)
924 return 0;
925
Dan Williams2522afb2020-01-30 12:06:23 -0800926 align = dpa_align(nd_region);
927 if (!align)
928 return 0;
929
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400930 map_start = nd_mapping->start;
931 map_end = map_start + nd_mapping->size - 1;
932 blk_start = max(map_start, map_end + 1 - *overlap);
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700933 for_each_dpa_resource(ndd, res) {
Dan Williams2522afb2020-01-30 12:06:23 -0800934 resource_size_t start, end;
935
936 start = ALIGN_DOWN(res->start, align);
937 end = ALIGN(res->end + 1, align) - 1;
938 if (start >= map_start && start < map_end) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400939 if (strncmp(res->name, "blk", 3) == 0)
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700940 blk_start = min(blk_start,
Dan Williams2522afb2020-01-30 12:06:23 -0800941 max(map_start, start));
942 else if (end > map_end) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400943 reason = "misaligned to iset";
944 goto err;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700945 } else
Dan Williams2522afb2020-01-30 12:06:23 -0800946 busy += end - start + 1;
947 } else if (end >= map_start && end <= map_end) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400948 if (strncmp(res->name, "blk", 3) == 0) {
949 /*
950 * If a BLK allocation overlaps the start of
951 * PMEM the entire interleave set may now only
952 * be used for BLK.
953 */
954 blk_start = map_start;
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700955 } else
Dan Williams2522afb2020-01-30 12:06:23 -0800956 busy += end - start + 1;
957 } else if (map_start > start && map_start < end) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400958 /* total eclipse of the mapping */
959 busy += nd_mapping->size;
960 blk_start = map_start;
961 }
Dan Williamsa1f3e4d2016-09-30 17:28:58 -0700962 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400963
964 *overlap = map_end + 1 - blk_start;
965 available = blk_start - map_start;
966 if (busy < available)
Dan Williams2522afb2020-01-30 12:06:23 -0800967 return ALIGN_DOWN(available - busy, align);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400968 return 0;
969
970 err:
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400971 nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
972 return 0;
973}
974
Dan Williams4a826c82015-06-09 16:09:36 -0400975void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
976{
977 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
978 kfree(res->name);
979 __release_region(&ndd->dpa, res->start, resource_size(res));
980}
981
982struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
983 struct nd_label_id *label_id, resource_size_t start,
984 resource_size_t n)
985{
986 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
987 struct resource *res;
988
989 if (!name)
990 return NULL;
991
992 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
993 res = __request_region(&ndd->dpa, start, n, name, 0);
994 if (!res)
995 kfree(name);
996 return res;
997}
998
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400999/**
1000 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
1001 * @nvdimm: container of dpa-resource-root + labels
1002 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
1003 */
1004resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
1005 struct nd_label_id *label_id)
1006{
1007 resource_size_t allocated = 0;
1008 struct resource *res;
1009
1010 for_each_dpa_resource(ndd, res)
1011 if (strcmp(res->name, label_id->id) == 0)
1012 allocated += resource_size(res);
1013
1014 return allocated;
1015}
1016
Dan Williams4d88a972015-05-31 14:41:48 -04001017static int count_dimms(struct device *dev, void *c)
1018{
1019 int *count = c;
1020
1021 if (is_nvdimm(dev))
1022 (*count)++;
1023 return 0;
1024}
1025
1026int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
1027{
1028 int count = 0;
1029 /* Flush any possible dimm registration failures */
1030 nd_synchronize();
1031
1032 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
Dan Williams426824d2018-03-05 16:39:31 -08001033 dev_dbg(&nvdimm_bus->dev, "count: %d\n", count);
Dan Williams4d88a972015-05-31 14:41:48 -04001034 if (count != dimm_count)
1035 return -ENXIO;
1036 return 0;
1037}
1038EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
Dan Williamsb354aba2016-05-17 20:24:16 -07001039
1040void __exit nvdimm_devs_exit(void)
1041{
1042 ida_destroy(&dimm_ida);
1043}