libnvdimm, nfit: initial libnvdimm infrastructure and NFIT support
[firefly-linux-kernel-4.4.55.git] / drivers / nvdimm / core.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 #include <linux/libnvdimm.h>
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include "nd-core.h"
19
20 static DEFINE_IDA(nd_ida);
21
22 static void nvdimm_bus_release(struct device *dev)
23 {
24         struct nvdimm_bus *nvdimm_bus;
25
26         nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
27         ida_simple_remove(&nd_ida, nvdimm_bus->id);
28         kfree(nvdimm_bus);
29 }
30
31 struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
32                 struct nvdimm_bus_descriptor *nd_desc)
33 {
34         struct nvdimm_bus *nvdimm_bus;
35         int rc;
36
37         nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
38         if (!nvdimm_bus)
39                 return NULL;
40         nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
41         if (nvdimm_bus->id < 0) {
42                 kfree(nvdimm_bus);
43                 return NULL;
44         }
45         nvdimm_bus->nd_desc = nd_desc;
46         nvdimm_bus->dev.parent = parent;
47         nvdimm_bus->dev.release = nvdimm_bus_release;
48         dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
49         rc = device_register(&nvdimm_bus->dev);
50         if (rc) {
51                 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
52                 put_device(&nvdimm_bus->dev);
53                 return NULL;
54         }
55
56         return nvdimm_bus;
57 }
58 EXPORT_SYMBOL_GPL(nvdimm_bus_register);
59
60 void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
61 {
62         if (!nvdimm_bus)
63                 return;
64         device_unregister(&nvdimm_bus->dev);
65 }
66 EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
67
68 MODULE_LICENSE("GPL v2");
69 MODULE_AUTHOR("Intel Corporation");