1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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>
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
24 INT_LBASIZE_ALIGNMENT = 64,
28 struct nvdimm_drvdata {
31 struct nd_cmd_get_config_size nsarea;
33 int ns_current, ns_next;
38 static inline const u8 *nsl_ref_name(struct nvdimm_drvdata *ndd,
39 struct nd_namespace_label *nd_label)
41 return nd_label->name;
44 static inline u8 *nsl_get_name(struct nvdimm_drvdata *ndd,
45 struct nd_namespace_label *nd_label, u8 *name)
47 return memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
50 static inline u8 *nsl_set_name(struct nvdimm_drvdata *ndd,
51 struct nd_namespace_label *nd_label, u8 *name)
55 return memcpy(nd_label->name, name, NSLABEL_NAME_LEN);
58 static inline u32 nsl_get_slot(struct nvdimm_drvdata *ndd,
59 struct nd_namespace_label *nd_label)
61 return __le32_to_cpu(nd_label->slot);
64 static inline void nsl_set_slot(struct nvdimm_drvdata *ndd,
65 struct nd_namespace_label *nd_label, u32 slot)
67 nd_label->slot = __cpu_to_le32(slot);
70 static inline u64 nsl_get_checksum(struct nvdimm_drvdata *ndd,
71 struct nd_namespace_label *nd_label)
73 return __le64_to_cpu(nd_label->checksum);
76 static inline void nsl_set_checksum(struct nvdimm_drvdata *ndd,
77 struct nd_namespace_label *nd_label,
80 nd_label->checksum = __cpu_to_le64(checksum);
83 static inline u32 nsl_get_flags(struct nvdimm_drvdata *ndd,
84 struct nd_namespace_label *nd_label)
86 return __le32_to_cpu(nd_label->flags);
89 static inline void nsl_set_flags(struct nvdimm_drvdata *ndd,
90 struct nd_namespace_label *nd_label, u32 flags)
92 nd_label->flags = __cpu_to_le32(flags);
95 static inline u64 nsl_get_dpa(struct nvdimm_drvdata *ndd,
96 struct nd_namespace_label *nd_label)
98 return __le64_to_cpu(nd_label->dpa);
101 static inline void nsl_set_dpa(struct nvdimm_drvdata *ndd,
102 struct nd_namespace_label *nd_label, u64 dpa)
104 nd_label->dpa = __cpu_to_le64(dpa);
107 static inline u64 nsl_get_rawsize(struct nvdimm_drvdata *ndd,
108 struct nd_namespace_label *nd_label)
110 return __le64_to_cpu(nd_label->rawsize);
113 static inline void nsl_set_rawsize(struct nvdimm_drvdata *ndd,
114 struct nd_namespace_label *nd_label,
117 nd_label->rawsize = __cpu_to_le64(rawsize);
120 static inline u64 nsl_get_isetcookie(struct nvdimm_drvdata *ndd,
121 struct nd_namespace_label *nd_label)
123 return __le64_to_cpu(nd_label->isetcookie);
126 static inline void nsl_set_isetcookie(struct nvdimm_drvdata *ndd,
127 struct nd_namespace_label *nd_label,
130 nd_label->isetcookie = __cpu_to_le64(isetcookie);
133 static inline bool nsl_validate_isetcookie(struct nvdimm_drvdata *ndd,
134 struct nd_namespace_label *nd_label,
137 return cookie == __le64_to_cpu(nd_label->isetcookie);
140 static inline u16 nsl_get_position(struct nvdimm_drvdata *ndd,
141 struct nd_namespace_label *nd_label)
143 return __le16_to_cpu(nd_label->position);
146 static inline void nsl_set_position(struct nvdimm_drvdata *ndd,
147 struct nd_namespace_label *nd_label,
150 nd_label->position = __cpu_to_le16(position);
154 static inline u16 nsl_get_nlabel(struct nvdimm_drvdata *ndd,
155 struct nd_namespace_label *nd_label)
157 return __le16_to_cpu(nd_label->nlabel);
160 static inline void nsl_set_nlabel(struct nvdimm_drvdata *ndd,
161 struct nd_namespace_label *nd_label,
164 nd_label->nlabel = __cpu_to_le16(nlabel);
167 static inline u64 nsl_get_lbasize(struct nvdimm_drvdata *ndd,
168 struct nd_namespace_label *nd_label)
170 return __le64_to_cpu(nd_label->lbasize);
173 static inline void nsl_set_lbasize(struct nvdimm_drvdata *ndd,
174 struct nd_namespace_label *nd_label,
177 nd_label->lbasize = __cpu_to_le64(lbasize);
180 static inline const uuid_t *nsl_get_uuid(struct nvdimm_drvdata *ndd,
181 struct nd_namespace_label *nd_label,
184 import_uuid(uuid, nd_label->uuid);
188 static inline const uuid_t *nsl_set_uuid(struct nvdimm_drvdata *ndd,
189 struct nd_namespace_label *nd_label,
192 export_uuid(nd_label->uuid, uuid);
196 static inline bool nsl_uuid_equal(struct nvdimm_drvdata *ndd,
197 struct nd_namespace_label *nd_label,
202 import_uuid(&tmp, nd_label->uuid);
203 return uuid_equal(&tmp, uuid);
206 static inline const u8 *nsl_uuid_raw(struct nvdimm_drvdata *ndd,
207 struct nd_namespace_label *nd_label)
209 return nd_label->uuid;
212 bool nsl_validate_blk_isetcookie(struct nvdimm_drvdata *ndd,
213 struct nd_namespace_label *nd_label,
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);
220 struct nd_region_data {
223 unsigned int hints_shift;
224 void __iomem *flush_wpq[];
227 static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
230 unsigned int num = 1 << ndrd->hints_shift;
231 unsigned int mask = num - 1;
233 return ndrd->flush_wpq[dimm * num + (hint & mask)];
236 static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
237 int hint, void __iomem *flush)
239 unsigned int num = 1 << ndrd->hints_shift;
240 unsigned int mask = num - 1;
242 ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
245 static inline struct nd_namespace_index *to_namespace_index(
246 struct nvdimm_drvdata *ndd, int i)
251 return ndd->data + sizeof_namespace_index(ndd) * i;
254 static inline struct nd_namespace_index *to_current_namespace_index(
255 struct nvdimm_drvdata *ndd)
257 return to_namespace_index(ndd, ndd->ns_current);
260 static inline struct nd_namespace_index *to_next_namespace_index(
261 struct nvdimm_drvdata *ndd)
263 return to_namespace_index(ndd, ndd->ns_next);
266 unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
268 #define namespace_label_has(ndd, field) \
269 (offsetof(struct nd_namespace_label, field) \
270 < sizeof_namespace_label(ndd))
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)
278 #define for_each_dpa_resource(ndd, res) \
279 for (res = (ndd)->dpa.child; res; res = res->sibling)
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)
285 struct nd_percpu_lane {
290 enum nd_label_flags {
293 struct nd_label_ent {
294 struct list_head list;
296 struct nd_namespace_label *label;
299 enum nd_mapping_lock_class {
301 ND_MAPPING_UUID_SCAN,
305 struct nvdimm *nvdimm;
309 struct list_head labels;
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
317 struct nvdimm_drvdata *ndd;
327 struct device *ns_seed;
328 struct device *btt_seed;
329 struct device *pfn_seed;
330 struct device *dax_seed;
335 int id, num_lanes, ro, numa_node, target_node;
337 struct kernfs_node *bb_state;
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[];
345 static inline bool nsl_validate_nlabel(struct nd_region *nd_region,
346 struct nvdimm_drvdata *ndd,
347 struct nd_namespace_label *nd_label)
349 return nsl_get_nlabel(ndd, nd_label) == nd_region->ndr_mappings;
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;
361 * Lookup next in the repeating sequence of 01, 10, and 11.
363 static inline unsigned nd_inc_seq(unsigned seq)
365 static const unsigned next[] = { 0, 2, 3, 1 };
367 return next[seq & 3];
373 struct nd_namespace_common *ndns;
375 unsigned long lbasize;
396 enum nd_pfn_mode mode;
397 struct nd_pfn_sb *pfn_sb;
398 struct nd_namespace_common *ndns;
402 struct nd_pfn nd_pfn;
405 static inline u32 nd_info_block_reserve(void)
407 return ALIGN(SZ_8K, PAGE_SIZE);
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,
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);
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,
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);
452 static inline int nvdimm_security_unlock(struct device *dev)
457 struct nd_btt *to_nd_btt(struct device *dev);
460 char reserved[SZ_4K - 8];
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);
470 static inline int nd_btt_probe(struct device *dev,
471 struct nd_namespace_common *ndns)
476 static inline bool is_nd_btt(struct device *dev)
481 static inline struct device *nd_btt_create(struct nd_region *nd_region)
487 struct nd_pfn *to_nd_pfn(struct device *dev);
488 #if IS_ENABLED(CONFIG_NVDIMM_PFN)
490 #define MAX_NVDIMM_ALIGN 4
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[];
500 static inline int nd_pfn_probe(struct device *dev,
501 struct nd_namespace_common *ndns)
506 static inline bool is_nd_pfn(struct device *dev)
511 static inline struct device *nd_pfn_create(struct nd_region *nd_region)
516 static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
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);
528 static inline int nd_dax_probe(struct device *dev,
529 struct nd_namespace_common *ndns)
534 static inline bool is_nd_dax(struct device *dev)
539 static inline struct device *nd_dax_create(struct nd_region *nd_region)
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,
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,
568 unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
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);
581 static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
582 struct dev_pagemap *pgmap)
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,
596 return !!badblocks_check(bb, sector, len / 512, &first_bad,
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__ */