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/slab.h> 12 #include <linux/sysfs.h> 13 #include <linux/mdev.h> 14 15 #include "mdev_private.h" 16 17 #define DRIVER_VERSION "0.1" 18 #define DRIVER_AUTHOR "NVIDIA Corporation" 19 #define DRIVER_DESC "Mediated device Core Driver" 20 21 static struct class_compat *mdev_bus_compat_class; 22 23 static LIST_HEAD(mdev_list); 24 static DEFINE_MUTEX(mdev_list_lock); 25 26 /* Caller must hold parent unreg_sem read or write lock */ 27 static void mdev_device_remove_common(struct mdev_device *mdev) 28 { 29 struct mdev_parent *parent = mdev->type->parent; 30 31 mdev_remove_sysfs_files(mdev); 32 device_del(&mdev->dev); 33 lockdep_assert_held(&parent->unreg_sem); 34 /* Balances with device_initialize() */ 35 put_device(&mdev->dev); 36 } 37 38 static int mdev_device_remove_cb(struct device *dev, void *data) 39 { 40 if (dev->bus == &mdev_bus_type) 41 mdev_device_remove_common(to_mdev_device(dev)); 42 return 0; 43 } 44 45 /* 46 * mdev_register_parent: Register a device as parent for mdevs 47 * @parent: parent structure registered 48 * @dev: device structure representing parent device. 49 * @mdev_driver: Device driver to bind to the newly created mdev 50 * @types: Array of supported mdev types 51 * @nr_types: Number of entries in @types 52 * 53 * Registers the @parent stucture as a parent for mdev types and thus mdev 54 * devices. The caller needs to hold a reference on @dev that must not be 55 * released until after the call to mdev_unregister_parent(). 56 * 57 * Returns a negative value on error, otherwise 0. 58 */ 59 int mdev_register_parent(struct mdev_parent *parent, struct device *dev, 60 struct mdev_driver *mdev_driver, struct mdev_type **types, 61 unsigned int nr_types) 62 { 63 char *env_string = "MDEV_STATE=registered"; 64 char *envp[] = { env_string, NULL }; 65 int ret; 66 67 memset(parent, 0, sizeof(*parent)); 68 init_rwsem(&parent->unreg_sem); 69 parent->dev = dev; 70 parent->mdev_driver = mdev_driver; 71 parent->types = types; 72 parent->nr_types = nr_types; 73 74 if (!mdev_bus_compat_class) { 75 mdev_bus_compat_class = class_compat_register("mdev_bus"); 76 if (!mdev_bus_compat_class) 77 return -ENOMEM; 78 } 79 80 ret = parent_create_sysfs_files(parent); 81 if (ret) 82 return ret; 83 84 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL); 85 if (ret) 86 dev_warn(dev, "Failed to create compatibility class link\n"); 87 88 dev_info(dev, "MDEV: Registered\n"); 89 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); 90 return 0; 91 } 92 EXPORT_SYMBOL(mdev_register_parent); 93 94 /* 95 * mdev_unregister_parent : Unregister a parent device 96 * @parent: parent structure to unregister 97 */ 98 void mdev_unregister_parent(struct mdev_parent *parent) 99 { 100 char *env_string = "MDEV_STATE=unregistered"; 101 char *envp[] = { env_string, NULL }; 102 103 dev_info(parent->dev, "MDEV: Unregistering\n"); 104 105 down_write(&parent->unreg_sem); 106 class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL); 107 device_for_each_child(parent->dev, NULL, mdev_device_remove_cb); 108 parent_remove_sysfs_files(parent); 109 up_write(&parent->unreg_sem); 110 111 kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp); 112 } 113 EXPORT_SYMBOL(mdev_unregister_parent); 114 115 static void mdev_device_release(struct device *dev) 116 { 117 struct mdev_device *mdev = to_mdev_device(dev); 118 119 /* Pairs with the get in mdev_device_create() */ 120 kobject_put(&mdev->type->kobj); 121 122 mutex_lock(&mdev_list_lock); 123 list_del(&mdev->next); 124 mutex_unlock(&mdev_list_lock); 125 126 dev_dbg(&mdev->dev, "MDEV: destroying\n"); 127 kfree(mdev); 128 } 129 130 int mdev_device_create(struct mdev_type *type, const guid_t *uuid) 131 { 132 int ret; 133 struct mdev_device *mdev, *tmp; 134 struct mdev_parent *parent = type->parent; 135 struct mdev_driver *drv = parent->mdev_driver; 136 137 mutex_lock(&mdev_list_lock); 138 139 /* Check for duplicate */ 140 list_for_each_entry(tmp, &mdev_list, next) { 141 if (guid_equal(&tmp->uuid, uuid)) { 142 mutex_unlock(&mdev_list_lock); 143 return -EEXIST; 144 } 145 } 146 147 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); 148 if (!mdev) { 149 mutex_unlock(&mdev_list_lock); 150 return -ENOMEM; 151 } 152 153 device_initialize(&mdev->dev); 154 mdev->dev.parent = parent->dev; 155 mdev->dev.bus = &mdev_bus_type; 156 mdev->dev.release = mdev_device_release; 157 mdev->dev.groups = mdev_device_groups; 158 mdev->type = type; 159 /* Pairs with the put in mdev_device_release() */ 160 kobject_get(&type->kobj); 161 162 guid_copy(&mdev->uuid, uuid); 163 list_add(&mdev->next, &mdev_list); 164 mutex_unlock(&mdev_list_lock); 165 166 ret = dev_set_name(&mdev->dev, "%pUl", uuid); 167 if (ret) 168 goto out_put_device; 169 170 /* Check if parent unregistration has started */ 171 if (!down_read_trylock(&parent->unreg_sem)) { 172 ret = -ENODEV; 173 goto out_put_device; 174 } 175 176 ret = device_add(&mdev->dev); 177 if (ret) 178 goto out_unlock; 179 180 ret = device_driver_attach(&drv->driver, &mdev->dev); 181 if (ret) 182 goto out_del; 183 184 ret = mdev_create_sysfs_files(mdev); 185 if (ret) 186 goto out_del; 187 188 mdev->active = true; 189 dev_dbg(&mdev->dev, "MDEV: created\n"); 190 up_read(&parent->unreg_sem); 191 192 return 0; 193 194 out_del: 195 device_del(&mdev->dev); 196 out_unlock: 197 up_read(&parent->unreg_sem); 198 out_put_device: 199 put_device(&mdev->dev); 200 return ret; 201 } 202 203 int mdev_device_remove(struct mdev_device *mdev) 204 { 205 struct mdev_device *tmp; 206 struct mdev_parent *parent = mdev->type->parent; 207 208 mutex_lock(&mdev_list_lock); 209 list_for_each_entry(tmp, &mdev_list, next) { 210 if (tmp == mdev) 211 break; 212 } 213 214 if (tmp != mdev) { 215 mutex_unlock(&mdev_list_lock); 216 return -ENODEV; 217 } 218 219 if (!mdev->active) { 220 mutex_unlock(&mdev_list_lock); 221 return -EAGAIN; 222 } 223 224 mdev->active = false; 225 mutex_unlock(&mdev_list_lock); 226 227 /* Check if parent unregistration has started */ 228 if (!down_read_trylock(&parent->unreg_sem)) 229 return -ENODEV; 230 231 mdev_device_remove_common(mdev); 232 up_read(&parent->unreg_sem); 233 return 0; 234 } 235 236 static int __init mdev_init(void) 237 { 238 return bus_register(&mdev_bus_type); 239 } 240 241 static void __exit mdev_exit(void) 242 { 243 if (mdev_bus_compat_class) 244 class_compat_unregister(mdev_bus_compat_class); 245 bus_unregister(&mdev_bus_type); 246 } 247 248 subsys_initcall(mdev_init) 249 module_exit(mdev_exit) 250 251 MODULE_VERSION(DRIVER_VERSION); 252 MODULE_LICENSE("GPL v2"); 253 MODULE_AUTHOR(DRIVER_AUTHOR); 254 MODULE_DESCRIPTION(DRIVER_DESC); 255