blob: e034b003a5e28daa1a50e81fb997923b3be83b1b [file] [log] [blame]
Dan Williams3d880022015-05-31 15:02:11 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/module.h>
14#include <linux/device.h>
Dan Williams6ff3e912016-10-05 14:04:15 -070015#include <linux/sort.h>
Dan Williams3d880022015-05-31 15:02:11 -040016#include <linux/slab.h>
Dan Williams004f1af2015-08-24 19:20:23 -040017#include <linux/pmem.h>
Dan Williamsae8219f2016-09-19 16:04:21 -070018#include <linux/list.h>
Dan Williams3d880022015-05-31 15:02:11 -040019#include <linux/nd.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040020#include "nd-core.h"
Dan Williams3d880022015-05-31 15:02:11 -040021#include "nd.h"
22
23static void namespace_io_release(struct device *dev)
24{
25 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
26
27 kfree(nsio);
28}
29
Dan Williamsbf9bccc2015-06-17 17:14:46 -040030static void namespace_pmem_release(struct device *dev)
31{
32 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0e3b0d12016-10-06 23:13:15 -070033 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040034
Dan Williams0e3b0d12016-10-06 23:13:15 -070035 if (nspm->id >= 0)
36 ida_simple_remove(&nd_region->ns_ida, nspm->id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040037 kfree(nspm->alt_name);
38 kfree(nspm->uuid);
39 kfree(nspm);
40}
41
42static void namespace_blk_release(struct device *dev)
43{
Dan Williams1b40e092015-05-01 13:34:01 -040044 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
45 struct nd_region *nd_region = to_nd_region(dev->parent);
46
47 if (nsblk->id >= 0)
48 ida_simple_remove(&nd_region->ns_ida, nsblk->id);
49 kfree(nsblk->alt_name);
50 kfree(nsblk->uuid);
51 kfree(nsblk->res);
52 kfree(nsblk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -040053}
54
Bhumika Goyal970d14e2017-01-25 00:54:07 +053055static const struct device_type namespace_io_device_type = {
Dan Williams3d880022015-05-31 15:02:11 -040056 .name = "nd_namespace_io",
57 .release = namespace_io_release,
58};
59
Bhumika Goyal970d14e2017-01-25 00:54:07 +053060static const struct device_type namespace_pmem_device_type = {
Dan Williamsbf9bccc2015-06-17 17:14:46 -040061 .name = "nd_namespace_pmem",
62 .release = namespace_pmem_release,
63};
64
Bhumika Goyal970d14e2017-01-25 00:54:07 +053065static const struct device_type namespace_blk_device_type = {
Dan Williamsbf9bccc2015-06-17 17:14:46 -040066 .name = "nd_namespace_blk",
67 .release = namespace_blk_release,
68};
69
Dan Williams6ff3e912016-10-05 14:04:15 -070070static bool is_namespace_pmem(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -040071{
72 return dev ? dev->type == &namespace_pmem_device_type : false;
73}
74
Dan Williams6ff3e912016-10-05 14:04:15 -070075static bool is_namespace_blk(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -040076{
77 return dev ? dev->type == &namespace_blk_device_type : false;
78}
79
Dan Williams6ff3e912016-10-05 14:04:15 -070080static bool is_namespace_io(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -040081{
82 return dev ? dev->type == &namespace_io_device_type : false;
83}
84
Dan Williamse07ecd72016-01-05 18:37:23 -080085static int is_uuid_busy(struct device *dev, void *data)
86{
87 u8 *uuid1 = data, *uuid2 = NULL;
88
89 if (is_namespace_pmem(dev)) {
90 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
91
92 uuid2 = nspm->uuid;
93 } else if (is_namespace_blk(dev)) {
94 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
95
96 uuid2 = nsblk->uuid;
97 } else if (is_nd_btt(dev)) {
98 struct nd_btt *nd_btt = to_nd_btt(dev);
99
100 uuid2 = nd_btt->uuid;
101 } else if (is_nd_pfn(dev)) {
102 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
103
104 uuid2 = nd_pfn->uuid;
105 }
106
107 if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
108 return -EBUSY;
109
110 return 0;
111}
112
113static int is_namespace_uuid_busy(struct device *dev, void *data)
114{
115 if (is_nd_pmem(dev) || is_nd_blk(dev))
116 return device_for_each_child(dev, data, is_uuid_busy);
117 return 0;
118}
119
120/**
121 * nd_is_uuid_unique - verify that no other namespace has @uuid
122 * @dev: any device on a nvdimm_bus
123 * @uuid: uuid to check
124 */
125bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
126{
127 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
128
129 if (!nvdimm_bus)
130 return false;
131 WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
132 if (device_for_each_child(&nvdimm_bus->dev, uuid,
133 is_namespace_uuid_busy) != 0)
134 return false;
135 return true;
136}
137
Dan Williams004f1af2015-08-24 19:20:23 -0400138bool pmem_should_map_pages(struct device *dev)
139{
140 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamscfe30b82016-03-03 09:38:00 -0800141 struct nd_namespace_io *nsio;
Dan Williams004f1af2015-08-24 19:20:23 -0400142
143 if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
144 return false;
145
146 if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
147 return false;
148
149 if (is_nd_pfn(dev) || is_nd_btt(dev))
150 return false;
151
Dan Williamscfe30b82016-03-03 09:38:00 -0800152 nsio = to_nd_namespace_io(dev);
153 if (region_intersects(nsio->res.start, resource_size(&nsio->res),
154 IORESOURCE_SYSTEM_RAM,
155 IORES_DESC_NONE) == REGION_MIXED)
156 return false;
157
Dan Williams004f1af2015-08-24 19:20:23 -0400158#ifdef ARCH_MEMREMAP_PMEM
159 return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
160#else
161 return false;
162#endif
163}
164EXPORT_SYMBOL(pmem_should_map_pages);
165
Dan Williamsf979b132017-06-04 12:12:07 +0900166unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
167{
168 if (is_namespace_pmem(&ndns->dev)) {
169 struct nd_namespace_pmem *nspm;
170
171 nspm = to_nd_namespace_pmem(&ndns->dev);
172 if (nspm->lbasize == 0 || nspm->lbasize == 512)
173 /* default */;
174 else if (nspm->lbasize == 4096)
175 return 4096;
176 else
177 dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
178 nspm->lbasize);
179 }
180
181 /*
182 * There is no namespace label (is_namespace_io()), or the label
183 * indicates the default sector size.
184 */
185 return 512;
186}
187EXPORT_SYMBOL(pmem_sector_size);
188
Vishal Verma5212e112015-06-25 04:20:32 -0400189const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
190 char *name)
191{
192 struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
Dan Williams004f1af2015-08-24 19:20:23 -0400193 const char *suffix = NULL;
Vishal Verma5212e112015-06-25 04:20:32 -0400194
Dan Williams0731de02015-12-14 15:34:15 -0800195 if (ndns->claim && is_nd_btt(ndns->claim))
196 suffix = "s";
Vishal Verma5212e112015-06-25 04:20:32 -0400197
Dan Williams004f1af2015-08-24 19:20:23 -0400198 if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
Dan Williams01220732016-10-05 09:09:44 -0700199 int nsidx = 0;
200
201 if (is_namespace_pmem(&ndns->dev)) {
202 struct nd_namespace_pmem *nspm;
203
204 nspm = to_nd_namespace_pmem(&ndns->dev);
205 nsidx = nspm->id;
206 }
207
208 if (nsidx)
209 sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
210 suffix ? suffix : "");
211 else
212 sprintf(name, "pmem%d%s", nd_region->id,
213 suffix ? suffix : "");
Dan Williams004f1af2015-08-24 19:20:23 -0400214 } else if (is_namespace_blk(&ndns->dev)) {
Vishal Verma5212e112015-06-25 04:20:32 -0400215 struct nd_namespace_blk *nsblk;
216
217 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400218 sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
219 suffix ? suffix : "");
Vishal Verma5212e112015-06-25 04:20:32 -0400220 } else {
221 return NULL;
222 }
223
224 return name;
225}
226EXPORT_SYMBOL(nvdimm_namespace_disk_name);
227
Vishal Verma6ec68952015-07-29 14:58:09 -0600228const u8 *nd_dev_to_uuid(struct device *dev)
229{
230 static const u8 null_uuid[16];
231
232 if (!dev)
233 return null_uuid;
234
235 if (is_namespace_pmem(dev)) {
236 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
237
238 return nspm->uuid;
239 } else if (is_namespace_blk(dev)) {
240 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
241
242 return nsblk->uuid;
243 } else
244 return null_uuid;
245}
246EXPORT_SYMBOL(nd_dev_to_uuid);
247
Dan Williams3d880022015-05-31 15:02:11 -0400248static ssize_t nstype_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 struct nd_region *nd_region = to_nd_region(dev->parent);
252
253 return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
254}
255static DEVICE_ATTR_RO(nstype);
256
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400257static ssize_t __alt_name_store(struct device *dev, const char *buf,
258 const size_t len)
259{
260 char *input, *pos, *alt_name, **ns_altname;
261 ssize_t rc;
262
263 if (is_namespace_pmem(dev)) {
264 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
265
266 ns_altname = &nspm->alt_name;
267 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400268 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
269
270 ns_altname = &nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400271 } else
272 return -ENXIO;
273
Dan Williams8c2f7e82015-06-25 04:20:04 -0400274 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400275 return -EBUSY;
276
277 input = kmemdup(buf, len + 1, GFP_KERNEL);
278 if (!input)
279 return -ENOMEM;
280
281 input[len] = '\0';
282 pos = strim(input);
283 if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
284 rc = -EINVAL;
285 goto out;
286 }
287
288 alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
289 if (!alt_name) {
290 rc = -ENOMEM;
291 goto out;
292 }
293 kfree(*ns_altname);
294 *ns_altname = alt_name;
295 sprintf(*ns_altname, "%s", pos);
296 rc = len;
297
298out:
299 kfree(input);
300 return rc;
301}
302
Dan Williams1b40e092015-05-01 13:34:01 -0400303static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
304{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400305 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
Dan Williams1b40e092015-05-01 13:34:01 -0400306 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
307 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
308 struct nd_label_id label_id;
309 resource_size_t size = 0;
310 struct resource *res;
311
312 if (!nsblk->uuid)
313 return 0;
314 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
315 for_each_dpa_resource(ndd, res)
316 if (strcmp(res->name, label_id.id) == 0)
317 size += resource_size(res);
318 return size;
319}
320
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400321static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
322{
323 struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
324 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
325 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
326 struct nd_label_id label_id;
327 struct resource *res;
328 int count, i;
329
330 if (!nsblk->uuid || !nsblk->lbasize || !ndd)
331 return false;
332
333 count = 0;
334 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
335 for_each_dpa_resource(ndd, res) {
336 if (strcmp(res->name, label_id.id) != 0)
337 continue;
338 /*
Geert Uytterhoevenae551e92016-08-31 11:45:25 +0200339 * Resources with unacknowledged adjustments indicate a
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400340 * failure to update labels
341 */
342 if (res->flags & DPA_RESOURCE_ADJUSTED)
343 return false;
344 count++;
345 }
346
347 /* These values match after a successful label update */
348 if (count != nsblk->num_resources)
349 return false;
350
351 for (i = 0; i < nsblk->num_resources; i++) {
352 struct resource *found = NULL;
353
354 for_each_dpa_resource(ndd, res)
355 if (res == nsblk->res[i]) {
356 found = res;
357 break;
358 }
359 /* stale resource */
360 if (!found)
361 return false;
362 }
363
364 return true;
365}
366
367resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
368{
369 resource_size_t size;
370
371 nvdimm_bus_lock(&nsblk->common.dev);
372 size = __nd_namespace_blk_validate(nsblk);
373 nvdimm_bus_unlock(&nsblk->common.dev);
374
375 return size;
376}
377EXPORT_SYMBOL(nd_namespace_blk_validate);
378
379
Dan Williamsf524bf22015-05-30 12:36:02 -0400380static int nd_namespace_label_update(struct nd_region *nd_region,
381 struct device *dev)
382{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400383 dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
Dan Williamsf524bf22015-05-30 12:36:02 -0400384 "namespace must be idle during label update\n");
Dan Williams8c2f7e82015-06-25 04:20:04 -0400385 if (dev->driver || to_ndns(dev)->claim)
Dan Williamsf524bf22015-05-30 12:36:02 -0400386 return 0;
387
388 /*
389 * Only allow label writes that will result in a valid namespace
390 * or deletion of an existing namespace.
391 */
392 if (is_namespace_pmem(dev)) {
393 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
Dan Williams0ba1c632015-05-30 12:35:36 -0400394 resource_size_t size = resource_size(&nspm->nsio.res);
Dan Williamsf524bf22015-05-30 12:36:02 -0400395
396 if (size == 0 && nspm->uuid)
397 /* delete allocation */;
398 else if (!nspm->uuid)
399 return 0;
400
401 return nd_pmem_namespace_label_update(nd_region, nspm, size);
402 } else if (is_namespace_blk(dev)) {
Dan Williams0ba1c632015-05-30 12:35:36 -0400403 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
404 resource_size_t size = nd_namespace_blk_size(nsblk);
405
406 if (size == 0 && nsblk->uuid)
407 /* delete allocation */;
408 else if (!nsblk->uuid || !nsblk->lbasize)
409 return 0;
410
411 return nd_blk_namespace_label_update(nd_region, nsblk, size);
Dan Williamsf524bf22015-05-30 12:36:02 -0400412 } else
413 return -ENXIO;
414}
415
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400416static ssize_t alt_name_store(struct device *dev,
417 struct device_attribute *attr, const char *buf, size_t len)
418{
Dan Williamsf524bf22015-05-30 12:36:02 -0400419 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400420 ssize_t rc;
421
422 device_lock(dev);
423 nvdimm_bus_lock(dev);
424 wait_nvdimm_bus_probe_idle(dev);
425 rc = __alt_name_store(dev, buf, len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400426 if (rc >= 0)
427 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400428 dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
429 nvdimm_bus_unlock(dev);
430 device_unlock(dev);
431
Dan Williamsf524bf22015-05-30 12:36:02 -0400432 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400433}
434
435static ssize_t alt_name_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
437{
438 char *ns_altname;
439
440 if (is_namespace_pmem(dev)) {
441 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
442
443 ns_altname = nspm->alt_name;
444 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -0400445 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
446
447 ns_altname = nsblk->alt_name;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400448 } else
449 return -ENXIO;
450
451 return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
452}
453static DEVICE_ATTR_RW(alt_name);
454
455static int scan_free(struct nd_region *nd_region,
456 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
457 resource_size_t n)
458{
459 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
460 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
461 int rc = 0;
462
463 while (n) {
464 struct resource *res, *last;
465 resource_size_t new_start;
466
467 last = NULL;
468 for_each_dpa_resource(ndd, res)
469 if (strcmp(res->name, label_id->id) == 0)
470 last = res;
471 res = last;
472 if (!res)
473 return 0;
474
475 if (n >= resource_size(res)) {
476 n -= resource_size(res);
477 nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
478 nvdimm_free_dpa(ndd, res);
479 /* retry with last resource deleted */
480 continue;
481 }
482
483 /*
484 * Keep BLK allocations relegated to high DPA as much as
485 * possible
486 */
487 if (is_blk)
488 new_start = res->start + n;
489 else
490 new_start = res->start;
491
492 rc = adjust_resource(res, new_start, resource_size(res) - n);
Dan Williams1b40e092015-05-01 13:34:01 -0400493 if (rc == 0)
494 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400495 nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
496 break;
497 }
498
499 return rc;
500}
501
502/**
503 * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
504 * @nd_region: the set of dimms to reclaim @n bytes from
505 * @label_id: unique identifier for the namespace consuming this dpa range
506 * @n: number of bytes per-dimm to release
507 *
508 * Assumes resources are ordered. Starting from the end try to
509 * adjust_resource() the allocation to @n, but if @n is larger than the
510 * allocation delete it and find the 'new' last allocation in the label
511 * set.
512 */
513static int shrink_dpa_allocation(struct nd_region *nd_region,
514 struct nd_label_id *label_id, resource_size_t n)
515{
516 int i;
517
518 for (i = 0; i < nd_region->ndr_mappings; i++) {
519 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
520 int rc;
521
522 rc = scan_free(nd_region, nd_mapping, label_id, n);
523 if (rc)
524 return rc;
525 }
526
527 return 0;
528}
529
530static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
531 struct nd_region *nd_region, struct nd_mapping *nd_mapping,
532 resource_size_t n)
533{
534 bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
535 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
536 resource_size_t first_dpa;
537 struct resource *res;
538 int rc = 0;
539
540 /* allocate blk from highest dpa first */
541 if (is_blk)
542 first_dpa = nd_mapping->start + nd_mapping->size - n;
543 else
544 first_dpa = nd_mapping->start;
545
546 /* first resource allocation for this label-id or dimm */
547 res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
548 if (!res)
549 rc = -EBUSY;
550
551 nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
552 return rc ? n : 0;
553}
554
Dan Williams762d0672016-10-04 16:09:59 -0700555
556/**
557 * space_valid() - validate free dpa space against constraints
558 * @nd_region: hosting region of the free space
559 * @ndd: dimm device data for debug
560 * @label_id: namespace id to allocate space
561 * @prev: potential allocation that precedes free space
562 * @next: allocation that follows the given free space range
563 * @exist: first allocation with same id in the mapping
564 * @n: range that must satisfied for pmem allocations
565 * @valid: free space range to validate
566 *
567 * BLK-space is valid as long as it does not precede a PMEM
568 * allocation in a given region. PMEM-space must be contiguous
569 * and adjacent to an existing existing allocation (if one
570 * exists). If reserving PMEM any space is valid.
571 */
572static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
573 struct nd_label_id *label_id, struct resource *prev,
574 struct resource *next, struct resource *exist,
575 resource_size_t n, struct resource *valid)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400576{
Dan Williams762d0672016-10-04 16:09:59 -0700577 bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
578 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
579
580 if (valid->start >= valid->end)
581 goto invalid;
582
583 if (is_reserve)
584 return;
585
586 if (!is_pmem) {
587 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
588 struct nvdimm_bus *nvdimm_bus;
589 struct blk_alloc_info info = {
590 .nd_mapping = nd_mapping,
591 .available = nd_mapping->size,
592 .res = valid,
593 };
594
595 WARN_ON(!is_nd_blk(&nd_region->dev));
596 nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
597 device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
598 return;
599 }
600
601 /* allocation needs to be contiguous, so this is all or nothing */
602 if (resource_size(valid) < n)
603 goto invalid;
604
605 /* we've got all the space we need and no existing allocation */
606 if (!exist)
607 return;
608
609 /* allocation needs to be contiguous with the existing namespace */
610 if (valid->start == exist->end + 1
611 || valid->end == exist->start - 1)
612 return;
613
614 invalid:
615 /* truncate @valid size to 0 */
616 valid->end = valid->start - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400617}
618
619enum alloc_loc {
620 ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
621};
622
623static resource_size_t scan_allocate(struct nd_region *nd_region,
624 struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
625 resource_size_t n)
626{
627 resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
628 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
629 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams762d0672016-10-04 16:09:59 -0700630 struct resource *res, *exist = NULL, valid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400631 const resource_size_t to_allocate = n;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400632 int first;
633
Dan Williams762d0672016-10-04 16:09:59 -0700634 for_each_dpa_resource(ndd, res)
635 if (strcmp(label_id->id, res->name) == 0)
636 exist = res;
637
638 valid.start = nd_mapping->start;
639 valid.end = mapping_end;
640 valid.name = "free space";
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400641 retry:
642 first = 0;
643 for_each_dpa_resource(ndd, res) {
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400644 struct resource *next = res->sibling, *new_res = NULL;
Dan Williams762d0672016-10-04 16:09:59 -0700645 resource_size_t allocate, available = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400646 enum alloc_loc loc = ALLOC_ERR;
647 const char *action;
648 int rc = 0;
649
650 /* ignore resources outside this nd_mapping */
651 if (res->start > mapping_end)
652 continue;
653 if (res->end < nd_mapping->start)
654 continue;
655
656 /* space at the beginning of the mapping */
657 if (!first++ && res->start > nd_mapping->start) {
Dan Williams762d0672016-10-04 16:09:59 -0700658 valid.start = nd_mapping->start;
659 valid.end = res->start - 1;
660 space_valid(nd_region, ndd, label_id, NULL, next, exist,
661 to_allocate, &valid);
662 available = resource_size(&valid);
663 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400664 loc = ALLOC_BEFORE;
665 }
666
667 /* space between allocations */
668 if (!loc && next) {
Dan Williams762d0672016-10-04 16:09:59 -0700669 valid.start = res->start + resource_size(res);
670 valid.end = min(mapping_end, next->start - 1);
671 space_valid(nd_region, ndd, label_id, res, next, exist,
672 to_allocate, &valid);
673 available = resource_size(&valid);
674 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400675 loc = ALLOC_MID;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400676 }
677
678 /* space at the end of the mapping */
679 if (!loc && !next) {
Dan Williams762d0672016-10-04 16:09:59 -0700680 valid.start = res->start + resource_size(res);
681 valid.end = mapping_end;
682 space_valid(nd_region, ndd, label_id, res, next, exist,
683 to_allocate, &valid);
684 available = resource_size(&valid);
685 if (available)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400686 loc = ALLOC_AFTER;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400687 }
688
689 if (!loc || !available)
690 continue;
691 allocate = min(available, n);
692 switch (loc) {
693 case ALLOC_BEFORE:
694 if (strcmp(res->name, label_id->id) == 0) {
695 /* adjust current resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400696 rc = adjust_resource(res, res->start - allocate,
697 resource_size(res) + allocate);
698 action = "cur grow up";
699 } else
700 action = "allocate";
701 break;
702 case ALLOC_MID:
703 if (strcmp(next->name, label_id->id) == 0) {
704 /* adjust next resource up */
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400705 rc = adjust_resource(next, next->start
706 - allocate, resource_size(next)
707 + allocate);
708 new_res = next;
709 action = "next grow up";
710 } else if (strcmp(res->name, label_id->id) == 0) {
711 action = "grow down";
712 } else
713 action = "allocate";
714 break;
715 case ALLOC_AFTER:
716 if (strcmp(res->name, label_id->id) == 0)
717 action = "grow down";
718 else
719 action = "allocate";
720 break;
721 default:
722 return n;
723 }
724
725 if (strcmp(action, "allocate") == 0) {
726 /* BLK allocate bottom up */
727 if (!is_pmem)
Dan Williams762d0672016-10-04 16:09:59 -0700728 valid.start += available - allocate;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400729
730 new_res = nvdimm_allocate_dpa(ndd, label_id,
Dan Williams762d0672016-10-04 16:09:59 -0700731 valid.start, allocate);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400732 if (!new_res)
733 rc = -EBUSY;
734 } else if (strcmp(action, "grow down") == 0) {
735 /* adjust current resource down */
736 rc = adjust_resource(res, res->start, resource_size(res)
737 + allocate);
Dan Williams1b40e092015-05-01 13:34:01 -0400738 if (rc == 0)
739 res->flags |= DPA_RESOURCE_ADJUSTED;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400740 }
741
742 if (!new_res)
743 new_res = res;
744
745 nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
746 action, loc, rc);
747
748 if (rc)
749 return n;
750
751 n -= allocate;
752 if (n) {
753 /*
754 * Retry scan with newly inserted resources.
755 * For example, if we did an ALLOC_BEFORE
756 * insertion there may also have been space
757 * available for an ALLOC_AFTER insertion, so we
758 * need to check this same resource again
759 */
760 goto retry;
761 } else
762 return 0;
763 }
764
Dan Williams1b40e092015-05-01 13:34:01 -0400765 /*
766 * If we allocated nothing in the BLK case it may be because we are in
767 * an initial "pmem-reserve pass". Only do an initial BLK allocation
768 * when none of the DPA space is reserved.
769 */
770 if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400771 return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
772 return n;
773}
774
Dan Williams1b40e092015-05-01 13:34:01 -0400775static int merge_dpa(struct nd_region *nd_region,
776 struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
777{
778 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
779 struct resource *res;
780
781 if (strncmp("pmem", label_id->id, 4) == 0)
782 return 0;
783 retry:
784 for_each_dpa_resource(ndd, res) {
785 int rc;
786 struct resource *next = res->sibling;
787 resource_size_t end = res->start + resource_size(res);
788
789 if (!next || strcmp(res->name, label_id->id) != 0
790 || strcmp(next->name, label_id->id) != 0
791 || end != next->start)
792 continue;
793 end += resource_size(next);
794 nvdimm_free_dpa(ndd, next);
795 rc = adjust_resource(res, res->start, end - res->start);
796 nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
797 if (rc)
798 return rc;
799 res->flags |= DPA_RESOURCE_ADJUSTED;
800 goto retry;
801 }
802
803 return 0;
804}
805
806static int __reserve_free_pmem(struct device *dev, void *data)
807{
808 struct nvdimm *nvdimm = data;
809 struct nd_region *nd_region;
810 struct nd_label_id label_id;
811 int i;
812
813 if (!is_nd_pmem(dev))
814 return 0;
815
816 nd_region = to_nd_region(dev);
817 if (nd_region->ndr_mappings == 0)
818 return 0;
819
820 memset(&label_id, 0, sizeof(label_id));
821 strcat(label_id.id, "pmem-reserve");
822 for (i = 0; i < nd_region->ndr_mappings; i++) {
823 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
824 resource_size_t n, rem = 0;
825
826 if (nd_mapping->nvdimm != nvdimm)
827 continue;
828
829 n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
830 if (n == 0)
831 return 0;
832 rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
833 dev_WARN_ONCE(&nd_region->dev, rem,
834 "pmem reserve underrun: %#llx of %#llx bytes\n",
835 (unsigned long long) n - rem,
836 (unsigned long long) n);
837 return rem ? -ENXIO : 0;
838 }
839
840 return 0;
841}
842
843static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
844 struct nd_mapping *nd_mapping)
845{
846 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
847 struct resource *res, *_res;
848
849 for_each_dpa_resource_safe(ndd, res, _res)
850 if (strcmp(res->name, "pmem-reserve") == 0)
851 nvdimm_free_dpa(ndd, res);
852}
853
854static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
855 struct nd_mapping *nd_mapping)
856{
857 struct nvdimm *nvdimm = nd_mapping->nvdimm;
858 int rc;
859
860 rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
861 __reserve_free_pmem);
862 if (rc)
863 release_free_pmem(nvdimm_bus, nd_mapping);
864 return rc;
865}
866
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400867/**
868 * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
869 * @nd_region: the set of dimms to allocate @n more bytes from
870 * @label_id: unique identifier for the namespace consuming this dpa range
871 * @n: number of bytes per-dimm to add to the existing allocation
872 *
873 * Assumes resources are ordered. For BLK regions, first consume
874 * BLK-only available DPA free space, then consume PMEM-aliased DPA
875 * space starting at the highest DPA. For PMEM regions start
876 * allocations from the start of an interleave set and end at the first
877 * BLK allocation or the end of the interleave set, whichever comes
878 * first.
879 */
880static int grow_dpa_allocation(struct nd_region *nd_region,
881 struct nd_label_id *label_id, resource_size_t n)
882{
Dan Williams1b40e092015-05-01 13:34:01 -0400883 struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
884 bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400885 int i;
886
887 for (i = 0; i < nd_region->ndr_mappings; i++) {
888 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams1b40e092015-05-01 13:34:01 -0400889 resource_size_t rem = n;
890 int rc, j;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400891
Dan Williams1b40e092015-05-01 13:34:01 -0400892 /*
893 * In the BLK case try once with all unallocated PMEM
894 * reserved, and once without
895 */
896 for (j = is_pmem; j < 2; j++) {
897 bool blk_only = j == 0;
898
899 if (blk_only) {
900 rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
901 if (rc)
902 return rc;
903 }
904 rem = scan_allocate(nd_region, nd_mapping,
905 label_id, rem);
906 if (blk_only)
907 release_free_pmem(nvdimm_bus, nd_mapping);
908
909 /* try again and allow encroachments into PMEM */
910 if (rem == 0)
911 break;
912 }
913
914 dev_WARN_ONCE(&nd_region->dev, rem,
915 "allocation underrun: %#llx of %#llx bytes\n",
916 (unsigned long long) n - rem,
917 (unsigned long long) n);
918 if (rem)
919 return -ENXIO;
920
921 rc = merge_dpa(nd_region, nd_mapping, label_id);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400922 if (rc)
923 return rc;
924 }
925
926 return 0;
927}
928
Dan Williams0e3b0d12016-10-06 23:13:15 -0700929static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400930 struct nd_namespace_pmem *nspm, resource_size_t size)
931{
932 struct resource *res = &nspm->nsio.res;
Dan Williams0e3b0d12016-10-06 23:13:15 -0700933 resource_size_t offset = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400934
Dan Williams0e3b0d12016-10-06 23:13:15 -0700935 if (size && !nspm->uuid) {
936 WARN_ON_ONCE(1);
937 size = 0;
938 }
939
940 if (size && nspm->uuid) {
941 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
942 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
943 struct nd_label_id label_id;
944 struct resource *res;
945
946 if (!ndd) {
947 size = 0;
948 goto out;
949 }
950
951 nd_label_gen_id(&label_id, nspm->uuid, 0);
952
953 /* calculate a spa offset from the dpa allocation offset */
954 for_each_dpa_resource(ndd, res)
955 if (strcmp(res->name, label_id.id) == 0) {
956 offset = (res->start - nd_mapping->start)
957 * nd_region->ndr_mappings;
958 goto out;
959 }
960
961 WARN_ON_ONCE(1);
962 size = 0;
963 }
964
965 out:
966 res->start = nd_region->ndr_start + offset;
967 res->end = res->start + size - 1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400968}
969
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +0300970static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
971{
972 if (!uuid) {
973 dev_dbg(dev, "%s: uuid not set\n", where);
974 return true;
975 }
976 return false;
977}
978
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400979static ssize_t __size_store(struct device *dev, unsigned long long val)
980{
981 resource_size_t allocated = 0, available = 0;
982 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams1f19b982017-01-09 17:30:49 -0800983 struct nd_namespace_common *ndns = to_ndns(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400984 struct nd_mapping *nd_mapping;
985 struct nvdimm_drvdata *ndd;
986 struct nd_label_id label_id;
987 u32 flags = 0, remainder;
Dan Williams9d032f42017-01-25 00:54:07 +0530988 int rc, i, id = -1;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400989 u8 *uuid = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400990
Dan Williams1f19b982017-01-09 17:30:49 -0800991 if (dev->driver || ndns->claim)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400992 return -EBUSY;
993
994 if (is_namespace_pmem(dev)) {
995 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
996
997 uuid = nspm->uuid;
Dan Williams9d032f42017-01-25 00:54:07 +0530998 id = nspm->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400999 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001000 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1001
1002 uuid = nsblk->uuid;
1003 flags = NSLABEL_FLAG_LOCAL;
Dan Williams9d032f42017-01-25 00:54:07 +05301004 id = nsblk->id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001005 }
1006
1007 /*
1008 * We need a uuid for the allocation-label and dimm(s) on which
1009 * to store the label.
1010 */
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001011 if (uuid_not_set(uuid, dev, __func__))
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001012 return -ENXIO;
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001013 if (nd_region->ndr_mappings == 0) {
1014 dev_dbg(dev, "%s: not associated with dimm(s)\n", __func__);
1015 return -ENXIO;
1016 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001017
1018 div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
1019 if (remainder) {
1020 dev_dbg(dev, "%llu is not %dK aligned\n", val,
1021 (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
1022 return -EINVAL;
1023 }
1024
1025 nd_label_gen_id(&label_id, uuid, flags);
1026 for (i = 0; i < nd_region->ndr_mappings; i++) {
1027 nd_mapping = &nd_region->mapping[i];
1028 ndd = to_ndd(nd_mapping);
1029
1030 /*
1031 * All dimms in an interleave set, or the base dimm for a blk
1032 * region, need to be enabled for the size to be changed.
1033 */
1034 if (!ndd)
1035 return -ENXIO;
1036
1037 allocated += nvdimm_allocated_dpa(ndd, &label_id);
1038 }
1039 available = nd_region_available_dpa(nd_region);
1040
1041 if (val > available + allocated)
1042 return -ENOSPC;
1043
1044 if (val == allocated)
1045 return 0;
1046
1047 val = div_u64(val, nd_region->ndr_mappings);
1048 allocated = div_u64(allocated, nd_region->ndr_mappings);
1049 if (val < allocated)
1050 rc = shrink_dpa_allocation(nd_region, &label_id,
1051 allocated - val);
1052 else
1053 rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
1054
1055 if (rc)
1056 return rc;
1057
1058 if (is_namespace_pmem(dev)) {
1059 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1060
Dan Williams0e3b0d12016-10-06 23:13:15 -07001061 nd_namespace_pmem_set_resource(nd_region, nspm,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001062 val * nd_region->ndr_mappings);
1063 }
1064
Dan Williams1f19b982017-01-09 17:30:49 -08001065 /*
1066 * Try to delete the namespace if we deleted all of its
Dan Williams9d032f42017-01-25 00:54:07 +05301067 * allocation, this is not the seed or 0th device for the
1068 * region, and it is not actively claimed by a btt, pfn, or dax
1069 * instance.
Dan Williams1f19b982017-01-09 17:30:49 -08001070 */
Dan Williams9d032f42017-01-25 00:54:07 +05301071 if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
Dan Williams1f19b982017-01-09 17:30:49 -08001072 nd_device_unregister(dev, ND_ASYNC);
1073
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001074 return rc;
1075}
1076
1077static ssize_t size_store(struct device *dev,
1078 struct device_attribute *attr, const char *buf, size_t len)
1079{
Dan Williamsf524bf22015-05-30 12:36:02 -04001080 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001081 unsigned long long val;
1082 u8 **uuid = NULL;
1083 int rc;
1084
1085 rc = kstrtoull(buf, 0, &val);
1086 if (rc)
1087 return rc;
1088
1089 device_lock(dev);
1090 nvdimm_bus_lock(dev);
1091 wait_nvdimm_bus_probe_idle(dev);
1092 rc = __size_store(dev, val);
Dan Williamsf524bf22015-05-30 12:36:02 -04001093 if (rc >= 0)
1094 rc = nd_namespace_label_update(nd_region, dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001095
1096 if (is_namespace_pmem(dev)) {
1097 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1098
1099 uuid = &nspm->uuid;
1100 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001101 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1102
1103 uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001104 }
1105
1106 if (rc == 0 && val == 0 && uuid) {
1107 /* setting size zero == 'delete namespace' */
1108 kfree(*uuid);
1109 *uuid = NULL;
1110 }
1111
1112 dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
1113 ? "fail" : "success", rc);
1114
1115 nvdimm_bus_unlock(dev);
1116 device_unlock(dev);
1117
Dan Williamsf524bf22015-05-30 12:36:02 -04001118 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001119}
1120
Dan Williams8c2f7e82015-06-25 04:20:04 -04001121resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001122{
Dan Williams8c2f7e82015-06-25 04:20:04 -04001123 struct device *dev = &ndns->dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001124
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001125 if (is_namespace_pmem(dev)) {
1126 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1127
Dan Williams8c2f7e82015-06-25 04:20:04 -04001128 return resource_size(&nspm->nsio.res);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001129 } else if (is_namespace_blk(dev)) {
Dan Williams8c2f7e82015-06-25 04:20:04 -04001130 return nd_namespace_blk_size(to_nd_namespace_blk(dev));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001131 } else if (is_namespace_io(dev)) {
1132 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1133
Dan Williams8c2f7e82015-06-25 04:20:04 -04001134 return resource_size(&nsio->res);
1135 } else
1136 WARN_ONCE(1, "unknown namespace type\n");
1137 return 0;
1138}
Dan Williams1b40e092015-05-01 13:34:01 -04001139
Dan Williams8c2f7e82015-06-25 04:20:04 -04001140resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
1141{
1142 resource_size_t size;
1143
1144 nvdimm_bus_lock(&ndns->dev);
1145 size = __nvdimm_namespace_capacity(ndns);
1146 nvdimm_bus_unlock(&ndns->dev);
1147
1148 return size;
1149}
1150EXPORT_SYMBOL(nvdimm_namespace_capacity);
1151
1152static ssize_t size_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
1154{
1155 return sprintf(buf, "%llu\n", (unsigned long long)
1156 nvdimm_namespace_capacity(to_ndns(dev)));
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001157}
Fabian Frederickb44fe762016-12-04 10:54:08 -08001158static DEVICE_ATTR(size, 0444, size_show, size_store);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001159
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001160static u8 *namespace_to_uuid(struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001161{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001162 if (is_namespace_pmem(dev)) {
1163 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1164
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001165 return nspm->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001166 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001167 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1168
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001169 return nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001170 } else
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001171 return ERR_PTR(-ENXIO);
1172}
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001173
Dan Williamsf95b4bc2016-09-21 18:16:21 -07001174static ssize_t uuid_show(struct device *dev,
1175 struct device_attribute *attr, char *buf)
1176{
1177 u8 *uuid = namespace_to_uuid(dev);
1178
1179 if (IS_ERR(uuid))
1180 return PTR_ERR(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001181 if (uuid)
1182 return sprintf(buf, "%pUb\n", uuid);
1183 return sprintf(buf, "\n");
1184}
1185
1186/**
1187 * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
1188 * @nd_region: parent region so we can updates all dimms in the set
1189 * @dev: namespace type for generating label_id
1190 * @new_uuid: incoming uuid
1191 * @old_uuid: reference to the uuid storage location in the namespace object
1192 */
1193static int namespace_update_uuid(struct nd_region *nd_region,
1194 struct device *dev, u8 *new_uuid, u8 **old_uuid)
1195{
1196 u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
1197 struct nd_label_id old_label_id;
1198 struct nd_label_id new_label_id;
Dan Williamsf524bf22015-05-30 12:36:02 -04001199 int i;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001200
Dan Williamsf524bf22015-05-30 12:36:02 -04001201 if (!nd_is_uuid_unique(dev, new_uuid))
1202 return -EINVAL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001203
1204 if (*old_uuid == NULL)
1205 goto out;
1206
Dan Williamsf524bf22015-05-30 12:36:02 -04001207 /*
1208 * If we've already written a label with this uuid, then it's
1209 * too late to rename because we can't reliably update the uuid
1210 * without losing the old namespace. Userspace must delete this
1211 * namespace to abandon the old uuid.
1212 */
1213 for (i = 0; i < nd_region->ndr_mappings; i++) {
1214 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1215
1216 /*
1217 * This check by itself is sufficient because old_uuid
1218 * would be NULL above if this uuid did not exist in the
1219 * currently written set.
1220 *
1221 * FIXME: can we delete uuid with zero dpa allocated?
1222 */
Dan Williamsae8219f2016-09-19 16:04:21 -07001223 if (list_empty(&nd_mapping->labels))
Dan Williamsf524bf22015-05-30 12:36:02 -04001224 return -EBUSY;
1225 }
1226
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001227 nd_label_gen_id(&old_label_id, *old_uuid, flags);
1228 nd_label_gen_id(&new_label_id, new_uuid, flags);
1229 for (i = 0; i < nd_region->ndr_mappings; i++) {
1230 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1231 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1232 struct resource *res;
1233
1234 for_each_dpa_resource(ndd, res)
1235 if (strcmp(res->name, old_label_id.id) == 0)
1236 sprintf((void *) res->name, "%s",
1237 new_label_id.id);
1238 }
1239 kfree(*old_uuid);
1240 out:
1241 *old_uuid = new_uuid;
1242 return 0;
1243}
1244
1245static ssize_t uuid_store(struct device *dev,
1246 struct device_attribute *attr, const char *buf, size_t len)
1247{
1248 struct nd_region *nd_region = to_nd_region(dev->parent);
1249 u8 *uuid = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001250 ssize_t rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001251 u8 **ns_uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001252
1253 if (is_namespace_pmem(dev)) {
1254 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1255
1256 ns_uuid = &nspm->uuid;
1257 } else if (is_namespace_blk(dev)) {
Dan Williams1b40e092015-05-01 13:34:01 -04001258 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1259
1260 ns_uuid = &nsblk->uuid;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001261 } else
1262 return -ENXIO;
1263
1264 device_lock(dev);
1265 nvdimm_bus_lock(dev);
1266 wait_nvdimm_bus_probe_idle(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001267 if (to_ndns(dev)->claim)
1268 rc = -EBUSY;
1269 if (rc >= 0)
1270 rc = nd_uuid_store(dev, &uuid, buf, len);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001271 if (rc >= 0)
1272 rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
Dan Williamsf524bf22015-05-30 12:36:02 -04001273 if (rc >= 0)
1274 rc = nd_namespace_label_update(nd_region, dev);
1275 else
1276 kfree(uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001277 dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
1278 rc, buf, buf[len - 1] == '\n' ? "" : "\n");
1279 nvdimm_bus_unlock(dev);
1280 device_unlock(dev);
1281
Dan Williamsf524bf22015-05-30 12:36:02 -04001282 return rc < 0 ? rc : len;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001283}
1284static DEVICE_ATTR_RW(uuid);
1285
1286static ssize_t resource_show(struct device *dev,
1287 struct device_attribute *attr, char *buf)
1288{
1289 struct resource *res;
1290
1291 if (is_namespace_pmem(dev)) {
1292 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1293
1294 res = &nspm->nsio.res;
1295 } else if (is_namespace_io(dev)) {
1296 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
1297
1298 res = &nsio->res;
1299 } else
1300 return -ENXIO;
1301
1302 /* no address to convey if the namespace has no allocation */
1303 if (resource_size(res) == 0)
1304 return -ENXIO;
1305 return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
1306}
1307static DEVICE_ATTR_RO(resource);
1308
Dan Williamsf979b132017-06-04 12:12:07 +09001309static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
Vishal Vermafcae6952015-06-25 04:22:39 -04001310 4096, 4104, 4160, 4224, 0 };
Dan Williams1b40e092015-05-01 13:34:01 -04001311
Dan Williamsf979b132017-06-04 12:12:07 +09001312static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
1313
Dan Williams1b40e092015-05-01 13:34:01 -04001314static ssize_t sector_size_show(struct device *dev,
1315 struct device_attribute *attr, char *buf)
1316{
Dan Williamsf979b132017-06-04 12:12:07 +09001317 if (is_namespace_blk(dev)) {
1318 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
Dan Williams1b40e092015-05-01 13:34:01 -04001319
Dan Williamsf979b132017-06-04 12:12:07 +09001320 return nd_sector_size_show(nsblk->lbasize,
1321 blk_lbasize_supported, buf);
1322 }
Dan Williams1b40e092015-05-01 13:34:01 -04001323
Dan Williamsf979b132017-06-04 12:12:07 +09001324 if (is_namespace_pmem(dev)) {
1325 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1326
1327 return nd_sector_size_show(nspm->lbasize,
1328 pmem_lbasize_supported, buf);
1329 }
1330 return -ENXIO;
Dan Williams1b40e092015-05-01 13:34:01 -04001331}
1332
1333static ssize_t sector_size_store(struct device *dev,
1334 struct device_attribute *attr, const char *buf, size_t len)
1335{
Dan Williamsf524bf22015-05-30 12:36:02 -04001336 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williamsf979b132017-06-04 12:12:07 +09001337 const unsigned long *supported;
1338 unsigned long *lbasize;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001339 ssize_t rc = 0;
Dan Williams1b40e092015-05-01 13:34:01 -04001340
Dan Williamsf979b132017-06-04 12:12:07 +09001341 if (is_namespace_blk(dev)) {
1342 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1343
1344 lbasize = &nsblk->lbasize;
1345 supported = blk_lbasize_supported;
1346 } else if (is_namespace_pmem(dev)) {
1347 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1348
1349 lbasize = &nspm->lbasize;
1350 supported = pmem_lbasize_supported;
1351 } else
Dan Williams1b40e092015-05-01 13:34:01 -04001352 return -ENXIO;
1353
1354 device_lock(dev);
1355 nvdimm_bus_lock(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001356 if (to_ndns(dev)->claim)
1357 rc = -EBUSY;
1358 if (rc >= 0)
Dan Williamsf979b132017-06-04 12:12:07 +09001359 rc = nd_sector_size_store(dev, buf, lbasize, supported);
Dan Williamsf524bf22015-05-30 12:36:02 -04001360 if (rc >= 0)
1361 rc = nd_namespace_label_update(nd_region, dev);
1362 dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
1363 rc, rc < 0 ? "tried" : "wrote", buf,
1364 buf[len - 1] == '\n' ? "" : "\n");
Dan Williams1b40e092015-05-01 13:34:01 -04001365 nvdimm_bus_unlock(dev);
1366 device_unlock(dev);
1367
1368 return rc ? rc : len;
1369}
1370static DEVICE_ATTR_RW(sector_size);
1371
Dan Williams0ba1c632015-05-30 12:35:36 -04001372static ssize_t dpa_extents_show(struct device *dev,
1373 struct device_attribute *attr, char *buf)
1374{
1375 struct nd_region *nd_region = to_nd_region(dev->parent);
1376 struct nd_label_id label_id;
1377 int count = 0, i;
1378 u8 *uuid = NULL;
1379 u32 flags = 0;
1380
1381 nvdimm_bus_lock(dev);
1382 if (is_namespace_pmem(dev)) {
1383 struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
1384
1385 uuid = nspm->uuid;
1386 flags = 0;
1387 } else if (is_namespace_blk(dev)) {
1388 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
1389
1390 uuid = nsblk->uuid;
1391 flags = NSLABEL_FLAG_LOCAL;
1392 }
1393
1394 if (!uuid)
1395 goto out;
1396
1397 nd_label_gen_id(&label_id, uuid, flags);
1398 for (i = 0; i < nd_region->ndr_mappings; i++) {
1399 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1400 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1401 struct resource *res;
1402
1403 for_each_dpa_resource(ndd, res)
1404 if (strcmp(res->name, label_id.id) == 0)
1405 count++;
1406 }
1407 out:
1408 nvdimm_bus_unlock(dev);
1409
1410 return sprintf(buf, "%d\n", count);
1411}
1412static DEVICE_ATTR_RO(dpa_extents);
1413
Dan Williams8c2f7e82015-06-25 04:20:04 -04001414static ssize_t holder_show(struct device *dev,
1415 struct device_attribute *attr, char *buf)
1416{
1417 struct nd_namespace_common *ndns = to_ndns(dev);
1418 ssize_t rc;
1419
1420 device_lock(dev);
1421 rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
1422 device_unlock(dev);
1423
1424 return rc;
1425}
1426static DEVICE_ATTR_RO(holder);
1427
Dan Williams0731de02015-12-14 15:34:15 -08001428static ssize_t mode_show(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1430{
1431 struct nd_namespace_common *ndns = to_ndns(dev);
1432 struct device *claim;
1433 char *mode;
1434 ssize_t rc;
1435
1436 device_lock(dev);
1437 claim = ndns->claim;
Dan Williams9c412422016-01-23 15:34:10 -08001438 if (claim && is_nd_btt(claim))
Dan Williams0731de02015-12-14 15:34:15 -08001439 mode = "safe";
Dan Williams9c412422016-01-23 15:34:10 -08001440 else if (claim && is_nd_pfn(claim))
1441 mode = "memory";
Dan Williamscd034122016-03-11 10:15:36 -08001442 else if (claim && is_nd_dax(claim))
1443 mode = "dax";
Dan Williams9c412422016-01-23 15:34:10 -08001444 else if (!claim && pmem_should_map_pages(dev))
1445 mode = "memory";
Dan Williams0731de02015-12-14 15:34:15 -08001446 else
1447 mode = "raw";
1448 rc = sprintf(buf, "%s\n", mode);
1449 device_unlock(dev);
1450
1451 return rc;
1452}
1453static DEVICE_ATTR_RO(mode);
1454
Dan Williams8c2f7e82015-06-25 04:20:04 -04001455static ssize_t force_raw_store(struct device *dev,
1456 struct device_attribute *attr, const char *buf, size_t len)
1457{
1458 bool force_raw;
1459 int rc = strtobool(buf, &force_raw);
1460
1461 if (rc)
1462 return rc;
1463
1464 to_ndns(dev)->force_raw = force_raw;
1465 return len;
1466}
1467
1468static ssize_t force_raw_show(struct device *dev,
1469 struct device_attribute *attr, char *buf)
1470{
1471 return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
1472}
1473static DEVICE_ATTR_RW(force_raw);
1474
Dan Williams3d880022015-05-31 15:02:11 -04001475static struct attribute *nd_namespace_attributes[] = {
1476 &dev_attr_nstype.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001477 &dev_attr_size.attr,
Dan Williams0731de02015-12-14 15:34:15 -08001478 &dev_attr_mode.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001479 &dev_attr_uuid.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001480 &dev_attr_holder.attr,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001481 &dev_attr_resource.attr,
1482 &dev_attr_alt_name.attr,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001483 &dev_attr_force_raw.attr,
Dan Williams1b40e092015-05-01 13:34:01 -04001484 &dev_attr_sector_size.attr,
Dan Williams0ba1c632015-05-30 12:35:36 -04001485 &dev_attr_dpa_extents.attr,
Dan Williams3d880022015-05-31 15:02:11 -04001486 NULL,
1487};
1488
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001489static umode_t namespace_visible(struct kobject *kobj,
1490 struct attribute *a, int n)
1491{
1492 struct device *dev = container_of(kobj, struct device, kobj);
1493
1494 if (a == &dev_attr_resource.attr) {
1495 if (is_namespace_blk(dev))
1496 return 0;
1497 return a->mode;
1498 }
1499
1500 if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
1501 if (a == &dev_attr_size.attr)
Fabian Frederickb44fe762016-12-04 10:54:08 -08001502 return 0644;
Dan Williams1b40e092015-05-01 13:34:01 -04001503
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001504 return a->mode;
1505 }
1506
Dan Williams8c2f7e82015-06-25 04:20:04 -04001507 if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
1508 || a == &dev_attr_holder.attr
Dan Williams0731de02015-12-14 15:34:15 -08001509 || a == &dev_attr_force_raw.attr
1510 || a == &dev_attr_mode.attr)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001511 return a->mode;
1512
1513 return 0;
1514}
1515
Dan Williams3d880022015-05-31 15:02:11 -04001516static struct attribute_group nd_namespace_attribute_group = {
1517 .attrs = nd_namespace_attributes,
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001518 .is_visible = namespace_visible,
Dan Williams3d880022015-05-31 15:02:11 -04001519};
1520
1521static const struct attribute_group *nd_namespace_attribute_groups[] = {
1522 &nd_device_attribute_group,
1523 &nd_namespace_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001524 &nd_numa_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001525 NULL,
1526};
1527
Dan Williams8c2f7e82015-06-25 04:20:04 -04001528struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
1529{
1530 struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
Dan Williamse1455742015-07-30 17:57:47 -04001531 struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
Dan Williamscd034122016-03-11 10:15:36 -08001532 struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001533 struct nd_namespace_common *ndns = NULL;
Dan Williams8c2f7e82015-06-25 04:20:04 -04001534 resource_size_t size;
1535
Dan Williamscd034122016-03-11 10:15:36 -08001536 if (nd_btt || nd_pfn || nd_dax) {
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001537 if (nd_btt)
Dan Williamse1455742015-07-30 17:57:47 -04001538 ndns = nd_btt->ndns;
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001539 else if (nd_pfn)
Dan Williamse1455742015-07-30 17:57:47 -04001540 ndns = nd_pfn->ndns;
Dan Williamscd034122016-03-11 10:15:36 -08001541 else if (nd_dax)
1542 ndns = nd_dax->nd_pfn.ndns;
Dan Williamse1455742015-07-30 17:57:47 -04001543
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001544 if (!ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -04001545 return ERR_PTR(-ENODEV);
1546
1547 /*
1548 * Flush any in-progess probes / removals in the driver
1549 * for the raw personality of this namespace.
1550 */
1551 device_lock(&ndns->dev);
1552 device_unlock(&ndns->dev);
1553 if (ndns->dev.driver) {
1554 dev_dbg(&ndns->dev, "is active, can't bind %s\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001555 dev_name(dev));
Dan Williams8c2f7e82015-06-25 04:20:04 -04001556 return ERR_PTR(-EBUSY);
1557 }
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001558 if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
Dan Williams8c2f7e82015-06-25 04:20:04 -04001559 "host (%s) vs claim (%s) mismatch\n",
Dan Williams0bfb8dd2016-04-13 17:06:48 -07001560 dev_name(dev),
Dan Williams8c2f7e82015-06-25 04:20:04 -04001561 dev_name(ndns->claim)))
1562 return ERR_PTR(-ENXIO);
1563 } else {
1564 ndns = to_ndns(dev);
1565 if (ndns->claim) {
1566 dev_dbg(dev, "claimed by %s, failing probe\n",
1567 dev_name(ndns->claim));
1568
1569 return ERR_PTR(-ENXIO);
1570 }
1571 }
1572
1573 size = nvdimm_namespace_capacity(ndns);
1574 if (size < ND_MIN_NAMESPACE_SIZE) {
1575 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
1576 &size, ND_MIN_NAMESPACE_SIZE);
1577 return ERR_PTR(-ENODEV);
1578 }
1579
1580 if (is_namespace_pmem(&ndns->dev)) {
1581 struct nd_namespace_pmem *nspm;
1582
1583 nspm = to_nd_namespace_pmem(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001584 if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
Dan Williams8c2f7e82015-06-25 04:20:04 -04001585 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001586 } else if (is_namespace_blk(&ndns->dev)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001587 struct nd_namespace_blk *nsblk;
1588
1589 nsblk = to_nd_namespace_blk(&ndns->dev);
Dmitry Krivenokbd26d0d2015-12-02 00:48:12 +03001590 if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
1591 return ERR_PTR(-ENODEV);
1592 if (!nsblk->lbasize) {
1593 dev_dbg(&ndns->dev, "%s: sector size not set\n",
1594 __func__);
1595 return ERR_PTR(-ENODEV);
1596 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001597 if (!nd_namespace_blk_validate(nsblk))
1598 return ERR_PTR(-ENODEV);
Dan Williams8c2f7e82015-06-25 04:20:04 -04001599 }
1600
1601 return ndns;
1602}
1603EXPORT_SYMBOL(nvdimm_namespace_common_probe);
1604
Dan Williams3d880022015-05-31 15:02:11 -04001605static struct device **create_namespace_io(struct nd_region *nd_region)
1606{
1607 struct nd_namespace_io *nsio;
1608 struct device *dev, **devs;
1609 struct resource *res;
1610
1611 nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
1612 if (!nsio)
1613 return NULL;
1614
1615 devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
1616 if (!devs) {
1617 kfree(nsio);
1618 return NULL;
1619 }
1620
Dan Williams8c2f7e82015-06-25 04:20:04 -04001621 dev = &nsio->common.dev;
Dan Williams3d880022015-05-31 15:02:11 -04001622 dev->type = &namespace_io_device_type;
1623 dev->parent = &nd_region->dev;
1624 res = &nsio->res;
1625 res->name = dev_name(&nd_region->dev);
1626 res->flags = IORESOURCE_MEM;
1627 res->start = nd_region->ndr_start;
1628 res->end = res->start + nd_region->ndr_size - 1;
1629
1630 devs[0] = dev;
1631 return devs;
1632}
1633
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001634static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
1635 u64 cookie, u16 pos)
1636{
1637 struct nd_namespace_label *found = NULL;
1638 int i;
1639
1640 for (i = 0; i < nd_region->ndr_mappings; i++) {
1641 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001642 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001643 bool found_uuid = false;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001644
Dan Williamsae8219f2016-09-19 16:04:21 -07001645 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1646 struct nd_namespace_label *nd_label = label_ent->label;
1647 u16 position, nlabel;
1648 u64 isetcookie;
1649
1650 if (!nd_label)
1651 continue;
1652 isetcookie = __le64_to_cpu(nd_label->isetcookie);
1653 position = __le16_to_cpu(nd_label->position);
1654 nlabel = __le16_to_cpu(nd_label->nlabel);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001655
1656 if (isetcookie != cookie)
1657 continue;
1658
1659 if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
1660 continue;
1661
1662 if (found_uuid) {
1663 dev_dbg(to_ndd(nd_mapping)->dev,
1664 "%s duplicate entry for uuid\n",
1665 __func__);
1666 return false;
1667 }
1668 found_uuid = true;
1669 if (nlabel != nd_region->ndr_mappings)
1670 continue;
1671 if (position != pos)
1672 continue;
1673 found = nd_label;
1674 break;
1675 }
1676 if (found)
1677 break;
1678 }
1679 return found != NULL;
1680}
1681
1682static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
1683{
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001684 int i;
1685
1686 if (!pmem_id)
1687 return -ENODEV;
1688
1689 for (i = 0; i < nd_region->ndr_mappings; i++) {
1690 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
Dan Williams0e3b0d12016-10-06 23:13:15 -07001691 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williamsae8219f2016-09-19 16:04:21 -07001692 struct nd_namespace_label *nd_label = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001693 u64 hw_start, hw_end, pmem_start, pmem_end;
Dan Williamsae8219f2016-09-19 16:04:21 -07001694 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001695
Dan Williams9cf8bd52016-12-15 20:04:31 -08001696 lockdep_assert_held(&nd_mapping->lock);
Dan Williamsae8219f2016-09-19 16:04:21 -07001697 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
1698 nd_label = label_ent->label;
1699 if (!nd_label)
1700 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001701 if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
1702 break;
Dan Williamsae8219f2016-09-19 16:04:21 -07001703 nd_label = NULL;
1704 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001705
1706 if (!nd_label) {
1707 WARN_ON(1);
1708 return -EINVAL;
1709 }
1710
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001711 /*
1712 * Check that this label is compliant with the dpa
1713 * range published in NFIT
1714 */
1715 hw_start = nd_mapping->start;
1716 hw_end = hw_start + nd_mapping->size;
Dan Williamsae8219f2016-09-19 16:04:21 -07001717 pmem_start = __le64_to_cpu(nd_label->dpa);
1718 pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
Dan Williams0e3b0d12016-10-06 23:13:15 -07001719 if (pmem_start >= hw_start && pmem_start < hw_end
1720 && pmem_end <= hw_end && pmem_end > hw_start)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001721 /* pass */;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001722 else {
1723 dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
1724 dev_name(ndd->dev), nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001725 return -EINVAL;
Dan Williams0e3b0d12016-10-06 23:13:15 -07001726 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001727
Dan Williams8a5f50d2016-09-22 15:42:59 -07001728 /* move recently validated label to the front of the list */
1729 list_move(&label_ent->list, &nd_mapping->labels);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001730 }
1731 return 0;
1732}
1733
1734/**
Dan Williams8a5f50d2016-09-22 15:42:59 -07001735 * create_namespace_pmem - validate interleave set labelling, retrieve label0
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001736 * @nd_region: region with mappings to validate
Dan Williams8a5f50d2016-09-22 15:42:59 -07001737 * @nspm: target namespace to create
1738 * @nd_label: target pmem namespace label to evaluate
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001739 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001740struct device *create_namespace_pmem(struct nd_region *nd_region,
Dan Williamsc12c48c2017-06-04 10:59:15 +09001741 struct nd_namespace_index *nsindex,
Dan Williams8a5f50d2016-09-22 15:42:59 -07001742 struct nd_namespace_label *nd_label)
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001743{
Dan Williamsc12c48c2017-06-04 10:59:15 +09001744 u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
Dan Williams86ef58a2017-02-28 18:32:48 -08001745 u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
Dan Williamsae8219f2016-09-19 16:04:21 -07001746 struct nd_label_ent *label_ent;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001747 struct nd_namespace_pmem *nspm;
Dan Williamsae8219f2016-09-19 16:04:21 -07001748 struct nd_mapping *nd_mapping;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001749 resource_size_t size = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001750 struct resource *res;
1751 struct device *dev;
Dan Williamsae8219f2016-09-19 16:04:21 -07001752 int rc = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001753 u16 i;
1754
Dan Williams47652182016-09-15 18:08:05 -07001755 if (cookie == 0) {
1756 dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
Dan Williams8a5f50d2016-09-22 15:42:59 -07001757 return ERR_PTR(-ENXIO);
Dan Williams47652182016-09-15 18:08:05 -07001758 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001759
Dan Williams8a5f50d2016-09-22 15:42:59 -07001760 if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
1761 dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
1762 nd_label->uuid);
Dan Williams86ef58a2017-02-28 18:32:48 -08001763 if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
1764 return ERR_PTR(-EAGAIN);
1765
1766 dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
1767 nd_label->uuid);
Dan Williamsae8219f2016-09-19 16:04:21 -07001768 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001769
Dan Williams8a5f50d2016-09-22 15:42:59 -07001770 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1771 if (!nspm)
1772 return ERR_PTR(-ENOMEM);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001773
Dan Williams0e3b0d12016-10-06 23:13:15 -07001774 nspm->id = -1;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001775 dev = &nspm->nsio.common.dev;
1776 dev->type = &namespace_pmem_device_type;
1777 dev->parent = &nd_region->dev;
1778 res = &nspm->nsio.res;
1779 res->name = dev_name(&nd_region->dev);
1780 res->flags = IORESOURCE_MEM;
1781
Dan Williams86ef58a2017-02-28 18:32:48 -08001782 for (i = 0; i < nd_region->ndr_mappings; i++) {
1783 if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
1784 continue;
1785 if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
1786 continue;
1787 break;
1788 }
1789
Dan Williams8a5f50d2016-09-22 15:42:59 -07001790 if (i < nd_region->ndr_mappings) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07001791 struct nvdimm_drvdata *ndd = to_ndd(&nd_region->mapping[i]);
1792
Dan Williams8a5f50d2016-09-22 15:42:59 -07001793 /*
1794 * Give up if we don't find an instance of a uuid at each
1795 * position (from 0 to nd_region->ndr_mappings - 1), or if we
1796 * find a dimm with two instances of the same uuid.
1797 */
Dan Williams0e3b0d12016-10-06 23:13:15 -07001798 dev_err(&nd_region->dev, "%s missing label for %pUb\n",
1799 dev_name(ndd->dev), nd_label->uuid);
Dan Williams8a5f50d2016-09-22 15:42:59 -07001800 rc = -EINVAL;
Dan Williamsae8219f2016-09-19 16:04:21 -07001801 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07001802 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001803
1804 /*
1805 * Fix up each mapping's 'labels' to have the validated pmem label for
1806 * that position at labels[0], and NULL at labels[1]. In the process,
1807 * check that the namespace aligns with interleave-set. We know
1808 * that it does not overlap with any blk namespaces by virtue of
1809 * the dimm being enabled (i.e. nd_label_reserve_dpa()
1810 * succeeded).
1811 */
Dan Williams8a5f50d2016-09-22 15:42:59 -07001812 rc = select_pmem_id(nd_region, nd_label->uuid);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001813 if (rc)
1814 goto err;
1815
1816 /* Calculate total size and populate namespace properties from label0 */
1817 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williamsae8219f2016-09-19 16:04:21 -07001818 struct nd_namespace_label *label0;
1819
1820 nd_mapping = &nd_region->mapping[i];
Dan Williamsae8219f2016-09-19 16:04:21 -07001821 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1822 typeof(*label_ent), list);
1823 label0 = label_ent ? label_ent->label : 0;
Dan Williamsae8219f2016-09-19 16:04:21 -07001824
1825 if (!label0) {
1826 WARN_ON(1);
1827 continue;
1828 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001829
1830 size += __le64_to_cpu(label0->rawsize);
1831 if (__le16_to_cpu(label0->position) != 0)
1832 continue;
1833 WARN_ON(nspm->alt_name || nspm->uuid);
1834 nspm->alt_name = kmemdup((void __force *) label0->name,
1835 NSLABEL_NAME_LEN, GFP_KERNEL);
1836 nspm->uuid = kmemdup((void __force *) label0->uuid,
1837 NSLABEL_UUID_LEN, GFP_KERNEL);
Dan Williamsf979b132017-06-04 12:12:07 +09001838 nspm->lbasize = __le64_to_cpu(label0->lbasize);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001839 }
1840
1841 if (!nspm->alt_name || !nspm->uuid) {
1842 rc = -ENOMEM;
1843 goto err;
1844 }
1845
Dan Williams0e3b0d12016-10-06 23:13:15 -07001846 nd_namespace_pmem_set_resource(nd_region, nspm, size);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001847
Dan Williams8a5f50d2016-09-22 15:42:59 -07001848 return dev;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001849 err:
Dan Williams8a5f50d2016-09-22 15:42:59 -07001850 namespace_pmem_release(dev);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001851 switch (rc) {
1852 case -EINVAL:
1853 dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
1854 break;
1855 case -ENODEV:
1856 dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
1857 break;
1858 default:
1859 dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
1860 __func__, rc);
1861 break;
1862 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07001863 return ERR_PTR(rc);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04001864}
1865
Dan Williams1b40e092015-05-01 13:34:01 -04001866struct resource *nsblk_add_resource(struct nd_region *nd_region,
1867 struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
1868 resource_size_t start)
1869{
1870 struct nd_label_id label_id;
1871 struct resource *res;
1872
1873 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1874 res = krealloc(nsblk->res,
1875 sizeof(void *) * (nsblk->num_resources + 1),
1876 GFP_KERNEL);
1877 if (!res)
1878 return NULL;
1879 nsblk->res = (struct resource **) res;
1880 for_each_dpa_resource(ndd, res)
1881 if (strcmp(res->name, label_id.id) == 0
1882 && res->start == start) {
1883 nsblk->res[nsblk->num_resources++] = res;
1884 return res;
1885 }
1886 return NULL;
1887}
1888
1889static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
1890{
1891 struct nd_namespace_blk *nsblk;
1892 struct device *dev;
1893
1894 if (!is_nd_blk(&nd_region->dev))
1895 return NULL;
1896
1897 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
1898 if (!nsblk)
1899 return NULL;
1900
Dan Williams8c2f7e82015-06-25 04:20:04 -04001901 dev = &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001902 dev->type = &namespace_blk_device_type;
1903 nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1904 if (nsblk->id < 0) {
1905 kfree(nsblk);
1906 return NULL;
1907 }
1908 dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
1909 dev->parent = &nd_region->dev;
1910 dev->groups = nd_namespace_attribute_groups;
1911
Dan Williams8c2f7e82015-06-25 04:20:04 -04001912 return &nsblk->common.dev;
Dan Williams1b40e092015-05-01 13:34:01 -04001913}
1914
Dan Williams98a29c32016-09-30 15:28:27 -07001915static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
1916{
1917 struct nd_namespace_pmem *nspm;
1918 struct resource *res;
1919 struct device *dev;
1920
1921 if (!is_nd_pmem(&nd_region->dev))
1922 return NULL;
1923
1924 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
1925 if (!nspm)
1926 return NULL;
1927
1928 dev = &nspm->nsio.common.dev;
1929 dev->type = &namespace_pmem_device_type;
1930 dev->parent = &nd_region->dev;
1931 res = &nspm->nsio.res;
1932 res->name = dev_name(&nd_region->dev);
1933 res->flags = IORESOURCE_MEM;
1934
1935 nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
1936 if (nspm->id < 0) {
1937 kfree(nspm);
1938 return NULL;
1939 }
1940 dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
1941 dev->parent = &nd_region->dev;
1942 dev->groups = nd_namespace_attribute_groups;
1943 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
1944
1945 return dev;
1946}
1947
1948void nd_region_create_ns_seed(struct nd_region *nd_region)
Dan Williams1b40e092015-05-01 13:34:01 -04001949{
1950 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
Dan Williams98a29c32016-09-30 15:28:27 -07001951
1952 if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
1953 return;
1954
1955 if (is_nd_blk(&nd_region->dev))
1956 nd_region->ns_seed = nd_namespace_blk_create(nd_region);
1957 else
1958 nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
1959
Dan Williams1b40e092015-05-01 13:34:01 -04001960 /*
1961 * Seed creation failures are not fatal, provisioning is simply
1962 * disabled until memory becomes available
1963 */
1964 if (!nd_region->ns_seed)
Dan Williams98a29c32016-09-30 15:28:27 -07001965 dev_err(&nd_region->dev, "failed to create %s namespace\n",
1966 is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
Dan Williams1b40e092015-05-01 13:34:01 -04001967 else
1968 nd_device_register(nd_region->ns_seed);
1969}
1970
Dan Williamscd034122016-03-11 10:15:36 -08001971void nd_region_create_dax_seed(struct nd_region *nd_region)
1972{
1973 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1974 nd_region->dax_seed = nd_dax_create(nd_region);
1975 /*
1976 * Seed creation failures are not fatal, provisioning is simply
1977 * disabled until memory becomes available
1978 */
1979 if (!nd_region->dax_seed)
1980 dev_err(&nd_region->dev, "failed to create dax namespace\n");
1981}
1982
Dan Williams2dc43332015-12-13 11:41:36 -08001983void nd_region_create_pfn_seed(struct nd_region *nd_region)
1984{
1985 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1986 nd_region->pfn_seed = nd_pfn_create(nd_region);
1987 /*
1988 * Seed creation failures are not fatal, provisioning is simply
1989 * disabled until memory becomes available
1990 */
1991 if (!nd_region->pfn_seed)
1992 dev_err(&nd_region->dev, "failed to create pfn namespace\n");
1993}
1994
Dan Williams8c2f7e82015-06-25 04:20:04 -04001995void nd_region_create_btt_seed(struct nd_region *nd_region)
1996{
1997 WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
1998 nd_region->btt_seed = nd_btt_create(nd_region);
1999 /*
2000 * Seed creation failures are not fatal, provisioning is simply
2001 * disabled until memory becomes available
2002 */
2003 if (!nd_region->btt_seed)
2004 dev_err(&nd_region->dev, "failed to create btt namespace\n");
2005}
2006
Dan Williams8a5f50d2016-09-22 15:42:59 -07002007static int add_namespace_resource(struct nd_region *nd_region,
2008 struct nd_namespace_label *nd_label, struct device **devs,
2009 int count)
Dan Williams1b40e092015-05-01 13:34:01 -04002010{
Dan Williams8a5f50d2016-09-22 15:42:59 -07002011 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
Dan Williamsae8219f2016-09-19 16:04:21 -07002012 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002013 int i;
2014
2015 for (i = 0; i < count; i++) {
2016 u8 *uuid = namespace_to_uuid(devs[i]);
2017 struct resource *res;
2018
2019 if (IS_ERR_OR_NULL(uuid)) {
2020 WARN_ON(1);
2021 continue;
2022 }
2023
2024 if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
2025 continue;
2026 if (is_namespace_blk(devs[i])) {
2027 res = nsblk_add_resource(nd_region, ndd,
2028 to_nd_namespace_blk(devs[i]),
2029 __le64_to_cpu(nd_label->dpa));
2030 if (!res)
2031 return -ENXIO;
2032 nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
2033 } else {
2034 dev_err(&nd_region->dev,
2035 "error: conflicting extents for uuid: %pUb\n",
2036 nd_label->uuid);
2037 return -ENXIO;
2038 }
2039 break;
2040 }
2041
2042 return i;
2043}
2044
2045struct device *create_namespace_blk(struct nd_region *nd_region,
2046 struct nd_namespace_label *nd_label, int count)
2047{
2048
2049 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2050 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002051 struct nd_namespace_blk *nsblk;
Nicolas Iooss238b3232016-11-26 20:18:04 +01002052 char name[NSLABEL_NAME_LEN];
Dan Williams8a5f50d2016-09-22 15:42:59 -07002053 struct device *dev = NULL;
2054 struct resource *res;
2055
2056 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2057 if (!nsblk)
2058 return ERR_PTR(-ENOMEM);
2059 dev = &nsblk->common.dev;
2060 dev->type = &namespace_blk_device_type;
2061 dev->parent = &nd_region->dev;
2062 nsblk->id = -1;
2063 nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
2064 nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
2065 GFP_KERNEL);
2066 if (!nsblk->uuid)
2067 goto blk_err;
2068 memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
2069 if (name[0])
2070 nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
2071 GFP_KERNEL);
2072 res = nsblk_add_resource(nd_region, ndd, nsblk,
2073 __le64_to_cpu(nd_label->dpa));
2074 if (!res)
2075 goto blk_err;
2076 nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
2077 return dev;
2078 blk_err:
2079 namespace_blk_release(dev);
2080 return ERR_PTR(-ENXIO);
2081}
2082
Dan Williams6ff3e912016-10-05 14:04:15 -07002083static int cmp_dpa(const void *a, const void *b)
2084{
2085 const struct device *dev_a = *(const struct device **) a;
2086 const struct device *dev_b = *(const struct device **) b;
2087 struct nd_namespace_blk *nsblk_a, *nsblk_b;
2088 struct nd_namespace_pmem *nspm_a, *nspm_b;
2089
2090 if (is_namespace_io(dev_a))
2091 return 0;
2092
2093 if (is_namespace_blk(dev_a)) {
2094 nsblk_a = to_nd_namespace_blk(dev_a);
2095 nsblk_b = to_nd_namespace_blk(dev_b);
2096
2097 return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
2098 sizeof(resource_size_t));
2099 }
2100
2101 nspm_a = to_nd_namespace_pmem(dev_a);
2102 nspm_b = to_nd_namespace_pmem(dev_b);
2103
2104 return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
2105 sizeof(resource_size_t));
2106}
2107
Dan Williams8a5f50d2016-09-22 15:42:59 -07002108static struct device **scan_labels(struct nd_region *nd_region)
2109{
Dan Williamsc969e242016-10-05 15:54:46 -07002110 int i, count = 0;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002111 struct device *dev, **devs = NULL;
2112 struct nd_label_ent *label_ent, *e;
Dan Williamsc969e242016-10-05 15:54:46 -07002113 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2114 resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
Dan Williams1b40e092015-05-01 13:34:01 -04002115
Dan Williams8a5f50d2016-09-22 15:42:59 -07002116 /* "safe" because create_namespace_pmem() might list_move() label_ent */
2117 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
Dan Williamsae8219f2016-09-19 16:04:21 -07002118 struct nd_namespace_label *nd_label = label_ent->label;
Dan Williams1b40e092015-05-01 13:34:01 -04002119 struct device **__devs;
Dan Williamsae8219f2016-09-19 16:04:21 -07002120 u32 flags;
Dan Williams1b40e092015-05-01 13:34:01 -04002121
Dan Williamsae8219f2016-09-19 16:04:21 -07002122 if (!nd_label)
2123 continue;
2124 flags = __le32_to_cpu(nd_label->flags);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002125 if (is_nd_blk(&nd_region->dev)
2126 == !!(flags & NSLABEL_FLAG_LOCAL))
2127 /* pass, region matches label type */;
Dan Williams1b40e092015-05-01 13:34:01 -04002128 else
2129 continue;
2130
Dan Williamsc969e242016-10-05 15:54:46 -07002131 /* skip labels that describe extents outside of the region */
2132 if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
2133 continue;
2134
Dan Williams8a5f50d2016-09-22 15:42:59 -07002135 i = add_namespace_resource(nd_region, nd_label, devs, count);
2136 if (i < 0)
2137 goto err;
Dan Williams1b40e092015-05-01 13:34:01 -04002138 if (i < count)
2139 continue;
2140 __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
2141 if (!__devs)
2142 goto err;
2143 memcpy(__devs, devs, sizeof(dev) * count);
2144 kfree(devs);
2145 devs = __devs;
2146
Dan Williams8a5f50d2016-09-22 15:42:59 -07002147 if (is_nd_blk(&nd_region->dev)) {
2148 dev = create_namespace_blk(nd_region, nd_label, count);
2149 if (IS_ERR(dev))
2150 goto err;
2151 devs[count++] = dev;
2152 } else {
Dan Williamsc12c48c2017-06-04 10:59:15 +09002153 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2154 struct nd_namespace_index *nsindex;
2155
2156 nsindex = to_namespace_index(ndd, ndd->ns_current);
2157 dev = create_namespace_pmem(nd_region, nsindex, nd_label);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002158 if (IS_ERR(dev)) {
2159 switch (PTR_ERR(dev)) {
2160 case -EAGAIN:
2161 /* skip invalid labels */
2162 continue;
2163 case -ENODEV:
2164 /* fallthrough to seed creation */
2165 break;
2166 default:
2167 goto err;
2168 }
2169 } else
2170 devs[count++] = dev;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002171 }
Dan Williams1b40e092015-05-01 13:34:01 -04002172 }
2173
Dan Williams8a5f50d2016-09-22 15:42:59 -07002174 dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
2175 __func__, count, is_nd_blk(&nd_region->dev)
2176 ? "blk" : "pmem", count == 1 ? "" : "s");
Dan Williams1b40e092015-05-01 13:34:01 -04002177
2178 if (count == 0) {
2179 /* Publish a zero-sized namespace for userspace to configure. */
Dan Williamsae8219f2016-09-19 16:04:21 -07002180 nd_mapping_free_labels(nd_mapping);
Dan Williams1b40e092015-05-01 13:34:01 -04002181
2182 devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
2183 if (!devs)
2184 goto err;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002185 if (is_nd_blk(&nd_region->dev)) {
2186 struct nd_namespace_blk *nsblk;
2187
2188 nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
2189 if (!nsblk)
2190 goto err;
2191 dev = &nsblk->common.dev;
2192 dev->type = &namespace_blk_device_type;
2193 } else {
2194 struct nd_namespace_pmem *nspm;
2195
2196 nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
2197 if (!nspm)
2198 goto err;
2199 dev = &nspm->nsio.common.dev;
2200 dev->type = &namespace_pmem_device_type;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002201 nd_namespace_pmem_set_resource(nd_region, nspm, 0);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002202 }
Dan Williams1b40e092015-05-01 13:34:01 -04002203 dev->parent = &nd_region->dev;
2204 devs[count++] = dev;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002205 } else if (is_nd_pmem(&nd_region->dev)) {
2206 /* clean unselected labels */
2207 for (i = 0; i < nd_region->ndr_mappings; i++) {
Dan Williams0e3b0d12016-10-06 23:13:15 -07002208 struct list_head *l, *e;
2209 LIST_HEAD(list);
2210 int j;
2211
Dan Williams8a5f50d2016-09-22 15:42:59 -07002212 nd_mapping = &nd_region->mapping[i];
2213 if (list_empty(&nd_mapping->labels)) {
2214 WARN_ON(1);
2215 continue;
2216 }
Dan Williams0e3b0d12016-10-06 23:13:15 -07002217
2218 j = count;
2219 list_for_each_safe(l, e, &nd_mapping->labels) {
2220 if (!j--)
2221 break;
2222 list_move_tail(l, &list);
2223 }
Dan Williams8a5f50d2016-09-22 15:42:59 -07002224 nd_mapping_free_labels(nd_mapping);
Dan Williams0e3b0d12016-10-06 23:13:15 -07002225 list_splice_init(&list, &nd_mapping->labels);
Dan Williams8a5f50d2016-09-22 15:42:59 -07002226 }
Dan Williams1b40e092015-05-01 13:34:01 -04002227 }
2228
Dan Williams6ff3e912016-10-05 14:04:15 -07002229 if (count > 1)
2230 sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
2231
Dan Williams1b40e092015-05-01 13:34:01 -04002232 return devs;
2233
Dan Williamsae8219f2016-09-19 16:04:21 -07002234 err:
Dan Carpenter75d29712016-10-12 09:34:29 +03002235 if (devs) {
2236 for (i = 0; devs[i]; i++)
2237 if (is_nd_blk(&nd_region->dev))
2238 namespace_blk_release(devs[i]);
2239 else
2240 namespace_pmem_release(devs[i]);
2241 kfree(devs);
2242 }
Dan Williams1b40e092015-05-01 13:34:01 -04002243 return NULL;
2244}
2245
Dan Williams8a5f50d2016-09-22 15:42:59 -07002246static struct device **create_namespaces(struct nd_region *nd_region)
Dan Williamsae8219f2016-09-19 16:04:21 -07002247{
2248 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
2249 struct device **devs;
Dan Williams8a5f50d2016-09-22 15:42:59 -07002250 int i;
Dan Williamsae8219f2016-09-19 16:04:21 -07002251
2252 if (nd_region->ndr_mappings == 0)
2253 return NULL;
2254
Dan Williams8a5f50d2016-09-22 15:42:59 -07002255 /* lock down all mappings while we scan labels */
2256 for (i = 0; i < nd_region->ndr_mappings; i++) {
2257 nd_mapping = &nd_region->mapping[i];
2258 mutex_lock_nested(&nd_mapping->lock, i);
2259 }
2260
2261 devs = scan_labels(nd_region);
2262
2263 for (i = 0; i < nd_region->ndr_mappings; i++) {
2264 int reverse = nd_region->ndr_mappings - 1 - i;
2265
2266 nd_mapping = &nd_region->mapping[reverse];
2267 mutex_unlock(&nd_mapping->lock);
2268 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002269
2270 return devs;
2271}
2272
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002273static int init_active_labels(struct nd_region *nd_region)
2274{
2275 int i;
2276
2277 for (i = 0; i < nd_region->ndr_mappings; i++) {
2278 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
2279 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
2280 struct nvdimm *nvdimm = nd_mapping->nvdimm;
Dan Williamsae8219f2016-09-19 16:04:21 -07002281 struct nd_label_ent *label_ent;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002282 int count, j;
2283
2284 /*
Dan Williams9d62ed92017-05-04 11:47:22 -07002285 * If the dimm is disabled then we may need to prevent
2286 * the region from being activated.
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002287 */
2288 if (!ndd) {
Dan Williams9d62ed92017-05-04 11:47:22 -07002289 if (test_bit(NDD_LOCKED, &nvdimm->flags))
2290 /* fail, label data may be unreadable */;
2291 else if (test_bit(NDD_ALIASING, &nvdimm->flags))
2292 /* fail, labels needed to disambiguate dpa */;
2293 else
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002294 return 0;
Dan Williams9d62ed92017-05-04 11:47:22 -07002295
2296 dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
2297 dev_name(&nd_mapping->nvdimm->dev),
2298 test_bit(NDD_LOCKED, &nvdimm->flags)
2299 ? "locked" : "disabled");
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002300 return -ENXIO;
2301 }
2302 nd_mapping->ndd = ndd;
2303 atomic_inc(&nvdimm->busy);
2304 get_ndd(ndd);
2305
2306 count = nd_label_active_count(ndd);
2307 dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
2308 if (!count)
2309 continue;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002310 for (j = 0; j < count; j++) {
2311 struct nd_namespace_label *label;
2312
Dan Williamsae8219f2016-09-19 16:04:21 -07002313 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
2314 if (!label_ent)
2315 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002316 label = nd_label_active(ndd, j);
Dan Williamsae8219f2016-09-19 16:04:21 -07002317 label_ent->label = label;
2318
2319 mutex_lock(&nd_mapping->lock);
2320 list_add_tail(&label_ent->list, &nd_mapping->labels);
2321 mutex_unlock(&nd_mapping->lock);
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002322 }
Dan Williamsae8219f2016-09-19 16:04:21 -07002323
2324 if (j >= count)
2325 continue;
2326
2327 mutex_lock(&nd_mapping->lock);
2328 nd_mapping_free_labels(nd_mapping);
2329 mutex_unlock(&nd_mapping->lock);
2330 return -ENOMEM;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002331 }
2332
2333 return 0;
2334}
2335
Dan Williams3d880022015-05-31 15:02:11 -04002336int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
2337{
2338 struct device **devs = NULL;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002339 int i, rc = 0, type;
Dan Williams3d880022015-05-31 15:02:11 -04002340
2341 *err = 0;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002342 nvdimm_bus_lock(&nd_region->dev);
2343 rc = init_active_labels(nd_region);
2344 if (rc) {
2345 nvdimm_bus_unlock(&nd_region->dev);
2346 return rc;
2347 }
2348
2349 type = nd_region_to_nstype(nd_region);
2350 switch (type) {
Dan Williams3d880022015-05-31 15:02:11 -04002351 case ND_DEVICE_NAMESPACE_IO:
2352 devs = create_namespace_io(nd_region);
2353 break;
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002354 case ND_DEVICE_NAMESPACE_PMEM:
Dan Williams1b40e092015-05-01 13:34:01 -04002355 case ND_DEVICE_NAMESPACE_BLK:
Dan Williams8a5f50d2016-09-22 15:42:59 -07002356 devs = create_namespaces(nd_region);
Dan Williams1b40e092015-05-01 13:34:01 -04002357 break;
Dan Williams3d880022015-05-31 15:02:11 -04002358 default:
2359 break;
2360 }
Dan Williamsbf9bccc2015-06-17 17:14:46 -04002361 nvdimm_bus_unlock(&nd_region->dev);
Dan Williams3d880022015-05-31 15:02:11 -04002362
2363 if (!devs)
2364 return -ENODEV;
2365
Dan Williams3d880022015-05-31 15:02:11 -04002366 for (i = 0; devs[i]; i++) {
2367 struct device *dev = devs[i];
Dan Williams1b40e092015-05-01 13:34:01 -04002368 int id;
Dan Williams3d880022015-05-31 15:02:11 -04002369
Dan Williams1b40e092015-05-01 13:34:01 -04002370 if (type == ND_DEVICE_NAMESPACE_BLK) {
2371 struct nd_namespace_blk *nsblk;
2372
2373 nsblk = to_nd_namespace_blk(dev);
2374 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2375 GFP_KERNEL);
2376 nsblk->id = id;
Dan Williams0e3b0d12016-10-06 23:13:15 -07002377 } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
2378 struct nd_namespace_pmem *nspm;
2379
2380 nspm = to_nd_namespace_pmem(dev);
2381 id = ida_simple_get(&nd_region->ns_ida, 0, 0,
2382 GFP_KERNEL);
2383 nspm->id = id;
Dan Williams1b40e092015-05-01 13:34:01 -04002384 } else
2385 id = i;
2386
2387 if (id < 0)
2388 break;
2389 dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
Dan Williams3d880022015-05-31 15:02:11 -04002390 dev->groups = nd_namespace_attribute_groups;
2391 nd_device_register(dev);
2392 }
Dan Williams1b40e092015-05-01 13:34:01 -04002393 if (i)
2394 nd_region->ns_seed = devs[0];
2395
2396 if (devs[i]) {
2397 int j;
2398
2399 for (j = i; devs[j]; j++) {
2400 struct device *dev = devs[j];
2401
2402 device_initialize(dev);
2403 put_device(dev);
2404 }
2405 *err = j - i;
2406 /*
2407 * All of the namespaces we tried to register failed, so
2408 * fail region activation.
2409 */
2410 if (*err == 0)
2411 rc = -ENODEV;
2412 }
Dan Williams3d880022015-05-31 15:02:11 -04002413 kfree(devs);
2414
Dan Williams1b40e092015-05-01 13:34:01 -04002415 if (rc == -ENODEV)
2416 return rc;
2417
Dan Williams3d880022015-05-31 15:02:11 -04002418 return i;
2419}