1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Mediated device definition 4 * 5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. 6 * Author: Neo Jia <[email protected]> 7 * Kirti Wankhede <[email protected]> 8 */ 9 10 #ifndef MDEV_H 11 #define MDEV_H 12 13 struct mdev_type; 14 15 struct mdev_device { 16 struct device dev; 17 guid_t uuid; 18 void *driver_data; 19 struct list_head next; 20 struct mdev_type *type; 21 struct device *iommu_device; 22 bool active; 23 }; 24 25 static inline struct mdev_device *to_mdev_device(struct device *dev) 26 { 27 return container_of(dev, struct mdev_device, dev); 28 } 29 30 /* 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. 34 * 35 * @dev: the mediated device that iommu will isolate. 36 * @iommu_device: a pci device which represents the iommu for @dev. 37 */ 38 static inline void mdev_set_iommu_device(struct mdev_device *mdev, 39 struct device *iommu_device) 40 { 41 mdev->iommu_device = iommu_device; 42 } 43 44 static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev) 45 { 46 return mdev->iommu_device; 47 } 48 49 /** 50 * struct mdev_parent_ops - Structure to be registered for each parent device to 51 * register the device to mdev module. 52 * 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' 67 * ops. 68 * @mdev: mdev_device device structure which is being 69 * destroyed 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 78 * @buf: read buffer 79 * @count: number of bytes to read 80 * @ppos: address. 81 * Retuns number on bytes read on success or error. 82 * @write: Write emulation callback 83 * @mdev: mediated device structure 84 * @buf: write buffer 85 * @count: number of bytes to be written 86 * @ppos: address. 87 * Retuns number on bytes written on success or error. 88 * @ioctl: IOCTL callback 89 * @mdev: mediated device structure 90 * @cmd: ioctl command 91 * @arg: arguments to ioctl 92 * @mmap: mmap callback 93 * @mdev: mediated device structure 94 * @vma: vma 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. 100 **/ 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; 106 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, 116 unsigned long arg); 117 int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma); 118 void (*request)(struct mdev_device *mdev, unsigned int count); 119 }; 120 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); 127 }; 128 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) 138 139 /** 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 144 * 145 **/ 146 struct mdev_driver { 147 int (*probe)(struct mdev_device *dev); 148 void (*remove)(struct mdev_device *dev); 149 struct device_driver driver; 150 }; 151 152 static inline void *mdev_get_drvdata(struct mdev_device *mdev) 153 { 154 return mdev->driver_data; 155 } 156 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data) 157 { 158 mdev->driver_data = data; 159 } 160 static inline const guid_t *mdev_uuid(struct mdev_device *mdev) 161 { 162 return &mdev->uuid; 163 } 164 165 extern struct bus_type mdev_bus_type; 166 167 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops); 168 void mdev_unregister_device(struct device *dev); 169 170 int mdev_register_driver(struct mdev_driver *drv); 171 void mdev_unregister_driver(struct mdev_driver *drv); 172 173 struct device *mdev_parent_dev(struct mdev_device *mdev); 174 static inline struct device *mdev_dev(struct mdev_device *mdev) 175 { 176 return &mdev->dev; 177 } 178 static inline struct mdev_device *mdev_from_dev(struct device *dev) 179 { 180 return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL; 181 } 182 183 #endif /* MDEV_H */ 184