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