xref: /linux-6.15/include/linux/mdev.h (revision 6b42f491)
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 /* interface for exporting mdev supported type attributes */
34 struct mdev_type_attribute {
35 	struct attribute attr;
36 	ssize_t (*show)(struct mdev_type *mtype,
37 			struct mdev_type_attribute *attr, char *buf);
38 	ssize_t (*store)(struct mdev_type *mtype,
39 			 struct mdev_type_attribute *attr, const char *buf,
40 			 size_t count);
41 };
42 
43 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
44 struct mdev_type_attribute mdev_type_attr_##_name =		\
45 	__ATTR(_name, _mode, _show, _store)
46 #define MDEV_TYPE_ATTR_RW(_name) \
47 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
48 #define MDEV_TYPE_ATTR_RO(_name) \
49 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
50 #define MDEV_TYPE_ATTR_WO(_name) \
51 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
52 
53 /**
54  * struct mdev_driver - Mediated device driver
55  * @probe: called when new device created
56  * @remove: called when device removed
57  * @supported_type_groups: Attributes to define supported types. It is mandatory
58  *			to provide supported types.
59  * @driver: device driver structure
60  *
61  **/
62 struct mdev_driver {
63 	int (*probe)(struct mdev_device *dev);
64 	void (*remove)(struct mdev_device *dev);
65 	struct attribute_group **supported_type_groups;
66 	struct device_driver driver;
67 };
68 
69 static inline void *mdev_get_drvdata(struct mdev_device *mdev)
70 {
71 	return mdev->driver_data;
72 }
73 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
74 {
75 	mdev->driver_data = data;
76 }
77 static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
78 {
79 	return &mdev->uuid;
80 }
81 
82 extern struct bus_type mdev_bus_type;
83 
84 int mdev_register_device(struct device *dev, struct mdev_driver *mdev_driver);
85 void mdev_unregister_device(struct device *dev);
86 
87 int mdev_register_driver(struct mdev_driver *drv);
88 void mdev_unregister_driver(struct mdev_driver *drv);
89 
90 struct device *mdev_parent_dev(struct mdev_device *mdev);
91 static inline struct device *mdev_dev(struct mdev_device *mdev)
92 {
93 	return &mdev->dev;
94 }
95 static inline struct mdev_device *mdev_from_dev(struct device *dev)
96 {
97 	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
98 }
99 
100 #endif /* MDEV_H */
101