libnvdimm/label: Add a helper for nlabel validation
[linux-2.6-microblaze.git] / drivers / nvdimm / nd.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #ifndef __ND_H__
6 #define __ND_H__
7 #include <linux/libnvdimm.h>
8 #include <linux/badblocks.h>
9 #include <linux/blkdev.h>
10 #include <linux/device.h>
11 #include <linux/mutex.h>
12 #include <linux/ndctl.h>
13 #include <linux/types.h>
14 #include <linux/nd.h>
15 #include "label.h"
16
17 enum {
18         /*
19          * Limits the maximum number of block apertures a dimm can
20          * support and is an input to the geometry/on-disk-format of a
21          * BTT instance
22          */
23         ND_MAX_LANES = 256,
24         INT_LBASIZE_ALIGNMENT = 64,
25         NVDIMM_IO_ATOMIC = 1,
26 };
27
28 struct nvdimm_drvdata {
29         struct device *dev;
30         int nslabel_size;
31         struct nd_cmd_get_config_size nsarea;
32         void *data;
33         int ns_current, ns_next;
34         struct resource dpa;
35         struct kref kref;
36 };
37
38 static inline const u8 *nsl_ref_name(struct nvdimm_drvdata *ndd,
39                                      struct nd_namespace_label *nd_label)
40 {
41         return nd_label->name;
42 }
43
44 static inline u8 *nsl_get_name(struct nvdimm_drvdata *ndd,
45                                struct nd_namespace_label *nd_label, u8 *name)
46 {
47         return memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
48 }
49
50 static inline u8 *nsl_set_name(struct nvdimm_drvdata *ndd,
51                                struct nd_namespace_label *nd_label, u8 *name)
52 {
53         if (!name)
54                 return NULL;
55         return memcpy(nd_label->name, name, NSLABEL_NAME_LEN);
56 }
57
58 static inline u32 nsl_get_slot(struct nvdimm_drvdata *ndd,
59                                struct nd_namespace_label *nd_label)
60 {
61         return __le32_to_cpu(nd_label->slot);
62 }
63
64 static inline void nsl_set_slot(struct nvdimm_drvdata *ndd,
65                                 struct nd_namespace_label *nd_label, u32 slot)
66 {
67         nd_label->slot = __cpu_to_le32(slot);
68 }
69
70 static inline u64 nsl_get_checksum(struct nvdimm_drvdata *ndd,
71                                    struct nd_namespace_label *nd_label)
72 {
73         return __le64_to_cpu(nd_label->checksum);
74 }
75
76 static inline void nsl_set_checksum(struct nvdimm_drvdata *ndd,
77                                     struct nd_namespace_label *nd_label,
78                                     u64 checksum)
79 {
80         nd_label->checksum = __cpu_to_le64(checksum);
81 }
82
83 static inline u32 nsl_get_flags(struct nvdimm_drvdata *ndd,
84                                 struct nd_namespace_label *nd_label)
85 {
86         return __le32_to_cpu(nd_label->flags);
87 }
88
89 static inline void nsl_set_flags(struct nvdimm_drvdata *ndd,
90                                  struct nd_namespace_label *nd_label, u32 flags)
91 {
92         nd_label->flags = __cpu_to_le32(flags);
93 }
94
95 static inline u64 nsl_get_dpa(struct nvdimm_drvdata *ndd,
96                               struct nd_namespace_label *nd_label)
97 {
98         return __le64_to_cpu(nd_label->dpa);
99 }
100
101 static inline void nsl_set_dpa(struct nvdimm_drvdata *ndd,
102                                struct nd_namespace_label *nd_label, u64 dpa)
103 {
104         nd_label->dpa = __cpu_to_le64(dpa);
105 }
106
107 static inline u64 nsl_get_rawsize(struct nvdimm_drvdata *ndd,
108                                   struct nd_namespace_label *nd_label)
109 {
110         return __le64_to_cpu(nd_label->rawsize);
111 }
112
113 static inline void nsl_set_rawsize(struct nvdimm_drvdata *ndd,
114                                    struct nd_namespace_label *nd_label,
115                                    u64 rawsize)
116 {
117         nd_label->rawsize = __cpu_to_le64(rawsize);
118 }
119
120 static inline u64 nsl_get_isetcookie(struct nvdimm_drvdata *ndd,
121                                      struct nd_namespace_label *nd_label)
122 {
123         return __le64_to_cpu(nd_label->isetcookie);
124 }
125
126 static inline void nsl_set_isetcookie(struct nvdimm_drvdata *ndd,
127                                       struct nd_namespace_label *nd_label,
128                                       u64 isetcookie)
129 {
130         nd_label->isetcookie = __cpu_to_le64(isetcookie);
131 }
132
133 static inline bool nsl_validate_isetcookie(struct nvdimm_drvdata *ndd,
134                                            struct nd_namespace_label *nd_label,
135                                            u64 cookie)
136 {
137         return cookie == __le64_to_cpu(nd_label->isetcookie);
138 }
139
140 static inline u16 nsl_get_position(struct nvdimm_drvdata *ndd,
141                                    struct nd_namespace_label *nd_label)
142 {
143         return __le16_to_cpu(nd_label->position);
144 }
145
146 static inline void nsl_set_position(struct nvdimm_drvdata *ndd,
147                                     struct nd_namespace_label *nd_label,
148                                     u16 position)
149 {
150         nd_label->position = __cpu_to_le16(position);
151 }
152
153
154 static inline u16 nsl_get_nlabel(struct nvdimm_drvdata *ndd,
155                                  struct nd_namespace_label *nd_label)
156 {
157         return __le16_to_cpu(nd_label->nlabel);
158 }
159
160 static inline void nsl_set_nlabel(struct nvdimm_drvdata *ndd,
161                                   struct nd_namespace_label *nd_label,
162                                   u16 nlabel)
163 {
164         nd_label->nlabel = __cpu_to_le16(nlabel);
165 }
166
167 static inline u64 nsl_get_lbasize(struct nvdimm_drvdata *ndd,
168                                   struct nd_namespace_label *nd_label)
169 {
170         return __le64_to_cpu(nd_label->lbasize);
171 }
172
173 static inline void nsl_set_lbasize(struct nvdimm_drvdata *ndd,
174                                    struct nd_namespace_label *nd_label,
175                                    u64 lbasize)
176 {
177         nd_label->lbasize = __cpu_to_le64(lbasize);
178 }
179
180 static inline const uuid_t *nsl_get_uuid(struct nvdimm_drvdata *ndd,
181                                          struct nd_namespace_label *nd_label,
182                                          uuid_t *uuid)
183 {
184         import_uuid(uuid, nd_label->uuid);
185         return uuid;
186 }
187
188 static inline const uuid_t *nsl_set_uuid(struct nvdimm_drvdata *ndd,
189                                          struct nd_namespace_label *nd_label,
190                                          const uuid_t *uuid)
191 {
192         export_uuid(nd_label->uuid, uuid);
193         return uuid;
194 }
195
196 static inline bool nsl_uuid_equal(struct nvdimm_drvdata *ndd,
197                                   struct nd_namespace_label *nd_label,
198                                   const uuid_t *uuid)
199 {
200         uuid_t tmp;
201
202         import_uuid(&tmp, nd_label->uuid);
203         return uuid_equal(&tmp, uuid);
204 }
205
206 static inline const u8 *nsl_uuid_raw(struct nvdimm_drvdata *ndd,
207                                      struct nd_namespace_label *nd_label)
208 {
209         return nd_label->uuid;
210 }
211
212 bool nsl_validate_blk_isetcookie(struct nvdimm_drvdata *ndd,
213                                  struct nd_namespace_label *nd_label,
214                                  u64 isetcookie);
215 bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
216                             struct nd_namespace_label *nd_label, guid_t *guid);
217 enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
218                                             struct nd_namespace_label *nd_label);
219
220 struct nd_region_data {
221         int ns_count;
222         int ns_active;
223         unsigned int hints_shift;
224         void __iomem *flush_wpq[];
225 };
226
227 static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
228                 int dimm, int hint)
229 {
230         unsigned int num = 1 << ndrd->hints_shift;
231         unsigned int mask = num - 1;
232
233         return ndrd->flush_wpq[dimm * num + (hint & mask)];
234 }
235
236 static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
237                 int hint, void __iomem *flush)
238 {
239         unsigned int num = 1 << ndrd->hints_shift;
240         unsigned int mask = num - 1;
241
242         ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
243 }
244
245 static inline struct nd_namespace_index *to_namespace_index(
246                 struct nvdimm_drvdata *ndd, int i)
247 {
248         if (i < 0)
249                 return NULL;
250
251         return ndd->data + sizeof_namespace_index(ndd) * i;
252 }
253
254 static inline struct nd_namespace_index *to_current_namespace_index(
255                 struct nvdimm_drvdata *ndd)
256 {
257         return to_namespace_index(ndd, ndd->ns_current);
258 }
259
260 static inline struct nd_namespace_index *to_next_namespace_index(
261                 struct nvdimm_drvdata *ndd)
262 {
263         return to_namespace_index(ndd, ndd->ns_next);
264 }
265
266 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
267
268 #define namespace_label_has(ndd, field) \
269         (offsetof(struct nd_namespace_label, field) \
270                 < sizeof_namespace_label(ndd))
271
272 #define nd_dbg_dpa(r, d, res, fmt, arg...) \
273         dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
274                 (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
275                 (unsigned long long) (res ? resource_size(res) : 0), \
276                 (unsigned long long) (res ? res->start : 0), ##arg)
277
278 #define for_each_dpa_resource(ndd, res) \
279         for (res = (ndd)->dpa.child; res; res = res->sibling)
280
281 #define for_each_dpa_resource_safe(ndd, res, next) \
282         for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
283                         res; res = next, next = next ? next->sibling : NULL)
284
285 struct nd_percpu_lane {
286         int count;
287         spinlock_t lock;
288 };
289
290 enum nd_label_flags {
291         ND_LABEL_REAP,
292 };
293 struct nd_label_ent {
294         struct list_head list;
295         unsigned long flags;
296         struct nd_namespace_label *label;
297 };
298
299 enum nd_mapping_lock_class {
300         ND_MAPPING_CLASS0,
301         ND_MAPPING_UUID_SCAN,
302 };
303
304 struct nd_mapping {
305         struct nvdimm *nvdimm;
306         u64 start;
307         u64 size;
308         int position;
309         struct list_head labels;
310         struct mutex lock;
311         /*
312          * @ndd is for private use at region enable / disable time for
313          * get_ndd() + put_ndd(), all other nd_mapping to ndd
314          * conversions use to_ndd() which respects enabled state of the
315          * nvdimm.
316          */
317         struct nvdimm_drvdata *ndd;
318 };
319
320 struct nd_region {
321         struct device dev;
322         struct ida ns_ida;
323         struct ida btt_ida;
324         struct ida pfn_ida;
325         struct ida dax_ida;
326         unsigned long flags;
327         struct device *ns_seed;
328         struct device *btt_seed;
329         struct device *pfn_seed;
330         struct device *dax_seed;
331         unsigned long align;
332         u16 ndr_mappings;
333         u64 ndr_size;
334         u64 ndr_start;
335         int id, num_lanes, ro, numa_node, target_node;
336         void *provider_data;
337         struct kernfs_node *bb_state;
338         struct badblocks bb;
339         struct nd_interleave_set *nd_set;
340         struct nd_percpu_lane __percpu *lane;
341         int (*flush)(struct nd_region *nd_region, struct bio *bio);
342         struct nd_mapping mapping[];
343 };
344
345 static inline bool nsl_validate_nlabel(struct nd_region *nd_region,
346                                        struct nvdimm_drvdata *ndd,
347                                        struct nd_namespace_label *nd_label)
348 {
349         return nsl_get_nlabel(ndd, nd_label) == nd_region->ndr_mappings;
350 }
351
352 struct nd_blk_region {
353         int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
354         int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
355                         void *iobuf, u64 len, int rw);
356         void *blk_provider_data;
357         struct nd_region nd_region;
358 };
359
360 /*
361  * Lookup next in the repeating sequence of 01, 10, and 11.
362  */
363 static inline unsigned nd_inc_seq(unsigned seq)
364 {
365         static const unsigned next[] = { 0, 2, 3, 1 };
366
367         return next[seq & 3];
368 }
369
370 struct btt;
371 struct nd_btt {
372         struct device dev;
373         struct nd_namespace_common *ndns;
374         struct btt *btt;
375         unsigned long lbasize;
376         u64 size;
377         uuid_t *uuid;
378         int id;
379         int initial_offset;
380         u16 version_major;
381         u16 version_minor;
382 };
383
384 enum nd_pfn_mode {
385         PFN_MODE_NONE,
386         PFN_MODE_RAM,
387         PFN_MODE_PMEM,
388 };
389
390 struct nd_pfn {
391         int id;
392         uuid_t *uuid;
393         struct device dev;
394         unsigned long align;
395         unsigned long npfns;
396         enum nd_pfn_mode mode;
397         struct nd_pfn_sb *pfn_sb;
398         struct nd_namespace_common *ndns;
399 };
400
401 struct nd_dax {
402         struct nd_pfn nd_pfn;
403 };
404
405 static inline u32 nd_info_block_reserve(void)
406 {
407         return ALIGN(SZ_8K, PAGE_SIZE);
408 }
409
410 enum nd_async_mode {
411         ND_SYNC,
412         ND_ASYNC,
413 };
414
415 int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
416 void wait_nvdimm_bus_probe_idle(struct device *dev);
417 void nd_device_register(struct device *dev);
418 void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
419 void nd_device_notify(struct device *dev, enum nvdimm_event event);
420 int nd_uuid_store(struct device *dev, uuid_t **uuid_out, const char *buf,
421                 size_t len);
422 ssize_t nd_size_select_show(unsigned long current_size,
423                 const unsigned long *supported, char *buf);
424 ssize_t nd_size_select_store(struct device *dev, const char *buf,
425                 unsigned long *current_size, const unsigned long *supported);
426 int __init nvdimm_init(void);
427 int __init nd_region_init(void);
428 int __init nd_label_init(void);
429 void nvdimm_exit(void);
430 void nd_region_exit(void);
431 struct nvdimm;
432 extern const struct attribute_group nd_device_attribute_group;
433 extern const struct attribute_group nd_numa_attribute_group;
434 extern const struct attribute_group *nvdimm_bus_attribute_groups[];
435 struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
436 int nvdimm_check_config_data(struct device *dev);
437 int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
438 int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
439 int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
440                            size_t offset, size_t len);
441 int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
442                 void *buf, size_t len);
443 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
444                 unsigned int len);
445 void nvdimm_set_labeling(struct device *dev);
446 void nvdimm_set_locked(struct device *dev);
447 void nvdimm_clear_locked(struct device *dev);
448 int nvdimm_security_setup_events(struct device *dev);
449 #if IS_ENABLED(CONFIG_NVDIMM_KEYS)
450 int nvdimm_security_unlock(struct device *dev);
451 #else
452 static inline int nvdimm_security_unlock(struct device *dev)
453 {
454         return 0;
455 }
456 #endif
457 struct nd_btt *to_nd_btt(struct device *dev);
458
459 struct nd_gen_sb {
460         char reserved[SZ_4K - 8];
461         __le64 checksum;
462 };
463
464 u64 nd_sb_checksum(struct nd_gen_sb *sb);
465 #if IS_ENABLED(CONFIG_BTT)
466 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
467 bool is_nd_btt(struct device *dev);
468 struct device *nd_btt_create(struct nd_region *nd_region);
469 #else
470 static inline int nd_btt_probe(struct device *dev,
471                 struct nd_namespace_common *ndns)
472 {
473         return -ENODEV;
474 }
475
476 static inline bool is_nd_btt(struct device *dev)
477 {
478         return false;
479 }
480
481 static inline struct device *nd_btt_create(struct nd_region *nd_region)
482 {
483         return NULL;
484 }
485 #endif
486
487 struct nd_pfn *to_nd_pfn(struct device *dev);
488 #if IS_ENABLED(CONFIG_NVDIMM_PFN)
489
490 #define MAX_NVDIMM_ALIGN        4
491
492 int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
493 bool is_nd_pfn(struct device *dev);
494 struct device *nd_pfn_create(struct nd_region *nd_region);
495 struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
496                 struct nd_namespace_common *ndns);
497 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
498 extern const struct attribute_group *nd_pfn_attribute_groups[];
499 #else
500 static inline int nd_pfn_probe(struct device *dev,
501                 struct nd_namespace_common *ndns)
502 {
503         return -ENODEV;
504 }
505
506 static inline bool is_nd_pfn(struct device *dev)
507 {
508         return false;
509 }
510
511 static inline struct device *nd_pfn_create(struct nd_region *nd_region)
512 {
513         return NULL;
514 }
515
516 static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
517 {
518         return -ENODEV;
519 }
520 #endif
521
522 struct nd_dax *to_nd_dax(struct device *dev);
523 #if IS_ENABLED(CONFIG_NVDIMM_DAX)
524 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
525 bool is_nd_dax(struct device *dev);
526 struct device *nd_dax_create(struct nd_region *nd_region);
527 #else
528 static inline int nd_dax_probe(struct device *dev,
529                 struct nd_namespace_common *ndns)
530 {
531         return -ENODEV;
532 }
533
534 static inline bool is_nd_dax(struct device *dev)
535 {
536         return false;
537 }
538
539 static inline struct device *nd_dax_create(struct nd_region *nd_region)
540 {
541         return NULL;
542 }
543 #endif
544
545 int nd_region_to_nstype(struct nd_region *nd_region);
546 int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
547 u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
548                 struct nd_namespace_index *nsindex);
549 u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
550 void nvdimm_bus_lock(struct device *dev);
551 void nvdimm_bus_unlock(struct device *dev);
552 bool is_nvdimm_bus_locked(struct device *dev);
553 void nvdimm_check_and_set_ro(struct gendisk *disk);
554 void nvdimm_drvdata_release(struct kref *kref);
555 void put_ndd(struct nvdimm_drvdata *ndd);
556 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
557 void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
558 struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
559                 struct nd_label_id *label_id, resource_size_t start,
560                 resource_size_t n);
561 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
562 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns);
563 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
564 int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
565 int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
566 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
567                 char *name);
568 unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
569 struct range;
570 void nvdimm_badblocks_populate(struct nd_region *nd_region,
571                 struct badblocks *bb, const struct range *range);
572 int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
573                 resource_size_t size);
574 void devm_namespace_disable(struct device *dev,
575                 struct nd_namespace_common *ndns);
576 #if IS_ENABLED(CONFIG_ND_CLAIM)
577 /* max struct page size independent of kernel config */
578 #define MAX_STRUCT_PAGE_SIZE 64
579 int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap);
580 #else
581 static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
582                                    struct dev_pagemap *pgmap)
583 {
584         return -ENXIO;
585 }
586 #endif
587 int nd_blk_region_init(struct nd_region *nd_region);
588 int nd_region_activate(struct nd_region *nd_region);
589 static inline bool is_bad_pmem(struct badblocks *bb, sector_t sector,
590                 unsigned int len)
591 {
592         if (bb->count) {
593                 sector_t first_bad;
594                 int num_bad;
595
596                 return !!badblocks_check(bb, sector, len / 512, &first_bad,
597                                 &num_bad);
598         }
599
600         return false;
601 }
602 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
603 const uuid_t *nd_dev_to_uuid(struct device *dev);
604 bool pmem_should_map_pages(struct device *dev);
605 #endif /* __ND_H__ */