blob: a410154d75fb9da3443b2c304b9a03a8bdfdae2f [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>
Dan Williamsd2007812018-11-07 15:31:23 -08005#include <linux/mutex.h>
6#include <linux/list.h>
Dan Williams51cf7842017-07-12 17:58:21 -07007#include <linux/slab.h>
8#include <linux/dax.h>
9#include "dax-private.h"
10#include "bus.h"
11
Dan Williams730926c2017-07-16 13:51:53 -070012static struct class *dax_class;
13
Dan Williamsd2007812018-11-07 15:31:23 -080014static DEFINE_MUTEX(dax_bus_lock);
15
16#define DAX_NAME_LEN 30
17struct dax_id {
18 struct list_head list;
19 char dev_name[DAX_NAME_LEN];
20};
21
Dan Williams9567da02017-07-12 17:58:21 -070022static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
23{
24 /*
25 * We only ever expect to handle device-dax instances, i.e. the
26 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
27 */
28 return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
29}
30
Dan Williamsd2007812018-11-07 15:31:23 -080031static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
32{
33 return container_of(drv, struct dax_device_driver, drv);
34}
35
36static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
37 const char *dev_name)
38{
39 struct dax_id *dax_id;
40
41 lockdep_assert_held(&dax_bus_lock);
42
43 list_for_each_entry(dax_id, &dax_drv->ids, list)
44 if (sysfs_streq(dax_id->dev_name, dev_name))
45 return dax_id;
46 return NULL;
47}
48
49static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
50{
51 int match;
52
53 mutex_lock(&dax_bus_lock);
54 match = !!__dax_match_id(dax_drv, dev_name(dev));
55 mutex_unlock(&dax_bus_lock);
56
57 return match;
58}
59
Dan Williams664525b2019-01-24 13:12:04 -080060enum id_action {
61 ID_REMOVE,
62 ID_ADD,
63};
64
Dan Williamsd2007812018-11-07 15:31:23 -080065static ssize_t do_id_store(struct device_driver *drv, const char *buf,
Dan Williams664525b2019-01-24 13:12:04 -080066 size_t count, enum id_action action)
Dan Williamsd2007812018-11-07 15:31:23 -080067{
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;
72 ssize_t rc = count;
73 int fields;
74
75 fields = sscanf(buf, "dax%d.%d", &region_id, &id);
76 if (fields != 2)
77 return -EINVAL;
78 sprintf(devname, "dax%d.%d", region_id, id);
79 if (!sysfs_streq(buf, devname))
80 return -EINVAL;
81
82 mutex_lock(&dax_bus_lock);
83 dax_id = __dax_match_id(dax_drv, buf);
84 if (!dax_id) {
Dan Williams664525b2019-01-24 13:12:04 -080085 if (action == ID_ADD) {
Dan Williamsd2007812018-11-07 15:31:23 -080086 dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
87 if (dax_id) {
88 strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
89 list_add(&dax_id->list, &dax_drv->ids);
90 } else
91 rc = -ENOMEM;
92 } else
93 /* nothing to remove */;
Dan Williams664525b2019-01-24 13:12:04 -080094 } else if (action == ID_REMOVE) {
Dan Williamsd2007812018-11-07 15:31:23 -080095 list_del(&dax_id->list);
96 kfree(dax_id);
97 } else
98 /* dax_id already added */;
99 mutex_unlock(&dax_bus_lock);
Dan Williams664525b2019-01-24 13:12:04 -0800100
101 if (rc < 0)
102 return rc;
103 if (action == ID_ADD)
104 rc = driver_attach(drv);
105 if (rc)
106 return rc;
107 return count;
Dan Williamsd2007812018-11-07 15:31:23 -0800108}
109
110static ssize_t new_id_store(struct device_driver *drv, const char *buf,
111 size_t count)
112{
Dan Williams664525b2019-01-24 13:12:04 -0800113 return do_id_store(drv, buf, count, ID_ADD);
Dan Williamsd2007812018-11-07 15:31:23 -0800114}
115static DRIVER_ATTR_WO(new_id);
116
117static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
118 size_t count)
119{
Dan Williams664525b2019-01-24 13:12:04 -0800120 return do_id_store(drv, buf, count, ID_REMOVE);
Dan Williamsd2007812018-11-07 15:31:23 -0800121}
122static DRIVER_ATTR_WO(remove_id);
123
124static struct attribute *dax_drv_attrs[] = {
125 &driver_attr_new_id.attr,
126 &driver_attr_remove_id.attr,
127 NULL,
128};
129ATTRIBUTE_GROUPS(dax_drv);
130
Dan Williams9567da02017-07-12 17:58:21 -0700131static int dax_bus_match(struct device *dev, struct device_driver *drv);
132
133static struct bus_type dax_bus_type = {
134 .name = "dax",
135 .uevent = dax_bus_uevent,
136 .match = dax_bus_match,
Dan Williamsd2007812018-11-07 15:31:23 -0800137 .drv_groups = dax_drv_groups,
Dan Williams9567da02017-07-12 17:58:21 -0700138};
139
140static int dax_bus_match(struct device *dev, struct device_driver *drv)
141{
Dan Williamsd2007812018-11-07 15:31:23 -0800142 struct dax_device_driver *dax_drv = to_dax_drv(drv);
143
Dan Williams9567da02017-07-12 17:58:21 -0700144 /*
Dan Williamsd2007812018-11-07 15:31:23 -0800145 * All but the 'device-dax' driver, which has 'match_always'
146 * set, requires an exact id match.
Dan Williams9567da02017-07-12 17:58:21 -0700147 */
Dan Williamsd2007812018-11-07 15:31:23 -0800148 if (dax_drv->match_always)
149 return 1;
150
151 return dax_match_id(dax_drv, dev);
Dan Williams9567da02017-07-12 17:58:21 -0700152}
153
Dan Williams51cf7842017-07-12 17:58:21 -0700154/*
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.
158 */
159static ssize_t id_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
161{
162 struct dax_region *dax_region = dev_get_drvdata(dev);
163
164 return sprintf(buf, "%d\n", dax_region->id);
165}
166static DEVICE_ATTR_RO(id);
167
168static ssize_t region_size_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
171 struct dax_region *dax_region = dev_get_drvdata(dev);
172
173 return sprintf(buf, "%llu\n", (unsigned long long)
174 resource_size(&dax_region->res));
175}
176static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
177 region_size_show, NULL);
178
179static ssize_t align_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct dax_region *dax_region = dev_get_drvdata(dev);
183
184 return sprintf(buf, "%u\n", dax_region->align);
185}
186static DEVICE_ATTR_RO(align);
187
188static struct attribute *dax_region_attributes[] = {
189 &dev_attr_region_size.attr,
190 &dev_attr_align.attr,
191 &dev_attr_id.attr,
192 NULL,
193};
194
195static const struct attribute_group dax_region_attribute_group = {
196 .name = "dax_region",
197 .attrs = dax_region_attributes,
198};
199
200static const struct attribute_group *dax_region_attribute_groups[] = {
201 &dax_region_attribute_group,
202 NULL,
203};
204
205static void dax_region_free(struct kref *kref)
206{
207 struct dax_region *dax_region;
208
209 dax_region = container_of(kref, struct dax_region, kref);
210 kfree(dax_region);
211}
212
213void dax_region_put(struct dax_region *dax_region)
214{
215 kref_put(&dax_region->kref, dax_region_free);
216}
217EXPORT_SYMBOL_GPL(dax_region_put);
218
219static void dax_region_unregister(void *region)
220{
221 struct dax_region *dax_region = region;
222
223 sysfs_remove_groups(&dax_region->dev->kobj,
224 dax_region_attribute_groups);
225 dax_region_put(dax_region);
226}
227
228struct dax_region *alloc_dax_region(struct device *parent, int region_id,
Dan Williams8fc5c732018-11-09 12:43:07 -0800229 struct resource *res, int target_node, unsigned int align,
Dan Williams51cf7842017-07-12 17:58:21 -0700230 unsigned long pfn_flags)
231{
232 struct dax_region *dax_region;
233
234 /*
235 * The DAX core assumes that it can store its private data in
236 * parent->driver_data. This WARN is a reminder / safeguard for
237 * developers of device-dax drivers.
238 */
239 if (dev_get_drvdata(parent)) {
240 dev_WARN(parent, "dax core failed to setup private data\n");
241 return NULL;
242 }
243
244 if (!IS_ALIGNED(res->start, align)
245 || !IS_ALIGNED(resource_size(res), align))
246 return NULL;
247
248 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
249 if (!dax_region)
250 return NULL;
251
252 dev_set_drvdata(parent, dax_region);
253 memcpy(&dax_region->res, res, sizeof(*res));
254 dax_region->pfn_flags = pfn_flags;
255 kref_init(&dax_region->kref);
256 dax_region->id = region_id;
257 dax_region->align = align;
258 dax_region->dev = parent;
Dan Williams8fc5c732018-11-09 12:43:07 -0800259 dax_region->target_node = target_node;
Dan Williams51cf7842017-07-12 17:58:21 -0700260 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
261 kfree(dax_region);
262 return NULL;
263 }
264
265 kref_get(&dax_region->kref);
266 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
267 return NULL;
268 return dax_region;
269}
270EXPORT_SYMBOL_GPL(alloc_dax_region);
271
272static ssize_t size_show(struct device *dev,
273 struct device_attribute *attr, char *buf)
274{
275 struct dev_dax *dev_dax = to_dev_dax(dev);
276 unsigned long long size = resource_size(&dev_dax->region->res);
277
278 return sprintf(buf, "%llu\n", size);
279}
280static DEVICE_ATTR_RO(size);
281
282static struct attribute *dev_dax_attributes[] = {
283 &dev_attr_size.attr,
284 NULL,
285};
286
287static const struct attribute_group dev_dax_attribute_group = {
288 .attrs = dev_dax_attributes,
289};
290
Dan Williams9567da02017-07-12 17:58:21 -0700291static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams51cf7842017-07-12 17:58:21 -0700292 &dev_dax_attribute_group,
293 NULL,
294};
Dan Williams51cf7842017-07-12 17:58:21 -0700295
296void kill_dev_dax(struct dev_dax *dev_dax)
297{
298 struct dax_device *dax_dev = dev_dax->dax_dev;
299 struct inode *inode = dax_inode(dax_dev);
300
301 kill_dax(dax_dev);
302 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
303}
304EXPORT_SYMBOL_GPL(kill_dev_dax);
305
Dan Williams9567da02017-07-12 17:58:21 -0700306static void dev_dax_release(struct device *dev)
Dan Williams51cf7842017-07-12 17:58:21 -0700307{
308 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams9567da02017-07-12 17:58:21 -0700309 struct dax_region *dax_region = dev_dax->region;
Dan Williams51cf7842017-07-12 17:58:21 -0700310 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams51cf7842017-07-12 17:58:21 -0700311
Dan Williams9567da02017-07-12 17:58:21 -0700312 dax_region_put(dax_region);
313 put_dax(dax_dev);
314 kfree(dev_dax);
315}
316
317static void unregister_dev_dax(void *dev)
318{
319 struct dev_dax *dev_dax = to_dev_dax(dev);
320
321 dev_dbg(dev, "%s\n", __func__);
Dan Williams51cf7842017-07-12 17:58:21 -0700322
323 kill_dev_dax(dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -0700324 device_del(dev);
Dan Williams51cf7842017-07-12 17:58:21 -0700325 put_device(dev);
326}
Dan Williams9567da02017-07-12 17:58:21 -0700327
Dan Williams730926c2017-07-16 13:51:53 -0700328struct dev_dax *__devm_create_dev_dax(struct dax_region *dax_region, int id,
329 struct dev_pagemap *pgmap, enum dev_dax_subsys subsys)
Dan Williams9567da02017-07-12 17:58:21 -0700330{
331 struct device *parent = dax_region->dev;
332 struct dax_device *dax_dev;
333 struct dev_dax *dev_dax;
334 struct inode *inode;
335 struct device *dev;
336 int rc = -ENOMEM;
337
338 if (id < 0)
339 return ERR_PTR(-EINVAL);
340
341 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
342 if (!dev_dax)
343 return ERR_PTR(-ENOMEM);
344
Dan Williams89ec9f22018-10-29 15:52:42 -0700345 memcpy(&dev_dax->pgmap, pgmap, sizeof(*pgmap));
346
Dan Williams9567da02017-07-12 17:58:21 -0700347 /*
348 * No 'host' or dax_operations since there is no access to this
349 * device outside of mmap of the resulting character device.
350 */
351 dax_dev = alloc_dax(dev_dax, NULL, NULL);
352 if (!dax_dev)
353 goto err;
354
355 /* a device_dax instance is dead while the driver is not attached */
356 kill_dax(dax_dev);
357
358 /* from here on we're committed to teardown via dax_dev_release() */
359 dev = &dev_dax->dev;
360 device_initialize(dev);
361
362 dev_dax->dax_dev = dax_dev;
363 dev_dax->region = dax_region;
Dan Williams8fc5c732018-11-09 12:43:07 -0800364 dev_dax->target_node = dax_region->target_node;
Dan Williams9567da02017-07-12 17:58:21 -0700365 kref_get(&dax_region->kref);
366
367 inode = dax_inode(dax_dev);
368 dev->devt = inode->i_rdev;
Dan Williams730926c2017-07-16 13:51:53 -0700369 if (subsys == DEV_DAX_BUS)
370 dev->bus = &dax_bus_type;
371 else
372 dev->class = dax_class;
Dan Williams9567da02017-07-12 17:58:21 -0700373 dev->parent = parent;
374 dev->groups = dax_attribute_groups;
375 dev->release = dev_dax_release;
376 dev_set_name(dev, "dax%d.%d", dax_region->id, id);
377
378 rc = device_add(dev);
379 if (rc) {
380 kill_dev_dax(dev_dax);
381 put_device(dev);
382 return ERR_PTR(rc);
383 }
384
385 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
386 if (rc)
387 return ERR_PTR(rc);
388
389 return dev_dax;
390
391 err:
392 kfree(dev_dax);
393
394 return ERR_PTR(rc);
395}
Dan Williams730926c2017-07-16 13:51:53 -0700396EXPORT_SYMBOL_GPL(__devm_create_dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -0700397
Dan Williamsd2007812018-11-07 15:31:23 -0800398static int match_always_count;
399
400int __dax_driver_register(struct dax_device_driver *dax_drv,
Dan Williams9567da02017-07-12 17:58:21 -0700401 struct module *module, const char *mod_name)
402{
Dan Williamsd2007812018-11-07 15:31:23 -0800403 struct device_driver *drv = &dax_drv->drv;
404 int rc = 0;
405
406 INIT_LIST_HEAD(&dax_drv->ids);
Dan Williams9567da02017-07-12 17:58:21 -0700407 drv->owner = module;
408 drv->name = mod_name;
409 drv->mod_name = mod_name;
410 drv->bus = &dax_bus_type;
Dan Williamsd2007812018-11-07 15:31:23 -0800411
412 /* there can only be one default driver */
413 mutex_lock(&dax_bus_lock);
414 match_always_count += dax_drv->match_always;
415 if (match_always_count > 1) {
416 match_always_count--;
417 WARN_ON(1);
418 rc = -EINVAL;
419 }
420 mutex_unlock(&dax_bus_lock);
421 if (rc)
422 return rc;
Dan Williams9567da02017-07-12 17:58:21 -0700423 return driver_register(drv);
424}
425EXPORT_SYMBOL_GPL(__dax_driver_register);
426
Dan Williamsd2007812018-11-07 15:31:23 -0800427void dax_driver_unregister(struct dax_device_driver *dax_drv)
428{
Dan Williams730926c2017-07-16 13:51:53 -0700429 struct device_driver *drv = &dax_drv->drv;
Dan Williamsd2007812018-11-07 15:31:23 -0800430 struct dax_id *dax_id, *_id;
431
432 mutex_lock(&dax_bus_lock);
433 match_always_count -= dax_drv->match_always;
434 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
435 list_del(&dax_id->list);
436 kfree(dax_id);
437 }
438 mutex_unlock(&dax_bus_lock);
Dan Williams730926c2017-07-16 13:51:53 -0700439 driver_unregister(drv);
Dan Williamsd2007812018-11-07 15:31:23 -0800440}
441EXPORT_SYMBOL_GPL(dax_driver_unregister);
442
Dan Williams9567da02017-07-12 17:58:21 -0700443int __init dax_bus_init(void)
444{
Dan Williams730926c2017-07-16 13:51:53 -0700445 int rc;
446
447 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
448 dax_class = class_create(THIS_MODULE, "dax");
449 if (IS_ERR(dax_class))
450 return PTR_ERR(dax_class);
451 }
452
453 rc = bus_register(&dax_bus_type);
454 if (rc)
455 class_destroy(dax_class);
456 return rc;
Dan Williams9567da02017-07-12 17:58:21 -0700457}
458
459void __exit dax_bus_exit(void)
460{
461 bus_unregister(&dax_bus_type);
Dan Williams730926c2017-07-16 13:51:53 -0700462 class_destroy(dax_class);
Dan Williams9567da02017-07-12 17:58:21 -0700463}