Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 4 | */ |
| 5 | #ifndef __LINUX_ND_H__ |
| 6 | #define __LINUX_ND_H__ |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 7 | #include <linux/fs.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 8 | #include <linux/ndctl.h> |
| 9 | #include <linux/device.h> |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 10 | #include <linux/badblocks.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 11 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 12 | enum nvdimm_event { |
| 13 | NVDIMM_REVALIDATE_POISON, |
| 14 | }; |
| 15 | |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 16 | enum nvdimm_claim_class { |
| 17 | NVDIMM_CCLASS_NONE, |
| 18 | NVDIMM_CCLASS_BTT, |
Vishal Verma | 14e4945 | 2017-06-28 14:25:00 -0600 | [diff] [blame] | 19 | NVDIMM_CCLASS_BTT2, |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 20 | NVDIMM_CCLASS_PFN, |
| 21 | NVDIMM_CCLASS_DAX, |
| 22 | NVDIMM_CCLASS_UNKNOWN, |
| 23 | }; |
| 24 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 25 | struct 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 Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 30 | void (*shutdown)(struct device *dev); |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 31 | void (*notify)(struct device *dev, enum nvdimm_event event); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static 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 Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 38 | }; |
| 39 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 40 | /** |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 41 | * 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 Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 45 | * @claim_class: restrict claim type to a given class |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 46 | * @rw_bytes: access the raw namespace capacity with byte-aligned transfers |
| 47 | */ |
| 48 | struct nd_namespace_common { |
| 49 | int force_raw; |
| 50 | struct device dev; |
| 51 | struct device *claim; |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 52 | enum nvdimm_claim_class claim_class; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 53 | int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 54 | void *buf, size_t size, int rw, unsigned long flags); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static 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 Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 63 | * struct nd_namespace_io - device representation of a persistent memory range |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 64 | * @dev: namespace device created by the nd region driver |
| 65 | * @res: struct resource conversion of a NFIT SPA table |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 66 | * @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 Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 69 | */ |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 70 | struct nd_namespace_io { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 71 | struct nd_namespace_common common; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 72 | struct resource res; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 73 | resource_size_t size; |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 74 | void *addr; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 75 | struct badblocks bb; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 78 | /** |
| 79 | * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory |
| 80 | * @nsio: device and system physical address range to drive |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 81 | * @lbasize: logical sector size for the namespace in block-device-mode |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 82 | * @alt_name: namespace name supplied in the dimm label |
| 83 | * @uuid: namespace name supplied in the dimm label |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 84 | * @id: ida allocated id |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 85 | */ |
| 86 | struct nd_namespace_pmem { |
| 87 | struct nd_namespace_io nsio; |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 88 | unsigned long lbasize; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 89 | char *alt_name; |
| 90 | u8 *uuid; |
Dan Williams | 0e3b0d1 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 91 | int id; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 92 | }; |
| 93 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 94 | /** |
| 95 | * struct nd_namespace_blk - namespace for dimm-bounded persistent memory |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 96 | * @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 Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 100 | * @size: sum of all the resource ranges allocated to this namespace |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 101 | * @num_resources: number of dpa extents to claim |
| 102 | * @res: discontiguous dpa extents for given dimm |
| 103 | */ |
| 104 | struct nd_namespace_blk { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 105 | struct nd_namespace_common common; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 106 | char *alt_name; |
| 107 | u8 *uuid; |
| 108 | int id; |
| 109 | unsigned long lbasize; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 110 | resource_size_t size; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 111 | int num_resources; |
| 112 | struct resource **res; |
| 113 | }; |
| 114 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 115 | static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev) |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 116 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 117 | return container_of(dev, struct nd_namespace_io, common.dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 120 | static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev) |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 121 | { |
| 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 Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 127 | static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev) |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 128 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 129 | 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 | */ |
| 141 | static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 142 | resource_size_t offset, void *buf, size_t size, |
| 143 | unsigned long flags) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 144 | { |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 145 | return ndns->rw_bytes(ndns, offset, buf, size, READ, flags); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace |
Ira Weiny | 2f47414 | 2019-11-14 19:06:47 -0800 | [diff] [blame] | 150 | * @ndns: device to write |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 151 | * @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 | */ |
| 160 | static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 161 | resource_size_t offset, void *buf, size_t size, |
| 162 | unsigned long flags) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 163 | { |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 164 | return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 167 | #define MODULE_ALIAS_ND_DEVICE(type) \ |
| 168 | MODULE_ALIAS("nd:t" __stringify(type) "*") |
| 169 | #define ND_DEVICE_MODALIAS_FMT "nd:t%d" |
| 170 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 171 | struct nd_region; |
| 172 | void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 173 | int __must_check __nd_driver_register(struct nd_device_driver *nd_drv, |
| 174 | struct module *module, const char *mod_name); |
Johannes Thumshirn | 71cfdd0 | 2018-03-14 19:25:06 +0100 | [diff] [blame] | 175 | static inline void nd_driver_unregister(struct nd_device_driver *drv) |
| 176 | { |
| 177 | driver_unregister(&drv->drv); |
| 178 | } |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 179 | #define nd_driver_register(driver) \ |
| 180 | __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME) |
Johannes Thumshirn | 71cfdd0 | 2018-03-14 19:25:06 +0100 | [diff] [blame] | 181 | #define module_nd_driver(driver) \ |
| 182 | module_driver(driver, nd_driver_register, nd_driver_unregister) |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 183 | #endif /* __LINUX_ND_H__ */ |