blob: 0ce30ca78db0466e02cf8d3373d84ff5ab13a5fd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kirti Wankhede7b969532016-11-17 02:16:13 +05302/*
3 * Mediated device definition
4 *
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
Kirti Wankhede7b969532016-11-17 02:16:13 +05308 */
9
10#ifndef MDEV_H
11#define MDEV_H
12
Alex Williamson99e31232016-12-30 08:13:44 -070013struct mdev_device;
Kirti Wankhede7b969532016-11-17 02:16:13 +053014
Lu Baolu8ac13172019-04-12 12:13:24 +080015/*
16 * Called by the parent device driver to set the device which represents
17 * this mdev in iommu protection scope. By default, the iommu device is
18 * NULL, that indicates using vendor defined isolation.
19 *
20 * @dev: the mediated device that iommu will isolate.
21 * @iommu_device: a pci device which represents the iommu for @dev.
22 *
23 * Return 0 for success, otherwise negative error value.
24 */
25int mdev_set_iommu_device(struct device *dev, struct device *iommu_device);
26
27struct device *mdev_get_iommu_device(struct device *dev);
28
Kirti Wankhede7b969532016-11-17 02:16:13 +053029/**
Alex Williamson42930552016-12-30 08:13:38 -070030 * struct mdev_parent_ops - Structure to be registered for each parent device to
Kirti Wankhede7b969532016-11-17 02:16:13 +053031 * register the device to mdev module.
32 *
33 * @owner: The module owner.
34 * @dev_attr_groups: Attributes of the parent device.
35 * @mdev_attr_groups: Attributes of the mediated device.
36 * @supported_type_groups: Attributes to define supported types. It is mandatory
37 * to provide supported types.
38 * @create: Called to allocate basic resources in parent device's
39 * driver for a particular mediated device. It is
40 * mandatory to provide create ops.
41 * @kobj: kobject of type for which 'create' is called.
42 * @mdev: mdev_device structure on of mediated device
43 * that is being created
44 * Returns integer: success (0) or error (< 0)
45 * @remove: Called to free resources in parent device's driver for a
46 * a mediated device. It is mandatory to provide 'remove'
47 * ops.
48 * @mdev: mdev_device device structure which is being
49 * destroyed
50 * Returns integer: success (0) or error (< 0)
51 * @open: Open mediated device.
52 * @mdev: mediated device.
53 * Returns integer: success (0) or error (< 0)
54 * @release: release mediated device
55 * @mdev: mediated device.
56 * @read: Read emulation callback
57 * @mdev: mediated device structure
58 * @buf: read buffer
59 * @count: number of bytes to read
60 * @ppos: address.
61 * Retuns number on bytes read on success or error.
62 * @write: Write emulation callback
63 * @mdev: mediated device structure
64 * @buf: write buffer
65 * @count: number of bytes to be written
66 * @ppos: address.
67 * Retuns number on bytes written on success or error.
68 * @ioctl: IOCTL callback
69 * @mdev: mediated device structure
70 * @cmd: ioctl command
71 * @arg: arguments to ioctl
72 * @mmap: mmap callback
73 * @mdev: mediated device structure
74 * @vma: vma structure
75 * Parent device that support mediated device should be registered with mdev
Alex Williamson42930552016-12-30 08:13:38 -070076 * module with mdev_parent_ops structure.
Kirti Wankhede7b969532016-11-17 02:16:13 +053077 **/
Alex Williamson42930552016-12-30 08:13:38 -070078struct mdev_parent_ops {
Kirti Wankhede7b969532016-11-17 02:16:13 +053079 struct module *owner;
80 const struct attribute_group **dev_attr_groups;
81 const struct attribute_group **mdev_attr_groups;
82 struct attribute_group **supported_type_groups;
83
84 int (*create)(struct kobject *kobj, struct mdev_device *mdev);
85 int (*remove)(struct mdev_device *mdev);
86 int (*open)(struct mdev_device *mdev);
87 void (*release)(struct mdev_device *mdev);
88 ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
89 size_t count, loff_t *ppos);
90 ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
91 size_t count, loff_t *ppos);
Paul Gortmakerc6ef7fd2017-01-04 15:08:15 -050092 long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
Kirti Wankhede7b969532016-11-17 02:16:13 +053093 unsigned long arg);
94 int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
95};
96
97/* interface for exporting mdev supported type attributes */
98struct mdev_type_attribute {
99 struct attribute attr;
100 ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
101 ssize_t (*store)(struct kobject *kobj, struct device *dev,
102 const char *buf, size_t count);
103};
104
105#define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \
106struct mdev_type_attribute mdev_type_attr_##_name = \
107 __ATTR(_name, _mode, _show, _store)
108#define MDEV_TYPE_ATTR_RW(_name) \
109 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
110#define MDEV_TYPE_ATTR_RO(_name) \
111 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
112#define MDEV_TYPE_ATTR_WO(_name) \
113 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
114
115/**
116 * struct mdev_driver - Mediated device driver
117 * @name: driver name
118 * @probe: called when new device created
119 * @remove: called when device removed
120 * @driver: device driver structure
121 *
122 **/
123struct mdev_driver {
124 const char *name;
125 int (*probe)(struct device *dev);
126 void (*remove)(struct device *dev);
127 struct device_driver driver;
128};
129
130#define to_mdev_driver(drv) container_of(drv, struct mdev_driver, driver)
Kirti Wankhede7b969532016-11-17 02:16:13 +0530131
Parav Pandit50732af2019-04-30 17:49:30 -0500132void *mdev_get_drvdata(struct mdev_device *mdev);
133void mdev_set_drvdata(struct mdev_device *mdev, void *data);
134const guid_t *mdev_uuid(struct mdev_device *mdev);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530135
136extern struct bus_type mdev_bus_type;
137
Parav Pandit50732af2019-04-30 17:49:30 -0500138int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
139void mdev_unregister_device(struct device *dev);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530140
Parav Pandit50732af2019-04-30 17:49:30 -0500141int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
142void mdev_unregister_driver(struct mdev_driver *drv);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530143
Parav Pandit50732af2019-04-30 17:49:30 -0500144struct device *mdev_parent_dev(struct mdev_device *mdev);
145struct device *mdev_dev(struct mdev_device *mdev);
146struct mdev_device *mdev_from_dev(struct device *dev);
Alex Williamson9372e6fe2016-12-30 08:13:41 -0700147
Kirti Wankhede7b969532016-11-17 02:16:13 +0530148#endif /* MDEV_H */