blob: 96069c54389027ec3cf1cd4a677ae480e32b916c [file] [log] [blame]
Dan Williams4d88a972015-05-31 14:41:48 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
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.
7 *
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.
12 */
13#ifndef __LINUX_ND_H__
14#define __LINUX_ND_H__
Dan Williams8c2f7e82015-06-25 04:20:04 -040015#include <linux/fs.h>
Dan Williams4d88a972015-05-31 14:41:48 -040016#include <linux/ndctl.h>
17#include <linux/device.h>
Dan Williams200c79d2016-03-22 00:22:16 -070018#include <linux/badblocks.h>
Dan Williams4d88a972015-05-31 14:41:48 -040019
Dan Williams71999462016-02-18 10:29:49 -080020enum nvdimm_event {
21 NVDIMM_REVALIDATE_POISON,
22};
23
Dan Williamsb3fde742017-06-04 10:18:39 +090024enum nvdimm_claim_class {
25 NVDIMM_CCLASS_NONE,
26 NVDIMM_CCLASS_BTT,
27 NVDIMM_CCLASS_PFN,
28 NVDIMM_CCLASS_DAX,
29 NVDIMM_CCLASS_UNKNOWN,
30};
31
Dan Williams4d88a972015-05-31 14:41:48 -040032struct nd_device_driver {
33 struct device_driver drv;
34 unsigned long type;
35 int (*probe)(struct device *dev);
36 int (*remove)(struct device *dev);
Dan Williams476f8482016-07-09 00:12:52 -070037 void (*shutdown)(struct device *dev);
Dan Williams71999462016-02-18 10:29:49 -080038 void (*notify)(struct device *dev, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -040039};
40
41static inline struct nd_device_driver *to_nd_device_driver(
42 struct device_driver *drv)
43{
44 return container_of(drv, struct nd_device_driver, drv);
Dan Williams3d880022015-05-31 15:02:11 -040045};
46
Dan Williamsbf9bccc2015-06-17 17:14:46 -040047/**
Dan Williams8c2f7e82015-06-25 04:20:04 -040048 * struct nd_namespace_common - core infrastructure of a namespace
49 * @force_raw: ignore other personalities for the namespace (e.g. btt)
50 * @dev: device model node
51 * @claim: when set a another personality has taken ownership of the namespace
Dan Williamsb3fde742017-06-04 10:18:39 +090052 * @claim_class: restrict claim type to a given class
Dan Williams8c2f7e82015-06-25 04:20:04 -040053 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
54 */
55struct nd_namespace_common {
56 int force_raw;
57 struct device dev;
58 struct device *claim;
Dan Williamsb3fde742017-06-04 10:18:39 +090059 enum nvdimm_claim_class claim_class;
Dan Williams8c2f7e82015-06-25 04:20:04 -040060 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
Vishal Verma3ae3d672017-05-10 15:01:30 -060061 void *buf, size_t size, int rw, unsigned long flags);
Dan Williams8c2f7e82015-06-25 04:20:04 -040062};
63
64static inline struct nd_namespace_common *to_ndns(struct device *dev)
65{
66 return container_of(dev, struct nd_namespace_common, dev);
67}
68
69/**
Dan Williams200c79d2016-03-22 00:22:16 -070070 * struct nd_namespace_io - device representation of a persistent memory range
Dan Williamsbf9bccc2015-06-17 17:14:46 -040071 * @dev: namespace device created by the nd region driver
72 * @res: struct resource conversion of a NFIT SPA table
Dan Williams200c79d2016-03-22 00:22:16 -070073 * @size: cached resource_size(@res) for fast path size checks
74 * @addr: virtual address to access the namespace range
75 * @bb: badblocks list for the namespace range
Dan Williamsbf9bccc2015-06-17 17:14:46 -040076 */
Dan Williams3d880022015-05-31 15:02:11 -040077struct nd_namespace_io {
Dan Williams8c2f7e82015-06-25 04:20:04 -040078 struct nd_namespace_common common;
Dan Williams3d880022015-05-31 15:02:11 -040079 struct resource res;
Dan Williams200c79d2016-03-22 00:22:16 -070080 resource_size_t size;
Dan Williams7a9eb202016-06-03 18:06:47 -070081 void *addr;
Dan Williams200c79d2016-03-22 00:22:16 -070082 struct badblocks bb;
Dan Williams3d880022015-05-31 15:02:11 -040083};
84
Dan Williamsbf9bccc2015-06-17 17:14:46 -040085/**
86 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
87 * @nsio: device and system physical address range to drive
Dan Williamsf979b132017-06-04 12:12:07 +090088 * @lbasize: logical sector size for the namespace in block-device-mode
Dan Williamsbf9bccc2015-06-17 17:14:46 -040089 * @alt_name: namespace name supplied in the dimm label
90 * @uuid: namespace name supplied in the dimm label
Dan Williams0e3b0d12016-10-06 23:13:15 -070091 * @id: ida allocated id
Dan Williamsbf9bccc2015-06-17 17:14:46 -040092 */
93struct nd_namespace_pmem {
94 struct nd_namespace_io nsio;
Dan Williamsf979b132017-06-04 12:12:07 +090095 unsigned long lbasize;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040096 char *alt_name;
97 u8 *uuid;
Dan Williams0e3b0d12016-10-06 23:13:15 -070098 int id;
Dan Williamsbf9bccc2015-06-17 17:14:46 -040099};
100
Dan Williams1b40e092015-05-01 13:34:01 -0400101/**
102 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
Dan Williams1b40e092015-05-01 13:34:01 -0400103 * @alt_name: namespace name supplied in the dimm label
104 * @uuid: namespace name supplied in the dimm label
105 * @id: ida allocated id
106 * @lbasize: blk namespaces have a native sector size when btt not present
Dan Williams9d90725d2016-03-18 11:27:36 -0700107 * @size: sum of all the resource ranges allocated to this namespace
Dan Williams1b40e092015-05-01 13:34:01 -0400108 * @num_resources: number of dpa extents to claim
109 * @res: discontiguous dpa extents for given dimm
110 */
111struct nd_namespace_blk {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400112 struct nd_namespace_common common;
Dan Williams1b40e092015-05-01 13:34:01 -0400113 char *alt_name;
114 u8 *uuid;
115 int id;
116 unsigned long lbasize;
Dan Williams9d90725d2016-03-18 11:27:36 -0700117 resource_size_t size;
Dan Williams1b40e092015-05-01 13:34:01 -0400118 int num_resources;
119 struct resource **res;
120};
121
Dan Williams6ff3e912016-10-05 14:04:15 -0700122static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
Dan Williams3d880022015-05-31 15:02:11 -0400123{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400124 return container_of(dev, struct nd_namespace_io, common.dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400125}
126
Dan Williams6ff3e912016-10-05 14:04:15 -0700127static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400128{
129 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
130
131 return container_of(nsio, struct nd_namespace_pmem, nsio);
132}
133
Dan Williams6ff3e912016-10-05 14:04:15 -0700134static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
Dan Williams1b40e092015-05-01 13:34:01 -0400135{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400136 return container_of(dev, struct nd_namespace_blk, common.dev);
137}
138
139/**
140 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
141 * @ndns: device to read
142 * @offset: namespace-relative starting offset
143 * @buf: buffer to fill
144 * @size: transfer length
145 *
146 * @buf is up-to-date upon return from this routine.
147 */
148static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
Vishal Verma3ae3d672017-05-10 15:01:30 -0600149 resource_size_t offset, void *buf, size_t size,
150 unsigned long flags)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400151{
Vishal Verma3ae3d672017-05-10 15:01:30 -0600152 return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400153}
154
155/**
156 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
157 * @ndns: device to read
158 * @offset: namespace-relative starting offset
159 * @buf: buffer to drain
160 * @size: transfer length
161 *
162 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
163 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
164 * or on backing memory media upon return from this routine. Flushing
165 * to media is handled internal to the @ndns driver, if at all.
166 */
167static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
Vishal Verma3ae3d672017-05-10 15:01:30 -0600168 resource_size_t offset, void *buf, size_t size,
169 unsigned long flags)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400170{
Vishal Verma3ae3d672017-05-10 15:01:30 -0600171 return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
Dan Williams1b40e092015-05-01 13:34:01 -0400172}
173
Dan Williams4d88a972015-05-31 14:41:48 -0400174#define MODULE_ALIAS_ND_DEVICE(type) \
175 MODULE_ALIAS("nd:t" __stringify(type) "*")
176#define ND_DEVICE_MODALIAS_FMT "nd:t%d"
177
Dan Williams71999462016-02-18 10:29:49 -0800178struct nd_region;
179void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -0400180int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
181 struct module *module, const char *mod_name);
182#define nd_driver_register(driver) \
183 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
184#endif /* __LINUX_ND_H__ */