blob: 5ea4aec7fd635de64320ceccb8776fd60ab154d8 [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
Dan Williams9d90725d2016-03-18 11:27:36 -070085 * @size: sum of all the resource ranges allocated to this namespace
Dan Williams1b40e092015-05-01 13:34:01 -040086 * @num_resources: number of dpa extents to claim
87 * @res: discontiguous dpa extents for given dimm
88 */
89struct nd_namespace_blk {
Dan Williams8c2f7e82015-06-25 04:20:04 -040090 struct nd_namespace_common common;
Dan Williams1b40e092015-05-01 13:34:01 -040091 char *alt_name;
92 u8 *uuid;
93 int id;
94 unsigned long lbasize;
Dan Williams9d90725d2016-03-18 11:27:36 -070095 resource_size_t size;
Dan Williams1b40e092015-05-01 13:34:01 -040096 int num_resources;
97 struct resource **res;
98};
99
Dan Williams3d880022015-05-31 15:02:11 -0400100static inline struct nd_namespace_io *to_nd_namespace_io(struct device *dev)
101{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400102 return container_of(dev, struct nd_namespace_io, common.dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400103}
104
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400105static inline struct nd_namespace_pmem *to_nd_namespace_pmem(struct device *dev)
106{
107 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
108
109 return container_of(nsio, struct nd_namespace_pmem, nsio);
110}
111
Dan Williams1b40e092015-05-01 13:34:01 -0400112static inline struct nd_namespace_blk *to_nd_namespace_blk(struct device *dev)
113{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400114 return container_of(dev, struct nd_namespace_blk, common.dev);
115}
116
117/**
118 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
119 * @ndns: device to read
120 * @offset: namespace-relative starting offset
121 * @buf: buffer to fill
122 * @size: transfer length
123 *
124 * @buf is up-to-date upon return from this routine.
125 */
126static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
127 resource_size_t offset, void *buf, size_t size)
128{
129 return ndns->rw_bytes(ndns, offset, buf, size, READ);
130}
131
132/**
133 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
134 * @ndns: device to read
135 * @offset: namespace-relative starting offset
136 * @buf: buffer to drain
137 * @size: transfer length
138 *
139 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
140 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
141 * or on backing memory media upon return from this routine. Flushing
142 * to media is handled internal to the @ndns driver, if at all.
143 */
144static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
145 resource_size_t offset, void *buf, size_t size)
146{
147 return ndns->rw_bytes(ndns, offset, buf, size, WRITE);
Dan Williams1b40e092015-05-01 13:34:01 -0400148}
149
Dan Williams4d88a972015-05-31 14:41:48 -0400150#define MODULE_ALIAS_ND_DEVICE(type) \
151 MODULE_ALIAS("nd:t" __stringify(type) "*")
152#define ND_DEVICE_MODALIAS_FMT "nd:t%d"
153
Dan Williams71999462016-02-18 10:29:49 -0800154struct nd_region;
155void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -0400156int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
157 struct module *module, const char *mod_name);
158#define nd_driver_register(driver) \
159 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
160#endif /* __LINUX_ND_H__ */