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