1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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>
19 static DEFINE_IDA(dimm_ida
);
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_LABELING
, &nvdimm
->flags
))
44 static int validate_dimm(struct nvdimm_drvdata
*ndd
)
51 rc
= nvdimm_check_config_data(ndd
->dev
);
53 dev_dbg(ndd
->dev
, "%ps: %s error: %d\n",
54 __builtin_return_address(0), __func__
, rc
);
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
);
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
);
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
;
97 if (offset
+ len
> ndd
->nsarea
.config_size
)
100 max_cmd_size
= min_t(u32
, len
, ndd
->nsarea
.max_xfer
);
101 cmd
= kvzalloc(max_cmd_size
+ sizeof(*cmd
), GFP_KERNEL
);
105 for (buf_offset
= 0; len
;
106 len
-= cmd
->in_length
, buf_offset
+= cmd
->in_length
) {
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
);
123 /* out_buf should be valid, copy it into our output buffer */
124 memcpy(buf
+ buf_offset
, cmd
->out_buf
, cmd
->in_length
);
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
;
143 if (offset
+ len
> ndd
->nsarea
.config_size
)
146 max_cmd_size
= min_t(u32
, len
, ndd
->nsarea
.max_xfer
);
147 cmd
= kvzalloc(max_cmd_size
+ sizeof(*cmd
) + sizeof(u32
), GFP_KERNEL
);
151 for (buf_offset
= 0; len
; len
-= cmd
->in_length
,
152 buf_offset
+= cmd
->in_length
) {
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
);
176 void nvdimm_set_labeling(struct device
*dev
)
178 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
180 set_bit(NDD_LABELING
, &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
);
205 struct nvdimm
*to_nvdimm(struct device
*dev
)
207 struct nvdimm
*nvdimm
= container_of(dev
, struct nvdimm
, dev
);
209 WARN_ON(!is_nvdimm(dev
));
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
);
257 void get_ndd(struct nvdimm_drvdata
*ndd
)
259 kref_get(&ndd
->kref
);
262 void put_ndd(struct nvdimm_drvdata
*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
)
289 return nvdimm
->provider_data
;
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
);
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");
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%s\n",
316 test_bit(NDD_ALIASING
, &nvdimm
->flags
) ? "alias " : "",
317 test_bit(NDD_LABELING
, &nvdimm
->flags
) ? "label " : "",
318 test_bit(NDD_LOCKED
, &nvdimm
->flags
) ? "lock " : "");
320 static DEVICE_ATTR_RO(flags
);
322 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
325 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
328 * The state may be in the process of changing, userspace should
329 * quiesce probing if it wants a static answer
331 nvdimm_bus_lock(dev
);
332 nvdimm_bus_unlock(dev
);
333 return sprintf(buf
, "%s\n", atomic_read(&nvdimm
->busy
)
334 ? "active" : "idle");
336 static DEVICE_ATTR_RO(state
);
338 static ssize_t
available_slots_show(struct device
*dev
,
339 struct device_attribute
*attr
, char *buf
)
341 struct nvdimm_drvdata
*ndd
= dev_get_drvdata(dev
);
348 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");
355 rc
= sprintf(buf
, "%d\n", nfree
);
356 nvdimm_bus_unlock(dev
);
359 static DEVICE_ATTR_RO(available_slots
);
361 __weak ssize_t
security_show(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
366 if (test_bit(NVDIMM_SECURITY_DISABLED
, &nvdimm
->sec
.flags
))
367 return sprintf(buf
, "disabled\n");
368 if (test_bit(NVDIMM_SECURITY_UNLOCKED
, &nvdimm
->sec
.flags
))
369 return sprintf(buf
, "unlocked\n");
370 if (test_bit(NVDIMM_SECURITY_LOCKED
, &nvdimm
->sec
.flags
))
371 return sprintf(buf
, "locked\n");
372 if (test_bit(NVDIMM_SECURITY_OVERWRITE
, &nvdimm
->sec
.flags
))
373 return sprintf(buf
, "overwrite\n");
377 static ssize_t
frozen_show(struct device
*dev
,
378 struct device_attribute
*attr
, char *buf
)
380 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
382 return sprintf(buf
, "%d\n", test_bit(NVDIMM_SECURITY_FROZEN
,
383 &nvdimm
->sec
.flags
));
385 static DEVICE_ATTR_RO(frozen
);
387 static ssize_t
security_store(struct device
*dev
,
388 struct device_attribute
*attr
, const char *buf
, size_t len
)
394 * Require all userspace triggered security management to be
395 * done while probing is idle and the DIMM is not in active use
399 nvdimm_bus_lock(dev
);
400 wait_nvdimm_bus_probe_idle(dev
);
401 rc
= nvdimm_security_store(dev
, buf
, len
);
402 nvdimm_bus_unlock(dev
);
403 nd_device_unlock(dev
);
407 static DEVICE_ATTR_RW(security
);
409 static struct attribute
*nvdimm_attributes
[] = {
410 &dev_attr_state
.attr
,
411 &dev_attr_flags
.attr
,
412 &dev_attr_commands
.attr
,
413 &dev_attr_available_slots
.attr
,
414 &dev_attr_security
.attr
,
415 &dev_attr_frozen
.attr
,
419 static umode_t
nvdimm_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
421 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
422 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
424 if (a
!= &dev_attr_security
.attr
&& a
!= &dev_attr_frozen
.attr
)
426 if (!nvdimm
->sec
.flags
)
429 if (a
== &dev_attr_security
.attr
) {
430 /* Are there any state mutation ops (make writable)? */
431 if (nvdimm
->sec
.ops
->freeze
|| nvdimm
->sec
.ops
->disable
432 || nvdimm
->sec
.ops
->change_key
433 || nvdimm
->sec
.ops
->erase
434 || nvdimm
->sec
.ops
->overwrite
)
439 if (nvdimm
->sec
.ops
->freeze
)
444 static const struct attribute_group nvdimm_attribute_group
= {
445 .attrs
= nvdimm_attributes
,
446 .is_visible
= nvdimm_visible
,
449 static const struct attribute_group
*nvdimm_attribute_groups
[] = {
450 &nd_device_attribute_group
,
451 &nvdimm_attribute_group
,
455 static const struct device_type nvdimm_device_type
= {
457 .release
= nvdimm_release
,
458 .groups
= nvdimm_attribute_groups
,
461 bool is_nvdimm(struct device
*dev
)
463 return dev
->type
== &nvdimm_device_type
;
466 struct nvdimm
*__nvdimm_create(struct nvdimm_bus
*nvdimm_bus
,
467 void *provider_data
, const struct attribute_group
**groups
,
468 unsigned long flags
, unsigned long cmd_mask
, int num_flush
,
469 struct resource
*flush_wpq
, const char *dimm_id
,
470 const struct nvdimm_security_ops
*sec_ops
)
472 struct nvdimm
*nvdimm
= kzalloc(sizeof(*nvdimm
), GFP_KERNEL
);
478 nvdimm
->id
= ida_simple_get(&dimm_ida
, 0, 0, GFP_KERNEL
);
479 if (nvdimm
->id
< 0) {
484 nvdimm
->dimm_id
= dimm_id
;
485 nvdimm
->provider_data
= provider_data
;
487 flags
|= 1 << NDD_NOBLK
;
488 nvdimm
->flags
= flags
;
489 nvdimm
->cmd_mask
= cmd_mask
;
490 nvdimm
->num_flush
= num_flush
;
491 nvdimm
->flush_wpq
= flush_wpq
;
492 atomic_set(&nvdimm
->busy
, 0);
494 dev_set_name(dev
, "nmem%d", nvdimm
->id
);
495 dev
->parent
= &nvdimm_bus
->dev
;
496 dev
->type
= &nvdimm_device_type
;
497 dev
->devt
= MKDEV(nvdimm_major
, nvdimm
->id
);
498 dev
->groups
= groups
;
499 nvdimm
->sec
.ops
= sec_ops
;
500 nvdimm
->sec
.overwrite_tmo
= 0;
501 INIT_DELAYED_WORK(&nvdimm
->dwork
, nvdimm_security_overwrite_query
);
503 * Security state must be initialized before device_add() for
504 * attribute visibility.
506 /* get security state and extended (master) state */
507 nvdimm
->sec
.flags
= nvdimm_security_flags(nvdimm
, NVDIMM_USER
);
508 nvdimm
->sec
.ext_flags
= nvdimm_security_flags(nvdimm
, NVDIMM_MASTER
);
509 nd_device_register(dev
);
513 EXPORT_SYMBOL_GPL(__nvdimm_create
);
515 static void shutdown_security_notify(void *data
)
517 struct nvdimm
*nvdimm
= data
;
519 sysfs_put(nvdimm
->sec
.overwrite_state
);
522 int nvdimm_security_setup_events(struct device
*dev
)
524 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
526 if (!nvdimm
->sec
.flags
|| !nvdimm
->sec
.ops
527 || !nvdimm
->sec
.ops
->overwrite
)
529 nvdimm
->sec
.overwrite_state
= sysfs_get_dirent(dev
->kobj
.sd
, "security");
530 if (!nvdimm
->sec
.overwrite_state
)
533 return devm_add_action_or_reset(dev
, shutdown_security_notify
, nvdimm
);
535 EXPORT_SYMBOL_GPL(nvdimm_security_setup_events
);
537 int nvdimm_in_overwrite(struct nvdimm
*nvdimm
)
539 return test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
541 EXPORT_SYMBOL_GPL(nvdimm_in_overwrite
);
543 int nvdimm_security_freeze(struct nvdimm
*nvdimm
)
547 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm
->dev
));
549 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->freeze
)
552 if (!nvdimm
->sec
.flags
)
555 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
556 dev_warn(&nvdimm
->dev
, "Overwrite operation in progress.\n");
560 rc
= nvdimm
->sec
.ops
->freeze(nvdimm
);
561 nvdimm
->sec
.flags
= nvdimm_security_flags(nvdimm
, NVDIMM_USER
);
566 static unsigned long dpa_align(struct nd_region
*nd_region
)
568 struct device
*dev
= &nd_region
->dev
;
570 if (dev_WARN_ONCE(dev
, !is_nvdimm_bus_locked(dev
),
571 "bus lock required for capacity provision\n"))
573 if (dev_WARN_ONCE(dev
, !nd_region
->ndr_mappings
|| nd_region
->align
574 % nd_region
->ndr_mappings
,
575 "invalid region align %#lx mappings: %d\n",
576 nd_region
->align
, nd_region
->ndr_mappings
))
578 return nd_region
->align
/ nd_region
->ndr_mappings
;
581 int alias_dpa_busy(struct device
*dev
, void *data
)
583 resource_size_t map_end
, blk_start
, new;
584 struct blk_alloc_info
*info
= data
;
585 struct nd_mapping
*nd_mapping
;
586 struct nd_region
*nd_region
;
587 struct nvdimm_drvdata
*ndd
;
588 struct resource
*res
;
595 nd_region
= to_nd_region(dev
);
596 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
597 nd_mapping
= &nd_region
->mapping
[i
];
598 if (nd_mapping
->nvdimm
== info
->nd_mapping
->nvdimm
)
602 if (i
>= nd_region
->ndr_mappings
)
605 ndd
= to_ndd(nd_mapping
);
606 map_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
607 blk_start
= nd_mapping
->start
;
610 * In the allocation case ->res is set to free space that we are
611 * looking to validate against PMEM aliasing collision rules
612 * (i.e. BLK is allocated after all aliased PMEM).
615 if (info
->res
->start
>= nd_mapping
->start
616 && info
->res
->start
< map_end
)
624 * Find the free dpa from the end of the last pmem allocation to
625 * the end of the interleave-set mapping.
627 align
= dpa_align(nd_region
);
631 for_each_dpa_resource(ndd
, res
) {
632 resource_size_t start
, end
;
634 if (strncmp(res
->name
, "pmem", 4) != 0)
637 start
= ALIGN_DOWN(res
->start
, align
);
638 end
= ALIGN(res
->end
+ 1, align
) - 1;
639 if ((start
>= blk_start
&& start
< map_end
)
640 || (end
>= blk_start
&& end
<= map_end
)) {
641 new = max(blk_start
, min(map_end
, end
) + 1);
642 if (new != blk_start
) {
649 /* update the free space range with the probed blk_start */
650 if (info
->res
&& blk_start
> info
->res
->start
) {
651 info
->res
->start
= max(info
->res
->start
, blk_start
);
652 if (info
->res
->start
> info
->res
->end
)
653 info
->res
->end
= info
->res
->start
- 1;
657 info
->available
-= blk_start
- nd_mapping
->start
;
663 * nd_blk_available_dpa - account the unused dpa of BLK region
664 * @nd_mapping: container of dpa-resource-root + labels
666 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
667 * we arrange for them to never start at an lower dpa than the last
668 * PMEM allocation in an aliased region.
670 resource_size_t
nd_blk_available_dpa(struct nd_region
*nd_region
)
672 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
673 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
674 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
675 struct blk_alloc_info info
= {
676 .nd_mapping
= nd_mapping
,
677 .available
= nd_mapping
->size
,
680 struct resource
*res
;
686 device_for_each_child(&nvdimm_bus
->dev
, &info
, alias_dpa_busy
);
688 /* now account for busy blk allocations in unaliased dpa */
689 align
= dpa_align(nd_region
);
692 for_each_dpa_resource(ndd
, res
) {
693 resource_size_t start
, end
, size
;
695 if (strncmp(res
->name
, "blk", 3) != 0)
697 start
= ALIGN_DOWN(res
->start
, align
);
698 end
= ALIGN(res
->end
+ 1, align
) - 1;
699 size
= end
- start
+ 1;
700 if (size
>= info
.available
)
702 info
.available
-= size
;
705 return info
.available
;
709 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
710 * contiguous unallocated dpa range.
711 * @nd_region: constrain available space check to this reference region
712 * @nd_mapping: container of dpa-resource-root + labels
714 resource_size_t
nd_pmem_max_contiguous_dpa(struct nd_region
*nd_region
,
715 struct nd_mapping
*nd_mapping
)
717 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
718 struct nvdimm_bus
*nvdimm_bus
;
719 resource_size_t max
= 0;
720 struct resource
*res
;
723 /* if a dimm is disabled the available capacity is zero */
727 align
= dpa_align(nd_region
);
731 nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
732 if (__reserve_free_pmem(&nd_region
->dev
, nd_mapping
->nvdimm
))
734 for_each_dpa_resource(ndd
, res
) {
735 resource_size_t start
, end
;
737 if (strcmp(res
->name
, "pmem-reserve") != 0)
739 /* trim free space relative to current alignment setting */
740 start
= ALIGN(res
->start
, align
);
741 end
= ALIGN_DOWN(res
->end
+ 1, align
) - 1;
744 if (end
- start
+ 1 > max
)
745 max
= end
- start
+ 1;
747 release_free_pmem(nvdimm_bus
, nd_mapping
);
752 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
753 * @nd_mapping: container of dpa-resource-root + labels
754 * @nd_region: constrain available space check to this reference region
755 * @overlap: calculate available space assuming this level of overlap
757 * Validate that a PMEM label, if present, aligns with the start of an
758 * interleave set and truncate the available size at the lowest BLK
761 * The expectation is that this routine is called multiple times as it
762 * probes for the largest BLK encroachment for any single member DIMM of
763 * the interleave set. Once that value is determined the PMEM-limit for
764 * the set can be established.
766 resource_size_t
nd_pmem_available_dpa(struct nd_region
*nd_region
,
767 struct nd_mapping
*nd_mapping
, resource_size_t
*overlap
)
769 resource_size_t map_start
, map_end
, busy
= 0, available
, blk_start
;
770 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
771 struct resource
*res
;
778 align
= dpa_align(nd_region
);
782 map_start
= nd_mapping
->start
;
783 map_end
= map_start
+ nd_mapping
->size
- 1;
784 blk_start
= max(map_start
, map_end
+ 1 - *overlap
);
785 for_each_dpa_resource(ndd
, res
) {
786 resource_size_t start
, end
;
788 start
= ALIGN_DOWN(res
->start
, align
);
789 end
= ALIGN(res
->end
+ 1, align
) - 1;
790 if (start
>= map_start
&& start
< map_end
) {
791 if (strncmp(res
->name
, "blk", 3) == 0)
792 blk_start
= min(blk_start
,
793 max(map_start
, start
));
794 else if (end
> map_end
) {
795 reason
= "misaligned to iset";
798 busy
+= end
- start
+ 1;
799 } else if (end
>= map_start
&& end
<= map_end
) {
800 if (strncmp(res
->name
, "blk", 3) == 0) {
802 * If a BLK allocation overlaps the start of
803 * PMEM the entire interleave set may now only
806 blk_start
= map_start
;
808 busy
+= end
- start
+ 1;
809 } else if (map_start
> start
&& map_start
< end
) {
810 /* total eclipse of the mapping */
811 busy
+= nd_mapping
->size
;
812 blk_start
= map_start
;
816 *overlap
= map_end
+ 1 - blk_start
;
817 available
= blk_start
- map_start
;
818 if (busy
< available
)
819 return ALIGN_DOWN(available
- busy
, align
);
823 nd_dbg_dpa(nd_region
, ndd
, res
, "%s\n", reason
);
827 void nvdimm_free_dpa(struct nvdimm_drvdata
*ndd
, struct resource
*res
)
829 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
831 __release_region(&ndd
->dpa
, res
->start
, resource_size(res
));
834 struct resource
*nvdimm_allocate_dpa(struct nvdimm_drvdata
*ndd
,
835 struct nd_label_id
*label_id
, resource_size_t start
,
838 char *name
= kmemdup(label_id
, sizeof(*label_id
), GFP_KERNEL
);
839 struct resource
*res
;
844 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
845 res
= __request_region(&ndd
->dpa
, start
, n
, name
, 0);
852 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
853 * @nvdimm: container of dpa-resource-root + labels
854 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
856 resource_size_t
nvdimm_allocated_dpa(struct nvdimm_drvdata
*ndd
,
857 struct nd_label_id
*label_id
)
859 resource_size_t allocated
= 0;
860 struct resource
*res
;
862 for_each_dpa_resource(ndd
, res
)
863 if (strcmp(res
->name
, label_id
->id
) == 0)
864 allocated
+= resource_size(res
);
869 static int count_dimms(struct device
*dev
, void *c
)
878 int nvdimm_bus_check_dimm_count(struct nvdimm_bus
*nvdimm_bus
, int dimm_count
)
881 /* Flush any possible dimm registration failures */
884 device_for_each_child(&nvdimm_bus
->dev
, &count
, count_dimms
);
885 dev_dbg(&nvdimm_bus
->dev
, "count: %d\n", count
);
886 if (count
!= dimm_count
)
890 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count
);
892 void __exit
nvdimm_devs_exit(void)
894 ida_destroy(&dimm_ida
);