libnvdimm/labels: Add isetcookie validation helper
[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 u32 nsl_get_slot(struct nvdimm_drvdata *ndd,
51                                struct nd_namespace_label *nd_label)
52 {
53         return __le32_to_cpu(nd_label->slot);
54 }
55
56 static inline u64 nsl_get_checksum(struct nvdimm_drvdata *ndd,
57                                    struct nd_namespace_label *nd_label)
58 {
59         return __le64_to_cpu(nd_label->checksum);
60 }
61
62 static inline u32 nsl_get_flags(struct nvdimm_drvdata *ndd,
63                                 struct nd_namespace_label *nd_label)
64 {
65         return __le32_to_cpu(nd_label->flags);
66 }
67
68 static inline u64 nsl_get_dpa(struct nvdimm_drvdata *ndd,
69                               struct nd_namespace_label *nd_label)
70 {
71         return __le64_to_cpu(nd_label->dpa);
72 }
73
74 static inline u64 nsl_get_rawsize(struct nvdimm_drvdata *ndd,
75                                   struct nd_namespace_label *nd_label)
76 {
77         return __le64_to_cpu(nd_label->rawsize);
78 }
79
80 static inline u64 nsl_get_isetcookie(struct nvdimm_drvdata *ndd,
81                                      struct nd_namespace_label *nd_label)
82 {
83         return __le64_to_cpu(nd_label->isetcookie);
84 }
85
86 static inline bool nsl_validate_isetcookie(struct nvdimm_drvdata *ndd,
87                                            struct nd_namespace_label *nd_label,
88                                            u64 cookie)
89 {
90         return cookie == __le64_to_cpu(nd_label->isetcookie);
91 }
92
93 static inline u16 nsl_get_position(struct nvdimm_drvdata *ndd,
94                                    struct nd_namespace_label *nd_label)
95 {
96         return __le16_to_cpu(nd_label->position);
97 }
98
99 static inline u16 nsl_get_nlabel(struct nvdimm_drvdata *ndd,
100                                  struct nd_namespace_label *nd_label)
101 {
102         return __le16_to_cpu(nd_label->nlabel);
103 }
104
105 static inline u64 nsl_get_lbasize(struct nvdimm_drvdata *ndd,
106                                   struct nd_namespace_label *nd_label)
107 {
108         return __le64_to_cpu(nd_label->lbasize);
109 }
110
111 struct nd_region_data {
112         int ns_count;
113         int ns_active;
114         unsigned int hints_shift;
115         void __iomem *flush_wpq[];
116 };
117
118 static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
119                 int dimm, int hint)
120 {
121         unsigned int num = 1 << ndrd->hints_shift;
122         unsigned int mask = num - 1;
123
124         return ndrd->flush_wpq[dimm * num + (hint & mask)];
125 }
126
127 static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
128                 int hint, void __iomem *flush)
129 {
130         unsigned int num = 1 << ndrd->hints_shift;
131         unsigned int mask = num - 1;
132
133         ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
134 }
135
136 static inline struct nd_namespace_index *to_namespace_index(
137                 struct nvdimm_drvdata *ndd, int i)
138 {
139         if (i < 0)
140                 return NULL;
141
142         return ndd->data + sizeof_namespace_index(ndd) * i;
143 }
144
145 static inline struct nd_namespace_index *to_current_namespace_index(
146                 struct nvdimm_drvdata *ndd)
147 {
148         return to_namespace_index(ndd, ndd->ns_current);
149 }
150
151 static inline struct nd_namespace_index *to_next_namespace_index(
152                 struct nvdimm_drvdata *ndd)
153 {
154         return to_namespace_index(ndd, ndd->ns_next);
155 }
156
157 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
158
159 #define namespace_label_has(ndd, field) \
160         (offsetof(struct nd_namespace_label, field) \
161                 < sizeof_namespace_label(ndd))
162
163 #define nd_dbg_dpa(r, d, res, fmt, arg...) \
164         dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
165                 (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
166                 (unsigned long long) (res ? resource_size(res) : 0), \
167                 (unsigned long long) (res ? res->start : 0), ##arg)
168
169 #define for_each_dpa_resource(ndd, res) \
170         for (res = (ndd)->dpa.child; res; res = res->sibling)
171
172 #define for_each_dpa_resource_safe(ndd, res, next) \
173         for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
174                         res; res = next, next = next ? next->sibling : NULL)
175
176 struct nd_percpu_lane {
177         int count;
178         spinlock_t lock;
179 };
180
181 enum nd_label_flags {
182         ND_LABEL_REAP,
183 };
184 struct nd_label_ent {
185         struct list_head list;
186         unsigned long flags;
187         struct nd_namespace_label *label;
188 };
189
190 enum nd_mapping_lock_class {
191         ND_MAPPING_CLASS0,
192         ND_MAPPING_UUID_SCAN,
193 };
194
195 struct nd_mapping {
196         struct nvdimm *nvdimm;
197         u64 start;
198         u64 size;
199         int position;
200         struct list_head labels;
201         struct mutex lock;
202         /*
203          * @ndd is for private use at region enable / disable time for
204          * get_ndd() + put_ndd(), all other nd_mapping to ndd
205          * conversions use to_ndd() which respects enabled state of the
206          * nvdimm.
207          */
208         struct nvdimm_drvdata *ndd;
209 };
210
211 struct nd_region {
212         struct device dev;
213         struct ida ns_ida;
214         struct ida btt_ida;
215         struct ida pfn_ida;
216         struct ida dax_ida;
217         unsigned long flags;
218         struct device *ns_seed;
219         struct device *btt_seed;
220         struct device *pfn_seed;
221         struct device *dax_seed;
222         unsigned long align;
223         u16 ndr_mappings;
224         u64 ndr_size;
225         u64 ndr_start;
226         int id, num_lanes, ro, numa_node, target_node;
227         void *provider_data;
228         struct kernfs_node *bb_state;
229         struct badblocks bb;
230         struct nd_interleave_set *nd_set;
231         struct nd_percpu_lane __percpu *lane;
232         int (*flush)(struct nd_region *nd_region, struct bio *bio);
233         struct nd_mapping mapping[];
234 };
235
236 struct nd_blk_region {
237         int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
238         int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
239                         void *iobuf, u64 len, int rw);
240         void *blk_provider_data;
241         struct nd_region nd_region;
242 };
243
244 /*
245  * Lookup next in the repeating sequence of 01, 10, and 11.
246  */
247 static inline unsigned nd_inc_seq(unsigned seq)
248 {
249         static const unsigned next[] = { 0, 2, 3, 1 };
250
251         return next[seq & 3];
252 }
253
254 struct btt;
255 struct nd_btt {
256         struct device dev;
257         struct nd_namespace_common *ndns;
258         struct btt *btt;
259         unsigned long lbasize;
260         u64 size;
261         u8 *uuid;
262         int id;
263         int initial_offset;
264         u16 version_major;
265         u16 version_minor;
266 };
267
268 enum nd_pfn_mode {
269         PFN_MODE_NONE,
270         PFN_MODE_RAM,
271         PFN_MODE_PMEM,
272 };
273
274 struct nd_pfn {
275         int id;
276         u8 *uuid;
277         struct device dev;
278         unsigned long align;
279         unsigned long npfns;
280         enum nd_pfn_mode mode;
281         struct nd_pfn_sb *pfn_sb;
282         struct nd_namespace_common *ndns;
283 };
284
285 struct nd_dax {
286         struct nd_pfn nd_pfn;
287 };
288
289 static inline u32 nd_info_block_reserve(void)
290 {
291         return ALIGN(SZ_8K, PAGE_SIZE);
292 }
293
294 enum nd_async_mode {
295         ND_SYNC,
296         ND_ASYNC,
297 };
298
299 int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
300 void wait_nvdimm_bus_probe_idle(struct device *dev);
301 void nd_device_register(struct device *dev);
302 void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
303 void nd_device_notify(struct device *dev, enum nvdimm_event event);
304 int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
305                 size_t len);
306 ssize_t nd_size_select_show(unsigned long current_size,
307                 const unsigned long *supported, char *buf);
308 ssize_t nd_size_select_store(struct device *dev, const char *buf,
309                 unsigned long *current_size, const unsigned long *supported);
310 int __init nvdimm_init(void);
311 int __init nd_region_init(void);
312 int __init nd_label_init(void);
313 void nvdimm_exit(void);
314 void nd_region_exit(void);
315 struct nvdimm;
316 extern const struct attribute_group nd_device_attribute_group;
317 extern const struct attribute_group nd_numa_attribute_group;
318 extern const struct attribute_group *nvdimm_bus_attribute_groups[];
319 struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
320 int nvdimm_check_config_data(struct device *dev);
321 int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
322 int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
323 int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
324                            size_t offset, size_t len);
325 int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
326                 void *buf, size_t len);
327 long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
328                 unsigned int len);
329 void nvdimm_set_labeling(struct device *dev);
330 void nvdimm_set_locked(struct device *dev);
331 void nvdimm_clear_locked(struct device *dev);
332 int nvdimm_security_setup_events(struct device *dev);
333 #if IS_ENABLED(CONFIG_NVDIMM_KEYS)
334 int nvdimm_security_unlock(struct device *dev);
335 #else
336 static inline int nvdimm_security_unlock(struct device *dev)
337 {
338         return 0;
339 }
340 #endif
341 struct nd_btt *to_nd_btt(struct device *dev);
342
343 struct nd_gen_sb {
344         char reserved[SZ_4K - 8];
345         __le64 checksum;
346 };
347
348 u64 nd_sb_checksum(struct nd_gen_sb *sb);
349 #if IS_ENABLED(CONFIG_BTT)
350 int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
351 bool is_nd_btt(struct device *dev);
352 struct device *nd_btt_create(struct nd_region *nd_region);
353 #else
354 static inline int nd_btt_probe(struct device *dev,
355                 struct nd_namespace_common *ndns)
356 {
357         return -ENODEV;
358 }
359
360 static inline bool is_nd_btt(struct device *dev)
361 {
362         return false;
363 }
364
365 static inline struct device *nd_btt_create(struct nd_region *nd_region)
366 {
367         return NULL;
368 }
369 #endif
370
371 struct nd_pfn *to_nd_pfn(struct device *dev);
372 #if IS_ENABLED(CONFIG_NVDIMM_PFN)
373
374 #define MAX_NVDIMM_ALIGN        4
375
376 int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
377 bool is_nd_pfn(struct device *dev);
378 struct device *nd_pfn_create(struct nd_region *nd_region);
379 struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
380                 struct nd_namespace_common *ndns);
381 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
382 extern const struct attribute_group *nd_pfn_attribute_groups[];
383 #else
384 static inline int nd_pfn_probe(struct device *dev,
385                 struct nd_namespace_common *ndns)
386 {
387         return -ENODEV;
388 }
389
390 static inline bool is_nd_pfn(struct device *dev)
391 {
392         return false;
393 }
394
395 static inline struct device *nd_pfn_create(struct nd_region *nd_region)
396 {
397         return NULL;
398 }
399
400 static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
401 {
402         return -ENODEV;
403 }
404 #endif
405
406 struct nd_dax *to_nd_dax(struct device *dev);
407 #if IS_ENABLED(CONFIG_NVDIMM_DAX)
408 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
409 bool is_nd_dax(struct device *dev);
410 struct device *nd_dax_create(struct nd_region *nd_region);
411 #else
412 static inline int nd_dax_probe(struct device *dev,
413                 struct nd_namespace_common *ndns)
414 {
415         return -ENODEV;
416 }
417
418 static inline bool is_nd_dax(struct device *dev)
419 {
420         return false;
421 }
422
423 static inline struct device *nd_dax_create(struct nd_region *nd_region)
424 {
425         return NULL;
426 }
427 #endif
428
429 int nd_region_to_nstype(struct nd_region *nd_region);
430 int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
431 u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
432                 struct nd_namespace_index *nsindex);
433 u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
434 void nvdimm_bus_lock(struct device *dev);
435 void nvdimm_bus_unlock(struct device *dev);
436 bool is_nvdimm_bus_locked(struct device *dev);
437 void nvdimm_check_and_set_ro(struct gendisk *disk);
438 void nvdimm_drvdata_release(struct kref *kref);
439 void put_ndd(struct nvdimm_drvdata *ndd);
440 int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
441 void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
442 struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
443                 struct nd_label_id *label_id, resource_size_t start,
444                 resource_size_t n);
445 resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
446 bool nvdimm_namespace_locked(struct nd_namespace_common *ndns);
447 struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
448 int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
449 int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
450 const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
451                 char *name);
452 unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
453 struct range;
454 void nvdimm_badblocks_populate(struct nd_region *nd_region,
455                 struct badblocks *bb, const struct range *range);
456 int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
457                 resource_size_t size);
458 void devm_namespace_disable(struct device *dev,
459                 struct nd_namespace_common *ndns);
460 #if IS_ENABLED(CONFIG_ND_CLAIM)
461 /* max struct page size independent of kernel config */
462 #define MAX_STRUCT_PAGE_SIZE 64
463 int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap);
464 #else
465 static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
466                                    struct dev_pagemap *pgmap)
467 {
468         return -ENXIO;
469 }
470 #endif
471 int nd_blk_region_init(struct nd_region *nd_region);
472 int nd_region_activate(struct nd_region *nd_region);
473 static inline bool is_bad_pmem(struct badblocks *bb, sector_t sector,
474                 unsigned int len)
475 {
476         if (bb->count) {
477                 sector_t first_bad;
478                 int num_bad;
479
480                 return !!badblocks_check(bb, sector, len / 512, &first_bad,
481                                 &num_bad);
482         }
483
484         return false;
485 }
486 resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
487 const u8 *nd_dev_to_uuid(struct device *dev);
488 bool pmem_should_map_pages(struct device *dev);
489 #endif /* __ND_H__ */