libnvdimm: write blk label set
[deliverable/linux.git] / drivers / nvdimm / dimm_devs.c
1 /*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
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.
7 *
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.
12 */
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>
18 #include <linux/io.h>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include "nd-core.h"
22 #include "label.h"
23 #include "nd.h"
24
25 static DEFINE_IDA(dimm_ida);
26
27 /*
28 * Retrieve bus and dimm handle and return if this bus supports
29 * get_config_data commands
30 */
31 static int __validate_dimm(struct nvdimm_drvdata *ndd)
32 {
33 struct nvdimm *nvdimm;
34
35 if (!ndd)
36 return -EINVAL;
37
38 nvdimm = to_nvdimm(ndd->dev);
39
40 if (!nvdimm->dsm_mask)
41 return -ENXIO;
42 if (!test_bit(ND_CMD_GET_CONFIG_DATA, nvdimm->dsm_mask))
43 return -ENXIO;
44
45 return 0;
46 }
47
48 static int validate_dimm(struct nvdimm_drvdata *ndd)
49 {
50 int rc = __validate_dimm(ndd);
51
52 if (rc && ndd)
53 dev_dbg(ndd->dev, "%pf: %s error: %d\n",
54 __builtin_return_address(0), __func__, rc);
55 return rc;
56 }
57
58 /**
59 * nvdimm_init_nsarea - determine the geometry of a dimm's namespace area
60 * @nvdimm: dimm to initialize
61 */
62 int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd)
63 {
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);
68
69 if (rc)
70 return rc;
71
72 if (cmd->config_size)
73 return 0; /* already valid */
74
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));
79 }
80
81 int nvdimm_init_config_data(struct nvdimm_drvdata *ndd)
82 {
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;
88 size_t offset;
89
90 if (rc)
91 return rc;
92
93 if (ndd->data)
94 return 0;
95
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);
100 return -ENXIO;
101 }
102
103 ndd->data = kmalloc(ndd->nsarea.config_size, GFP_KERNEL);
104 if (!ndd->data)
105 ndd->data = vmalloc(ndd->nsarea.config_size);
106
107 if (!ndd->data)
108 return -ENOMEM;
109
110 max_cmd_size = min_t(u32, PAGE_SIZE, ndd->nsarea.max_xfer);
111 cmd = kzalloc(max_cmd_size + sizeof(*cmd), GFP_KERNEL);
112 if (!cmd)
113 return -ENOMEM;
114
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) {
125 rc = -ENXIO;
126 break;
127 }
128 memcpy(ndd->data + offset, cmd->out_buf, cmd->in_length);
129 }
130 dev_dbg(ndd->dev, "%s: len: %zu rc: %d\n", __func__, offset, rc);
131 kfree(cmd);
132
133 return rc;
134 }
135
136 int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
137 void *buf, size_t len)
138 {
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;
144
145 if (rc)
146 return rc;
147
148 if (!ndd->data)
149 return -ENXIO;
150
151 if (offset + len > ndd->nsarea.config_size)
152 return -ENXIO;
153
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);
157 if (!cmd)
158 return -ENOMEM;
159
160 for (buf_offset = 0; len; len -= cmd->in_length,
161 buf_offset += cmd->in_length) {
162 size_t cmd_size;
163 u32 *status;
164
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);
168
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);
172
173 rc = nd_desc->ndctl(nd_desc, to_nvdimm(ndd->dev),
174 ND_CMD_SET_CONFIG_DATA, cmd, cmd_size);
175 if (rc || *status) {
176 rc = rc ? rc : -ENXIO;
177 break;
178 }
179 }
180 kfree(cmd);
181
182 return rc;
183 }
184
185 static void nvdimm_release(struct device *dev)
186 {
187 struct nvdimm *nvdimm = to_nvdimm(dev);
188
189 ida_simple_remove(&dimm_ida, nvdimm->id);
190 kfree(nvdimm);
191 }
192
193 static struct device_type nvdimm_device_type = {
194 .name = "nvdimm",
195 .release = nvdimm_release,
196 };
197
198 bool is_nvdimm(struct device *dev)
199 {
200 return dev->type == &nvdimm_device_type;
201 }
202
203 struct nvdimm *to_nvdimm(struct device *dev)
204 {
205 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
206
207 WARN_ON(!is_nvdimm(dev));
208 return nvdimm;
209 }
210 EXPORT_SYMBOL_GPL(to_nvdimm);
211
212 struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping)
213 {
214 struct nvdimm *nvdimm = nd_mapping->nvdimm;
215
216 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm->dev));
217
218 return dev_get_drvdata(&nvdimm->dev);
219 }
220 EXPORT_SYMBOL(to_ndd);
221
222 void nvdimm_drvdata_release(struct kref *kref)
223 {
224 struct nvdimm_drvdata *ndd = container_of(kref, typeof(*ndd), kref);
225 struct device *dev = ndd->dev;
226 struct resource *res, *_r;
227
228 dev_dbg(dev, "%s\n", __func__);
229
230 nvdimm_bus_lock(dev);
231 for_each_dpa_resource_safe(ndd, res, _r)
232 nvdimm_free_dpa(ndd, res);
233 nvdimm_bus_unlock(dev);
234
235 if (ndd->data && is_vmalloc_addr(ndd->data))
236 vfree(ndd->data);
237 else
238 kfree(ndd->data);
239 kfree(ndd);
240 put_device(dev);
241 }
242
243 void get_ndd(struct nvdimm_drvdata *ndd)
244 {
245 kref_get(&ndd->kref);
246 }
247
248 void put_ndd(struct nvdimm_drvdata *ndd)
249 {
250 if (ndd)
251 kref_put(&ndd->kref, nvdimm_drvdata_release);
252 }
253
254 const char *nvdimm_name(struct nvdimm *nvdimm)
255 {
256 return dev_name(&nvdimm->dev);
257 }
258 EXPORT_SYMBOL_GPL(nvdimm_name);
259
260 void *nvdimm_provider_data(struct nvdimm *nvdimm)
261 {
262 if (nvdimm)
263 return nvdimm->provider_data;
264 return NULL;
265 }
266 EXPORT_SYMBOL_GPL(nvdimm_provider_data);
267
268 static ssize_t commands_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270 {
271 struct nvdimm *nvdimm = to_nvdimm(dev);
272 int cmd, len = 0;
273
274 if (!nvdimm->dsm_mask)
275 return sprintf(buf, "\n");
276
277 for_each_set_bit(cmd, nvdimm->dsm_mask, BITS_PER_LONG)
278 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
279 len += sprintf(buf + len, "\n");
280 return len;
281 }
282 static DEVICE_ATTR_RO(commands);
283
284 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
285 char *buf)
286 {
287 struct nvdimm *nvdimm = to_nvdimm(dev);
288
289 /*
290 * The state may be in the process of changing, userspace should
291 * quiesce probing if it wants a static answer
292 */
293 nvdimm_bus_lock(dev);
294 nvdimm_bus_unlock(dev);
295 return sprintf(buf, "%s\n", atomic_read(&nvdimm->busy)
296 ? "active" : "idle");
297 }
298 static DEVICE_ATTR_RO(state);
299
300 static ssize_t available_slots_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
302 {
303 struct nvdimm_drvdata *ndd = dev_get_drvdata(dev);
304 ssize_t rc;
305 u32 nfree;
306
307 if (!ndd)
308 return -ENXIO;
309
310 nvdimm_bus_lock(dev);
311 nfree = nd_label_nfree(ndd);
312 if (nfree - 1 > nfree) {
313 dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
314 nfree = 0;
315 } else
316 nfree--;
317 rc = sprintf(buf, "%d\n", nfree);
318 nvdimm_bus_unlock(dev);
319 return rc;
320 }
321 static DEVICE_ATTR_RO(available_slots);
322
323 static struct attribute *nvdimm_attributes[] = {
324 &dev_attr_state.attr,
325 &dev_attr_commands.attr,
326 &dev_attr_available_slots.attr,
327 NULL,
328 };
329
330 struct attribute_group nvdimm_attribute_group = {
331 .attrs = nvdimm_attributes,
332 };
333 EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
334
335 struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
336 const struct attribute_group **groups, unsigned long flags,
337 unsigned long *dsm_mask)
338 {
339 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
340 struct device *dev;
341
342 if (!nvdimm)
343 return NULL;
344
345 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
346 if (nvdimm->id < 0) {
347 kfree(nvdimm);
348 return NULL;
349 }
350 nvdimm->provider_data = provider_data;
351 nvdimm->flags = flags;
352 nvdimm->dsm_mask = dsm_mask;
353 atomic_set(&nvdimm->busy, 0);
354 dev = &nvdimm->dev;
355 dev_set_name(dev, "nmem%d", nvdimm->id);
356 dev->parent = &nvdimm_bus->dev;
357 dev->type = &nvdimm_device_type;
358 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
359 dev->groups = groups;
360 nd_device_register(dev);
361
362 return nvdimm;
363 }
364 EXPORT_SYMBOL_GPL(nvdimm_create);
365
366 /**
367 * nd_blk_available_dpa - account the unused dpa of BLK region
368 * @nd_mapping: container of dpa-resource-root + labels
369 *
370 * Unlike PMEM, BLK namespaces can occupy discontiguous DPA ranges.
371 */
372 resource_size_t nd_blk_available_dpa(struct nd_mapping *nd_mapping)
373 {
374 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
375 resource_size_t map_end, busy = 0, available;
376 struct resource *res;
377
378 if (!ndd)
379 return 0;
380
381 map_end = nd_mapping->start + nd_mapping->size - 1;
382 for_each_dpa_resource(ndd, res)
383 if (res->start >= nd_mapping->start && res->start < map_end) {
384 resource_size_t end = min(map_end, res->end);
385
386 busy += end - res->start + 1;
387 } else if (res->end >= nd_mapping->start
388 && res->end <= map_end) {
389 busy += res->end - nd_mapping->start;
390 } else if (nd_mapping->start > res->start
391 && nd_mapping->start < res->end) {
392 /* total eclipse of the BLK region mapping */
393 busy += nd_mapping->size;
394 }
395
396 available = map_end - nd_mapping->start + 1;
397 if (busy < available)
398 return available - busy;
399 return 0;
400 }
401
402 /**
403 * nd_pmem_available_dpa - for the given dimm+region account unallocated dpa
404 * @nd_mapping: container of dpa-resource-root + labels
405 * @nd_region: constrain available space check to this reference region
406 * @overlap: calculate available space assuming this level of overlap
407 *
408 * Validate that a PMEM label, if present, aligns with the start of an
409 * interleave set and truncate the available size at the lowest BLK
410 * overlap point.
411 *
412 * The expectation is that this routine is called multiple times as it
413 * probes for the largest BLK encroachment for any single member DIMM of
414 * the interleave set. Once that value is determined the PMEM-limit for
415 * the set can be established.
416 */
417 resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
418 struct nd_mapping *nd_mapping, resource_size_t *overlap)
419 {
420 resource_size_t map_start, map_end, busy = 0, available, blk_start;
421 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
422 struct resource *res;
423 const char *reason;
424
425 if (!ndd)
426 return 0;
427
428 map_start = nd_mapping->start;
429 map_end = map_start + nd_mapping->size - 1;
430 blk_start = max(map_start, map_end + 1 - *overlap);
431 for_each_dpa_resource(ndd, res)
432 if (res->start >= map_start && res->start < map_end) {
433 if (strncmp(res->name, "blk", 3) == 0)
434 blk_start = min(blk_start, res->start);
435 else if (res->start != map_start) {
436 reason = "misaligned to iset";
437 goto err;
438 } else {
439 if (busy) {
440 reason = "duplicate overlapping PMEM reservations?";
441 goto err;
442 }
443 busy += resource_size(res);
444 continue;
445 }
446 } else if (res->end >= map_start && res->end <= map_end) {
447 if (strncmp(res->name, "blk", 3) == 0) {
448 /*
449 * If a BLK allocation overlaps the start of
450 * PMEM the entire interleave set may now only
451 * be used for BLK.
452 */
453 blk_start = map_start;
454 } else {
455 reason = "misaligned to iset";
456 goto err;
457 }
458 } else if (map_start > res->start && map_start < res->end) {
459 /* total eclipse of the mapping */
460 busy += nd_mapping->size;
461 blk_start = map_start;
462 }
463
464 *overlap = map_end + 1 - blk_start;
465 available = blk_start - map_start;
466 if (busy < available)
467 return available - busy;
468 return 0;
469
470 err:
471 /*
472 * Something is wrong, PMEM must align with the start of the
473 * interleave set, and there can only be one allocation per set.
474 */
475 nd_dbg_dpa(nd_region, ndd, res, "%s\n", reason);
476 return 0;
477 }
478
479 void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res)
480 {
481 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
482 kfree(res->name);
483 __release_region(&ndd->dpa, res->start, resource_size(res));
484 }
485
486 struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
487 struct nd_label_id *label_id, resource_size_t start,
488 resource_size_t n)
489 {
490 char *name = kmemdup(label_id, sizeof(*label_id), GFP_KERNEL);
491 struct resource *res;
492
493 if (!name)
494 return NULL;
495
496 WARN_ON_ONCE(!is_nvdimm_bus_locked(ndd->dev));
497 res = __request_region(&ndd->dpa, start, n, name, 0);
498 if (!res)
499 kfree(name);
500 return res;
501 }
502
503 /**
504 * nvdimm_allocated_dpa - sum up the dpa currently allocated to this label_id
505 * @nvdimm: container of dpa-resource-root + labels
506 * @label_id: dpa resource name of the form {pmem|blk}-<human readable uuid>
507 */
508 resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
509 struct nd_label_id *label_id)
510 {
511 resource_size_t allocated = 0;
512 struct resource *res;
513
514 for_each_dpa_resource(ndd, res)
515 if (strcmp(res->name, label_id->id) == 0)
516 allocated += resource_size(res);
517
518 return allocated;
519 }
520
521 static int count_dimms(struct device *dev, void *c)
522 {
523 int *count = c;
524
525 if (is_nvdimm(dev))
526 (*count)++;
527 return 0;
528 }
529
530 int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count)
531 {
532 int count = 0;
533 /* Flush any possible dimm registration failures */
534 nd_synchronize();
535
536 device_for_each_child(&nvdimm_bus->dev, &count, count_dimms);
537 dev_dbg(&nvdimm_bus->dev, "%s: count: %d\n", __func__, count);
538 if (count != dimm_count)
539 return -ENXIO;
540 return 0;
541 }
542 EXPORT_SYMBOL_GPL(nvdimm_bus_check_dimm_count);
This page took 0.041752 seconds and 5 git commands to generate.