]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/nd.h
libnvdimm, label: honor the lba size specified in v1.2 labels
[mirror_ubuntu-bionic-kernel.git] / include / linux / nd.h
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 __LINUX_ND_H__
14 #define __LINUX_ND_H__
15 #include <linux/fs.h>
16 #include <linux/ndctl.h>
17 #include <linux/device.h>
18 #include <linux/badblocks.h>
19
20 enum nvdimm_event {
21 NVDIMM_REVALIDATE_POISON,
22 };
23
24 struct nd_device_driver {
25 struct device_driver drv;
26 unsigned long type;
27 int (*probe)(struct device *dev);
28 int (*remove)(struct device *dev);
29 void (*shutdown)(struct device *dev);
30 void (*notify)(struct device *dev, enum nvdimm_event event);
31 };
32
33 static inline struct nd_device_driver *to_nd_device_driver(
34 struct device_driver *drv)
35 {
36 return container_of(drv, struct nd_device_driver, drv);
37 };
38
39 /**
40 * struct nd_namespace_common - core infrastructure of a namespace
41 * @force_raw: ignore other personalities for the namespace (e.g. btt)
42 * @dev: device model node
43 * @claim: when set a another personality has taken ownership of the namespace
44 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
45 */
46 struct nd_namespace_common {
47 int force_raw;
48 struct device dev;
49 struct device *claim;
50 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
51 void *buf, size_t size, int rw, unsigned long flags);
52 };
53
54 static inline struct nd_namespace_common *to_ndns(struct device *dev)
55 {
56 return container_of(dev, struct nd_namespace_common, dev);
57 }
58
59 /**
60 * struct nd_namespace_io - device representation of a persistent memory range
61 * @dev: namespace device created by the nd region driver
62 * @res: struct resource conversion of a NFIT SPA table
63 * @size: cached resource_size(@res) for fast path size checks
64 * @addr: virtual address to access the namespace range
65 * @bb: badblocks list for the namespace range
66 */
67 struct nd_namespace_io {
68 struct nd_namespace_common common;
69 struct resource res;
70 resource_size_t size;
71 void *addr;
72 struct badblocks bb;
73 };
74
75 /**
76 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
77 * @nsio: device and system physical address range to drive
78 * @lbasize: logical sector size for the namespace in block-device-mode
79 * @alt_name: namespace name supplied in the dimm label
80 * @uuid: namespace name supplied in the dimm label
81 * @id: ida allocated id
82 */
83 struct nd_namespace_pmem {
84 struct nd_namespace_io nsio;
85 unsigned long lbasize;
86 char *alt_name;
87 u8 *uuid;
88 int id;
89 };
90
91 /**
92 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
93 * @alt_name: namespace name supplied in the dimm label
94 * @uuid: namespace name supplied in the dimm label
95 * @id: ida allocated id
96 * @lbasize: blk namespaces have a native sector size when btt not present
97 * @size: sum of all the resource ranges allocated to this namespace
98 * @num_resources: number of dpa extents to claim
99 * @res: discontiguous dpa extents for given dimm
100 */
101 struct nd_namespace_blk {
102 struct nd_namespace_common common;
103 char *alt_name;
104 u8 *uuid;
105 int id;
106 unsigned long lbasize;
107 resource_size_t size;
108 int num_resources;
109 struct resource **res;
110 };
111
112 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
113 {
114 return container_of(dev, struct nd_namespace_io, common.dev);
115 }
116
117 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
118 {
119 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
120
121 return container_of(nsio, struct nd_namespace_pmem, nsio);
122 }
123
124 static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
125 {
126 return container_of(dev, struct nd_namespace_blk, common.dev);
127 }
128
129 /**
130 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
131 * @ndns: device to read
132 * @offset: namespace-relative starting offset
133 * @buf: buffer to fill
134 * @size: transfer length
135 *
136 * @buf is up-to-date upon return from this routine.
137 */
138 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
139 resource_size_t offset, void *buf, size_t size,
140 unsigned long flags)
141 {
142 return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
143 }
144
145 /**
146 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
147 * @ndns: device to read
148 * @offset: namespace-relative starting offset
149 * @buf: buffer to drain
150 * @size: transfer length
151 *
152 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
153 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
154 * or on backing memory media upon return from this routine. Flushing
155 * to media is handled internal to the @ndns driver, if at all.
156 */
157 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
158 resource_size_t offset, void *buf, size_t size,
159 unsigned long flags)
160 {
161 return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
162 }
163
164 #define MODULE_ALIAS_ND_DEVICE(type) \
165 MODULE_ALIAS("nd:t" __stringify(type) "*")
166 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
167
168 struct nd_region;
169 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
170 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
171 struct module *module, const char *mod_name);
172 #define nd_driver_register(driver) \
173 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
174 #endif /* __LINUX_ND_H__ */