blob: c578a49867ace1699274cbf5cff5556c581243cc [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -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#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
20static DEFINE_IDA(nd_ida);
21
22static 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
31struct 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}
58EXPORT_SYMBOL_GPL(nvdimm_bus_register);
59
60void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
61{
62 if (!nvdimm_bus)
63 return;
64 device_unregister(&nvdimm_bus->dev);
65}
66EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
67
68MODULE_LICENSE("GPL v2");
69MODULE_AUTHOR("Intel Corporation");