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