blob: c3dd7227d1bbf3760631bd073a5fa2dc9e9fded5 [file] [log] [blame]
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001/*
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>
Dan Williams62232e452015-06-08 14:27:06 -040015#include <linux/ndctl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040016#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
22static DEFINE_IDA(dimm_ida);
23
24static 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
32static struct device_type nvdimm_device_type = {
33 .name = "nvdimm",
34 .release = nvdimm_release,
35};
36
Dan Williams62232e452015-06-08 14:27:06 -040037bool is_nvdimm(struct device *dev)
Dan Williamse6dfb2d2015-04-25 03:56:17 -040038{
39 return dev->type == &nvdimm_device_type;
40}
41
42struct 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}
49EXPORT_SYMBOL_GPL(to_nvdimm);
50
51const char *nvdimm_name(struct nvdimm *nvdimm)
52{
53 return dev_name(&nvdimm->dev);
54}
55EXPORT_SYMBOL_GPL(nvdimm_name);
56
57void *nvdimm_provider_data(struct nvdimm *nvdimm)
58{
Dan Williams62232e452015-06-08 14:27:06 -040059 if (nvdimm)
60 return nvdimm->provider_data;
61 return NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -040062}
63EXPORT_SYMBOL_GPL(nvdimm_provider_data);
64
Dan Williams62232e452015-06-08 14:27:06 -040065static 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}
79static DEVICE_ATTR_RO(commands);
80
81static struct attribute *nvdimm_attributes[] = {
82 &dev_attr_commands.attr,
83 NULL,
84};
85
86struct attribute_group nvdimm_attribute_group = {
87 .attrs = nvdimm_attributes,
88};
89EXPORT_SYMBOL_GPL(nvdimm_attribute_group);
90
Dan Williamse6dfb2d2015-04-25 03:56:17 -040091struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
Dan Williams62232e452015-06-08 14:27:06 -040092 const struct attribute_group **groups, unsigned long flags,
93 unsigned long *dsm_mask)
Dan Williamse6dfb2d2015-04-25 03:56:17 -040094{
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;
Dan Williams62232e452015-06-08 14:27:06 -0400108 nvdimm->dsm_mask = dsm_mask;
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400109
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;
Dan Williams62232e452015-06-08 14:27:06 -0400115 dev->devt = MKDEV(nvdimm_major, nvdimm->id);
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400116 dev->groups = groups;
117 if (device_register(dev) != 0) {
118 put_device(dev);
119 return NULL;
120 }
121
122 return nvdimm;
123}
124EXPORT_SYMBOL_GPL(nvdimm_create);