Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 1 | /* |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 2 | * Copyright(c) 2013-2016 Intel Corporation. All rights reserved. |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 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 | */ |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 13 | #include <linux/memremap.h> |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 14 | #include <linux/blkdev.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/genhd.h> |
| 17 | #include <linux/sizes.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include "nd-core.h" |
| 22 | #include "pfn.h" |
| 23 | #include "nd.h" |
| 24 | |
| 25 | static void nd_pfn_release(struct device *dev) |
| 26 | { |
| 27 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 28 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 29 | |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 30 | dev_dbg(dev, "trace\n"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 31 | nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns); |
| 32 | ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id); |
| 33 | kfree(nd_pfn->uuid); |
| 34 | kfree(nd_pfn); |
| 35 | } |
| 36 | |
| 37 | static struct device_type nd_pfn_device_type = { |
| 38 | .name = "nd_pfn", |
| 39 | .release = nd_pfn_release, |
| 40 | }; |
| 41 | |
| 42 | bool is_nd_pfn(struct device *dev) |
| 43 | { |
| 44 | return dev ? dev->type == &nd_pfn_device_type : false; |
| 45 | } |
| 46 | EXPORT_SYMBOL(is_nd_pfn); |
| 47 | |
| 48 | struct nd_pfn *to_nd_pfn(struct device *dev) |
| 49 | { |
| 50 | struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev); |
| 51 | |
| 52 | WARN_ON(!is_nd_pfn(dev)); |
| 53 | return nd_pfn; |
| 54 | } |
| 55 | EXPORT_SYMBOL(to_nd_pfn); |
| 56 | |
| 57 | static ssize_t mode_show(struct device *dev, |
| 58 | struct device_attribute *attr, char *buf) |
| 59 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 60 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 61 | |
| 62 | switch (nd_pfn->mode) { |
| 63 | case PFN_MODE_RAM: |
| 64 | return sprintf(buf, "ram\n"); |
| 65 | case PFN_MODE_PMEM: |
| 66 | return sprintf(buf, "pmem\n"); |
| 67 | default: |
| 68 | return sprintf(buf, "none\n"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static ssize_t mode_store(struct device *dev, |
| 73 | struct device_attribute *attr, const char *buf, size_t len) |
| 74 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 75 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 76 | ssize_t rc = 0; |
| 77 | |
| 78 | device_lock(dev); |
| 79 | nvdimm_bus_lock(dev); |
| 80 | if (dev->driver) |
| 81 | rc = -EBUSY; |
| 82 | else { |
| 83 | size_t n = len - 1; |
| 84 | |
| 85 | if (strncmp(buf, "pmem\n", n) == 0 |
| 86 | || strncmp(buf, "pmem", n) == 0) { |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 87 | nd_pfn->mode = PFN_MODE_PMEM; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 88 | } else if (strncmp(buf, "ram\n", n) == 0 |
| 89 | || strncmp(buf, "ram", n) == 0) |
| 90 | nd_pfn->mode = PFN_MODE_RAM; |
| 91 | else if (strncmp(buf, "none\n", n) == 0 |
| 92 | || strncmp(buf, "none", n) == 0) |
| 93 | nd_pfn->mode = PFN_MODE_NONE; |
| 94 | else |
| 95 | rc = -EINVAL; |
| 96 | } |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 97 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 98 | buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 99 | nvdimm_bus_unlock(dev); |
| 100 | device_unlock(dev); |
| 101 | |
| 102 | return rc ? rc : len; |
| 103 | } |
| 104 | static DEVICE_ATTR_RW(mode); |
| 105 | |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 106 | static ssize_t align_show(struct device *dev, |
| 107 | struct device_attribute *attr, char *buf) |
| 108 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 109 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 110 | |
Dan Williams | af7d9f0 | 2016-12-10 08:12:05 -0800 | [diff] [blame] | 111 | return sprintf(buf, "%ld\n", nd_pfn->align); |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Oliver O'Halloran | 1fdadbe | 2017-06-27 19:56:12 +1000 | [diff] [blame] | 114 | static const unsigned long *nd_pfn_supported_alignments(void) |
| 115 | { |
| 116 | /* |
| 117 | * This needs to be a non-static variable because the *_SIZE |
| 118 | * macros aren't always constants. |
| 119 | */ |
| 120 | const unsigned long supported_alignments[] = { |
| 121 | PAGE_SIZE, |
| 122 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 123 | HPAGE_PMD_SIZE, |
| 124 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 125 | HPAGE_PUD_SIZE, |
| 126 | #endif |
| 127 | #endif |
| 128 | 0, |
| 129 | }; |
| 130 | static unsigned long data[ARRAY_SIZE(supported_alignments)]; |
| 131 | |
| 132 | memcpy(data, supported_alignments, sizeof(data)); |
| 133 | |
| 134 | return data; |
| 135 | } |
| 136 | |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 137 | static ssize_t align_store(struct device *dev, |
| 138 | struct device_attribute *attr, const char *buf, size_t len) |
| 139 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 140 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 141 | ssize_t rc; |
| 142 | |
| 143 | device_lock(dev); |
| 144 | nvdimm_bus_lock(dev); |
Dan Williams | f13d2b6 | 2017-08-11 17:54:48 -0700 | [diff] [blame] | 145 | rc = nd_size_select_store(dev, buf, &nd_pfn->align, |
| 146 | nd_pfn_supported_alignments()); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 147 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 148 | buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 149 | nvdimm_bus_unlock(dev); |
| 150 | device_unlock(dev); |
| 151 | |
| 152 | return rc ? rc : len; |
| 153 | } |
| 154 | static DEVICE_ATTR_RW(align); |
| 155 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 156 | static ssize_t uuid_show(struct device *dev, |
| 157 | struct device_attribute *attr, char *buf) |
| 158 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 159 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 160 | |
| 161 | if (nd_pfn->uuid) |
| 162 | return sprintf(buf, "%pUb\n", nd_pfn->uuid); |
| 163 | return sprintf(buf, "\n"); |
| 164 | } |
| 165 | |
| 166 | static ssize_t uuid_store(struct device *dev, |
| 167 | struct device_attribute *attr, const char *buf, size_t len) |
| 168 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 169 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 170 | ssize_t rc; |
| 171 | |
| 172 | device_lock(dev); |
| 173 | rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 174 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 175 | buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 176 | device_unlock(dev); |
| 177 | |
| 178 | return rc ? rc : len; |
| 179 | } |
| 180 | static DEVICE_ATTR_RW(uuid); |
| 181 | |
| 182 | static ssize_t namespace_show(struct device *dev, |
| 183 | struct device_attribute *attr, char *buf) |
| 184 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 185 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 186 | ssize_t rc; |
| 187 | |
| 188 | nvdimm_bus_lock(dev); |
| 189 | rc = sprintf(buf, "%s\n", nd_pfn->ndns |
| 190 | ? dev_name(&nd_pfn->ndns->dev) : ""); |
| 191 | nvdimm_bus_unlock(dev); |
| 192 | return rc; |
| 193 | } |
| 194 | |
| 195 | static ssize_t namespace_store(struct device *dev, |
| 196 | struct device_attribute *attr, const char *buf, size_t len) |
| 197 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 198 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 199 | ssize_t rc; |
| 200 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 201 | device_lock(dev); |
Axel Lin | 4ca8b57a | 2015-09-16 21:25:38 +0800 | [diff] [blame] | 202 | nvdimm_bus_lock(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 203 | rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 204 | dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf, |
| 205 | buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 206 | nvdimm_bus_unlock(dev); |
Axel Lin | 4ca8b57a | 2015-09-16 21:25:38 +0800 | [diff] [blame] | 207 | device_unlock(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 208 | |
| 209 | return rc; |
| 210 | } |
| 211 | static DEVICE_ATTR_RW(namespace); |
| 212 | |
Dan Williams | f6ed58c | 2016-03-03 09:46:04 -0800 | [diff] [blame] | 213 | static ssize_t resource_show(struct device *dev, |
| 214 | struct device_attribute *attr, char *buf) |
| 215 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 216 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | f6ed58c | 2016-03-03 09:46:04 -0800 | [diff] [blame] | 217 | ssize_t rc; |
| 218 | |
| 219 | device_lock(dev); |
| 220 | if (dev->driver) { |
| 221 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 222 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); |
| 223 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 224 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); |
| 225 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 226 | |
| 227 | rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start |
| 228 | + start_pad + offset); |
| 229 | } else { |
| 230 | /* no address to convey if the pfn instance is disabled */ |
| 231 | rc = -ENXIO; |
| 232 | } |
| 233 | device_unlock(dev); |
| 234 | |
| 235 | return rc; |
| 236 | } |
| 237 | static DEVICE_ATTR_RO(resource); |
| 238 | |
| 239 | static ssize_t size_show(struct device *dev, |
| 240 | struct device_attribute *attr, char *buf) |
| 241 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 242 | struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev); |
Dan Williams | f6ed58c | 2016-03-03 09:46:04 -0800 | [diff] [blame] | 243 | ssize_t rc; |
| 244 | |
| 245 | device_lock(dev); |
| 246 | if (dev->driver) { |
| 247 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 248 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); |
| 249 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 250 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); |
| 251 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); |
| 252 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 253 | |
| 254 | rc = sprintf(buf, "%llu\n", (unsigned long long) |
| 255 | resource_size(&nsio->res) - start_pad |
| 256 | - end_trunc - offset); |
| 257 | } else { |
| 258 | /* no size to convey if the pfn instance is disabled */ |
| 259 | rc = -ENXIO; |
| 260 | } |
| 261 | device_unlock(dev); |
| 262 | |
| 263 | return rc; |
| 264 | } |
| 265 | static DEVICE_ATTR_RO(size); |
| 266 | |
Oliver O'Halloran | 1fdadbe | 2017-06-27 19:56:12 +1000 | [diff] [blame] | 267 | static ssize_t supported_alignments_show(struct device *dev, |
| 268 | struct device_attribute *attr, char *buf) |
| 269 | { |
| 270 | return nd_size_select_show(0, nd_pfn_supported_alignments(), buf); |
| 271 | } |
| 272 | static DEVICE_ATTR_RO(supported_alignments); |
| 273 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 274 | static struct attribute *nd_pfn_attributes[] = { |
| 275 | &dev_attr_mode.attr, |
| 276 | &dev_attr_namespace.attr, |
| 277 | &dev_attr_uuid.attr, |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 278 | &dev_attr_align.attr, |
Dan Williams | f6ed58c | 2016-03-03 09:46:04 -0800 | [diff] [blame] | 279 | &dev_attr_resource.attr, |
| 280 | &dev_attr_size.attr, |
Oliver O'Halloran | 1fdadbe | 2017-06-27 19:56:12 +1000 | [diff] [blame] | 281 | &dev_attr_supported_alignments.attr, |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 282 | NULL, |
| 283 | }; |
| 284 | |
Dan Williams | 26417ae | 2017-09-26 13:07:06 -0700 | [diff] [blame] | 285 | static umode_t pfn_visible(struct kobject *kobj, struct attribute *a, int n) |
| 286 | { |
| 287 | if (a == &dev_attr_resource.attr) |
| 288 | return 0400; |
| 289 | return a->mode; |
| 290 | } |
| 291 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 292 | struct attribute_group nd_pfn_attribute_group = { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 293 | .attrs = nd_pfn_attributes, |
Dan Williams | 26417ae | 2017-09-26 13:07:06 -0700 | [diff] [blame] | 294 | .is_visible = pfn_visible, |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | static const struct attribute_group *nd_pfn_attribute_groups[] = { |
| 298 | &nd_pfn_attribute_group, |
| 299 | &nd_device_attribute_group, |
| 300 | &nd_numa_attribute_group, |
| 301 | NULL, |
| 302 | }; |
| 303 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 304 | struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn, |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 305 | struct nd_namespace_common *ndns) |
| 306 | { |
Colin Ian King | 0cbfeef | 2018-02-05 14:08:52 +0000 | [diff] [blame] | 307 | struct device *dev; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 308 | |
| 309 | if (!nd_pfn) |
| 310 | return NULL; |
| 311 | |
| 312 | nd_pfn->mode = PFN_MODE_NONE; |
Oliver O'Halloran | 0dd6964 | 2017-06-27 19:56:33 +1000 | [diff] [blame] | 313 | nd_pfn->align = PFN_DEFAULT_ALIGNMENT; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 314 | dev = &nd_pfn->dev; |
| 315 | device_initialize(&nd_pfn->dev); |
| 316 | if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) { |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 317 | dev_dbg(&ndns->dev, "failed, already claimed by %s\n", |
| 318 | dev_name(ndns->claim)); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 319 | put_device(dev); |
| 320 | return NULL; |
| 321 | } |
| 322 | return dev; |
| 323 | } |
| 324 | |
| 325 | static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region) |
| 326 | { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 327 | struct nd_pfn *nd_pfn; |
| 328 | struct device *dev; |
| 329 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 330 | nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL); |
| 331 | if (!nd_pfn) |
| 332 | return NULL; |
| 333 | |
| 334 | nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL); |
| 335 | if (nd_pfn->id < 0) { |
| 336 | kfree(nd_pfn); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 340 | dev = &nd_pfn->dev; |
| 341 | dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 342 | dev->groups = nd_pfn_attribute_groups; |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 343 | dev->type = &nd_pfn_device_type; |
| 344 | dev->parent = &nd_region->dev; |
| 345 | |
| 346 | return nd_pfn; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | struct device *nd_pfn_create(struct nd_region *nd_region) |
| 350 | { |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 351 | struct nd_pfn *nd_pfn; |
| 352 | struct device *dev; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 353 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 354 | if (!is_memory(&nd_region->dev)) |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 355 | return NULL; |
| 356 | |
| 357 | nd_pfn = nd_pfn_alloc(nd_region); |
| 358 | dev = nd_pfn_devinit(nd_pfn, NULL); |
| 359 | |
| 360 | __nd_device_register(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 361 | return dev; |
| 362 | } |
| 363 | |
Vishal Verma | 48af2f7 | 2018-09-18 17:48:31 -0600 | [diff] [blame] | 364 | /* |
| 365 | * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap |
| 366 | * space associated with the namespace. If the memmap is set to DRAM, then |
| 367 | * this is a no-op. Since the memmap area is freshly initialized during |
| 368 | * probe, we have an opportunity to clear any badblocks in this area. |
| 369 | */ |
| 370 | static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn) |
| 371 | { |
| 372 | struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent); |
| 373 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 374 | void *zero_page = page_address(ZERO_PAGE(0)); |
| 375 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 376 | int num_bad, meta_num, rc, bb_present; |
| 377 | sector_t first_bad, meta_start; |
| 378 | struct nd_namespace_io *nsio; |
| 379 | |
| 380 | if (nd_pfn->mode != PFN_MODE_PMEM) |
| 381 | return 0; |
| 382 | |
| 383 | nsio = to_nd_namespace_io(&ndns->dev); |
| 384 | meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9; |
| 385 | meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start; |
| 386 | |
| 387 | do { |
| 388 | unsigned long zero_len; |
| 389 | u64 nsoff; |
| 390 | |
| 391 | bb_present = badblocks_check(&nd_region->bb, meta_start, |
| 392 | meta_num, &first_bad, &num_bad); |
| 393 | if (bb_present) { |
| 394 | dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %lx\n", |
| 395 | num_bad, first_bad); |
| 396 | nsoff = ALIGN_DOWN((nd_region->ndr_start |
| 397 | + (first_bad << 9)) - nsio->res.start, |
| 398 | PAGE_SIZE); |
| 399 | zero_len = ALIGN(num_bad << 9, PAGE_SIZE); |
| 400 | while (zero_len) { |
| 401 | unsigned long chunk = min(zero_len, PAGE_SIZE); |
| 402 | |
| 403 | rc = nvdimm_write_bytes(ndns, nsoff, zero_page, |
| 404 | chunk, 0); |
| 405 | if (rc) |
| 406 | break; |
| 407 | |
| 408 | zero_len -= chunk; |
| 409 | nsoff += chunk; |
| 410 | } |
| 411 | if (rc) { |
| 412 | dev_err(&nd_pfn->dev, |
| 413 | "error clearing %x badblocks at %lx\n", |
| 414 | num_bad, first_bad); |
| 415 | return rc; |
| 416 | } |
| 417 | } |
| 418 | } while (bb_present); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 423 | int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 424 | { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 425 | u64 checksum, offset; |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 426 | enum nd_pfn_mode mode; |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 427 | struct nd_namespace_io *nsio; |
Dan Williams | 19deaa2 | 2017-12-19 15:07:10 -0800 | [diff] [blame] | 428 | unsigned long align, start_pad; |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 429 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 430 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 431 | const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 432 | |
| 433 | if (!pfn_sb || !ndns) |
| 434 | return -ENODEV; |
| 435 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 436 | if (!is_memory(nd_pfn->dev.parent)) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 437 | return -ENODEV; |
| 438 | |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 439 | if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0)) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 440 | return -ENXIO; |
| 441 | |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 442 | if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 443 | return -ENODEV; |
| 444 | |
| 445 | checksum = le64_to_cpu(pfn_sb->checksum); |
| 446 | pfn_sb->checksum = 0; |
| 447 | if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb)) |
| 448 | return -ENODEV; |
| 449 | pfn_sb->checksum = cpu_to_le64(checksum); |
| 450 | |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 451 | if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0) |
| 452 | return -ENODEV; |
| 453 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 454 | if (__le16_to_cpu(pfn_sb->version_minor) < 1) { |
| 455 | pfn_sb->start_pad = 0; |
| 456 | pfn_sb->end_trunc = 0; |
| 457 | } |
| 458 | |
Dan Williams | 45a0dac | 2016-03-31 15:41:18 -0700 | [diff] [blame] | 459 | if (__le16_to_cpu(pfn_sb->version_minor) < 2) |
| 460 | pfn_sb->align = 0; |
| 461 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 462 | switch (le32_to_cpu(pfn_sb->mode)) { |
| 463 | case PFN_MODE_RAM: |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 464 | case PFN_MODE_PMEM: |
Dan Williams | 45eb570 | 2016-01-29 17:42:51 -0800 | [diff] [blame] | 465 | break; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 466 | default: |
| 467 | return -ENXIO; |
| 468 | } |
| 469 | |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 470 | align = le32_to_cpu(pfn_sb->align); |
| 471 | offset = le64_to_cpu(pfn_sb->dataoff); |
Dan Williams | 19deaa2 | 2017-12-19 15:07:10 -0800 | [diff] [blame] | 472 | start_pad = le32_to_cpu(pfn_sb->start_pad); |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 473 | if (align == 0) |
| 474 | align = 1UL << ilog2(offset); |
| 475 | mode = le32_to_cpu(pfn_sb->mode); |
| 476 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 477 | if (!nd_pfn->uuid) { |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 478 | /* |
| 479 | * When probing a namepace via nd_pfn_probe() the uuid |
| 480 | * is NULL (see: nd_pfn_devinit()) we init settings from |
| 481 | * pfn_sb |
| 482 | */ |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 483 | nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL); |
| 484 | if (!nd_pfn->uuid) |
| 485 | return -ENOMEM; |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 486 | nd_pfn->align = align; |
| 487 | nd_pfn->mode = mode; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 488 | } else { |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 489 | /* |
| 490 | * When probing a pfn / dax instance we validate the |
| 491 | * live settings against the pfn_sb |
| 492 | */ |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 493 | if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0) |
Dan Williams | e567056 | 2016-04-07 19:59:27 -0700 | [diff] [blame] | 494 | return -ENODEV; |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * If the uuid validates, but other settings mismatch |
| 498 | * return EINVAL because userspace has managed to change |
| 499 | * the configuration without specifying new |
| 500 | * identification. |
| 501 | */ |
| 502 | if (nd_pfn->align != align || nd_pfn->mode != mode) { |
| 503 | dev_err(&nd_pfn->dev, |
| 504 | "init failed, settings mismatch\n"); |
| 505 | dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n", |
| 506 | nd_pfn->align, align, nd_pfn->mode, |
| 507 | mode); |
| 508 | return -EINVAL; |
| 509 | } |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 512 | if (align > nvdimm_namespace_capacity(ndns)) { |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 513 | dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n", |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 514 | align, nvdimm_namespace_capacity(ndns)); |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 515 | return -EINVAL; |
| 516 | } |
| 517 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 518 | /* |
| 519 | * These warnings are verbose because they can only trigger in |
| 520 | * the case where the physical address alignment of the |
| 521 | * namespace has changed since the pfn superblock was |
| 522 | * established. |
| 523 | */ |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 524 | nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 9f1e8ce | 2015-12-10 15:14:20 -0800 | [diff] [blame] | 525 | if (offset >= resource_size(&nsio->res)) { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 526 | dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", |
| 527 | dev_name(&ndns->dev)); |
| 528 | return -EBUSY; |
| 529 | } |
| 530 | |
Dan Williams | 19deaa2 | 2017-12-19 15:07:10 -0800 | [diff] [blame] | 531 | if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align)) |
Dan Williams | 5e24c9f | 2016-05-21 11:01:41 -0700 | [diff] [blame] | 532 | || !IS_ALIGNED(offset, PAGE_SIZE)) { |
Dan Williams | 1ee6667 | 2016-06-23 17:50:39 -0700 | [diff] [blame] | 533 | dev_err(&nd_pfn->dev, |
| 534 | "bad offset: %#llx dax disabled align: %#lx\n", |
| 535 | offset, align); |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 536 | return -ENXIO; |
| 537 | } |
| 538 | |
Vishal Verma | 48af2f7 | 2018-09-18 17:48:31 -0600 | [diff] [blame] | 539 | return nd_pfn_clear_memmap_errors(nd_pfn); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 540 | } |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 541 | EXPORT_SYMBOL(nd_pfn_validate); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 542 | |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 543 | int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 544 | { |
| 545 | int rc; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 546 | struct nd_pfn *nd_pfn; |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame] | 547 | struct device *pfn_dev; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 548 | struct nd_pfn_sb *pfn_sb; |
| 549 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); |
| 550 | |
| 551 | if (ndns->force_raw) |
| 552 | return -ENODEV; |
| 553 | |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 554 | switch (ndns->claim_class) { |
| 555 | case NVDIMM_CCLASS_NONE: |
| 556 | case NVDIMM_CCLASS_PFN: |
| 557 | break; |
| 558 | default: |
| 559 | return -ENODEV; |
| 560 | } |
| 561 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 562 | nvdimm_bus_lock(&ndns->dev); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 563 | nd_pfn = nd_pfn_alloc(nd_region); |
| 564 | pfn_dev = nd_pfn_devinit(nd_pfn, ndns); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 565 | nvdimm_bus_unlock(&ndns->dev); |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame] | 566 | if (!pfn_dev) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 567 | return -ENOMEM; |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame] | 568 | pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); |
| 569 | nd_pfn = to_nd_pfn(pfn_dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 570 | nd_pfn->pfn_sb = pfn_sb; |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 571 | rc = nd_pfn_validate(nd_pfn, PFN_SIG); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 572 | dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 573 | if (rc < 0) { |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 574 | nd_detach_ndns(pfn_dev, &nd_pfn->ndns); |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame] | 575 | put_device(pfn_dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 576 | } else |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame] | 577 | __nd_device_register(pfn_dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 578 | |
| 579 | return rc; |
| 580 | } |
| 581 | EXPORT_SYMBOL(nd_pfn_probe); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * We hotplug memory at section granularity, pad the reserved area from |
| 585 | * the previous section base to the namespace base address. |
| 586 | */ |
| 587 | static unsigned long init_altmap_base(resource_size_t base) |
| 588 | { |
| 589 | unsigned long base_pfn = PHYS_PFN(base); |
| 590 | |
| 591 | return PFN_SECTION_ALIGN_DOWN(base_pfn); |
| 592 | } |
| 593 | |
| 594 | static unsigned long init_altmap_reserve(resource_size_t base) |
| 595 | { |
| 596 | unsigned long reserve = PHYS_PFN(SZ_8K); |
| 597 | unsigned long base_pfn = PHYS_PFN(base); |
| 598 | |
| 599 | reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn); |
| 600 | return reserve; |
| 601 | } |
| 602 | |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 603 | static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap) |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 604 | { |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 605 | struct resource *res = &pgmap->res; |
| 606 | struct vmem_altmap *altmap = &pgmap->altmap; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 607 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 608 | u64 offset = le64_to_cpu(pfn_sb->dataoff); |
| 609 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); |
| 610 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); |
| 611 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 612 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 613 | resource_size_t base = nsio->res.start + start_pad; |
| 614 | struct vmem_altmap __altmap = { |
| 615 | .base_pfn = init_altmap_base(base), |
| 616 | .reserve = init_altmap_reserve(base), |
| 617 | }; |
| 618 | |
| 619 | memcpy(res, &nsio->res, sizeof(*res)); |
| 620 | res->start += start_pad; |
| 621 | res->end -= end_trunc; |
| 622 | |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 623 | if (nd_pfn->mode == PFN_MODE_RAM) { |
| 624 | if (offset < SZ_8K) |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 625 | return -EINVAL; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 626 | nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns); |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 627 | pgmap->altmap_valid = false; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 628 | } else if (nd_pfn->mode == PFN_MODE_PMEM) { |
Dan Williams | d5483fe | 2017-05-04 19:54:42 -0700 | [diff] [blame] | 629 | nd_pfn->npfns = PFN_SECTION_ALIGN_UP((resource_size(res) |
| 630 | - offset) / PAGE_SIZE); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 631 | if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns) |
| 632 | dev_info(&nd_pfn->dev, |
| 633 | "number of pfns truncated from %lld to %ld\n", |
| 634 | le64_to_cpu(nd_pfn->pfn_sb->npfns), |
| 635 | nd_pfn->npfns); |
| 636 | memcpy(altmap, &__altmap, sizeof(*altmap)); |
| 637 | altmap->free = PHYS_PFN(offset - SZ_8K); |
| 638 | altmap->alloc = 0; |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 639 | pgmap->altmap_valid = true; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 640 | } else |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 641 | return -ENXIO; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 642 | |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 643 | return 0; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Dan Williams | 41fce90 | 2017-12-04 14:07:43 -0800 | [diff] [blame] | 646 | static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys) |
| 647 | { |
| 648 | return min_t(u64, PHYS_SECTION_ALIGN_DOWN(phys), |
| 649 | ALIGN_DOWN(phys, nd_pfn->align)); |
| 650 | } |
| 651 | |
Dan Williams | ae86cbf | 2018-11-24 10:47:04 -0800 | [diff] [blame] | 652 | /* |
| 653 | * Check if pmem collides with 'System RAM', or other regions when |
| 654 | * section aligned. Trim it accordingly. |
| 655 | */ |
| 656 | static void trim_pfn_device(struct nd_pfn *nd_pfn, u32 *start_pad, u32 *end_trunc) |
| 657 | { |
| 658 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 659 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 660 | struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent); |
| 661 | const resource_size_t start = nsio->res.start; |
| 662 | const resource_size_t end = start + resource_size(&nsio->res); |
| 663 | resource_size_t adjust, size; |
| 664 | |
| 665 | *start_pad = 0; |
| 666 | *end_trunc = 0; |
| 667 | |
| 668 | adjust = start - PHYS_SECTION_ALIGN_DOWN(start); |
| 669 | size = resource_size(&nsio->res) + adjust; |
| 670 | if (region_intersects(start - adjust, size, IORESOURCE_SYSTEM_RAM, |
| 671 | IORES_DESC_NONE) == REGION_MIXED |
| 672 | || nd_region_conflict(nd_region, start - adjust, size)) |
| 673 | *start_pad = PHYS_SECTION_ALIGN_UP(start) - start; |
| 674 | |
| 675 | /* Now check that end of the range does not collide. */ |
| 676 | adjust = PHYS_SECTION_ALIGN_UP(end) - end; |
| 677 | size = resource_size(&nsio->res) + adjust; |
| 678 | if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM, |
| 679 | IORES_DESC_NONE) == REGION_MIXED |
| 680 | || !IS_ALIGNED(end, nd_pfn->align) |
| 681 | || nd_region_conflict(nd_region, start, size + adjust)) |
| 682 | *end_trunc = end - phys_pmem_align_down(nd_pfn, end); |
| 683 | } |
| 684 | |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 685 | static int nd_pfn_init(struct nd_pfn *nd_pfn) |
| 686 | { |
Dan Williams | 52ac23b | 2016-03-31 09:37:11 -0700 | [diff] [blame] | 687 | u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 688 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
Dan Williams | ae86cbf | 2018-11-24 10:47:04 -0800 | [diff] [blame] | 689 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 690 | resource_size_t start, size; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 691 | struct nd_region *nd_region; |
Dan Williams | ae86cbf | 2018-11-24 10:47:04 -0800 | [diff] [blame] | 692 | u32 start_pad, end_trunc; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 693 | struct nd_pfn_sb *pfn_sb; |
| 694 | unsigned long npfns; |
| 695 | phys_addr_t offset; |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 696 | const char *sig; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 697 | u64 checksum; |
| 698 | int rc; |
| 699 | |
| 700 | pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL); |
| 701 | if (!pfn_sb) |
| 702 | return -ENOMEM; |
| 703 | |
| 704 | nd_pfn->pfn_sb = pfn_sb; |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 705 | if (is_nd_dax(&nd_pfn->dev)) |
| 706 | sig = DAX_SIG; |
| 707 | else |
| 708 | sig = PFN_SIG; |
| 709 | rc = nd_pfn_validate(nd_pfn, sig); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 710 | if (rc != -ENODEV) |
| 711 | return rc; |
| 712 | |
| 713 | /* no info block, do init */; |
| 714 | nd_region = to_nd_region(nd_pfn->dev.parent); |
| 715 | if (nd_region->ro) { |
| 716 | dev_info(&nd_pfn->dev, |
| 717 | "%s is read-only, unable to init metadata\n", |
| 718 | dev_name(&nd_region->dev)); |
| 719 | return -ENXIO; |
| 720 | } |
| 721 | |
| 722 | memset(pfn_sb, 0, sizeof(*pfn_sb)); |
| 723 | |
Dan Williams | ae86cbf | 2018-11-24 10:47:04 -0800 | [diff] [blame] | 724 | trim_pfn_device(nd_pfn, &start_pad, &end_trunc); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 725 | if (start_pad + end_trunc) |
Dan Williams | 41fce90 | 2017-12-04 14:07:43 -0800 | [diff] [blame] | 726 | dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n", |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 727 | dev_name(&ndns->dev), start_pad + end_trunc); |
| 728 | |
| 729 | /* |
| 730 | * Note, we use 64 here for the standard size of struct page, |
| 731 | * debugging options may cause it to be larger in which case the |
| 732 | * implementation will limit the pfns advertised through |
| 733 | * ->direct_access() to those that are included in the memmap. |
| 734 | */ |
Dan Williams | ae86cbf | 2018-11-24 10:47:04 -0800 | [diff] [blame] | 735 | start = nsio->res.start + start_pad; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 736 | size = resource_size(&nsio->res); |
Dan Williams | d5483fe | 2017-05-04 19:54:42 -0700 | [diff] [blame] | 737 | npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K) |
| 738 | / PAGE_SIZE); |
Dan Williams | 594d6d9 | 2016-05-18 09:59:34 -0700 | [diff] [blame] | 739 | if (nd_pfn->mode == PFN_MODE_PMEM) { |
Dan Williams | 594d6d9 | 2016-05-18 09:59:34 -0700 | [diff] [blame] | 740 | /* |
Oliver O'Halloran | 0dd6964 | 2017-06-27 19:56:33 +1000 | [diff] [blame] | 741 | * The altmap should be padded out to the block size used |
| 742 | * when populating the vmemmap. This *should* be equal to |
| 743 | * PMD_SIZE for most architectures. |
Dan Williams | 594d6d9 | 2016-05-18 09:59:34 -0700 | [diff] [blame] | 744 | */ |
Dan Williams | bfb3452 | 2017-02-04 14:47:31 -0800 | [diff] [blame] | 745 | offset = ALIGN(start + SZ_8K + 64 * npfns + dax_label_reserve, |
Oliver O'Halloran | 0dd6964 | 2017-06-27 19:56:33 +1000 | [diff] [blame] | 746 | max(nd_pfn->align, PMD_SIZE)) - start; |
Dan Williams | 594d6d9 | 2016-05-18 09:59:34 -0700 | [diff] [blame] | 747 | } else if (nd_pfn->mode == PFN_MODE_RAM) |
Dan Williams | 52ac23b | 2016-03-31 09:37:11 -0700 | [diff] [blame] | 748 | offset = ALIGN(start + SZ_8K + dax_label_reserve, |
| 749 | nd_pfn->align) - start; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 750 | else |
| 751 | return -ENXIO; |
| 752 | |
| 753 | if (offset + start_pad + end_trunc >= size) { |
| 754 | dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n", |
| 755 | dev_name(&ndns->dev)); |
| 756 | return -ENXIO; |
| 757 | } |
| 758 | |
| 759 | npfns = (size - offset - start_pad - end_trunc) / SZ_4K; |
| 760 | pfn_sb->mode = cpu_to_le32(nd_pfn->mode); |
| 761 | pfn_sb->dataoff = cpu_to_le64(offset); |
| 762 | pfn_sb->npfns = cpu_to_le64(npfns); |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 763 | memcpy(pfn_sb->signature, sig, PFN_SIG_LEN); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 764 | memcpy(pfn_sb->uuid, nd_pfn->uuid, 16); |
| 765 | memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16); |
| 766 | pfn_sb->version_major = cpu_to_le16(1); |
Dan Williams | 45a0dac | 2016-03-31 15:41:18 -0700 | [diff] [blame] | 767 | pfn_sb->version_minor = cpu_to_le16(2); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 768 | pfn_sb->start_pad = cpu_to_le32(start_pad); |
| 769 | pfn_sb->end_trunc = cpu_to_le32(end_trunc); |
Dan Williams | 45a0dac | 2016-03-31 15:41:18 -0700 | [diff] [blame] | 770 | pfn_sb->align = cpu_to_le32(nd_pfn->align); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 771 | checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb); |
| 772 | pfn_sb->checksum = cpu_to_le64(checksum); |
| 773 | |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 774 | return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
| 778 | * Determine the effective resource range and vmem_altmap from an nd_pfn |
| 779 | * instance. |
| 780 | */ |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 781 | int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap) |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 782 | { |
| 783 | int rc; |
| 784 | |
| 785 | if (!nd_pfn->uuid || !nd_pfn->ndns) |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 786 | return -ENODEV; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 787 | |
| 788 | rc = nd_pfn_init(nd_pfn); |
| 789 | if (rc) |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 790 | return rc; |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 791 | |
Christoph Hellwig | e8d5134 | 2017-12-29 08:54:05 +0100 | [diff] [blame] | 792 | /* we need a valid pfn_sb before we can init a dev_pagemap */ |
| 793 | return __nvdimm_setup_pfn(nd_pfn, pgmap); |
Dan Williams | ac515c0 | 2016-03-22 00:29:43 -0700 | [diff] [blame] | 794 | } |
| 795 | EXPORT_SYMBOL_GPL(nvdimm_setup_pfn); |