blob: 1ecd64643512215a233c79c8bc384e66e079dbbc [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 Williams4d88a972015-05-31 14:41:48 -040024struct nd_device_driver {
25 struct device_driver drv;
26 unsigned long type;
27 int (*probe)(struct device *dev);
28 int (*remove)(struct device *dev);
Dan Williams476f8482016-07-09 00:12:52 -070029 void (*shutdown)(struct device *dev);
Dan Williams71999462016-02-18 10:29:49 -080030 void (*notify)(struct device *dev, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -040031};
32
33static inline struct nd_device_driver *to_nd_device_driver(
34 struct device_driver *drv)
35{
36 return container_of(drv, struct nd_device_driver, drv);
Dan Williams3d880022015-05-31 15:02:11 -040037};
38
Dan Williamsbf9bccc2015-06-17 17:14:46 -040039/**
Dan Williams8c2f7e82015-06-25 04:20:04 -040040 * struct nd_namespace_common - core infrastructure of a namespace
41 * @force_raw: ignore other personalities for the namespace (e.g. btt)
42 * @dev: device model node
43 * @claim: when set a another personality has taken ownership of the namespace
44 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
45 */
46struct nd_namespace_common {
47 int force_raw;
48 struct device dev;
49 struct device *claim;
50 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
51 void *buf, size_t size, int rw);
52};
53
54static inline struct nd_namespace_common *to_ndns(struct device *dev)
55{
56 return container_of(dev, struct nd_namespace_common, dev);
57}
58
59/**
Dan Williams200c79d2016-03-22 00:22:16 -070060 * struct nd_namespace_io - device representation of a persistent memory range
Dan Williamsbf9bccc2015-06-17 17:14:46 -040061 * @dev: namespace device created by the nd region driver
62 * @res: struct resource conversion of a NFIT SPA table
Dan Williams200c79d2016-03-22 00:22:16 -070063 * @size: cached resource_size(@res) for fast path size checks
64 * @addr: virtual address to access the namespace range
65 * @bb: badblocks list for the namespace range
Dan Williamsbf9bccc2015-06-17 17:14:46 -040066 */
Dan Williams3d880022015-05-31 15:02:11 -040067struct nd_namespace_io {
Dan Williams8c2f7e82015-06-25 04:20:04 -040068 struct nd_namespace_common common;
Dan Williams3d880022015-05-31 15:02:11 -040069 struct resource res;
Dan Williams200c79d2016-03-22 00:22:16 -070070 resource_size_t size;
71 void __pmem *addr;
72 struct badblocks bb;
Dan Williams3d880022015-05-31 15:02:11 -040073};
74
Dan Williamsbf9bccc2015-06-17 17:14:46 -040075/**
76 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
77 * @nsio: device and system physical address range to drive
78 * @alt_name: namespace name supplied in the dimm label
79 * @uuid: namespace name supplied in the dimm label
80 */
81struct nd_namespace_pmem {
82 struct nd_namespace_io nsio;
83 char *alt_name;
84 u8 *uuid;
85};
86
Dan Williams1b40e092015-05-01 13:34:01 -040087/**
88 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
Dan Williams1b40e092015-05-01 13:34:01 -040089 * @alt_name: namespace name supplied in the dimm label
90 * @uuid: namespace name supplied in the dimm label
91 * @id: ida allocated id
92 * @lbasize: blk namespaces have a native sector size when btt not present
Dan Williams9d90725d2016-03-18 11:27:36 -070093 * @size: sum of all the resource ranges allocated to this namespace
Dan Williams1b40e092015-05-01 13:34:01 -040094 * @num_resources: number of dpa extents to claim
95 * @res: discontiguous dpa extents for given dimm
96 */
97struct nd_namespace_blk {
Dan Williams8c2f7e82015-06-25 04:20:04 -040098 struct nd_namespace_common common;
Dan Williams1b40e092015-05-01 13:34:01 -040099 char *alt_name;
100 u8 *uuid;
101 int id;
102 unsigned long lbasize;
Dan Williams9d90725d2016-03-18 11:27:36 -0700103 resource_size_t size;
Dan Williams1b40e092015-05-01 13:34:01 -0400104 int num_resources;
105 struct resource **res;
106};
107
Dan Williams3d880022015-05-31 15:02:11 -0400108static inline struct nd_namespace_io *to_nd_namespace_io(struct device *dev)
109{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400110 return container_of(dev, struct nd_namespace_io, common.dev);
Dan Williams4d88a972015-05-31 14:41:48 -0400111}
112
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400113static inline struct nd_namespace_pmem *to_nd_namespace_pmem(struct device *dev)
114{
115 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
116
117 return container_of(nsio, struct nd_namespace_pmem, nsio);
118}
119
Dan Williams1b40e092015-05-01 13:34:01 -0400120static inline struct nd_namespace_blk *to_nd_namespace_blk(struct device *dev)
121{
Dan Williams8c2f7e82015-06-25 04:20:04 -0400122 return container_of(dev, struct nd_namespace_blk, common.dev);
123}
124
125/**
126 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
127 * @ndns: device to read
128 * @offset: namespace-relative starting offset
129 * @buf: buffer to fill
130 * @size: transfer length
131 *
132 * @buf is up-to-date upon return from this routine.
133 */
134static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
135 resource_size_t offset, void *buf, size_t size)
136{
137 return ndns->rw_bytes(ndns, offset, buf, size, READ);
138}
139
140/**
141 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
142 * @ndns: device to read
143 * @offset: namespace-relative starting offset
144 * @buf: buffer to drain
145 * @size: transfer length
146 *
147 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
148 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
149 * or on backing memory media upon return from this routine. Flushing
150 * to media is handled internal to the @ndns driver, if at all.
151 */
152static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
153 resource_size_t offset, void *buf, size_t size)
154{
155 return ndns->rw_bytes(ndns, offset, buf, size, WRITE);
Dan Williams1b40e092015-05-01 13:34:01 -0400156}
157
Dan Williams4d88a972015-05-31 14:41:48 -0400158#define MODULE_ALIAS_ND_DEVICE(type) \
159 MODULE_ALIAS("nd:t" __stringify(type) "*")
160#define ND_DEVICE_MODALIAS_FMT "nd:t%d"
161
Dan Williams71999462016-02-18 10:29:49 -0800162struct nd_region;
163void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
Dan Williams4d88a972015-05-31 14:41:48 -0400164int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
165 struct module *module, const char *mod_name);
166#define nd_driver_register(driver) \
167 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
168#endif /* __LINUX_ND_H__ */