blob: 69aae2cbd45f1e1fa807ed0ad1f36edfcdd838cd [file] [log] [blame]
Dan Williams51cf7842017-07-12 17:58:21 -07001// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
Dan Williams89ec9f22018-10-29 15:52:42 -07003#include <linux/memremap.h>
Dan Williams51cf7842017-07-12 17:58:21 -07004#include <linux/device.h>
5#include <linux/slab.h>
6#include <linux/dax.h>
7#include "dax-private.h"
8#include "bus.h"
9
Dan Williams9567da02017-07-12 17:58:21 -070010static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
11{
12 /*
13 * We only ever expect to handle device-dax instances, i.e. the
14 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
15 */
16 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
17}
18
19static int dax_bus_match(struct device *dev, struct device_driver *drv);
20
21static struct bus_type dax_bus_type = {
22 .name = "dax",
23 .uevent = dax_bus_uevent,
24 .match = dax_bus_match,
25};
26
27static int dax_bus_match(struct device *dev, struct device_driver *drv)
28{
29 /*
30 * The drivers that can register on the 'dax' bus are private to
31 * drivers/dax/ so any device and driver on the bus always
32 * match.
33 */
34 return 1;
35}
36
Dan Williams51cf7842017-07-12 17:58:21 -070037/*
38 * Rely on the fact that drvdata is set before the attributes are
39 * registered, and that the attributes are unregistered before drvdata
40 * is cleared to assume that drvdata is always valid.
41 */
42static ssize_t id_show(struct device *dev,
43 struct device_attribute *attr, char *buf)
44{
45 struct dax_region *dax_region = dev_get_drvdata(dev);
46
47 return sprintf(buf, "%d\n", dax_region->id);
48}
49static DEVICE_ATTR_RO(id);
50
51static ssize_t region_size_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
53{
54 struct dax_region *dax_region = dev_get_drvdata(dev);
55
56 return sprintf(buf, "%llu\n", (unsigned long long)
57 resource_size(&dax_region->res));
58}
59static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
60 region_size_show, NULL);
61
62static ssize_t align_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
64{
65 struct dax_region *dax_region = dev_get_drvdata(dev);
66
67 return sprintf(buf, "%u\n", dax_region->align);
68}
69static DEVICE_ATTR_RO(align);
70
71static struct attribute *dax_region_attributes[] = {
72 &dev_attr_region_size.attr,
73 &dev_attr_align.attr,
74 &dev_attr_id.attr,
75 NULL,
76};
77
78static const struct attribute_group dax_region_attribute_group = {
79 .name = "dax_region",
80 .attrs = dax_region_attributes,
81};
82
83static const struct attribute_group *dax_region_attribute_groups[] = {
84 &dax_region_attribute_group,
85 NULL,
86};
87
88static void dax_region_free(struct kref *kref)
89{
90 struct dax_region *dax_region;
91
92 dax_region = container_of(kref, struct dax_region, kref);
93 kfree(dax_region);
94}
95
96void dax_region_put(struct dax_region *dax_region)
97{
98 kref_put(&dax_region->kref, dax_region_free);
99}
100EXPORT_SYMBOL_GPL(dax_region_put);
101
102static void dax_region_unregister(void *region)
103{
104 struct dax_region *dax_region = region;
105
106 sysfs_remove_groups(&dax_region->dev->kobj,
107 dax_region_attribute_groups);
108 dax_region_put(dax_region);
109}
110
111struct dax_region *alloc_dax_region(struct device *parent, int region_id,
112 struct resource *res, unsigned int align,
113 unsigned long pfn_flags)
114{
115 struct dax_region *dax_region;
116
117 /*
118 * The DAX core assumes that it can store its private data in
119 * parent->driver_data. This WARN is a reminder / safeguard for
120 * developers of device-dax drivers.
121 */
122 if (dev_get_drvdata(parent)) {
123 dev_WARN(parent, "dax core failed to setup private data\n");
124 return NULL;
125 }
126
127 if (!IS_ALIGNED(res->start, align)
128 || !IS_ALIGNED(resource_size(res), align))
129 return NULL;
130
131 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
132 if (!dax_region)
133 return NULL;
134
135 dev_set_drvdata(parent, dax_region);
136 memcpy(&dax_region->res, res, sizeof(*res));
137 dax_region->pfn_flags = pfn_flags;
138 kref_init(&dax_region->kref);
139 dax_region->id = region_id;
140 dax_region->align = align;
141 dax_region->dev = parent;
142 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
143 kfree(dax_region);
144 return NULL;
145 }
146
147 kref_get(&dax_region->kref);
148 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
149 return NULL;
150 return dax_region;
151}
152EXPORT_SYMBOL_GPL(alloc_dax_region);
153
154static ssize_t size_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
157 struct dev_dax *dev_dax = to_dev_dax(dev);
158 unsigned long long size = resource_size(&dev_dax->region->res);
159
160 return sprintf(buf, "%llu\n", size);
161}
162static DEVICE_ATTR_RO(size);
163
164static struct attribute *dev_dax_attributes[] = {
165 &dev_attr_size.attr,
166 NULL,
167};
168
169static const struct attribute_group dev_dax_attribute_group = {
170 .attrs = dev_dax_attributes,
171};
172
Dan Williams9567da02017-07-12 17:58:21 -0700173static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams51cf7842017-07-12 17:58:21 -0700174 &dev_dax_attribute_group,
175 NULL,
176};
Dan Williams51cf7842017-07-12 17:58:21 -0700177
178void kill_dev_dax(struct dev_dax *dev_dax)
179{
180 struct dax_device *dax_dev = dev_dax->dax_dev;
181 struct inode *inode = dax_inode(dax_dev);
182
183 kill_dax(dax_dev);
184 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
185}
186EXPORT_SYMBOL_GPL(kill_dev_dax);
187
Dan Williams9567da02017-07-12 17:58:21 -0700188static void dev_dax_release(struct device *dev)
Dan Williams51cf7842017-07-12 17:58:21 -0700189{
190 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams9567da02017-07-12 17:58:21 -0700191 struct dax_region *dax_region = dev_dax->region;
Dan Williams51cf7842017-07-12 17:58:21 -0700192 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams51cf7842017-07-12 17:58:21 -0700193
Dan Williams9567da02017-07-12 17:58:21 -0700194 dax_region_put(dax_region);
195 put_dax(dax_dev);
196 kfree(dev_dax);
197}
198
199static void unregister_dev_dax(void *dev)
200{
201 struct dev_dax *dev_dax = to_dev_dax(dev);
202
203 dev_dbg(dev, "%s\n", __func__);
Dan Williams51cf7842017-07-12 17:58:21 -0700204
205 kill_dev_dax(dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -0700206 device_del(dev);
Dan Williams51cf7842017-07-12 17:58:21 -0700207 put_device(dev);
208}
Dan Williams9567da02017-07-12 17:58:21 -0700209
Dan Williams89ec9f22018-10-29 15:52:42 -0700210struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id,
211 struct dev_pagemap *pgmap)
Dan Williams9567da02017-07-12 17:58:21 -0700212{
213 struct device *parent = dax_region->dev;
214 struct dax_device *dax_dev;
215 struct dev_dax *dev_dax;
216 struct inode *inode;
217 struct device *dev;
218 int rc = -ENOMEM;
219
220 if (id < 0)
221 return ERR_PTR(-EINVAL);
222
223 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
224 if (!dev_dax)
225 return ERR_PTR(-ENOMEM);
226
Dan Williams89ec9f22018-10-29 15:52:42 -0700227 memcpy(&dev_dax->pgmap, pgmap, sizeof(*pgmap));
228
Dan Williams9567da02017-07-12 17:58:21 -0700229 /*
230 * No 'host' or dax_operations since there is no access to this
231 * device outside of mmap of the resulting character device.
232 */
233 dax_dev = alloc_dax(dev_dax, NULL, NULL);
234 if (!dax_dev)
235 goto err;
236
237 /* a device_dax instance is dead while the driver is not attached */
238 kill_dax(dax_dev);
239
240 /* from here on we're committed to teardown via dax_dev_release() */
241 dev = &dev_dax->dev;
242 device_initialize(dev);
243
244 dev_dax->dax_dev = dax_dev;
245 dev_dax->region = dax_region;
246 kref_get(&dax_region->kref);
247
248 inode = dax_inode(dax_dev);
249 dev->devt = inode->i_rdev;
250 dev->bus = &dax_bus_type;
251 dev->parent = parent;
252 dev->groups = dax_attribute_groups;
253 dev->release = dev_dax_release;
254 dev_set_name(dev, "dax%d.%d", dax_region->id, id);
255
256 rc = device_add(dev);
257 if (rc) {
258 kill_dev_dax(dev_dax);
259 put_device(dev);
260 return ERR_PTR(rc);
261 }
262
263 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
264 if (rc)
265 return ERR_PTR(rc);
266
267 return dev_dax;
268
269 err:
270 kfree(dev_dax);
271
272 return ERR_PTR(rc);
273}
274EXPORT_SYMBOL_GPL(devm_create_dev_dax);
275
276int __dax_driver_register(struct device_driver *drv,
277 struct module *module, const char *mod_name)
278{
279 drv->owner = module;
280 drv->name = mod_name;
281 drv->mod_name = mod_name;
282 drv->bus = &dax_bus_type;
283 return driver_register(drv);
284}
285EXPORT_SYMBOL_GPL(__dax_driver_register);
286
287int __init dax_bus_init(void)
288{
289 return bus_register(&dax_bus_type);
290}
291
292void __exit dax_bus_exit(void)
293{
294 bus_unregister(&dax_bus_type);
295}