xref: /linux-6.15/include/linux/mdev.h (revision 6c7f98b3)
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 struct mdev_type;
14 
15 struct mdev_device {
16 	struct device dev;
17 	guid_t uuid;
18 	void *driver_data;
19 	struct list_head next;
20 	struct mdev_type *type;
21 	bool active;
22 };
23 
24 static inline struct mdev_device *to_mdev_device(struct device *dev)
25 {
26 	return container_of(dev, struct mdev_device, dev);
27 }
28 
29 unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
30 unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
31 struct device *mtype_get_parent_dev(struct mdev_type *mtype);
32 
33 /**
34  * struct mdev_parent_ops - Structure to be registered for each parent device to
35  * register the device to mdev module.
36  *
37  * @owner:		The module owner.
38  * @device_driver:	Which device driver to probe() on newly created devices
39  * @dev_attr_groups:	Attributes of the parent device.
40  * @mdev_attr_groups:	Attributes of the mediated device.
41  * @supported_type_groups: Attributes to define supported types. It is mandatory
42  *			to provide supported types.
43  *
44  * Parent device that support mediated device should be registered with mdev
45  * module with mdev_parent_ops structure.
46  **/
47 struct mdev_parent_ops {
48 	struct module   *owner;
49 	struct mdev_driver *device_driver;
50 	const struct attribute_group **dev_attr_groups;
51 	const struct attribute_group **mdev_attr_groups;
52 	struct attribute_group **supported_type_groups;
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(_name, _mode, _show, _store)		\
66 struct mdev_type_attribute mdev_type_attr_##_name =		\
67 	__ATTR(_name, _mode, _show, _store)
68 #define MDEV_TYPE_ATTR_RW(_name) \
69 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
70 #define MDEV_TYPE_ATTR_RO(_name) \
71 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
72 #define MDEV_TYPE_ATTR_WO(_name) \
73 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
74 
75 /**
76  * struct mdev_driver - Mediated device driver
77  * @probe: called when new device created
78  * @remove: called when device removed
79  * @driver: device driver structure
80  *
81  **/
82 struct mdev_driver {
83 	int (*probe)(struct mdev_device *dev);
84 	void (*remove)(struct mdev_device *dev);
85 	struct device_driver driver;
86 };
87 
88 static inline void *mdev_get_drvdata(struct mdev_device *mdev)
89 {
90 	return mdev->driver_data;
91 }
92 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
93 {
94 	mdev->driver_data = data;
95 }
96 static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
97 {
98 	return &mdev->uuid;
99 }
100 
101 extern struct bus_type mdev_bus_type;
102 
103 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
104 void mdev_unregister_device(struct device *dev);
105 
106 int mdev_register_driver(struct mdev_driver *drv);
107 void mdev_unregister_driver(struct mdev_driver *drv);
108 
109 struct device *mdev_parent_dev(struct mdev_device *mdev);
110 static inline struct device *mdev_dev(struct mdev_device *mdev)
111 {
112 	return &mdev->dev;
113 }
114 static inline struct mdev_device *mdev_from_dev(struct device *dev)
115 {
116 	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
117 }
118 
119 #endif /* MDEV_H */
120