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/vmalloc.h>
15 #include <linux/device.h>
16 #include <linux/ndctl.h>
17 #include <linux/slab.h>
25 static DEFINE_IDA(dimm_ida
);
28 * Retrieve bus and dimm handle and return if this bus supports
29 * get_config_data commands
31 static int __validate_dimm(struct nvdimm_drvdata
*ndd
)
33 struct nvdimm
*nvdimm
;
38 nvdimm
= to_nvdimm(ndd
->dev
);
40 if (!nvdimm
->dsm_mask
)
42 if (!test_bit(ND_CMD_GET_CONFIG_DATA
, nvdimm
->dsm_mask
))
48 static int validate_dimm(struct nvdimm_drvdata
*ndd
)
50 int rc
= __validate_dimm(ndd
);
53 dev_dbg(ndd
->dev
, "%pf: %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
);
73 return 0; /* already valid */
75 memset(cmd
, 0, sizeof(*cmd
));
76 nd_desc
= nvdimm_bus
->nd_desc
;
77 return nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
78 ND_CMD_GET_CONFIG_SIZE
, cmd
, sizeof(*cmd
));
81 int nvdimm_init_config_data(struct nvdimm_drvdata
*ndd
)
83 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
84 struct nd_cmd_get_config_data_hdr
*cmd
;
85 struct nvdimm_bus_descriptor
*nd_desc
;
86 int rc
= validate_dimm(ndd
);
87 u32 max_cmd_size
, config_size
;
96 if (ndd
->nsarea
.status
|| ndd
->nsarea
.max_xfer
== 0
97 || ndd
->nsarea
.config_size
< ND_LABEL_MIN_SIZE
) {
98 dev_dbg(ndd
->dev
, "failed to init config data area: (%d:%d)\n",
99 ndd
->nsarea
.max_xfer
, ndd
->nsarea
.config_size
);
103 ndd
->data
= kmalloc(ndd
->nsarea
.config_size
, GFP_KERNEL
);
105 ndd
->data
= vmalloc(ndd
->nsarea
.config_size
);
110 max_cmd_size
= min_t(u32
, PAGE_SIZE
, ndd
->nsarea
.max_xfer
);
111 cmd
= kzalloc(max_cmd_size
+ sizeof(*cmd
), GFP_KERNEL
);
115 nd_desc
= nvdimm_bus
->nd_desc
;
116 for (config_size
= ndd
->nsarea
.config_size
, offset
= 0;
117 config_size
; config_size
-= cmd
->in_length
,
118 offset
+= cmd
->in_length
) {
119 cmd
->in_length
= min(config_size
, max_cmd_size
);
120 cmd
->in_offset
= offset
;
121 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
122 ND_CMD_GET_CONFIG_DATA
, cmd
,
123 cmd
->in_length
+ sizeof(*cmd
));
124 if (rc
|| cmd
->status
) {
128 memcpy(ndd
->data
+ offset
, cmd
->out_buf
, cmd
->in_length
);
130 dev_dbg(ndd
->dev
, "%s: len: %zu rc: %d\n", __func__
, offset
, rc
);
136 int nvdimm_set_config_data(struct nvdimm_drvdata
*ndd
, size_t offset
,
137 void *buf
, size_t len
)
139 int rc
= validate_dimm(ndd
);
140 size_t max_cmd_size
, buf_offset
;
141 struct nd_cmd_set_config_hdr
*cmd
;
142 struct nvdimm_bus
*nvdimm_bus
= walk_to_nvdimm_bus(ndd
->dev
);
143 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
, PAGE_SIZE
, len
);
155 max_cmd_size
= min_t(u32
, max_cmd_size
, ndd
->nsarea
.max_xfer
);
156 cmd
= kzalloc(max_cmd_size
+ sizeof(*cmd
) + sizeof(u32
), GFP_KERNEL
);
160 for (buf_offset
= 0; len
; len
-= cmd
->in_length
,
161 buf_offset
+= cmd
->in_length
) {
165 cmd
->in_offset
= offset
+ buf_offset
;
166 cmd
->in_length
= min(max_cmd_size
, len
);
167 memcpy(cmd
->in_buf
, buf
+ buf_offset
, cmd
->in_length
);
169 /* status is output in the last 4-bytes of the command buffer */
170 cmd_size
= sizeof(*cmd
) + cmd
->in_length
+ sizeof(u32
);
171 status
= ((void *) cmd
) + cmd_size
- sizeof(u32
);
173 rc
= nd_desc
->ndctl(nd_desc
, to_nvdimm(ndd
->dev
),
174 ND_CMD_SET_CONFIG_DATA
, cmd
, cmd_size
);
176 rc
= rc
? rc
: -ENXIO
;
185 static void nvdimm_release(struct device
*dev
)
187 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
189 ida_simple_remove(&dimm_ida
, nvdimm
->id
);
193 static struct device_type nvdimm_device_type
= {
195 .release
= nvdimm_release
,
198 bool is_nvdimm(struct device
*dev
)
200 return dev
->type
== &nvdimm_device_type
;
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
*nd_blk_region_to_dimm(struct nd_blk_region
*ndbr
)
214 struct nd_region
*nd_region
= &ndbr
->nd_region
;
215 struct nd_mapping
*nd_mapping
= &nd_region
->mapping
[0];
217 return nd_mapping
->nvdimm
;
219 EXPORT_SYMBOL_GPL(nd_blk_region_to_dimm
);
221 struct nvdimm_drvdata
*to_ndd(struct nd_mapping
*nd_mapping
)
223 struct nvdimm
*nvdimm
= nd_mapping
->nvdimm
;
225 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm
->dev
));
227 return dev_get_drvdata(&nvdimm
->dev
);
229 EXPORT_SYMBOL(to_ndd
);
231 void nvdimm_drvdata_release(struct kref
*kref
)
233 struct nvdimm_drvdata
*ndd
= container_of(kref
, typeof(*ndd
), kref
);
234 struct device
*dev
= ndd
->dev
;
235 struct resource
*res
, *_r
;
237 dev_dbg(dev
, "%s\n", __func__
);
239 nvdimm_bus_lock(dev
);
240 for_each_dpa_resource_safe(ndd
, res
, _r
)
241 nvdimm_free_dpa(ndd
, res
);
242 nvdimm_bus_unlock(dev
);
244 if (ndd
->data
&& is_vmalloc_addr(ndd
->data
))
252 void get_ndd(struct nvdimm_drvdata
*ndd
)
254 kref_get(&ndd
->kref
);
257 void put_ndd(struct nvdimm_drvdata
*ndd
)
260 kref_put(&ndd
->kref
, nvdimm_drvdata_release
);
263 const char *nvdimm_name(struct nvdimm
*nvdimm
)
265 return dev_name(&nvdimm
->dev
);
267 EXPORT_SYMBOL_GPL(nvdimm_name
);
269 void *nvdimm_provider_data(struct nvdimm
*nvdimm
)
272 return nvdimm
->provider_data
;
275 EXPORT_SYMBOL_GPL(nvdimm_provider_data
);
277 static ssize_t
commands_show(struct device
*dev
,
278 struct device_attribute
*attr
, char *buf
)
280 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
283 if (!nvdimm
->dsm_mask
)
284 return sprintf(buf
, "\n");
286 for_each_set_bit(cmd
, nvdimm
->dsm_mask
, BITS_PER_LONG
)
287 len
+= sprintf(buf
+ len
, "%s ", nvdimm_cmd_name(cmd
));
288 len
+= sprintf(buf
+ len
, "\n");
291 static DEVICE_ATTR_RO(commands
);
293 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
296 struct nvdimm
*nvdimm
= to_nvdimm(dev
);
299 * The state may be in the process of changing, userspace should
300 * quiesce probing if it wants a static answer
302 nvdimm_bus_lock(dev
);
303 nvdimm_bus_unlock(dev
);
304 return sprintf(buf
, "%s\n", atomic_read(&nvdimm
->busy
)
305 ? "active" : "idle");
307 static DEVICE_ATTR_RO(state
);
309 static ssize_t
available_slots_show(struct device
*dev
,
310 struct device_attribute
*attr
, char *buf
)
312 struct nvdimm_drvdata
*ndd
= dev_get_drvdata(dev
);
319 nvdimm_bus_lock(dev
);
320 nfree
= nd_label_nfree(ndd
);
321 if (nfree
- 1 > nfree
) {
322 dev_WARN_ONCE(dev
, 1, "we ate our last label?\n");
326 rc
= sprintf(buf
, "%d\n", nfree
);
327 nvdimm_bus_unlock(dev
);
330 static DEVICE_ATTR_RO(available_slots
);
332 static struct attribute
*nvdimm_attributes
[] = {
333 &dev_attr_state
.attr
,
334 &dev_attr_commands
.attr
,
335 &dev_attr_available_slots
.attr
,
339 struct attribute_group nvdimm_attribute_group
= {
340 .attrs
= nvdimm_attributes
,
342 EXPORT_SYMBOL_GPL(nvdimm_attribute_group
);
344 struct nvdimm
*nvdimm_create(struct nvdimm_bus
*nvdimm_bus
, void *provider_data
,
345 const struct attribute_group
**groups
, unsigned long flags
,
346 unsigned long *dsm_mask
)
348 struct nvdimm
*nvdimm
= kzalloc(sizeof(*nvdimm
), GFP_KERNEL
);
354 nvdimm
->id
= ida_simple_get(&dimm_ida
, 0, 0, GFP_KERNEL
);
355 if (nvdimm
->id
< 0) {
359 nvdimm
->provider_data
= provider_data
;
360 nvdimm
->flags
= flags
;
361 nvdimm
->dsm_mask
= dsm_mask
;
362 atomic_set(&nvdimm
->busy
, 0);
364 dev_set_name(dev
, "nmem%d", nvdimm
->id
);
365 dev
->parent
= &nvdimm_bus
->dev
;
366 dev
->type
= &nvdimm_device_type
;
367 dev
->devt
= MKDEV(nvdimm_major
, nvdimm
->id
);
368 dev
->groups
= groups
;
369 nd_device_register(dev
);
373 EXPORT_SYMBOL_GPL(nvdimm_create
);
376 * nd_blk_available_dpa - account the unused dpa of BLK region
377 * @nd_mapping: container of dpa-resource-root + labels
379 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges.
381 resource_size_t
nd_blk_available_dpa(struct nd_mapping
*nd_mapping
)
383 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
384 resource_size_t map_end
, busy
= 0, available
;
385 struct resource
*res
;
390 map_end
= nd_mapping
->start
+ nd_mapping
->size
- 1;
391 for_each_dpa_resource(ndd
, res
)
392 if (res
->start
>= nd_mapping
->start
&& res
->start
< map_end
) {
393 resource_size_t end
= min(map_end
, res
->end
);
395 busy
+= end
- res
->start
+ 1;
396 } else if (res
->end
>= nd_mapping
->start
397 && res
->end
<= map_end
) {
398 busy
+= res
->end
- nd_mapping
->start
;
399 } else if (nd_mapping
->start
> res
->start
400 && nd_mapping
->start
< res
->end
) {
401 /* total eclipse of the BLK region mapping */
402 busy
+= nd_mapping
->size
;
405 available
= map_end
- nd_mapping
->start
+ 1;
406 if (busy
< available
)
407 return available
- busy
;
412 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
413 * @nd_mapping: container of dpa-resource-root + labels
414 * @nd_region: constrain available space check to this reference region
415 * @overlap: calculate available space assuming this level of overlap
417 * Validate that a PMEM label, if present, aligns with the start of an
418 * interleave set and truncate the available size at the lowest BLK
421 * The expectation is that this routine is called multiple times as it
422 * probes for the largest BLK encroachment for any single member DIMM of
423 * the interleave set. Once that value is determined the PMEM-limit for
424 * the set can be established.
426 resource_size_t
nd_pmem_available_dpa(struct nd_region
*nd_region
,
427 struct nd_mapping
*nd_mapping
, resource_size_t
*overlap
)
429 resource_size_t map_start
, map_end
, busy
= 0, available
, blk_start
;
430 struct nvdimm_drvdata
*ndd
= to_ndd(nd_mapping
);
431 struct resource
*res
;
437 map_start
= nd_mapping
->start
;
438 map_end
= map_start
+ nd_mapping
->size
- 1;
439 blk_start
= max(map_start
, map_end
+ 1 - *overlap
);
440 for_each_dpa_resource(ndd
, res
)
441 if (res
->start
>= map_start
&& res
->start
< map_end
) {
442 if (strncmp(res
->name
, "blk", 3) == 0)
443 blk_start
= min(blk_start
, res
->start
);
444 else if (res
->start
!= map_start
) {
445 reason
= "misaligned to iset";
449 reason
= "duplicate overlapping PMEM reservations?";
452 busy
+= resource_size(res
);
455 } else if (res
->end
>= map_start
&& res
->end
<= map_end
) {
456 if (strncmp(res
->name
, "blk", 3) == 0) {
458 * If a BLK allocation overlaps the start of
459 * PMEM the entire interleave set may now only
462 blk_start
= map_start
;
464 reason
= "misaligned to iset";
467 } else if (map_start
> res
->start
&& map_start
< res
->end
) {
468 /* total eclipse of the mapping */
469 busy
+= nd_mapping
->size
;
470 blk_start
= map_start
;
473 *overlap
= map_end
+ 1 - blk_start
;
474 available
= blk_start
- map_start
;
475 if (busy
< available
)
476 return available
- busy
;
481 * Something is wrong, PMEM must align with the start of the
482 * interleave set, and there can only be one allocation per set.
484 nd_dbg_dpa(nd_region
, ndd
, res
, "%s\n", reason
);
488 void nvdimm_free_dpa(struct nvdimm_drvdata
*ndd
, struct resource
*res
)
490 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
492 __release_region(&ndd
->dpa
, res
->start
, resource_size(res
));
495 struct resource
*nvdimm_allocate_dpa(struct nvdimm_drvdata
*ndd
,
496 struct nd_label_id
*label_id
, resource_size_t start
,
499 char *name
= kmemdup(label_id
, sizeof(*label_id
), GFP_KERNEL
);
500 struct resource
*res
;
505 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd
->dev
));
506 res
= __request_region(&ndd
->dpa
, start
, n
, name
, 0);
513 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
514 * @nvdimm: container of dpa-resource-root + labels
515 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
517 resource_size_t
nvdimm_allocated_dpa(struct nvdimm_drvdata
*ndd
,
518 struct nd_label_id
*label_id
)
520 resource_size_t allocated
= 0;
521 struct resource
*res
;
523 for_each_dpa_resource(ndd
, res
)
524 if (strcmp(res
->name
, label_id
->id
) == 0)
525 allocated
+= resource_size(res
);
530 static int count_dimms(struct device
*dev
, void *c
)
539 int nvdimm_bus_check_dimm_count(struct nvdimm_bus
*nvdimm_bus
, int dimm_count
)
542 /* Flush any possible dimm registration failures */
545 device_for_each_child(&nvdimm_bus
->dev
, &count
, count_dimms
);
546 dev_dbg(&nvdimm_bus
->dev
, "%s: count: %d\n", __func__
, count
);
547 if (count
!= dimm_count
)
551 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count
);