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