123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- #ifndef _LINUX_VDPA_H
- #define _LINUX_VDPA_H
- #include <linux/kernel.h>
- #include <linux/device.h>
- #include <linux/interrupt.h>
- #include <linux/vhost_iotlb.h>
- #include <linux/virtio_net.h>
- #include <linux/if_ether.h>
- struct vdpa_callback {
- irqreturn_t (*callback)(void *data);
- void *private;
- };
- struct vdpa_notification_area {
- resource_size_t addr;
- resource_size_t size;
- };
- struct vdpa_vq_state_split {
- u16 avail_index;
- };
- struct vdpa_vq_state_packed {
- u16 last_avail_counter:1;
- u16 last_avail_idx:15;
- u16 last_used_counter:1;
- u16 last_used_idx:15;
- };
- struct vdpa_vq_state {
- union {
- struct vdpa_vq_state_split split;
- struct vdpa_vq_state_packed packed;
- };
- };
- struct vdpa_mgmt_dev;
- struct vdpa_device {
- struct device dev;
- struct device *dma_dev;
- const char *driver_override;
- const struct vdpa_config_ops *config;
- struct rw_semaphore cf_lock;
- unsigned int index;
- bool features_valid;
- bool use_va;
- u32 nvqs;
- struct vdpa_mgmt_dev *mdev;
- unsigned int ngroups;
- unsigned int nas;
- };
- struct vdpa_iova_range {
- u64 first;
- u64 last;
- };
- struct vdpa_dev_set_config {
- u64 device_features;
- struct {
- u8 mac[ETH_ALEN];
- u16 mtu;
- u16 max_vq_pairs;
- } net;
- u64 mask;
- };
- struct vdpa_map_file {
- struct file *file;
- u64 offset;
- };
- struct vdpa_config_ops {
-
- int (*set_vq_address)(struct vdpa_device *vdev,
- u16 idx, u64 desc_area, u64 driver_area,
- u64 device_area);
- void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
- void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
- void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
- struct vdpa_callback *cb);
- void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
- bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
- int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
- const struct vdpa_vq_state *state);
- int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
- struct vdpa_vq_state *state);
- int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
- struct sk_buff *msg,
- struct netlink_ext_ack *extack);
- struct vdpa_notification_area
- (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
-
- int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
-
- u32 (*get_vq_align)(struct vdpa_device *vdev);
- u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
- u64 (*get_device_features)(struct vdpa_device *vdev);
- int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
- u64 (*get_driver_features)(struct vdpa_device *vdev);
- void (*set_config_cb)(struct vdpa_device *vdev,
- struct vdpa_callback *cb);
- u16 (*get_vq_num_max)(struct vdpa_device *vdev);
- u16 (*get_vq_num_min)(struct vdpa_device *vdev);
- u32 (*get_device_id)(struct vdpa_device *vdev);
- u32 (*get_vendor_id)(struct vdpa_device *vdev);
- u8 (*get_status)(struct vdpa_device *vdev);
- void (*set_status)(struct vdpa_device *vdev, u8 status);
- int (*reset)(struct vdpa_device *vdev);
- int (*suspend)(struct vdpa_device *vdev);
- size_t (*get_config_size)(struct vdpa_device *vdev);
- void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
- void *buf, unsigned int len);
- void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
- const void *buf, unsigned int len);
- u32 (*get_generation)(struct vdpa_device *vdev);
- struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
-
- int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
- struct vhost_iotlb *iotlb);
- int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
- u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
- int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
- u64 iova, u64 size);
- int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
- unsigned int asid);
-
- void (*free)(struct vdpa_device *vdev);
- };
- struct vdpa_device *__vdpa_alloc_device(struct device *parent,
- const struct vdpa_config_ops *config,
- unsigned int ngroups, unsigned int nas,
- size_t size, const char *name,
- bool use_va);
- #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
- name, use_va) \
- container_of((__vdpa_alloc_device( \
- parent, config, ngroups, nas, \
- (sizeof(dev_struct) + \
- BUILD_BUG_ON_ZERO(offsetof( \
- dev_struct, member))), name, use_va)), \
- dev_struct, member)
- int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
- void vdpa_unregister_device(struct vdpa_device *vdev);
- int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
- void _vdpa_unregister_device(struct vdpa_device *vdev);
- struct vdpa_driver {
- struct device_driver driver;
- int (*probe)(struct vdpa_device *vdev);
- void (*remove)(struct vdpa_device *vdev);
- };
- #define vdpa_register_driver(drv) \
- __vdpa_register_driver(drv, THIS_MODULE)
- int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
- void vdpa_unregister_driver(struct vdpa_driver *drv);
- #define module_vdpa_driver(__vdpa_driver) \
- module_driver(__vdpa_driver, vdpa_register_driver, \
- vdpa_unregister_driver)
- static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
- {
- return container_of(driver, struct vdpa_driver, driver);
- }
- static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
- {
- return container_of(_dev, struct vdpa_device, dev);
- }
- static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
- {
- return dev_get_drvdata(&vdev->dev);
- }
- static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
- {
- dev_set_drvdata(&vdev->dev, data);
- }
- static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
- {
- return vdev->dma_dev;
- }
- static inline int vdpa_reset(struct vdpa_device *vdev)
- {
- const struct vdpa_config_ops *ops = vdev->config;
- int ret;
- down_write(&vdev->cf_lock);
- vdev->features_valid = false;
- ret = ops->reset(vdev);
- up_write(&vdev->cf_lock);
- return ret;
- }
- static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
- {
- const struct vdpa_config_ops *ops = vdev->config;
- int ret;
- vdev->features_valid = true;
- ret = ops->set_driver_features(vdev, features);
- return ret;
- }
- static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
- {
- int ret;
- down_write(&vdev->cf_lock);
- ret = vdpa_set_features_unlocked(vdev, features);
- up_write(&vdev->cf_lock);
- return ret;
- }
- void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
- void *buf, unsigned int len);
- void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
- const void *buf, unsigned int length);
- void vdpa_set_status(struct vdpa_device *vdev, u8 status);
- struct vdpa_mgmtdev_ops {
- int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
- const struct vdpa_dev_set_config *config);
- void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
- };
- struct vdpa_mgmt_dev {
- struct device *device;
- const struct vdpa_mgmtdev_ops *ops;
- struct virtio_device_id *id_table;
- u64 config_attr_mask;
- struct list_head list;
- u64 supported_features;
- u32 max_supported_vqs;
- };
- int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
- void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
- #endif
|