Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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 | |
Alex Williamson | 99e3123 | 2016-12-30 08:13:44 -0700 | [diff] [blame] | 16 | struct mdev_device; |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 17 | |
Lu Baolu | 8ac1317 | 2019-04-12 12:13:24 +0800 | [diff] [blame^] | 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 | |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 32 | /** |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame] | 33 | * struct mdev_parent_ops - Structure to be registered for each parent device to |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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 |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame] | 79 | * module with mdev_parent_ops structure. |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 80 | **/ |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame] | 81 | struct mdev_parent_ops { |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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); |
Paul Gortmaker | c6ef7fd | 2017-01-04 15:08:15 -0500 | [diff] [blame] | 95 | long (*ioctl)(struct mdev_device *mdev, unsigned int cmd, |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 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) |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 134 | |
Alex Williamson | 99e3123 | 2016-12-30 08:13:44 -0700 | [diff] [blame] | 135 | extern void *mdev_get_drvdata(struct mdev_device *mdev); |
| 136 | extern void mdev_set_drvdata(struct mdev_device *mdev, void *data); |
Andy Shevchenko | 278bca7 | 2019-01-10 21:00:27 +0200 | [diff] [blame] | 137 | extern const guid_t *mdev_uuid(struct mdev_device *mdev); |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 138 | |
| 139 | extern struct bus_type mdev_bus_type; |
| 140 | |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 141 | extern int mdev_register_device(struct device *dev, |
Alex Williamson | 4293055 | 2016-12-30 08:13:38 -0700 | [diff] [blame] | 142 | const struct mdev_parent_ops *ops); |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 143 | extern void mdev_unregister_device(struct device *dev); |
| 144 | |
| 145 | extern int mdev_register_driver(struct mdev_driver *drv, struct module *owner); |
| 146 | extern void mdev_unregister_driver(struct mdev_driver *drv); |
| 147 | |
Alex Williamson | 9372e6fe | 2016-12-30 08:13:41 -0700 | [diff] [blame] | 148 | extern struct device *mdev_parent_dev(struct mdev_device *mdev); |
Alex Williamson | 99e3123 | 2016-12-30 08:13:44 -0700 | [diff] [blame] | 149 | extern struct device *mdev_dev(struct mdev_device *mdev); |
| 150 | extern struct mdev_device *mdev_from_dev(struct device *dev); |
Alex Williamson | 9372e6fe | 2016-12-30 08:13:41 -0700 | [diff] [blame] | 151 | |
Kirti Wankhede | 7b96953 | 2016-11-17 02:16:13 +0530 | [diff] [blame] | 152 | #endif /* MDEV_H */ |