libnvdimm: control (ioctl) messages for nvdimm_bus and nvdimm devices
[firefly-linux-kernel-4.4.55.git] / drivers / nvdimm / dimm_devs.c
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/device.h>
15 #include <linux/ndctl.h>
16 #include <linux/slab.h>
17 #include <linux/io.h>
18 #include <linux/fs.h>
19 #include <linux/mm.h>
20 #include "nd-core.h"
21
22 static DEFINE_IDA(dimm_ida);
23
24 static void nvdimm_release(struct device *dev)
25 {
26         struct nvdimm *nvdimm = to_nvdimm(dev);
27
28         ida_simple_remove(&dimm_ida, nvdimm->id);
29         kfree(nvdimm);
30 }
31
32 static struct device_type nvdimm_device_type = {
33         .name = "nvdimm",
34         .release = nvdimm_release,
35 };
36
37 bool is_nvdimm(struct device *dev)
38 {
39         return dev->type == &nvdimm_device_type;
40 }
41
42 struct nvdimm *to_nvdimm(struct device *dev)
43 {
44         struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
45
46         WARN_ON(!is_nvdimm(dev));
47         return nvdimm;
48 }
49 EXPORT_SYMBOL_GPL(to_nvdimm);
50
51 const char *nvdimm_name(struct nvdimm *nvdimm)
52 {
53         return dev_name(&nvdimm->dev);
54 }
55 EXPORT_SYMBOL_GPL(nvdimm_name);
56
57 void *nvdimm_provider_data(struct nvdimm *nvdimm)
58 {
59         if (nvdimm)
60                 return nvdimm->provider_data;
61         return NULL;
62 }
63 EXPORT_SYMBOL_GPL(nvdimm_provider_data);
64
65 static ssize_t commands_show(struct device *dev,
66                 struct device_attribute *attr, char *buf)
67 {
68         struct nvdimm *nvdimm = to_nvdimm(dev);
69         int cmd, len = 0;
70
71         if (!nvdimm->dsm_mask)
72                 return sprintf(buf, "\n");
73
74         for_each_set_bit(cmd, nvdimm->dsm_mask, BITS_PER_LONG)
75                 len += sprintf(buf + len, "%s ", nvdimm_cmd_name(cmd));
76         len += sprintf(buf + len, "\n");
77         return len;
78 }
79 static DEVICE_ATTR_RO(commands);
80
81 static struct attribute *nvdimm_attributes[] = {
82         &dev_attr_commands.attr,
83         NULL,
84 };
85
86 struct attribute_group nvdimm_attribute_group = {
87         .attrs = nvdimm_attributes,
88 };
89 EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
90
91 struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
92                 const struct attribute_group **groups, unsigned long flags,
93                 unsigned long *dsm_mask)
94 {
95         struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
96         struct device *dev;
97
98         if (!nvdimm)
99                 return NULL;
100
101         nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
102         if (nvdimm->id < 0) {
103                 kfree(nvdimm);
104                 return NULL;
105         }
106         nvdimm->provider_data = provider_data;
107         nvdimm->flags = flags;
108         nvdimm->dsm_mask = dsm_mask;
109
110         dev = &nvdimm->dev;
111         dev_set_name(dev, "nmem%d", nvdimm->id);
112         dev->parent = &nvdimm_bus->dev;
113         dev->type = &nvdimm_device_type;
114         dev->bus = &nvdimm_bus_type;
115         dev->devt = MKDEV(nvdimm_major, nvdimm->id);
116         dev->groups = groups;
117         if (device_register(dev) != 0) {
118                 put_device(dev);
119                 return NULL;
120         }
121
122         return nvdimm;
123 }
124 EXPORT_SYMBOL_GPL(nvdimm_create);