blob: 036638bdb7e3fd972a95d2a8aa5edb3cd5bc6cd3 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Dan Williams4d88a972015-05-31 14:41:48 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams4d88a972015-05-31 14:41:48 -04004 */
5#ifndef __ND_H__
6#define __ND_H__
Dan Williams1f7df6f2015-06-09 20:13:14 -04007#include <linux/libnvdimm.h>
Dan Williams200c79d2016-03-22 00:22:16 -07008#include <linux/badblocks.h>
Dan Williamsf0dc0892015-05-16 12:28:53 -04009#include <linux/blkdev.h>
Dan Williams4d88a972015-05-31 14:41:48 -040010#include <linux/device.h>
11#include <linux/mutex.h>
12#include <linux/ndctl.h>
Dan Williamsbf9bccc2015-06-17 17:14:46 -040013#include <linux/types.h>
Dan Williams71999462016-02-18 10:29:49 -080014#include <linux/nd.h>
Dan Williams4a826c82015-06-09 16:09:36 -040015#include "label.h"
Dan Williams4d88a972015-05-31 14:41:48 -040016
Dan Williams8c2f7e82015-06-25 04:20:04 -040017enum {
Vishal Verma5212e112015-06-25 04:20:32 -040018 /*
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,
Vishal Vermafcae6952015-06-25 04:22:39 -040024 INT_LBASIZE_ALIGNMENT = 64,
Vishal Verma3ae3d672017-05-10 15:01:30 -060025 NVDIMM_IO_ATOMIC = 1,
Dan Williams8c2f7e82015-06-25 04:20:04 -040026};
27
Dan Williams4d88a972015-05-31 14:41:48 -040028struct nvdimm_drvdata {
29 struct device *dev;
Dan Williams028817682017-08-29 18:28:18 -070030 int nslabel_size;
Dan Williams4d88a972015-05-31 14:41:48 -040031 struct nd_cmd_get_config_size nsarea;
32 void *data;
Dan Williams4a826c82015-06-09 16:09:36 -040033 int ns_current, ns_next;
34 struct resource dpa;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040035 struct kref kref;
Dan Williams4d88a972015-05-31 14:41:48 -040036};
37
Dan Williamsb4366a82021-08-24 09:05:30 -070038static 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
Dan Williams8176f142021-08-24 09:05:41 -070050static 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
Dan Williamsb4366a82021-08-24 09:05:30 -070058static 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
Dan Williams8176f142021-08-24 09:05:41 -070064static 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
Dan Williamsb4366a82021-08-24 09:05:30 -070070static 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
Dan Williams8176f142021-08-24 09:05:41 -070076static 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
Dan Williamsb4366a82021-08-24 09:05:30 -070083static 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
Dan Williams8176f142021-08-24 09:05:41 -070089static 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
Dan Williamsb4366a82021-08-24 09:05:30 -070095static 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
Dan Williams8176f142021-08-24 09:05:41 -0700101static 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
Dan Williamsb4366a82021-08-24 09:05:30 -0700107static 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
Dan Williams8176f142021-08-24 09:05:41 -0700113static 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
Dan Williamsb4366a82021-08-24 09:05:30 -0700120static 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
Dan Williams8176f142021-08-24 09:05:41 -0700126static 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
Dan Williams9761b022021-08-24 09:05:35 -0700133static 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
Dan Williamsb4366a82021-08-24 09:05:30 -0700140static 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
Dan Williams8176f142021-08-24 09:05:41 -0700146static 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
Dan Williamsb4366a82021-08-24 09:05:30 -0700154static 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
Dan Williams8176f142021-08-24 09:05:41 -0700160static 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
Dan Williamsb4366a82021-08-24 09:05:30 -0700167static 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
Dan Williams8176f142021-08-24 09:05:41 -0700173static 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
Dan Williamsd1c6e082021-09-08 22:11:37 -0700180static 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
188static 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
196static 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
206static 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
Dan Williamsf56541a2021-08-24 09:05:51 -0700212bool nsl_validate_blk_isetcookie(struct nvdimm_drvdata *ndd,
213 struct nd_namespace_label *nd_label,
214 u64 isetcookie);
Dan Williams8b03aa02021-08-24 09:06:02 -0700215bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
216 struct nd_namespace_label *nd_label, guid_t *guid);
Dan Williamsa6e6d722021-08-24 09:06:07 -0700217enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
218 struct nd_namespace_label *nd_label);
Dan Williamsf56541a2021-08-24 09:05:51 -0700219
Dan Williamse5ae3b22016-06-07 17:00:04 -0700220struct nd_region_data {
221 int ns_count;
222 int ns_active;
Dan Williams595c7302016-09-23 17:53:52 -0700223 unsigned int hints_shift;
Gustavo A. R. Silva91061372020-03-19 18:09:37 -0500224 void __iomem *flush_wpq[];
Dan Williams3d880022015-05-31 15:02:11 -0400225};
226
Dan Williams595c7302016-09-23 17:53:52 -0700227static 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
236static 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
Dan Williams4a826c82015-06-09 16:09:36 -0400245static 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
254static 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
260static 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
Dan Williams564e8712017-06-03 18:30:43 +0900266unsigned 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
Dan Williams4a826c82015-06-09 16:09:36 -0400272#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
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400278#define for_each_dpa_resource(ndd, res) \
279 for (res = (ndd)->dpa.child; res; res = res->sibling)
280
Dan Williams4a826c82015-06-09 16:09:36 -0400281#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
Vishal Verma5212e112015-06-25 04:20:32 -0400285struct nd_percpu_lane {
286 int count;
287 spinlock_t lock;
288};
289
Dan Williamsc4703ce2019-04-30 21:51:21 -0700290enum nd_label_flags {
291 ND_LABEL_REAP,
292};
Dan Williamsae8219f2016-09-19 16:04:21 -0700293struct nd_label_ent {
294 struct list_head list;
Dan Williamsc4703ce2019-04-30 21:51:21 -0700295 unsigned long flags;
Dan Williamsae8219f2016-09-19 16:04:21 -0700296 struct nd_namespace_label *label;
297};
298
299enum nd_mapping_lock_class {
300 ND_MAPPING_CLASS0,
301 ND_MAPPING_UUID_SCAN,
302};
303
Dan Williams44c462e2016-09-19 16:38:50 -0700304struct nd_mapping {
305 struct nvdimm *nvdimm;
Dan Williams44c462e2016-09-19 16:38:50 -0700306 u64 start;
307 u64 size;
Dan Williams401c0a12017-08-04 17:20:16 -0700308 int position;
Dan Williamsae8219f2016-09-19 16:04:21 -0700309 struct list_head labels;
310 struct mutex lock;
Dan Williams44c462e2016-09-19 16:38:50 -0700311 /*
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
Dan Williams1f7df6f2015-06-09 20:13:14 -0400320struct nd_region {
321 struct device dev;
Dan Williams1b40e092015-05-01 13:34:01 -0400322 struct ida ns_ida;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400323 struct ida btt_ida;
Dan Williamse1455742015-07-30 17:57:47 -0400324 struct ida pfn_ida;
Dan Williamscd034122016-03-11 10:15:36 -0800325 struct ida dax_ida;
Dan Williams004f1af2015-08-24 19:20:23 -0400326 unsigned long flags;
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400327 struct device *ns_seed;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400328 struct device *btt_seed;
Dan Williamse1455742015-07-30 17:57:47 -0400329 struct device *pfn_seed;
Dan Williamscd034122016-03-11 10:15:36 -0800330 struct device *dax_seed;
Dan Williams2522afb2020-01-30 12:06:23 -0800331 unsigned long align;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400332 u16 ndr_mappings;
333 u64 ndr_size;
334 u64 ndr_start;
Dan Williams8fc5c732018-11-09 12:43:07 -0800335 int id, num_lanes, ro, numa_node, target_node;
Dan Williams1f7df6f2015-06-09 20:13:14 -0400336 void *provider_data;
Toshi Kani975750a2017-06-12 16:25:11 -0600337 struct kernfs_node *bb_state;
Dave Jiang6a6bef92017-04-07 15:33:20 -0700338 struct badblocks bb;
Dan Williamseaf96152015-05-01 13:11:27 -0400339 struct nd_interleave_set *nd_set;
Vishal Verma5212e112015-06-25 04:20:32 -0400340 struct nd_percpu_lane __percpu *lane;
Pankaj Guptac5d43552019-07-05 19:33:22 +0530341 int (*flush)(struct nd_region *nd_region, struct bio *bio);
Gustavo A. R. Silva91061372020-03-19 18:09:37 -0500342 struct nd_mapping mapping[];
Dan Williams1f7df6f2015-06-09 20:13:14 -0400343};
344
Dan Williams8172db92021-09-08 22:11:42 -0700345static 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
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400352struct nd_blk_region {
353 int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400354 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
Dan Williams4a826c82015-06-09 16:09:36 -0400360/*
361 * Lookup next in the repeating sequence of 01, 10, and 11.
362 */
363static inline unsigned nd_inc_seq(unsigned seq)
364{
365 static const unsigned next[] = { 0, 2, 3, 1 };
366
367 return next[seq & 3];
368}
Dan Williamsf524bf22015-05-30 12:36:02 -0400369
Vishal Verma5212e112015-06-25 04:20:32 -0400370struct btt;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400371struct nd_btt {
372 struct device dev;
373 struct nd_namespace_common *ndns;
Vishal Verma5212e112015-06-25 04:20:32 -0400374 struct btt *btt;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400375 unsigned long lbasize;
Vishal Vermaabe8b4e2016-07-27 16:38:59 -0600376 u64 size;
Dan Williamsd1c6e082021-09-08 22:11:37 -0700377 uuid_t *uuid;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400378 int id;
Vishal Verma14e49452017-06-28 14:25:00 -0600379 int initial_offset;
380 u16 version_major;
381 u16 version_minor;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400382};
383
Dan Williamse1455742015-07-30 17:57:47 -0400384enum nd_pfn_mode {
385 PFN_MODE_NONE,
386 PFN_MODE_RAM,
387 PFN_MODE_PMEM,
388};
389
390struct nd_pfn {
391 int id;
Dan Williamsd1c6e082021-09-08 22:11:37 -0700392 uuid_t *uuid;
Dan Williamse1455742015-07-30 17:57:47 -0400393 struct device dev;
Dan Williams315c5622015-12-10 14:45:23 -0800394 unsigned long align;
Dan Williamse1455742015-07-30 17:57:47 -0400395 unsigned long npfns;
396 enum nd_pfn_mode mode;
397 struct nd_pfn_sb *pfn_sb;
398 struct nd_namespace_common *ndns;
399};
400
Dan Williamscd034122016-03-11 10:15:36 -0800401struct nd_dax {
402 struct nd_pfn nd_pfn;
403};
404
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530405static inline u32 nd_info_block_reserve(void)
406{
407 return ALIGN(SZ_8K, PAGE_SIZE);
408}
409
Dan Williams4d88a972015-05-31 14:41:48 -0400410enum nd_async_mode {
411 ND_SYNC,
412 ND_ASYNC,
413};
414
Vishal Verma41cd8b72015-06-25 04:21:52 -0400415int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400416void wait_nvdimm_bus_probe_idle(struct device *dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400417void nd_device_register(struct device *dev);
418void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
Dan Williams71999462016-02-18 10:29:49 -0800419void nd_device_notify(struct device *dev, enum nvdimm_event event);
Dan Williamsd1c6e082021-09-08 22:11:37 -0700420int nd_uuid_store(struct device *dev, uuid_t **uuid_out, const char *buf,
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400421 size_t len);
Dan Williamsb2c48f92017-08-11 17:36:54 -0700422ssize_t nd_size_select_show(unsigned long current_size,
Dan Williams1b40e092015-05-01 13:34:01 -0400423 const unsigned long *supported, char *buf);
Dan Williamsb2c48f92017-08-11 17:36:54 -0700424ssize_t nd_size_select_store(struct device *dev, const char *buf,
425 unsigned long *current_size, const unsigned long *supported);
Dan Williams4d88a972015-05-31 14:41:48 -0400426int __init nvdimm_init(void);
Dan Williams3d880022015-05-31 15:02:11 -0400427int __init nd_region_init(void);
Dan Williamsb3fde742017-06-04 10:18:39 +0900428int __init nd_label_init(void);
Dan Williams4d88a972015-05-31 14:41:48 -0400429void nvdimm_exit(void);
Dan Williams3d880022015-05-31 15:02:11 -0400430void nd_region_exit(void);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400431struct nvdimm;
Dan Williamsadbb6822019-11-12 17:00:24 -0800432extern const struct attribute_group nd_device_attribute_group;
Dan Williamse2f6a0e2019-11-19 09:51:54 -0800433extern const struct attribute_group nd_numa_attribute_group;
Dan Williamse755799a2019-11-12 17:08:56 -0800434extern const struct attribute_group *nvdimm_bus_attribute_groups[];
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400435struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
Toshi Kaniaee65982016-08-16 13:08:40 -0600436int nvdimm_check_config_data(struct device *dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400437int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
438int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
Alexander Duyck2d657d12018-10-10 16:39:20 -0700439int nvdimm_get_config_data(struct nvdimm_drvdata *ndd, void *buf,
440 size_t offset, size_t len);
Dan Williamsf524bf22015-05-30 12:36:02 -0400441int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
442 void *buf, size_t len);
Dan Williams59e64732016-03-08 07:16:07 -0800443long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
444 unsigned int len);
Dan Williamsa0e37452020-01-30 12:06:18 -0800445void nvdimm_set_labeling(struct device *dev);
Dan Williams8f078b32017-05-04 14:01:24 -0700446void nvdimm_set_locked(struct device *dev);
Dan Williamsd34cb802017-09-25 11:01:31 -0700447void nvdimm_clear_locked(struct device *dev);
Dan Williams1cd73862019-01-19 08:45:56 -0800448int nvdimm_security_setup_events(struct device *dev);
Dave Jiang4c6926a2018-12-06 12:40:01 -0800449#if IS_ENABLED(CONFIG_NVDIMM_KEYS)
450int nvdimm_security_unlock(struct device *dev);
451#else
452static inline int nvdimm_security_unlock(struct device *dev)
453{
454 return 0;
455}
456#endif
Dan Williams8c2f7e82015-06-25 04:20:04 -0400457struct nd_btt *to_nd_btt(struct device *dev);
Dan Williamse1455742015-07-30 17:57:47 -0400458
459struct nd_gen_sb {
460 char reserved[SZ_4K - 8];
461 __le64 checksum;
462};
463
464u64 nd_sb_checksum(struct nd_gen_sb *sb);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400465#if IS_ENABLED(CONFIG_BTT)
Dan Williams200c79d2016-03-22 00:22:16 -0700466int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400467bool is_nd_btt(struct device *dev);
468struct device *nd_btt_create(struct nd_region *nd_region);
469#else
Dan Williamse32bc722016-03-17 18:23:09 -0700470static inline int nd_btt_probe(struct device *dev,
Dan Williams200c79d2016-03-22 00:22:16 -0700471 struct nd_namespace_common *ndns)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400472{
473 return -ENODEV;
474}
475
476static inline bool is_nd_btt(struct device *dev)
477{
478 return false;
479}
480
481static inline struct device *nd_btt_create(struct nd_region *nd_region)
482{
483 return NULL;
484}
Dan Williams8c2f7e82015-06-25 04:20:04 -0400485#endif
Dan Williamse1455742015-07-30 17:57:47 -0400486
487struct nd_pfn *to_nd_pfn(struct device *dev);
488#if IS_ENABLED(CONFIG_NVDIMM_PFN)
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000489
Aneesh Kumar K.Vf5376692019-09-05 21:16:03 +0530490#define MAX_NVDIMM_ALIGN 4
Oliver O'Halloran0dd69642017-06-27 19:56:33 +1000491
Dan Williams200c79d2016-03-22 00:22:16 -0700492int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
Dan Williamse1455742015-07-30 17:57:47 -0400493bool is_nd_pfn(struct device *dev);
494struct device *nd_pfn_create(struct nd_region *nd_region);
Dan Williamscd034122016-03-11 10:15:36 -0800495struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
496 struct nd_namespace_common *ndns);
Dan Williamsc5ed9262016-05-18 14:50:12 -0700497int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
Dan Williams78c81cc2019-11-06 19:56:41 -0800498extern const struct attribute_group *nd_pfn_attribute_groups[];
Dan Williamse1455742015-07-30 17:57:47 -0400499#else
Dan Williams200c79d2016-03-22 00:22:16 -0700500static inline int nd_pfn_probe(struct device *dev,
501 struct nd_namespace_common *ndns)
Dan Williamse1455742015-07-30 17:57:47 -0400502{
503 return -ENODEV;
504}
505
506static inline bool is_nd_pfn(struct device *dev)
507{
508 return false;
509}
510
511static inline struct device *nd_pfn_create(struct nd_region *nd_region)
512{
513 return NULL;
514}
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400515
Dan Williamsc5ed9262016-05-18 14:50:12 -0700516static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400517{
518 return -ENODEV;
519}
Dan Williamse1455742015-07-30 17:57:47 -0400520#endif
521
Dan Williamscd034122016-03-11 10:15:36 -0800522struct nd_dax *to_nd_dax(struct device *dev);
523#if IS_ENABLED(CONFIG_NVDIMM_DAX)
Dan Williamsc5ed9262016-05-18 14:50:12 -0700524int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
Dan Williamscd034122016-03-11 10:15:36 -0800525bool is_nd_dax(struct device *dev);
526struct device *nd_dax_create(struct nd_region *nd_region);
527#else
Dan Williamsc5ed9262016-05-18 14:50:12 -0700528static inline int nd_dax_probe(struct device *dev,
529 struct nd_namespace_common *ndns)
530{
531 return -ENODEV;
532}
533
Dan Williamscd034122016-03-11 10:15:36 -0800534static inline bool is_nd_dax(struct device *dev)
535{
536 return false;
537}
538
539static inline struct device *nd_dax_create(struct nd_region *nd_region)
540{
541 return NULL;
542}
543#endif
544
Dan Williams3d880022015-05-31 15:02:11 -0400545int nd_region_to_nstype(struct nd_region *nd_region);
546int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
Dan Williamsc12c48c2017-06-04 10:59:15 +0900547u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
548 struct nd_namespace_index *nsindex);
Dan Williams86ef58a2017-02-28 18:32:48 -0800549u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
Dan Williams3d880022015-05-31 15:02:11 -0400550void nvdimm_bus_lock(struct device *dev);
551void nvdimm_bus_unlock(struct device *dev);
552bool is_nvdimm_bus_locked(struct device *dev);
Christoph Hellwig32f61d62020-09-01 17:57:47 +0200553void nvdimm_check_and_set_ro(struct gendisk *disk);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400554void nvdimm_drvdata_release(struct kref *kref);
555void put_ndd(struct nvdimm_drvdata *ndd);
Dan Williams4a826c82015-06-09 16:09:36 -0400556int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
557void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
558struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
559 struct nd_label_id *label_id, resource_size_t start,
560 resource_size_t n);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400561resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
Dan Williams08e6b3c2018-06-13 09:08:36 -0700562bool nvdimm_namespace_locked(struct nd_namespace_common *ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400563struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
Vishal Verma5212e112015-06-25 04:20:32 -0400564int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
Dan Williams298f2bc2016-03-15 16:41:04 -0700565int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
Vishal Verma5212e112015-06-25 04:20:32 -0400566const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
567 char *name);
Dan Williamsf979b132017-06-04 12:12:07 +0900568unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
Dan Williamsa4574f62020-10-13 16:50:29 -0700569struct range;
Dan Williamsa3901802016-04-07 20:02:06 -0700570void nvdimm_badblocks_populate(struct nd_region *nd_region,
Dan Williamsa4574f62020-10-13 16:50:29 -0700571 struct badblocks *bb, const struct range *range);
Aneesh Kumar K.V8f4b01f2019-10-31 16:27:41 +0530572int devm_namespace_enable(struct device *dev, struct nd_namespace_common *ndns,
573 resource_size_t size);
574void devm_namespace_disable(struct device *dev,
575 struct nd_namespace_common *ndns);
Dan Williams200c79d2016-03-22 00:22:16 -0700576#if IS_ENABLED(CONFIG_ND_CLAIM)
Aneesh Kumar K.Ve96f0bf2019-09-05 21:15:59 +0530577/* max struct page size independent of kernel config */
578#define MAX_STRUCT_PAGE_SIZE 64
Christoph Hellwige8d51342017-12-29 08:54:05 +0100579int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap);
Dan Williams200c79d2016-03-22 00:22:16 -0700580#else
Christoph Hellwige8d51342017-12-29 08:54:05 +0100581static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
582 struct dev_pagemap *pgmap)
Dan Williamsac515c02016-03-22 00:29:43 -0700583{
Christoph Hellwige8d51342017-12-29 08:54:05 +0100584 return -ENXIO;
Dan Williamsac515c02016-03-22 00:29:43 -0700585}
Dan Williams200c79d2016-03-22 00:22:16 -0700586#endif
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400587int nd_blk_region_init(struct nd_region *nd_region);
Dan Williamse5ae3b22016-06-07 17:00:04 -0700588int nd_region_activate(struct nd_region *nd_region);
Dan Williams200c79d2016-03-22 00:22:16 -0700589static 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}
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400602resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
Dan Williamsd1c6e082021-09-08 22:11:37 -0700603const uuid_t *nd_dev_to_uuid(struct device *dev);
Dan Williams004f1af2015-08-24 19:20:23 -0400604bool pmem_should_map_pages(struct device *dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400605#endif /* __ND_H__ */