linux/drivers/vfio/mdev/mdev_core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Mediated device Core Driver
   4 *
   5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
   6 *     Author: Neo Jia <cjia@nvidia.com>
   7 *             Kirti Wankhede <kwankhede@nvidia.com>
   8 */
   9
  10#include <linux/module.h>
  11#include <linux/device.h>
  12#include <linux/slab.h>
  13#include <linux/uuid.h>
  14#include <linux/sysfs.h>
  15#include <linux/mdev.h>
  16
  17#include "mdev_private.h"
  18
  19#define DRIVER_VERSION          "0.1"
  20#define DRIVER_AUTHOR           "NVIDIA Corporation"
  21#define DRIVER_DESC             "Mediated device Core Driver"
  22
  23static LIST_HEAD(parent_list);
  24static DEFINE_MUTEX(parent_list_lock);
  25static struct class_compat *mdev_bus_compat_class;
  26
  27static LIST_HEAD(mdev_list);
  28static DEFINE_MUTEX(mdev_list_lock);
  29
  30struct device *mdev_parent_dev(struct mdev_device *mdev)
  31{
  32        return mdev->type->parent->dev;
  33}
  34EXPORT_SYMBOL(mdev_parent_dev);
  35
  36/*
  37 * Return the index in supported_type_groups that this mdev_device was created
  38 * from.
  39 */
  40unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
  41{
  42        return mdev->type->type_group_id;
  43}
  44EXPORT_SYMBOL(mdev_get_type_group_id);
  45
  46/*
  47 * Used in mdev_type_attribute sysfs functions to return the index in the
  48 * supported_type_groups that the sysfs is called from.
  49 */
  50unsigned int mtype_get_type_group_id(struct mdev_type *mtype)
  51{
  52        return mtype->type_group_id;
  53}
  54EXPORT_SYMBOL(mtype_get_type_group_id);
  55
  56/*
  57 * Used in mdev_type_attribute sysfs functions to return the parent struct
  58 * device
  59 */
  60struct device *mtype_get_parent_dev(struct mdev_type *mtype)
  61{
  62        return mtype->parent->dev;
  63}
  64EXPORT_SYMBOL(mtype_get_parent_dev);
  65
  66/* Should be called holding parent_list_lock */
  67static struct mdev_parent *__find_parent_device(struct device *dev)
  68{
  69        struct mdev_parent *parent;
  70
  71        list_for_each_entry(parent, &parent_list, next) {
  72                if (parent->dev == dev)
  73                        return parent;
  74        }
  75        return NULL;
  76}
  77
  78void mdev_release_parent(struct kref *kref)
  79{
  80        struct mdev_parent *parent = container_of(kref, struct mdev_parent,
  81                                                  ref);
  82        struct device *dev = parent->dev;
  83
  84        kfree(parent);
  85        put_device(dev);
  86}
  87
  88/* Caller must hold parent unreg_sem read or write lock */
  89static void mdev_device_remove_common(struct mdev_device *mdev)
  90{
  91        struct mdev_parent *parent = mdev->type->parent;
  92        int ret;
  93
  94        mdev_remove_sysfs_files(mdev);
  95        device_del(&mdev->dev);
  96        lockdep_assert_held(&parent->unreg_sem);
  97        if (parent->ops->remove) {
  98                ret = parent->ops->remove(mdev);
  99                if (ret)
 100                        dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
 101        }
 102
 103        /* Balances with device_initialize() */
 104        put_device(&mdev->dev);
 105}
 106
 107static int mdev_device_remove_cb(struct device *dev, void *data)
 108{
 109        struct mdev_device *mdev = mdev_from_dev(dev);
 110
 111        if (mdev)
 112                mdev_device_remove_common(mdev);
 113        return 0;
 114}
 115
 116/*
 117 * mdev_register_device : Register a device
 118 * @dev: device structure representing parent device.
 119 * @ops: Parent device operation structure to be registered.
 120 *
 121 * Add device to list of registered parent devices.
 122 * Returns a negative value on error, otherwise 0.
 123 */
 124int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
 125{
 126        int ret;
 127        struct mdev_parent *parent;
 128        char *env_string = "MDEV_STATE=registered";
 129        char *envp[] = { env_string, NULL };
 130
 131        /* check for mandatory ops */
 132        if (!ops || !ops->supported_type_groups)
 133                return -EINVAL;
 134        if (!ops->device_driver && (!ops->create || !ops->remove))
 135                return -EINVAL;
 136
 137        dev = get_device(dev);
 138        if (!dev)
 139                return -EINVAL;
 140
 141        mutex_lock(&parent_list_lock);
 142
 143        /* Check for duplicate */
 144        parent = __find_parent_device(dev);
 145        if (parent) {
 146                parent = NULL;
 147                ret = -EEXIST;
 148                goto add_dev_err;
 149        }
 150
 151        parent = kzalloc(sizeof(*parent), GFP_KERNEL);
 152        if (!parent) {
 153                ret = -ENOMEM;
 154                goto add_dev_err;
 155        }
 156
 157        kref_init(&parent->ref);
 158        init_rwsem(&parent->unreg_sem);
 159
 160        parent->dev = dev;
 161        parent->ops = ops;
 162
 163        if (!mdev_bus_compat_class) {
 164                mdev_bus_compat_class = class_compat_register("mdev_bus");
 165                if (!mdev_bus_compat_class) {
 166                        ret = -ENOMEM;
 167                        goto add_dev_err;
 168                }
 169        }
 170
 171        ret = parent_create_sysfs_files(parent);
 172        if (ret)
 173                goto add_dev_err;
 174
 175        ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
 176        if (ret)
 177                dev_warn(dev, "Failed to create compatibility class link\n");
 178
 179        list_add(&parent->next, &parent_list);
 180        mutex_unlock(&parent_list_lock);
 181
 182        dev_info(dev, "MDEV: Registered\n");
 183        kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
 184
 185        return 0;
 186
 187add_dev_err:
 188        mutex_unlock(&parent_list_lock);
 189        if (parent)
 190                mdev_put_parent(parent);
 191        else
 192                put_device(dev);
 193        return ret;
 194}
 195EXPORT_SYMBOL(mdev_register_device);
 196
 197/*
 198 * mdev_unregister_device : Unregister a parent device
 199 * @dev: device structure representing parent device.
 200 *
 201 * Remove device from list of registered parent devices. Give a chance to free
 202 * existing mediated devices for given device.
 203 */
 204
 205void mdev_unregister_device(struct device *dev)
 206{
 207        struct mdev_parent *parent;
 208        char *env_string = "MDEV_STATE=unregistered";
 209        char *envp[] = { env_string, NULL };
 210
 211        mutex_lock(&parent_list_lock);
 212        parent = __find_parent_device(dev);
 213
 214        if (!parent) {
 215                mutex_unlock(&parent_list_lock);
 216                return;
 217        }
 218        dev_info(dev, "MDEV: Unregistering\n");
 219
 220        list_del(&parent->next);
 221        mutex_unlock(&parent_list_lock);
 222
 223        down_write(&parent->unreg_sem);
 224
 225        class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
 226
 227        device_for_each_child(dev, NULL, mdev_device_remove_cb);
 228
 229        parent_remove_sysfs_files(parent);
 230        up_write(&parent->unreg_sem);
 231
 232        mdev_put_parent(parent);
 233
 234        /* We still have the caller's reference to use for the uevent */
 235        kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
 236}
 237EXPORT_SYMBOL(mdev_unregister_device);
 238
 239static void mdev_device_release(struct device *dev)
 240{
 241        struct mdev_device *mdev = to_mdev_device(dev);
 242
 243        /* Pairs with the get in mdev_device_create() */
 244        kobject_put(&mdev->type->kobj);
 245
 246        mutex_lock(&mdev_list_lock);
 247        list_del(&mdev->next);
 248        mutex_unlock(&mdev_list_lock);
 249
 250        dev_dbg(&mdev->dev, "MDEV: destroying\n");
 251        kfree(mdev);
 252}
 253
 254int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 255{
 256        int ret;
 257        struct mdev_device *mdev, *tmp;
 258        struct mdev_parent *parent = type->parent;
 259        struct mdev_driver *drv = parent->ops->device_driver;
 260
 261        mutex_lock(&mdev_list_lock);
 262
 263        /* Check for duplicate */
 264        list_for_each_entry(tmp, &mdev_list, next) {
 265                if (guid_equal(&tmp->uuid, uuid)) {
 266                        mutex_unlock(&mdev_list_lock);
 267                        return -EEXIST;
 268                }
 269        }
 270
 271        mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
 272        if (!mdev) {
 273                mutex_unlock(&mdev_list_lock);
 274                return -ENOMEM;
 275        }
 276
 277        device_initialize(&mdev->dev);
 278        mdev->dev.parent  = parent->dev;
 279        mdev->dev.bus = &mdev_bus_type;
 280        mdev->dev.release = mdev_device_release;
 281        mdev->dev.groups = parent->ops->mdev_attr_groups;
 282        mdev->type = type;
 283        /* Pairs with the put in mdev_device_release() */
 284        kobject_get(&type->kobj);
 285
 286        guid_copy(&mdev->uuid, uuid);
 287        list_add(&mdev->next, &mdev_list);
 288        mutex_unlock(&mdev_list_lock);
 289
 290        ret = dev_set_name(&mdev->dev, "%pUl", uuid);
 291        if (ret)
 292                goto out_put_device;
 293
 294        /* Check if parent unregistration has started */
 295        if (!down_read_trylock(&parent->unreg_sem)) {
 296                ret = -ENODEV;
 297                goto out_put_device;
 298        }
 299
 300        if (parent->ops->create) {
 301                ret = parent->ops->create(mdev);
 302                if (ret)
 303                        goto out_unlock;
 304        }
 305
 306        ret = device_add(&mdev->dev);
 307        if (ret)
 308                goto out_remove;
 309
 310        if (!drv)
 311                drv = &vfio_mdev_driver;
 312        ret = device_driver_attach(&drv->driver, &mdev->dev);
 313        if (ret)
 314                goto out_del;
 315
 316        ret = mdev_create_sysfs_files(mdev);
 317        if (ret)
 318                goto out_del;
 319
 320        mdev->active = true;
 321        dev_dbg(&mdev->dev, "MDEV: created\n");
 322        up_read(&parent->unreg_sem);
 323
 324        return 0;
 325
 326out_del:
 327        device_del(&mdev->dev);
 328out_remove:
 329        if (parent->ops->remove)
 330                parent->ops->remove(mdev);
 331out_unlock:
 332        up_read(&parent->unreg_sem);
 333out_put_device:
 334        put_device(&mdev->dev);
 335        return ret;
 336}
 337
 338int mdev_device_remove(struct mdev_device *mdev)
 339{
 340        struct mdev_device *tmp;
 341        struct mdev_parent *parent = mdev->type->parent;
 342
 343        mutex_lock(&mdev_list_lock);
 344        list_for_each_entry(tmp, &mdev_list, next) {
 345                if (tmp == mdev)
 346                        break;
 347        }
 348
 349        if (tmp != mdev) {
 350                mutex_unlock(&mdev_list_lock);
 351                return -ENODEV;
 352        }
 353
 354        if (!mdev->active) {
 355                mutex_unlock(&mdev_list_lock);
 356                return -EAGAIN;
 357        }
 358
 359        mdev->active = false;
 360        mutex_unlock(&mdev_list_lock);
 361
 362        /* Check if parent unregistration has started */
 363        if (!down_read_trylock(&parent->unreg_sem))
 364                return -ENODEV;
 365
 366        mdev_device_remove_common(mdev);
 367        up_read(&parent->unreg_sem);
 368        return 0;
 369}
 370
 371static int __init mdev_init(void)
 372{
 373        int rc;
 374
 375        rc = mdev_bus_register();
 376        if (rc)
 377                return rc;
 378        rc = mdev_register_driver(&vfio_mdev_driver);
 379        if (rc)
 380                goto err_bus;
 381        return 0;
 382err_bus:
 383        mdev_bus_unregister();
 384        return rc;
 385}
 386
 387static void __exit mdev_exit(void)
 388{
 389        mdev_unregister_driver(&vfio_mdev_driver);
 390
 391        if (mdev_bus_compat_class)
 392                class_compat_unregister(mdev_bus_compat_class);
 393
 394        mdev_bus_unregister();
 395}
 396
 397subsys_initcall(mdev_init)
 398module_exit(mdev_exit)
 399
 400MODULE_VERSION(DRIVER_VERSION);
 401MODULE_LICENSE("GPL v2");
 402MODULE_AUTHOR(DRIVER_AUTHOR);
 403MODULE_DESCRIPTION(DRIVER_DESC);
 404