^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright(c) 2017-2018 Intel Corporation. All rights reserved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/memremap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/dax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "dax-private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "bus.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static struct class *dax_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static DEFINE_MUTEX(dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DAX_NAME_LEN 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct dax_id {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) char dev_name[DAX_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int dax_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * We only ever expect to handle device-dax instances, i.e. the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return add_uevent_var(env, "MODALIAS=" DAX_DEVICE_MODALIAS_FMT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct dax_device_driver *to_dax_drv(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return container_of(drv, struct dax_device_driver, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct dax_id *__dax_match_id(struct dax_device_driver *dax_drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) const char *dev_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct dax_id *dax_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) lockdep_assert_held(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) list_for_each_entry(dax_id, &dax_drv->ids, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (sysfs_streq(dax_id->dev_name, dev_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return dax_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int dax_match_id(struct dax_device_driver *dax_drv, struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) mutex_lock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) match = !!__dax_match_id(dax_drv, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) mutex_unlock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) enum id_action {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ID_REMOVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ID_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static ssize_t do_id_store(struct device_driver *drv, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) size_t count, enum id_action action)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct dax_device_driver *dax_drv = to_dax_drv(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int region_id, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) char devname[DAX_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct dax_id *dax_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ssize_t rc = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fields = sscanf(buf, "dax%d.%d", ®ion_id, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (fields != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) sprintf(devname, "dax%d.%d", region_id, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!sysfs_streq(buf, devname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) mutex_lock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dax_id = __dax_match_id(dax_drv, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!dax_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (action == ID_ADD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) dax_id = kzalloc(sizeof(*dax_id), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (dax_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) strncpy(dax_id->dev_name, buf, DAX_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) list_add(&dax_id->list, &dax_drv->ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* nothing to remove */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) } else if (action == ID_REMOVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) list_del(&dax_id->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kfree(dax_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* dax_id already added */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) mutex_unlock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (action == ID_ADD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) rc = driver_attach(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static ssize_t new_id_store(struct device_driver *drv, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return do_id_store(drv, buf, count, ID_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static DRIVER_ATTR_WO(new_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static ssize_t remove_id_store(struct device_driver *drv, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return do_id_store(drv, buf, count, ID_REMOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static DRIVER_ATTR_WO(remove_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct attribute *dax_drv_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) &driver_attr_new_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) &driver_attr_remove_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ATTRIBUTE_GROUPS(dax_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int dax_bus_match(struct device *dev, struct device_driver *drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static bool is_static(struct dax_region *dax_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return (dax_region->res.flags & IORESOURCE_DAX_STATIC) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static u64 dev_dax_size(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u64 size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) device_lock_assert(&dev_dax->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) for (i = 0; i < dev_dax->nr_range; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) size += range_len(&dev_dax->ranges[i].range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int dax_bus_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (dev_dax_size(dev_dax) == 0 || dev_dax->id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) rc = dax_drv->probe(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (rc || is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Track new seed creation only after successful probe of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * previous seed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (dax_region->seed == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dax_region->seed = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int dax_bus_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct dax_device_driver *dax_drv = to_dax_drv(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return dax_drv->remove(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static struct bus_type dax_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .name = "dax",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .uevent = dax_bus_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .match = dax_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .probe = dax_bus_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .remove = dax_bus_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .drv_groups = dax_drv_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int dax_bus_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct dax_device_driver *dax_drv = to_dax_drv(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * All but the 'device-dax' driver, which has 'match_always'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * set, requires an exact id match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (dax_drv->match_always)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return dax_match_id(dax_drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * Rely on the fact that drvdata is set before the attributes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * registered, and that the attributes are unregistered before drvdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * is cleared to assume that drvdata is always valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static ssize_t id_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return sprintf(buf, "%d\n", dax_region->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static DEVICE_ATTR_RO(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static ssize_t region_size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return sprintf(buf, "%llu\n", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) resource_size(&dax_region->res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) region_size_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static ssize_t region_align_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return sprintf(buf, "%u\n", dax_region->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static struct device_attribute dev_attr_region_align =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) __ATTR(align, 0400, region_align_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #define for_each_dax_region_resource(dax_region, res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) for (res = (dax_region)->res.child; res; res = res->sibling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static unsigned long long dax_region_avail_size(struct dax_region *dax_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) resource_size_t size = resource_size(&dax_region->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) device_lock_assert(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) for_each_dax_region_resource(dax_region, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) size -= resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static ssize_t available_size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned long long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) size = dax_region_avail_size(dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return sprintf(buf, "%llu\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static DEVICE_ATTR_RO(available_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static ssize_t seed_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct device *seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) seed = dax_region->seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static DEVICE_ATTR_RO(seed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static ssize_t create_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct device *youngest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) youngest = dax_region->youngest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static ssize_t create_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) unsigned long long avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) rc = kstrtoint(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (val != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) avail = dax_region_avail_size(dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (avail == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) rc = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct dev_dax_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .dax_region = dax_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) .size = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) .id = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct dev_dax *dev_dax = devm_create_dev_dax(&data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (IS_ERR(dev_dax))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) rc = PTR_ERR(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * In support of crafting multiple new devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * simultaneously multiple seeds can be created,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * but only the first one that has not been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * successfully bound is tracked as the region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * seed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!dax_region->seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) dax_region->seed = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) dax_region->youngest = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) rc = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static DEVICE_ATTR_RW(create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) void kill_dev_dax(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct dax_device *dax_dev = dev_dax->dax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct inode *inode = dax_inode(dax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) kill_dax(dax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) unmap_mapping_range(inode->i_mapping, 0, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) EXPORT_SYMBOL_GPL(kill_dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static void trim_dev_dax_range(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int i = dev_dax->nr_range - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct range *range = &dev_dax->ranges[i].range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) device_lock_assert(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) dev_dbg(&dev_dax->dev, "delete range[%d]: %#llx:%#llx\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) (unsigned long long)range->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) (unsigned long long)range->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) __release_region(&dax_region->res, range->start, range_len(range));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (--dev_dax->nr_range == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) kfree(dev_dax->ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) dev_dax->ranges = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void free_dev_dax_ranges(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) while (dev_dax->nr_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) trim_dev_dax_range(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static void unregister_dev_dax(void *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) dev_dbg(dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) kill_dev_dax(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) free_dev_dax_ranges(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) device_del(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /* a return value >= 0 indicates this invocation invalidated the id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static int __free_dev_dax_id(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) int rc = dev_dax->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) device_lock_assert(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (is_static(dax_region) || dev_dax->id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ida_free(&dax_region->ida, dev_dax->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) dev_dax->id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int free_dev_dax_id(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) rc = __free_dev_dax_id(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static ssize_t delete_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct dev_dax *dev_dax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct device *victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) bool do_del = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) victim = device_find_child_by_name(dax_region->dev, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (!victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) device_lock(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) dev_dax = to_dev_dax(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (victim->driver || dev_dax_size(dev_dax))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * Invalidate the device so it does not become active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * again, but always preserve device-id-0 so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * /sys/bus/dax/ is guaranteed to be populated while any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * dax_region is registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (dev_dax->id > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) do_del = __free_dev_dax_id(dev_dax) >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) rc = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (dax_region->seed == victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) dax_region->seed = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (dax_region->youngest == victim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) dax_region->youngest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) device_unlock(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* won the race to invalidate the device, clean it up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (do_del)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) devm_release_action(dev, unregister_dev_dax, victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) put_device(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static DEVICE_ATTR_WO(delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct dax_region *dax_region = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (a == &dev_attr_available_size.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) || a == &dev_attr_create.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) || a == &dev_attr_seed.attr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) || a == &dev_attr_delete.attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return a->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) static struct attribute *dax_region_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) &dev_attr_available_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) &dev_attr_region_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) &dev_attr_region_align.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) &dev_attr_create.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) &dev_attr_seed.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) &dev_attr_delete.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) &dev_attr_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static const struct attribute_group dax_region_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) .name = "dax_region",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) .attrs = dax_region_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .is_visible = dax_region_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static const struct attribute_group *dax_region_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) &dax_region_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void dax_region_free(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct dax_region *dax_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) dax_region = container_of(kref, struct dax_region, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) kfree(dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) void dax_region_put(struct dax_region *dax_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) kref_put(&dax_region->kref, dax_region_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) EXPORT_SYMBOL_GPL(dax_region_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static void dax_region_unregister(void *region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct dax_region *dax_region = region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) sysfs_remove_groups(&dax_region->dev->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) dax_region_attribute_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dax_region_put(dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct dax_region *alloc_dax_region(struct device *parent, int region_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) struct range *range, int target_node, unsigned int align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct dax_region *dax_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * The DAX core assumes that it can store its private data in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * parent->driver_data. This WARN is a reminder / safeguard for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * developers of device-dax drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (dev_get_drvdata(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) dev_WARN(parent, "dax core failed to setup private data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (!IS_ALIGNED(range->start, align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) || !IS_ALIGNED(range_len(range), align))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (!dax_region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) dev_set_drvdata(parent, dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) kref_init(&dax_region->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) dax_region->id = region_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) dax_region->align = align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) dax_region->dev = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) dax_region->target_node = target_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) ida_init(&dax_region->ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) dax_region->res = (struct resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) .start = range->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) .end = range->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) .flags = IORESOURCE_MEM | flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) kfree(dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) kref_get(&dax_region->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return dax_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) EXPORT_SYMBOL_GPL(alloc_dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static void dax_mapping_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct dax_mapping *mapping = to_dax_mapping(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct dev_dax *dev_dax = to_dev_dax(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ida_free(&dev_dax->ida, mapping->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) kfree(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static void unregister_dax_mapping(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct device *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct dax_mapping *mapping = to_dax_mapping(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) struct dev_dax *dev_dax = to_dev_dax(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) dev_dbg(dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) device_lock_assert(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) dev_dax->ranges[mapping->range_id].mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) mapping->range_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) device_del(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) static struct dev_dax_range *get_dax_range(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) struct dax_mapping *mapping = to_dax_mapping(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct dev_dax *dev_dax = to_dev_dax(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) device_lock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (mapping->range_id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return &dev_dax->ranges[mapping->range_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) static void put_dax_range(struct dev_dax_range *dax_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct dax_mapping *mapping = dax_range->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct dev_dax *dev_dax = to_dev_dax(mapping->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static ssize_t start_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct dev_dax_range *dax_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) dax_range = get_dax_range(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!dax_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) rc = sprintf(buf, "%#llx\n", dax_range->range.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) put_dax_range(dax_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static DEVICE_ATTR(start, 0400, start_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static ssize_t end_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) struct dev_dax_range *dax_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) dax_range = get_dax_range(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (!dax_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) rc = sprintf(buf, "%#llx\n", dax_range->range.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) put_dax_range(dax_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static DEVICE_ATTR(end, 0400, end_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static ssize_t pgoff_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct dev_dax_range *dax_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) dax_range = get_dax_range(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (!dax_range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) rc = sprintf(buf, "%#lx\n", dax_range->pgoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) put_dax_range(dax_range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) static DEVICE_ATTR(page_offset, 0400, pgoff_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static struct attribute *dax_mapping_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) &dev_attr_start.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) &dev_attr_end.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) &dev_attr_page_offset.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static const struct attribute_group dax_mapping_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) .attrs = dax_mapping_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) static const struct attribute_group *dax_mapping_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) &dax_mapping_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) static struct device_type dax_mapping_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) .release = dax_mapping_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) .groups = dax_mapping_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static int devm_register_dax_mapping(struct dev_dax *dev_dax, int range_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) struct dax_mapping *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) device_lock_assert(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (dev_WARN_ONCE(&dev_dax->dev, !dax_region->dev->driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) "region disabled\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (!mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) mapping->range_id = range_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) mapping->id = ida_alloc(&dev_dax->ida, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (mapping->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) kfree(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) dev_dax->ranges[range_id].mapping = mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) dev = &mapping->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) dev->parent = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) dev->type = &dax_mapping_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) dev_set_name(dev, "mapping%d", mapping->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) rc = device_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) struct resource *res = &dax_region->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) struct dev_dax_range *ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) unsigned long pgoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) struct resource *alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) device_lock_assert(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /* handle the seed alloc special case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (!size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (dev_WARN_ONCE(dev, dev_dax->nr_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) "0-size allocation must be first\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) /* nr_range == 0 is elsewhere special cased as 0-size device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ranges = krealloc(dev_dax->ranges, sizeof(*ranges)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * (dev_dax->nr_range + 1), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (!ranges)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) alloc = __request_region(res, start, size, dev_name(dev), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) if (!alloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * If this was an empty set of ranges nothing else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * will release @ranges, so do it now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (!dev_dax->nr_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) kfree(ranges);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) ranges = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) dev_dax->ranges = ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) for (i = 0; i < dev_dax->nr_range; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) pgoff += PHYS_PFN(range_len(&ranges[i].range));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) dev_dax->ranges = ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) ranges[dev_dax->nr_range++] = (struct dev_dax_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) .pgoff = pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) .range = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) .start = alloc->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) .end = alloc->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) dev_dbg(dev, "alloc range[%d]: %pa:%pa\n", dev_dax->nr_range - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) &alloc->start, &alloc->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * A dev_dax instance must be registered before mapping device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * children can be added. Defer to devm_create_dev_dax() to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * the initial mapping device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (!device_is_registered(&dev_dax->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) rc = devm_register_dax_mapping(dev_dax, dev_dax->nr_range - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) trim_dev_dax_range(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) int last_range = dev_dax->nr_range - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) struct dev_dax_range *dax_range = &dev_dax->ranges[last_range];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) bool is_shrink = resource_size(res) > size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct range *range = &dax_range->range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) device_lock_assert(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (dev_WARN_ONCE(dev, !size, "deletion is handled by dev_dax_shrink\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) rc = adjust_resource(res, range->start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) *range = (struct range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) .start = range->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) .end = range->start + size - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) dev_dbg(dev, "%s range[%d]: %#llx:%#llx\n", is_shrink ? "shrink" : "extend",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) last_range, (unsigned long long) range->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) (unsigned long long) range->end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) static ssize_t size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) unsigned long long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) size = dev_dax_size(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return sprintf(buf, "%llu\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) static bool alloc_is_aligned(struct dev_dax *dev_dax, resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * The minimum mapping granularity for a device instance is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * single subsection, unless the arch says otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) return IS_ALIGNED(size, max_t(unsigned long, dev_dax->align, memremap_compat_align()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) resource_size_t to_shrink = dev_dax_size(dev_dax) - size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) for (i = dev_dax->nr_range - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) struct range *range = &dev_dax->ranges[i].range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) struct dax_mapping *mapping = dev_dax->ranges[i].mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) struct resource *adjust = NULL, *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) resource_size_t shrink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) shrink = min_t(u64, to_shrink, range_len(range));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (shrink >= range_len(range)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) devm_release_action(dax_region->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) unregister_dax_mapping, &mapping->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) trim_dev_dax_range(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) to_shrink -= shrink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) if (!to_shrink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) for_each_dax_region_resource(dax_region, res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (strcmp(res->name, dev_name(dev)) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) && res->start == range->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) adjust = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (dev_WARN_ONCE(dev, !adjust || i != dev_dax->nr_range - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) "failed to find matching resource\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) return adjust_dev_dax_range(dev_dax, adjust, range_len(range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) - shrink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * Only allow adjustments that preserve the relative pgoff of existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) * allocations. I.e. the dev_dax->ranges array is ordered by increasing pgoff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) static bool adjust_ok(struct dev_dax *dev_dax, struct resource *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) struct dev_dax_range *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (dev_dax->nr_range == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) if (strcmp(res->name, dev_name(&dev_dax->dev)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) last = &dev_dax->ranges[dev_dax->nr_range - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if (last->range.start != res->start || last->range.end != res->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) for (i = 0; i < dev_dax->nr_range - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) struct dev_dax_range *dax_range = &dev_dax->ranges[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) if (dax_range->pgoff > last->pgoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) static ssize_t dev_dax_resize(struct dax_region *dax_region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) struct dev_dax *dev_dax, resource_size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) resource_size_t avail = dax_region_avail_size(dax_region), to_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) resource_size_t dev_size = dev_dax_size(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) struct resource *region_res = &dax_region->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) struct resource *res, *first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) resource_size_t alloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (size == dev_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (size > dev_size && size - dev_size > avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (size < dev_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) return dev_dax_shrink(dev_dax, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) to_alloc = size - dev_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (dev_WARN_ONCE(dev, !alloc_is_aligned(dev_dax, to_alloc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) "resize of %pa misaligned\n", &to_alloc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * Expand the device into the unused portion of the region. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * may involve adjusting the end of an existing resource, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * allocating a new resource.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) first = region_res->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (!first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) rc = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) for (res = first; res; res = res->sibling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct resource *next = res->sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) /* space at the beginning of the region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (res == first && res->start > dax_region->res.start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) alloc = min(res->start - dax_region->res.start, to_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) alloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) /* space between allocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (next && next->start > res->end + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) alloc = min(next->start - (res->end + 1), to_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) /* space at the end of the region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (!alloc && !next && res->end < region_res->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) alloc = min(region_res->end - res->end, to_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (!alloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (adjust_ok(dev_dax, res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) rc = adjust_dev_dax_range(dev_dax, res, resource_size(res) + alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) rc = alloc_dev_dax_range(dev_dax, res->end + 1, alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) to_alloc -= alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (to_alloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) static ssize_t size_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) unsigned long long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) rc = kstrtoull(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (!alloc_is_aligned(dev_dax, val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) device_lock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (!dax_region->dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) rc = dev_dax_resize(dax_region, dev_dax, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return rc == 0 ? len : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) static DEVICE_ATTR_RW(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static ssize_t range_parse(const char *opt, size_t len, struct range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) unsigned long long addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) char *start, *end, *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) ssize_t rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) str = kstrdup(opt, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) end = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) start = strsep(&end, "-");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (!start || !end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) rc = kstrtoull(start, 16, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) range->start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) rc = kstrtoull(end, 16, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) range->end = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) kfree(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) static ssize_t mapping_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) size_t to_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) struct range r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) rc = range_parse(buf, len, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) rc = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) device_lock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) if (!dax_region->dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) to_alloc = range_len(&r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if (alloc_is_aligned(dev_dax, to_alloc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) return rc == 0 ? len : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) static DEVICE_ATTR_WO(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) static ssize_t align_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) return sprintf(buf, "%d\n", dev_dax->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) static ssize_t dev_dax_validate_align(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) resource_size_t dev_size = dev_dax_size(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (dev_size > 0 && !alloc_is_aligned(dev_dax, dev_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) dev_dbg(dev, "%s: align %u invalid for size %pa\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) __func__, dev_dax->align, &dev_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) for (i = 0; i < dev_dax->nr_range; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) size_t len = range_len(&dev_dax->ranges[i].range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (!alloc_is_aligned(dev_dax, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) dev_dbg(dev, "%s: align %u invalid for range %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) __func__, dev_dax->align, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static ssize_t align_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) unsigned long val, align_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) rc = kstrtoul(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (!dax_align_valid(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) device_lock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (!dax_region->dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) align_save = dev_dax->align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) dev_dax->align = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) rc = dev_dax_validate_align(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) dev_dax->align = align_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) device_unlock(dax_region->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) return rc == 0 ? len : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) static DEVICE_ATTR_RW(align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) static int dev_dax_target_node(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) return dax_region->target_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) static ssize_t target_node_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) static DEVICE_ATTR_RO(target_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) static ssize_t resource_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) unsigned long long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (dev_dax->nr_range < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) start = dax_region->res.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) start = dev_dax->ranges[0].range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) return sprintf(buf, "%#llx\n", start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) static DEVICE_ATTR(resource, 0400, resource_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) * We only ever expect to handle device-dax instances, i.e. the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) static DEVICE_ATTR_RO(modalias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) static ssize_t numa_node_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) return sprintf(buf, "%d\n", dev_to_node(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) static DEVICE_ATTR_RO(numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) struct device *dev = container_of(kobj, struct device, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (a == &dev_attr_mapping.attr && is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) if ((a == &dev_attr_align.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) a == &dev_attr_size.attr) && is_static(dax_region))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) return 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) return a->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static struct attribute *dev_dax_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) &dev_attr_modalias.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) &dev_attr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) &dev_attr_mapping.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) &dev_attr_target_node.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) &dev_attr_align.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) &dev_attr_resource.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) &dev_attr_numa_node.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) static const struct attribute_group dev_dax_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) .attrs = dev_dax_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) .is_visible = dev_dax_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) static const struct attribute_group *dax_attribute_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) &dev_dax_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) static void dev_dax_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) struct dev_dax *dev_dax = to_dev_dax(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) struct dax_region *dax_region = dev_dax->region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) struct dax_device *dax_dev = dev_dax->dax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) put_dax(dax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) free_dev_dax_id(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) dax_region_put(dax_region);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) kfree(dev_dax->pgmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) kfree(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) static const struct device_type dev_dax_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) .release = dev_dax_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) .groups = dax_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) struct dax_region *dax_region = data->dax_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) struct device *parent = dax_region->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) struct dax_device *dax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) struct dev_dax *dev_dax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) if (!dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) if (is_static(dax_region)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (dev_WARN_ONCE(parent, data->id < 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) "dynamic id specified to static region\n")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) goto err_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) dev_dax->id = data->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (dev_WARN_ONCE(parent, data->id >= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) "static id specified to dynamic region\n")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) goto err_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) rc = ida_alloc(&dax_region->ida, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) goto err_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) dev_dax->id = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) dev_dax->region = dax_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) rc = alloc_dev_dax_range(dev_dax, dax_region->res.start, data->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) goto err_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) if (data->pgmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) dev_WARN_ONCE(parent, !is_static(dax_region),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) "custom dev_pagemap requires a static dax_region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) dev_dax->pgmap = kmemdup(data->pgmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) sizeof(struct dev_pagemap), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) if (!dev_dax->pgmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) goto err_pgmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) * No 'host' or dax_operations since there is no access to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) * device outside of mmap of the resulting character device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) if (IS_ERR(dax_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) rc = PTR_ERR(dax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) goto err_alloc_dax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) /* a device_dax instance is dead while the driver is not attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) kill_dax(dax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) dev_dax->dax_dev = dax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) dev_dax->target_node = dax_region->target_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) dev_dax->align = dax_region->align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) ida_init(&dev_dax->ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) kref_get(&dax_region->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) inode = dax_inode(dax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) dev->devt = inode->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) if (data->subsys == DEV_DAX_BUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) dev->bus = &dax_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) dev->class = dax_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) dev->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) dev->type = &dev_dax_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) rc = device_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) kill_dev_dax(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) /* register mapping device for the initial allocation range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (dev_dax->nr_range && range_len(&dev_dax->ranges[0].range)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) rc = devm_register_dax_mapping(dev_dax, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) return dev_dax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) err_alloc_dax:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) kfree(dev_dax->pgmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) err_pgmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) free_dev_dax_ranges(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) err_range:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) free_dev_dax_id(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) err_id:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) kfree(dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) EXPORT_SYMBOL_GPL(devm_create_dev_dax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) static int match_always_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) int __dax_driver_register(struct dax_device_driver *dax_drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) struct module *module, const char *mod_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) struct device_driver *drv = &dax_drv->drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) INIT_LIST_HEAD(&dax_drv->ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) drv->owner = module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) drv->name = mod_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) drv->mod_name = mod_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) drv->bus = &dax_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /* there can only be one default driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) mutex_lock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) match_always_count += dax_drv->match_always;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) if (match_always_count > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) match_always_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) mutex_unlock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return driver_register(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) EXPORT_SYMBOL_GPL(__dax_driver_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) void dax_driver_unregister(struct dax_device_driver *dax_drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) struct device_driver *drv = &dax_drv->drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) struct dax_id *dax_id, *_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) mutex_lock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) match_always_count -= dax_drv->match_always;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) list_del(&dax_id->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) kfree(dax_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) mutex_unlock(&dax_bus_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) driver_unregister(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) EXPORT_SYMBOL_GPL(dax_driver_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) int __init dax_bus_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) dax_class = class_create(THIS_MODULE, "dax");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (IS_ERR(dax_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) return PTR_ERR(dax_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) rc = bus_register(&dax_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) class_destroy(dax_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) void __exit dax_bus_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) bus_unregister(&dax_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) class_destroy(dax_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }