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