xref: /openbmc/linux/drivers/dax/bus.c (revision d200781ef237a354d918ceff5cee350d88a93d42)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
3 #include <linux/memremap.h>
4 #include <linux/device.h>
5 #include <linux/mutex.h>
6 #include <linux/list.h>
7 #include <linux/slab.h>
8 #include <linux/dax.h>
9 #include "dax-private.h"
10 #include "bus.h"
11 
12 static DEFINE_MUTEX(dax_bus_lock);
13 
14 #define DAX_NAME_LEN 30
15 struct dax_id {
16 	struct list_head list;
17 	char dev_name[DAX_NAME_LEN];
18 };
19 
20 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
21 {
22 	/*
23 	 * We only ever expect to handle device-dax instances, i.e. the
24 	 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
25 	 */
26 	return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
27 }
28 
29 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
30 {
31 	return container_of(drv, struct dax_device_driver, drv);
32 }
33 
34 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
35 		const char *dev_name)
36 {
37 	struct dax_id *dax_id;
38 
39 	lockdep_assert_held(&dax_bus_lock);
40 
41 	list_for_each_entry(dax_id, &dax_drv->ids, list)
42 		if (sysfs_streq(dax_id->dev_name, dev_name))
43 			return dax_id;
44 	return NULL;
45 }
46 
47 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
48 {
49 	int match;
50 
51 	mutex_lock(&dax_bus_lock);
52 	match = !!__dax_match_id(dax_drv, dev_name(dev));
53 	mutex_unlock(&dax_bus_lock);
54 
55 	return match;
56 }
57 
58 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
59 		size_t count, bool add)
60 {
61 	struct dax_device_driver *dax_drv = to_dax_drv(drv);
62 	unsigned int region_id, id;
63 	char devname[DAX_NAME_LEN];
64 	struct dax_id *dax_id;
65 	ssize_t rc = count;
66 	int fields;
67 
68 	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
69 	if (fields != 2)
70 		return -EINVAL;
71 	sprintf(devname, "dax%d.%d", region_id, id);
72 	if (!sysfs_streq(buf, devname))
73 		return -EINVAL;
74 
75 	mutex_lock(&dax_bus_lock);
76 	dax_id = __dax_match_id(dax_drv, buf);
77 	if (!dax_id) {
78 		if (add) {
79 			dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
80 			if (dax_id) {
81 				strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
82 				list_add(&dax_id->list, &dax_drv->ids);
83 			} else
84 				rc = -ENOMEM;
85 		} else
86 			/* nothing to remove */;
87 	} else if (!add) {
88 		list_del(&dax_id->list);
89 		kfree(dax_id);
90 	} else
91 		/* dax_id already added */;
92 	mutex_unlock(&dax_bus_lock);
93 	return rc;
94 }
95 
96 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
97 		size_t count)
98 {
99 	return do_id_store(drv, buf, count, true);
100 }
101 static DRIVER_ATTR_WO(new_id);
102 
103 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
104 		size_t count)
105 {
106 	return do_id_store(drv, buf, count, false);
107 }
108 static DRIVER_ATTR_WO(remove_id);
109 
110 static struct attribute *dax_drv_attrs[] = {
111 	&driver_attr_new_id.attr,
112 	&driver_attr_remove_id.attr,
113 	NULL,
114 };
115 ATTRIBUTE_GROUPS(dax_drv);
116 
117 static int dax_bus_match(struct device *dev, struct device_driver *drv);
118 
119 static struct bus_type dax_bus_type = {
120 	.name = "dax",
121 	.uevent = dax_bus_uevent,
122 	.match = dax_bus_match,
123 	.drv_groups = dax_drv_groups,
124 };
125 
126 static int dax_bus_match(struct device *dev, struct device_driver *drv)
127 {
128 	struct dax_device_driver *dax_drv = to_dax_drv(drv);
129 
130 	/*
131 	 * All but the 'device-dax' driver, which has 'match_always'
132 	 * set, requires an exact id match.
133 	 */
134 	if (dax_drv->match_always)
135 		return 1;
136 
137 	return dax_match_id(dax_drv, dev);
138 }
139 
140 /*
141  * Rely on the fact that drvdata is set before the attributes are
142  * registered, and that the attributes are unregistered before drvdata
143  * is cleared to assume that drvdata is always valid.
144  */
145 static ssize_t id_show(struct device *dev,
146 		struct device_attribute *attr, char *buf)
147 {
148 	struct dax_region *dax_region = dev_get_drvdata(dev);
149 
150 	return sprintf(buf, "%d\n", dax_region->id);
151 }
152 static DEVICE_ATTR_RO(id);
153 
154 static ssize_t region_size_show(struct device *dev,
155 		struct device_attribute *attr, char *buf)
156 {
157 	struct dax_region *dax_region = dev_get_drvdata(dev);
158 
159 	return sprintf(buf, "%llu\n", (unsigned long long)
160 			resource_size(&dax_region->res));
161 }
162 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
163 		region_size_show, NULL);
164 
165 static ssize_t align_show(struct device *dev,
166 		struct device_attribute *attr, char *buf)
167 {
168 	struct dax_region *dax_region = dev_get_drvdata(dev);
169 
170 	return sprintf(buf, "%u\n", dax_region->align);
171 }
172 static DEVICE_ATTR_RO(align);
173 
174 static struct attribute *dax_region_attributes[] = {
175 	&dev_attr_region_size.attr,
176 	&dev_attr_align.attr,
177 	&dev_attr_id.attr,
178 	NULL,
179 };
180 
181 static const struct attribute_group dax_region_attribute_group = {
182 	.name = "dax_region",
183 	.attrs = dax_region_attributes,
184 };
185 
186 static const struct attribute_group *dax_region_attribute_groups[] = {
187 	&dax_region_attribute_group,
188 	NULL,
189 };
190 
191 static void dax_region_free(struct kref *kref)
192 {
193 	struct dax_region *dax_region;
194 
195 	dax_region = container_of(kref, struct dax_region, kref);
196 	kfree(dax_region);
197 }
198 
199 void dax_region_put(struct dax_region *dax_region)
200 {
201 	kref_put(&dax_region->kref, dax_region_free);
202 }
203 EXPORT_SYMBOL_GPL(dax_region_put);
204 
205 static void dax_region_unregister(void *region)
206 {
207 	struct dax_region *dax_region = region;
208 
209 	sysfs_remove_groups(&dax_region->dev->kobj,
210 			dax_region_attribute_groups);
211 	dax_region_put(dax_region);
212 }
213 
214 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
215 		struct resource *res, unsigned int align,
216 		unsigned long pfn_flags)
217 {
218 	struct dax_region *dax_region;
219 
220 	/*
221 	 * The DAX core assumes that it can store its private data in
222 	 * parent->driver_data. This WARN is a reminder / safeguard for
223 	 * developers of device-dax drivers.
224 	 */
225 	if (dev_get_drvdata(parent)) {
226 		dev_WARN(parent, "dax core failed to setup private data\n");
227 		return NULL;
228 	}
229 
230 	if (!IS_ALIGNED(res->start, align)
231 			|| !IS_ALIGNED(resource_size(res), align))
232 		return NULL;
233 
234 	dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
235 	if (!dax_region)
236 		return NULL;
237 
238 	dev_set_drvdata(parent, dax_region);
239 	memcpy(&dax_region->res, res, sizeof(*res));
240 	dax_region->pfn_flags = pfn_flags;
241 	kref_init(&dax_region->kref);
242 	dax_region->id = region_id;
243 	dax_region->align = align;
244 	dax_region->dev = parent;
245 	if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
246 		kfree(dax_region);
247 		return NULL;
248 	}
249 
250 	kref_get(&dax_region->kref);
251 	if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
252 		return NULL;
253 	return dax_region;
254 }
255 EXPORT_SYMBOL_GPL(alloc_dax_region);
256 
257 static ssize_t size_show(struct device *dev,
258 		struct device_attribute *attr, char *buf)
259 {
260 	struct dev_dax *dev_dax = to_dev_dax(dev);
261 	unsigned long long size = resource_size(&dev_dax->region->res);
262 
263 	return sprintf(buf, "%llu\n", size);
264 }
265 static DEVICE_ATTR_RO(size);
266 
267 static struct attribute *dev_dax_attributes[] = {
268 	&dev_attr_size.attr,
269 	NULL,
270 };
271 
272 static const struct attribute_group dev_dax_attribute_group = {
273 	.attrs = dev_dax_attributes,
274 };
275 
276 static const struct attribute_group *dax_attribute_groups[] = {
277 	&dev_dax_attribute_group,
278 	NULL,
279 };
280 
281 void kill_dev_dax(struct dev_dax *dev_dax)
282 {
283 	struct dax_device *dax_dev = dev_dax->dax_dev;
284 	struct inode *inode = dax_inode(dax_dev);
285 
286 	kill_dax(dax_dev);
287 	unmap_mapping_range(inode->i_mapping, 0, 0, 1);
288 }
289 EXPORT_SYMBOL_GPL(kill_dev_dax);
290 
291 static void dev_dax_release(struct device *dev)
292 {
293 	struct dev_dax *dev_dax = to_dev_dax(dev);
294 	struct dax_region *dax_region = dev_dax->region;
295 	struct dax_device *dax_dev = dev_dax->dax_dev;
296 
297 	dax_region_put(dax_region);
298 	put_dax(dax_dev);
299 	kfree(dev_dax);
300 }
301 
302 static void unregister_dev_dax(void *dev)
303 {
304 	struct dev_dax *dev_dax = to_dev_dax(dev);
305 
306 	dev_dbg(dev, "%s\n", __func__);
307 
308 	kill_dev_dax(dev_dax);
309 	device_del(dev);
310 	put_device(dev);
311 }
312 
313 struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id,
314 		struct dev_pagemap *pgmap)
315 {
316 	struct device *parent = dax_region->dev;
317 	struct dax_device *dax_dev;
318 	struct dev_dax *dev_dax;
319 	struct inode *inode;
320 	struct device *dev;
321 	int rc = -ENOMEM;
322 
323 	if (id < 0)
324 		return ERR_PTR(-EINVAL);
325 
326 	dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
327 	if (!dev_dax)
328 		return ERR_PTR(-ENOMEM);
329 
330 	memcpy(&dev_dax->pgmap, pgmap, sizeof(*pgmap));
331 
332 	/*
333 	 * No 'host' or dax_operations since there is no access to this
334 	 * device outside of mmap of the resulting character device.
335 	 */
336 	dax_dev = alloc_dax(dev_dax, NULL, NULL);
337 	if (!dax_dev)
338 		goto err;
339 
340 	/* a device_dax instance is dead while the driver is not attached */
341 	kill_dax(dax_dev);
342 
343 	/* from here on we're committed to teardown via dax_dev_release() */
344 	dev = &dev_dax->dev;
345 	device_initialize(dev);
346 
347 	dev_dax->dax_dev = dax_dev;
348 	dev_dax->region = dax_region;
349 	kref_get(&dax_region->kref);
350 
351 	inode = dax_inode(dax_dev);
352 	dev->devt = inode->i_rdev;
353 	dev->bus = &dax_bus_type;
354 	dev->parent = parent;
355 	dev->groups = dax_attribute_groups;
356 	dev->release = dev_dax_release;
357 	dev_set_name(dev, "dax%d.%d", dax_region->id, id);
358 
359 	rc = device_add(dev);
360 	if (rc) {
361 		kill_dev_dax(dev_dax);
362 		put_device(dev);
363 		return ERR_PTR(rc);
364 	}
365 
366 	rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
367 	if (rc)
368 		return ERR_PTR(rc);
369 
370 	return dev_dax;
371 
372  err:
373 	kfree(dev_dax);
374 
375 	return ERR_PTR(rc);
376 }
377 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
378 
379 static int match_always_count;
380 
381 int __dax_driver_register(struct dax_device_driver *dax_drv,
382 		struct module *module, const char *mod_name)
383 {
384 	struct device_driver *drv = &dax_drv->drv;
385 	int rc = 0;
386 
387 	INIT_LIST_HEAD(&dax_drv->ids);
388 	drv->owner = module;
389 	drv->name = mod_name;
390 	drv->mod_name = mod_name;
391 	drv->bus = &dax_bus_type;
392 
393 	/* there can only be one default driver */
394 	mutex_lock(&dax_bus_lock);
395 	match_always_count += dax_drv->match_always;
396 	if (match_always_count > 1) {
397 		match_always_count--;
398 		WARN_ON(1);
399 		rc = -EINVAL;
400 	}
401 	mutex_unlock(&dax_bus_lock);
402 	if (rc)
403 		return rc;
404 	return driver_register(drv);
405 }
406 EXPORT_SYMBOL_GPL(__dax_driver_register);
407 
408 void dax_driver_unregister(struct dax_device_driver *dax_drv)
409 {
410 	struct dax_id *dax_id, *_id;
411 
412 	mutex_lock(&dax_bus_lock);
413 	match_always_count -= dax_drv->match_always;
414 	list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
415 		list_del(&dax_id->list);
416 		kfree(dax_id);
417 	}
418 	mutex_unlock(&dax_bus_lock);
419 }
420 EXPORT_SYMBOL_GPL(dax_driver_unregister);
421 
422 int __init dax_bus_init(void)
423 {
424 	return bus_register(&dax_bus_type);
425 }
426 
427 void __exit dax_bus_exit(void)
428 {
429 	bus_unregister(&dax_bus_type);
430 }
431