xref: /linux-6.15/include/linux/mdev.h (revision f2fbc72e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Mediated device definition
4  *
5  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6  *     Author: Neo Jia <[email protected]>
7  *             Kirti Wankhede <[email protected]>
8  */
9 
10 #ifndef MDEV_H
11 #define MDEV_H
12 
13 #include <linux/device.h>
14 #include <linux/uuid.h>
15 
16 struct mdev_type;
17 
18 struct mdev_device {
19 	struct device dev;
20 	guid_t uuid;
21 	struct list_head next;
22 	struct mdev_type *type;
23 	bool active;
24 };
25 
26 struct mdev_type {
27 	/* set by the driver before calling mdev_register parent: */
28 	const char *sysfs_name;
29 	const char *pretty_name;
30 
31 	/* set by the core, can be used drivers */
32 	struct mdev_parent *parent;
33 
34 	/* internal only */
35 	struct kobject kobj;
36 	struct kobject *devices_kobj;
37 };
38 
39 /* embedded into the struct device that the mdev devices hang off */
40 struct mdev_parent {
41 	struct device *dev;
42 	struct mdev_driver *mdev_driver;
43 	struct kset *mdev_types_kset;
44 	/* Synchronize device creation/removal with parent unregistration */
45 	struct rw_semaphore unreg_sem;
46 	struct mdev_type **types;
47 	unsigned int nr_types;
48 };
49 
50 static inline struct mdev_device *to_mdev_device(struct device *dev)
51 {
52 	return container_of(dev, struct mdev_device, dev);
53 }
54 
55 /* interface for exporting mdev supported type attributes */
56 struct mdev_type_attribute {
57 	struct attribute attr;
58 	ssize_t (*show)(struct mdev_type *mtype,
59 			struct mdev_type_attribute *attr, char *buf);
60 	ssize_t (*store)(struct mdev_type *mtype,
61 			 struct mdev_type_attribute *attr, const char *buf,
62 			 size_t count);
63 };
64 
65 #define MDEV_TYPE_ATTR_RO(_name) \
66 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
67 #define MDEV_TYPE_ATTR_WO(_name) \
68 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
69 
70 /**
71  * struct mdev_driver - Mediated device driver
72  * @device_api: string to return for the device_api sysfs
73  * @probe: called when new device created
74  * @remove: called when device removed
75  * @get_available: Return the max number of instances that can be created
76  * @types_attrs: attributes to the type kobjects.
77  * @driver: device driver structure
78  **/
79 struct mdev_driver {
80 	const char *device_api;
81 	int (*probe)(struct mdev_device *dev);
82 	void (*remove)(struct mdev_device *dev);
83 	unsigned int (*get_available)(struct mdev_type *mtype);
84 	const struct attribute * const *types_attrs;
85 	struct device_driver driver;
86 };
87 
88 int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
89 		struct mdev_driver *mdev_driver, struct mdev_type **types,
90 		unsigned int nr_types);
91 void mdev_unregister_parent(struct mdev_parent *parent);
92 
93 int mdev_register_driver(struct mdev_driver *drv);
94 void mdev_unregister_driver(struct mdev_driver *drv);
95 
96 static inline struct device *mdev_dev(struct mdev_device *mdev)
97 {
98 	return &mdev->dev;
99 }
100 
101 #endif /* MDEV_H */
102