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