dm writecache: fix incorrect flush sequence when doing SSD mode commit
[linux/fpc-iii.git] / drivers / nvdimm / dimm_devs.c
blob94ea6dba6b4ff8361ab3baeb74facac65fc9a797
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"
19 static DEFINE_IDA(dimm_ida);
21 static bool noblk;
22 module_param(noblk, bool, 0444);
23 MODULE_PARM_DESC(noblk, "force disable BLK / local alias support");
26 * Retrieve bus and dimm handle and return if this bus supports
27 * get_config_data commands
29 int nvdimm_check_config_data(struct device *dev)
31 struct nvdimm *nvdimm = to_nvdimm(dev);
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;
41 return 0;
44 static int validate_dimm(struct nvdimm_drvdata *ndd)
46 int rc;
48 if (!ndd)
49 return -EINVAL;
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;
58 /**
59 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
60 * @nvdimm: dimm to initialize
62 int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
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;
70 if (rc)
71 return rc;
73 if (cmd->config_size)
74 return 0; /* already valid */
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;
85 int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
86 size_t offset, size_t len)
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;
94 if (rc)
95 return rc;
97 if (offset + len > ndd->nsarea.config_size)
98 return -ENXIO;
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;
105 for (buf_offset = 0; len;
106 len -= cmd->in_length, buf_offset += cmd->in_length) {
107 size_t cmd_size;
109 cmd->in_offset = offset + buf_offset;
110 cmd->in_length = min(max_cmd_size, len);
112 cmd_size = sizeof(*cmd) + cmd->in_length;
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;
123 /* out_buf should be valid, copy it into our output buffer */
124 memcpy(buf + buf_offset, cmd->out_buf, cmd->in_length);
126 kvfree(cmd);
128 return rc;
131 int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
132 void *buf, size_t len)
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;
140 if (rc)
141 return rc;
143 if (offset + len > ndd->nsarea.config_size)
144 return -ENXIO;
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;
151 for (buf_offset = 0; len; len -= cmd->in_length,
152 buf_offset += cmd->in_length) {
153 size_t cmd_size;
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);
159 /* status is output in the last 4-bytes of the command buffer */
160 cmd_size = sizeof(*cmd) + cmd->in_length + sizeof(u32);
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;
171 kvfree(cmd);
173 return rc;
176 void nvdimm_set_aliasing(struct device *dev)
178 struct nvdimm *nvdimm = to_nvdimm(dev);
180 set_bit(NDD_ALIASING, &nvdimm->flags);
183 void nvdimm_set_locked(struct device *dev)
185 struct nvdimm *nvdimm = to_nvdimm(dev);
187 set_bit(NDD_LOCKED, &nvdimm->flags);
190 void nvdimm_clear_locked(struct device *dev)
192 struct nvdimm *nvdimm = to_nvdimm(dev);
194 clear_bit(NDD_LOCKED, &nvdimm->flags);
197 static void nvdimm_release(struct device *dev)
199 struct nvdimm *nvdimm = to_nvdimm(dev);
201 ida_simple_remove(&dimm_ida, nvdimm->id);
202 kfree(nvdimm);
205 struct nvdimm *to_nvdimm(struct device *dev)
207 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
209 WARN_ON(!is_nvdimm(dev));
210 return nvdimm;
212 EXPORT_SYMBOL_GPL(to_nvdimm);
214 struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr)
216 struct nd_region *nd_region = &ndbr->nd_region;
217 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
219 return nd_mapping->nvdimm;
221 EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm);
223 unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr)
225 /* pmem mapping properties are private to libnvdimm */
226 return ARCH_MEMREMAP_PMEM;
228 EXPORT_SYMBOL_GPL(nd_blk_memremap_flags);
230 struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
232 struct nvdimm *nvdimm = nd_mapping->nvdimm;
234 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
236 return dev_get_drvdata(&nvdimm->dev);
238 EXPORT_SYMBOL(to_ndd);
240 void nvdimm_drvdata_release(struct kref *kref)
242 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
243 struct device *dev = ndd->dev;
244 struct resource *res, *_r;
246 dev_dbg(dev, "trace\n");
247 nvdimm_bus_lock(dev);
248 for_each_dpa_resource_safe(ndd, res, _r)
249 nvdimm_free_dpa(ndd, res);
250 nvdimm_bus_unlock(dev);
252 kvfree(ndd->data);
253 kfree(ndd);
254 put_device(dev);
257 void get_ndd(struct nvdimm_drvdata *ndd)
259 kref_get(&ndd->kref);
262 void put_ndd(struct nvdimm_drvdata *ndd)
264 if (ndd)
265 kref_put(&ndd->kref, nvdimm_drvdata_release);
268 const char *nvdimm_name(struct nvdimm *nvdimm)
270 return dev_name(&nvdimm->dev);
272 EXPORT_SYMBOL_GPL(nvdimm_name);
274 struct kobject *nvdimm_kobj(struct nvdimm *nvdimm)
276 return &nvdimm->dev.kobj;
278 EXPORT_SYMBOL_GPL(nvdimm_kobj);
280 unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm)
282 return nvdimm->cmd_mask;
284 EXPORT_SYMBOL_GPL(nvdimm_cmd_mask);
286 void *nvdimm_provider_data(struct nvdimm *nvdimm)
288 if (nvdimm)
289 return nvdimm->provider_data;
290 return NULL;
292 EXPORT_SYMBOL_GPL(nvdimm_provider_data);
294 static ssize_t commands_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
297 struct nvdimm *nvdimm = to_nvdimm(dev);
298 int cmd, len = 0;
300 if (!nvdimm->cmd_mask)
301 return sprintf(buf, "\n");
303 for_each_set_bit(cmd, &nvdimm->cmd_mask, BITS_PER_LONG)
304 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
305 len += sprintf(buf + len, "\n");
306 return len;
308 static DEVICE_ATTR_RO(commands);
310 static ssize_t flags_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct nvdimm *nvdimm = to_nvdimm(dev);
315 return sprintf(buf, "%s%s\n",
316 test_bit(NDD_ALIASING, &nvdimm->flags) ? "alias " : "",
317 test_bit(NDD_LOCKED, &nvdimm->flags) ? "lock " : "");
319 static DEVICE_ATTR_RO(flags);
321 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
322 char *buf)
324 struct nvdimm *nvdimm = to_nvdimm(dev);
327 * The state may be in the process of changing, userspace should
328 * quiesce probing if it wants a static answer
330 nvdimm_bus_lock(dev);
331 nvdimm_bus_unlock(dev);
332 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
333 ? "active" : "idle");
335 static DEVICE_ATTR_RO(state);
337 static ssize_t available_slots_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
341 ssize_t rc;
342 u32 nfree;
344 if (!ndd)
345 return -ENXIO;
347 nvdimm_bus_lock(dev);
348 nfree = nd_label_nfree(ndd);
349 if (nfree - 1 > nfree) {
350 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
351 nfree = 0;
352 } else
353 nfree--;
354 rc = sprintf(buf, "%d\n", nfree);
355 nvdimm_bus_unlock(dev);
356 return rc;
358 static DEVICE_ATTR_RO(available_slots);
360 __weak ssize_t security_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
363 struct nvdimm *nvdimm = to_nvdimm(dev);
365 if (test_bit(NVDIMM_SECURITY_DISABLED, &nvdimm->sec.flags))
366 return sprintf(buf, "disabled\n");
367 if (test_bit(NVDIMM_SECURITY_UNLOCKED, &nvdimm->sec.flags))
368 return sprintf(buf, "unlocked\n");
369 if (test_bit(NVDIMM_SECURITY_LOCKED, &nvdimm->sec.flags))
370 return sprintf(buf, "locked\n");
371 if (test_bit(NVDIMM_SECURITY_OVERWRITE, &nvdimm->sec.flags))
372 return sprintf(buf, "overwrite\n");
373 return -ENOTTY;
376 static ssize_t frozen_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct nvdimm *nvdimm = to_nvdimm(dev);
381 return sprintf(buf, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN,
382 &nvdimm->sec.flags));
384 static DEVICE_ATTR_RO(frozen);
386 static ssize_t security_store(struct device *dev,
387 struct device_attribute *attr, const char *buf, size_t len)
390 ssize_t rc;
393 * Require all userspace triggered security management to be
394 * done while probing is idle and the DIMM is not in active use
395 * in any region.
397 nd_device_lock(dev);
398 nvdimm_bus_lock(dev);
399 wait_nvdimm_bus_probe_idle(dev);
400 rc = nvdimm_security_store(dev, buf, len);
401 nvdimm_bus_unlock(dev);
402 nd_device_unlock(dev);
404 return rc;
406 static DEVICE_ATTR_RW(security);
408 static struct attribute *nvdimm_attributes[] = {
409 &dev_attr_state.attr,
410 &dev_attr_flags.attr,
411 &dev_attr_commands.attr,
412 &dev_attr_available_slots.attr,
413 &dev_attr_security.attr,
414 &dev_attr_frozen.attr,
415 NULL,
418 static umode_t nvdimm_visible(struct kobject *kobj, struct attribute *a, int n)
420 struct device *dev = container_of(kobj, typeof(*dev), kobj);
421 struct nvdimm *nvdimm = to_nvdimm(dev);
423 if (a != &dev_attr_security.attr && a != &dev_attr_frozen.attr)
424 return a->mode;
425 if (!nvdimm->sec.flags)
426 return 0;
428 if (a == &dev_attr_security.attr) {
429 /* Are there any state mutation ops (make writable)? */
430 if (nvdimm->sec.ops->freeze || nvdimm->sec.ops->disable
431 || nvdimm->sec.ops->change_key
432 || nvdimm->sec.ops->erase
433 || nvdimm->sec.ops->overwrite)
434 return a->mode;
435 return 0444;
438 if (nvdimm->sec.ops->freeze)
439 return a->mode;
440 return 0;
443 static const struct attribute_group nvdimm_attribute_group = {
444 .attrs = nvdimm_attributes,
445 .is_visible = nvdimm_visible,
448 static const struct attribute_group *nvdimm_attribute_groups[] = {
449 &nd_device_attribute_group,
450 &nvdimm_attribute_group,
451 NULL,
454 static const struct device_type nvdimm_device_type = {
455 .name = "nvdimm",
456 .release = nvdimm_release,
457 .groups = nvdimm_attribute_groups,
460 bool is_nvdimm(struct device *dev)
462 return dev->type == &nvdimm_device_type;
465 struct 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,
468 struct resource *flush_wpq, const char *dimm_id,
469 const struct nvdimm_security_ops *sec_ops)
471 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
472 struct device *dev;
474 if (!nvdimm)
475 return NULL;
477 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
478 if (nvdimm->id < 0) {
479 kfree(nvdimm);
480 return NULL;
483 nvdimm->dimm_id = dimm_id;
484 nvdimm->provider_data = provider_data;
485 if (noblk)
486 flags |= 1 << NDD_NOBLK;
487 nvdimm->flags = flags;
488 nvdimm->cmd_mask = cmd_mask;
489 nvdimm->num_flush = num_flush;
490 nvdimm->flush_wpq = flush_wpq;
491 atomic_set(&nvdimm->busy, 0);
492 dev = &nvdimm->dev;
493 dev_set_name(dev, "nmem%d", nvdimm->id);
494 dev->parent = &nvdimm_bus->dev;
495 dev->type = &nvdimm_device_type;
496 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
497 dev->groups = groups;
498 nvdimm->sec.ops = sec_ops;
499 nvdimm->sec.overwrite_tmo = 0;
500 INIT_DELAYED_WORK(&nvdimm->dwork, nvdimm_security_overwrite_query);
502 * Security state must be initialized before device_add() for
503 * attribute visibility.
505 /* get security state and extended (master) state */
506 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
507 nvdimm->sec.ext_flags = nvdimm_security_flags(nvdimm, NVDIMM_MASTER);
508 nd_device_register(dev);
510 return nvdimm;
512 EXPORT_SYMBOL_GPL(__nvdimm_create);
514 static void shutdown_security_notify(void *data)
516 struct nvdimm *nvdimm = data;
518 sysfs_put(nvdimm->sec.overwrite_state);
521 int nvdimm_security_setup_events(struct device *dev)
523 struct nvdimm *nvdimm = to_nvdimm(dev);
525 if (!nvdimm->sec.flags || !nvdimm->sec.ops
526 || !nvdimm->sec.ops->overwrite)
527 return 0;
528 nvdimm->sec.overwrite_state = sysfs_get_dirent(dev->kobj.sd, "security");
529 if (!nvdimm->sec.overwrite_state)
530 return -ENOMEM;
532 return devm_add_action_or_reset(dev, shutdown_security_notify, nvdimm);
534 EXPORT_SYMBOL_GPL(nvdimm_security_setup_events);
536 int nvdimm_in_overwrite(struct nvdimm *nvdimm)
538 return test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags);
540 EXPORT_SYMBOL_GPL(nvdimm_in_overwrite);
542 int nvdimm_security_freeze(struct nvdimm *nvdimm)
544 int rc;
546 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
548 if (!nvdimm->sec.ops || !nvdimm->sec.ops->freeze)
549 return -EOPNOTSUPP;
551 if (!nvdimm->sec.flags)
552 return -EIO;
554 if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
555 dev_warn(&nvdimm->dev, "Overwrite operation in progress.\n");
556 return -EBUSY;
559 rc = nvdimm->sec.ops->freeze(nvdimm);
560 nvdimm->sec.flags = nvdimm_security_flags(nvdimm, NVDIMM_USER);
562 return rc;
565 int alias_dpa_busy(struct device *dev, void *data)
567 resource_size_t map_end, blk_start, new;
568 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;
575 if (!is_memory(dev))
576 return 0;
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;
585 if (i >= nd_region->ndr_mappings)
586 return 0;
588 ndd = to_ndd(nd_mapping);
589 map_end = nd_mapping->start + nd_mapping->size - 1;
590 blk_start = nd_mapping->start;
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).
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;
605 retry:
607 * Find the free dpa from the end of the last pmem allocation to
608 * the end of the interleave-set mapping.
610 for_each_dpa_resource(ndd, res) {
611 if (strncmp(res->name, "pmem", 4) != 0)
612 continue;
613 if ((res->start >= blk_start && res->start < map_end)
614 || (res->end >= blk_start
615 && res->end <= map_end)) {
616 new = max(blk_start, min(map_end + 1, res->end + 1));
617 if (new != blk_start) {
618 blk_start = new;
619 goto retry;
624 /* 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;
632 info->available -= blk_start - nd_mapping->start;
634 return 0;
638 * nd_blk_available_dpa - account the unused dpa of BLK region
639 * @nd_mapping: container of dpa-resource-root + labels
641 * 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.
645 resource_size_t nd_blk_available_dpa(struct nd_region *nd_region)
647 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
648 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
649 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
650 struct blk_alloc_info info = {
651 .nd_mapping = nd_mapping,
652 .available = nd_mapping->size,
653 .res = NULL,
655 struct resource *res;
657 if (!ndd)
658 return 0;
660 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
662 /* 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;
666 info.available -= resource_size(res);
669 return info.available;
673 * 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
678 resource_size_t nd_pmem_max_contiguous_dpa(struct nd_region *nd_region,
679 struct nd_mapping *nd_mapping)
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;
686 /* if a dimm is disabled the available capacity is zero */
687 if (!ndd)
688 return 0;
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);
699 release_free_pmem(nvdimm_bus, nd_mapping);
700 return max;
704 * 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
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.
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.
718 resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
719 struct nd_mapping *nd_mapping, resource_size_t *overlap)
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;
726 if (!ndd)
727 return 0;
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);
732 for_each_dpa_resource(ndd, res) {
733 if (res->start >= map_start && res->start < map_end) {
734 if (strncmp(res->name, "blk", 3) == 0)
735 blk_start = min(blk_start,
736 max(map_start, res->start));
737 else if (res->end > map_end) {
738 reason = "misaligned to iset";
739 goto err;
740 } else
741 busy += resource_size(res);
742 } else if (res->end >= map_start && res->end <= map_end) {
743 if (strncmp(res->name, "blk", 3) == 0) {
745 * If a BLK allocation overlaps the start of
746 * PMEM the entire interleave set may now only
747 * be used for BLK.
749 blk_start = map_start;
750 } else
751 busy += resource_size(res);
752 } 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;
759 *overlap = map_end + 1 - blk_start;
760 available = blk_start - map_start;
761 if (busy < available)
762 return available - busy;
763 return 0;
765 err:
766 nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
767 return 0;
770 void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
772 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
773 kfree(res->name);
774 __release_region(&ndd->dpa, res->start, resource_size(res));
777 struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
778 struct nd_label_id *label_id, resource_size_t start,
779 resource_size_t n)
781 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
782 struct resource *res;
784 if (!name)
785 return NULL;
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;
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>
799 resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
800 struct nd_label_id *label_id)
802 resource_size_t allocated = 0;
803 struct resource *res;
805 for_each_dpa_resource(ndd, res)
806 if (strcmp(res->name, label_id->id) == 0)
807 allocated += resource_size(res);
809 return allocated;
812 static int count_dimms(struct device *dev, void *c)
814 int *count = c;
816 if (is_nvdimm(dev))
817 (*count)++;
818 return 0;
821 int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
823 int count = 0;
824 /* Flush any possible dimm registration failures */
825 nd_synchronize();
827 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
828 dev_dbg(&nvdimm_bus->dev, "count: %d\n", count);
829 if (count != dimm_count)
830 return -ENXIO;
831 return 0;
833 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
835 void __exit nvdimm_devs_exit(void)
837 ida_destroy(&dimm_ida);