1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
5 #include <linux/device.h>
6 #include <linux/ndctl.h>
7 #include <linux/uuid.h>
8 #include <linux/slab.h>
15 static guid_t nvdimm_btt_guid;
16 static guid_t nvdimm_btt2_guid;
17 static guid_t nvdimm_pfn_guid;
18 static guid_t nvdimm_dax_guid;
20 static uuid_t nvdimm_btt_uuid;
21 static uuid_t nvdimm_btt2_uuid;
22 static uuid_t nvdimm_pfn_uuid;
23 static uuid_t nvdimm_dax_uuid;
25 static uuid_t cxl_region_uuid;
26 static uuid_t cxl_namespace_uuid;
28 static const char NSINDEX_SIGNATURE[] = "NAMESPACE_INDEX\0";
30 static u32 best_seq(u32 a, u32 b)
32 a &= NSINDEX_SEQ_MASK;
33 b &= NSINDEX_SEQ_MASK;
39 else if (nd_inc_seq(a) == b)
45 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
47 return ndd->nslabel_size;
50 static size_t __sizeof_namespace_index(u32 nslot)
52 return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8),
56 static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd,
59 return (ndd->nsarea.config_size - index_size * 2) /
60 sizeof_namespace_label(ndd);
63 int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
67 tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd);
68 n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN;
70 return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n);
73 size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
75 u32 nslot, space, size;
78 * Per UEFI 2.7, the minimum size of the Label Storage Area is large
79 * enough to hold 2 index blocks and 2 labels. The minimum index
80 * block size is 256 bytes. The label size is 128 for namespaces
81 * prior to version 1.2 and at minimum 256 for version 1.2 and later.
83 nslot = nvdimm_num_label_slots(ndd);
84 space = ndd->nsarea.config_size - nslot * sizeof_namespace_label(ndd);
85 size = __sizeof_namespace_index(nslot) * 2;
86 if (size <= space && nslot >= 2)
89 dev_err(ndd->dev, "label area (%d) too small to host (%d byte) labels\n",
90 ndd->nsarea.config_size, sizeof_namespace_label(ndd));
94 static int __nd_label_validate(struct nvdimm_drvdata *ndd)
97 * On media label format consists of two index blocks followed
98 * by an array of labels. None of these structures are ever
99 * updated in place. A sequence number tracks the current
100 * active index and the next one to write, while labels are
101 * written to free slots.
123 struct nd_namespace_index *nsindex[] = {
124 to_namespace_index(ndd, 0),
125 to_namespace_index(ndd, 1),
127 const int num_index = ARRAY_SIZE(nsindex);
128 struct device *dev = ndd->dev;
129 bool valid[2] = { 0 };
130 int i, num_valid = 0;
133 for (i = 0; i < num_index; i++) {
135 u8 sig[NSINDEX_SIG_LEN];
136 u64 sum_save, sum, size;
137 unsigned int version, labelsize;
139 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
140 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
141 dev_dbg(dev, "nsindex%d signature invalid\n", i);
145 /* label sizes larger than 128 arrived with v1.2 */
146 version = __le16_to_cpu(nsindex[i]->major) * 100
147 + __le16_to_cpu(nsindex[i]->minor);
149 labelsize = 1 << (7 + nsindex[i]->labelsize);
153 if (labelsize != sizeof_namespace_label(ndd)) {
154 dev_dbg(dev, "nsindex%d labelsize %d invalid\n",
155 i, nsindex[i]->labelsize);
159 sum_save = __le64_to_cpu(nsindex[i]->checksum);
160 nsindex[i]->checksum = __cpu_to_le64(0);
161 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
162 nsindex[i]->checksum = __cpu_to_le64(sum_save);
163 if (sum != sum_save) {
164 dev_dbg(dev, "nsindex%d checksum invalid\n", i);
168 seq = __le32_to_cpu(nsindex[i]->seq);
169 if ((seq & NSINDEX_SEQ_MASK) == 0) {
170 dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq);
174 /* sanity check the index against expected values */
175 if (__le64_to_cpu(nsindex[i]->myoff)
176 != i * sizeof_namespace_index(ndd)) {
177 dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n",
178 i, (unsigned long long)
179 __le64_to_cpu(nsindex[i]->myoff));
182 if (__le64_to_cpu(nsindex[i]->otheroff)
183 != (!i) * sizeof_namespace_index(ndd)) {
184 dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n",
185 i, (unsigned long long)
186 __le64_to_cpu(nsindex[i]->otheroff));
189 if (__le64_to_cpu(nsindex[i]->labeloff)
190 != 2 * sizeof_namespace_index(ndd)) {
191 dev_dbg(dev, "nsindex%d labeloff: %#llx invalid\n",
192 i, (unsigned long long)
193 __le64_to_cpu(nsindex[i]->labeloff));
197 size = __le64_to_cpu(nsindex[i]->mysize);
198 if (size > sizeof_namespace_index(ndd)
199 || size < sizeof(struct nd_namespace_index)) {
200 dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size);
204 nslot = __le32_to_cpu(nsindex[i]->nslot);
205 if (nslot * sizeof_namespace_label(ndd)
206 + 2 * sizeof_namespace_index(ndd)
207 > ndd->nsarea.config_size) {
208 dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n",
209 i, nslot, ndd->nsarea.config_size);
220 for (i = 0; i < num_index; i++)
223 /* can't have num_valid > 0 but valid[] = { false, false } */
227 /* pick the best index... */
228 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
229 __le32_to_cpu(nsindex[1]->seq));
230 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
240 static int nd_label_validate(struct nvdimm_drvdata *ndd)
243 * In order to probe for and validate namespace index blocks we
244 * need to know the size of the labels, and we can't trust the
245 * size of the labels until we validate the index blocks.
246 * Resolve this dependency loop by probing for known label
247 * sizes, but default to v1.2 256-byte namespace labels if
250 int label_size[] = { 128, 256 };
253 for (i = 0; i < ARRAY_SIZE(label_size); i++) {
254 ndd->nslabel_size = label_size[i];
255 rc = __nd_label_validate(ndd);
263 static void nd_label_copy(struct nvdimm_drvdata *ndd,
264 struct nd_namespace_index *dst,
265 struct nd_namespace_index *src)
267 /* just exit if either destination or source is NULL */
271 memcpy(dst, src, sizeof_namespace_index(ndd));
274 static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
276 void *base = to_namespace_index(ndd, 0);
278 return base + 2 * sizeof_namespace_index(ndd);
281 static int to_slot(struct nvdimm_drvdata *ndd,
282 struct nd_namespace_label *nd_label)
284 unsigned long label, base;
286 label = (unsigned long) nd_label;
287 base = (unsigned long) nd_label_base(ndd);
289 return (label - base) / sizeof_namespace_label(ndd);
292 static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
294 unsigned long label, base;
296 base = (unsigned long) nd_label_base(ndd);
297 label = base + sizeof_namespace_label(ndd) * slot;
299 return (struct nd_namespace_label *) label;
302 #define for_each_clear_bit_le(bit, addr, size) \
303 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
305 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
308 * preamble_index - common variable initialization for nd_label_* routines
309 * @ndd: dimm container for the relevant label set
310 * @idx: namespace_index index
311 * @nsindex_out: on return set to the currently active namespace index
312 * @free: on return set to the free label bitmap in the index
313 * @nslot: on return set to the number of slots in the label space
315 static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
316 struct nd_namespace_index **nsindex_out,
317 unsigned long **free, u32 *nslot)
319 struct nd_namespace_index *nsindex;
321 nsindex = to_namespace_index(ndd, idx);
325 *free = (unsigned long *) nsindex->free;
326 *nslot = __le32_to_cpu(nsindex->nslot);
327 *nsindex_out = nsindex;
332 char *nd_label_gen_id(struct nd_label_id *label_id, const uuid_t *uuid,
335 if (!label_id || !uuid)
337 snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
338 flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
342 static bool preamble_current(struct nvdimm_drvdata *ndd,
343 struct nd_namespace_index **nsindex,
344 unsigned long **free, u32 *nslot)
346 return preamble_index(ndd, ndd->ns_current, nsindex,
350 static bool preamble_next(struct nvdimm_drvdata *ndd,
351 struct nd_namespace_index **nsindex,
352 unsigned long **free, u32 *nslot)
354 return preamble_index(ndd, ndd->ns_next, nsindex,
358 static bool nsl_validate_checksum(struct nvdimm_drvdata *ndd,
359 struct nd_namespace_label *nd_label)
363 if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
366 sum_save = nsl_get_checksum(ndd, nd_label);
367 nsl_set_checksum(ndd, nd_label, 0);
368 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
369 nsl_set_checksum(ndd, nd_label, sum_save);
370 return sum == sum_save;
373 static void nsl_calculate_checksum(struct nvdimm_drvdata *ndd,
374 struct nd_namespace_label *nd_label)
378 if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
380 nsl_set_checksum(ndd, nd_label, 0);
381 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
382 nsl_set_checksum(ndd, nd_label, sum);
385 static bool slot_valid(struct nvdimm_drvdata *ndd,
386 struct nd_namespace_label *nd_label, u32 slot)
390 /* check that we are written where we expect to be written */
391 if (slot != nsl_get_slot(ndd, nd_label))
393 valid = nsl_validate_checksum(ndd, nd_label);
395 dev_dbg(ndd->dev, "fail checksum. slot: %d\n", slot);
399 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
401 struct nd_namespace_index *nsindex;
405 if (!preamble_current(ndd, &nsindex, &free, &nslot))
406 return 0; /* no label, nothing to reserve */
408 for_each_clear_bit_le(slot, free, nslot) {
409 struct nvdimm *nvdimm = to_nvdimm(ndd->dev);
410 struct nd_namespace_label *nd_label;
411 struct nd_region *nd_region = NULL;
412 struct nd_label_id label_id;
413 struct resource *res;
417 nd_label = to_label(ndd, slot);
419 if (!slot_valid(ndd, nd_label, slot))
422 nsl_get_uuid(ndd, nd_label, &label_uuid);
423 flags = nsl_get_flags(ndd, nd_label);
424 if (test_bit(NDD_NOBLK, &nvdimm->flags))
425 flags &= ~NSLABEL_FLAG_LOCAL;
426 nd_label_gen_id(&label_id, &label_uuid, flags);
427 res = nvdimm_allocate_dpa(ndd, &label_id,
428 nsl_get_dpa(ndd, nd_label),
429 nsl_get_rawsize(ndd, nd_label));
430 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
438 int nd_label_data_init(struct nvdimm_drvdata *ndd)
440 size_t config_size, read_size, max_xfer, offset;
441 struct nd_namespace_index *nsindex;
449 if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0) {
450 dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n",
451 ndd->nsarea.max_xfer, ndd->nsarea.config_size);
456 * We need to determine the maximum index area as this is the section
457 * we must read and validate before we can start processing labels.
459 * If the area is too small to contain the two indexes and 2 labels
462 * Start at a label size of 128 as this should result in the largest
463 * possible namespace index size.
465 ndd->nslabel_size = 128;
466 read_size = sizeof_namespace_index(ndd) * 2;
470 /* Allocate config data */
471 config_size = ndd->nsarea.config_size;
472 ndd->data = kvzalloc(config_size, GFP_KERNEL);
477 * We want to guarantee as few reads as possible while conserving
478 * memory. To do that we figure out how much unused space will be left
479 * in the last read, divide that by the total number of reads it is
480 * going to take given our maximum transfer size, and then reduce our
481 * maximum transfer size based on that result.
483 max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size);
484 if (read_size < max_xfer) {
486 max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) /
487 DIV_ROUND_UP(config_size, max_xfer);
488 /* make certain we read indexes in exactly 1 read */
489 if (max_xfer < read_size)
490 max_xfer = read_size;
493 /* Make our initial read size a multiple of max_xfer size */
494 read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer,
497 /* Read the index data */
498 rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size);
502 /* Validate index data, if not valid assume all labels are invalid */
503 ndd->ns_current = nd_label_validate(ndd);
504 if (ndd->ns_current < 0)
507 /* Record our index values */
508 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
510 /* Copy "current" index on top of the "next" index */
511 nsindex = to_current_namespace_index(ndd);
512 nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex);
514 /* Determine starting offset for label data */
515 offset = __le64_to_cpu(nsindex->labeloff);
516 nslot = __le32_to_cpu(nsindex->nslot);
518 /* Loop through the free list pulling in any active labels */
519 for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) {
520 size_t label_read_size;
522 /* zero out the unused labels */
523 if (test_bit_le(i, nsindex->free)) {
524 memset(ndd->data + offset, 0, ndd->nslabel_size);
528 /* if we already read past here then just continue */
529 if (offset + ndd->nslabel_size <= read_size)
532 /* if we haven't read in a while reset our read_size offset */
533 if (read_size < offset)
536 /* determine how much more will be read after this next call. */
537 label_read_size = offset + ndd->nslabel_size - read_size;
538 label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) *
541 /* truncate last read if needed */
542 if (read_size + label_read_size > config_size)
543 label_read_size = config_size - read_size;
545 /* Read the label data */
546 rc = nvdimm_get_config_data(ndd, ndd->data + read_size,
547 read_size, label_read_size);
551 /* push read_size to next read offset */
552 read_size += label_read_size;
555 dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc);
560 int nd_label_active_count(struct nvdimm_drvdata *ndd)
562 struct nd_namespace_index *nsindex;
567 if (!preamble_current(ndd, &nsindex, &free, &nslot))
570 for_each_clear_bit_le(slot, free, nslot) {
571 struct nd_namespace_label *nd_label;
573 nd_label = to_label(ndd, slot);
575 if (!slot_valid(ndd, nd_label, slot)) {
576 u32 label_slot = nsl_get_slot(ndd, nd_label);
577 u64 size = nsl_get_rawsize(ndd, nd_label);
578 u64 dpa = nsl_get_dpa(ndd, nd_label);
581 "slot%d invalid slot: %d dpa: %llx size: %llx\n",
582 slot, label_slot, dpa, size);
590 struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
592 struct nd_namespace_index *nsindex;
596 if (!preamble_current(ndd, &nsindex, &free, &nslot))
599 for_each_clear_bit_le(slot, free, nslot) {
600 struct nd_namespace_label *nd_label;
602 nd_label = to_label(ndd, slot);
603 if (!slot_valid(ndd, nd_label, slot))
607 return to_label(ndd, slot);
613 u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
615 struct nd_namespace_index *nsindex;
619 if (!preamble_next(ndd, &nsindex, &free, &nslot))
622 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
624 slot = find_next_bit_le(free, nslot, 0);
628 clear_bit_le(slot, free);
633 bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
635 struct nd_namespace_index *nsindex;
639 if (!preamble_next(ndd, &nsindex, &free, &nslot))
642 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
645 return !test_and_set_bit_le(slot, free);
649 u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
651 struct nd_namespace_index *nsindex;
655 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
657 if (!preamble_next(ndd, &nsindex, &free, &nslot))
658 return nvdimm_num_label_slots(ndd);
660 return bitmap_weight(free, nslot);
663 static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
666 struct nd_namespace_index *nsindex;
667 unsigned long offset;
672 nsindex = to_namespace_index(ndd, index);
673 if (flags & ND_NSINDEX_INIT)
674 nslot = nvdimm_num_label_slots(ndd);
676 nslot = __le32_to_cpu(nsindex->nslot);
678 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
679 memset(&nsindex->flags, 0, 3);
680 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
681 nsindex->seq = __cpu_to_le32(seq);
682 offset = (unsigned long) nsindex
683 - (unsigned long) to_namespace_index(ndd, 0);
684 nsindex->myoff = __cpu_to_le64(offset);
685 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
686 offset = (unsigned long) to_namespace_index(ndd,
687 nd_label_next_nsindex(index))
688 - (unsigned long) to_namespace_index(ndd, 0);
689 nsindex->otheroff = __cpu_to_le64(offset);
690 offset = (unsigned long) nd_label_base(ndd)
691 - (unsigned long) to_namespace_index(ndd, 0);
692 nsindex->labeloff = __cpu_to_le64(offset);
693 nsindex->nslot = __cpu_to_le32(nslot);
694 nsindex->major = __cpu_to_le16(1);
695 if (sizeof_namespace_label(ndd) < 256)
696 nsindex->minor = __cpu_to_le16(1);
698 nsindex->minor = __cpu_to_le16(2);
699 nsindex->checksum = __cpu_to_le64(0);
700 if (flags & ND_NSINDEX_INIT) {
701 unsigned long *free = (unsigned long *) nsindex->free;
702 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
705 memset(nsindex->free, 0xff, nfree / 8);
706 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
707 clear_bit_le(nslot + i, free);
709 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
710 nsindex->checksum = __cpu_to_le64(checksum);
711 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
712 nsindex, sizeof_namespace_index(ndd));
716 if (flags & ND_NSINDEX_INIT)
719 /* copy the index we just wrote to the new 'next' */
720 WARN_ON(index != ndd->ns_next);
721 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
722 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
723 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
724 WARN_ON(ndd->ns_current == ndd->ns_next);
729 static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
730 struct nd_namespace_label *nd_label)
732 return (unsigned long) nd_label
733 - (unsigned long) to_namespace_index(ndd, 0);
736 static enum nvdimm_claim_class guid_to_nvdimm_cclass(guid_t *guid)
738 if (guid_equal(guid, &nvdimm_btt_guid))
739 return NVDIMM_CCLASS_BTT;
740 else if (guid_equal(guid, &nvdimm_btt2_guid))
741 return NVDIMM_CCLASS_BTT2;
742 else if (guid_equal(guid, &nvdimm_pfn_guid))
743 return NVDIMM_CCLASS_PFN;
744 else if (guid_equal(guid, &nvdimm_dax_guid))
745 return NVDIMM_CCLASS_DAX;
746 else if (guid_equal(guid, &guid_null))
747 return NVDIMM_CCLASS_NONE;
749 return NVDIMM_CCLASS_UNKNOWN;
752 /* CXL labels store UUIDs instead of GUIDs for the same data */
753 static enum nvdimm_claim_class uuid_to_nvdimm_cclass(uuid_t *uuid)
755 if (uuid_equal(uuid, &nvdimm_btt_uuid))
756 return NVDIMM_CCLASS_BTT;
757 else if (uuid_equal(uuid, &nvdimm_btt2_uuid))
758 return NVDIMM_CCLASS_BTT2;
759 else if (uuid_equal(uuid, &nvdimm_pfn_uuid))
760 return NVDIMM_CCLASS_PFN;
761 else if (uuid_equal(uuid, &nvdimm_dax_uuid))
762 return NVDIMM_CCLASS_DAX;
763 else if (uuid_equal(uuid, &uuid_null))
764 return NVDIMM_CCLASS_NONE;
766 return NVDIMM_CCLASS_UNKNOWN;
769 static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
772 if (claim_class == NVDIMM_CCLASS_BTT)
773 return &nvdimm_btt_guid;
774 else if (claim_class == NVDIMM_CCLASS_BTT2)
775 return &nvdimm_btt2_guid;
776 else if (claim_class == NVDIMM_CCLASS_PFN)
777 return &nvdimm_pfn_guid;
778 else if (claim_class == NVDIMM_CCLASS_DAX)
779 return &nvdimm_dax_guid;
780 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
782 * If we're modifying a namespace for which we don't
783 * know the claim_class, don't touch the existing guid.
790 /* CXL labels store UUIDs instead of GUIDs for the same data */
791 static const uuid_t *to_abstraction_uuid(enum nvdimm_claim_class claim_class,
794 if (claim_class == NVDIMM_CCLASS_BTT)
795 return &nvdimm_btt_uuid;
796 else if (claim_class == NVDIMM_CCLASS_BTT2)
797 return &nvdimm_btt2_uuid;
798 else if (claim_class == NVDIMM_CCLASS_PFN)
799 return &nvdimm_pfn_uuid;
800 else if (claim_class == NVDIMM_CCLASS_DAX)
801 return &nvdimm_dax_uuid;
802 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
804 * If we're modifying a namespace for which we don't
805 * know the claim_class, don't touch the existing uuid.
812 static void reap_victim(struct nd_mapping *nd_mapping,
813 struct nd_label_ent *victim)
815 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
816 u32 slot = to_slot(ndd, victim->label);
818 dev_dbg(ndd->dev, "free: %d\n", slot);
819 nd_label_free_slot(ndd, slot);
820 victim->label = NULL;
823 static void nsl_set_type_guid(struct nvdimm_drvdata *ndd,
824 struct nd_namespace_label *nd_label, guid_t *guid)
826 if (efi_namespace_label_has(ndd, type_guid))
827 guid_copy(&nd_label->efi.type_guid, guid);
830 bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
831 struct nd_namespace_label *nd_label, guid_t *guid)
833 if (ndd->cxl || !efi_namespace_label_has(ndd, type_guid))
835 if (!guid_equal(&nd_label->efi.type_guid, guid)) {
836 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", guid,
837 &nd_label->efi.type_guid);
843 static void nsl_set_claim_class(struct nvdimm_drvdata *ndd,
844 struct nd_namespace_label *nd_label,
845 enum nvdimm_claim_class claim_class)
850 import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
851 export_uuid(nd_label->cxl.abstraction_uuid,
852 to_abstraction_uuid(claim_class, &uuid));
856 if (!efi_namespace_label_has(ndd, abstraction_guid))
858 guid_copy(&nd_label->efi.abstraction_guid,
859 to_abstraction_guid(claim_class,
860 &nd_label->efi.abstraction_guid));
863 enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
864 struct nd_namespace_label *nd_label)
869 import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
870 return uuid_to_nvdimm_cclass(&uuid);
872 if (!efi_namespace_label_has(ndd, abstraction_guid))
873 return NVDIMM_CCLASS_NONE;
874 return guid_to_nvdimm_cclass(&nd_label->efi.abstraction_guid);
877 static int __pmem_label_update(struct nd_region *nd_region,
878 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
879 int pos, unsigned long flags)
881 struct nd_namespace_common *ndns = &nspm->nsio.common;
882 struct nd_interleave_set *nd_set = nd_region->nd_set;
883 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
884 struct nd_namespace_label *nd_label;
885 struct nd_namespace_index *nsindex;
886 struct nd_label_ent *label_ent;
887 struct nd_label_id label_id;
888 struct resource *res;
895 if (!preamble_next(ndd, &nsindex, &free, &nslot))
898 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
899 nd_label_gen_id(&label_id, nspm->uuid, 0);
900 for_each_dpa_resource(ndd, res)
901 if (strcmp(res->name, label_id.id) == 0)
909 /* allocate and write the label to the staging (next) index */
910 slot = nd_label_alloc_slot(ndd);
911 if (slot == UINT_MAX)
913 dev_dbg(ndd->dev, "allocated: %d\n", slot);
915 nd_label = to_label(ndd, slot);
916 memset(nd_label, 0, sizeof_namespace_label(ndd));
917 nsl_set_uuid(ndd, nd_label, nspm->uuid);
918 nsl_set_name(ndd, nd_label, nspm->alt_name);
919 nsl_set_flags(ndd, nd_label, flags);
920 nsl_set_nlabel(ndd, nd_label, nd_region->ndr_mappings);
921 nsl_set_nrange(ndd, nd_label, 1);
922 nsl_set_position(ndd, nd_label, pos);
923 nsl_set_isetcookie(ndd, nd_label, cookie);
924 nsl_set_rawsize(ndd, nd_label, resource_size(res));
925 nsl_set_lbasize(ndd, nd_label, nspm->lbasize);
926 nsl_set_dpa(ndd, nd_label, res->start);
927 nsl_set_slot(ndd, nd_label, slot);
928 nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid);
929 nsl_set_claim_class(ndd, nd_label, ndns->claim_class);
930 nsl_calculate_checksum(ndd, nd_label);
931 nd_dbg_dpa(nd_region, ndd, res, "\n");
934 offset = nd_label_offset(ndd, nd_label);
935 rc = nvdimm_set_config_data(ndd, offset, nd_label,
936 sizeof_namespace_label(ndd));
940 /* Garbage collect the previous label */
941 mutex_lock(&nd_mapping->lock);
942 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
943 if (!label_ent->label)
945 if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags) ||
946 nsl_uuid_equal(ndd, label_ent->label, nspm->uuid))
947 reap_victim(nd_mapping, label_ent);
951 rc = nd_label_write_index(ndd, ndd->ns_next,
952 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
954 list_for_each_entry(label_ent, &nd_mapping->labels, list)
955 if (!label_ent->label) {
956 label_ent->label = nd_label;
960 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
961 "failed to track label: %d\n",
962 to_slot(ndd, nd_label));
966 mutex_unlock(&nd_mapping->lock);
971 static bool is_old_resource(struct resource *res, struct resource **list, int n)
975 if (res->flags & DPA_RESOURCE_ADJUSTED)
977 for (i = 0; i < n; i++)
983 static struct resource *to_resource(struct nvdimm_drvdata *ndd,
984 struct nd_namespace_label *nd_label)
986 struct resource *res;
988 for_each_dpa_resource(ndd, res) {
989 if (res->start != nsl_get_dpa(ndd, nd_label))
991 if (resource_size(res) != nsl_get_rawsize(ndd, nd_label))
1000 * Use the presence of the type_guid as a flag to determine isetcookie
1001 * usage and nlabel + position policy for blk-aperture namespaces.
1003 static void nsl_set_blk_isetcookie(struct nvdimm_drvdata *ndd,
1004 struct nd_namespace_label *nd_label,
1007 if (efi_namespace_label_has(ndd, type_guid)) {
1008 nsl_set_isetcookie(ndd, nd_label, isetcookie);
1011 nsl_set_isetcookie(ndd, nd_label, 0); /* N/A */
1014 bool nsl_validate_blk_isetcookie(struct nvdimm_drvdata *ndd,
1015 struct nd_namespace_label *nd_label,
1018 if (!efi_namespace_label_has(ndd, type_guid))
1021 if (nsl_get_isetcookie(ndd, nd_label) != isetcookie) {
1022 dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n", isetcookie,
1023 nsl_get_isetcookie(ndd, nd_label));
1030 static void nsl_set_blk_nlabel(struct nvdimm_drvdata *ndd,
1031 struct nd_namespace_label *nd_label, int nlabel,
1034 if (!efi_namespace_label_has(ndd, type_guid)) {
1035 nsl_set_nlabel(ndd, nd_label, 0); /* N/A */
1038 nsl_set_nlabel(ndd, nd_label, first ? nlabel : 0xffff);
1041 static void nsl_set_blk_position(struct nvdimm_drvdata *ndd,
1042 struct nd_namespace_label *nd_label,
1045 if (!efi_namespace_label_has(ndd, type_guid)) {
1046 nsl_set_position(ndd, nd_label, 0);
1049 nsl_set_position(ndd, nd_label, first ? 0 : 0xffff);
1053 * 1/ Account all the labels that can be freed after this update
1054 * 2/ Allocate and write the label to the staging (next) index
1055 * 3/ Record the resources in the namespace device
1057 static int __blk_label_update(struct nd_region *nd_region,
1058 struct nd_mapping *nd_mapping, struct nd_namespace_blk *nsblk,
1061 int i, alloc, victims, nfree, old_num_resources, nlabel, rc = -ENXIO;
1062 struct nd_interleave_set *nd_set = nd_region->nd_set;
1063 struct nd_namespace_common *ndns = &nsblk->common;
1064 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1065 struct nd_namespace_label *nd_label;
1066 struct nd_label_ent *label_ent, *e;
1067 struct nd_namespace_index *nsindex;
1068 unsigned long *free, *victim_map = NULL;
1069 struct resource *res, **old_res_list;
1070 struct nd_label_id label_id;
1071 int min_dpa_idx = 0;
1075 if (!preamble_next(ndd, &nsindex, &free, &nslot))
1078 old_res_list = nsblk->res;
1079 nfree = nd_label_nfree(ndd);
1080 old_num_resources = nsblk->num_resources;
1081 nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
1084 * We need to loop over the old resources a few times, which seems a
1085 * bit inefficient, but we need to know that we have the label
1086 * space before we start mutating the tracking structures.
1087 * Otherwise the recovery method of last resort for userspace is
1088 * disable and re-enable the parent region.
1091 for_each_dpa_resource(ndd, res) {
1092 if (strcmp(res->name, label_id.id) != 0)
1094 if (!is_old_resource(res, old_res_list, old_num_resources))
1099 if (old_num_resources) {
1100 /* convert old local-label-map to dimm-slot victim-map */
1101 victim_map = bitmap_zalloc(nslot, GFP_KERNEL);
1105 /* mark unused labels for garbage collection */
1106 for_each_clear_bit_le(slot, free, nslot) {
1107 nd_label = to_label(ndd, slot);
1108 if (!nsl_uuid_equal(ndd, nd_label, nsblk->uuid))
1110 res = to_resource(ndd, nd_label);
1111 if (res && is_old_resource(res, old_res_list,
1114 slot = to_slot(ndd, nd_label);
1115 set_bit(slot, victim_map);
1120 /* don't allow updates that consume the last label */
1121 if (nfree - alloc < 0 || nfree - alloc + victims < 1) {
1122 dev_info(&nsblk->common.dev, "insufficient label space\n");
1123 bitmap_free(victim_map);
1126 /* from here on we need to abort on error */
1129 /* assign all resources to the namespace before writing the labels */
1131 nsblk->num_resources = 0;
1132 for_each_dpa_resource(ndd, res) {
1133 if (strcmp(res->name, label_id.id) != 0)
1135 if (!nsblk_add_resource(nd_region, ndd, nsblk, res->start)) {
1141 /* release slots associated with any invalidated UUIDs */
1142 mutex_lock(&nd_mapping->lock);
1143 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list)
1144 if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags)) {
1145 reap_victim(nd_mapping, label_ent);
1146 list_move(&label_ent->list, &list);
1148 mutex_unlock(&nd_mapping->lock);
1151 * Find the resource associated with the first label in the set
1152 * per the v1.2 namespace specification.
1154 for (i = 0; i < nsblk->num_resources; i++) {
1155 struct resource *min = nsblk->res[min_dpa_idx];
1157 res = nsblk->res[i];
1158 if (res->start < min->start)
1162 for (i = 0; i < nsblk->num_resources; i++) {
1165 res = nsblk->res[i];
1166 if (is_old_resource(res, old_res_list, old_num_resources))
1167 continue; /* carry-over */
1168 slot = nd_label_alloc_slot(ndd);
1169 if (slot == UINT_MAX) {
1173 dev_dbg(ndd->dev, "allocated: %d\n", slot);
1175 nd_label = to_label(ndd, slot);
1176 memset(nd_label, 0, sizeof_namespace_label(ndd));
1177 nsl_set_uuid(ndd, nd_label, nsblk->uuid);
1178 nsl_set_name(ndd, nd_label, nsblk->alt_name);
1179 nsl_set_flags(ndd, nd_label, NSLABEL_FLAG_LOCAL);
1181 nsl_set_blk_nlabel(ndd, nd_label, nsblk->num_resources,
1183 nsl_set_blk_position(ndd, nd_label, i == min_dpa_idx);
1184 nsl_set_blk_isetcookie(ndd, nd_label, nd_set->cookie2);
1186 nsl_set_dpa(ndd, nd_label, res->start);
1187 nsl_set_rawsize(ndd, nd_label, resource_size(res));
1188 nsl_set_lbasize(ndd, nd_label, nsblk->lbasize);
1189 nsl_set_slot(ndd, nd_label, slot);
1190 nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid);
1191 nsl_set_claim_class(ndd, nd_label, ndns->claim_class);
1192 nsl_calculate_checksum(ndd, nd_label);
1195 offset = nd_label_offset(ndd, nd_label);
1196 rc = nvdimm_set_config_data(ndd, offset, nd_label,
1197 sizeof_namespace_label(ndd));
1202 /* free up now unused slots in the new index */
1203 for_each_set_bit(slot, victim_map, victim_map ? nslot : 0) {
1204 dev_dbg(ndd->dev, "free: %d\n", slot);
1205 nd_label_free_slot(ndd, slot);
1209 rc = nd_label_write_index(ndd, ndd->ns_next,
1210 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1215 * Now that the on-dimm labels are up to date, fix up the tracking
1216 * entries in nd_mapping->labels
1219 mutex_lock(&nd_mapping->lock);
1220 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1221 nd_label = label_ent->label;
1225 if (!nsl_uuid_equal(ndd, nd_label, nsblk->uuid))
1228 list_move(&label_ent->list, &list);
1229 label_ent->label = NULL;
1231 list_splice_tail_init(&list, &nd_mapping->labels);
1232 mutex_unlock(&nd_mapping->lock);
1234 if (nlabel + nsblk->num_resources > num_labels) {
1236 * Bug, we can't end up with more resources than
1244 mutex_lock(&nd_mapping->lock);
1245 label_ent = list_first_entry_or_null(&nd_mapping->labels,
1246 typeof(*label_ent), list);
1249 mutex_unlock(&nd_mapping->lock);
1253 for_each_clear_bit_le(slot, free, nslot) {
1254 nd_label = to_label(ndd, slot);
1255 if (!nsl_uuid_equal(ndd, nd_label, nsblk->uuid))
1257 res = to_resource(ndd, nd_label);
1258 res->flags &= ~DPA_RESOURCE_ADJUSTED;
1259 dev_vdbg(&nsblk->common.dev, "assign label slot: %d\n", slot);
1260 list_for_each_entry_from(label_ent, &nd_mapping->labels, list) {
1261 if (label_ent->label)
1263 label_ent->label = nd_label;
1268 dev_WARN(&nsblk->common.dev,
1269 "failed to track label slot%d\n", slot);
1271 mutex_unlock(&nd_mapping->lock);
1274 kfree(old_res_list);
1275 bitmap_free(victim_map);
1280 * 1/ repair the allocated label bitmap in the index
1281 * 2/ restore the resource list
1283 nd_label_copy(ndd, nsindex, to_current_namespace_index(ndd));
1285 nsblk->res = old_res_list;
1286 nsblk->num_resources = old_num_resources;
1287 old_res_list = NULL;
1291 static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
1293 int i, old_num_labels = 0;
1294 struct nd_label_ent *label_ent;
1295 struct nd_namespace_index *nsindex;
1296 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1298 mutex_lock(&nd_mapping->lock);
1299 list_for_each_entry(label_ent, &nd_mapping->labels, list)
1301 mutex_unlock(&nd_mapping->lock);
1304 * We need to preserve all the old labels for the mapping so
1305 * they can be garbage collected after writing the new labels.
1307 for (i = old_num_labels; i < num_labels; i++) {
1308 label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
1311 mutex_lock(&nd_mapping->lock);
1312 list_add_tail(&label_ent->list, &nd_mapping->labels);
1313 mutex_unlock(&nd_mapping->lock);
1316 if (ndd->ns_current == -1 || ndd->ns_next == -1)
1319 return max(num_labels, old_num_labels);
1321 nsindex = to_namespace_index(ndd, 0);
1322 memset(nsindex, 0, ndd->nsarea.config_size);
1323 for (i = 0; i < 2; i++) {
1324 int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
1330 ndd->ns_current = 0;
1332 return max(num_labels, old_num_labels);
1335 static int del_labels(struct nd_mapping *nd_mapping, uuid_t *uuid)
1337 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1338 struct nd_label_ent *label_ent, *e;
1339 struct nd_namespace_index *nsindex;
1340 unsigned long *free;
1348 /* no index || no labels == nothing to delete */
1349 if (!preamble_next(ndd, &nsindex, &free, &nslot))
1352 mutex_lock(&nd_mapping->lock);
1353 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1354 struct nd_namespace_label *nd_label = label_ent->label;
1359 if (!nsl_uuid_equal(ndd, nd_label, uuid))
1362 slot = to_slot(ndd, nd_label);
1363 nd_label_free_slot(ndd, slot);
1364 dev_dbg(ndd->dev, "free: %d\n", slot);
1365 list_move_tail(&label_ent->list, &list);
1366 label_ent->label = NULL;
1368 list_splice_tail_init(&list, &nd_mapping->labels);
1371 nd_mapping_free_labels(nd_mapping);
1372 dev_dbg(ndd->dev, "no more active labels\n");
1374 mutex_unlock(&nd_mapping->lock);
1376 return nd_label_write_index(ndd, ndd->ns_next,
1377 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1380 int nd_pmem_namespace_label_update(struct nd_region *nd_region,
1381 struct nd_namespace_pmem *nspm, resource_size_t size)
1385 for (i = 0; i < nd_region->ndr_mappings; i++) {
1386 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1387 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1388 struct resource *res;
1392 rc = del_labels(nd_mapping, nspm->uuid);
1398 for_each_dpa_resource(ndd, res)
1399 if (strncmp(res->name, "pmem", 4) == 0)
1401 WARN_ON_ONCE(!count);
1403 rc = init_labels(nd_mapping, count);
1407 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
1408 NSLABEL_FLAG_UPDATING);
1416 /* Clear the UPDATING flag per UEFI 2.7 expectations */
1417 for (i = 0; i < nd_region->ndr_mappings; i++) {
1418 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1420 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
1428 int nd_blk_namespace_label_update(struct nd_region *nd_region,
1429 struct nd_namespace_blk *nsblk, resource_size_t size)
1431 struct nd_mapping *nd_mapping = &nd_region->mapping[0];
1432 struct resource *res;
1436 return del_labels(nd_mapping, nsblk->uuid);
1438 for_each_dpa_resource(to_ndd(nd_mapping), res)
1441 count = init_labels(nd_mapping, count);
1445 return __blk_label_update(nd_region, nd_mapping, nsblk, count);
1448 int __init nd_label_init(void)
1450 WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
1451 WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
1452 WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
1453 WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
1455 WARN_ON(uuid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_uuid));
1456 WARN_ON(uuid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_uuid));
1457 WARN_ON(uuid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_uuid));
1458 WARN_ON(uuid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_uuid));
1460 WARN_ON(uuid_parse(CXL_REGION_UUID, &cxl_region_uuid));
1461 WARN_ON(uuid_parse(CXL_NAMESPACE_UUID, &cxl_namespace_uuid));