2 * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
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.
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.
13 #include <linux/device.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
20 static void nd_dax_release(struct device *dev)
22 struct nd_region *nd_region = to_nd_region(dev->parent);
23 struct nd_dax *nd_dax = to_nd_dax(dev);
24 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
26 dev_dbg(dev, "%s\n", __func__);
27 nd_detach_ndns(dev, &nd_pfn->ndns);
28 ida_simple_remove(&nd_region->dax_ida, nd_pfn->id);
33 static struct device_type nd_dax_device_type = {
35 .release = nd_dax_release,
38 bool is_nd_dax(struct device *dev)
40 return dev ? dev->type == &nd_dax_device_type : false;
42 EXPORT_SYMBOL(is_nd_dax);
44 struct nd_dax *to_nd_dax(struct device *dev)
46 struct nd_dax *nd_dax = container_of(dev, struct nd_dax, nd_pfn.dev);
48 WARN_ON(!is_nd_dax(dev));
51 EXPORT_SYMBOL(to_nd_dax);
53 static const struct attribute_group *nd_dax_attribute_groups[] = {
54 &nd_pfn_attribute_group,
55 &nd_device_attribute_group,
56 &nd_numa_attribute_group,
60 static struct nd_dax *nd_dax_alloc(struct nd_region *nd_region)
62 struct nd_pfn *nd_pfn;
63 struct nd_dax *nd_dax;
66 nd_dax = kzalloc(sizeof(*nd_dax), GFP_KERNEL);
70 nd_pfn = &nd_dax->nd_pfn;
71 nd_pfn->id = ida_simple_get(&nd_region->dax_ida, 0, 0, GFP_KERNEL);
78 dev_set_name(dev, "dax%d.%d", nd_region->id, nd_pfn->id);
79 dev->groups = nd_dax_attribute_groups;
80 dev->type = &nd_dax_device_type;
81 dev->parent = &nd_region->dev;
86 struct device *nd_dax_create(struct nd_region *nd_region)
88 struct device *dev = NULL;
89 struct nd_dax *nd_dax;
91 if (!is_nd_pmem(&nd_region->dev))
94 nd_dax = nd_dax_alloc(nd_region);
96 dev = nd_pfn_devinit(&nd_dax->nd_pfn, NULL);
97 __nd_device_register(dev);