Merge branch 'efi-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / nvdimm / nd.h
CommitLineData
4d88a97a
DW
1/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#ifndef __ND_H__
14#define __ND_H__
1f7df6f8 15#include <linux/libnvdimm.h>
200c79da 16#include <linux/badblocks.h>
f0dc089c 17#include <linux/blkdev.h>
4d88a97a
DW
18#include <linux/device.h>
19#include <linux/mutex.h>
20#include <linux/ndctl.h>
bf9bccc1 21#include <linux/types.h>
71999466 22#include <linux/nd.h>
4a826c83 23#include "label.h"
4d88a97a 24
8c2f7e86 25enum {
5212e11f
VV
26 /*
27 * Limits the maximum number of block apertures a dimm can
28 * support and is an input to the geometry/on-disk-format of a
29 * BTT instance
30 */
31 ND_MAX_LANES = 256,
8c2f7e86 32 SECTOR_SHIFT = 9,
fcae6957 33 INT_LBASIZE_ALIGNMENT = 64,
3ae3d67b 34 NVDIMM_IO_ATOMIC = 1,
8c2f7e86
DW
35};
36
4d88a97a
DW
37struct nvdimm_drvdata {
38 struct device *dev;
02881768 39 int nslabel_size;
4d88a97a
DW
40 struct nd_cmd_get_config_size nsarea;
41 void *data;
4a826c83
DW
42 int ns_current, ns_next;
43 struct resource dpa;
bf9bccc1 44 struct kref kref;
4d88a97a
DW
45};
46
e5ae3b25
DW
47struct nd_region_data {
48 int ns_count;
49 int ns_active;
595c7307
DW
50 unsigned int hints_shift;
51 void __iomem *flush_wpq[0];
3d88002e
DW
52};
53
595c7307
DW
54static inline void __iomem *ndrd_get_flush_wpq(struct nd_region_data *ndrd,
55 int dimm, int hint)
56{
57 unsigned int num = 1 << ndrd->hints_shift;
58 unsigned int mask = num - 1;
59
60 return ndrd->flush_wpq[dimm * num + (hint & mask)];
61}
62
63static inline void ndrd_set_flush_wpq(struct nd_region_data *ndrd, int dimm,
64 int hint, void __iomem *flush)
65{
66 unsigned int num = 1 << ndrd->hints_shift;
67 unsigned int mask = num - 1;
68
69 ndrd->flush_wpq[dimm * num + (hint & mask)] = flush;
70}
71
4a826c83
DW
72static inline struct nd_namespace_index *to_namespace_index(
73 struct nvdimm_drvdata *ndd, int i)
74{
75 if (i < 0)
76 return NULL;
77
78 return ndd->data + sizeof_namespace_index(ndd) * i;
79}
80
81static inline struct nd_namespace_index *to_current_namespace_index(
82 struct nvdimm_drvdata *ndd)
83{
84 return to_namespace_index(ndd, ndd->ns_current);
85}
86
87static inline struct nd_namespace_index *to_next_namespace_index(
88 struct nvdimm_drvdata *ndd)
89{
90 return to_namespace_index(ndd, ndd->ns_next);
91}
92
564e871a
DW
93unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd);
94
95#define namespace_label_has(ndd, field) \
96 (offsetof(struct nd_namespace_label, field) \
97 < sizeof_namespace_label(ndd))
98
4a826c83
DW
99#define nd_dbg_dpa(r, d, res, fmt, arg...) \
100 dev_dbg((r) ? &(r)->dev : (d)->dev, "%s: %.13s: %#llx @ %#llx " fmt, \
101 (r) ? dev_name((d)->dev) : "", res ? res->name : "null", \
102 (unsigned long long) (res ? resource_size(res) : 0), \
103 (unsigned long long) (res ? res->start : 0), ##arg)
104
bf9bccc1
DW
105#define for_each_dpa_resource(ndd, res) \
106 for (res = (ndd)->dpa.child; res; res = res->sibling)
107
4a826c83
DW
108#define for_each_dpa_resource_safe(ndd, res, next) \
109 for (res = (ndd)->dpa.child, next = res ? res->sibling : NULL; \
110 res; res = next, next = next ? next->sibling : NULL)
111
5212e11f
VV
112struct nd_percpu_lane {
113 int count;
114 spinlock_t lock;
115};
116
ae8219f1
DW
117struct nd_label_ent {
118 struct list_head list;
119 struct nd_namespace_label *label;
120};
121
122enum nd_mapping_lock_class {
123 ND_MAPPING_CLASS0,
124 ND_MAPPING_UUID_SCAN,
125};
126
44c462eb
DW
127struct nd_mapping {
128 struct nvdimm *nvdimm;
44c462eb
DW
129 u64 start;
130 u64 size;
401c0a19 131 int position;
ae8219f1
DW
132 struct list_head labels;
133 struct mutex lock;
44c462eb
DW
134 /*
135 * @ndd is for private use at region enable / disable time for
136 * get_ndd() + put_ndd(), all other nd_mapping to ndd
137 * conversions use to_ndd() which respects enabled state of the
138 * nvdimm.
139 */
140 struct nvdimm_drvdata *ndd;
141};
142
1f7df6f8
DW
143struct nd_region {
144 struct device dev;
1b40e09a 145 struct ida ns_ida;
8c2f7e86 146 struct ida btt_ida;
e1455744 147 struct ida pfn_ida;
cd03412a 148 struct ida dax_ida;
004f1afb 149 unsigned long flags;
bf9bccc1 150 struct device *ns_seed;
8c2f7e86 151 struct device *btt_seed;
e1455744 152 struct device *pfn_seed;
cd03412a 153 struct device *dax_seed;
1f7df6f8
DW
154 u16 ndr_mappings;
155 u64 ndr_size;
156 u64 ndr_start;
41d7a6d6 157 int id, num_lanes, ro, numa_node;
1f7df6f8 158 void *provider_data;
975750a9 159 struct kernfs_node *bb_state;
6a6bef90 160 struct badblocks bb;
eaf96153 161 struct nd_interleave_set *nd_set;
5212e11f 162 struct nd_percpu_lane __percpu *lane;
1f7df6f8
DW
163 struct nd_mapping mapping[0];
164};
165
047fc8a1
RZ
166struct nd_blk_region {
167 int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
047fc8a1
RZ
168 int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
169 void *iobuf, u64 len, int rw);
170 void *blk_provider_data;
171 struct nd_region nd_region;
172};
173
4a826c83
DW
174/*
175 * Lookup next in the repeating sequence of 01, 10, and 11.
176 */
177static inline unsigned nd_inc_seq(unsigned seq)
178{
179 static const unsigned next[] = { 0, 2, 3, 1 };
180
181 return next[seq & 3];
182}
f524bf27 183
5212e11f 184struct btt;
8c2f7e86
DW
185struct nd_btt {
186 struct device dev;
187 struct nd_namespace_common *ndns;
5212e11f 188 struct btt *btt;
8c2f7e86 189 unsigned long lbasize;
abe8b4e3 190 u64 size;
8c2f7e86
DW
191 u8 *uuid;
192 int id;
14e49454
VV
193 int initial_offset;
194 u16 version_major;
195 u16 version_minor;
8c2f7e86
DW
196};
197
e1455744
DW
198enum nd_pfn_mode {
199 PFN_MODE_NONE,
200 PFN_MODE_RAM,
201 PFN_MODE_PMEM,
202};
203
204struct nd_pfn {
205 int id;
206 u8 *uuid;
207 struct device dev;
315c5625 208 unsigned long align;
e1455744
DW
209 unsigned long npfns;
210 enum nd_pfn_mode mode;
211 struct nd_pfn_sb *pfn_sb;
212 struct nd_namespace_common *ndns;
213};
214
cd03412a
DW
215struct nd_dax {
216 struct nd_pfn nd_pfn;
217};
218
4d88a97a
DW
219enum nd_async_mode {
220 ND_SYNC,
221 ND_ASYNC,
222};
223
41cd8b70 224int nd_integrity_init(struct gendisk *disk, unsigned long meta_size);
bf9bccc1 225void wait_nvdimm_bus_probe_idle(struct device *dev);
4d88a97a
DW
226void nd_device_register(struct device *dev);
227void nd_device_unregister(struct device *dev, enum nd_async_mode mode);
71999466 228void nd_device_notify(struct device *dev, enum nvdimm_event event);
bf9bccc1
DW
229int nd_uuid_store(struct device *dev, u8 **uuid_out, const char *buf,
230 size_t len);
b2c48f9f 231ssize_t nd_size_select_show(unsigned long current_size,
1b40e09a 232 const unsigned long *supported, char *buf);
b2c48f9f
DW
233ssize_t nd_size_select_store(struct device *dev, const char *buf,
234 unsigned long *current_size, const unsigned long *supported);
4d88a97a 235int __init nvdimm_init(void);
3d88002e 236int __init nd_region_init(void);
b3fde74e 237int __init nd_label_init(void);
4d88a97a 238void nvdimm_exit(void);
3d88002e 239void nd_region_exit(void);
bf9bccc1
DW
240struct nvdimm;
241struct nvdimm_drvdata *to_ndd(struct nd_mapping *nd_mapping);
aee65987 242int nvdimm_check_config_data(struct device *dev);
4d88a97a
DW
243int nvdimm_init_nsarea(struct nvdimm_drvdata *ndd);
244int nvdimm_init_config_data(struct nvdimm_drvdata *ndd);
f524bf27
DW
245int nvdimm_set_config_data(struct nvdimm_drvdata *ndd, size_t offset,
246 void *buf, size_t len);
59e64739
DW
247long nvdimm_clear_poison(struct device *dev, phys_addr_t phys,
248 unsigned int len);
42237e39 249void nvdimm_set_aliasing(struct device *dev);
8f078b38 250void nvdimm_set_locked(struct device *dev);
d34cb808 251void nvdimm_clear_locked(struct device *dev);
8c2f7e86 252struct nd_btt *to_nd_btt(struct device *dev);
e1455744
DW
253
254struct nd_gen_sb {
255 char reserved[SZ_4K - 8];
256 __le64 checksum;
257};
258
259u64 nd_sb_checksum(struct nd_gen_sb *sb);
8c2f7e86 260#if IS_ENABLED(CONFIG_BTT)
200c79da 261int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns);
8c2f7e86
DW
262bool is_nd_btt(struct device *dev);
263struct device *nd_btt_create(struct nd_region *nd_region);
264#else
e32bc729 265static inline int nd_btt_probe(struct device *dev,
200c79da 266 struct nd_namespace_common *ndns)
8c2f7e86
DW
267{
268 return -ENODEV;
269}
270
271static inline bool is_nd_btt(struct device *dev)
272{
273 return false;
274}
275
276static inline struct device *nd_btt_create(struct nd_region *nd_region)
277{
278 return NULL;
279}
e1455744 280#endif
8c2f7e86 281
e1455744
DW
282struct nd_pfn *to_nd_pfn(struct device *dev);
283#if IS_ENABLED(CONFIG_NVDIMM_PFN)
0dd69643
OH
284
285#ifdef CONFIG_TRANSPARENT_HUGEPAGE
286#define PFN_DEFAULT_ALIGNMENT HPAGE_PMD_SIZE
287#else
288#define PFN_DEFAULT_ALIGNMENT PAGE_SIZE
289#endif
290
200c79da 291int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns);
e1455744
DW
292bool is_nd_pfn(struct device *dev);
293struct device *nd_pfn_create(struct nd_region *nd_region);
cd03412a
DW
294struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
295 struct nd_namespace_common *ndns);
c5ed9268 296int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig);
cd03412a 297extern struct attribute_group nd_pfn_attribute_group;
e1455744 298#else
200c79da
DW
299static inline int nd_pfn_probe(struct device *dev,
300 struct nd_namespace_common *ndns)
e1455744
DW
301{
302 return -ENODEV;
303}
304
305static inline bool is_nd_pfn(struct device *dev)
306{
307 return false;
308}
309
310static inline struct device *nd_pfn_create(struct nd_region *nd_region)
311{
312 return NULL;
313}
32ab0a3f 314
c5ed9268 315static inline int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
32ab0a3f
DW
316{
317 return -ENODEV;
318}
8c2f7e86 319#endif
e1455744 320
cd03412a
DW
321struct nd_dax *to_nd_dax(struct device *dev);
322#if IS_ENABLED(CONFIG_NVDIMM_DAX)
c5ed9268 323int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
cd03412a
DW
324bool is_nd_dax(struct device *dev);
325struct device *nd_dax_create(struct nd_region *nd_region);
326#else
c5ed9268
DW
327static inline int nd_dax_probe(struct device *dev,
328 struct nd_namespace_common *ndns)
329{
330 return -ENODEV;
331}
332
cd03412a
DW
333static inline bool is_nd_dax(struct device *dev)
334{
335 return false;
336}
337
338static inline struct device *nd_dax_create(struct nd_region *nd_region)
339{
340 return NULL;
341}
342#endif
343
3d88002e
DW
344struct nd_region *to_nd_region(struct device *dev);
345int nd_region_to_nstype(struct nd_region *nd_region);
346int nd_region_register_namespaces(struct nd_region *nd_region, int *err);
c12c48ce
DW
347u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
348 struct nd_namespace_index *nsindex);
86ef58a4 349u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region);
3d88002e
DW
350void nvdimm_bus_lock(struct device *dev);
351void nvdimm_bus_unlock(struct device *dev);
352bool is_nvdimm_bus_locked(struct device *dev);
58138820 353int nvdimm_revalidate_disk(struct gendisk *disk);
bf9bccc1
DW
354void nvdimm_drvdata_release(struct kref *kref);
355void put_ndd(struct nvdimm_drvdata *ndd);
4a826c83
DW
356int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd);
357void nvdimm_free_dpa(struct nvdimm_drvdata *ndd, struct resource *res);
358struct resource *nvdimm_allocate_dpa(struct nvdimm_drvdata *ndd,
359 struct nd_label_id *label_id, resource_size_t start,
360 resource_size_t n);
8c2f7e86
DW
361resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns);
362struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev);
5212e11f 363int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns);
298f2bc5 364int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt);
5212e11f
VV
365const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
366 char *name);
f979b13c 367unsigned int pmem_sector_size(struct nd_namespace_common *ndns);
a3901802
DW
368void nvdimm_badblocks_populate(struct nd_region *nd_region,
369 struct badblocks *bb, const struct resource *res);
200c79da 370#if IS_ENABLED(CONFIG_ND_CLAIM)
e8d51348 371int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap);
200c79da
DW
372int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio);
373void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio);
374#else
e8d51348
CH
375static inline int nvdimm_setup_pfn(struct nd_pfn *nd_pfn,
376 struct dev_pagemap *pgmap)
ac515c08 377{
e8d51348 378 return -ENXIO;
ac515c08 379}
200c79da
DW
380static inline int devm_nsio_enable(struct device *dev,
381 struct nd_namespace_io *nsio)
382{
383 return -ENXIO;
384}
385static inline void devm_nsio_disable(struct device *dev,
386 struct nd_namespace_io *nsio)
387{
388}
389#endif
047fc8a1 390int nd_blk_region_init(struct nd_region *nd_region);
e5ae3b25 391int nd_region_activate(struct nd_region *nd_region);
f0dc089c
DW
392void __nd_iostat_start(struct bio *bio, unsigned long *start);
393static inline bool nd_iostat_start(struct bio *bio, unsigned long *start)
394{
74d46992 395 struct gendisk *disk = bio->bi_disk;
f0dc089c
DW
396
397 if (!blk_queue_io_stat(disk->queue))
398 return false;
399
8d7c22ac 400 *start = jiffies;
d62e26b3 401 generic_start_io_acct(disk->queue, bio_data_dir(bio),
8d7c22ac 402 bio_sectors(bio), &disk->part0);
f0dc089c
DW
403 return true;
404}
8d7c22ac
TK
405static inline void nd_iostat_end(struct bio *bio, unsigned long start)
406{
74d46992 407 struct gendisk *disk = bio->bi_disk;
8d7c22ac 408
d62e26b3
JA
409 generic_end_io_acct(disk->queue, bio_data_dir(bio), &disk->part0,
410 start);
8d7c22ac 411}
200c79da
DW
412static inline bool is_bad_pmem(struct badblocks *bb, sector_t sector,
413 unsigned int len)
414{
415 if (bb->count) {
416 sector_t first_bad;
417 int num_bad;
418
419 return !!badblocks_check(bb, sector, len / 512, &first_bad,
420 &num_bad);
421 }
422
423 return false;
424}
047fc8a1 425resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk);
6ec68954 426const u8 *nd_dev_to_uuid(struct device *dev);
004f1afb 427bool pmem_should_map_pages(struct device *dev);
4d88a97a 428#endif /* __ND_H__ */