1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
2 /*
3  * Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All rights reserved.
4  */
5 
6 #include "efa_com.h"
7 #include "efa_com_cmd.h"
8 
efa_com_create_qp(struct efa_com_dev * edev,struct efa_com_create_qp_params * params,struct efa_com_create_qp_result * res)9 int efa_com_create_qp(struct efa_com_dev *edev,
10 		      struct efa_com_create_qp_params *params,
11 		      struct efa_com_create_qp_result *res)
12 {
13 	struct efa_admin_create_qp_cmd create_qp_cmd = {};
14 	struct efa_admin_create_qp_resp cmd_completion;
15 	struct efa_com_admin_queue *aq = &edev->aq;
16 	int err;
17 
18 	create_qp_cmd.aq_common_desc.opcode = EFA_ADMIN_CREATE_QP;
19 
20 	create_qp_cmd.pd = params->pd;
21 	create_qp_cmd.qp_type = params->qp_type;
22 	create_qp_cmd.rq_base_addr = params->rq_base_addr;
23 	create_qp_cmd.send_cq_idx = params->send_cq_idx;
24 	create_qp_cmd.recv_cq_idx = params->recv_cq_idx;
25 	create_qp_cmd.qp_alloc_size.send_queue_ring_size =
26 		params->sq_ring_size_in_bytes;
27 	create_qp_cmd.qp_alloc_size.send_queue_depth =
28 			params->sq_depth;
29 	create_qp_cmd.qp_alloc_size.recv_queue_ring_size =
30 			params->rq_ring_size_in_bytes;
31 	create_qp_cmd.qp_alloc_size.recv_queue_depth =
32 			params->rq_depth;
33 	create_qp_cmd.uar = params->uarn;
34 
35 	err = efa_com_cmd_exec(aq,
36 			       (struct efa_admin_aq_entry *)&create_qp_cmd,
37 			       sizeof(create_qp_cmd),
38 			       (struct efa_admin_acq_entry *)&cmd_completion,
39 			       sizeof(cmd_completion));
40 	if (err) {
41 		ibdev_err_ratelimited(edev->efa_dev,
42 				      "Failed to create qp [%d]\n", err);
43 		return err;
44 	}
45 
46 	res->qp_handle = cmd_completion.qp_handle;
47 	res->qp_num = cmd_completion.qp_num;
48 	res->sq_db_offset = cmd_completion.sq_db_offset;
49 	res->rq_db_offset = cmd_completion.rq_db_offset;
50 	res->llq_descriptors_offset = cmd_completion.llq_descriptors_offset;
51 	res->send_sub_cq_idx = cmd_completion.send_sub_cq_idx;
52 	res->recv_sub_cq_idx = cmd_completion.recv_sub_cq_idx;
53 
54 	return 0;
55 }
56 
efa_com_modify_qp(struct efa_com_dev * edev,struct efa_com_modify_qp_params * params)57 int efa_com_modify_qp(struct efa_com_dev *edev,
58 		      struct efa_com_modify_qp_params *params)
59 {
60 	struct efa_com_admin_queue *aq = &edev->aq;
61 	struct efa_admin_modify_qp_cmd cmd = {};
62 	struct efa_admin_modify_qp_resp resp;
63 	int err;
64 
65 	cmd.aq_common_desc.opcode = EFA_ADMIN_MODIFY_QP;
66 	cmd.modify_mask = params->modify_mask;
67 	cmd.qp_handle = params->qp_handle;
68 	cmd.qp_state = params->qp_state;
69 	cmd.cur_qp_state = params->cur_qp_state;
70 	cmd.qkey = params->qkey;
71 	cmd.sq_psn = params->sq_psn;
72 	cmd.sq_drained_async_notify = params->sq_drained_async_notify;
73 	cmd.rnr_retry = params->rnr_retry;
74 
75 	err = efa_com_cmd_exec(aq,
76 			       (struct efa_admin_aq_entry *)&cmd,
77 			       sizeof(cmd),
78 			       (struct efa_admin_acq_entry *)&resp,
79 			       sizeof(resp));
80 	if (err) {
81 		ibdev_err_ratelimited(
82 			edev->efa_dev,
83 			"Failed to modify qp-%u modify_mask[%#x] [%d]\n",
84 			cmd.qp_handle, cmd.modify_mask, err);
85 		return err;
86 	}
87 
88 	return 0;
89 }
90 
efa_com_query_qp(struct efa_com_dev * edev,struct efa_com_query_qp_params * params,struct efa_com_query_qp_result * result)91 int efa_com_query_qp(struct efa_com_dev *edev,
92 		     struct efa_com_query_qp_params *params,
93 		     struct efa_com_query_qp_result *result)
94 {
95 	struct efa_com_admin_queue *aq = &edev->aq;
96 	struct efa_admin_query_qp_cmd cmd = {};
97 	struct efa_admin_query_qp_resp resp;
98 	int err;
99 
100 	cmd.aq_common_desc.opcode = EFA_ADMIN_QUERY_QP;
101 	cmd.qp_handle = params->qp_handle;
102 
103 	err = efa_com_cmd_exec(aq,
104 			       (struct efa_admin_aq_entry *)&cmd,
105 			       sizeof(cmd),
106 			       (struct efa_admin_acq_entry *)&resp,
107 			       sizeof(resp));
108 	if (err) {
109 		ibdev_err_ratelimited(edev->efa_dev,
110 				      "Failed to query qp-%u [%d]\n",
111 				      cmd.qp_handle, err);
112 		return err;
113 	}
114 
115 	result->qp_state = resp.qp_state;
116 	result->qkey = resp.qkey;
117 	result->sq_draining = resp.sq_draining;
118 	result->sq_psn = resp.sq_psn;
119 	result->rnr_retry = resp.rnr_retry;
120 
121 	return 0;
122 }
123 
efa_com_destroy_qp(struct efa_com_dev * edev,struct efa_com_destroy_qp_params * params)124 int efa_com_destroy_qp(struct efa_com_dev *edev,
125 		       struct efa_com_destroy_qp_params *params)
126 {
127 	struct efa_admin_destroy_qp_resp cmd_completion;
128 	struct efa_admin_destroy_qp_cmd qp_cmd = {};
129 	struct efa_com_admin_queue *aq = &edev->aq;
130 	int err;
131 
132 	qp_cmd.aq_common_desc.opcode = EFA_ADMIN_DESTROY_QP;
133 	qp_cmd.qp_handle = params->qp_handle;
134 
135 	err = efa_com_cmd_exec(aq,
136 			       (struct efa_admin_aq_entry *)&qp_cmd,
137 			       sizeof(qp_cmd),
138 			       (struct efa_admin_acq_entry *)&cmd_completion,
139 			       sizeof(cmd_completion));
140 	if (err) {
141 		ibdev_err_ratelimited(edev->efa_dev,
142 				      "Failed to destroy qp-%u [%d]\n",
143 				      qp_cmd.qp_handle, err);
144 		return err;
145 	}
146 
147 	return 0;
148 }
149 
efa_com_create_cq(struct efa_com_dev * edev,struct efa_com_create_cq_params * params,struct efa_com_create_cq_result * result)150 int efa_com_create_cq(struct efa_com_dev *edev,
151 		      struct efa_com_create_cq_params *params,
152 		      struct efa_com_create_cq_result *result)
153 {
154 	struct efa_admin_create_cq_resp cmd_completion = {};
155 	struct efa_admin_create_cq_cmd create_cmd = {};
156 	struct efa_com_admin_queue *aq = &edev->aq;
157 	int err;
158 
159 	create_cmd.aq_common_desc.opcode = EFA_ADMIN_CREATE_CQ;
160 	EFA_SET(&create_cmd.cq_caps_2,
161 		EFA_ADMIN_CREATE_CQ_CMD_CQ_ENTRY_SIZE_WORDS,
162 		params->entry_size_in_bytes / 4);
163 	create_cmd.cq_depth = params->cq_depth;
164 	create_cmd.num_sub_cqs = params->num_sub_cqs;
165 	create_cmd.uar = params->uarn;
166 	if (params->interrupt_mode_enabled) {
167 		EFA_SET(&create_cmd.cq_caps_1,
168 			EFA_ADMIN_CREATE_CQ_CMD_INTERRUPT_MODE_ENABLED, 1);
169 		create_cmd.eqn = params->eqn;
170 	}
171 	if (params->set_src_addr) {
172 		EFA_SET(&create_cmd.cq_caps_2,
173 			EFA_ADMIN_CREATE_CQ_CMD_SET_SRC_ADDR, 1);
174 	}
175 	efa_com_set_dma_addr(params->dma_addr,
176 			     &create_cmd.cq_ba.mem_addr_high,
177 			     &create_cmd.cq_ba.mem_addr_low);
178 
179 	err = efa_com_cmd_exec(aq,
180 			       (struct efa_admin_aq_entry *)&create_cmd,
181 			       sizeof(create_cmd),
182 			       (struct efa_admin_acq_entry *)&cmd_completion,
183 			       sizeof(cmd_completion));
184 	if (err) {
185 		ibdev_err_ratelimited(edev->efa_dev,
186 				      "Failed to create cq[%d]\n", err);
187 		return err;
188 	}
189 
190 	result->cq_idx = cmd_completion.cq_idx;
191 	result->actual_depth = params->cq_depth;
192 	result->db_off = cmd_completion.db_offset;
193 	result->db_valid = EFA_GET(&cmd_completion.flags,
194 				   EFA_ADMIN_CREATE_CQ_RESP_DB_VALID);
195 
196 	return 0;
197 }
198 
efa_com_destroy_cq(struct efa_com_dev * edev,struct efa_com_destroy_cq_params * params)199 int efa_com_destroy_cq(struct efa_com_dev *edev,
200 		       struct efa_com_destroy_cq_params *params)
201 {
202 	struct efa_admin_destroy_cq_cmd destroy_cmd = {};
203 	struct efa_admin_destroy_cq_resp destroy_resp;
204 	struct efa_com_admin_queue *aq = &edev->aq;
205 	int err;
206 
207 	destroy_cmd.cq_idx = params->cq_idx;
208 	destroy_cmd.aq_common_desc.opcode = EFA_ADMIN_DESTROY_CQ;
209 
210 	err = efa_com_cmd_exec(aq,
211 			       (struct efa_admin_aq_entry *)&destroy_cmd,
212 			       sizeof(destroy_cmd),
213 			       (struct efa_admin_acq_entry *)&destroy_resp,
214 			       sizeof(destroy_resp));
215 
216 	if (err) {
217 		ibdev_err_ratelimited(edev->efa_dev,
218 				      "Failed to destroy CQ-%u [%d]\n",
219 				      params->cq_idx, err);
220 		return err;
221 	}
222 
223 	return 0;
224 }
225 
efa_com_register_mr(struct efa_com_dev * edev,struct efa_com_reg_mr_params * params,struct efa_com_reg_mr_result * result)226 int efa_com_register_mr(struct efa_com_dev *edev,
227 			struct efa_com_reg_mr_params *params,
228 			struct efa_com_reg_mr_result *result)
229 {
230 	struct efa_admin_reg_mr_resp cmd_completion;
231 	struct efa_com_admin_queue *aq = &edev->aq;
232 	struct efa_admin_reg_mr_cmd mr_cmd = {};
233 	int err;
234 
235 	mr_cmd.aq_common_desc.opcode = EFA_ADMIN_REG_MR;
236 	mr_cmd.pd = params->pd;
237 	mr_cmd.mr_length = params->mr_length_in_bytes;
238 	EFA_SET(&mr_cmd.flags, EFA_ADMIN_REG_MR_CMD_PHYS_PAGE_SIZE_SHIFT,
239 		params->page_shift);
240 	mr_cmd.iova = params->iova;
241 	mr_cmd.permissions = params->permissions;
242 
243 	if (params->inline_pbl) {
244 		memcpy(mr_cmd.pbl.inline_pbl_array,
245 		       params->pbl.inline_pbl_array,
246 		       sizeof(mr_cmd.pbl.inline_pbl_array));
247 	} else {
248 		mr_cmd.pbl.pbl.length = params->pbl.pbl.length;
249 		mr_cmd.pbl.pbl.address.mem_addr_low =
250 			params->pbl.pbl.address.mem_addr_low;
251 		mr_cmd.pbl.pbl.address.mem_addr_high =
252 			params->pbl.pbl.address.mem_addr_high;
253 		EFA_SET(&mr_cmd.aq_common_desc.flags,
254 			EFA_ADMIN_AQ_COMMON_DESC_CTRL_DATA, 1);
255 		if (params->indirect)
256 			EFA_SET(&mr_cmd.aq_common_desc.flags,
257 				EFA_ADMIN_AQ_COMMON_DESC_CTRL_DATA_INDIRECT, 1);
258 	}
259 
260 	err = efa_com_cmd_exec(aq,
261 			       (struct efa_admin_aq_entry *)&mr_cmd,
262 			       sizeof(mr_cmd),
263 			       (struct efa_admin_acq_entry *)&cmd_completion,
264 			       sizeof(cmd_completion));
265 	if (err) {
266 		ibdev_err_ratelimited(edev->efa_dev,
267 				      "Failed to register mr [%d]\n", err);
268 		return err;
269 	}
270 
271 	result->l_key = cmd_completion.l_key;
272 	result->r_key = cmd_completion.r_key;
273 
274 	return 0;
275 }
276 
efa_com_dereg_mr(struct efa_com_dev * edev,struct efa_com_dereg_mr_params * params)277 int efa_com_dereg_mr(struct efa_com_dev *edev,
278 		     struct efa_com_dereg_mr_params *params)
279 {
280 	struct efa_admin_dereg_mr_resp cmd_completion;
281 	struct efa_com_admin_queue *aq = &edev->aq;
282 	struct efa_admin_dereg_mr_cmd mr_cmd = {};
283 	int err;
284 
285 	mr_cmd.aq_common_desc.opcode = EFA_ADMIN_DEREG_MR;
286 	mr_cmd.l_key = params->l_key;
287 
288 	err = efa_com_cmd_exec(aq,
289 			       (struct efa_admin_aq_entry *)&mr_cmd,
290 			       sizeof(mr_cmd),
291 			       (struct efa_admin_acq_entry *)&cmd_completion,
292 			       sizeof(cmd_completion));
293 	if (err) {
294 		ibdev_err_ratelimited(edev->efa_dev,
295 				      "Failed to de-register mr(lkey-%u) [%d]\n",
296 				      mr_cmd.l_key, err);
297 		return err;
298 	}
299 
300 	return 0;
301 }
302 
efa_com_create_ah(struct efa_com_dev * edev,struct efa_com_create_ah_params * params,struct efa_com_create_ah_result * result)303 int efa_com_create_ah(struct efa_com_dev *edev,
304 		      struct efa_com_create_ah_params *params,
305 		      struct efa_com_create_ah_result *result)
306 {
307 	struct efa_admin_create_ah_resp cmd_completion;
308 	struct efa_com_admin_queue *aq = &edev->aq;
309 	struct efa_admin_create_ah_cmd ah_cmd = {};
310 	int err;
311 
312 	ah_cmd.aq_common_desc.opcode = EFA_ADMIN_CREATE_AH;
313 
314 	memcpy(ah_cmd.dest_addr, params->dest_addr, sizeof(ah_cmd.dest_addr));
315 	ah_cmd.pd = params->pdn;
316 
317 	err = efa_com_cmd_exec(aq,
318 			       (struct efa_admin_aq_entry *)&ah_cmd,
319 			       sizeof(ah_cmd),
320 			       (struct efa_admin_acq_entry *)&cmd_completion,
321 			       sizeof(cmd_completion));
322 	if (err) {
323 		ibdev_err_ratelimited(edev->efa_dev,
324 				      "Failed to create ah for %pI6 [%d]\n",
325 				      ah_cmd.dest_addr, err);
326 		return err;
327 	}
328 
329 	result->ah = cmd_completion.ah;
330 
331 	return 0;
332 }
333 
efa_com_destroy_ah(struct efa_com_dev * edev,struct efa_com_destroy_ah_params * params)334 int efa_com_destroy_ah(struct efa_com_dev *edev,
335 		       struct efa_com_destroy_ah_params *params)
336 {
337 	struct efa_admin_destroy_ah_resp cmd_completion;
338 	struct efa_admin_destroy_ah_cmd ah_cmd = {};
339 	struct efa_com_admin_queue *aq = &edev->aq;
340 	int err;
341 
342 	ah_cmd.aq_common_desc.opcode = EFA_ADMIN_DESTROY_AH;
343 	ah_cmd.ah = params->ah;
344 	ah_cmd.pd = params->pdn;
345 
346 	err = efa_com_cmd_exec(aq,
347 			       (struct efa_admin_aq_entry *)&ah_cmd,
348 			       sizeof(ah_cmd),
349 			       (struct efa_admin_acq_entry *)&cmd_completion,
350 			       sizeof(cmd_completion));
351 	if (err) {
352 		ibdev_err_ratelimited(edev->efa_dev,
353 				      "Failed to destroy ah-%d pd-%d [%d]\n",
354 				      ah_cmd.ah, ah_cmd.pd, err);
355 		return err;
356 	}
357 
358 	return 0;
359 }
360 
361 bool
efa_com_check_supported_feature_id(struct efa_com_dev * edev,enum efa_admin_aq_feature_id feature_id)362 efa_com_check_supported_feature_id(struct efa_com_dev *edev,
363 				   enum efa_admin_aq_feature_id feature_id)
364 {
365 	u32 feature_mask = 1 << feature_id;
366 
367 	/* Device attributes is always supported */
368 	if (feature_id != EFA_ADMIN_DEVICE_ATTR &&
369 	    !(edev->supported_features & feature_mask))
370 		return false;
371 
372 	return true;
373 }
374 
efa_com_get_feature_ex(struct efa_com_dev * edev,struct efa_admin_get_feature_resp * get_resp,enum efa_admin_aq_feature_id feature_id,dma_addr_t control_buf_dma_addr,u32 control_buff_size)375 static int efa_com_get_feature_ex(struct efa_com_dev *edev,
376 				  struct efa_admin_get_feature_resp *get_resp,
377 				  enum efa_admin_aq_feature_id feature_id,
378 				  dma_addr_t control_buf_dma_addr,
379 				  u32 control_buff_size)
380 {
381 	struct efa_admin_get_feature_cmd get_cmd = {};
382 	struct efa_com_admin_queue *aq;
383 	int err;
384 
385 	if (!efa_com_check_supported_feature_id(edev, feature_id)) {
386 		ibdev_err_ratelimited(edev->efa_dev,
387 				      "Feature %d isn't supported\n",
388 				      feature_id);
389 		return -EOPNOTSUPP;
390 	}
391 
392 	aq = &edev->aq;
393 
394 	get_cmd.aq_common_descriptor.opcode = EFA_ADMIN_GET_FEATURE;
395 
396 	if (control_buff_size)
397 		EFA_SET(&get_cmd.aq_common_descriptor.flags,
398 			EFA_ADMIN_AQ_COMMON_DESC_CTRL_DATA, 1);
399 
400 	efa_com_set_dma_addr(control_buf_dma_addr,
401 			     &get_cmd.control_buffer.address.mem_addr_high,
402 			     &get_cmd.control_buffer.address.mem_addr_low);
403 
404 	get_cmd.control_buffer.length = control_buff_size;
405 	get_cmd.feature_common.feature_id = feature_id;
406 	err = efa_com_cmd_exec(aq,
407 			       (struct efa_admin_aq_entry *)
408 			       &get_cmd,
409 			       sizeof(get_cmd),
410 			       (struct efa_admin_acq_entry *)
411 			       get_resp,
412 			       sizeof(*get_resp));
413 
414 	if (err) {
415 		ibdev_err_ratelimited(
416 			edev->efa_dev,
417 			"Failed to submit get_feature command %d [%d]\n",
418 			feature_id, err);
419 		return err;
420 	}
421 
422 	return 0;
423 }
424 
efa_com_get_feature(struct efa_com_dev * edev,struct efa_admin_get_feature_resp * get_resp,enum efa_admin_aq_feature_id feature_id)425 static int efa_com_get_feature(struct efa_com_dev *edev,
426 			       struct efa_admin_get_feature_resp *get_resp,
427 			       enum efa_admin_aq_feature_id feature_id)
428 {
429 	return efa_com_get_feature_ex(edev, get_resp, feature_id, 0, 0);
430 }
431 
efa_com_get_device_attr(struct efa_com_dev * edev,struct efa_com_get_device_attr_result * result)432 int efa_com_get_device_attr(struct efa_com_dev *edev,
433 			    struct efa_com_get_device_attr_result *result)
434 {
435 	struct efa_admin_get_feature_resp resp;
436 	int err;
437 
438 	err = efa_com_get_feature(edev, &resp, EFA_ADMIN_DEVICE_ATTR);
439 	if (err) {
440 		ibdev_err_ratelimited(edev->efa_dev,
441 				      "Failed to get device attributes %d\n",
442 				      err);
443 		return err;
444 	}
445 
446 	result->page_size_cap = resp.u.device_attr.page_size_cap;
447 	result->fw_version = resp.u.device_attr.fw_version;
448 	result->admin_api_version = resp.u.device_attr.admin_api_version;
449 	result->device_version = resp.u.device_attr.device_version;
450 	result->supported_features = resp.u.device_attr.supported_features;
451 	result->phys_addr_width = resp.u.device_attr.phys_addr_width;
452 	result->virt_addr_width = resp.u.device_attr.virt_addr_width;
453 	result->db_bar = resp.u.device_attr.db_bar;
454 	result->max_rdma_size = resp.u.device_attr.max_rdma_size;
455 	result->device_caps = resp.u.device_attr.device_caps;
456 
457 	if (result->admin_api_version < 1) {
458 		ibdev_err_ratelimited(
459 			edev->efa_dev,
460 			"Failed to get device attr api version [%u < 1]\n",
461 			result->admin_api_version);
462 		return -EINVAL;
463 	}
464 
465 	edev->supported_features = resp.u.device_attr.supported_features;
466 	err = efa_com_get_feature(edev, &resp,
467 				  EFA_ADMIN_QUEUE_ATTR);
468 	if (err) {
469 		ibdev_err_ratelimited(edev->efa_dev,
470 				      "Failed to get queue attributes %d\n",
471 				      err);
472 		return err;
473 	}
474 
475 	result->max_qp = resp.u.queue_attr.max_qp;
476 	result->max_sq_depth = resp.u.queue_attr.max_sq_depth;
477 	result->max_rq_depth = resp.u.queue_attr.max_rq_depth;
478 	result->max_cq = resp.u.queue_attr.max_cq;
479 	result->max_cq_depth = resp.u.queue_attr.max_cq_depth;
480 	result->inline_buf_size = resp.u.queue_attr.inline_buf_size;
481 	result->max_sq_sge = resp.u.queue_attr.max_wr_send_sges;
482 	result->max_rq_sge = resp.u.queue_attr.max_wr_recv_sges;
483 	result->max_mr = resp.u.queue_attr.max_mr;
484 	result->max_mr_pages = resp.u.queue_attr.max_mr_pages;
485 	result->max_pd = resp.u.queue_attr.max_pd;
486 	result->max_ah = resp.u.queue_attr.max_ah;
487 	result->max_llq_size = resp.u.queue_attr.max_llq_size;
488 	result->sub_cqs_per_cq = resp.u.queue_attr.sub_cqs_per_cq;
489 	result->max_wr_rdma_sge = resp.u.queue_attr.max_wr_rdma_sges;
490 	result->max_tx_batch = resp.u.queue_attr.max_tx_batch;
491 	result->min_sq_depth = resp.u.queue_attr.min_sq_depth;
492 
493 	err = efa_com_get_feature(edev, &resp, EFA_ADMIN_NETWORK_ATTR);
494 	if (err) {
495 		ibdev_err_ratelimited(edev->efa_dev,
496 				      "Failed to get network attributes %d\n",
497 				      err);
498 		return err;
499 	}
500 
501 	memcpy(result->addr, resp.u.network_attr.addr,
502 	       sizeof(resp.u.network_attr.addr));
503 	result->mtu = resp.u.network_attr.mtu;
504 
505 	if (efa_com_check_supported_feature_id(edev,
506 					       EFA_ADMIN_EVENT_QUEUE_ATTR)) {
507 		err = efa_com_get_feature(edev, &resp,
508 					  EFA_ADMIN_EVENT_QUEUE_ATTR);
509 		if (err) {
510 			ibdev_err_ratelimited(
511 				edev->efa_dev,
512 				"Failed to get event queue attributes %d\n",
513 				err);
514 			return err;
515 		}
516 
517 		result->max_eq = resp.u.event_queue_attr.max_eq;
518 		result->max_eq_depth = resp.u.event_queue_attr.max_eq_depth;
519 		result->event_bitmask = resp.u.event_queue_attr.event_bitmask;
520 	}
521 
522 	return 0;
523 }
524 
efa_com_get_hw_hints(struct efa_com_dev * edev,struct efa_com_get_hw_hints_result * result)525 int efa_com_get_hw_hints(struct efa_com_dev *edev,
526 			 struct efa_com_get_hw_hints_result *result)
527 {
528 	struct efa_admin_get_feature_resp resp;
529 	int err;
530 
531 	err = efa_com_get_feature(edev, &resp, EFA_ADMIN_HW_HINTS);
532 	if (err) {
533 		ibdev_err_ratelimited(edev->efa_dev,
534 				      "Failed to get hw hints %d\n", err);
535 		return err;
536 	}
537 
538 	result->admin_completion_timeout = resp.u.hw_hints.admin_completion_timeout;
539 	result->driver_watchdog_timeout = resp.u.hw_hints.driver_watchdog_timeout;
540 	result->mmio_read_timeout = resp.u.hw_hints.mmio_read_timeout;
541 	result->poll_interval = resp.u.hw_hints.poll_interval;
542 
543 	return 0;
544 }
545 
efa_com_set_feature_ex(struct efa_com_dev * edev,struct efa_admin_set_feature_resp * set_resp,struct efa_admin_set_feature_cmd * set_cmd,enum efa_admin_aq_feature_id feature_id,dma_addr_t control_buf_dma_addr,u32 control_buff_size)546 int efa_com_set_feature_ex(struct efa_com_dev *edev,
547 			   struct efa_admin_set_feature_resp *set_resp,
548 			   struct efa_admin_set_feature_cmd *set_cmd,
549 			   enum efa_admin_aq_feature_id feature_id,
550 			   dma_addr_t control_buf_dma_addr,
551 			   u32 control_buff_size)
552 {
553 	struct efa_com_admin_queue *aq;
554 	int err;
555 
556 	if (!efa_com_check_supported_feature_id(edev, feature_id)) {
557 		ibdev_err_ratelimited(edev->efa_dev,
558 				      "Feature %d isn't supported\n",
559 				      feature_id);
560 		return -EOPNOTSUPP;
561 	}
562 
563 	aq = &edev->aq;
564 
565 	set_cmd->aq_common_descriptor.opcode = EFA_ADMIN_SET_FEATURE;
566 	if (control_buff_size) {
567 		set_cmd->aq_common_descriptor.flags = 0;
568 		EFA_SET(&set_cmd->aq_common_descriptor.flags,
569 			EFA_ADMIN_AQ_COMMON_DESC_CTRL_DATA, 1);
570 		efa_com_set_dma_addr(control_buf_dma_addr,
571 				     &set_cmd->control_buffer.address.mem_addr_high,
572 				     &set_cmd->control_buffer.address.mem_addr_low);
573 	}
574 
575 	set_cmd->control_buffer.length = control_buff_size;
576 	set_cmd->feature_common.feature_id = feature_id;
577 	err = efa_com_cmd_exec(aq,
578 			       (struct efa_admin_aq_entry *)set_cmd,
579 			       sizeof(*set_cmd),
580 			       (struct efa_admin_acq_entry *)set_resp,
581 			       sizeof(*set_resp));
582 
583 	if (err) {
584 		ibdev_err_ratelimited(
585 			edev->efa_dev,
586 			"Failed to submit set_feature command %d error: %d\n",
587 			feature_id, err);
588 		return err;
589 	}
590 
591 	return 0;
592 }
593 
efa_com_set_feature(struct efa_com_dev * edev,struct efa_admin_set_feature_resp * set_resp,struct efa_admin_set_feature_cmd * set_cmd,enum efa_admin_aq_feature_id feature_id)594 static int efa_com_set_feature(struct efa_com_dev *edev,
595 			       struct efa_admin_set_feature_resp *set_resp,
596 			       struct efa_admin_set_feature_cmd *set_cmd,
597 			       enum efa_admin_aq_feature_id feature_id)
598 {
599 	return efa_com_set_feature_ex(edev, set_resp, set_cmd, feature_id,
600 				      0, 0);
601 }
602 
efa_com_set_aenq_config(struct efa_com_dev * edev,u32 groups)603 int efa_com_set_aenq_config(struct efa_com_dev *edev, u32 groups)
604 {
605 	struct efa_admin_get_feature_resp get_resp;
606 	struct efa_admin_set_feature_resp set_resp;
607 	struct efa_admin_set_feature_cmd cmd = {};
608 	int err;
609 
610 	ibdev_dbg(edev->efa_dev, "Configuring aenq with groups[%#x]\n", groups);
611 
612 	err = efa_com_get_feature(edev, &get_resp, EFA_ADMIN_AENQ_CONFIG);
613 	if (err) {
614 		ibdev_err_ratelimited(edev->efa_dev,
615 				      "Failed to get aenq attributes: %d\n",
616 				      err);
617 		return err;
618 	}
619 
620 	ibdev_dbg(edev->efa_dev,
621 		  "Get aenq groups: supported[%#x] enabled[%#x]\n",
622 		  get_resp.u.aenq.supported_groups,
623 		  get_resp.u.aenq.enabled_groups);
624 
625 	if ((get_resp.u.aenq.supported_groups & groups) != groups) {
626 		ibdev_err_ratelimited(
627 			edev->efa_dev,
628 			"Trying to set unsupported aenq groups[%#x] supported[%#x]\n",
629 			groups, get_resp.u.aenq.supported_groups);
630 		return -EOPNOTSUPP;
631 	}
632 
633 	cmd.u.aenq.enabled_groups = groups;
634 	err = efa_com_set_feature(edev, &set_resp, &cmd,
635 				  EFA_ADMIN_AENQ_CONFIG);
636 	if (err) {
637 		ibdev_err_ratelimited(edev->efa_dev,
638 				      "Failed to set aenq attributes: %d\n",
639 				      err);
640 		return err;
641 	}
642 
643 	return 0;
644 }
645 
efa_com_alloc_pd(struct efa_com_dev * edev,struct efa_com_alloc_pd_result * result)646 int efa_com_alloc_pd(struct efa_com_dev *edev,
647 		     struct efa_com_alloc_pd_result *result)
648 {
649 	struct efa_com_admin_queue *aq = &edev->aq;
650 	struct efa_admin_alloc_pd_cmd cmd = {};
651 	struct efa_admin_alloc_pd_resp resp;
652 	int err;
653 
654 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_ALLOC_PD;
655 
656 	err = efa_com_cmd_exec(aq,
657 			       (struct efa_admin_aq_entry *)&cmd,
658 			       sizeof(cmd),
659 			       (struct efa_admin_acq_entry *)&resp,
660 			       sizeof(resp));
661 	if (err) {
662 		ibdev_err_ratelimited(edev->efa_dev,
663 				      "Failed to allocate pd[%d]\n", err);
664 		return err;
665 	}
666 
667 	result->pdn = resp.pd;
668 
669 	return 0;
670 }
671 
efa_com_dealloc_pd(struct efa_com_dev * edev,struct efa_com_dealloc_pd_params * params)672 int efa_com_dealloc_pd(struct efa_com_dev *edev,
673 		       struct efa_com_dealloc_pd_params *params)
674 {
675 	struct efa_com_admin_queue *aq = &edev->aq;
676 	struct efa_admin_dealloc_pd_cmd cmd = {};
677 	struct efa_admin_dealloc_pd_resp resp;
678 	int err;
679 
680 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_DEALLOC_PD;
681 	cmd.pd = params->pdn;
682 
683 	err = efa_com_cmd_exec(aq,
684 			       (struct efa_admin_aq_entry *)&cmd,
685 			       sizeof(cmd),
686 			       (struct efa_admin_acq_entry *)&resp,
687 			       sizeof(resp));
688 	if (err) {
689 		ibdev_err_ratelimited(edev->efa_dev,
690 				      "Failed to deallocate pd-%u [%d]\n",
691 				      cmd.pd, err);
692 		return err;
693 	}
694 
695 	return 0;
696 }
697 
efa_com_alloc_uar(struct efa_com_dev * edev,struct efa_com_alloc_uar_result * result)698 int efa_com_alloc_uar(struct efa_com_dev *edev,
699 		      struct efa_com_alloc_uar_result *result)
700 {
701 	struct efa_com_admin_queue *aq = &edev->aq;
702 	struct efa_admin_alloc_uar_cmd cmd = {};
703 	struct efa_admin_alloc_uar_resp resp;
704 	int err;
705 
706 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_ALLOC_UAR;
707 
708 	err = efa_com_cmd_exec(aq,
709 			       (struct efa_admin_aq_entry *)&cmd,
710 			       sizeof(cmd),
711 			       (struct efa_admin_acq_entry *)&resp,
712 			       sizeof(resp));
713 	if (err) {
714 		ibdev_err_ratelimited(edev->efa_dev,
715 				      "Failed to allocate uar[%d]\n", err);
716 		return err;
717 	}
718 
719 	result->uarn = resp.uar;
720 
721 	return 0;
722 }
723 
efa_com_dealloc_uar(struct efa_com_dev * edev,struct efa_com_dealloc_uar_params * params)724 int efa_com_dealloc_uar(struct efa_com_dev *edev,
725 			struct efa_com_dealloc_uar_params *params)
726 {
727 	struct efa_com_admin_queue *aq = &edev->aq;
728 	struct efa_admin_dealloc_uar_cmd cmd = {};
729 	struct efa_admin_dealloc_uar_resp resp;
730 	int err;
731 
732 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_DEALLOC_UAR;
733 	cmd.uar = params->uarn;
734 
735 	err = efa_com_cmd_exec(aq,
736 			       (struct efa_admin_aq_entry *)&cmd,
737 			       sizeof(cmd),
738 			       (struct efa_admin_acq_entry *)&resp,
739 			       sizeof(resp));
740 	if (err) {
741 		ibdev_err_ratelimited(edev->efa_dev,
742 				      "Failed to deallocate uar-%u [%d]\n",
743 				      cmd.uar, err);
744 		return err;
745 	}
746 
747 	return 0;
748 }
749 
efa_com_get_stats(struct efa_com_dev * edev,struct efa_com_get_stats_params * params,union efa_com_get_stats_result * result)750 int efa_com_get_stats(struct efa_com_dev *edev,
751 		      struct efa_com_get_stats_params *params,
752 		      union efa_com_get_stats_result *result)
753 {
754 	struct efa_com_admin_queue *aq = &edev->aq;
755 	struct efa_admin_aq_get_stats_cmd cmd = {};
756 	struct efa_admin_acq_get_stats_resp resp;
757 	int err;
758 
759 	cmd.aq_common_descriptor.opcode = EFA_ADMIN_GET_STATS;
760 	cmd.type = params->type;
761 	cmd.scope = params->scope;
762 	cmd.scope_modifier = params->scope_modifier;
763 
764 	err = efa_com_cmd_exec(aq,
765 			       (struct efa_admin_aq_entry *)&cmd,
766 			       sizeof(cmd),
767 			       (struct efa_admin_acq_entry *)&resp,
768 			       sizeof(resp));
769 	if (err) {
770 		ibdev_err_ratelimited(
771 			edev->efa_dev,
772 			"Failed to get stats type-%u scope-%u.%u [%d]\n",
773 			cmd.type, cmd.scope, cmd.scope_modifier, err);
774 		return err;
775 	}
776 
777 	switch (cmd.type) {
778 	case EFA_ADMIN_GET_STATS_TYPE_BASIC:
779 		result->basic_stats.tx_bytes = resp.u.basic_stats.tx_bytes;
780 		result->basic_stats.tx_pkts = resp.u.basic_stats.tx_pkts;
781 		result->basic_stats.rx_bytes = resp.u.basic_stats.rx_bytes;
782 		result->basic_stats.rx_pkts = resp.u.basic_stats.rx_pkts;
783 		result->basic_stats.rx_drops = resp.u.basic_stats.rx_drops;
784 		break;
785 	case EFA_ADMIN_GET_STATS_TYPE_MESSAGES:
786 		result->messages_stats.send_bytes = resp.u.messages_stats.send_bytes;
787 		result->messages_stats.send_wrs = resp.u.messages_stats.send_wrs;
788 		result->messages_stats.recv_bytes = resp.u.messages_stats.recv_bytes;
789 		result->messages_stats.recv_wrs = resp.u.messages_stats.recv_wrs;
790 		break;
791 	case EFA_ADMIN_GET_STATS_TYPE_RDMA_READ:
792 		result->rdma_read_stats.read_wrs = resp.u.rdma_read_stats.read_wrs;
793 		result->rdma_read_stats.read_bytes = resp.u.rdma_read_stats.read_bytes;
794 		result->rdma_read_stats.read_wr_err = resp.u.rdma_read_stats.read_wr_err;
795 		result->rdma_read_stats.read_resp_bytes = resp.u.rdma_read_stats.read_resp_bytes;
796 		break;
797 	}
798 
799 	return 0;
800 }
801