blob: 05d99a8b317516e8a0b73fd31e6aa28b805e4151 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams3d880022015-05-31 15:02:11 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams3d880022015-05-31 15:02:11 -04004 */
5#include <linux/module.h>
6#include <linux/device.h>
Dan Williams6ff3e912016-10-05 14:04:15 -07007#include <linux/sort.h>
Dan Williams3d880022015-05-31 15:02:11 -04008#include <linux/slab.h>
Dan Williamsae8219f2016-09-19 16:04:21 -07009#include <linux/list.h>
Dan Williams3d880022015-05-31 15:02:11 -040010#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040011#include "nd-core.h"
Dan Williamsca6a4652017-01-13 20:36:58 -080012#include "pmem.h"
Dan Williams3d880022015-05-31 15:02:11 -040013#include "nd.h"
14
15static void namespace_io_release(struct device *dev)
16{
17 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
18
19 kfree(nsio);
20}
21
Dan Williamsbf9bccc2015-06-17 17:14:46 -040022static void namespace_pmem_release(struct device *dev)
23{
24 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0e3b0d12016-10-06 23:13:15 -070025 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040026
Dan Williams0e3b0d12016-10-06 23:13:15 -070027 if (nspm->id >= 0)
28 ida_simple_remove(&nd_region->ns_ida, nspm->id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040029 kfree(nspm->alt_name);
30 kfree(nspm->uuid);
31 kfree(nspm);
32}
33
34static void namespace_blk_release(struct device *dev)
35{
Dan Williams1b40e092015-05-01 13:34:01 -040036 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
37 struct nd_region *nd_region = to_nd_region(dev->parent);
38
39 if (nsblk->id >= 0)
40 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
41 kfree(nsblk->alt_name);
42 kfree(nsblk->uuid);
43 kfree(nsblk->res);
44 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040045}
46
Dan Williams78c81cc2019-11-06 19:56:41 -080047static bool is_namespace_pmem(const struct device *dev);
48static bool is_namespace_blk(const struct device *dev);
49static bool is_namespace_io(const struct device *dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040050
Dan Williamse07ecd72016-01-05 18:37:23 -080051static int is_uuid_busy(struct device *dev, void *data)
52{
53 u8 *uuid1 = data, *uuid2 = NULL;
54
55 if (is_namespace_pmem(dev)) {
56 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
57
58 uuid2 = nspm->uuid;
59 } else if (is_namespace_blk(dev)) {
60 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
61
62 uuid2 = nsblk->uuid;
63 } else if (is_nd_btt(dev)) {
64 struct nd_btt *nd_btt = to_nd_btt(dev);
65
66 uuid2 = nd_btt->uuid;
67 } else if (is_nd_pfn(dev)) {
68 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
69
70 uuid2 = nd_pfn->uuid;
71 }
72
73 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
74 return -EBUSY;
75
76 return 0;
77}
78
79static int is_namespace_uuid_busy(struct device *dev, void *data)
80{
Dan Williamsc9e582a2017-05-29 23:12:19 -070081 if (is_nd_region(dev))
Dan Williamse07ecd72016-01-05 18:37:23 -080082 return device_for_each_child(dev, data, is_uuid_busy);
83 return 0;
84}
85
86/**
87 * nd_is_uuid_unique - verify that no other namespace has @uuid
88 * @dev: any device on a nvdimm_bus
89 * @uuid: uuid to check
90 */
91bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
92{
93 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
94
95 if (!nvdimm_bus)
96 return false;
97 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
98 if (device_for_each_child(&nvdimm_bus->dev, uuid,
99 is_namespace_uuid_busy) != 0)
100 return false;
101 return true;
102}
103
Dan Williams004f1af2015-08-24 19:20:23 -0400104bool pmem_should_map_pages(struct device *dev)
105{
106 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsfa7d2e62019-01-24 17:33:06 -0800107 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamscfe30b82016-03-03 09:38:00 -0800108 struct nd_namespace_io *nsio;
Dan Williams004f1af2015-08-24 19:20:23 -0400109
110 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
111 return false;
112
113 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
114 return false;
115
116 if (is_nd_pfn(dev) || is_nd_btt(dev))
117 return false;
118
Dan Williamsfa7d2e62019-01-24 17:33:06 -0800119 if (ndns->force_raw)
120 return false;
121
Dan Williamscfe30b82016-03-03 09:38:00 -0800122 nsio = to_nd_namespace_io(dev);
123 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
124 IORESOURCE_SYSTEM_RAM,
125 IORES_DESC_NONE) == REGION_MIXED)
126 return false;
127
Dan Williams004f1af2015-08-24 19:20:23 -0400128 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
Dan Williams004f1af2015-08-24 19:20:23 -0400129}
130EXPORT_SYMBOL(pmem_should_map_pages);
131
Dan Williamsf979b132017-06-04 12:12:07 +0900132unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
133{
134 if (is_namespace_pmem(&ndns->dev)) {
135 struct nd_namespace_pmem *nspm;
136
137 nspm = to_nd_namespace_pmem(&ndns->dev);
138 if (nspm->lbasize == 0 || nspm->lbasize == 512)
139 /* default */;
140 else if (nspm->lbasize == 4096)
141 return 4096;
142 else
143 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
144 nspm->lbasize);
145 }
146
147 /*
148 * There is no namespace label (is_namespace_io()), or the label
149 * indicates the default sector size.
150 */
151 return 512;
152}
153EXPORT_SYMBOL(pmem_sector_size);
154
Vishal Verma5212e112015-06-25 04:20:32 -0400155const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
156 char *name)
157{
158 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400159 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400160
Dan Williams0731de02015-12-14 15:34:15 -0800161 if (ndns->claim && is_nd_btt(ndns->claim))
162 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400163
Dan Williams004f1af2015-08-24 19:20:23 -0400164 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams01220732016-10-05 09:09:44 -0700165 int nsidx = 0;
166
167 if (is_namespace_pmem(&ndns->dev)) {
168 struct nd_namespace_pmem *nspm;
169
170 nspm = to_nd_namespace_pmem(&ndns->dev);
171 nsidx = nspm->id;
172 }
173
174 if (nsidx)
175 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
176 suffix ? suffix : "");
177 else
178 sprintf(name, "pmem%d%s", nd_region->id,
179 suffix ? suffix : "");
Dan Williams004f1af2015-08-24 19:20:23 -0400180 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400181 struct nd_namespace_blk *nsblk;
182
183 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400184 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
185 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400186 } else {
187 return NULL;
188 }
189
190 return name;
191}
192EXPORT_SYMBOL(nvdimm_namespace_disk_name);
193
Vishal Verma6ec68952015-07-29 14:58:09 -0600194const u8 *nd_dev_to_uuid(struct device *dev)
195{
196 static const u8 null_uuid[16];
197
198 if (!dev)
199 return null_uuid;
200
201 if (is_namespace_pmem(dev)) {
202 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
203
204 return nspm->uuid;
205 } else if (is_namespace_blk(dev)) {
206 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
207
208 return nsblk->uuid;
209 } else
210 return null_uuid;
211}
212EXPORT_SYMBOL(nd_dev_to_uuid);
213
Dan Williams3d880022015-05-31 15:02:11 -0400214static ssize_t nstype_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
216{
217 struct nd_region *nd_region = to_nd_region(dev->parent);
218
219 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
220}
221static DEVICE_ATTR_RO(nstype);
222
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400223static ssize_t __alt_name_store(struct device *dev, const char *buf,
224 const size_t len)
225{
226 char *input, *pos, *alt_name, **ns_altname;
227 ssize_t rc;
228
229 if (is_namespace_pmem(dev)) {
230 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
231
232 ns_altname = &nspm->alt_name;
233 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400234 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
235
236 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400237 } else
238 return -ENXIO;
239
Dan Williams8c2f7e82015-06-25 04:20:04 -0400240 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400241 return -EBUSY;
242
Andy Shevchenko3d9cbe32018-06-11 16:47:21 +0300243 input = kstrndup(buf, len, GFP_KERNEL);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400244 if (!input)
245 return -ENOMEM;
246
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400247 pos = strim(input);
248 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
249 rc = -EINVAL;
250 goto out;
251 }
252
253 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
254 if (!alt_name) {
255 rc = -ENOMEM;
256 goto out;
257 }
258 kfree(*ns_altname);
259 *ns_altname = alt_name;
260 sprintf(*ns_altname, "%s", pos);
261 rc = len;
262
263out:
264 kfree(input);
265 return rc;
266}
267
Dan Williams1b40e092015-05-01 13:34:01 -0400268static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
269{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400270 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400271 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
272 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
273 struct nd_label_id label_id;
274 resource_size_t size = 0;
275 struct resource *res;
276
277 if (!nsblk->uuid)
278 return 0;
279 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
280 for_each_dpa_resource(ndd, res)
281 if (strcmp(res->name, label_id.id) == 0)
282 size += resource_size(res);
283 return size;
284}
285
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400286static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
287{
288 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
289 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
290 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
291 struct nd_label_id label_id;
292 struct resource *res;
293 int count, i;
294
295 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
296 return false;
297
298 count = 0;
299 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
300 for_each_dpa_resource(ndd, res) {
301 if (strcmp(res->name, label_id.id) != 0)
302 continue;
303 /*
Geert Uytterhoevenae551e92016-08-31 11:45:25 +0200304 * Resources with unacknowledged adjustments indicate a
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400305 * failure to update labels
306 */
307 if (res->flags & DPA_RESOURCE_ADJUSTED)
308 return false;
309 count++;
310 }
311
312 /* These values match after a successful label update */
313 if (count != nsblk->num_resources)
314 return false;
315
316 for (i = 0; i < nsblk->num_resources; i++) {
317 struct resource *found = NULL;
318
319 for_each_dpa_resource(ndd, res)
320 if (res == nsblk->res[i]) {
321 found = res;
322 break;
323 }
324 /* stale resource */
325 if (!found)
326 return false;
327 }
328
329 return true;
330}
331
332resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
333{
334 resource_size_t size;
335
336 nvdimm_bus_lock(&nsblk->common.dev);
337 size = __nd_namespace_blk_validate(nsblk);
338 nvdimm_bus_unlock(&nsblk->common.dev);
339
340 return size;
341}
342EXPORT_SYMBOL(nd_namespace_blk_validate);
343
344
Dan Williamsf524bf22015-05-30 12:36:02 -0400345static int nd_namespace_label_update(struct nd_region *nd_region,
346 struct device *dev)
347{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400348 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400349 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400350 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400351 return 0;
352
353 /*
354 * Only allow label writes that will result in a valid namespace
355 * or deletion of an existing namespace.
356 */
357 if (is_namespace_pmem(dev)) {
358 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400359 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400360
361 if (size == 0 && nspm->uuid)
362 /* delete allocation */;
363 else if (!nspm->uuid)
364 return 0;
365
366 return nd_pmem_namespace_label_update(nd_region, nspm, size);
367 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400368 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
369 resource_size_t size = nd_namespace_blk_size(nsblk);
370
371 if (size == 0 && nsblk->uuid)
372 /* delete allocation */;
373 else if (!nsblk->uuid || !nsblk->lbasize)
374 return 0;
375
376 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400377 } else
378 return -ENXIO;
379}
380
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400381static ssize_t alt_name_store(struct device *dev,
382 struct device_attribute *attr, const char *buf, size_t len)
383{
Dan Williamsf524bf22015-05-30 12:36:02 -0400384 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400385 ssize_t rc;
386
Dan Williams87a30e12019-07-17 18:08:26 -0700387 nd_device_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400388 nvdimm_bus_lock(dev);
389 wait_nvdimm_bus_probe_idle(dev);
390 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400391 if (rc >= 0)
392 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -0800393 dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400394 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -0700395 nd_device_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400396
Dan Williamsf524bf22015-05-30 12:36:02 -0400397 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400398}
399
400static ssize_t alt_name_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
402{
403 char *ns_altname;
404
405 if (is_namespace_pmem(dev)) {
406 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
407
408 ns_altname = nspm->alt_name;
409 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400410 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
411
412 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400413 } else
414 return -ENXIO;
415
416 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
417}
418static DEVICE_ATTR_RW(alt_name);
419
420static int scan_free(struct nd_region *nd_region,
421 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
422 resource_size_t n)
423{
424 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
425 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
426 int rc = 0;
427
428 while (n) {
429 struct resource *res, *last;
430 resource_size_t new_start;
431
432 last = NULL;
433 for_each_dpa_resource(ndd, res)
434 if (strcmp(res->name, label_id->id) == 0)
435 last = res;
436 res = last;
437 if (!res)
438 return 0;
439
440 if (n >= resource_size(res)) {
441 n -= resource_size(res);
442 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
443 nvdimm_free_dpa(ndd, res);
444 /* retry with last resource deleted */
445 continue;
446 }
447
448 /*
449 * Keep BLK allocations relegated to high DPA as much as
450 * possible
451 */
452 if (is_blk)
453 new_start = res->start + n;
454 else
455 new_start = res->start;
456
457 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400458 if (rc == 0)
459 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400460 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
461 break;
462 }
463
464 return rc;
465}
466
467/**
468 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
469 * @nd_region: the set of dimms to reclaim @n bytes from
470 * @label_id: unique identifier for the namespace consuming this dpa range
471 * @n: number of bytes per-dimm to release
472 *
473 * Assumes resources are ordered. Starting from the end try to
474 * adjust_resource() the allocation to @n, but if @n is larger than the
475 * allocation delete it and find the 'new' last allocation in the label
476 * set.
477 */
478static int shrink_dpa_allocation(struct nd_region *nd_region,
479 struct nd_label_id *label_id, resource_size_t n)
480{
481 int i;
482
483 for (i = 0; i < nd_region->ndr_mappings; i++) {
484 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
485 int rc;
486
487 rc = scan_free(nd_region, nd_mapping, label_id, n);
488 if (rc)
489 return rc;
490 }
491
492 return 0;
493}
494
495static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
496 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
497 resource_size_t n)
498{
499 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
500 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
501 resource_size_t first_dpa;
502 struct resource *res;
503 int rc = 0;
504
505 /* allocate blk from highest dpa first */
506 if (is_blk)
507 first_dpa = nd_mapping->start + nd_mapping->size - n;
508 else
509 first_dpa = nd_mapping->start;
510
511 /* first resource allocation for this label-id or dimm */
512 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
513 if (!res)
514 rc = -EBUSY;
515
516 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
517 return rc ? n : 0;
518}
519
Dan Williams762d0672016-10-04 16:09:59 -0700520
521/**
522 * space_valid() - validate free dpa space against constraints
523 * @nd_region: hosting region of the free space
524 * @ndd: dimm device data for debug
525 * @label_id: namespace id to allocate space
526 * @prev: potential allocation that precedes free space
527 * @next: allocation that follows the given free space range
528 * @exist: first allocation with same id in the mapping
529 * @n: range that must satisfied for pmem allocations
530 * @valid: free space range to validate
531 *
532 * BLK-space is valid as long as it does not precede a PMEM
533 * allocation in a given region. PMEM-space must be contiguous
534 * and adjacent to an existing existing allocation (if one
535 * exists). If reserving PMEM any space is valid.
536 */
537static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
538 struct nd_label_id *label_id, struct resource *prev,
539 struct resource *next, struct resource *exist,
540 resource_size_t n, struct resource *valid)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400541{
Dan Williams762d0672016-10-04 16:09:59 -0700542 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
543 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
544
545 if (valid->start >= valid->end)
546 goto invalid;
547
548 if (is_reserve)
549 return;
550
551 if (!is_pmem) {
552 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
553 struct nvdimm_bus *nvdimm_bus;
554 struct blk_alloc_info info = {
555 .nd_mapping = nd_mapping,
556 .available = nd_mapping->size,
557 .res = valid,
558 };
559
560 WARN_ON(!is_nd_blk(&nd_region->dev));
561 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
562 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
563 return;
564 }
565
566 /* allocation needs to be contiguous, so this is all or nothing */
567 if (resource_size(valid) < n)
568 goto invalid;
569
570 /* we've got all the space we need and no existing allocation */
571 if (!exist)
572 return;
573
574 /* allocation needs to be contiguous with the existing namespace */
575 if (valid->start == exist->end + 1
576 || valid->end == exist->start - 1)
577 return;
578
579 invalid:
580 /* truncate @valid size to 0 */
581 valid->end = valid->start - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400582}
583
584enum alloc_loc {
585 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
586};
587
588static resource_size_t scan_allocate(struct nd_region *nd_region,
589 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
590 resource_size_t n)
591{
592 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
593 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
594 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams762d0672016-10-04 16:09:59 -0700595 struct resource *res, *exist = NULL, valid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400596 const resource_size_t to_allocate = n;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400597 int first;
598
Dan Williams762d0672016-10-04 16:09:59 -0700599 for_each_dpa_resource(ndd, res)
600 if (strcmp(label_id->id, res->name) == 0)
601 exist = res;
602
603 valid.start = nd_mapping->start;
604 valid.end = mapping_end;
605 valid.name = "free space";
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400606 retry:
607 first = 0;
608 for_each_dpa_resource(ndd, res) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400609 struct resource *next = res->sibling, *new_res = NULL;
Dan Williams762d0672016-10-04 16:09:59 -0700610 resource_size_t allocate, available = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400611 enum alloc_loc loc = ALLOC_ERR;
612 const char *action;
613 int rc = 0;
614
615 /* ignore resources outside this nd_mapping */
616 if (res->start > mapping_end)
617 continue;
618 if (res->end < nd_mapping->start)
619 continue;
620
621 /* space at the beginning of the mapping */
622 if (!first++ && res->start > nd_mapping->start) {
Dan Williams762d0672016-10-04 16:09:59 -0700623 valid.start = nd_mapping->start;
624 valid.end = res->start - 1;
625 space_valid(nd_region, ndd, label_id, NULL, next, exist,
626 to_allocate, &valid);
627 available = resource_size(&valid);
628 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400629 loc = ALLOC_BEFORE;
630 }
631
632 /* space between allocations */
633 if (!loc && next) {
Dan Williams762d0672016-10-04 16:09:59 -0700634 valid.start = res->start + resource_size(res);
635 valid.end = min(mapping_end, next->start - 1);
636 space_valid(nd_region, ndd, label_id, res, next, exist,
637 to_allocate, &valid);
638 available = resource_size(&valid);
639 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400640 loc = ALLOC_MID;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400641 }
642
643 /* space at the end of the mapping */
644 if (!loc && !next) {
Dan Williams762d0672016-10-04 16:09:59 -0700645 valid.start = res->start + resource_size(res);
646 valid.end = mapping_end;
647 space_valid(nd_region, ndd, label_id, res, next, exist,
648 to_allocate, &valid);
649 available = resource_size(&valid);
650 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400651 loc = ALLOC_AFTER;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400652 }
653
654 if (!loc || !available)
655 continue;
656 allocate = min(available, n);
657 switch (loc) {
658 case ALLOC_BEFORE:
659 if (strcmp(res->name, label_id->id) == 0) {
660 /* adjust current resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400661 rc = adjust_resource(res, res->start - allocate,
662 resource_size(res) + allocate);
663 action = "cur grow up";
664 } else
665 action = "allocate";
666 break;
667 case ALLOC_MID:
668 if (strcmp(next->name, label_id->id) == 0) {
669 /* adjust next resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400670 rc = adjust_resource(next, next->start
671 - allocate, resource_size(next)
672 + allocate);
673 new_res = next;
674 action = "next grow up";
675 } else if (strcmp(res->name, label_id->id) == 0) {
676 action = "grow down";
677 } else
678 action = "allocate";
679 break;
680 case ALLOC_AFTER:
681 if (strcmp(res->name, label_id->id) == 0)
682 action = "grow down";
683 else
684 action = "allocate";
685 break;
686 default:
687 return n;
688 }
689
690 if (strcmp(action, "allocate") == 0) {
691 /* BLK allocate bottom up */
692 if (!is_pmem)
Dan Williams762d0672016-10-04 16:09:59 -0700693 valid.start += available - allocate;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400694
695 new_res = nvdimm_allocate_dpa(ndd, label_id,
Dan Williams762d0672016-10-04 16:09:59 -0700696 valid.start, allocate);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400697 if (!new_res)
698 rc = -EBUSY;
699 } else if (strcmp(action, "grow down") == 0) {
700 /* adjust current resource down */
701 rc = adjust_resource(res, res->start, resource_size(res)
702 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400703 if (rc == 0)
704 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400705 }
706
707 if (!new_res)
708 new_res = res;
709
710 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
711 action, loc, rc);
712
713 if (rc)
714 return n;
715
716 n -= allocate;
717 if (n) {
718 /*
719 * Retry scan with newly inserted resources.
720 * For example, if we did an ALLOC_BEFORE
721 * insertion there may also have been space
722 * available for an ALLOC_AFTER insertion, so we
723 * need to check this same resource again
724 */
725 goto retry;
726 } else
727 return 0;
728 }
729
Dan Williams1b40e092015-05-01 13:34:01 -0400730 /*
731 * If we allocated nothing in the BLK case it may be because we are in
732 * an initial "pmem-reserve pass". Only do an initial BLK allocation
733 * when none of the DPA space is reserved.
734 */
735 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400736 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
737 return n;
738}
739
Dan Williams1b40e092015-05-01 13:34:01 -0400740static int merge_dpa(struct nd_region *nd_region,
741 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
742{
743 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
744 struct resource *res;
745
746 if (strncmp("pmem", label_id->id, 4) == 0)
747 return 0;
748 retry:
749 for_each_dpa_resource(ndd, res) {
750 int rc;
751 struct resource *next = res->sibling;
752 resource_size_t end = res->start + resource_size(res);
753
754 if (!next || strcmp(res->name, label_id->id) != 0
755 || strcmp(next->name, label_id->id) != 0
756 || end != next->start)
757 continue;
758 end += resource_size(next);
759 nvdimm_free_dpa(ndd, next);
760 rc = adjust_resource(res, res->start, end - res->start);
761 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
762 if (rc)
763 return rc;
764 res->flags |= DPA_RESOURCE_ADJUSTED;
765 goto retry;
766 }
767
768 return 0;
769}
770
Keith Busch12e31292018-07-24 15:07:57 -0600771int __reserve_free_pmem(struct device *dev, void *data)
Dan Williams1b40e092015-05-01 13:34:01 -0400772{
773 struct nvdimm *nvdimm = data;
774 struct nd_region *nd_region;
775 struct nd_label_id label_id;
776 int i;
777
Dan Williamsc9e582a2017-05-29 23:12:19 -0700778 if (!is_memory(dev))
Dan Williams1b40e092015-05-01 13:34:01 -0400779 return 0;
780
781 nd_region = to_nd_region(dev);
782 if (nd_region->ndr_mappings == 0)
783 return 0;
784
785 memset(&label_id, 0, sizeof(label_id));
786 strcat(label_id.id, "pmem-reserve");
787 for (i = 0; i < nd_region->ndr_mappings; i++) {
788 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
789 resource_size_t n, rem = 0;
790
791 if (nd_mapping->nvdimm != nvdimm)
792 continue;
793
794 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
795 if (n == 0)
796 return 0;
797 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
798 dev_WARN_ONCE(&nd_region->dev, rem,
799 "pmem reserve underrun: %#llx of %#llx bytes\n",
800 (unsigned long long) n - rem,
801 (unsigned long long) n);
802 return rem ? -ENXIO : 0;
803 }
804
805 return 0;
806}
807
Keith Busch12e31292018-07-24 15:07:57 -0600808void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
Dan Williams1b40e092015-05-01 13:34:01 -0400809 struct nd_mapping *nd_mapping)
810{
811 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
812 struct resource *res, *_res;
813
814 for_each_dpa_resource_safe(ndd, res, _res)
815 if (strcmp(res->name, "pmem-reserve") == 0)
816 nvdimm_free_dpa(ndd, res);
817}
818
819static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
820 struct nd_mapping *nd_mapping)
821{
822 struct nvdimm *nvdimm = nd_mapping->nvdimm;
823 int rc;
824
825 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
826 __reserve_free_pmem);
827 if (rc)
828 release_free_pmem(nvdimm_bus, nd_mapping);
829 return rc;
830}
831
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400832/**
833 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
834 * @nd_region: the set of dimms to allocate @n more bytes from
835 * @label_id: unique identifier for the namespace consuming this dpa range
836 * @n: number of bytes per-dimm to add to the existing allocation
837 *
838 * Assumes resources are ordered. For BLK regions, first consume
839 * BLK-only available DPA free space, then consume PMEM-aliased DPA
840 * space starting at the highest DPA. For PMEM regions start
841 * allocations from the start of an interleave set and end at the first
842 * BLK allocation or the end of the interleave set, whichever comes
843 * first.
844 */
845static int grow_dpa_allocation(struct nd_region *nd_region,
846 struct nd_label_id *label_id, resource_size_t n)
847{
Dan Williams1b40e092015-05-01 13:34:01 -0400848 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
849 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400850 int i;
851
852 for (i = 0; i < nd_region->ndr_mappings; i++) {
853 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400854 resource_size_t rem = n;
855 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400856
Dan Williams1b40e092015-05-01 13:34:01 -0400857 /*
858 * In the BLK case try once with all unallocated PMEM
859 * reserved, and once without
860 */
861 for (j = is_pmem; j < 2; j++) {
862 bool blk_only = j == 0;
863
864 if (blk_only) {
865 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
866 if (rc)
867 return rc;
868 }
869 rem = scan_allocate(nd_region, nd_mapping,
870 label_id, rem);
871 if (blk_only)
872 release_free_pmem(nvdimm_bus, nd_mapping);
873
874 /* try again and allow encroachments into PMEM */
875 if (rem == 0)
876 break;
877 }
878
879 dev_WARN_ONCE(&nd_region->dev, rem,
880 "allocation underrun: %#llx of %#llx bytes\n",
881 (unsigned long long) n - rem,
882 (unsigned long long) n);
883 if (rem)
884 return -ENXIO;
885
886 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400887 if (rc)
888 return rc;
889 }
890
891 return 0;
892}
893
Dan Williams0e3b0d12016-10-06 23:13:15 -0700894static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400895 struct nd_namespace_pmem *nspm, resource_size_t size)
896{
897 struct resource *res = &nspm->nsio.res;
Dan Williams0e3b0d12016-10-06 23:13:15 -0700898 resource_size_t offset = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400899
Dan Williams0e3b0d12016-10-06 23:13:15 -0700900 if (size && !nspm->uuid) {
901 WARN_ON_ONCE(1);
902 size = 0;
903 }
904
905 if (size && nspm->uuid) {
906 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
907 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
908 struct nd_label_id label_id;
909 struct resource *res;
910
911 if (!ndd) {
912 size = 0;
913 goto out;
914 }
915
916 nd_label_gen_id(&label_id, nspm->uuid, 0);
917
918 /* calculate a spa offset from the dpa allocation offset */
919 for_each_dpa_resource(ndd, res)
920 if (strcmp(res->name, label_id.id) == 0) {
921 offset = (res->start - nd_mapping->start)
922 * nd_region->ndr_mappings;
923 goto out;
924 }
925
926 WARN_ON_ONCE(1);
927 size = 0;
928 }
929
930 out:
931 res->start = nd_region->ndr_start + offset;
932 res->end = res->start + size - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400933}
934
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300935static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
936{
937 if (!uuid) {
938 dev_dbg(dev, "%s: uuid not set\n", where);
939 return true;
940 }
941 return false;
942}
943
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400944static ssize_t __size_store(struct device *dev, unsigned long long val)
945{
946 resource_size_t allocated = 0, available = 0;
947 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams1f19b982017-01-09 17:30:49 -0800948 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400949 struct nd_mapping *nd_mapping;
950 struct nvdimm_drvdata *ndd;
951 struct nd_label_id label_id;
952 u32 flags = 0, remainder;
Dan Williams9d032f42017-01-25 00:54:07 +0530953 int rc, i, id = -1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400954 u8 *uuid = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400955
Dan Williams1f19b982017-01-09 17:30:49 -0800956 if (dev->driver || ndns->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400957 return -EBUSY;
958
959 if (is_namespace_pmem(dev)) {
960 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
961
962 uuid = nspm->uuid;
Dan Williams9d032f42017-01-25 00:54:07 +0530963 id = nspm->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400964 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400965 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
966
967 uuid = nsblk->uuid;
968 flags = NSLABEL_FLAG_LOCAL;
Dan Williams9d032f42017-01-25 00:54:07 +0530969 id = nsblk->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400970 }
971
972 /*
973 * We need a uuid for the allocation-label and dimm(s) on which
974 * to store the label.
975 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300976 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400977 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300978 if (nd_region->ndr_mappings == 0) {
Dan Williams426824d2018-03-05 16:39:31 -0800979 dev_dbg(dev, "not associated with dimm(s)\n");
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300980 return -ENXIO;
981 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400982
Aneesh Kumar K.V5b26db92019-09-05 21:16:02 +0530983 div_u64_rem(val, PAGE_SIZE * nd_region->ndr_mappings, &remainder);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400984 if (remainder) {
Aneesh Kumar K.V5b26db92019-09-05 21:16:02 +0530985 dev_dbg(dev, "%llu is not %ldK aligned\n", val,
986 (PAGE_SIZE * nd_region->ndr_mappings) / SZ_1K);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400987 return -EINVAL;
988 }
989
990 nd_label_gen_id(&label_id, uuid, flags);
991 for (i = 0; i < nd_region->ndr_mappings; i++) {
992 nd_mapping = &nd_region->mapping[i];
993 ndd = to_ndd(nd_mapping);
994
995 /*
996 * All dimms in an interleave set, or the base dimm for a blk
997 * region, need to be enabled for the size to be changed.
998 */
999 if (!ndd)
1000 return -ENXIO;
1001
1002 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1003 }
Keith Busch12e31292018-07-24 15:07:57 -06001004 available = nd_region_allocatable_dpa(nd_region);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001005
1006 if (val > available + allocated)
1007 return -ENOSPC;
1008
1009 if (val == allocated)
1010 return 0;
1011
1012 val = div_u64(val, nd_region->ndr_mappings);
1013 allocated = div_u64(allocated, nd_region->ndr_mappings);
1014 if (val < allocated)
1015 rc = shrink_dpa_allocation(nd_region, &label_id,
1016 allocated - val);
1017 else
1018 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1019
1020 if (rc)
1021 return rc;
1022
1023 if (is_namespace_pmem(dev)) {
1024 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1025
Dan Williams0e3b0d12016-10-06 23:13:15 -07001026 nd_namespace_pmem_set_resource(nd_region, nspm,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001027 val * nd_region->ndr_mappings);
1028 }
1029
Dan Williams1f19b982017-01-09 17:30:49 -08001030 /*
1031 * Try to delete the namespace if we deleted all of its
Dan Williams9d032f42017-01-25 00:54:07 +05301032 * allocation, this is not the seed or 0th device for the
1033 * region, and it is not actively claimed by a btt, pfn, or dax
1034 * instance.
Dan Williams1f19b982017-01-09 17:30:49 -08001035 */
Dan Williams9d032f42017-01-25 00:54:07 +05301036 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
Dan Williams1f19b982017-01-09 17:30:49 -08001037 nd_device_unregister(dev, ND_ASYNC);
1038
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001039 return rc;
1040}
1041
1042static ssize_t size_store(struct device *dev,
1043 struct device_attribute *attr, const char *buf, size_t len)
1044{
Dan Williamsf524bf22015-05-30 12:36:02 -04001045 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001046 unsigned long long val;
1047 u8 **uuid = NULL;
1048 int rc;
1049
1050 rc = kstrtoull(buf, 0, &val);
1051 if (rc)
1052 return rc;
1053
Dan Williams87a30e12019-07-17 18:08:26 -07001054 nd_device_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001055 nvdimm_bus_lock(dev);
1056 wait_nvdimm_bus_probe_idle(dev);
1057 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -04001058 if (rc >= 0)
1059 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001060
1061 if (is_namespace_pmem(dev)) {
1062 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1063
1064 uuid = &nspm->uuid;
1065 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001066 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1067
1068 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001069 }
1070
1071 if (rc == 0 && val == 0 && uuid) {
1072 /* setting size zero == 'delete namespace' */
1073 kfree(*uuid);
1074 *uuid = NULL;
1075 }
1076
Dan Williams426824d2018-03-05 16:39:31 -08001077 dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001078
1079 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001080 nd_device_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001081
Dan Williamsf524bf22015-05-30 12:36:02 -04001082 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001083}
1084
Dan Williams8c2f7e82015-06-25 04:20:04 -04001085resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001086{
Dan Williams8c2f7e82015-06-25 04:20:04 -04001087 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001088
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001089 if (is_namespace_pmem(dev)) {
1090 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1091
Dan Williams8c2f7e82015-06-25 04:20:04 -04001092 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001093 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -04001094 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001095 } else if (is_namespace_io(dev)) {
1096 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1097
Dan Williams8c2f7e82015-06-25 04:20:04 -04001098 return resource_size(&nsio->res);
1099 } else
1100 WARN_ONCE(1, "unknown namespace type\n");
1101 return 0;
1102}
Dan Williams1b40e092015-05-01 13:34:01 -04001103
Dan Williams8c2f7e82015-06-25 04:20:04 -04001104resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1105{
1106 resource_size_t size;
1107
1108 nvdimm_bus_lock(&ndns->dev);
1109 size = __nvdimm_namespace_capacity(ndns);
1110 nvdimm_bus_unlock(&ndns->dev);
1111
1112 return size;
1113}
1114EXPORT_SYMBOL(nvdimm_namespace_capacity);
1115
Dan Williams08e6b3c2018-06-13 09:08:36 -07001116bool nvdimm_namespace_locked(struct nd_namespace_common *ndns)
1117{
1118 int i;
1119 bool locked = false;
1120 struct device *dev = &ndns->dev;
1121 struct nd_region *nd_region = to_nd_region(dev->parent);
1122
1123 for (i = 0; i < nd_region->ndr_mappings; i++) {
1124 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1125 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1126
1127 if (test_bit(NDD_LOCKED, &nvdimm->flags)) {
1128 dev_dbg(dev, "%s locked\n", nvdimm_name(nvdimm));
1129 locked = true;
1130 }
1131 }
1132 return locked;
1133}
1134EXPORT_SYMBOL(nvdimm_namespace_locked);
1135
Dan Williams8c2f7e82015-06-25 04:20:04 -04001136static ssize_t size_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1138{
1139 return sprintf(buf, "%llu\n", (unsigned long long)
1140 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001141}
Fabian Frederickb44fe762016-12-04 10:54:08 -08001142static DEVICE_ATTR(size, 0444, size_show, size_store);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001143
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001144static u8 *namespace_to_uuid(struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001145{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001146 if (is_namespace_pmem(dev)) {
1147 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1148
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001149 return nspm->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001150 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001151 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1152
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001153 return nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001154 } else
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001155 return ERR_PTR(-ENXIO);
1156}
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001157
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001158static ssize_t uuid_show(struct device *dev,
1159 struct device_attribute *attr, char *buf)
1160{
1161 u8 *uuid = namespace_to_uuid(dev);
1162
1163 if (IS_ERR(uuid))
1164 return PTR_ERR(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001165 if (uuid)
1166 return sprintf(buf, "%pUb\n", uuid);
1167 return sprintf(buf, "\n");
1168}
1169
1170/**
1171 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1172 * @nd_region: parent region so we can updates all dimms in the set
1173 * @dev: namespace type for generating label_id
1174 * @new_uuid: incoming uuid
1175 * @old_uuid: reference to the uuid storage location in the namespace object
1176 */
1177static int namespace_update_uuid(struct nd_region *nd_region,
1178 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1179{
1180 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1181 struct nd_label_id old_label_id;
1182 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001183 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001184
Dan Williamsf524bf22015-05-30 12:36:02 -04001185 if (!nd_is_uuid_unique(dev, new_uuid))
1186 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001187
1188 if (*old_uuid == NULL)
1189 goto out;
1190
Dan Williamsf524bf22015-05-30 12:36:02 -04001191 /*
1192 * If we've already written a label with this uuid, then it's
1193 * too late to rename because we can't reliably update the uuid
1194 * without losing the old namespace. Userspace must delete this
1195 * namespace to abandon the old uuid.
1196 */
1197 for (i = 0; i < nd_region->ndr_mappings; i++) {
1198 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1199
1200 /*
1201 * This check by itself is sufficient because old_uuid
1202 * would be NULL above if this uuid did not exist in the
1203 * currently written set.
1204 *
1205 * FIXME: can we delete uuid with zero dpa allocated?
1206 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001207 if (list_empty(&nd_mapping->labels))
Dan Williamsf524bf22015-05-30 12:36:02 -04001208 return -EBUSY;
1209 }
1210
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001211 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1212 nd_label_gen_id(&new_label_id, new_uuid, flags);
1213 for (i = 0; i < nd_region->ndr_mappings; i++) {
1214 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1215 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsc4703ce2019-04-30 21:51:21 -07001216 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001217 struct resource *res;
1218
1219 for_each_dpa_resource(ndd, res)
1220 if (strcmp(res->name, old_label_id.id) == 0)
1221 sprintf((void *) res->name, "%s",
1222 new_label_id.id);
Dan Williamsc4703ce2019-04-30 21:51:21 -07001223
1224 mutex_lock(&nd_mapping->lock);
1225 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1226 struct nd_namespace_label *nd_label = label_ent->label;
1227 struct nd_label_id label_id;
1228
1229 if (!nd_label)
1230 continue;
1231 nd_label_gen_id(&label_id, nd_label->uuid,
1232 __le32_to_cpu(nd_label->flags));
1233 if (strcmp(old_label_id.id, label_id.id) == 0)
1234 set_bit(ND_LABEL_REAP, &label_ent->flags);
1235 }
1236 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001237 }
1238 kfree(*old_uuid);
1239 out:
1240 *old_uuid = new_uuid;
1241 return 0;
1242}
1243
1244static ssize_t uuid_store(struct device *dev,
1245 struct device_attribute *attr, const char *buf, size_t len)
1246{
1247 struct nd_region *nd_region = to_nd_region(dev->parent);
1248 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001249 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001250 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001251
1252 if (is_namespace_pmem(dev)) {
1253 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1254
1255 ns_uuid = &nspm->uuid;
1256 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001257 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1258
1259 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001260 } else
1261 return -ENXIO;
1262
Dan Williams87a30e12019-07-17 18:08:26 -07001263 nd_device_lock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001264 nvdimm_bus_lock(dev);
1265 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001266 if (to_ndns(dev)->claim)
1267 rc = -EBUSY;
1268 if (rc >= 0)
1269 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001270 if (rc >= 0)
1271 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001272 if (rc >= 0)
1273 rc = nd_namespace_label_update(nd_region, dev);
1274 else
1275 kfree(uuid);
Dan Williams426824d2018-03-05 16:39:31 -08001276 dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
1277 buf[len - 1] == '\n' ? "" : "\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001278 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001279 nd_device_unlock(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001280
Dan Williamsf524bf22015-05-30 12:36:02 -04001281 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001282}
1283static DEVICE_ATTR_RW(uuid);
1284
1285static ssize_t resource_show(struct device *dev,
1286 struct device_attribute *attr, char *buf)
1287{
1288 struct resource *res;
1289
1290 if (is_namespace_pmem(dev)) {
1291 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1292
1293 res = &nspm->nsio.res;
1294 } else if (is_namespace_io(dev)) {
1295 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1296
1297 res = &nsio->res;
1298 } else
1299 return -ENXIO;
1300
1301 /* no address to convey if the namespace has no allocation */
1302 if (resource_size(res) == 0)
1303 return -ENXIO;
1304 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1305}
1306static DEVICE_ATTR_RO(resource);
1307
Dan Williamsf979b132017-06-04 12:12:07 +09001308static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
Vishal Vermafcae6952015-06-25 04:22:39 -04001309 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001310
Dan Williamsf979b132017-06-04 12:12:07 +09001311static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1312
Dan Williams1b40e092015-05-01 13:34:01 -04001313static ssize_t sector_size_show(struct device *dev,
1314 struct device_attribute *attr, char *buf)
1315{
Dan Williamsf979b132017-06-04 12:12:07 +09001316 if (is_namespace_blk(dev)) {
1317 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001318
Dan Williamsb2c48f92017-08-11 17:36:54 -07001319 return nd_size_select_show(nsblk->lbasize,
Dan Williamsf979b132017-06-04 12:12:07 +09001320 blk_lbasize_supported, buf);
1321 }
Dan Williams1b40e092015-05-01 13:34:01 -04001322
Dan Williamsf979b132017-06-04 12:12:07 +09001323 if (is_namespace_pmem(dev)) {
1324 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1325
Dan Williamsb2c48f92017-08-11 17:36:54 -07001326 return nd_size_select_show(nspm->lbasize,
Dan Williamsf979b132017-06-04 12:12:07 +09001327 pmem_lbasize_supported, buf);
1328 }
1329 return -ENXIO;
Dan Williams1b40e092015-05-01 13:34:01 -04001330}
1331
1332static ssize_t sector_size_store(struct device *dev,
1333 struct device_attribute *attr, const char *buf, size_t len)
1334{
Dan Williamsf524bf22015-05-30 12:36:02 -04001335 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsf979b132017-06-04 12:12:07 +09001336 const unsigned long *supported;
1337 unsigned long *lbasize;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001338 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001339
Dan Williamsf979b132017-06-04 12:12:07 +09001340 if (is_namespace_blk(dev)) {
1341 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1342
1343 lbasize = &nsblk->lbasize;
1344 supported = blk_lbasize_supported;
1345 } else if (is_namespace_pmem(dev)) {
1346 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1347
1348 lbasize = &nspm->lbasize;
1349 supported = pmem_lbasize_supported;
1350 } else
Dan Williams1b40e092015-05-01 13:34:01 -04001351 return -ENXIO;
1352
Dan Williams87a30e12019-07-17 18:08:26 -07001353 nd_device_lock(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001354 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001355 if (to_ndns(dev)->claim)
1356 rc = -EBUSY;
1357 if (rc >= 0)
Dan Williamsb2c48f92017-08-11 17:36:54 -07001358 rc = nd_size_select_store(dev, buf, lbasize, supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001359 if (rc >= 0)
1360 rc = nd_namespace_label_update(nd_region, dev);
Dan Williams426824d2018-03-05 16:39:31 -08001361 dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
1362 buf, buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001363 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001364 nd_device_unlock(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001365
1366 return rc ? rc : len;
1367}
1368static DEVICE_ATTR_RW(sector_size);
1369
Dan Williams0ba1c632015-05-30 12:35:36 -04001370static ssize_t dpa_extents_show(struct device *dev,
1371 struct device_attribute *attr, char *buf)
1372{
1373 struct nd_region *nd_region = to_nd_region(dev->parent);
1374 struct nd_label_id label_id;
1375 int count = 0, i;
1376 u8 *uuid = NULL;
1377 u32 flags = 0;
1378
1379 nvdimm_bus_lock(dev);
1380 if (is_namespace_pmem(dev)) {
1381 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1382
1383 uuid = nspm->uuid;
1384 flags = 0;
1385 } else if (is_namespace_blk(dev)) {
1386 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1387
1388 uuid = nsblk->uuid;
1389 flags = NSLABEL_FLAG_LOCAL;
1390 }
1391
1392 if (!uuid)
1393 goto out;
1394
1395 nd_label_gen_id(&label_id, uuid, flags);
1396 for (i = 0; i < nd_region->ndr_mappings; i++) {
1397 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1398 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1399 struct resource *res;
1400
1401 for_each_dpa_resource(ndd, res)
1402 if (strcmp(res->name, label_id.id) == 0)
1403 count++;
1404 }
1405 out:
1406 nvdimm_bus_unlock(dev);
1407
1408 return sprintf(buf, "%d\n", count);
1409}
1410static DEVICE_ATTR_RO(dpa_extents);
1411
Vishal Verma14e49452017-06-28 14:25:00 -06001412static int btt_claim_class(struct device *dev)
1413{
1414 struct nd_region *nd_region = to_nd_region(dev->parent);
1415 int i, loop_bitmask = 0;
1416
1417 for (i = 0; i < nd_region->ndr_mappings; i++) {
1418 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1419 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1420 struct nd_namespace_index *nsindex;
1421
Dan Williams33a56082017-09-18 14:48:58 -07001422 /*
1423 * If any of the DIMMs do not support labels the only
1424 * possible BTT format is v1.
1425 */
1426 if (!ndd) {
1427 loop_bitmask = 0;
1428 break;
1429 }
1430
Vishal Verma14e49452017-06-28 14:25:00 -06001431 nsindex = to_namespace_index(ndd, ndd->ns_current);
1432 if (nsindex == NULL)
1433 loop_bitmask |= 1;
1434 else {
1435 /* check whether existing labels are v1.1 or v1.2 */
1436 if (__le16_to_cpu(nsindex->major) == 1
1437 && __le16_to_cpu(nsindex->minor) == 1)
1438 loop_bitmask |= 2;
1439 else
1440 loop_bitmask |= 4;
1441 }
1442 }
1443 /*
1444 * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
1445 * block is found, a v1.1 label for any mapping will set bit 1, and a
1446 * v1.2 label will set bit 2.
1447 *
1448 * At the end of the loop, at most one of the three bits must be set.
1449 * If multiple bits were set, it means the different mappings disagree
1450 * about their labels, and this must be cleaned up first.
1451 *
1452 * If all the label index blocks are found to agree, nsindex of NULL
1453 * implies labels haven't been initialized yet, and when they will,
1454 * they will be of the 1.2 format, so we can assume BTT2.0
1455 *
1456 * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
1457 * found, we enforce BTT2.0
1458 *
1459 * If the loop was never entered, default to BTT1.1 (legacy namespaces)
1460 */
1461 switch (loop_bitmask) {
1462 case 0:
1463 case 2:
1464 return NVDIMM_CCLASS_BTT;
1465 case 1:
1466 case 4:
1467 return NVDIMM_CCLASS_BTT2;
1468 default:
1469 return -ENXIO;
1470 }
1471}
1472
Dan Williams8c2f7e82015-06-25 04:20:04 -04001473static ssize_t holder_show(struct device *dev,
1474 struct device_attribute *attr, char *buf)
1475{
1476 struct nd_namespace_common *ndns = to_ndns(dev);
1477 ssize_t rc;
1478
Dan Williams87a30e12019-07-17 18:08:26 -07001479 nd_device_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001480 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
Dan Williams87a30e12019-07-17 18:08:26 -07001481 nd_device_unlock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001482
1483 return rc;
1484}
1485static DEVICE_ATTR_RO(holder);
1486
Ira Weinyab84b772019-11-14 19:06:34 -08001487static int __holder_class_store(struct device *dev, const char *buf)
Dan Williamsb3fde742017-06-04 10:18:39 +09001488{
1489 struct nd_namespace_common *ndns = to_ndns(dev);
1490
1491 if (dev->driver || ndns->claim)
1492 return -EBUSY;
1493
Ira Weinyab84b772019-11-14 19:06:34 -08001494 if (sysfs_streq(buf, "btt")) {
1495 int rc = btt_claim_class(dev);
1496
1497 if (rc < NVDIMM_CCLASS_NONE)
1498 return rc;
1499 ndns->claim_class = rc;
1500 } else if (sysfs_streq(buf, "pfn"))
Dan Williamsb3fde742017-06-04 10:18:39 +09001501 ndns->claim_class = NVDIMM_CCLASS_PFN;
Dan Williams075c3fd2019-03-04 12:14:04 -08001502 else if (sysfs_streq(buf, "dax"))
Dan Williamsb3fde742017-06-04 10:18:39 +09001503 ndns->claim_class = NVDIMM_CCLASS_DAX;
Dan Williams075c3fd2019-03-04 12:14:04 -08001504 else if (sysfs_streq(buf, ""))
Dan Williamsb3fde742017-06-04 10:18:39 +09001505 ndns->claim_class = NVDIMM_CCLASS_NONE;
1506 else
1507 return -EINVAL;
1508
1509 return 0;
1510}
1511
1512static ssize_t holder_class_store(struct device *dev,
1513 struct device_attribute *attr, const char *buf, size_t len)
1514{
1515 struct nd_region *nd_region = to_nd_region(dev->parent);
Ira Weinyab84b772019-11-14 19:06:34 -08001516 int rc;
Dan Williamsb3fde742017-06-04 10:18:39 +09001517
Dan Williams87a30e12019-07-17 18:08:26 -07001518 nd_device_lock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001519 nvdimm_bus_lock(dev);
1520 wait_nvdimm_bus_probe_idle(dev);
1521 rc = __holder_class_store(dev, buf);
1522 if (rc >= 0)
1523 rc = nd_namespace_label_update(nd_region, dev);
Ira Weinyab84b772019-11-14 19:06:34 -08001524 dev_dbg(dev, "%s(%d)\n", rc < 0 ? "fail " : "", rc);
Dan Williamsb3fde742017-06-04 10:18:39 +09001525 nvdimm_bus_unlock(dev);
Dan Williams87a30e12019-07-17 18:08:26 -07001526 nd_device_unlock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001527
1528 return rc < 0 ? rc : len;
1529}
1530
1531static ssize_t holder_class_show(struct device *dev,
1532 struct device_attribute *attr, char *buf)
1533{
1534 struct nd_namespace_common *ndns = to_ndns(dev);
1535 ssize_t rc;
1536
Dan Williams87a30e12019-07-17 18:08:26 -07001537 nd_device_lock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001538 if (ndns->claim_class == NVDIMM_CCLASS_NONE)
1539 rc = sprintf(buf, "\n");
Vishal Verma14e49452017-06-28 14:25:00 -06001540 else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
1541 (ndns->claim_class == NVDIMM_CCLASS_BTT2))
Dan Williamsb3fde742017-06-04 10:18:39 +09001542 rc = sprintf(buf, "btt\n");
1543 else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
1544 rc = sprintf(buf, "pfn\n");
1545 else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
1546 rc = sprintf(buf, "dax\n");
1547 else
1548 rc = sprintf(buf, "<unknown>\n");
Dan Williams87a30e12019-07-17 18:08:26 -07001549 nd_device_unlock(dev);
Dan Williamsb3fde742017-06-04 10:18:39 +09001550
1551 return rc;
1552}
1553static DEVICE_ATTR_RW(holder_class);
1554
Dan Williams0731de02015-12-14 15:34:15 -08001555static ssize_t mode_show(struct device *dev,
1556 struct device_attribute *attr, char *buf)
1557{
1558 struct nd_namespace_common *ndns = to_ndns(dev);
1559 struct device *claim;
1560 char *mode;
1561 ssize_t rc;
1562
Dan Williams87a30e12019-07-17 18:08:26 -07001563 nd_device_lock(dev);
Dan Williams0731de02015-12-14 15:34:15 -08001564 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001565 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001566 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001567 else if (claim && is_nd_pfn(claim))
1568 mode = "memory";
Dan Williamscd034122016-03-11 10:15:36 -08001569 else if (claim && is_nd_dax(claim))
1570 mode = "dax";
Dan Williams9c412422016-01-23 15:34:10 -08001571 else if (!claim && pmem_should_map_pages(dev))
1572 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001573 else
1574 mode = "raw";
1575 rc = sprintf(buf, "%s\n", mode);
Dan Williams87a30e12019-07-17 18:08:26 -07001576 nd_device_unlock(dev);
Dan Williams0731de02015-12-14 15:34:15 -08001577
1578 return rc;
1579}
1580static DEVICE_ATTR_RO(mode);
1581
Dan Williams8c2f7e82015-06-25 04:20:04 -04001582static ssize_t force_raw_store(struct device *dev,
1583 struct device_attribute *attr, const char *buf, size_t len)
1584{
1585 bool force_raw;
1586 int rc = strtobool(buf, &force_raw);
1587
1588 if (rc)
1589 return rc;
1590
1591 to_ndns(dev)->force_raw = force_raw;
1592 return len;
1593}
1594
1595static ssize_t force_raw_show(struct device *dev,
1596 struct device_attribute *attr, char *buf)
1597{
1598 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1599}
1600static DEVICE_ATTR_RW(force_raw);
1601
Dan Williams3d880022015-05-31 15:02:11 -04001602static struct attribute *nd_namespace_attributes[] = {
1603 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001604 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001605 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001606 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001607 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001608 &dev_attr_resource.attr,
1609 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001610 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001611 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001612 &dev_attr_dpa_extents.attr,
Dan Williamsb3fde742017-06-04 10:18:39 +09001613 &dev_attr_holder_class.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001614 NULL,
1615};
1616
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001617static umode_t namespace_visible(struct kobject *kobj,
1618 struct attribute *a, int n)
1619{
1620 struct device *dev = container_of(kobj, struct device, kobj);
1621
1622 if (a == &dev_attr_resource.attr) {
1623 if (is_namespace_blk(dev))
1624 return 0;
Dan Williamsc1fb3542017-09-26 11:21:24 -07001625 return 0400;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001626 }
1627
1628 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1629 if (a == &dev_attr_size.attr)
Fabian Frederickb44fe762016-12-04 10:54:08 -08001630 return 0644;
Dan Williams1b40e092015-05-01 13:34:01 -04001631
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001632 return a->mode;
1633 }
1634
Dan Williams8c2f7e82015-06-25 04:20:04 -04001635 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1636 || a == &dev_attr_holder.attr
Dan Williamsb3fde742017-06-04 10:18:39 +09001637 || a == &dev_attr_holder_class.attr
Dan Williams0731de02015-12-14 15:34:15 -08001638 || a == &dev_attr_force_raw.attr
1639 || a == &dev_attr_mode.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001640 return a->mode;
1641
1642 return 0;
1643}
1644
Dan Williams3d880022015-05-31 15:02:11 -04001645static struct attribute_group nd_namespace_attribute_group = {
1646 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001647 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001648};
1649
1650static const struct attribute_group *nd_namespace_attribute_groups[] = {
1651 &nd_device_attribute_group,
1652 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001653 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001654 NULL,
1655};
1656
Dan Williams78c81cc2019-11-06 19:56:41 -08001657static const struct device_type namespace_io_device_type = {
1658 .name = "nd_namespace_io",
1659 .release = namespace_io_release,
1660 .groups = nd_namespace_attribute_groups,
1661};
1662
1663static const struct device_type namespace_pmem_device_type = {
1664 .name = "nd_namespace_pmem",
1665 .release = namespace_pmem_release,
1666 .groups = nd_namespace_attribute_groups,
1667};
1668
1669static const struct device_type namespace_blk_device_type = {
1670 .name = "nd_namespace_blk",
1671 .release = namespace_blk_release,
1672 .groups = nd_namespace_attribute_groups,
1673};
1674
1675static bool is_namespace_pmem(const struct device *dev)
1676{
1677 return dev ? dev->type == &namespace_pmem_device_type : false;
1678}
1679
1680static bool is_namespace_blk(const struct device *dev)
1681{
1682 return dev ? dev->type == &namespace_blk_device_type : false;
1683}
1684
1685static bool is_namespace_io(const struct device *dev)
1686{
1687 return dev ? dev->type == &namespace_io_device_type : false;
1688}
1689
Dan Williams8c2f7e82015-06-25 04:20:04 -04001690struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1691{
1692 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001693 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williamscd034122016-03-11 10:15:36 -08001694 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001695 struct nd_namespace_common *ndns = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001696 resource_size_t size;
1697
Dan Williamscd034122016-03-11 10:15:36 -08001698 if (nd_btt || nd_pfn || nd_dax) {
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001699 if (nd_btt)
Dan Williamse1455742015-07-30 17:57:47 -04001700 ndns = nd_btt->ndns;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001701 else if (nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -04001702 ndns = nd_pfn->ndns;
Dan Williamscd034122016-03-11 10:15:36 -08001703 else if (nd_dax)
1704 ndns = nd_dax->nd_pfn.ndns;
Dan Williamse1455742015-07-30 17:57:47 -04001705
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001706 if (!ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001707 return ERR_PTR(-ENODEV);
1708
1709 /*
1710 * Flush any in-progess probes / removals in the driver
1711 * for the raw personality of this namespace.
1712 */
Dan Williams87a30e12019-07-17 18:08:26 -07001713 nd_device_lock(&ndns->dev);
1714 nd_device_unlock(&ndns->dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001715 if (ndns->dev.driver) {
1716 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001717 dev_name(dev));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001718 return ERR_PTR(-EBUSY);
1719 }
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001720 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001721 "host (%s) vs claim (%s) mismatch\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001722 dev_name(dev),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001723 dev_name(ndns->claim)))
1724 return ERR_PTR(-ENXIO);
1725 } else {
1726 ndns = to_ndns(dev);
1727 if (ndns->claim) {
1728 dev_dbg(dev, "claimed by %s, failing probe\n",
1729 dev_name(ndns->claim));
1730
1731 return ERR_PTR(-ENXIO);
1732 }
1733 }
1734
Dan Williams08e6b3c2018-06-13 09:08:36 -07001735 if (nvdimm_namespace_locked(ndns))
1736 return ERR_PTR(-EACCES);
1737
Dan Williams8c2f7e82015-06-25 04:20:04 -04001738 size = nvdimm_namespace_capacity(ndns);
1739 if (size < ND_MIN_NAMESPACE_SIZE) {
1740 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1741 &size, ND_MIN_NAMESPACE_SIZE);
1742 return ERR_PTR(-ENODEV);
1743 }
1744
1745 if (is_namespace_pmem(&ndns->dev)) {
1746 struct nd_namespace_pmem *nspm;
1747
1748 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001749 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001750 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001751 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001752 struct nd_namespace_blk *nsblk;
1753
1754 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001755 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1756 return ERR_PTR(-ENODEV);
1757 if (!nsblk->lbasize) {
Dan Williams426824d2018-03-05 16:39:31 -08001758 dev_dbg(&ndns->dev, "sector size not set\n");
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001759 return ERR_PTR(-ENODEV);
1760 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001761 if (!nd_namespace_blk_validate(nsblk))
1762 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001763 }
1764
1765 return ndns;
1766}
1767EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1768
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +05301769int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
1770 resource_size_t size)
1771{
1772 if (is_namespace_blk(&ndns->dev))
1773 return 0;
1774 return devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev), size);
1775}
1776EXPORT_SYMBOL_GPL(devm_namespace_enable);
1777
1778void devm_namespace_disable(struct device *dev, struct nd_namespace_common *ndns)
1779{
1780 if (is_namespace_blk(&ndns->dev))
1781 return;
1782 devm_nsio_disable(dev, to_nd_namespace_io(&ndns->dev));
1783}
1784EXPORT_SYMBOL_GPL(devm_namespace_disable);
1785
Dan Williams3d880022015-05-31 15:02:11 -04001786static struct device **create_namespace_io(struct nd_region *nd_region)
1787{
1788 struct nd_namespace_io *nsio;
1789 struct device *dev, **devs;
1790 struct resource *res;
1791
1792 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1793 if (!nsio)
1794 return NULL;
1795
1796 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1797 if (!devs) {
1798 kfree(nsio);
1799 return NULL;
1800 }
1801
Dan Williams8c2f7e82015-06-25 04:20:04 -04001802 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001803 dev->type = &namespace_io_device_type;
1804 dev->parent = &nd_region->dev;
1805 res = &nsio->res;
1806 res->name = dev_name(&nd_region->dev);
1807 res->flags = IORESOURCE_MEM;
1808 res->start = nd_region->ndr_start;
1809 res->end = res->start + nd_region->ndr_size - 1;
1810
1811 devs[0] = dev;
1812 return devs;
1813}
1814
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001815static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1816 u64 cookie, u16 pos)
1817{
1818 struct nd_namespace_label *found = NULL;
1819 int i;
1820
1821 for (i = 0; i < nd_region->ndr_mappings; i++) {
1822 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsfaec6f82017-06-06 11:10:51 -07001823 struct nd_interleave_set *nd_set = nd_region->nd_set;
1824 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001825 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001826 bool found_uuid = false;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001827
Dan Williamsae8219f2016-09-19 16:04:21 -07001828 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1829 struct nd_namespace_label *nd_label = label_ent->label;
1830 u16 position, nlabel;
1831 u64 isetcookie;
1832
1833 if (!nd_label)
1834 continue;
1835 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1836 position = __le16_to_cpu(nd_label->position);
1837 nlabel = __le16_to_cpu(nd_label->nlabel);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001838
1839 if (isetcookie != cookie)
1840 continue;
1841
1842 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1843 continue;
1844
Dan Williamsfaec6f82017-06-06 11:10:51 -07001845 if (namespace_label_has(ndd, type_guid)
1846 && !guid_equal(&nd_set->type_guid,
1847 &nd_label->type_guid)) {
1848 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
Andy Shevchenkodb5d00c2019-06-21 14:45:18 +03001849 &nd_set->type_guid,
1850 &nd_label->type_guid);
Dan Williamsfaec6f82017-06-06 11:10:51 -07001851 continue;
1852 }
1853
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001854 if (found_uuid) {
Dan Williams426824d2018-03-05 16:39:31 -08001855 dev_dbg(ndd->dev, "duplicate entry for uuid\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001856 return false;
1857 }
1858 found_uuid = true;
1859 if (nlabel != nd_region->ndr_mappings)
1860 continue;
1861 if (position != pos)
1862 continue;
1863 found = nd_label;
1864 break;
1865 }
1866 if (found)
1867 break;
1868 }
1869 return found != NULL;
1870}
1871
1872static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1873{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001874 int i;
1875
1876 if (!pmem_id)
1877 return -ENODEV;
1878
1879 for (i = 0; i < nd_region->ndr_mappings; i++) {
1880 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams0e3b0d12016-10-06 23:13:15 -07001881 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001882 struct nd_namespace_label *nd_label = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001883 u64 hw_start, hw_end, pmem_start, pmem_end;
Dan Williamsae8219f2016-09-19 16:04:21 -07001884 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001885
Dan Williams9cf8bd52016-12-15 20:04:31 -08001886 lockdep_assert_held(&nd_mapping->lock);
Dan Williamsae8219f2016-09-19 16:04:21 -07001887 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1888 nd_label = label_ent->label;
1889 if (!nd_label)
1890 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001891 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1892 break;
Dan Williamsae8219f2016-09-19 16:04:21 -07001893 nd_label = NULL;
1894 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001895
1896 if (!nd_label) {
1897 WARN_ON(1);
1898 return -EINVAL;
1899 }
1900
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001901 /*
1902 * Check that this label is compliant with the dpa
1903 * range published in NFIT
1904 */
1905 hw_start = nd_mapping->start;
1906 hw_end = hw_start + nd_mapping->size;
Dan Williamsae8219f2016-09-19 16:04:21 -07001907 pmem_start = __le64_to_cpu(nd_label->dpa);
1908 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
Dan Williams0e3b0d12016-10-06 23:13:15 -07001909 if (pmem_start >= hw_start && pmem_start < hw_end
1910 && pmem_end <= hw_end && pmem_end > hw_start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001911 /* pass */;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001912 else {
1913 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1914 dev_name(ndd->dev), nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001915 return -EINVAL;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001916 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001917
Dan Williams8a5f50d2016-09-22 15:42:59 -07001918 /* move recently validated label to the front of the list */
1919 list_move(&label_ent->list, &nd_mapping->labels);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001920 }
1921 return 0;
1922}
1923
1924/**
Dan Williams8a5f50d2016-09-22 15:42:59 -07001925 * create_namespace_pmem - validate interleave set labelling, retrieve label0
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001926 * @nd_region: region with mappings to validate
Dan Williams8a5f50d2016-09-22 15:42:59 -07001927 * @nspm: target namespace to create
1928 * @nd_label: target pmem namespace label to evaluate
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001929 */
Colin Ian King65853a12017-10-05 10:55:57 +01001930static struct device *create_namespace_pmem(struct nd_region *nd_region,
Dan Williamsc12c48c2017-06-04 10:59:15 +09001931 struct nd_namespace_index *nsindex,
Dan Williams8a5f50d2016-09-22 15:42:59 -07001932 struct nd_namespace_label *nd_label)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001933{
Dan Williamsc12c48c2017-06-04 10:59:15 +09001934 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
Dan Williams86ef58a2017-02-28 18:32:48 -08001935 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07001936 struct nd_label_ent *label_ent;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001937 struct nd_namespace_pmem *nspm;
Dan Williamsae8219f2016-09-19 16:04:21 -07001938 struct nd_mapping *nd_mapping;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001939 resource_size_t size = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001940 struct resource *res;
1941 struct device *dev;
Dan Williamsae8219f2016-09-19 16:04:21 -07001942 int rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001943 u16 i;
1944
Dan Williams47652182016-09-15 18:08:05 -07001945 if (cookie == 0) {
1946 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
Dan Williams8a5f50d2016-09-22 15:42:59 -07001947 return ERR_PTR(-ENXIO);
Dan Williams47652182016-09-15 18:08:05 -07001948 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001949
Dan Williams8a5f50d2016-09-22 15:42:59 -07001950 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1951 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1952 nd_label->uuid);
Dan Williams86ef58a2017-02-28 18:32:48 -08001953 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1954 return ERR_PTR(-EAGAIN);
1955
1956 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1957 nd_label->uuid);
Dan Williamsae8219f2016-09-19 16:04:21 -07001958 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001959
Dan Williams8a5f50d2016-09-22 15:42:59 -07001960 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1961 if (!nspm)
1962 return ERR_PTR(-ENOMEM);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001963
Dan Williams0e3b0d12016-10-06 23:13:15 -07001964 nspm->id = -1;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001965 dev = &nspm->nsio.common.dev;
1966 dev->type = &namespace_pmem_device_type;
1967 dev->parent = &nd_region->dev;
1968 res = &nspm->nsio.res;
1969 res->name = dev_name(&nd_region->dev);
1970 res->flags = IORESOURCE_MEM;
1971
Dan Williams86ef58a2017-02-28 18:32:48 -08001972 for (i = 0; i < nd_region->ndr_mappings; i++) {
1973 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1974 continue;
1975 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1976 continue;
1977 break;
1978 }
1979
Dan Williams8a5f50d2016-09-22 15:42:59 -07001980 if (i < nd_region->ndr_mappings) {
Dan Williams4f867222018-04-06 16:37:21 -07001981 struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001982
Dan Williams8a5f50d2016-09-22 15:42:59 -07001983 /*
1984 * Give up if we don't find an instance of a uuid at each
1985 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1986 * find a dimm with two instances of the same uuid.
1987 */
Dan Williams0e3b0d12016-10-06 23:13:15 -07001988 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
Dan Williams4f867222018-04-06 16:37:21 -07001989 nvdimm_name(nvdimm), nd_label->uuid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001990 rc = -EINVAL;
Dan Williamsae8219f2016-09-19 16:04:21 -07001991 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001992 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001993
1994 /*
1995 * Fix up each mapping's 'labels' to have the validated pmem label for
1996 * that position at labels[0], and NULL at labels[1]. In the process,
1997 * check that the namespace aligns with interleave-set. We know
1998 * that it does not overlap with any blk namespaces by virtue of
1999 * the dimm being enabled (i.e. nd_label_reserve_dpa()
2000 * succeeded).
2001 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07002002 rc = select_pmem_id(nd_region, nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002003 if (rc)
2004 goto err;
2005
2006 /* Calculate total size and populate namespace properties from label0 */
2007 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002008 struct nd_namespace_label *label0;
Dan Williamsb3fde742017-06-04 10:18:39 +09002009 struct nvdimm_drvdata *ndd;
Dan Williamsae8219f2016-09-19 16:04:21 -07002010
2011 nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07002012 label_ent = list_first_entry_or_null(&nd_mapping->labels,
2013 typeof(*label_ent), list);
Aneesh Kumar K.V86aa6662019-08-09 13:17:26 +05302014 label0 = label_ent ? label_ent->label : NULL;
Dan Williamsae8219f2016-09-19 16:04:21 -07002015
2016 if (!label0) {
2017 WARN_ON(1);
2018 continue;
2019 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002020
2021 size += __le64_to_cpu(label0->rawsize);
2022 if (__le16_to_cpu(label0->position) != 0)
2023 continue;
2024 WARN_ON(nspm->alt_name || nspm->uuid);
2025 nspm->alt_name = kmemdup((void __force *) label0->name,
2026 NSLABEL_NAME_LEN, GFP_KERNEL);
2027 nspm->uuid = kmemdup((void __force *) label0->uuid,
2028 NSLABEL_UUID_LEN, GFP_KERNEL);
Dan Williamsf979b132017-06-04 12:12:07 +09002029 nspm->lbasize = __le64_to_cpu(label0->lbasize);
Dan Williamsb3fde742017-06-04 10:18:39 +09002030 ndd = to_ndd(nd_mapping);
2031 if (namespace_label_has(ndd, abstraction_guid))
2032 nspm->nsio.common.claim_class
2033 = to_nvdimm_cclass(&label0->abstraction_guid);
2034
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002035 }
2036
2037 if (!nspm->alt_name || !nspm->uuid) {
2038 rc = -ENOMEM;
2039 goto err;
2040 }
2041
Dan Williams0e3b0d12016-10-06 23:13:15 -07002042 nd_namespace_pmem_set_resource(nd_region, nspm, size);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002043
Dan Williams8a5f50d2016-09-22 15:42:59 -07002044 return dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002045 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002046 namespace_pmem_release(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002047 switch (rc) {
2048 case -EINVAL:
Dan Williams426824d2018-03-05 16:39:31 -08002049 dev_dbg(&nd_region->dev, "invalid label(s)\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002050 break;
2051 case -ENODEV:
Dan Williams426824d2018-03-05 16:39:31 -08002052 dev_dbg(&nd_region->dev, "label not found\n");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002053 break;
2054 default:
Dan Williams426824d2018-03-05 16:39:31 -08002055 dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002056 break;
2057 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002058 return ERR_PTR(rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002059}
2060
Dan Williams1b40e092015-05-01 13:34:01 -04002061struct resource *nsblk_add_resource(struct nd_region *nd_region,
2062 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
2063 resource_size_t start)
2064{
2065 struct nd_label_id label_id;
2066 struct resource *res;
2067
2068 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
2069 res = krealloc(nsblk->res,
2070 sizeof(void *) * (nsblk->num_resources + 1),
2071 GFP_KERNEL);
2072 if (!res)
2073 return NULL;
2074 nsblk->res = (struct resource **) res;
2075 for_each_dpa_resource(ndd, res)
2076 if (strcmp(res->name, label_id.id) == 0
2077 && res->start == start) {
2078 nsblk->res[nsblk->num_resources++] = res;
2079 return res;
2080 }
2081 return NULL;
2082}
2083
2084static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
2085{
2086 struct nd_namespace_blk *nsblk;
2087 struct device *dev;
2088
2089 if (!is_nd_blk(&nd_region->dev))
2090 return NULL;
2091
2092 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2093 if (!nsblk)
2094 return NULL;
2095
Dan Williams8c2f7e82015-06-25 04:20:04 -04002096 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002097 dev->type = &namespace_blk_device_type;
2098 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2099 if (nsblk->id < 0) {
2100 kfree(nsblk);
2101 return NULL;
2102 }
2103 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
2104 dev->parent = &nd_region->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002105
Dan Williams8c2f7e82015-06-25 04:20:04 -04002106 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04002107}
2108
Dan Williams98a29c32016-09-30 15:28:27 -07002109static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
2110{
2111 struct nd_namespace_pmem *nspm;
2112 struct resource *res;
2113 struct device *dev;
2114
Dan Williamsc9e582a2017-05-29 23:12:19 -07002115 if (!is_memory(&nd_region->dev))
Dan Williams98a29c32016-09-30 15:28:27 -07002116 return NULL;
2117
2118 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2119 if (!nspm)
2120 return NULL;
2121
2122 dev = &nspm->nsio.common.dev;
2123 dev->type = &namespace_pmem_device_type;
2124 dev->parent = &nd_region->dev;
2125 res = &nspm->nsio.res;
2126 res->name = dev_name(&nd_region->dev);
2127 res->flags = IORESOURCE_MEM;
2128
2129 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
2130 if (nspm->id < 0) {
2131 kfree(nspm);
2132 return NULL;
2133 }
2134 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
Dan Williams98a29c32016-09-30 15:28:27 -07002135 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
2136
2137 return dev;
2138}
2139
2140void nd_region_create_ns_seed(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -04002141{
2142 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
Dan Williams98a29c32016-09-30 15:28:27 -07002143
2144 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
2145 return;
2146
2147 if (is_nd_blk(&nd_region->dev))
2148 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
2149 else
2150 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
2151
Dan Williams1b40e092015-05-01 13:34:01 -04002152 /*
2153 * Seed creation failures are not fatal, provisioning is simply
2154 * disabled until memory becomes available
2155 */
2156 if (!nd_region->ns_seed)
Dan Williams98a29c32016-09-30 15:28:27 -07002157 dev_err(&nd_region->dev, "failed to create %s namespace\n",
2158 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
Dan Williams1b40e092015-05-01 13:34:01 -04002159 else
2160 nd_device_register(nd_region->ns_seed);
2161}
2162
Dan Williamscd034122016-03-11 10:15:36 -08002163void nd_region_create_dax_seed(struct nd_region *nd_region)
2164{
2165 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2166 nd_region->dax_seed = nd_dax_create(nd_region);
2167 /*
2168 * Seed creation failures are not fatal, provisioning is simply
2169 * disabled until memory becomes available
2170 */
2171 if (!nd_region->dax_seed)
2172 dev_err(&nd_region->dev, "failed to create dax namespace\n");
2173}
2174
Dan Williams2dc43332015-12-13 11:41:36 -08002175void nd_region_create_pfn_seed(struct nd_region *nd_region)
2176{
2177 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2178 nd_region->pfn_seed = nd_pfn_create(nd_region);
2179 /*
2180 * Seed creation failures are not fatal, provisioning is simply
2181 * disabled until memory becomes available
2182 */
2183 if (!nd_region->pfn_seed)
2184 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
2185}
2186
Dan Williams8c2f7e82015-06-25 04:20:04 -04002187void nd_region_create_btt_seed(struct nd_region *nd_region)
2188{
2189 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
2190 nd_region->btt_seed = nd_btt_create(nd_region);
2191 /*
2192 * Seed creation failures are not fatal, provisioning is simply
2193 * disabled until memory becomes available
2194 */
2195 if (!nd_region->btt_seed)
2196 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2197}
2198
Dan Williams8a5f50d2016-09-22 15:42:59 -07002199static int add_namespace_resource(struct nd_region *nd_region,
2200 struct nd_namespace_label *nd_label, struct device **devs,
2201 int count)
Dan Williams1b40e092015-05-01 13:34:01 -04002202{
Dan Williams8a5f50d2016-09-22 15:42:59 -07002203 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsae8219f2016-09-19 16:04:21 -07002204 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002205 int i;
2206
2207 for (i = 0; i < count; i++) {
2208 u8 *uuid = namespace_to_uuid(devs[i]);
2209 struct resource *res;
2210
2211 if (IS_ERR_OR_NULL(uuid)) {
2212 WARN_ON(1);
2213 continue;
2214 }
2215
2216 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2217 continue;
2218 if (is_namespace_blk(devs[i])) {
2219 res = nsblk_add_resource(nd_region, ndd,
2220 to_nd_namespace_blk(devs[i]),
2221 __le64_to_cpu(nd_label->dpa));
2222 if (!res)
2223 return -ENXIO;
2224 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2225 } else {
2226 dev_err(&nd_region->dev,
2227 "error: conflicting extents for uuid: %pUb\n",
2228 nd_label->uuid);
2229 return -ENXIO;
2230 }
2231 break;
2232 }
2233
2234 return i;
2235}
2236
Colin Ian King65853a12017-10-05 10:55:57 +01002237static struct device *create_namespace_blk(struct nd_region *nd_region,
Dan Williams8a5f50d2016-09-22 15:42:59 -07002238 struct nd_namespace_label *nd_label, int count)
2239{
2240
2241 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsfaec6f82017-06-06 11:10:51 -07002242 struct nd_interleave_set *nd_set = nd_region->nd_set;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002243 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002244 struct nd_namespace_blk *nsblk;
Nicolas Iooss238b3232016-11-26 20:18:04 +01002245 char name[NSLABEL_NAME_LEN];
Dan Williams8a5f50d2016-09-22 15:42:59 -07002246 struct device *dev = NULL;
2247 struct resource *res;
2248
Dan Williams8f2bc242017-06-06 11:39:30 -07002249 if (namespace_label_has(ndd, type_guid)) {
2250 if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
2251 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
Andy Shevchenkodb5d00c2019-06-21 14:45:18 +03002252 &nd_set->type_guid,
2253 &nd_label->type_guid);
Dan Williams8f2bc242017-06-06 11:39:30 -07002254 return ERR_PTR(-EAGAIN);
2255 }
2256
2257 if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
2258 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
2259 nd_set->cookie2,
2260 __le64_to_cpu(nd_label->isetcookie));
2261 return ERR_PTR(-EAGAIN);
2262 }
Dan Williamsfaec6f82017-06-06 11:10:51 -07002263 }
2264
Dan Williams8a5f50d2016-09-22 15:42:59 -07002265 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2266 if (!nsblk)
2267 return ERR_PTR(-ENOMEM);
2268 dev = &nsblk->common.dev;
2269 dev->type = &namespace_blk_device_type;
2270 dev->parent = &nd_region->dev;
2271 nsblk->id = -1;
2272 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2273 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2274 GFP_KERNEL);
Dan Williamsb3fde742017-06-04 10:18:39 +09002275 if (namespace_label_has(ndd, abstraction_guid))
2276 nsblk->common.claim_class
2277 = to_nvdimm_cclass(&nd_label->abstraction_guid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002278 if (!nsblk->uuid)
2279 goto blk_err;
2280 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
Kangjie Lu55c1fc02019-03-12 03:20:34 -05002281 if (name[0]) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07002282 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2283 GFP_KERNEL);
Kangjie Lu55c1fc02019-03-12 03:20:34 -05002284 if (!nsblk->alt_name)
2285 goto blk_err;
2286 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002287 res = nsblk_add_resource(nd_region, ndd, nsblk,
2288 __le64_to_cpu(nd_label->dpa));
2289 if (!res)
2290 goto blk_err;
2291 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2292 return dev;
2293 blk_err:
2294 namespace_blk_release(dev);
2295 return ERR_PTR(-ENXIO);
2296}
2297
Dan Williams6ff3e912016-10-05 14:04:15 -07002298static int cmp_dpa(const void *a, const void *b)
2299{
2300 const struct device *dev_a = *(const struct device **) a;
2301 const struct device *dev_b = *(const struct device **) b;
2302 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2303 struct nd_namespace_pmem *nspm_a, *nspm_b;
2304
2305 if (is_namespace_io(dev_a))
2306 return 0;
2307
2308 if (is_namespace_blk(dev_a)) {
2309 nsblk_a = to_nd_namespace_blk(dev_a);
2310 nsblk_b = to_nd_namespace_blk(dev_b);
2311
2312 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2313 sizeof(resource_size_t));
2314 }
2315
2316 nspm_a = to_nd_namespace_pmem(dev_a);
2317 nspm_b = to_nd_namespace_pmem(dev_b);
2318
2319 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2320 sizeof(resource_size_t));
2321}
2322
Dan Williams8a5f50d2016-09-22 15:42:59 -07002323static struct device **scan_labels(struct nd_region *nd_region)
2324{
Dan Williamsc969e242016-10-05 15:54:46 -07002325 int i, count = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002326 struct device *dev, **devs = NULL;
2327 struct nd_label_ent *label_ent, *e;
Dan Williamsc969e242016-10-05 15:54:46 -07002328 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2329 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -04002330
Dan Williams8a5f50d2016-09-22 15:42:59 -07002331 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2332 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002333 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams1b40e092015-05-01 13:34:01 -04002334 struct device **__devs;
Dan Williamsae8219f2016-09-19 16:04:21 -07002335 u32 flags;
Dan Williams1b40e092015-05-01 13:34:01 -04002336
Dan Williamsae8219f2016-09-19 16:04:21 -07002337 if (!nd_label)
2338 continue;
2339 flags = __le32_to_cpu(nd_label->flags);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002340 if (is_nd_blk(&nd_region->dev)
2341 == !!(flags & NSLABEL_FLAG_LOCAL))
2342 /* pass, region matches label type */;
Dan Williams1b40e092015-05-01 13:34:01 -04002343 else
2344 continue;
2345
Dan Williamsc969e242016-10-05 15:54:46 -07002346 /* skip labels that describe extents outside of the region */
Aneesh Kumar K.V86aa6662019-08-09 13:17:26 +05302347 if (__le64_to_cpu(nd_label->dpa) < nd_mapping->start ||
2348 __le64_to_cpu(nd_label->dpa) > map_end)
2349 continue;
Dan Williamsc969e242016-10-05 15:54:46 -07002350
Dan Williams8a5f50d2016-09-22 15:42:59 -07002351 i = add_namespace_resource(nd_region, nd_label, devs, count);
2352 if (i < 0)
2353 goto err;
Dan Williams1b40e092015-05-01 13:34:01 -04002354 if (i < count)
2355 continue;
2356 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2357 if (!__devs)
2358 goto err;
2359 memcpy(__devs, devs, sizeof(dev) * count);
2360 kfree(devs);
2361 devs = __devs;
2362
Dan Williamsfaec6f82017-06-06 11:10:51 -07002363 if (is_nd_blk(&nd_region->dev))
Dan Williams8a5f50d2016-09-22 15:42:59 -07002364 dev = create_namespace_blk(nd_region, nd_label, count);
Dan Williamsfaec6f82017-06-06 11:10:51 -07002365 else {
Dan Williamsc12c48c2017-06-04 10:59:15 +09002366 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2367 struct nd_namespace_index *nsindex;
2368
2369 nsindex = to_namespace_index(ndd, ndd->ns_current);
2370 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002371 }
Dan Williamsfaec6f82017-06-06 11:10:51 -07002372
2373 if (IS_ERR(dev)) {
2374 switch (PTR_ERR(dev)) {
2375 case -EAGAIN:
2376 /* skip invalid labels */
2377 continue;
2378 case -ENODEV:
2379 /* fallthrough to seed creation */
2380 break;
2381 default:
2382 goto err;
2383 }
2384 } else
2385 devs[count++] = dev;
2386
Dan Williams1b40e092015-05-01 13:34:01 -04002387 }
2388
Dan Williams426824d2018-03-05 16:39:31 -08002389 dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
2390 count, is_nd_blk(&nd_region->dev)
Dan Williams8a5f50d2016-09-22 15:42:59 -07002391 ? "blk" : "pmem", count == 1 ? "" : "s");
Dan Williams1b40e092015-05-01 13:34:01 -04002392
2393 if (count == 0) {
2394 /* Publish a zero-sized namespace for userspace to configure. */
Dan Williamsae8219f2016-09-19 16:04:21 -07002395 nd_mapping_free_labels(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002396
2397 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2398 if (!devs)
2399 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002400 if (is_nd_blk(&nd_region->dev)) {
2401 struct nd_namespace_blk *nsblk;
2402
2403 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2404 if (!nsblk)
2405 goto err;
2406 dev = &nsblk->common.dev;
2407 dev->type = &namespace_blk_device_type;
2408 } else {
2409 struct nd_namespace_pmem *nspm;
2410
2411 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2412 if (!nspm)
2413 goto err;
2414 dev = &nspm->nsio.common.dev;
2415 dev->type = &namespace_pmem_device_type;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002416 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002417 }
Dan Williams1b40e092015-05-01 13:34:01 -04002418 dev->parent = &nd_region->dev;
2419 devs[count++] = dev;
Dan Williamsc9e582a2017-05-29 23:12:19 -07002420 } else if (is_memory(&nd_region->dev)) {
Dan Williams8a5f50d2016-09-22 15:42:59 -07002421 /* clean unselected labels */
2422 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07002423 struct list_head *l, *e;
2424 LIST_HEAD(list);
2425 int j;
2426
Dan Williams8a5f50d2016-09-22 15:42:59 -07002427 nd_mapping = &nd_region->mapping[i];
2428 if (list_empty(&nd_mapping->labels)) {
2429 WARN_ON(1);
2430 continue;
2431 }
Dan Williams0e3b0d12016-10-06 23:13:15 -07002432
2433 j = count;
2434 list_for_each_safe(l, e, &nd_mapping->labels) {
2435 if (!j--)
2436 break;
2437 list_move_tail(l, &list);
2438 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002439 nd_mapping_free_labels(nd_mapping);
Dan Williams0e3b0d12016-10-06 23:13:15 -07002440 list_splice_init(&list, &nd_mapping->labels);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002441 }
Dan Williams1b40e092015-05-01 13:34:01 -04002442 }
2443
Dan Williams6ff3e912016-10-05 14:04:15 -07002444 if (count > 1)
2445 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2446
Dan Williams1b40e092015-05-01 13:34:01 -04002447 return devs;
2448
Dan Williamsae8219f2016-09-19 16:04:21 -07002449 err:
Dan Carpenter75d29712016-10-12 09:34:29 +03002450 if (devs) {
2451 for (i = 0; devs[i]; i++)
2452 if (is_nd_blk(&nd_region->dev))
2453 namespace_blk_release(devs[i]);
2454 else
2455 namespace_pmem_release(devs[i]);
2456 kfree(devs);
2457 }
Dan Williams1b40e092015-05-01 13:34:01 -04002458 return NULL;
2459}
2460
Dan Williams8a5f50d2016-09-22 15:42:59 -07002461static struct device **create_namespaces(struct nd_region *nd_region)
Dan Williamsae8219f2016-09-19 16:04:21 -07002462{
Colin Ian King59858d32018-01-30 17:47:07 +00002463 struct nd_mapping *nd_mapping;
Dan Williamsae8219f2016-09-19 16:04:21 -07002464 struct device **devs;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002465 int i;
Dan Williamsae8219f2016-09-19 16:04:21 -07002466
2467 if (nd_region->ndr_mappings == 0)
2468 return NULL;
2469
Dan Williams8a5f50d2016-09-22 15:42:59 -07002470 /* lock down all mappings while we scan labels */
2471 for (i = 0; i < nd_region->ndr_mappings; i++) {
2472 nd_mapping = &nd_region->mapping[i];
2473 mutex_lock_nested(&nd_mapping->lock, i);
2474 }
2475
2476 devs = scan_labels(nd_region);
2477
2478 for (i = 0; i < nd_region->ndr_mappings; i++) {
2479 int reverse = nd_region->ndr_mappings - 1 - i;
2480
2481 nd_mapping = &nd_region->mapping[reverse];
2482 mutex_unlock(&nd_mapping->lock);
2483 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002484
2485 return devs;
2486}
2487
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302488static void deactivate_labels(void *region)
2489{
2490 struct nd_region *nd_region = region;
2491 int i;
2492
2493 for (i = 0; i < nd_region->ndr_mappings; i++) {
2494 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2495 struct nvdimm_drvdata *ndd = nd_mapping->ndd;
2496 struct nvdimm *nvdimm = nd_mapping->nvdimm;
2497
2498 mutex_lock(&nd_mapping->lock);
2499 nd_mapping_free_labels(nd_mapping);
2500 mutex_unlock(&nd_mapping->lock);
2501
2502 put_ndd(ndd);
2503 nd_mapping->ndd = NULL;
2504 if (ndd)
2505 atomic_dec(&nvdimm->busy);
2506 }
2507}
2508
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002509static int init_active_labels(struct nd_region *nd_region)
2510{
2511 int i;
2512
2513 for (i = 0; i < nd_region->ndr_mappings; i++) {
2514 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2515 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2516 struct nvdimm *nvdimm = nd_mapping->nvdimm;
Dan Williamsae8219f2016-09-19 16:04:21 -07002517 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002518 int count, j;
2519
2520 /*
Dan Williams9d62ed92017-05-04 11:47:22 -07002521 * If the dimm is disabled then we may need to prevent
2522 * the region from being activated.
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002523 */
2524 if (!ndd) {
Dan Williams9d62ed92017-05-04 11:47:22 -07002525 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2526 /* fail, label data may be unreadable */;
2527 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2528 /* fail, labels needed to disambiguate dpa */;
2529 else
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002530 return 0;
Dan Williams9d62ed92017-05-04 11:47:22 -07002531
2532 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2533 dev_name(&nd_mapping->nvdimm->dev),
2534 test_bit(NDD_LOCKED, &nvdimm->flags)
2535 ? "locked" : "disabled");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002536 return -ENXIO;
2537 }
2538 nd_mapping->ndd = ndd;
2539 atomic_inc(&nvdimm->busy);
2540 get_ndd(ndd);
2541
2542 count = nd_label_active_count(ndd);
Dan Williams426824d2018-03-05 16:39:31 -08002543 dev_dbg(ndd->dev, "count: %d\n", count);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002544 if (!count)
2545 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002546 for (j = 0; j < count; j++) {
2547 struct nd_namespace_label *label;
2548
Dan Williamsae8219f2016-09-19 16:04:21 -07002549 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2550 if (!label_ent)
2551 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002552 label = nd_label_active(ndd, j);
Dan Williamsd5d30d52019-02-02 16:35:26 -08002553 if (test_bit(NDD_NOBLK, &nvdimm->flags)) {
2554 u32 flags = __le32_to_cpu(label->flags);
2555
2556 flags &= ~NSLABEL_FLAG_LOCAL;
2557 label->flags = __cpu_to_le32(flags);
2558 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002559 label_ent->label = label;
2560
2561 mutex_lock(&nd_mapping->lock);
2562 list_add_tail(&label_ent->list, &nd_mapping->labels);
2563 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002564 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002565
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302566 if (j < count)
2567 break;
2568 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002569
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302570 if (i < nd_region->ndr_mappings) {
2571 deactivate_labels(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07002572 return -ENOMEM;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002573 }
2574
Dan Williamsa2d1c7a2019-09-05 21:15:57 +05302575 return devm_add_action_or_reset(&nd_region->dev, deactivate_labels,
2576 nd_region);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002577}
2578
Dan Williams3d880022015-05-31 15:02:11 -04002579int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2580{
2581 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002582 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04002583
2584 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002585 nvdimm_bus_lock(&nd_region->dev);
2586 rc = init_active_labels(nd_region);
2587 if (rc) {
2588 nvdimm_bus_unlock(&nd_region->dev);
2589 return rc;
2590 }
2591
2592 type = nd_region_to_nstype(nd_region);
2593 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04002594 case ND_DEVICE_NAMESPACE_IO:
2595 devs = create_namespace_io(nd_region);
2596 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002597 case ND_DEVICE_NAMESPACE_PMEM:
Dan Williams1b40e092015-05-01 13:34:01 -04002598 case ND_DEVICE_NAMESPACE_BLK:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002599 devs = create_namespaces(nd_region);
Dan Williams1b40e092015-05-01 13:34:01 -04002600 break;
Dan Williams3d880022015-05-31 15:02:11 -04002601 default:
2602 break;
2603 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002604 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04002605
2606 if (!devs)
2607 return -ENODEV;
2608
Dan Williams3d880022015-05-31 15:02:11 -04002609 for (i = 0; devs[i]; i++) {
2610 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04002611 int id;
Dan Williams3d880022015-05-31 15:02:11 -04002612
Dan Williams1b40e092015-05-01 13:34:01 -04002613 if (type == ND_DEVICE_NAMESPACE_BLK) {
2614 struct nd_namespace_blk *nsblk;
2615
2616 nsblk = to_nd_namespace_blk(dev);
2617 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2618 GFP_KERNEL);
2619 nsblk->id = id;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002620 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2621 struct nd_namespace_pmem *nspm;
2622
2623 nspm = to_nd_namespace_pmem(dev);
2624 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2625 GFP_KERNEL);
2626 nspm->id = id;
Dan Williams1b40e092015-05-01 13:34:01 -04002627 } else
2628 id = i;
2629
2630 if (id < 0)
2631 break;
2632 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002633 nd_device_register(dev);
2634 }
Dan Williams1b40e092015-05-01 13:34:01 -04002635 if (i)
2636 nd_region->ns_seed = devs[0];
2637
2638 if (devs[i]) {
2639 int j;
2640
2641 for (j = i; devs[j]; j++) {
2642 struct device *dev = devs[j];
2643
2644 device_initialize(dev);
2645 put_device(dev);
2646 }
2647 *err = j - i;
2648 /*
2649 * All of the namespaces we tried to register failed, so
2650 * fail region activation.
2651 */
2652 if (*err == 0)
2653 rc = -ENODEV;
2654 }
Dan Williams3d880022015-05-31 15:02:11 -04002655 kfree(devs);
2656
Dan Williams1b40e092015-05-01 13:34:01 -04002657 if (rc == -ENODEV)
2658 return rc;
2659
Dan Williams3d880022015-05-31 15:02:11 -04002660 return i;
2661}