xref: /linux/include/linux/mdev.h (revision 9c92ab61914157664a2fbdf926df0eb937838e45)
1 /*
2  * Mediated device definition
3  *
4  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5  *     Author: Neo Jia <cjia@nvidia.com>
6  *             Kirti Wankhede <kwankhede@nvidia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #ifndef MDEV_H
14 #define MDEV_H
15 
16 struct mdev_device;
17 
18 /*
19  * Called by the parent device driver to set the device which represents
20  * this mdev in iommu protection scope. By default, the iommu device is
21  * NULL, that indicates using vendor defined isolation.
22  *
23  * @dev: the mediated device that iommu will isolate.
24  * @iommu_device: a pci device which represents the iommu for @dev.
25  *
26  * Return 0 for success, otherwise negative error value.
27  */
28 int mdev_set_iommu_device(struct device *dev, struct device *iommu_device);
29 
30 struct device *mdev_get_iommu_device(struct device *dev);
31 
32 /**
33  * struct mdev_parent_ops - Structure to be registered for each parent device to
34  * register the device to mdev module.
35  *
36  * @owner:		The module owner.
37  * @dev_attr_groups:	Attributes of the parent device.
38  * @mdev_attr_groups:	Attributes of the mediated device.
39  * @supported_type_groups: Attributes to define supported types. It is mandatory
40  *			to provide supported types.
41  * @create:		Called to allocate basic resources in parent device's
42  *			driver for a particular mediated device. It is
43  *			mandatory to provide create ops.
44  *			@kobj: kobject of type for which 'create' is called.
45  *			@mdev: mdev_device structure on of mediated device
46  *			      that is being created
47  *			Returns integer: success (0) or error (< 0)
48  * @remove:		Called to free resources in parent device's driver for a
49  *			a mediated device. It is mandatory to provide 'remove'
50  *			ops.
51  *			@mdev: mdev_device device structure which is being
52  *			       destroyed
53  *			Returns integer: success (0) or error (< 0)
54  * @open:		Open mediated device.
55  *			@mdev: mediated device.
56  *			Returns integer: success (0) or error (< 0)
57  * @release:		release mediated device
58  *			@mdev: mediated device.
59  * @read:		Read emulation callback
60  *			@mdev: mediated device structure
61  *			@buf: read buffer
62  *			@count: number of bytes to read
63  *			@ppos: address.
64  *			Retuns number on bytes read on success or error.
65  * @write:		Write emulation callback
66  *			@mdev: mediated device structure
67  *			@buf: write buffer
68  *			@count: number of bytes to be written
69  *			@ppos: address.
70  *			Retuns number on bytes written on success or error.
71  * @ioctl:		IOCTL callback
72  *			@mdev: mediated device structure
73  *			@cmd: ioctl command
74  *			@arg: arguments to ioctl
75  * @mmap:		mmap callback
76  *			@mdev: mediated device structure
77  *			@vma: vma structure
78  * Parent device that support mediated device should be registered with mdev
79  * module with mdev_parent_ops structure.
80  **/
81 struct mdev_parent_ops {
82 	struct module   *owner;
83 	const struct attribute_group **dev_attr_groups;
84 	const struct attribute_group **mdev_attr_groups;
85 	struct attribute_group **supported_type_groups;
86 
87 	int     (*create)(struct kobject *kobj, struct mdev_device *mdev);
88 	int     (*remove)(struct mdev_device *mdev);
89 	int     (*open)(struct mdev_device *mdev);
90 	void    (*release)(struct mdev_device *mdev);
91 	ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
92 			size_t count, loff_t *ppos);
93 	ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
94 			 size_t count, loff_t *ppos);
95 	long	(*ioctl)(struct mdev_device *mdev, unsigned int cmd,
96 			 unsigned long arg);
97 	int	(*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
98 };
99 
100 /* interface for exporting mdev supported type attributes */
101 struct mdev_type_attribute {
102 	struct attribute attr;
103 	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
104 	ssize_t (*store)(struct kobject *kobj, struct device *dev,
105 			 const char *buf, size_t count);
106 };
107 
108 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
109 struct mdev_type_attribute mdev_type_attr_##_name =		\
110 	__ATTR(_name, _mode, _show, _store)
111 #define MDEV_TYPE_ATTR_RW(_name) \
112 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
113 #define MDEV_TYPE_ATTR_RO(_name) \
114 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
115 #define MDEV_TYPE_ATTR_WO(_name) \
116 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
117 
118 /**
119  * struct mdev_driver - Mediated device driver
120  * @name: driver name
121  * @probe: called when new device created
122  * @remove: called when device removed
123  * @driver: device driver structure
124  *
125  **/
126 struct mdev_driver {
127 	const char *name;
128 	int  (*probe)(struct device *dev);
129 	void (*remove)(struct device *dev);
130 	struct device_driver driver;
131 };
132 
133 #define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
134 
135 void *mdev_get_drvdata(struct mdev_device *mdev);
136 void mdev_set_drvdata(struct mdev_device *mdev, void *data);
137 const guid_t *mdev_uuid(struct mdev_device *mdev);
138 
139 extern struct bus_type mdev_bus_type;
140 
141 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
142 void mdev_unregister_device(struct device *dev);
143 
144 int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
145 void mdev_unregister_driver(struct mdev_driver *drv);
146 
147 struct device *mdev_parent_dev(struct mdev_device *mdev);
148 struct device *mdev_dev(struct mdev_device *mdev);
149 struct mdev_device *mdev_from_dev(struct device *dev);
150 
151 #endif /* MDEV_H */
152