libnvdimm: write pmem label set
[firefly-linux-kernel-4.4.55.git] / drivers / nvdimm / label.c
1 /*
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/device.h>
14 #include <linux/ndctl.h>
15 #include <linux/slab.h>
16 #include <linux/io.h>
17 #include <linux/nd.h>
18 #include "nd-core.h"
19 #include "label.h"
20 #include "nd.h"
21
22 static u32 best_seq(u32 a, u32 b)
23 {
24         a &= NSINDEX_SEQ_MASK;
25         b &= NSINDEX_SEQ_MASK;
26
27         if (a == 0 || a == b)
28                 return b;
29         else if (b == 0)
30                 return a;
31         else if (nd_inc_seq(a) == b)
32                 return b;
33         else
34                 return a;
35 }
36
37 size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
38 {
39         u32 index_span;
40
41         if (ndd->nsindex_size)
42                 return ndd->nsindex_size;
43
44         /*
45          * The minimum index space is 512 bytes, with that amount of
46          * index we can describe ~1400 labels which is less than a byte
47          * of overhead per label.  Round up to a byte of overhead per
48          * label and determine the size of the index region.  Yes, this
49          * starts to waste space at larger config_sizes, but it's
50          * unlikely we'll ever see anything but 128K.
51          */
52         index_span = ndd->nsarea.config_size / 129;
53         index_span /= NSINDEX_ALIGN * 2;
54         ndd->nsindex_size = index_span * NSINDEX_ALIGN;
55
56         return ndd->nsindex_size;
57 }
58
59 static int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
60 {
61         return ndd->nsarea.config_size / 129;
62 }
63
64 int nd_label_validate(struct nvdimm_drvdata *ndd)
65 {
66         /*
67          * On media label format consists of two index blocks followed
68          * by an array of labels.  None of these structures are ever
69          * updated in place.  A sequence number tracks the current
70          * active index and the next one to write, while labels are
71          * written to free slots.
72          *
73          *     +------------+
74          *     |            |
75          *     |  nsindex0  |
76          *     |            |
77          *     +------------+
78          *     |            |
79          *     |  nsindex1  |
80          *     |            |
81          *     +------------+
82          *     |   label0   |
83          *     +------------+
84          *     |   label1   |
85          *     +------------+
86          *     |            |
87          *      ....nslot...
88          *     |            |
89          *     +------------+
90          *     |   labelN   |
91          *     +------------+
92          */
93         struct nd_namespace_index *nsindex[] = {
94                 to_namespace_index(ndd, 0),
95                 to_namespace_index(ndd, 1),
96         };
97         const int num_index = ARRAY_SIZE(nsindex);
98         struct device *dev = ndd->dev;
99         bool valid[2] = { 0 };
100         int i, num_valid = 0;
101         u32 seq;
102
103         for (i = 0; i < num_index; i++) {
104                 u32 nslot;
105                 u8 sig[NSINDEX_SIG_LEN];
106                 u64 sum_save, sum, size;
107
108                 memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
109                 if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
110                         dev_dbg(dev, "%s: nsindex%d signature invalid\n",
111                                         __func__, i);
112                         continue;
113                 }
114                 sum_save = __le64_to_cpu(nsindex[i]->checksum);
115                 nsindex[i]->checksum = __cpu_to_le64(0);
116                 sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
117                 nsindex[i]->checksum = __cpu_to_le64(sum_save);
118                 if (sum != sum_save) {
119                         dev_dbg(dev, "%s: nsindex%d checksum invalid\n",
120                                         __func__, i);
121                         continue;
122                 }
123
124                 seq = __le32_to_cpu(nsindex[i]->seq);
125                 if ((seq & NSINDEX_SEQ_MASK) == 0) {
126                         dev_dbg(dev, "%s: nsindex%d sequence: %#x invalid\n",
127                                         __func__, i, seq);
128                         continue;
129                 }
130
131                 /* sanity check the index against expected values */
132                 if (__le64_to_cpu(nsindex[i]->myoff)
133                                 != i * sizeof_namespace_index(ndd)) {
134                         dev_dbg(dev, "%s: nsindex%d myoff: %#llx invalid\n",
135                                         __func__, i, (unsigned long long)
136                                         __le64_to_cpu(nsindex[i]->myoff));
137                         continue;
138                 }
139                 if (__le64_to_cpu(nsindex[i]->otheroff)
140                                 != (!i) * sizeof_namespace_index(ndd)) {
141                         dev_dbg(dev, "%s: nsindex%d otheroff: %#llx invalid\n",
142                                         __func__, i, (unsigned long long)
143                                         __le64_to_cpu(nsindex[i]->otheroff));
144                         continue;
145                 }
146
147                 size = __le64_to_cpu(nsindex[i]->mysize);
148                 if (size > sizeof_namespace_index(ndd)
149                                 || size < sizeof(struct nd_namespace_index)) {
150                         dev_dbg(dev, "%s: nsindex%d mysize: %#llx invalid\n",
151                                         __func__, i, size);
152                         continue;
153                 }
154
155                 nslot = __le32_to_cpu(nsindex[i]->nslot);
156                 if (nslot * sizeof(struct nd_namespace_label)
157                                 + 2 * sizeof_namespace_index(ndd)
158                                 > ndd->nsarea.config_size) {
159                         dev_dbg(dev, "%s: nsindex%d nslot: %u invalid, config_size: %#x\n",
160                                         __func__, i, nslot,
161                                         ndd->nsarea.config_size);
162                         continue;
163                 }
164                 valid[i] = true;
165                 num_valid++;
166         }
167
168         switch (num_valid) {
169         case 0:
170                 break;
171         case 1:
172                 for (i = 0; i < num_index; i++)
173                         if (valid[i])
174                                 return i;
175                 /* can't have num_valid > 0 but valid[] = { false, false } */
176                 WARN_ON(1);
177                 break;
178         default:
179                 /* pick the best index... */
180                 seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
181                                 __le32_to_cpu(nsindex[1]->seq));
182                 if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
183                         return 1;
184                 else
185                         return 0;
186                 break;
187         }
188
189         return -1;
190 }
191
192 void nd_label_copy(struct nvdimm_drvdata *ndd, struct nd_namespace_index *dst,
193                 struct nd_namespace_index *src)
194 {
195         if (dst && src)
196                 /* pass */;
197         else
198                 return;
199
200         memcpy(dst, src, sizeof_namespace_index(ndd));
201 }
202
203 static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
204 {
205         void *base = to_namespace_index(ndd, 0);
206
207         return base + 2 * sizeof_namespace_index(ndd);
208 }
209
210 static int to_slot(struct nvdimm_drvdata *ndd,
211                 struct nd_namespace_label *nd_label)
212 {
213         return nd_label - nd_label_base(ndd);
214 }
215
216 #define for_each_clear_bit_le(bit, addr, size) \
217         for ((bit) = find_next_zero_bit_le((addr), (size), 0);  \
218              (bit) < (size);                                    \
219              (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
220
221 /**
222  * preamble_index - common variable initialization for nd_label_* routines
223  * @ndd: dimm container for the relevant label set
224  * @idx: namespace_index index
225  * @nsindex_out: on return set to the currently active namespace index
226  * @free: on return set to the free label bitmap in the index
227  * @nslot: on return set to the number of slots in the label space
228  */
229 static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
230                 struct nd_namespace_index **nsindex_out,
231                 unsigned long **free, u32 *nslot)
232 {
233         struct nd_namespace_index *nsindex;
234
235         nsindex = to_namespace_index(ndd, idx);
236         if (nsindex == NULL)
237                 return false;
238
239         *free = (unsigned long *) nsindex->free;
240         *nslot = __le32_to_cpu(nsindex->nslot);
241         *nsindex_out = nsindex;
242
243         return true;
244 }
245
246 char *nd_label_gen_id(struct nd_label_id *label_id, u8 *uuid, u32 flags)
247 {
248         if (!label_id || !uuid)
249                 return NULL;
250         snprintf(label_id->id, ND_LABEL_ID_SIZE, "%s-%pUb",
251                         flags & NSLABEL_FLAG_LOCAL ? "blk" : "pmem", uuid);
252         return label_id->id;
253 }
254
255 static bool preamble_current(struct nvdimm_drvdata *ndd,
256                 struct nd_namespace_index **nsindex,
257                 unsigned long **free, u32 *nslot)
258 {
259         return preamble_index(ndd, ndd->ns_current, nsindex,
260                         free, nslot);
261 }
262
263 static bool preamble_next(struct nvdimm_drvdata *ndd,
264                 struct nd_namespace_index **nsindex,
265                 unsigned long **free, u32 *nslot)
266 {
267         return preamble_index(ndd, ndd->ns_next, nsindex,
268                         free, nslot);
269 }
270
271 static bool slot_valid(struct nd_namespace_label *nd_label, u32 slot)
272 {
273         /* check that we are written where we expect to be written */
274         if (slot != __le32_to_cpu(nd_label->slot))
275                 return false;
276
277         /* check that DPA allocations are page aligned */
278         if ((__le64_to_cpu(nd_label->dpa)
279                                 | __le64_to_cpu(nd_label->rawsize)) % SZ_4K)
280                 return false;
281
282         return true;
283 }
284
285 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
286 {
287         struct nd_namespace_index *nsindex;
288         unsigned long *free;
289         u32 nslot, slot;
290
291         if (!preamble_current(ndd, &nsindex, &free, &nslot))
292                 return 0; /* no label, nothing to reserve */
293
294         for_each_clear_bit_le(slot, free, nslot) {
295                 struct nd_namespace_label *nd_label;
296                 struct nd_region *nd_region = NULL;
297                 u8 label_uuid[NSLABEL_UUID_LEN];
298                 struct nd_label_id label_id;
299                 struct resource *res;
300                 u32 flags;
301
302                 nd_label = nd_label_base(ndd) + slot;
303
304                 if (!slot_valid(nd_label, slot))
305                         continue;
306
307                 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
308                 flags = __le32_to_cpu(nd_label->flags);
309                 nd_label_gen_id(&label_id, label_uuid, flags);
310                 res = nvdimm_allocate_dpa(ndd, &label_id,
311                                 __le64_to_cpu(nd_label->dpa),
312                                 __le64_to_cpu(nd_label->rawsize));
313                 nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
314                 if (!res)
315                         return -EBUSY;
316         }
317
318         return 0;
319 }
320
321 int nd_label_active_count(struct nvdimm_drvdata *ndd)
322 {
323         struct nd_namespace_index *nsindex;
324         unsigned long *free;
325         u32 nslot, slot;
326         int count = 0;
327
328         if (!preamble_current(ndd, &nsindex, &free, &nslot))
329                 return 0;
330
331         for_each_clear_bit_le(slot, free, nslot) {
332                 struct nd_namespace_label *nd_label;
333
334                 nd_label = nd_label_base(ndd) + slot;
335
336                 if (!slot_valid(nd_label, slot)) {
337                         u32 label_slot = __le32_to_cpu(nd_label->slot);
338                         u64 size = __le64_to_cpu(nd_label->rawsize);
339                         u64 dpa = __le64_to_cpu(nd_label->dpa);
340
341                         dev_dbg(ndd->dev,
342                                 "%s: slot%d invalid slot: %d dpa: %llx size: %llx\n",
343                                         __func__, slot, label_slot, dpa, size);
344                         continue;
345                 }
346                 count++;
347         }
348         return count;
349 }
350
351 struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
352 {
353         struct nd_namespace_index *nsindex;
354         unsigned long *free;
355         u32 nslot, slot;
356
357         if (!preamble_current(ndd, &nsindex, &free, &nslot))
358                 return NULL;
359
360         for_each_clear_bit_le(slot, free, nslot) {
361                 struct nd_namespace_label *nd_label;
362
363                 nd_label = nd_label_base(ndd) + slot;
364                 if (!slot_valid(nd_label, slot))
365                         continue;
366
367                 if (n-- == 0)
368                         return nd_label_base(ndd) + slot;
369         }
370
371         return NULL;
372 }
373
374 static u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
375 {
376         struct nd_namespace_index *nsindex;
377         unsigned long *free;
378         u32 nslot, slot;
379
380         if (!preamble_next(ndd, &nsindex, &free, &nslot))
381                 return UINT_MAX;
382
383         WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
384
385         slot = find_next_bit_le(free, nslot, 0);
386         if (slot == nslot)
387                 return UINT_MAX;
388
389         clear_bit_le(slot, free);
390
391         return slot;
392 }
393
394 static bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
395 {
396         struct nd_namespace_index *nsindex;
397         unsigned long *free;
398         u32 nslot;
399
400         if (!preamble_next(ndd, &nsindex, &free, &nslot))
401                 return false;
402
403         WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
404
405         if (slot < nslot)
406                 return !test_and_set_bit_le(slot, free);
407         return false;
408 }
409
410 u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
411 {
412         struct nd_namespace_index *nsindex;
413         unsigned long *free;
414         u32 nslot;
415
416         WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
417
418         if (!preamble_next(ndd, &nsindex, &free, &nslot))
419                 return 0;
420
421         return bitmap_weight(free, nslot);
422 }
423
424 static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
425                 unsigned long flags)
426 {
427         struct nd_namespace_index *nsindex;
428         unsigned long offset;
429         u64 checksum;
430         u32 nslot;
431         int rc;
432
433         nsindex = to_namespace_index(ndd, index);
434         if (flags & ND_NSINDEX_INIT)
435                 nslot = nvdimm_num_label_slots(ndd);
436         else
437                 nslot = __le32_to_cpu(nsindex->nslot);
438
439         memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
440         nsindex->flags = __cpu_to_le32(0);
441         nsindex->seq = __cpu_to_le32(seq);
442         offset = (unsigned long) nsindex
443                 - (unsigned long) to_namespace_index(ndd, 0);
444         nsindex->myoff = __cpu_to_le64(offset);
445         nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
446         offset = (unsigned long) to_namespace_index(ndd,
447                         nd_label_next_nsindex(index))
448                 - (unsigned long) to_namespace_index(ndd, 0);
449         nsindex->otheroff = __cpu_to_le64(offset);
450         offset = (unsigned long) nd_label_base(ndd)
451                 - (unsigned long) to_namespace_index(ndd, 0);
452         nsindex->labeloff = __cpu_to_le64(offset);
453         nsindex->nslot = __cpu_to_le32(nslot);
454         nsindex->major = __cpu_to_le16(1);
455         nsindex->minor = __cpu_to_le16(1);
456         nsindex->checksum = __cpu_to_le64(0);
457         if (flags & ND_NSINDEX_INIT) {
458                 unsigned long *free = (unsigned long *) nsindex->free;
459                 u32 nfree = ALIGN(nslot, BITS_PER_LONG);
460                 int last_bits, i;
461
462                 memset(nsindex->free, 0xff, nfree / 8);
463                 for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
464                         clear_bit_le(nslot + i, free);
465         }
466         checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
467         nsindex->checksum = __cpu_to_le64(checksum);
468         rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
469                         nsindex, sizeof_namespace_index(ndd));
470         if (rc < 0)
471                 return rc;
472
473         if (flags & ND_NSINDEX_INIT)
474                 return 0;
475
476         /* copy the index we just wrote to the new 'next' */
477         WARN_ON(index != ndd->ns_next);
478         nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
479         ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
480         ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
481         WARN_ON(ndd->ns_current == ndd->ns_next);
482
483         return 0;
484 }
485
486 static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
487                 struct nd_namespace_label *nd_label)
488 {
489         return (unsigned long) nd_label
490                 - (unsigned long) to_namespace_index(ndd, 0);
491 }
492
493 static int __pmem_label_update(struct nd_region *nd_region,
494                 struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
495                 int pos)
496 {
497         u64 cookie = nd_region_interleave_set_cookie(nd_region), rawsize;
498         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
499         struct nd_namespace_label *victim_label;
500         struct nd_namespace_label *nd_label;
501         struct nd_namespace_index *nsindex;
502         unsigned long *free;
503         u32 nslot, slot;
504         size_t offset;
505         int rc;
506
507         if (!preamble_next(ndd, &nsindex, &free, &nslot))
508                 return -ENXIO;
509
510         /* allocate and write the label to the staging (next) index */
511         slot = nd_label_alloc_slot(ndd);
512         if (slot == UINT_MAX)
513                 return -ENXIO;
514         dev_dbg(ndd->dev, "%s: allocated: %d\n", __func__, slot);
515
516         nd_label = nd_label_base(ndd) + slot;
517         memset(nd_label, 0, sizeof(struct nd_namespace_label));
518         memcpy(nd_label->uuid, nspm->uuid, NSLABEL_UUID_LEN);
519         if (nspm->alt_name)
520                 memcpy(nd_label->name, nspm->alt_name, NSLABEL_NAME_LEN);
521         nd_label->flags = __cpu_to_le32(NSLABEL_FLAG_UPDATING);
522         nd_label->nlabel = __cpu_to_le16(nd_region->ndr_mappings);
523         nd_label->position = __cpu_to_le16(pos);
524         nd_label->isetcookie = __cpu_to_le64(cookie);
525         rawsize = div_u64(resource_size(&nspm->nsio.res),
526                         nd_region->ndr_mappings);
527         nd_label->rawsize = __cpu_to_le64(rawsize);
528         nd_label->dpa = __cpu_to_le64(nd_mapping->start);
529         nd_label->slot = __cpu_to_le32(slot);
530
531         /* update label */
532         offset = nd_label_offset(ndd, nd_label);
533         rc = nvdimm_set_config_data(ndd, offset, nd_label,
534                         sizeof(struct nd_namespace_label));
535         if (rc < 0)
536                 return rc;
537
538         /* Garbage collect the previous label */
539         victim_label = nd_mapping->labels[0];
540         if (victim_label) {
541                 slot = to_slot(ndd, victim_label);
542                 nd_label_free_slot(ndd, slot);
543                 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
544         }
545
546         /* update index */
547         rc = nd_label_write_index(ndd, ndd->ns_next,
548                         nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
549         if (rc < 0)
550                 return rc;
551
552         nd_mapping->labels[0] = nd_label;
553
554         return 0;
555 }
556
557 static int init_labels(struct nd_mapping *nd_mapping)
558 {
559         int i;
560         struct nd_namespace_index *nsindex;
561         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
562
563         if (!nd_mapping->labels)
564                 nd_mapping->labels = kcalloc(2, sizeof(void *), GFP_KERNEL);
565
566         if (!nd_mapping->labels)
567                 return -ENOMEM;
568
569         if (ndd->ns_current == -1 || ndd->ns_next == -1)
570                 /* pass */;
571         else
572                 return 0;
573
574         nsindex = to_namespace_index(ndd, 0);
575         memset(nsindex, 0, ndd->nsarea.config_size);
576         for (i = 0; i < 2; i++) {
577                 int rc = nd_label_write_index(ndd, i, i*2, ND_NSINDEX_INIT);
578
579                 if (rc)
580                         return rc;
581         }
582         ndd->ns_next = 1;
583         ndd->ns_current = 0;
584
585         return 0;
586 }
587
588 static int del_labels(struct nd_mapping *nd_mapping, u8 *uuid)
589 {
590         struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
591         struct nd_namespace_label *nd_label;
592         struct nd_namespace_index *nsindex;
593         u8 label_uuid[NSLABEL_UUID_LEN];
594         int l, num_freed = 0;
595         unsigned long *free;
596         u32 nslot, slot;
597
598         if (!uuid)
599                 return 0;
600
601         /* no index || no labels == nothing to delete */
602         if (!preamble_next(ndd, &nsindex, &free, &nslot)
603                         || !nd_mapping->labels)
604                 return 0;
605
606         for_each_label(l, nd_label, nd_mapping->labels) {
607                 int j;
608
609                 memcpy(label_uuid, nd_label->uuid, NSLABEL_UUID_LEN);
610                 if (memcmp(label_uuid, uuid, NSLABEL_UUID_LEN) != 0)
611                         continue;
612                 slot = to_slot(ndd, nd_label);
613                 nd_label_free_slot(ndd, slot);
614                 dev_dbg(ndd->dev, "%s: free: %d\n", __func__, slot);
615                 for (j = l; nd_mapping->labels[j + 1]; j++) {
616                         struct nd_namespace_label *next_label;
617
618                         next_label = nd_mapping->labels[j + 1];
619                         nd_mapping->labels[j] = next_label;
620                 }
621                 nd_mapping->labels[j] = NULL;
622                 num_freed++;
623         }
624
625         if (num_freed > l) {
626                 /*
627                  * num_freed will only ever be > l when we delete the last
628                  * label
629                  */
630                 kfree(nd_mapping->labels);
631                 nd_mapping->labels = NULL;
632                 dev_dbg(ndd->dev, "%s: no more labels\n", __func__);
633         }
634
635         return nd_label_write_index(ndd, ndd->ns_next,
636                         nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
637 }
638
639 int nd_pmem_namespace_label_update(struct nd_region *nd_region,
640                 struct nd_namespace_pmem *nspm, resource_size_t size)
641 {
642         int i;
643
644         for (i = 0; i < nd_region->ndr_mappings; i++) {
645                 struct nd_mapping *nd_mapping = &nd_region->mapping[i];
646                 int rc;
647
648                 if (size == 0) {
649                         rc = del_labels(nd_mapping, nspm->uuid);
650                         if (rc)
651                                 return rc;
652                         continue;
653                 }
654
655                 rc = init_labels(nd_mapping);
656                 if (rc)
657                         return rc;
658
659                 rc = __pmem_label_update(nd_region, nd_mapping, nspm, i);
660                 if (rc)
661                         return rc;
662         }
663
664         return 0;
665 }