1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef __LIBNVDIMM_H__
16#define __LIBNVDIMM_H__
17#include <linux/kernel.h>
18#include <linux/sizes.h>
19#include <linux/types.h>
20#include <linux/uuid.h>
21#include <linux/spinlock.h>
22#include <linux/bio.h>
23
24struct badrange_entry {
25 u64 start;
26 u64 length;
27 struct list_head list;
28};
29
30struct badrange {
31 struct list_head list;
32 spinlock_t lock;
33};
34
35enum {
36
37 NDD_ALIASING = 0,
38
39 NDD_UNARMED = 1,
40
41 NDD_LOCKED = 2,
42
43 NDD_SECURITY_OVERWRITE = 3,
44
45 NDD_WORK_PENDING = 4,
46
47 NDD_NOBLK = 5,
48
49 NDD_LABELING = 6,
50
51
52 ND_IOCTL_MAX_BUFLEN = SZ_4M,
53 ND_CMD_MAX_ELEM = 5,
54 ND_CMD_MAX_ENVELOPE = 256,
55 ND_MAX_MAPPINGS = 32,
56
57
58 ND_REGION_PAGEMAP = 0,
59
60
61
62
63 ND_REGION_PERSIST_CACHE = 1,
64
65
66
67
68
69 ND_REGION_PERSIST_MEMCTRL = 2,
70
71
72 ND_REGION_ASYNC = 3,
73
74
75 DPA_RESOURCE_ADJUSTED = 1 << 0,
76};
77
78struct nvdimm;
79struct nvdimm_bus_descriptor;
80typedef int (*ndctl_fn)(struct nvdimm_bus_descriptor *nd_desc,
81 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
82 unsigned int buf_len, int *cmd_rc);
83
84struct device_node;
85struct nvdimm_bus_descriptor {
86 const struct attribute_group **attr_groups;
87 unsigned long bus_dsm_mask;
88 unsigned long cmd_mask;
89 struct module *module;
90 char *provider_name;
91 struct device_node *of_node;
92 ndctl_fn ndctl;
93 int (*flush_probe)(struct nvdimm_bus_descriptor *nd_desc);
94 int (*clear_to_send)(struct nvdimm_bus_descriptor *nd_desc,
95 struct nvdimm *nvdimm, unsigned int cmd, void *data);
96};
97
98struct nd_cmd_desc {
99 int in_num;
100 int out_num;
101 u32 in_sizes[ND_CMD_MAX_ELEM];
102 int out_sizes[ND_CMD_MAX_ELEM];
103};
104
105struct nd_interleave_set {
106
107 u64 cookie1;
108
109 u64 cookie2;
110
111 u64 altcookie;
112
113 guid_t type_guid;
114};
115
116struct nd_mapping_desc {
117 struct nvdimm *nvdimm;
118 u64 start;
119 u64 size;
120 int position;
121};
122
123struct nd_region;
124struct nd_region_desc {
125 struct resource *res;
126 struct nd_mapping_desc *mapping;
127 u16 num_mappings;
128 const struct attribute_group **attr_groups;
129 struct nd_interleave_set *nd_set;
130 void *provider_data;
131 int num_lanes;
132 int numa_node;
133 int target_node;
134 unsigned long flags;
135 struct device_node *of_node;
136 int (*flush)(struct nd_region *nd_region, struct bio *bio);
137};
138
139struct device;
140void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset,
141 size_t size, unsigned long flags);
142static inline void __iomem *devm_nvdimm_ioremap(struct device *dev,
143 resource_size_t offset, size_t size)
144{
145 return (void __iomem *) devm_nvdimm_memremap(dev, offset, size, 0);
146}
147
148struct nvdimm_bus;
149struct module;
150struct device;
151struct nd_blk_region;
152struct nd_blk_region_desc {
153 int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
154 int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
155 void *iobuf, u64 len, int rw);
156 struct nd_region_desc ndr_desc;
157};
158
159static inline struct nd_blk_region_desc *to_blk_region_desc(
160 struct nd_region_desc *ndr_desc)
161{
162 return container_of(ndr_desc, struct nd_blk_region_desc, ndr_desc);
163
164}
165
166
167
168
169
170enum nvdimm_security_bits {
171 NVDIMM_SECURITY_DISABLED,
172 NVDIMM_SECURITY_UNLOCKED,
173 NVDIMM_SECURITY_LOCKED,
174 NVDIMM_SECURITY_FROZEN,
175 NVDIMM_SECURITY_OVERWRITE,
176};
177
178#define NVDIMM_PASSPHRASE_LEN 32
179#define NVDIMM_KEY_DESC_LEN 22
180
181struct nvdimm_key_data {
182 u8 data[NVDIMM_PASSPHRASE_LEN];
183};
184
185enum nvdimm_passphrase_type {
186 NVDIMM_USER,
187 NVDIMM_MASTER,
188};
189
190struct nvdimm_security_ops {
191 unsigned long (*get_flags)(struct nvdimm *nvdimm,
192 enum nvdimm_passphrase_type pass_type);
193 int (*freeze)(struct nvdimm *nvdimm);
194 int (*change_key)(struct nvdimm *nvdimm,
195 const struct nvdimm_key_data *old_data,
196 const struct nvdimm_key_data *new_data,
197 enum nvdimm_passphrase_type pass_type);
198 int (*unlock)(struct nvdimm *nvdimm,
199 const struct nvdimm_key_data *key_data);
200 int (*disable)(struct nvdimm *nvdimm,
201 const struct nvdimm_key_data *key_data);
202 int (*erase)(struct nvdimm *nvdimm,
203 const struct nvdimm_key_data *key_data,
204 enum nvdimm_passphrase_type pass_type);
205 int (*overwrite)(struct nvdimm *nvdimm,
206 const struct nvdimm_key_data *key_data);
207 int (*query_overwrite)(struct nvdimm *nvdimm);
208};
209
210void badrange_init(struct badrange *badrange);
211int badrange_add(struct badrange *badrange, u64 addr, u64 length);
212void badrange_forget(struct badrange *badrange, phys_addr_t start,
213 unsigned int len);
214int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
215 u64 length);
216struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
217 struct nvdimm_bus_descriptor *nfit_desc);
218void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
219struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
220struct nvdimm_bus *nvdimm_to_bus(struct nvdimm *nvdimm);
221struct nvdimm *to_nvdimm(struct device *dev);
222struct nd_region *to_nd_region(struct device *dev);
223struct device *nd_region_dev(struct nd_region *nd_region);
224struct nd_blk_region *to_nd_blk_region(struct device *dev);
225struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
226struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
227const char *nvdimm_name(struct nvdimm *nvdimm);
228struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
229unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
230void *nvdimm_provider_data(struct nvdimm *nvdimm);
231struct nvdimm *__nvdimm_create(struct nvdimm_bus *nvdimm_bus,
232 void *provider_data, const struct attribute_group **groups,
233 unsigned long flags, unsigned long cmd_mask, int num_flush,
234 struct resource *flush_wpq, const char *dimm_id,
235 const struct nvdimm_security_ops *sec_ops);
236static inline struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus,
237 void *provider_data, const struct attribute_group **groups,
238 unsigned long flags, unsigned long cmd_mask, int num_flush,
239 struct resource *flush_wpq)
240{
241 return __nvdimm_create(nvdimm_bus, provider_data, groups, flags,
242 cmd_mask, num_flush, flush_wpq, NULL, NULL);
243}
244
245const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
246const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
247u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
248 const struct nd_cmd_desc *desc, int idx, void *buf);
249u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
250 const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
251 const u32 *out_field, unsigned long remainder);
252int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
253struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
254 struct nd_region_desc *ndr_desc);
255struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
256 struct nd_region_desc *ndr_desc);
257struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
258 struct nd_region_desc *ndr_desc);
259void *nd_region_provider_data(struct nd_region *nd_region);
260void *nd_blk_region_provider_data(struct nd_blk_region *ndbr);
261void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data);
262struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr);
263unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr);
264unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
265void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
266u64 nd_fletcher64(void *addr, size_t len, bool le);
267int nvdimm_flush(struct nd_region *nd_region, struct bio *bio);
268int generic_nvdimm_flush(struct nd_region *nd_region);
269int nvdimm_has_flush(struct nd_region *nd_region);
270int nvdimm_has_cache(struct nd_region *nd_region);
271int nvdimm_in_overwrite(struct nvdimm *nvdimm);
272bool is_nvdimm_sync(struct nd_region *nd_region);
273
274static inline int nvdimm_ctl(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
275 unsigned int buf_len, int *cmd_rc)
276{
277 struct nvdimm_bus *nvdimm_bus = nvdimm_to_bus(nvdimm);
278 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
279
280 return nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, cmd_rc);
281}
282
283#ifdef CONFIG_ARCH_HAS_PMEM_API
284#define ARCH_MEMREMAP_PMEM MEMREMAP_WB
285void arch_wb_cache_pmem(void *addr, size_t size);
286void arch_invalidate_pmem(void *addr, size_t size);
287#else
288#define ARCH_MEMREMAP_PMEM MEMREMAP_WT
289static inline void arch_wb_cache_pmem(void *addr, size_t size)
290{
291}
292static inline void arch_invalidate_pmem(void *addr, size_t size)
293{
294}
295#endif
296
297#endif
298