2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/moduleparam.h>
15 #include <linux/vmalloc.h>
16 #include <linux/device.h>
17 #include <linux/ndctl.h>
18 #include <linux/slab.h>
27 static DEFINE_IDA(dimm_ida
);
30 module_param(noblk
, bool, 0444);
31 MODULE_PARM_DESC(noblk
, "force disable BLK / local alias support");
34 * Retrieve bus and dimm handle and return if this bus supports
35 * get_config_data commands
37 int nvdimm_check_config_data(struct device
*dev
)
39 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
41 if (!nvdimm
->cmd_mask
||
42 !test_bit(ND_CMD_GET_CONFIG_DATA
, &nvdimm
->cmd_mask
)) {
43 if (test_bit(NDD_ALIASING
, &nvdimm
->flags
))
52 static int validate_dimm(struct nvdimm_drvdata
*ndd
)
59 rc
= nvdimm_check_config_data(ndd
->dev
);
61 dev_dbg(ndd
->dev
, "%pf: %s error: %d\n",
62 __builtin_return_address(0), __func__
, rc
);
67 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
68 * @nvdimm: dimm to initialize
70 int nvdimm_init_nsarea(struct nvdimm_drvdata
*ndd
)
72 struct nd_cmd_get_config_size
*cmd
= &ndd
->nsarea
;
73 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
74 struct nvdimm_bus_descriptor
*nd_desc
;
75 int rc
= validate_dimm(ndd
);
82 return 0; /* already valid */
84 memset(cmd
, 0, sizeof(*cmd
));
85 nd_desc
= nvdimm_bus
->nd_desc
;
86 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
87 ND_CMD_GET_CONFIG_SIZE
, cmd
, sizeof(*cmd
), &cmd_rc
);
93 int nvdimm_get_config_data(struct nvdimm_drvdata
*ndd
, void *buf
,
94 size_t offset
, size_t len
)
96 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
97 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
98 int rc
= validate_dimm(ndd
), cmd_rc
= 0;
99 struct nd_cmd_get_config_data_hdr
*cmd
;
100 size_t max_cmd_size
, buf_offset
;
105 if (offset
+ len
> ndd
->nsarea
.config_size
)
108 max_cmd_size
= min_t(u32
, len
, ndd
->nsarea
.max_xfer
);
109 cmd
= kvzalloc(max_cmd_size
+ sizeof(*cmd
), GFP_KERNEL
);
113 for (buf_offset
= 0; len
;
114 len
-= cmd
->in_length
, buf_offset
+= cmd
->in_length
) {
117 cmd
->in_offset
= offset
+ buf_offset
;
118 cmd
->in_length
= min(max_cmd_size
, len
);
120 cmd_size
= sizeof(*cmd
) + cmd
->in_length
;
122 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
123 ND_CMD_GET_CONFIG_DATA
, cmd
, cmd_size
, &cmd_rc
);
131 /* out_buf should be valid, copy it into our output buffer */
132 memcpy(buf
+ buf_offset
, cmd
->out_buf
, cmd
->in_length
);
139 int nvdimm_set_config_data(struct nvdimm_drvdata
*ndd
, size_t offset
,
140 void *buf
, size_t len
)
142 size_t max_cmd_size
, buf_offset
;
143 struct nd_cmd_set_config_hdr
*cmd
;
144 int rc
= validate_dimm(ndd
), cmd_rc
= 0;
145 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
146 struct nvdimm_bus_descriptor
*nd_desc
= nvdimm_bus
->nd_desc
;
151 if (offset
+ len
> ndd
->nsarea
.config_size
)
154 max_cmd_size
= min_t(u32
, len
, ndd
->nsarea
.max_xfer
);
155 cmd
= kvzalloc(max_cmd_size
+ sizeof(*cmd
) + sizeof(u32
), GFP_KERNEL
);
159 for (buf_offset
= 0; len
; len
-= cmd
->in_length
,
160 buf_offset
+= cmd
->in_length
) {
163 cmd
->in_offset
= offset
+ buf_offset
;
164 cmd
->in_length
= min(max_cmd_size
, len
);
165 memcpy(cmd
->in_buf
, buf
+ buf_offset
, cmd
->in_length
);
167 /* status is output in the last 4-bytes of the command buffer */
168 cmd_size
= sizeof(*cmd
) + cmd
->in_length
+ sizeof(u32
);
170 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
171 ND_CMD_SET_CONFIG_DATA
, cmd
, cmd_size
, &cmd_rc
);
184 void nvdimm_set_aliasing(struct device
*dev
)
186 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
188 set_bit(NDD_ALIASING
, &nvdimm
->flags
);
191 void nvdimm_set_locked(struct device
*dev
)
193 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
195 set_bit(NDD_LOCKED
, &nvdimm
->flags
);
198 void nvdimm_clear_locked(struct device
*dev
)
200 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
202 clear_bit(NDD_LOCKED
, &nvdimm
->flags
);
205 static void nvdimm_release(struct device
*dev
)
207 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
209 ida_simple_remove(&dimm_ida
, nvdimm
->id
);
213 static struct device_type nvdimm_device_type
= {
215 .release
= nvdimm_release
,
218 bool is_nvdimm(struct device
*dev
)
220 return dev
->type
== &nvdimm_device_type
;
223 struct nvdimm
*to_nvdimm(struct device
*dev
)
225 struct nvdimm
*nvdimm
= container_of(dev
, struct nvdimm
, dev
);
227 WARN_ON(!is_nvdimm(dev
));
230 EXPORT_SYMBOL_GPL(to_nvdimm
);
232 struct nvdimm
*nd_blk_region_to_dimm(struct nd_blk_region
*ndbr
)
234 struct nd_region
*nd_region
= &ndbr
->nd_region
;
235 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
237 return nd_mapping
->nvdimm
;
239 EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm
);
241 unsigned long nd_blk_memremap_flags(struct nd_blk_region
*ndbr
)
243 /* pmem mapping properties are private to libnvdimm */
244 return ARCH_MEMREMAP_PMEM
;
246 EXPORT_SYMBOL_GPL(nd_blk_memremap_flags
);
248 struct nvdimm_drvdata
*to_ndd(struct nd_mapping
*nd_mapping
)
250 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
252 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm
->dev
));
254 return dev_get_drvdata(&nvdimm
->dev
);
256 EXPORT_SYMBOL(to_ndd
);
258 void nvdimm_drvdata_release(struct kref
*kref
)
260 struct nvdimm_drvdata
*ndd
= container_of(kref
, typeof(*ndd
), kref
);
261 struct device
*dev
= ndd
->dev
;
262 struct resource
*res
, *_r
;
264 dev_dbg(dev
, "trace\n");
265 nvdimm_bus_lock(dev
);
266 for_each_dpa_resource_safe(ndd
, res
, _r
)
267 nvdimm_free_dpa(ndd
, res
);
268 nvdimm_bus_unlock(dev
);
275 void get_ndd(struct nvdimm_drvdata
*ndd
)
277 kref_get(&ndd
->kref
);
280 void put_ndd(struct nvdimm_drvdata
*ndd
)
283 kref_put(&ndd
->kref
, nvdimm_drvdata_release
);
286 const char *nvdimm_name(struct nvdimm
*nvdimm
)
288 return dev_name(&nvdimm
->dev
);
290 EXPORT_SYMBOL_GPL(nvdimm_name
);
292 struct kobject
*nvdimm_kobj(struct nvdimm
*nvdimm
)
294 return &nvdimm
->dev
.kobj
;
296 EXPORT_SYMBOL_GPL(nvdimm_kobj
);
298 unsigned long nvdimm_cmd_mask(struct nvdimm
*nvdimm
)
300 return nvdimm
->cmd_mask
;
302 EXPORT_SYMBOL_GPL(nvdimm_cmd_mask
);
304 void *nvdimm_provider_data(struct nvdimm
*nvdimm
)
307 return nvdimm
->provider_data
;
310 EXPORT_SYMBOL_GPL(nvdimm_provider_data
);
312 static ssize_t
commands_show(struct device
*dev
,
313 struct device_attribute
*attr
, char *buf
)
315 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
318 if (!nvdimm
->cmd_mask
)
319 return sprintf(buf
, "\n");
321 for_each_set_bit(cmd
, &nvdimm
->cmd_mask
, BITS_PER_LONG
)
322 len
+= sprintf(buf
+ len
, "%s ", nvdimm_cmd_name(cmd
));
323 len
+= sprintf(buf
+ len
, "\n");
326 static DEVICE_ATTR_RO(commands
);
328 static ssize_t
flags_show(struct device
*dev
,
329 struct device_attribute
*attr
, char *buf
)
331 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
333 return sprintf(buf
, "%s%s\n",
334 test_bit(NDD_ALIASING
, &nvdimm
->flags
) ? "alias " : "",
335 test_bit(NDD_LOCKED
, &nvdimm
->flags
) ? "lock " : "");
337 static DEVICE_ATTR_RO(flags
);
339 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
342 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
345 * The state may be in the process of changing, userspace should
346 * quiesce probing if it wants a static answer
348 nvdimm_bus_lock(dev
);
349 nvdimm_bus_unlock(dev
);
350 return sprintf(buf
, "%s\n", atomic_read(&nvdimm
->busy
)
351 ? "active" : "idle");
353 static DEVICE_ATTR_RO(state
);
355 static ssize_t
available_slots_show(struct device
*dev
,
356 struct device_attribute
*attr
, char *buf
)
358 struct nvdimm_drvdata
*ndd
= dev_get_drvdata(dev
);
365 nvdimm_bus_lock(dev
);
366 nfree
= nd_label_nfree(ndd
);
367 if (nfree
- 1 > nfree
) {
368 dev_WARN_ONCE(dev
, 1, "we ate our last label?\n");
372 rc
= sprintf(buf
, "%d\n", nfree
);
373 nvdimm_bus_unlock(dev
);
376 static DEVICE_ATTR_RO(available_slots
);
378 __weak ssize_t
security_show(struct device
*dev
,
379 struct device_attribute
*attr
, char *buf
)
381 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
383 switch (nvdimm
->sec
.state
) {
384 case NVDIMM_SECURITY_DISABLED
:
385 return sprintf(buf
, "disabled\n");
386 case NVDIMM_SECURITY_UNLOCKED
:
387 return sprintf(buf
, "unlocked\n");
388 case NVDIMM_SECURITY_LOCKED
:
389 return sprintf(buf
, "locked\n");
390 case NVDIMM_SECURITY_FROZEN
:
391 return sprintf(buf
, "frozen\n");
392 case NVDIMM_SECURITY_OVERWRITE
:
393 return sprintf(buf
, "overwrite\n");
402 C( OP_FREEZE, "freeze", 1), \
403 C( OP_DISABLE, "disable", 2), \
404 C( OP_UPDATE, "update", 3), \
405 C( OP_ERASE, "erase", 2), \
406 C( OP_OVERWRITE, "overwrite", 2), \
407 C( OP_MASTER_UPDATE, "master_update", 3), \
408 C( OP_MASTER_ERASE, "master_erase", 2)
411 enum nvdimmsec_op_ids
{ OPS
};
413 #define C(a, b, c) { b, c }
420 #define SEC_CMD_SIZE 32
421 #define KEY_ID_SIZE 10
423 static ssize_t
__security_store(struct device
*dev
, const char *buf
, size_t len
)
425 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
427 char cmd
[SEC_CMD_SIZE
+1], keystr
[KEY_ID_SIZE
+1],
428 nkeystr
[KEY_ID_SIZE
+1];
429 unsigned int key
, newkey
;
432 if (atomic_read(&nvdimm
->busy
))
435 rc
= sscanf(buf
, "%"__stringify(SEC_CMD_SIZE
)"s"
436 " %"__stringify(KEY_ID_SIZE
)"s"
437 " %"__stringify(KEY_ID_SIZE
)"s",
438 cmd
, keystr
, nkeystr
);
441 for (i
= 0; i
< ARRAY_SIZE(ops
); i
++)
442 if (sysfs_streq(cmd
, ops
[i
].name
))
444 if (i
>= ARRAY_SIZE(ops
))
447 rc
= kstrtouint(keystr
, 0, &key
);
448 if (rc
>= 0 && ops
[i
].args
> 2)
449 rc
= kstrtouint(nkeystr
, 0, &newkey
);
453 if (i
== OP_FREEZE
) {
454 dev_dbg(dev
, "freeze\n");
455 rc
= nvdimm_security_freeze(nvdimm
);
456 } else if (i
== OP_DISABLE
) {
457 dev_dbg(dev
, "disable %u\n", key
);
458 rc
= nvdimm_security_disable(nvdimm
, key
);
459 } else if (i
== OP_UPDATE
) {
460 dev_dbg(dev
, "update %u %u\n", key
, newkey
);
461 rc
= nvdimm_security_update(nvdimm
, key
, newkey
, NVDIMM_USER
);
462 } else if (i
== OP_ERASE
) {
463 dev_dbg(dev
, "erase %u\n", key
);
464 rc
= nvdimm_security_erase(nvdimm
, key
, NVDIMM_USER
);
465 } else if (i
== OP_OVERWRITE
) {
466 dev_dbg(dev
, "overwrite %u\n", key
);
467 rc
= nvdimm_security_overwrite(nvdimm
, key
);
468 } else if (i
== OP_MASTER_UPDATE
) {
469 dev_dbg(dev
, "master_update %u %u\n", key
, newkey
);
470 rc
= nvdimm_security_update(nvdimm
, key
, newkey
,
472 } else if (i
== OP_MASTER_ERASE
) {
473 dev_dbg(dev
, "master_erase %u\n", key
);
474 rc
= nvdimm_security_erase(nvdimm
, key
,
484 static ssize_t
security_store(struct device
*dev
,
485 struct device_attribute
*attr
, const char *buf
, size_t len
)
491 * Require all userspace triggered security management to be
492 * done while probing is idle and the DIMM is not in active use
496 nvdimm_bus_lock(dev
);
497 wait_nvdimm_bus_probe_idle(dev
);
498 rc
= __security_store(dev
, buf
, len
);
499 nvdimm_bus_unlock(dev
);
504 static DEVICE_ATTR_RW(security
);
506 static struct attribute
*nvdimm_attributes
[] = {
507 &dev_attr_state
.attr
,
508 &dev_attr_flags
.attr
,
509 &dev_attr_commands
.attr
,
510 &dev_attr_available_slots
.attr
,
511 &dev_attr_security
.attr
,
515 static umode_t
nvdimm_visible(struct kobject
*kobj
, struct attribute
*a
, int n
)
517 struct device
*dev
= container_of(kobj
, typeof(*dev
), kobj
);
518 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
520 if (a
!= &dev_attr_security
.attr
)
522 if (nvdimm
->sec
.state
< 0)
524 /* Are there any state mutation ops? */
525 if (nvdimm
->sec
.ops
->freeze
|| nvdimm
->sec
.ops
->disable
526 || nvdimm
->sec
.ops
->change_key
527 || nvdimm
->sec
.ops
->erase
528 || nvdimm
->sec
.ops
->overwrite
)
533 struct attribute_group nvdimm_attribute_group
= {
534 .attrs
= nvdimm_attributes
,
535 .is_visible
= nvdimm_visible
,
537 EXPORT_SYMBOL_GPL(nvdimm_attribute_group
);
539 struct nvdimm
*__nvdimm_create(struct nvdimm_bus
*nvdimm_bus
,
540 void *provider_data
, const struct attribute_group
**groups
,
541 unsigned long flags
, unsigned long cmd_mask
, int num_flush
,
542 struct resource
*flush_wpq
, const char *dimm_id
,
543 const struct nvdimm_security_ops
*sec_ops
)
545 struct nvdimm
*nvdimm
= kzalloc(sizeof(*nvdimm
), GFP_KERNEL
);
551 nvdimm
->id
= ida_simple_get(&dimm_ida
, 0, 0, GFP_KERNEL
);
552 if (nvdimm
->id
< 0) {
557 nvdimm
->dimm_id
= dimm_id
;
558 nvdimm
->provider_data
= provider_data
;
560 flags
|= 1 << NDD_NOBLK
;
561 nvdimm
->flags
= flags
;
562 nvdimm
->cmd_mask
= cmd_mask
;
563 nvdimm
->num_flush
= num_flush
;
564 nvdimm
->flush_wpq
= flush_wpq
;
565 atomic_set(&nvdimm
->busy
, 0);
567 dev_set_name(dev
, "nmem%d", nvdimm
->id
);
568 dev
->parent
= &nvdimm_bus
->dev
;
569 dev
->type
= &nvdimm_device_type
;
570 dev
->devt
= MKDEV(nvdimm_major
, nvdimm
->id
);
571 dev
->groups
= groups
;
572 nvdimm
->sec
.ops
= sec_ops
;
573 nvdimm
->sec
.overwrite_tmo
= 0;
574 INIT_DELAYED_WORK(&nvdimm
->dwork
, nvdimm_security_overwrite_query
);
576 * Security state must be initialized before device_add() for
577 * attribute visibility.
579 /* get security state and extended (master) state */
580 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
581 nvdimm
->sec
.ext_state
= nvdimm_security_state(nvdimm
, NVDIMM_MASTER
);
582 nd_device_register(dev
);
586 EXPORT_SYMBOL_GPL(__nvdimm_create
);
588 static void shutdown_security_notify(void *data
)
590 struct nvdimm
*nvdimm
= data
;
592 sysfs_put(nvdimm
->sec
.overwrite_state
);
595 int nvdimm_security_setup_events(struct device
*dev
)
597 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
599 if (nvdimm
->sec
.state
< 0 || !nvdimm
->sec
.ops
600 || !nvdimm
->sec
.ops
->overwrite
)
602 nvdimm
->sec
.overwrite_state
= sysfs_get_dirent(dev
->kobj
.sd
, "security");
603 if (!nvdimm
->sec
.overwrite_state
)
606 return devm_add_action_or_reset(dev
, shutdown_security_notify
, nvdimm
);
608 EXPORT_SYMBOL_GPL(nvdimm_security_setup_events
);
610 int nvdimm_in_overwrite(struct nvdimm
*nvdimm
)
612 return test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
);
614 EXPORT_SYMBOL_GPL(nvdimm_in_overwrite
);
616 int nvdimm_security_freeze(struct nvdimm
*nvdimm
)
620 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm
->dev
));
622 if (!nvdimm
->sec
.ops
|| !nvdimm
->sec
.ops
->freeze
)
625 if (nvdimm
->sec
.state
< 0)
628 if (test_bit(NDD_SECURITY_OVERWRITE
, &nvdimm
->flags
)) {
629 dev_warn(&nvdimm
->dev
, "Overwrite operation in progress.\n");
633 rc
= nvdimm
->sec
.ops
->freeze(nvdimm
);
634 nvdimm
->sec
.state
= nvdimm_security_state(nvdimm
, NVDIMM_USER
);
639 int alias_dpa_busy(struct device
*dev
, void *data
)
641 resource_size_t map_end
, blk_start
, new;
642 struct blk_alloc_info
*info
= data
;
643 struct nd_mapping
*nd_mapping
;
644 struct nd_region
*nd_region
;
645 struct nvdimm_drvdata
*ndd
;
646 struct resource
*res
;
652 nd_region
= to_nd_region(dev
);
653 for (i
= 0; i
< nd_region
->ndr_mappings
; i
++) {
654 nd_mapping
= &nd_region
->mapping
[i
];
655 if (nd_mapping
->nvdimm
== info
->nd_mapping
->nvdimm
)
659 if (i
>= nd_region
->ndr_mappings
)
662 ndd
= to_ndd(nd_mapping
);
663 map_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
664 blk_start
= nd_mapping
->start
;
667 * In the allocation case ->res is set to free space that we are
668 * looking to validate against PMEM aliasing collision rules
669 * (i.e. BLK is allocated after all aliased PMEM).
672 if (info
->res
->start
>= nd_mapping
->start
673 && info
->res
->start
< map_end
)
681 * Find the free dpa from the end of the last pmem allocation to
682 * the end of the interleave-set mapping.
684 for_each_dpa_resource(ndd
, res
) {
685 if (strncmp(res
->name
, "pmem", 4) != 0)
687 if ((res
->start
>= blk_start
&& res
->start
< map_end
)
688 || (res
->end
>= blk_start
689 && res
->end
<= map_end
)) {
690 new = max(blk_start
, min(map_end
+ 1, res
->end
+ 1));
691 if (new != blk_start
) {
698 /* update the free space range with the probed blk_start */
699 if (info
->res
&& blk_start
> info
->res
->start
) {
700 info
->res
->start
= max(info
->res
->start
, blk_start
);
701 if (info
->res
->start
> info
->res
->end
)
702 info
->res
->end
= info
->res
->start
- 1;
706 info
->available
-= blk_start
- nd_mapping
->start
;
712 * nd_blk_available_dpa - account the unused dpa of BLK region
713 * @nd_mapping: container of dpa-resource-root + labels
715 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges, but
716 * we arrange for them to never start at an lower dpa than the last
717 * PMEM allocation in an aliased region.
719 resource_size_t
nd_blk_available_dpa(struct nd_region
*nd_region
)
721 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(&nd_region
->dev
);
722 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
723 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
724 struct blk_alloc_info info
= {
725 .nd_mapping
= nd_mapping
,
726 .available
= nd_mapping
->size
,
729 struct resource
*res
;
734 device_for_each_child(&nvdimm_bus
->dev
, &info
, alias_dpa_busy
);
736 /* now account for busy blk allocations in unaliased dpa */
737 for_each_dpa_resource(ndd
, res
) {
738 if (strncmp(res
->name
, "blk", 3) != 0)
740 info
.available
-= resource_size(res
);
743 return info
.available
;
747 * nd_pmem_max_contiguous_dpa - For the given dimm+region, return the max
748 * contiguous unallocated dpa range.
749 * @nd_region: constrain available space check to this reference region
750 * @nd_mapping: container of dpa-resource-root + labels
752 resource_size_t
nd_pmem_max_contiguous_dpa(struct nd_region
*nd_region
,
753 struct nd_mapping
*nd_mapping
)
755 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
756 struct nvdimm_bus
*nvdimm_bus
;
757 resource_size_t max
= 0;
758 struct resource
*res
;
760 /* if a dimm is disabled the available capacity is zero */
764 nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
765 if (__reserve_free_pmem(&nd_region
->dev
, nd_mapping
->nvdimm
))
767 for_each_dpa_resource(ndd
, res
) {
768 if (strcmp(res
->name
, "pmem-reserve") != 0)
770 if (resource_size(res
) > max
)
771 max
= resource_size(res
);
773 release_free_pmem(nvdimm_bus
, nd_mapping
);
778 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
779 * @nd_mapping: container of dpa-resource-root + labels
780 * @nd_region: constrain available space check to this reference region
781 * @overlap: calculate available space assuming this level of overlap
783 * Validate that a PMEM label, if present, aligns with the start of an
784 * interleave set and truncate the available size at the lowest BLK
787 * The expectation is that this routine is called multiple times as it
788 * probes for the largest BLK encroachment for any single member DIMM of
789 * the interleave set. Once that value is determined the PMEM-limit for
790 * the set can be established.
792 resource_size_t
nd_pmem_available_dpa(struct nd_region
*nd_region
,
793 struct nd_mapping
*nd_mapping
, resource_size_t
*overlap
)
795 resource_size_t map_start
, map_end
, busy
= 0, available
, blk_start
;
796 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
797 struct resource
*res
;
803 map_start
= nd_mapping
->start
;
804 map_end
= map_start
+ nd_mapping
->size
- 1;
805 blk_start
= max(map_start
, map_end
+ 1 - *overlap
);
806 for_each_dpa_resource(ndd
, res
) {
807 if (res
->start
>= map_start
&& res
->start
< map_end
) {
808 if (strncmp(res
->name
, "blk", 3) == 0)
809 blk_start
= min(blk_start
,
810 max(map_start
, res
->start
));
811 else if (res
->end
> map_end
) {
812 reason
= "misaligned to iset";
815 busy
+= resource_size(res
);
816 } else if (res
->end
>= map_start
&& res
->end
<= map_end
) {
817 if (strncmp(res
->name
, "blk", 3) == 0) {
819 * If a BLK allocation overlaps the start of
820 * PMEM the entire interleave set may now only
823 blk_start
= map_start
;
825 busy
+= resource_size(res
);
826 } else if (map_start
> res
->start
&& map_start
< res
->end
) {
827 /* total eclipse of the mapping */
828 busy
+= nd_mapping
->size
;
829 blk_start
= map_start
;
833 *overlap
= map_end
+ 1 - blk_start
;
834 available
= blk_start
- map_start
;
835 if (busy
< available
)
836 return available
- busy
;
840 nd_dbg_dpa(nd_region
, ndd
, res
, "%s\n", reason
);
844 void nvdimm_free_dpa(struct nvdimm_drvdata
*ndd
, struct resource
*res
)
846 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
848 __release_region(&ndd
->dpa
, res
->start
, resource_size(res
));
851 struct resource
*nvdimm_allocate_dpa(struct nvdimm_drvdata
*ndd
,
852 struct nd_label_id
*label_id
, resource_size_t start
,
855 char *name
= kmemdup(label_id
, sizeof(*label_id
), GFP_KERNEL
);
856 struct resource
*res
;
861 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
862 res
= __request_region(&ndd
->dpa
, start
, n
, name
, 0);
869 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
870 * @nvdimm: container of dpa-resource-root + labels
871 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
873 resource_size_t
nvdimm_allocated_dpa(struct nvdimm_drvdata
*ndd
,
874 struct nd_label_id
*label_id
)
876 resource_size_t allocated
= 0;
877 struct resource
*res
;
879 for_each_dpa_resource(ndd
, res
)
880 if (strcmp(res
->name
, label_id
->id
) == 0)
881 allocated
+= resource_size(res
);
886 static int count_dimms(struct device
*dev
, void *c
)
895 int nvdimm_bus_check_dimm_count(struct nvdimm_bus
*nvdimm_bus
, int dimm_count
)
898 /* Flush any possible dimm registration failures */
901 device_for_each_child(&nvdimm_bus
->dev
, &count
, count_dimms
);
902 dev_dbg(&nvdimm_bus
->dev
, "count: %d\n", count
);
903 if (count
!= dimm_count
)
907 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count
);
909 void __exit
nvdimm_devs_exit(void)
911 ida_destroy(&dimm_ida
);