libnvdimm, nfit: dimm/memory-devices
[deliverable/linux.git] / drivers / nvdimm / dimm_devs.c
CommitLineData
e6dfb2de
DW
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/slab.h>
16#include <linux/io.h>
17#include <linux/fs.h>
18#include <linux/mm.h>
19#include "nd-core.h"
20
21static DEFINE_IDA(dimm_ida);
22
23static void nvdimm_release(struct device *dev)
24{
25 struct nvdimm *nvdimm = to_nvdimm(dev);
26
27 ida_simple_remove(&dimm_ida, nvdimm->id);
28 kfree(nvdimm);
29}
30
31static struct device_type nvdimm_device_type = {
32 .name = "nvdimm",
33 .release = nvdimm_release,
34};
35
36static bool is_nvdimm(struct device *dev)
37{
38 return dev->type == &nvdimm_device_type;
39}
40
41struct nvdimm *to_nvdimm(struct device *dev)
42{
43 struct nvdimm *nvdimm = container_of(dev, struct nvdimm, dev);
44
45 WARN_ON(!is_nvdimm(dev));
46 return nvdimm;
47}
48EXPORT_SYMBOL_GPL(to_nvdimm);
49
50const char *nvdimm_name(struct nvdimm *nvdimm)
51{
52 return dev_name(&nvdimm->dev);
53}
54EXPORT_SYMBOL_GPL(nvdimm_name);
55
56void *nvdimm_provider_data(struct nvdimm *nvdimm)
57{
58 return nvdimm->provider_data;
59}
60EXPORT_SYMBOL_GPL(nvdimm_provider_data);
61
62struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
63 const struct attribute_group **groups, unsigned long flags)
64{
65 struct nvdimm *nvdimm = kzalloc(sizeof(*nvdimm), GFP_KERNEL);
66 struct device *dev;
67
68 if (!nvdimm)
69 return NULL;
70
71 nvdimm->id = ida_simple_get(&dimm_ida, 0, 0, GFP_KERNEL);
72 if (nvdimm->id < 0) {
73 kfree(nvdimm);
74 return NULL;
75 }
76 nvdimm->provider_data = provider_data;
77 nvdimm->flags = flags;
78
79 dev = &nvdimm->dev;
80 dev_set_name(dev, "nmem%d", nvdimm->id);
81 dev->parent = &nvdimm_bus->dev;
82 dev->type = &nvdimm_device_type;
83 dev->bus = &nvdimm_bus_type;
84 dev->groups = groups;
85 if (device_register(dev) != 0) {
86 put_device(dev);
87 return NULL;
88 }
89
90 return nvdimm;
91}
92EXPORT_SYMBOL_GPL(nvdimm_create);
This page took 0.030684 seconds and 5 git commands to generate.