blob: 89a52fd5756e482d1cc216afb4fbe700483b675a [file] [log] [blame]
Kirti Wankhede7b969532016-11-17 02:16:13 +05301/*
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
Alex Williamson99e31232016-12-30 08:13:44 -070016struct mdev_device;
Kirti Wankhede7b969532016-11-17 02:16:13 +053017
Lu Baolu8ac13172019-04-12 12:13:24 +080018/*
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 */
28int mdev_set_iommu_device(struct device *dev, struct device *iommu_device);
29
30struct device *mdev_get_iommu_device(struct device *dev);
31
Kirti Wankhede7b969532016-11-17 02:16:13 +053032/**
Alex Williamson42930552016-12-30 08:13:38 -070033 * struct mdev_parent_ops - Structure to be registered for each parent device to
Kirti Wankhede7b969532016-11-17 02:16:13 +053034 * 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
Alex Williamson42930552016-12-30 08:13:38 -070079 * module with mdev_parent_ops structure.
Kirti Wankhede7b969532016-11-17 02:16:13 +053080 **/
Alex Williamson42930552016-12-30 08:13:38 -070081struct mdev_parent_ops {
Kirti Wankhede7b969532016-11-17 02:16:13 +053082 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);
Paul Gortmakerc6ef7fd2017-01-04 15:08:15 -050095 long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
Kirti Wankhede7b969532016-11-17 02:16:13 +053096 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 */
101struct 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) \
109struct 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 **/
126struct 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)
Kirti Wankhede7b969532016-11-17 02:16:13 +0530134
Parav Pandit50732af2019-04-30 17:49:30 -0500135void *mdev_get_drvdata(struct mdev_device *mdev);
136void mdev_set_drvdata(struct mdev_device *mdev, void *data);
137const guid_t *mdev_uuid(struct mdev_device *mdev);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530138
139extern struct bus_type mdev_bus_type;
140
Parav Pandit50732af2019-04-30 17:49:30 -0500141int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
142void mdev_unregister_device(struct device *dev);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530143
Parav Pandit50732af2019-04-30 17:49:30 -0500144int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
145void mdev_unregister_driver(struct mdev_driver *drv);
Kirti Wankhede7b969532016-11-17 02:16:13 +0530146
Parav Pandit50732af2019-04-30 17:49:30 -0500147struct device *mdev_parent_dev(struct mdev_device *mdev);
148struct device *mdev_dev(struct mdev_device *mdev);
149struct mdev_device *mdev_from_dev(struct device *dev);
Alex Williamson9372e6fe2016-12-30 08:13:41 -0700150
Kirti Wankhede7b969532016-11-17 02:16:13 +0530151#endif /* MDEV_H */