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 | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 139 | static int dax_bus_probe(struct device *dev) |
| 140 | { |
| 141 | struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); |
| 142 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 143 | struct dax_region *dax_region = dev_dax->region; |
| 144 | struct range *range = &dev_dax->range; |
| 145 | int rc; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 146 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 147 | if (range_len(range) == 0 || dev_dax->id < 0) |
| 148 | return -ENXIO; |
| 149 | |
| 150 | rc = dax_drv->probe(dev_dax); |
| 151 | |
| 152 | if (rc || is_static(dax_region)) |
| 153 | return rc; |
| 154 | |
| 155 | /* |
| 156 | * Track new seed creation only after successful probe of the |
| 157 | * previous seed. |
| 158 | */ |
| 159 | if (dax_region->seed == dev) |
| 160 | dax_region->seed = NULL; |
| 161 | |
| 162 | return 0; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int dax_bus_remove(struct device *dev) |
| 166 | { |
| 167 | struct dax_device_driver *dax_drv = to_dax_drv(dev->driver); |
| 168 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 169 | |
| 170 | return dax_drv->remove(dev_dax); |
| 171 | } |
| 172 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 173 | static struct bus_type dax_bus_type = { |
| 174 | .name = "dax", |
| 175 | .uevent = dax_bus_uevent, |
| 176 | .match = dax_bus_match, |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 177 | .probe = dax_bus_probe, |
| 178 | .remove = dax_bus_remove, |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 179 | .drv_groups = dax_drv_groups, |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static int dax_bus_match(struct device *dev, struct device_driver *drv) |
| 183 | { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 184 | struct dax_device_driver *dax_drv = to_dax_drv(drv); |
| 185 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 186 | /* |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 187 | * All but the 'device-dax' driver, which has 'match_always' |
| 188 | * set, requires an exact id match. |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 189 | */ |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 190 | if (dax_drv->match_always) |
| 191 | return 1; |
| 192 | |
| 193 | return dax_match_id(dax_drv, dev); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 196 | /* |
| 197 | * Rely on the fact that drvdata is set before the attributes are |
| 198 | * registered, and that the attributes are unregistered before drvdata |
| 199 | * is cleared to assume that drvdata is always valid. |
| 200 | */ |
| 201 | static ssize_t id_show(struct device *dev, |
| 202 | struct device_attribute *attr, char *buf) |
| 203 | { |
| 204 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 205 | |
| 206 | return sprintf(buf, "%d\n", dax_region->id); |
| 207 | } |
| 208 | static DEVICE_ATTR_RO(id); |
| 209 | |
| 210 | static ssize_t region_size_show(struct device *dev, |
| 211 | struct device_attribute *attr, char *buf) |
| 212 | { |
| 213 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 214 | |
| 215 | return sprintf(buf, "%llu\n", (unsigned long long) |
| 216 | resource_size(&dax_region->res)); |
| 217 | } |
| 218 | static struct device_attribute dev_attr_region_size = __ATTR(size, 0444, |
| 219 | region_size_show, NULL); |
| 220 | |
| 221 | static ssize_t align_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, "%u\n", dax_region->align); |
| 227 | } |
| 228 | static DEVICE_ATTR_RO(align); |
| 229 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 230 | #define for_each_dax_region_resource(dax_region, res) \ |
| 231 | for (res = (dax_region)->res.child; res; res = res->sibling) |
| 232 | |
| 233 | static unsigned long long dax_region_avail_size(struct dax_region *dax_region) |
| 234 | { |
| 235 | resource_size_t size = resource_size(&dax_region->res); |
| 236 | struct resource *res; |
| 237 | |
| 238 | device_lock_assert(dax_region->dev); |
| 239 | |
| 240 | for_each_dax_region_resource(dax_region, res) |
| 241 | size -= resource_size(res); |
| 242 | return size; |
| 243 | } |
| 244 | |
| 245 | static ssize_t available_size_show(struct device *dev, |
| 246 | struct device_attribute *attr, char *buf) |
| 247 | { |
| 248 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 249 | unsigned long long size; |
| 250 | |
| 251 | device_lock(dev); |
| 252 | size = dax_region_avail_size(dax_region); |
| 253 | device_unlock(dev); |
| 254 | |
| 255 | return sprintf(buf, "%llu\n", size); |
| 256 | } |
| 257 | static DEVICE_ATTR_RO(available_size); |
| 258 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 259 | static ssize_t seed_show(struct device *dev, |
| 260 | struct device_attribute *attr, char *buf) |
| 261 | { |
| 262 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 263 | struct device *seed; |
| 264 | ssize_t rc; |
| 265 | |
| 266 | if (is_static(dax_region)) |
| 267 | return -EINVAL; |
| 268 | |
| 269 | device_lock(dev); |
| 270 | seed = dax_region->seed; |
| 271 | rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : ""); |
| 272 | device_unlock(dev); |
| 273 | |
| 274 | return rc; |
| 275 | } |
| 276 | static DEVICE_ATTR_RO(seed); |
| 277 | |
| 278 | static ssize_t create_show(struct device *dev, |
| 279 | struct device_attribute *attr, char *buf) |
| 280 | { |
| 281 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 282 | struct device *youngest; |
| 283 | ssize_t rc; |
| 284 | |
| 285 | if (is_static(dax_region)) |
| 286 | return -EINVAL; |
| 287 | |
| 288 | device_lock(dev); |
| 289 | youngest = dax_region->youngest; |
| 290 | rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : ""); |
| 291 | device_unlock(dev); |
| 292 | |
| 293 | return rc; |
| 294 | } |
| 295 | |
| 296 | static ssize_t create_store(struct device *dev, struct device_attribute *attr, |
| 297 | const char *buf, size_t len) |
| 298 | { |
| 299 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 300 | unsigned long long avail; |
| 301 | ssize_t rc; |
| 302 | int val; |
| 303 | |
| 304 | if (is_static(dax_region)) |
| 305 | return -EINVAL; |
| 306 | |
| 307 | rc = kstrtoint(buf, 0, &val); |
| 308 | if (rc) |
| 309 | return rc; |
| 310 | if (val != 1) |
| 311 | return -EINVAL; |
| 312 | |
| 313 | device_lock(dev); |
| 314 | avail = dax_region_avail_size(dax_region); |
| 315 | if (avail == 0) |
| 316 | rc = -ENOSPC; |
| 317 | else { |
| 318 | struct dev_dax_data data = { |
| 319 | .dax_region = dax_region, |
| 320 | .size = 0, |
| 321 | .id = -1, |
| 322 | }; |
| 323 | struct dev_dax *dev_dax = devm_create_dev_dax(&data); |
| 324 | |
| 325 | if (IS_ERR(dev_dax)) |
| 326 | rc = PTR_ERR(dev_dax); |
| 327 | else { |
| 328 | /* |
| 329 | * In support of crafting multiple new devices |
| 330 | * simultaneously multiple seeds can be created, |
| 331 | * but only the first one that has not been |
| 332 | * successfully bound is tracked as the region |
| 333 | * seed. |
| 334 | */ |
| 335 | if (!dax_region->seed) |
| 336 | dax_region->seed = &dev_dax->dev; |
| 337 | dax_region->youngest = &dev_dax->dev; |
| 338 | rc = len; |
| 339 | } |
| 340 | } |
| 341 | device_unlock(dev); |
| 342 | |
| 343 | return rc; |
| 344 | } |
| 345 | static DEVICE_ATTR_RW(create); |
| 346 | |
| 347 | void kill_dev_dax(struct dev_dax *dev_dax) |
| 348 | { |
| 349 | struct dax_device *dax_dev = dev_dax->dax_dev; |
| 350 | struct inode *inode = dax_inode(dax_dev); |
| 351 | |
| 352 | kill_dax(dax_dev); |
| 353 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(kill_dev_dax); |
| 356 | |
| 357 | static void free_dev_dax_range(struct dev_dax *dev_dax) |
| 358 | { |
| 359 | struct dax_region *dax_region = dev_dax->region; |
| 360 | struct range *range = &dev_dax->range; |
| 361 | |
| 362 | device_lock_assert(dax_region->dev); |
| 363 | if (range_len(range)) |
| 364 | __release_region(&dax_region->res, range->start, |
| 365 | range_len(range)); |
| 366 | } |
| 367 | |
| 368 | static void unregister_dev_dax(void *dev) |
| 369 | { |
| 370 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 371 | |
| 372 | dev_dbg(dev, "%s\n", __func__); |
| 373 | |
| 374 | kill_dev_dax(dev_dax); |
| 375 | free_dev_dax_range(dev_dax); |
| 376 | device_del(dev); |
| 377 | put_device(dev); |
| 378 | } |
| 379 | |
| 380 | /* a return value >= 0 indicates this invocation invalidated the id */ |
| 381 | static int __free_dev_dax_id(struct dev_dax *dev_dax) |
| 382 | { |
| 383 | struct dax_region *dax_region = dev_dax->region; |
| 384 | struct device *dev = &dev_dax->dev; |
| 385 | int rc = dev_dax->id; |
| 386 | |
| 387 | device_lock_assert(dev); |
| 388 | |
| 389 | if (is_static(dax_region) || dev_dax->id < 0) |
| 390 | return -1; |
| 391 | ida_free(&dax_region->ida, dev_dax->id); |
| 392 | dev_dax->id = -1; |
| 393 | return rc; |
| 394 | } |
| 395 | |
| 396 | static int free_dev_dax_id(struct dev_dax *dev_dax) |
| 397 | { |
| 398 | struct device *dev = &dev_dax->dev; |
| 399 | int rc; |
| 400 | |
| 401 | device_lock(dev); |
| 402 | rc = __free_dev_dax_id(dev_dax); |
| 403 | device_unlock(dev); |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | static ssize_t delete_store(struct device *dev, struct device_attribute *attr, |
| 408 | const char *buf, size_t len) |
| 409 | { |
| 410 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 411 | struct dev_dax *dev_dax; |
| 412 | struct device *victim; |
| 413 | bool do_del = false; |
| 414 | int rc; |
| 415 | |
| 416 | if (is_static(dax_region)) |
| 417 | return -EINVAL; |
| 418 | |
| 419 | victim = device_find_child_by_name(dax_region->dev, buf); |
| 420 | if (!victim) |
| 421 | return -ENXIO; |
| 422 | |
| 423 | device_lock(dev); |
| 424 | device_lock(victim); |
| 425 | dev_dax = to_dev_dax(victim); |
| 426 | if (victim->driver || range_len(&dev_dax->range)) |
| 427 | rc = -EBUSY; |
| 428 | else { |
| 429 | /* |
| 430 | * Invalidate the device so it does not become active |
| 431 | * again, but always preserve device-id-0 so that |
| 432 | * /sys/bus/dax/ is guaranteed to be populated while any |
| 433 | * dax_region is registered. |
| 434 | */ |
| 435 | if (dev_dax->id > 0) { |
| 436 | do_del = __free_dev_dax_id(dev_dax) >= 0; |
| 437 | rc = len; |
| 438 | if (dax_region->seed == victim) |
| 439 | dax_region->seed = NULL; |
| 440 | if (dax_region->youngest == victim) |
| 441 | dax_region->youngest = NULL; |
| 442 | } else |
| 443 | rc = -EBUSY; |
| 444 | } |
| 445 | device_unlock(victim); |
| 446 | |
| 447 | /* won the race to invalidate the device, clean it up */ |
| 448 | if (do_del) |
| 449 | devm_release_action(dev, unregister_dev_dax, victim); |
| 450 | device_unlock(dev); |
| 451 | put_device(victim); |
| 452 | |
| 453 | return rc; |
| 454 | } |
| 455 | static DEVICE_ATTR_WO(delete); |
| 456 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 457 | static umode_t dax_region_visible(struct kobject *kobj, struct attribute *a, |
| 458 | int n) |
| 459 | { |
| 460 | struct device *dev = container_of(kobj, struct device, kobj); |
| 461 | struct dax_region *dax_region = dev_get_drvdata(dev); |
| 462 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 463 | if (is_static(dax_region)) |
| 464 | if (a == &dev_attr_available_size.attr |
| 465 | || a == &dev_attr_create.attr |
| 466 | || a == &dev_attr_seed.attr |
| 467 | || a == &dev_attr_delete.attr) |
| 468 | return 0; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 469 | return a->mode; |
| 470 | } |
| 471 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 472 | static struct attribute *dax_region_attributes[] = { |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 473 | &dev_attr_available_size.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 474 | &dev_attr_region_size.attr, |
| 475 | &dev_attr_align.attr, |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 476 | &dev_attr_create.attr, |
| 477 | &dev_attr_seed.attr, |
| 478 | &dev_attr_delete.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 479 | &dev_attr_id.attr, |
| 480 | NULL, |
| 481 | }; |
| 482 | |
| 483 | static const struct attribute_group dax_region_attribute_group = { |
| 484 | .name = "dax_region", |
| 485 | .attrs = dax_region_attributes, |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 486 | .is_visible = dax_region_visible, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | static const struct attribute_group *dax_region_attribute_groups[] = { |
| 490 | &dax_region_attribute_group, |
| 491 | NULL, |
| 492 | }; |
| 493 | |
| 494 | static void dax_region_free(struct kref *kref) |
| 495 | { |
| 496 | struct dax_region *dax_region; |
| 497 | |
| 498 | dax_region = container_of(kref, struct dax_region, kref); |
| 499 | kfree(dax_region); |
| 500 | } |
| 501 | |
| 502 | void dax_region_put(struct dax_region *dax_region) |
| 503 | { |
| 504 | kref_put(&dax_region->kref, dax_region_free); |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(dax_region_put); |
| 507 | |
| 508 | static void dax_region_unregister(void *region) |
| 509 | { |
| 510 | struct dax_region *dax_region = region; |
| 511 | |
| 512 | sysfs_remove_groups(&dax_region->dev->kobj, |
| 513 | dax_region_attribute_groups); |
| 514 | dax_region_put(dax_region); |
| 515 | } |
| 516 | |
| 517 | struct dax_region *alloc_dax_region(struct device *parent, int region_id, |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 518 | struct resource *res, int target_node, unsigned int align, |
| 519 | unsigned long flags) |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 520 | { |
| 521 | struct dax_region *dax_region; |
| 522 | |
| 523 | /* |
| 524 | * The DAX core assumes that it can store its private data in |
| 525 | * parent->driver_data. This WARN is a reminder / safeguard for |
| 526 | * developers of device-dax drivers. |
| 527 | */ |
| 528 | if (dev_get_drvdata(parent)) { |
| 529 | dev_WARN(parent, "dax core failed to setup private data\n"); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | if (!IS_ALIGNED(res->start, align) |
| 534 | || !IS_ALIGNED(resource_size(res), align)) |
| 535 | return NULL; |
| 536 | |
| 537 | dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL); |
| 538 | if (!dax_region) |
| 539 | return NULL; |
| 540 | |
| 541 | dev_set_drvdata(parent, dax_region); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 542 | kref_init(&dax_region->kref); |
| 543 | dax_region->id = region_id; |
| 544 | dax_region->align = align; |
| 545 | dax_region->dev = parent; |
Dan Williams | 8fc5c73 | 2018-11-09 12:43:07 -0800 | [diff] [blame] | 546 | dax_region->target_node = target_node; |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 547 | ida_init(&dax_region->ida); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 548 | dax_region->res = (struct resource) { |
| 549 | .start = res->start, |
| 550 | .end = res->end, |
| 551 | .flags = IORESOURCE_MEM | flags, |
| 552 | }; |
| 553 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 554 | if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) { |
| 555 | kfree(dax_region); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | kref_get(&dax_region->kref); |
| 560 | if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region)) |
| 561 | return NULL; |
| 562 | return dax_region; |
| 563 | } |
| 564 | EXPORT_SYMBOL_GPL(alloc_dax_region); |
| 565 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 566 | static int alloc_dev_dax_range(struct dev_dax *dev_dax, u64 start, |
| 567 | resource_size_t size) |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 568 | { |
| 569 | struct dax_region *dax_region = dev_dax->region; |
| 570 | struct resource *res = &dax_region->res; |
| 571 | struct device *dev = &dev_dax->dev; |
| 572 | struct resource *alloc; |
| 573 | |
| 574 | device_lock_assert(dax_region->dev); |
| 575 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 576 | /* handle the seed alloc special case */ |
| 577 | if (!size) { |
| 578 | dev_dax->range = (struct range) { |
| 579 | .start = res->start, |
| 580 | .end = res->start - 1, |
| 581 | }; |
| 582 | return 0; |
| 583 | } |
| 584 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 585 | alloc = __request_region(res, start, size, dev_name(dev), 0); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 586 | if (!alloc) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | dev_dax->range = (struct range) { |
| 590 | .start = alloc->start, |
| 591 | .end = alloc->end, |
| 592 | }; |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 597 | static int adjust_dev_dax_range(struct dev_dax *dev_dax, struct resource *res, resource_size_t size) |
| 598 | { |
| 599 | struct dax_region *dax_region = dev_dax->region; |
| 600 | struct range *range = &dev_dax->range; |
| 601 | int rc = 0; |
| 602 | |
| 603 | device_lock_assert(dax_region->dev); |
| 604 | |
| 605 | if (size) |
| 606 | rc = adjust_resource(res, range->start, size); |
| 607 | else |
| 608 | __release_region(&dax_region->res, range->start, range_len(range)); |
| 609 | if (rc) |
| 610 | return rc; |
| 611 | |
| 612 | dev_dax->range = (struct range) { |
| 613 | .start = range->start, |
| 614 | .end = range->start + size - 1, |
| 615 | }; |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 620 | static ssize_t size_show(struct device *dev, |
| 621 | struct device_attribute *attr, char *buf) |
| 622 | { |
| 623 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 624 | unsigned long long size = range_len(&dev_dax->range); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 625 | |
| 626 | return sprintf(buf, "%llu\n", size); |
| 627 | } |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 628 | |
| 629 | static bool alloc_is_aligned(struct dax_region *dax_region, |
| 630 | resource_size_t size) |
| 631 | { |
| 632 | /* |
| 633 | * The minimum mapping granularity for a device instance is a |
| 634 | * single subsection, unless the arch says otherwise. |
| 635 | */ |
| 636 | return IS_ALIGNED(size, max_t(unsigned long, dax_region->align, |
| 637 | memremap_compat_align())); |
| 638 | } |
| 639 | |
| 640 | static int dev_dax_shrink(struct dev_dax *dev_dax, resource_size_t size) |
| 641 | { |
| 642 | struct dax_region *dax_region = dev_dax->region; |
| 643 | struct range *range = &dev_dax->range; |
| 644 | struct resource *res, *adjust = NULL; |
| 645 | struct device *dev = &dev_dax->dev; |
| 646 | |
| 647 | for_each_dax_region_resource(dax_region, res) |
| 648 | if (strcmp(res->name, dev_name(dev)) == 0 |
| 649 | && res->start == range->start) { |
| 650 | adjust = res; |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | if (dev_WARN_ONCE(dev, !adjust, "failed to find matching resource\n")) |
| 655 | return -ENXIO; |
| 656 | return adjust_dev_dax_range(dev_dax, adjust, size); |
| 657 | } |
| 658 | |
| 659 | static ssize_t dev_dax_resize(struct dax_region *dax_region, |
| 660 | struct dev_dax *dev_dax, resource_size_t size) |
| 661 | { |
| 662 | resource_size_t avail = dax_region_avail_size(dax_region), to_alloc; |
| 663 | resource_size_t dev_size = range_len(&dev_dax->range); |
| 664 | struct resource *region_res = &dax_region->res; |
| 665 | struct device *dev = &dev_dax->dev; |
| 666 | const char *name = dev_name(dev); |
| 667 | struct resource *res, *first; |
| 668 | |
| 669 | if (dev->driver) |
| 670 | return -EBUSY; |
| 671 | if (size == dev_size) |
| 672 | return 0; |
| 673 | if (size > dev_size && size - dev_size > avail) |
| 674 | return -ENOSPC; |
| 675 | if (size < dev_size) |
| 676 | return dev_dax_shrink(dev_dax, size); |
| 677 | |
| 678 | to_alloc = size - dev_size; |
| 679 | if (dev_WARN_ONCE(dev, !alloc_is_aligned(dax_region, to_alloc), |
| 680 | "resize of %pa misaligned\n", &to_alloc)) |
| 681 | return -ENXIO; |
| 682 | |
| 683 | /* |
| 684 | * Expand the device into the unused portion of the region. This |
| 685 | * may involve adjusting the end of an existing resource, or |
| 686 | * allocating a new resource. |
| 687 | */ |
| 688 | first = region_res->child; |
| 689 | if (!first) |
| 690 | return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc); |
| 691 | for (res = first; to_alloc && res; res = res->sibling) { |
| 692 | struct resource *next = res->sibling; |
| 693 | resource_size_t free; |
| 694 | |
| 695 | /* space at the beginning of the region */ |
| 696 | free = 0; |
| 697 | if (res == first && res->start > dax_region->res.start) |
| 698 | free = res->start - dax_region->res.start; |
| 699 | if (free >= to_alloc && dev_size == 0) |
| 700 | return alloc_dev_dax_range(dev_dax, dax_region->res.start, to_alloc); |
| 701 | |
| 702 | free = 0; |
| 703 | /* space between allocations */ |
| 704 | if (next && next->start > res->end + 1) |
| 705 | free = next->start - res->end + 1; |
| 706 | |
| 707 | /* space at the end of the region */ |
| 708 | if (free < to_alloc && !next && res->end < region_res->end) |
| 709 | free = region_res->end - res->end; |
| 710 | |
| 711 | if (free >= to_alloc && strcmp(name, res->name) == 0) |
| 712 | return adjust_dev_dax_range(dev_dax, res, resource_size(res) + to_alloc); |
| 713 | else if (free >= to_alloc && dev_size == 0) |
| 714 | return alloc_dev_dax_range(dev_dax, res->end + 1, to_alloc); |
| 715 | } |
| 716 | return -ENOSPC; |
| 717 | } |
| 718 | |
| 719 | static ssize_t size_store(struct device *dev, struct device_attribute *attr, |
| 720 | const char *buf, size_t len) |
| 721 | { |
| 722 | ssize_t rc; |
| 723 | unsigned long long val; |
| 724 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 725 | struct dax_region *dax_region = dev_dax->region; |
| 726 | |
| 727 | rc = kstrtoull(buf, 0, &val); |
| 728 | if (rc) |
| 729 | return rc; |
| 730 | |
| 731 | if (!alloc_is_aligned(dax_region, val)) { |
| 732 | dev_dbg(dev, "%s: size: %lld misaligned\n", __func__, val); |
| 733 | return -EINVAL; |
| 734 | } |
| 735 | |
| 736 | device_lock(dax_region->dev); |
| 737 | if (!dax_region->dev->driver) { |
| 738 | device_unlock(dax_region->dev); |
| 739 | return -ENXIO; |
| 740 | } |
| 741 | device_lock(dev); |
| 742 | rc = dev_dax_resize(dax_region, dev_dax, val); |
| 743 | device_unlock(dev); |
| 744 | device_unlock(dax_region->dev); |
| 745 | |
| 746 | return rc == 0 ? len : rc; |
| 747 | } |
| 748 | static DEVICE_ATTR_RW(size); |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 749 | |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 750 | static int dev_dax_target_node(struct dev_dax *dev_dax) |
| 751 | { |
| 752 | struct dax_region *dax_region = dev_dax->region; |
| 753 | |
| 754 | return dax_region->target_node; |
| 755 | } |
| 756 | |
| 757 | static ssize_t target_node_show(struct device *dev, |
| 758 | struct device_attribute *attr, char *buf) |
| 759 | { |
| 760 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 761 | |
| 762 | return sprintf(buf, "%d\n", dev_dax_target_node(dev_dax)); |
| 763 | } |
| 764 | static DEVICE_ATTR_RO(target_node); |
| 765 | |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 766 | static ssize_t resource_show(struct device *dev, |
| 767 | struct device_attribute *attr, char *buf) |
| 768 | { |
| 769 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 770 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 771 | return sprintf(buf, "%#llx\n", dev_dax->range.start); |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 772 | } |
Dan Williams | 153dd28 | 2019-11-12 17:12:38 -0800 | [diff] [blame] | 773 | static DEVICE_ATTR(resource, 0400, resource_show, NULL); |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 774 | |
Vishal Verma | c347bd7 | 2019-02-22 16:58:54 -0700 | [diff] [blame] | 775 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 776 | char *buf) |
| 777 | { |
| 778 | /* |
| 779 | * We only ever expect to handle device-dax instances, i.e. the |
| 780 | * @type argument to MODULE_ALIAS_DAX_DEVICE() is always zero |
| 781 | */ |
| 782 | return sprintf(buf, DAX_DEVICE_MODALIAS_FMT "\n", 0); |
| 783 | } |
| 784 | static DEVICE_ATTR_RO(modalias); |
| 785 | |
Dan Williams | cb4dd72 | 2019-11-12 17:13:31 -0800 | [diff] [blame] | 786 | static ssize_t numa_node_show(struct device *dev, |
| 787 | struct device_attribute *attr, char *buf) |
| 788 | { |
| 789 | return sprintf(buf, "%d\n", dev_to_node(dev)); |
| 790 | } |
| 791 | static DEVICE_ATTR_RO(numa_node); |
| 792 | |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 793 | static umode_t dev_dax_visible(struct kobject *kobj, struct attribute *a, int n) |
| 794 | { |
| 795 | struct device *dev = container_of(kobj, struct device, kobj); |
| 796 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 797 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 798 | |
| 799 | if (a == &dev_attr_target_node.attr && dev_dax_target_node(dev_dax) < 0) |
| 800 | return 0; |
Dan Williams | cb4dd72 | 2019-11-12 17:13:31 -0800 | [diff] [blame] | 801 | if (a == &dev_attr_numa_node.attr && !IS_ENABLED(CONFIG_NUMA)) |
| 802 | return 0; |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 803 | if (a == &dev_attr_size.attr && is_static(dax_region)) |
| 804 | return 0444; |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 805 | return a->mode; |
| 806 | } |
| 807 | |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 808 | static struct attribute *dev_dax_attributes[] = { |
Vishal Verma | c347bd7 | 2019-02-22 16:58:54 -0700 | [diff] [blame] | 809 | &dev_attr_modalias.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 810 | &dev_attr_size.attr, |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 811 | &dev_attr_target_node.attr, |
Vishal Verma | 40cdc60 | 2019-06-20 18:40:38 -0600 | [diff] [blame] | 812 | &dev_attr_resource.attr, |
Dan Williams | cb4dd72 | 2019-11-12 17:13:31 -0800 | [diff] [blame] | 813 | &dev_attr_numa_node.attr, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 814 | NULL, |
| 815 | }; |
| 816 | |
| 817 | static const struct attribute_group dev_dax_attribute_group = { |
| 818 | .attrs = dev_dax_attributes, |
Dan Williams | 21c7576 | 2019-02-20 11:39:36 -0800 | [diff] [blame] | 819 | .is_visible = dev_dax_visible, |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 820 | }; |
| 821 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 822 | static const struct attribute_group *dax_attribute_groups[] = { |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 823 | &dev_dax_attribute_group, |
| 824 | NULL, |
| 825 | }; |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 826 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 827 | static void dev_dax_release(struct device *dev) |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 828 | { |
| 829 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 830 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 831 | struct dax_device *dax_dev = dev_dax->dax_dev; |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 832 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 833 | put_dax(dax_dev); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 834 | free_dev_dax_id(dev_dax); |
| 835 | dax_region_put(dax_region); |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 836 | kfree(dev_dax->pgmap); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 837 | kfree(dev_dax); |
| 838 | } |
| 839 | |
Dan Williams | 770619a | 2019-11-12 17:12:23 -0800 | [diff] [blame] | 840 | static const struct device_type dev_dax_type = { |
| 841 | .release = dev_dax_release, |
| 842 | .groups = dax_attribute_groups, |
| 843 | }; |
| 844 | |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 845 | struct dev_dax *devm_create_dev_dax(struct dev_dax_data *data) |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 846 | { |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 847 | struct dax_region *dax_region = data->dax_region; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 848 | struct device *parent = dax_region->dev; |
| 849 | struct dax_device *dax_dev; |
| 850 | struct dev_dax *dev_dax; |
| 851 | struct inode *inode; |
| 852 | struct device *dev; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 853 | int rc; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 854 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 855 | dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL); |
| 856 | if (!dev_dax) |
| 857 | return ERR_PTR(-ENOMEM); |
| 858 | |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 859 | if (is_static(dax_region)) { |
| 860 | if (dev_WARN_ONCE(parent, data->id < 0, |
| 861 | "dynamic id specified to static region\n")) { |
| 862 | rc = -EINVAL; |
| 863 | goto err_id; |
| 864 | } |
| 865 | |
| 866 | dev_dax->id = data->id; |
| 867 | } else { |
| 868 | if (dev_WARN_ONCE(parent, data->id >= 0, |
| 869 | "static id specified to dynamic region\n")) { |
| 870 | rc = -EINVAL; |
| 871 | goto err_id; |
| 872 | } |
| 873 | |
| 874 | rc = ida_alloc(&dax_region->ida, GFP_KERNEL); |
| 875 | if (rc < 0) |
| 876 | goto err_id; |
| 877 | dev_dax->id = rc; |
| 878 | } |
| 879 | |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 880 | dev_dax->region = dax_region; |
| 881 | dev = &dev_dax->dev; |
| 882 | device_initialize(dev); |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 883 | 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] | 884 | |
Dan Williams | fcffb6a | 2020-10-13 16:50:24 -0700 | [diff] [blame^] | 885 | 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] | 886 | if (rc) |
| 887 | goto err_range; |
| 888 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 889 | if (data->pgmap) { |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 890 | dev_WARN_ONCE(parent, !is_static(dax_region), |
| 891 | "custom dev_pagemap requires a static dax_region\n"); |
| 892 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 893 | dev_dax->pgmap = kmemdup(data->pgmap, |
| 894 | sizeof(struct dev_pagemap), GFP_KERNEL); |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 895 | if (!dev_dax->pgmap) { |
| 896 | rc = -ENOMEM; |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 897 | goto err_pgmap; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 898 | } |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 899 | } |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 900 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 901 | /* |
| 902 | * No 'host' or dax_operations since there is no access to this |
| 903 | * device outside of mmap of the resulting character device. |
| 904 | */ |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 905 | dax_dev = alloc_dax(dev_dax, NULL, NULL, DAXDEV_F_SYNC); |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 906 | if (IS_ERR(dax_dev)) { |
| 907 | rc = PTR_ERR(dax_dev); |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 908 | goto err_alloc_dax; |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 909 | } |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 910 | |
| 911 | /* a device_dax instance is dead while the driver is not attached */ |
| 912 | kill_dax(dax_dev); |
| 913 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 914 | /* from here on we're committed to teardown via dev_dax_release() */ |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 915 | dev_dax->dax_dev = dax_dev; |
Dan Williams | 8fc5c73 | 2018-11-09 12:43:07 -0800 | [diff] [blame] | 916 | dev_dax->target_node = dax_region->target_node; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 917 | kref_get(&dax_region->kref); |
| 918 | |
| 919 | inode = dax_inode(dax_dev); |
| 920 | dev->devt = inode->i_rdev; |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 921 | if (data->subsys == DEV_DAX_BUS) |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 922 | dev->bus = &dax_bus_type; |
| 923 | else |
| 924 | dev->class = dax_class; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 925 | dev->parent = parent; |
Dan Williams | 770619a | 2019-11-12 17:12:23 -0800 | [diff] [blame] | 926 | dev->type = &dev_dax_type; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 927 | |
| 928 | rc = device_add(dev); |
| 929 | if (rc) { |
| 930 | kill_dev_dax(dev_dax); |
| 931 | put_device(dev); |
| 932 | return ERR_PTR(rc); |
| 933 | } |
| 934 | |
| 935 | rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev); |
| 936 | if (rc) |
| 937 | return ERR_PTR(rc); |
| 938 | |
| 939 | return dev_dax; |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 940 | |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 941 | err_alloc_dax: |
| 942 | kfree(dev_dax->pgmap); |
| 943 | err_pgmap: |
Dan Williams | c2f3011 | 2020-10-13 16:50:03 -0700 | [diff] [blame] | 944 | free_dev_dax_range(dev_dax); |
| 945 | err_range: |
Dan Williams | 0f3da14 | 2020-10-13 16:50:13 -0700 | [diff] [blame] | 946 | free_dev_dax_id(dev_dax); |
| 947 | err_id: |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 948 | kfree(dev_dax); |
| 949 | |
| 950 | return ERR_PTR(rc); |
| 951 | } |
Dan Williams | 174ebec | 2020-10-13 16:49:38 -0700 | [diff] [blame] | 952 | EXPORT_SYMBOL_GPL(devm_create_dev_dax); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 953 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 954 | static int match_always_count; |
| 955 | |
| 956 | int __dax_driver_register(struct dax_device_driver *dax_drv, |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 957 | struct module *module, const char *mod_name) |
| 958 | { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 959 | struct device_driver *drv = &dax_drv->drv; |
| 960 | int rc = 0; |
| 961 | |
| 962 | INIT_LIST_HEAD(&dax_drv->ids); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 963 | drv->owner = module; |
| 964 | drv->name = mod_name; |
| 965 | drv->mod_name = mod_name; |
| 966 | drv->bus = &dax_bus_type; |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 967 | |
| 968 | /* there can only be one default driver */ |
| 969 | mutex_lock(&dax_bus_lock); |
| 970 | match_always_count += dax_drv->match_always; |
| 971 | if (match_always_count > 1) { |
| 972 | match_always_count--; |
| 973 | WARN_ON(1); |
| 974 | rc = -EINVAL; |
| 975 | } |
| 976 | mutex_unlock(&dax_bus_lock); |
| 977 | if (rc) |
| 978 | return rc; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 979 | return driver_register(drv); |
| 980 | } |
| 981 | EXPORT_SYMBOL_GPL(__dax_driver_register); |
| 982 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 983 | void dax_driver_unregister(struct dax_device_driver *dax_drv) |
| 984 | { |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 985 | struct device_driver *drv = &dax_drv->drv; |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 986 | struct dax_id *dax_id, *_id; |
| 987 | |
| 988 | mutex_lock(&dax_bus_lock); |
| 989 | match_always_count -= dax_drv->match_always; |
| 990 | list_for_each_entry_safe(dax_id, _id, &dax_drv->ids, list) { |
| 991 | list_del(&dax_id->list); |
| 992 | kfree(dax_id); |
| 993 | } |
| 994 | mutex_unlock(&dax_bus_lock); |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 995 | driver_unregister(drv); |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 996 | } |
| 997 | EXPORT_SYMBOL_GPL(dax_driver_unregister); |
| 998 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 999 | int __init dax_bus_init(void) |
| 1000 | { |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1001 | int rc; |
| 1002 | |
| 1003 | if (IS_ENABLED(CONFIG_DEV_DAX_PMEM_COMPAT)) { |
| 1004 | dax_class = class_create(THIS_MODULE, "dax"); |
| 1005 | if (IS_ERR(dax_class)) |
| 1006 | return PTR_ERR(dax_class); |
| 1007 | } |
| 1008 | |
| 1009 | rc = bus_register(&dax_bus_type); |
| 1010 | if (rc) |
| 1011 | class_destroy(dax_class); |
| 1012 | return rc; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | void __exit dax_bus_exit(void) |
| 1016 | { |
| 1017 | bus_unregister(&dax_bus_type); |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 1018 | class_destroy(dax_class); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1019 | } |