1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
8 #include <linux/ndctl.h>
9 #include <linux/device.h>
10 #include <linux/badblocks.h>
11 #include <linux/perf_event.h>
14 NVDIMM_REVALIDATE_POISON,
15 NVDIMM_REVALIDATE_REGION,
18 enum nvdimm_claim_class {
24 NVDIMM_CCLASS_UNKNOWN,
28 * struct nvdimm_pmu - data structure for nvdimm perf driver
29 * @pmu: pmu data structure for nvdimm performance stats.
30 * @dev: nvdimm device pointer.
31 * @cpu: designated cpu for counter access.
32 * @node: node for cpu hotplug notifier link.
33 * @cpuhp_state: state for cpu hotplug notification.
34 * @arch_cpumask: cpumask to get designated cpu for counter access.
40 struct hlist_node node;
41 enum cpuhp_state cpuhp_state;
42 /* cpumask provided by arch/platform specific code */
43 struct cpumask arch_cpumask;
46 struct nd_device_driver {
47 struct device_driver drv;
49 int (*probe)(struct device *dev);
50 void (*remove)(struct device *dev);
51 void (*shutdown)(struct device *dev);
52 void (*notify)(struct device *dev, enum nvdimm_event event);
55 static inline struct nd_device_driver *to_nd_device_driver(
56 struct device_driver *drv)
58 return container_of(drv, struct nd_device_driver, drv);
62 * struct nd_namespace_common - core infrastructure of a namespace
63 * @force_raw: ignore other personalities for the namespace (e.g. btt)
64 * @dev: device model node
65 * @claim: when set a another personality has taken ownership of the namespace
66 * @claim_class: restrict claim type to a given class
67 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
69 struct nd_namespace_common {
73 enum nvdimm_claim_class claim_class;
74 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
75 void *buf, size_t size, int rw, unsigned long flags);
78 static inline struct nd_namespace_common *to_ndns(struct device *dev)
80 return container_of(dev, struct nd_namespace_common, dev);
84 * struct nd_namespace_io - device representation of a persistent memory range
85 * @dev: namespace device created by the nd region driver
86 * @res: struct resource conversion of a NFIT SPA table
87 * @size: cached resource_size(@res) for fast path size checks
88 * @addr: virtual address to access the namespace range
89 * @bb: badblocks list for the namespace range
91 struct nd_namespace_io {
92 struct nd_namespace_common common;
100 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
101 * @nsio: device and system physical address range to drive
102 * @lbasize: logical sector size for the namespace in block-device-mode
103 * @alt_name: namespace name supplied in the dimm label
104 * @uuid: namespace name supplied in the dimm label
105 * @id: ida allocated id
107 struct nd_namespace_pmem {
108 struct nd_namespace_io nsio;
109 unsigned long lbasize;
116 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
117 * @alt_name: namespace name supplied in the dimm label
118 * @uuid: namespace name supplied in the dimm label
119 * @id: ida allocated id
120 * @lbasize: blk namespaces have a native sector size when btt not present
121 * @size: sum of all the resource ranges allocated to this namespace
122 * @num_resources: number of dpa extents to claim
123 * @res: discontiguous dpa extents for given dimm
125 struct nd_namespace_blk {
126 struct nd_namespace_common common;
130 unsigned long lbasize;
131 resource_size_t size;
133 struct resource **res;
136 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
138 return container_of(dev, struct nd_namespace_io, common.dev);
141 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
143 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
145 return container_of(nsio, struct nd_namespace_pmem, nsio);
148 static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
150 return container_of(dev, struct nd_namespace_blk, common.dev);
154 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
155 * @ndns: device to read
156 * @offset: namespace-relative starting offset
157 * @buf: buffer to fill
158 * @size: transfer length
160 * @buf is up-to-date upon return from this routine.
162 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
163 resource_size_t offset, void *buf, size_t size,
166 return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
170 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
171 * @ndns: device to write
172 * @offset: namespace-relative starting offset
173 * @buf: buffer to drain
174 * @size: transfer length
176 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
177 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
178 * or on backing memory media upon return from this routine. Flushing
179 * to media is handled internal to the @ndns driver, if at all.
181 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
182 resource_size_t offset, void *buf, size_t size,
185 return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
188 #define MODULE_ALIAS_ND_DEVICE(type) \
189 MODULE_ALIAS("nd:t" __stringify(type) "*")
190 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
193 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
194 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
195 struct module *module, const char *mod_name);
196 static inline void nd_driver_unregister(struct nd_device_driver *drv)
198 driver_unregister(&drv->drv);
200 #define nd_driver_register(driver) \
201 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
202 #define module_nd_driver(driver) \
203 module_driver(driver, nd_driver_register, nd_driver_unregister)
204 #endif /* __LINUX_ND_H__ */