Dan Williams | cd03412 | 2016-03-11 10:15:36 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright(c) 2013-2016 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/device.h> |
| 14 | #include <linux/sizes.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include "nd-core.h" |
| 18 | #include "nd.h" |
| 19 | |
| 20 | static void nd_dax_release(struct device *dev) |
| 21 | { |
| 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; |
| 25 | |
| 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); |
| 29 | kfree(nd_pfn->uuid); |
| 30 | kfree(nd_dax); |
| 31 | } |
| 32 | |
| 33 | static struct device_type nd_dax_device_type = { |
| 34 | .name = "nd_dax", |
| 35 | .release = nd_dax_release, |
| 36 | }; |
| 37 | |
| 38 | bool is_nd_dax(struct device *dev) |
| 39 | { |
| 40 | return dev ? dev->type == &nd_dax_device_type : false; |
| 41 | } |
| 42 | EXPORT_SYMBOL(is_nd_dax); |
| 43 | |
| 44 | struct nd_dax *to_nd_dax(struct device *dev) |
| 45 | { |
| 46 | struct nd_dax *nd_dax = container_of(dev, struct nd_dax, nd_pfn.dev); |
| 47 | |
| 48 | WARN_ON(!is_nd_dax(dev)); |
| 49 | return nd_dax; |
| 50 | } |
| 51 | EXPORT_SYMBOL(to_nd_dax); |
| 52 | |
| 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, |
| 57 | NULL, |
| 58 | }; |
| 59 | |
| 60 | static struct nd_dax *nd_dax_alloc(struct nd_region *nd_region) |
| 61 | { |
| 62 | struct nd_pfn *nd_pfn; |
| 63 | struct nd_dax *nd_dax; |
| 64 | struct device *dev; |
| 65 | |
| 66 | nd_dax = kzalloc(sizeof(*nd_dax), GFP_KERNEL); |
| 67 | if (!nd_dax) |
| 68 | return NULL; |
| 69 | |
| 70 | nd_pfn = &nd_dax->nd_pfn; |
| 71 | nd_pfn->id = ida_simple_get(&nd_region->dax_ida, 0, 0, GFP_KERNEL); |
| 72 | if (nd_pfn->id < 0) { |
| 73 | kfree(nd_dax); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | dev = &nd_pfn->dev; |
| 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; |
| 82 | |
| 83 | return nd_dax; |
| 84 | } |
| 85 | |
| 86 | struct device *nd_dax_create(struct nd_region *nd_region) |
| 87 | { |
| 88 | struct device *dev = NULL; |
| 89 | struct nd_dax *nd_dax; |
| 90 | |
| 91 | if (!is_nd_pmem(&nd_region->dev)) |
| 92 | return NULL; |
| 93 | |
| 94 | nd_dax = nd_dax_alloc(nd_region); |
| 95 | if (nd_dax) |
| 96 | dev = nd_pfn_devinit(&nd_dax->nd_pfn, NULL); |
| 97 | __nd_device_register(dev); |
| 98 | return dev; |
| 99 | } |