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