1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4 */
5 #include <linux/device.h>
6 #include <linux/ndctl.h>
7 #include <linux/uuid.h>
8 #include <linux/slab.h>
9 #include <linux/io.h>
10 #include <linux/nd.h>
11 #include "nd-core.h"
12 #include "label.h"
13 #include "nd.h"
14
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;
19
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;
24
25 static uuid_t cxl_region_uuid;
26 static uuid_t cxl_namespace_uuid;
27
28 static const char NSINDEX_SIGNATURE[] = "NAMESPACE_INDEX\0";
29
best_seq(u32 a,u32 b)30 static u32 best_seq(u32 a, u32 b)
31 {
32 a &= NSINDEX_SEQ_MASK;
33 b &= NSINDEX_SEQ_MASK;
34
35 if (a == 0 || a == b)
36 return b;
37 else if (b == 0)
38 return a;
39 else if (nd_inc_seq(a) == b)
40 return b;
41 else
42 return a;
43 }
44
sizeof_namespace_label(struct nvdimm_drvdata * ndd)45 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
46 {
47 return ndd->nslabel_size;
48 }
49
__sizeof_namespace_index(u32 nslot)50 static size_t __sizeof_namespace_index(u32 nslot)
51 {
52 return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8),
53 NSINDEX_ALIGN);
54 }
55
__nvdimm_num_label_slots(struct nvdimm_drvdata * ndd,size_t index_size)56 static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd,
57 size_t index_size)
58 {
59 return (ndd->nsarea.config_size - index_size * 2) /
60 sizeof_namespace_label(ndd);
61 }
62
nvdimm_num_label_slots(struct nvdimm_drvdata * ndd)63 int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
64 {
65 u32 tmp_nslot, n;
66
67 tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd);
68 n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN;
69
70 return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n);
71 }
72
sizeof_namespace_index(struct nvdimm_drvdata * ndd)73 size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
74 {
75 u32 nslot, space, size;
76
77 /*
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.
82 */
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)
87 return size / 2;
88
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));
91 return 0;
92 }
93
__nd_label_validate(struct nvdimm_drvdata * ndd)94 static int __nd_label_validate(struct nvdimm_drvdata *ndd)
95 {
96 /*
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.
102 *
103 * +------------+
104 * | |
105 * | nsindex0 |
106 * | |
107 * +------------+
108 * | |
109 * | nsindex1 |
110 * | |
111 * +------------+
112 * | label0 |
113 * +------------+
114 * | label1 |
115 * +------------+
116 * | |
117 * ....nslot...
118 * | |
119 * +------------+
120 * | labelN |
121 * +------------+
122 */
123 struct nd_namespace_index *nsindex[] = {
124 to_namespace_index(ndd, 0),
125 to_namespace_index(ndd, 1),
126 };
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;
131 u32 seq;
132
133 for (i = 0; i < num_index; i++) {
134 u32 nslot;
135 u8 sig[NSINDEX_SIG_LEN];
136 u64 sum_save, sum, size;
137 unsigned int version, labelsize;
138
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);
142 continue;
143 }
144
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);
148 if (version >= 102) {
149 /*
150 * labelsize feeds the shift below; only 0 (128-byte)
151 * and 1 (256-byte) are valid -- a larger value would
152 * overflow or exceed the width of int.
153 */
154 if (nsindex[i]->labelsize > 1) {
155 dev_dbg(dev, "nsindex%d labelsize: %d invalid\n",
156 i, nsindex[i]->labelsize);
157 continue;
158 }
159 labelsize = 1 << (7 + nsindex[i]->labelsize);
160 } else {
161 labelsize = 128;
162 }
163
164 if (labelsize != sizeof_namespace_label(ndd)) {
165 dev_dbg(dev, "nsindex%d labelsize %d invalid\n",
166 i, nsindex[i]->labelsize);
167 continue;
168 }
169
170 sum_save = __le64_to_cpu(nsindex[i]->checksum);
171 nsindex[i]->checksum = __cpu_to_le64(0);
172 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
173 nsindex[i]->checksum = __cpu_to_le64(sum_save);
174 if (sum != sum_save) {
175 dev_dbg(dev, "nsindex%d checksum invalid\n", i);
176 continue;
177 }
178
179 seq = __le32_to_cpu(nsindex[i]->seq);
180 if ((seq & NSINDEX_SEQ_MASK) == 0) {
181 dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq);
182 continue;
183 }
184
185 /* sanity check the index against expected values */
186 if (__le64_to_cpu(nsindex[i]->myoff)
187 != i * sizeof_namespace_index(ndd)) {
188 dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n",
189 i, (unsigned long long)
190 __le64_to_cpu(nsindex[i]->myoff));
191 continue;
192 }
193 if (__le64_to_cpu(nsindex[i]->otheroff)
194 != (!i) * sizeof_namespace_index(ndd)) {
195 dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n",
196 i, (unsigned long long)
197 __le64_to_cpu(nsindex[i]->otheroff));
198 continue;
199 }
200 if (__le64_to_cpu(nsindex[i]->labeloff)
201 != 2 * sizeof_namespace_index(ndd)) {
202 dev_dbg(dev, "nsindex%d labeloff: %#llx invalid\n",
203 i, (unsigned long long)
204 __le64_to_cpu(nsindex[i]->labeloff));
205 continue;
206 }
207
208 size = __le64_to_cpu(nsindex[i]->mysize);
209 if (size > sizeof_namespace_index(ndd)
210 || size < sizeof(struct nd_namespace_index)) {
211 dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size);
212 continue;
213 }
214
215 nslot = __le32_to_cpu(nsindex[i]->nslot);
216 if ((u64)nslot * sizeof_namespace_label(ndd)
217 + 2 * sizeof_namespace_index(ndd)
218 > ndd->nsarea.config_size) {
219 dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n",
220 i, nslot, ndd->nsarea.config_size);
221 continue;
222 }
223 valid[i] = true;
224 num_valid++;
225 }
226
227 switch (num_valid) {
228 case 0:
229 break;
230 case 1:
231 for (i = 0; i < num_index; i++)
232 if (valid[i])
233 return i;
234 /* can't have num_valid > 0 but valid[] = { false, false } */
235 WARN_ON(1);
236 break;
237 default:
238 /* pick the best index... */
239 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
240 __le32_to_cpu(nsindex[1]->seq));
241 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
242 return 1;
243 else
244 return 0;
245 break;
246 }
247
248 return -1;
249 }
250
nd_label_validate(struct nvdimm_drvdata * ndd)251 static int nd_label_validate(struct nvdimm_drvdata *ndd)
252 {
253 /*
254 * In order to probe for and validate namespace index blocks we
255 * need to know the size of the labels, and we can't trust the
256 * size of the labels until we validate the index blocks.
257 * Resolve this dependency loop by probing for known label
258 * sizes, but default to v1.2 256-byte namespace labels if
259 * discovery fails.
260 */
261 int label_size[] = { 128, 256 };
262 int i, rc;
263
264 for (i = 0; i < ARRAY_SIZE(label_size); i++) {
265 ndd->nslabel_size = label_size[i];
266 rc = __nd_label_validate(ndd);
267 if (rc >= 0)
268 return rc;
269 }
270
271 return -1;
272 }
273
nd_label_copy(struct nvdimm_drvdata * ndd,struct nd_namespace_index * dst,struct nd_namespace_index * src)274 static void nd_label_copy(struct nvdimm_drvdata *ndd,
275 struct nd_namespace_index *dst,
276 struct nd_namespace_index *src)
277 {
278 /* just exit if either destination or source is NULL */
279 if (!dst || !src)
280 return;
281
282 memcpy(dst, src, sizeof_namespace_index(ndd));
283 }
284
nd_label_base(struct nvdimm_drvdata * ndd)285 static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
286 {
287 void *base = to_namespace_index(ndd, 0);
288
289 return base + 2 * sizeof_namespace_index(ndd);
290 }
291
to_slot(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)292 static int to_slot(struct nvdimm_drvdata *ndd,
293 struct nd_namespace_label *nd_label)
294 {
295 unsigned long label, base;
296
297 label = (unsigned long) nd_label;
298 base = (unsigned long) nd_label_base(ndd);
299
300 return (label - base) / sizeof_namespace_label(ndd);
301 }
302
to_label(struct nvdimm_drvdata * ndd,int slot)303 static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
304 {
305 unsigned long label, base;
306
307 base = (unsigned long) nd_label_base(ndd);
308 label = base + sizeof_namespace_label(ndd) * slot;
309
310 return (struct nd_namespace_label *) label;
311 }
312
313 #define for_each_clear_bit_le(bit, addr, size) \
314 for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
315 (bit) < (size); \
316 (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
317
318 /**
319 * preamble_index - common variable initialization for nd_label_* routines
320 * @ndd: dimm container for the relevant label set
321 * @idx: namespace_index index
322 * @nsindex_out: on return set to the currently active namespace index
323 * @free: on return set to the free label bitmap in the index
324 * @nslot: on return set to the number of slots in the label space
325 */
preamble_index(struct nvdimm_drvdata * ndd,int idx,struct nd_namespace_index ** nsindex_out,unsigned long ** free,u32 * nslot)326 static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
327 struct nd_namespace_index **nsindex_out,
328 unsigned long **free, u32 *nslot)
329 {
330 struct nd_namespace_index *nsindex;
331
332 nsindex = to_namespace_index(ndd, idx);
333 if (nsindex == NULL)
334 return false;
335
336 *free = (unsigned long *) nsindex->free;
337 *nslot = __le32_to_cpu(nsindex->nslot);
338 *nsindex_out = nsindex;
339
340 return true;
341 }
342
nd_label_gen_id(struct nd_label_id * label_id,const uuid_t * uuid,u32 flags)343 char *nd_label_gen_id(struct nd_label_id *label_id, const uuid_t *uuid,
344 u32 flags)
345 {
346 if (!label_id || !uuid)
347 return NULL;
348 snprintf(label_id->id, ND_LABEL_ID_SIZE, "pmem-%pUb", uuid);
349 return label_id->id;
350 }
351
preamble_current(struct nvdimm_drvdata * ndd,struct nd_namespace_index ** nsindex,unsigned long ** free,u32 * nslot)352 static bool preamble_current(struct nvdimm_drvdata *ndd,
353 struct nd_namespace_index **nsindex,
354 unsigned long **free, u32 *nslot)
355 {
356 return preamble_index(ndd, ndd->ns_current, nsindex,
357 free, nslot);
358 }
359
preamble_next(struct nvdimm_drvdata * ndd,struct nd_namespace_index ** nsindex,unsigned long ** free,u32 * nslot)360 static bool preamble_next(struct nvdimm_drvdata *ndd,
361 struct nd_namespace_index **nsindex,
362 unsigned long **free, u32 *nslot)
363 {
364 return preamble_index(ndd, ndd->ns_next, nsindex,
365 free, nslot);
366 }
367
nsl_validate_checksum(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)368 static bool nsl_validate_checksum(struct nvdimm_drvdata *ndd,
369 struct nd_namespace_label *nd_label)
370 {
371 u64 sum, sum_save;
372
373 if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
374 return true;
375
376 sum_save = nsl_get_checksum(ndd, nd_label);
377 nsl_set_checksum(ndd, nd_label, 0);
378 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
379 nsl_set_checksum(ndd, nd_label, sum_save);
380 return sum == sum_save;
381 }
382
nsl_calculate_checksum(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)383 static void nsl_calculate_checksum(struct nvdimm_drvdata *ndd,
384 struct nd_namespace_label *nd_label)
385 {
386 u64 sum;
387
388 if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
389 return;
390 nsl_set_checksum(ndd, nd_label, 0);
391 sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
392 nsl_set_checksum(ndd, nd_label, sum);
393 }
394
slot_valid(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,u32 slot)395 static bool slot_valid(struct nvdimm_drvdata *ndd,
396 struct nd_namespace_label *nd_label, u32 slot)
397 {
398 bool valid;
399
400 /* check that we are written where we expect to be written */
401 if (slot != nsl_get_slot(ndd, nd_label))
402 return false;
403 valid = nsl_validate_checksum(ndd, nd_label);
404 if (!valid)
405 dev_dbg(ndd->dev, "fail checksum. slot: %d\n", slot);
406 return valid;
407 }
408
nd_label_reserve_dpa(struct nvdimm_drvdata * ndd)409 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
410 {
411 struct nd_namespace_index *nsindex;
412 unsigned long *free;
413 u32 nslot, slot;
414
415 if (!preamble_current(ndd, &nsindex, &free, &nslot))
416 return 0; /* no label, nothing to reserve */
417
418 for_each_clear_bit_le(slot, free, nslot) {
419 struct nd_namespace_label *nd_label;
420 struct nd_region *nd_region = NULL;
421 struct nd_label_id label_id;
422 struct resource *res;
423 uuid_t label_uuid;
424 u32 flags;
425
426 nd_label = to_label(ndd, slot);
427
428 if (!slot_valid(ndd, nd_label, slot))
429 continue;
430
431 nsl_get_uuid(ndd, nd_label, &label_uuid);
432 flags = nsl_get_flags(ndd, nd_label);
433 nd_label_gen_id(&label_id, &label_uuid, flags);
434 res = nvdimm_allocate_dpa(ndd, &label_id,
435 nsl_get_dpa(ndd, nd_label),
436 nsl_get_rawsize(ndd, nd_label));
437 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
438 if (!res)
439 return -EBUSY;
440 }
441
442 return 0;
443 }
444
nd_label_data_init(struct nvdimm_drvdata * ndd)445 int nd_label_data_init(struct nvdimm_drvdata *ndd)
446 {
447 size_t config_size, read_size, max_xfer, offset;
448 struct nd_namespace_index *nsindex;
449 unsigned int i;
450 int rc = 0;
451 u32 nslot;
452
453 if (ndd->data)
454 return 0;
455
456 if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0 ||
457 ndd->nsarea.config_size == 0) {
458 dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n",
459 ndd->nsarea.max_xfer, ndd->nsarea.config_size);
460 return -ENXIO;
461 }
462
463 /*
464 * We need to determine the maximum index area as this is the section
465 * we must read and validate before we can start processing labels.
466 *
467 * If the area is too small to contain the two indexes and 2 labels
468 * then we abort.
469 *
470 * Start at a label size of 128 as this should result in the largest
471 * possible namespace index size.
472 */
473 ndd->nslabel_size = 128;
474 read_size = sizeof_namespace_index(ndd) * 2;
475 if (!read_size)
476 return -ENXIO;
477
478 /* Allocate config data */
479 config_size = ndd->nsarea.config_size;
480 ndd->data = kvzalloc(config_size, GFP_KERNEL);
481 if (!ndd->data)
482 return -ENOMEM;
483
484 /*
485 * We want to guarantee as few reads as possible while conserving
486 * memory. To do that we figure out how much unused space will be left
487 * in the last read, divide that by the total number of reads it is
488 * going to take given our maximum transfer size, and then reduce our
489 * maximum transfer size based on that result.
490 */
491 max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size);
492 if (read_size < max_xfer) {
493 /* trim waste */
494 max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) /
495 DIV_ROUND_UP(config_size, max_xfer);
496 /* make certain we read indexes in exactly 1 read */
497 if (max_xfer < read_size)
498 max_xfer = read_size;
499 }
500
501 /* Make our initial read size a multiple of max_xfer size */
502 read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer,
503 config_size);
504
505 /* Read the index data */
506 rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size);
507 if (rc)
508 goto out_err;
509
510 /* Validate index data, if not valid assume all labels are invalid */
511 ndd->ns_current = nd_label_validate(ndd);
512 if (ndd->ns_current < 0)
513 return 0;
514
515 /* Record our index values */
516 ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
517
518 /* Copy "current" index on top of the "next" index */
519 nsindex = to_current_namespace_index(ndd);
520 nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex);
521
522 /* Determine starting offset for label data */
523 offset = __le64_to_cpu(nsindex->labeloff);
524 nslot = __le32_to_cpu(nsindex->nslot);
525
526 /* Loop through the free list pulling in any active labels */
527 for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) {
528 size_t label_read_size;
529
530 /* zero out the unused labels */
531 if (test_bit_le(i, nsindex->free)) {
532 memset(ndd->data + offset, 0, ndd->nslabel_size);
533 continue;
534 }
535
536 /* if we already read past here then just continue */
537 if (offset + ndd->nslabel_size <= read_size)
538 continue;
539
540 /* if we haven't read in a while reset our read_size offset */
541 if (read_size < offset)
542 read_size = offset;
543
544 /* determine how much more will be read after this next call. */
545 label_read_size = offset + ndd->nslabel_size - read_size;
546 label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) *
547 max_xfer;
548
549 /* truncate last read if needed */
550 if (read_size + label_read_size > config_size)
551 label_read_size = config_size - read_size;
552
553 /* Read the label data */
554 rc = nvdimm_get_config_data(ndd, ndd->data + read_size,
555 read_size, label_read_size);
556 if (rc)
557 goto out_err;
558
559 /* push read_size to next read offset */
560 read_size += label_read_size;
561 }
562
563 dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc);
564 out_err:
565 return rc;
566 }
567
nd_label_active_count(struct nvdimm_drvdata * ndd)568 int nd_label_active_count(struct nvdimm_drvdata *ndd)
569 {
570 struct nd_namespace_index *nsindex;
571 unsigned long *free;
572 u32 nslot, slot;
573 int count = 0;
574
575 if (!preamble_current(ndd, &nsindex, &free, &nslot))
576 return 0;
577
578 for_each_clear_bit_le(slot, free, nslot) {
579 struct nd_namespace_label *nd_label;
580
581 nd_label = to_label(ndd, slot);
582
583 if (!slot_valid(ndd, nd_label, slot)) {
584 u32 label_slot = nsl_get_slot(ndd, nd_label);
585 u64 size = nsl_get_rawsize(ndd, nd_label);
586 u64 dpa = nsl_get_dpa(ndd, nd_label);
587
588 dev_dbg(ndd->dev,
589 "slot%d invalid slot: %d dpa: %llx size: %llx\n",
590 slot, label_slot, dpa, size);
591 continue;
592 }
593 count++;
594 }
595 return count;
596 }
597
nd_label_active(struct nvdimm_drvdata * ndd,int n)598 struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
599 {
600 struct nd_namespace_index *nsindex;
601 unsigned long *free;
602 u32 nslot, slot;
603
604 if (!preamble_current(ndd, &nsindex, &free, &nslot))
605 return NULL;
606
607 for_each_clear_bit_le(slot, free, nslot) {
608 struct nd_namespace_label *nd_label;
609
610 nd_label = to_label(ndd, slot);
611 if (!slot_valid(ndd, nd_label, slot))
612 continue;
613
614 if (n-- == 0)
615 return to_label(ndd, slot);
616 }
617
618 return NULL;
619 }
620
nd_label_alloc_slot(struct nvdimm_drvdata * ndd)621 u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
622 {
623 struct nd_namespace_index *nsindex;
624 unsigned long *free;
625 u32 nslot, slot;
626
627 if (!preamble_next(ndd, &nsindex, &free, &nslot))
628 return UINT_MAX;
629
630 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
631
632 slot = find_next_bit_le(free, nslot, 0);
633 if (slot == nslot)
634 return UINT_MAX;
635
636 clear_bit_le(slot, free);
637
638 return slot;
639 }
640
nd_label_free_slot(struct nvdimm_drvdata * ndd,u32 slot)641 bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
642 {
643 struct nd_namespace_index *nsindex;
644 unsigned long *free;
645 u32 nslot;
646
647 if (!preamble_next(ndd, &nsindex, &free, &nslot))
648 return false;
649
650 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
651
652 if (slot < nslot)
653 return !test_and_set_bit_le(slot, free);
654 return false;
655 }
656
nd_label_nfree(struct nvdimm_drvdata * ndd)657 u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
658 {
659 struct nd_namespace_index *nsindex;
660 unsigned long *free;
661 u32 nslot;
662
663 WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
664
665 if (!preamble_next(ndd, &nsindex, &free, &nslot))
666 return nvdimm_num_label_slots(ndd);
667
668 return bitmap_weight(free, nslot);
669 }
670
nd_label_write_index(struct nvdimm_drvdata * ndd,int index,u32 seq,unsigned long flags)671 static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
672 unsigned long flags)
673 {
674 struct nd_namespace_index *nsindex;
675 unsigned long offset;
676 u64 checksum;
677 u32 nslot;
678 int rc;
679
680 nsindex = to_namespace_index(ndd, index);
681 if (flags & ND_NSINDEX_INIT)
682 nslot = nvdimm_num_label_slots(ndd);
683 else
684 nslot = __le32_to_cpu(nsindex->nslot);
685
686 memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
687 memset(&nsindex->flags, 0, 3);
688 nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
689 nsindex->seq = __cpu_to_le32(seq);
690 offset = (unsigned long) nsindex
691 - (unsigned long) to_namespace_index(ndd, 0);
692 nsindex->myoff = __cpu_to_le64(offset);
693 nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
694 offset = (unsigned long) to_namespace_index(ndd,
695 nd_label_next_nsindex(index))
696 - (unsigned long) to_namespace_index(ndd, 0);
697 nsindex->otheroff = __cpu_to_le64(offset);
698 offset = (unsigned long) nd_label_base(ndd)
699 - (unsigned long) to_namespace_index(ndd, 0);
700 nsindex->labeloff = __cpu_to_le64(offset);
701 nsindex->nslot = __cpu_to_le32(nslot);
702 nsindex->major = __cpu_to_le16(1);
703 if (sizeof_namespace_label(ndd) < 256)
704 nsindex->minor = __cpu_to_le16(1);
705 else
706 nsindex->minor = __cpu_to_le16(2);
707 nsindex->checksum = __cpu_to_le64(0);
708 if (flags & ND_NSINDEX_INIT) {
709 unsigned long *free = (unsigned long *) nsindex->free;
710 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
711 int last_bits, i;
712
713 memset(nsindex->free, 0xff, nfree / 8);
714 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
715 clear_bit_le(nslot + i, free);
716 }
717 checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
718 nsindex->checksum = __cpu_to_le64(checksum);
719 rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
720 nsindex, sizeof_namespace_index(ndd));
721 if (rc < 0)
722 return rc;
723
724 if (flags & ND_NSINDEX_INIT)
725 return 0;
726
727 /* copy the index we just wrote to the new 'next' */
728 WARN_ON(index != ndd->ns_next);
729 nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
730 ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
731 ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
732 WARN_ON(ndd->ns_current == ndd->ns_next);
733
734 return 0;
735 }
736
nd_label_offset(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)737 static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
738 struct nd_namespace_label *nd_label)
739 {
740 return (unsigned long) nd_label
741 - (unsigned long) to_namespace_index(ndd, 0);
742 }
743
guid_to_nvdimm_cclass(guid_t * guid)744 static enum nvdimm_claim_class guid_to_nvdimm_cclass(guid_t *guid)
745 {
746 if (guid_equal(guid, &nvdimm_btt_guid))
747 return NVDIMM_CCLASS_BTT;
748 else if (guid_equal(guid, &nvdimm_btt2_guid))
749 return NVDIMM_CCLASS_BTT2;
750 else if (guid_equal(guid, &nvdimm_pfn_guid))
751 return NVDIMM_CCLASS_PFN;
752 else if (guid_equal(guid, &nvdimm_dax_guid))
753 return NVDIMM_CCLASS_DAX;
754 else if (guid_equal(guid, &guid_null))
755 return NVDIMM_CCLASS_NONE;
756
757 return NVDIMM_CCLASS_UNKNOWN;
758 }
759
760 /* CXL labels store UUIDs instead of GUIDs for the same data */
uuid_to_nvdimm_cclass(uuid_t * uuid)761 static enum nvdimm_claim_class uuid_to_nvdimm_cclass(uuid_t *uuid)
762 {
763 if (uuid_equal(uuid, &nvdimm_btt_uuid))
764 return NVDIMM_CCLASS_BTT;
765 else if (uuid_equal(uuid, &nvdimm_btt2_uuid))
766 return NVDIMM_CCLASS_BTT2;
767 else if (uuid_equal(uuid, &nvdimm_pfn_uuid))
768 return NVDIMM_CCLASS_PFN;
769 else if (uuid_equal(uuid, &nvdimm_dax_uuid))
770 return NVDIMM_CCLASS_DAX;
771 else if (uuid_equal(uuid, &uuid_null))
772 return NVDIMM_CCLASS_NONE;
773
774 return NVDIMM_CCLASS_UNKNOWN;
775 }
776
to_abstraction_guid(enum nvdimm_claim_class claim_class,guid_t * target)777 static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
778 guid_t *target)
779 {
780 if (claim_class == NVDIMM_CCLASS_BTT)
781 return &nvdimm_btt_guid;
782 else if (claim_class == NVDIMM_CCLASS_BTT2)
783 return &nvdimm_btt2_guid;
784 else if (claim_class == NVDIMM_CCLASS_PFN)
785 return &nvdimm_pfn_guid;
786 else if (claim_class == NVDIMM_CCLASS_DAX)
787 return &nvdimm_dax_guid;
788 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
789 /*
790 * If we're modifying a namespace for which we don't
791 * know the claim_class, don't touch the existing guid.
792 */
793 return target;
794 } else
795 return &guid_null;
796 }
797
798 /* CXL labels store UUIDs instead of GUIDs for the same data */
to_abstraction_uuid(enum nvdimm_claim_class claim_class,uuid_t * target)799 static const uuid_t *to_abstraction_uuid(enum nvdimm_claim_class claim_class,
800 uuid_t *target)
801 {
802 if (claim_class == NVDIMM_CCLASS_BTT)
803 return &nvdimm_btt_uuid;
804 else if (claim_class == NVDIMM_CCLASS_BTT2)
805 return &nvdimm_btt2_uuid;
806 else if (claim_class == NVDIMM_CCLASS_PFN)
807 return &nvdimm_pfn_uuid;
808 else if (claim_class == NVDIMM_CCLASS_DAX)
809 return &nvdimm_dax_uuid;
810 else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
811 /*
812 * If we're modifying a namespace for which we don't
813 * know the claim_class, don't touch the existing uuid.
814 */
815 return target;
816 } else
817 return &uuid_null;
818 }
819
reap_victim(struct nd_mapping * nd_mapping,struct nd_label_ent * victim)820 static void reap_victim(struct nd_mapping *nd_mapping,
821 struct nd_label_ent *victim)
822 {
823 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
824 u32 slot = to_slot(ndd, victim->label);
825
826 dev_dbg(ndd->dev, "free: %d\n", slot);
827 nd_label_free_slot(ndd, slot);
828 victim->label = NULL;
829 }
830
nsl_set_type_guid(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,guid_t * guid)831 static void nsl_set_type_guid(struct nvdimm_drvdata *ndd,
832 struct nd_namespace_label *nd_label, guid_t *guid)
833 {
834 if (efi_namespace_label_has(ndd, type_guid))
835 guid_copy(&nd_label->efi.type_guid, guid);
836 }
837
nsl_validate_type_guid(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,guid_t * guid)838 bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
839 struct nd_namespace_label *nd_label, guid_t *guid)
840 {
841 if (ndd->cxl || !efi_namespace_label_has(ndd, type_guid))
842 return true;
843 if (!guid_equal(&nd_label->efi.type_guid, guid)) {
844 dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", guid,
845 &nd_label->efi.type_guid);
846 return false;
847 }
848 return true;
849 }
850
nsl_set_claim_class(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label,enum nvdimm_claim_class claim_class)851 static void nsl_set_claim_class(struct nvdimm_drvdata *ndd,
852 struct nd_namespace_label *nd_label,
853 enum nvdimm_claim_class claim_class)
854 {
855 if (ndd->cxl) {
856 uuid_t uuid;
857
858 import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
859 export_uuid(nd_label->cxl.abstraction_uuid,
860 to_abstraction_uuid(claim_class, &uuid));
861 return;
862 }
863
864 if (!efi_namespace_label_has(ndd, abstraction_guid))
865 return;
866 guid_copy(&nd_label->efi.abstraction_guid,
867 to_abstraction_guid(claim_class,
868 &nd_label->efi.abstraction_guid));
869 }
870
nsl_get_claim_class(struct nvdimm_drvdata * ndd,struct nd_namespace_label * nd_label)871 enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
872 struct nd_namespace_label *nd_label)
873 {
874 if (ndd->cxl) {
875 uuid_t uuid;
876
877 import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
878 return uuid_to_nvdimm_cclass(&uuid);
879 }
880 if (!efi_namespace_label_has(ndd, abstraction_guid))
881 return NVDIMM_CCLASS_NONE;
882 return guid_to_nvdimm_cclass(&nd_label->efi.abstraction_guid);
883 }
884
__pmem_label_update(struct nd_region * nd_region,struct nd_mapping * nd_mapping,struct nd_namespace_pmem * nspm,int pos,unsigned long flags)885 static int __pmem_label_update(struct nd_region *nd_region,
886 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
887 int pos, unsigned long flags)
888 {
889 struct nd_namespace_common *ndns = &nspm->nsio.common;
890 struct nd_interleave_set *nd_set = nd_region->nd_set;
891 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
892 struct nd_namespace_label *nd_label;
893 struct nd_namespace_index *nsindex;
894 struct nd_label_ent *label_ent;
895 struct nd_label_id label_id;
896 struct resource *res;
897 unsigned long *free;
898 u32 nslot, slot;
899 size_t offset;
900 u64 cookie;
901 int rc;
902
903 if (!preamble_next(ndd, &nsindex, &free, &nslot))
904 return -ENXIO;
905
906 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
907 nd_label_gen_id(&label_id, nspm->uuid, 0);
908 for_each_dpa_resource(ndd, res)
909 if (strcmp(res->name, label_id.id) == 0)
910 break;
911
912 if (!res) {
913 WARN_ON_ONCE(1);
914 return -ENXIO;
915 }
916
917 /* allocate and write the label to the staging (next) index */
918 slot = nd_label_alloc_slot(ndd);
919 if (slot == UINT_MAX)
920 return -ENXIO;
921 dev_dbg(ndd->dev, "allocated: %d\n", slot);
922
923 nd_label = to_label(ndd, slot);
924 memset(nd_label, 0, sizeof_namespace_label(ndd));
925 nsl_set_uuid(ndd, nd_label, nspm->uuid);
926 nsl_set_name(ndd, nd_label, nspm->alt_name);
927 nsl_set_flags(ndd, nd_label, flags);
928 nsl_set_nlabel(ndd, nd_label, nd_region->ndr_mappings);
929 nsl_set_nrange(ndd, nd_label, 1);
930 nsl_set_position(ndd, nd_label, pos);
931 nsl_set_isetcookie(ndd, nd_label, cookie);
932 nsl_set_rawsize(ndd, nd_label, resource_size(res));
933 nsl_set_lbasize(ndd, nd_label, nspm->lbasize);
934 nsl_set_dpa(ndd, nd_label, res->start);
935 nsl_set_slot(ndd, nd_label, slot);
936 nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid);
937 nsl_set_claim_class(ndd, nd_label, ndns->claim_class);
938 nsl_calculate_checksum(ndd, nd_label);
939 nd_dbg_dpa(nd_region, ndd, res, "\n");
940
941 /* update label */
942 offset = nd_label_offset(ndd, nd_label);
943 rc = nvdimm_set_config_data(ndd, offset, nd_label,
944 sizeof_namespace_label(ndd));
945 if (rc < 0)
946 return rc;
947
948 /* Garbage collect the previous label */
949 mutex_lock(&nd_mapping->lock);
950 list_for_each_entry(label_ent, &nd_mapping->labels, list) {
951 if (!label_ent->label)
952 continue;
953 if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags) ||
954 nsl_uuid_equal(ndd, label_ent->label, nspm->uuid))
955 reap_victim(nd_mapping, label_ent);
956 }
957
958 /* update index */
959 rc = nd_label_write_index(ndd, ndd->ns_next,
960 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
961 if (rc == 0) {
962 list_for_each_entry(label_ent, &nd_mapping->labels, list)
963 if (!label_ent->label) {
964 label_ent->label = nd_label;
965 nd_label = NULL;
966 break;
967 }
968 dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
969 "failed to track label: %d\n",
970 to_slot(ndd, nd_label));
971 if (nd_label)
972 rc = -ENXIO;
973 }
974 mutex_unlock(&nd_mapping->lock);
975
976 return rc;
977 }
978
init_labels(struct nd_mapping * nd_mapping,int num_labels)979 static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
980 {
981 int i, old_num_labels = 0;
982 struct nd_label_ent *label_ent;
983 struct nd_namespace_index *nsindex;
984 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
985
986 mutex_lock(&nd_mapping->lock);
987 list_for_each_entry(label_ent, &nd_mapping->labels, list)
988 old_num_labels++;
989 mutex_unlock(&nd_mapping->lock);
990
991 /*
992 * We need to preserve all the old labels for the mapping so
993 * they can be garbage collected after writing the new labels.
994 */
995 for (i = old_num_labels; i < num_labels; i++) {
996 label_ent = kzalloc_obj(*label_ent);
997 if (!label_ent)
998 return -ENOMEM;
999 mutex_lock(&nd_mapping->lock);
1000 list_add_tail(&label_ent->list, &nd_mapping->labels);
1001 mutex_unlock(&nd_mapping->lock);
1002 }
1003
1004 if (ndd->ns_current == -1 || ndd->ns_next == -1)
1005 /* pass */;
1006 else
1007 return max(num_labels, old_num_labels);
1008
1009 nsindex = to_namespace_index(ndd, 0);
1010 memset(nsindex, 0, ndd->nsarea.config_size);
1011 for (i = 0; i < 2; i++) {
1012 int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
1013
1014 if (rc)
1015 return rc;
1016 }
1017 ndd->ns_next = 1;
1018 ndd->ns_current = 0;
1019
1020 return max(num_labels, old_num_labels);
1021 }
1022
del_labels(struct nd_mapping * nd_mapping,uuid_t * uuid)1023 static int del_labels(struct nd_mapping *nd_mapping, uuid_t *uuid)
1024 {
1025 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1026 struct nd_label_ent *label_ent, *e;
1027 struct nd_namespace_index *nsindex;
1028 unsigned long *free;
1029 LIST_HEAD(list);
1030 u32 nslot, slot;
1031 int active = 0;
1032
1033 if (!uuid)
1034 return 0;
1035
1036 /* no index || no labels == nothing to delete */
1037 if (!preamble_next(ndd, &nsindex, &free, &nslot))
1038 return 0;
1039
1040 mutex_lock(&nd_mapping->lock);
1041 list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
1042 struct nd_namespace_label *nd_label = label_ent->label;
1043
1044 if (!nd_label)
1045 continue;
1046 active++;
1047 if (!nsl_uuid_equal(ndd, nd_label, uuid))
1048 continue;
1049 active--;
1050 slot = to_slot(ndd, nd_label);
1051 nd_label_free_slot(ndd, slot);
1052 dev_dbg(ndd->dev, "free: %d\n", slot);
1053 list_move_tail(&label_ent->list, &list);
1054 label_ent->label = NULL;
1055 }
1056 list_splice_tail_init(&list, &nd_mapping->labels);
1057
1058 if (active == 0) {
1059 nd_mapping_free_labels(nd_mapping);
1060 dev_dbg(ndd->dev, "no more active labels\n");
1061 }
1062 mutex_unlock(&nd_mapping->lock);
1063
1064 return nd_label_write_index(ndd, ndd->ns_next,
1065 nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
1066 }
1067
nd_pmem_namespace_label_update(struct nd_region * nd_region,struct nd_namespace_pmem * nspm,resource_size_t size)1068 int nd_pmem_namespace_label_update(struct nd_region *nd_region,
1069 struct nd_namespace_pmem *nspm, resource_size_t size)
1070 {
1071 int i, rc;
1072
1073 for (i = 0; i < nd_region->ndr_mappings; i++) {
1074 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1075 struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
1076 struct resource *res;
1077 int count = 0;
1078
1079 if (size == 0) {
1080 rc = del_labels(nd_mapping, nspm->uuid);
1081 if (rc)
1082 return rc;
1083 continue;
1084 }
1085
1086 for_each_dpa_resource(ndd, res)
1087 if (strncmp(res->name, "pmem", 4) == 0)
1088 count++;
1089 WARN_ON_ONCE(!count);
1090
1091 rc = init_labels(nd_mapping, count);
1092 if (rc < 0)
1093 return rc;
1094
1095 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
1096 NSLABEL_FLAG_UPDATING);
1097 if (rc)
1098 return rc;
1099 }
1100
1101 if (size == 0)
1102 return 0;
1103
1104 /* Clear the UPDATING flag per UEFI 2.7 expectations */
1105 for (i = 0; i < nd_region->ndr_mappings; i++) {
1106 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
1107
1108 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
1109 if (rc)
1110 return rc;
1111 }
1112
1113 return 0;
1114 }
1115
nd_label_init(void)1116 int __init nd_label_init(void)
1117 {
1118 WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
1119 WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
1120 WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
1121 WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
1122
1123 WARN_ON(uuid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_uuid));
1124 WARN_ON(uuid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_uuid));
1125 WARN_ON(uuid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_uuid));
1126 WARN_ON(uuid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_uuid));
1127
1128 WARN_ON(uuid_parse(CXL_REGION_UUID, &cxl_region_uuid));
1129 WARN_ON(uuid_parse(CXL_NAMESPACE_UUID, &cxl_namespace_uuid));
1130
1131 return 0;
1132 }
1133