blob: 0a48ce378686dfc76b5650f79a15d8d36d23c76c [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
Dan Williamsc2f30112020-10-13 16:50:03 -0700133static bool is_static(struct dax_region *dax_region)
134{
135 return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
136}
137
Dan Williams9567da02017-07-12 17:58:21 -0700138static struct bus_type dax_bus_type = {
139 .name = "dax",
140 .uevent = dax_bus_uevent,
141 .match = dax_bus_match,
Dan Williamsd2007812018-11-07 15:31:23 -0800142 .drv_groups = dax_drv_groups,
Dan Williams9567da02017-07-12 17:58:21 -0700143};
144
145static int dax_bus_match(struct device *dev, struct device_driver *drv)
146{
Dan Williamsd2007812018-11-07 15:31:23 -0800147 struct dax_device_driver *dax_drv = to_dax_drv(drv);
148
Dan Williams9567da02017-07-12 17:58:21 -0700149 /*
Dan Williamsd2007812018-11-07 15:31:23 -0800150 * All but the 'device-dax' driver, which has 'match_always'
151 * set, requires an exact id match.
Dan Williams9567da02017-07-12 17:58:21 -0700152 */
Dan Williamsd2007812018-11-07 15:31:23 -0800153 if (dax_drv->match_always)
154 return 1;
155
156 return dax_match_id(dax_drv, dev);
Dan Williams9567da02017-07-12 17:58:21 -0700157}
158
Dan Williams51cf7842017-07-12 17:58:21 -0700159/*
160 * Rely on the fact that drvdata is set before the attributes are
161 * registered, and that the attributes are unregistered before drvdata
162 * is cleared to assume that drvdata is always valid.
163 */
164static ssize_t id_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
167 struct dax_region *dax_region = dev_get_drvdata(dev);
168
169 return sprintf(buf, "%d\n", dax_region->id);
170}
171static DEVICE_ATTR_RO(id);
172
173static ssize_t region_size_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
176 struct dax_region *dax_region = dev_get_drvdata(dev);
177
178 return sprintf(buf, "%llu\n", (unsigned long long)
179 resource_size(&dax_region->res));
180}
181static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
182 region_size_show, NULL);
183
184static ssize_t align_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
186{
187 struct dax_region *dax_region = dev_get_drvdata(dev);
188
189 return sprintf(buf, "%u\n", dax_region->align);
190}
191static DEVICE_ATTR_RO(align);
192
Dan Williamsc2f30112020-10-13 16:50:03 -0700193#define for_each_dax_region_resource(dax_region, res) \
194 for (res = (dax_region)->res.child; res; res = res->sibling)
195
196static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
197{
198 resource_size_t size = resource_size(&dax_region->res);
199 struct resource *res;
200
201 device_lock_assert(dax_region->dev);
202
203 for_each_dax_region_resource(dax_region, res)
204 size -= resource_size(res);
205 return size;
206}
207
208static ssize_t available_size_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 struct dax_region *dax_region = dev_get_drvdata(dev);
212 unsigned long long size;
213
214 device_lock(dev);
215 size = dax_region_avail_size(dax_region);
216 device_unlock(dev);
217
218 return sprintf(buf, "%llu\n", size);
219}
220static DEVICE_ATTR_RO(available_size);
221
222static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
223 int n)
224{
225 struct device *dev = container_of(kobj, struct device, kobj);
226 struct dax_region *dax_region = dev_get_drvdata(dev);
227
228 if (is_static(dax_region) && a == &dev_attr_available_size.attr)
229 return 0;
230 return a->mode;
231}
232
Dan Williams51cf7842017-07-12 17:58:21 -0700233static struct attribute *dax_region_attributes[] = {
Dan Williamsc2f30112020-10-13 16:50:03 -0700234 &dev_attr_available_size.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700235 &dev_attr_region_size.attr,
236 &dev_attr_align.attr,
237 &dev_attr_id.attr,
238 NULL,
239};
240
241static const struct attribute_group dax_region_attribute_group = {
242 .name = "dax_region",
243 .attrs = dax_region_attributes,
Dan Williamsc2f30112020-10-13 16:50:03 -0700244 .is_visible = dax_region_visible,
Dan Williams51cf7842017-07-12 17:58:21 -0700245};
246
247static const struct attribute_group *dax_region_attribute_groups[] = {
248 &dax_region_attribute_group,
249 NULL,
250};
251
252static void dax_region_free(struct kref *kref)
253{
254 struct dax_region *dax_region;
255
256 dax_region = container_of(kref, struct dax_region, kref);
257 kfree(dax_region);
258}
259
260void dax_region_put(struct dax_region *dax_region)
261{
262 kref_put(&dax_region->kref, dax_region_free);
263}
264EXPORT_SYMBOL_GPL(dax_region_put);
265
266static void dax_region_unregister(void *region)
267{
268 struct dax_region *dax_region = region;
269
270 sysfs_remove_groups(&dax_region->dev->kobj,
271 dax_region_attribute_groups);
272 dax_region_put(dax_region);
273}
274
275struct dax_region *alloc_dax_region(struct device *parent, int region_id,
Dan Williamsc2f30112020-10-13 16:50:03 -0700276 struct resource *res, int target_node, unsigned int align,
277 unsigned long flags)
Dan Williams51cf7842017-07-12 17:58:21 -0700278{
279 struct dax_region *dax_region;
280
281 /*
282 * The DAX core assumes that it can store its private data in
283 * parent->driver_data. This WARN is a reminder / safeguard for
284 * developers of device-dax drivers.
285 */
286 if (dev_get_drvdata(parent)) {
287 dev_WARN(parent, "dax core failed to setup private data\n");
288 return NULL;
289 }
290
291 if (!IS_ALIGNED(res->start, align)
292 || !IS_ALIGNED(resource_size(res), align))
293 return NULL;
294
295 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
296 if (!dax_region)
297 return NULL;
298
299 dev_set_drvdata(parent, dax_region);
Dan Williams51cf7842017-07-12 17:58:21 -0700300 kref_init(&dax_region->kref);
301 dax_region->id = region_id;
302 dax_region->align = align;
303 dax_region->dev = parent;
Dan Williams8fc5c732018-11-09 12:43:07 -0800304 dax_region->target_node = target_node;
Dan Williamsc2f30112020-10-13 16:50:03 -0700305 dax_region->res = (struct resource) {
306 .start = res->start,
307 .end = res->end,
308 .flags = IORESOURCE_MEM | flags,
309 };
310
Dan Williams51cf7842017-07-12 17:58:21 -0700311 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
312 kfree(dax_region);
313 return NULL;
314 }
315
316 kref_get(&dax_region->kref);
317 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
318 return NULL;
319 return dax_region;
320}
321EXPORT_SYMBOL_GPL(alloc_dax_region);
322
Dan Williamsc2f30112020-10-13 16:50:03 -0700323static int alloc_dev_dax_range(struct dev_dax *dev_dax, resource_size_t size)
324{
325 struct dax_region *dax_region = dev_dax->region;
326 struct resource *res = &dax_region->res;
327 struct device *dev = &dev_dax->dev;
328 struct resource *alloc;
329
330 device_lock_assert(dax_region->dev);
331
332 /* TODO: handle multiple allocations per region */
333 if (res->child)
334 return -ENOMEM;
335
336 alloc = __request_region(res, res->start, size, dev_name(dev), 0);
337
338 if (!alloc)
339 return -ENOMEM;
340
341 dev_dax->range = (struct range) {
342 .start = alloc->start,
343 .end = alloc->end,
344 };
345
346 return 0;
347}
348
Dan Williams51cf7842017-07-12 17:58:21 -0700349static ssize_t size_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
351{
352 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williamsf5516ec2020-10-13 16:49:43 -0700353 unsigned long long size = range_len(&dev_dax->range);
Dan Williams51cf7842017-07-12 17:58:21 -0700354
355 return sprintf(buf, "%llu\n", size);
356}
357static DEVICE_ATTR_RO(size);
358
Dan Williams21c75762019-02-20 11:39:36 -0800359static int dev_dax_target_node(struct dev_dax *dev_dax)
360{
361 struct dax_region *dax_region = dev_dax->region;
362
363 return dax_region->target_node;
364}
365
366static ssize_t target_node_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
369 struct dev_dax *dev_dax = to_dev_dax(dev);
370
371 return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
372}
373static DEVICE_ATTR_RO(target_node);
374
Vishal Verma40cdc602019-06-20 18:40:38 -0600375static ssize_t resource_show(struct device *dev,
376 struct device_attribute *attr, char *buf)
377{
378 struct dev_dax *dev_dax = to_dev_dax(dev);
379
Dan Williamsf5516ec2020-10-13 16:49:43 -0700380 return sprintf(buf, "%#llx\n", dev_dax->range.start);
Vishal Verma40cdc602019-06-20 18:40:38 -0600381}
Dan Williams153dd282019-11-12 17:12:38 -0800382static DEVICE_ATTR(resource, 0400, resource_show, NULL);
Vishal Verma40cdc602019-06-20 18:40:38 -0600383
Vishal Vermac347bd72019-02-22 16:58:54 -0700384static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
385 char *buf)
386{
387 /*
388 * We only ever expect to handle device-dax instances, i.e. the
389 * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
390 */
391 return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
392}
393static DEVICE_ATTR_RO(modalias);
394
Dan Williamscb4dd722019-11-12 17:13:31 -0800395static ssize_t numa_node_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
397{
398 return sprintf(buf, "%d\n", dev_to_node(dev));
399}
400static DEVICE_ATTR_RO(numa_node);
401
Dan Williams21c75762019-02-20 11:39:36 -0800402static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
403{
404 struct device *dev = container_of(kobj, struct device, kobj);
405 struct dev_dax *dev_dax = to_dev_dax(dev);
406
407 if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
408 return 0;
Dan Williamscb4dd722019-11-12 17:13:31 -0800409 if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
410 return 0;
Dan Williams21c75762019-02-20 11:39:36 -0800411 return a->mode;
412}
413
Dan Williams51cf7842017-07-12 17:58:21 -0700414static struct attribute *dev_dax_attributes[] = {
Vishal Vermac347bd72019-02-22 16:58:54 -0700415 &dev_attr_modalias.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700416 &dev_attr_size.attr,
Dan Williams21c75762019-02-20 11:39:36 -0800417 &dev_attr_target_node.attr,
Vishal Verma40cdc602019-06-20 18:40:38 -0600418 &dev_attr_resource.attr,
Dan Williamscb4dd722019-11-12 17:13:31 -0800419 &dev_attr_numa_node.attr,
Dan Williams51cf7842017-07-12 17:58:21 -0700420 NULL,
421};
422
423static const struct attribute_group dev_dax_attribute_group = {
424 .attrs = dev_dax_attributes,
Dan Williams21c75762019-02-20 11:39:36 -0800425 .is_visible = dev_dax_visible,
Dan Williams51cf7842017-07-12 17:58:21 -0700426};
427
Dan Williams9567da02017-07-12 17:58:21 -0700428static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams51cf7842017-07-12 17:58:21 -0700429 &dev_dax_attribute_group,
430 NULL,
431};
Dan Williams51cf7842017-07-12 17:58:21 -0700432
433void kill_dev_dax(struct dev_dax *dev_dax)
434{
435 struct dax_device *dax_dev = dev_dax->dax_dev;
436 struct inode *inode = dax_inode(dax_dev);
437
438 kill_dax(dax_dev);
439 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
440}
441EXPORT_SYMBOL_GPL(kill_dev_dax);
442
Dan Williamsc2f30112020-10-13 16:50:03 -0700443static void free_dev_dax_range(struct dev_dax *dev_dax)
444{
445 struct dax_region *dax_region = dev_dax->region;
446 struct range *range = &dev_dax->range;
447
448 device_lock_assert(dax_region->dev);
449 __release_region(&dax_region->res, range->start, range_len(range));
450}
451
Dan Williams9567da02017-07-12 17:58:21 -0700452static void dev_dax_release(struct device *dev)
Dan Williams51cf7842017-07-12 17:58:21 -0700453{
454 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams9567da02017-07-12 17:58:21 -0700455 struct dax_region *dax_region = dev_dax->region;
Dan Williams51cf7842017-07-12 17:58:21 -0700456 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams51cf7842017-07-12 17:58:21 -0700457
Dan Williams9567da02017-07-12 17:58:21 -0700458 dax_region_put(dax_region);
459 put_dax(dax_dev);
Dan Williamsf5516ec2020-10-13 16:49:43 -0700460 kfree(dev_dax->pgmap);
Dan Williams9567da02017-07-12 17:58:21 -0700461 kfree(dev_dax);
462}
463
Dan Williams770619a2019-11-12 17:12:23 -0800464static const struct device_type dev_dax_type = {
465 .release = dev_dax_release,
466 .groups = dax_attribute_groups,
467};
468
Dan Williams9567da02017-07-12 17:58:21 -0700469static void unregister_dev_dax(void *dev)
470{
471 struct dev_dax *dev_dax = to_dev_dax(dev);
472
473 dev_dbg(dev, "%s\n", __func__);
Dan Williams51cf7842017-07-12 17:58:21 -0700474
475 kill_dev_dax(dev_dax);
Dan Williamsc2f30112020-10-13 16:50:03 -0700476 free_dev_dax_range(dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -0700477 device_del(dev);
Dan Williams51cf7842017-07-12 17:58:21 -0700478 put_device(dev);
479}
Dan Williams9567da02017-07-12 17:58:21 -0700480
Dan Williams174ebec2020-10-13 16:49:38 -0700481struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
Dan Williams9567da02017-07-12 17:58:21 -0700482{
Dan Williams174ebec2020-10-13 16:49:38 -0700483 struct dax_region *dax_region = data->dax_region;
Dan Williams9567da02017-07-12 17:58:21 -0700484 struct device *parent = dax_region->dev;
485 struct dax_device *dax_dev;
486 struct dev_dax *dev_dax;
487 struct inode *inode;
488 struct device *dev;
Dan Williamsc2f30112020-10-13 16:50:03 -0700489 int rc;
Dan Williams9567da02017-07-12 17:58:21 -0700490
Dan Williams174ebec2020-10-13 16:49:38 -0700491 if (data->id < 0)
Dan Williams9567da02017-07-12 17:58:21 -0700492 return ERR_PTR(-EINVAL);
493
494 dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
495 if (!dev_dax)
496 return ERR_PTR(-ENOMEM);
497
Dan Williamsc2f30112020-10-13 16:50:03 -0700498 dev_dax->region = dax_region;
499 dev = &dev_dax->dev;
500 device_initialize(dev);
501 dev_set_name(dev, "dax%d.%d", dax_region->id, data->id);
502
503 rc = alloc_dev_dax_range(dev_dax, data->size);
504 if (rc)
505 goto err_range;
506
Dan Williamsf5516ec2020-10-13 16:49:43 -0700507 if (data->pgmap) {
Dan Williamsc2f30112020-10-13 16:50:03 -0700508 dev_WARN_ONCE(parent, !is_static(dax_region),
509 "custom dev_pagemap requires a static dax_region\n");
510
Dan Williamsf5516ec2020-10-13 16:49:43 -0700511 dev_dax->pgmap = kmemdup(data->pgmap,
512 sizeof(struct dev_pagemap), GFP_KERNEL);
Dan Williamsc2f30112020-10-13 16:50:03 -0700513 if (!dev_dax->pgmap) {
514 rc = -ENOMEM;
Dan Williamsf5516ec2020-10-13 16:49:43 -0700515 goto err_pgmap;
Dan Williamsc2f30112020-10-13 16:50:03 -0700516 }
Dan Williamsf5516ec2020-10-13 16:49:43 -0700517 }
Dan Williams89ec9f22018-10-29 15:52:42 -0700518
Dan Williams9567da02017-07-12 17:58:21 -0700519 /*
520 * No 'host' or dax_operations since there is no access to this
521 * device outside of mmap of the resulting character device.
522 */
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530523 dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400524 if (IS_ERR(dax_dev)) {
525 rc = PTR_ERR(dax_dev);
Dan Williamsf5516ec2020-10-13 16:49:43 -0700526 goto err_alloc_dax;
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400527 }
Dan Williams9567da02017-07-12 17:58:21 -0700528
529 /* a device_dax instance is dead while the driver is not attached */
530 kill_dax(dax_dev);
531
Dan Williamsf5516ec2020-10-13 16:49:43 -0700532 /* from here on we're committed to teardown via dev_dax_release() */
Dan Williams9567da02017-07-12 17:58:21 -0700533 dev_dax->dax_dev = dax_dev;
Dan Williams8fc5c732018-11-09 12:43:07 -0800534 dev_dax->target_node = dax_region->target_node;
Dan Williams9567da02017-07-12 17:58:21 -0700535 kref_get(&dax_region->kref);
536
537 inode = dax_inode(dax_dev);
538 dev->devt = inode->i_rdev;
Dan Williams174ebec2020-10-13 16:49:38 -0700539 if (data->subsys == DEV_DAX_BUS)
Dan Williams730926c2017-07-16 13:51:53 -0700540 dev->bus = &dax_bus_type;
541 else
542 dev->class = dax_class;
Dan Williams9567da02017-07-12 17:58:21 -0700543 dev->parent = parent;
Dan Williams770619a2019-11-12 17:12:23 -0800544 dev->type = &dev_dax_type;
Dan Williams9567da02017-07-12 17:58:21 -0700545
546 rc = device_add(dev);
547 if (rc) {
548 kill_dev_dax(dev_dax);
549 put_device(dev);
550 return ERR_PTR(rc);
551 }
552
553 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
554 if (rc)
555 return ERR_PTR(rc);
556
557 return dev_dax;
Dan Williamsc2f30112020-10-13 16:50:03 -0700558
Dan Williamsf5516ec2020-10-13 16:49:43 -0700559err_alloc_dax:
560 kfree(dev_dax->pgmap);
561err_pgmap:
Dan Williamsc2f30112020-10-13 16:50:03 -0700562 free_dev_dax_range(dev_dax);
563err_range:
Dan Williams9567da02017-07-12 17:58:21 -0700564 kfree(dev_dax);
565
566 return ERR_PTR(rc);
567}
Dan Williams174ebec2020-10-13 16:49:38 -0700568EXPORT_SYMBOL_GPL(devm_create_dev_dax);
Dan Williams9567da02017-07-12 17:58:21 -0700569
Dan Williamsd2007812018-11-07 15:31:23 -0800570static int match_always_count;
571
572int __dax_driver_register(struct dax_device_driver *dax_drv,
Dan Williams9567da02017-07-12 17:58:21 -0700573 struct module *module, const char *mod_name)
574{
Dan Williamsd2007812018-11-07 15:31:23 -0800575 struct device_driver *drv = &dax_drv->drv;
576 int rc = 0;
577
578 INIT_LIST_HEAD(&dax_drv->ids);
Dan Williams9567da02017-07-12 17:58:21 -0700579 drv->owner = module;
580 drv->name = mod_name;
581 drv->mod_name = mod_name;
582 drv->bus = &dax_bus_type;
Dan Williamsd2007812018-11-07 15:31:23 -0800583
584 /* there can only be one default driver */
585 mutex_lock(&dax_bus_lock);
586 match_always_count += dax_drv->match_always;
587 if (match_always_count > 1) {
588 match_always_count--;
589 WARN_ON(1);
590 rc = -EINVAL;
591 }
592 mutex_unlock(&dax_bus_lock);
593 if (rc)
594 return rc;
Dan Williams9567da02017-07-12 17:58:21 -0700595 return driver_register(drv);
596}
597EXPORT_SYMBOL_GPL(__dax_driver_register);
598
Dan Williamsd2007812018-11-07 15:31:23 -0800599void dax_driver_unregister(struct dax_device_driver *dax_drv)
600{
Dan Williams730926c2017-07-16 13:51:53 -0700601 struct device_driver *drv = &dax_drv->drv;
Dan Williamsd2007812018-11-07 15:31:23 -0800602 struct dax_id *dax_id, *_id;
603
604 mutex_lock(&dax_bus_lock);
605 match_always_count -= dax_drv->match_always;
606 list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
607 list_del(&dax_id->list);
608 kfree(dax_id);
609 }
610 mutex_unlock(&dax_bus_lock);
Dan Williams730926c2017-07-16 13:51:53 -0700611 driver_unregister(drv);
Dan Williamsd2007812018-11-07 15:31:23 -0800612}
613EXPORT_SYMBOL_GPL(dax_driver_unregister);
614
Dan Williams9567da02017-07-12 17:58:21 -0700615int __init dax_bus_init(void)
616{
Dan Williams730926c2017-07-16 13:51:53 -0700617 int rc;
618
619 if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
620 dax_class = class_create(THIS_MODULE, "dax");
621 if (IS_ERR(dax_class))
622 return PTR_ERR(dax_class);
623 }
624
625 rc = bus_register(&dax_bus_type);
626 if (rc)
627 class_destroy(dax_class);
628 return rc;
Dan Williams9567da02017-07-12 17:58:21 -0700629}
630
631void __exit dax_bus_exit(void)
632{
633 bus_unregister(&dax_bus_type);
Dan Williams730926c2017-07-16 13:51:53 -0700634 class_destroy(dax_class);
Dan Williams9567da02017-07-12 17:58:21 -0700635}