2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
13 #ifndef __LINUX_ND_H__
14 #define __LINUX_ND_H__
16 #include <linux/ndctl.h>
17 #include <linux/device.h>
18 #include <linux/badblocks.h>
21 NVDIMM_REVALIDATE_POISON,
24 enum nvdimm_claim_class {
30 NVDIMM_CCLASS_UNKNOWN,
33 struct nd_device_driver {
34 struct device_driver drv;
36 int (*probe)(struct device *dev);
37 int (*remove)(struct device *dev);
38 void (*shutdown)(struct device *dev);
39 void (*notify)(struct device *dev, enum nvdimm_event event);
42 static inline struct nd_device_driver *to_nd_device_driver(
43 struct device_driver *drv)
45 return container_of(drv, struct nd_device_driver, drv);
49 * struct nd_namespace_common - core infrastructure of a namespace
50 * @force_raw: ignore other personalities for the namespace (e.g. btt)
51 * @dev: device model node
52 * @claim: when set a another personality has taken ownership of the namespace
53 * @claim_class: restrict claim type to a given class
54 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
56 struct nd_namespace_common {
60 enum nvdimm_claim_class claim_class;
61 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
62 void *buf, size_t size, int rw, unsigned long flags);
65 static inline struct nd_namespace_common *to_ndns(struct device *dev)
67 return container_of(dev, struct nd_namespace_common, dev);
71 * struct nd_namespace_io - device representation of a persistent memory range
72 * @dev: namespace device created by the nd region driver
73 * @res: struct resource conversion of a NFIT SPA table
74 * @size: cached resource_size(@res) for fast path size checks
75 * @addr: virtual address to access the namespace range
76 * @bb: badblocks list for the namespace range
78 struct nd_namespace_io {
79 struct nd_namespace_common common;
87 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
88 * @nsio: device and system physical address range to drive
89 * @lbasize: logical sector size for the namespace in block-device-mode
90 * @alt_name: namespace name supplied in the dimm label
91 * @uuid: namespace name supplied in the dimm label
92 * @id: ida allocated id
94 struct nd_namespace_pmem {
95 struct nd_namespace_io nsio;
96 unsigned long lbasize;
103 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
104 * @alt_name: namespace name supplied in the dimm label
105 * @uuid: namespace name supplied in the dimm label
106 * @id: ida allocated id
107 * @lbasize: blk namespaces have a native sector size when btt not present
108 * @size: sum of all the resource ranges allocated to this namespace
109 * @num_resources: number of dpa extents to claim
110 * @res: discontiguous dpa extents for given dimm
112 struct nd_namespace_blk {
113 struct nd_namespace_common common;
117 unsigned long lbasize;
118 resource_size_t size;
120 struct resource **res;
123 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
125 return container_of(dev, struct nd_namespace_io, common.dev);
128 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
130 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
132 return container_of(nsio, struct nd_namespace_pmem, nsio);
135 static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
137 return container_of(dev, struct nd_namespace_blk, common.dev);
141 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
142 * @ndns: device to read
143 * @offset: namespace-relative starting offset
144 * @buf: buffer to fill
145 * @size: transfer length
147 * @buf is up-to-date upon return from this routine.
149 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
150 resource_size_t offset, void *buf, size_t size,
153 return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
157 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
158 * @ndns: device to read
159 * @offset: namespace-relative starting offset
160 * @buf: buffer to drain
161 * @size: transfer length
163 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
164 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
165 * or on backing memory media upon return from this routine. Flushing
166 * to media is handled internal to the @ndns driver, if at all.
168 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
169 resource_size_t offset, void *buf, size_t size,
172 return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
175 #define MODULE_ALIAS_ND_DEVICE(type) \
176 MODULE_ALIAS("nd:t" __stringify(type) "*")
177 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
180 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
181 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
182 struct module *module, const char *mod_name);
183 static inline void nd_driver_unregister(struct nd_device_driver *drv)
185 driver_unregister(&drv->drv);
187 #define nd_driver_register(driver) \
188 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
189 #define module_nd_driver(driver) \
190 module_driver(driver, nd_driver_register, nd_driver_unregister)
191 #endif /* __LINUX_ND_H__ */