Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 1 | /* |
| 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 Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 15 | #include <linux/fs.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 16 | #include <linux/ndctl.h> |
| 17 | #include <linux/device.h> |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 18 | #include <linux/badblocks.h> |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 19 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 20 | enum nvdimm_event { |
| 21 | NVDIMM_REVALIDATE_POISON, |
| 22 | }; |
| 23 | |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 24 | enum nvdimm_claim_class { |
| 25 | NVDIMM_CCLASS_NONE, |
| 26 | NVDIMM_CCLASS_BTT, |
Vishal Verma | 14e4945 | 2017-06-28 14:25:00 -0600 | [diff] [blame] | 27 | NVDIMM_CCLASS_BTT2, |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 28 | NVDIMM_CCLASS_PFN, |
| 29 | NVDIMM_CCLASS_DAX, |
| 30 | NVDIMM_CCLASS_UNKNOWN, |
| 31 | }; |
| 32 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 33 | struct nd_device_driver { |
| 34 | struct device_driver drv; |
| 35 | unsigned long type; |
| 36 | int (*probe)(struct device *dev); |
| 37 | int (*remove)(struct device *dev); |
Dan Williams | 476f848 | 2016-07-09 00:12:52 -0700 | [diff] [blame] | 38 | void (*shutdown)(struct device *dev); |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 39 | void (*notify)(struct device *dev, enum nvdimm_event event); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static inline struct nd_device_driver *to_nd_device_driver( |
| 43 | struct device_driver *drv) |
| 44 | { |
| 45 | return container_of(drv, struct nd_device_driver, drv); |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 46 | }; |
| 47 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 48 | /** |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 49 | * struct nd_namespace_common - core infrastructure of a namespace |
| 50 | * @force_raw: ignore other personalities for the namespace (e.g. btt) |
| 51 | * @dev: device model node |
| 52 | * @claim: when set a another personality has taken ownership of the namespace |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 53 | * @claim_class: restrict claim type to a given class |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 54 | * @rw_bytes: access the raw namespace capacity with byte-aligned transfers |
| 55 | */ |
| 56 | struct nd_namespace_common { |
| 57 | int force_raw; |
| 58 | struct device dev; |
| 59 | struct device *claim; |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 60 | enum nvdimm_claim_class claim_class; |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 61 | int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 62 | void *buf, size_t size, int rw, unsigned long flags); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static inline struct nd_namespace_common *to_ndns(struct device *dev) |
| 66 | { |
| 67 | return container_of(dev, struct nd_namespace_common, dev); |
| 68 | } |
| 69 | |
| 70 | /** |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 71 | * struct nd_namespace_io - device representation of a persistent memory range |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 72 | * @dev: namespace device created by the nd region driver |
| 73 | * @res: struct resource conversion of a NFIT SPA table |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 74 | * @size: cached resource_size(@res) for fast path size checks |
| 75 | * @addr: virtual address to access the namespace range |
| 76 | * @bb: badblocks list for the namespace range |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 77 | */ |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 78 | struct nd_namespace_io { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 79 | struct nd_namespace_common common; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 80 | struct resource res; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 81 | resource_size_t size; |
Dan Williams | 7a9eb20 | 2016-06-03 18:06:47 -0700 | [diff] [blame] | 82 | void *addr; |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 83 | struct badblocks bb; |
Dan Williams | 3d88002 | 2015-05-31 15:02:11 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 86 | /** |
| 87 | * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory |
| 88 | * @nsio: device and system physical address range to drive |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 89 | * @lbasize: logical sector size for the namespace in block-device-mode |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 90 | * @alt_name: namespace name supplied in the dimm label |
| 91 | * @uuid: namespace name supplied in the dimm label |
Dan Williams | 0e3b0d12 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 92 | * @id: ida allocated id |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 93 | */ |
| 94 | struct nd_namespace_pmem { |
| 95 | struct nd_namespace_io nsio; |
Dan Williams | f979b13 | 2017-06-04 12:12:07 +0900 | [diff] [blame] | 96 | unsigned long lbasize; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 97 | char *alt_name; |
| 98 | u8 *uuid; |
Dan Williams | 0e3b0d12 | 2016-10-06 23:13:15 -0700 | [diff] [blame] | 99 | int id; |
Dan Williams | bf9bccc | 2015-06-17 17:14:46 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 102 | /** |
| 103 | * struct nd_namespace_blk - namespace for dimm-bounded persistent memory |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 104 | * @alt_name: namespace name supplied in the dimm label |
| 105 | * @uuid: namespace name supplied in the dimm label |
| 106 | * @id: ida allocated id |
| 107 | * @lbasize: blk namespaces have a native sector size when btt not present |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 108 | * @size: sum of all the resource ranges allocated to this namespace |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 109 | * @num_resources: number of dpa extents to claim |
| 110 | * @res: discontiguous dpa extents for given dimm |
| 111 | */ |
| 112 | struct nd_namespace_blk { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 113 | struct nd_namespace_common common; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 114 | char *alt_name; |
| 115 | u8 *uuid; |
| 116 | int id; |
| 117 | unsigned long lbasize; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 118 | resource_size_t size; |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 119 | int num_resources; |
| 120 | struct resource **res; |
| 121 | }; |
| 122 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 123 | 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] | 124 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 125 | return container_of(dev, struct nd_namespace_io, common.dev); |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 128 | 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] | 129 | { |
| 130 | struct nd_namespace_io *nsio = to_nd_namespace_io(dev); |
| 131 | |
| 132 | return container_of(nsio, struct nd_namespace_pmem, nsio); |
| 133 | } |
| 134 | |
Dan Williams | 6ff3e91 | 2016-10-05 14:04:15 -0700 | [diff] [blame] | 135 | 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] | 136 | { |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 137 | return container_of(dev, struct nd_namespace_blk, common.dev); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace |
| 142 | * @ndns: device to read |
| 143 | * @offset: namespace-relative starting offset |
| 144 | * @buf: buffer to fill |
| 145 | * @size: transfer length |
| 146 | * |
| 147 | * @buf is up-to-date upon return from this routine. |
| 148 | */ |
| 149 | static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 150 | resource_size_t offset, void *buf, size_t size, |
| 151 | unsigned long flags) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 152 | { |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 153 | return ndns->rw_bytes(ndns, offset, buf, size, READ, flags); |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace |
| 158 | * @ndns: device to read |
| 159 | * @offset: namespace-relative starting offset |
| 160 | * @buf: buffer to drain |
| 161 | * @size: transfer length |
| 162 | * |
| 163 | * NVDIMM Namepaces disks do not implement sectors internally. Depending on |
| 164 | * the @ndns, the contents of @buf may be in cpu cache, platform buffers, |
| 165 | * or on backing memory media upon return from this routine. Flushing |
| 166 | * to media is handled internal to the @ndns driver, if at all. |
| 167 | */ |
| 168 | static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 169 | resource_size_t offset, void *buf, size_t size, |
| 170 | unsigned long flags) |
Dan Williams | 8c2f7e8 | 2015-06-25 04:20:04 -0400 | [diff] [blame] | 171 | { |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 172 | return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags); |
Dan Williams | 1b40e09 | 2015-05-01 13:34:01 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Dan Williams | 4d88a97 | 2015-05-31 14:41:48 -0400 | [diff] [blame] | 175 | #define MODULE_ALIAS_ND_DEVICE(type) \ |
| 176 | MODULE_ALIAS("nd:t" __stringify(type) "*") |
| 177 | #define ND_DEVICE_MODALIAS_FMT "nd:t%d" |
| 178 | |
Dan Williams | 7199946 | 2016-02-18 10:29:49 -0800 | [diff] [blame] | 179 | struct nd_region; |
| 180 | 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] | 181 | int __must_check __nd_driver_register(struct nd_device_driver *nd_drv, |
| 182 | struct module *module, const char *mod_name); |
| 183 | #define nd_driver_register(driver) \ |
| 184 | __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME) |
| 185 | #endif /* __LINUX_ND_H__ */ |