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