blob: ef957eb37c904b6939c5349abe58f456e915c5cb [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 Williamse6dfb2d2015-04-25 03:56:17 -040021LIST_HEAD(nvdimm_bus_list);
22DEFINE_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
Dan Williamse6dfb2d2015-04-25 03:56:17 -040051struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
52{
53 struct device *dev;
54
55 for (dev = nd_dev; dev; dev = dev->parent)
56 if (dev->release == nvdimm_bus_release)
57 break;
58 dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
59 if (dev)
60 return to_nvdimm_bus(dev);
61 return NULL;
62}
63
Dan Williams45def222015-04-26 19:26:48 -040064static const char *nvdimm_bus_provider(struct nvdimm_bus *nvdimm_bus)
65{
66 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
67 struct device *parent = nvdimm_bus->dev.parent;
68
69 if (nd_desc->provider_name)
70 return nd_desc->provider_name;
71 else if (parent)
72 return dev_name(parent);
73 else
74 return "unknown";
75}
76
77static ssize_t provider_show(struct device *dev,
78 struct device_attribute *attr, char *buf)
79{
80 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
81
82 return sprintf(buf, "%s\n", nvdimm_bus_provider(nvdimm_bus));
83}
84static DEVICE_ATTR_RO(provider);
85
86static struct attribute *nvdimm_bus_attributes[] = {
87 &dev_attr_provider.attr,
88 NULL,
89};
90
91struct attribute_group nvdimm_bus_attribute_group = {
92 .attrs = nvdimm_bus_attributes,
93};
94EXPORT_SYMBOL_GPL(nvdimm_bus_attribute_group);
95
Dan Williamsb94d5232015-05-19 22:54:31 -040096struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
97 struct nvdimm_bus_descriptor *nd_desc)
98{
99 struct nvdimm_bus *nvdimm_bus;
100 int rc;
101
102 nvdimm_bus = kzalloc(sizeof(*nvdimm_bus), GFP_KERNEL);
103 if (!nvdimm_bus)
104 return NULL;
Dan Williams45def222015-04-26 19:26:48 -0400105 INIT_LIST_HEAD(&nvdimm_bus->list);
Dan Williamsb94d5232015-05-19 22:54:31 -0400106 nvdimm_bus->id = ida_simple_get(&nd_ida, 0, 0, GFP_KERNEL);
107 if (nvdimm_bus->id < 0) {
108 kfree(nvdimm_bus);
109 return NULL;
110 }
111 nvdimm_bus->nd_desc = nd_desc;
112 nvdimm_bus->dev.parent = parent;
113 nvdimm_bus->dev.release = nvdimm_bus_release;
Dan Williams45def222015-04-26 19:26:48 -0400114 nvdimm_bus->dev.groups = nd_desc->attr_groups;
Dan Williamsb94d5232015-05-19 22:54:31 -0400115 dev_set_name(&nvdimm_bus->dev, "ndbus%d", nvdimm_bus->id);
116 rc = device_register(&nvdimm_bus->dev);
117 if (rc) {
118 dev_dbg(&nvdimm_bus->dev, "registration failed: %d\n", rc);
Dan Williams45def222015-04-26 19:26:48 -0400119 goto err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400120 }
121
Dan Williams45def222015-04-26 19:26:48 -0400122 rc = nvdimm_bus_create_ndctl(nvdimm_bus);
123 if (rc)
124 goto err;
125
126 mutex_lock(&nvdimm_bus_list_mutex);
127 list_add_tail(&nvdimm_bus->list, &nvdimm_bus_list);
128 mutex_unlock(&nvdimm_bus_list_mutex);
129
Dan Williamsb94d5232015-05-19 22:54:31 -0400130 return nvdimm_bus;
Dan Williams45def222015-04-26 19:26:48 -0400131 err:
132 put_device(&nvdimm_bus->dev);
133 return NULL;
Dan Williamsb94d5232015-05-19 22:54:31 -0400134}
135EXPORT_SYMBOL_GPL(nvdimm_bus_register);
136
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400137static int child_unregister(struct device *dev, void *data)
138{
139 /*
140 * the singular ndctl class device per bus needs to be
141 * "device_destroy"ed, so skip it here
142 *
143 * i.e. remove classless children
144 */
145 if (dev->class)
146 /* pass */;
147 else
148 device_unregister(dev);
149 return 0;
150}
151
Dan Williamsb94d5232015-05-19 22:54:31 -0400152void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus)
153{
154 if (!nvdimm_bus)
155 return;
Dan Williams45def222015-04-26 19:26:48 -0400156
157 mutex_lock(&nvdimm_bus_list_mutex);
158 list_del_init(&nvdimm_bus->list);
159 mutex_unlock(&nvdimm_bus_list_mutex);
160
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400161 device_for_each_child(&nvdimm_bus->dev, NULL, child_unregister);
Dan Williams45def222015-04-26 19:26:48 -0400162 nvdimm_bus_destroy_ndctl(nvdimm_bus);
163
Dan Williamsb94d5232015-05-19 22:54:31 -0400164 device_unregister(&nvdimm_bus->dev);
165}
166EXPORT_SYMBOL_GPL(nvdimm_bus_unregister);
167
Dan Williams45def222015-04-26 19:26:48 -0400168static __init int libnvdimm_init(void)
169{
170 return nvdimm_bus_init();
171}
172
173static __exit void libnvdimm_exit(void)
174{
175 WARN_ON(!list_empty(&nvdimm_bus_list));
176 nvdimm_bus_exit();
177}
178
Dan Williamsb94d5232015-05-19 22:54:31 -0400179MODULE_LICENSE("GPL v2");
180MODULE_AUTHOR("Intel Corporation");
Dan Williams45def222015-04-26 19:26:48 -0400181subsys_initcall(libnvdimm_init);
182module_exit(libnvdimm_exit);