1
2
3
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
17enum {
18
19
20
21
22
23 ND_MAX_LANES = 256,
24 INT_LBASIZE_ALIGNMENT = 64,
25 NVDIMM_IO_ATOMIC = 1,
26};
27
28struct 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
38static 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
44static 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
50static 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
58static 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
64static 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
70static 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
76static 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
83static 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
89static 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
95static 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
101static 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
107static 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
113static 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
120static 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
126static 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
133static 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
140static 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
146static 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
154static 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
160static 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
167static 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
173static 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
180bool nsl_validate_blk_isetcookie(struct nvdimm_drvdata *ndd,
181 struct nd_namespace_label *nd_label,
182 u64 isetcookie);
183bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
184 struct nd_namespace_label *nd_label, guid_t *guid);
185enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
186 struct nd_namespace_label *nd_label);
187
188struct nd_region_data {
189 int ns_count;
190 int ns_active;
191 unsigned int hints_shift;
192 void __iomem *flush_wpq[];
193};
194
195static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
196 int dimm, int hint)
197{
198 unsigned int num = 1 << ndrd->hints_shift;
199 unsigned int mask = num - 1;
200
201 return ndrd->flush_wpq[dimm * num + (hint & mask)];
202}
203
204static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
205 int hint, void __iomem *flush)
206{
207 unsigned int num = 1 << ndrd->hints_shift;
208 unsigned int mask = num - 1;
209
210 ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
211}
212
213static inline struct nd_namespace_index *to_namespace_index(
214 struct nvdimm_drvdata *ndd, int i)
215{
216 if (i < 0)
217 return NULL;
218
219 return ndd->data + sizeof_namespace_index(ndd) * i;
220}
221
222static inline struct nd_namespace_index *to_current_namespace_index(
223 struct nvdimm_drvdata *ndd)
224{
225 return to_namespace_index(ndd, ndd->ns_current);
226}
227
228static inline struct nd_namespace_index *to_next_namespace_index(
229 struct nvdimm_drvdata *ndd)
230{
231 return to_namespace_index(ndd, ndd->ns_next);
232}
233
234unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
235
236#define namespace_label_has(ndd, field) \
237 (offsetof(struct nd_namespace_label, field) \
238 < sizeof_namespace_label(ndd))
239
240#define nd_dbg_dpa(r, d, res, fmt, arg...) \
241 dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
242 (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
243 (unsigned long long) (res ? resource_size(res) : 0), \
244 (unsigned long long) (res ? res->start : 0), ##arg)
245
246#define for_each_dpa_resource(ndd, res) \
247 for (res = (ndd)->dpa.child; res; res = res->sibling)
248
249#define for_each_dpa_resource_safe(ndd, res, next) \
250 for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
251 res; res = next, next = next ? next->sibling : NULL)
252
253struct nd_percpu_lane {
254 int count;
255 spinlock_t lock;
256};
257
258enum nd_label_flags {
259 ND_LABEL_REAP,
260};
261struct nd_label_ent {
262 struct list_head list;
263 unsigned long flags;
264 struct nd_namespace_label *label;
265};
266
267enum nd_mapping_lock_class {
268 ND_MAPPING_CLASS0,
269 ND_MAPPING_UUID_SCAN,
270};
271
272struct nd_mapping {
273 struct nvdimm *nvdimm;
274 u64 start;
275 u64 size;
276 int position;
277 struct list_head labels;
278 struct mutex lock;
279
280
281
282
283
284
285 struct nvdimm_drvdata *ndd;
286};
287
288struct nd_region {
289 struct device dev;
290 struct ida ns_ida;
291 struct ida btt_ida;
292 struct ida pfn_ida;
293 struct ida dax_ida;
294 unsigned long flags;
295 struct device *ns_seed;
296 struct device *btt_seed;
297 struct device *pfn_seed;
298 struct device *dax_seed;
299 unsigned long align;
300 u16 ndr_mappings;
301 u64 ndr_size;
302 u64 ndr_start;
303 int id, num_lanes, ro, numa_node, target_node;
304 void *provider_data;
305 struct kernfs_node *bb_state;
306 struct badblocks bb;
307 struct nd_interleave_set *nd_set;
308 struct nd_percpu_lane __percpu *lane;
309 int (*flush)(struct nd_region *nd_region, struct bio *bio);
310 struct nd_mapping mapping[];
311};
312
313struct nd_blk_region {
314 int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
315 int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
316 void *iobuf, u64 len, int rw);
317 void *blk_provider_data;
318 struct nd_region nd_region;
319};
320
321
322
323
324static inline unsigned nd_inc_seq(unsigned seq)
325{
326 static const unsigned next[] = { 0, 2, 3, 1 };
327
328 return next[seq & 3];
329}
330
331struct btt;
332struct nd_btt {
333 struct device dev;
334 struct nd_namespace_common *ndns;
335 struct btt *btt;
336 unsigned long lbasize;
337 u64 size;
338 u8 *uuid;
339 int id;
340 int initial_offset;
341 u16 version_major;
342 u16 version_minor;
343};
344
345enum nd_pfn_mode {
346 PFN_MODE_NONE,
347 PFN_MODE_RAM,
348 PFN_MODE_PMEM,
349};
350
351struct nd_pfn {
352 int id;
353 u8 *uuid;
354 struct device dev;
355 unsigned long align;
356 unsigned long npfns;
357 enum nd_pfn_mode mode;
358 struct nd_pfn_sb *pfn_sb;
359 struct nd_namespace_common *ndns;
360};
361
362struct nd_dax {
363 struct nd_pfn nd_pfn;
364};
365
366static inline u32 nd_info_block_reserve(void)
367{
368 return ALIGN(SZ_8K, PAGE_SIZE);
369}
370
371enum nd_async_mode {
372 ND_SYNC,
373 ND_ASYNC,
374};
375
376int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
377void wait_nvdimm_bus_probe_idle(struct device *dev);
378void nd_device_register(struct device *dev);
379void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
380void nd_device_notify(struct device *dev, enum nvdimm_event event);
381int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
382 size_t len);
383ssize_t nd_size_select_show(unsigned long current_size,
384 const unsigned long *supported, char *buf);
385ssize_t nd_size_select_store(struct device *dev, const char *buf,
386 unsigned long *current_size, const unsigned long *supported);
387int __init nvdimm_init(void);
388int __init nd_region_init(void);
389int __init nd_label_init(void);
390void nvdimm_exit(void);
391void nd_region_exit(void);
392struct nvdimm;
393extern const struct attribute_group nd_device_attribute_group;
394extern const struct attribute_group nd_numa_attribute_group;
395extern const struct attribute_group *nvdimm_bus_attribute_groups[];
396struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
397int nvdimm_check_config_data(struct device *dev);
398int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
399int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
400int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
401 size_t offset, size_t len);
402int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
403 void *buf, size_t len);
404long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
405 unsigned int len);
406void nvdimm_set_labeling(struct device *dev);
407void nvdimm_set_locked(struct device *dev);
408void nvdimm_clear_locked(struct device *dev);
409int nvdimm_security_setup_events(struct device *dev);
410#if IS_ENABLED(CONFIG_NVDIMM_KEYS)
411int nvdimm_security_unlock(struct device *dev);
412#else
413static inline int nvdimm_security_unlock(struct device *dev)
414{
415 return 0;
416}
417#endif
418struct nd_btt *to_nd_btt(struct device *dev);
419
420struct nd_gen_sb {
421 char reserved[SZ_4K - 8];
422 __le64 checksum;
423};
424
425u64 nd_sb_checksum(struct nd_gen_sb *sb);
426#if IS_ENABLED(CONFIG_BTT)
427int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
428bool is_nd_btt(struct device *dev);
429struct device *nd_btt_create(struct nd_region *nd_region);
430#else
431static inline int nd_btt_probe(struct device *dev,
432 struct nd_namespace_common *ndns)
433{
434 return -ENODEV;
435}
436
437static inline bool is_nd_btt(struct device *dev)
438{
439 return false;
440}
441
442static inline struct device *nd_btt_create(struct nd_region *nd_region)
443{
444 return NULL;
445}
446#endif
447
448struct nd_pfn *to_nd_pfn(struct device *dev);
449#if IS_ENABLED(CONFIG_NVDIMM_PFN)
450
451#define MAX_NVDIMM_ALIGN 4
452
453int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
454bool is_nd_pfn(struct device *dev);
455struct device *nd_pfn_create(struct nd_region *nd_region);
456struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
457 struct nd_namespace_common *ndns);
458int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
459extern const struct attribute_group *nd_pfn_attribute_groups[];
460#else
461static inline int nd_pfn_probe(struct device *dev,
462 struct nd_namespace_common *ndns)
463{
464 return -ENODEV;
465}
466
467static inline bool is_nd_pfn(struct device *dev)
468{
469 return false;
470}
471
472static inline struct device *nd_pfn_create(struct nd_region *nd_region)
473{
474 return NULL;
475}
476
477static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
478{
479 return -ENODEV;
480}
481#endif
482
483struct nd_dax *to_nd_dax(struct device *dev);
484#if IS_ENABLED(CONFIG_NVDIMM_DAX)
485int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
486bool is_nd_dax(struct device *dev);
487struct device *nd_dax_create(struct nd_region *nd_region);
488#else
489static inline int nd_dax_probe(struct device *dev,
490 struct nd_namespace_common *ndns)
491{
492 return -ENODEV;
493}
494
495static inline bool is_nd_dax(struct device *dev)
496{
497 return false;
498}
499
500static inline struct device *nd_dax_create(struct nd_region *nd_region)
501{
502 return NULL;
503}
504#endif
505
506int nd_region_to_nstype(struct nd_region *nd_region);
507int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
508u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
509 struct nd_namespace_index *nsindex);
510u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
511void nvdimm_bus_lock(struct device *dev);
512void nvdimm_bus_unlock(struct device *dev);
513bool is_nvdimm_bus_locked(struct device *dev);
514void nvdimm_check_and_set_ro(struct gendisk *disk);
515void nvdimm_drvdata_release(struct kref *kref);
516void put_ndd(struct nvdimm_drvdata *ndd);
517int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
518void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
519struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
520 struct nd_label_id *label_id, resource_size_t start,
521 resource_size_t n);
522resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
523bool nvdimm_namespace_locked(struct nd_namespace_common *ndns);
524struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
525int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
526int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
527const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
528 char *name);
529unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
530struct range;
531void nvdimm_badblocks_populate(struct nd_region *nd_region,
532 struct badblocks *bb, const struct range *range);
533int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
534 resource_size_t size);
535void devm_namespace_disable(struct device *dev,
536 struct nd_namespace_common *ndns);
537#if IS_ENABLED(CONFIG_ND_CLAIM)
538
539#define MAX_STRUCT_PAGE_SIZE 64
540int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap);
541#else
542static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
543 struct dev_pagemap *pgmap)
544{
545 return -ENXIO;
546}
547#endif
548int nd_blk_region_init(struct nd_region *nd_region);
549int nd_region_activate(struct nd_region *nd_region);
550static inline bool is_bad_pmem(struct badblocks *bb, sector_t sector,
551 unsigned int len)
552{
553 if (bb->count) {
554 sector_t first_bad;
555 int num_bad;
556
557 return !!badblocks_check(bb, sector, len / 512, &first_bad,
558 &num_bad);
559 }
560
561 return false;
562}
563resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
564const u8 *nd_dev_to_uuid(struct device *dev);
565bool pmem_should_map_pages(struct device *dev);
566#endif
567