blob: 5489ab756d1a5e58b4861411227fd1e3552fd93c [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>
18
Dan Williams71999462016-02-18 10:29:49 -080019enum nvdimm_event {
20 NVDIMM_REVALIDATE_POISON,
21};
22
Dan Williams4d88a972015-05-31 14:41:48 -040023struct nd_device_driver {
24 struct device_driver drv;
25 unsigned long type;
26 int (*probe)(struct device *dev);
27 int (*remove)(struct device *dev);
Dan Williams71999462016-02-18 10:29:49 -080028 void (*notify)(struct device *dev, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -040029};
30
31static inline struct nd_device_driver *to_nd_device_driver(
32 struct device_driver *drv)
33{
34 return container_of(drv, struct nd_device_driver, drv);
Dan Williams3d880022015-05-31 15:02:11 -040035};
36
Dan Williamsbf9bccc2015-06-17 17:14:46 -040037/**
Dan Williams8c2f7e82015-06-25 04:20:04 -040038 * struct nd_namespace_common - core infrastructure of a namespace
39 * @force_raw: ignore other personalities for the namespace (e.g. btt)
40 * @dev: device model node
41 * @claim: when set a another personality has taken ownership of the namespace
42 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
43 */
44struct nd_namespace_common {
45 int force_raw;
46 struct device dev;
47 struct device *claim;
48 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
49 void *buf, size_t size, int rw);
50};
51
52static inline struct nd_namespace_common *to_ndns(struct device *dev)
53{
54 return container_of(dev, struct nd_namespace_common, dev);
55}
56
57/**
Dan Williamsbf9bccc2015-06-17 17:14:46 -040058 * struct nd_namespace_io - infrastructure for loading an nd_pmem instance
59 * @dev: namespace device created by the nd region driver
60 * @res: struct resource conversion of a NFIT SPA table
61 */
Dan Williams3d880022015-05-31 15:02:11 -040062struct nd_namespace_io {
Dan Williams8c2f7e82015-06-25 04:20:04 -040063 struct nd_namespace_common common;
Dan Williams3d880022015-05-31 15:02:11 -040064 struct resource res;
65};
66
Dan Williamsbf9bccc2015-06-17 17:14:46 -040067/**
68 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
69 * @nsio: device and system physical address range to drive
70 * @alt_name: namespace name supplied in the dimm label
71 * @uuid: namespace name supplied in the dimm label
72 */
73struct nd_namespace_pmem {
74 struct nd_namespace_io nsio;
75 char *alt_name;
76 u8 *uuid;
77};
78
Dan Williams1b40e092015-05-01 13:34:01 -040079/**
80 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
Dan Williams1b40e092015-05-01 13:34:01 -040081 * @alt_name: namespace name supplied in the dimm label
82 * @uuid: namespace name supplied in the dimm label
83 * @id: ida allocated id
84 * @lbasize: blk namespaces have a native sector size when btt not present
85 * @num_resources: number of dpa extents to claim
86 * @res: discontiguous dpa extents for given dimm
87 */
88struct nd_namespace_blk {
Dan Williams8c2f7e82015-06-25 04:20:04 -040089 struct nd_namespace_common common;
Dan Williams1b40e092015-05-01 13:34:01 -040090 char *alt_name;
91 u8 *uuid;
92 int id;
93 unsigned long lbasize;
94 int num_resources;
95 struct resource **res;
96};
97
Dan Williams3d880022015-05-31 15:02:11 -040098static inline struct nd_namespace_io *to_nd_namespace_io(struct device *dev)
99{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400100 return container_of(dev, struct nd_namespace_io, common.dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400101}
102
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400103static inline struct nd_namespace_pmem *to_nd_namespace_pmem(struct device *dev)
104{
105 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
106
107 return container_of(nsio, struct nd_namespace_pmem, nsio);
108}
109
Dan Williams1b40e092015-05-01 13:34:01 -0400110static inline struct nd_namespace_blk *to_nd_namespace_blk(struct device *dev)
111{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400112 return container_of(dev, struct nd_namespace_blk, common.dev);
113}
114
115/**
116 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
117 * @ndns: device to read
118 * @offset: namespace-relative starting offset
119 * @buf: buffer to fill
120 * @size: transfer length
121 *
122 * @buf is up-to-date upon return from this routine.
123 */
124static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
125 resource_size_t offset, void *buf, size_t size)
126{
127 return ndns->rw_bytes(ndns, offset, buf, size, READ);
128}
129
130/**
131 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
132 * @ndns: device to read
133 * @offset: namespace-relative starting offset
134 * @buf: buffer to drain
135 * @size: transfer length
136 *
137 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
138 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
139 * or on backing memory media upon return from this routine. Flushing
140 * to media is handled internal to the @ndns driver, if at all.
141 */
142static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
143 resource_size_t offset, void *buf, size_t size)
144{
145 return ndns->rw_bytes(ndns, offset, buf, size, WRITE);
Dan Williams1b40e092015-05-01 13:34:01 -0400146}
147
Dan Williams4d88a972015-05-31 14:41:48 -0400148#define MODULE_ALIAS_ND_DEVICE(type) \
149 MODULE_ALIAS("nd:t" __stringify(type) "*")
150#define ND_DEVICE_MODALIAS_FMT "nd:t%d"
151
Dan Williams71999462016-02-18 10:29:49 -0800152struct nd_region;
153void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -0400154int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
155 struct module *module, const char *mod_name);
156#define nd_driver_register(driver) \
157 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
158#endif /* __LINUX_ND_H__ */