GNU Linux-libre 5.4.241-gnu1
[releases.git] / drivers / nvdimm / dimm_devs.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/moduleparam.h>
7 #include <linux/vmalloc.h>
8 #include <linux/device.h>
9 #include <linux/ndctl.h>
10 #include <linux/slab.h>
11 #include <linux/io.h>
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include "nd-core.h"
15 #include "label.h"
16 #include "pmem.h"
17 #include "nd.h"
18
19 static DEFINE_IDA(dimm_ida);
20
21 static bool noblk;
22 module_param(noblk, bool, 0444);
23 MODULE_PARM_DESC(noblk, "force disable BLK / local alias support");
24
25 /*
26  * Retrieve bus and dimm handle and return if this bus supports
27  * get_config_data commands
28  */
29 int nvdimm_check_config_data(struct device *dev)
30 {
31         struct nvdimm *nvdimm = to_nvdimm(dev);
32
33         if (!nvdimm->cmd_mask ||
34             !test_bit(ND_CMD_GET_CONFIG_DATA, &nvdimm->cmd_mask)) {
35                 if (test_bit(NDD_ALIASING, &nvdimm->flags))
36                         return -ENXIO;
37                 else
38                         return -ENOTTY;
39         }
40
41         return 0;
42 }
43
44 static int validate_dimm(struct nvdimm_drvdata *ndd)
45 {
46         int rc;
47
48         if (!ndd)
49                 return -EINVAL;
50
51         rc = nvdimm_check_config_data(ndd->dev);
52         if (rc)
53                 dev_dbg(ndd->dev, "%ps: %s error: %d\n",
54                                 __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  */
62 int 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);
68         int cmd_rc = 0;
69
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;
78         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;
83 }
84
85 int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
86                            size_t offset, size_t len)
87 {
88         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
89         struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
90         int rc = validate_dimm(ndd), cmd_rc = 0;
91         struct nd_cmd_get_config_data_hdr *cmd;
92         size_t max_cmd_size, buf_offset;
93
94         if (rc)
95                 return rc;
96
97         if (offset + len > ndd->nsarea.config_size)
98                 return -ENXIO;
99
100         max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
101         cmd = kvzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
102         if (!cmd)
103                 return -ENOMEM;
104
105         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
114                 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
115                                 ND_CMD_GET_CONFIG_DATA, cmd, cmd_size, &cmd_rc);
116                 if (rc < 0)
117                         break;
118                 if (cmd_rc < 0) {
119                         rc = cmd_rc;
120                         break;
121                 }
122
123                 /* out_buf should be valid, copy it into our output buffer */
124                 memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length);
125         }
126         kvfree(cmd);
127
128         return rc;
129 }
130
131 int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
132                 void *buf, size_t len)
133 {
134         size_t max_cmd_size, buf_offset;
135         struct nd_cmd_set_config_hdr *cmd;
136         int rc = validate_dimm(ndd), cmd_rc = 0;
137         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
143         if (offset + len > ndd->nsarea.config_size)
144                 return -ENXIO;
145
146         max_cmd_size = min_t(u32, len, ndd->nsarea.max_xfer);
147         cmd = kvzalloc(max_cmd_size + sizeof(*cmd) + sizeof(u32), GFP_KERNEL);
148         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;
154
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);
161
162                 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
163                                 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;
168                         break;
169                 }
170         }
171         kvfree(cmd);
172
173         return rc;
174 }
175
176 void nvdimm_set_aliasing(struct device *dev)
177 {
178         struct nvdimm *nvdimm = to_nvdimm(dev);
179
180         set_bit(NDD_ALIASING, &nvdimm->flags);
181 }
182
183 void nvdimm_set_locked(struct device *dev)
184 {
185         struct nvdimm *nvdimm = to_nvdimm(dev);
186
187         set_bit(NDD_LOCKED, &nvdimm->flags);
188 }
189
190 void nvdimm_clear_locked(struct device *dev)
191 {
192         struct nvdimm *nvdimm = to_nvdimm(dev);
193
194         clear_bit(NDD_LOCKED, &nvdimm->flags);
195 }
196
197 static 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
205 static struct device_type nvdimm_device_type = {
206         .name = "nvdimm",
207         .release = nvdimm_release,
208 };
209
210 bool is_nvdimm(struct device *dev)
211 {
212         return dev->type == &nvdimm_device_type;
213 }
214
215 struct nvdimm *to_nvdimm(struct device *dev)
216 {
217         struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
218
219         WARN_ON(!is_nvdimm(dev));
220         return nvdimm;
221 }
222 EXPORT_SYMBOL_GPL(to_nvdimm);
223
224 struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
225 {
226         struct nd_region *nd_region = &ndbr->nd_region;
227         struct nd_mapping *nd_mapping = &nd_region->mapping[0];
228
229         return nd_mapping->nvdimm;
230 }
231 EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
232
233 unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr)
234 {
235         /* pmem mapping properties are private to libnvdimm */
236         return ARCH_MEMREMAP_PMEM;
237 }
238 EXPORT_SYMBOL_GPL(nd_blk_memremap_flags);
239
240 struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
241 {
242         struct nvdimm *nvdimm = nd_mapping->nvdimm;
243
244         WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
245
246         return dev_get_drvdata(&nvdimm->dev);
247 }
248 EXPORT_SYMBOL(to_ndd);
249
250 void nvdimm_drvdata_release(struct kref *kref)
251 {
252         struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
253         struct device *dev = ndd->dev;
254         struct resource *res, *_r;
255
256         dev_dbg(dev, "trace\n");
257         nvdimm_bus_lock(dev);
258         for_each_dpa_resource_safe(ndd, res, _r)
259                 nvdimm_free_dpa(ndd, res);
260         nvdimm_bus_unlock(dev);
261
262         kvfree(ndd->data);
263         kfree(ndd);
264         put_device(dev);
265 }
266
267 void get_ndd(struct nvdimm_drvdata *ndd)
268 {
269         kref_get(&ndd->kref);
270 }
271
272 void put_ndd(struct nvdimm_drvdata *ndd)
273 {
274         if (ndd)
275                 kref_put(&ndd->kref, nvdimm_drvdata_release);
276 }
277
278 const char *nvdimm_name(struct nvdimm *nvdimm)
279 {
280         return dev_name(&nvdimm->dev);
281 }
282 EXPORT_SYMBOL_GPL(nvdimm_name);
283
284 struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
285 {
286         return &nvdimm->dev.kobj;
287 }
288 EXPORT_SYMBOL_GPL(nvdimm_kobj);
289
290 unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
291 {
292         return nvdimm->cmd_mask;
293 }
294 EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
295
296 void *nvdimm_provider_data(struct nvdimm *nvdimm)
297 {
298         if (nvdimm)
299                 return nvdimm->provider_data;
300         return NULL;
301 }
302 EXPORT_SYMBOL_GPL(nvdimm_provider_data);
303
304 static ssize_t commands_show(struct device *dev,
305                 struct device_attribute *attr, char *buf)
306 {
307         struct nvdimm *nvdimm = to_nvdimm(dev);
308         int cmd, len = 0;
309
310         if (!nvdimm->cmd_mask)
311                 return sprintf(buf, "\n");
312
313         for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
314                 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
315         len += sprintf(buf + len, "\n");
316         return len;
317 }
318 static DEVICE_ATTR_RO(commands);
319
320 static ssize_t flags_show(struct device *dev,
321                 struct device_attribute *attr, char *buf)
322 {
323         struct nvdimm *nvdimm = to_nvdimm(dev);
324
325         return sprintf(buf, "%s%s\n",
326                         test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "",
327                         test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
328 }
329 static DEVICE_ATTR_RO(flags);
330
331 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
332                 char *buf)
333 {
334         struct nvdimm *nvdimm = to_nvdimm(dev);
335
336         /*
337          * The state may be in the process of changing, userspace should
338          * quiesce probing if it wants a static answer
339          */
340         nvdimm_bus_lock(dev);
341         nvdimm_bus_unlock(dev);
342         return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
343                         ? "active" : "idle");
344 }
345 static DEVICE_ATTR_RO(state);
346
347 static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf)
348 {
349         struct device *dev;
350         ssize_t rc;
351         u32 nfree;
352
353         if (!ndd)
354                 return -ENXIO;
355
356         dev = ndd->dev;
357         nvdimm_bus_lock(dev);
358         nfree = nd_label_nfree(ndd);
359         if (nfree - 1 > nfree) {
360                 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
361                 nfree = 0;
362         } else
363                 nfree--;
364         rc = sprintf(buf, "%d\n", nfree);
365         nvdimm_bus_unlock(dev);
366         return rc;
367 }
368
369 static ssize_t available_slots_show(struct device *dev,
370                                     struct device_attribute *attr, char *buf)
371 {
372         ssize_t rc;
373
374         nd_device_lock(dev);
375         rc = __available_slots_show(dev_get_drvdata(dev), buf);
376         nd_device_unlock(dev);
377
378         return rc;
379 }
380 static DEVICE_ATTR_RO(available_slots);
381
382 __weak ssize_t security_show(struct device *dev,
383                 struct device_attribute *attr, char *buf)
384 {
385         struct nvdimm *nvdimm = to_nvdimm(dev);
386
387         if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
388                 return sprintf(buf, "disabled\n");
389         if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
390                 return sprintf(buf, "unlocked\n");
391         if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
392                 return sprintf(buf, "locked\n");
393         if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
394                 return sprintf(buf, "overwrite\n");
395         return -ENOTTY;
396 }
397
398 static ssize_t frozen_show(struct device *dev,
399                 struct device_attribute *attr, char *buf)
400 {
401         struct nvdimm *nvdimm = to_nvdimm(dev);
402
403         return sprintf(buf, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN,
404                                 &nvdimm->sec.flags));
405 }
406 static DEVICE_ATTR_RO(frozen);
407
408 static ssize_t security_store(struct device *dev,
409                 struct device_attribute *attr, const char *buf, size_t len)
410
411 {
412         ssize_t rc;
413
414         /*
415          * Require all userspace triggered security management to be
416          * done while probing is idle and the DIMM is not in active use
417          * in any region.
418          */
419         nd_device_lock(dev);
420         nvdimm_bus_lock(dev);
421         wait_nvdimm_bus_probe_idle(dev);
422         rc = nvdimm_security_store(dev, buf, len);
423         nvdimm_bus_unlock(dev);
424         nd_device_unlock(dev);
425
426         return rc;
427 }
428 static DEVICE_ATTR_RW(security);
429
430 static struct attribute *nvdimm_attributes[] = {
431         &dev_attr_state.attr,
432         &dev_attr_flags.attr,
433         &dev_attr_commands.attr,
434         &dev_attr_available_slots.attr,
435         &dev_attr_security.attr,
436         &dev_attr_frozen.attr,
437         NULL,
438 };
439
440 static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
441 {
442         struct device *dev = container_of(kobj, typeof(*dev), kobj);
443         struct nvdimm *nvdimm = to_nvdimm(dev);
444
445         if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
446                 return a->mode;
447         if (!nvdimm->sec.flags)
448                 return 0;
449
450         if (a == &dev_attr_security.attr) {
451                 /* Are there any state mutation ops (make writable)? */
452                 if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
453                                 || nvdimm->sec.ops->change_key
454                                 || nvdimm->sec.ops->erase
455                                 || nvdimm->sec.ops->overwrite)
456                         return a->mode;
457                 return 0444;
458         }
459
460         if (nvdimm->sec.ops->freeze)
461                 return a->mode;
462         return 0;
463 }
464
465 struct attribute_group nvdimm_attribute_group = {
466         .attrs = nvdimm_attributes,
467         .is_visible = nvdimm_visible,
468 };
469 EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
470
471 struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
472                 void *provider_data, const struct attribute_group **groups,
473                 unsigned long flags, unsigned long cmd_mask, int num_flush,
474                 struct resource *flush_wpq, const char *dimm_id,
475                 const struct nvdimm_security_ops *sec_ops)
476 {
477         struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
478         struct device *dev;
479
480         if (!nvdimm)
481                 return NULL;
482
483         nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
484         if (nvdimm->id < 0) {
485                 kfree(nvdimm);
486                 return NULL;
487         }
488
489         nvdimm->dimm_id = dimm_id;
490         nvdimm->provider_data = provider_data;
491         if (noblk)
492                 flags |= 1 << NDD_NOBLK;
493         nvdimm->flags = flags;
494         nvdimm->cmd_mask = cmd_mask;
495         nvdimm->num_flush = num_flush;
496         nvdimm->flush_wpq = flush_wpq;
497         atomic_set(&nvdimm->busy, 0);
498         dev = &nvdimm->dev;
499         dev_set_name(dev, "nmem%d", nvdimm->id);
500         dev->parent = &nvdimm_bus->dev;
501         dev->type = &nvdimm_device_type;
502         dev->devt = MKDEV(nvdimm_major, nvdimm->id);
503         dev->groups = groups;
504         nvdimm->sec.ops = sec_ops;
505         nvdimm->sec.overwrite_tmo = 0;
506         INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
507         /*
508          * Security state must be initialized before device_add() for
509          * attribute visibility.
510          */
511         /* get security state and extended (master) state */
512         nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
513         nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
514         nd_device_register(dev);
515
516         return nvdimm;
517 }
518 EXPORT_SYMBOL_GPL(__nvdimm_create);
519
520 static void shutdown_security_notify(void *data)
521 {
522         struct nvdimm *nvdimm = data;
523
524         sysfs_put(nvdimm->sec.overwrite_state);
525 }
526
527 int nvdimm_security_setup_events(struct device *dev)
528 {
529         struct nvdimm *nvdimm = to_nvdimm(dev);
530
531         if (!nvdimm->sec.flags || !nvdimm->sec.ops
532                         || !nvdimm->sec.ops->overwrite)
533                 return 0;
534         nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
535         if (!nvdimm->sec.overwrite_state)
536                 return -ENOMEM;
537
538         return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
539 }
540 EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
541
542 int nvdimm_in_overwrite(struct nvdimm *nvdimm)
543 {
544         return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
545 }
546 EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
547
548 int nvdimm_security_freeze(struct nvdimm *nvdimm)
549 {
550         int rc;
551
552         WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
553
554         if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
555                 return -EOPNOTSUPP;
556
557         if (!nvdimm->sec.flags)
558                 return -EIO;
559
560         if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
561                 dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n");
562                 return -EBUSY;
563         }
564
565         rc = nvdimm->sec.ops->freeze(nvdimm);
566         nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
567
568         return rc;
569 }
570
571 int alias_dpa_busy(struct device *dev, void *data)
572 {
573         resource_size_t map_end, blk_start, new;
574         struct blk_alloc_info *info = data;
575         struct nd_mapping *nd_mapping;
576         struct nd_region *nd_region;
577         struct nvdimm_drvdata *ndd;
578         struct resource *res;
579         int i;
580
581         if (!is_memory(dev))
582                 return 0;
583
584         nd_region = to_nd_region(dev);
585         for (i = 0; i < nd_region->ndr_mappings; i++) {
586                 nd_mapping  = &nd_region->mapping[i];
587                 if (nd_mapping->nvdimm == info->nd_mapping->nvdimm)
588                         break;
589         }
590
591         if (i >= nd_region->ndr_mappings)
592                 return 0;
593
594         ndd = to_ndd(nd_mapping);
595         map_end = nd_mapping->start + nd_mapping->size - 1;
596         blk_start = nd_mapping->start;
597
598         /*
599          * In the allocation case ->res is set to free space that we are
600          * looking to validate against PMEM aliasing collision rules
601          * (i.e. BLK is allocated after all aliased PMEM).
602          */
603         if (info->res) {
604                 if (info->res->start >= nd_mapping->start
605                                 && info->res->start < map_end)
606                         /* pass */;
607                 else
608                         return 0;
609         }
610
611  retry:
612         /*
613          * Find the free dpa from the end of the last pmem allocation to
614          * the end of the interleave-set mapping.
615          */
616         for_each_dpa_resource(ndd, res) {
617                 if (strncmp(res->name, "pmem", 4) != 0)
618                         continue;
619                 if ((res->start >= blk_start && res->start < map_end)
620                                 || (res->end >= blk_start
621                                         && res->end <= map_end)) {
622                         new = max(blk_start, min(map_end + 1, res->end + 1));
623                         if (new != blk_start) {
624                                 blk_start = new;
625                                 goto retry;
626                         }
627                 }
628         }
629
630         /* update the free space range with the probed blk_start */
631         if (info->res && blk_start > info->res->start) {
632                 info->res->start = max(info->res->start, blk_start);
633                 if (info->res->start > info->res->end)
634                         info->res->end = info->res->start - 1;
635                 return 1;
636         }
637
638         info->available -= blk_start - nd_mapping->start;
639
640         return 0;
641 }
642
643 /**
644  * nd_blk_available_dpa - account the unused dpa of BLK region
645  * @nd_mapping: container of dpa-resource-root + labels
646  *
647  * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
648  * we arrange for them to never start at an lower dpa than the last
649  * PMEM allocation in an aliased region.
650  */
651 resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
652 {
653         struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
654         struct nd_mapping *nd_mapping = &nd_region->mapping[0];
655         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
656         struct blk_alloc_info info = {
657                 .nd_mapping = nd_mapping,
658                 .available = nd_mapping->size,
659                 .res = NULL,
660         };
661         struct resource *res;
662
663         if (!ndd)
664                 return 0;
665
666         device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
667
668         /* now account for busy blk allocations in unaliased dpa */
669         for_each_dpa_resource(ndd, res) {
670                 if (strncmp(res->name, "blk", 3) != 0)
671                         continue;
672                 info.available -= resource_size(res);
673         }
674
675         return info.available;
676 }
677
678 /**
679  * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
680  *                         contiguous unallocated dpa range.
681  * @nd_region: constrain available space check to this reference region
682  * @nd_mapping: container of dpa-resource-root + labels
683  */
684 resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
685                                            struct nd_mapping *nd_mapping)
686 {
687         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
688         struct nvdimm_bus *nvdimm_bus;
689         resource_size_t max = 0;
690         struct resource *res;
691
692         /* if a dimm is disabled the available capacity is zero */
693         if (!ndd)
694                 return 0;
695
696         nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
697         if (__reserve_free_pmem(&nd_region->dev, nd_mapping->nvdimm))
698                 return 0;
699         for_each_dpa_resource(ndd, res) {
700                 if (strcmp(res->name, "pmem-reserve") != 0)
701                         continue;
702                 if (resource_size(res) > max)
703                         max = resource_size(res);
704         }
705         release_free_pmem(nvdimm_bus, nd_mapping);
706         return max;
707 }
708
709 /**
710  * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
711  * @nd_mapping: container of dpa-resource-root + labels
712  * @nd_region: constrain available space check to this reference region
713  * @overlap: calculate available space assuming this level of overlap
714  *
715  * Validate that a PMEM label, if present, aligns with the start of an
716  * interleave set and truncate the available size at the lowest BLK
717  * overlap point.
718  *
719  * The expectation is that this routine is called multiple times as it
720  * probes for the largest BLK encroachment for any single member DIMM of
721  * the interleave set.  Once that value is determined the PMEM-limit for
722  * the set can be established.
723  */
724 resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
725                 struct nd_mapping *nd_mapping, resource_size_t *overlap)
726 {
727         resource_size_t map_start, map_end, busy = 0, available, blk_start;
728         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
729         struct resource *res;
730         const char *reason;
731
732         if (!ndd)
733                 return 0;
734
735         map_start = nd_mapping->start;
736         map_end = map_start + nd_mapping->size - 1;
737         blk_start = max(map_start, map_end + 1 - *overlap);
738         for_each_dpa_resource(ndd, res) {
739                 if (res->start >= map_start && res->start < map_end) {
740                         if (strncmp(res->name, "blk", 3) == 0)
741                                 blk_start = min(blk_start,
742                                                 max(map_start, res->start));
743                         else if (res->end > map_end) {
744                                 reason = "misaligned to iset";
745                                 goto err;
746                         } else
747                                 busy += resource_size(res);
748                 } else if (res->end >= map_start && res->end <= map_end) {
749                         if (strncmp(res->name, "blk", 3) == 0) {
750                                 /*
751                                  * If a BLK allocation overlaps the start of
752                                  * PMEM the entire interleave set may now only
753                                  * be used for BLK.
754                                  */
755                                 blk_start = map_start;
756                         } else
757                                 busy += resource_size(res);
758                 } else if (map_start > res->start && map_start < res->end) {
759                         /* total eclipse of the mapping */
760                         busy += nd_mapping->size;
761                         blk_start = map_start;
762                 }
763         }
764
765         *overlap = map_end + 1 - blk_start;
766         available = blk_start - map_start;
767         if (busy < available)
768                 return available - busy;
769         return 0;
770
771  err:
772         nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
773         return 0;
774 }
775
776 void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
777 {
778         WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
779         kfree(res->name);
780         __release_region(&ndd->dpa, res->start, resource_size(res));
781 }
782
783 struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
784                 struct nd_label_id *label_id, resource_size_t start,
785                 resource_size_t n)
786 {
787         char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
788         struct resource *res;
789
790         if (!name)
791                 return NULL;
792
793         WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
794         res = __request_region(&ndd->dpa, start, n, name, 0);
795         if (!res)
796                 kfree(name);
797         return res;
798 }
799
800 /**
801  * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
802  * @nvdimm: container of dpa-resource-root + labels
803  * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
804  */
805 resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
806                 struct nd_label_id *label_id)
807 {
808         resource_size_t allocated = 0;
809         struct resource *res;
810
811         for_each_dpa_resource(ndd, res)
812                 if (strcmp(res->name, label_id->id) == 0)
813                         allocated += resource_size(res);
814
815         return allocated;
816 }
817
818 static int count_dimms(struct device *dev, void *c)
819 {
820         int *count = c;
821
822         if (is_nvdimm(dev))
823                 (*count)++;
824         return 0;
825 }
826
827 int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
828 {
829         int count = 0;
830         /* Flush any possible dimm registration failures */
831         nd_synchronize();
832
833         device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
834         dev_dbg(&nvdimm_bus->dev, "count: %d\n", count);
835         if (count != dimm_count)
836                 return -ENXIO;
837         return 0;
838 }
839 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
840
841 void __exit nvdimm_devs_exit(void)
842 {
843         ida_destroy(&dimm_ida);
844 }