blob: f293556cbbf6d747004b132a23c440296ec760f7 [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{
Dan Williamsc9e582a2017-05-29 23:12:19 -0700115 if (is_nd_region(dev))
Dan Williamse07ecd72016-01-05 18:37:23 -0800116 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 Williamsfa7d2e62019-01-24 17:33:06 -0800141 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamscfe30b82016-03-03 09:38:00 -0800142 struct nd_namespace_io *nsio;
Dan Williams004f1af2015-08-24 19:20:23 -0400143
144 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
145 return false;
146
147 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
148 return false;
149
150 if (is_nd_pfn(dev) || is_nd_btt(dev))
151 return false;
152
Dan Williamsfa7d2e62019-01-24 17:33:06 -0800153 if (ndns->force_raw)
154 return false;
155
Dan Williamscfe30b82016-03-03 09:38:00 -0800156 nsio = to_nd_namespace_io(dev);
157 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
158 IORESOURCE_SYSTEM_RAM,
159 IORES_DESC_NONE) == REGION_MIXED)
160 return false;
161
Dan Williams004f1af2015-08-24 19:20:23 -0400162 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
Dan Williams004f1af2015-08-24 19:20:23 -0400163}
164EXPORT_SYMBOL(pmem_should_map_pages);
165
Dan Williamsf979b132017-06-04 12:12:07 +0900166unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
167{
168 if (is_namespace_pmem(&ndns->dev)) {
169 struct nd_namespace_pmem *nspm;
170
171 nspm = to_nd_namespace_pmem(&ndns->dev);
172 if (nspm->lbasize == 0 || nspm->lbasize == 512)
173 /* default */;
174 else if (nspm->lbasize == 4096)
175 return 4096;
176 else
177 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
178 nspm->lbasize);
179 }
180
181 /*
182 * There is no namespace label (is_namespace_io()), or the label
183 * indicates the default sector size.
184 */
185 return 512;
186}
187EXPORT_SYMBOL(pmem_sector_size);
188
Vishal Verma5212e112015-06-25 04:20:32 -0400189const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
190 char *name)
191{
192 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400193 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400194
Dan Williams0731de02015-12-14 15:34:15 -0800195 if (ndns->claim && is_nd_btt(ndns->claim))
196 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400197
Dan Williams004f1af2015-08-24 19:20:23 -0400198 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams01220732016-10-05 09:09:44 -0700199 int nsidx = 0;
200
201 if (is_namespace_pmem(&ndns->dev)) {
202 struct nd_namespace_pmem *nspm;
203
204 nspm = to_nd_namespace_pmem(&ndns->dev);
205 nsidx = nspm->id;
206 }
207
208 if (nsidx)
209 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
210 suffix ? suffix : "");
211 else
212 sprintf(name, "pmem%d%s", nd_region->id,
213 suffix ? suffix : "");
Dan Williams004f1af2015-08-24 19:20:23 -0400214 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400215 struct nd_namespace_blk *nsblk;
216
217 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400218 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
219 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400220 } else {
221 return NULL;
222 }
223
224 return name;
225}
226EXPORT_SYMBOL(nvdimm_namespace_disk_name);
227
Vishal Verma6ec68952015-07-29 14:58:09 -0600228const u8 *nd_dev_to_uuid(struct device *dev)
229{
230 static const u8 null_uuid[16];
231
232 if (!dev)
233 return null_uuid;
234
235 if (is_namespace_pmem(dev)) {
236 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
237
238 return nspm->uuid;
239 } else if (is_namespace_blk(dev)) {
240 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
241
242 return nsblk->uuid;
243 } else
244 return null_uuid;
245}
246EXPORT_SYMBOL(nd_dev_to_uuid);
247
Dan Williams3d880022015-05-31 15:02:11 -0400248static ssize_t nstype_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 struct nd_region *nd_region = to_nd_region(dev->parent);
252
253 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
254}
255static DEVICE_ATTR_RO(nstype);
256
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400257static ssize_t __alt_name_store(struct device *dev, const char *buf,
258 const size_t len)
259{
260 char *input, *pos, *alt_name, **ns_altname;
261 ssize_t rc;
262
263 if (is_namespace_pmem(dev)) {
264 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
265
266 ns_altname = &nspm->alt_name;
267 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400268 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
269
270 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400271 } else
272 return -ENXIO;
273
Dan Williams8c2f7e82015-06-25 04:20:04 -0400274 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400275 return -EBUSY;
276
Andy Shevchenko3d9cbe32018-06-11 16:47:21 +0300277 input = kstrndup(buf, len, GFP_KERNEL);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400278 if (!input)
279 return -ENOMEM;
280
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400281 pos = strim(input);
282 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
283 rc = -EINVAL;
284 goto out;
285 }
286
287 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
288 if (!alt_name) {
289 rc = -ENOMEM;
290 goto out;
291 }
292 kfree(*ns_altname);
293 *ns_altname = alt_name;
294 sprintf(*ns_altname, "%s", pos);
295 rc = len;
296
297out:
298 kfree(input);
299 return rc;
300}
301
Dan Williams1b40e092015-05-01 13:34:01 -0400302static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
303{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400304 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400305 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
306 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
307 struct nd_label_id label_id;
308 resource_size_t size = 0;
309 struct resource *res;
310
311 if (!nsblk->uuid)
312 return 0;
313 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
314 for_each_dpa_resource(ndd, res)
315 if (strcmp(res->name, label_id.id) == 0)
316 size += resource_size(res);
317 return size;
318}
319
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400320static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
321{
322 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
323 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
324 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
325 struct nd_label_id label_id;
326 struct resource *res;
327 int count, i;
328
329 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
330 return false;
331
332 count = 0;
333 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
334 for_each_dpa_resource(ndd, res) {
335 if (strcmp(res->name, label_id.id) != 0)
336 continue;
337 /*
Geert Uytterhoevenae551e92016-08-31 11:45:25 +0200338 * Resources with unacknowledged adjustments indicate a
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400339 * failure to update labels
340 */
341 if (res->flags & DPA_RESOURCE_ADJUSTED)
342 return false;
343 count++;
344 }
345
346 /* These values match after a successful label update */
347 if (count != nsblk->num_resources)
348 return false;
349
350 for (i = 0; i < nsblk->num_resources; i++) {
351 struct resource *found = NULL;
352
353 for_each_dpa_resource(ndd, res)
354 if (res == nsblk->res[i]) {
355 found = res;
356 break;
357 }
358 /* stale resource */
359 if (!found)
360 return false;
361 }
362
363 return true;
364}
365
366resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
367{
368 resource_size_t size;
369
370 nvdimm_bus_lock(&nsblk->common.dev);
371 size = __nd_namespace_blk_validate(nsblk);
372 nvdimm_bus_unlock(&nsblk->common.dev);
373
374 return size;
375}
376EXPORT_SYMBOL(nd_namespace_blk_validate);
377
378
Dan Williamsf524bf22015-05-30 12:36:02 -0400379static int nd_namespace_label_update(struct nd_region *nd_region,
380 struct device *dev)
381{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400382 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400383 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400384 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400385 return 0;
386
387 /*
388 * Only allow label writes that will result in a valid namespace
389 * or deletion of an existing namespace.
390 */
391 if (is_namespace_pmem(dev)) {
392 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400393 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400394
395 if (size == 0 && nspm->uuid)
396 /* delete allocation */;
397 else if (!nspm->uuid)
398 return 0;
399
400 return nd_pmem_namespace_label_update(nd_region, nspm, size);
401 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400402 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
403 resource_size_t size = nd_namespace_blk_size(nsblk);
404
405 if (size == 0 && nsblk->uuid)
406 /* delete allocation */;
407 else if (!nsblk->uuid || !nsblk->lbasize)
408 return 0;
409
410 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400411 } else
412 return -ENXIO;
413}
414
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400415static ssize_t alt_name_store(struct device *dev,
416 struct device_attribute *attr, const char *buf, size_t len)
417{
Dan Williamsf524bf22015-05-30 12:36:02 -0400418 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400419 ssize_t rc;
420
421 device_lock(dev);
422 nvdimm_bus_lock(dev);
423 wait_nvdimm_bus_probe_idle(dev);
424 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400425 if (rc >= 0)
426 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -0800427 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400428 nvdimm_bus_unlock(dev);
429 device_unlock(dev);
430
Dan Williamsf524bf22015-05-30 12:36:02 -0400431 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400432}
433
434static ssize_t alt_name_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
436{
437 char *ns_altname;
438
439 if (is_namespace_pmem(dev)) {
440 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
441
442 ns_altname = nspm->alt_name;
443 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400444 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
445
446 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400447 } else
448 return -ENXIO;
449
450 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
451}
452static DEVICE_ATTR_RW(alt_name);
453
454static int scan_free(struct nd_region *nd_region,
455 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
456 resource_size_t n)
457{
458 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
459 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
460 int rc = 0;
461
462 while (n) {
463 struct resource *res, *last;
464 resource_size_t new_start;
465
466 last = NULL;
467 for_each_dpa_resource(ndd, res)
468 if (strcmp(res->name, label_id->id) == 0)
469 last = res;
470 res = last;
471 if (!res)
472 return 0;
473
474 if (n >= resource_size(res)) {
475 n -= resource_size(res);
476 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
477 nvdimm_free_dpa(ndd, res);
478 /* retry with last resource deleted */
479 continue;
480 }
481
482 /*
483 * Keep BLK allocations relegated to high DPA as much as
484 * possible
485 */
486 if (is_blk)
487 new_start = res->start + n;
488 else
489 new_start = res->start;
490
491 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400492 if (rc == 0)
493 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400494 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
495 break;
496 }
497
498 return rc;
499}
500
501/**
502 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
503 * @nd_region: the set of dimms to reclaim @n bytes from
504 * @label_id: unique identifier for the namespace consuming this dpa range
505 * @n: number of bytes per-dimm to release
506 *
507 * Assumes resources are ordered. Starting from the end try to
508 * adjust_resource() the allocation to @n, but if @n is larger than the
509 * allocation delete it and find the 'new' last allocation in the label
510 * set.
511 */
512static int shrink_dpa_allocation(struct nd_region *nd_region,
513 struct nd_label_id *label_id, resource_size_t n)
514{
515 int i;
516
517 for (i = 0; i < nd_region->ndr_mappings; i++) {
518 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
519 int rc;
520
521 rc = scan_free(nd_region, nd_mapping, label_id, n);
522 if (rc)
523 return rc;
524 }
525
526 return 0;
527}
528
529static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
530 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
531 resource_size_t n)
532{
533 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
534 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
535 resource_size_t first_dpa;
536 struct resource *res;
537 int rc = 0;
538
539 /* allocate blk from highest dpa first */
540 if (is_blk)
541 first_dpa = nd_mapping->start + nd_mapping->size - n;
542 else
543 first_dpa = nd_mapping->start;
544
545 /* first resource allocation for this label-id or dimm */
546 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
547 if (!res)
548 rc = -EBUSY;
549
550 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
551 return rc ? n : 0;
552}
553
Dan Williams762d0672016-10-04 16:09:59 -0700554
555/**
556 * space_valid() - validate free dpa space against constraints
557 * @nd_region: hosting region of the free space
558 * @ndd: dimm device data for debug
559 * @label_id: namespace id to allocate space
560 * @prev: potential allocation that precedes free space
561 * @next: allocation that follows the given free space range
562 * @exist: first allocation with same id in the mapping
563 * @n: range that must satisfied for pmem allocations
564 * @valid: free space range to validate
565 *
566 * BLK-space is valid as long as it does not precede a PMEM
567 * allocation in a given region. PMEM-space must be contiguous
568 * and adjacent to an existing existing allocation (if one
569 * exists). If reserving PMEM any space is valid.
570 */
571static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
572 struct nd_label_id *label_id, struct resource *prev,
573 struct resource *next, struct resource *exist,
574 resource_size_t n, struct resource *valid)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400575{
Dan Williams762d0672016-10-04 16:09:59 -0700576 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
577 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
578
579 if (valid->start >= valid->end)
580 goto invalid;
581
582 if (is_reserve)
583 return;
584
585 if (!is_pmem) {
586 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
587 struct nvdimm_bus *nvdimm_bus;
588 struct blk_alloc_info info = {
589 .nd_mapping = nd_mapping,
590 .available = nd_mapping->size,
591 .res = valid,
592 };
593
594 WARN_ON(!is_nd_blk(&nd_region->dev));
595 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
596 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
597 return;
598 }
599
600 /* allocation needs to be contiguous, so this is all or nothing */
601 if (resource_size(valid) < n)
602 goto invalid;
603
604 /* we've got all the space we need and no existing allocation */
605 if (!exist)
606 return;
607
608 /* allocation needs to be contiguous with the existing namespace */
609 if (valid->start == exist->end + 1
610 || valid->end == exist->start - 1)
611 return;
612
613 invalid:
614 /* truncate @valid size to 0 */
615 valid->end = valid->start - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400616}
617
618enum alloc_loc {
619 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
620};
621
622static resource_size_t scan_allocate(struct nd_region *nd_region,
623 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
624 resource_size_t n)
625{
626 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
627 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
628 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams762d0672016-10-04 16:09:59 -0700629 struct resource *res, *exist = NULL, valid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400630 const resource_size_t to_allocate = n;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400631 int first;
632
Dan Williams762d0672016-10-04 16:09:59 -0700633 for_each_dpa_resource(ndd, res)
634 if (strcmp(label_id->id, res->name) == 0)
635 exist = res;
636
637 valid.start = nd_mapping->start;
638 valid.end = mapping_end;
639 valid.name = "free space";
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400640 retry:
641 first = 0;
642 for_each_dpa_resource(ndd, res) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400643 struct resource *next = res->sibling, *new_res = NULL;
Dan Williams762d0672016-10-04 16:09:59 -0700644 resource_size_t allocate, available = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400645 enum alloc_loc loc = ALLOC_ERR;
646 const char *action;
647 int rc = 0;
648
649 /* ignore resources outside this nd_mapping */
650 if (res->start > mapping_end)
651 continue;
652 if (res->end < nd_mapping->start)
653 continue;
654
655 /* space at the beginning of the mapping */
656 if (!first++ && res->start > nd_mapping->start) {
Dan Williams762d0672016-10-04 16:09:59 -0700657 valid.start = nd_mapping->start;
658 valid.end = res->start - 1;
659 space_valid(nd_region, ndd, label_id, NULL, next, exist,
660 to_allocate, &valid);
661 available = resource_size(&valid);
662 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400663 loc = ALLOC_BEFORE;
664 }
665
666 /* space between allocations */
667 if (!loc && next) {
Dan Williams762d0672016-10-04 16:09:59 -0700668 valid.start = res->start + resource_size(res);
669 valid.end = min(mapping_end, next->start - 1);
670 space_valid(nd_region, ndd, label_id, res, next, exist,
671 to_allocate, &valid);
672 available = resource_size(&valid);
673 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400674 loc = ALLOC_MID;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400675 }
676
677 /* space at the end of the mapping */
678 if (!loc && !next) {
Dan Williams762d0672016-10-04 16:09:59 -0700679 valid.start = res->start + resource_size(res);
680 valid.end = mapping_end;
681 space_valid(nd_region, ndd, label_id, res, next, exist,
682 to_allocate, &valid);
683 available = resource_size(&valid);
684 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400685 loc = ALLOC_AFTER;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400686 }
687
688 if (!loc || !available)
689 continue;
690 allocate = min(available, n);
691 switch (loc) {
692 case ALLOC_BEFORE:
693 if (strcmp(res->name, label_id->id) == 0) {
694 /* adjust current resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400695 rc = adjust_resource(res, res->start - allocate,
696 resource_size(res) + allocate);
697 action = "cur grow up";
698 } else
699 action = "allocate";
700 break;
701 case ALLOC_MID:
702 if (strcmp(next->name, label_id->id) == 0) {
703 /* adjust next resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400704 rc = adjust_resource(next, next->start
705 - allocate, resource_size(next)
706 + allocate);
707 new_res = next;
708 action = "next grow up";
709 } else if (strcmp(res->name, label_id->id) == 0) {
710 action = "grow down";
711 } else
712 action = "allocate";
713 break;
714 case ALLOC_AFTER:
715 if (strcmp(res->name, label_id->id) == 0)
716 action = "grow down";
717 else
718 action = "allocate";
719 break;
720 default:
721 return n;
722 }
723
724 if (strcmp(action, "allocate") == 0) {
725 /* BLK allocate bottom up */
726 if (!is_pmem)
Dan Williams762d0672016-10-04 16:09:59 -0700727 valid.start += available - allocate;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400728
729 new_res = nvdimm_allocate_dpa(ndd, label_id,
Dan Williams762d0672016-10-04 16:09:59 -0700730 valid.start, allocate);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400731 if (!new_res)
732 rc = -EBUSY;
733 } else if (strcmp(action, "grow down") == 0) {
734 /* adjust current resource down */
735 rc = adjust_resource(res, res->start, resource_size(res)
736 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400737 if (rc == 0)
738 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400739 }
740
741 if (!new_res)
742 new_res = res;
743
744 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
745 action, loc, rc);
746
747 if (rc)
748 return n;
749
750 n -= allocate;
751 if (n) {
752 /*
753 * Retry scan with newly inserted resources.
754 * For example, if we did an ALLOC_BEFORE
755 * insertion there may also have been space
756 * available for an ALLOC_AFTER insertion, so we
757 * need to check this same resource again
758 */
759 goto retry;
760 } else
761 return 0;
762 }
763
Dan Williams1b40e092015-05-01 13:34:01 -0400764 /*
765 * If we allocated nothing in the BLK case it may be because we are in
766 * an initial "pmem-reserve pass". Only do an initial BLK allocation
767 * when none of the DPA space is reserved.
768 */
769 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400770 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
771 return n;
772}
773
Dan Williams1b40e092015-05-01 13:34:01 -0400774static int merge_dpa(struct nd_region *nd_region,
775 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
776{
777 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
778 struct resource *res;
779
780 if (strncmp("pmem", label_id->id, 4) == 0)
781 return 0;
782 retry:
783 for_each_dpa_resource(ndd, res) {
784 int rc;
785 struct resource *next = res->sibling;
786 resource_size_t end = res->start + resource_size(res);
787
788 if (!next || strcmp(res->name, label_id->id) != 0
789 || strcmp(next->name, label_id->id) != 0
790 || end != next->start)
791 continue;
792 end += resource_size(next);
793 nvdimm_free_dpa(ndd, next);
794 rc = adjust_resource(res, res->start, end - res->start);
795 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
796 if (rc)
797 return rc;
798 res->flags |= DPA_RESOURCE_ADJUSTED;
799 goto retry;
800 }
801
802 return 0;
803}
804
Keith Busch12e31292018-07-24 15:07:57 -0600805int __reserve_free_pmem(struct device *dev, void *data)
Dan Williams1b40e092015-05-01 13:34:01 -0400806{
807 struct nvdimm *nvdimm = data;
808 struct nd_region *nd_region;
809 struct nd_label_id label_id;
810 int i;
811
Dan Williamsc9e582a2017-05-29 23:12:19 -0700812 if (!is_memory(dev))
Dan Williams1b40e092015-05-01 13:34:01 -0400813 return 0;
814
815 nd_region = to_nd_region(dev);
816 if (nd_region->ndr_mappings == 0)
817 return 0;
818
819 memset(&label_id, 0, sizeof(label_id));
820 strcat(label_id.id, "pmem-reserve");
821 for (i = 0; i < nd_region->ndr_mappings; i++) {
822 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
823 resource_size_t n, rem = 0;
824
825 if (nd_mapping->nvdimm != nvdimm)
826 continue;
827
828 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
829 if (n == 0)
830 return 0;
831 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
832 dev_WARN_ONCE(&nd_region->dev, rem,
833 "pmem reserve underrun: %#llx of %#llx bytes\n",
834 (unsigned long long) n - rem,
835 (unsigned long long) n);
836 return rem ? -ENXIO : 0;
837 }
838
839 return 0;
840}
841
Keith Busch12e31292018-07-24 15:07:57 -0600842void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
Dan Williams1b40e092015-05-01 13:34:01 -0400843 struct nd_mapping *nd_mapping)
844{
845 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
846 struct resource *res, *_res;
847
848 for_each_dpa_resource_safe(ndd, res, _res)
849 if (strcmp(res->name, "pmem-reserve") == 0)
850 nvdimm_free_dpa(ndd, res);
851}
852
853static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
854 struct nd_mapping *nd_mapping)
855{
856 struct nvdimm *nvdimm = nd_mapping->nvdimm;
857 int rc;
858
859 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
860 __reserve_free_pmem);
861 if (rc)
862 release_free_pmem(nvdimm_bus, nd_mapping);
863 return rc;
864}
865
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400866/**
867 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
868 * @nd_region: the set of dimms to allocate @n more bytes from
869 * @label_id: unique identifier for the namespace consuming this dpa range
870 * @n: number of bytes per-dimm to add to the existing allocation
871 *
872 * Assumes resources are ordered. For BLK regions, first consume
873 * BLK-only available DPA free space, then consume PMEM-aliased DPA
874 * space starting at the highest DPA. For PMEM regions start
875 * allocations from the start of an interleave set and end at the first
876 * BLK allocation or the end of the interleave set, whichever comes
877 * first.
878 */
879static int grow_dpa_allocation(struct nd_region *nd_region,
880 struct nd_label_id *label_id, resource_size_t n)
881{
Dan Williams1b40e092015-05-01 13:34:01 -0400882 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
883 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400884 int i;
885
886 for (i = 0; i < nd_region->ndr_mappings; i++) {
887 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400888 resource_size_t rem = n;
889 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400890
Dan Williams1b40e092015-05-01 13:34:01 -0400891 /*
892 * In the BLK case try once with all unallocated PMEM
893 * reserved, and once without
894 */
895 for (j = is_pmem; j < 2; j++) {
896 bool blk_only = j == 0;
897
898 if (blk_only) {
899 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
900 if (rc)
901 return rc;
902 }
903 rem = scan_allocate(nd_region, nd_mapping,
904 label_id, rem);
905 if (blk_only)
906 release_free_pmem(nvdimm_bus, nd_mapping);
907
908 /* try again and allow encroachments into PMEM */
909 if (rem == 0)
910 break;
911 }
912
913 dev_WARN_ONCE(&nd_region->dev, rem,
914 "allocation underrun: %#llx of %#llx bytes\n",
915 (unsigned long long) n - rem,
916 (unsigned long long) n);
917 if (rem)
918 return -ENXIO;
919
920 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400921 if (rc)
922 return rc;
923 }
924
925 return 0;
926}
927
Dan Williams0e3b0d12016-10-06 23:13:15 -0700928static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400929 struct nd_namespace_pmem *nspm, resource_size_t size)
930{
931 struct resource *res = &nspm->nsio.res;
Dan Williams0e3b0d12016-10-06 23:13:15 -0700932 resource_size_t offset = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400933
Dan Williams0e3b0d12016-10-06 23:13:15 -0700934 if (size && !nspm->uuid) {
935 WARN_ON_ONCE(1);
936 size = 0;
937 }
938
939 if (size && nspm->uuid) {
940 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
941 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
942 struct nd_label_id label_id;
943 struct resource *res;
944
945 if (!ndd) {
946 size = 0;
947 goto out;
948 }
949
950 nd_label_gen_id(&label_id, nspm->uuid, 0);
951
952 /* calculate a spa offset from the dpa allocation offset */
953 for_each_dpa_resource(ndd, res)
954 if (strcmp(res->name, label_id.id) == 0) {
955 offset = (res->start - nd_mapping->start)
956 * nd_region->ndr_mappings;
957 goto out;
958 }
959
960 WARN_ON_ONCE(1);
961 size = 0;
962 }
963
964 out:
965 res->start = nd_region->ndr_start + offset;
966 res->end = res->start + size - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400967}
968
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300969static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
970{
971 if (!uuid) {
972 dev_dbg(dev, "%s: uuid not set\n", where);
973 return true;
974 }
975 return false;
976}
977
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400978static ssize_t __size_store(struct device *dev, unsigned long long val)
979{
980 resource_size_t allocated = 0, available = 0;
981 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams1f19b982017-01-09 17:30:49 -0800982 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400983 struct nd_mapping *nd_mapping;
984 struct nvdimm_drvdata *ndd;
985 struct nd_label_id label_id;
986 u32 flags = 0, remainder;
Dan Williams9d032f42017-01-25 00:54:07 +0530987 int rc, i, id = -1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400988 u8 *uuid = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400989
Dan Williams1f19b982017-01-09 17:30:49 -0800990 if (dev->driver || ndns->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400991 return -EBUSY;
992
993 if (is_namespace_pmem(dev)) {
994 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
995
996 uuid = nspm->uuid;
Dan Williams9d032f42017-01-25 00:54:07 +0530997 id = nspm->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400998 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400999 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1000
1001 uuid = nsblk->uuid;
1002 flags = NSLABEL_FLAG_LOCAL;
Dan Williams9d032f42017-01-25 00:54:07 +05301003 id = nsblk->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001004 }
1005
1006 /*
1007 * We need a uuid for the allocation-label and dimm(s) on which
1008 * to store the label.
1009 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001010 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001011 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001012 if (nd_region->ndr_mappings == 0) {
Dan Williams426824d2018-03-05 16:39:31 -08001013 dev_dbg(dev, "not associated with dimm(s)\n");
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001014 return -ENXIO;
1015 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001016
1017 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
1018 if (remainder) {
1019 dev_dbg(dev, "%llu is not %dK aligned\n", val,
1020 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
1021 return -EINVAL;
1022 }
1023
1024 nd_label_gen_id(&label_id, uuid, flags);
1025 for (i = 0; i < nd_region->ndr_mappings; i++) {
1026 nd_mapping = &nd_region->mapping[i];
1027 ndd = to_ndd(nd_mapping);
1028
1029 /*
1030 * All dimms in an interleave set, or the base dimm for a blk
1031 * region, need to be enabled for the size to be changed.
1032 */
1033 if (!ndd)
1034 return -ENXIO;
1035
1036 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1037 }
Keith Busch12e31292018-07-24 15:07:57 -06001038 available = nd_region_allocatable_dpa(nd_region);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001039
1040 if (val > available + allocated)
1041 return -ENOSPC;
1042
1043 if (val == allocated)
1044 return 0;
1045
1046 val = div_u64(val, nd_region->ndr_mappings);
1047 allocated = div_u64(allocated, nd_region->ndr_mappings);
1048 if (val < allocated)
1049 rc = shrink_dpa_allocation(nd_region, &label_id,
1050 allocated - val);
1051 else
1052 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1053
1054 if (rc)
1055 return rc;
1056
1057 if (is_namespace_pmem(dev)) {
1058 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1059
Dan Williams0e3b0d12016-10-06 23:13:15 -07001060 nd_namespace_pmem_set_resource(nd_region, nspm,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001061 val * nd_region->ndr_mappings);
1062 }
1063
Dan Williams1f19b982017-01-09 17:30:49 -08001064 /*
1065 * Try to delete the namespace if we deleted all of its
Dan Williams9d032f42017-01-25 00:54:07 +05301066 * allocation, this is not the seed or 0th device for the
1067 * region, and it is not actively claimed by a btt, pfn, or dax
1068 * instance.
Dan Williams1f19b982017-01-09 17:30:49 -08001069 */
Dan Williams9d032f42017-01-25 00:54:07 +05301070 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
Dan Williams1f19b982017-01-09 17:30:49 -08001071 nd_device_unregister(dev, ND_ASYNC);
1072
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001073 return rc;
1074}
1075
1076static ssize_t size_store(struct device *dev,
1077 struct device_attribute *attr, const char *buf, size_t len)
1078{
Dan Williamsf524bf22015-05-30 12:36:02 -04001079 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001080 unsigned long long val;
1081 u8 **uuid = NULL;
1082 int rc;
1083
1084 rc = kstrtoull(buf, 0, &val);
1085 if (rc)
1086 return rc;
1087
1088 device_lock(dev);
1089 nvdimm_bus_lock(dev);
1090 wait_nvdimm_bus_probe_idle(dev);
1091 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -04001092 if (rc >= 0)
1093 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001094
1095 if (is_namespace_pmem(dev)) {
1096 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1097
1098 uuid = &nspm->uuid;
1099 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001100 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1101
1102 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001103 }
1104
1105 if (rc == 0 && val == 0 && uuid) {
1106 /* setting size zero == 'delete namespace' */
1107 kfree(*uuid);
1108 *uuid = NULL;
1109 }
1110
Dan Williams426824d2018-03-05 16:39:31 -08001111 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001112
1113 nvdimm_bus_unlock(dev);
1114 device_unlock(dev);
1115
Dan Williamsf524bf22015-05-30 12:36:02 -04001116 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001117}
1118
Dan Williams8c2f7e82015-06-25 04:20:04 -04001119resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001120{
Dan Williams8c2f7e82015-06-25 04:20:04 -04001121 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001122
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001123 if (is_namespace_pmem(dev)) {
1124 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1125
Dan Williams8c2f7e82015-06-25 04:20:04 -04001126 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001127 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -04001128 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001129 } else if (is_namespace_io(dev)) {
1130 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1131
Dan Williams8c2f7e82015-06-25 04:20:04 -04001132 return resource_size(&nsio->res);
1133 } else
1134 WARN_ONCE(1, "unknown namespace type\n");
1135 return 0;
1136}
Dan Williams1b40e092015-05-01 13:34:01 -04001137
Dan Williams8c2f7e82015-06-25 04:20:04 -04001138resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1139{
1140 resource_size_t size;
1141
1142 nvdimm_bus_lock(&ndns->dev);
1143 size = __nvdimm_namespace_capacity(ndns);
1144 nvdimm_bus_unlock(&ndns->dev);
1145
1146 return size;
1147}
1148EXPORT_SYMBOL(nvdimm_namespace_capacity);
1149
Dan Williams08e6b3c2018-06-13 09:08:36 -07001150bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1151{
1152 int i;
1153 bool locked = false;
1154 struct device *dev = &ndns->dev;
1155 struct nd_region *nd_region = to_nd_region(dev->parent);
1156
1157 for (i = 0; i < nd_region->ndr_mappings; i++) {
1158 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1159 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1160
1161 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1162 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1163 locked = true;
1164 }
1165 }
1166 return locked;
1167}
1168EXPORT_SYMBOL(nvdimm_namespace_locked);
1169
Dan Williams8c2f7e82015-06-25 04:20:04 -04001170static ssize_t size_show(struct device *dev,
1171 struct device_attribute *attr, char *buf)
1172{
1173 return sprintf(buf, "%llu\n", (unsigned long long)
1174 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001175}
Fabian Frederickb44fe762016-12-04 10:54:08 -08001176static DEVICE_ATTR(size, 0444, size_show, size_store);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001177
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001178static u8 *namespace_to_uuid(struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001179{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001180 if (is_namespace_pmem(dev)) {
1181 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1182
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001183 return nspm->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001184 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001185 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1186
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001187 return nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001188 } else
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001189 return ERR_PTR(-ENXIO);
1190}
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001191
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001192static ssize_t uuid_show(struct device *dev,
1193 struct device_attribute *attr, char *buf)
1194{
1195 u8 *uuid = namespace_to_uuid(dev);
1196
1197 if (IS_ERR(uuid))
1198 return PTR_ERR(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001199 if (uuid)
1200 return sprintf(buf, "%pUb\n", uuid);
1201 return sprintf(buf, "\n");
1202}
1203
1204/**
1205 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1206 * @nd_region: parent region so we can updates all dimms in the set
1207 * @dev: namespace type for generating label_id
1208 * @new_uuid: incoming uuid
1209 * @old_uuid: reference to the uuid storage location in the namespace object
1210 */
1211static int namespace_update_uuid(struct nd_region *nd_region,
1212 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1213{
1214 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1215 struct nd_label_id old_label_id;
1216 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001217 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001218
Dan Williamsf524bf22015-05-30 12:36:02 -04001219 if (!nd_is_uuid_unique(dev, new_uuid))
1220 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001221
1222 if (*old_uuid == NULL)
1223 goto out;
1224
Dan Williamsf524bf22015-05-30 12:36:02 -04001225 /*
1226 * If we've already written a label with this uuid, then it's
1227 * too late to rename because we can't reliably update the uuid
1228 * without losing the old namespace. Userspace must delete this
1229 * namespace to abandon the old uuid.
1230 */
1231 for (i = 0; i < nd_region->ndr_mappings; i++) {
1232 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1233
1234 /*
1235 * This check by itself is sufficient because old_uuid
1236 * would be NULL above if this uuid did not exist in the
1237 * currently written set.
1238 *
1239 * FIXME: can we delete uuid with zero dpa allocated?
1240 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001241 if (list_empty(&nd_mapping->labels))
Dan Williamsf524bf22015-05-30 12:36:02 -04001242 return -EBUSY;
1243 }
1244
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001245 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1246 nd_label_gen_id(&new_label_id, new_uuid, flags);
1247 for (i = 0; i < nd_region->ndr_mappings; i++) {
1248 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1249 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1250 struct resource *res;
1251
1252 for_each_dpa_resource(ndd, res)
1253 if (strcmp(res->name, old_label_id.id) == 0)
1254 sprintf((void *) res->name, "%s",
1255 new_label_id.id);
1256 }
1257 kfree(*old_uuid);
1258 out:
1259 *old_uuid = new_uuid;
1260 return 0;
1261}
1262
1263static ssize_t uuid_store(struct device *dev,
1264 struct device_attribute *attr, const char *buf, size_t len)
1265{
1266 struct nd_region *nd_region = to_nd_region(dev->parent);
1267 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001268 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001269 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001270
1271 if (is_namespace_pmem(dev)) {
1272 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1273
1274 ns_uuid = &nspm->uuid;
1275 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001276 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1277
1278 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001279 } else
1280 return -ENXIO;
1281
1282 device_lock(dev);
1283 nvdimm_bus_lock(dev);
1284 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001285 if (to_ndns(dev)->claim)
1286 rc = -EBUSY;
1287 if (rc >= 0)
1288 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001289 if (rc >= 0)
1290 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001291 if (rc >= 0)
1292 rc = nd_namespace_label_update(nd_region, dev);
1293 else
1294 kfree(uuid);
Dan Williams426824d2018-03-05 16:39:31 -08001295 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1296 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001297 nvdimm_bus_unlock(dev);
1298 device_unlock(dev);
1299
Dan Williamsf524bf22015-05-30 12:36:02 -04001300 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001301}
1302static DEVICE_ATTR_RW(uuid);
1303
1304static ssize_t resource_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1306{
1307 struct resource *res;
1308
1309 if (is_namespace_pmem(dev)) {
1310 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1311
1312 res = &nspm->nsio.res;
1313 } else if (is_namespace_io(dev)) {
1314 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1315
1316 res = &nsio->res;
1317 } else
1318 return -ENXIO;
1319
1320 /* no address to convey if the namespace has no allocation */
1321 if (resource_size(res) == 0)
1322 return -ENXIO;
1323 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1324}
1325static DEVICE_ATTR_RO(resource);
1326
Dan Williamsf979b132017-06-04 12:12:07 +09001327static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
Vishal Vermafcae6952015-06-25 04:22:39 -04001328 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001329
Dan Williamsf979b132017-06-04 12:12:07 +09001330static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1331
Dan Williams1b40e092015-05-01 13:34:01 -04001332static ssize_t sector_size_show(struct device *dev,
1333 struct device_attribute *attr, char *buf)
1334{
Dan Williamsf979b132017-06-04 12:12:07 +09001335 if (is_namespace_blk(dev)) {
1336 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001337
Dan Williamsb2c48f92017-08-11 17:36:54 -07001338 return nd_size_select_show(nsblk->lbasize,
Dan Williamsf979b132017-06-04 12:12:07 +09001339 blk_lbasize_supported, buf);
1340 }
Dan Williams1b40e092015-05-01 13:34:01 -04001341
Dan Williamsf979b132017-06-04 12:12:07 +09001342 if (is_namespace_pmem(dev)) {
1343 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1344
Dan Williamsb2c48f92017-08-11 17:36:54 -07001345 return nd_size_select_show(nspm->lbasize,
Dan Williamsf979b132017-06-04 12:12:07 +09001346 pmem_lbasize_supported, buf);
1347 }
1348 return -ENXIO;
Dan Williams1b40e092015-05-01 13:34:01 -04001349}
1350
1351static ssize_t sector_size_store(struct device *dev,
1352 struct device_attribute *attr, const char *buf, size_t len)
1353{
Dan Williamsf524bf22015-05-30 12:36:02 -04001354 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsf979b132017-06-04 12:12:07 +09001355 const unsigned long *supported;
1356 unsigned long *lbasize;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001357 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001358
Dan Williamsf979b132017-06-04 12:12:07 +09001359 if (is_namespace_blk(dev)) {
1360 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1361
1362 lbasize = &nsblk->lbasize;
1363 supported = blk_lbasize_supported;
1364 } else if (is_namespace_pmem(dev)) {
1365 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1366
1367 lbasize = &nspm->lbasize;
1368 supported = pmem_lbasize_supported;
1369 } else
Dan Williams1b40e092015-05-01 13:34:01 -04001370 return -ENXIO;
1371
1372 device_lock(dev);
1373 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001374 if (to_ndns(dev)->claim)
1375 rc = -EBUSY;
1376 if (rc >= 0)
Dan Williamsb2c48f92017-08-11 17:36:54 -07001377 rc = nd_size_select_store(dev, buf, lbasize, supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001378 if (rc >= 0)
1379 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -08001380 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1381 buf, buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001382 nvdimm_bus_unlock(dev);
1383 device_unlock(dev);
1384
1385 return rc ? rc : len;
1386}
1387static DEVICE_ATTR_RW(sector_size);
1388
Dan Williams0ba1c632015-05-30 12:35:36 -04001389static ssize_t dpa_extents_show(struct device *dev,
1390 struct device_attribute *attr, char *buf)
1391{
1392 struct nd_region *nd_region = to_nd_region(dev->parent);
1393 struct nd_label_id label_id;
1394 int count = 0, i;
1395 u8 *uuid = NULL;
1396 u32 flags = 0;
1397
1398 nvdimm_bus_lock(dev);
1399 if (is_namespace_pmem(dev)) {
1400 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1401
1402 uuid = nspm->uuid;
1403 flags = 0;
1404 } else if (is_namespace_blk(dev)) {
1405 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1406
1407 uuid = nsblk->uuid;
1408 flags = NSLABEL_FLAG_LOCAL;
1409 }
1410
1411 if (!uuid)
1412 goto out;
1413
1414 nd_label_gen_id(&label_id, uuid, flags);
1415 for (i = 0; i < nd_region->ndr_mappings; i++) {
1416 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1417 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1418 struct resource *res;
1419
1420 for_each_dpa_resource(ndd, res)
1421 if (strcmp(res->name, label_id.id) == 0)
1422 count++;
1423 }
1424 out:
1425 nvdimm_bus_unlock(dev);
1426
1427 return sprintf(buf, "%d\n", count);
1428}
1429static DEVICE_ATTR_RO(dpa_extents);
1430
Vishal Verma14e49452017-06-28 14:25:00 -06001431static int btt_claim_class(struct device *dev)
1432{
1433 struct nd_region *nd_region = to_nd_region(dev->parent);
1434 int i, loop_bitmask = 0;
1435
1436 for (i = 0; i < nd_region->ndr_mappings; i++) {
1437 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1438 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1439 struct nd_namespace_index *nsindex;
1440
Dan Williams33a56082017-09-18 14:48:58 -07001441 /*
1442 * If any of the DIMMs do not support labels the only
1443 * possible BTT format is v1.
1444 */
1445 if (!ndd) {
1446 loop_bitmask = 0;
1447 break;
1448 }
1449
Vishal Verma14e49452017-06-28 14:25:00 -06001450 nsindex = to_namespace_index(ndd, ndd->ns_current);
1451 if (nsindex == NULL)
1452 loop_bitmask |= 1;
1453 else {
1454 /* check whether existing labels are v1.1 or v1.2 */
1455 if (__le16_to_cpu(nsindex->major) == 1
1456 && __le16_to_cpu(nsindex->minor) == 1)
1457 loop_bitmask |= 2;
1458 else
1459 loop_bitmask |= 4;
1460 }
1461 }
1462 /*
1463 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1464 * block is found, a v1.1 label for any mapping will set bit 1, and a
1465 * v1.2 label will set bit 2.
1466 *
1467 * At the end of the loop, at most one of the three bits must be set.
1468 * If multiple bits were set, it means the different mappings disagree
1469 * about their labels, and this must be cleaned up first.
1470 *
1471 * If all the label index blocks are found to agree, nsindex of NULL
1472 * implies labels haven't been initialized yet, and when they will,
1473 * they will be of the 1.2 format, so we can assume BTT2.0
1474 *
1475 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1476 * found, we enforce BTT2.0
1477 *
1478 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1479 */
1480 switch (loop_bitmask) {
1481 case 0:
1482 case 2:
1483 return NVDIMM_CCLASS_BTT;
1484 case 1:
1485 case 4:
1486 return NVDIMM_CCLASS_BTT2;
1487 default:
1488 return -ENXIO;
1489 }
1490}
1491
Dan Williams8c2f7e82015-06-25 04:20:04 -04001492static ssize_t holder_show(struct device *dev,
1493 struct device_attribute *attr, char *buf)
1494{
1495 struct nd_namespace_common *ndns = to_ndns(dev);
1496 ssize_t rc;
1497
1498 device_lock(dev);
1499 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1500 device_unlock(dev);
1501
1502 return rc;
1503}
1504static DEVICE_ATTR_RO(holder);
1505
Dan Williamsb3fde742017-06-04 10:18:39 +09001506static ssize_t __holder_class_store(struct device *dev, const char *buf)
1507{
1508 struct nd_namespace_common *ndns = to_ndns(dev);
1509
1510 if (dev->driver || ndns->claim)
1511 return -EBUSY;
1512
Dan Williams075c3fd2019-03-04 12:14:04 -08001513 if (sysfs_streq(buf, "btt"))
Vishal Verma14e49452017-06-28 14:25:00 -06001514 ndns->claim_class = btt_claim_class(dev);
Dan Williams075c3fd2019-03-04 12:14:04 -08001515 else if (sysfs_streq(buf, "pfn"))
Dan Williamsb3fde742017-06-04 10:18:39 +09001516 ndns->claim_class = NVDIMM_CCLASS_PFN;
Dan Williams075c3fd2019-03-04 12:14:04 -08001517 else if (sysfs_streq(buf, "dax"))
Dan Williamsb3fde742017-06-04 10:18:39 +09001518 ndns->claim_class = NVDIMM_CCLASS_DAX;
Dan Williams075c3fd2019-03-04 12:14:04 -08001519 else if (sysfs_streq(buf, ""))
Dan Williamsb3fde742017-06-04 10:18:39 +09001520 ndns->claim_class = NVDIMM_CCLASS_NONE;
1521 else
1522 return -EINVAL;
1523
Vishal Verma14e49452017-06-28 14:25:00 -06001524 /* btt_claim_class() could've returned an error */
1525 if (ndns->claim_class < 0)
1526 return ndns->claim_class;
1527
Dan Williamsb3fde742017-06-04 10:18:39 +09001528 return 0;
1529}
1530
1531static ssize_t holder_class_store(struct device *dev,
1532 struct device_attribute *attr, const char *buf, size_t len)
1533{
1534 struct nd_region *nd_region = to_nd_region(dev->parent);
1535 ssize_t rc;
1536
1537 device_lock(dev);
1538 nvdimm_bus_lock(dev);
1539 wait_nvdimm_bus_probe_idle(dev);
1540 rc = __holder_class_store(dev, buf);
1541 if (rc >= 0)
1542 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -08001543 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
Dan Williamsb3fde742017-06-04 10:18:39 +09001544 nvdimm_bus_unlock(dev);
1545 device_unlock(dev);
1546
1547 return rc < 0 ? rc : len;
1548}
1549
1550static ssize_t holder_class_show(struct device *dev,
1551 struct device_attribute *attr, char *buf)
1552{
1553 struct nd_namespace_common *ndns = to_ndns(dev);
1554 ssize_t rc;
1555
1556 device_lock(dev);
1557 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1558 rc = sprintf(buf, "\n");
Vishal Verma14e49452017-06-28 14:25:00 -06001559 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1560 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
Dan Williamsb3fde742017-06-04 10:18:39 +09001561 rc = sprintf(buf, "btt\n");
1562 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1563 rc = sprintf(buf, "pfn\n");
1564 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1565 rc = sprintf(buf, "dax\n");
1566 else
1567 rc = sprintf(buf, "<unknown>\n");
1568 device_unlock(dev);
1569
1570 return rc;
1571}
1572static DEVICE_ATTR_RW(holder_class);
1573
Dan Williams0731de02015-12-14 15:34:15 -08001574static ssize_t mode_show(struct device *dev,
1575 struct device_attribute *attr, char *buf)
1576{
1577 struct nd_namespace_common *ndns = to_ndns(dev);
1578 struct device *claim;
1579 char *mode;
1580 ssize_t rc;
1581
1582 device_lock(dev);
1583 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001584 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001585 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001586 else if (claim && is_nd_pfn(claim))
1587 mode = "memory";
Dan Williamscd034122016-03-11 10:15:36 -08001588 else if (claim && is_nd_dax(claim))
1589 mode = "dax";
Dan Williams9c412422016-01-23 15:34:10 -08001590 else if (!claim && pmem_should_map_pages(dev))
1591 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001592 else
1593 mode = "raw";
1594 rc = sprintf(buf, "%s\n", mode);
1595 device_unlock(dev);
1596
1597 return rc;
1598}
1599static DEVICE_ATTR_RO(mode);
1600
Dan Williams8c2f7e82015-06-25 04:20:04 -04001601static ssize_t force_raw_store(struct device *dev,
1602 struct device_attribute *attr, const char *buf, size_t len)
1603{
1604 bool force_raw;
1605 int rc = strtobool(buf, &force_raw);
1606
1607 if (rc)
1608 return rc;
1609
1610 to_ndns(dev)->force_raw = force_raw;
1611 return len;
1612}
1613
1614static ssize_t force_raw_show(struct device *dev,
1615 struct device_attribute *attr, char *buf)
1616{
1617 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1618}
1619static DEVICE_ATTR_RW(force_raw);
1620
Dan Williams3d880022015-05-31 15:02:11 -04001621static struct attribute *nd_namespace_attributes[] = {
1622 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001623 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001624 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001625 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001626 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001627 &dev_attr_resource.attr,
1628 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001629 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001630 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001631 &dev_attr_dpa_extents.attr,
Dan Williamsb3fde742017-06-04 10:18:39 +09001632 &dev_attr_holder_class.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001633 NULL,
1634};
1635
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001636static umode_t namespace_visible(struct kobject *kobj,
1637 struct attribute *a, int n)
1638{
1639 struct device *dev = container_of(kobj, struct device, kobj);
1640
1641 if (a == &dev_attr_resource.attr) {
1642 if (is_namespace_blk(dev))
1643 return 0;
Dan Williamsc1fb3542017-09-26 11:21:24 -07001644 return 0400;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001645 }
1646
1647 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1648 if (a == &dev_attr_size.attr)
Fabian Frederickb44fe762016-12-04 10:54:08 -08001649 return 0644;
Dan Williams1b40e092015-05-01 13:34:01 -04001650
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001651 return a->mode;
1652 }
1653
Dan Williams8c2f7e82015-06-25 04:20:04 -04001654 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1655 || a == &dev_attr_holder.attr
Dan Williamsb3fde742017-06-04 10:18:39 +09001656 || a == &dev_attr_holder_class.attr
Dan Williams0731de02015-12-14 15:34:15 -08001657 || a == &dev_attr_force_raw.attr
1658 || a == &dev_attr_mode.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001659 return a->mode;
1660
1661 return 0;
1662}
1663
Dan Williams3d880022015-05-31 15:02:11 -04001664static struct attribute_group nd_namespace_attribute_group = {
1665 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001666 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001667};
1668
1669static const struct attribute_group *nd_namespace_attribute_groups[] = {
1670 &nd_device_attribute_group,
1671 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001672 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001673 NULL,
1674};
1675
Dan Williams8c2f7e82015-06-25 04:20:04 -04001676struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1677{
1678 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001679 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williamscd034122016-03-11 10:15:36 -08001680 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001681 struct nd_namespace_common *ndns = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001682 resource_size_t size;
1683
Dan Williamscd034122016-03-11 10:15:36 -08001684 if (nd_btt || nd_pfn || nd_dax) {
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001685 if (nd_btt)
Dan Williamse1455742015-07-30 17:57:47 -04001686 ndns = nd_btt->ndns;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001687 else if (nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -04001688 ndns = nd_pfn->ndns;
Dan Williamscd034122016-03-11 10:15:36 -08001689 else if (nd_dax)
1690 ndns = nd_dax->nd_pfn.ndns;
Dan Williamse1455742015-07-30 17:57:47 -04001691
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001692 if (!ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001693 return ERR_PTR(-ENODEV);
1694
1695 /*
1696 * Flush any in-progess probes / removals in the driver
1697 * for the raw personality of this namespace.
1698 */
1699 device_lock(&ndns->dev);
1700 device_unlock(&ndns->dev);
1701 if (ndns->dev.driver) {
1702 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001703 dev_name(dev));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001704 return ERR_PTR(-EBUSY);
1705 }
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001706 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001707 "host (%s) vs claim (%s) mismatch\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001708 dev_name(dev),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001709 dev_name(ndns->claim)))
1710 return ERR_PTR(-ENXIO);
1711 } else {
1712 ndns = to_ndns(dev);
1713 if (ndns->claim) {
1714 dev_dbg(dev, "claimed by %s, failing probe\n",
1715 dev_name(ndns->claim));
1716
1717 return ERR_PTR(-ENXIO);
1718 }
1719 }
1720
Dan Williams08e6b3c2018-06-13 09:08:36 -07001721 if (nvdimm_namespace_locked(ndns))
1722 return ERR_PTR(-EACCES);
1723
Dan Williams8c2f7e82015-06-25 04:20:04 -04001724 size = nvdimm_namespace_capacity(ndns);
1725 if (size < ND_MIN_NAMESPACE_SIZE) {
1726 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1727 &size, ND_MIN_NAMESPACE_SIZE);
1728 return ERR_PTR(-ENODEV);
1729 }
1730
1731 if (is_namespace_pmem(&ndns->dev)) {
1732 struct nd_namespace_pmem *nspm;
1733
1734 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001735 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001736 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001737 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001738 struct nd_namespace_blk *nsblk;
1739
1740 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001741 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1742 return ERR_PTR(-ENODEV);
1743 if (!nsblk->lbasize) {
Dan Williams426824d2018-03-05 16:39:31 -08001744 dev_dbg(&ndns->dev, "sector size not set\n");
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001745 return ERR_PTR(-ENODEV);
1746 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001747 if (!nd_namespace_blk_validate(nsblk))
1748 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001749 }
1750
1751 return ndns;
1752}
1753EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1754
Dan Williams3d880022015-05-31 15:02:11 -04001755static struct device **create_namespace_io(struct nd_region *nd_region)
1756{
1757 struct nd_namespace_io *nsio;
1758 struct device *dev, **devs;
1759 struct resource *res;
1760
1761 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1762 if (!nsio)
1763 return NULL;
1764
1765 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1766 if (!devs) {
1767 kfree(nsio);
1768 return NULL;
1769 }
1770
Dan Williams8c2f7e82015-06-25 04:20:04 -04001771 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001772 dev->type = &namespace_io_device_type;
1773 dev->parent = &nd_region->dev;
1774 res = &nsio->res;
1775 res->name = dev_name(&nd_region->dev);
1776 res->flags = IORESOURCE_MEM;
1777 res->start = nd_region->ndr_start;
1778 res->end = res->start + nd_region->ndr_size - 1;
1779
1780 devs[0] = dev;
1781 return devs;
1782}
1783
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001784static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1785 u64 cookie, u16 pos)
1786{
1787 struct nd_namespace_label *found = NULL;
1788 int i;
1789
1790 for (i = 0; i < nd_region->ndr_mappings; i++) {
1791 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsfaec6f82017-06-06 11:10:51 -07001792 struct nd_interleave_set *nd_set = nd_region->nd_set;
1793 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001794 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001795 bool found_uuid = false;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001796
Dan Williamsae8219f2016-09-19 16:04:21 -07001797 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1798 struct nd_namespace_label *nd_label = label_ent->label;
1799 u16 position, nlabel;
1800 u64 isetcookie;
1801
1802 if (!nd_label)
1803 continue;
1804 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1805 position = __le16_to_cpu(nd_label->position);
1806 nlabel = __le16_to_cpu(nd_label->nlabel);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001807
1808 if (isetcookie != cookie)
1809 continue;
1810
1811 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1812 continue;
1813
Dan Williamsfaec6f82017-06-06 11:10:51 -07001814 if (namespace_label_has(ndd, type_guid)
1815 && !guid_equal(&nd_set->type_guid,
1816 &nd_label->type_guid)) {
1817 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
1818 nd_set->type_guid.b,
1819 nd_label->type_guid.b);
1820 continue;
1821 }
1822
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001823 if (found_uuid) {
Dan Williams426824d2018-03-05 16:39:31 -08001824 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001825 return false;
1826 }
1827 found_uuid = true;
1828 if (nlabel != nd_region->ndr_mappings)
1829 continue;
1830 if (position != pos)
1831 continue;
1832 found = nd_label;
1833 break;
1834 }
1835 if (found)
1836 break;
1837 }
1838 return found != NULL;
1839}
1840
1841static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1842{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001843 int i;
1844
1845 if (!pmem_id)
1846 return -ENODEV;
1847
1848 for (i = 0; i < nd_region->ndr_mappings; i++) {
1849 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams0e3b0d12016-10-06 23:13:15 -07001850 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001851 struct nd_namespace_label *nd_label = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001852 u64 hw_start, hw_end, pmem_start, pmem_end;
Dan Williamsae8219f2016-09-19 16:04:21 -07001853 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001854
Dan Williams9cf8bd52016-12-15 20:04:31 -08001855 lockdep_assert_held(&nd_mapping->lock);
Dan Williamsae8219f2016-09-19 16:04:21 -07001856 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1857 nd_label = label_ent->label;
1858 if (!nd_label)
1859 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001860 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1861 break;
Dan Williamsae8219f2016-09-19 16:04:21 -07001862 nd_label = NULL;
1863 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001864
1865 if (!nd_label) {
1866 WARN_ON(1);
1867 return -EINVAL;
1868 }
1869
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001870 /*
1871 * Check that this label is compliant with the dpa
1872 * range published in NFIT
1873 */
1874 hw_start = nd_mapping->start;
1875 hw_end = hw_start + nd_mapping->size;
Dan Williamsae8219f2016-09-19 16:04:21 -07001876 pmem_start = __le64_to_cpu(nd_label->dpa);
1877 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
Dan Williams0e3b0d12016-10-06 23:13:15 -07001878 if (pmem_start >= hw_start && pmem_start < hw_end
1879 && pmem_end <= hw_end && pmem_end > hw_start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001880 /* pass */;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001881 else {
1882 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1883 dev_name(ndd->dev), nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001884 return -EINVAL;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001885 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001886
Dan Williams8a5f50d2016-09-22 15:42:59 -07001887 /* move recently validated label to the front of the list */
1888 list_move(&label_ent->list, &nd_mapping->labels);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001889 }
1890 return 0;
1891}
1892
1893/**
Dan Williams8a5f50d2016-09-22 15:42:59 -07001894 * create_namespace_pmem - validate interleave set labelling, retrieve label0
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001895 * @nd_region: region with mappings to validate
Dan Williams8a5f50d2016-09-22 15:42:59 -07001896 * @nspm: target namespace to create
1897 * @nd_label: target pmem namespace label to evaluate
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001898 */
Colin Ian King65853a12017-10-05 10:55:57 +01001899static struct device *create_namespace_pmem(struct nd_region *nd_region,
Dan Williamsc12c48c2017-06-04 10:59:15 +09001900 struct nd_namespace_index *nsindex,
Dan Williams8a5f50d2016-09-22 15:42:59 -07001901 struct nd_namespace_label *nd_label)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001902{
Dan Williamsc12c48c2017-06-04 10:59:15 +09001903 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
Dan Williams86ef58a2017-02-28 18:32:48 -08001904 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07001905 struct nd_label_ent *label_ent;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001906 struct nd_namespace_pmem *nspm;
Dan Williamsae8219f2016-09-19 16:04:21 -07001907 struct nd_mapping *nd_mapping;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001908 resource_size_t size = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001909 struct resource *res;
1910 struct device *dev;
Dan Williamsae8219f2016-09-19 16:04:21 -07001911 int rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001912 u16 i;
1913
Dan Williams47652182016-09-15 18:08:05 -07001914 if (cookie == 0) {
1915 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
Dan Williams8a5f50d2016-09-22 15:42:59 -07001916 return ERR_PTR(-ENXIO);
Dan Williams47652182016-09-15 18:08:05 -07001917 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001918
Dan Williams8a5f50d2016-09-22 15:42:59 -07001919 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1920 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1921 nd_label->uuid);
Dan Williams86ef58a2017-02-28 18:32:48 -08001922 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1923 return ERR_PTR(-EAGAIN);
1924
1925 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1926 nd_label->uuid);
Dan Williamsae8219f2016-09-19 16:04:21 -07001927 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001928
Dan Williams8a5f50d2016-09-22 15:42:59 -07001929 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1930 if (!nspm)
1931 return ERR_PTR(-ENOMEM);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001932
Dan Williams0e3b0d12016-10-06 23:13:15 -07001933 nspm->id = -1;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001934 dev = &nspm->nsio.common.dev;
1935 dev->type = &namespace_pmem_device_type;
1936 dev->parent = &nd_region->dev;
1937 res = &nspm->nsio.res;
1938 res->name = dev_name(&nd_region->dev);
1939 res->flags = IORESOURCE_MEM;
1940
Dan Williams86ef58a2017-02-28 18:32:48 -08001941 for (i = 0; i < nd_region->ndr_mappings; i++) {
1942 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1943 continue;
1944 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1945 continue;
1946 break;
1947 }
1948
Dan Williams8a5f50d2016-09-22 15:42:59 -07001949 if (i < nd_region->ndr_mappings) {
Dan Williams4f867222018-04-06 16:37:21 -07001950 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001951
Dan Williams8a5f50d2016-09-22 15:42:59 -07001952 /*
1953 * Give up if we don't find an instance of a uuid at each
1954 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1955 * find a dimm with two instances of the same uuid.
1956 */
Dan Williams0e3b0d12016-10-06 23:13:15 -07001957 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
Dan Williams4f867222018-04-06 16:37:21 -07001958 nvdimm_name(nvdimm), nd_label->uuid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001959 rc = -EINVAL;
Dan Williamsae8219f2016-09-19 16:04:21 -07001960 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001961 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001962
1963 /*
1964 * Fix up each mapping's 'labels' to have the validated pmem label for
1965 * that position at labels[0], and NULL at labels[1]. In the process,
1966 * check that the namespace aligns with interleave-set. We know
1967 * that it does not overlap with any blk namespaces by virtue of
1968 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1969 * succeeded).
1970 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001971 rc = select_pmem_id(nd_region, nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001972 if (rc)
1973 goto err;
1974
1975 /* Calculate total size and populate namespace properties from label0 */
1976 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsae8219f2016-09-19 16:04:21 -07001977 struct nd_namespace_label *label0;
Dan Williamsb3fde742017-06-04 10:18:39 +09001978 struct nvdimm_drvdata *ndd;
Dan Williamsae8219f2016-09-19 16:04:21 -07001979
1980 nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001981 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1982 typeof(*label_ent), list);
1983 label0 = label_ent ? label_ent->label : 0;
Dan Williamsae8219f2016-09-19 16:04:21 -07001984
1985 if (!label0) {
1986 WARN_ON(1);
1987 continue;
1988 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001989
1990 size += __le64_to_cpu(label0->rawsize);
1991 if (__le16_to_cpu(label0->position) != 0)
1992 continue;
1993 WARN_ON(nspm->alt_name || nspm->uuid);
1994 nspm->alt_name = kmemdup((void __force *) label0->name,
1995 NSLABEL_NAME_LEN, GFP_KERNEL);
1996 nspm->uuid = kmemdup((void __force *) label0->uuid,
1997 NSLABEL_UUID_LEN, GFP_KERNEL);
Dan Williamsf979b132017-06-04 12:12:07 +09001998 nspm->lbasize = __le64_to_cpu(label0->lbasize);
Dan Williamsb3fde742017-06-04 10:18:39 +09001999 ndd = to_ndd(nd_mapping);
2000 if (namespace_label_has(ndd, abstraction_guid))
2001 nspm->nsio.common.claim_class
2002 = to_nvdimm_cclass(&label0->abstraction_guid);
2003
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002004 }
2005
2006 if (!nspm->alt_name || !nspm->uuid) {
2007 rc = -ENOMEM;
2008 goto err;
2009 }
2010
Dan Williams0e3b0d12016-10-06 23:13:15 -07002011 nd_namespace_pmem_set_resource(nd_region, nspm, size);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002012
Dan Williams8a5f50d2016-09-22 15:42:59 -07002013 return dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002014 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002015 namespace_pmem_release(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002016 switch (rc) {
2017 case -EINVAL:
Dan Williams426824d2018-03-05 16:39:31 -08002018 dev_dbg(&nd_region->dev, "invalid label(s)\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002019 break;
2020 case -ENODEV:
Dan Williams426824d2018-03-05 16:39:31 -08002021 dev_dbg(&nd_region->dev, "label not found\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002022 break;
2023 default:
Dan Williams426824d2018-03-05 16:39:31 -08002024 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002025 break;
2026 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002027 return ERR_PTR(rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002028}
2029
Dan Williams1b40e092015-05-01 13:34:01 -04002030struct resource *nsblk_add_resource(struct nd_region *nd_region,
2031 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2032 resource_size_t start)
2033{
2034 struct nd_label_id label_id;
2035 struct resource *res;
2036
2037 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2038 res = krealloc(nsblk->res,
2039 sizeof(void *) * (nsblk->num_resources + 1),
2040 GFP_KERNEL);
2041 if (!res)
2042 return NULL;
2043 nsblk->res = (struct resource **) res;
2044 for_each_dpa_resource(ndd, res)
2045 if (strcmp(res->name, label_id.id) == 0
2046 && res->start == start) {
2047 nsblk->res[nsblk->num_resources++] = res;
2048 return res;
2049 }
2050 return NULL;
2051}
2052
2053static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2054{
2055 struct nd_namespace_blk *nsblk;
2056 struct device *dev;
2057
2058 if (!is_nd_blk(&nd_region->dev))
2059 return NULL;
2060
2061 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2062 if (!nsblk)
2063 return NULL;
2064
Dan Williams8c2f7e82015-06-25 04:20:04 -04002065 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002066 dev->type = &namespace_blk_device_type;
2067 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2068 if (nsblk->id < 0) {
2069 kfree(nsblk);
2070 return NULL;
2071 }
2072 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2073 dev->parent = &nd_region->dev;
2074 dev->groups = nd_namespace_attribute_groups;
2075
Dan Williams8c2f7e82015-06-25 04:20:04 -04002076 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002077}
2078
Dan Williams98a29c32016-09-30 15:28:27 -07002079static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2080{
2081 struct nd_namespace_pmem *nspm;
2082 struct resource *res;
2083 struct device *dev;
2084
Dan Williamsc9e582a2017-05-29 23:12:19 -07002085 if (!is_memory(&nd_region->dev))
Dan Williams98a29c32016-09-30 15:28:27 -07002086 return NULL;
2087
2088 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2089 if (!nspm)
2090 return NULL;
2091
2092 dev = &nspm->nsio.common.dev;
2093 dev->type = &namespace_pmem_device_type;
2094 dev->parent = &nd_region->dev;
2095 res = &nspm->nsio.res;
2096 res->name = dev_name(&nd_region->dev);
2097 res->flags = IORESOURCE_MEM;
2098
2099 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2100 if (nspm->id < 0) {
2101 kfree(nspm);
2102 return NULL;
2103 }
2104 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
Dan Williams98a29c32016-09-30 15:28:27 -07002105 dev->groups = nd_namespace_attribute_groups;
2106 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2107
2108 return dev;
2109}
2110
2111void nd_region_create_ns_seed(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -04002112{
2113 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
Dan Williams98a29c32016-09-30 15:28:27 -07002114
2115 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2116 return;
2117
2118 if (is_nd_blk(&nd_region->dev))
2119 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2120 else
2121 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2122
Dan Williams1b40e092015-05-01 13:34:01 -04002123 /*
2124 * Seed creation failures are not fatal, provisioning is simply
2125 * disabled until memory becomes available
2126 */
2127 if (!nd_region->ns_seed)
Dan Williams98a29c32016-09-30 15:28:27 -07002128 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2129 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
Dan Williams1b40e092015-05-01 13:34:01 -04002130 else
2131 nd_device_register(nd_region->ns_seed);
2132}
2133
Dan Williamscd034122016-03-11 10:15:36 -08002134void nd_region_create_dax_seed(struct nd_region *nd_region)
2135{
2136 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2137 nd_region->dax_seed = nd_dax_create(nd_region);
2138 /*
2139 * Seed creation failures are not fatal, provisioning is simply
2140 * disabled until memory becomes available
2141 */
2142 if (!nd_region->dax_seed)
2143 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2144}
2145
Dan Williams2dc43332015-12-13 11:41:36 -08002146void nd_region_create_pfn_seed(struct nd_region *nd_region)
2147{
2148 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2149 nd_region->pfn_seed = nd_pfn_create(nd_region);
2150 /*
2151 * Seed creation failures are not fatal, provisioning is simply
2152 * disabled until memory becomes available
2153 */
2154 if (!nd_region->pfn_seed)
2155 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2156}
2157
Dan Williams8c2f7e82015-06-25 04:20:04 -04002158void nd_region_create_btt_seed(struct nd_region *nd_region)
2159{
2160 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2161 nd_region->btt_seed = nd_btt_create(nd_region);
2162 /*
2163 * Seed creation failures are not fatal, provisioning is simply
2164 * disabled until memory becomes available
2165 */
2166 if (!nd_region->btt_seed)
2167 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2168}
2169
Dan Williams8a5f50d2016-09-22 15:42:59 -07002170static int add_namespace_resource(struct nd_region *nd_region,
2171 struct nd_namespace_label *nd_label, struct device **devs,
2172 int count)
Dan Williams1b40e092015-05-01 13:34:01 -04002173{
Dan Williams8a5f50d2016-09-22 15:42:59 -07002174 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsae8219f2016-09-19 16:04:21 -07002175 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002176 int i;
2177
2178 for (i = 0; i < count; i++) {
2179 u8 *uuid = namespace_to_uuid(devs[i]);
2180 struct resource *res;
2181
2182 if (IS_ERR_OR_NULL(uuid)) {
2183 WARN_ON(1);
2184 continue;
2185 }
2186
2187 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2188 continue;
2189 if (is_namespace_blk(devs[i])) {
2190 res = nsblk_add_resource(nd_region, ndd,
2191 to_nd_namespace_blk(devs[i]),
2192 __le64_to_cpu(nd_label->dpa));
2193 if (!res)
2194 return -ENXIO;
2195 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2196 } else {
2197 dev_err(&nd_region->dev,
2198 "error: conflicting extents for uuid: %pUb\n",
2199 nd_label->uuid);
2200 return -ENXIO;
2201 }
2202 break;
2203 }
2204
2205 return i;
2206}
2207
Colin Ian King65853a12017-10-05 10:55:57 +01002208static struct device *create_namespace_blk(struct nd_region *nd_region,
Dan Williams8a5f50d2016-09-22 15:42:59 -07002209 struct nd_namespace_label *nd_label, int count)
2210{
2211
2212 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsfaec6f82017-06-06 11:10:51 -07002213 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002214 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002215 struct nd_namespace_blk *nsblk;
Nicolas Iooss238b3232016-11-26 20:18:04 +01002216 char name[NSLABEL_NAME_LEN];
Dan Williams8a5f50d2016-09-22 15:42:59 -07002217 struct device *dev = NULL;
2218 struct resource *res;
2219
Dan Williams8f2bc242017-06-06 11:39:30 -07002220 if (namespace_label_has(ndd, type_guid)) {
2221 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
2222 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
2223 nd_set->type_guid.b,
2224 nd_label->type_guid.b);
2225 return ERR_PTR(-EAGAIN);
2226 }
2227
2228 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
2229 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
2230 nd_set->cookie2,
2231 __le64_to_cpu(nd_label->isetcookie));
2232 return ERR_PTR(-EAGAIN);
2233 }
Dan Williamsfaec6f82017-06-06 11:10:51 -07002234 }
2235
Dan Williams8a5f50d2016-09-22 15:42:59 -07002236 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2237 if (!nsblk)
2238 return ERR_PTR(-ENOMEM);
2239 dev = &nsblk->common.dev;
2240 dev->type = &namespace_blk_device_type;
2241 dev->parent = &nd_region->dev;
2242 nsblk->id = -1;
2243 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2244 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2245 GFP_KERNEL);
Dan Williamsb3fde742017-06-04 10:18:39 +09002246 if (namespace_label_has(ndd, abstraction_guid))
2247 nsblk->common.claim_class
2248 = to_nvdimm_cclass(&nd_label->abstraction_guid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002249 if (!nsblk->uuid)
2250 goto blk_err;
2251 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
Kangjie Lu55c1fc02019-03-12 03:20:34 -05002252 if (name[0]) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07002253 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2254 GFP_KERNEL);
Kangjie Lu55c1fc02019-03-12 03:20:34 -05002255 if (!nsblk->alt_name)
2256 goto blk_err;
2257 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002258 res = nsblk_add_resource(nd_region, ndd, nsblk,
2259 __le64_to_cpu(nd_label->dpa));
2260 if (!res)
2261 goto blk_err;
2262 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2263 return dev;
2264 blk_err:
2265 namespace_blk_release(dev);
2266 return ERR_PTR(-ENXIO);
2267}
2268
Dan Williams6ff3e912016-10-05 14:04:15 -07002269static int cmp_dpa(const void *a, const void *b)
2270{
2271 const struct device *dev_a = *(const struct device **) a;
2272 const struct device *dev_b = *(const struct device **) b;
2273 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2274 struct nd_namespace_pmem *nspm_a, *nspm_b;
2275
2276 if (is_namespace_io(dev_a))
2277 return 0;
2278
2279 if (is_namespace_blk(dev_a)) {
2280 nsblk_a = to_nd_namespace_blk(dev_a);
2281 nsblk_b = to_nd_namespace_blk(dev_b);
2282
2283 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2284 sizeof(resource_size_t));
2285 }
2286
2287 nspm_a = to_nd_namespace_pmem(dev_a);
2288 nspm_b = to_nd_namespace_pmem(dev_b);
2289
2290 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2291 sizeof(resource_size_t));
2292}
2293
Dan Williams8a5f50d2016-09-22 15:42:59 -07002294static struct device **scan_labels(struct nd_region *nd_region)
2295{
Dan Williamsc969e242016-10-05 15:54:46 -07002296 int i, count = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002297 struct device *dev, **devs = NULL;
2298 struct nd_label_ent *label_ent, *e;
Dan Williamsc969e242016-10-05 15:54:46 -07002299 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2300 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -04002301
Dan Williams8a5f50d2016-09-22 15:42:59 -07002302 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2303 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002304 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams1b40e092015-05-01 13:34:01 -04002305 struct device **__devs;
Dan Williamsae8219f2016-09-19 16:04:21 -07002306 u32 flags;
Dan Williams1b40e092015-05-01 13:34:01 -04002307
Dan Williamsae8219f2016-09-19 16:04:21 -07002308 if (!nd_label)
2309 continue;
2310 flags = __le32_to_cpu(nd_label->flags);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002311 if (is_nd_blk(&nd_region->dev)
2312 == !!(flags & NSLABEL_FLAG_LOCAL))
2313 /* pass, region matches label type */;
Dan Williams1b40e092015-05-01 13:34:01 -04002314 else
2315 continue;
2316
Dan Williamsc969e242016-10-05 15:54:46 -07002317 /* skip labels that describe extents outside of the region */
2318 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
2319 continue;
2320
Dan Williams8a5f50d2016-09-22 15:42:59 -07002321 i = add_namespace_resource(nd_region, nd_label, devs, count);
2322 if (i < 0)
2323 goto err;
Dan Williams1b40e092015-05-01 13:34:01 -04002324 if (i < count)
2325 continue;
2326 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2327 if (!__devs)
2328 goto err;
2329 memcpy(__devs, devs, sizeof(dev) * count);
2330 kfree(devs);
2331 devs = __devs;
2332
Dan Williamsfaec6f82017-06-06 11:10:51 -07002333 if (is_nd_blk(&nd_region->dev))
Dan Williams8a5f50d2016-09-22 15:42:59 -07002334 dev = create_namespace_blk(nd_region, nd_label, count);
Dan Williamsfaec6f82017-06-06 11:10:51 -07002335 else {
Dan Williamsc12c48c2017-06-04 10:59:15 +09002336 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2337 struct nd_namespace_index *nsindex;
2338
2339 nsindex = to_namespace_index(ndd, ndd->ns_current);
2340 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002341 }
Dan Williamsfaec6f82017-06-06 11:10:51 -07002342
2343 if (IS_ERR(dev)) {
2344 switch (PTR_ERR(dev)) {
2345 case -EAGAIN:
2346 /* skip invalid labels */
2347 continue;
2348 case -ENODEV:
2349 /* fallthrough to seed creation */
2350 break;
2351 default:
2352 goto err;
2353 }
2354 } else
2355 devs[count++] = dev;
2356
Dan Williams1b40e092015-05-01 13:34:01 -04002357 }
2358
Dan Williams426824d2018-03-05 16:39:31 -08002359 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2360 count, is_nd_blk(&nd_region->dev)
Dan Williams8a5f50d2016-09-22 15:42:59 -07002361 ? "blk" : "pmem", count == 1 ? "" : "s");
Dan Williams1b40e092015-05-01 13:34:01 -04002362
2363 if (count == 0) {
2364 /* Publish a zero-sized namespace for userspace to configure. */
Dan Williamsae8219f2016-09-19 16:04:21 -07002365 nd_mapping_free_labels(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002366
2367 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2368 if (!devs)
2369 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002370 if (is_nd_blk(&nd_region->dev)) {
2371 struct nd_namespace_blk *nsblk;
2372
2373 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2374 if (!nsblk)
2375 goto err;
2376 dev = &nsblk->common.dev;
2377 dev->type = &namespace_blk_device_type;
2378 } else {
2379 struct nd_namespace_pmem *nspm;
2380
2381 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2382 if (!nspm)
2383 goto err;
2384 dev = &nspm->nsio.common.dev;
2385 dev->type = &namespace_pmem_device_type;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002386 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002387 }
Dan Williams1b40e092015-05-01 13:34:01 -04002388 dev->parent = &nd_region->dev;
2389 devs[count++] = dev;
Dan Williamsc9e582a2017-05-29 23:12:19 -07002390 } else if (is_memory(&nd_region->dev)) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07002391 /* clean unselected labels */
2392 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07002393 struct list_head *l, *e;
2394 LIST_HEAD(list);
2395 int j;
2396
Dan Williams8a5f50d2016-09-22 15:42:59 -07002397 nd_mapping = &nd_region->mapping[i];
2398 if (list_empty(&nd_mapping->labels)) {
2399 WARN_ON(1);
2400 continue;
2401 }
Dan Williams0e3b0d12016-10-06 23:13:15 -07002402
2403 j = count;
2404 list_for_each_safe(l, e, &nd_mapping->labels) {
2405 if (!j--)
2406 break;
2407 list_move_tail(l, &list);
2408 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002409 nd_mapping_free_labels(nd_mapping);
Dan Williams0e3b0d12016-10-06 23:13:15 -07002410 list_splice_init(&list, &nd_mapping->labels);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002411 }
Dan Williams1b40e092015-05-01 13:34:01 -04002412 }
2413
Dan Williams6ff3e912016-10-05 14:04:15 -07002414 if (count > 1)
2415 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2416
Dan Williams1b40e092015-05-01 13:34:01 -04002417 return devs;
2418
Dan Williamsae8219f2016-09-19 16:04:21 -07002419 err:
Dan Carpenter75d29712016-10-12 09:34:29 +03002420 if (devs) {
2421 for (i = 0; devs[i]; i++)
2422 if (is_nd_blk(&nd_region->dev))
2423 namespace_blk_release(devs[i]);
2424 else
2425 namespace_pmem_release(devs[i]);
2426 kfree(devs);
2427 }
Dan Williams1b40e092015-05-01 13:34:01 -04002428 return NULL;
2429}
2430
Dan Williams8a5f50d2016-09-22 15:42:59 -07002431static struct device **create_namespaces(struct nd_region *nd_region)
Dan Williamsae8219f2016-09-19 16:04:21 -07002432{
Colin Ian King59858d32018-01-30 17:47:07 +00002433 struct nd_mapping *nd_mapping;
Dan Williamsae8219f2016-09-19 16:04:21 -07002434 struct device **devs;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002435 int i;
Dan Williamsae8219f2016-09-19 16:04:21 -07002436
2437 if (nd_region->ndr_mappings == 0)
2438 return NULL;
2439
Dan Williams8a5f50d2016-09-22 15:42:59 -07002440 /* lock down all mappings while we scan labels */
2441 for (i = 0; i < nd_region->ndr_mappings; i++) {
2442 nd_mapping = &nd_region->mapping[i];
2443 mutex_lock_nested(&nd_mapping->lock, i);
2444 }
2445
2446 devs = scan_labels(nd_region);
2447
2448 for (i = 0; i < nd_region->ndr_mappings; i++) {
2449 int reverse = nd_region->ndr_mappings - 1 - i;
2450
2451 nd_mapping = &nd_region->mapping[reverse];
2452 mutex_unlock(&nd_mapping->lock);
2453 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002454
2455 return devs;
2456}
2457
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002458static int init_active_labels(struct nd_region *nd_region)
2459{
2460 int i;
2461
2462 for (i = 0; i < nd_region->ndr_mappings; i++) {
2463 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2464 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2465 struct nvdimm *nvdimm = nd_mapping->nvdimm;
Dan Williamsae8219f2016-09-19 16:04:21 -07002466 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002467 int count, j;
2468
2469 /*
Dan Williams9d62ed92017-05-04 11:47:22 -07002470 * If the dimm is disabled then we may need to prevent
2471 * the region from being activated.
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002472 */
2473 if (!ndd) {
Dan Williams9d62ed92017-05-04 11:47:22 -07002474 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2475 /* fail, label data may be unreadable */;
2476 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2477 /* fail, labels needed to disambiguate dpa */;
2478 else
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002479 return 0;
Dan Williams9d62ed92017-05-04 11:47:22 -07002480
2481 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2482 dev_name(&nd_mapping->nvdimm->dev),
2483 test_bit(NDD_LOCKED, &nvdimm->flags)
2484 ? "locked" : "disabled");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002485 return -ENXIO;
2486 }
2487 nd_mapping->ndd = ndd;
2488 atomic_inc(&nvdimm->busy);
2489 get_ndd(ndd);
2490
2491 count = nd_label_active_count(ndd);
Dan Williams426824d2018-03-05 16:39:31 -08002492 dev_dbg(ndd->dev, "count: %d\n", count);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002493 if (!count)
2494 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002495 for (j = 0; j < count; j++) {
2496 struct nd_namespace_label *label;
2497
Dan Williamsae8219f2016-09-19 16:04:21 -07002498 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2499 if (!label_ent)
2500 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002501 label = nd_label_active(ndd, j);
Dan Williamsd5d30d52019-02-02 16:35:26 -08002502 if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2503 u32 flags = __le32_to_cpu(label->flags);
2504
2505 flags &= ~NSLABEL_FLAG_LOCAL;
2506 label->flags = __cpu_to_le32(flags);
2507 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002508 label_ent->label = label;
2509
2510 mutex_lock(&nd_mapping->lock);
2511 list_add_tail(&label_ent->list, &nd_mapping->labels);
2512 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002513 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002514
2515 if (j >= count)
2516 continue;
2517
2518 mutex_lock(&nd_mapping->lock);
2519 nd_mapping_free_labels(nd_mapping);
2520 mutex_unlock(&nd_mapping->lock);
2521 return -ENOMEM;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002522 }
2523
2524 return 0;
2525}
2526
Dan Williams3d880022015-05-31 15:02:11 -04002527int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2528{
2529 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002530 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04002531
2532 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002533 nvdimm_bus_lock(&nd_region->dev);
2534 rc = init_active_labels(nd_region);
2535 if (rc) {
2536 nvdimm_bus_unlock(&nd_region->dev);
2537 return rc;
2538 }
2539
2540 type = nd_region_to_nstype(nd_region);
2541 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04002542 case ND_DEVICE_NAMESPACE_IO:
2543 devs = create_namespace_io(nd_region);
2544 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002545 case ND_DEVICE_NAMESPACE_PMEM:
Dan Williams1b40e092015-05-01 13:34:01 -04002546 case ND_DEVICE_NAMESPACE_BLK:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002547 devs = create_namespaces(nd_region);
Dan Williams1b40e092015-05-01 13:34:01 -04002548 break;
Dan Williams3d880022015-05-31 15:02:11 -04002549 default:
2550 break;
2551 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002552 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04002553
2554 if (!devs)
2555 return -ENODEV;
2556
Dan Williams3d880022015-05-31 15:02:11 -04002557 for (i = 0; devs[i]; i++) {
2558 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04002559 int id;
Dan Williams3d880022015-05-31 15:02:11 -04002560
Dan Williams1b40e092015-05-01 13:34:01 -04002561 if (type == ND_DEVICE_NAMESPACE_BLK) {
2562 struct nd_namespace_blk *nsblk;
2563
2564 nsblk = to_nd_namespace_blk(dev);
2565 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2566 GFP_KERNEL);
2567 nsblk->id = id;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002568 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2569 struct nd_namespace_pmem *nspm;
2570
2571 nspm = to_nd_namespace_pmem(dev);
2572 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2573 GFP_KERNEL);
2574 nspm->id = id;
Dan Williams1b40e092015-05-01 13:34:01 -04002575 } else
2576 id = i;
2577
2578 if (id < 0)
2579 break;
2580 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002581 dev->groups = nd_namespace_attribute_groups;
2582 nd_device_register(dev);
2583 }
Dan Williams1b40e092015-05-01 13:34:01 -04002584 if (i)
2585 nd_region->ns_seed = devs[0];
2586
2587 if (devs[i]) {
2588 int j;
2589
2590 for (j = i; devs[j]; j++) {
2591 struct device *dev = devs[j];
2592
2593 device_initialize(dev);
2594 put_device(dev);
2595 }
2596 *err = j - i;
2597 /*
2598 * All of the namespaces we tried to register failed, so
2599 * fail region activation.
2600 */
2601 if (*err == 0)
2602 rc = -ENODEV;
2603 }
Dan Williams3d880022015-05-31 15:02:11 -04002604 kfree(devs);
2605
Dan Williams1b40e092015-05-01 13:34:01 -04002606 if (rc == -ENODEV)
2607 return rc;
2608
Dan Williams3d880022015-05-31 15:02:11 -04002609 return i;
2610}