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