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