xref: /linux-6.15/include/linux/mdev.h (revision 062e720c)
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 
30 	/* set by the core, can be used drivers */
31 	struct mdev_parent *parent;
32 
33 	/* internal only */
34 	struct kobject kobj;
35 	struct kobject *devices_kobj;
36 };
37 
38 /* embedded into the struct device that the mdev devices hang off */
39 struct mdev_parent {
40 	struct device *dev;
41 	struct mdev_driver *mdev_driver;
42 	struct kset *mdev_types_kset;
43 	/* Synchronize device creation/removal with parent unregistration */
44 	struct rw_semaphore unreg_sem;
45 	struct mdev_type **types;
46 	unsigned int nr_types;
47 };
48 
49 static inline struct mdev_device *to_mdev_device(struct device *dev)
50 {
51 	return container_of(dev, struct mdev_device, dev);
52 }
53 
54 struct device *mtype_get_parent_dev(struct mdev_type *mtype);
55 
56 /* interface for exporting mdev supported type attributes */
57 struct mdev_type_attribute {
58 	struct attribute attr;
59 	ssize_t (*show)(struct mdev_type *mtype,
60 			struct mdev_type_attribute *attr, char *buf);
61 	ssize_t (*store)(struct mdev_type *mtype,
62 			 struct mdev_type_attribute *attr, const char *buf,
63 			 size_t count);
64 };
65 
66 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
67 struct mdev_type_attribute mdev_type_attr_##_name =		\
68 	__ATTR(_name, _mode, _show, _store)
69 #define MDEV_TYPE_ATTR_RW(_name) \
70 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
71 #define MDEV_TYPE_ATTR_RO(_name) \
72 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
73 #define MDEV_TYPE_ATTR_WO(_name) \
74 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
75 
76 /**
77  * struct mdev_driver - Mediated device driver
78  * @probe: called when new device created
79  * @remove: called when device removed
80  * @types_attrs: attributes to the type kobjects.
81  * @driver: device driver structure
82  **/
83 struct mdev_driver {
84 	int (*probe)(struct mdev_device *dev);
85 	void (*remove)(struct mdev_device *dev);
86 	const struct attribute * const *types_attrs;
87 	struct device_driver driver;
88 };
89 
90 int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
91 		struct mdev_driver *mdev_driver, struct mdev_type **types,
92 		unsigned int nr_types);
93 void mdev_unregister_parent(struct mdev_parent *parent);
94 
95 int mdev_register_driver(struct mdev_driver *drv);
96 void mdev_unregister_driver(struct mdev_driver *drv);
97 
98 static inline struct device *mdev_dev(struct mdev_device *mdev)
99 {
100 	return &mdev->dev;
101 }
102 
103 #endif /* MDEV_H */
104