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