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 struct mlx5_vdpa_direct_mr {
15 	u64 start;
16 	u64 end;
17 	u32 perm;
18 	u32 mr;
19 	struct sg_table sg_head;
20 	int log_size;
21 	int nsg;
22 	int nent;
23 	struct list_head list;
24 	u64 offset;
25 };
26 
27 struct mlx5_vdpa_mr {
28 	u32 mkey;
29 
30 	/* list of direct MRs descendants of this indirect mr */
31 	struct list_head head;
32 	unsigned long num_directs;
33 	unsigned long num_klms;
34 	/* state of dvq mr */
35 	bool initialized;
36 
37 	/* serialize mkey creation and destruction */
38 	struct mutex mkey_mtx;
39 	bool user_mr;
40 };
41 
42 struct mlx5_vdpa_resources {
43 	u32 pdn;
44 	struct mlx5_uars_page *uar;
45 	void __iomem *kick_addr;
46 	u64 phys_kick_addr;
47 	u16 uid;
48 	u32 null_mkey;
49 	bool valid;
50 };
51 
52 struct mlx5_control_vq {
53 	struct vhost_iotlb *iotlb;
54 	/* spinlock to synchronize iommu table */
55 	spinlock_t iommu_lock;
56 	struct vringh vring;
57 	bool ready;
58 	u64 desc_addr;
59 	u64 device_addr;
60 	u64 driver_addr;
61 	struct vdpa_callback event_cb;
62 	struct vringh_kiov riov;
63 	struct vringh_kiov wiov;
64 	unsigned short head;
65 	unsigned int received_desc;
66 	unsigned int completed_desc;
67 };
68 
69 struct mlx5_vdpa_wq_ent {
70 	struct work_struct work;
71 	struct mlx5_vdpa_dev *mvdev;
72 };
73 
74 enum {
75 	MLX5_VDPA_DATAVQ_GROUP,
76 	MLX5_VDPA_CVQ_GROUP,
77 	MLX5_VDPA_NUMVQ_GROUPS
78 };
79 
80 enum {
81 	MLX5_VDPA_NUM_AS = MLX5_VDPA_NUMVQ_GROUPS
82 };
83 
84 struct mlx5_vdpa_dev {
85 	struct vdpa_device vdev;
86 	struct mlx5_core_dev *mdev;
87 	struct mlx5_vdpa_resources res;
88 
89 	u64 mlx_features;
90 	u64 actual_features;
91 	u8 status;
92 	u32 max_vqs;
93 	u16 max_idx;
94 	u32 generation;
95 
96 	struct mlx5_vdpa_mr mr;
97 	struct mlx5_control_vq cvq;
98 	struct workqueue_struct *wq;
99 	unsigned int group2asid[MLX5_VDPA_NUMVQ_GROUPS];
100 	bool suspended;
101 };
102 
103 int mlx5_vdpa_create_tis(struct mlx5_vdpa_dev *mvdev, void *in, u32 *tisn);
104 void mlx5_vdpa_destroy_tis(struct mlx5_vdpa_dev *mvdev, u32 tisn);
105 int mlx5_vdpa_create_rqt(struct mlx5_vdpa_dev *mvdev, void *in, int inlen, u32 *rqtn);
106 int mlx5_vdpa_modify_rqt(struct mlx5_vdpa_dev *mvdev, void *in, int inlen, u32 rqtn);
107 void mlx5_vdpa_destroy_rqt(struct mlx5_vdpa_dev *mvdev, u32 rqtn);
108 int mlx5_vdpa_create_tir(struct mlx5_vdpa_dev *mvdev, void *in, u32 *tirn);
109 void mlx5_vdpa_destroy_tir(struct mlx5_vdpa_dev *mvdev, u32 tirn);
110 int mlx5_vdpa_alloc_transport_domain(struct mlx5_vdpa_dev *mvdev, u32 *tdn);
111 void mlx5_vdpa_dealloc_transport_domain(struct mlx5_vdpa_dev *mvdev, u32 tdn);
112 int mlx5_vdpa_alloc_resources(struct mlx5_vdpa_dev *mvdev);
113 void mlx5_vdpa_free_resources(struct mlx5_vdpa_dev *mvdev);
114 int mlx5_vdpa_create_mkey(struct mlx5_vdpa_dev *mvdev, u32 *mkey, u32 *in,
115 			  int inlen);
116 int mlx5_vdpa_destroy_mkey(struct mlx5_vdpa_dev *mvdev, u32 mkey);
117 int mlx5_vdpa_handle_set_map(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
118 			     bool *change_map, unsigned int asid);
119 int mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
120 			unsigned int asid);
121 void mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev);
122 void mlx5_vdpa_destroy_mr_asid(struct mlx5_vdpa_dev *mvdev, unsigned int asid);
123 
124 #define mlx5_vdpa_warn(__dev, format, ...)                                                         \
125 	dev_warn((__dev)->mdev->device, "%s:%d:(pid %d) warning: " format, __func__, __LINE__,     \
126 		 current->pid, ##__VA_ARGS__)
127 
128 #define mlx5_vdpa_info(__dev, format, ...)                                                         \
129 	dev_info((__dev)->mdev->device, "%s:%d:(pid %d): " format, __func__, __LINE__,             \
130 		 current->pid, ##__VA_ARGS__)
131 
132 #define mlx5_vdpa_dbg(__dev, format, ...)                                                          \
133 	dev_debug((__dev)->mdev->device, "%s:%d:(pid %d): " format, __func__, __LINE__,            \
134 		  current->pid, ##__VA_ARGS__)
135 
136 #endif /* __MLX5_VDPA_H__ */
137