1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 2 /* Copyright (c) 2020 Mellanox Technologies Ltd. */ 3 4 #ifndef __MLX5_VDPA_H__ 5 #define __MLX5_VDPA_H__ 6 7 #include <linux/etherdevice.h> 8 #include <linux/vringh.h> 9 #include <linux/vdpa.h> 10 #include <linux/mlx5/driver.h> 11 12 #define MLX5V_ETH_HARD_MTU (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN) 13 14 extern int mlx5_vdpa_max_iotlb_entries; 15 16 struct mlx5_vdpa_direct_mr { 17 u64 start; 18 u64 end; 19 u32 perm; 20 u32 mr; 21 struct sg_table sg_head; 22 int log_size; 23 int nsg; 24 int nent; 25 struct list_head list; 26 u64 offset; 27 }; 28 29 struct mlx5_vdpa_mr { 30 u32 mkey; 31 32 /* list of direct MRs descendants of this indirect mr */ 33 struct list_head head; 34 unsigned long num_directs; 35 unsigned long num_klms; 36 37 struct vhost_iotlb *iotlb; 38 39 bool user_mr; 40 41 refcount_t refcount; 42 struct list_head mr_list; 43 }; 44 45 struct mlx5_vdpa_resources { 46 u32 pdn; 47 struct mlx5_uars_page *uar; 48 void __iomem *kick_addr; 49 u64 phys_kick_addr; 50 u16 uid; 51 u32 null_mkey; 52 bool valid; 53 }; 54 55 struct mlx5_control_vq { 56 struct vhost_iotlb *iotlb; 57 /* spinlock to synchronize iommu table */ 58 spinlock_t iommu_lock; 59 struct vringh vring; 60 bool ready; 61 u64 desc_addr; 62 u64 device_addr; 63 u64 driver_addr; 64 struct vdpa_callback event_cb; 65 struct vringh_kiov riov; 66 struct vringh_kiov wiov; 67 unsigned short head; 68 unsigned int received_desc; 69 unsigned int completed_desc; 70 }; 71 72 struct mlx5_vdpa_wq_ent { 73 struct work_struct work; 74 struct mlx5_vdpa_dev *mvdev; 75 }; 76 77 enum { 78 MLX5_VDPA_DATAVQ_GROUP, 79 MLX5_VDPA_CVQ_GROUP, 80 MLX5_VDPA_DATAVQ_DESC_GROUP, 81 MLX5_VDPA_NUMVQ_GROUPS 82 }; 83 84 enum { 85 MLX5_VDPA_NUM_AS = 2 86 }; 87 88 struct mlx5_vdpa_mr_resources { 89 struct mlx5_vdpa_mr *mr[MLX5_VDPA_NUM_AS]; 90 unsigned int group2asid[MLX5_VDPA_NUMVQ_GROUPS]; 91 92 /* Pre-deletion mr list */ 93 struct list_head mr_list_head; 94 95 /* Deferred mr list */ 96 struct list_head mr_gc_list_head; 97 struct workqueue_struct *wq_gc; 98 struct delayed_work gc_dwork_ent; 99 100 struct mutex lock; 101 102 atomic_t shutdown; 103 }; 104 105 struct mlx5_vdpa_dev { 106 struct vdpa_device vdev; 107 struct mlx5_core_dev *mdev; 108 struct mlx5_vdpa_resources res; 109 struct mlx5_vdpa_mr_resources mres; 110 111 u64 mlx_features; 112 u64 actual_features; 113 u8 status; 114 u32 max_vqs; 115 u16 max_idx; 116 u32 generation; 117 118 struct mlx5_control_vq cvq; 119 struct workqueue_struct *wq; 120 bool suspended; 121 122 struct mlx5_async_ctx async_ctx; 123 }; 124 125 struct mlx5_vdpa_async_cmd { 126 int err; 127 struct mlx5_async_work cb_work; 128 struct completion cmd_done; 129 130 void *in; 131 size_t inlen; 132 133 void *out; 134 size_t outlen; 135 }; 136 137 int mlx5_vdpa_create_tis(struct mlx5_vdpa_dev *mvdev, void *in, u32 *tisn); 138 void mlx5_vdpa_destroy_tis(struct mlx5_vdpa_dev *mvdev, u32 tisn); 139 int mlx5_vdpa_create_rqt(struct mlx5_vdpa_dev *mvdev, void *in, int inlen, u32 *rqtn); 140 int mlx5_vdpa_modify_rqt(struct mlx5_vdpa_dev *mvdev, void *in, int inlen, u32 rqtn); 141 void mlx5_vdpa_destroy_rqt(struct mlx5_vdpa_dev *mvdev, u32 rqtn); 142 int mlx5_vdpa_create_tir(struct mlx5_vdpa_dev *mvdev, void *in, u32 *tirn); 143 void mlx5_vdpa_destroy_tir(struct mlx5_vdpa_dev *mvdev, u32 tirn); 144 int mlx5_vdpa_alloc_transport_domain(struct mlx5_vdpa_dev *mvdev, u32 *tdn); 145 void mlx5_vdpa_dealloc_transport_domain(struct mlx5_vdpa_dev *mvdev, u32 tdn); 146 int mlx5_vdpa_alloc_resources(struct mlx5_vdpa_dev *mvdev); 147 void mlx5_vdpa_free_resources(struct mlx5_vdpa_dev *mvdev); 148 int mlx5_vdpa_create_mkey(struct mlx5_vdpa_dev *mvdev, u32 *mkey, u32 *in, 149 int inlen); 150 int mlx5_vdpa_destroy_mkey(struct mlx5_vdpa_dev *mvdev, u32 mkey); 151 struct mlx5_vdpa_mr *mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev, 152 struct vhost_iotlb *iotlb); 153 int mlx5_vdpa_init_mr_resources(struct mlx5_vdpa_dev *mvdev); 154 void mlx5_vdpa_destroy_mr_resources(struct mlx5_vdpa_dev *mvdev); 155 void mlx5_vdpa_clean_mrs(struct mlx5_vdpa_dev *mvdev); 156 void mlx5_vdpa_get_mr(struct mlx5_vdpa_dev *mvdev, 157 struct mlx5_vdpa_mr *mr); 158 void mlx5_vdpa_put_mr(struct mlx5_vdpa_dev *mvdev, 159 struct mlx5_vdpa_mr *mr); 160 void mlx5_vdpa_update_mr(struct mlx5_vdpa_dev *mvdev, 161 struct mlx5_vdpa_mr *mr, 162 unsigned int asid); 163 int mlx5_vdpa_update_cvq_iotlb(struct mlx5_vdpa_dev *mvdev, 164 struct vhost_iotlb *iotlb, 165 unsigned int asid); 166 int mlx5_vdpa_create_dma_mr(struct mlx5_vdpa_dev *mvdev); 167 int mlx5_vdpa_reset_mr(struct mlx5_vdpa_dev *mvdev, unsigned int asid); 168 int mlx5_vdpa_exec_async_cmds(struct mlx5_vdpa_dev *mvdev, 169 struct mlx5_vdpa_async_cmd *cmds, 170 int num_cmds); 171 172 #define mlx5_vdpa_err(__dev, format, ...) \ 173 dev_err((__dev)->mdev->device, "%s:%d:(pid %d) error: " format, __func__, __LINE__, \ 174 current->pid, ##__VA_ARGS__) 175 176 177 #define mlx5_vdpa_warn(__dev, format, ...) \ 178 dev_warn((__dev)->mdev->device, "%s:%d:(pid %d) warning: " format, __func__, __LINE__, \ 179 current->pid, ##__VA_ARGS__) 180 181 #define mlx5_vdpa_info(__dev, format, ...) \ 182 dev_info((__dev)->mdev->device, "%s:%d:(pid %d): " format, __func__, __LINE__, \ 183 current->pid, ##__VA_ARGS__) 184 185 #define mlx5_vdpa_dbg(__dev, format, ...) \ 186 dev_debug((__dev)->mdev->device, "%s:%d:(pid %d): " format, __func__, __LINE__, \ 187 current->pid, ##__VA_ARGS__) 188 189 #endif /* __MLX5_VDPA_H__ */ 190