2 * Copyright(c) 2013-2015 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/device.h>
15 #include <linux/ndctl.h>
16 #include <linux/slab.h>
22 static DEFINE_IDA(dimm_ida);
24 static void nvdimm_release(struct device *dev)
26 struct nvdimm *nvdimm = to_nvdimm(dev);
28 ida_simple_remove(&dimm_ida, nvdimm->id);
32 static struct device_type nvdimm_device_type = {
34 .release = nvdimm_release,
37 bool is_nvdimm(struct device *dev)
39 return dev->type == &nvdimm_device_type;
42 struct nvdimm *to_nvdimm(struct device *dev)
44 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
46 WARN_ON(!is_nvdimm(dev));
49 EXPORT_SYMBOL_GPL(to_nvdimm);
51 const char *nvdimm_name(struct nvdimm *nvdimm)
53 return dev_name(&nvdimm->dev);
55 EXPORT_SYMBOL_GPL(nvdimm_name);
57 void *nvdimm_provider_data(struct nvdimm *nvdimm)
60 return nvdimm->provider_data;
63 EXPORT_SYMBOL_GPL(nvdimm_provider_data);
65 static ssize_t commands_show(struct device *dev,
66 struct device_attribute *attr, char *buf)
68 struct nvdimm *nvdimm = to_nvdimm(dev);
71 if (!nvdimm->dsm_mask)
72 return sprintf(buf, "\n");
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");
79 static DEVICE_ATTR_RO(commands);
81 static struct attribute *nvdimm_attributes[] = {
82 &dev_attr_commands.attr,
86 struct attribute_group nvdimm_attribute_group = {
87 .attrs = nvdimm_attributes,
89 EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
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)
95 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
101 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
102 if (nvdimm->id < 0) {
106 nvdimm->provider_data = provider_data;
107 nvdimm->flags = flags;
108 nvdimm->dsm_mask = dsm_mask;
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) {
124 EXPORT_SYMBOL_GPL(nvdimm_create);