xref: /linux-6.15/include/linux/mdev.h (revision 89345d51)
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 /* embedded into the struct device that the mdev devices hang off */
27 struct mdev_parent {
28 	struct device *dev;
29 	struct mdev_driver *mdev_driver;
30 	struct kset *mdev_types_kset;
31 	struct list_head type_list;
32 	/* Synchronize device creation/removal with parent unregistration */
33 	struct rw_semaphore unreg_sem;
34 };
35 
36 static inline struct mdev_device *to_mdev_device(struct device *dev)
37 {
38 	return container_of(dev, struct mdev_device, dev);
39 }
40 
41 unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
42 unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
43 struct device *mtype_get_parent_dev(struct mdev_type *mtype);
44 
45 /* interface for exporting mdev supported type attributes */
46 struct mdev_type_attribute {
47 	struct attribute attr;
48 	ssize_t (*show)(struct mdev_type *mtype,
49 			struct mdev_type_attribute *attr, char *buf);
50 	ssize_t (*store)(struct mdev_type *mtype,
51 			 struct mdev_type_attribute *attr, const char *buf,
52 			 size_t count);
53 };
54 
55 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
56 struct mdev_type_attribute mdev_type_attr_##_name =		\
57 	__ATTR(_name, _mode, _show, _store)
58 #define MDEV_TYPE_ATTR_RW(_name) \
59 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
60 #define MDEV_TYPE_ATTR_RO(_name) \
61 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
62 #define MDEV_TYPE_ATTR_WO(_name) \
63 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
64 
65 /**
66  * struct mdev_driver - Mediated device driver
67  * @probe: called when new device created
68  * @remove: called when device removed
69  * @supported_type_groups: Attributes to define supported types. It is mandatory
70  *			to provide supported types.
71  * @driver: device driver structure
72  *
73  **/
74 struct mdev_driver {
75 	int (*probe)(struct mdev_device *dev);
76 	void (*remove)(struct mdev_device *dev);
77 	struct attribute_group **supported_type_groups;
78 	struct device_driver driver;
79 };
80 
81 extern struct bus_type mdev_bus_type;
82 
83 int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
84 		struct mdev_driver *mdev_driver);
85 void mdev_unregister_parent(struct mdev_parent *parent);
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