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>
24 static DEFINE_IDA(dimm_ida);
27 * Retrieve bus and dimm handle and return if this bus supports
28 * get_config_data commands
30 static int __validate_dimm(struct nvdimm_drvdata *ndd)
32 struct nvdimm *nvdimm;
37 nvdimm = to_nvdimm(ndd->dev);
39 if (!nvdimm->dsm_mask)
41 if (!test_bit(ND_CMD_GET_CONFIG_DATA, nvdimm->dsm_mask))
47 static int validate_dimm(struct nvdimm_drvdata *ndd)
49 int rc = __validate_dimm(ndd);
52 dev_dbg(ndd->dev, "%pf: %s error: %d\n",
53 __builtin_return_address(0), __func__, rc);
58 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
59 * @nvdimm: dimm to initialize
61 int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
63 struct nd_cmd_get_config_size *cmd = &ndd->nsarea;
64 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
65 struct nvdimm_bus_descriptor *nd_desc;
66 int rc = validate_dimm(ndd);
72 return 0; /* already valid */
74 memset(cmd, 0, sizeof(*cmd));
75 nd_desc = nvdimm_bus->nd_desc;
76 return nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
77 ND_CMD_GET_CONFIG_SIZE, cmd, sizeof(*cmd));
80 int nvdimm_init_config_data(struct nvdimm_drvdata *ndd)
82 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(ndd->dev);
83 struct nd_cmd_get_config_data_hdr *cmd;
84 struct nvdimm_bus_descriptor *nd_desc;
85 int rc = validate_dimm(ndd);
86 u32 max_cmd_size, config_size;
95 if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0
96 || ndd->nsarea.config_size < ND_LABEL_MIN_SIZE) {
97 dev_dbg(ndd->dev, "failed to init config data area: (%d:%d)\n",
98 ndd->nsarea.max_xfer, ndd->nsarea.config_size);
102 ndd->data = kmalloc(ndd->nsarea.config_size, GFP_KERNEL);
104 ndd->data = vmalloc(ndd->nsarea.config_size);
109 max_cmd_size = min_t(u32, PAGE_SIZE, ndd->nsarea.max_xfer);
110 cmd = kzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
114 nd_desc = nvdimm_bus->nd_desc;
115 for (config_size = ndd->nsarea.config_size, offset = 0;
116 config_size; config_size -= cmd->in_length,
117 offset += cmd->in_length) {
118 cmd->in_length = min(config_size, max_cmd_size);
119 cmd->in_offset = offset;
120 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
121 ND_CMD_GET_CONFIG_DATA, cmd,
122 cmd->in_length + sizeof(*cmd));
123 if (rc || cmd->status) {
127 memcpy(ndd->data + offset, cmd->out_buf, cmd->in_length);
129 dev_dbg(ndd->dev, "%s: len: %zu rc: %d\n", __func__, offset, rc);
135 static void nvdimm_release(struct device *dev)
137 struct nvdimm *nvdimm = to_nvdimm(dev);
139 ida_simple_remove(&dimm_ida, nvdimm->id);
143 static struct device_type nvdimm_device_type = {
145 .release = nvdimm_release,
148 bool is_nvdimm(struct device *dev)
150 return dev->type == &nvdimm_device_type;
153 struct nvdimm *to_nvdimm(struct device *dev)
155 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
157 WARN_ON(!is_nvdimm(dev));
160 EXPORT_SYMBOL_GPL(to_nvdimm);
162 struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
164 struct nvdimm *nvdimm = nd_mapping->nvdimm;
166 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
168 return dev_get_drvdata(&nvdimm->dev);
170 EXPORT_SYMBOL(to_ndd);
172 void nvdimm_drvdata_release(struct kref *kref)
174 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
175 struct device *dev = ndd->dev;
176 struct resource *res, *_r;
178 dev_dbg(dev, "%s\n", __func__);
180 nvdimm_bus_lock(dev);
181 for_each_dpa_resource_safe(ndd, res, _r)
182 nvdimm_free_dpa(ndd, res);
183 nvdimm_bus_unlock(dev);
185 if (ndd->data && is_vmalloc_addr(ndd->data))
193 void get_ndd(struct nvdimm_drvdata *ndd)
195 kref_get(&ndd->kref);
198 void put_ndd(struct nvdimm_drvdata *ndd)
201 kref_put(&ndd->kref, nvdimm_drvdata_release);
204 const char *nvdimm_name(struct nvdimm *nvdimm)
206 return dev_name(&nvdimm->dev);
208 EXPORT_SYMBOL_GPL(nvdimm_name);
210 void *nvdimm_provider_data(struct nvdimm *nvdimm)
213 return nvdimm->provider_data;
216 EXPORT_SYMBOL_GPL(nvdimm_provider_data);
218 static ssize_t commands_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
221 struct nvdimm *nvdimm = to_nvdimm(dev);
224 if (!nvdimm->dsm_mask)
225 return sprintf(buf, "\n");
227 for_each_set_bit(cmd, nvdimm->dsm_mask, BITS_PER_LONG)
228 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
229 len += sprintf(buf + len, "\n");
232 static DEVICE_ATTR_RO(commands);
234 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
237 struct nvdimm *nvdimm = to_nvdimm(dev);
240 * The state may be in the process of changing, userspace should
241 * quiesce probing if it wants a static answer
243 nvdimm_bus_lock(dev);
244 nvdimm_bus_unlock(dev);
245 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
246 ? "active" : "idle");
248 static DEVICE_ATTR_RO(state);
250 static struct attribute *nvdimm_attributes[] = {
251 &dev_attr_state.attr,
252 &dev_attr_commands.attr,
256 struct attribute_group nvdimm_attribute_group = {
257 .attrs = nvdimm_attributes,
259 EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
261 struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
262 const struct attribute_group **groups, unsigned long flags,
263 unsigned long *dsm_mask)
265 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
271 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
272 if (nvdimm->id < 0) {
276 nvdimm->provider_data = provider_data;
277 nvdimm->flags = flags;
278 nvdimm->dsm_mask = dsm_mask;
279 atomic_set(&nvdimm->busy, 0);
281 dev_set_name(dev, "nmem%d", nvdimm->id);
282 dev->parent = &nvdimm_bus->dev;
283 dev->type = &nvdimm_device_type;
284 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
285 dev->groups = groups;
286 nd_device_register(dev);
290 EXPORT_SYMBOL_GPL(nvdimm_create);
293 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
294 * @nd_mapping: container of dpa-resource-root + labels
295 * @nd_region: constrain available space check to this reference region
296 * @overlap: calculate available space assuming this level of overlap
298 * Validate that a PMEM label, if present, aligns with the start of an
299 * interleave set and truncate the available size at the lowest BLK
302 * The expectation is that this routine is called multiple times as it
303 * probes for the largest BLK encroachment for any single member DIMM of
304 * the interleave set. Once that value is determined the PMEM-limit for
305 * the set can be established.
307 resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
308 struct nd_mapping *nd_mapping, resource_size_t *overlap)
310 resource_size_t map_start, map_end, busy = 0, available, blk_start;
311 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
312 struct resource *res;
318 map_start = nd_mapping->start;
319 map_end = map_start + nd_mapping->size - 1;
320 blk_start = max(map_start, map_end + 1 - *overlap);
321 for_each_dpa_resource(ndd, res)
322 if (res->start >= map_start && res->start < map_end) {
323 if (strncmp(res->name, "blk", 3) == 0)
324 blk_start = min(blk_start, res->start);
325 else if (res->start != map_start) {
326 reason = "misaligned to iset";
330 reason = "duplicate overlapping PMEM reservations?";
333 busy += resource_size(res);
336 } else if (res->end >= map_start && res->end <= map_end) {
337 if (strncmp(res->name, "blk", 3) == 0) {
339 * If a BLK allocation overlaps the start of
340 * PMEM the entire interleave set may now only
343 blk_start = map_start;
345 reason = "misaligned to iset";
348 } else if (map_start > res->start && map_start < res->end) {
349 /* total eclipse of the mapping */
350 busy += nd_mapping->size;
351 blk_start = map_start;
354 *overlap = map_end + 1 - blk_start;
355 available = blk_start - map_start;
356 if (busy < available)
357 return available - busy;
362 * Something is wrong, PMEM must align with the start of the
363 * interleave set, and there can only be one allocation per set.
365 nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
369 void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
371 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
373 __release_region(&ndd->dpa, res->start, resource_size(res));
376 struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
377 struct nd_label_id *label_id, resource_size_t start,
380 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
381 struct resource *res;
386 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
387 res = __request_region(&ndd->dpa, start, n, name, 0);
394 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
395 * @nvdimm: container of dpa-resource-root + labels
396 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
398 resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
399 struct nd_label_id *label_id)
401 resource_size_t allocated = 0;
402 struct resource *res;
404 for_each_dpa_resource(ndd, res)
405 if (strcmp(res->name, label_id->id) == 0)
406 allocated += resource_size(res);
411 static int count_dimms(struct device *dev, void *c)
420 int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
423 /* Flush any possible dimm registration failures */
426 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
427 dev_dbg(&nvdimm_bus->dev, "%s: count: %d\n", __func__, count);
428 if (count != dimm_count)
432 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);