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