1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Mediated device definition
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
19 struct list_head next;
20 struct mdev_type *type;
21 struct device *iommu_device;
25 static inline struct mdev_device *to_mdev_device(struct device *dev)
27 return container_of(dev, struct mdev_device, dev);
31 * Called by the parent device driver to set the device which represents
32 * this mdev in iommu protection scope. By default, the iommu device is
33 * NULL, that indicates using vendor defined isolation.
35 * @dev: the mediated device that iommu will isolate.
36 * @iommu_device: a pci device which represents the iommu for @dev.
38 static inline void mdev_set_iommu_device(struct mdev_device *mdev,
39 struct device *iommu_device)
41 mdev->iommu_device = iommu_device;
44 static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev)
46 return mdev->iommu_device;
50 * struct mdev_parent_ops - Structure to be registered for each parent device to
51 * register the device to mdev module.
53 * @owner: The module owner.
54 * @dev_attr_groups: Attributes of the parent device.
55 * @mdev_attr_groups: Attributes of the mediated device.
56 * @supported_type_groups: Attributes to define supported types. It is mandatory
57 * to provide supported types.
58 * @create: Called to allocate basic resources in parent device's
59 * driver for a particular mediated device. It is
60 * mandatory to provide create ops.
61 * @kobj: kobject of type for which 'create' is called.
62 * @mdev: mdev_device structure on of mediated device
63 * that is being created
64 * Returns integer: success (0) or error (< 0)
65 * @remove: Called to free resources in parent device's driver for
66 * a mediated device. It is mandatory to provide 'remove'
68 * @mdev: mdev_device device structure which is being
70 * Returns integer: success (0) or error (< 0)
71 * @open: Open mediated device.
72 * @mdev: mediated device.
73 * Returns integer: success (0) or error (< 0)
74 * @release: release mediated device
75 * @mdev: mediated device.
76 * @read: Read emulation callback
77 * @mdev: mediated device structure
79 * @count: number of bytes to read
81 * Retuns number on bytes read on success or error.
82 * @write: Write emulation callback
83 * @mdev: mediated device structure
85 * @count: number of bytes to be written
87 * Retuns number on bytes written on success or error.
88 * @ioctl: IOCTL callback
89 * @mdev: mediated device structure
91 * @arg: arguments to ioctl
92 * @mmap: mmap callback
93 * @mdev: mediated device structure
95 * @request: request callback to release device
96 * @mdev: mediated device structure
97 * @count: request sequence number
98 * Parent device that support mediated device should be registered with mdev
99 * module with mdev_parent_ops structure.
101 struct mdev_parent_ops {
102 struct module *owner;
103 const struct attribute_group **dev_attr_groups;
104 const struct attribute_group **mdev_attr_groups;
105 struct attribute_group **supported_type_groups;
107 int (*create)(struct kobject *kobj, struct mdev_device *mdev);
108 int (*remove)(struct mdev_device *mdev);
109 int (*open)(struct mdev_device *mdev);
110 void (*release)(struct mdev_device *mdev);
111 ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
112 size_t count, loff_t *ppos);
113 ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
114 size_t count, loff_t *ppos);
115 long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
117 int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
118 void (*request)(struct mdev_device *mdev, unsigned int count);
121 /* interface for exporting mdev supported type attributes */
122 struct mdev_type_attribute {
123 struct attribute attr;
124 ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
125 ssize_t (*store)(struct kobject *kobj, struct device *dev,
126 const char *buf, size_t count);
129 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \
130 struct mdev_type_attribute mdev_type_attr_##_name = \
131 __ATTR(_name, _mode, _show, _store)
132 #define MDEV_TYPE_ATTR_RW(_name) \
133 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
134 #define MDEV_TYPE_ATTR_RO(_name) \
135 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
136 #define MDEV_TYPE_ATTR_WO(_name) \
137 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
140 * struct mdev_driver - Mediated device driver
141 * @probe: called when new device created
142 * @remove: called when device removed
143 * @driver: device driver structure
147 int (*probe)(struct mdev_device *dev);
148 void (*remove)(struct mdev_device *dev);
149 struct device_driver driver;
152 static inline void *mdev_get_drvdata(struct mdev_device *mdev)
154 return mdev->driver_data;
156 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
158 mdev->driver_data = data;
160 static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
165 extern struct bus_type mdev_bus_type;
167 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
168 void mdev_unregister_device(struct device *dev);
170 int mdev_register_driver(struct mdev_driver *drv);
171 void mdev_unregister_driver(struct mdev_driver *drv);
173 struct device *mdev_parent_dev(struct mdev_device *mdev);
174 static inline struct device *mdev_dev(struct mdev_device *mdev)
178 static inline struct mdev_device *mdev_from_dev(struct device *dev)
180 return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;