Dan Williams | e145574 | 2015-07-30 17:57:47 -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 | #include <linux/blkdev.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/genhd.h> |
| 16 | #include <linux/sizes.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include "nd-core.h" |
| 21 | #include "pfn.h" |
| 22 | #include "nd.h" |
| 23 | |
| 24 | static void nd_pfn_release(struct device *dev) |
| 25 | { |
| 26 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 27 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 28 | |
| 29 | dev_dbg(dev, "%s\n", __func__); |
| 30 | nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns); |
| 31 | ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id); |
| 32 | kfree(nd_pfn->uuid); |
| 33 | kfree(nd_pfn); |
| 34 | } |
| 35 | |
| 36 | static struct device_type nd_pfn_device_type = { |
| 37 | .name = "nd_pfn", |
| 38 | .release = nd_pfn_release, |
| 39 | }; |
| 40 | |
| 41 | bool is_nd_pfn(struct device *dev) |
| 42 | { |
| 43 | return dev ? dev->type == &nd_pfn_device_type : false; |
| 44 | } |
| 45 | EXPORT_SYMBOL(is_nd_pfn); |
| 46 | |
| 47 | struct nd_pfn *to_nd_pfn(struct device *dev) |
| 48 | { |
| 49 | struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev); |
| 50 | |
| 51 | WARN_ON(!is_nd_pfn(dev)); |
| 52 | return nd_pfn; |
| 53 | } |
| 54 | EXPORT_SYMBOL(to_nd_pfn); |
| 55 | |
| 56 | static ssize_t mode_show(struct device *dev, |
| 57 | struct device_attribute *attr, char *buf) |
| 58 | { |
| 59 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 60 | |
| 61 | switch (nd_pfn->mode) { |
| 62 | case PFN_MODE_RAM: |
| 63 | return sprintf(buf, "ram\n"); |
| 64 | case PFN_MODE_PMEM: |
| 65 | return sprintf(buf, "pmem\n"); |
| 66 | default: |
| 67 | return sprintf(buf, "none\n"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static ssize_t mode_store(struct device *dev, |
| 72 | struct device_attribute *attr, const char *buf, size_t len) |
| 73 | { |
| 74 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 75 | ssize_t rc = 0; |
| 76 | |
| 77 | device_lock(dev); |
| 78 | nvdimm_bus_lock(dev); |
| 79 | if (dev->driver) |
| 80 | rc = -EBUSY; |
| 81 | else { |
| 82 | size_t n = len - 1; |
| 83 | |
| 84 | if (strncmp(buf, "pmem\n", n) == 0 |
| 85 | || strncmp(buf, "pmem", n) == 0) { |
Dan Williams | d2c0f04 | 2016-01-15 16:56:26 -0800 | [diff] [blame] | 86 | nd_pfn->mode = PFN_MODE_PMEM; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 87 | } else if (strncmp(buf, "ram\n", n) == 0 |
| 88 | || strncmp(buf, "ram", n) == 0) |
| 89 | nd_pfn->mode = PFN_MODE_RAM; |
| 90 | else if (strncmp(buf, "none\n", n) == 0 |
| 91 | || strncmp(buf, "none", n) == 0) |
| 92 | nd_pfn->mode = PFN_MODE_NONE; |
| 93 | else |
| 94 | rc = -EINVAL; |
| 95 | } |
| 96 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
| 97 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); |
| 98 | nvdimm_bus_unlock(dev); |
| 99 | device_unlock(dev); |
| 100 | |
| 101 | return rc ? rc : len; |
| 102 | } |
| 103 | static DEVICE_ATTR_RW(mode); |
| 104 | |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 105 | static ssize_t align_show(struct device *dev, |
| 106 | struct device_attribute *attr, char *buf) |
| 107 | { |
| 108 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 109 | |
| 110 | return sprintf(buf, "%lx\n", nd_pfn->align); |
| 111 | } |
| 112 | |
| 113 | static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf) |
| 114 | { |
| 115 | unsigned long val; |
| 116 | int rc; |
| 117 | |
| 118 | rc = kstrtoul(buf, 0, &val); |
| 119 | if (rc) |
| 120 | return rc; |
| 121 | |
| 122 | if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | if (nd_pfn->dev.driver) |
| 126 | return -EBUSY; |
| 127 | else |
| 128 | nd_pfn->align = val; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static ssize_t align_store(struct device *dev, |
| 134 | struct device_attribute *attr, const char *buf, size_t len) |
| 135 | { |
| 136 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 137 | ssize_t rc; |
| 138 | |
| 139 | device_lock(dev); |
| 140 | nvdimm_bus_lock(dev); |
| 141 | rc = __align_store(nd_pfn, buf); |
| 142 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
| 143 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); |
| 144 | nvdimm_bus_unlock(dev); |
| 145 | device_unlock(dev); |
| 146 | |
| 147 | return rc ? rc : len; |
| 148 | } |
| 149 | static DEVICE_ATTR_RW(align); |
| 150 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 151 | static ssize_t uuid_show(struct device *dev, |
| 152 | struct device_attribute *attr, char *buf) |
| 153 | { |
| 154 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 155 | |
| 156 | if (nd_pfn->uuid) |
| 157 | return sprintf(buf, "%pUb\n", nd_pfn->uuid); |
| 158 | return sprintf(buf, "\n"); |
| 159 | } |
| 160 | |
| 161 | static ssize_t uuid_store(struct device *dev, |
| 162 | struct device_attribute *attr, const char *buf, size_t len) |
| 163 | { |
| 164 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 165 | ssize_t rc; |
| 166 | |
| 167 | device_lock(dev); |
| 168 | rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len); |
| 169 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
| 170 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); |
| 171 | device_unlock(dev); |
| 172 | |
| 173 | return rc ? rc : len; |
| 174 | } |
| 175 | static DEVICE_ATTR_RW(uuid); |
| 176 | |
| 177 | static ssize_t namespace_show(struct device *dev, |
| 178 | struct device_attribute *attr, char *buf) |
| 179 | { |
| 180 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 181 | ssize_t rc; |
| 182 | |
| 183 | nvdimm_bus_lock(dev); |
| 184 | rc = sprintf(buf, "%s\n", nd_pfn->ndns |
| 185 | ? dev_name(&nd_pfn->ndns->dev) : ""); |
| 186 | nvdimm_bus_unlock(dev); |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | static ssize_t namespace_store(struct device *dev, |
| 191 | struct device_attribute *attr, const char *buf, size_t len) |
| 192 | { |
| 193 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 194 | ssize_t rc; |
| 195 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 196 | device_lock(dev); |
Axel Lin | 4ca8b57a | 2015-09-16 21:25:38 +0800 | [diff] [blame] | 197 | nvdimm_bus_lock(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 198 | rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len); |
| 199 | dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__, |
| 200 | rc, buf, buf[len - 1] == '\n' ? "" : "\n"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 201 | nvdimm_bus_unlock(dev); |
Axel Lin | 4ca8b57a | 2015-09-16 21:25:38 +0800 | [diff] [blame] | 202 | device_unlock(dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 203 | |
| 204 | return rc; |
| 205 | } |
| 206 | static DEVICE_ATTR_RW(namespace); |
| 207 | |
Dan Williams | f6ed58c | 2016-03-03 09:46:04 -0800 | [diff] [blame] | 208 | static ssize_t resource_show(struct device *dev, |
| 209 | struct device_attribute *attr, char *buf) |
| 210 | { |
| 211 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 212 | ssize_t rc; |
| 213 | |
| 214 | device_lock(dev); |
| 215 | if (dev->driver) { |
| 216 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 217 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); |
| 218 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 219 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); |
| 220 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 221 | |
| 222 | rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start |
| 223 | + start_pad + offset); |
| 224 | } else { |
| 225 | /* no address to convey if the pfn instance is disabled */ |
| 226 | rc = -ENXIO; |
| 227 | } |
| 228 | device_unlock(dev); |
| 229 | |
| 230 | return rc; |
| 231 | } |
| 232 | static DEVICE_ATTR_RO(resource); |
| 233 | |
| 234 | static ssize_t size_show(struct device *dev, |
| 235 | struct device_attribute *attr, char *buf) |
| 236 | { |
| 237 | struct nd_pfn *nd_pfn = to_nd_pfn(dev); |
| 238 | ssize_t rc; |
| 239 | |
| 240 | device_lock(dev); |
| 241 | if (dev->driver) { |
| 242 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 243 | u64 offset = __le64_to_cpu(pfn_sb->dataoff); |
| 244 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 245 | u32 start_pad = __le32_to_cpu(pfn_sb->start_pad); |
| 246 | u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); |
| 247 | struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); |
| 248 | |
| 249 | rc = sprintf(buf, "%llu\n", (unsigned long long) |
| 250 | resource_size(&nsio->res) - start_pad |
| 251 | - end_trunc - offset); |
| 252 | } else { |
| 253 | /* no size to convey if the pfn instance is disabled */ |
| 254 | rc = -ENXIO; |
| 255 | } |
| 256 | device_unlock(dev); |
| 257 | |
| 258 | return rc; |
| 259 | } |
| 260 | static DEVICE_ATTR_RO(size); |
| 261 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 262 | static struct attribute *nd_pfn_attributes[] = { |
| 263 | &dev_attr_mode.attr, |
| 264 | &dev_attr_namespace.attr, |
| 265 | &dev_attr_uuid.attr, |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 266 | &dev_attr_align.attr, |
Dan Williams | f6ed58c | 2016-03-03 09:46:04 -0800 | [diff] [blame] | 267 | &dev_attr_resource.attr, |
| 268 | &dev_attr_size.attr, |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 269 | NULL, |
| 270 | }; |
| 271 | |
| 272 | static struct attribute_group nd_pfn_attribute_group = { |
| 273 | .attrs = nd_pfn_attributes, |
| 274 | }; |
| 275 | |
| 276 | static const struct attribute_group *nd_pfn_attribute_groups[] = { |
| 277 | &nd_pfn_attribute_group, |
| 278 | &nd_device_attribute_group, |
| 279 | &nd_numa_attribute_group, |
| 280 | NULL, |
| 281 | }; |
| 282 | |
| 283 | static struct device *__nd_pfn_create(struct nd_region *nd_region, |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 284 | struct nd_namespace_common *ndns) |
| 285 | { |
| 286 | struct nd_pfn *nd_pfn; |
| 287 | struct device *dev; |
| 288 | |
| 289 | /* we can only create pages for contiguous ranged of pmem */ |
| 290 | if (!is_nd_pmem(&nd_region->dev)) |
| 291 | return NULL; |
| 292 | |
| 293 | nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL); |
| 294 | if (!nd_pfn) |
| 295 | return NULL; |
| 296 | |
| 297 | nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL); |
| 298 | if (nd_pfn->id < 0) { |
| 299 | kfree(nd_pfn); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Dan Williams | f7c6ab8 | 2015-12-10 16:11:59 -0800 | [diff] [blame] | 303 | nd_pfn->mode = PFN_MODE_NONE; |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 304 | nd_pfn->align = HPAGE_SIZE; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 305 | dev = &nd_pfn->dev; |
| 306 | dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id); |
| 307 | dev->parent = &nd_region->dev; |
| 308 | dev->type = &nd_pfn_device_type; |
| 309 | dev->groups = nd_pfn_attribute_groups; |
| 310 | device_initialize(&nd_pfn->dev); |
| 311 | if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) { |
| 312 | dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n", |
| 313 | __func__, dev_name(ndns->claim)); |
| 314 | put_device(dev); |
| 315 | return NULL; |
| 316 | } |
| 317 | return dev; |
| 318 | } |
| 319 | |
| 320 | struct device *nd_pfn_create(struct nd_region *nd_region) |
| 321 | { |
Dan Williams | f7c6ab8 | 2015-12-10 16:11:59 -0800 | [diff] [blame] | 322 | struct device *dev = __nd_pfn_create(nd_region, NULL); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 323 | |
| 324 | if (dev) |
| 325 | __nd_device_register(dev); |
| 326 | return dev; |
| 327 | } |
| 328 | |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 329 | int nd_pfn_validate(struct nd_pfn *nd_pfn) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 330 | { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 331 | u64 checksum, offset; |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 332 | struct nd_namespace_io *nsio; |
| 333 | struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; |
| 334 | struct nd_namespace_common *ndns = nd_pfn->ndns; |
| 335 | const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 336 | |
| 337 | if (!pfn_sb || !ndns) |
| 338 | return -ENODEV; |
| 339 | |
| 340 | if (!is_nd_pmem(nd_pfn->dev.parent)) |
| 341 | return -ENODEV; |
| 342 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 343 | if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb))) |
| 344 | return -ENXIO; |
| 345 | |
| 346 | if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0) |
| 347 | return -ENODEV; |
| 348 | |
| 349 | checksum = le64_to_cpu(pfn_sb->checksum); |
| 350 | pfn_sb->checksum = 0; |
| 351 | if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb)) |
| 352 | return -ENODEV; |
| 353 | pfn_sb->checksum = cpu_to_le64(checksum); |
| 354 | |
Dan Williams | a34d5e8 | 2015-12-12 16:09:14 -0800 | [diff] [blame] | 355 | if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0) |
| 356 | return -ENODEV; |
| 357 | |
Dan Williams | cfe30b8 | 2016-03-03 09:38:00 -0800 | [diff] [blame] | 358 | if (__le16_to_cpu(pfn_sb->version_minor) < 1) { |
| 359 | pfn_sb->start_pad = 0; |
| 360 | pfn_sb->end_trunc = 0; |
| 361 | } |
| 362 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 363 | switch (le32_to_cpu(pfn_sb->mode)) { |
| 364 | case PFN_MODE_RAM: |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 365 | case PFN_MODE_PMEM: |
Dan Williams | 45eb570 | 2016-01-29 17:42:51 -0800 | [diff] [blame] | 366 | break; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 367 | default: |
| 368 | return -ENXIO; |
| 369 | } |
| 370 | |
| 371 | if (!nd_pfn->uuid) { |
| 372 | /* from probe we allocate */ |
| 373 | nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL); |
| 374 | if (!nd_pfn->uuid) |
| 375 | return -ENOMEM; |
| 376 | } else { |
| 377 | /* from init we validate */ |
| 378 | if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0) |
Dan Williams | e567056 | 2016-04-07 19:59:27 -0700 | [diff] [blame] | 379 | return -ENODEV; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 382 | if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) { |
| 383 | dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n", |
| 384 | nd_pfn->align, nvdimm_namespace_capacity(ndns)); |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 388 | /* |
| 389 | * These warnings are verbose because they can only trigger in |
| 390 | * the case where the physical address alignment of the |
| 391 | * namespace has changed since the pfn superblock was |
| 392 | * established. |
| 393 | */ |
| 394 | offset = le64_to_cpu(pfn_sb->dataoff); |
| 395 | nsio = to_nd_namespace_io(&ndns->dev); |
Dan Williams | 9f1e8ce | 2015-12-10 15:14:20 -0800 | [diff] [blame] | 396 | if (offset >= resource_size(&nsio->res)) { |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 397 | dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", |
| 398 | dev_name(&ndns->dev)); |
| 399 | return -EBUSY; |
| 400 | } |
| 401 | |
Dan Williams | 315c562 | 2015-12-10 14:45:23 -0800 | [diff] [blame] | 402 | nd_pfn->align = 1UL << ilog2(offset); |
| 403 | if (!is_power_of_2(offset) || offset < PAGE_SIZE) { |
| 404 | dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n", |
| 405 | offset); |
| 406 | return -ENXIO; |
| 407 | } |
| 408 | |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 409 | return 0; |
| 410 | } |
Dan Williams | 32ab0a3f | 2015-08-01 02:16:37 -0400 | [diff] [blame] | 411 | EXPORT_SYMBOL(nd_pfn_validate); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 412 | |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 413 | int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns, |
| 414 | void *drvdata) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 415 | { |
| 416 | int rc; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 417 | struct nd_pfn *nd_pfn; |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 418 | struct device *pfn_dev; |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 419 | struct nd_pfn_sb *pfn_sb; |
| 420 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); |
| 421 | |
| 422 | if (ndns->force_raw) |
| 423 | return -ENODEV; |
| 424 | |
| 425 | nvdimm_bus_lock(&ndns->dev); |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 426 | pfn_dev = __nd_pfn_create(nd_region, ndns); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 427 | nvdimm_bus_unlock(&ndns->dev); |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 428 | if (!pfn_dev) |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 429 | return -ENOMEM; |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 430 | dev_set_drvdata(pfn_dev, drvdata); |
| 431 | pfn_sb = devm_kzalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); |
| 432 | nd_pfn = to_nd_pfn(pfn_dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 433 | nd_pfn->pfn_sb = pfn_sb; |
| 434 | rc = nd_pfn_validate(nd_pfn); |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 435 | dev_dbg(dev, "%s: pfn: %s\n", __func__, |
| 436 | rc == 0 ? dev_name(pfn_dev) : "<none>"); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 437 | if (rc < 0) { |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 438 | __nd_detach_ndns(pfn_dev, &nd_pfn->ndns); |
| 439 | put_device(pfn_dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 440 | } else |
Dan Williams | bd03294 | 2016-03-17 18:16:15 -0700 | [diff] [blame^] | 441 | __nd_device_register(pfn_dev); |
Dan Williams | e145574 | 2015-07-30 17:57:47 -0400 | [diff] [blame] | 442 | |
| 443 | return rc; |
| 444 | } |
| 445 | EXPORT_SYMBOL(nd_pfn_probe); |