blob: 4fd92389fa7e5d619c84bb509e8f79334ab93a79 [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 Williams8c2f7e82015-06-25 04:20:04 -0400299static ssize_t btt_seed_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct nd_region *nd_region = to_nd_region(dev);
303 ssize_t rc;
304
305 nvdimm_bus_lock(dev);
306 if (nd_region->btt_seed)
307 rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
308 else
309 rc = sprintf(buf, "\n");
310 nvdimm_bus_unlock(dev);
311
312 return rc;
313}
314static DEVICE_ATTR_RO(btt_seed);
315
Dan Williams1f7df6f2015-06-09 20:13:14 -0400316static struct attribute *nd_region_attributes[] = {
317 &dev_attr_size.attr,
Dan Williams3d880022015-05-31 15:02:11 -0400318 &dev_attr_nstype.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400319 &dev_attr_mappings.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -0400320 &dev_attr_btt_seed.attr,
Dan Williamseaf96152015-05-01 13:11:27 -0400321 &dev_attr_set_cookie.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400322 &dev_attr_available_size.attr,
323 &dev_attr_namespace_seed.attr,
Dan Williams3d880022015-05-31 15:02:11 -0400324 &dev_attr_init_namespaces.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400325 NULL,
326};
327
Dan Williamseaf96152015-05-01 13:11:27 -0400328static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
329{
330 struct device *dev = container_of(kobj, typeof(*dev), kobj);
331 struct nd_region *nd_region = to_nd_region(dev);
332 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400333 int type = nd_region_to_nstype(nd_region);
Dan Williamseaf96152015-05-01 13:11:27 -0400334
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400335 if (a != &dev_attr_set_cookie.attr
336 && a != &dev_attr_available_size.attr)
Dan Williamseaf96152015-05-01 13:11:27 -0400337 return a->mode;
338
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400339 if ((type == ND_DEVICE_NAMESPACE_PMEM
340 || type == ND_DEVICE_NAMESPACE_BLK)
341 && a == &dev_attr_available_size.attr)
342 return a->mode;
343 else if (is_nd_pmem(dev) && nd_set)
344 return a->mode;
Dan Williamseaf96152015-05-01 13:11:27 -0400345
346 return 0;
347}
348
Dan Williams1f7df6f2015-06-09 20:13:14 -0400349struct attribute_group nd_region_attribute_group = {
350 .attrs = nd_region_attributes,
Dan Williamseaf96152015-05-01 13:11:27 -0400351 .is_visible = region_visible,
Dan Williams1f7df6f2015-06-09 20:13:14 -0400352};
353EXPORT_SYMBOL_GPL(nd_region_attribute_group);
354
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400355u64 nd_region_interleave_set_cookie(struct nd_region *nd_region)
356{
357 struct nd_interleave_set *nd_set = nd_region->nd_set;
358
359 if (nd_set)
360 return nd_set->cookie;
361 return 0;
362}
363
Dan Williamseaf96152015-05-01 13:11:27 -0400364/*
365 * Upon successful probe/remove, take/release a reference on the
Dan Williams8c2f7e82015-06-25 04:20:04 -0400366 * associated interleave set (if present), and plant new btt + namespace
367 * seeds.
Dan Williamseaf96152015-05-01 13:11:27 -0400368 */
369static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
370 struct device *dev, bool probe)
371{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400372 struct nd_region *nd_region;
373
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400374 if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) {
Dan Williamseaf96152015-05-01 13:11:27 -0400375 int i;
376
Dan Williams8c2f7e82015-06-25 04:20:04 -0400377 nd_region = to_nd_region(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400378 for (i = 0; i < nd_region->ndr_mappings; i++) {
379 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400380 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
Dan Williamseaf96152015-05-01 13:11:27 -0400381 struct nvdimm *nvdimm = nd_mapping->nvdimm;
382
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400383 kfree(nd_mapping->labels);
384 nd_mapping->labels = NULL;
385 put_ndd(ndd);
386 nd_mapping->ndd = NULL;
387 atomic_dec(&nvdimm->busy);
Dan Williamseaf96152015-05-01 13:11:27 -0400388 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400389 }
390 if (dev->parent && is_nd_blk(dev->parent) && probe) {
391 nd_region = to_nd_region(dev->parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400392 nvdimm_bus_lock(dev);
393 if (nd_region->ns_seed == dev)
394 nd_region_create_blk_seed(nd_region);
395 nvdimm_bus_unlock(dev);
Dan Williamseaf96152015-05-01 13:11:27 -0400396 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400397 if (is_nd_btt(dev) && probe) {
398 nd_region = to_nd_region(dev->parent);
399 nvdimm_bus_lock(dev);
400 if (nd_region->btt_seed == dev)
401 nd_region_create_btt_seed(nd_region);
402 nvdimm_bus_unlock(dev);
403 }
Dan Williamseaf96152015-05-01 13:11:27 -0400404}
405
406void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
407{
408 nd_region_notify_driver_action(nvdimm_bus, dev, true);
409}
410
411void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
412{
413 nd_region_notify_driver_action(nvdimm_bus, dev, false);
414}
415
Dan Williams1f7df6f2015-06-09 20:13:14 -0400416static ssize_t mappingN(struct device *dev, char *buf, int n)
417{
418 struct nd_region *nd_region = to_nd_region(dev);
419 struct nd_mapping *nd_mapping;
420 struct nvdimm *nvdimm;
421
422 if (n >= nd_region->ndr_mappings)
423 return -ENXIO;
424 nd_mapping = &nd_region->mapping[n];
425 nvdimm = nd_mapping->nvdimm;
426
427 return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev),
428 nd_mapping->start, nd_mapping->size);
429}
430
431#define REGION_MAPPING(idx) \
432static ssize_t mapping##idx##_show(struct device *dev, \
433 struct device_attribute *attr, char *buf) \
434{ \
435 return mappingN(dev, buf, idx); \
436} \
437static DEVICE_ATTR_RO(mapping##idx)
438
439/*
440 * 32 should be enough for a while, even in the presence of socket
441 * interleave a 32-way interleave set is a degenerate case.
442 */
443REGION_MAPPING(0);
444REGION_MAPPING(1);
445REGION_MAPPING(2);
446REGION_MAPPING(3);
447REGION_MAPPING(4);
448REGION_MAPPING(5);
449REGION_MAPPING(6);
450REGION_MAPPING(7);
451REGION_MAPPING(8);
452REGION_MAPPING(9);
453REGION_MAPPING(10);
454REGION_MAPPING(11);
455REGION_MAPPING(12);
456REGION_MAPPING(13);
457REGION_MAPPING(14);
458REGION_MAPPING(15);
459REGION_MAPPING(16);
460REGION_MAPPING(17);
461REGION_MAPPING(18);
462REGION_MAPPING(19);
463REGION_MAPPING(20);
464REGION_MAPPING(21);
465REGION_MAPPING(22);
466REGION_MAPPING(23);
467REGION_MAPPING(24);
468REGION_MAPPING(25);
469REGION_MAPPING(26);
470REGION_MAPPING(27);
471REGION_MAPPING(28);
472REGION_MAPPING(29);
473REGION_MAPPING(30);
474REGION_MAPPING(31);
475
476static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
477{
478 struct device *dev = container_of(kobj, struct device, kobj);
479 struct nd_region *nd_region = to_nd_region(dev);
480
481 if (n < nd_region->ndr_mappings)
482 return a->mode;
483 return 0;
484}
485
486static struct attribute *mapping_attributes[] = {
487 &dev_attr_mapping0.attr,
488 &dev_attr_mapping1.attr,
489 &dev_attr_mapping2.attr,
490 &dev_attr_mapping3.attr,
491 &dev_attr_mapping4.attr,
492 &dev_attr_mapping5.attr,
493 &dev_attr_mapping6.attr,
494 &dev_attr_mapping7.attr,
495 &dev_attr_mapping8.attr,
496 &dev_attr_mapping9.attr,
497 &dev_attr_mapping10.attr,
498 &dev_attr_mapping11.attr,
499 &dev_attr_mapping12.attr,
500 &dev_attr_mapping13.attr,
501 &dev_attr_mapping14.attr,
502 &dev_attr_mapping15.attr,
503 &dev_attr_mapping16.attr,
504 &dev_attr_mapping17.attr,
505 &dev_attr_mapping18.attr,
506 &dev_attr_mapping19.attr,
507 &dev_attr_mapping20.attr,
508 &dev_attr_mapping21.attr,
509 &dev_attr_mapping22.attr,
510 &dev_attr_mapping23.attr,
511 &dev_attr_mapping24.attr,
512 &dev_attr_mapping25.attr,
513 &dev_attr_mapping26.attr,
514 &dev_attr_mapping27.attr,
515 &dev_attr_mapping28.attr,
516 &dev_attr_mapping29.attr,
517 &dev_attr_mapping30.attr,
518 &dev_attr_mapping31.attr,
519 NULL,
520};
521
522struct attribute_group nd_mapping_attribute_group = {
523 .is_visible = mapping_visible,
524 .attrs = mapping_attributes,
525};
526EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
527
528void *nd_region_provider_data(struct nd_region *nd_region)
529{
530 return nd_region->provider_data;
531}
532EXPORT_SYMBOL_GPL(nd_region_provider_data);
533
534static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
535 struct nd_region_desc *ndr_desc, struct device_type *dev_type,
536 const char *caller)
537{
538 struct nd_region *nd_region;
539 struct device *dev;
540 u16 i;
541
542 for (i = 0; i < ndr_desc->num_mappings; i++) {
543 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
544 struct nvdimm *nvdimm = nd_mapping->nvdimm;
545
546 if ((nd_mapping->start | nd_mapping->size) % SZ_4K) {
547 dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
548 caller, dev_name(&nvdimm->dev), i);
549
550 return NULL;
551 }
552 }
553
554 nd_region = kzalloc(sizeof(struct nd_region)
555 + sizeof(struct nd_mapping) * ndr_desc->num_mappings,
556 GFP_KERNEL);
557 if (!nd_region)
558 return NULL;
559 nd_region->id = ida_simple_get(&region_ida, 0, 0, GFP_KERNEL);
560 if (nd_region->id < 0) {
561 kfree(nd_region);
562 return NULL;
563 }
564
565 memcpy(nd_region->mapping, ndr_desc->nd_mapping,
566 sizeof(struct nd_mapping) * ndr_desc->num_mappings);
567 for (i = 0; i < ndr_desc->num_mappings; i++) {
568 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
569 struct nvdimm *nvdimm = nd_mapping->nvdimm;
570
571 get_device(&nvdimm->dev);
572 }
573 nd_region->ndr_mappings = ndr_desc->num_mappings;
574 nd_region->provider_data = ndr_desc->provider_data;
Dan Williamseaf96152015-05-01 13:11:27 -0400575 nd_region->nd_set = ndr_desc->nd_set;
Dan Williams1b40e092015-05-01 13:34:01 -0400576 ida_init(&nd_region->ns_ida);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400577 ida_init(&nd_region->btt_ida);
Dan Williams1f7df6f2015-06-09 20:13:14 -0400578 dev = &nd_region->dev;
579 dev_set_name(dev, "region%d", nd_region->id);
580 dev->parent = &nvdimm_bus->dev;
581 dev->type = dev_type;
582 dev->groups = ndr_desc->attr_groups;
583 nd_region->ndr_size = resource_size(ndr_desc->res);
584 nd_region->ndr_start = ndr_desc->res->start;
585 nd_device_register(dev);
586
587 return nd_region;
588}
589
590struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
591 struct nd_region_desc *ndr_desc)
592{
593 return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
594 __func__);
595}
596EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
597
598struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
599 struct nd_region_desc *ndr_desc)
600{
601 if (ndr_desc->num_mappings > 1)
602 return NULL;
603 return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
604 __func__);
605}
606EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
607
608struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
609 struct nd_region_desc *ndr_desc)
610{
611 return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
612 __func__);
613}
614EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);