blob: ac21ce419bebc9c353d8ccea9ce9d4235906b663 [file] [log] [blame]
Dan Williams1f7df6f2015-06-09 20:13:14 -04001/*
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 */
Dan Williamseaf96152015-05-01 13:11:27 -040013#include <linux/scatterlist.h>
14#include <linux/sched.h>
Dan Williams1f7df6f2015-06-09 20:13:14 -040015#include <linux/slab.h>
Dan Williamseaf96152015-05-01 13:11:27 -040016#include <linux/sort.h>
Dan Williams1f7df6f2015-06-09 20:13:14 -040017#include <linux/io.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040018#include <linux/nd.h>
Dan Williams1f7df6f2015-06-09 20:13:14 -040019#include "nd-core.h"
20#include "nd.h"
21
22static DEFINE_IDA(region_ida);
23
24static void nd_region_release(struct device *dev)
25{
26 struct nd_region *nd_region = to_nd_region(dev);
27 u16 i;
28
29 for (i = 0; i < nd_region->ndr_mappings; i++) {
30 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
31 struct nvdimm *nvdimm = nd_mapping->nvdimm;
32
33 put_device(&nvdimm->dev);
34 }
35 ida_simple_remove(&region_ida, nd_region->id);
36 kfree(nd_region);
37}
38
39static struct device_type nd_blk_device_type = {
40 .name = "nd_blk",
41 .release = nd_region_release,
42};
43
44static struct device_type nd_pmem_device_type = {
45 .name = "nd_pmem",
46 .release = nd_region_release,
47};
48
49static struct device_type nd_volatile_device_type = {
50 .name = "nd_volatile",
51 .release = nd_region_release,
52};
53
Dan Williams3d880022015-05-31 15:02:11 -040054bool is_nd_pmem(struct device *dev)
Dan Williams1f7df6f2015-06-09 20:13:14 -040055{
56 return dev ? dev->type == &nd_pmem_device_type : false;
57}
58
Dan Williams3d880022015-05-31 15:02:11 -040059bool is_nd_blk(struct device *dev)
60{
61 return dev ? dev->type == &nd_blk_device_type : false;
62}
63
Dan Williams1f7df6f2015-06-09 20:13:14 -040064struct nd_region *to_nd_region(struct device *dev)
65{
66 struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
67
68 WARN_ON(dev->type->release != nd_region_release);
69 return nd_region;
70}
71EXPORT_SYMBOL_GPL(to_nd_region);
72
Dan Williams3d880022015-05-31 15:02:11 -040073/**
74 * nd_region_to_nstype() - region to an integer namespace type
75 * @nd_region: region-device to interrogate
76 *
77 * This is the 'nstype' attribute of a region as well, an input to the
78 * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
79 * namespace devices with namespace drivers.
80 */
81int nd_region_to_nstype(struct nd_region *nd_region)
82{
83 if (is_nd_pmem(&nd_region->dev)) {
84 u16 i, alias;
85
86 for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) {
87 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
88 struct nvdimm *nvdimm = nd_mapping->nvdimm;
89
90 if (nvdimm->flags & NDD_ALIASING)
91 alias++;
92 }
93 if (alias)
94 return ND_DEVICE_NAMESPACE_PMEM;
95 else
96 return ND_DEVICE_NAMESPACE_IO;
97 } else if (is_nd_blk(&nd_region->dev)) {
98 return ND_DEVICE_NAMESPACE_BLK;
99 }
100
101 return 0;
102}
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400103EXPORT_SYMBOL(nd_region_to_nstype);
104
105static int is_uuid_busy(struct device *dev, void *data)
106{
107 struct nd_region *nd_region = to_nd_region(dev->parent);
108 u8 *uuid = data;
109
110 switch (nd_region_to_nstype(nd_region)) {
111 case ND_DEVICE_NAMESPACE_PMEM: {
112 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
113
114 if (!nspm->uuid)
115 break;
116 if (memcmp(uuid, nspm->uuid, NSLABEL_UUID_LEN) == 0)
117 return -EBUSY;
118 break;
119 }
120 case ND_DEVICE_NAMESPACE_BLK: {
Dan Williams1b40e092015-05-01 13:34:01 -0400121 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
122
123 if (!nsblk->uuid)
124 break;
125 if (memcmp(uuid, nsblk->uuid, NSLABEL_UUID_LEN) == 0)
126 return -EBUSY;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400127 break;
128 }
129 default:
130 break;
131 }
132
133 return 0;
134}
135
136static int is_namespace_uuid_busy(struct device *dev, void *data)
137{
138 if (is_nd_pmem(dev) || is_nd_blk(dev))
139 return device_for_each_child(dev, data, is_uuid_busy);
140 return 0;
141}
142
143/**
144 * nd_is_uuid_unique - verify that no other namespace has @uuid
145 * @dev: any device on a nvdimm_bus
146 * @uuid: uuid to check
147 */
148bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
149{
150 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
151
152 if (!nvdimm_bus)
153 return false;
154 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
155 if (device_for_each_child(&nvdimm_bus->dev, uuid,
156 is_namespace_uuid_busy) != 0)
157 return false;
158 return true;
159}
Dan Williams3d880022015-05-31 15:02:11 -0400160
Dan Williams1f7df6f2015-06-09 20:13:14 -0400161static ssize_t size_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
164 struct nd_region *nd_region = to_nd_region(dev);
165 unsigned long long size = 0;
166
167 if (is_nd_pmem(dev)) {
168 size = nd_region->ndr_size;
169 } else if (nd_region->ndr_mappings == 1) {
170 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
171
172 size = nd_mapping->size;
173 }
174
175 return sprintf(buf, "%llu\n", size);
176}
177static DEVICE_ATTR_RO(size);
178
179static ssize_t mappings_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct nd_region *nd_region = to_nd_region(dev);
183
184 return sprintf(buf, "%d\n", nd_region->ndr_mappings);
185}
186static DEVICE_ATTR_RO(mappings);
187
Dan Williams3d880022015-05-31 15:02:11 -0400188static ssize_t nstype_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 struct nd_region *nd_region = to_nd_region(dev);
192
193 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
194}
195static DEVICE_ATTR_RO(nstype);
196
Dan Williamseaf96152015-05-01 13:11:27 -0400197static ssize_t set_cookie_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
200 struct nd_region *nd_region = to_nd_region(dev);
201 struct nd_interleave_set *nd_set = nd_region->nd_set;
202
203 if (is_nd_pmem(dev) && nd_set)
204 /* pass, should be precluded by region_visible */;
205 else
206 return -ENXIO;
207
208 return sprintf(buf, "%#llx\n", nd_set->cookie);
209}
210static DEVICE_ATTR_RO(set_cookie);
211
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400212resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
213{
214 resource_size_t blk_max_overlap = 0, available, overlap;
215 int i;
216
217 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
218
219 retry:
220 available = 0;
221 overlap = blk_max_overlap;
222 for (i = 0; i < nd_region->ndr_mappings; i++) {
223 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
224 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
225
226 /* if a dimm is disabled the available capacity is zero */
227 if (!ndd)
228 return 0;
229
230 if (is_nd_pmem(&nd_region->dev)) {
231 available += nd_pmem_available_dpa(nd_region,
232 nd_mapping, &overlap);
233 if (overlap > blk_max_overlap) {
234 blk_max_overlap = overlap;
235 goto retry;
236 }
237 } else if (is_nd_blk(&nd_region->dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400238 available += nd_blk_available_dpa(nd_mapping);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400239 }
240 }
241
242 return available;
243}
244
245static ssize_t available_size_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
247{
248 struct nd_region *nd_region = to_nd_region(dev);
249 unsigned long long available = 0;
250
251 /*
252 * Flush in-flight updates and grab a snapshot of the available
253 * size. Of course, this value is potentially invalidated the
254 * memory nvdimm_bus_lock() is dropped, but that's userspace's
255 * problem to not race itself.
256 */
257 nvdimm_bus_lock(dev);
258 wait_nvdimm_bus_probe_idle(dev);
259 available = nd_region_available_dpa(nd_region);
260 nvdimm_bus_unlock(dev);
261
262 return sprintf(buf, "%llu\n", available);
263}
264static DEVICE_ATTR_RO(available_size);
265
Dan Williams3d880022015-05-31 15:02:11 -0400266static ssize_t init_namespaces_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 struct nd_region_namespaces *num_ns = dev_get_drvdata(dev);
270 ssize_t rc;
271
272 nvdimm_bus_lock(dev);
273 if (num_ns)
274 rc = sprintf(buf, "%d/%d\n", num_ns->active, num_ns->count);
275 else
276 rc = -ENXIO;
277 nvdimm_bus_unlock(dev);
278
279 return rc;
280}
281static DEVICE_ATTR_RO(init_namespaces);
282
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400283static ssize_t namespace_seed_show(struct device *dev,
284 struct device_attribute *attr, char *buf)
285{
286 struct nd_region *nd_region = to_nd_region(dev);
287 ssize_t rc;
288
289 nvdimm_bus_lock(dev);
290 if (nd_region->ns_seed)
291 rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
292 else
293 rc = sprintf(buf, "\n");
294 nvdimm_bus_unlock(dev);
295 return rc;
296}
297static DEVICE_ATTR_RO(namespace_seed);
298
Dan Williams1f7df6f2015-06-09 20:13:14 -0400299static struct attribute *nd_region_attributes[] = {
300 &dev_attr_size.attr,
Dan Williams3d880022015-05-31 15:02:11 -0400301 &dev_attr_nstype.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400302 &dev_attr_mappings.attr,
Dan Williamseaf96152015-05-01 13:11:27 -0400303 &dev_attr_set_cookie.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400304 &dev_attr_available_size.attr,
305 &dev_attr_namespace_seed.attr,
Dan Williams3d880022015-05-31 15:02:11 -0400306 &dev_attr_init_namespaces.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400307 NULL,
308};
309
Dan Williamseaf96152015-05-01 13:11:27 -0400310static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
311{
312 struct device *dev = container_of(kobj, typeof(*dev), kobj);
313 struct nd_region *nd_region = to_nd_region(dev);
314 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400315 int type = nd_region_to_nstype(nd_region);
Dan Williamseaf96152015-05-01 13:11:27 -0400316
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400317 if (a != &dev_attr_set_cookie.attr
318 && a != &dev_attr_available_size.attr)
Dan Williamseaf96152015-05-01 13:11:27 -0400319 return a->mode;
320
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400321 if ((type == ND_DEVICE_NAMESPACE_PMEM
322 || type == ND_DEVICE_NAMESPACE_BLK)
323 && a == &dev_attr_available_size.attr)
324 return a->mode;
325 else if (is_nd_pmem(dev) && nd_set)
326 return a->mode;
Dan Williamseaf96152015-05-01 13:11:27 -0400327
328 return 0;
329}
330
Dan Williams1f7df6f2015-06-09 20:13:14 -0400331struct attribute_group nd_region_attribute_group = {
332 .attrs = nd_region_attributes,
Dan Williamseaf96152015-05-01 13:11:27 -0400333 .is_visible = region_visible,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400334};
335EXPORT_SYMBOL_GPL(nd_region_attribute_group);
336
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400337u64 nd_region_interleave_set_cookie(struct nd_region *nd_region)
338{
339 struct nd_interleave_set *nd_set = nd_region->nd_set;
340
341 if (nd_set)
342 return nd_set->cookie;
343 return 0;
344}
345
Dan Williamseaf96152015-05-01 13:11:27 -0400346/*
347 * Upon successful probe/remove, take/release a reference on the
348 * associated interleave set (if present)
349 */
350static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
351 struct device *dev, bool probe)
352{
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400353 if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) {
Dan Williamseaf96152015-05-01 13:11:27 -0400354 struct nd_region *nd_region = to_nd_region(dev);
355 int i;
356
357 for (i = 0; i < nd_region->ndr_mappings; i++) {
358 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400359 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
Dan Williamseaf96152015-05-01 13:11:27 -0400360 struct nvdimm *nvdimm = nd_mapping->nvdimm;
361
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400362 kfree(nd_mapping->labels);
363 nd_mapping->labels = NULL;
364 put_ndd(ndd);
365 nd_mapping->ndd = NULL;
366 atomic_dec(&nvdimm->busy);
Dan Williamseaf96152015-05-01 13:11:27 -0400367 }
Dan Williams1b40e092015-05-01 13:34:01 -0400368 } else if (dev->parent && is_nd_blk(dev->parent) && probe) {
369 struct nd_region *nd_region = to_nd_region(dev->parent);
370
371 nvdimm_bus_lock(dev);
372 if (nd_region->ns_seed == dev)
373 nd_region_create_blk_seed(nd_region);
374 nvdimm_bus_unlock(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400375 }
376}
377
378void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
379{
380 nd_region_notify_driver_action(nvdimm_bus, dev, true);
381}
382
383void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
384{
385 nd_region_notify_driver_action(nvdimm_bus, dev, false);
386}
387
Dan Williams1f7df6f2015-06-09 20:13:14 -0400388static ssize_t mappingN(struct device *dev, char *buf, int n)
389{
390 struct nd_region *nd_region = to_nd_region(dev);
391 struct nd_mapping *nd_mapping;
392 struct nvdimm *nvdimm;
393
394 if (n >= nd_region->ndr_mappings)
395 return -ENXIO;
396 nd_mapping = &nd_region->mapping[n];
397 nvdimm = nd_mapping->nvdimm;
398
399 return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev),
400 nd_mapping->start, nd_mapping->size);
401}
402
403#define REGION_MAPPING(idx) \
404static ssize_t mapping##idx##_show(struct device *dev, \
405 struct device_attribute *attr, char *buf) \
406{ \
407 return mappingN(dev, buf, idx); \
408} \
409static DEVICE_ATTR_RO(mapping##idx)
410
411/*
412 * 32 should be enough for a while, even in the presence of socket
413 * interleave a 32-way interleave set is a degenerate case.
414 */
415REGION_MAPPING(0);
416REGION_MAPPING(1);
417REGION_MAPPING(2);
418REGION_MAPPING(3);
419REGION_MAPPING(4);
420REGION_MAPPING(5);
421REGION_MAPPING(6);
422REGION_MAPPING(7);
423REGION_MAPPING(8);
424REGION_MAPPING(9);
425REGION_MAPPING(10);
426REGION_MAPPING(11);
427REGION_MAPPING(12);
428REGION_MAPPING(13);
429REGION_MAPPING(14);
430REGION_MAPPING(15);
431REGION_MAPPING(16);
432REGION_MAPPING(17);
433REGION_MAPPING(18);
434REGION_MAPPING(19);
435REGION_MAPPING(20);
436REGION_MAPPING(21);
437REGION_MAPPING(22);
438REGION_MAPPING(23);
439REGION_MAPPING(24);
440REGION_MAPPING(25);
441REGION_MAPPING(26);
442REGION_MAPPING(27);
443REGION_MAPPING(28);
444REGION_MAPPING(29);
445REGION_MAPPING(30);
446REGION_MAPPING(31);
447
448static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
449{
450 struct device *dev = container_of(kobj, struct device, kobj);
451 struct nd_region *nd_region = to_nd_region(dev);
452
453 if (n < nd_region->ndr_mappings)
454 return a->mode;
455 return 0;
456}
457
458static struct attribute *mapping_attributes[] = {
459 &dev_attr_mapping0.attr,
460 &dev_attr_mapping1.attr,
461 &dev_attr_mapping2.attr,
462 &dev_attr_mapping3.attr,
463 &dev_attr_mapping4.attr,
464 &dev_attr_mapping5.attr,
465 &dev_attr_mapping6.attr,
466 &dev_attr_mapping7.attr,
467 &dev_attr_mapping8.attr,
468 &dev_attr_mapping9.attr,
469 &dev_attr_mapping10.attr,
470 &dev_attr_mapping11.attr,
471 &dev_attr_mapping12.attr,
472 &dev_attr_mapping13.attr,
473 &dev_attr_mapping14.attr,
474 &dev_attr_mapping15.attr,
475 &dev_attr_mapping16.attr,
476 &dev_attr_mapping17.attr,
477 &dev_attr_mapping18.attr,
478 &dev_attr_mapping19.attr,
479 &dev_attr_mapping20.attr,
480 &dev_attr_mapping21.attr,
481 &dev_attr_mapping22.attr,
482 &dev_attr_mapping23.attr,
483 &dev_attr_mapping24.attr,
484 &dev_attr_mapping25.attr,
485 &dev_attr_mapping26.attr,
486 &dev_attr_mapping27.attr,
487 &dev_attr_mapping28.attr,
488 &dev_attr_mapping29.attr,
489 &dev_attr_mapping30.attr,
490 &dev_attr_mapping31.attr,
491 NULL,
492};
493
494struct attribute_group nd_mapping_attribute_group = {
495 .is_visible = mapping_visible,
496 .attrs = mapping_attributes,
497};
498EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
499
500void *nd_region_provider_data(struct nd_region *nd_region)
501{
502 return nd_region->provider_data;
503}
504EXPORT_SYMBOL_GPL(nd_region_provider_data);
505
506static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
507 struct nd_region_desc *ndr_desc, struct device_type *dev_type,
508 const char *caller)
509{
510 struct nd_region *nd_region;
511 struct device *dev;
512 u16 i;
513
514 for (i = 0; i < ndr_desc->num_mappings; i++) {
515 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
516 struct nvdimm *nvdimm = nd_mapping->nvdimm;
517
518 if ((nd_mapping->start | nd_mapping->size) % SZ_4K) {
519 dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
520 caller, dev_name(&nvdimm->dev), i);
521
522 return NULL;
523 }
524 }
525
526 nd_region = kzalloc(sizeof(struct nd_region)
527 + sizeof(struct nd_mapping) * ndr_desc->num_mappings,
528 GFP_KERNEL);
529 if (!nd_region)
530 return NULL;
531 nd_region->id = ida_simple_get(&region_ida, 0, 0, GFP_KERNEL);
532 if (nd_region->id < 0) {
533 kfree(nd_region);
534 return NULL;
535 }
536
537 memcpy(nd_region->mapping, ndr_desc->nd_mapping,
538 sizeof(struct nd_mapping) * ndr_desc->num_mappings);
539 for (i = 0; i < ndr_desc->num_mappings; i++) {
540 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
541 struct nvdimm *nvdimm = nd_mapping->nvdimm;
542
543 get_device(&nvdimm->dev);
544 }
545 nd_region->ndr_mappings = ndr_desc->num_mappings;
546 nd_region->provider_data = ndr_desc->provider_data;
Dan Williamseaf96152015-05-01 13:11:27 -0400547 nd_region->nd_set = ndr_desc->nd_set;
Dan Williams1b40e092015-05-01 13:34:01 -0400548 ida_init(&nd_region->ns_ida);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400549 dev = &nd_region->dev;
550 dev_set_name(dev, "region%d", nd_region->id);
551 dev->parent = &nvdimm_bus->dev;
552 dev->type = dev_type;
553 dev->groups = ndr_desc->attr_groups;
554 nd_region->ndr_size = resource_size(ndr_desc->res);
555 nd_region->ndr_start = ndr_desc->res->start;
556 nd_device_register(dev);
557
558 return nd_region;
559}
560
561struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
562 struct nd_region_desc *ndr_desc)
563{
564 return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
565 __func__);
566}
567EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
568
569struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
570 struct nd_region_desc *ndr_desc)
571{
572 if (ndr_desc->num_mappings > 1)
573 return NULL;
574 return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
575 __func__);
576}
577EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
578
579struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
580 struct nd_region_desc *ndr_desc)
581{
582 return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
583 __func__);
584}
585EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);