blob: 55c7359978054cd586a9172a58e3240ad3ae7868 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Dan Williams4d88a972015-05-31 14:41:48 -04002/*
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
Dan Williams4d88a972015-05-31 14:41:48 -04004 */
5#ifndef __LINUX_ND_H__
6#define __LINUX_ND_H__
Dan Williams8c2f7e82015-06-25 04:20:04 -04007#include <linux/fs.h>
Dan Williams4d88a972015-05-31 14:41:48 -04008#include <linux/ndctl.h>
9#include <linux/device.h>
Dan Williams200c79d2016-03-22 00:22:16 -070010#include <linux/badblocks.h>
Dan Williams4d88a972015-05-31 14:41:48 -040011
Dan Williams71999462016-02-18 10:29:49 -080012enum nvdimm_event {
13 NVDIMM_REVALIDATE_POISON,
14};
15
Dan Williamsb3fde742017-06-04 10:18:39 +090016enum nvdimm_claim_class {
17 NVDIMM_CCLASS_NONE,
18 NVDIMM_CCLASS_BTT,
Vishal Verma14e49452017-06-28 14:25:00 -060019 NVDIMM_CCLASS_BTT2,
Dan Williamsb3fde742017-06-04 10:18:39 +090020 NVDIMM_CCLASS_PFN,
21 NVDIMM_CCLASS_DAX,
22 NVDIMM_CCLASS_UNKNOWN,
23};
24
Dan Williams4d88a972015-05-31 14:41:48 -040025struct nd_device_driver {
26 struct device_driver drv;
27 unsigned long type;
28 int (*probe)(struct device *dev);
29 int (*remove)(struct device *dev);
Dan Williams476f8482016-07-09 00:12:52 -070030 void (*shutdown)(struct device *dev);
Dan Williams71999462016-02-18 10:29:49 -080031 void (*notify)(struct device *dev, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -040032};
33
34static inline struct nd_device_driver *to_nd_device_driver(
35 struct device_driver *drv)
36{
37 return container_of(drv, struct nd_device_driver, drv);
Dan Williams3d880022015-05-31 15:02:11 -040038};
39
Dan Williamsbf9bccc2015-06-17 17:14:46 -040040/**
Dan Williams8c2f7e82015-06-25 04:20:04 -040041 * struct nd_namespace_common - core infrastructure of a namespace
42 * @force_raw: ignore other personalities for the namespace (e.g. btt)
43 * @dev: device model node
44 * @claim: when set a another personality has taken ownership of the namespace
Dan Williamsb3fde742017-06-04 10:18:39 +090045 * @claim_class: restrict claim type to a given class
Dan Williams8c2f7e82015-06-25 04:20:04 -040046 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
47 */
48struct nd_namespace_common {
49 int force_raw;
50 struct device dev;
51 struct device *claim;
Dan Williamsb3fde742017-06-04 10:18:39 +090052 enum nvdimm_claim_class claim_class;
Dan Williams8c2f7e82015-06-25 04:20:04 -040053 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
Vishal Verma3ae3d672017-05-10 15:01:30 -060054 void *buf, size_t size, int rw, unsigned long flags);
Dan Williams8c2f7e82015-06-25 04:20:04 -040055};
56
57static inline struct nd_namespace_common *to_ndns(struct device *dev)
58{
59 return container_of(dev, struct nd_namespace_common, dev);
60}
61
62/**
Dan Williams200c79d2016-03-22 00:22:16 -070063 * struct nd_namespace_io - device representation of a persistent memory range
Dan Williamsbf9bccc2015-06-17 17:14:46 -040064 * @dev: namespace device created by the nd region driver
65 * @res: struct resource conversion of a NFIT SPA table
Dan Williams200c79d2016-03-22 00:22:16 -070066 * @size: cached resource_size(@res) for fast path size checks
67 * @addr: virtual address to access the namespace range
68 * @bb: badblocks list for the namespace range
Dan Williamsbf9bccc2015-06-17 17:14:46 -040069 */
Dan Williams3d880022015-05-31 15:02:11 -040070struct nd_namespace_io {
Dan Williams8c2f7e82015-06-25 04:20:04 -040071 struct nd_namespace_common common;
Dan Williams3d880022015-05-31 15:02:11 -040072 struct resource res;
Dan Williams200c79d2016-03-22 00:22:16 -070073 resource_size_t size;
Dan Williams7a9eb202016-06-03 18:06:47 -070074 void *addr;
Dan Williams200c79d2016-03-22 00:22:16 -070075 struct badblocks bb;
Dan Williams3d880022015-05-31 15:02:11 -040076};
77
Dan Williamsbf9bccc2015-06-17 17:14:46 -040078/**
79 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
80 * @nsio: device and system physical address range to drive
Dan Williamsf979b132017-06-04 12:12:07 +090081 * @lbasize: logical sector size for the namespace in block-device-mode
Dan Williamsbf9bccc2015-06-17 17:14:46 -040082 * @alt_name: namespace name supplied in the dimm label
83 * @uuid: namespace name supplied in the dimm label
Dan Williams0e3b0d12016-10-06 23:13:15 -070084 * @id: ida allocated id
Dan Williamsbf9bccc2015-06-17 17:14:46 -040085 */
86struct nd_namespace_pmem {
87 struct nd_namespace_io nsio;
Dan Williamsf979b132017-06-04 12:12:07 +090088 unsigned long lbasize;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040089 char *alt_name;
90 u8 *uuid;
Dan Williams0e3b0d12016-10-06 23:13:15 -070091 int id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040092};
93
Dan Williams1b40e092015-05-01 13:34:01 -040094/**
95 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
Dan Williams1b40e092015-05-01 13:34:01 -040096 * @alt_name: namespace name supplied in the dimm label
97 * @uuid: namespace name supplied in the dimm label
98 * @id: ida allocated id
99 * @lbasize: blk namespaces have a native sector size when btt not present
Dan Williams9d90725d2016-03-18 11:27:36 -0700100 * @size: sum of all the resource ranges allocated to this namespace
Dan Williams1b40e092015-05-01 13:34:01 -0400101 * @num_resources: number of dpa extents to claim
102 * @res: discontiguous dpa extents for given dimm
103 */
104struct nd_namespace_blk {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400105 struct nd_namespace_common common;
Dan Williams1b40e092015-05-01 13:34:01 -0400106 char *alt_name;
107 u8 *uuid;
108 int id;
109 unsigned long lbasize;
Dan Williams9d90725d2016-03-18 11:27:36 -0700110 resource_size_t size;
Dan Williams1b40e092015-05-01 13:34:01 -0400111 int num_resources;
112 struct resource **res;
113};
114
Dan Williams6ff3e912016-10-05 14:04:15 -0700115static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
Dan Williams3d880022015-05-31 15:02:11 -0400116{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400117 return container_of(dev, struct nd_namespace_io, common.dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400118}
119
Dan Williams6ff3e912016-10-05 14:04:15 -0700120static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400121{
122 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
123
124 return container_of(nsio, struct nd_namespace_pmem, nsio);
125}
126
Dan Williams6ff3e912016-10-05 14:04:15 -0700127static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
Dan Williams1b40e092015-05-01 13:34:01 -0400128{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400129 return container_of(dev, struct nd_namespace_blk, common.dev);
130}
131
132/**
133 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
134 * @ndns: device to read
135 * @offset: namespace-relative starting offset
136 * @buf: buffer to fill
137 * @size: transfer length
138 *
139 * @buf is up-to-date upon return from this routine.
140 */
141static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
Vishal Verma3ae3d672017-05-10 15:01:30 -0600142 resource_size_t offset, void *buf, size_t size,
143 unsigned long flags)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400144{
Vishal Verma3ae3d672017-05-10 15:01:30 -0600145 return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400146}
147
148/**
149 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
Ira Weiny2f474142019-11-14 19:06:47 -0800150 * @ndns: device to write
Dan Williams8c2f7e82015-06-25 04:20:04 -0400151 * @offset: namespace-relative starting offset
152 * @buf: buffer to drain
153 * @size: transfer length
154 *
155 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
156 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
157 * or on backing memory media upon return from this routine. Flushing
158 * to media is handled internal to the @ndns driver, if at all.
159 */
160static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
Vishal Verma3ae3d672017-05-10 15:01:30 -0600161 resource_size_t offset, void *buf, size_t size,
162 unsigned long flags)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400163{
Vishal Verma3ae3d672017-05-10 15:01:30 -0600164 return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
Dan Williams1b40e092015-05-01 13:34:01 -0400165}
166
Dan Williams4d88a972015-05-31 14:41:48 -0400167#define MODULE_ALIAS_ND_DEVICE(type) \
168 MODULE_ALIAS("nd:t" __stringify(type) "*")
169#define ND_DEVICE_MODALIAS_FMT "nd:t%d"
170
Dan Williams71999462016-02-18 10:29:49 -0800171struct nd_region;
172void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -0400173int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
174 struct module *module, const char *mod_name);
Johannes Thumshirn71cfdd02018-03-14 19:25:06 +0100175static inline void nd_driver_unregister(struct nd_device_driver *drv)
176{
177 driver_unregister(&drv->drv);
178}
Dan Williams4d88a972015-05-31 14:41:48 -0400179#define nd_driver_register(driver) \
180 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
Johannes Thumshirn71cfdd02018-03-14 19:25:06 +0100181#define module_nd_driver(driver) \
182 module_driver(driver, nd_driver_register, nd_driver_unregister)
Dan Williams4d88a972015-05-31 14:41:48 -0400183#endif /* __LINUX_ND_H__ */