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 * Retrieve bus and dimm handle and return if this bus supports
23 * get_config_data commands
25 int nvdimm_check_config_data(struct device
*dev
)
27 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
29 if (!nvdimm
->cmd_mask
||
30 !test_bit(ND_CMD_GET_CONFIG_DATA
, &nvdimm
->cmd_mask
)) {
31 if (test_bit(NDD_LABELING
, &nvdimm
->flags
))
40 static int validate_dimm(struct nvdimm_drvdata
*ndd
)
47 rc
= nvdimm_check_config_data(ndd
->dev
);
49 dev_dbg(ndd
->dev
, "%ps: %s error: %d\n",
50 __builtin_return_address(0), __func__
, rc
);
55 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
56 * @ndd: dimm to initialize
58 * Returns: %0 if the area is already valid, -errno on error
60 int nvdimm_init_nsarea(struct nvdimm_drvdata
*ndd
)
62 struct nd_cmd_get_config_size
*cmd
= &ndd
->nsarea
;
63 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
64 struct nvdimm_bus_descriptor
*nd_desc
;
65 int rc
= validate_dimm(ndd
);
72 return 0; /* already valid */
74 memset(cmd
, 0, sizeof(*cmd
));
75 nd_desc
= nvdimm_bus
->nd_desc
;
76 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
77 ND_CMD_GET_CONFIG_SIZE
, cmd
, sizeof(*cmd
), &cmd_rc
);
83 int nvdimm_get_config_data(struct nvdimm_drvdata
*ndd
, void *buf
,
84 size_t offset
, size_t len
)
86 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
87 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
88 int rc
= validate_dimm(ndd
), cmd_rc
= 0;
89 struct nd_cmd_get_config_data_hdr
*cmd
;
90 size_t max_cmd_size
, buf_offset
;
95 if (offset
+ len
> ndd
->nsarea
.config_size
)
98 max_cmd_size
= min_t(u32
, len
, ndd
->nsarea
.max_xfer
);
99 cmd
= kvzalloc(max_cmd_size
+ sizeof(*cmd
), GFP_KERNEL
);
103 for (buf_offset
= 0; len
;
104 len
-= cmd
->in_length
, buf_offset
+= cmd
->in_length
) {
107 cmd
->in_offset
= offset
+ buf_offset
;
108 cmd
->in_length
= min(max_cmd_size
, len
);
110 cmd_size
= sizeof(*cmd
) + cmd
->in_length
;
112 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
113 ND_CMD_GET_CONFIG_DATA
, cmd
, cmd_size
, &cmd_rc
);
121 /* out_buf should be valid, copy it into our output buffer */
122 memcpy(buf
+ buf_offset
, cmd
->out_buf
, cmd
->in_length
);
129 int nvdimm_set_config_data(struct nvdimm_drvdata
*ndd
, size_t offset
,
130 void *buf
, size_t len
)
132 size_t max_cmd_size
, buf_offset
;
133 struct nd_cmd_set_config_hdr
*cmd
;
134 int rc
= validate_dimm(ndd
), cmd_rc
= 0;
135 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
136 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
141 if (offset
+ len
> ndd
->nsarea
.config_size
)
144 max_cmd_size
= min_t(u32
, len
, ndd
->nsarea
.max_xfer
);
145 cmd
= kvzalloc(max_cmd_size
+ sizeof(*cmd
) + sizeof(u32
), GFP_KERNEL
);
149 for (buf_offset
= 0; len
; len
-= cmd
->in_length
,
150 buf_offset
+= cmd
->in_length
) {
153 cmd
->in_offset
= offset
+ buf_offset
;
154 cmd
->in_length
= min(max_cmd_size
, len
);
155 memcpy(cmd
->in_buf
, buf
+ buf_offset
, cmd
->in_length
);
157 /* status is output in the last 4-bytes of the command buffer */
158 cmd_size
= sizeof(*cmd
) + cmd
->in_length
+ sizeof(u32
);
160 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
161 ND_CMD_SET_CONFIG_DATA
, cmd
, cmd_size
, &cmd_rc
);
174 void nvdimm_set_labeling(struct device
*dev
)
176 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
178 set_bit(NDD_LABELING
, &nvdimm
->flags
);
181 void nvdimm_set_locked(struct device
*dev
)
183 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
185 set_bit(NDD_LOCKED
, &nvdimm
->flags
);
188 void nvdimm_clear_locked(struct device
*dev
)
190 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
192 clear_bit(NDD_LOCKED
, &nvdimm
->flags
);
195 static void nvdimm_release(struct device
*dev
)
197 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
199 ida_free(&dimm_ida
, nvdimm
->id
);
203 struct nvdimm
*to_nvdimm(struct device
*dev
)
205 struct nvdimm
*nvdimm
= container_of(dev
, struct nvdimm
, dev
);
207 WARN_ON(!is_nvdimm(dev
));
210 EXPORT_SYMBOL_GPL(to_nvdimm
);
212 struct nvdimm_drvdata
*to_ndd(struct nd_mapping
*nd_mapping
)
214 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
216 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm
->dev
));
218 return dev_get_drvdata(&nvdimm
->dev
);
220 EXPORT_SYMBOL(to_ndd
);
222 void nvdimm_drvdata_release(struct kref
*kref
)
224 struct nvdimm_drvdata
*ndd
= container_of(kref
, typeof(*ndd
), kref
);
225 struct device
*dev
= ndd
->dev
;
226 struct resource
*res
, *_r
;
228 dev_dbg(dev
, "trace\n");
229 nvdimm_bus_lock(dev
);
230 for_each_dpa_resource_safe(ndd
, res
, _r
)
231 nvdimm_free_dpa(ndd
, res
);
232 nvdimm_bus_unlock(dev
);
239 void get_ndd(struct nvdimm_drvdata
*ndd
)
241 kref_get(&ndd
->kref
);
244 void put_ndd(struct nvdimm_drvdata
*ndd
)
247 kref_put(&ndd
->kref
, nvdimm_drvdata_release
);
250 const char *nvdimm_name(struct nvdimm
*nvdimm
)
252 return dev_name(&nvdimm
->dev
);
254 EXPORT_SYMBOL_GPL(nvdimm_name
);
256 struct kobject
*nvdimm_kobj(struct nvdimm
*nvdimm
)
258 return &nvdimm
->dev
.kobj
;
260 EXPORT_SYMBOL_GPL(nvdimm_kobj
);
262 unsigned long nvdimm_cmd_mask(struct nvdimm
*nvdimm
)
264 return nvdimm
->cmd_mask
;
266 EXPORT_SYMBOL_GPL(nvdimm_cmd_mask
);
268 void *nvdimm_provider_data(struct nvdimm
*nvdimm
)
271 return nvdimm
->provider_data
;
274 EXPORT_SYMBOL_GPL(nvdimm_provider_data
);
276 static ssize_t
commands_show(struct device
*dev
,
277 struct device_attribute
*attr
, char *buf
)
279 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
282 if (!nvdimm
->cmd_mask
)
283 return sprintf(buf
, "\n");
285 for_each_set_bit(cmd
, &nvdimm
->cmd_mask
, BITS_PER_LONG
)
286 len
+= sprintf(buf
+ len
, "%s ", nvdimm_cmd_name(cmd
));
287 len
+= sprintf(buf
+ len
, "\n");
290 static DEVICE_ATTR_RO(commands
);
292 static ssize_t
flags_show(struct device
*dev
,
293 struct device_attribute
*attr
, char *buf
)
295 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
297 return sprintf(buf
, "%s%s\n",
298 test_bit(NDD_LABELING
, &nvdimm
->flags
) ? "label " : "",
299 test_bit(NDD_LOCKED
, &nvdimm
->flags
) ? "lock " : "");
301 static DEVICE_ATTR_RO(flags
);
303 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
306 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
309 * The state may be in the process of changing, userspace should
310 * quiesce probing if it wants a static answer
312 nvdimm_bus_lock(dev
);
313 nvdimm_bus_unlock(dev
);
314 return sprintf(buf
, "%s\n", atomic_read(&nvdimm
->busy
)
315 ? "active" : "idle");
317 static DEVICE_ATTR_RO(state
);
319 static ssize_t
__available_slots_show(struct nvdimm_drvdata
*ndd
, char *buf
)
329 nvdimm_bus_lock(dev
);
330 nfree
= nd_label_nfree(ndd
);
331 if (nfree
- 1 > nfree
) {
332 dev_WARN_ONCE(dev
, 1, "we ate our last label?\n");
336 rc
= sprintf(buf
, "%d\n", nfree
);
337 nvdimm_bus_unlock(dev
);
341 static ssize_t
available_slots_show(struct device
*dev
,
342 struct device_attribute
*attr
, char *buf
)
347 rc
= __available_slots_show(dev_get_drvdata(dev
), buf
);
352 static DEVICE_ATTR_RO(available_slots
);
354 static ssize_t
security_show(struct device
*dev
,
355 struct device_attribute
*attr
, char *buf
)
357 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
360 * For the test version we need to poll the "hardware" in order
361 * to get the updated status for unlock testing.
363 if (IS_ENABLED(CONFIG_NVDIMM_SECURITY_TEST
))
364 nvdimm
->sec
.flags
= nvdimm_security_flags(nvdimm
, NVDIMM_USER
);
366 if (test_bit(NVDIMM_SECURITY_OVERWRITE
, &nvdimm
->sec
.flags
))
367 return sprintf(buf
, "overwrite\n");
368 if (test_bit(NVDIMM_SECURITY_DISABLED
, &nvdimm
->sec
.flags
))
369 return sprintf(buf
, "disabled\n");
370 if (test_bit(NVDIMM_SECURITY_UNLOCKED
, &nvdimm
->sec
.flags
))
371 return sprintf(buf
, "unlocked\n");
372 if (test_bit(NVDIMM_SECURITY_LOCKED
, &nvdimm
->sec
.flags
))
373 return sprintf(buf
, "locked\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
);
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 ssize_t
result_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
451 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
452 enum nvdimm_fwa_result result
;
457 nvdimm_bus_lock(dev
);
458 result
= nvdimm
->fw_ops
->activate_result(nvdimm
);
459 nvdimm_bus_unlock(dev
);
462 case NVDIMM_FWA_RESULT_NONE
:
463 return sprintf(buf
, "none\n");
464 case NVDIMM_FWA_RESULT_SUCCESS
:
465 return sprintf(buf
, "success\n");
466 case NVDIMM_FWA_RESULT_FAIL
:
467 return sprintf(buf
, "fail\n");
468 case NVDIMM_FWA_RESULT_NOTSTAGED
:
469 return sprintf(buf
, "not_staged\n");
470 case NVDIMM_FWA_RESULT_NEEDRESET
:
471 return sprintf(buf
, "need_reset\n");
476 static DEVICE_ATTR_ADMIN_RO(result
);
478 static ssize_t
activate_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
480 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
481 enum nvdimm_fwa_state state
;
486 nvdimm_bus_lock(dev
);
487 state
= nvdimm
->fw_ops
->activate_state(nvdimm
);
488 nvdimm_bus_unlock(dev
);
491 case NVDIMM_FWA_IDLE
:
492 return sprintf(buf
, "idle\n");
493 case NVDIMM_FWA_BUSY
:
494 return sprintf(buf
, "busy\n");
495 case NVDIMM_FWA_ARMED
:
496 return sprintf(buf
, "armed\n");
502 static ssize_t
activate_store(struct device
*dev
, struct device_attribute
*attr
,
503 const char *buf
, size_t len
)
505 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
506 enum nvdimm_fwa_trigger arg
;
512 if (sysfs_streq(buf
, "arm"))
513 arg
= NVDIMM_FWA_ARM
;
514 else if (sysfs_streq(buf
, "disarm"))
515 arg
= NVDIMM_FWA_DISARM
;
519 nvdimm_bus_lock(dev
);
520 rc
= nvdimm
->fw_ops
->arm(nvdimm
, arg
);
521 nvdimm_bus_unlock(dev
);
527 static DEVICE_ATTR_ADMIN_RW(activate
);
529 static struct attribute
*nvdimm_firmware_attributes
[] = {
530 &dev_attr_activate
.attr
,
531 &dev_attr_result
.attr
,
535 static umode_t
nvdimm_firmware_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
537 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
538 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(dev
);
539 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
540 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
541 enum nvdimm_fwa_capability cap
;
543 if (!nd_desc
->fw_ops
)
548 nvdimm_bus_lock(dev
);
549 cap
= nd_desc
->fw_ops
->capability(nd_desc
);
550 nvdimm_bus_unlock(dev
);
552 if (cap
< NVDIMM_FWA_CAP_QUIESCE
)
558 static const struct attribute_group nvdimm_firmware_attribute_group
= {
560 .attrs
= nvdimm_firmware_attributes
,
561 .is_visible
= nvdimm_firmware_visible
,
564 static const struct attribute_group
*nvdimm_attribute_groups
[] = {
565 &nd_device_attribute_group
,
566 &nvdimm_attribute_group
,
567 &nvdimm_firmware_attribute_group
,
571 static const struct device_type nvdimm_device_type
= {
573 .release
= nvdimm_release
,
574 .groups
= nvdimm_attribute_groups
,
577 bool is_nvdimm(const struct device
*dev
)
579 return dev
->type
== &nvdimm_device_type
;
582 static struct lock_class_key nvdimm_key
;
584 struct nvdimm
*__nvdimm_create(struct nvdimm_bus
*nvdimm_bus
,
585 void *provider_data
, const struct attribute_group
**groups
,
586 unsigned long flags
, unsigned long cmd_mask
, int num_flush
,
587 struct resource
*flush_wpq
, const char *dimm_id
,
588 const struct nvdimm_security_ops
*sec_ops
,
589 const struct nvdimm_fw_ops
*fw_ops
)
591 struct nvdimm
*nvdimm
= kzalloc(sizeof(*nvdimm
), GFP_KERNEL
);
597 nvdimm
->id
= ida_alloc(&dimm_ida
, GFP_KERNEL
);
598 if (nvdimm
->id
< 0) {
603 nvdimm
->dimm_id
= dimm_id
;
604 nvdimm
->provider_data
= provider_data
;
605 nvdimm
->flags
= flags
;
606 nvdimm
->cmd_mask
= cmd_mask
;
607 nvdimm
->num_flush
= num_flush
;
608 nvdimm
->flush_wpq
= flush_wpq
;
609 atomic_set(&nvdimm
->busy
, 0);
611 dev_set_name(dev
, "nmem%d", nvdimm
->id
);
612 dev
->parent
= &nvdimm_bus
->dev
;
613 dev
->type
= &nvdimm_device_type
;
614 dev
->devt
= MKDEV(nvdimm_major
, nvdimm
->id
);
615 dev
->groups
= groups
;
616 nvdimm
->sec
.ops
= sec_ops
;
617 nvdimm
->fw_ops
= fw_ops
;
618 nvdimm
->sec
.overwrite_tmo
= 0;
619 INIT_DELAYED_WORK(&nvdimm
->dwork
, nvdimm_security_overwrite_query
);
621 * Security state must be initialized before device_add() for
622 * attribute visibility.
624 /* get security state and extended (master) state */
625 nvdimm
->sec
.flags
= nvdimm_security_flags(nvdimm
, NVDIMM_USER
);
626 nvdimm
->sec
.ext_flags
= nvdimm_security_flags(nvdimm
, NVDIMM_MASTER
);
627 device_initialize(dev
);
628 lockdep_set_class(&dev
->mutex
, &nvdimm_key
);
629 if (test_bit(NDD_REGISTER_SYNC
, &flags
))
630 nd_device_register_sync(dev
);
632 nd_device_register(dev
);
636 EXPORT_SYMBOL_GPL(__nvdimm_create
);
638 void nvdimm_delete(struct nvdimm
*nvdimm
)
640 struct device
*dev
= &nvdimm
->dev
;
641 bool dev_put
= false;
643 /* We are shutting down. Make state frozen artificially. */
644 nvdimm_bus_lock(dev
);
645 set_bit(NVDIMM_SECURITY_FROZEN
, &nvdimm
->sec
.flags
);
646 if (test_and_clear_bit(NDD_WORK_PENDING
, &nvdimm
->flags
))
648 nvdimm_bus_unlock(dev
);
649 cancel_delayed_work_sync(&nvdimm
->dwork
);
652 nd_device_unregister(dev
, ND_SYNC
);
654 EXPORT_SYMBOL_GPL(nvdimm_delete
);
656 static void shutdown_security_notify(void *data
)
658 struct nvdimm
*nvdimm
= data
;
660 sysfs_put(nvdimm
->sec
.overwrite_state
);
663 int nvdimm_security_setup_events(struct device
*dev
)
665 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
667 if (!nvdimm
->sec
.flags
|| !nvdimm
->sec
.ops
668 || !nvdimm
->sec
.ops
->overwrite
)
670 nvdimm
->sec
.overwrite_state
= sysfs_get_dirent(dev
->kobj
.sd
, "security");
671 if (!nvdimm
->sec
.overwrite_state
)
674 return devm_add_action_or_reset(dev
, shutdown_security_notify
, nvdimm
);
676 EXPORT_SYMBOL_GPL(nvdimm_security_setup_events
);
678 int nvdimm_in_overwrite(struct nvdimm
*nvdimm
)
680 return test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
682 EXPORT_SYMBOL_GPL(nvdimm_in_overwrite
);
684 int nvdimm_security_freeze(struct nvdimm
*nvdimm
)
688 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm
->dev
));
690 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->freeze
)
693 if (!nvdimm
->sec
.flags
)
696 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
697 dev_warn(&nvdimm
->dev
, "Overwrite operation in progress.\n");
701 rc
= nvdimm
->sec
.ops
->freeze(nvdimm
);
702 nvdimm
->sec
.flags
= nvdimm_security_flags(nvdimm
, NVDIMM_USER
);
707 static unsigned long dpa_align(struct nd_region
*nd_region
)
709 struct device
*dev
= &nd_region
->dev
;
711 if (dev_WARN_ONCE(dev
, !is_nvdimm_bus_locked(dev
),
712 "bus lock required for capacity provision\n"))
714 if (dev_WARN_ONCE(dev
, !nd_region
->ndr_mappings
|| nd_region
->align
715 % nd_region
->ndr_mappings
,
716 "invalid region align %#lx mappings: %d\n",
717 nd_region
->align
, nd_region
->ndr_mappings
))
719 return nd_region
->align
/ nd_region
->ndr_mappings
;
723 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
724 * contiguous unallocated dpa range.
725 * @nd_region: constrain available space check to this reference region
726 * @nd_mapping: container of dpa-resource-root + labels
728 * Returns: %0 if there is an alignment error, otherwise the max
729 * unallocated dpa range
731 resource_size_t
nd_pmem_max_contiguous_dpa(struct nd_region
*nd_region
,
732 struct nd_mapping
*nd_mapping
)
734 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
735 struct nvdimm_bus
*nvdimm_bus
;
736 resource_size_t max
= 0;
737 struct resource
*res
;
740 /* if a dimm is disabled the available capacity is zero */
744 align
= dpa_align(nd_region
);
748 nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
749 if (__reserve_free_pmem(&nd_region
->dev
, nd_mapping
->nvdimm
))
751 for_each_dpa_resource(ndd
, res
) {
752 resource_size_t start
, end
;
754 if (strcmp(res
->name
, "pmem-reserve") != 0)
756 /* trim free space relative to current alignment setting */
757 start
= ALIGN(res
->start
, align
);
758 end
= ALIGN_DOWN(res
->end
+ 1, align
) - 1;
761 if (end
- start
+ 1 > max
)
762 max
= end
- start
+ 1;
764 release_free_pmem(nvdimm_bus
, nd_mapping
);
769 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
770 * @nd_mapping: container of dpa-resource-root + labels
771 * @nd_region: constrain available space check to this reference region
773 * Validate that a PMEM label, if present, aligns with the start of an
776 * Returns: %0 if there is an alignment error, otherwise the unallocated dpa
778 resource_size_t
nd_pmem_available_dpa(struct nd_region
*nd_region
,
779 struct nd_mapping
*nd_mapping
)
781 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
782 resource_size_t map_start
, map_end
, busy
= 0;
783 struct resource
*res
;
789 align
= dpa_align(nd_region
);
793 map_start
= nd_mapping
->start
;
794 map_end
= map_start
+ nd_mapping
->size
- 1;
795 for_each_dpa_resource(ndd
, res
) {
796 resource_size_t start
, end
;
798 start
= ALIGN_DOWN(res
->start
, align
);
799 end
= ALIGN(res
->end
+ 1, align
) - 1;
800 if (start
>= map_start
&& start
< map_end
) {
802 nd_dbg_dpa(nd_region
, ndd
, res
,
803 "misaligned to iset\n");
806 busy
+= end
- start
+ 1;
807 } else if (end
>= map_start
&& end
<= map_end
) {
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
;
815 if (busy
< nd_mapping
->size
)
816 return ALIGN_DOWN(nd_mapping
->size
- busy
, align
);
820 void nvdimm_free_dpa(struct nvdimm_drvdata
*ndd
, struct resource
*res
)
822 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
824 __release_region(&ndd
->dpa
, res
->start
, resource_size(res
));
827 struct resource
*nvdimm_allocate_dpa(struct nvdimm_drvdata
*ndd
,
828 struct nd_label_id
*label_id
, resource_size_t start
,
831 char *name
= kmemdup(label_id
, sizeof(*label_id
), GFP_KERNEL
);
832 struct resource
*res
;
837 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
838 res
= __request_region(&ndd
->dpa
, start
, n
, name
, 0);
845 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
846 * @ndd: container of dpa-resource-root + labels
847 * @label_id: dpa resource name of the form pmem-<human readable uuid>
849 * Returns: sum of the dpa allocated to the label_id
851 resource_size_t
nvdimm_allocated_dpa(struct nvdimm_drvdata
*ndd
,
852 struct nd_label_id
*label_id
)
854 resource_size_t allocated
= 0;
855 struct resource
*res
;
857 for_each_dpa_resource(ndd
, res
)
858 if (strcmp(res
->name
, label_id
->id
) == 0)
859 allocated
+= resource_size(res
);
864 static int count_dimms(struct device
*dev
, void *c
)
873 int nvdimm_bus_check_dimm_count(struct nvdimm_bus
*nvdimm_bus
, int dimm_count
)
876 /* Flush any possible dimm registration failures */
879 device_for_each_child(&nvdimm_bus
->dev
, &count
, count_dimms
);
880 dev_dbg(&nvdimm_bus
->dev
, "count: %d\n", count
);
881 if (count
!= dimm_count
)
885 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count
);
887 void __exit
nvdimm_devs_exit(void)
889 ida_destroy(&dimm_ida
);