Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2013-2016 Intel Corporation. All rights reserved. |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/device.h> |
| 6 | #include <linux/sizes.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include "nd-core.h" |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 10 | #include "pfn.h" |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 11 | #include "nd.h" |
| 12 | |
| 13 | static void nd_dax_release(struct device *dev) |
| 14 | { |
| 15 | struct nd_region *nd_region = to_nd_region(dev->parent); |
| 16 | struct nd_dax *nd_dax = to_nd_dax(dev); |
| 17 | struct nd_pfn *nd_pfn = &nd_dax->nd_pfn; |
| 18 | |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 19 | dev_dbg(dev, "trace\n"); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 20 | nd_detach_ndns(dev, &nd_pfn->ndns); |
| 21 | ida_simple_remove(&nd_region->dax_ida, nd_pfn->id); |
| 22 | kfree(nd_pfn->uuid); |
| 23 | kfree(nd_dax); |
| 24 | } |
| 25 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 26 | struct nd_dax *to_nd_dax(struct device *dev) |
| 27 | { |
| 28 | struct nd_dax *nd_dax = container_of(dev, struct nd_dax, nd_pfn.dev); |
| 29 | |
| 30 | WARN_ON(!is_nd_dax(dev)); |
| 31 | return nd_dax; |
| 32 | } |
| 33 | EXPORT_SYMBOL(to_nd_dax); |
| 34 | |
Dan Williams | 78c81cc | 2019-11-06 19:56:41 -0800 | [diff] [blame] | 35 | static const struct device_type nd_dax_device_type = { |
| 36 | .name = "nd_dax", |
| 37 | .release = nd_dax_release, |
| 38 | .groups = nd_pfn_attribute_groups, |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Dan Williams | 78c81cc | 2019-11-06 19:56:41 -0800 | [diff] [blame] | 41 | bool is_nd_dax(struct device *dev) |
| 42 | { |
| 43 | return dev ? dev->type == &nd_dax_device_type : false; |
| 44 | } |
| 45 | EXPORT_SYMBOL(is_nd_dax); |
| 46 | |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 47 | static struct nd_dax *nd_dax_alloc(struct nd_region *nd_region) |
| 48 | { |
| 49 | struct nd_pfn *nd_pfn; |
| 50 | struct nd_dax *nd_dax; |
| 51 | struct device *dev; |
| 52 | |
| 53 | nd_dax = kzalloc(sizeof(*nd_dax), GFP_KERNEL); |
| 54 | if (!nd_dax) |
| 55 | return NULL; |
| 56 | |
| 57 | nd_pfn = &nd_dax->nd_pfn; |
| 58 | nd_pfn->id = ida_simple_get(&nd_region->dax_ida, 0, 0, GFP_KERNEL); |
| 59 | if (nd_pfn->id < 0) { |
| 60 | kfree(nd_dax); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | dev = &nd_pfn->dev; |
| 65 | dev_set_name(dev, "dax%d.%d", nd_region->id, nd_pfn->id); |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 66 | dev->type = &nd_dax_device_type; |
| 67 | dev->parent = &nd_region->dev; |
| 68 | |
| 69 | return nd_dax; |
| 70 | } |
| 71 | |
| 72 | struct device *nd_dax_create(struct nd_region *nd_region) |
| 73 | { |
| 74 | struct device *dev = NULL; |
| 75 | struct nd_dax *nd_dax; |
| 76 | |
Dan Williams | c9e582a | 2017-05-29 23:12:19 -0700 | [diff] [blame] | 77 | if (!is_memory(&nd_region->dev)) |
Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame] | 78 | return NULL; |
| 79 | |
| 80 | nd_dax = nd_dax_alloc(nd_region); |
| 81 | if (nd_dax) |
| 82 | dev = nd_pfn_devinit(&nd_dax->nd_pfn, NULL); |
| 83 | __nd_device_register(dev); |
| 84 | return dev; |
| 85 | } |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 86 | |
| 87 | int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns) |
| 88 | { |
| 89 | int rc; |
| 90 | struct nd_dax *nd_dax; |
| 91 | struct device *dax_dev; |
| 92 | struct nd_pfn *nd_pfn; |
| 93 | struct nd_pfn_sb *pfn_sb; |
| 94 | struct nd_region *nd_region = to_nd_region(ndns->dev.parent); |
| 95 | |
| 96 | if (ndns->force_raw) |
| 97 | return -ENODEV; |
| 98 | |
Dan Williams | b3fde74 | 2017-06-04 10:18:39 +0900 | [diff] [blame] | 99 | switch (ndns->claim_class) { |
| 100 | case NVDIMM_CCLASS_NONE: |
| 101 | case NVDIMM_CCLASS_DAX: |
| 102 | break; |
| 103 | default: |
| 104 | return -ENODEV; |
| 105 | } |
| 106 | |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 107 | nvdimm_bus_lock(&ndns->dev); |
| 108 | nd_dax = nd_dax_alloc(nd_region); |
| 109 | nd_pfn = &nd_dax->nd_pfn; |
| 110 | dax_dev = nd_pfn_devinit(nd_pfn, ndns); |
| 111 | nvdimm_bus_unlock(&ndns->dev); |
| 112 | if (!dax_dev) |
| 113 | return -ENOMEM; |
Dan Williams | 7e3e888 | 2019-07-18 15:58:36 -0700 | [diff] [blame] | 114 | pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL); |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 115 | nd_pfn->pfn_sb = pfn_sb; |
| 116 | rc = nd_pfn_validate(nd_pfn, DAX_SIG); |
Dan Williams | 426824d | 2018-03-05 16:39:31 -0800 | [diff] [blame] | 117 | dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>"); |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 118 | if (rc < 0) { |
Dan Williams | 452bae0 | 2017-04-28 22:05:14 -0700 | [diff] [blame] | 119 | nd_detach_ndns(dax_dev, &nd_pfn->ndns); |
Dan Williams | c5ed926 | 2016-05-18 14:50:12 -0700 | [diff] [blame] | 120 | put_device(dax_dev); |
| 121 | } else |
| 122 | __nd_device_register(dax_dev); |
| 123 | |
| 124 | return rc; |
| 125 | } |
| 126 | EXPORT_SYMBOL(nd_dax_probe); |