blob: fa7ab5ad03182a5eb9ffa061fb46b8919736bd7b [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>
Dan Williams45def222015-04-26 19:26:48 -040017#include <linux/mutex.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040018#include <linux/slab.h>
19#include "nd-core.h"
20
Dan Williams45def222015-04-26 19:26:48 -040021static LIST_HEAD(nvdimm_bus_list);
22static DEFINE_MUTEX(nvdimm_bus_list_mutex);
Dan Williamsb94d5232015-05-19 22:54:31 -040023static DEFINE_IDA(nd_ida);
24
25static void nvdimm_bus_release(struct device *dev)
26{
27 struct nvdimm_bus *nvdimm_bus;
28
29 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
30 ida_simple_remove(&nd_ida, nvdimm_bus->id);
31 kfree(nvdimm_bus);
32}
33
Dan Williams45def222015-04-26 19:26:48 -040034struct nvdimm_bus *to_nvdimm_bus(struct device *dev)
35{
36 struct nvdimm_bus *nvdimm_bus;
37
38 nvdimm_bus = container_of(dev, struct nvdimm_bus, dev);
39 WARN_ON(nvdimm_bus->dev.release != nvdimm_bus_release);
40 return nvdimm_bus;
41}
42EXPORT_SYMBOL_GPL(to_nvdimm_bus);
43
44struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus)
45{
46 /* struct nvdimm_bus definition is private to libnvdimm */
47 return nvdimm_bus->nd_desc;
48}
49EXPORT_SYMBOL_GPL(to_nd_desc);
50
51static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
52{
53 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
54 struct device *parent = nvdimm_bus->dev.parent;
55
56 if (nd_desc->provider_name)
57 return nd_desc->provider_name;
58 else if (parent)
59 return dev_name(parent);
60 else
61 return "unknown";
62}
63
64static ssize_t provider_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
66{
67 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
68
69 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
70}
71static DEVICE_ATTR_RO(provider);
72
73static struct attribute *nvdimm_bus_attributes[] = {
74 &dev_attr_provider.attr,
75 NULL,
76};
77
78struct attribute_group nvdimm_bus_attribute_group = {
79 .attrs = nvdimm_bus_attributes,
80};
81EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
82
Dan Williamsb94d5232015-05-19 22:54:31 -040083struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
84 struct nvdimm_bus_descriptor *nd_desc)
85{
86 struct nvdimm_bus *nvdimm_bus;
87 int rc;
88
89 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
90 if (!nvdimm_bus)
91 return NULL;
Dan Williams45def222015-04-26 19:26:48 -040092 INIT_LIST_HEAD(&nvdimm_bus->list);
Dan Williamsb94d5232015-05-19 22:54:31 -040093 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
94 if (nvdimm_bus->id < 0) {
95 kfree(nvdimm_bus);
96 return NULL;
97 }
98 nvdimm_bus->nd_desc = nd_desc;
99 nvdimm_bus->dev.parent = parent;
100 nvdimm_bus->dev.release = nvdimm_bus_release;
Dan Williams45def222015-04-26 19:26:48 -0400101 nvdimm_bus->dev.groups = nd_desc->attr_groups;
Dan Williamsb94d5232015-05-19 22:54:31 -0400102 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
103 rc = device_register(&nvdimm_bus->dev);
104 if (rc) {
105 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
Dan Williams45def222015-04-26 19:26:48 -0400106 goto err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400107 }
108
Dan Williams45def222015-04-26 19:26:48 -0400109 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
110 if (rc)
111 goto err;
112
113 mutex_lock(&nvdimm_bus_list_mutex);
114 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
115 mutex_unlock(&nvdimm_bus_list_mutex);
116
Dan Williamsb94d5232015-05-19 22:54:31 -0400117 return nvdimm_bus;
Dan Williams45def222015-04-26 19:26:48 -0400118 err:
119 put_device(&nvdimm_bus->dev);
120 return NULL;
Dan Williamsb94d5232015-05-19 22:54:31 -0400121}
122EXPORT_SYMBOL_GPL(nvdimm_bus_register);
123
124void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
125{
126 if (!nvdimm_bus)
127 return;
Dan Williams45def222015-04-26 19:26:48 -0400128
129 mutex_lock(&nvdimm_bus_list_mutex);
130 list_del_init(&nvdimm_bus->list);
131 mutex_unlock(&nvdimm_bus_list_mutex);
132
133 nvdimm_bus_destroy_ndctl(nvdimm_bus);
134
Dan Williamsb94d5232015-05-19 22:54:31 -0400135 device_unregister(&nvdimm_bus->dev);
136}
137EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
138
Dan Williams45def222015-04-26 19:26:48 -0400139static __init int libnvdimm_init(void)
140{
141 return nvdimm_bus_init();
142}
143
144static __exit void libnvdimm_exit(void)
145{
146 WARN_ON(!list_empty(&nvdimm_bus_list));
147 nvdimm_bus_exit();
148}
149
Dan Williamsb94d5232015-05-19 22:54:31 -0400150MODULE_LICENSE("GPL v2");
151MODULE_AUTHOR("Intel Corporation");
Dan Williams45def222015-04-26 19:26:48 -0400152subsys_initcall(libnvdimm_init);
153module_exit(libnvdimm_exit);