1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
15 #include <rte_debug.h>
16 #include <rte_atomic.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev.h>
23 #include "base/avf_prototype.h"
24 #include "base/avf_adminq_cmd.h"
25 #include "base/avf_type.h"
30 #define MAX_TRY_TIMES 200
31 #define ASQ_DELAY_MS 10
33 /* Read data in admin queue to get msg from pf driver */
34 static enum avf_status_code
35 avf_read_msg_from_pf(struct avf_adapter *adapter, uint16_t buf_len,
38 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
39 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
40 struct avf_arq_event_info event;
41 enum virtchnl_ops opcode;
44 event.buf_len = buf_len;
46 ret = avf_clean_arq_element(hw, &event, NULL);
47 /* Can't read any msg from adminQ */
49 PMD_DRV_LOG(DEBUG, "Can't read msg from AQ");
53 opcode = (enum virtchnl_ops)rte_le_to_cpu_32(event.desc.cookie_high);
54 vf->cmd_retval = (enum virtchnl_status_code)rte_le_to_cpu_32(
55 event.desc.cookie_low);
57 PMD_DRV_LOG(DEBUG, "AQ from pf carries opcode %u, retval %d",
58 opcode, vf->cmd_retval);
60 if (opcode != vf->pend_cmd)
61 PMD_DRV_LOG(WARNING, "command mismatch, expect %u, get %u",
62 vf->pend_cmd, opcode);
68 avf_execute_vf_cmd(struct avf_adapter *adapter, struct avf_cmd_info *args)
70 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
71 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
72 struct avf_arq_event_info event_info;
73 enum avf_status_code ret;
77 if (_atomic_set_cmd(vf, args->ops))
80 ret = avf_aq_send_msg_to_pf(hw, args->ops, AVF_SUCCESS,
81 args->in_args, args->in_args_size, NULL);
83 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
89 case VIRTCHNL_OP_RESET_VF:
90 /*no need to wait for response */
93 case VIRTCHNL_OP_VERSION:
94 case VIRTCHNL_OP_GET_VF_RESOURCES:
95 /* for init virtchnl ops, need to poll the response */
97 ret = avf_read_msg_from_pf(adapter, args->out_size,
99 if (ret == AVF_SUCCESS)
101 rte_delay_ms(ASQ_DELAY_MS);
102 } while (i++ < MAX_TRY_TIMES);
103 if (i >= MAX_TRY_TIMES ||
104 vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
106 PMD_DRV_LOG(ERR, "No response or return failure (%d)"
107 " for cmd %d", vf->cmd_retval, args->ops);
113 /* For other virtchnl ops in running time,
114 * wait for the cmd done flag.
117 if (vf->pend_cmd == VIRTCHNL_OP_UNKNOWN)
119 rte_delay_ms(ASQ_DELAY_MS);
120 /* If don't read msg or read sys event, continue */
121 } while (i++ < MAX_TRY_TIMES);
122 /* If there's no response is received, clear command */
123 if (i >= MAX_TRY_TIMES ||
124 vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
126 PMD_DRV_LOG(ERR, "No response or return failure (%d)"
127 " for cmd %d", vf->cmd_retval, args->ops);
137 avf_handle_virtchnl_msg(struct rte_eth_dev *dev)
139 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
140 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
141 struct avf_arq_event_info info;
142 uint16_t pending, aq_opc;
143 enum virtchnl_ops msg_opc;
144 enum avf_status_code msg_ret;
147 info.buf_len = AVF_AQ_BUF_SZ;
149 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
152 info.msg_buf = vf->aq_resp;
156 ret = avf_clean_arq_element(hw, &info, &pending);
158 if (ret != AVF_SUCCESS) {
159 PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
163 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
164 /* For the message sent from pf to vf, opcode is stored in
165 * cookie_high of struct avf_aq_desc, while return error code
166 * are stored in cookie_low, Which is done by PF driver.
168 msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
169 info.desc.cookie_high);
170 msg_ret = (enum avf_status_code)rte_le_to_cpu_32(
171 info.desc.cookie_low);
173 case avf_aqc_opc_send_msg_to_vf:
174 if (msg_opc == VIRTCHNL_OP_EVENT) {
177 /* read message and it's expected one */
178 if (msg_opc == vf->pend_cmd) {
179 vf->cmd_retval = msg_ret;
180 /* prevent compiler reordering */
181 rte_compiler_barrier();
184 PMD_DRV_LOG(ERR, "command mismatch,"
186 vf->pend_cmd, msg_opc);
188 "adminq response is received,"
189 " opcode = %d", msg_opc);
193 PMD_DRV_LOG(ERR, "Request %u is not supported yet",
201 avf_enable_vlan_strip(struct avf_adapter *adapter)
203 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
204 struct avf_cmd_info args;
207 memset(&args, 0, sizeof(args));
208 args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
210 args.in_args_size = 0;
211 args.out_buffer = vf->aq_resp;
212 args.out_size = AVF_AQ_BUF_SZ;
213 ret = avf_execute_vf_cmd(adapter, &args);
215 PMD_DRV_LOG(ERR, "Failed to execute command of"
216 " OP_ENABLE_VLAN_STRIPPING");
222 avf_disable_vlan_strip(struct avf_adapter *adapter)
224 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
225 struct avf_cmd_info args;
228 memset(&args, 0, sizeof(args));
229 args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
231 args.in_args_size = 0;
232 args.out_buffer = vf->aq_resp;
233 args.out_size = AVF_AQ_BUF_SZ;
234 ret = avf_execute_vf_cmd(adapter, &args);
236 PMD_DRV_LOG(ERR, "Failed to execute command of"
237 " OP_DISABLE_VLAN_STRIPPING");
242 #define VIRTCHNL_VERSION_MAJOR_START 1
243 #define VIRTCHNL_VERSION_MINOR_START 1
245 /* Check API version with sync wait until version read from admin queue */
247 avf_check_api_version(struct avf_adapter *adapter)
249 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
250 struct virtchnl_version_info version, *pver;
251 struct avf_cmd_info args;
254 version.major = VIRTCHNL_VERSION_MAJOR;
255 version.minor = VIRTCHNL_VERSION_MINOR;
257 args.ops = VIRTCHNL_OP_VERSION;
258 args.in_args = (uint8_t *)&version;
259 args.in_args_size = sizeof(version);
260 args.out_buffer = vf->aq_resp;
261 args.out_size = AVF_AQ_BUF_SZ;
263 err = avf_execute_vf_cmd(adapter, &args);
265 PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
269 pver = (struct virtchnl_version_info *)args.out_buffer;
270 vf->virtchnl_version = *pver;
272 if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
273 (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
274 vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
275 PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
276 " than (%u.%u) to support Adapative VF",
277 VIRTCHNL_VERSION_MAJOR_START,
278 VIRTCHNL_VERSION_MAJOR_START);
280 } else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
281 (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
282 vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
283 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
284 vf->virtchnl_version.major,
285 vf->virtchnl_version.minor,
286 VIRTCHNL_VERSION_MAJOR,
287 VIRTCHNL_VERSION_MINOR);
291 PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
296 avf_get_vf_resource(struct avf_adapter *adapter)
298 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
299 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
300 struct avf_cmd_info args;
304 args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
305 args.out_buffer = vf->aq_resp;
306 args.out_size = AVF_AQ_BUF_SZ;
308 /* TODO: basic offload capabilities, need to
309 * add advanced/optional offload capabilities
312 caps = AVF_BASIC_OFFLOAD_CAPS;
314 args.in_args = (uint8_t *)∩︀
315 args.in_args_size = sizeof(caps);
317 err = avf_execute_vf_cmd(adapter, &args);
321 "Failed to execute command of OP_GET_VF_RESOURCE");
325 len = sizeof(struct virtchnl_vf_resource) +
326 AVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
328 rte_memcpy(vf->vf_res, args.out_buffer,
329 RTE_MIN(args.out_size, len));
330 /* parse VF config message back from PF*/
331 avf_parse_hw_config(hw, vf->vf_res);
332 for (i = 0; i < vf->vf_res->num_vsis; i++) {
333 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
334 vf->vsi_res = &vf->vf_res->vsi_res[i];
338 PMD_INIT_LOG(ERR, "no LAN VSI found");
342 vf->vsi.vsi_id = vf->vsi_res->vsi_id;
343 vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
344 vf->vsi.adapter = adapter;
350 avf_enable_queues(struct avf_adapter *adapter)
352 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
353 struct virtchnl_queue_select queue_select;
354 struct avf_cmd_info args;
357 memset(&queue_select, 0, sizeof(queue_select));
358 queue_select.vsi_id = vf->vsi_res->vsi_id;
360 queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
361 queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
363 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
364 args.in_args = (u8 *)&queue_select;
365 args.in_args_size = sizeof(queue_select);
366 args.out_buffer = vf->aq_resp;
367 args.out_size = AVF_AQ_BUF_SZ;
368 err = avf_execute_vf_cmd(adapter, &args);
371 "Failed to execute command of OP_ENABLE_QUEUES");
378 avf_disable_queues(struct avf_adapter *adapter)
380 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
381 struct virtchnl_queue_select queue_select;
382 struct avf_cmd_info args;
385 memset(&queue_select, 0, sizeof(queue_select));
386 queue_select.vsi_id = vf->vsi_res->vsi_id;
388 queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
389 queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
391 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
392 args.in_args = (u8 *)&queue_select;
393 args.in_args_size = sizeof(queue_select);
394 args.out_buffer = vf->aq_resp;
395 args.out_size = AVF_AQ_BUF_SZ;
396 err = avf_execute_vf_cmd(adapter, &args);
399 "Failed to execute command of OP_DISABLE_QUEUES");
406 avf_switch_queue(struct avf_adapter *adapter, uint16_t qid,
409 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
410 struct virtchnl_queue_select queue_select;
411 struct avf_cmd_info args;
414 memset(&queue_select, 0, sizeof(queue_select));
415 queue_select.vsi_id = vf->vsi_res->vsi_id;
417 queue_select.rx_queues |= 1 << qid;
419 queue_select.tx_queues |= 1 << qid;
422 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
424 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
425 args.in_args = (u8 *)&queue_select;
426 args.in_args_size = sizeof(queue_select);
427 args.out_buffer = vf->aq_resp;
428 args.out_size = AVF_AQ_BUF_SZ;
429 err = avf_execute_vf_cmd(adapter, &args);
431 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
432 on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
437 avf_configure_rss_lut(struct avf_adapter *adapter)
439 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
440 struct virtchnl_rss_lut *rss_lut;
441 struct avf_cmd_info args;
444 len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
445 rss_lut = rte_zmalloc("rss_lut", len, 0);
449 rss_lut->vsi_id = vf->vsi_res->vsi_id;
450 rss_lut->lut_entries = vf->vf_res->rss_lut_size;
451 rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
453 args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
454 args.in_args = (u8 *)rss_lut;
455 args.in_args_size = len;
456 args.out_buffer = vf->aq_resp;
457 args.out_size = AVF_AQ_BUF_SZ;
459 err = avf_execute_vf_cmd(adapter, &args);
462 "Failed to execute command of OP_CONFIG_RSS_LUT");
469 avf_configure_rss_key(struct avf_adapter *adapter)
471 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
472 struct virtchnl_rss_key *rss_key;
473 struct avf_cmd_info args;
476 len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
477 rss_key = rte_zmalloc("rss_key", len, 0);
481 rss_key->vsi_id = vf->vsi_res->vsi_id;
482 rss_key->key_len = vf->vf_res->rss_key_size;
483 rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
485 args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
486 args.in_args = (u8 *)rss_key;
487 args.in_args_size = len;
488 args.out_buffer = vf->aq_resp;
489 args.out_size = AVF_AQ_BUF_SZ;
491 err = avf_execute_vf_cmd(adapter, &args);
494 "Failed to execute command of OP_CONFIG_RSS_KEY");
501 avf_configure_queues(struct avf_adapter *adapter)
503 struct avf_rx_queue **rxq =
504 (struct avf_rx_queue **)adapter->eth_dev->data->rx_queues;
505 struct avf_tx_queue **txq =
506 (struct avf_tx_queue **)adapter->eth_dev->data->tx_queues;
507 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
508 struct virtchnl_vsi_queue_config_info *vc_config;
509 struct virtchnl_queue_pair_info *vc_qp;
510 struct avf_cmd_info args;
514 size = sizeof(*vc_config) +
515 sizeof(vc_config->qpair[0]) * vf->num_queue_pairs;
516 vc_config = rte_zmalloc("cfg_queue", size, 0);
520 vc_config->vsi_id = vf->vsi_res->vsi_id;
521 vc_config->num_queue_pairs = vf->num_queue_pairs;
523 for (i = 0, vc_qp = vc_config->qpair;
524 i < vf->num_queue_pairs;
526 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
527 vc_qp->txq.queue_id = i;
528 /* Virtchnnl configure queues by pairs */
529 if (i < adapter->eth_dev->data->nb_tx_queues) {
530 vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
531 vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
533 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
534 vc_qp->rxq.queue_id = i;
535 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
536 /* Virtchnnl configure queues by pairs */
537 if (i < adapter->eth_dev->data->nb_rx_queues) {
538 vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
539 vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
540 vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
544 memset(&args, 0, sizeof(args));
545 args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
546 args.in_args = (uint8_t *)vc_config;
547 args.in_args_size = size;
548 args.out_buffer = vf->aq_resp;
549 args.out_size = AVF_AQ_BUF_SZ;
551 err = avf_execute_vf_cmd(adapter, &args);
553 PMD_DRV_LOG(ERR, "Failed to execute command of"
554 " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
561 avf_config_irq_map(struct avf_adapter *adapter)
563 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
564 struct virtchnl_irq_map_info *map_info;
565 struct virtchnl_vector_map *vecmap;
566 struct avf_cmd_info args;
570 len = sizeof(struct virtchnl_irq_map_info) +
571 sizeof(struct virtchnl_vector_map) * vf->nb_msix;
573 map_info = rte_zmalloc("map_info", len, 0);
577 map_info->num_vectors = vf->nb_msix;
578 for (i = 0; i < vf->nb_msix; i++) {
579 vecmap = &map_info->vecmap[i];
580 vecmap->vsi_id = vf->vsi_res->vsi_id;
581 vecmap->rxitr_idx = AVF_ITR_INDEX_DEFAULT;
582 vecmap->vector_id = vf->msix_base + i;
584 vecmap->rxq_map = vf->rxq_map[vf->msix_base + i];
587 args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
588 args.in_args = (u8 *)map_info;
589 args.in_args_size = len;
590 args.out_buffer = vf->aq_resp;
591 args.out_size = AVF_AQ_BUF_SZ;
592 err = avf_execute_vf_cmd(adapter, &args);
594 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
601 avf_add_del_all_mac_addr(struct avf_adapter *adapter, bool add)
603 struct virtchnl_ether_addr_list *list;
604 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
605 struct ether_addr *addr;
606 struct avf_cmd_info args;
613 len = sizeof(struct virtchnl_ether_addr_list);
614 for (i = begin; i < AVF_NUM_MACADDR_MAX; i++, next_begin++) {
615 addr = &adapter->eth_dev->data->mac_addrs[i];
616 if (is_zero_ether_addr(addr))
618 len += sizeof(struct virtchnl_ether_addr);
619 if (len >= AVF_AQ_BUF_SZ) {
625 list = rte_zmalloc("avf_del_mac_buffer", len, 0);
627 PMD_DRV_LOG(ERR, "fail to allocate memory");
631 for (i = begin; i < next_begin; i++) {
632 addr = &adapter->eth_dev->data->mac_addrs[i];
633 if (is_zero_ether_addr(addr))
635 rte_memcpy(list->list[j].addr, addr->addr_bytes,
636 sizeof(addr->addr_bytes));
637 PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
638 addr->addr_bytes[0], addr->addr_bytes[1],
639 addr->addr_bytes[2], addr->addr_bytes[3],
640 addr->addr_bytes[4], addr->addr_bytes[5]);
643 list->vsi_id = vf->vsi_res->vsi_id;
644 list->num_elements = j;
645 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
646 VIRTCHNL_OP_DEL_ETH_ADDR;
647 args.in_args = (uint8_t *)list;
648 args.in_args_size = len;
649 args.out_buffer = vf->aq_resp;
650 args.out_size = AVF_AQ_BUF_SZ;
651 err = avf_execute_vf_cmd(adapter, &args);
653 PMD_DRV_LOG(ERR, "fail to execute command %s",
654 add ? "OP_ADD_ETHER_ADDRESS" :
655 "OP_DEL_ETHER_ADDRESS");
658 } while (begin < AVF_NUM_MACADDR_MAX);