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