xref: /dpdk/drivers/net/iavf/iavf_vchnl.c (revision b5cd7351)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11 #include <inttypes.h>
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14 
15 #include <rte_debug.h>
16 #include <rte_alarm.h>
17 #include <rte_atomic.h>
18 #include <rte_eal.h>
19 #include <rte_ether.h>
20 #include <ethdev_driver.h>
21 #include <ethdev_pci.h>
22 #include <rte_dev.h>
23 
24 #include "iavf.h"
25 #include "iavf_rxtx.h"
26 
27 #define MAX_TRY_TIMES 2000
28 #define ASQ_DELAY_MS  1
29 
30 static uint32_t
iavf_convert_link_speed(enum virtchnl_link_speed virt_link_speed)31 iavf_convert_link_speed(enum virtchnl_link_speed virt_link_speed)
32 {
33 	uint32_t speed;
34 
35 	switch (virt_link_speed) {
36 	case VIRTCHNL_LINK_SPEED_100MB:
37 		speed = 100;
38 		break;
39 	case VIRTCHNL_LINK_SPEED_1GB:
40 		speed = 1000;
41 		break;
42 	case VIRTCHNL_LINK_SPEED_10GB:
43 		speed = 10000;
44 		break;
45 	case VIRTCHNL_LINK_SPEED_40GB:
46 		speed = 40000;
47 		break;
48 	case VIRTCHNL_LINK_SPEED_20GB:
49 		speed = 20000;
50 		break;
51 	case VIRTCHNL_LINK_SPEED_25GB:
52 		speed = 25000;
53 		break;
54 	case VIRTCHNL_LINK_SPEED_2_5GB:
55 		speed = 2500;
56 		break;
57 	case VIRTCHNL_LINK_SPEED_5GB:
58 		speed = 5000;
59 		break;
60 	default:
61 		speed = 0;
62 		break;
63 	}
64 
65 	return speed;
66 }
67 
68 /* Read data in admin queue to get msg from pf driver */
69 static enum iavf_aq_result
iavf_read_msg_from_pf(struct iavf_adapter * adapter,uint16_t buf_len,uint8_t * buf)70 iavf_read_msg_from_pf(struct iavf_adapter *adapter, uint16_t buf_len,
71 		     uint8_t *buf)
72 {
73 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
74 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
75 	struct iavf_arq_event_info event;
76 	enum iavf_aq_result result = IAVF_MSG_NON;
77 	enum virtchnl_ops opcode;
78 	int ret;
79 
80 	event.buf_len = buf_len;
81 	event.msg_buf = buf;
82 	ret = iavf_clean_arq_element(hw, &event, NULL);
83 	/* Can't read any msg from adminQ */
84 	if (ret) {
85 		PMD_DRV_LOG(DEBUG, "Can't read msg from AQ");
86 		if (ret != IAVF_ERR_ADMIN_QUEUE_NO_WORK)
87 			result = IAVF_MSG_ERR;
88 		return result;
89 	}
90 
91 	opcode = (enum virtchnl_ops)rte_le_to_cpu_32(event.desc.cookie_high);
92 	vf->cmd_retval = (enum virtchnl_status_code)rte_le_to_cpu_32(
93 			event.desc.cookie_low);
94 
95 	PMD_DRV_LOG(DEBUG, "AQ from pf carries opcode %u, retval %d",
96 		    opcode, vf->cmd_retval);
97 
98 	if (opcode == VIRTCHNL_OP_EVENT) {
99 		struct virtchnl_pf_event *vpe =
100 			(struct virtchnl_pf_event *)event.msg_buf;
101 
102 		result = IAVF_MSG_SYS;
103 		switch (vpe->event) {
104 		case VIRTCHNL_EVENT_LINK_CHANGE:
105 			vf->link_up =
106 				vpe->event_data.link_event.link_status;
107 			if (vf->vf_res->vf_cap_flags &
108 				VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
109 				vf->link_speed =
110 				    vpe->event_data.link_event_adv.link_speed;
111 			} else {
112 				enum virtchnl_link_speed speed;
113 				speed = vpe->event_data.link_event.link_speed;
114 				vf->link_speed = iavf_convert_link_speed(speed);
115 			}
116 			iavf_dev_link_update(vf->eth_dev, 0);
117 			PMD_DRV_LOG(INFO, "Link status update:%s",
118 					vf->link_up ? "up" : "down");
119 			break;
120 		case VIRTCHNL_EVENT_RESET_IMPENDING:
121 			vf->vf_reset = true;
122 			PMD_DRV_LOG(INFO, "VF is resetting");
123 			break;
124 		case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
125 			vf->dev_closed = true;
126 			PMD_DRV_LOG(INFO, "PF driver closed");
127 			break;
128 		default:
129 			PMD_DRV_LOG(ERR, "%s: Unknown event %d from pf",
130 					__func__, vpe->event);
131 		}
132 	}  else {
133 		/* async reply msg on command issued by vf previously */
134 		result = IAVF_MSG_CMD;
135 		if (opcode != vf->pend_cmd) {
136 			PMD_DRV_LOG(WARNING, "command mismatch, expect %u, get %u",
137 					vf->pend_cmd, opcode);
138 			result = IAVF_MSG_ERR;
139 		}
140 	}
141 
142 	return result;
143 }
144 
145 static int
iavf_execute_vf_cmd(struct iavf_adapter * adapter,struct iavf_cmd_info * args,int async)146 iavf_execute_vf_cmd(struct iavf_adapter *adapter, struct iavf_cmd_info *args,
147 	int async)
148 {
149 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
150 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
151 	enum iavf_aq_result result;
152 	enum iavf_status ret;
153 	int err = 0;
154 	int i = 0;
155 
156 	if (vf->vf_reset)
157 		return -EIO;
158 
159 
160 	if (async) {
161 		if (_atomic_set_async_response_cmd(vf, args->ops))
162 			return -1;
163 	} else {
164 		if (_atomic_set_cmd(vf, args->ops))
165 			return -1;
166 	}
167 
168 	ret = iavf_aq_send_msg_to_pf(hw, args->ops, IAVF_SUCCESS,
169 				    args->in_args, args->in_args_size, NULL);
170 	if (ret) {
171 		PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
172 		_clear_cmd(vf);
173 		return err;
174 	}
175 
176 	switch (args->ops) {
177 	case VIRTCHNL_OP_RESET_VF:
178 		/*no need to wait for response */
179 		_clear_cmd(vf);
180 		break;
181 	case VIRTCHNL_OP_VERSION:
182 	case VIRTCHNL_OP_GET_VF_RESOURCES:
183 	case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
184 	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
185 		/* for init virtchnl ops, need to poll the response */
186 		do {
187 			result = iavf_read_msg_from_pf(adapter, args->out_size,
188 						   args->out_buffer);
189 			if (result == IAVF_MSG_CMD)
190 				break;
191 			iavf_msec_delay(ASQ_DELAY_MS);
192 		} while (i++ < MAX_TRY_TIMES);
193 		if (i >= MAX_TRY_TIMES ||
194 		    vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
195 			err = -1;
196 			PMD_DRV_LOG(ERR, "No response or return failure (%d)"
197 				    " for cmd %d", vf->cmd_retval, args->ops);
198 		}
199 		_clear_cmd(vf);
200 		break;
201 	case VIRTCHNL_OP_REQUEST_QUEUES:
202 		/*
203 		 * ignore async reply, only wait for system message,
204 		 * vf_reset = true if get VIRTCHNL_EVENT_RESET_IMPENDING,
205 		 * if not, means request queues failed.
206 		 */
207 		do {
208 			result = iavf_read_msg_from_pf(adapter, args->out_size,
209 						   args->out_buffer);
210 			if (result == IAVF_MSG_SYS && vf->vf_reset) {
211 				break;
212 			} else if (result == IAVF_MSG_CMD ||
213 				result == IAVF_MSG_ERR) {
214 				err = -1;
215 				break;
216 			}
217 			iavf_msec_delay(ASQ_DELAY_MS);
218 			/* If don't read msg or read sys event, continue */
219 		} while (i++ < MAX_TRY_TIMES);
220 		if (i >= MAX_TRY_TIMES ||
221 			vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
222 			err = -1;
223 			PMD_DRV_LOG(ERR, "No response or return failure (%d)"
224 				    " for cmd %d", vf->cmd_retval, args->ops);
225 		}
226 		_clear_cmd(vf);
227 		break;
228 	default:
229 		/* For other virtchnl ops in running time,
230 		 * wait for the cmd done flag.
231 		 */
232 		do {
233 			if (vf->pend_cmd == VIRTCHNL_OP_UNKNOWN)
234 				break;
235 			iavf_msec_delay(ASQ_DELAY_MS);
236 			/* If don't read msg or read sys event, continue */
237 		} while (i++ < MAX_TRY_TIMES);
238 
239 		if (i >= MAX_TRY_TIMES) {
240 			PMD_DRV_LOG(ERR, "No response for cmd %d", args->ops);
241 			_clear_cmd(vf);
242 			err = -EIO;
243 		} else if (vf->cmd_retval ==
244 			   VIRTCHNL_STATUS_ERR_NOT_SUPPORTED) {
245 			PMD_DRV_LOG(ERR, "Cmd %d not supported", args->ops);
246 			err = -ENOTSUP;
247 		} else if (vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
248 			PMD_DRV_LOG(ERR, "Return failure %d for cmd %d",
249 				    vf->cmd_retval, args->ops);
250 			err = -EINVAL;
251 		}
252 		break;
253 	}
254 
255 	return err;
256 }
257 
258 static void
iavf_handle_pf_event_msg(struct rte_eth_dev * dev,uint8_t * msg,uint16_t msglen)259 iavf_handle_pf_event_msg(struct rte_eth_dev *dev, uint8_t *msg,
260 			uint16_t msglen)
261 {
262 	struct iavf_adapter *adapter =
263 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
264 	struct iavf_info *vf = &adapter->vf;
265 	struct virtchnl_pf_event *pf_msg =
266 			(struct virtchnl_pf_event *)msg;
267 
268 	if (msglen < sizeof(struct virtchnl_pf_event)) {
269 		PMD_DRV_LOG(DEBUG, "Error event");
270 		return;
271 	}
272 	switch (pf_msg->event) {
273 	case VIRTCHNL_EVENT_RESET_IMPENDING:
274 		PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
275 		vf->vf_reset = true;
276 		rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
277 					      NULL);
278 		break;
279 	case VIRTCHNL_EVENT_LINK_CHANGE:
280 		PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
281 		vf->link_up = pf_msg->event_data.link_event.link_status;
282 		if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
283 			vf->link_speed =
284 				pf_msg->event_data.link_event_adv.link_speed;
285 		} else {
286 			enum virtchnl_link_speed speed;
287 			speed = pf_msg->event_data.link_event.link_speed;
288 			vf->link_speed = iavf_convert_link_speed(speed);
289 		}
290 		iavf_dev_link_update(dev, 0);
291 		rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
292 		break;
293 	case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
294 		PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
295 		break;
296 	default:
297 		PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
298 		break;
299 	}
300 }
301 
302 void
iavf_handle_virtchnl_msg(struct rte_eth_dev * dev)303 iavf_handle_virtchnl_msg(struct rte_eth_dev *dev)
304 {
305 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
306 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
307 	struct iavf_arq_event_info info;
308 	uint16_t pending, aq_opc;
309 	enum virtchnl_ops msg_opc;
310 	enum iavf_status msg_ret;
311 	int ret;
312 
313 	info.buf_len = IAVF_AQ_BUF_SZ;
314 	if (!vf->aq_resp) {
315 		PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
316 		return;
317 	}
318 	info.msg_buf = vf->aq_resp;
319 
320 	pending = 1;
321 	while (pending) {
322 		ret = iavf_clean_arq_element(hw, &info, &pending);
323 
324 		if (ret != IAVF_SUCCESS) {
325 			PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
326 				    "ret: %d", ret);
327 			break;
328 		}
329 		aq_opc = rte_le_to_cpu_16(info.desc.opcode);
330 		/* For the message sent from pf to vf, opcode is stored in
331 		 * cookie_high of struct iavf_aq_desc, while return error code
332 		 * are stored in cookie_low, Which is done by PF driver.
333 		 */
334 		msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
335 						  info.desc.cookie_high);
336 		msg_ret = (enum iavf_status)rte_le_to_cpu_32(
337 						  info.desc.cookie_low);
338 		switch (aq_opc) {
339 		case iavf_aqc_opc_send_msg_to_vf:
340 			if (msg_opc == VIRTCHNL_OP_EVENT) {
341 				iavf_handle_pf_event_msg(dev, info.msg_buf,
342 						info.msg_len);
343 			} else {
344 				/* check for inline IPsec events */
345 				struct inline_ipsec_msg *imsg =
346 					(struct inline_ipsec_msg *)info.msg_buf;
347 				struct rte_eth_event_ipsec_desc desc;
348 				if (msg_opc ==
349 					VIRTCHNL_OP_INLINE_IPSEC_CRYPTO &&
350 					imsg->ipsec_opcode ==
351 						INLINE_IPSEC_OP_EVENT) {
352 					struct virtchnl_ipsec_event *ev =
353 							imsg->ipsec_data.event;
354 					desc.subtype =
355 						RTE_ETH_EVENT_IPSEC_UNKNOWN;
356 					desc.metadata = ev->ipsec_event_data;
357 					rte_eth_dev_callback_process(dev,
358 							RTE_ETH_EVENT_IPSEC,
359 							&desc);
360 					return;
361 				}
362 
363 				/* read message and it's expected one */
364 				if (msg_opc == vf->pend_cmd) {
365 					uint32_t cmd_count =
366 					__atomic_sub_fetch(&vf->pend_cmd_count,
367 							1, __ATOMIC_RELAXED);
368 					if (cmd_count == 0)
369 						_notify_cmd(vf, msg_ret);
370 				} else {
371 					PMD_DRV_LOG(ERR,
372 					"command mismatch, expect %u, get %u",
373 						vf->pend_cmd, msg_opc);
374 				}
375 				PMD_DRV_LOG(DEBUG,
376 				"adminq response is received, opcode = %d",
377 						msg_opc);
378 			}
379 			break;
380 		default:
381 			PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
382 				    aq_opc);
383 			break;
384 		}
385 	}
386 }
387 
388 int
iavf_enable_vlan_strip(struct iavf_adapter * adapter)389 iavf_enable_vlan_strip(struct iavf_adapter *adapter)
390 {
391 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
392 	struct iavf_cmd_info args;
393 	int ret;
394 
395 	memset(&args, 0, sizeof(args));
396 	args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
397 	args.in_args = NULL;
398 	args.in_args_size = 0;
399 	args.out_buffer = vf->aq_resp;
400 	args.out_size = IAVF_AQ_BUF_SZ;
401 	ret = iavf_execute_vf_cmd(adapter, &args, 0);
402 	if (ret)
403 		PMD_DRV_LOG(ERR, "Failed to execute command of"
404 			    " OP_ENABLE_VLAN_STRIPPING");
405 
406 	return ret;
407 }
408 
409 int
iavf_disable_vlan_strip(struct iavf_adapter * adapter)410 iavf_disable_vlan_strip(struct iavf_adapter *adapter)
411 {
412 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
413 	struct iavf_cmd_info args;
414 	int ret;
415 
416 	memset(&args, 0, sizeof(args));
417 	args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
418 	args.in_args = NULL;
419 	args.in_args_size = 0;
420 	args.out_buffer = vf->aq_resp;
421 	args.out_size = IAVF_AQ_BUF_SZ;
422 	ret = iavf_execute_vf_cmd(adapter, &args, 0);
423 	if (ret)
424 		PMD_DRV_LOG(ERR, "Failed to execute command of"
425 			    " OP_DISABLE_VLAN_STRIPPING");
426 
427 	return ret;
428 }
429 
430 #define VIRTCHNL_VERSION_MAJOR_START 1
431 #define VIRTCHNL_VERSION_MINOR_START 1
432 
433 /* Check API version with sync wait until version read from admin queue */
434 int
iavf_check_api_version(struct iavf_adapter * adapter)435 iavf_check_api_version(struct iavf_adapter *adapter)
436 {
437 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
438 	struct virtchnl_version_info version, *pver;
439 	struct iavf_cmd_info args;
440 	int err;
441 
442 	version.major = VIRTCHNL_VERSION_MAJOR;
443 	version.minor = VIRTCHNL_VERSION_MINOR;
444 
445 	args.ops = VIRTCHNL_OP_VERSION;
446 	args.in_args = (uint8_t *)&version;
447 	args.in_args_size = sizeof(version);
448 	args.out_buffer = vf->aq_resp;
449 	args.out_size = IAVF_AQ_BUF_SZ;
450 
451 	err = iavf_execute_vf_cmd(adapter, &args, 0);
452 	if (err) {
453 		PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
454 		return err;
455 	}
456 
457 	pver = (struct virtchnl_version_info *)args.out_buffer;
458 	vf->virtchnl_version = *pver;
459 
460 	if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
461 	    (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
462 	     vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
463 		PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
464 			     " than (%u.%u) to support Adaptive VF",
465 			     VIRTCHNL_VERSION_MAJOR_START,
466 			     VIRTCHNL_VERSION_MAJOR_START);
467 		return -1;
468 	} else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
469 		   (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
470 		    vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
471 		PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
472 			     vf->virtchnl_version.major,
473 			     vf->virtchnl_version.minor,
474 			     VIRTCHNL_VERSION_MAJOR,
475 			     VIRTCHNL_VERSION_MINOR);
476 		return -1;
477 	}
478 
479 	PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
480 	return 0;
481 }
482 
483 int
iavf_get_vf_resource(struct iavf_adapter * adapter)484 iavf_get_vf_resource(struct iavf_adapter *adapter)
485 {
486 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
487 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
488 	struct iavf_cmd_info args;
489 	uint32_t caps, len;
490 	int err, i;
491 
492 	args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
493 	args.out_buffer = vf->aq_resp;
494 	args.out_size = IAVF_AQ_BUF_SZ;
495 
496 	caps = IAVF_BASIC_OFFLOAD_CAPS | VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
497 		VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC |
498 		VIRTCHNL_VF_OFFLOAD_FDIR_PF |
499 		VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF |
500 		VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
501 		VIRTCHNL_VF_OFFLOAD_CRC |
502 		VIRTCHNL_VF_OFFLOAD_VLAN_V2 |
503 		VIRTCHNL_VF_LARGE_NUM_QPAIRS |
504 		VIRTCHNL_VF_OFFLOAD_QOS |
505 		VIRTCHNL_VF_OFFLOAD_INLINE_IPSEC_CRYPTO |
506 		VIRTCHNL_VF_CAP_PTP;
507 
508 	args.in_args = (uint8_t *)&caps;
509 	args.in_args_size = sizeof(caps);
510 
511 	err = iavf_execute_vf_cmd(adapter, &args, 0);
512 
513 	if (err) {
514 		PMD_DRV_LOG(ERR,
515 			    "Failed to execute command of OP_GET_VF_RESOURCE");
516 		return -1;
517 	}
518 
519 	len =  sizeof(struct virtchnl_vf_resource) +
520 		      IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
521 
522 	rte_memcpy(vf->vf_res, args.out_buffer,
523 		   RTE_MIN(args.out_size, len));
524 	/* parse  VF config message back from PF*/
525 	iavf_vf_parse_hw_config(hw, vf->vf_res);
526 	for (i = 0; i < vf->vf_res->num_vsis; i++) {
527 		if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
528 			vf->vsi_res = &vf->vf_res->vsi_res[i];
529 	}
530 
531 	if (!vf->vsi_res) {
532 		PMD_INIT_LOG(ERR, "no LAN VSI found");
533 		return -1;
534 	}
535 
536 	vf->vsi.vsi_id = vf->vsi_res->vsi_id;
537 	vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
538 	vf->vsi.adapter = adapter;
539 
540 	return 0;
541 }
542 
543 int
iavf_get_supported_rxdid(struct iavf_adapter * adapter)544 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
545 {
546 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
547 	struct iavf_cmd_info args;
548 	int ret;
549 
550 	args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
551 	args.in_args = NULL;
552 	args.in_args_size = 0;
553 	args.out_buffer = vf->aq_resp;
554 	args.out_size = IAVF_AQ_BUF_SZ;
555 
556 	ret = iavf_execute_vf_cmd(adapter, &args, 0);
557 	if (ret) {
558 		PMD_DRV_LOG(ERR,
559 			    "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
560 		return ret;
561 	}
562 
563 	vf->supported_rxdid =
564 		((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
565 
566 	return 0;
567 }
568 
569 int
iavf_config_vlan_strip_v2(struct iavf_adapter * adapter,bool enable)570 iavf_config_vlan_strip_v2(struct iavf_adapter *adapter, bool enable)
571 {
572 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
573 	struct virtchnl_vlan_supported_caps *stripping_caps;
574 	struct virtchnl_vlan_setting vlan_strip;
575 	struct iavf_cmd_info args;
576 	uint32_t *ethertype;
577 	int ret;
578 
579 	stripping_caps = &vf->vlan_v2_caps.offloads.stripping_support;
580 
581 	if ((stripping_caps->outer & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
582 	    (stripping_caps->outer & VIRTCHNL_VLAN_TOGGLE))
583 		ethertype = &vlan_strip.outer_ethertype_setting;
584 	else if ((stripping_caps->inner & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
585 		 (stripping_caps->inner & VIRTCHNL_VLAN_TOGGLE))
586 		ethertype = &vlan_strip.inner_ethertype_setting;
587 	else
588 		return -ENOTSUP;
589 
590 	memset(&vlan_strip, 0, sizeof(vlan_strip));
591 	vlan_strip.vport_id = vf->vsi_res->vsi_id;
592 	*ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
593 
594 	args.ops = enable ? VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2 :
595 			    VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2;
596 	args.in_args = (uint8_t *)&vlan_strip;
597 	args.in_args_size = sizeof(vlan_strip);
598 	args.out_buffer = vf->aq_resp;
599 	args.out_size = IAVF_AQ_BUF_SZ;
600 	ret = iavf_execute_vf_cmd(adapter, &args, 0);
601 	if (ret)
602 		PMD_DRV_LOG(ERR, "fail to execute command %s",
603 			    enable ? "VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2" :
604 				     "VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2");
605 
606 	return ret;
607 }
608 
609 int
iavf_config_vlan_insert_v2(struct iavf_adapter * adapter,bool enable)610 iavf_config_vlan_insert_v2(struct iavf_adapter *adapter, bool enable)
611 {
612 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
613 	struct virtchnl_vlan_supported_caps *insertion_caps;
614 	struct virtchnl_vlan_setting vlan_insert;
615 	struct iavf_cmd_info args;
616 	uint32_t *ethertype;
617 	int ret;
618 
619 	insertion_caps = &vf->vlan_v2_caps.offloads.insertion_support;
620 
621 	if ((insertion_caps->outer & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
622 	    (insertion_caps->outer & VIRTCHNL_VLAN_TOGGLE))
623 		ethertype = &vlan_insert.outer_ethertype_setting;
624 	else if ((insertion_caps->inner & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
625 		 (insertion_caps->inner & VIRTCHNL_VLAN_TOGGLE))
626 		ethertype = &vlan_insert.inner_ethertype_setting;
627 	else
628 		return -ENOTSUP;
629 
630 	memset(&vlan_insert, 0, sizeof(vlan_insert));
631 	vlan_insert.vport_id = vf->vsi_res->vsi_id;
632 	*ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
633 
634 	args.ops = enable ? VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2 :
635 			    VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2;
636 	args.in_args = (uint8_t *)&vlan_insert;
637 	args.in_args_size = sizeof(vlan_insert);
638 	args.out_buffer = vf->aq_resp;
639 	args.out_size = IAVF_AQ_BUF_SZ;
640 	ret = iavf_execute_vf_cmd(adapter, &args, 0);
641 	if (ret)
642 		PMD_DRV_LOG(ERR, "fail to execute command %s",
643 			    enable ? "VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2" :
644 				     "VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2");
645 
646 	return ret;
647 }
648 
649 int
iavf_add_del_vlan_v2(struct iavf_adapter * adapter,uint16_t vlanid,bool add)650 iavf_add_del_vlan_v2(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
651 {
652 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
653 	struct virtchnl_vlan_supported_caps *supported_caps;
654 	struct virtchnl_vlan_filter_list_v2 vlan_filter;
655 	struct virtchnl_vlan *vlan_setting;
656 	struct iavf_cmd_info args;
657 	uint32_t filtering_caps;
658 	int err;
659 
660 	supported_caps = &vf->vlan_v2_caps.filtering.filtering_support;
661 	if (supported_caps->outer) {
662 		filtering_caps = supported_caps->outer;
663 		vlan_setting = &vlan_filter.filters[0].outer;
664 	} else {
665 		filtering_caps = supported_caps->inner;
666 		vlan_setting = &vlan_filter.filters[0].inner;
667 	}
668 
669 	if (!(filtering_caps & VIRTCHNL_VLAN_ETHERTYPE_8100))
670 		return -ENOTSUP;
671 
672 	memset(&vlan_filter, 0, sizeof(vlan_filter));
673 	vlan_filter.vport_id = vf->vsi_res->vsi_id;
674 	vlan_filter.num_elements = 1;
675 	vlan_setting->tpid = RTE_ETHER_TYPE_VLAN;
676 	vlan_setting->tci = vlanid;
677 
678 	args.ops = add ? VIRTCHNL_OP_ADD_VLAN_V2 : VIRTCHNL_OP_DEL_VLAN_V2;
679 	args.in_args = (uint8_t *)&vlan_filter;
680 	args.in_args_size = sizeof(vlan_filter);
681 	args.out_buffer = vf->aq_resp;
682 	args.out_size = IAVF_AQ_BUF_SZ;
683 	err = iavf_execute_vf_cmd(adapter, &args, 0);
684 	if (err)
685 		PMD_DRV_LOG(ERR, "fail to execute command %s",
686 			    add ? "OP_ADD_VLAN_V2" :  "OP_DEL_VLAN_V2");
687 
688 	return err;
689 }
690 
691 int
iavf_get_vlan_offload_caps_v2(struct iavf_adapter * adapter)692 iavf_get_vlan_offload_caps_v2(struct iavf_adapter *adapter)
693 {
694 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
695 	struct iavf_cmd_info args;
696 	int ret;
697 
698 	args.ops = VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS;
699 	args.in_args = NULL;
700 	args.in_args_size = 0;
701 	args.out_buffer = vf->aq_resp;
702 	args.out_size = IAVF_AQ_BUF_SZ;
703 
704 	ret = iavf_execute_vf_cmd(adapter, &args, 0);
705 	if (ret) {
706 		PMD_DRV_LOG(ERR,
707 			    "Failed to execute command of VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS");
708 		return ret;
709 	}
710 
711 	rte_memcpy(&vf->vlan_v2_caps, vf->aq_resp, sizeof(vf->vlan_v2_caps));
712 
713 	return 0;
714 }
715 
716 int
iavf_enable_queues(struct iavf_adapter * adapter)717 iavf_enable_queues(struct iavf_adapter *adapter)
718 {
719 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
720 	struct virtchnl_queue_select queue_select;
721 	struct iavf_cmd_info args;
722 	int err;
723 
724 	memset(&queue_select, 0, sizeof(queue_select));
725 	queue_select.vsi_id = vf->vsi_res->vsi_id;
726 
727 	queue_select.rx_queues = BIT(adapter->dev_data->nb_rx_queues) - 1;
728 	queue_select.tx_queues = BIT(adapter->dev_data->nb_tx_queues) - 1;
729 
730 	args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
731 	args.in_args = (u8 *)&queue_select;
732 	args.in_args_size = sizeof(queue_select);
733 	args.out_buffer = vf->aq_resp;
734 	args.out_size = IAVF_AQ_BUF_SZ;
735 	err = iavf_execute_vf_cmd(adapter, &args, 0);
736 	if (err) {
737 		PMD_DRV_LOG(ERR,
738 			    "Failed to execute command of OP_ENABLE_QUEUES");
739 		return err;
740 	}
741 	return 0;
742 }
743 
744 int
iavf_disable_queues(struct iavf_adapter * adapter)745 iavf_disable_queues(struct iavf_adapter *adapter)
746 {
747 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
748 	struct virtchnl_queue_select queue_select;
749 	struct iavf_cmd_info args;
750 	int err;
751 
752 	memset(&queue_select, 0, sizeof(queue_select));
753 	queue_select.vsi_id = vf->vsi_res->vsi_id;
754 
755 	queue_select.rx_queues = BIT(adapter->dev_data->nb_rx_queues) - 1;
756 	queue_select.tx_queues = BIT(adapter->dev_data->nb_tx_queues) - 1;
757 
758 	args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
759 	args.in_args = (u8 *)&queue_select;
760 	args.in_args_size = sizeof(queue_select);
761 	args.out_buffer = vf->aq_resp;
762 	args.out_size = IAVF_AQ_BUF_SZ;
763 	err = iavf_execute_vf_cmd(adapter, &args, 0);
764 	if (err) {
765 		PMD_DRV_LOG(ERR,
766 			    "Failed to execute command of OP_DISABLE_QUEUES");
767 		return err;
768 	}
769 	return 0;
770 }
771 
772 int
iavf_switch_queue(struct iavf_adapter * adapter,uint16_t qid,bool rx,bool on)773 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
774 		 bool rx, bool on)
775 {
776 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
777 	struct virtchnl_queue_select queue_select;
778 	struct iavf_cmd_info args;
779 	int err;
780 
781 	memset(&queue_select, 0, sizeof(queue_select));
782 	queue_select.vsi_id = vf->vsi_res->vsi_id;
783 	if (rx)
784 		queue_select.rx_queues |= 1 << qid;
785 	else
786 		queue_select.tx_queues |= 1 << qid;
787 
788 	if (on)
789 		args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
790 	else
791 		args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
792 	args.in_args = (u8 *)&queue_select;
793 	args.in_args_size = sizeof(queue_select);
794 	args.out_buffer = vf->aq_resp;
795 	args.out_size = IAVF_AQ_BUF_SZ;
796 	err = iavf_execute_vf_cmd(adapter, &args, 0);
797 	if (err)
798 		PMD_DRV_LOG(ERR, "Failed to execute command of %s",
799 			    on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
800 	return err;
801 }
802 
803 int
iavf_enable_queues_lv(struct iavf_adapter * adapter)804 iavf_enable_queues_lv(struct iavf_adapter *adapter)
805 {
806 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
807 	struct virtchnl_del_ena_dis_queues *queue_select;
808 	struct virtchnl_queue_chunk *queue_chunk;
809 	struct iavf_cmd_info args;
810 	int err, len;
811 
812 	len = sizeof(struct virtchnl_del_ena_dis_queues) +
813 		  sizeof(struct virtchnl_queue_chunk) *
814 		  (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
815 	queue_select = rte_zmalloc("queue_select", len, 0);
816 	if (!queue_select)
817 		return -ENOMEM;
818 
819 	queue_chunk = queue_select->chunks.chunks;
820 	queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
821 	queue_select->vport_id = vf->vsi_res->vsi_id;
822 
823 	queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
824 	queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
825 	queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
826 		adapter->dev_data->nb_tx_queues;
827 
828 	queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
829 	queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
830 	queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
831 		adapter->dev_data->nb_rx_queues;
832 
833 	args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
834 	args.in_args = (u8 *)queue_select;
835 	args.in_args_size = len;
836 	args.out_buffer = vf->aq_resp;
837 	args.out_size = IAVF_AQ_BUF_SZ;
838 	err = iavf_execute_vf_cmd(adapter, &args, 0);
839 	if (err)
840 		PMD_DRV_LOG(ERR,
841 			    "Failed to execute command of OP_ENABLE_QUEUES_V2");
842 
843 	rte_free(queue_select);
844 	return err;
845 }
846 
847 int
iavf_disable_queues_lv(struct iavf_adapter * adapter)848 iavf_disable_queues_lv(struct iavf_adapter *adapter)
849 {
850 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
851 	struct virtchnl_del_ena_dis_queues *queue_select;
852 	struct virtchnl_queue_chunk *queue_chunk;
853 	struct iavf_cmd_info args;
854 	int err, len;
855 
856 	len = sizeof(struct virtchnl_del_ena_dis_queues) +
857 		  sizeof(struct virtchnl_queue_chunk) *
858 		  (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
859 	queue_select = rte_zmalloc("queue_select", len, 0);
860 	if (!queue_select)
861 		return -ENOMEM;
862 
863 	queue_chunk = queue_select->chunks.chunks;
864 	queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
865 	queue_select->vport_id = vf->vsi_res->vsi_id;
866 
867 	queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
868 	queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
869 	queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
870 		adapter->dev_data->nb_tx_queues;
871 
872 	queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
873 	queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
874 	queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
875 		adapter->dev_data->nb_rx_queues;
876 
877 	args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
878 	args.in_args = (u8 *)queue_select;
879 	args.in_args_size = len;
880 	args.out_buffer = vf->aq_resp;
881 	args.out_size = IAVF_AQ_BUF_SZ;
882 	err = iavf_execute_vf_cmd(adapter, &args, 0);
883 	if (err)
884 		PMD_DRV_LOG(ERR,
885 			    "Failed to execute command of OP_DISABLE_QUEUES_V2");
886 
887 	rte_free(queue_select);
888 	return err;
889 }
890 
891 int
iavf_switch_queue_lv(struct iavf_adapter * adapter,uint16_t qid,bool rx,bool on)892 iavf_switch_queue_lv(struct iavf_adapter *adapter, uint16_t qid,
893 		 bool rx, bool on)
894 {
895 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
896 	struct virtchnl_del_ena_dis_queues *queue_select;
897 	struct virtchnl_queue_chunk *queue_chunk;
898 	struct iavf_cmd_info args;
899 	int err, len;
900 
901 	len = sizeof(struct virtchnl_del_ena_dis_queues);
902 	queue_select = rte_zmalloc("queue_select", len, 0);
903 	if (!queue_select)
904 		return -ENOMEM;
905 
906 	queue_chunk = queue_select->chunks.chunks;
907 	queue_select->chunks.num_chunks = 1;
908 	queue_select->vport_id = vf->vsi_res->vsi_id;
909 
910 	if (rx) {
911 		queue_chunk->type = VIRTCHNL_QUEUE_TYPE_RX;
912 		queue_chunk->start_queue_id = qid;
913 		queue_chunk->num_queues = 1;
914 	} else {
915 		queue_chunk->type = VIRTCHNL_QUEUE_TYPE_TX;
916 		queue_chunk->start_queue_id = qid;
917 		queue_chunk->num_queues = 1;
918 	}
919 
920 	if (on)
921 		args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
922 	else
923 		args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
924 	args.in_args = (u8 *)queue_select;
925 	args.in_args_size = len;
926 	args.out_buffer = vf->aq_resp;
927 	args.out_size = IAVF_AQ_BUF_SZ;
928 	err = iavf_execute_vf_cmd(adapter, &args, 0);
929 	if (err)
930 		PMD_DRV_LOG(ERR, "Failed to execute command of %s",
931 			    on ? "OP_ENABLE_QUEUES_V2" : "OP_DISABLE_QUEUES_V2");
932 
933 	rte_free(queue_select);
934 	return err;
935 }
936 
937 int
iavf_configure_rss_lut(struct iavf_adapter * adapter)938 iavf_configure_rss_lut(struct iavf_adapter *adapter)
939 {
940 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
941 	struct virtchnl_rss_lut *rss_lut;
942 	struct iavf_cmd_info args;
943 	int len, err = 0;
944 
945 	len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
946 	rss_lut = rte_zmalloc("rss_lut", len, 0);
947 	if (!rss_lut)
948 		return -ENOMEM;
949 
950 	rss_lut->vsi_id = vf->vsi_res->vsi_id;
951 	rss_lut->lut_entries = vf->vf_res->rss_lut_size;
952 	rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
953 
954 	args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
955 	args.in_args = (u8 *)rss_lut;
956 	args.in_args_size = len;
957 	args.out_buffer = vf->aq_resp;
958 	args.out_size = IAVF_AQ_BUF_SZ;
959 
960 	err = iavf_execute_vf_cmd(adapter, &args, 0);
961 	if (err)
962 		PMD_DRV_LOG(ERR,
963 			    "Failed to execute command of OP_CONFIG_RSS_LUT");
964 
965 	rte_free(rss_lut);
966 	return err;
967 }
968 
969 int
iavf_configure_rss_key(struct iavf_adapter * adapter)970 iavf_configure_rss_key(struct iavf_adapter *adapter)
971 {
972 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
973 	struct virtchnl_rss_key *rss_key;
974 	struct iavf_cmd_info args;
975 	int len, err = 0;
976 
977 	len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
978 	rss_key = rte_zmalloc("rss_key", len, 0);
979 	if (!rss_key)
980 		return -ENOMEM;
981 
982 	rss_key->vsi_id = vf->vsi_res->vsi_id;
983 	rss_key->key_len = vf->vf_res->rss_key_size;
984 	rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
985 
986 	args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
987 	args.in_args = (u8 *)rss_key;
988 	args.in_args_size = len;
989 	args.out_buffer = vf->aq_resp;
990 	args.out_size = IAVF_AQ_BUF_SZ;
991 
992 	err = iavf_execute_vf_cmd(adapter, &args, 0);
993 	if (err)
994 		PMD_DRV_LOG(ERR,
995 			    "Failed to execute command of OP_CONFIG_RSS_KEY");
996 
997 	rte_free(rss_key);
998 	return err;
999 }
1000 
1001 int
iavf_configure_queues(struct iavf_adapter * adapter,uint16_t num_queue_pairs,uint16_t index)1002 iavf_configure_queues(struct iavf_adapter *adapter,
1003 		uint16_t num_queue_pairs, uint16_t index)
1004 {
1005 	struct iavf_rx_queue **rxq =
1006 		(struct iavf_rx_queue **)adapter->dev_data->rx_queues;
1007 	struct iavf_tx_queue **txq =
1008 		(struct iavf_tx_queue **)adapter->dev_data->tx_queues;
1009 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1010 	struct virtchnl_vsi_queue_config_info *vc_config;
1011 	struct virtchnl_queue_pair_info *vc_qp;
1012 	struct iavf_cmd_info args;
1013 	uint16_t i, size;
1014 	int err;
1015 
1016 	size = sizeof(*vc_config) +
1017 	       sizeof(vc_config->qpair[0]) * num_queue_pairs;
1018 	vc_config = rte_zmalloc("cfg_queue", size, 0);
1019 	if (!vc_config)
1020 		return -ENOMEM;
1021 
1022 	vc_config->vsi_id = vf->vsi_res->vsi_id;
1023 	vc_config->num_queue_pairs = num_queue_pairs;
1024 
1025 	for (i = index, vc_qp = vc_config->qpair;
1026 		 i < index + num_queue_pairs;
1027 	     i++, vc_qp++) {
1028 		vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
1029 		vc_qp->txq.queue_id = i;
1030 
1031 		/* Virtchnnl configure tx queues by pairs */
1032 		if (i < adapter->dev_data->nb_tx_queues) {
1033 			vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
1034 			vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
1035 		}
1036 
1037 		vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
1038 		vc_qp->rxq.queue_id = i;
1039 		vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
1040 
1041 		if (i >= adapter->dev_data->nb_rx_queues)
1042 			continue;
1043 
1044 		/* Virtchnnl configure rx queues by pairs */
1045 		vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
1046 		vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
1047 		vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
1048 		vc_qp->rxq.crc_disable = rxq[i]->crc_len != 0 ? 1 : 0;
1049 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
1050 		if (vf->vf_res->vf_cap_flags &
1051 		    VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1052 			if (vf->supported_rxdid & BIT(rxq[i]->rxdid)) {
1053 				vc_qp->rxq.rxdid = rxq[i]->rxdid;
1054 				PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
1055 					    vc_qp->rxq.rxdid, i);
1056 			} else {
1057 				PMD_DRV_LOG(NOTICE, "RXDID[%d] is not supported, "
1058 					    "request default RXDID[%d] in Queue[%d]",
1059 					    rxq[i]->rxdid, IAVF_RXDID_LEGACY_1, i);
1060 				vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
1061 			}
1062 
1063 			if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_PTP &&
1064 			    vf->ptp_caps & VIRTCHNL_1588_PTP_CAP_RX_TSTAMP)
1065 				vc_qp->rxq.flags |= VIRTCHNL_PTP_RX_TSTAMP;
1066 		}
1067 #else
1068 		if (vf->vf_res->vf_cap_flags &
1069 			VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
1070 			vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
1071 			vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
1072 			PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
1073 				    vc_qp->rxq.rxdid, i);
1074 		} else {
1075 			PMD_DRV_LOG(ERR, "RXDID[%d] is not supported",
1076 				    IAVF_RXDID_LEGACY_0);
1077 			return -1;
1078 		}
1079 #endif
1080 	}
1081 
1082 	memset(&args, 0, sizeof(args));
1083 	args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
1084 	args.in_args = (uint8_t *)vc_config;
1085 	args.in_args_size = size;
1086 	args.out_buffer = vf->aq_resp;
1087 	args.out_size = IAVF_AQ_BUF_SZ;
1088 
1089 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1090 	if (err)
1091 		PMD_DRV_LOG(ERR, "Failed to execute command of"
1092 			    " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
1093 
1094 	rte_free(vc_config);
1095 	return err;
1096 }
1097 
1098 int
iavf_config_irq_map(struct iavf_adapter * adapter)1099 iavf_config_irq_map(struct iavf_adapter *adapter)
1100 {
1101 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1102 	struct virtchnl_irq_map_info *map_info;
1103 	struct virtchnl_vector_map *vecmap;
1104 	struct iavf_cmd_info args;
1105 	int len, i, err;
1106 
1107 	len = sizeof(struct virtchnl_irq_map_info) +
1108 	      sizeof(struct virtchnl_vector_map) * vf->nb_msix;
1109 
1110 	map_info = rte_zmalloc("map_info", len, 0);
1111 	if (!map_info)
1112 		return -ENOMEM;
1113 
1114 	map_info->num_vectors = vf->nb_msix;
1115 	for (i = 0; i < adapter->dev_data->nb_rx_queues; i++) {
1116 		vecmap =
1117 		    &map_info->vecmap[vf->qv_map[i].vector_id - vf->msix_base];
1118 		vecmap->vsi_id = vf->vsi_res->vsi_id;
1119 		vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
1120 		vecmap->vector_id = vf->qv_map[i].vector_id;
1121 		vecmap->txq_map = 0;
1122 		vecmap->rxq_map |= 1 << vf->qv_map[i].queue_id;
1123 	}
1124 
1125 	args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
1126 	args.in_args = (u8 *)map_info;
1127 	args.in_args_size = len;
1128 	args.out_buffer = vf->aq_resp;
1129 	args.out_size = IAVF_AQ_BUF_SZ;
1130 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1131 	if (err)
1132 		PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
1133 
1134 	rte_free(map_info);
1135 	return err;
1136 }
1137 
1138 int
iavf_config_irq_map_lv(struct iavf_adapter * adapter,uint16_t num,uint16_t index)1139 iavf_config_irq_map_lv(struct iavf_adapter *adapter, uint16_t num,
1140 		uint16_t index)
1141 {
1142 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1143 	struct virtchnl_queue_vector_maps *map_info;
1144 	struct virtchnl_queue_vector *qv_maps;
1145 	struct iavf_cmd_info args;
1146 	int len, i, err;
1147 	int count = 0;
1148 
1149 	len = sizeof(struct virtchnl_queue_vector_maps) +
1150 	      sizeof(struct virtchnl_queue_vector) * (num - 1);
1151 
1152 	map_info = rte_zmalloc("map_info", len, 0);
1153 	if (!map_info)
1154 		return -ENOMEM;
1155 
1156 	map_info->vport_id = vf->vsi_res->vsi_id;
1157 	map_info->num_qv_maps = num;
1158 	for (i = index; i < index + map_info->num_qv_maps; i++) {
1159 		qv_maps = &map_info->qv_maps[count++];
1160 		qv_maps->itr_idx = VIRTCHNL_ITR_IDX_0;
1161 		qv_maps->queue_type = VIRTCHNL_QUEUE_TYPE_RX;
1162 		qv_maps->queue_id = vf->qv_map[i].queue_id;
1163 		qv_maps->vector_id = vf->qv_map[i].vector_id;
1164 	}
1165 
1166 	args.ops = VIRTCHNL_OP_MAP_QUEUE_VECTOR;
1167 	args.in_args = (u8 *)map_info;
1168 	args.in_args_size = len;
1169 	args.out_buffer = vf->aq_resp;
1170 	args.out_size = IAVF_AQ_BUF_SZ;
1171 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1172 	if (err)
1173 		PMD_DRV_LOG(ERR, "fail to execute command OP_MAP_QUEUE_VECTOR");
1174 
1175 	rte_free(map_info);
1176 	return err;
1177 }
1178 
1179 void
iavf_add_del_all_mac_addr(struct iavf_adapter * adapter,bool add)1180 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
1181 {
1182 	struct virtchnl_ether_addr_list *list;
1183 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1184 	struct rte_ether_addr *addr;
1185 	struct iavf_cmd_info args;
1186 	int len, err, i, j;
1187 	int next_begin = 0;
1188 	int begin = 0;
1189 
1190 	do {
1191 		j = 0;
1192 		len = sizeof(struct virtchnl_ether_addr_list);
1193 		for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
1194 			addr = &adapter->dev_data->mac_addrs[i];
1195 			if (rte_is_zero_ether_addr(addr))
1196 				continue;
1197 			len += sizeof(struct virtchnl_ether_addr);
1198 			if (len >= IAVF_AQ_BUF_SZ) {
1199 				next_begin = i + 1;
1200 				break;
1201 			}
1202 		}
1203 
1204 		list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
1205 		if (!list) {
1206 			PMD_DRV_LOG(ERR, "fail to allocate memory");
1207 			return;
1208 		}
1209 
1210 		for (i = begin; i < next_begin; i++) {
1211 			addr = &adapter->dev_data->mac_addrs[i];
1212 			if (rte_is_zero_ether_addr(addr))
1213 				continue;
1214 			rte_memcpy(list->list[j].addr, addr->addr_bytes,
1215 				   sizeof(addr->addr_bytes));
1216 			list->list[j].type = (j == 0 ?
1217 					      VIRTCHNL_ETHER_ADDR_PRIMARY :
1218 					      VIRTCHNL_ETHER_ADDR_EXTRA);
1219 			PMD_DRV_LOG(DEBUG, "add/rm mac:" RTE_ETHER_ADDR_PRT_FMT,
1220 				    RTE_ETHER_ADDR_BYTES(addr));
1221 			j++;
1222 		}
1223 		list->vsi_id = vf->vsi_res->vsi_id;
1224 		list->num_elements = j;
1225 		args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
1226 			   VIRTCHNL_OP_DEL_ETH_ADDR;
1227 		args.in_args = (uint8_t *)list;
1228 		args.in_args_size = len;
1229 		args.out_buffer = vf->aq_resp;
1230 		args.out_size = IAVF_AQ_BUF_SZ;
1231 		err = iavf_execute_vf_cmd(adapter, &args, 0);
1232 		if (err)
1233 			PMD_DRV_LOG(ERR, "fail to execute command %s",
1234 				    add ? "OP_ADD_ETHER_ADDRESS" :
1235 				    "OP_DEL_ETHER_ADDRESS");
1236 		rte_free(list);
1237 		begin = next_begin;
1238 	} while (begin < IAVF_NUM_MACADDR_MAX);
1239 }
1240 
1241 int
iavf_query_stats(struct iavf_adapter * adapter,struct virtchnl_eth_stats ** pstats)1242 iavf_query_stats(struct iavf_adapter *adapter,
1243 		struct virtchnl_eth_stats **pstats)
1244 {
1245 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1246 	struct virtchnl_queue_select q_stats;
1247 	struct iavf_cmd_info args;
1248 	int err;
1249 
1250 	memset(&q_stats, 0, sizeof(q_stats));
1251 	q_stats.vsi_id = vf->vsi_res->vsi_id;
1252 	args.ops = VIRTCHNL_OP_GET_STATS;
1253 	args.in_args = (uint8_t *)&q_stats;
1254 	args.in_args_size = sizeof(q_stats);
1255 	args.out_buffer = vf->aq_resp;
1256 	args.out_size = IAVF_AQ_BUF_SZ;
1257 
1258 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1259 	if (err) {
1260 		PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
1261 		*pstats = NULL;
1262 		return err;
1263 	}
1264 	*pstats = (struct virtchnl_eth_stats *)args.out_buffer;
1265 	return 0;
1266 }
1267 
1268 int
iavf_config_promisc(struct iavf_adapter * adapter,bool enable_unicast,bool enable_multicast)1269 iavf_config_promisc(struct iavf_adapter *adapter,
1270 		   bool enable_unicast,
1271 		   bool enable_multicast)
1272 {
1273 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1274 	struct virtchnl_promisc_info promisc;
1275 	struct iavf_cmd_info args;
1276 	int err;
1277 
1278 	promisc.flags = 0;
1279 	promisc.vsi_id = vf->vsi_res->vsi_id;
1280 
1281 	if (enable_unicast)
1282 		promisc.flags |= FLAG_VF_UNICAST_PROMISC;
1283 
1284 	if (enable_multicast)
1285 		promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
1286 
1287 	args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
1288 	args.in_args = (uint8_t *)&promisc;
1289 	args.in_args_size = sizeof(promisc);
1290 	args.out_buffer = vf->aq_resp;
1291 	args.out_size = IAVF_AQ_BUF_SZ;
1292 
1293 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1294 
1295 	if (err) {
1296 		PMD_DRV_LOG(ERR,
1297 			    "fail to execute command CONFIG_PROMISCUOUS_MODE");
1298 
1299 		if (err == -ENOTSUP)
1300 			return err;
1301 
1302 		return -EAGAIN;
1303 	}
1304 
1305 	vf->promisc_unicast_enabled = enable_unicast;
1306 	vf->promisc_multicast_enabled = enable_multicast;
1307 	return 0;
1308 }
1309 
1310 int
iavf_add_del_eth_addr(struct iavf_adapter * adapter,struct rte_ether_addr * addr,bool add,uint8_t type)1311 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
1312 		     bool add, uint8_t type)
1313 {
1314 	struct virtchnl_ether_addr_list *list;
1315 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1316 	uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1317 			   sizeof(struct virtchnl_ether_addr)];
1318 	struct iavf_cmd_info args;
1319 	int err;
1320 
1321 	list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1322 	list->vsi_id = vf->vsi_res->vsi_id;
1323 	list->num_elements = 1;
1324 	list->list[0].type = type;
1325 	rte_memcpy(list->list[0].addr, addr->addr_bytes,
1326 		   sizeof(addr->addr_bytes));
1327 
1328 	args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1329 	args.in_args = cmd_buffer;
1330 	args.in_args_size = sizeof(cmd_buffer);
1331 	args.out_buffer = vf->aq_resp;
1332 	args.out_size = IAVF_AQ_BUF_SZ;
1333 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1334 	if (err)
1335 		PMD_DRV_LOG(ERR, "fail to execute command %s",
1336 			    add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
1337 	return err;
1338 }
1339 
1340 int
iavf_add_del_vlan(struct iavf_adapter * adapter,uint16_t vlanid,bool add)1341 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
1342 {
1343 	struct virtchnl_vlan_filter_list *vlan_list;
1344 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1345 	uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
1346 							sizeof(uint16_t)];
1347 	struct iavf_cmd_info args;
1348 	int err;
1349 
1350 	vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
1351 	vlan_list->vsi_id = vf->vsi_res->vsi_id;
1352 	vlan_list->num_elements = 1;
1353 	vlan_list->vlan_id[0] = vlanid;
1354 
1355 	args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
1356 	args.in_args = cmd_buffer;
1357 	args.in_args_size = sizeof(cmd_buffer);
1358 	args.out_buffer = vf->aq_resp;
1359 	args.out_size = IAVF_AQ_BUF_SZ;
1360 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1361 	if (err)
1362 		PMD_DRV_LOG(ERR, "fail to execute command %s",
1363 			    add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
1364 
1365 	return err;
1366 }
1367 
1368 int
iavf_fdir_add(struct iavf_adapter * adapter,struct iavf_fdir_conf * filter)1369 iavf_fdir_add(struct iavf_adapter *adapter,
1370 	struct iavf_fdir_conf *filter)
1371 {
1372 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1373 	struct virtchnl_fdir_add *fdir_ret;
1374 
1375 	struct iavf_cmd_info args;
1376 	int err;
1377 
1378 	filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1379 	filter->add_fltr.validate_only = 0;
1380 
1381 	args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1382 	args.in_args = (uint8_t *)(&filter->add_fltr);
1383 	args.in_args_size = sizeof(*(&filter->add_fltr));
1384 	args.out_buffer = vf->aq_resp;
1385 	args.out_size = IAVF_AQ_BUF_SZ;
1386 
1387 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1388 	if (err) {
1389 		PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_FDIR_FILTER");
1390 		return err;
1391 	}
1392 
1393 	fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1394 	filter->flow_id = fdir_ret->flow_id;
1395 
1396 	if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1397 		PMD_DRV_LOG(INFO,
1398 			"Succeed in adding rule request by PF");
1399 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE) {
1400 		PMD_DRV_LOG(ERR,
1401 			"Failed to add rule request due to no hw resource");
1402 		return -1;
1403 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_EXIST) {
1404 		PMD_DRV_LOG(ERR,
1405 			"Failed to add rule request due to the rule is already existed");
1406 		return -1;
1407 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT) {
1408 		PMD_DRV_LOG(ERR,
1409 			"Failed to add rule request due to the rule is conflict with existing rule");
1410 		return -1;
1411 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1412 		PMD_DRV_LOG(ERR,
1413 			"Failed to add rule request due to the hw doesn't support");
1414 		return -1;
1415 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1416 		PMD_DRV_LOG(ERR,
1417 			"Failed to add rule request due to time out for programming");
1418 		return -1;
1419 	} else {
1420 		PMD_DRV_LOG(ERR,
1421 			"Failed to add rule request due to other reasons");
1422 		return -1;
1423 	}
1424 
1425 	return 0;
1426 };
1427 
1428 int
iavf_fdir_del(struct iavf_adapter * adapter,struct iavf_fdir_conf * filter)1429 iavf_fdir_del(struct iavf_adapter *adapter,
1430 	struct iavf_fdir_conf *filter)
1431 {
1432 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1433 	struct virtchnl_fdir_del *fdir_ret;
1434 
1435 	struct iavf_cmd_info args;
1436 	int err;
1437 
1438 	filter->del_fltr.vsi_id = vf->vsi_res->vsi_id;
1439 	filter->del_fltr.flow_id = filter->flow_id;
1440 
1441 	args.ops = VIRTCHNL_OP_DEL_FDIR_FILTER;
1442 	args.in_args = (uint8_t *)(&filter->del_fltr);
1443 	args.in_args_size = sizeof(filter->del_fltr);
1444 	args.out_buffer = vf->aq_resp;
1445 	args.out_size = IAVF_AQ_BUF_SZ;
1446 
1447 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1448 	if (err) {
1449 		PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_FDIR_FILTER");
1450 		return err;
1451 	}
1452 
1453 	fdir_ret = (struct virtchnl_fdir_del *)args.out_buffer;
1454 
1455 	if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1456 		PMD_DRV_LOG(INFO,
1457 			"Succeed in deleting rule request by PF");
1458 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST) {
1459 		PMD_DRV_LOG(ERR,
1460 			"Failed to delete rule request due to this rule doesn't exist");
1461 		return -1;
1462 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1463 		PMD_DRV_LOG(ERR,
1464 			"Failed to delete rule request due to time out for programming");
1465 		return -1;
1466 	} else {
1467 		PMD_DRV_LOG(ERR,
1468 			"Failed to delete rule request due to other reasons");
1469 		return -1;
1470 	}
1471 
1472 	return 0;
1473 };
1474 
1475 int
iavf_fdir_check(struct iavf_adapter * adapter,struct iavf_fdir_conf * filter)1476 iavf_fdir_check(struct iavf_adapter *adapter,
1477 		struct iavf_fdir_conf *filter)
1478 {
1479 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1480 	struct virtchnl_fdir_add *fdir_ret;
1481 
1482 	struct iavf_cmd_info args;
1483 	int err;
1484 
1485 	filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1486 	filter->add_fltr.validate_only = 1;
1487 
1488 	args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1489 	args.in_args = (uint8_t *)(&filter->add_fltr);
1490 	args.in_args_size = sizeof(*(&filter->add_fltr));
1491 	args.out_buffer = vf->aq_resp;
1492 	args.out_size = IAVF_AQ_BUF_SZ;
1493 
1494 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1495 	if (err) {
1496 		PMD_DRV_LOG(ERR, "fail to check flow director rule");
1497 		return err;
1498 	}
1499 
1500 	fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1501 
1502 	if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1503 		PMD_DRV_LOG(INFO,
1504 			"Succeed in checking rule request by PF");
1505 	} else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1506 		PMD_DRV_LOG(ERR,
1507 			"Failed to check rule request due to parameters validation"
1508 			" or HW doesn't support");
1509 		return -1;
1510 	} else {
1511 		PMD_DRV_LOG(ERR,
1512 			"Failed to check rule request due to other reasons");
1513 		return -1;
1514 	}
1515 
1516 	return 0;
1517 }
1518 
1519 int
iavf_add_del_rss_cfg(struct iavf_adapter * adapter,struct virtchnl_rss_cfg * rss_cfg,bool add)1520 iavf_add_del_rss_cfg(struct iavf_adapter *adapter,
1521 		     struct virtchnl_rss_cfg *rss_cfg, bool add)
1522 {
1523 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1524 	struct iavf_cmd_info args;
1525 	int err;
1526 
1527 	memset(&args, 0, sizeof(args));
1528 	args.ops = add ? VIRTCHNL_OP_ADD_RSS_CFG :
1529 		VIRTCHNL_OP_DEL_RSS_CFG;
1530 	args.in_args = (u8 *)rss_cfg;
1531 	args.in_args_size = sizeof(*rss_cfg);
1532 	args.out_buffer = vf->aq_resp;
1533 	args.out_size = IAVF_AQ_BUF_SZ;
1534 
1535 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1536 	if (err)
1537 		PMD_DRV_LOG(ERR,
1538 			    "Failed to execute command of %s",
1539 			    add ? "OP_ADD_RSS_CFG" :
1540 			    "OP_DEL_RSS_INPUT_CFG");
1541 
1542 	return err;
1543 }
1544 
1545 int
iavf_get_hena_caps(struct iavf_adapter * adapter,uint64_t * caps)1546 iavf_get_hena_caps(struct iavf_adapter *adapter, uint64_t *caps)
1547 {
1548 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1549 	struct iavf_cmd_info args;
1550 	int err;
1551 
1552 	args.ops = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
1553 	args.in_args = NULL;
1554 	args.in_args_size = 0;
1555 	args.out_buffer = vf->aq_resp;
1556 	args.out_size = IAVF_AQ_BUF_SZ;
1557 
1558 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1559 	if (err) {
1560 		PMD_DRV_LOG(ERR,
1561 			    "Failed to execute command of OP_GET_RSS_HENA_CAPS");
1562 		return err;
1563 	}
1564 
1565 	*caps = ((struct virtchnl_rss_hena *)args.out_buffer)->hena;
1566 	return 0;
1567 }
1568 
1569 int
iavf_set_hena(struct iavf_adapter * adapter,uint64_t hena)1570 iavf_set_hena(struct iavf_adapter *adapter, uint64_t hena)
1571 {
1572 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1573 	struct virtchnl_rss_hena vrh;
1574 	struct iavf_cmd_info args;
1575 	int err;
1576 
1577 	vrh.hena = hena;
1578 	args.ops = VIRTCHNL_OP_SET_RSS_HENA;
1579 	args.in_args = (u8 *)&vrh;
1580 	args.in_args_size = sizeof(vrh);
1581 	args.out_buffer = vf->aq_resp;
1582 	args.out_size = IAVF_AQ_BUF_SZ;
1583 
1584 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1585 	if (err)
1586 		PMD_DRV_LOG(ERR,
1587 			    "Failed to execute command of OP_SET_RSS_HENA");
1588 
1589 	return err;
1590 }
1591 
1592 int
iavf_get_qos_cap(struct iavf_adapter * adapter)1593 iavf_get_qos_cap(struct iavf_adapter *adapter)
1594 {
1595 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1596 	struct iavf_cmd_info args;
1597 	uint32_t len;
1598 	int err;
1599 
1600 	args.ops = VIRTCHNL_OP_GET_QOS_CAPS;
1601 	args.in_args = NULL;
1602 	args.in_args_size = 0;
1603 	args.out_buffer = vf->aq_resp;
1604 	args.out_size = IAVF_AQ_BUF_SZ;
1605 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1606 
1607 	if (err) {
1608 		PMD_DRV_LOG(ERR,
1609 			    "Failed to execute command of OP_GET_VF_RESOURCE");
1610 		return -1;
1611 	}
1612 
1613 	len =  sizeof(struct virtchnl_qos_cap_list) +
1614 		IAVF_MAX_TRAFFIC_CLASS * sizeof(struct virtchnl_qos_cap_elem);
1615 
1616 	rte_memcpy(vf->qos_cap, args.out_buffer,
1617 		   RTE_MIN(args.out_size, len));
1618 
1619 	return 0;
1620 }
1621 
iavf_set_q_tc_map(struct rte_eth_dev * dev,struct virtchnl_queue_tc_mapping * q_tc_mapping,uint16_t size)1622 int iavf_set_q_tc_map(struct rte_eth_dev *dev,
1623 		struct virtchnl_queue_tc_mapping *q_tc_mapping, uint16_t size)
1624 {
1625 	struct iavf_adapter *adapter =
1626 			IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1627 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1628 	struct iavf_cmd_info args;
1629 	int err;
1630 
1631 	memset(&args, 0, sizeof(args));
1632 	args.ops = VIRTCHNL_OP_CONFIG_QUEUE_TC_MAP;
1633 	args.in_args = (uint8_t *)q_tc_mapping;
1634 	args.in_args_size = size;
1635 	args.out_buffer = vf->aq_resp;
1636 	args.out_size = IAVF_AQ_BUF_SZ;
1637 
1638 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1639 	if (err)
1640 		PMD_DRV_LOG(ERR, "Failed to execute command of"
1641 			    " VIRTCHNL_OP_CONFIG_TC_MAP");
1642 	return err;
1643 }
1644 
iavf_set_q_bw(struct rte_eth_dev * dev,struct virtchnl_queues_bw_cfg * q_bw,uint16_t size)1645 int iavf_set_q_bw(struct rte_eth_dev *dev,
1646 		struct virtchnl_queues_bw_cfg *q_bw, uint16_t size)
1647 {
1648 	struct iavf_adapter *adapter =
1649 			IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1650 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1651 	struct iavf_cmd_info args;
1652 	int err;
1653 
1654 	memset(&args, 0, sizeof(args));
1655 	args.ops = VIRTCHNL_OP_CONFIG_QUEUE_BW;
1656 	args.in_args = (uint8_t *)q_bw;
1657 	args.in_args_size = size;
1658 	args.out_buffer = vf->aq_resp;
1659 	args.out_size = IAVF_AQ_BUF_SZ;
1660 
1661 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1662 	if (err)
1663 		PMD_DRV_LOG(ERR, "Failed to execute command of"
1664 			    " VIRTCHNL_OP_CONFIG_QUEUE_BW");
1665 	return err;
1666 }
1667 
1668 int
iavf_add_del_mc_addr_list(struct iavf_adapter * adapter,struct rte_ether_addr * mc_addrs,uint32_t mc_addrs_num,bool add)1669 iavf_add_del_mc_addr_list(struct iavf_adapter *adapter,
1670 			struct rte_ether_addr *mc_addrs,
1671 			uint32_t mc_addrs_num, bool add)
1672 {
1673 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1674 	uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1675 		(IAVF_NUM_MACADDR_MAX * sizeof(struct virtchnl_ether_addr))];
1676 	struct virtchnl_ether_addr_list *list;
1677 	struct iavf_cmd_info args;
1678 	uint32_t i;
1679 	int err;
1680 
1681 	if (mc_addrs == NULL || mc_addrs_num == 0)
1682 		return 0;
1683 
1684 	list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1685 	list->vsi_id = vf->vsi_res->vsi_id;
1686 	list->num_elements = mc_addrs_num;
1687 
1688 	for (i = 0; i < mc_addrs_num; i++) {
1689 		if (!IAVF_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
1690 			PMD_DRV_LOG(ERR, "Invalid mac:" RTE_ETHER_ADDR_PRT_FMT,
1691 				    RTE_ETHER_ADDR_BYTES(&mc_addrs[i]));
1692 			return -EINVAL;
1693 		}
1694 
1695 		memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
1696 			sizeof(list->list[i].addr));
1697 		list->list[i].type = VIRTCHNL_ETHER_ADDR_EXTRA;
1698 	}
1699 
1700 	args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1701 	args.in_args = cmd_buffer;
1702 	args.in_args_size = sizeof(struct virtchnl_ether_addr_list) +
1703 		i * sizeof(struct virtchnl_ether_addr);
1704 	args.out_buffer = vf->aq_resp;
1705 	args.out_size = IAVF_AQ_BUF_SZ;
1706 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1707 
1708 	if (err) {
1709 		PMD_DRV_LOG(ERR, "fail to execute command %s",
1710 			add ? "OP_ADD_ETH_ADDR" : "OP_DEL_ETH_ADDR");
1711 		return err;
1712 	}
1713 
1714 	return 0;
1715 }
1716 
1717 int
iavf_request_queues(struct rte_eth_dev * dev,uint16_t num)1718 iavf_request_queues(struct rte_eth_dev *dev, uint16_t num)
1719 {
1720 	struct iavf_adapter *adapter =
1721 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1722 	struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
1723 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1724 	struct virtchnl_vf_res_request vfres;
1725 	struct iavf_cmd_info args;
1726 	uint16_t num_queue_pairs;
1727 	int err;
1728 
1729 	if (!(vf->vf_res->vf_cap_flags &
1730 		VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
1731 		PMD_DRV_LOG(ERR, "request queues not supported");
1732 		return -1;
1733 	}
1734 
1735 	if (num == 0) {
1736 		PMD_DRV_LOG(ERR, "queue number cannot be zero");
1737 		return -1;
1738 	}
1739 	vfres.num_queue_pairs = num;
1740 
1741 	args.ops = VIRTCHNL_OP_REQUEST_QUEUES;
1742 	args.in_args = (u8 *)&vfres;
1743 	args.in_args_size = sizeof(vfres);
1744 	args.out_buffer = vf->aq_resp;
1745 	args.out_size = IAVF_AQ_BUF_SZ;
1746 
1747 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
1748 		/* disable interrupt to avoid the admin queue message to be read
1749 		 * before iavf_read_msg_from_pf.
1750 		 */
1751 		rte_intr_disable(pci_dev->intr_handle);
1752 		err = iavf_execute_vf_cmd(adapter, &args, 0);
1753 		rte_intr_enable(pci_dev->intr_handle);
1754 	} else {
1755 		rte_eal_alarm_cancel(iavf_dev_alarm_handler, dev);
1756 		err = iavf_execute_vf_cmd(adapter, &args, 0);
1757 		rte_eal_alarm_set(IAVF_ALARM_INTERVAL,
1758 				  iavf_dev_alarm_handler, dev);
1759 	}
1760 
1761 	if (err) {
1762 		PMD_DRV_LOG(ERR, "fail to execute command OP_REQUEST_QUEUES");
1763 		return err;
1764 	}
1765 
1766 	/* request queues succeeded, vf is resetting */
1767 	if (vf->vf_reset) {
1768 		PMD_DRV_LOG(INFO, "vf is resetting");
1769 		return 0;
1770 	}
1771 
1772 	/* request additional queues failed, return available number */
1773 	num_queue_pairs =
1774 	  ((struct virtchnl_vf_res_request *)args.out_buffer)->num_queue_pairs;
1775 	PMD_DRV_LOG(ERR, "request queues failed, only %u queues "
1776 		"available", num_queue_pairs);
1777 
1778 	return -1;
1779 }
1780 
1781 int
iavf_get_max_rss_queue_region(struct iavf_adapter * adapter)1782 iavf_get_max_rss_queue_region(struct iavf_adapter *adapter)
1783 {
1784 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1785 	struct iavf_cmd_info args;
1786 	uint16_t qregion_width;
1787 	int err;
1788 
1789 	args.ops = VIRTCHNL_OP_GET_MAX_RSS_QREGION;
1790 	args.in_args = NULL;
1791 	args.in_args_size = 0;
1792 	args.out_buffer = vf->aq_resp;
1793 	args.out_size = IAVF_AQ_BUF_SZ;
1794 
1795 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1796 	if (err) {
1797 		PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL_OP_GET_MAX_RSS_QREGION");
1798 		return err;
1799 	}
1800 
1801 	qregion_width =
1802 	((struct virtchnl_max_rss_qregion *)args.out_buffer)->qregion_width;
1803 
1804 	vf->max_rss_qregion = (uint16_t)(1 << qregion_width);
1805 
1806 	return 0;
1807 }
1808 
1809 
1810 
1811 int
iavf_ipsec_crypto_request(struct iavf_adapter * adapter,uint8_t * msg,size_t msg_len,uint8_t * resp_msg,size_t resp_msg_len)1812 iavf_ipsec_crypto_request(struct iavf_adapter *adapter,
1813 		uint8_t *msg, size_t msg_len,
1814 		uint8_t *resp_msg, size_t resp_msg_len)
1815 {
1816 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1817 	struct iavf_cmd_info args;
1818 	int err;
1819 
1820 	args.ops = VIRTCHNL_OP_INLINE_IPSEC_CRYPTO;
1821 	args.in_args = msg;
1822 	args.in_args_size = msg_len;
1823 	args.out_buffer = vf->aq_resp;
1824 	args.out_size = IAVF_AQ_BUF_SZ;
1825 
1826 	err = iavf_execute_vf_cmd(adapter, &args, 1);
1827 	if (err) {
1828 		PMD_DRV_LOG(ERR, "fail to execute command %s",
1829 				"OP_INLINE_IPSEC_CRYPTO");
1830 		return err;
1831 	}
1832 
1833 	memcpy(resp_msg, args.out_buffer, resp_msg_len);
1834 
1835 	return 0;
1836 }
1837 
1838 int
iavf_set_vf_quanta_size(struct iavf_adapter * adapter,u16 start_queue_id,u16 num_queues)1839 iavf_set_vf_quanta_size(struct iavf_adapter *adapter, u16 start_queue_id, u16 num_queues)
1840 {
1841 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1842 	struct iavf_cmd_info args;
1843 	struct virtchnl_quanta_cfg q_quanta;
1844 	int err;
1845 
1846 	if (adapter->devargs.quanta_size == 0)
1847 		return 0;
1848 
1849 	q_quanta.quanta_size = adapter->devargs.quanta_size;
1850 	q_quanta.queue_select.type = VIRTCHNL_QUEUE_TYPE_TX;
1851 	q_quanta.queue_select.start_queue_id = start_queue_id;
1852 	q_quanta.queue_select.num_queues = num_queues;
1853 
1854 	args.ops = VIRTCHNL_OP_CONFIG_QUANTA;
1855 	args.in_args = (uint8_t *)&q_quanta;
1856 	args.in_args_size = sizeof(q_quanta);
1857 	args.out_buffer = vf->aq_resp;
1858 	args.out_size = IAVF_AQ_BUF_SZ;
1859 
1860 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1861 	if (err) {
1862 		PMD_DRV_LOG(ERR, "Failed to execute command VIRTCHNL_OP_CONFIG_QUANTA");
1863 		return err;
1864 	}
1865 
1866 	return 0;
1867 }
1868 
1869 int
iavf_get_ptp_cap(struct iavf_adapter * adapter)1870 iavf_get_ptp_cap(struct iavf_adapter *adapter)
1871 {
1872 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1873 	struct virtchnl_ptp_caps ptp_caps;
1874 	struct iavf_cmd_info args;
1875 	int err;
1876 
1877 	ptp_caps.caps = VIRTCHNL_1588_PTP_CAP_RX_TSTAMP |
1878 			VIRTCHNL_1588_PTP_CAP_READ_PHC;
1879 
1880 	args.ops = VIRTCHNL_OP_1588_PTP_GET_CAPS;
1881 	args.in_args = (uint8_t *)&ptp_caps;
1882 	args.in_args_size = sizeof(ptp_caps);
1883 	args.out_buffer = vf->aq_resp;
1884 	args.out_size = IAVF_AQ_BUF_SZ;
1885 
1886 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1887 	if (err) {
1888 		PMD_DRV_LOG(ERR,
1889 			    "Failed to execute command of OP_1588_PTP_GET_CAPS");
1890 		return err;
1891 	}
1892 
1893 	vf->ptp_caps = ((struct virtchnl_ptp_caps *)args.out_buffer)->caps;
1894 
1895 	return 0;
1896 }
1897 
1898 int
iavf_get_phc_time(struct iavf_adapter * adapter)1899 iavf_get_phc_time(struct iavf_adapter *adapter)
1900 {
1901 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1902 	struct virtchnl_phc_time phc_time;
1903 	struct iavf_cmd_info args;
1904 	int err;
1905 
1906 	args.ops = VIRTCHNL_OP_1588_PTP_GET_TIME;
1907 	args.in_args = (uint8_t *)&phc_time;
1908 	args.in_args_size = sizeof(phc_time);
1909 	args.out_buffer = vf->aq_resp;
1910 	args.out_size = IAVF_AQ_BUF_SZ;
1911 
1912 	err = iavf_execute_vf_cmd(adapter, &args, 0);
1913 	if (err) {
1914 		PMD_DRV_LOG(ERR,
1915 			    "Failed to execute command of VIRTCHNL_OP_1588_PTP_GET_TIME");
1916 		return err;
1917 	}
1918 
1919 	adapter->phc_time = ((struct virtchnl_phc_time *)args.out_buffer)->time;
1920 
1921 	return 0;
1922 }
1923