xref: /linux-6.15/include/linux/nd.h (revision f8669f1d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #ifndef __LINUX_ND_H__
6 #define __LINUX_ND_H__
7 #include <linux/fs.h>
8 #include <linux/ndctl.h>
9 #include <linux/device.h>
10 #include <linux/badblocks.h>
11 #include <linux/perf_event.h>
12 #include <linux/platform_device.h>
13 
14 enum nvdimm_event {
15 	NVDIMM_REVALIDATE_POISON,
16 	NVDIMM_REVALIDATE_REGION,
17 };
18 
19 enum nvdimm_claim_class {
20 	NVDIMM_CCLASS_NONE,
21 	NVDIMM_CCLASS_BTT,
22 	NVDIMM_CCLASS_BTT2,
23 	NVDIMM_CCLASS_PFN,
24 	NVDIMM_CCLASS_DAX,
25 	NVDIMM_CCLASS_UNKNOWN,
26 };
27 
28 #define NVDIMM_EVENT_VAR(_id)  event_attr_##_id
29 #define NVDIMM_EVENT_PTR(_id)  (&event_attr_##_id.attr.attr)
30 
31 #define NVDIMM_EVENT_ATTR(_name, _id)				\
32 	PMU_EVENT_ATTR(_name, NVDIMM_EVENT_VAR(_id), _id,	\
33 			nvdimm_events_sysfs_show)
34 
35 /* Event attribute array index */
36 #define NVDIMM_PMU_FORMAT_ATTR	0
37 #define NVDIMM_PMU_EVENT_ATTR	1
38 #define NVDIMM_PMU_CPUMASK_ATTR	2
39 #define NVDIMM_PMU_NULL_ATTR	3
40 
41 /**
42  * struct nvdimm_pmu - data structure for nvdimm perf driver
43  * @pmu: pmu data structure for nvdimm performance stats.
44  * @dev: nvdimm device pointer.
45  * @cpu: designated cpu for counter access.
46  * @node: node for cpu hotplug notifier link.
47  * @cpuhp_state: state for cpu hotplug notification.
48  * @arch_cpumask: cpumask to get designated cpu for counter access.
49  */
50 struct nvdimm_pmu {
51 	struct pmu pmu;
52 	struct device *dev;
53 	int cpu;
54 	struct hlist_node node;
55 	enum cpuhp_state cpuhp_state;
56 	/* cpumask provided by arch/platform specific code */
57 	struct cpumask arch_cpumask;
58 };
59 
60 extern ssize_t nvdimm_events_sysfs_show(struct device *dev,
61 					struct device_attribute *attr,
62 					char *page);
63 
64 int register_nvdimm_pmu(struct nvdimm_pmu *nvdimm, struct platform_device *pdev);
65 void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu);
66 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu);
67 int perf_pmu_register(struct pmu *pmu, const char *name, int type);
68 void perf_pmu_unregister(struct pmu *pmu);
69 
70 struct nd_device_driver {
71 	struct device_driver drv;
72 	unsigned long type;
73 	int (*probe)(struct device *dev);
74 	void (*remove)(struct device *dev);
75 	void (*shutdown)(struct device *dev);
76 	void (*notify)(struct device *dev, enum nvdimm_event event);
77 };
78 
79 static inline struct nd_device_driver *to_nd_device_driver(
80 		struct device_driver *drv)
81 {
82 	return container_of(drv, struct nd_device_driver, drv);
83 };
84 
85 /**
86  * struct nd_namespace_common - core infrastructure of a namespace
87  * @force_raw: ignore other personalities for the namespace (e.g. btt)
88  * @dev: device model node
89  * @claim: when set a another personality has taken ownership of the namespace
90  * @claim_class: restrict claim type to a given class
91  * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
92  */
93 struct nd_namespace_common {
94 	int force_raw;
95 	struct device dev;
96 	struct device *claim;
97 	enum nvdimm_claim_class claim_class;
98 	int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
99 			void *buf, size_t size, int rw, unsigned long flags);
100 };
101 
102 static inline struct nd_namespace_common *to_ndns(struct device *dev)
103 {
104 	return container_of(dev, struct nd_namespace_common, dev);
105 }
106 
107 /**
108  * struct nd_namespace_io - device representation of a persistent memory range
109  * @dev: namespace device created by the nd region driver
110  * @res: struct resource conversion of a NFIT SPA table
111  * @size: cached resource_size(@res) for fast path size checks
112  * @addr: virtual address to access the namespace range
113  * @bb: badblocks list for the namespace range
114  */
115 struct nd_namespace_io {
116 	struct nd_namespace_common common;
117 	struct resource res;
118 	resource_size_t size;
119 	void *addr;
120 	struct badblocks bb;
121 };
122 
123 /**
124  * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
125  * @nsio: device and system physical address range to drive
126  * @lbasize: logical sector size for the namespace in block-device-mode
127  * @alt_name: namespace name supplied in the dimm label
128  * @uuid: namespace name supplied in the dimm label
129  * @id: ida allocated id
130  */
131 struct nd_namespace_pmem {
132 	struct nd_namespace_io nsio;
133 	unsigned long lbasize;
134 	char *alt_name;
135 	uuid_t *uuid;
136 	int id;
137 };
138 
139 /**
140  * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
141  * @alt_name: namespace name supplied in the dimm label
142  * @uuid: namespace name supplied in the dimm label
143  * @id: ida allocated id
144  * @lbasize: blk namespaces have a native sector size when btt not present
145  * @size: sum of all the resource ranges allocated to this namespace
146  * @num_resources: number of dpa extents to claim
147  * @res: discontiguous dpa extents for given dimm
148  */
149 struct nd_namespace_blk {
150 	struct nd_namespace_common common;
151 	char *alt_name;
152 	uuid_t *uuid;
153 	int id;
154 	unsigned long lbasize;
155 	resource_size_t size;
156 	int num_resources;
157 	struct resource **res;
158 };
159 
160 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
161 {
162 	return container_of(dev, struct nd_namespace_io, common.dev);
163 }
164 
165 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
166 {
167 	struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
168 
169 	return container_of(nsio, struct nd_namespace_pmem, nsio);
170 }
171 
172 static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
173 {
174 	return container_of(dev, struct nd_namespace_blk, common.dev);
175 }
176 
177 /**
178  * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
179  * @ndns: device to read
180  * @offset: namespace-relative starting offset
181  * @buf: buffer to fill
182  * @size: transfer length
183  *
184  * @buf is up-to-date upon return from this routine.
185  */
186 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
187 		resource_size_t offset, void *buf, size_t size,
188 		unsigned long flags)
189 {
190 	return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
191 }
192 
193 /**
194  * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
195  * @ndns: device to write
196  * @offset: namespace-relative starting offset
197  * @buf: buffer to drain
198  * @size: transfer length
199  *
200  * NVDIMM Namepaces disks do not implement sectors internally.  Depending on
201  * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
202  * or on backing memory media upon return from this routine.  Flushing
203  * to media is handled internal to the @ndns driver, if at all.
204  */
205 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
206 		resource_size_t offset, void *buf, size_t size,
207 		unsigned long flags)
208 {
209 	return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
210 }
211 
212 #define MODULE_ALIAS_ND_DEVICE(type) \
213 	MODULE_ALIAS("nd:t" __stringify(type) "*")
214 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
215 
216 struct nd_region;
217 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
218 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
219 		struct module *module, const char *mod_name);
220 static inline void nd_driver_unregister(struct nd_device_driver *drv)
221 {
222 	driver_unregister(&drv->drv);
223 }
224 #define nd_driver_register(driver) \
225 	__nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
226 #define module_nd_driver(driver) \
227 	module_driver(driver, nd_driver_register, nd_driver_unregister)
228 #endif /* __LINUX_ND_H__ */
229