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>
9 #include "dax-private.h"
12 static struct class *dax_class;
14 static DEFINE_MUTEX(dax_bus_lock);
16 #define DAX_NAME_LEN 30
18 struct list_head list;
19 char dev_name[DAX_NAME_LEN];
22 static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
25 * We only ever expect to handle device-dax instances, i.e. the
26 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
28 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
31 static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
33 return container_of(drv, struct dax_device_driver, drv);
36 static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
39 struct dax_id *dax_id;
41 lockdep_assert_held(&dax_bus_lock);
43 list_for_each_entry(dax_id, &dax_drv->ids, list)
44 if (sysfs_streq(dax_id->dev_name, dev_name))
49 static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
53 mutex_lock(&dax_bus_lock);
54 match = !!__dax_match_id(dax_drv, dev_name(dev));
55 mutex_unlock(&dax_bus_lock);
65 static ssize_t do_id_store(struct device_driver *drv, const char *buf,
66 size_t count, enum id_action action)
68 struct dax_device_driver *dax_drv = to_dax_drv(drv);
69 unsigned int region_id, id;
70 char devname[DAX_NAME_LEN];
71 struct dax_id *dax_id;
75 fields = sscanf(buf, "dax%d.%d", ®ion_id, &id);
78 sprintf(devname, "dax%d.%d", region_id, id);
79 if (!sysfs_streq(buf, devname))
82 mutex_lock(&dax_bus_lock);
83 dax_id = __dax_match_id(dax_drv, buf);
85 if (action == ID_ADD) {
86 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
88 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
89 list_add(&dax_id->list, &dax_drv->ids);
93 /* nothing to remove */;
94 } else if (action == ID_REMOVE) {
95 list_del(&dax_id->list);
98 /* dax_id already added */;
99 mutex_unlock(&dax_bus_lock);
103 if (action == ID_ADD)
104 rc = driver_attach(drv);
110 static ssize_t new_id_store(struct device_driver *drv, const char *buf,
113 return do_id_store(drv, buf, count, ID_ADD);
115 static DRIVER_ATTR_WO(new_id);
117 static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
120 return do_id_store(drv, buf, count, ID_REMOVE);
122 static DRIVER_ATTR_WO(remove_id);
124 static struct attribute *dax_drv_attrs[] = {
125 &driver_attr_new_id.attr,
126 &driver_attr_remove_id.attr,
129 ATTRIBUTE_GROUPS(dax_drv);
131 static int dax_bus_match(struct device *dev, struct device_driver *drv);
133 static struct bus_type dax_bus_type = {
135 .uevent = dax_bus_uevent,
136 .match = dax_bus_match,
137 .drv_groups = dax_drv_groups,
140 static int dax_bus_match(struct device *dev, struct device_driver *drv)
142 struct dax_device_driver *dax_drv = to_dax_drv(drv);
145 * All but the 'device-dax' driver, which has 'match_always'
146 * set, requires an exact id match.
148 if (dax_drv->match_always)
151 return dax_match_id(dax_drv, dev);
155 * Rely on the fact that drvdata is set before the attributes are
156 * registered, and that the attributes are unregistered before drvdata
157 * is cleared to assume that drvdata is always valid.
159 static ssize_t id_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct dax_region *dax_region = dev_get_drvdata(dev);
164 return sprintf(buf, "%d\n", dax_region->id);
166 static DEVICE_ATTR_RO(id);
168 static ssize_t region_size_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct dax_region *dax_region = dev_get_drvdata(dev);
173 return sprintf(buf, "%llu\n", (unsigned long long)
174 resource_size(&dax_region->res));
176 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
177 region_size_show, NULL);
179 static ssize_t align_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
182 struct dax_region *dax_region = dev_get_drvdata(dev);
184 return sprintf(buf, "%u\n", dax_region->align);
186 static DEVICE_ATTR_RO(align);
188 static struct attribute *dax_region_attributes[] = {
189 &dev_attr_region_size.attr,
190 &dev_attr_align.attr,
195 static const struct attribute_group dax_region_attribute_group = {
196 .name = "dax_region",
197 .attrs = dax_region_attributes,
200 static const struct attribute_group *dax_region_attribute_groups[] = {
201 &dax_region_attribute_group,
205 static void dax_region_free(struct kref *kref)
207 struct dax_region *dax_region;
209 dax_region = container_of(kref, struct dax_region, kref);
213 void dax_region_put(struct dax_region *dax_region)
215 kref_put(&dax_region->kref, dax_region_free);
217 EXPORT_SYMBOL_GPL(dax_region_put);
219 static void dax_region_unregister(void *region)
221 struct dax_region *dax_region = region;
223 sysfs_remove_groups(&dax_region->dev->kobj,
224 dax_region_attribute_groups);
225 dax_region_put(dax_region);
228 struct dax_region *alloc_dax_region(struct device *parent, int region_id,
229 struct resource *res, int target_node, unsigned int align)
231 struct dax_region *dax_region;
234 * The DAX core assumes that it can store its private data in
235 * parent->driver_data. This WARN is a reminder / safeguard for
236 * developers of device-dax drivers.
238 if (dev_get_drvdata(parent)) {
239 dev_WARN(parent, "dax core failed to setup private data\n");
243 if (!IS_ALIGNED(res->start, align)
244 || !IS_ALIGNED(resource_size(res), align))
247 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
251 dev_set_drvdata(parent, dax_region);
252 memcpy(&dax_region->res, res, sizeof(*res));
253 kref_init(&dax_region->kref);
254 dax_region->id = region_id;
255 dax_region->align = align;
256 dax_region->dev = parent;
257 dax_region->target_node = target_node;
258 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
263 kref_get(&dax_region->kref);
264 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
268 EXPORT_SYMBOL_GPL(alloc_dax_region);
270 static ssize_t size_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct dev_dax *dev_dax = to_dev_dax(dev);
274 unsigned long long size = resource_size(&dev_dax->region->res);
276 return sprintf(buf, "%llu\n", size);
278 static DEVICE_ATTR_RO(size);
280 static int dev_dax_target_node(struct dev_dax *dev_dax)
282 struct dax_region *dax_region = dev_dax->region;
284 return dax_region->target_node;
287 static ssize_t target_node_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
290 struct dev_dax *dev_dax = to_dev_dax(dev);
292 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
294 static DEVICE_ATTR_RO(target_node);
296 static unsigned long long dev_dax_resource(struct dev_dax *dev_dax)
298 struct dax_region *dax_region = dev_dax->region;
300 return dax_region->res.start;
303 static ssize_t resource_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct dev_dax *dev_dax = to_dev_dax(dev);
308 return sprintf(buf, "%#llx\n", dev_dax_resource(dev_dax));
310 static DEVICE_ATTR(resource, 0400, resource_show, NULL);
312 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
316 * We only ever expect to handle device-dax instances, i.e. the
317 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
319 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
321 static DEVICE_ATTR_RO(modalias);
323 static ssize_t numa_node_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 return sprintf(buf, "%d\n", dev_to_node(dev));
328 static DEVICE_ATTR_RO(numa_node);
330 static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
332 struct device *dev = container_of(kobj, struct device, kobj);
333 struct dev_dax *dev_dax = to_dev_dax(dev);
335 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
337 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
342 static struct attribute *dev_dax_attributes[] = {
343 &dev_attr_modalias.attr,
345 &dev_attr_target_node.attr,
346 &dev_attr_resource.attr,
347 &dev_attr_numa_node.attr,
351 static const struct attribute_group dev_dax_attribute_group = {
352 .attrs = dev_dax_attributes,
353 .is_visible = dev_dax_visible,
356 static const struct attribute_group *dax_attribute_groups[] = {
357 &dev_dax_attribute_group,
361 void kill_dev_dax(struct dev_dax *dev_dax)
363 struct dax_device *dax_dev = dev_dax->dax_dev;
364 struct inode *inode = dax_inode(dax_dev);
367 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
369 EXPORT_SYMBOL_GPL(kill_dev_dax);
371 static void dev_dax_release(struct device *dev)
373 struct dev_dax *dev_dax = to_dev_dax(dev);
374 struct dax_region *dax_region = dev_dax->region;
375 struct dax_device *dax_dev = dev_dax->dax_dev;
377 dax_region_put(dax_region);
382 static const struct device_type dev_dax_type = {
383 .release = dev_dax_release,
384 .groups = dax_attribute_groups,
387 static void unregister_dev_dax(void *dev)
389 struct dev_dax *dev_dax = to_dev_dax(dev);
391 dev_dbg(dev, "%s\n", __func__);
393 kill_dev_dax(dev_dax);
398 struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
400 struct dax_region *dax_region = data->dax_region;
401 struct device *parent = dax_region->dev;
402 struct dax_device *dax_dev;
403 struct dev_dax *dev_dax;
409 return ERR_PTR(-EINVAL);
411 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
413 return ERR_PTR(-ENOMEM);
415 memcpy(&dev_dax->pgmap, data->pgmap, sizeof(struct dev_pagemap));
418 * No 'host' or dax_operations since there is no access to this
419 * device outside of mmap of the resulting character device.
421 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
422 if (IS_ERR(dax_dev)) {
423 rc = PTR_ERR(dax_dev);
427 /* a device_dax instance is dead while the driver is not attached */
430 /* from here on we're committed to teardown via dax_dev_release() */
432 device_initialize(dev);
434 dev_dax->dax_dev = dax_dev;
435 dev_dax->region = dax_region;
436 dev_dax->target_node = dax_region->target_node;
437 kref_get(&dax_region->kref);
439 inode = dax_inode(dax_dev);
440 dev->devt = inode->i_rdev;
441 if (data->subsys == DEV_DAX_BUS)
442 dev->bus = &dax_bus_type;
444 dev->class = dax_class;
445 dev->parent = parent;
446 dev->type = &dev_dax_type;
447 dev_set_name(dev, "dax%d.%d", dax_region->id, data->id);
449 rc = device_add(dev);
451 kill_dev_dax(dev_dax);
456 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
467 EXPORT_SYMBOL_GPL(devm_create_dev_dax);
469 static int match_always_count;
471 int __dax_driver_register(struct dax_device_driver *dax_drv,
472 struct module *module, const char *mod_name)
474 struct device_driver *drv = &dax_drv->drv;
477 INIT_LIST_HEAD(&dax_drv->ids);
479 drv->name = mod_name;
480 drv->mod_name = mod_name;
481 drv->bus = &dax_bus_type;
483 /* there can only be one default driver */
484 mutex_lock(&dax_bus_lock);
485 match_always_count += dax_drv->match_always;
486 if (match_always_count > 1) {
487 match_always_count--;
491 mutex_unlock(&dax_bus_lock);
494 return driver_register(drv);
496 EXPORT_SYMBOL_GPL(__dax_driver_register);
498 void dax_driver_unregister(struct dax_device_driver *dax_drv)
500 struct device_driver *drv = &dax_drv->drv;
501 struct dax_id *dax_id, *_id;
503 mutex_lock(&dax_bus_lock);
504 match_always_count -= dax_drv->match_always;
505 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
506 list_del(&dax_id->list);
509 mutex_unlock(&dax_bus_lock);
510 driver_unregister(drv);
512 EXPORT_SYMBOL_GPL(dax_driver_unregister);
514 int __init dax_bus_init(void)
518 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
519 dax_class = class_create(THIS_MODULE, "dax");
520 if (IS_ERR(dax_class))
521 return PTR_ERR(dax_class);
524 rc = bus_register(&dax_bus_type);
526 class_destroy(dax_class);
530 void __exit dax_bus_exit(void)
532 bus_unregister(&dax_bus_type);
533 class_destroy(dax_class);