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 unsigned int mdev_get_type_group_id(struct mdev_device *mdev); 50 unsigned int mtype_get_type_group_id(struct mdev_type *mtype); 51 struct device *mtype_get_parent_dev(struct mdev_type *mtype); 52 53 /** 54 * struct mdev_parent_ops - Structure to be registered for each parent device to 55 * register the device to mdev module. 56 * 57 * @owner: The module owner. 58 * @device_driver: Which device driver to probe() on newly created devices 59 * @dev_attr_groups: Attributes of the parent device. 60 * @mdev_attr_groups: Attributes of the mediated device. 61 * @supported_type_groups: Attributes to define supported types. It is mandatory 62 * to provide supported types. 63 * @create: Called to allocate basic resources in parent device's 64 * driver for a particular mediated device. It is 65 * mandatory to provide create ops. 66 * @mdev: mdev_device structure on of mediated device 67 * that is being created 68 * Returns integer: success (0) or error (< 0) 69 * @remove: Called to free resources in parent device's driver for 70 * a mediated device. It is mandatory to provide 'remove' 71 * ops. 72 * @mdev: mdev_device device structure which is being 73 * destroyed 74 * Returns integer: success (0) or error (< 0) 75 * @open: Open mediated device. 76 * @mdev: mediated device. 77 * Returns integer: success (0) or error (< 0) 78 * @release: release mediated device 79 * @mdev: mediated device. 80 * @read: Read emulation callback 81 * @mdev: mediated device structure 82 * @buf: read buffer 83 * @count: number of bytes to read 84 * @ppos: address. 85 * Retuns number on bytes read on success or error. 86 * @write: Write emulation callback 87 * @mdev: mediated device structure 88 * @buf: write buffer 89 * @count: number of bytes to be written 90 * @ppos: address. 91 * Retuns number on bytes written on success or error. 92 * @ioctl: IOCTL callback 93 * @mdev: mediated device structure 94 * @cmd: ioctl command 95 * @arg: arguments to ioctl 96 * @mmap: mmap callback 97 * @mdev: mediated device structure 98 * @vma: vma structure 99 * @request: request callback to release device 100 * @mdev: mediated device structure 101 * @count: request sequence number 102 * Parent device that support mediated device should be registered with mdev 103 * module with mdev_parent_ops structure. 104 **/ 105 struct mdev_parent_ops { 106 struct module *owner; 107 struct mdev_driver *device_driver; 108 const struct attribute_group **dev_attr_groups; 109 const struct attribute_group **mdev_attr_groups; 110 struct attribute_group **supported_type_groups; 111 112 int (*create)(struct mdev_device *mdev); 113 int (*remove)(struct mdev_device *mdev); 114 int (*open_device)(struct mdev_device *mdev); 115 void (*close_device)(struct mdev_device *mdev); 116 int (*open)(struct mdev_device *mdev); 117 void (*release)(struct mdev_device *mdev); 118 ssize_t (*read)(struct mdev_device *mdev, char __user *buf, 119 size_t count, loff_t *ppos); 120 ssize_t (*write)(struct mdev_device *mdev, const char __user *buf, 121 size_t count, loff_t *ppos); 122 long (*ioctl)(struct mdev_device *mdev, unsigned int cmd, 123 unsigned long arg); 124 int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma); 125 void (*request)(struct mdev_device *mdev, unsigned int count); 126 }; 127 128 /* interface for exporting mdev supported type attributes */ 129 struct mdev_type_attribute { 130 struct attribute attr; 131 ssize_t (*show)(struct mdev_type *mtype, 132 struct mdev_type_attribute *attr, char *buf); 133 ssize_t (*store)(struct mdev_type *mtype, 134 struct mdev_type_attribute *attr, const char *buf, 135 size_t count); 136 }; 137 138 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \ 139 struct mdev_type_attribute mdev_type_attr_##_name = \ 140 __ATTR(_name, _mode, _show, _store) 141 #define MDEV_TYPE_ATTR_RW(_name) \ 142 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name) 143 #define MDEV_TYPE_ATTR_RO(_name) \ 144 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name) 145 #define MDEV_TYPE_ATTR_WO(_name) \ 146 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name) 147 148 /** 149 * struct mdev_driver - Mediated device driver 150 * @probe: called when new device created 151 * @remove: called when device removed 152 * @driver: device driver structure 153 * 154 **/ 155 struct mdev_driver { 156 int (*probe)(struct mdev_device *dev); 157 void (*remove)(struct mdev_device *dev); 158 struct device_driver driver; 159 }; 160 161 static inline void *mdev_get_drvdata(struct mdev_device *mdev) 162 { 163 return mdev->driver_data; 164 } 165 static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data) 166 { 167 mdev->driver_data = data; 168 } 169 static inline const guid_t *mdev_uuid(struct mdev_device *mdev) 170 { 171 return &mdev->uuid; 172 } 173 174 extern struct bus_type mdev_bus_type; 175 176 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops); 177 void mdev_unregister_device(struct device *dev); 178 179 int mdev_register_driver(struct mdev_driver *drv); 180 void mdev_unregister_driver(struct mdev_driver *drv); 181 182 struct device *mdev_parent_dev(struct mdev_device *mdev); 183 static inline struct device *mdev_dev(struct mdev_device *mdev) 184 { 185 return &mdev->dev; 186 } 187 static inline struct mdev_device *mdev_from_dev(struct device *dev) 188 { 189 return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL; 190 } 191 192 #endif /* MDEV_H */ 193