blob: 4e9261ef8a95af6c70cd4b46c8ecf4b0d8c8cc1b [file] [log] [blame]
Dan Williams3d880022015-05-31 15:02:11 -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 */
13#include <linux/module.h>
14#include <linux/device.h>
Dan Williams6ff3e912016-10-05 14:04:15 -070015#include <linux/sort.h>
Dan Williams3d880022015-05-31 15:02:11 -040016#include <linux/slab.h>
Dan Williamsae8219f2016-09-19 16:04:21 -070017#include <linux/list.h>
Dan Williams3d880022015-05-31 15:02:11 -040018#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040019#include "nd-core.h"
Dan Williamsca6a4652017-01-13 20:36:58 -080020#include "pmem.h"
Dan Williams3d880022015-05-31 15:02:11 -040021#include "nd.h"
22
23static void namespace_io_release(struct device *dev)
24{
25 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
26
27 kfree(nsio);
28}
29
Dan Williamsbf9bccc2015-06-17 17:14:46 -040030static void namespace_pmem_release(struct device *dev)
31{
32 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0e3b0d12016-10-06 23:13:15 -070033 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040034
Dan Williams0e3b0d12016-10-06 23:13:15 -070035 if (nspm->id >= 0)
36 ida_simple_remove(&nd_region->ns_ida, nspm->id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040037 kfree(nspm->alt_name);
38 kfree(nspm->uuid);
39 kfree(nspm);
40}
41
42static void namespace_blk_release(struct device *dev)
43{
Dan Williams1b40e092015-05-01 13:34:01 -040044 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
45 struct nd_region *nd_region = to_nd_region(dev->parent);
46
47 if (nsblk->id >= 0)
48 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
49 kfree(nsblk->alt_name);
50 kfree(nsblk->uuid);
51 kfree(nsblk->res);
52 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040053}
54
Bhumika Goyal970d14e2017-01-25 00:54:07 +053055static const struct device_type namespace_io_device_type = {
Dan Williams3d880022015-05-31 15:02:11 -040056 .name = "nd_namespace_io",
57 .release = namespace_io_release,
58};
59
Bhumika Goyal970d14e2017-01-25 00:54:07 +053060static const struct device_type namespace_pmem_device_type = {
Dan Williamsbf9bccc2015-06-17 17:14:46 -040061 .name = "nd_namespace_pmem",
62 .release = namespace_pmem_release,
63};
64
Bhumika Goyal970d14e2017-01-25 00:54:07 +053065static const struct device_type namespace_blk_device_type = {
Dan Williamsbf9bccc2015-06-17 17:14:46 -040066 .name = "nd_namespace_blk",
67 .release = namespace_blk_release,
68};
69
Dan Williams6ff3e912016-10-05 14:04:15 -070070static bool is_namespace_pmem(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -040071{
72 return dev ? dev->type == &namespace_pmem_device_type : false;
73}
74
Dan Williams6ff3e912016-10-05 14:04:15 -070075static bool is_namespace_blk(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -040076{
77 return dev ? dev->type == &namespace_blk_device_type : false;
78}
79
Dan Williams6ff3e912016-10-05 14:04:15 -070080static bool is_namespace_io(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -040081{
82 return dev ? dev->type == &namespace_io_device_type : false;
83}
84
Dan Williamse07ecd72016-01-05 18:37:23 -080085static int is_uuid_busy(struct device *dev, void *data)
86{
87 u8 *uuid1 = data, *uuid2 = NULL;
88
89 if (is_namespace_pmem(dev)) {
90 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
91
92 uuid2 = nspm->uuid;
93 } else if (is_namespace_blk(dev)) {
94 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
95
96 uuid2 = nsblk->uuid;
97 } else if (is_nd_btt(dev)) {
98 struct nd_btt *nd_btt = to_nd_btt(dev);
99
100 uuid2 = nd_btt->uuid;
101 } else if (is_nd_pfn(dev)) {
102 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
103
104 uuid2 = nd_pfn->uuid;
105 }
106
107 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
108 return -EBUSY;
109
110 return 0;
111}
112
113static int is_namespace_uuid_busy(struct device *dev, void *data)
114{
115 if (is_nd_pmem(dev) || is_nd_blk(dev))
116 return device_for_each_child(dev, data, is_uuid_busy);
117 return 0;
118}
119
120/**
121 * nd_is_uuid_unique - verify that no other namespace has @uuid
122 * @dev: any device on a nvdimm_bus
123 * @uuid: uuid to check
124 */
125bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
126{
127 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
128
129 if (!nvdimm_bus)
130 return false;
131 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
132 if (device_for_each_child(&nvdimm_bus->dev, uuid,
133 is_namespace_uuid_busy) != 0)
134 return false;
135 return true;
136}
137
Dan Williams004f1af2015-08-24 19:20:23 -0400138bool pmem_should_map_pages(struct device *dev)
139{
140 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamscfe30b82016-03-03 09:38:00 -0800141 struct nd_namespace_io *nsio;
Dan Williams004f1af2015-08-24 19:20:23 -0400142
143 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
144 return false;
145
146 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
147 return false;
148
149 if (is_nd_pfn(dev) || is_nd_btt(dev))
150 return false;
151
Dan Williamscfe30b82016-03-03 09:38:00 -0800152 nsio = to_nd_namespace_io(dev);
153 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
154 IORESOURCE_SYSTEM_RAM,
155 IORES_DESC_NONE) == REGION_MIXED)
156 return false;
157
Dan Williams004f1af2015-08-24 19:20:23 -0400158 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
Dan Williams004f1af2015-08-24 19:20:23 -0400159}
160EXPORT_SYMBOL(pmem_should_map_pages);
161
Vishal Verma5212e112015-06-25 04:20:32 -0400162const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
163 char *name)
164{
165 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400166 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400167
Dan Williams0731de02015-12-14 15:34:15 -0800168 if (ndns->claim && is_nd_btt(ndns->claim))
169 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400170
Dan Williams004f1af2015-08-24 19:20:23 -0400171 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams01220732016-10-05 09:09:44 -0700172 int nsidx = 0;
173
174 if (is_namespace_pmem(&ndns->dev)) {
175 struct nd_namespace_pmem *nspm;
176
177 nspm = to_nd_namespace_pmem(&ndns->dev);
178 nsidx = nspm->id;
179 }
180
181 if (nsidx)
182 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
183 suffix ? suffix : "");
184 else
185 sprintf(name, "pmem%d%s", nd_region->id,
186 suffix ? suffix : "");
Dan Williams004f1af2015-08-24 19:20:23 -0400187 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400188 struct nd_namespace_blk *nsblk;
189
190 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400191 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
192 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400193 } else {
194 return NULL;
195 }
196
197 return name;
198}
199EXPORT_SYMBOL(nvdimm_namespace_disk_name);
200
Vishal Verma6ec68952015-07-29 14:58:09 -0600201const u8 *nd_dev_to_uuid(struct device *dev)
202{
203 static const u8 null_uuid[16];
204
205 if (!dev)
206 return null_uuid;
207
208 if (is_namespace_pmem(dev)) {
209 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
210
211 return nspm->uuid;
212 } else if (is_namespace_blk(dev)) {
213 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
214
215 return nsblk->uuid;
216 } else
217 return null_uuid;
218}
219EXPORT_SYMBOL(nd_dev_to_uuid);
220
Dan Williams3d880022015-05-31 15:02:11 -0400221static ssize_t nstype_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 struct nd_region *nd_region = to_nd_region(dev->parent);
225
226 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
227}
228static DEVICE_ATTR_RO(nstype);
229
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400230static ssize_t __alt_name_store(struct device *dev, const char *buf,
231 const size_t len)
232{
233 char *input, *pos, *alt_name, **ns_altname;
234 ssize_t rc;
235
236 if (is_namespace_pmem(dev)) {
237 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
238
239 ns_altname = &nspm->alt_name;
240 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400241 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
242
243 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400244 } else
245 return -ENXIO;
246
Dan Williams8c2f7e82015-06-25 04:20:04 -0400247 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400248 return -EBUSY;
249
250 input = kmemdup(buf, len + 1, GFP_KERNEL);
251 if (!input)
252 return -ENOMEM;
253
254 input[len] = '\0';
255 pos = strim(input);
256 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
257 rc = -EINVAL;
258 goto out;
259 }
260
261 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
262 if (!alt_name) {
263 rc = -ENOMEM;
264 goto out;
265 }
266 kfree(*ns_altname);
267 *ns_altname = alt_name;
268 sprintf(*ns_altname, "%s", pos);
269 rc = len;
270
271out:
272 kfree(input);
273 return rc;
274}
275
Dan Williams1b40e092015-05-01 13:34:01 -0400276static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
277{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400278 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400279 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
280 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
281 struct nd_label_id label_id;
282 resource_size_t size = 0;
283 struct resource *res;
284
285 if (!nsblk->uuid)
286 return 0;
287 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
288 for_each_dpa_resource(ndd, res)
289 if (strcmp(res->name, label_id.id) == 0)
290 size += resource_size(res);
291 return size;
292}
293
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400294static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
295{
296 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
297 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
298 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
299 struct nd_label_id label_id;
300 struct resource *res;
301 int count, i;
302
303 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
304 return false;
305
306 count = 0;
307 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
308 for_each_dpa_resource(ndd, res) {
309 if (strcmp(res->name, label_id.id) != 0)
310 continue;
311 /*
Geert Uytterhoevenae551e92016-08-31 11:45:25 +0200312 * Resources with unacknowledged adjustments indicate a
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400313 * failure to update labels
314 */
315 if (res->flags & DPA_RESOURCE_ADJUSTED)
316 return false;
317 count++;
318 }
319
320 /* These values match after a successful label update */
321 if (count != nsblk->num_resources)
322 return false;
323
324 for (i = 0; i < nsblk->num_resources; i++) {
325 struct resource *found = NULL;
326
327 for_each_dpa_resource(ndd, res)
328 if (res == nsblk->res[i]) {
329 found = res;
330 break;
331 }
332 /* stale resource */
333 if (!found)
334 return false;
335 }
336
337 return true;
338}
339
340resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
341{
342 resource_size_t size;
343
344 nvdimm_bus_lock(&nsblk->common.dev);
345 size = __nd_namespace_blk_validate(nsblk);
346 nvdimm_bus_unlock(&nsblk->common.dev);
347
348 return size;
349}
350EXPORT_SYMBOL(nd_namespace_blk_validate);
351
352
Dan Williamsf524bf22015-05-30 12:36:02 -0400353static int nd_namespace_label_update(struct nd_region *nd_region,
354 struct device *dev)
355{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400356 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400357 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400358 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400359 return 0;
360
361 /*
362 * Only allow label writes that will result in a valid namespace
363 * or deletion of an existing namespace.
364 */
365 if (is_namespace_pmem(dev)) {
366 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400367 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400368
369 if (size == 0 && nspm->uuid)
370 /* delete allocation */;
371 else if (!nspm->uuid)
372 return 0;
373
374 return nd_pmem_namespace_label_update(nd_region, nspm, size);
375 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400376 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
377 resource_size_t size = nd_namespace_blk_size(nsblk);
378
379 if (size == 0 && nsblk->uuid)
380 /* delete allocation */;
381 else if (!nsblk->uuid || !nsblk->lbasize)
382 return 0;
383
384 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400385 } else
386 return -ENXIO;
387}
388
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400389static ssize_t alt_name_store(struct device *dev,
390 struct device_attribute *attr, const char *buf, size_t len)
391{
Dan Williamsf524bf22015-05-30 12:36:02 -0400392 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400393 ssize_t rc;
394
395 device_lock(dev);
396 nvdimm_bus_lock(dev);
397 wait_nvdimm_bus_probe_idle(dev);
398 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400399 if (rc >= 0)
400 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400401 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
402 nvdimm_bus_unlock(dev);
403 device_unlock(dev);
404
Dan Williamsf524bf22015-05-30 12:36:02 -0400405 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400406}
407
408static ssize_t alt_name_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
410{
411 char *ns_altname;
412
413 if (is_namespace_pmem(dev)) {
414 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
415
416 ns_altname = nspm->alt_name;
417 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400418 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
419
420 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400421 } else
422 return -ENXIO;
423
424 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
425}
426static DEVICE_ATTR_RW(alt_name);
427
428static int scan_free(struct nd_region *nd_region,
429 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
430 resource_size_t n)
431{
432 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
433 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
434 int rc = 0;
435
436 while (n) {
437 struct resource *res, *last;
438 resource_size_t new_start;
439
440 last = NULL;
441 for_each_dpa_resource(ndd, res)
442 if (strcmp(res->name, label_id->id) == 0)
443 last = res;
444 res = last;
445 if (!res)
446 return 0;
447
448 if (n >= resource_size(res)) {
449 n -= resource_size(res);
450 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
451 nvdimm_free_dpa(ndd, res);
452 /* retry with last resource deleted */
453 continue;
454 }
455
456 /*
457 * Keep BLK allocations relegated to high DPA as much as
458 * possible
459 */
460 if (is_blk)
461 new_start = res->start + n;
462 else
463 new_start = res->start;
464
465 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400466 if (rc == 0)
467 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400468 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
469 break;
470 }
471
472 return rc;
473}
474
475/**
476 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
477 * @nd_region: the set of dimms to reclaim @n bytes from
478 * @label_id: unique identifier for the namespace consuming this dpa range
479 * @n: number of bytes per-dimm to release
480 *
481 * Assumes resources are ordered. Starting from the end try to
482 * adjust_resource() the allocation to @n, but if @n is larger than the
483 * allocation delete it and find the 'new' last allocation in the label
484 * set.
485 */
486static int shrink_dpa_allocation(struct nd_region *nd_region,
487 struct nd_label_id *label_id, resource_size_t n)
488{
489 int i;
490
491 for (i = 0; i < nd_region->ndr_mappings; i++) {
492 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
493 int rc;
494
495 rc = scan_free(nd_region, nd_mapping, label_id, n);
496 if (rc)
497 return rc;
498 }
499
500 return 0;
501}
502
503static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
504 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
505 resource_size_t n)
506{
507 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
508 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
509 resource_size_t first_dpa;
510 struct resource *res;
511 int rc = 0;
512
513 /* allocate blk from highest dpa first */
514 if (is_blk)
515 first_dpa = nd_mapping->start + nd_mapping->size - n;
516 else
517 first_dpa = nd_mapping->start;
518
519 /* first resource allocation for this label-id or dimm */
520 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
521 if (!res)
522 rc = -EBUSY;
523
524 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
525 return rc ? n : 0;
526}
527
Dan Williams762d0672016-10-04 16:09:59 -0700528
529/**
530 * space_valid() - validate free dpa space against constraints
531 * @nd_region: hosting region of the free space
532 * @ndd: dimm device data for debug
533 * @label_id: namespace id to allocate space
534 * @prev: potential allocation that precedes free space
535 * @next: allocation that follows the given free space range
536 * @exist: first allocation with same id in the mapping
537 * @n: range that must satisfied for pmem allocations
538 * @valid: free space range to validate
539 *
540 * BLK-space is valid as long as it does not precede a PMEM
541 * allocation in a given region. PMEM-space must be contiguous
542 * and adjacent to an existing existing allocation (if one
543 * exists). If reserving PMEM any space is valid.
544 */
545static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
546 struct nd_label_id *label_id, struct resource *prev,
547 struct resource *next, struct resource *exist,
548 resource_size_t n, struct resource *valid)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400549{
Dan Williams762d0672016-10-04 16:09:59 -0700550 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
551 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
552
553 if (valid->start >= valid->end)
554 goto invalid;
555
556 if (is_reserve)
557 return;
558
559 if (!is_pmem) {
560 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
561 struct nvdimm_bus *nvdimm_bus;
562 struct blk_alloc_info info = {
563 .nd_mapping = nd_mapping,
564 .available = nd_mapping->size,
565 .res = valid,
566 };
567
568 WARN_ON(!is_nd_blk(&nd_region->dev));
569 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
570 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
571 return;
572 }
573
574 /* allocation needs to be contiguous, so this is all or nothing */
575 if (resource_size(valid) < n)
576 goto invalid;
577
578 /* we've got all the space we need and no existing allocation */
579 if (!exist)
580 return;
581
582 /* allocation needs to be contiguous with the existing namespace */
583 if (valid->start == exist->end + 1
584 || valid->end == exist->start - 1)
585 return;
586
587 invalid:
588 /* truncate @valid size to 0 */
589 valid->end = valid->start - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400590}
591
592enum alloc_loc {
593 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
594};
595
596static resource_size_t scan_allocate(struct nd_region *nd_region,
597 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
598 resource_size_t n)
599{
600 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
601 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
602 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams762d0672016-10-04 16:09:59 -0700603 struct resource *res, *exist = NULL, valid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400604 const resource_size_t to_allocate = n;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400605 int first;
606
Dan Williams762d0672016-10-04 16:09:59 -0700607 for_each_dpa_resource(ndd, res)
608 if (strcmp(label_id->id, res->name) == 0)
609 exist = res;
610
611 valid.start = nd_mapping->start;
612 valid.end = mapping_end;
613 valid.name = "free space";
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400614 retry:
615 first = 0;
616 for_each_dpa_resource(ndd, res) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400617 struct resource *next = res->sibling, *new_res = NULL;
Dan Williams762d0672016-10-04 16:09:59 -0700618 resource_size_t allocate, available = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400619 enum alloc_loc loc = ALLOC_ERR;
620 const char *action;
621 int rc = 0;
622
623 /* ignore resources outside this nd_mapping */
624 if (res->start > mapping_end)
625 continue;
626 if (res->end < nd_mapping->start)
627 continue;
628
629 /* space at the beginning of the mapping */
630 if (!first++ && res->start > nd_mapping->start) {
Dan Williams762d0672016-10-04 16:09:59 -0700631 valid.start = nd_mapping->start;
632 valid.end = res->start - 1;
633 space_valid(nd_region, ndd, label_id, NULL, next, exist,
634 to_allocate, &valid);
635 available = resource_size(&valid);
636 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400637 loc = ALLOC_BEFORE;
638 }
639
640 /* space between allocations */
641 if (!loc && next) {
Dan Williams762d0672016-10-04 16:09:59 -0700642 valid.start = res->start + resource_size(res);
643 valid.end = min(mapping_end, next->start - 1);
644 space_valid(nd_region, ndd, label_id, res, next, exist,
645 to_allocate, &valid);
646 available = resource_size(&valid);
647 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400648 loc = ALLOC_MID;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400649 }
650
651 /* space at the end of the mapping */
652 if (!loc && !next) {
Dan Williams762d0672016-10-04 16:09:59 -0700653 valid.start = res->start + resource_size(res);
654 valid.end = mapping_end;
655 space_valid(nd_region, ndd, label_id, res, next, exist,
656 to_allocate, &valid);
657 available = resource_size(&valid);
658 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400659 loc = ALLOC_AFTER;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400660 }
661
662 if (!loc || !available)
663 continue;
664 allocate = min(available, n);
665 switch (loc) {
666 case ALLOC_BEFORE:
667 if (strcmp(res->name, label_id->id) == 0) {
668 /* adjust current resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400669 rc = adjust_resource(res, res->start - allocate,
670 resource_size(res) + allocate);
671 action = "cur grow up";
672 } else
673 action = "allocate";
674 break;
675 case ALLOC_MID:
676 if (strcmp(next->name, label_id->id) == 0) {
677 /* adjust next resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400678 rc = adjust_resource(next, next->start
679 - allocate, resource_size(next)
680 + allocate);
681 new_res = next;
682 action = "next grow up";
683 } else if (strcmp(res->name, label_id->id) == 0) {
684 action = "grow down";
685 } else
686 action = "allocate";
687 break;
688 case ALLOC_AFTER:
689 if (strcmp(res->name, label_id->id) == 0)
690 action = "grow down";
691 else
692 action = "allocate";
693 break;
694 default:
695 return n;
696 }
697
698 if (strcmp(action, "allocate") == 0) {
699 /* BLK allocate bottom up */
700 if (!is_pmem)
Dan Williams762d0672016-10-04 16:09:59 -0700701 valid.start += available - allocate;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400702
703 new_res = nvdimm_allocate_dpa(ndd, label_id,
Dan Williams762d0672016-10-04 16:09:59 -0700704 valid.start, allocate);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400705 if (!new_res)
706 rc = -EBUSY;
707 } else if (strcmp(action, "grow down") == 0) {
708 /* adjust current resource down */
709 rc = adjust_resource(res, res->start, resource_size(res)
710 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400711 if (rc == 0)
712 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400713 }
714
715 if (!new_res)
716 new_res = res;
717
718 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
719 action, loc, rc);
720
721 if (rc)
722 return n;
723
724 n -= allocate;
725 if (n) {
726 /*
727 * Retry scan with newly inserted resources.
728 * For example, if we did an ALLOC_BEFORE
729 * insertion there may also have been space
730 * available for an ALLOC_AFTER insertion, so we
731 * need to check this same resource again
732 */
733 goto retry;
734 } else
735 return 0;
736 }
737
Dan Williams1b40e092015-05-01 13:34:01 -0400738 /*
739 * If we allocated nothing in the BLK case it may be because we are in
740 * an initial "pmem-reserve pass". Only do an initial BLK allocation
741 * when none of the DPA space is reserved.
742 */
743 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400744 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
745 return n;
746}
747
Dan Williams1b40e092015-05-01 13:34:01 -0400748static int merge_dpa(struct nd_region *nd_region,
749 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
750{
751 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
752 struct resource *res;
753
754 if (strncmp("pmem", label_id->id, 4) == 0)
755 return 0;
756 retry:
757 for_each_dpa_resource(ndd, res) {
758 int rc;
759 struct resource *next = res->sibling;
760 resource_size_t end = res->start + resource_size(res);
761
762 if (!next || strcmp(res->name, label_id->id) != 0
763 || strcmp(next->name, label_id->id) != 0
764 || end != next->start)
765 continue;
766 end += resource_size(next);
767 nvdimm_free_dpa(ndd, next);
768 rc = adjust_resource(res, res->start, end - res->start);
769 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
770 if (rc)
771 return rc;
772 res->flags |= DPA_RESOURCE_ADJUSTED;
773 goto retry;
774 }
775
776 return 0;
777}
778
779static int __reserve_free_pmem(struct device *dev, void *data)
780{
781 struct nvdimm *nvdimm = data;
782 struct nd_region *nd_region;
783 struct nd_label_id label_id;
784 int i;
785
786 if (!is_nd_pmem(dev))
787 return 0;
788
789 nd_region = to_nd_region(dev);
790 if (nd_region->ndr_mappings == 0)
791 return 0;
792
793 memset(&label_id, 0, sizeof(label_id));
794 strcat(label_id.id, "pmem-reserve");
795 for (i = 0; i < nd_region->ndr_mappings; i++) {
796 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
797 resource_size_t n, rem = 0;
798
799 if (nd_mapping->nvdimm != nvdimm)
800 continue;
801
802 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
803 if (n == 0)
804 return 0;
805 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
806 dev_WARN_ONCE(&nd_region->dev, rem,
807 "pmem reserve underrun: %#llx of %#llx bytes\n",
808 (unsigned long long) n - rem,
809 (unsigned long long) n);
810 return rem ? -ENXIO : 0;
811 }
812
813 return 0;
814}
815
816static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
817 struct nd_mapping *nd_mapping)
818{
819 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
820 struct resource *res, *_res;
821
822 for_each_dpa_resource_safe(ndd, res, _res)
823 if (strcmp(res->name, "pmem-reserve") == 0)
824 nvdimm_free_dpa(ndd, res);
825}
826
827static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
828 struct nd_mapping *nd_mapping)
829{
830 struct nvdimm *nvdimm = nd_mapping->nvdimm;
831 int rc;
832
833 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
834 __reserve_free_pmem);
835 if (rc)
836 release_free_pmem(nvdimm_bus, nd_mapping);
837 return rc;
838}
839
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400840/**
841 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
842 * @nd_region: the set of dimms to allocate @n more bytes from
843 * @label_id: unique identifier for the namespace consuming this dpa range
844 * @n: number of bytes per-dimm to add to the existing allocation
845 *
846 * Assumes resources are ordered. For BLK regions, first consume
847 * BLK-only available DPA free space, then consume PMEM-aliased DPA
848 * space starting at the highest DPA. For PMEM regions start
849 * allocations from the start of an interleave set and end at the first
850 * BLK allocation or the end of the interleave set, whichever comes
851 * first.
852 */
853static int grow_dpa_allocation(struct nd_region *nd_region,
854 struct nd_label_id *label_id, resource_size_t n)
855{
Dan Williams1b40e092015-05-01 13:34:01 -0400856 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
857 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400858 int i;
859
860 for (i = 0; i < nd_region->ndr_mappings; i++) {
861 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400862 resource_size_t rem = n;
863 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400864
Dan Williams1b40e092015-05-01 13:34:01 -0400865 /*
866 * In the BLK case try once with all unallocated PMEM
867 * reserved, and once without
868 */
869 for (j = is_pmem; j < 2; j++) {
870 bool blk_only = j == 0;
871
872 if (blk_only) {
873 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
874 if (rc)
875 return rc;
876 }
877 rem = scan_allocate(nd_region, nd_mapping,
878 label_id, rem);
879 if (blk_only)
880 release_free_pmem(nvdimm_bus, nd_mapping);
881
882 /* try again and allow encroachments into PMEM */
883 if (rem == 0)
884 break;
885 }
886
887 dev_WARN_ONCE(&nd_region->dev, rem,
888 "allocation underrun: %#llx of %#llx bytes\n",
889 (unsigned long long) n - rem,
890 (unsigned long long) n);
891 if (rem)
892 return -ENXIO;
893
894 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400895 if (rc)
896 return rc;
897 }
898
899 return 0;
900}
901
Dan Williams0e3b0d12016-10-06 23:13:15 -0700902static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400903 struct nd_namespace_pmem *nspm, resource_size_t size)
904{
905 struct resource *res = &nspm->nsio.res;
Dan Williams0e3b0d12016-10-06 23:13:15 -0700906 resource_size_t offset = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400907
Dan Williams0e3b0d12016-10-06 23:13:15 -0700908 if (size && !nspm->uuid) {
909 WARN_ON_ONCE(1);
910 size = 0;
911 }
912
913 if (size && nspm->uuid) {
914 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
915 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
916 struct nd_label_id label_id;
917 struct resource *res;
918
919 if (!ndd) {
920 size = 0;
921 goto out;
922 }
923
924 nd_label_gen_id(&label_id, nspm->uuid, 0);
925
926 /* calculate a spa offset from the dpa allocation offset */
927 for_each_dpa_resource(ndd, res)
928 if (strcmp(res->name, label_id.id) == 0) {
929 offset = (res->start - nd_mapping->start)
930 * nd_region->ndr_mappings;
931 goto out;
932 }
933
934 WARN_ON_ONCE(1);
935 size = 0;
936 }
937
938 out:
939 res->start = nd_region->ndr_start + offset;
940 res->end = res->start + size - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400941}
942
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300943static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
944{
945 if (!uuid) {
946 dev_dbg(dev, "%s: uuid not set\n", where);
947 return true;
948 }
949 return false;
950}
951
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400952static ssize_t __size_store(struct device *dev, unsigned long long val)
953{
954 resource_size_t allocated = 0, available = 0;
955 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams1f19b982017-01-09 17:30:49 -0800956 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400957 struct nd_mapping *nd_mapping;
958 struct nvdimm_drvdata *ndd;
959 struct nd_label_id label_id;
960 u32 flags = 0, remainder;
Dan Williams9d032f42017-01-25 00:54:07 +0530961 int rc, i, id = -1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400962 u8 *uuid = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400963
Dan Williams1f19b982017-01-09 17:30:49 -0800964 if (dev->driver || ndns->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400965 return -EBUSY;
966
967 if (is_namespace_pmem(dev)) {
968 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
969
970 uuid = nspm->uuid;
Dan Williams9d032f42017-01-25 00:54:07 +0530971 id = nspm->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400972 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400973 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
974
975 uuid = nsblk->uuid;
976 flags = NSLABEL_FLAG_LOCAL;
Dan Williams9d032f42017-01-25 00:54:07 +0530977 id = nsblk->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400978 }
979
980 /*
981 * We need a uuid for the allocation-label and dimm(s) on which
982 * to store the label.
983 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300984 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400985 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300986 if (nd_region->ndr_mappings == 0) {
987 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
988 return -ENXIO;
989 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400990
991 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
992 if (remainder) {
993 dev_dbg(dev, "%llu is not %dK aligned\n", val,
994 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
995 return -EINVAL;
996 }
997
998 nd_label_gen_id(&label_id, uuid, flags);
999 for (i = 0; i < nd_region->ndr_mappings; i++) {
1000 nd_mapping = &nd_region->mapping[i];
1001 ndd = to_ndd(nd_mapping);
1002
1003 /*
1004 * All dimms in an interleave set, or the base dimm for a blk
1005 * region, need to be enabled for the size to be changed.
1006 */
1007 if (!ndd)
1008 return -ENXIO;
1009
1010 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1011 }
1012 available = nd_region_available_dpa(nd_region);
1013
1014 if (val > available + allocated)
1015 return -ENOSPC;
1016
1017 if (val == allocated)
1018 return 0;
1019
1020 val = div_u64(val, nd_region->ndr_mappings);
1021 allocated = div_u64(allocated, nd_region->ndr_mappings);
1022 if (val < allocated)
1023 rc = shrink_dpa_allocation(nd_region, &label_id,
1024 allocated - val);
1025 else
1026 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1027
1028 if (rc)
1029 return rc;
1030
1031 if (is_namespace_pmem(dev)) {
1032 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1033
Dan Williams0e3b0d12016-10-06 23:13:15 -07001034 nd_namespace_pmem_set_resource(nd_region, nspm,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001035 val * nd_region->ndr_mappings);
1036 }
1037
Dan Williams1f19b982017-01-09 17:30:49 -08001038 /*
1039 * Try to delete the namespace if we deleted all of its
Dan Williams9d032f42017-01-25 00:54:07 +05301040 * allocation, this is not the seed or 0th device for the
1041 * region, and it is not actively claimed by a btt, pfn, or dax
1042 * instance.
Dan Williams1f19b982017-01-09 17:30:49 -08001043 */
Dan Williams9d032f42017-01-25 00:54:07 +05301044 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
Dan Williams1f19b982017-01-09 17:30:49 -08001045 nd_device_unregister(dev, ND_ASYNC);
1046
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001047 return rc;
1048}
1049
1050static ssize_t size_store(struct device *dev,
1051 struct device_attribute *attr, const char *buf, size_t len)
1052{
Dan Williamsf524bf22015-05-30 12:36:02 -04001053 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001054 unsigned long long val;
1055 u8 **uuid = NULL;
1056 int rc;
1057
1058 rc = kstrtoull(buf, 0, &val);
1059 if (rc)
1060 return rc;
1061
1062 device_lock(dev);
1063 nvdimm_bus_lock(dev);
1064 wait_nvdimm_bus_probe_idle(dev);
1065 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -04001066 if (rc >= 0)
1067 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001068
1069 if (is_namespace_pmem(dev)) {
1070 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1071
1072 uuid = &nspm->uuid;
1073 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001074 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1075
1076 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001077 }
1078
1079 if (rc == 0 && val == 0 && uuid) {
1080 /* setting size zero == 'delete namespace' */
1081 kfree(*uuid);
1082 *uuid = NULL;
1083 }
1084
1085 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
1086 ? "fail" : "success", rc);
1087
1088 nvdimm_bus_unlock(dev);
1089 device_unlock(dev);
1090
Dan Williamsf524bf22015-05-30 12:36:02 -04001091 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001092}
1093
Dan Williams8c2f7e82015-06-25 04:20:04 -04001094resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001095{
Dan Williams8c2f7e82015-06-25 04:20:04 -04001096 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001097
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001098 if (is_namespace_pmem(dev)) {
1099 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1100
Dan Williams8c2f7e82015-06-25 04:20:04 -04001101 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001102 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -04001103 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001104 } else if (is_namespace_io(dev)) {
1105 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1106
Dan Williams8c2f7e82015-06-25 04:20:04 -04001107 return resource_size(&nsio->res);
1108 } else
1109 WARN_ONCE(1, "unknown namespace type\n");
1110 return 0;
1111}
Dan Williams1b40e092015-05-01 13:34:01 -04001112
Dan Williams8c2f7e82015-06-25 04:20:04 -04001113resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1114{
1115 resource_size_t size;
1116
1117 nvdimm_bus_lock(&ndns->dev);
1118 size = __nvdimm_namespace_capacity(ndns);
1119 nvdimm_bus_unlock(&ndns->dev);
1120
1121 return size;
1122}
1123EXPORT_SYMBOL(nvdimm_namespace_capacity);
1124
1125static ssize_t size_show(struct device *dev,
1126 struct device_attribute *attr, char *buf)
1127{
1128 return sprintf(buf, "%llu\n", (unsigned long long)
1129 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001130}
Fabian Frederickb44fe762016-12-04 10:54:08 -08001131static DEVICE_ATTR(size, 0444, size_show, size_store);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001132
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001133static u8 *namespace_to_uuid(struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001134{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001135 if (is_namespace_pmem(dev)) {
1136 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1137
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001138 return nspm->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001139 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001140 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1141
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001142 return nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001143 } else
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001144 return ERR_PTR(-ENXIO);
1145}
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001146
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001147static ssize_t uuid_show(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1149{
1150 u8 *uuid = namespace_to_uuid(dev);
1151
1152 if (IS_ERR(uuid))
1153 return PTR_ERR(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001154 if (uuid)
1155 return sprintf(buf, "%pUb\n", uuid);
1156 return sprintf(buf, "\n");
1157}
1158
1159/**
1160 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1161 * @nd_region: parent region so we can updates all dimms in the set
1162 * @dev: namespace type for generating label_id
1163 * @new_uuid: incoming uuid
1164 * @old_uuid: reference to the uuid storage location in the namespace object
1165 */
1166static int namespace_update_uuid(struct nd_region *nd_region,
1167 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1168{
1169 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1170 struct nd_label_id old_label_id;
1171 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001172 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001173
Dan Williamsf524bf22015-05-30 12:36:02 -04001174 if (!nd_is_uuid_unique(dev, new_uuid))
1175 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001176
1177 if (*old_uuid == NULL)
1178 goto out;
1179
Dan Williamsf524bf22015-05-30 12:36:02 -04001180 /*
1181 * If we've already written a label with this uuid, then it's
1182 * too late to rename because we can't reliably update the uuid
1183 * without losing the old namespace. Userspace must delete this
1184 * namespace to abandon the old uuid.
1185 */
1186 for (i = 0; i < nd_region->ndr_mappings; i++) {
1187 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1188
1189 /*
1190 * This check by itself is sufficient because old_uuid
1191 * would be NULL above if this uuid did not exist in the
1192 * currently written set.
1193 *
1194 * FIXME: can we delete uuid with zero dpa allocated?
1195 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001196 if (list_empty(&nd_mapping->labels))
Dan Williamsf524bf22015-05-30 12:36:02 -04001197 return -EBUSY;
1198 }
1199
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001200 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1201 nd_label_gen_id(&new_label_id, new_uuid, flags);
1202 for (i = 0; i < nd_region->ndr_mappings; i++) {
1203 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1204 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1205 struct resource *res;
1206
1207 for_each_dpa_resource(ndd, res)
1208 if (strcmp(res->name, old_label_id.id) == 0)
1209 sprintf((void *) res->name, "%s",
1210 new_label_id.id);
1211 }
1212 kfree(*old_uuid);
1213 out:
1214 *old_uuid = new_uuid;
1215 return 0;
1216}
1217
1218static ssize_t uuid_store(struct device *dev,
1219 struct device_attribute *attr, const char *buf, size_t len)
1220{
1221 struct nd_region *nd_region = to_nd_region(dev->parent);
1222 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001223 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001224 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001225
1226 if (is_namespace_pmem(dev)) {
1227 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1228
1229 ns_uuid = &nspm->uuid;
1230 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001231 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1232
1233 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001234 } else
1235 return -ENXIO;
1236
1237 device_lock(dev);
1238 nvdimm_bus_lock(dev);
1239 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001240 if (to_ndns(dev)->claim)
1241 rc = -EBUSY;
1242 if (rc >= 0)
1243 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001244 if (rc >= 0)
1245 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001246 if (rc >= 0)
1247 rc = nd_namespace_label_update(nd_region, dev);
1248 else
1249 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001250 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1251 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1252 nvdimm_bus_unlock(dev);
1253 device_unlock(dev);
1254
Dan Williamsf524bf22015-05-30 12:36:02 -04001255 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001256}
1257static DEVICE_ATTR_RW(uuid);
1258
1259static ssize_t resource_show(struct device *dev,
1260 struct device_attribute *attr, char *buf)
1261{
1262 struct resource *res;
1263
1264 if (is_namespace_pmem(dev)) {
1265 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1266
1267 res = &nspm->nsio.res;
1268 } else if (is_namespace_io(dev)) {
1269 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1270
1271 res = &nsio->res;
1272 } else
1273 return -ENXIO;
1274
1275 /* no address to convey if the namespace has no allocation */
1276 if (resource_size(res) == 0)
1277 return -ENXIO;
1278 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1279}
1280static DEVICE_ATTR_RO(resource);
1281
Vishal Vermafcae6952015-06-25 04:22:39 -04001282static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
1283 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001284
1285static ssize_t sector_size_show(struct device *dev,
1286 struct device_attribute *attr, char *buf)
1287{
1288 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1289
1290 if (!is_namespace_blk(dev))
1291 return -ENXIO;
1292
1293 return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
1294}
1295
1296static ssize_t sector_size_store(struct device *dev,
1297 struct device_attribute *attr, const char *buf, size_t len)
1298{
1299 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williamsf524bf22015-05-30 12:36:02 -04001300 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001301 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001302
1303 if (!is_namespace_blk(dev))
1304 return -ENXIO;
1305
1306 device_lock(dev);
1307 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001308 if (to_ndns(dev)->claim)
1309 rc = -EBUSY;
1310 if (rc >= 0)
1311 rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
1312 ns_lbasize_supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001313 if (rc >= 0)
1314 rc = nd_namespace_label_update(nd_region, dev);
1315 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1316 rc, rc < 0 ? "tried" : "wrote", buf,
1317 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001318 nvdimm_bus_unlock(dev);
1319 device_unlock(dev);
1320
1321 return rc ? rc : len;
1322}
1323static DEVICE_ATTR_RW(sector_size);
1324
Dan Williams0ba1c632015-05-30 12:35:36 -04001325static ssize_t dpa_extents_show(struct device *dev,
1326 struct device_attribute *attr, char *buf)
1327{
1328 struct nd_region *nd_region = to_nd_region(dev->parent);
1329 struct nd_label_id label_id;
1330 int count = 0, i;
1331 u8 *uuid = NULL;
1332 u32 flags = 0;
1333
1334 nvdimm_bus_lock(dev);
1335 if (is_namespace_pmem(dev)) {
1336 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1337
1338 uuid = nspm->uuid;
1339 flags = 0;
1340 } else if (is_namespace_blk(dev)) {
1341 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1342
1343 uuid = nsblk->uuid;
1344 flags = NSLABEL_FLAG_LOCAL;
1345 }
1346
1347 if (!uuid)
1348 goto out;
1349
1350 nd_label_gen_id(&label_id, uuid, flags);
1351 for (i = 0; i < nd_region->ndr_mappings; i++) {
1352 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1353 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1354 struct resource *res;
1355
1356 for_each_dpa_resource(ndd, res)
1357 if (strcmp(res->name, label_id.id) == 0)
1358 count++;
1359 }
1360 out:
1361 nvdimm_bus_unlock(dev);
1362
1363 return sprintf(buf, "%d\n", count);
1364}
1365static DEVICE_ATTR_RO(dpa_extents);
1366
Dan Williams8c2f7e82015-06-25 04:20:04 -04001367static ssize_t holder_show(struct device *dev,
1368 struct device_attribute *attr, char *buf)
1369{
1370 struct nd_namespace_common *ndns = to_ndns(dev);
1371 ssize_t rc;
1372
1373 device_lock(dev);
1374 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1375 device_unlock(dev);
1376
1377 return rc;
1378}
1379static DEVICE_ATTR_RO(holder);
1380
Dan Williams0731de02015-12-14 15:34:15 -08001381static ssize_t mode_show(struct device *dev,
1382 struct device_attribute *attr, char *buf)
1383{
1384 struct nd_namespace_common *ndns = to_ndns(dev);
1385 struct device *claim;
1386 char *mode;
1387 ssize_t rc;
1388
1389 device_lock(dev);
1390 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001391 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001392 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001393 else if (claim && is_nd_pfn(claim))
1394 mode = "memory";
Dan Williamscd034122016-03-11 10:15:36 -08001395 else if (claim && is_nd_dax(claim))
1396 mode = "dax";
Dan Williams9c412422016-01-23 15:34:10 -08001397 else if (!claim && pmem_should_map_pages(dev))
1398 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001399 else
1400 mode = "raw";
1401 rc = sprintf(buf, "%s\n", mode);
1402 device_unlock(dev);
1403
1404 return rc;
1405}
1406static DEVICE_ATTR_RO(mode);
1407
Dan Williams8c2f7e82015-06-25 04:20:04 -04001408static ssize_t force_raw_store(struct device *dev,
1409 struct device_attribute *attr, const char *buf, size_t len)
1410{
1411 bool force_raw;
1412 int rc = strtobool(buf, &force_raw);
1413
1414 if (rc)
1415 return rc;
1416
1417 to_ndns(dev)->force_raw = force_raw;
1418 return len;
1419}
1420
1421static ssize_t force_raw_show(struct device *dev,
1422 struct device_attribute *attr, char *buf)
1423{
1424 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1425}
1426static DEVICE_ATTR_RW(force_raw);
1427
Dan Williams3d880022015-05-31 15:02:11 -04001428static struct attribute *nd_namespace_attributes[] = {
1429 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001430 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001431 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001432 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001433 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001434 &dev_attr_resource.attr,
1435 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001436 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001437 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001438 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001439 NULL,
1440};
1441
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001442static umode_t namespace_visible(struct kobject *kobj,
1443 struct attribute *a, int n)
1444{
1445 struct device *dev = container_of(kobj, struct device, kobj);
1446
1447 if (a == &dev_attr_resource.attr) {
1448 if (is_namespace_blk(dev))
1449 return 0;
1450 return a->mode;
1451 }
1452
1453 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1454 if (a == &dev_attr_size.attr)
Fabian Frederickb44fe762016-12-04 10:54:08 -08001455 return 0644;
Dan Williams1b40e092015-05-01 13:34:01 -04001456
1457 if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
1458 return 0;
1459
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001460 return a->mode;
1461 }
1462
Dan Williams8c2f7e82015-06-25 04:20:04 -04001463 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1464 || a == &dev_attr_holder.attr
Dan Williams0731de02015-12-14 15:34:15 -08001465 || a == &dev_attr_force_raw.attr
1466 || a == &dev_attr_mode.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001467 return a->mode;
1468
1469 return 0;
1470}
1471
Dan Williams3d880022015-05-31 15:02:11 -04001472static struct attribute_group nd_namespace_attribute_group = {
1473 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001474 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001475};
1476
1477static const struct attribute_group *nd_namespace_attribute_groups[] = {
1478 &nd_device_attribute_group,
1479 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001480 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001481 NULL,
1482};
1483
Dan Williams8c2f7e82015-06-25 04:20:04 -04001484struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1485{
1486 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001487 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williamscd034122016-03-11 10:15:36 -08001488 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001489 struct nd_namespace_common *ndns = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001490 resource_size_t size;
1491
Dan Williamscd034122016-03-11 10:15:36 -08001492 if (nd_btt || nd_pfn || nd_dax) {
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001493 if (nd_btt)
Dan Williamse1455742015-07-30 17:57:47 -04001494 ndns = nd_btt->ndns;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001495 else if (nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -04001496 ndns = nd_pfn->ndns;
Dan Williamscd034122016-03-11 10:15:36 -08001497 else if (nd_dax)
1498 ndns = nd_dax->nd_pfn.ndns;
Dan Williamse1455742015-07-30 17:57:47 -04001499
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001500 if (!ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001501 return ERR_PTR(-ENODEV);
1502
1503 /*
1504 * Flush any in-progess probes / removals in the driver
1505 * for the raw personality of this namespace.
1506 */
1507 device_lock(&ndns->dev);
1508 device_unlock(&ndns->dev);
1509 if (ndns->dev.driver) {
1510 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001511 dev_name(dev));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001512 return ERR_PTR(-EBUSY);
1513 }
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001514 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001515 "host (%s) vs claim (%s) mismatch\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001516 dev_name(dev),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001517 dev_name(ndns->claim)))
1518 return ERR_PTR(-ENXIO);
1519 } else {
1520 ndns = to_ndns(dev);
1521 if (ndns->claim) {
1522 dev_dbg(dev, "claimed by %s, failing probe\n",
1523 dev_name(ndns->claim));
1524
1525 return ERR_PTR(-ENXIO);
1526 }
1527 }
1528
1529 size = nvdimm_namespace_capacity(ndns);
1530 if (size < ND_MIN_NAMESPACE_SIZE) {
1531 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1532 &size, ND_MIN_NAMESPACE_SIZE);
1533 return ERR_PTR(-ENODEV);
1534 }
1535
1536 if (is_namespace_pmem(&ndns->dev)) {
1537 struct nd_namespace_pmem *nspm;
1538
1539 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001540 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001541 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001542 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001543 struct nd_namespace_blk *nsblk;
1544
1545 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001546 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1547 return ERR_PTR(-ENODEV);
1548 if (!nsblk->lbasize) {
1549 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1550 __func__);
1551 return ERR_PTR(-ENODEV);
1552 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001553 if (!nd_namespace_blk_validate(nsblk))
1554 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001555 }
1556
1557 return ndns;
1558}
1559EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1560
Dan Williams3d880022015-05-31 15:02:11 -04001561static struct device **create_namespace_io(struct nd_region *nd_region)
1562{
1563 struct nd_namespace_io *nsio;
1564 struct device *dev, **devs;
1565 struct resource *res;
1566
1567 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1568 if (!nsio)
1569 return NULL;
1570
1571 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1572 if (!devs) {
1573 kfree(nsio);
1574 return NULL;
1575 }
1576
Dan Williams8c2f7e82015-06-25 04:20:04 -04001577 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001578 dev->type = &namespace_io_device_type;
1579 dev->parent = &nd_region->dev;
1580 res = &nsio->res;
1581 res->name = dev_name(&nd_region->dev);
1582 res->flags = IORESOURCE_MEM;
1583 res->start = nd_region->ndr_start;
1584 res->end = res->start + nd_region->ndr_size - 1;
1585
1586 devs[0] = dev;
1587 return devs;
1588}
1589
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001590static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1591 u64 cookie, u16 pos)
1592{
1593 struct nd_namespace_label *found = NULL;
1594 int i;
1595
1596 for (i = 0; i < nd_region->ndr_mappings; i++) {
1597 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001598 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001599 bool found_uuid = false;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001600
Dan Williamsae8219f2016-09-19 16:04:21 -07001601 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1602 struct nd_namespace_label *nd_label = label_ent->label;
1603 u16 position, nlabel;
1604 u64 isetcookie;
1605
1606 if (!nd_label)
1607 continue;
1608 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1609 position = __le16_to_cpu(nd_label->position);
1610 nlabel = __le16_to_cpu(nd_label->nlabel);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001611
1612 if (isetcookie != cookie)
1613 continue;
1614
1615 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1616 continue;
1617
1618 if (found_uuid) {
1619 dev_dbg(to_ndd(nd_mapping)->dev,
1620 "%s duplicate entry for uuid\n",
1621 __func__);
1622 return false;
1623 }
1624 found_uuid = true;
1625 if (nlabel != nd_region->ndr_mappings)
1626 continue;
1627 if (position != pos)
1628 continue;
1629 found = nd_label;
1630 break;
1631 }
1632 if (found)
1633 break;
1634 }
1635 return found != NULL;
1636}
1637
1638static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1639{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001640 int i;
1641
1642 if (!pmem_id)
1643 return -ENODEV;
1644
1645 for (i = 0; i < nd_region->ndr_mappings; i++) {
1646 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams0e3b0d12016-10-06 23:13:15 -07001647 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001648 struct nd_namespace_label *nd_label = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001649 u64 hw_start, hw_end, pmem_start, pmem_end;
Dan Williamsae8219f2016-09-19 16:04:21 -07001650 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001651
Dan Williams9cf8bd52016-12-15 20:04:31 -08001652 lockdep_assert_held(&nd_mapping->lock);
Dan Williamsae8219f2016-09-19 16:04:21 -07001653 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1654 nd_label = label_ent->label;
1655 if (!nd_label)
1656 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001657 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1658 break;
Dan Williamsae8219f2016-09-19 16:04:21 -07001659 nd_label = NULL;
1660 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001661
1662 if (!nd_label) {
1663 WARN_ON(1);
1664 return -EINVAL;
1665 }
1666
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001667 /*
1668 * Check that this label is compliant with the dpa
1669 * range published in NFIT
1670 */
1671 hw_start = nd_mapping->start;
1672 hw_end = hw_start + nd_mapping->size;
Dan Williamsae8219f2016-09-19 16:04:21 -07001673 pmem_start = __le64_to_cpu(nd_label->dpa);
1674 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
Dan Williams0e3b0d12016-10-06 23:13:15 -07001675 if (pmem_start >= hw_start && pmem_start < hw_end
1676 && pmem_end <= hw_end && pmem_end > hw_start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001677 /* pass */;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001678 else {
1679 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1680 dev_name(ndd->dev), nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001681 return -EINVAL;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001682 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001683
Dan Williams8a5f50d2016-09-22 15:42:59 -07001684 /* move recently validated label to the front of the list */
1685 list_move(&label_ent->list, &nd_mapping->labels);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001686 }
1687 return 0;
1688}
1689
1690/**
Dan Williams8a5f50d2016-09-22 15:42:59 -07001691 * create_namespace_pmem - validate interleave set labelling, retrieve label0
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001692 * @nd_region: region with mappings to validate
Dan Williams8a5f50d2016-09-22 15:42:59 -07001693 * @nspm: target namespace to create
1694 * @nd_label: target pmem namespace label to evaluate
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001695 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001696struct device *create_namespace_pmem(struct nd_region *nd_region,
1697 struct nd_namespace_label *nd_label)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001698{
Dan Williams86ef58a2017-02-28 18:32:48 -08001699 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001700 u64 cookie = nd_region_interleave_set_cookie(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07001701 struct nd_label_ent *label_ent;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001702 struct nd_namespace_pmem *nspm;
Dan Williamsae8219f2016-09-19 16:04:21 -07001703 struct nd_mapping *nd_mapping;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001704 resource_size_t size = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001705 struct resource *res;
1706 struct device *dev;
Dan Williamsae8219f2016-09-19 16:04:21 -07001707 int rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001708 u16 i;
1709
Dan Williams47652182016-09-15 18:08:05 -07001710 if (cookie == 0) {
1711 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
Dan Williams8a5f50d2016-09-22 15:42:59 -07001712 return ERR_PTR(-ENXIO);
Dan Williams47652182016-09-15 18:08:05 -07001713 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001714
Dan Williams8a5f50d2016-09-22 15:42:59 -07001715 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1716 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1717 nd_label->uuid);
Dan Williams86ef58a2017-02-28 18:32:48 -08001718 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1719 return ERR_PTR(-EAGAIN);
1720
1721 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1722 nd_label->uuid);
Dan Williamsae8219f2016-09-19 16:04:21 -07001723 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001724
Dan Williams8a5f50d2016-09-22 15:42:59 -07001725 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1726 if (!nspm)
1727 return ERR_PTR(-ENOMEM);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001728
Dan Williams0e3b0d12016-10-06 23:13:15 -07001729 nspm->id = -1;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001730 dev = &nspm->nsio.common.dev;
1731 dev->type = &namespace_pmem_device_type;
1732 dev->parent = &nd_region->dev;
1733 res = &nspm->nsio.res;
1734 res->name = dev_name(&nd_region->dev);
1735 res->flags = IORESOURCE_MEM;
1736
Dan Williams86ef58a2017-02-28 18:32:48 -08001737 for (i = 0; i < nd_region->ndr_mappings; i++) {
1738 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1739 continue;
1740 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1741 continue;
1742 break;
1743 }
1744
Dan Williams8a5f50d2016-09-22 15:42:59 -07001745 if (i < nd_region->ndr_mappings) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07001746 struct nvdimm_drvdata *ndd = to_ndd(&nd_region->mapping[i]);
1747
Dan Williams8a5f50d2016-09-22 15:42:59 -07001748 /*
1749 * Give up if we don't find an instance of a uuid at each
1750 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1751 * find a dimm with two instances of the same uuid.
1752 */
Dan Williams0e3b0d12016-10-06 23:13:15 -07001753 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1754 dev_name(ndd->dev), nd_label->uuid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001755 rc = -EINVAL;
Dan Williamsae8219f2016-09-19 16:04:21 -07001756 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001757 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001758
1759 /*
1760 * Fix up each mapping's 'labels' to have the validated pmem label for
1761 * that position at labels[0], and NULL at labels[1]. In the process,
1762 * check that the namespace aligns with interleave-set. We know
1763 * that it does not overlap with any blk namespaces by virtue of
1764 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1765 * succeeded).
1766 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001767 rc = select_pmem_id(nd_region, nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001768 if (rc)
1769 goto err;
1770
1771 /* Calculate total size and populate namespace properties from label0 */
1772 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsae8219f2016-09-19 16:04:21 -07001773 struct nd_namespace_label *label0;
1774
1775 nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001776 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1777 typeof(*label_ent), list);
1778 label0 = label_ent ? label_ent->label : 0;
Dan Williamsae8219f2016-09-19 16:04:21 -07001779
1780 if (!label0) {
1781 WARN_ON(1);
1782 continue;
1783 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001784
1785 size += __le64_to_cpu(label0->rawsize);
1786 if (__le16_to_cpu(label0->position) != 0)
1787 continue;
1788 WARN_ON(nspm->alt_name || nspm->uuid);
1789 nspm->alt_name = kmemdup((void __force *) label0->name,
1790 NSLABEL_NAME_LEN, GFP_KERNEL);
1791 nspm->uuid = kmemdup((void __force *) label0->uuid,
1792 NSLABEL_UUID_LEN, GFP_KERNEL);
1793 }
1794
1795 if (!nspm->alt_name || !nspm->uuid) {
1796 rc = -ENOMEM;
1797 goto err;
1798 }
1799
Dan Williams0e3b0d12016-10-06 23:13:15 -07001800 nd_namespace_pmem_set_resource(nd_region, nspm, size);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001801
Dan Williams8a5f50d2016-09-22 15:42:59 -07001802 return dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001803 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07001804 namespace_pmem_release(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001805 switch (rc) {
1806 case -EINVAL:
1807 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1808 break;
1809 case -ENODEV:
1810 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1811 break;
1812 default:
1813 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1814 __func__, rc);
1815 break;
1816 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07001817 return ERR_PTR(rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001818}
1819
Dan Williams1b40e092015-05-01 13:34:01 -04001820struct resource *nsblk_add_resource(struct nd_region *nd_region,
1821 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1822 resource_size_t start)
1823{
1824 struct nd_label_id label_id;
1825 struct resource *res;
1826
1827 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1828 res = krealloc(nsblk->res,
1829 sizeof(void *) * (nsblk->num_resources + 1),
1830 GFP_KERNEL);
1831 if (!res)
1832 return NULL;
1833 nsblk->res = (struct resource **) res;
1834 for_each_dpa_resource(ndd, res)
1835 if (strcmp(res->name, label_id.id) == 0
1836 && res->start == start) {
1837 nsblk->res[nsblk->num_resources++] = res;
1838 return res;
1839 }
1840 return NULL;
1841}
1842
1843static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1844{
1845 struct nd_namespace_blk *nsblk;
1846 struct device *dev;
1847
1848 if (!is_nd_blk(&nd_region->dev))
1849 return NULL;
1850
1851 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1852 if (!nsblk)
1853 return NULL;
1854
Dan Williams8c2f7e82015-06-25 04:20:04 -04001855 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001856 dev->type = &namespace_blk_device_type;
1857 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1858 if (nsblk->id < 0) {
1859 kfree(nsblk);
1860 return NULL;
1861 }
1862 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1863 dev->parent = &nd_region->dev;
1864 dev->groups = nd_namespace_attribute_groups;
1865
Dan Williams8c2f7e82015-06-25 04:20:04 -04001866 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001867}
1868
Dan Williams98a29c32016-09-30 15:28:27 -07001869static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
1870{
1871 struct nd_namespace_pmem *nspm;
1872 struct resource *res;
1873 struct device *dev;
1874
1875 if (!is_nd_pmem(&nd_region->dev))
1876 return NULL;
1877
1878 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1879 if (!nspm)
1880 return NULL;
1881
1882 dev = &nspm->nsio.common.dev;
1883 dev->type = &namespace_pmem_device_type;
1884 dev->parent = &nd_region->dev;
1885 res = &nspm->nsio.res;
1886 res->name = dev_name(&nd_region->dev);
1887 res->flags = IORESOURCE_MEM;
1888
1889 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1890 if (nspm->id < 0) {
1891 kfree(nspm);
1892 return NULL;
1893 }
1894 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
1895 dev->parent = &nd_region->dev;
1896 dev->groups = nd_namespace_attribute_groups;
1897 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1898
1899 return dev;
1900}
1901
1902void nd_region_create_ns_seed(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -04001903{
1904 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
Dan Williams98a29c32016-09-30 15:28:27 -07001905
1906 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
1907 return;
1908
1909 if (is_nd_blk(&nd_region->dev))
1910 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1911 else
1912 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
1913
Dan Williams1b40e092015-05-01 13:34:01 -04001914 /*
1915 * Seed creation failures are not fatal, provisioning is simply
1916 * disabled until memory becomes available
1917 */
1918 if (!nd_region->ns_seed)
Dan Williams98a29c32016-09-30 15:28:27 -07001919 dev_err(&nd_region->dev, "failed to create %s namespace\n",
1920 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
Dan Williams1b40e092015-05-01 13:34:01 -04001921 else
1922 nd_device_register(nd_region->ns_seed);
1923}
1924
Dan Williamscd034122016-03-11 10:15:36 -08001925void nd_region_create_dax_seed(struct nd_region *nd_region)
1926{
1927 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1928 nd_region->dax_seed = nd_dax_create(nd_region);
1929 /*
1930 * Seed creation failures are not fatal, provisioning is simply
1931 * disabled until memory becomes available
1932 */
1933 if (!nd_region->dax_seed)
1934 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1935}
1936
Dan Williams2dc43332015-12-13 11:41:36 -08001937void nd_region_create_pfn_seed(struct nd_region *nd_region)
1938{
1939 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1940 nd_region->pfn_seed = nd_pfn_create(nd_region);
1941 /*
1942 * Seed creation failures are not fatal, provisioning is simply
1943 * disabled until memory becomes available
1944 */
1945 if (!nd_region->pfn_seed)
1946 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1947}
1948
Dan Williams8c2f7e82015-06-25 04:20:04 -04001949void nd_region_create_btt_seed(struct nd_region *nd_region)
1950{
1951 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1952 nd_region->btt_seed = nd_btt_create(nd_region);
1953 /*
1954 * Seed creation failures are not fatal, provisioning is simply
1955 * disabled until memory becomes available
1956 */
1957 if (!nd_region->btt_seed)
1958 dev_err(&nd_region->dev, "failed to create btt namespace\n");
1959}
1960
Dan Williams8a5f50d2016-09-22 15:42:59 -07001961static int add_namespace_resource(struct nd_region *nd_region,
1962 struct nd_namespace_label *nd_label, struct device **devs,
1963 int count)
Dan Williams1b40e092015-05-01 13:34:01 -04001964{
Dan Williams8a5f50d2016-09-22 15:42:59 -07001965 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsae8219f2016-09-19 16:04:21 -07001966 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001967 int i;
1968
1969 for (i = 0; i < count; i++) {
1970 u8 *uuid = namespace_to_uuid(devs[i]);
1971 struct resource *res;
1972
1973 if (IS_ERR_OR_NULL(uuid)) {
1974 WARN_ON(1);
1975 continue;
1976 }
1977
1978 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
1979 continue;
1980 if (is_namespace_blk(devs[i])) {
1981 res = nsblk_add_resource(nd_region, ndd,
1982 to_nd_namespace_blk(devs[i]),
1983 __le64_to_cpu(nd_label->dpa));
1984 if (!res)
1985 return -ENXIO;
1986 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
1987 } else {
1988 dev_err(&nd_region->dev,
1989 "error: conflicting extents for uuid: %pUb\n",
1990 nd_label->uuid);
1991 return -ENXIO;
1992 }
1993 break;
1994 }
1995
1996 return i;
1997}
1998
1999struct device *create_namespace_blk(struct nd_region *nd_region,
2000 struct nd_namespace_label *nd_label, int count)
2001{
2002
2003 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2004 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002005 struct nd_namespace_blk *nsblk;
Nicolas Iooss238b3232016-11-26 20:18:04 +01002006 char name[NSLABEL_NAME_LEN];
Dan Williams8a5f50d2016-09-22 15:42:59 -07002007 struct device *dev = NULL;
2008 struct resource *res;
2009
2010 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2011 if (!nsblk)
2012 return ERR_PTR(-ENOMEM);
2013 dev = &nsblk->common.dev;
2014 dev->type = &namespace_blk_device_type;
2015 dev->parent = &nd_region->dev;
2016 nsblk->id = -1;
2017 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2018 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2019 GFP_KERNEL);
2020 if (!nsblk->uuid)
2021 goto blk_err;
2022 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2023 if (name[0])
2024 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2025 GFP_KERNEL);
2026 res = nsblk_add_resource(nd_region, ndd, nsblk,
2027 __le64_to_cpu(nd_label->dpa));
2028 if (!res)
2029 goto blk_err;
2030 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2031 return dev;
2032 blk_err:
2033 namespace_blk_release(dev);
2034 return ERR_PTR(-ENXIO);
2035}
2036
Dan Williams6ff3e912016-10-05 14:04:15 -07002037static int cmp_dpa(const void *a, const void *b)
2038{
2039 const struct device *dev_a = *(const struct device **) a;
2040 const struct device *dev_b = *(const struct device **) b;
2041 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2042 struct nd_namespace_pmem *nspm_a, *nspm_b;
2043
2044 if (is_namespace_io(dev_a))
2045 return 0;
2046
2047 if (is_namespace_blk(dev_a)) {
2048 nsblk_a = to_nd_namespace_blk(dev_a);
2049 nsblk_b = to_nd_namespace_blk(dev_b);
2050
2051 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2052 sizeof(resource_size_t));
2053 }
2054
2055 nspm_a = to_nd_namespace_pmem(dev_a);
2056 nspm_b = to_nd_namespace_pmem(dev_b);
2057
2058 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2059 sizeof(resource_size_t));
2060}
2061
Dan Williams8a5f50d2016-09-22 15:42:59 -07002062static struct device **scan_labels(struct nd_region *nd_region)
2063{
Dan Williamsc969e242016-10-05 15:54:46 -07002064 int i, count = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002065 struct device *dev, **devs = NULL;
2066 struct nd_label_ent *label_ent, *e;
Dan Williamsc969e242016-10-05 15:54:46 -07002067 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2068 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -04002069
Dan Williams8a5f50d2016-09-22 15:42:59 -07002070 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2071 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002072 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams1b40e092015-05-01 13:34:01 -04002073 struct device **__devs;
Dan Williamsae8219f2016-09-19 16:04:21 -07002074 u32 flags;
Dan Williams1b40e092015-05-01 13:34:01 -04002075
Dan Williamsae8219f2016-09-19 16:04:21 -07002076 if (!nd_label)
2077 continue;
2078 flags = __le32_to_cpu(nd_label->flags);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002079 if (is_nd_blk(&nd_region->dev)
2080 == !!(flags & NSLABEL_FLAG_LOCAL))
2081 /* pass, region matches label type */;
Dan Williams1b40e092015-05-01 13:34:01 -04002082 else
2083 continue;
2084
Dan Williamsc969e242016-10-05 15:54:46 -07002085 /* skip labels that describe extents outside of the region */
2086 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
2087 continue;
2088
Dan Williams8a5f50d2016-09-22 15:42:59 -07002089 i = add_namespace_resource(nd_region, nd_label, devs, count);
2090 if (i < 0)
2091 goto err;
Dan Williams1b40e092015-05-01 13:34:01 -04002092 if (i < count)
2093 continue;
2094 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2095 if (!__devs)
2096 goto err;
2097 memcpy(__devs, devs, sizeof(dev) * count);
2098 kfree(devs);
2099 devs = __devs;
2100
Dan Williams8a5f50d2016-09-22 15:42:59 -07002101 if (is_nd_blk(&nd_region->dev)) {
2102 dev = create_namespace_blk(nd_region, nd_label, count);
2103 if (IS_ERR(dev))
2104 goto err;
2105 devs[count++] = dev;
2106 } else {
2107 dev = create_namespace_pmem(nd_region, nd_label);
2108 if (IS_ERR(dev)) {
2109 switch (PTR_ERR(dev)) {
2110 case -EAGAIN:
2111 /* skip invalid labels */
2112 continue;
2113 case -ENODEV:
2114 /* fallthrough to seed creation */
2115 break;
2116 default:
2117 goto err;
2118 }
2119 } else
2120 devs[count++] = dev;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002121 }
Dan Williams1b40e092015-05-01 13:34:01 -04002122 }
2123
Dan Williams8a5f50d2016-09-22 15:42:59 -07002124 dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
2125 __func__, count, is_nd_blk(&nd_region->dev)
2126 ? "blk" : "pmem", count == 1 ? "" : "s");
Dan Williams1b40e092015-05-01 13:34:01 -04002127
2128 if (count == 0) {
2129 /* Publish a zero-sized namespace for userspace to configure. */
Dan Williamsae8219f2016-09-19 16:04:21 -07002130 nd_mapping_free_labels(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002131
2132 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2133 if (!devs)
2134 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002135 if (is_nd_blk(&nd_region->dev)) {
2136 struct nd_namespace_blk *nsblk;
2137
2138 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2139 if (!nsblk)
2140 goto err;
2141 dev = &nsblk->common.dev;
2142 dev->type = &namespace_blk_device_type;
2143 } else {
2144 struct nd_namespace_pmem *nspm;
2145
2146 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2147 if (!nspm)
2148 goto err;
2149 dev = &nspm->nsio.common.dev;
2150 dev->type = &namespace_pmem_device_type;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002151 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002152 }
Dan Williams1b40e092015-05-01 13:34:01 -04002153 dev->parent = &nd_region->dev;
2154 devs[count++] = dev;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002155 } else if (is_nd_pmem(&nd_region->dev)) {
2156 /* clean unselected labels */
2157 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07002158 struct list_head *l, *e;
2159 LIST_HEAD(list);
2160 int j;
2161
Dan Williams8a5f50d2016-09-22 15:42:59 -07002162 nd_mapping = &nd_region->mapping[i];
2163 if (list_empty(&nd_mapping->labels)) {
2164 WARN_ON(1);
2165 continue;
2166 }
Dan Williams0e3b0d12016-10-06 23:13:15 -07002167
2168 j = count;
2169 list_for_each_safe(l, e, &nd_mapping->labels) {
2170 if (!j--)
2171 break;
2172 list_move_tail(l, &list);
2173 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002174 nd_mapping_free_labels(nd_mapping);
Dan Williams0e3b0d12016-10-06 23:13:15 -07002175 list_splice_init(&list, &nd_mapping->labels);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002176 }
Dan Williams1b40e092015-05-01 13:34:01 -04002177 }
2178
Dan Williams6ff3e912016-10-05 14:04:15 -07002179 if (count > 1)
2180 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2181
Dan Williams1b40e092015-05-01 13:34:01 -04002182 return devs;
2183
Dan Williamsae8219f2016-09-19 16:04:21 -07002184 err:
Dan Carpenter75d29712016-10-12 09:34:29 +03002185 if (devs) {
2186 for (i = 0; devs[i]; i++)
2187 if (is_nd_blk(&nd_region->dev))
2188 namespace_blk_release(devs[i]);
2189 else
2190 namespace_pmem_release(devs[i]);
2191 kfree(devs);
2192 }
Dan Williams1b40e092015-05-01 13:34:01 -04002193 return NULL;
2194}
2195
Dan Williams8a5f50d2016-09-22 15:42:59 -07002196static struct device **create_namespaces(struct nd_region *nd_region)
Dan Williamsae8219f2016-09-19 16:04:21 -07002197{
2198 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2199 struct device **devs;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002200 int i;
Dan Williamsae8219f2016-09-19 16:04:21 -07002201
2202 if (nd_region->ndr_mappings == 0)
2203 return NULL;
2204
Dan Williams8a5f50d2016-09-22 15:42:59 -07002205 /* lock down all mappings while we scan labels */
2206 for (i = 0; i < nd_region->ndr_mappings; i++) {
2207 nd_mapping = &nd_region->mapping[i];
2208 mutex_lock_nested(&nd_mapping->lock, i);
2209 }
2210
2211 devs = scan_labels(nd_region);
2212
2213 for (i = 0; i < nd_region->ndr_mappings; i++) {
2214 int reverse = nd_region->ndr_mappings - 1 - i;
2215
2216 nd_mapping = &nd_region->mapping[reverse];
2217 mutex_unlock(&nd_mapping->lock);
2218 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002219
2220 return devs;
2221}
2222
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002223static int init_active_labels(struct nd_region *nd_region)
2224{
2225 int i;
2226
2227 for (i = 0; i < nd_region->ndr_mappings; i++) {
2228 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2229 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2230 struct nvdimm *nvdimm = nd_mapping->nvdimm;
Dan Williamsae8219f2016-09-19 16:04:21 -07002231 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002232 int count, j;
2233
2234 /*
Dan Williams9d62ed92017-05-04 11:47:22 -07002235 * If the dimm is disabled then we may need to prevent
2236 * the region from being activated.
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002237 */
2238 if (!ndd) {
Dan Williams9d62ed92017-05-04 11:47:22 -07002239 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2240 /* fail, label data may be unreadable */;
2241 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2242 /* fail, labels needed to disambiguate dpa */;
2243 else
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002244 return 0;
Dan Williams9d62ed92017-05-04 11:47:22 -07002245
2246 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2247 dev_name(&nd_mapping->nvdimm->dev),
2248 test_bit(NDD_LOCKED, &nvdimm->flags)
2249 ? "locked" : "disabled");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002250 return -ENXIO;
2251 }
2252 nd_mapping->ndd = ndd;
2253 atomic_inc(&nvdimm->busy);
2254 get_ndd(ndd);
2255
2256 count = nd_label_active_count(ndd);
2257 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
2258 if (!count)
2259 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002260 for (j = 0; j < count; j++) {
2261 struct nd_namespace_label *label;
2262
Dan Williamsae8219f2016-09-19 16:04:21 -07002263 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2264 if (!label_ent)
2265 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002266 label = nd_label_active(ndd, j);
Dan Williamsae8219f2016-09-19 16:04:21 -07002267 label_ent->label = label;
2268
2269 mutex_lock(&nd_mapping->lock);
2270 list_add_tail(&label_ent->list, &nd_mapping->labels);
2271 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002272 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002273
2274 if (j >= count)
2275 continue;
2276
2277 mutex_lock(&nd_mapping->lock);
2278 nd_mapping_free_labels(nd_mapping);
2279 mutex_unlock(&nd_mapping->lock);
2280 return -ENOMEM;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002281 }
2282
2283 return 0;
2284}
2285
Dan Williams3d880022015-05-31 15:02:11 -04002286int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2287{
2288 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002289 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04002290
2291 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002292 nvdimm_bus_lock(&nd_region->dev);
2293 rc = init_active_labels(nd_region);
2294 if (rc) {
2295 nvdimm_bus_unlock(&nd_region->dev);
2296 return rc;
2297 }
2298
2299 type = nd_region_to_nstype(nd_region);
2300 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04002301 case ND_DEVICE_NAMESPACE_IO:
2302 devs = create_namespace_io(nd_region);
2303 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002304 case ND_DEVICE_NAMESPACE_PMEM:
Dan Williams1b40e092015-05-01 13:34:01 -04002305 case ND_DEVICE_NAMESPACE_BLK:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002306 devs = create_namespaces(nd_region);
Dan Williams1b40e092015-05-01 13:34:01 -04002307 break;
Dan Williams3d880022015-05-31 15:02:11 -04002308 default:
2309 break;
2310 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002311 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04002312
2313 if (!devs)
2314 return -ENODEV;
2315
Dan Williams3d880022015-05-31 15:02:11 -04002316 for (i = 0; devs[i]; i++) {
2317 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04002318 int id;
Dan Williams3d880022015-05-31 15:02:11 -04002319
Dan Williams1b40e092015-05-01 13:34:01 -04002320 if (type == ND_DEVICE_NAMESPACE_BLK) {
2321 struct nd_namespace_blk *nsblk;
2322
2323 nsblk = to_nd_namespace_blk(dev);
2324 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2325 GFP_KERNEL);
2326 nsblk->id = id;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002327 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2328 struct nd_namespace_pmem *nspm;
2329
2330 nspm = to_nd_namespace_pmem(dev);
2331 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2332 GFP_KERNEL);
2333 nspm->id = id;
Dan Williams1b40e092015-05-01 13:34:01 -04002334 } else
2335 id = i;
2336
2337 if (id < 0)
2338 break;
2339 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002340 dev->groups = nd_namespace_attribute_groups;
2341 nd_device_register(dev);
2342 }
Dan Williams1b40e092015-05-01 13:34:01 -04002343 if (i)
2344 nd_region->ns_seed = devs[0];
2345
2346 if (devs[i]) {
2347 int j;
2348
2349 for (j = i; devs[j]; j++) {
2350 struct device *dev = devs[j];
2351
2352 device_initialize(dev);
2353 put_device(dev);
2354 }
2355 *err = j - i;
2356 /*
2357 * All of the namespaces we tried to register failed, so
2358 * fail region activation.
2359 */
2360 if (*err == 0)
2361 rc = -ENODEV;
2362 }
Dan Williams3d880022015-05-31 15:02:11 -04002363 kfree(devs);
2364
Dan Williams1b40e092015-05-01 13:34:01 -04002365 if (rc == -ENODEV)
2366 return rc;
2367
Dan Williams3d880022015-05-31 15:02:11 -04002368 return i;
2369}