1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
16 #include <rte_interrupts.h>
17 #include <rte_debug.h>
19 #include <rte_atomic.h>
21 #include <rte_ether.h>
22 #include <rte_ethdev.h>
23 #include <rte_ethdev_pci.h>
24 #include <rte_malloc.h>
25 #include <rte_memzone.h>
29 #include "base/avf_prototype.h"
30 #include "base/avf_adminq_cmd.h"
31 #include "base/avf_type.h"
36 static int avf_dev_configure(struct rte_eth_dev *dev);
37 static int avf_dev_start(struct rte_eth_dev *dev);
38 static void avf_dev_stop(struct rte_eth_dev *dev);
39 static void avf_dev_close(struct rte_eth_dev *dev);
40 static void avf_dev_info_get(struct rte_eth_dev *dev,
41 struct rte_eth_dev_info *dev_info);
42 static const uint32_t *avf_dev_supported_ptypes_get(struct rte_eth_dev *dev);
43 static int avf_dev_stats_get(struct rte_eth_dev *dev,
44 struct rte_eth_stats *stats);
45 static void avf_dev_promiscuous_enable(struct rte_eth_dev *dev);
46 static void avf_dev_promiscuous_disable(struct rte_eth_dev *dev);
47 static void avf_dev_allmulticast_enable(struct rte_eth_dev *dev);
48 static void avf_dev_allmulticast_disable(struct rte_eth_dev *dev);
49 static int avf_dev_add_mac_addr(struct rte_eth_dev *dev,
50 struct ether_addr *addr,
53 static void avf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index);
54 static int avf_dev_vlan_filter_set(struct rte_eth_dev *dev,
55 uint16_t vlan_id, int on);
56 static int avf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
57 static int avf_dev_rss_reta_update(struct rte_eth_dev *dev,
58 struct rte_eth_rss_reta_entry64 *reta_conf,
60 static int avf_dev_rss_reta_query(struct rte_eth_dev *dev,
61 struct rte_eth_rss_reta_entry64 *reta_conf,
63 static int avf_dev_rss_hash_update(struct rte_eth_dev *dev,
64 struct rte_eth_rss_conf *rss_conf);
65 static int avf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
66 struct rte_eth_rss_conf *rss_conf);
67 static int avf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
68 static void avf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
69 struct ether_addr *mac_addr);
72 int avf_logtype_driver;
73 static const struct rte_pci_id pci_id_avf_map[] = {
74 { RTE_PCI_DEVICE(AVF_INTEL_VENDOR_ID, AVF_DEV_ID_ADAPTIVE_VF) },
75 { .vendor_id = 0, /* sentinel */ },
78 static const struct eth_dev_ops avf_eth_dev_ops = {
79 .dev_configure = avf_dev_configure,
80 .dev_start = avf_dev_start,
81 .dev_stop = avf_dev_stop,
82 .dev_close = avf_dev_close,
83 .dev_infos_get = avf_dev_info_get,
84 .dev_supported_ptypes_get = avf_dev_supported_ptypes_get,
85 .link_update = avf_dev_link_update,
86 .stats_get = avf_dev_stats_get,
87 .promiscuous_enable = avf_dev_promiscuous_enable,
88 .promiscuous_disable = avf_dev_promiscuous_disable,
89 .allmulticast_enable = avf_dev_allmulticast_enable,
90 .allmulticast_disable = avf_dev_allmulticast_disable,
91 .mac_addr_add = avf_dev_add_mac_addr,
92 .mac_addr_remove = avf_dev_del_mac_addr,
93 .vlan_filter_set = avf_dev_vlan_filter_set,
94 .vlan_offload_set = avf_dev_vlan_offload_set,
95 .rx_queue_start = avf_dev_rx_queue_start,
96 .rx_queue_stop = avf_dev_rx_queue_stop,
97 .tx_queue_start = avf_dev_tx_queue_start,
98 .tx_queue_stop = avf_dev_tx_queue_stop,
99 .rx_queue_setup = avf_dev_rx_queue_setup,
100 .rx_queue_release = avf_dev_rx_queue_release,
101 .tx_queue_setup = avf_dev_tx_queue_setup,
102 .tx_queue_release = avf_dev_tx_queue_release,
103 .mac_addr_set = avf_dev_set_default_mac_addr,
104 .reta_update = avf_dev_rss_reta_update,
105 .reta_query = avf_dev_rss_reta_query,
106 .rss_hash_update = avf_dev_rss_hash_update,
107 .rss_hash_conf_get = avf_dev_rss_hash_conf_get,
108 .rxq_info_get = avf_dev_rxq_info_get,
109 .txq_info_get = avf_dev_txq_info_get,
110 .rx_queue_count = avf_dev_rxq_count,
111 .rx_descriptor_status = avf_dev_rx_desc_status,
112 .tx_descriptor_status = avf_dev_tx_desc_status,
113 .mtu_set = avf_dev_mtu_set,
117 avf_dev_configure(struct rte_eth_dev *dev)
119 struct avf_adapter *ad =
120 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
121 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(ad);
122 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
124 ad->rx_bulk_alloc_allowed = true;
125 #ifdef RTE_LIBRTE_AVF_INC_VECTOR
126 /* Initialize to TRUE. If any of Rx queues doesn't meet the
127 * vector Rx/Tx preconditions, it will be reset.
129 ad->rx_vec_allowed = true;
130 ad->tx_vec_allowed = true;
132 ad->rx_vec_allowed = false;
133 ad->tx_vec_allowed = false;
136 /* Vlan stripping setting */
137 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN) {
138 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
139 avf_enable_vlan_strip(ad);
141 avf_disable_vlan_strip(ad);
147 avf_init_rss(struct avf_adapter *adapter)
149 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
150 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
151 struct rte_eth_rss_conf *rss_conf;
155 rss_conf = &adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
156 nb_q = RTE_MIN(adapter->eth_dev->data->nb_rx_queues,
159 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF)) {
160 PMD_DRV_LOG(DEBUG, "RSS is not supported");
163 if (adapter->eth_dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
164 PMD_DRV_LOG(WARNING, "RSS is enabled by PF by default");
165 /* set all lut items to default queue */
166 for (i = 0; i < vf->vf_res->rss_lut_size; i++)
168 ret = avf_configure_rss_lut(adapter);
172 /* In AVF, RSS enablement is set by PF driver. It is not supported
173 * to set based on rss_conf->rss_hf.
176 /* configure RSS key */
177 if (!rss_conf->rss_key) {
178 /* Calculate the default hash key */
179 for (i = 0; i <= vf->vf_res->rss_key_size; i++)
180 vf->rss_key[i] = (uint8_t)rte_rand();
182 rte_memcpy(vf->rss_key, rss_conf->rss_key,
183 RTE_MIN(rss_conf->rss_key_len,
184 vf->vf_res->rss_key_size));
186 /* init RSS LUT table */
187 for (i = 0; i < vf->vf_res->rss_lut_size; i++, j++) {
192 /* send virtchnnl ops to configure rss*/
193 ret = avf_configure_rss_lut(adapter);
196 ret = avf_configure_rss_key(adapter);
204 avf_init_rxq(struct rte_eth_dev *dev, struct avf_rx_queue *rxq)
206 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
207 struct rte_eth_dev_data *dev_data = dev->data;
208 uint16_t buf_size, max_pkt_len, len;
210 buf_size = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
212 /* Calculate the maximum packet length allowed */
213 len = rxq->rx_buf_len * AVF_MAX_CHAINED_RX_BUFFERS;
214 max_pkt_len = RTE_MIN(len, dev->data->dev_conf.rxmode.max_rx_pkt_len);
216 /* Check if the jumbo frame and maximum packet length are set
219 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
220 if (max_pkt_len <= ETHER_MAX_LEN ||
221 max_pkt_len > AVF_FRAME_SIZE_MAX) {
222 PMD_DRV_LOG(ERR, "maximum packet length must be "
223 "larger than %u and smaller than %u, "
224 "as jumbo frame is enabled",
225 (uint32_t)ETHER_MAX_LEN,
226 (uint32_t)AVF_FRAME_SIZE_MAX);
230 if (max_pkt_len < ETHER_MIN_LEN ||
231 max_pkt_len > ETHER_MAX_LEN) {
232 PMD_DRV_LOG(ERR, "maximum packet length must be "
233 "larger than %u and smaller than %u, "
234 "as jumbo frame is disabled",
235 (uint32_t)ETHER_MIN_LEN,
236 (uint32_t)ETHER_MAX_LEN);
241 rxq->max_pkt_len = max_pkt_len;
242 if ((dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) ||
243 (rxq->max_pkt_len + 2 * AVF_VLAN_TAG_SIZE) > buf_size) {
244 dev_data->scattered_rx = 1;
246 AVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
253 avf_init_queues(struct rte_eth_dev *dev)
255 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
256 struct avf_rx_queue **rxq =
257 (struct avf_rx_queue **)dev->data->rx_queues;
258 struct avf_tx_queue **txq =
259 (struct avf_tx_queue **)dev->data->tx_queues;
260 int i, ret = AVF_SUCCESS;
262 for (i = 0; i < dev->data->nb_rx_queues; i++) {
263 if (!rxq[i] || !rxq[i]->q_set)
265 ret = avf_init_rxq(dev, rxq[i]);
266 if (ret != AVF_SUCCESS)
269 /* set rx/tx function to vector/scatter/single-segment
270 * according to parameters
272 avf_set_rx_function(dev);
273 avf_set_tx_function(dev);
279 avf_start_queues(struct rte_eth_dev *dev)
281 struct avf_rx_queue *rxq;
282 struct avf_tx_queue *txq;
285 for (i = 0; i < dev->data->nb_tx_queues; i++) {
286 txq = dev->data->tx_queues[i];
287 if (txq->tx_deferred_start)
289 if (avf_dev_tx_queue_start(dev, i) != 0) {
290 PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
295 for (i = 0; i < dev->data->nb_rx_queues; i++) {
296 rxq = dev->data->rx_queues[i];
297 if (rxq->rx_deferred_start)
299 if (avf_dev_rx_queue_start(dev, i) != 0) {
300 PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
309 avf_dev_start(struct rte_eth_dev *dev)
311 struct avf_adapter *adapter =
312 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
313 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
314 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
315 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
316 struct rte_intr_handle *intr_handle = dev->intr_handle;
320 PMD_INIT_FUNC_TRACE();
322 hw->adapter_stopped = 0;
324 vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
325 vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
326 dev->data->nb_tx_queues);
328 /* TODO: Rx interrupt */
330 if (avf_init_queues(dev) != 0) {
331 PMD_DRV_LOG(ERR, "failed to do Queue init");
335 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
336 if (avf_init_rss(adapter) != 0) {
337 PMD_DRV_LOG(ERR, "configure rss failed");
342 if (avf_configure_queues(adapter) != 0) {
343 PMD_DRV_LOG(ERR, "configure queues failed");
347 /* Map interrupt for writeback */
349 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
350 /* If WB_ON_ITR supports, enable it */
351 vf->msix_base = AVF_RX_VEC_START;
352 AVF_WRITE_REG(hw, AVFINT_DYN_CTLN1(vf->msix_base - 1),
353 AVFINT_DYN_CTLN1_ITR_INDX_MASK |
354 AVFINT_DYN_CTLN1_WB_ON_ITR_MASK);
356 /* If no WB_ON_ITR offload flags, need to set interrupt for
357 * descriptor write back.
359 vf->msix_base = AVF_MISC_VEC_ID;
362 interval = avf_calc_itr_interval(AVF_QUEUE_ITR_INTERVAL_MAX);
363 AVF_WRITE_REG(hw, AVFINT_DYN_CTL01,
364 AVFINT_DYN_CTL01_INTENA_MASK |
365 (AVF_ITR_INDEX_DEFAULT <<
366 AVFINT_DYN_CTL01_ITR_INDX_SHIFT) |
367 (interval << AVFINT_DYN_CTL01_INTERVAL_SHIFT));
370 /* map all queues to the same interrupt */
371 for (i = 0; i < dev->data->nb_rx_queues; i++)
372 vf->rxq_map[0] |= 1 << i;
373 if (avf_config_irq_map(adapter)) {
374 PMD_DRV_LOG(ERR, "config interrupt mapping failed");
378 /* Set all mac addrs */
379 avf_add_del_all_mac_addr(adapter, TRUE);
381 if (avf_start_queues(dev) != 0) {
382 PMD_DRV_LOG(ERR, "enable queues failed");
386 /* TODO: enable interrupt for RX interrupt */
390 avf_add_del_all_mac_addr(adapter, FALSE);
397 avf_dev_stop(struct rte_eth_dev *dev)
399 struct avf_adapter *adapter =
400 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
401 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev);
404 PMD_INIT_FUNC_TRACE();
406 if (hw->adapter_stopped == 1)
409 avf_stop_queues(dev);
411 /*TODO: Disable the interrupt for Rx*/
413 /* TODO: Rx interrupt vector mapping free */
415 /* remove all mac addrs */
416 avf_add_del_all_mac_addr(adapter, FALSE);
417 hw->adapter_stopped = 1;
421 avf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
423 struct avf_adapter *adapter =
424 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
425 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
427 memset(dev_info, 0, sizeof(*dev_info));
428 dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
429 dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
430 dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
431 dev_info->min_rx_bufsize = AVF_BUF_SIZE_MIN;
432 dev_info->max_rx_pktlen = AVF_FRAME_SIZE_MAX;
433 dev_info->hash_key_size = vf->vf_res->rss_key_size;
434 dev_info->reta_size = vf->vf_res->rss_lut_size;
435 dev_info->flow_type_rss_offloads = AVF_RSS_OFFLOAD_ALL;
436 dev_info->max_mac_addrs = AVF_NUM_MACADDR_MAX;
437 dev_info->rx_offload_capa =
438 DEV_RX_OFFLOAD_VLAN_STRIP |
439 DEV_RX_OFFLOAD_IPV4_CKSUM |
440 DEV_RX_OFFLOAD_UDP_CKSUM |
441 DEV_RX_OFFLOAD_TCP_CKSUM;
442 dev_info->tx_offload_capa =
443 DEV_TX_OFFLOAD_VLAN_INSERT |
444 DEV_TX_OFFLOAD_IPV4_CKSUM |
445 DEV_TX_OFFLOAD_UDP_CKSUM |
446 DEV_TX_OFFLOAD_TCP_CKSUM |
447 DEV_TX_OFFLOAD_SCTP_CKSUM |
448 DEV_TX_OFFLOAD_TCP_TSO;
450 dev_info->default_rxconf = (struct rte_eth_rxconf) {
451 .rx_free_thresh = AVF_DEFAULT_RX_FREE_THRESH,
455 dev_info->default_txconf = (struct rte_eth_txconf) {
456 .tx_free_thresh = AVF_DEFAULT_TX_FREE_THRESH,
457 .tx_rs_thresh = AVF_DEFAULT_TX_RS_THRESH,
458 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
459 ETH_TXQ_FLAGS_NOOFFLOADS,
462 dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
463 .nb_max = AVF_MAX_RING_DESC,
464 .nb_min = AVF_MIN_RING_DESC,
465 .nb_align = AVF_ALIGN_RING_DESC,
468 dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
469 .nb_max = AVF_MAX_RING_DESC,
470 .nb_min = AVF_MIN_RING_DESC,
471 .nb_align = AVF_ALIGN_RING_DESC,
475 static const uint32_t *
476 avf_dev_supported_ptypes_get(struct rte_eth_dev *dev)
478 static const uint32_t ptypes[] = {
480 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
483 RTE_PTYPE_L4_NONFRAG,
493 avf_dev_link_update(struct rte_eth_dev *dev,
494 __rte_unused int wait_to_complete)
496 struct rte_eth_link new_link;
497 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
499 /* Only read status info stored in VF, and the info is updated
500 * when receive LINK_CHANGE evnet from PF by Virtchnnl.
502 switch (vf->link_speed) {
503 case VIRTCHNL_LINK_SPEED_100MB:
504 new_link.link_speed = ETH_SPEED_NUM_100M;
506 case VIRTCHNL_LINK_SPEED_1GB:
507 new_link.link_speed = ETH_SPEED_NUM_1G;
509 case VIRTCHNL_LINK_SPEED_10GB:
510 new_link.link_speed = ETH_SPEED_NUM_10G;
512 case VIRTCHNL_LINK_SPEED_20GB:
513 new_link.link_speed = ETH_SPEED_NUM_20G;
515 case VIRTCHNL_LINK_SPEED_25GB:
516 new_link.link_speed = ETH_SPEED_NUM_25G;
518 case VIRTCHNL_LINK_SPEED_40GB:
519 new_link.link_speed = ETH_SPEED_NUM_40G;
522 new_link.link_speed = ETH_SPEED_NUM_NONE;
526 new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
527 new_link.link_status = vf->link_up ? ETH_LINK_UP :
529 new_link.link_autoneg = !!(dev->data->dev_conf.link_speeds &
530 ETH_LINK_SPEED_FIXED);
532 rte_atomic64_cmpset((uint64_t *)&dev->data->dev_link,
533 *(uint64_t *)&dev->data->dev_link,
534 *(uint64_t *)&new_link);
540 avf_dev_promiscuous_enable(struct rte_eth_dev *dev)
542 struct avf_adapter *adapter =
543 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
544 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
547 if (vf->promisc_unicast_enabled)
550 ret = avf_config_promisc(adapter, TRUE, vf->promisc_multicast_enabled);
552 vf->promisc_unicast_enabled = TRUE;
556 avf_dev_promiscuous_disable(struct rte_eth_dev *dev)
558 struct avf_adapter *adapter =
559 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
560 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
563 if (!vf->promisc_unicast_enabled)
566 ret = avf_config_promisc(adapter, FALSE, vf->promisc_multicast_enabled);
568 vf->promisc_unicast_enabled = FALSE;
572 avf_dev_allmulticast_enable(struct rte_eth_dev *dev)
574 struct avf_adapter *adapter =
575 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
576 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
579 if (vf->promisc_multicast_enabled)
582 ret = avf_config_promisc(adapter, vf->promisc_unicast_enabled, TRUE);
584 vf->promisc_multicast_enabled = TRUE;
588 avf_dev_allmulticast_disable(struct rte_eth_dev *dev)
590 struct avf_adapter *adapter =
591 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
592 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
595 if (!vf->promisc_multicast_enabled)
598 ret = avf_config_promisc(adapter, vf->promisc_unicast_enabled, FALSE);
600 vf->promisc_multicast_enabled = FALSE;
604 avf_dev_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr,
605 __rte_unused uint32_t index,
606 __rte_unused uint32_t pool)
608 struct avf_adapter *adapter =
609 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
610 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
613 if (is_zero_ether_addr(addr)) {
614 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
618 err = avf_add_del_eth_addr(adapter, addr, TRUE);
620 PMD_DRV_LOG(ERR, "fail to add MAC address");
630 avf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
632 struct avf_adapter *adapter =
633 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
634 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
635 struct ether_addr *addr;
638 addr = &dev->data->mac_addrs[index];
640 err = avf_add_del_eth_addr(adapter, addr, FALSE);
642 PMD_DRV_LOG(ERR, "fail to delete MAC address");
648 avf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
650 struct avf_adapter *adapter =
651 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
652 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
655 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
658 err = avf_add_del_vlan(adapter, vlan_id, on);
665 avf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
667 struct avf_adapter *adapter =
668 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
669 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
670 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
673 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
676 /* Vlan stripping setting */
677 if (mask & ETH_VLAN_STRIP_MASK) {
678 /* Enable or disable VLAN stripping */
679 if (dev_conf->rxmode.hw_vlan_strip)
680 err = avf_enable_vlan_strip(adapter);
682 err = avf_disable_vlan_strip(adapter);
691 avf_dev_rss_reta_update(struct rte_eth_dev *dev,
692 struct rte_eth_rss_reta_entry64 *reta_conf,
695 struct avf_adapter *adapter =
696 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
697 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
699 uint16_t i, idx, shift;
702 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
705 if (reta_size != vf->vf_res->rss_lut_size) {
706 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
707 "(%d) doesn't match the number of hardware can "
708 "support (%d)", reta_size, vf->vf_res->rss_lut_size);
712 lut = rte_zmalloc("rss_lut", reta_size, 0);
714 PMD_DRV_LOG(ERR, "No memory can be allocated");
717 /* store the old lut table temporarily */
718 rte_memcpy(lut, vf->rss_lut, reta_size);
720 for (i = 0; i < reta_size; i++) {
721 idx = i / RTE_RETA_GROUP_SIZE;
722 shift = i % RTE_RETA_GROUP_SIZE;
723 if (reta_conf[idx].mask & (1ULL << shift))
724 lut[i] = reta_conf[idx].reta[shift];
727 rte_memcpy(vf->rss_lut, lut, reta_size);
728 /* send virtchnnl ops to configure rss*/
729 ret = avf_configure_rss_lut(adapter);
730 if (ret) /* revert back */
731 rte_memcpy(vf->rss_lut, lut, reta_size);
738 avf_dev_rss_reta_query(struct rte_eth_dev *dev,
739 struct rte_eth_rss_reta_entry64 *reta_conf,
742 struct avf_adapter *adapter =
743 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
744 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
745 uint16_t i, idx, shift;
747 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
750 if (reta_size != vf->vf_res->rss_lut_size) {
751 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
752 "(%d) doesn't match the number of hardware can "
753 "support (%d)", reta_size, vf->vf_res->rss_lut_size);
757 for (i = 0; i < reta_size; i++) {
758 idx = i / RTE_RETA_GROUP_SIZE;
759 shift = i % RTE_RETA_GROUP_SIZE;
760 if (reta_conf[idx].mask & (1ULL << shift))
761 reta_conf[idx].reta[shift] = vf->rss_lut[i];
768 avf_dev_rss_hash_update(struct rte_eth_dev *dev,
769 struct rte_eth_rss_conf *rss_conf)
771 struct avf_adapter *adapter =
772 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
773 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
775 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
778 /* HENA setting, it is enabled by default, no change */
779 if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
780 PMD_DRV_LOG(DEBUG, "No key to be configured");
782 } else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
783 PMD_DRV_LOG(ERR, "The size of hash key configured "
784 "(%d) doesn't match the size of hardware can "
785 "support (%d)", rss_conf->rss_key_len,
786 vf->vf_res->rss_key_size);
790 rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
792 return avf_configure_rss_key(adapter);
796 avf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
797 struct rte_eth_rss_conf *rss_conf)
799 struct avf_adapter *adapter =
800 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
801 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
803 if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
806 /* Just set it to default value now. */
807 rss_conf->rss_hf = AVF_RSS_OFFLOAD_ALL;
809 if (!rss_conf->rss_key)
812 rss_conf->rss_key_len = vf->vf_res->rss_key_size;
813 rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
819 avf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
821 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
822 uint32_t frame_size = mtu + AVF_ETH_OVERHEAD;
825 if (mtu < ETHER_MIN_MTU || frame_size > AVF_FRAME_SIZE_MAX)
828 /* mtu setting is forbidden if port is start */
829 if (dev->data->dev_started) {
830 PMD_DRV_LOG(ERR, "port must be stopped before configuration");
834 if (frame_size > ETHER_MAX_LEN)
835 dev->data->dev_conf.rxmode.offloads |=
836 DEV_RX_OFFLOAD_JUMBO_FRAME;
838 dev->data->dev_conf.rxmode.offloads &=
839 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
841 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
847 avf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
848 struct ether_addr *mac_addr)
850 struct avf_adapter *adapter =
851 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
852 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
853 struct ether_addr *perm_addr, *old_addr;
856 old_addr = (struct ether_addr *)hw->mac.addr;
857 perm_addr = (struct ether_addr *)hw->mac.perm_addr;
859 if (is_same_ether_addr(mac_addr, old_addr))
862 /* If the MAC address is configured by host, skip the setting */
863 if (is_valid_assigned_ether_addr(perm_addr))
866 ret = avf_add_del_eth_addr(adapter, old_addr, FALSE);
868 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
869 " %02X:%02X:%02X:%02X:%02X:%02X",
870 old_addr->addr_bytes[0],
871 old_addr->addr_bytes[1],
872 old_addr->addr_bytes[2],
873 old_addr->addr_bytes[3],
874 old_addr->addr_bytes[4],
875 old_addr->addr_bytes[5]);
877 ret = avf_add_del_eth_addr(adapter, mac_addr, TRUE);
879 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
880 " %02X:%02X:%02X:%02X:%02X:%02X",
881 mac_addr->addr_bytes[0],
882 mac_addr->addr_bytes[1],
883 mac_addr->addr_bytes[2],
884 mac_addr->addr_bytes[3],
885 mac_addr->addr_bytes[4],
886 mac_addr->addr_bytes[5]);
888 ether_addr_copy(mac_addr, (struct ether_addr *)hw->mac.addr);
892 avf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
894 struct avf_adapter *adapter =
895 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
896 struct virtchnl_eth_stats *pstats = NULL;
899 ret = avf_query_stats(adapter, &pstats);
901 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
902 pstats->rx_broadcast;
903 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
905 stats->imissed = pstats->rx_discards;
906 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
907 stats->ibytes = pstats->rx_bytes;
908 stats->obytes = pstats->tx_bytes;
910 PMD_DRV_LOG(ERR, "Get statistics failed");
916 avf_check_vf_reset_done(struct avf_hw *hw)
920 for (i = 0; i < AVF_RESET_WAIT_CNT; i++) {
921 reset = AVF_READ_REG(hw, AVFGEN_RSTAT) &
922 AVFGEN_RSTAT_VFR_STATE_MASK;
923 reset = reset >> AVFGEN_RSTAT_VFR_STATE_SHIFT;
924 if (reset == VIRTCHNL_VFR_VFACTIVE ||
925 reset == VIRTCHNL_VFR_COMPLETED)
930 if (i >= AVF_RESET_WAIT_CNT)
937 avf_init_vf(struct rte_eth_dev *dev)
940 struct avf_adapter *adapter =
941 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
942 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
943 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
945 err = avf_set_mac_type(hw);
947 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
951 err = avf_check_vf_reset_done(hw);
953 PMD_INIT_LOG(ERR, "VF is still resetting");
957 avf_init_adminq_parameter(hw);
958 err = avf_init_adminq(hw);
960 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
964 vf->aq_resp = rte_zmalloc("vf_aq_resp", AVF_AQ_BUF_SZ, 0);
966 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
969 if (avf_check_api_version(adapter) != 0) {
970 PMD_INIT_LOG(ERR, "check_api version failed");
974 bufsz = sizeof(struct virtchnl_vf_resource) +
975 (AVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
976 vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
978 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
981 if (avf_get_vf_resource(adapter) != 0) {
982 PMD_INIT_LOG(ERR, "avf_get_vf_config failed");
985 /* Allocate memort for RSS info */
986 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
987 vf->rss_key = rte_zmalloc("rss_key",
988 vf->vf_res->rss_key_size, 0);
990 PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
993 vf->rss_lut = rte_zmalloc("rss_lut",
994 vf->vf_res->rss_lut_size, 0);
996 PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1002 rte_free(vf->rss_key);
1003 rte_free(vf->rss_lut);
1005 rte_free(vf->vf_res);
1008 rte_free(vf->aq_resp);
1010 avf_shutdown_adminq(hw);
1015 /* Enable default admin queue interrupt setting */
1017 avf_enable_irq0(struct avf_hw *hw)
1019 /* Enable admin queue interrupt trigger */
1020 AVF_WRITE_REG(hw, AVFINT_ICR0_ENA1, AVFINT_ICR0_ENA1_ADMINQ_MASK);
1022 AVF_WRITE_REG(hw, AVFINT_DYN_CTL01, AVFINT_DYN_CTL01_INTENA_MASK |
1023 AVFINT_DYN_CTL01_ITR_INDX_MASK);
1025 AVF_WRITE_FLUSH(hw);
1029 avf_disable_irq0(struct avf_hw *hw)
1031 /* Disable all interrupt types */
1032 AVF_WRITE_REG(hw, AVFINT_ICR0_ENA1, 0);
1033 AVF_WRITE_REG(hw, AVFINT_DYN_CTL01,
1034 AVFINT_DYN_CTL01_ITR_INDX_MASK);
1035 AVF_WRITE_FLUSH(hw);
1039 avf_dev_interrupt_handler(void *param)
1041 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1042 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1044 avf_disable_irq0(hw);
1046 avf_handle_virtchnl_msg(dev);
1049 avf_enable_irq0(hw);
1053 avf_dev_init(struct rte_eth_dev *eth_dev)
1055 struct avf_adapter *adapter =
1056 AVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1057 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
1058 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1060 PMD_INIT_FUNC_TRACE();
1062 /* assign ops func pointer */
1063 eth_dev->dev_ops = &avf_eth_dev_ops;
1064 eth_dev->rx_pkt_burst = &avf_recv_pkts;
1065 eth_dev->tx_pkt_burst = &avf_xmit_pkts;
1066 eth_dev->tx_pkt_prepare = &avf_prep_pkts;
1068 /* For secondary processes, we don't initialise any further as primary
1069 * has already done this work. Only check if we need a different RX
1072 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1073 avf_set_rx_function(eth_dev);
1074 avf_set_tx_function(eth_dev);
1077 rte_eth_copy_pci_info(eth_dev, pci_dev);
1079 hw->vendor_id = pci_dev->id.vendor_id;
1080 hw->device_id = pci_dev->id.device_id;
1081 hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1082 hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1083 hw->bus.bus_id = pci_dev->addr.bus;
1084 hw->bus.device = pci_dev->addr.devid;
1085 hw->bus.func = pci_dev->addr.function;
1086 hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1087 hw->back = AVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1088 adapter->eth_dev = eth_dev;
1090 if (avf_init_vf(eth_dev) != 0) {
1091 PMD_INIT_LOG(ERR, "Init vf failed");
1096 eth_dev->data->mac_addrs = rte_zmalloc(
1098 ETHER_ADDR_LEN * AVF_NUM_MACADDR_MAX,
1100 if (!eth_dev->data->mac_addrs) {
1101 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1102 " store MAC addresses",
1103 ETHER_ADDR_LEN * AVF_NUM_MACADDR_MAX);
1106 /* If the MAC address is not configured by host,
1107 * generate a random one.
1109 if (!is_valid_assigned_ether_addr((struct ether_addr *)hw->mac.addr))
1110 eth_random_addr(hw->mac.addr);
1111 ether_addr_copy((struct ether_addr *)hw->mac.addr,
1112 ð_dev->data->mac_addrs[0]);
1114 /* register callback func to eal lib */
1115 rte_intr_callback_register(&pci_dev->intr_handle,
1116 avf_dev_interrupt_handler,
1119 /* enable uio intr after callback register */
1120 rte_intr_enable(&pci_dev->intr_handle);
1122 /* configure and enable device interrupt */
1123 avf_enable_irq0(hw);
1129 avf_dev_close(struct rte_eth_dev *dev)
1131 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1132 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1133 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1136 avf_shutdown_adminq(hw);
1137 /* disable uio intr before callback unregister */
1138 rte_intr_disable(intr_handle);
1140 /* unregister callback func from eal lib */
1141 rte_intr_callback_unregister(intr_handle,
1142 avf_dev_interrupt_handler, dev);
1143 avf_disable_irq0(hw);
1147 avf_dev_uninit(struct rte_eth_dev *dev)
1149 struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1150 struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1152 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1155 dev->dev_ops = NULL;
1156 dev->rx_pkt_burst = NULL;
1157 dev->tx_pkt_burst = NULL;
1158 if (hw->adapter_stopped == 0)
1161 rte_free(vf->vf_res);
1165 rte_free(vf->aq_resp);
1168 rte_free(dev->data->mac_addrs);
1169 dev->data->mac_addrs = NULL;
1172 rte_free(vf->rss_lut);
1176 rte_free(vf->rss_key);
1183 static int eth_avf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1184 struct rte_pci_device *pci_dev)
1186 return rte_eth_dev_pci_generic_probe(pci_dev,
1187 sizeof(struct avf_adapter), avf_dev_init);
1190 static int eth_avf_pci_remove(struct rte_pci_device *pci_dev)
1192 return rte_eth_dev_pci_generic_remove(pci_dev, avf_dev_uninit);
1195 /* Adaptive virtual function driver struct */
1196 static struct rte_pci_driver rte_avf_pmd = {
1197 .id_table = pci_id_avf_map,
1198 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
1199 RTE_PCI_DRV_IOVA_AS_VA,
1200 .probe = eth_avf_pci_probe,
1201 .remove = eth_avf_pci_remove,
1204 RTE_PMD_REGISTER_PCI(net_avf, rte_avf_pmd);
1205 RTE_PMD_REGISTER_PCI_TABLE(net_avf, pci_id_avf_map);
1206 RTE_PMD_REGISTER_KMOD_DEP(net_avf, "* igb_uio | vfio-pci");
1207 RTE_INIT(avf_init_log);
1211 avf_logtype_init = rte_log_register("pmd.avf.init");
1212 if (avf_logtype_init >= 0)
1213 rte_log_set_level(avf_logtype_init, RTE_LOG_NOTICE);
1214 avf_logtype_driver = rte_log_register("pmd.avf.driver");
1215 if (avf_logtype_driver >= 0)
1216 rte_log_set_level(avf_logtype_driver, RTE_LOG_NOTICE);
1219 /* memory func for base code */
1220 enum avf_status_code
1221 avf_allocate_dma_mem_d(__rte_unused struct avf_hw *hw,
1222 struct avf_dma_mem *mem,
1226 const struct rte_memzone *mz = NULL;
1227 char z_name[RTE_MEMZONE_NAMESIZE];
1230 return AVF_ERR_PARAM;
1232 snprintf(z_name, sizeof(z_name), "avf_dma_%"PRIu64, rte_rand());
1233 mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0,
1234 alignment, RTE_PGSIZE_2M);
1236 return AVF_ERR_NO_MEMORY;
1240 mem->pa = mz->phys_addr;
1241 mem->zone = (const void *)mz;
1243 "memzone %s allocated with physical address: %"PRIu64,
1249 enum avf_status_code
1250 avf_free_dma_mem_d(__rte_unused struct avf_hw *hw,
1251 struct avf_dma_mem *mem)
1254 return AVF_ERR_PARAM;
1257 "memzone %s to be freed with physical address: %"PRIu64,
1258 ((const struct rte_memzone *)mem->zone)->name, mem->pa);
1259 rte_memzone_free((const struct rte_memzone *)mem->zone);
1267 enum avf_status_code
1268 avf_allocate_virt_mem_d(__rte_unused struct avf_hw *hw,
1269 struct avf_virt_mem *mem,
1273 return AVF_ERR_PARAM;
1276 mem->va = rte_zmalloc("avf", size, 0);
1281 return AVF_ERR_NO_MEMORY;
1284 enum avf_status_code
1285 avf_free_virt_mem_d(__rte_unused struct avf_hw *hw,
1286 struct avf_virt_mem *mem)
1289 return AVF_ERR_PARAM;