blob: 45c421c871007ed488df7df2fc1b58f322a4f661 [file] [log] [blame]
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/*
3 * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
4 */
5
6#include <rdma/ib_user_verbs.h>
7#include <rdma/ib_verbs.h>
8#include <rdma/uverbs_types.h>
9#include <rdma/uverbs_ioctl.h>
10#include <rdma/mlx5_user_ioctl_cmds.h>
11#include <rdma/ib_umem.h>
12#include <linux/mlx5/driver.h>
13#include <linux/mlx5/fs.h>
14#include "mlx5_ib.h"
15
Yishai Hadas8aa8c952018-06-17 13:00:00 +030016#define UVERBS_MODULE_NAME mlx5_ib
17#include <rdma/uverbs_named_ioctl.h>
18
Yishai Hadas7efce362018-06-17 13:00:01 +030019#define MLX5_MAX_DESTROY_INBOX_SIZE_DW MLX5_ST_SZ_DW(delete_fte_in)
20struct devx_obj {
21 struct mlx5_core_dev *mdev;
Yishai Hadas2351776e2018-10-07 12:06:34 +030022 u64 obj_id;
Yishai Hadas7efce362018-06-17 13:00:01 +030023 u32 dinlen; /* destroy inbox length */
24 u32 dinbox[MLX5_MAX_DESTROY_INBOX_SIZE_DW];
25};
26
Yishai Hadasaeae9452018-06-17 13:00:04 +030027struct devx_umem {
28 struct mlx5_core_dev *mdev;
29 struct ib_umem *umem;
30 u32 page_offset;
31 int page_shift;
32 int ncont;
33 u32 dinlen;
34 u32 dinbox[MLX5_ST_SZ_DW(general_obj_in_cmd_hdr)];
35};
36
37struct devx_umem_reg_cmd {
38 void *in;
39 u32 inlen;
40 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
41};
42
Yishai Hadas8aa8c952018-06-17 13:00:00 +030043static struct mlx5_ib_ucontext *devx_ufile2uctx(struct ib_uverbs_file *file)
44{
45 return to_mucontext(ib_uverbs_get_ucontext(file));
46}
47
Yishai Hadas76dc5a82018-09-20 21:45:19 +030048int mlx5_ib_devx_create(struct mlx5_ib_dev *dev)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030049{
50 u32 in[MLX5_ST_SZ_DW(create_uctx_in)] = {0};
51 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)] = {0};
52 u64 general_obj_types;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030053 void *hdr;
54 int err;
Yishai Hadas76dc5a82018-09-20 21:45:19 +030055 u16 uid;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030056
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030057 hdr = MLX5_ADDR_OF(create_uctx_in, in, hdr);
58
59 general_obj_types = MLX5_CAP_GEN_64(dev->mdev, general_obj_types);
60 if (!(general_obj_types & MLX5_GENERAL_OBJ_TYPES_CAP_UCTX) ||
61 !(general_obj_types & MLX5_GENERAL_OBJ_TYPES_CAP_UMEM))
62 return -EINVAL;
63
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030064 MLX5_SET(general_obj_in_cmd_hdr, hdr, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
65 MLX5_SET(general_obj_in_cmd_hdr, hdr, obj_type, MLX5_OBJ_TYPE_UCTX);
66
67 err = mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
68 if (err)
69 return err;
70
Yishai Hadas76dc5a82018-09-20 21:45:19 +030071 uid = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
72 return uid;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030073}
74
Yishai Hadas76dc5a82018-09-20 21:45:19 +030075void mlx5_ib_devx_destroy(struct mlx5_ib_dev *dev, u16 uid)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030076{
77 u32 in[MLX5_ST_SZ_DW(general_obj_in_cmd_hdr)] = {0};
78 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)] = {0};
79
80 MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
81 MLX5_SET(general_obj_in_cmd_hdr, in, obj_type, MLX5_OBJ_TYPE_UCTX);
Yishai Hadas76dc5a82018-09-20 21:45:19 +030082 MLX5_SET(general_obj_in_cmd_hdr, in, obj_id, uid);
Yishai Hadasa8b92ca2018-06-17 12:59:57 +030083
84 mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
85}
Yishai Hadas8aa8c952018-06-17 13:00:00 +030086
Yishai Hadas32269442018-07-23 15:25:09 +030087bool mlx5_ib_devx_is_flow_dest(void *obj, int *dest_id, int *dest_type)
88{
89 struct devx_obj *devx_obj = obj;
90 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox, opcode);
91
92 switch (opcode) {
93 case MLX5_CMD_OP_DESTROY_TIR:
94 *dest_type = MLX5_FLOW_DESTINATION_TYPE_TIR;
95 *dest_id = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox,
96 obj_id);
97 return true;
98
99 case MLX5_CMD_OP_DESTROY_FLOW_TABLE:
100 *dest_type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
101 *dest_id = MLX5_GET(destroy_flow_table_in, devx_obj->dinbox,
102 table_id);
103 return true;
104 default:
105 return false;
106 }
107}
108
Yishai Hadas2351776e2018-10-07 12:06:34 +0300109/*
110 * As the obj_id in the firmware is not globally unique the object type
111 * must be considered upon checking for a valid object id.
112 * For that the opcode of the creator command is encoded as part of the obj_id.
113 */
114static u64 get_enc_obj_id(u16 opcode, u32 obj_id)
115{
116 return ((u64)opcode << 32) | obj_id;
117}
118
Yishai Hadase662e142018-06-17 13:00:02 +0300119static int devx_is_valid_obj_id(struct devx_obj *obj, const void *in)
120{
121 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
Yishai Hadas2351776e2018-10-07 12:06:34 +0300122 u64 obj_id;
Yishai Hadase662e142018-06-17 13:00:02 +0300123
124 switch (opcode) {
125 case MLX5_CMD_OP_MODIFY_GENERAL_OBJECT:
126 case MLX5_CMD_OP_QUERY_GENERAL_OBJECT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300127 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_GENERAL_OBJECT,
128 MLX5_GET(general_obj_in_cmd_hdr, in,
129 obj_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300130 break;
131 case MLX5_CMD_OP_QUERY_MKEY:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300132 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_MKEY,
133 MLX5_GET(query_mkey_in, in,
134 mkey_index));
Yishai Hadase662e142018-06-17 13:00:02 +0300135 break;
136 case MLX5_CMD_OP_QUERY_CQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300137 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_CQ,
138 MLX5_GET(query_cq_in, in, cqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300139 break;
140 case MLX5_CMD_OP_MODIFY_CQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300141 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_CQ,
142 MLX5_GET(modify_cq_in, in, cqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300143 break;
144 case MLX5_CMD_OP_QUERY_SQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300145 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_SQ,
146 MLX5_GET(query_sq_in, in, sqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300147 break;
148 case MLX5_CMD_OP_MODIFY_SQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300149 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_SQ,
150 MLX5_GET(modify_sq_in, in, sqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300151 break;
152 case MLX5_CMD_OP_QUERY_RQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300153 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RQ,
154 MLX5_GET(query_rq_in, in, rqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300155 break;
156 case MLX5_CMD_OP_MODIFY_RQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300157 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RQ,
158 MLX5_GET(modify_rq_in, in, rqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300159 break;
160 case MLX5_CMD_OP_QUERY_RMP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300161 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RMP,
162 MLX5_GET(query_rmp_in, in, rmpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300163 break;
164 case MLX5_CMD_OP_MODIFY_RMP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300165 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RMP,
166 MLX5_GET(modify_rmp_in, in, rmpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300167 break;
168 case MLX5_CMD_OP_QUERY_RQT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300169 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RQT,
170 MLX5_GET(query_rqt_in, in, rqtn));
Yishai Hadase662e142018-06-17 13:00:02 +0300171 break;
172 case MLX5_CMD_OP_MODIFY_RQT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300173 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RQT,
174 MLX5_GET(modify_rqt_in, in, rqtn));
Yishai Hadase662e142018-06-17 13:00:02 +0300175 break;
176 case MLX5_CMD_OP_QUERY_TIR:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300177 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_TIR,
178 MLX5_GET(query_tir_in, in, tirn));
Yishai Hadase662e142018-06-17 13:00:02 +0300179 break;
180 case MLX5_CMD_OP_MODIFY_TIR:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300181 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_TIR,
182 MLX5_GET(modify_tir_in, in, tirn));
Yishai Hadase662e142018-06-17 13:00:02 +0300183 break;
184 case MLX5_CMD_OP_QUERY_TIS:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300185 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_TIS,
186 MLX5_GET(query_tis_in, in, tisn));
Yishai Hadase662e142018-06-17 13:00:02 +0300187 break;
188 case MLX5_CMD_OP_MODIFY_TIS:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300189 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_TIS,
190 MLX5_GET(modify_tis_in, in, tisn));
Yishai Hadase662e142018-06-17 13:00:02 +0300191 break;
192 case MLX5_CMD_OP_QUERY_FLOW_TABLE:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300193 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_FLOW_TABLE,
194 MLX5_GET(query_flow_table_in, in,
195 table_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300196 break;
197 case MLX5_CMD_OP_MODIFY_FLOW_TABLE:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300198 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_FLOW_TABLE,
199 MLX5_GET(modify_flow_table_in, in,
200 table_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300201 break;
202 case MLX5_CMD_OP_QUERY_FLOW_GROUP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300203 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_FLOW_GROUP,
204 MLX5_GET(query_flow_group_in, in,
205 group_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300206 break;
207 case MLX5_CMD_OP_QUERY_FLOW_TABLE_ENTRY:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300208 obj_id = get_enc_obj_id(MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY,
209 MLX5_GET(query_fte_in, in,
210 flow_index));
Yishai Hadase662e142018-06-17 13:00:02 +0300211 break;
212 case MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300213 obj_id = get_enc_obj_id(MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY,
214 MLX5_GET(set_fte_in, in, flow_index));
Yishai Hadase662e142018-06-17 13:00:02 +0300215 break;
216 case MLX5_CMD_OP_QUERY_Q_COUNTER:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300217 obj_id = get_enc_obj_id(MLX5_CMD_OP_ALLOC_Q_COUNTER,
218 MLX5_GET(query_q_counter_in, in,
219 counter_set_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300220 break;
221 case MLX5_CMD_OP_QUERY_FLOW_COUNTER:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300222 obj_id = get_enc_obj_id(MLX5_CMD_OP_ALLOC_FLOW_COUNTER,
223 MLX5_GET(query_flow_counter_in, in,
224 flow_counter_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300225 break;
226 case MLX5_CMD_OP_QUERY_MODIFY_HEADER_CONTEXT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300227 obj_id = get_enc_obj_id(MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT,
228 MLX5_GET(general_obj_in_cmd_hdr, in,
229 obj_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300230 break;
231 case MLX5_CMD_OP_QUERY_SCHEDULING_ELEMENT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300232 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_SCHEDULING_ELEMENT,
233 MLX5_GET(query_scheduling_element_in,
234 in, scheduling_element_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300235 break;
236 case MLX5_CMD_OP_MODIFY_SCHEDULING_ELEMENT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300237 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_SCHEDULING_ELEMENT,
238 MLX5_GET(modify_scheduling_element_in,
239 in, scheduling_element_id));
Yishai Hadase662e142018-06-17 13:00:02 +0300240 break;
241 case MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300242 obj_id = get_enc_obj_id(MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT,
243 MLX5_GET(add_vxlan_udp_dport_in, in,
244 vxlan_udp_port));
Yishai Hadase662e142018-06-17 13:00:02 +0300245 break;
246 case MLX5_CMD_OP_QUERY_L2_TABLE_ENTRY:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300247 obj_id = get_enc_obj_id(MLX5_CMD_OP_SET_L2_TABLE_ENTRY,
248 MLX5_GET(query_l2_table_entry_in, in,
249 table_index));
Yishai Hadase662e142018-06-17 13:00:02 +0300250 break;
251 case MLX5_CMD_OP_SET_L2_TABLE_ENTRY:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300252 obj_id = get_enc_obj_id(MLX5_CMD_OP_SET_L2_TABLE_ENTRY,
253 MLX5_GET(set_l2_table_entry_in, in,
254 table_index));
Yishai Hadase662e142018-06-17 13:00:02 +0300255 break;
256 case MLX5_CMD_OP_QUERY_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300257 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
258 MLX5_GET(query_qp_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300259 break;
260 case MLX5_CMD_OP_RST2INIT_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300261 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
262 MLX5_GET(rst2init_qp_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300263 break;
264 case MLX5_CMD_OP_INIT2RTR_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300265 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
266 MLX5_GET(init2rtr_qp_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300267 break;
268 case MLX5_CMD_OP_RTR2RTS_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300269 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
270 MLX5_GET(rtr2rts_qp_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300271 break;
272 case MLX5_CMD_OP_RTS2RTS_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300273 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
274 MLX5_GET(rts2rts_qp_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300275 break;
276 case MLX5_CMD_OP_SQERR2RTS_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300277 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
278 MLX5_GET(sqerr2rts_qp_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300279 break;
280 case MLX5_CMD_OP_2ERR_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300281 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
282 MLX5_GET(qp_2err_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300283 break;
284 case MLX5_CMD_OP_2RST_QP:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300285 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_QP,
286 MLX5_GET(qp_2rst_in, in, qpn));
Yishai Hadase662e142018-06-17 13:00:02 +0300287 break;
288 case MLX5_CMD_OP_QUERY_DCT:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300289 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_DCT,
290 MLX5_GET(query_dct_in, in, dctn));
Yishai Hadase662e142018-06-17 13:00:02 +0300291 break;
292 case MLX5_CMD_OP_QUERY_XRQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300293 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_XRQ,
294 MLX5_GET(query_xrq_in, in, xrqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300295 break;
296 case MLX5_CMD_OP_QUERY_XRC_SRQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300297 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_XRC_SRQ,
298 MLX5_GET(query_xrc_srq_in, in,
299 xrc_srqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300300 break;
301 case MLX5_CMD_OP_ARM_XRC_SRQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300302 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_XRC_SRQ,
303 MLX5_GET(arm_xrc_srq_in, in, xrc_srqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300304 break;
305 case MLX5_CMD_OP_QUERY_SRQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300306 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_SRQ,
307 MLX5_GET(query_srq_in, in, srqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300308 break;
309 case MLX5_CMD_OP_ARM_RQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300310 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_RQ,
311 MLX5_GET(arm_rq_in, in, srq_number));
Yishai Hadase662e142018-06-17 13:00:02 +0300312 break;
313 case MLX5_CMD_OP_DRAIN_DCT:
314 case MLX5_CMD_OP_ARM_DCT_FOR_KEY_VIOLATION:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300315 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_DCT,
316 MLX5_GET(drain_dct_in, in, dctn));
Yishai Hadase662e142018-06-17 13:00:02 +0300317 break;
318 case MLX5_CMD_OP_ARM_XRQ:
Yishai Hadas2351776e2018-10-07 12:06:34 +0300319 obj_id = get_enc_obj_id(MLX5_CMD_OP_CREATE_XRQ,
320 MLX5_GET(arm_xrq_in, in, xrqn));
Yishai Hadase662e142018-06-17 13:00:02 +0300321 break;
322 default:
323 return false;
324 }
325
326 if (obj_id == obj->obj_id)
327 return true;
328
329 return false;
330}
331
Yishai Hadasba1a0572018-09-20 21:39:33 +0300332static void devx_set_umem_valid(const void *in)
333{
334 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
335
336 switch (opcode) {
337 case MLX5_CMD_OP_CREATE_MKEY:
338 MLX5_SET(create_mkey_in, in, mkey_umem_valid, 1);
339 break;
340 case MLX5_CMD_OP_CREATE_CQ:
341 {
342 void *cqc;
343
344 MLX5_SET(create_cq_in, in, cq_umem_valid, 1);
345 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
346 MLX5_SET(cqc, cqc, dbr_umem_valid, 1);
347 break;
348 }
349 case MLX5_CMD_OP_CREATE_QP:
350 {
351 void *qpc;
352
353 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
354 MLX5_SET(qpc, qpc, dbr_umem_valid, 1);
355 MLX5_SET(create_qp_in, in, wq_umem_valid, 1);
356 break;
357 }
358
359 case MLX5_CMD_OP_CREATE_RQ:
360 {
361 void *rqc, *wq;
362
363 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
364 wq = MLX5_ADDR_OF(rqc, rqc, wq);
365 MLX5_SET(wq, wq, dbr_umem_valid, 1);
366 MLX5_SET(wq, wq, wq_umem_valid, 1);
367 break;
368 }
369
370 case MLX5_CMD_OP_CREATE_SQ:
371 {
372 void *sqc, *wq;
373
374 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
375 wq = MLX5_ADDR_OF(sqc, sqc, wq);
376 MLX5_SET(wq, wq, dbr_umem_valid, 1);
377 MLX5_SET(wq, wq, wq_umem_valid, 1);
378 break;
379 }
380
381 case MLX5_CMD_OP_MODIFY_CQ:
382 MLX5_SET(modify_cq_in, in, cq_umem_valid, 1);
383 break;
384
385 case MLX5_CMD_OP_CREATE_RMP:
386 {
387 void *rmpc, *wq;
388
389 rmpc = MLX5_ADDR_OF(create_rmp_in, in, ctx);
390 wq = MLX5_ADDR_OF(rmpc, rmpc, wq);
391 MLX5_SET(wq, wq, dbr_umem_valid, 1);
392 MLX5_SET(wq, wq, wq_umem_valid, 1);
393 break;
394 }
395
396 case MLX5_CMD_OP_CREATE_XRQ:
397 {
398 void *xrqc, *wq;
399
400 xrqc = MLX5_ADDR_OF(create_xrq_in, in, xrq_context);
401 wq = MLX5_ADDR_OF(xrqc, xrqc, wq);
402 MLX5_SET(wq, wq, dbr_umem_valid, 1);
403 MLX5_SET(wq, wq, wq_umem_valid, 1);
404 break;
405 }
406
407 case MLX5_CMD_OP_CREATE_XRC_SRQ:
408 {
409 void *xrc_srqc;
410
411 MLX5_SET(create_xrc_srq_in, in, xrc_srq_umem_valid, 1);
412 xrc_srqc = MLX5_ADDR_OF(create_xrc_srq_in, in,
413 xrc_srq_context_entry);
414 MLX5_SET(xrc_srqc, xrc_srqc, dbr_umem_valid, 1);
415 break;
416 }
417
418 default:
419 return;
420 }
421}
422
Yishai Hadas2351776e2018-10-07 12:06:34 +0300423static bool devx_is_obj_create_cmd(const void *in, u16 *opcode)
Yishai Hadas7efce362018-06-17 13:00:01 +0300424{
Yishai Hadas2351776e2018-10-07 12:06:34 +0300425 *opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
Yishai Hadas7efce362018-06-17 13:00:01 +0300426
Yishai Hadas2351776e2018-10-07 12:06:34 +0300427 switch (*opcode) {
Yishai Hadas7efce362018-06-17 13:00:01 +0300428 case MLX5_CMD_OP_CREATE_GENERAL_OBJECT:
429 case MLX5_CMD_OP_CREATE_MKEY:
430 case MLX5_CMD_OP_CREATE_CQ:
431 case MLX5_CMD_OP_ALLOC_PD:
432 case MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN:
433 case MLX5_CMD_OP_CREATE_RMP:
434 case MLX5_CMD_OP_CREATE_SQ:
435 case MLX5_CMD_OP_CREATE_RQ:
436 case MLX5_CMD_OP_CREATE_RQT:
437 case MLX5_CMD_OP_CREATE_TIR:
438 case MLX5_CMD_OP_CREATE_TIS:
439 case MLX5_CMD_OP_ALLOC_Q_COUNTER:
440 case MLX5_CMD_OP_CREATE_FLOW_TABLE:
441 case MLX5_CMD_OP_CREATE_FLOW_GROUP:
442 case MLX5_CMD_OP_ALLOC_FLOW_COUNTER:
Mark Bloch60786f02018-08-28 14:18:46 +0300443 case MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT:
Yishai Hadas7efce362018-06-17 13:00:01 +0300444 case MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT:
445 case MLX5_CMD_OP_CREATE_SCHEDULING_ELEMENT:
446 case MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT:
447 case MLX5_CMD_OP_SET_L2_TABLE_ENTRY:
448 case MLX5_CMD_OP_CREATE_QP:
449 case MLX5_CMD_OP_CREATE_SRQ:
450 case MLX5_CMD_OP_CREATE_XRC_SRQ:
451 case MLX5_CMD_OP_CREATE_DCT:
452 case MLX5_CMD_OP_CREATE_XRQ:
453 case MLX5_CMD_OP_ATTACH_TO_MCG:
454 case MLX5_CMD_OP_ALLOC_XRCD:
455 return true;
456 case MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY:
457 {
458 u16 op_mod = MLX5_GET(set_fte_in, in, op_mod);
459 if (op_mod == 0)
460 return true;
461 return false;
462 }
463 default:
464 return false;
465 }
466}
467
Yishai Hadase662e142018-06-17 13:00:02 +0300468static bool devx_is_obj_modify_cmd(const void *in)
469{
470 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
471
472 switch (opcode) {
473 case MLX5_CMD_OP_MODIFY_GENERAL_OBJECT:
474 case MLX5_CMD_OP_MODIFY_CQ:
475 case MLX5_CMD_OP_MODIFY_RMP:
476 case MLX5_CMD_OP_MODIFY_SQ:
477 case MLX5_CMD_OP_MODIFY_RQ:
478 case MLX5_CMD_OP_MODIFY_RQT:
479 case MLX5_CMD_OP_MODIFY_TIR:
480 case MLX5_CMD_OP_MODIFY_TIS:
481 case MLX5_CMD_OP_MODIFY_FLOW_TABLE:
482 case MLX5_CMD_OP_MODIFY_SCHEDULING_ELEMENT:
483 case MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT:
484 case MLX5_CMD_OP_SET_L2_TABLE_ENTRY:
485 case MLX5_CMD_OP_RST2INIT_QP:
486 case MLX5_CMD_OP_INIT2RTR_QP:
487 case MLX5_CMD_OP_RTR2RTS_QP:
488 case MLX5_CMD_OP_RTS2RTS_QP:
489 case MLX5_CMD_OP_SQERR2RTS_QP:
490 case MLX5_CMD_OP_2ERR_QP:
491 case MLX5_CMD_OP_2RST_QP:
492 case MLX5_CMD_OP_ARM_XRC_SRQ:
493 case MLX5_CMD_OP_ARM_RQ:
494 case MLX5_CMD_OP_DRAIN_DCT:
495 case MLX5_CMD_OP_ARM_DCT_FOR_KEY_VIOLATION:
496 case MLX5_CMD_OP_ARM_XRQ:
497 return true;
498 case MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY:
499 {
500 u16 op_mod = MLX5_GET(set_fte_in, in, op_mod);
501
502 if (op_mod == 1)
503 return true;
504 return false;
505 }
506 default:
507 return false;
508 }
509}
510
511static bool devx_is_obj_query_cmd(const void *in)
512{
513 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
514
515 switch (opcode) {
516 case MLX5_CMD_OP_QUERY_GENERAL_OBJECT:
517 case MLX5_CMD_OP_QUERY_MKEY:
518 case MLX5_CMD_OP_QUERY_CQ:
519 case MLX5_CMD_OP_QUERY_RMP:
520 case MLX5_CMD_OP_QUERY_SQ:
521 case MLX5_CMD_OP_QUERY_RQ:
522 case MLX5_CMD_OP_QUERY_RQT:
523 case MLX5_CMD_OP_QUERY_TIR:
524 case MLX5_CMD_OP_QUERY_TIS:
525 case MLX5_CMD_OP_QUERY_Q_COUNTER:
526 case MLX5_CMD_OP_QUERY_FLOW_TABLE:
527 case MLX5_CMD_OP_QUERY_FLOW_GROUP:
528 case MLX5_CMD_OP_QUERY_FLOW_TABLE_ENTRY:
529 case MLX5_CMD_OP_QUERY_FLOW_COUNTER:
530 case MLX5_CMD_OP_QUERY_MODIFY_HEADER_CONTEXT:
531 case MLX5_CMD_OP_QUERY_SCHEDULING_ELEMENT:
532 case MLX5_CMD_OP_QUERY_L2_TABLE_ENTRY:
533 case MLX5_CMD_OP_QUERY_QP:
534 case MLX5_CMD_OP_QUERY_SRQ:
535 case MLX5_CMD_OP_QUERY_XRC_SRQ:
536 case MLX5_CMD_OP_QUERY_DCT:
537 case MLX5_CMD_OP_QUERY_XRQ:
538 return true;
539 default:
540 return false;
541 }
542}
543
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300544static bool devx_is_whitelist_cmd(void *in)
545{
546 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
547
548 switch (opcode) {
549 case MLX5_CMD_OP_QUERY_HCA_CAP:
550 case MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT:
551 return true;
552 default:
553 return false;
554 }
555}
556
557static int devx_get_uid(struct mlx5_ib_ucontext *c, void *cmd_in)
558{
559 if (devx_is_whitelist_cmd(cmd_in)) {
560 struct mlx5_ib_dev *dev;
561
562 if (c->devx_uid)
563 return c->devx_uid;
564
565 dev = to_mdev(c->ibucontext.device);
566 if (dev->devx_whitelist_uid)
567 return dev->devx_whitelist_uid;
568
569 return -EOPNOTSUPP;
570 }
571
572 if (!c->devx_uid)
573 return -EINVAL;
574
575 if (!capable(CAP_NET_RAW))
576 return -EPERM;
577
578 return c->devx_uid;
579}
Yishai Hadase662e142018-06-17 13:00:02 +0300580static bool devx_is_general_cmd(void *in)
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300581{
582 u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, in, opcode);
583
584 switch (opcode) {
585 case MLX5_CMD_OP_QUERY_HCA_CAP:
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300586 case MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT:
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300587 case MLX5_CMD_OP_QUERY_VPORT_STATE:
588 case MLX5_CMD_OP_QUERY_ADAPTER:
589 case MLX5_CMD_OP_QUERY_ISSI:
590 case MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT:
591 case MLX5_CMD_OP_QUERY_ROCE_ADDRESS:
592 case MLX5_CMD_OP_QUERY_VNIC_ENV:
593 case MLX5_CMD_OP_QUERY_VPORT_COUNTER:
594 case MLX5_CMD_OP_GET_DROPPED_PACKET_LOG:
595 case MLX5_CMD_OP_NOP:
596 case MLX5_CMD_OP_QUERY_CONG_STATUS:
597 case MLX5_CMD_OP_QUERY_CONG_PARAMS:
598 case MLX5_CMD_OP_QUERY_CONG_STATISTICS:
599 return true;
600 default:
601 return false;
602 }
603}
604
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600605static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_QUERY_EQN)(
606 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadasf6fe01b2018-06-17 13:00:05 +0300607{
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600608 struct mlx5_ib_ucontext *c;
609 struct mlx5_ib_dev *dev;
Yishai Hadasf6fe01b2018-06-17 13:00:05 +0300610 int user_vector;
611 int dev_eqn;
612 unsigned int irqn;
613 int err;
614
615 if (uverbs_copy_from(&user_vector, attrs,
616 MLX5_IB_ATTR_DEVX_QUERY_EQN_USER_VEC))
617 return -EFAULT;
618
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600619 c = devx_ufile2uctx(file);
620 if (IS_ERR(c))
621 return PTR_ERR(c);
622 dev = to_mdev(c->ibucontext.device);
623
Yishai Hadasf6fe01b2018-06-17 13:00:05 +0300624 err = mlx5_vector2eqn(dev->mdev, user_vector, &dev_eqn, &irqn);
625 if (err < 0)
626 return err;
627
628 if (uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_QUERY_EQN_DEV_EQN,
629 &dev_eqn, sizeof(dev_eqn)))
630 return -EFAULT;
631
632 return 0;
633}
634
Yishai Hadas7c043e92018-06-17 13:00:03 +0300635/*
636 *Security note:
637 * The hardware protection mechanism works like this: Each device object that
638 * is subject to UAR doorbells (QP/SQ/CQ) gets a UAR ID (called uar_page in
639 * the device specification manual) upon its creation. Then upon doorbell,
640 * hardware fetches the object context for which the doorbell was rang, and
641 * validates that the UAR through which the DB was rang matches the UAR ID
642 * of the object.
643 * If no match the doorbell is silently ignored by the hardware. Of course,
644 * the user cannot ring a doorbell on a UAR that was not mapped to it.
645 * Now in devx, as the devx kernel does not manipulate the QP/SQ/CQ command
646 * mailboxes (except tagging them with UID), we expose to the user its UAR
647 * ID, so it can embed it in these objects in the expected specification
648 * format. So the only thing the user can do is hurt itself by creating a
649 * QP/SQ/CQ with a UAR ID other than his, and then in this case other users
650 * may ring a doorbell on its objects.
651 * The consequence of that will be that another user can schedule a QP/SQ
652 * of the buggy user for execution (just insert it to the hardware schedule
653 * queue or arm its CQ for event generation), no further harm is expected.
654 */
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600655static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_QUERY_UAR)(
656 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadas7c043e92018-06-17 13:00:03 +0300657{
Jason Gunthorpe22fa27f2018-07-10 13:43:06 -0600658 struct mlx5_ib_ucontext *c;
659 struct mlx5_ib_dev *dev;
Yishai Hadas7c043e92018-06-17 13:00:03 +0300660 u32 user_idx;
661 s32 dev_idx;
662
Jason Gunthorpe22fa27f2018-07-10 13:43:06 -0600663 c = devx_ufile2uctx(file);
664 if (IS_ERR(c))
665 return PTR_ERR(c);
666 dev = to_mdev(c->ibucontext.device);
667
Yishai Hadas7c043e92018-06-17 13:00:03 +0300668 if (uverbs_copy_from(&user_idx, attrs,
669 MLX5_IB_ATTR_DEVX_QUERY_UAR_USER_IDX))
670 return -EFAULT;
671
Jason Gunthorpe22fa27f2018-07-10 13:43:06 -0600672 dev_idx = bfregn_to_uar_index(dev, &c->bfregi, user_idx, true);
Yishai Hadas7c043e92018-06-17 13:00:03 +0300673 if (dev_idx < 0)
674 return dev_idx;
675
676 if (uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_QUERY_UAR_DEV_IDX,
677 &dev_idx, sizeof(dev_idx)))
678 return -EFAULT;
679
680 return 0;
681}
682
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600683static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OTHER)(
684 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300685{
Jason Gunthorpe22fa27f2018-07-10 13:43:06 -0600686 struct mlx5_ib_ucontext *c;
687 struct mlx5_ib_dev *dev;
Yishai Hadas7efce362018-06-17 13:00:01 +0300688 void *cmd_in = uverbs_attr_get_alloced_ptr(
689 attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_IN);
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300690 int cmd_out_len = uverbs_attr_get_len(attrs,
691 MLX5_IB_ATTR_DEVX_OTHER_CMD_OUT);
692 void *cmd_out;
693 int err;
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300694 int uid;
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300695
Jason Gunthorpe22fa27f2018-07-10 13:43:06 -0600696 c = devx_ufile2uctx(file);
697 if (IS_ERR(c))
698 return PTR_ERR(c);
699 dev = to_mdev(c->ibucontext.device);
700
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300701 uid = devx_get_uid(c, cmd_in);
702 if (uid < 0)
703 return uid;
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300704
705 /* Only white list of some general HCA commands are allowed for this method. */
706 if (!devx_is_general_cmd(cmd_in))
707 return -EINVAL;
708
Jason Gunthorpeb61815e2018-08-09 20:14:41 -0600709 cmd_out = uverbs_zalloc(attrs, cmd_out_len);
710 if (IS_ERR(cmd_out))
711 return PTR_ERR(cmd_out);
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300712
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300713 MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid);
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300714 err = mlx5_cmd_exec(dev->mdev, cmd_in,
715 uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_IN),
716 cmd_out, cmd_out_len);
717 if (err)
Jason Gunthorpeb61815e2018-08-09 20:14:41 -0600718 return err;
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300719
Jason Gunthorpeb61815e2018-08-09 20:14:41 -0600720 return uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_OUT, cmd_out,
721 cmd_out_len);
Yishai Hadas8aa8c952018-06-17 13:00:00 +0300722}
723
Yishai Hadas7efce362018-06-17 13:00:01 +0300724static void devx_obj_build_destroy_cmd(void *in, void *out, void *din,
725 u32 *dinlen,
726 u32 *obj_id)
727{
728 u16 obj_type = MLX5_GET(general_obj_in_cmd_hdr, in, obj_type);
729 u16 uid = MLX5_GET(general_obj_in_cmd_hdr, in, uid);
730
731 *obj_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
732 *dinlen = MLX5_ST_SZ_BYTES(general_obj_in_cmd_hdr);
733
734 MLX5_SET(general_obj_in_cmd_hdr, din, obj_id, *obj_id);
735 MLX5_SET(general_obj_in_cmd_hdr, din, uid, uid);
736
737 switch (MLX5_GET(general_obj_in_cmd_hdr, in, opcode)) {
738 case MLX5_CMD_OP_CREATE_GENERAL_OBJECT:
739 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
740 MLX5_SET(general_obj_in_cmd_hdr, din, obj_type, obj_type);
741 break;
742
743 case MLX5_CMD_OP_CREATE_MKEY:
744 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_MKEY);
745 break;
746 case MLX5_CMD_OP_CREATE_CQ:
747 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_CQ);
748 break;
749 case MLX5_CMD_OP_ALLOC_PD:
750 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DEALLOC_PD);
751 break;
752 case MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN:
753 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
754 MLX5_CMD_OP_DEALLOC_TRANSPORT_DOMAIN);
755 break;
756 case MLX5_CMD_OP_CREATE_RMP:
757 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_RMP);
758 break;
759 case MLX5_CMD_OP_CREATE_SQ:
760 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_SQ);
761 break;
762 case MLX5_CMD_OP_CREATE_RQ:
763 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_RQ);
764 break;
765 case MLX5_CMD_OP_CREATE_RQT:
766 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_RQT);
767 break;
768 case MLX5_CMD_OP_CREATE_TIR:
769 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_TIR);
770 break;
771 case MLX5_CMD_OP_CREATE_TIS:
772 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_TIS);
773 break;
774 case MLX5_CMD_OP_ALLOC_Q_COUNTER:
775 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
776 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
777 break;
778 case MLX5_CMD_OP_CREATE_FLOW_TABLE:
779 *dinlen = MLX5_ST_SZ_BYTES(destroy_flow_table_in);
780 *obj_id = MLX5_GET(create_flow_table_out, out, table_id);
781 MLX5_SET(destroy_flow_table_in, din, other_vport,
782 MLX5_GET(create_flow_table_in, in, other_vport));
783 MLX5_SET(destroy_flow_table_in, din, vport_number,
784 MLX5_GET(create_flow_table_in, in, vport_number));
785 MLX5_SET(destroy_flow_table_in, din, table_type,
786 MLX5_GET(create_flow_table_in, in, table_type));
787 MLX5_SET(destroy_flow_table_in, din, table_id, *obj_id);
788 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
789 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
790 break;
791 case MLX5_CMD_OP_CREATE_FLOW_GROUP:
792 *dinlen = MLX5_ST_SZ_BYTES(destroy_flow_group_in);
793 *obj_id = MLX5_GET(create_flow_group_out, out, group_id);
794 MLX5_SET(destroy_flow_group_in, din, other_vport,
795 MLX5_GET(create_flow_group_in, in, other_vport));
796 MLX5_SET(destroy_flow_group_in, din, vport_number,
797 MLX5_GET(create_flow_group_in, in, vport_number));
798 MLX5_SET(destroy_flow_group_in, din, table_type,
799 MLX5_GET(create_flow_group_in, in, table_type));
800 MLX5_SET(destroy_flow_group_in, din, table_id,
801 MLX5_GET(create_flow_group_in, in, table_id));
802 MLX5_SET(destroy_flow_group_in, din, group_id, *obj_id);
803 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
804 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
805 break;
806 case MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY:
807 *dinlen = MLX5_ST_SZ_BYTES(delete_fte_in);
808 *obj_id = MLX5_GET(set_fte_in, in, flow_index);
809 MLX5_SET(delete_fte_in, din, other_vport,
810 MLX5_GET(set_fte_in, in, other_vport));
811 MLX5_SET(delete_fte_in, din, vport_number,
812 MLX5_GET(set_fte_in, in, vport_number));
813 MLX5_SET(delete_fte_in, din, table_type,
814 MLX5_GET(set_fte_in, in, table_type));
815 MLX5_SET(delete_fte_in, din, table_id,
816 MLX5_GET(set_fte_in, in, table_id));
817 MLX5_SET(delete_fte_in, din, flow_index, *obj_id);
818 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
819 MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
820 break;
821 case MLX5_CMD_OP_ALLOC_FLOW_COUNTER:
822 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
823 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
824 break;
Mark Bloch60786f02018-08-28 14:18:46 +0300825 case MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT:
Yishai Hadas7efce362018-06-17 13:00:01 +0300826 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
Mark Bloch60786f02018-08-28 14:18:46 +0300827 MLX5_CMD_OP_DEALLOC_PACKET_REFORMAT_CONTEXT);
Yishai Hadas7efce362018-06-17 13:00:01 +0300828 break;
829 case MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT:
830 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
831 MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
832 break;
833 case MLX5_CMD_OP_CREATE_SCHEDULING_ELEMENT:
834 *dinlen = MLX5_ST_SZ_BYTES(destroy_scheduling_element_in);
835 *obj_id = MLX5_GET(create_scheduling_element_out, out,
836 scheduling_element_id);
837 MLX5_SET(destroy_scheduling_element_in, din,
838 scheduling_hierarchy,
839 MLX5_GET(create_scheduling_element_in, in,
840 scheduling_hierarchy));
841 MLX5_SET(destroy_scheduling_element_in, din,
842 scheduling_element_id, *obj_id);
843 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
844 MLX5_CMD_OP_DESTROY_SCHEDULING_ELEMENT);
845 break;
846 case MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT:
847 *dinlen = MLX5_ST_SZ_BYTES(delete_vxlan_udp_dport_in);
848 *obj_id = MLX5_GET(add_vxlan_udp_dport_in, in, vxlan_udp_port);
849 MLX5_SET(delete_vxlan_udp_dport_in, din, vxlan_udp_port, *obj_id);
850 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
851 MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
852 break;
853 case MLX5_CMD_OP_SET_L2_TABLE_ENTRY:
854 *dinlen = MLX5_ST_SZ_BYTES(delete_l2_table_entry_in);
855 *obj_id = MLX5_GET(set_l2_table_entry_in, in, table_index);
856 MLX5_SET(delete_l2_table_entry_in, din, table_index, *obj_id);
857 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
858 MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY);
859 break;
860 case MLX5_CMD_OP_CREATE_QP:
861 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_QP);
862 break;
863 case MLX5_CMD_OP_CREATE_SRQ:
864 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_SRQ);
865 break;
866 case MLX5_CMD_OP_CREATE_XRC_SRQ:
867 MLX5_SET(general_obj_in_cmd_hdr, din, opcode,
868 MLX5_CMD_OP_DESTROY_XRC_SRQ);
869 break;
870 case MLX5_CMD_OP_CREATE_DCT:
871 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_DCT);
872 break;
873 case MLX5_CMD_OP_CREATE_XRQ:
874 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DESTROY_XRQ);
875 break;
876 case MLX5_CMD_OP_ATTACH_TO_MCG:
877 *dinlen = MLX5_ST_SZ_BYTES(detach_from_mcg_in);
878 MLX5_SET(detach_from_mcg_in, din, qpn,
879 MLX5_GET(attach_to_mcg_in, in, qpn));
880 memcpy(MLX5_ADDR_OF(detach_from_mcg_in, din, multicast_gid),
881 MLX5_ADDR_OF(attach_to_mcg_in, in, multicast_gid),
882 MLX5_FLD_SZ_BYTES(attach_to_mcg_in, multicast_gid));
883 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DETACH_FROM_MCG);
884 break;
885 case MLX5_CMD_OP_ALLOC_XRCD:
886 MLX5_SET(general_obj_in_cmd_hdr, din, opcode, MLX5_CMD_OP_DEALLOC_XRCD);
887 break;
888 default:
889 /* The entry must match to one of the devx_is_obj_create_cmd */
890 WARN_ON(true);
891 break;
892 }
893}
894
895static int devx_obj_cleanup(struct ib_uobject *uobject,
896 enum rdma_remove_reason why)
897{
898 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
899 struct devx_obj *obj = uobject->object;
900 int ret;
901
902 ret = mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, out, sizeof(out));
Yishai Hadas1c774832018-06-20 17:11:39 +0300903 if (ib_is_destroy_retryable(ret, why, uobject))
Yishai Hadas7efce362018-06-17 13:00:01 +0300904 return ret;
905
906 kfree(obj);
907 return ret;
908}
909
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600910static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)(
911 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadas7efce362018-06-17 13:00:01 +0300912{
Yishai Hadas7efce362018-06-17 13:00:01 +0300913 void *cmd_in = uverbs_attr_get_alloced_ptr(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_IN);
914 int cmd_out_len = uverbs_attr_get_len(attrs,
915 MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT);
916 void *cmd_out;
Jason Gunthorpec36ee462018-07-10 20:55:22 -0600917 struct ib_uobject *uobj = uverbs_attr_get_uobject(
918 attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_HANDLE);
919 struct mlx5_ib_ucontext *c = to_mucontext(uobj->context);
920 struct mlx5_ib_dev *dev = to_mdev(c->ibucontext.device);
Yishai Hadase8ef0902018-09-25 12:11:12 +0300921 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
Yishai Hadas7efce362018-06-17 13:00:01 +0300922 struct devx_obj *obj;
923 int err;
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300924 int uid;
Yishai Hadas2351776e2018-10-07 12:06:34 +0300925 u32 obj_id;
926 u16 opcode;
Yishai Hadas7efce362018-06-17 13:00:01 +0300927
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300928 uid = devx_get_uid(c, cmd_in);
929 if (uid < 0)
930 return uid;
Yishai Hadas7efce362018-06-17 13:00:01 +0300931
Yishai Hadas2351776e2018-10-07 12:06:34 +0300932 if (!devx_is_obj_create_cmd(cmd_in, &opcode))
Yishai Hadas7efce362018-06-17 13:00:01 +0300933 return -EINVAL;
934
Jason Gunthorpeb61815e2018-08-09 20:14:41 -0600935 cmd_out = uverbs_zalloc(attrs, cmd_out_len);
936 if (IS_ERR(cmd_out))
937 return PTR_ERR(cmd_out);
938
Yishai Hadas7efce362018-06-17 13:00:01 +0300939 obj = kzalloc(sizeof(struct devx_obj), GFP_KERNEL);
940 if (!obj)
941 return -ENOMEM;
942
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300943 MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid);
Yishai Hadasba1a0572018-09-20 21:39:33 +0300944 devx_set_umem_valid(cmd_in);
945
Yishai Hadas7efce362018-06-17 13:00:01 +0300946 err = mlx5_cmd_exec(dev->mdev, cmd_in,
947 uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_IN),
948 cmd_out, cmd_out_len);
949 if (err)
Jason Gunthorpeb61815e2018-08-09 20:14:41 -0600950 goto obj_free;
Yishai Hadas7efce362018-06-17 13:00:01 +0300951
Yishai Hadas7efce362018-06-17 13:00:01 +0300952 uobj->object = obj;
953 obj->mdev = dev->mdev;
Yishai Hadas2351776e2018-10-07 12:06:34 +0300954 devx_obj_build_destroy_cmd(cmd_in, cmd_out, obj->dinbox, &obj->dinlen,
955 &obj_id);
Yishai Hadas7efce362018-06-17 13:00:01 +0300956 WARN_ON(obj->dinlen > MLX5_MAX_DESTROY_INBOX_SIZE_DW * sizeof(u32));
957
958 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT, cmd_out, cmd_out_len);
959 if (err)
Yishai Hadase8ef0902018-09-25 12:11:12 +0300960 goto obj_destroy;
Yishai Hadas7efce362018-06-17 13:00:01 +0300961
Yishai Hadas2351776e2018-10-07 12:06:34 +0300962 obj->obj_id = get_enc_obj_id(opcode, obj_id);
Yishai Hadas7efce362018-06-17 13:00:01 +0300963 return 0;
964
Yishai Hadase8ef0902018-09-25 12:11:12 +0300965obj_destroy:
966 mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, out, sizeof(out));
Yishai Hadas7efce362018-06-17 13:00:01 +0300967obj_free:
968 kfree(obj);
969 return err;
970}
971
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600972static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_MODIFY)(
973 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadase662e142018-06-17 13:00:02 +0300974{
Yishai Hadase662e142018-06-17 13:00:02 +0300975 void *cmd_in = uverbs_attr_get_alloced_ptr(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN);
976 int cmd_out_len = uverbs_attr_get_len(attrs,
977 MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_OUT);
978 struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs,
979 MLX5_IB_ATTR_DEVX_OBJ_MODIFY_HANDLE);
Jason Gunthorpec36ee462018-07-10 20:55:22 -0600980 struct mlx5_ib_ucontext *c = to_mucontext(uobj->context);
981 struct devx_obj *obj = uobj->object;
Yishai Hadase662e142018-06-17 13:00:02 +0300982 void *cmd_out;
983 int err;
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300984 int uid;
Yishai Hadase662e142018-06-17 13:00:02 +0300985
Yishai Hadas7e1335a2018-09-20 21:45:20 +0300986 uid = devx_get_uid(c, cmd_in);
987 if (uid < 0)
988 return uid;
Yishai Hadase662e142018-06-17 13:00:02 +0300989
990 if (!devx_is_obj_modify_cmd(cmd_in))
991 return -EINVAL;
992
Jason Gunthorpec36ee462018-07-10 20:55:22 -0600993 if (!devx_is_valid_obj_id(obj, cmd_in))
Yishai Hadase662e142018-06-17 13:00:02 +0300994 return -EINVAL;
995
Jason Gunthorpeb61815e2018-08-09 20:14:41 -0600996 cmd_out = uverbs_zalloc(attrs, cmd_out_len);
997 if (IS_ERR(cmd_out))
998 return PTR_ERR(cmd_out);
Yishai Hadase662e142018-06-17 13:00:02 +0300999
Yishai Hadas7e1335a2018-09-20 21:45:20 +03001000 MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid);
Yishai Hadasba1a0572018-09-20 21:39:33 +03001001 devx_set_umem_valid(cmd_in);
1002
Jason Gunthorpec36ee462018-07-10 20:55:22 -06001003 err = mlx5_cmd_exec(obj->mdev, cmd_in,
Yishai Hadase662e142018-06-17 13:00:02 +03001004 uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN),
1005 cmd_out, cmd_out_len);
1006 if (err)
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001007 return err;
Yishai Hadase662e142018-06-17 13:00:02 +03001008
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001009 return uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_OUT,
1010 cmd_out, cmd_out_len);
Yishai Hadase662e142018-06-17 13:00:02 +03001011}
1012
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -06001013static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)(
1014 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadase662e142018-06-17 13:00:02 +03001015{
Yishai Hadase662e142018-06-17 13:00:02 +03001016 void *cmd_in = uverbs_attr_get_alloced_ptr(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN);
1017 int cmd_out_len = uverbs_attr_get_len(attrs,
1018 MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_OUT);
1019 struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs,
1020 MLX5_IB_ATTR_DEVX_OBJ_QUERY_HANDLE);
Jason Gunthorpec36ee462018-07-10 20:55:22 -06001021 struct mlx5_ib_ucontext *c = to_mucontext(uobj->context);
1022 struct devx_obj *obj = uobj->object;
Yishai Hadase662e142018-06-17 13:00:02 +03001023 void *cmd_out;
1024 int err;
Yishai Hadas7e1335a2018-09-20 21:45:20 +03001025 int uid;
Yishai Hadase662e142018-06-17 13:00:02 +03001026
Yishai Hadas7e1335a2018-09-20 21:45:20 +03001027 uid = devx_get_uid(c, cmd_in);
1028 if (uid < 0)
1029 return uid;
Yishai Hadase662e142018-06-17 13:00:02 +03001030
1031 if (!devx_is_obj_query_cmd(cmd_in))
1032 return -EINVAL;
1033
Jason Gunthorpec36ee462018-07-10 20:55:22 -06001034 if (!devx_is_valid_obj_id(obj, cmd_in))
Yishai Hadase662e142018-06-17 13:00:02 +03001035 return -EINVAL;
1036
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001037 cmd_out = uverbs_zalloc(attrs, cmd_out_len);
1038 if (IS_ERR(cmd_out))
1039 return PTR_ERR(cmd_out);
Yishai Hadase662e142018-06-17 13:00:02 +03001040
Yishai Hadas7e1335a2018-09-20 21:45:20 +03001041 MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, uid);
Jason Gunthorpec36ee462018-07-10 20:55:22 -06001042 err = mlx5_cmd_exec(obj->mdev, cmd_in,
Yishai Hadase662e142018-06-17 13:00:02 +03001043 uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN),
1044 cmd_out, cmd_out_len);
1045 if (err)
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001046 return err;
Yishai Hadase662e142018-06-17 13:00:02 +03001047
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001048 return uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_OUT,
1049 cmd_out, cmd_out_len);
Yishai Hadase662e142018-06-17 13:00:02 +03001050}
1051
Yishai Hadasaeae9452018-06-17 13:00:04 +03001052static int devx_umem_get(struct mlx5_ib_dev *dev, struct ib_ucontext *ucontext,
1053 struct uverbs_attr_bundle *attrs,
1054 struct devx_umem *obj)
1055{
1056 u64 addr;
1057 size_t size;
Jason Gunthorpebccd0622018-07-26 16:37:14 -06001058 u32 access;
Yishai Hadasaeae9452018-06-17 13:00:04 +03001059 int npages;
1060 int err;
1061 u32 page_mask;
1062
1063 if (uverbs_copy_from(&addr, attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_ADDR) ||
Jason Gunthorpebccd0622018-07-26 16:37:14 -06001064 uverbs_copy_from(&size, attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_LEN))
Yishai Hadasaeae9452018-06-17 13:00:04 +03001065 return -EFAULT;
1066
Jason Gunthorpebccd0622018-07-26 16:37:14 -06001067 err = uverbs_get_flags32(&access, attrs,
1068 MLX5_IB_ATTR_DEVX_UMEM_REG_ACCESS,
Yishai Hadas47f07f02018-12-05 15:50:21 +02001069 IB_ACCESS_LOCAL_WRITE |
1070 IB_ACCESS_REMOTE_WRITE |
1071 IB_ACCESS_REMOTE_READ);
Jason Gunthorpebccd0622018-07-26 16:37:14 -06001072 if (err)
1073 return err;
1074
Yishai Hadasaeae9452018-06-17 13:00:04 +03001075 err = ib_check_mr_access(access);
1076 if (err)
1077 return err;
1078
1079 obj->umem = ib_umem_get(ucontext, addr, size, access, 0);
1080 if (IS_ERR(obj->umem))
1081 return PTR_ERR(obj->umem);
1082
1083 mlx5_ib_cont_pages(obj->umem, obj->umem->address,
1084 MLX5_MKEY_PAGE_SHIFT_MASK, &npages,
1085 &obj->page_shift, &obj->ncont, NULL);
1086
1087 if (!npages) {
1088 ib_umem_release(obj->umem);
1089 return -EINVAL;
1090 }
1091
1092 page_mask = (1 << obj->page_shift) - 1;
1093 obj->page_offset = obj->umem->address & page_mask;
1094
1095 return 0;
1096}
1097
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001098static int devx_umem_reg_cmd_alloc(struct uverbs_attr_bundle *attrs,
1099 struct devx_umem *obj,
Yishai Hadasaeae9452018-06-17 13:00:04 +03001100 struct devx_umem_reg_cmd *cmd)
1101{
1102 cmd->inlen = MLX5_ST_SZ_BYTES(create_umem_in) +
1103 (MLX5_ST_SZ_BYTES(mtt) * obj->ncont);
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001104 cmd->in = uverbs_zalloc(attrs, cmd->inlen);
1105 return PTR_ERR_OR_ZERO(cmd->in);
Yishai Hadasaeae9452018-06-17 13:00:04 +03001106}
1107
1108static void devx_umem_reg_cmd_build(struct mlx5_ib_dev *dev,
1109 struct devx_umem *obj,
1110 struct devx_umem_reg_cmd *cmd)
1111{
1112 void *umem;
1113 __be64 *mtt;
1114
1115 umem = MLX5_ADDR_OF(create_umem_in, cmd->in, umem);
1116 mtt = (__be64 *)MLX5_ADDR_OF(umem, umem, mtt);
1117
1118 MLX5_SET(general_obj_in_cmd_hdr, cmd->in, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
1119 MLX5_SET(general_obj_in_cmd_hdr, cmd->in, obj_type, MLX5_OBJ_TYPE_UMEM);
1120 MLX5_SET64(umem, umem, num_of_mtt, obj->ncont);
1121 MLX5_SET(umem, umem, log_page_size, obj->page_shift -
1122 MLX5_ADAPTER_PAGE_SHIFT);
1123 MLX5_SET(umem, umem, page_offset, obj->page_offset);
1124 mlx5_ib_populate_pas(dev, obj->umem, obj->page_shift, mtt,
1125 (obj->umem->writable ? MLX5_IB_MTT_WRITE : 0) |
1126 MLX5_IB_MTT_READ);
1127}
1128
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -06001129static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)(
1130 struct ib_uverbs_file *file, struct uverbs_attr_bundle *attrs)
Yishai Hadasaeae9452018-06-17 13:00:04 +03001131{
Yishai Hadasaeae9452018-06-17 13:00:04 +03001132 struct devx_umem_reg_cmd cmd;
1133 struct devx_umem *obj;
Jason Gunthorpec36ee462018-07-10 20:55:22 -06001134 struct ib_uobject *uobj = uverbs_attr_get_uobject(
1135 attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_HANDLE);
Yishai Hadasaeae9452018-06-17 13:00:04 +03001136 u32 obj_id;
Jason Gunthorpec36ee462018-07-10 20:55:22 -06001137 struct mlx5_ib_ucontext *c = to_mucontext(uobj->context);
1138 struct mlx5_ib_dev *dev = to_mdev(c->ibucontext.device);
Yishai Hadasaeae9452018-06-17 13:00:04 +03001139 int err;
1140
1141 if (!c->devx_uid)
Yishai Hadas7e1335a2018-09-20 21:45:20 +03001142 return -EINVAL;
1143
1144 if (!capable(CAP_NET_RAW))
Yishai Hadasaeae9452018-06-17 13:00:04 +03001145 return -EPERM;
1146
Yishai Hadasaeae9452018-06-17 13:00:04 +03001147 obj = kzalloc(sizeof(struct devx_umem), GFP_KERNEL);
1148 if (!obj)
1149 return -ENOMEM;
1150
1151 err = devx_umem_get(dev, &c->ibucontext, attrs, obj);
1152 if (err)
1153 goto err_obj_free;
1154
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001155 err = devx_umem_reg_cmd_alloc(attrs, obj, &cmd);
Yishai Hadasaeae9452018-06-17 13:00:04 +03001156 if (err)
1157 goto err_umem_release;
1158
1159 devx_umem_reg_cmd_build(dev, obj, &cmd);
1160
1161 MLX5_SET(general_obj_in_cmd_hdr, cmd.in, uid, c->devx_uid);
1162 err = mlx5_cmd_exec(dev->mdev, cmd.in, cmd.inlen, cmd.out,
1163 sizeof(cmd.out));
1164 if (err)
Jason Gunthorpeb61815e2018-08-09 20:14:41 -06001165 goto err_umem_release;
Yishai Hadasaeae9452018-06-17 13:00:04 +03001166
1167 obj->mdev = dev->mdev;
1168 uobj->object = obj;
1169 devx_obj_build_destroy_cmd(cmd.in, cmd.out, obj->dinbox, &obj->dinlen, &obj_id);
1170 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_UMEM_REG_OUT_ID, &obj_id, sizeof(obj_id));
1171 if (err)
1172 goto err_umem_destroy;
1173
Yishai Hadasaeae9452018-06-17 13:00:04 +03001174 return 0;
1175
1176err_umem_destroy:
1177 mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, cmd.out, sizeof(cmd.out));
Yishai Hadasaeae9452018-06-17 13:00:04 +03001178err_umem_release:
1179 ib_umem_release(obj->umem);
1180err_obj_free:
1181 kfree(obj);
1182 return err;
1183}
1184
Yishai Hadasaeae9452018-06-17 13:00:04 +03001185static int devx_umem_cleanup(struct ib_uobject *uobject,
1186 enum rdma_remove_reason why)
1187{
1188 struct devx_umem *obj = uobject->object;
1189 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
1190 int err;
1191
1192 err = mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, out, sizeof(out));
Yishai Hadas1c774832018-06-20 17:11:39 +03001193 if (ib_is_destroy_retryable(err, why, uobject))
Yishai Hadasaeae9452018-06-17 13:00:04 +03001194 return err;
1195
1196 ib_umem_release(obj->umem);
1197 kfree(obj);
1198 return 0;
1199}
1200
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001201DECLARE_UVERBS_NAMED_METHOD(
1202 MLX5_IB_METHOD_DEVX_UMEM_REG,
1203 UVERBS_ATTR_IDR(MLX5_IB_ATTR_DEVX_UMEM_REG_HANDLE,
1204 MLX5_IB_OBJECT_DEVX_UMEM,
1205 UVERBS_ACCESS_NEW,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001206 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001207 UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_DEVX_UMEM_REG_ADDR,
1208 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001209 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001210 UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_DEVX_UMEM_REG_LEN,
1211 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001212 UA_MANDATORY),
Jason Gunthorpebccd0622018-07-26 16:37:14 -06001213 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_DEVX_UMEM_REG_ACCESS,
1214 enum ib_access_flags),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001215 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_DEVX_UMEM_REG_OUT_ID,
1216 UVERBS_ATTR_TYPE(u32),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001217 UA_MANDATORY));
Yishai Hadasaeae9452018-06-17 13:00:04 +03001218
Yishai Hadas528922a2018-07-08 13:24:39 +03001219DECLARE_UVERBS_NAMED_METHOD_DESTROY(
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001220 MLX5_IB_METHOD_DEVX_UMEM_DEREG,
1221 UVERBS_ATTR_IDR(MLX5_IB_ATTR_DEVX_UMEM_DEREG_HANDLE,
1222 MLX5_IB_OBJECT_DEVX_UMEM,
1223 UVERBS_ACCESS_DESTROY,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001224 UA_MANDATORY));
Yishai Hadasaeae9452018-06-17 13:00:04 +03001225
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001226DECLARE_UVERBS_NAMED_METHOD(
1227 MLX5_IB_METHOD_DEVX_QUERY_EQN,
1228 UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_DEVX_QUERY_EQN_USER_VEC,
1229 UVERBS_ATTR_TYPE(u32),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001230 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001231 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_DEVX_QUERY_EQN_DEV_EQN,
1232 UVERBS_ATTR_TYPE(u32),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001233 UA_MANDATORY));
Yishai Hadasf6fe01b2018-06-17 13:00:05 +03001234
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001235DECLARE_UVERBS_NAMED_METHOD(
1236 MLX5_IB_METHOD_DEVX_QUERY_UAR,
1237 UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_DEVX_QUERY_UAR_USER_IDX,
1238 UVERBS_ATTR_TYPE(u32),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001239 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001240 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_DEVX_QUERY_UAR_DEV_IDX,
1241 UVERBS_ATTR_TYPE(u32),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001242 UA_MANDATORY));
Yishai Hadas7c043e92018-06-17 13:00:03 +03001243
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001244DECLARE_UVERBS_NAMED_METHOD(
1245 MLX5_IB_METHOD_DEVX_OTHER,
1246 UVERBS_ATTR_PTR_IN(
1247 MLX5_IB_ATTR_DEVX_OTHER_CMD_IN,
1248 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_in_cmd_hdr)),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001249 UA_MANDATORY,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001250 UA_ALLOC_AND_COPY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001251 UVERBS_ATTR_PTR_OUT(
1252 MLX5_IB_ATTR_DEVX_OTHER_CMD_OUT,
1253 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_out_cmd_hdr)),
Jason Gunthorpe540cd692018-07-04 08:50:30 +03001254 UA_MANDATORY));
Yishai Hadas8aa8c952018-06-17 13:00:00 +03001255
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001256DECLARE_UVERBS_NAMED_METHOD(
1257 MLX5_IB_METHOD_DEVX_OBJ_CREATE,
1258 UVERBS_ATTR_IDR(MLX5_IB_ATTR_DEVX_OBJ_CREATE_HANDLE,
1259 MLX5_IB_OBJECT_DEVX_OBJ,
1260 UVERBS_ACCESS_NEW,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001261 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001262 UVERBS_ATTR_PTR_IN(
1263 MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_IN,
1264 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_in_cmd_hdr)),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001265 UA_MANDATORY,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001266 UA_ALLOC_AND_COPY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001267 UVERBS_ATTR_PTR_OUT(
1268 MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT,
1269 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_out_cmd_hdr)),
Jason Gunthorpe540cd692018-07-04 08:50:30 +03001270 UA_MANDATORY));
Yishai Hadas7efce362018-06-17 13:00:01 +03001271
Yishai Hadas528922a2018-07-08 13:24:39 +03001272DECLARE_UVERBS_NAMED_METHOD_DESTROY(
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001273 MLX5_IB_METHOD_DEVX_OBJ_DESTROY,
1274 UVERBS_ATTR_IDR(MLX5_IB_ATTR_DEVX_OBJ_DESTROY_HANDLE,
1275 MLX5_IB_OBJECT_DEVX_OBJ,
1276 UVERBS_ACCESS_DESTROY,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001277 UA_MANDATORY));
Yishai Hadas7efce362018-06-17 13:00:01 +03001278
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001279DECLARE_UVERBS_NAMED_METHOD(
1280 MLX5_IB_METHOD_DEVX_OBJ_MODIFY,
1281 UVERBS_ATTR_IDR(MLX5_IB_ATTR_DEVX_OBJ_MODIFY_HANDLE,
1282 MLX5_IB_OBJECT_DEVX_OBJ,
1283 UVERBS_ACCESS_WRITE,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001284 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001285 UVERBS_ATTR_PTR_IN(
1286 MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN,
1287 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_in_cmd_hdr)),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001288 UA_MANDATORY,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001289 UA_ALLOC_AND_COPY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001290 UVERBS_ATTR_PTR_OUT(
1291 MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_OUT,
1292 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_out_cmd_hdr)),
Jason Gunthorpe540cd692018-07-04 08:50:30 +03001293 UA_MANDATORY));
Yishai Hadase662e142018-06-17 13:00:02 +03001294
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001295DECLARE_UVERBS_NAMED_METHOD(
1296 MLX5_IB_METHOD_DEVX_OBJ_QUERY,
1297 UVERBS_ATTR_IDR(MLX5_IB_ATTR_DEVX_OBJ_QUERY_HANDLE,
1298 MLX5_IB_OBJECT_DEVX_OBJ,
1299 UVERBS_ACCESS_READ,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001300 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001301 UVERBS_ATTR_PTR_IN(
1302 MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN,
1303 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_in_cmd_hdr)),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001304 UA_MANDATORY,
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03001305 UA_ALLOC_AND_COPY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001306 UVERBS_ATTR_PTR_OUT(
1307 MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_OUT,
1308 UVERBS_ATTR_MIN_SIZE(MLX5_ST_SZ_BYTES(general_obj_out_cmd_hdr)),
Jason Gunthorpe540cd692018-07-04 08:50:30 +03001309 UA_MANDATORY));
Yishai Hadase662e142018-06-17 13:00:02 +03001310
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +03001311DECLARE_UVERBS_GLOBAL_METHODS(MLX5_IB_OBJECT_DEVX,
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001312 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_OTHER),
1313 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_QUERY_UAR),
1314 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_QUERY_EQN));
Yishai Hadas8aa8c952018-06-17 13:00:00 +03001315
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +03001316DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_DEVX_OBJ,
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001317 UVERBS_TYPE_ALLOC_IDR(devx_obj_cleanup),
1318 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_OBJ_CREATE),
1319 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_OBJ_DESTROY),
1320 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_OBJ_MODIFY),
1321 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_OBJ_QUERY));
Yishai Hadas7efce362018-06-17 13:00:01 +03001322
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +03001323DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_DEVX_UMEM,
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001324 UVERBS_TYPE_ALLOC_IDR(devx_umem_cleanup),
1325 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_UMEM_REG),
1326 &UVERBS_METHOD(MLX5_IB_METHOD_DEVX_UMEM_DEREG));
Yishai Hadasaeae9452018-06-17 13:00:04 +03001327
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +03001328DECLARE_UVERBS_OBJECT_TREE(devx_objects,
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03001329 &UVERBS_OBJECT(MLX5_IB_OBJECT_DEVX),
1330 &UVERBS_OBJECT(MLX5_IB_OBJECT_DEVX_OBJ),
1331 &UVERBS_OBJECT(MLX5_IB_OBJECT_DEVX_UMEM));
Yishai Hadasc59450c2018-06-17 13:00:06 +03001332
1333const struct uverbs_object_tree_def *mlx5_ib_get_devx_tree(void)
1334{
1335 return &devx_objects;
1336}