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 #include <linux/scatterlist.h>
14 #include <linux/highmem.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
18 #include <linux/io.h>
19 #include <linux/nd.h>
20 #include "nd-core.h"
21 #include "nd.h"
22
23 static DEFINE_IDA(region_ida);
24
nd_region_release(struct device * dev)25 static void nd_region_release(struct device *dev)
26 {
27 struct nd_region *nd_region = to_nd_region(dev);
28 u16 i;
29
30 for (i = 0; i < nd_region->ndr_mappings; i++) {
31 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
32 struct nvdimm *nvdimm = nd_mapping->nvdimm;
33
34 put_device(&nvdimm->dev);
35 }
36 free_percpu(nd_region->lane);
37 ida_simple_remove(®ion_ida, nd_region->id);
38 if (is_nd_blk(dev))
39 kfree(to_nd_blk_region(dev));
40 else
41 kfree(nd_region);
42 }
43
44 static struct device_type nd_blk_device_type = {
45 .name = "nd_blk",
46 .release = nd_region_release,
47 };
48
49 static struct device_type nd_pmem_device_type = {
50 .name = "nd_pmem",
51 .release = nd_region_release,
52 };
53
54 static struct device_type nd_volatile_device_type = {
55 .name = "nd_volatile",
56 .release = nd_region_release,
57 };
58
is_nd_pmem(struct device * dev)59 bool is_nd_pmem(struct device *dev)
60 {
61 return dev ? dev->type == &nd_pmem_device_type : false;
62 }
63
is_nd_blk(struct device * dev)64 bool is_nd_blk(struct device *dev)
65 {
66 return dev ? dev->type == &nd_blk_device_type : false;
67 }
68
to_nd_region(struct device * dev)69 struct nd_region *to_nd_region(struct device *dev)
70 {
71 struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
72
73 WARN_ON(dev->type->release != nd_region_release);
74 return nd_region;
75 }
76 EXPORT_SYMBOL_GPL(to_nd_region);
77
to_nd_blk_region(struct device * dev)78 struct nd_blk_region *to_nd_blk_region(struct device *dev)
79 {
80 struct nd_region *nd_region = to_nd_region(dev);
81
82 WARN_ON(!is_nd_blk(dev));
83 return container_of(nd_region, struct nd_blk_region, nd_region);
84 }
85 EXPORT_SYMBOL_GPL(to_nd_blk_region);
86
nd_region_provider_data(struct nd_region * nd_region)87 void *nd_region_provider_data(struct nd_region *nd_region)
88 {
89 return nd_region->provider_data;
90 }
91 EXPORT_SYMBOL_GPL(nd_region_provider_data);
92
nd_blk_region_provider_data(struct nd_blk_region * ndbr)93 void *nd_blk_region_provider_data(struct nd_blk_region *ndbr)
94 {
95 return ndbr->blk_provider_data;
96 }
97 EXPORT_SYMBOL_GPL(nd_blk_region_provider_data);
98
nd_blk_region_set_provider_data(struct nd_blk_region * ndbr,void * data)99 void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data)
100 {
101 ndbr->blk_provider_data = data;
102 }
103 EXPORT_SYMBOL_GPL(nd_blk_region_set_provider_data);
104
105 /**
106 * nd_region_to_nstype() - region to an integer namespace type
107 * @nd_region: region-device to interrogate
108 *
109 * This is the 'nstype' attribute of a region as well, an input to the
110 * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
111 * namespace devices with namespace drivers.
112 */
nd_region_to_nstype(struct nd_region * nd_region)113 int nd_region_to_nstype(struct nd_region *nd_region)
114 {
115 if (is_nd_pmem(&nd_region->dev)) {
116 u16 i, alias;
117
118 for (i = 0, alias = 0; i < nd_region->ndr_mappings; i++) {
119 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
120 struct nvdimm *nvdimm = nd_mapping->nvdimm;
121
122 if (nvdimm->flags & NDD_ALIASING)
123 alias++;
124 }
125 if (alias)
126 return ND_DEVICE_NAMESPACE_PMEM;
127 else
128 return ND_DEVICE_NAMESPACE_IO;
129 } else if (is_nd_blk(&nd_region->dev)) {
130 return ND_DEVICE_NAMESPACE_BLK;
131 }
132
133 return 0;
134 }
135 EXPORT_SYMBOL(nd_region_to_nstype);
136
size_show(struct device * dev,struct device_attribute * attr,char * buf)137 static ssize_t size_show(struct device *dev,
138 struct device_attribute *attr, char *buf)
139 {
140 struct nd_region *nd_region = to_nd_region(dev);
141 unsigned long long size = 0;
142
143 if (is_nd_pmem(dev)) {
144 size = nd_region->ndr_size;
145 } else if (nd_region->ndr_mappings == 1) {
146 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
147
148 size = nd_mapping->size;
149 }
150
151 return sprintf(buf, "%llu\n", size);
152 }
153 static DEVICE_ATTR_RO(size);
154
mappings_show(struct device * dev,struct device_attribute * attr,char * buf)155 static ssize_t mappings_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
157 {
158 struct nd_region *nd_region = to_nd_region(dev);
159
160 return sprintf(buf, "%d\n", nd_region->ndr_mappings);
161 }
162 static DEVICE_ATTR_RO(mappings);
163
nstype_show(struct device * dev,struct device_attribute * attr,char * buf)164 static ssize_t nstype_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166 {
167 struct nd_region *nd_region = to_nd_region(dev);
168
169 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
170 }
171 static DEVICE_ATTR_RO(nstype);
172
set_cookie_show(struct device * dev,struct device_attribute * attr,char * buf)173 static ssize_t set_cookie_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175 {
176 struct nd_region *nd_region = to_nd_region(dev);
177 struct nd_interleave_set *nd_set = nd_region->nd_set;
178
179 if (is_nd_pmem(dev) && nd_set)
180 /* pass, should be precluded by region_visible */;
181 else
182 return -ENXIO;
183
184 return sprintf(buf, "%#llx\n", nd_set->cookie);
185 }
186 static DEVICE_ATTR_RO(set_cookie);
187
nd_region_available_dpa(struct nd_region * nd_region)188 resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
189 {
190 resource_size_t blk_max_overlap = 0, available, overlap;
191 int i;
192
193 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
194
195 retry:
196 available = 0;
197 overlap = blk_max_overlap;
198 for (i = 0; i < nd_region->ndr_mappings; i++) {
199 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
200 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
201
202 /* if a dimm is disabled the available capacity is zero */
203 if (!ndd)
204 return 0;
205
206 if (is_nd_pmem(&nd_region->dev)) {
207 available += nd_pmem_available_dpa(nd_region,
208 nd_mapping, &overlap);
209 if (overlap > blk_max_overlap) {
210 blk_max_overlap = overlap;
211 goto retry;
212 }
213 } else if (is_nd_blk(&nd_region->dev)) {
214 available += nd_blk_available_dpa(nd_mapping);
215 }
216 }
217
218 return available;
219 }
220
available_size_show(struct device * dev,struct device_attribute * attr,char * buf)221 static ssize_t available_size_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223 {
224 struct nd_region *nd_region = to_nd_region(dev);
225 unsigned long long available = 0;
226
227 /*
228 * Flush in-flight updates and grab a snapshot of the available
229 * size. Of course, this value is potentially invalidated the
230 * memory nvdimm_bus_lock() is dropped, but that's userspace's
231 * problem to not race itself.
232 */
233 nvdimm_bus_lock(dev);
234 wait_nvdimm_bus_probe_idle(dev);
235 available = nd_region_available_dpa(nd_region);
236 nvdimm_bus_unlock(dev);
237
238 return sprintf(buf, "%llu\n", available);
239 }
240 static DEVICE_ATTR_RO(available_size);
241
init_namespaces_show(struct device * dev,struct device_attribute * attr,char * buf)242 static ssize_t init_namespaces_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244 {
245 struct nd_region_namespaces *num_ns = dev_get_drvdata(dev);
246 ssize_t rc;
247
248 nvdimm_bus_lock(dev);
249 if (num_ns)
250 rc = sprintf(buf, "%d/%d\n", num_ns->active, num_ns->count);
251 else
252 rc = -ENXIO;
253 nvdimm_bus_unlock(dev);
254
255 return rc;
256 }
257 static DEVICE_ATTR_RO(init_namespaces);
258
namespace_seed_show(struct device * dev,struct device_attribute * attr,char * buf)259 static ssize_t namespace_seed_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
261 {
262 struct nd_region *nd_region = to_nd_region(dev);
263 ssize_t rc;
264
265 nvdimm_bus_lock(dev);
266 if (nd_region->ns_seed)
267 rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
268 else
269 rc = sprintf(buf, "\n");
270 nvdimm_bus_unlock(dev);
271 return rc;
272 }
273 static DEVICE_ATTR_RO(namespace_seed);
274
btt_seed_show(struct device * dev,struct device_attribute * attr,char * buf)275 static ssize_t btt_seed_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
277 {
278 struct nd_region *nd_region = to_nd_region(dev);
279 ssize_t rc;
280
281 nvdimm_bus_lock(dev);
282 if (nd_region->btt_seed)
283 rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
284 else
285 rc = sprintf(buf, "\n");
286 nvdimm_bus_unlock(dev);
287
288 return rc;
289 }
290 static DEVICE_ATTR_RO(btt_seed);
291
pfn_seed_show(struct device * dev,struct device_attribute * attr,char * buf)292 static ssize_t pfn_seed_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294 {
295 struct nd_region *nd_region = to_nd_region(dev);
296 ssize_t rc;
297
298 nvdimm_bus_lock(dev);
299 if (nd_region->pfn_seed)
300 rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
301 else
302 rc = sprintf(buf, "\n");
303 nvdimm_bus_unlock(dev);
304
305 return rc;
306 }
307 static DEVICE_ATTR_RO(pfn_seed);
308
read_only_show(struct device * dev,struct device_attribute * attr,char * buf)309 static ssize_t read_only_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
311 {
312 struct nd_region *nd_region = to_nd_region(dev);
313
314 return sprintf(buf, "%d\n", nd_region->ro);
315 }
316
read_only_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)317 static ssize_t read_only_store(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t len)
319 {
320 bool ro;
321 int rc = strtobool(buf, &ro);
322 struct nd_region *nd_region = to_nd_region(dev);
323
324 if (rc)
325 return rc;
326
327 nd_region->ro = ro;
328 return len;
329 }
330 static DEVICE_ATTR_RW(read_only);
331
332 static struct attribute *nd_region_attributes[] = {
333 &dev_attr_size.attr,
334 &dev_attr_nstype.attr,
335 &dev_attr_mappings.attr,
336 &dev_attr_btt_seed.attr,
337 &dev_attr_pfn_seed.attr,
338 &dev_attr_read_only.attr,
339 &dev_attr_set_cookie.attr,
340 &dev_attr_available_size.attr,
341 &dev_attr_namespace_seed.attr,
342 &dev_attr_init_namespaces.attr,
343 NULL,
344 };
345
region_visible(struct kobject * kobj,struct attribute * a,int n)346 static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
347 {
348 struct device *dev = container_of(kobj, typeof(*dev), kobj);
349 struct nd_region *nd_region = to_nd_region(dev);
350 struct nd_interleave_set *nd_set = nd_region->nd_set;
351 int type = nd_region_to_nstype(nd_region);
352
353 if (a != &dev_attr_set_cookie.attr
354 && a != &dev_attr_available_size.attr)
355 return a->mode;
356
357 if ((type == ND_DEVICE_NAMESPACE_PMEM
358 || type == ND_DEVICE_NAMESPACE_BLK)
359 && a == &dev_attr_available_size.attr)
360 return a->mode;
361 else if (is_nd_pmem(dev) && nd_set)
362 return a->mode;
363
364 return 0;
365 }
366
367 struct attribute_group nd_region_attribute_group = {
368 .attrs = nd_region_attributes,
369 .is_visible = region_visible,
370 };
371 EXPORT_SYMBOL_GPL(nd_region_attribute_group);
372
nd_region_interleave_set_cookie(struct nd_region * nd_region)373 u64 nd_region_interleave_set_cookie(struct nd_region *nd_region)
374 {
375 struct nd_interleave_set *nd_set = nd_region->nd_set;
376
377 if (nd_set)
378 return nd_set->cookie;
379 return 0;
380 }
381
382 /*
383 * Upon successful probe/remove, take/release a reference on the
384 * associated interleave set (if present), and plant new btt + namespace
385 * seeds. Also, on the removal of a BLK region, notify the provider to
386 * disable the region.
387 */
nd_region_notify_driver_action(struct nvdimm_bus * nvdimm_bus,struct device * dev,bool probe)388 static void nd_region_notify_driver_action(struct nvdimm_bus *nvdimm_bus,
389 struct device *dev, bool probe)
390 {
391 struct nd_region *nd_region;
392
393 if (!probe && (is_nd_pmem(dev) || is_nd_blk(dev))) {
394 int i;
395
396 nd_region = to_nd_region(dev);
397 for (i = 0; i < nd_region->ndr_mappings; i++) {
398 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
399 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
400 struct nvdimm *nvdimm = nd_mapping->nvdimm;
401
402 kfree(nd_mapping->labels);
403 nd_mapping->labels = NULL;
404 put_ndd(ndd);
405 nd_mapping->ndd = NULL;
406 if (ndd)
407 atomic_dec(&nvdimm->busy);
408 }
409
410 if (is_nd_pmem(dev))
411 return;
412
413 to_nd_blk_region(dev)->disable(nvdimm_bus, dev);
414 }
415 if (dev->parent && is_nd_blk(dev->parent) && probe) {
416 nd_region = to_nd_region(dev->parent);
417 nvdimm_bus_lock(dev);
418 if (nd_region->ns_seed == dev)
419 nd_region_create_blk_seed(nd_region);
420 nvdimm_bus_unlock(dev);
421 }
422 if (is_nd_btt(dev) && probe) {
423 struct nd_btt *nd_btt = to_nd_btt(dev);
424
425 nd_region = to_nd_region(dev->parent);
426 nvdimm_bus_lock(dev);
427 if (nd_region->btt_seed == dev)
428 nd_region_create_btt_seed(nd_region);
429 if (nd_region->ns_seed == &nd_btt->ndns->dev &&
430 is_nd_blk(dev->parent))
431 nd_region_create_blk_seed(nd_region);
432 nvdimm_bus_unlock(dev);
433 }
434 }
435
nd_region_probe_success(struct nvdimm_bus * nvdimm_bus,struct device * dev)436 void nd_region_probe_success(struct nvdimm_bus *nvdimm_bus, struct device *dev)
437 {
438 nd_region_notify_driver_action(nvdimm_bus, dev, true);
439 }
440
nd_region_disable(struct nvdimm_bus * nvdimm_bus,struct device * dev)441 void nd_region_disable(struct nvdimm_bus *nvdimm_bus, struct device *dev)
442 {
443 nd_region_notify_driver_action(nvdimm_bus, dev, false);
444 }
445
mappingN(struct device * dev,char * buf,int n)446 static ssize_t mappingN(struct device *dev, char *buf, int n)
447 {
448 struct nd_region *nd_region = to_nd_region(dev);
449 struct nd_mapping *nd_mapping;
450 struct nvdimm *nvdimm;
451
452 if (n >= nd_region->ndr_mappings)
453 return -ENXIO;
454 nd_mapping = &nd_region->mapping[n];
455 nvdimm = nd_mapping->nvdimm;
456
457 return sprintf(buf, "%s,%llu,%llu\n", dev_name(&nvdimm->dev),
458 nd_mapping->start, nd_mapping->size);
459 }
460
461 #define REGION_MAPPING(idx) \
462 static ssize_t mapping##idx##_show(struct device *dev, \
463 struct device_attribute *attr, char *buf) \
464 { \
465 return mappingN(dev, buf, idx); \
466 } \
467 static DEVICE_ATTR_RO(mapping##idx)
468
469 /*
470 * 32 should be enough for a while, even in the presence of socket
471 * interleave a 32-way interleave set is a degenerate case.
472 */
473 REGION_MAPPING(0);
474 REGION_MAPPING(1);
475 REGION_MAPPING(2);
476 REGION_MAPPING(3);
477 REGION_MAPPING(4);
478 REGION_MAPPING(5);
479 REGION_MAPPING(6);
480 REGION_MAPPING(7);
481 REGION_MAPPING(8);
482 REGION_MAPPING(9);
483 REGION_MAPPING(10);
484 REGION_MAPPING(11);
485 REGION_MAPPING(12);
486 REGION_MAPPING(13);
487 REGION_MAPPING(14);
488 REGION_MAPPING(15);
489 REGION_MAPPING(16);
490 REGION_MAPPING(17);
491 REGION_MAPPING(18);
492 REGION_MAPPING(19);
493 REGION_MAPPING(20);
494 REGION_MAPPING(21);
495 REGION_MAPPING(22);
496 REGION_MAPPING(23);
497 REGION_MAPPING(24);
498 REGION_MAPPING(25);
499 REGION_MAPPING(26);
500 REGION_MAPPING(27);
501 REGION_MAPPING(28);
502 REGION_MAPPING(29);
503 REGION_MAPPING(30);
504 REGION_MAPPING(31);
505
mapping_visible(struct kobject * kobj,struct attribute * a,int n)506 static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
507 {
508 struct device *dev = container_of(kobj, struct device, kobj);
509 struct nd_region *nd_region = to_nd_region(dev);
510
511 if (n < nd_region->ndr_mappings)
512 return a->mode;
513 return 0;
514 }
515
516 static struct attribute *mapping_attributes[] = {
517 &dev_attr_mapping0.attr,
518 &dev_attr_mapping1.attr,
519 &dev_attr_mapping2.attr,
520 &dev_attr_mapping3.attr,
521 &dev_attr_mapping4.attr,
522 &dev_attr_mapping5.attr,
523 &dev_attr_mapping6.attr,
524 &dev_attr_mapping7.attr,
525 &dev_attr_mapping8.attr,
526 &dev_attr_mapping9.attr,
527 &dev_attr_mapping10.attr,
528 &dev_attr_mapping11.attr,
529 &dev_attr_mapping12.attr,
530 &dev_attr_mapping13.attr,
531 &dev_attr_mapping14.attr,
532 &dev_attr_mapping15.attr,
533 &dev_attr_mapping16.attr,
534 &dev_attr_mapping17.attr,
535 &dev_attr_mapping18.attr,
536 &dev_attr_mapping19.attr,
537 &dev_attr_mapping20.attr,
538 &dev_attr_mapping21.attr,
539 &dev_attr_mapping22.attr,
540 &dev_attr_mapping23.attr,
541 &dev_attr_mapping24.attr,
542 &dev_attr_mapping25.attr,
543 &dev_attr_mapping26.attr,
544 &dev_attr_mapping27.attr,
545 &dev_attr_mapping28.attr,
546 &dev_attr_mapping29.attr,
547 &dev_attr_mapping30.attr,
548 &dev_attr_mapping31.attr,
549 NULL,
550 };
551
552 struct attribute_group nd_mapping_attribute_group = {
553 .is_visible = mapping_visible,
554 .attrs = mapping_attributes,
555 };
556 EXPORT_SYMBOL_GPL(nd_mapping_attribute_group);
557
nd_blk_region_init(struct nd_region * nd_region)558 int nd_blk_region_init(struct nd_region *nd_region)
559 {
560 struct device *dev = &nd_region->dev;
561 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
562
563 if (!is_nd_blk(dev))
564 return 0;
565
566 if (nd_region->ndr_mappings < 1) {
567 dev_err(dev, "invalid BLK region\n");
568 return -ENXIO;
569 }
570
571 return to_nd_blk_region(dev)->enable(nvdimm_bus, dev);
572 }
573
574 /**
575 * nd_region_acquire_lane - allocate and lock a lane
576 * @nd_region: region id and number of lanes possible
577 *
578 * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
579 * We optimize for the common case where there are 256 lanes, one
580 * per-cpu. For larger systems we need to lock to share lanes. For now
581 * this implementation assumes the cost of maintaining an allocator for
582 * free lanes is on the order of the lock hold time, so it implements a
583 * static lane = cpu % num_lanes mapping.
584 *
585 * In the case of a BTT instance on top of a BLK namespace a lane may be
586 * acquired recursively. We lock on the first instance.
587 *
588 * In the case of a BTT instance on top of PMEM, we only acquire a lane
589 * for the BTT metadata updates.
590 */
nd_region_acquire_lane(struct nd_region * nd_region)591 unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
592 {
593 unsigned int cpu, lane;
594
595 cpu = get_cpu();
596 if (nd_region->num_lanes < nr_cpu_ids) {
597 struct nd_percpu_lane *ndl_lock, *ndl_count;
598
599 lane = cpu % nd_region->num_lanes;
600 ndl_count = per_cpu_ptr(nd_region->lane, cpu);
601 ndl_lock = per_cpu_ptr(nd_region->lane, lane);
602 if (ndl_count->count++ == 0)
603 spin_lock(&ndl_lock->lock);
604 } else
605 lane = cpu;
606
607 return lane;
608 }
609 EXPORT_SYMBOL(nd_region_acquire_lane);
610
nd_region_release_lane(struct nd_region * nd_region,unsigned int lane)611 void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
612 {
613 if (nd_region->num_lanes < nr_cpu_ids) {
614 unsigned int cpu = get_cpu();
615 struct nd_percpu_lane *ndl_lock, *ndl_count;
616
617 ndl_count = per_cpu_ptr(nd_region->lane, cpu);
618 ndl_lock = per_cpu_ptr(nd_region->lane, lane);
619 if (--ndl_count->count == 0)
620 spin_unlock(&ndl_lock->lock);
621 put_cpu();
622 }
623 put_cpu();
624 }
625 EXPORT_SYMBOL(nd_region_release_lane);
626
nd_region_create(struct nvdimm_bus * nvdimm_bus,struct nd_region_desc * ndr_desc,struct device_type * dev_type,const char * caller)627 static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
628 struct nd_region_desc *ndr_desc, struct device_type *dev_type,
629 const char *caller)
630 {
631 struct nd_region *nd_region;
632 struct device *dev;
633 void *region_buf;
634 unsigned int i;
635 int ro = 0;
636
637 for (i = 0; i < ndr_desc->num_mappings; i++) {
638 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
639 struct nvdimm *nvdimm = nd_mapping->nvdimm;
640
641 if ((nd_mapping->start | nd_mapping->size) % SZ_4K) {
642 dev_err(&nvdimm_bus->dev, "%s: %s mapping%d is not 4K aligned\n",
643 caller, dev_name(&nvdimm->dev), i);
644
645 return NULL;
646 }
647
648 if (nvdimm->flags & NDD_UNARMED)
649 ro = 1;
650 }
651
652 if (dev_type == &nd_blk_device_type) {
653 struct nd_blk_region_desc *ndbr_desc;
654 struct nd_blk_region *ndbr;
655
656 ndbr_desc = to_blk_region_desc(ndr_desc);
657 ndbr = kzalloc(sizeof(*ndbr) + sizeof(struct nd_mapping)
658 * ndr_desc->num_mappings,
659 GFP_KERNEL);
660 if (ndbr) {
661 nd_region = &ndbr->nd_region;
662 ndbr->enable = ndbr_desc->enable;
663 ndbr->disable = ndbr_desc->disable;
664 ndbr->do_io = ndbr_desc->do_io;
665 }
666 region_buf = ndbr;
667 } else {
668 nd_region = kzalloc(sizeof(struct nd_region)
669 + sizeof(struct nd_mapping)
670 * ndr_desc->num_mappings,
671 GFP_KERNEL);
672 region_buf = nd_region;
673 }
674
675 if (!region_buf)
676 return NULL;
677 nd_region->id = ida_simple_get(®ion_ida, 0, 0, GFP_KERNEL);
678 if (nd_region->id < 0)
679 goto err_id;
680
681 nd_region->lane = alloc_percpu(struct nd_percpu_lane);
682 if (!nd_region->lane)
683 goto err_percpu;
684
685 for (i = 0; i < nr_cpu_ids; i++) {
686 struct nd_percpu_lane *ndl;
687
688 ndl = per_cpu_ptr(nd_region->lane, i);
689 spin_lock_init(&ndl->lock);
690 ndl->count = 0;
691 }
692
693 memcpy(nd_region->mapping, ndr_desc->nd_mapping,
694 sizeof(struct nd_mapping) * ndr_desc->num_mappings);
695 for (i = 0; i < ndr_desc->num_mappings; i++) {
696 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
697 struct nvdimm *nvdimm = nd_mapping->nvdimm;
698
699 get_device(&nvdimm->dev);
700 }
701 nd_region->ndr_mappings = ndr_desc->num_mappings;
702 nd_region->provider_data = ndr_desc->provider_data;
703 nd_region->nd_set = ndr_desc->nd_set;
704 nd_region->num_lanes = ndr_desc->num_lanes;
705 nd_region->flags = ndr_desc->flags;
706 nd_region->ro = ro;
707 nd_region->numa_node = ndr_desc->numa_node;
708 ida_init(&nd_region->ns_ida);
709 ida_init(&nd_region->btt_ida);
710 ida_init(&nd_region->pfn_ida);
711 dev = &nd_region->dev;
712 dev_set_name(dev, "region%d", nd_region->id);
713 dev->parent = &nvdimm_bus->dev;
714 dev->type = dev_type;
715 dev->groups = ndr_desc->attr_groups;
716 nd_region->ndr_size = resource_size(ndr_desc->res);
717 nd_region->ndr_start = ndr_desc->res->start;
718 nd_device_register(dev);
719
720 return nd_region;
721
722 err_percpu:
723 ida_simple_remove(®ion_ida, nd_region->id);
724 err_id:
725 kfree(region_buf);
726 return NULL;
727 }
728
nvdimm_pmem_region_create(struct nvdimm_bus * nvdimm_bus,struct nd_region_desc * ndr_desc)729 struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
730 struct nd_region_desc *ndr_desc)
731 {
732 ndr_desc->num_lanes = ND_MAX_LANES;
733 return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
734 __func__);
735 }
736 EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
737
nvdimm_blk_region_create(struct nvdimm_bus * nvdimm_bus,struct nd_region_desc * ndr_desc)738 struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
739 struct nd_region_desc *ndr_desc)
740 {
741 if (ndr_desc->num_mappings > 1)
742 return NULL;
743 ndr_desc->num_lanes = min(ndr_desc->num_lanes, ND_MAX_LANES);
744 return nd_region_create(nvdimm_bus, ndr_desc, &nd_blk_device_type,
745 __func__);
746 }
747 EXPORT_SYMBOL_GPL(nvdimm_blk_region_create);
748
nvdimm_volatile_region_create(struct nvdimm_bus * nvdimm_bus,struct nd_region_desc * ndr_desc)749 struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
750 struct nd_region_desc *ndr_desc)
751 {
752 ndr_desc->num_lanes = ND_MAX_LANES;
753 return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
754 __func__);
755 }
756 EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
757