xref: /dpdk/drivers/net/iavf/iavf_ethdev.c (revision 4113ddd4)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
15 
16 #include <rte_interrupts.h>
17 #include <rte_debug.h>
18 #include <rte_pci.h>
19 #include <rte_atomic.h>
20 #include <rte_eal.h>
21 #include <rte_ether.h>
22 #include <ethdev_driver.h>
23 #include <ethdev_pci.h>
24 #include <rte_malloc.h>
25 #include <rte_memzone.h>
26 #include <rte_dev.h>
27 
28 #include "iavf.h"
29 #include "iavf_rxtx.h"
30 #include "iavf_generic_flow.h"
31 #include "rte_pmd_iavf.h"
32 
33 /* devargs */
34 #define IAVF_PROTO_XTR_ARG         "proto_xtr"
35 
36 static const char * const iavf_valid_args[] = {
37 	IAVF_PROTO_XTR_ARG,
38 	NULL
39 };
40 
41 static const struct rte_mbuf_dynfield iavf_proto_xtr_metadata_param = {
42 	.name = "intel_pmd_dynfield_proto_xtr_metadata",
43 	.size = sizeof(uint32_t),
44 	.align = __alignof__(uint32_t),
45 	.flags = 0,
46 };
47 
48 struct iavf_proto_xtr_ol {
49 	const struct rte_mbuf_dynflag param;
50 	uint64_t *ol_flag;
51 	bool required;
52 };
53 
54 static struct iavf_proto_xtr_ol iavf_proto_xtr_params[] = {
55 	[IAVF_PROTO_XTR_VLAN] = {
56 		.param = { .name = "intel_pmd_dynflag_proto_xtr_vlan" },
57 		.ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_vlan_mask },
58 	[IAVF_PROTO_XTR_IPV4] = {
59 		.param = { .name = "intel_pmd_dynflag_proto_xtr_ipv4" },
60 		.ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ipv4_mask },
61 	[IAVF_PROTO_XTR_IPV6] = {
62 		.param = { .name = "intel_pmd_dynflag_proto_xtr_ipv6" },
63 		.ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ipv6_mask },
64 	[IAVF_PROTO_XTR_IPV6_FLOW] = {
65 		.param = { .name = "intel_pmd_dynflag_proto_xtr_ipv6_flow" },
66 		.ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ipv6_flow_mask },
67 	[IAVF_PROTO_XTR_TCP] = {
68 		.param = { .name = "intel_pmd_dynflag_proto_xtr_tcp" },
69 		.ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_tcp_mask },
70 	[IAVF_PROTO_XTR_IP_OFFSET] = {
71 		.param = { .name = "intel_pmd_dynflag_proto_xtr_ip_offset" },
72 		.ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ip_offset_mask },
73 };
74 
75 static int iavf_dev_configure(struct rte_eth_dev *dev);
76 static int iavf_dev_start(struct rte_eth_dev *dev);
77 static int iavf_dev_stop(struct rte_eth_dev *dev);
78 static int iavf_dev_close(struct rte_eth_dev *dev);
79 static int iavf_dev_reset(struct rte_eth_dev *dev);
80 static int iavf_dev_info_get(struct rte_eth_dev *dev,
81 			     struct rte_eth_dev_info *dev_info);
82 static const uint32_t *iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev);
83 static int iavf_dev_stats_get(struct rte_eth_dev *dev,
84 			     struct rte_eth_stats *stats);
85 static int iavf_dev_stats_reset(struct rte_eth_dev *dev);
86 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
87 				 struct rte_eth_xstat *xstats, unsigned int n);
88 static int iavf_dev_xstats_get_names(struct rte_eth_dev *dev,
89 				       struct rte_eth_xstat_name *xstats_names,
90 				       unsigned int limit);
91 static int iavf_dev_promiscuous_enable(struct rte_eth_dev *dev);
92 static int iavf_dev_promiscuous_disable(struct rte_eth_dev *dev);
93 static int iavf_dev_allmulticast_enable(struct rte_eth_dev *dev);
94 static int iavf_dev_allmulticast_disable(struct rte_eth_dev *dev);
95 static int iavf_dev_add_mac_addr(struct rte_eth_dev *dev,
96 				struct rte_ether_addr *addr,
97 				uint32_t index,
98 				uint32_t pool);
99 static void iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index);
100 static int iavf_dev_vlan_filter_set(struct rte_eth_dev *dev,
101 				   uint16_t vlan_id, int on);
102 static int iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
103 static int iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
104 				   struct rte_eth_rss_reta_entry64 *reta_conf,
105 				   uint16_t reta_size);
106 static int iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
107 				  struct rte_eth_rss_reta_entry64 *reta_conf,
108 				  uint16_t reta_size);
109 static int iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
110 				   struct rte_eth_rss_conf *rss_conf);
111 static int iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
112 				     struct rte_eth_rss_conf *rss_conf);
113 static int iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
114 static int iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
115 					 struct rte_ether_addr *mac_addr);
116 static int iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev,
117 					uint16_t queue_id);
118 static int iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev,
119 					 uint16_t queue_id);
120 static int iavf_dev_flow_ops_get(struct rte_eth_dev *dev,
121 				 const struct rte_flow_ops **ops);
122 static int iavf_set_mc_addr_list(struct rte_eth_dev *dev,
123 			struct rte_ether_addr *mc_addrs,
124 			uint32_t mc_addrs_num);
125 
126 static const struct rte_pci_id pci_id_iavf_map[] = {
127 	{ RTE_PCI_DEVICE(IAVF_INTEL_VENDOR_ID, IAVF_DEV_ID_ADAPTIVE_VF) },
128 	{ .vendor_id = 0, /* sentinel */ },
129 };
130 
131 struct rte_iavf_xstats_name_off {
132 	char name[RTE_ETH_XSTATS_NAME_SIZE];
133 	unsigned int offset;
134 };
135 
136 static const struct rte_iavf_xstats_name_off rte_iavf_stats_strings[] = {
137 	{"rx_bytes", offsetof(struct iavf_eth_stats, rx_bytes)},
138 	{"rx_unicast_packets", offsetof(struct iavf_eth_stats, rx_unicast)},
139 	{"rx_multicast_packets", offsetof(struct iavf_eth_stats, rx_multicast)},
140 	{"rx_broadcast_packets", offsetof(struct iavf_eth_stats, rx_broadcast)},
141 	{"rx_dropped_packets", offsetof(struct iavf_eth_stats, rx_discards)},
142 	{"rx_unknown_protocol_packets", offsetof(struct iavf_eth_stats,
143 		rx_unknown_protocol)},
144 	{"tx_bytes", offsetof(struct iavf_eth_stats, tx_bytes)},
145 	{"tx_unicast_packets", offsetof(struct iavf_eth_stats, tx_unicast)},
146 	{"tx_multicast_packets", offsetof(struct iavf_eth_stats, tx_multicast)},
147 	{"tx_broadcast_packets", offsetof(struct iavf_eth_stats, tx_broadcast)},
148 	{"tx_dropped_packets", offsetof(struct iavf_eth_stats, tx_discards)},
149 	{"tx_error_packets", offsetof(struct iavf_eth_stats, tx_errors)},
150 };
151 
152 #define IAVF_NB_XSTATS (sizeof(rte_iavf_stats_strings) / \
153 		sizeof(rte_iavf_stats_strings[0]))
154 
155 static const struct eth_dev_ops iavf_eth_dev_ops = {
156 	.dev_configure              = iavf_dev_configure,
157 	.dev_start                  = iavf_dev_start,
158 	.dev_stop                   = iavf_dev_stop,
159 	.dev_close                  = iavf_dev_close,
160 	.dev_reset                  = iavf_dev_reset,
161 	.dev_infos_get              = iavf_dev_info_get,
162 	.dev_supported_ptypes_get   = iavf_dev_supported_ptypes_get,
163 	.link_update                = iavf_dev_link_update,
164 	.stats_get                  = iavf_dev_stats_get,
165 	.stats_reset                = iavf_dev_stats_reset,
166 	.xstats_get                 = iavf_dev_xstats_get,
167 	.xstats_get_names           = iavf_dev_xstats_get_names,
168 	.xstats_reset               = iavf_dev_stats_reset,
169 	.promiscuous_enable         = iavf_dev_promiscuous_enable,
170 	.promiscuous_disable        = iavf_dev_promiscuous_disable,
171 	.allmulticast_enable        = iavf_dev_allmulticast_enable,
172 	.allmulticast_disable       = iavf_dev_allmulticast_disable,
173 	.mac_addr_add               = iavf_dev_add_mac_addr,
174 	.mac_addr_remove            = iavf_dev_del_mac_addr,
175 	.set_mc_addr_list			= iavf_set_mc_addr_list,
176 	.vlan_filter_set            = iavf_dev_vlan_filter_set,
177 	.vlan_offload_set           = iavf_dev_vlan_offload_set,
178 	.rx_queue_start             = iavf_dev_rx_queue_start,
179 	.rx_queue_stop              = iavf_dev_rx_queue_stop,
180 	.tx_queue_start             = iavf_dev_tx_queue_start,
181 	.tx_queue_stop              = iavf_dev_tx_queue_stop,
182 	.rx_queue_setup             = iavf_dev_rx_queue_setup,
183 	.rx_queue_release           = iavf_dev_rx_queue_release,
184 	.tx_queue_setup             = iavf_dev_tx_queue_setup,
185 	.tx_queue_release           = iavf_dev_tx_queue_release,
186 	.mac_addr_set               = iavf_dev_set_default_mac_addr,
187 	.reta_update                = iavf_dev_rss_reta_update,
188 	.reta_query                 = iavf_dev_rss_reta_query,
189 	.rss_hash_update            = iavf_dev_rss_hash_update,
190 	.rss_hash_conf_get          = iavf_dev_rss_hash_conf_get,
191 	.rxq_info_get               = iavf_dev_rxq_info_get,
192 	.txq_info_get               = iavf_dev_txq_info_get,
193 	.mtu_set                    = iavf_dev_mtu_set,
194 	.rx_queue_intr_enable       = iavf_dev_rx_queue_intr_enable,
195 	.rx_queue_intr_disable      = iavf_dev_rx_queue_intr_disable,
196 	.flow_ops_get               = iavf_dev_flow_ops_get,
197 	.tx_done_cleanup	    = iavf_dev_tx_done_cleanup,
198 	.get_monitor_addr           = iavf_get_monitor_addr,
199 };
200 
201 static int
202 iavf_set_mc_addr_list(struct rte_eth_dev *dev,
203 			struct rte_ether_addr *mc_addrs,
204 			uint32_t mc_addrs_num)
205 {
206 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
207 	struct iavf_adapter *adapter =
208 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
209 	int err, ret;
210 
211 	if (mc_addrs_num > IAVF_NUM_MACADDR_MAX) {
212 		PMD_DRV_LOG(ERR,
213 			    "can't add more than a limited number (%u) of addresses.",
214 			    (uint32_t)IAVF_NUM_MACADDR_MAX);
215 		return -EINVAL;
216 	}
217 
218 	/* flush previous addresses */
219 	err = iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
220 					false);
221 	if (err)
222 		return err;
223 
224 	/* add new ones */
225 	err = iavf_add_del_mc_addr_list(adapter, mc_addrs, mc_addrs_num, true);
226 
227 	if (err) {
228 		/* if adding mac address list fails, should add the previous
229 		 * addresses back.
230 		 */
231 		ret = iavf_add_del_mc_addr_list(adapter, vf->mc_addrs,
232 						vf->mc_addrs_num, true);
233 		if (ret)
234 			return ret;
235 	} else {
236 		vf->mc_addrs_num = mc_addrs_num;
237 		memcpy(vf->mc_addrs,
238 		       mc_addrs, mc_addrs_num * sizeof(*mc_addrs));
239 	}
240 
241 	return err;
242 }
243 
244 static int
245 iavf_init_rss(struct iavf_adapter *adapter)
246 {
247 	struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
248 	struct rte_eth_rss_conf *rss_conf;
249 	uint16_t i, j, nb_q;
250 	int ret;
251 
252 	rss_conf = &adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
253 	nb_q = RTE_MIN(adapter->eth_dev->data->nb_rx_queues,
254 		       vf->max_rss_qregion);
255 
256 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF)) {
257 		PMD_DRV_LOG(DEBUG, "RSS is not supported");
258 		return -ENOTSUP;
259 	}
260 	if (adapter->eth_dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
261 		PMD_DRV_LOG(WARNING, "RSS is enabled by PF by default");
262 		/* set all lut items to default queue */
263 		for (i = 0; i < vf->vf_res->rss_lut_size; i++)
264 			vf->rss_lut[i] = 0;
265 		ret = iavf_configure_rss_lut(adapter);
266 		return ret;
267 	}
268 
269 	/* configure RSS key */
270 	if (!rss_conf->rss_key) {
271 		/* Calculate the default hash key */
272 		for (i = 0; i <= vf->vf_res->rss_key_size; i++)
273 			vf->rss_key[i] = (uint8_t)rte_rand();
274 	} else
275 		rte_memcpy(vf->rss_key, rss_conf->rss_key,
276 			   RTE_MIN(rss_conf->rss_key_len,
277 				   vf->vf_res->rss_key_size));
278 
279 	/* init RSS LUT table */
280 	for (i = 0, j = 0; i < vf->vf_res->rss_lut_size; i++, j++) {
281 		if (j >= nb_q)
282 			j = 0;
283 		vf->rss_lut[i] = j;
284 	}
285 	/* send virtchnnl ops to configure rss*/
286 	ret = iavf_configure_rss_lut(adapter);
287 	if (ret)
288 		return ret;
289 	ret = iavf_configure_rss_key(adapter);
290 	if (ret)
291 		return ret;
292 
293 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF) {
294 		/* Set RSS hash configuration based on rss_conf->rss_hf. */
295 		ret = iavf_rss_hash_set(adapter, rss_conf->rss_hf, true);
296 		if (ret) {
297 			PMD_DRV_LOG(ERR, "fail to set default RSS");
298 			return ret;
299 		}
300 	}
301 
302 	return 0;
303 }
304 
305 static int
306 iavf_queues_req_reset(struct rte_eth_dev *dev, uint16_t num)
307 {
308 	struct iavf_adapter *ad =
309 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
310 	struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(ad);
311 	int ret;
312 
313 	ret = iavf_request_queues(ad, num);
314 	if (ret) {
315 		PMD_DRV_LOG(ERR, "request queues from PF failed");
316 		return ret;
317 	}
318 	PMD_DRV_LOG(INFO, "change queue pairs from %u to %u",
319 			vf->vsi_res->num_queue_pairs, num);
320 
321 	ret = iavf_dev_reset(dev);
322 	if (ret) {
323 		PMD_DRV_LOG(ERR, "vf reset failed");
324 		return ret;
325 	}
326 
327 	return 0;
328 }
329 
330 static int
331 iavf_dev_vlan_insert_set(struct rte_eth_dev *dev)
332 {
333 	struct iavf_adapter *adapter =
334 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
335 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
336 	bool enable;
337 
338 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2))
339 		return 0;
340 
341 	enable = !!(dev->data->dev_conf.txmode.offloads &
342 		    DEV_TX_OFFLOAD_VLAN_INSERT);
343 	iavf_config_vlan_insert_v2(adapter, enable);
344 
345 	return 0;
346 }
347 
348 static int
349 iavf_dev_init_vlan(struct rte_eth_dev *dev)
350 {
351 	int err;
352 
353 	err = iavf_dev_vlan_offload_set(dev,
354 					ETH_VLAN_STRIP_MASK |
355 					ETH_QINQ_STRIP_MASK |
356 					ETH_VLAN_FILTER_MASK |
357 					ETH_VLAN_EXTEND_MASK);
358 	if (err) {
359 		PMD_DRV_LOG(ERR, "Failed to update vlan offload");
360 		return err;
361 	}
362 
363 	err = iavf_dev_vlan_insert_set(dev);
364 	if (err)
365 		PMD_DRV_LOG(ERR, "Failed to update vlan insertion");
366 
367 	return err;
368 }
369 
370 static int
371 iavf_dev_configure(struct rte_eth_dev *dev)
372 {
373 	struct iavf_adapter *ad =
374 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
375 	struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(ad);
376 	uint16_t num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
377 		dev->data->nb_tx_queues);
378 	int ret;
379 
380 	ad->rx_bulk_alloc_allowed = true;
381 	/* Initialize to TRUE. If any of Rx queues doesn't meet the
382 	 * vector Rx/Tx preconditions, it will be reset.
383 	 */
384 	ad->rx_vec_allowed = true;
385 	ad->tx_vec_allowed = true;
386 
387 	if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
388 		dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
389 
390 	/* Large VF setting */
391 	if (num_queue_pairs > IAVF_MAX_NUM_QUEUES_DFLT) {
392 		if (!(vf->vf_res->vf_cap_flags &
393 				VIRTCHNL_VF_LARGE_NUM_QPAIRS)) {
394 			PMD_DRV_LOG(ERR, "large VF is not supported");
395 			return -1;
396 		}
397 
398 		if (num_queue_pairs > IAVF_MAX_NUM_QUEUES_LV) {
399 			PMD_DRV_LOG(ERR, "queue pairs number cannot be larger than %u",
400 				IAVF_MAX_NUM_QUEUES_LV);
401 			return -1;
402 		}
403 
404 		ret = iavf_queues_req_reset(dev, num_queue_pairs);
405 		if (ret)
406 			return ret;
407 
408 		ret = iavf_get_max_rss_queue_region(ad);
409 		if (ret) {
410 			PMD_INIT_LOG(ERR, "get max rss queue region failed");
411 			return ret;
412 		}
413 
414 		vf->lv_enabled = true;
415 	} else {
416 		/* Check if large VF is already enabled. If so, disable and
417 		 * release redundant queue resource.
418 		 * Or check if enough queue pairs. If not, request them from PF.
419 		 */
420 		if (vf->lv_enabled ||
421 		    num_queue_pairs > vf->vsi_res->num_queue_pairs) {
422 			ret = iavf_queues_req_reset(dev, num_queue_pairs);
423 			if (ret)
424 				return ret;
425 
426 			vf->lv_enabled = false;
427 		}
428 		/* if large VF is not required, use default rss queue region */
429 		vf->max_rss_qregion = IAVF_MAX_NUM_QUEUES_DFLT;
430 	}
431 
432 	ret = iavf_dev_init_vlan(dev);
433 	if (ret)
434 		PMD_DRV_LOG(ERR, "configure VLAN failed: %d", ret);
435 
436 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
437 		if (iavf_init_rss(ad) != 0) {
438 			PMD_DRV_LOG(ERR, "configure rss failed");
439 			return -1;
440 		}
441 	}
442 	return 0;
443 }
444 
445 static int
446 iavf_init_rxq(struct rte_eth_dev *dev, struct iavf_rx_queue *rxq)
447 {
448 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
449 	struct rte_eth_dev_data *dev_data = dev->data;
450 	uint16_t buf_size, max_pkt_len, len;
451 
452 	buf_size = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
453 
454 	/* Calculate the maximum packet length allowed */
455 	len = rxq->rx_buf_len * IAVF_MAX_CHAINED_RX_BUFFERS;
456 	max_pkt_len = RTE_MIN(len, dev->data->dev_conf.rxmode.max_rx_pkt_len);
457 
458 	/* Check if the jumbo frame and maximum packet length are set
459 	 * correctly.
460 	 */
461 	if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
462 		if (max_pkt_len <= IAVF_ETH_MAX_LEN ||
463 		    max_pkt_len > IAVF_FRAME_SIZE_MAX) {
464 			PMD_DRV_LOG(ERR, "maximum packet length must be "
465 				    "larger than %u and smaller than %u, "
466 				    "as jumbo frame is enabled",
467 				    (uint32_t)IAVF_ETH_MAX_LEN,
468 				    (uint32_t)IAVF_FRAME_SIZE_MAX);
469 			return -EINVAL;
470 		}
471 	} else {
472 		if (max_pkt_len < RTE_ETHER_MIN_LEN ||
473 		    max_pkt_len > IAVF_ETH_MAX_LEN) {
474 			PMD_DRV_LOG(ERR, "maximum packet length must be "
475 				    "larger than %u and smaller than %u, "
476 				    "as jumbo frame is disabled",
477 				    (uint32_t)RTE_ETHER_MIN_LEN,
478 				    (uint32_t)IAVF_ETH_MAX_LEN);
479 			return -EINVAL;
480 		}
481 	}
482 
483 	rxq->max_pkt_len = max_pkt_len;
484 	if ((dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) ||
485 	    rxq->max_pkt_len > buf_size) {
486 		dev_data->scattered_rx = 1;
487 	}
488 	IAVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
489 	IAVF_WRITE_FLUSH(hw);
490 
491 	return 0;
492 }
493 
494 static int
495 iavf_init_queues(struct rte_eth_dev *dev)
496 {
497 	struct iavf_rx_queue **rxq =
498 		(struct iavf_rx_queue **)dev->data->rx_queues;
499 	int i, ret = IAVF_SUCCESS;
500 
501 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
502 		if (!rxq[i] || !rxq[i]->q_set)
503 			continue;
504 		ret = iavf_init_rxq(dev, rxq[i]);
505 		if (ret != IAVF_SUCCESS)
506 			break;
507 	}
508 	/* set rx/tx function to vector/scatter/single-segment
509 	 * according to parameters
510 	 */
511 	iavf_set_rx_function(dev);
512 	iavf_set_tx_function(dev);
513 
514 	return ret;
515 }
516 
517 static int iavf_config_rx_queues_irqs(struct rte_eth_dev *dev,
518 				     struct rte_intr_handle *intr_handle)
519 {
520 	struct iavf_adapter *adapter =
521 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
522 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
523 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
524 	struct iavf_qv_map *qv_map;
525 	uint16_t interval, i;
526 	int vec;
527 
528 	if (rte_intr_cap_multiple(intr_handle) &&
529 	    dev->data->dev_conf.intr_conf.rxq) {
530 		if (rte_intr_efd_enable(intr_handle, dev->data->nb_rx_queues))
531 			return -1;
532 	}
533 
534 	if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
535 		intr_handle->intr_vec =
536 			rte_zmalloc("intr_vec",
537 				    dev->data->nb_rx_queues * sizeof(int), 0);
538 		if (!intr_handle->intr_vec) {
539 			PMD_DRV_LOG(ERR, "Failed to allocate %d rx intr_vec",
540 				    dev->data->nb_rx_queues);
541 			return -1;
542 		}
543 	}
544 
545 	qv_map = rte_zmalloc("qv_map",
546 		dev->data->nb_rx_queues * sizeof(struct iavf_qv_map), 0);
547 	if (!qv_map) {
548 		PMD_DRV_LOG(ERR, "Failed to allocate %d queue-vector map",
549 				dev->data->nb_rx_queues);
550 		return -1;
551 	}
552 
553 	if (!dev->data->dev_conf.intr_conf.rxq ||
554 	    !rte_intr_dp_is_en(intr_handle)) {
555 		/* Rx interrupt disabled, Map interrupt only for writeback */
556 		vf->nb_msix = 1;
557 		if (vf->vf_res->vf_cap_flags &
558 		    VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
559 			/* If WB_ON_ITR supports, enable it */
560 			vf->msix_base = IAVF_RX_VEC_START;
561 			/* Set the ITR for index zero, to 2us to make sure that
562 			 * we leave time for aggregation to occur, but don't
563 			 * increase latency dramatically.
564 			 */
565 			IAVF_WRITE_REG(hw,
566 				       IAVF_VFINT_DYN_CTLN1(vf->msix_base - 1),
567 				       (0 << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
568 				       IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
569 				       (2UL << IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT));
570 			/* debug - check for success! the return value
571 			 * should be 2, offset is 0x2800
572 			 */
573 			/* IAVF_READ_REG(hw, IAVF_VFINT_ITRN1(0, 0)); */
574 		} else {
575 			/* If no WB_ON_ITR offload flags, need to set
576 			 * interrupt for descriptor write back.
577 			 */
578 			vf->msix_base = IAVF_MISC_VEC_ID;
579 
580 			/* set ITR to max */
581 			interval = iavf_calc_itr_interval(
582 					IAVF_QUEUE_ITR_INTERVAL_MAX);
583 			IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
584 				       IAVF_VFINT_DYN_CTL01_INTENA_MASK |
585 				       (IAVF_ITR_INDEX_DEFAULT <<
586 					IAVF_VFINT_DYN_CTL01_ITR_INDX_SHIFT) |
587 				       (interval <<
588 					IAVF_VFINT_DYN_CTL01_INTERVAL_SHIFT));
589 		}
590 		IAVF_WRITE_FLUSH(hw);
591 		/* map all queues to the same interrupt */
592 		for (i = 0; i < dev->data->nb_rx_queues; i++) {
593 			qv_map[i].queue_id = i;
594 			qv_map[i].vector_id = vf->msix_base;
595 		}
596 		vf->qv_map = qv_map;
597 	} else {
598 		if (!rte_intr_allow_others(intr_handle)) {
599 			vf->nb_msix = 1;
600 			vf->msix_base = IAVF_MISC_VEC_ID;
601 			for (i = 0; i < dev->data->nb_rx_queues; i++) {
602 				qv_map[i].queue_id = i;
603 				qv_map[i].vector_id = vf->msix_base;
604 				intr_handle->intr_vec[i] = IAVF_MISC_VEC_ID;
605 			}
606 			vf->qv_map = qv_map;
607 			PMD_DRV_LOG(DEBUG,
608 				    "vector %u are mapping to all Rx queues",
609 				    vf->msix_base);
610 		} else {
611 			/* If Rx interrupt is reuquired, and we can use
612 			 * multi interrupts, then the vec is from 1
613 			 */
614 			vf->nb_msix = RTE_MIN(intr_handle->nb_efd,
615 				 (uint16_t)(vf->vf_res->max_vectors - 1));
616 			vf->msix_base = IAVF_RX_VEC_START;
617 			vec = IAVF_RX_VEC_START;
618 			for (i = 0; i < dev->data->nb_rx_queues; i++) {
619 				qv_map[i].queue_id = i;
620 				qv_map[i].vector_id = vec;
621 				intr_handle->intr_vec[i] = vec++;
622 				if (vec >= vf->nb_msix + IAVF_RX_VEC_START)
623 					vec = IAVF_RX_VEC_START;
624 			}
625 			vf->qv_map = qv_map;
626 			PMD_DRV_LOG(DEBUG,
627 				    "%u vectors are mapping to %u Rx queues",
628 				    vf->nb_msix, dev->data->nb_rx_queues);
629 		}
630 	}
631 
632 	if (!vf->lv_enabled) {
633 		if (iavf_config_irq_map(adapter)) {
634 			PMD_DRV_LOG(ERR, "config interrupt mapping failed");
635 			return -1;
636 		}
637 	} else {
638 		uint16_t num_qv_maps = dev->data->nb_rx_queues;
639 		uint16_t index = 0;
640 
641 		while (num_qv_maps > IAVF_IRQ_MAP_NUM_PER_BUF) {
642 			if (iavf_config_irq_map_lv(adapter,
643 					IAVF_IRQ_MAP_NUM_PER_BUF, index)) {
644 				PMD_DRV_LOG(ERR, "config interrupt mapping for large VF failed");
645 				return -1;
646 			}
647 			num_qv_maps -= IAVF_IRQ_MAP_NUM_PER_BUF;
648 			index += IAVF_IRQ_MAP_NUM_PER_BUF;
649 		}
650 
651 		if (iavf_config_irq_map_lv(adapter, num_qv_maps, index)) {
652 			PMD_DRV_LOG(ERR, "config interrupt mapping for large VF failed");
653 			return -1;
654 		}
655 	}
656 	return 0;
657 }
658 
659 static int
660 iavf_start_queues(struct rte_eth_dev *dev)
661 {
662 	struct iavf_rx_queue *rxq;
663 	struct iavf_tx_queue *txq;
664 	int i;
665 
666 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
667 		txq = dev->data->tx_queues[i];
668 		if (txq->tx_deferred_start)
669 			continue;
670 		if (iavf_dev_tx_queue_start(dev, i) != 0) {
671 			PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
672 			return -1;
673 		}
674 	}
675 
676 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
677 		rxq = dev->data->rx_queues[i];
678 		if (rxq->rx_deferred_start)
679 			continue;
680 		if (iavf_dev_rx_queue_start(dev, i) != 0) {
681 			PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
682 			return -1;
683 		}
684 	}
685 
686 	return 0;
687 }
688 
689 static int
690 iavf_dev_start(struct rte_eth_dev *dev)
691 {
692 	struct iavf_adapter *adapter =
693 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
694 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
695 	struct rte_intr_handle *intr_handle = dev->intr_handle;
696 	uint16_t num_queue_pairs;
697 	uint16_t index = 0;
698 
699 	PMD_INIT_FUNC_TRACE();
700 
701 	adapter->stopped = 0;
702 
703 	vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
704 	vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
705 				      dev->data->nb_tx_queues);
706 	num_queue_pairs = vf->num_queue_pairs;
707 
708 	if (iavf_init_queues(dev) != 0) {
709 		PMD_DRV_LOG(ERR, "failed to do Queue init");
710 		return -1;
711 	}
712 
713 	/* If needed, send configure queues msg multiple times to make the
714 	 * adminq buffer length smaller than the 4K limitation.
715 	 */
716 	while (num_queue_pairs > IAVF_CFG_Q_NUM_PER_BUF) {
717 		if (iavf_configure_queues(adapter,
718 				IAVF_CFG_Q_NUM_PER_BUF, index) != 0) {
719 			PMD_DRV_LOG(ERR, "configure queues failed");
720 			goto err_queue;
721 		}
722 		num_queue_pairs -= IAVF_CFG_Q_NUM_PER_BUF;
723 		index += IAVF_CFG_Q_NUM_PER_BUF;
724 	}
725 
726 	if (iavf_configure_queues(adapter, num_queue_pairs, index) != 0) {
727 		PMD_DRV_LOG(ERR, "configure queues failed");
728 		goto err_queue;
729 	}
730 
731 	if (iavf_config_rx_queues_irqs(dev, intr_handle) != 0) {
732 		PMD_DRV_LOG(ERR, "configure irq failed");
733 		goto err_queue;
734 	}
735 	/* re-enable intr again, because efd assign may change */
736 	if (dev->data->dev_conf.intr_conf.rxq != 0) {
737 		rte_intr_disable(intr_handle);
738 		rte_intr_enable(intr_handle);
739 	}
740 
741 	/* Set all mac addrs */
742 	iavf_add_del_all_mac_addr(adapter, true);
743 
744 	/* Set all multicast addresses */
745 	iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
746 				  true);
747 
748 	if (iavf_start_queues(dev) != 0) {
749 		PMD_DRV_LOG(ERR, "enable queues failed");
750 		goto err_mac;
751 	}
752 
753 	return 0;
754 
755 err_mac:
756 	iavf_add_del_all_mac_addr(adapter, false);
757 err_queue:
758 	return -1;
759 }
760 
761 static int
762 iavf_dev_stop(struct rte_eth_dev *dev)
763 {
764 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
765 	struct iavf_adapter *adapter =
766 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
767 	struct rte_intr_handle *intr_handle = dev->intr_handle;
768 
769 	PMD_INIT_FUNC_TRACE();
770 
771 	if (adapter->stopped == 1)
772 		return 0;
773 
774 	iavf_stop_queues(dev);
775 
776 	/* Disable the interrupt for Rx */
777 	rte_intr_efd_disable(intr_handle);
778 	/* Rx interrupt vector mapping free */
779 	if (intr_handle->intr_vec) {
780 		rte_free(intr_handle->intr_vec);
781 		intr_handle->intr_vec = NULL;
782 	}
783 
784 	/* remove all mac addrs */
785 	iavf_add_del_all_mac_addr(adapter, false);
786 
787 	/* remove all multicast addresses */
788 	iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
789 				  false);
790 
791 	adapter->stopped = 1;
792 	dev->data->dev_started = 0;
793 
794 	return 0;
795 }
796 
797 static int
798 iavf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
799 {
800 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
801 
802 	dev_info->max_rx_queues = IAVF_MAX_NUM_QUEUES_LV;
803 	dev_info->max_tx_queues = IAVF_MAX_NUM_QUEUES_LV;
804 	dev_info->min_rx_bufsize = IAVF_BUF_SIZE_MIN;
805 	dev_info->max_rx_pktlen = IAVF_FRAME_SIZE_MAX;
806 	dev_info->max_mtu = dev_info->max_rx_pktlen - IAVF_ETH_OVERHEAD;
807 	dev_info->min_mtu = RTE_ETHER_MIN_MTU;
808 	dev_info->hash_key_size = vf->vf_res->rss_key_size;
809 	dev_info->reta_size = vf->vf_res->rss_lut_size;
810 	dev_info->flow_type_rss_offloads = IAVF_RSS_OFFLOAD_ALL;
811 	dev_info->max_mac_addrs = IAVF_NUM_MACADDR_MAX;
812 	dev_info->rx_offload_capa =
813 		DEV_RX_OFFLOAD_VLAN_STRIP |
814 		DEV_RX_OFFLOAD_QINQ_STRIP |
815 		DEV_RX_OFFLOAD_IPV4_CKSUM |
816 		DEV_RX_OFFLOAD_UDP_CKSUM |
817 		DEV_RX_OFFLOAD_TCP_CKSUM |
818 		DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
819 		DEV_RX_OFFLOAD_SCATTER |
820 		DEV_RX_OFFLOAD_JUMBO_FRAME |
821 		DEV_RX_OFFLOAD_VLAN_FILTER |
822 		DEV_RX_OFFLOAD_RSS_HASH;
823 
824 	dev_info->tx_offload_capa =
825 		DEV_TX_OFFLOAD_VLAN_INSERT |
826 		DEV_TX_OFFLOAD_QINQ_INSERT |
827 		DEV_TX_OFFLOAD_IPV4_CKSUM |
828 		DEV_TX_OFFLOAD_UDP_CKSUM |
829 		DEV_TX_OFFLOAD_TCP_CKSUM |
830 		DEV_TX_OFFLOAD_SCTP_CKSUM |
831 		DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
832 		DEV_TX_OFFLOAD_TCP_TSO |
833 		DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
834 		DEV_TX_OFFLOAD_GRE_TNL_TSO |
835 		DEV_TX_OFFLOAD_IPIP_TNL_TSO |
836 		DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
837 		DEV_TX_OFFLOAD_MULTI_SEGS |
838 		DEV_TX_OFFLOAD_MBUF_FAST_FREE;
839 
840 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_CRC)
841 		dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_KEEP_CRC;
842 
843 	dev_info->default_rxconf = (struct rte_eth_rxconf) {
844 		.rx_free_thresh = IAVF_DEFAULT_RX_FREE_THRESH,
845 		.rx_drop_en = 0,
846 		.offloads = 0,
847 	};
848 
849 	dev_info->default_txconf = (struct rte_eth_txconf) {
850 		.tx_free_thresh = IAVF_DEFAULT_TX_FREE_THRESH,
851 		.tx_rs_thresh = IAVF_DEFAULT_TX_RS_THRESH,
852 		.offloads = 0,
853 	};
854 
855 	dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
856 		.nb_max = IAVF_MAX_RING_DESC,
857 		.nb_min = IAVF_MIN_RING_DESC,
858 		.nb_align = IAVF_ALIGN_RING_DESC,
859 	};
860 
861 	dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
862 		.nb_max = IAVF_MAX_RING_DESC,
863 		.nb_min = IAVF_MIN_RING_DESC,
864 		.nb_align = IAVF_ALIGN_RING_DESC,
865 	};
866 
867 	return 0;
868 }
869 
870 static const uint32_t *
871 iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
872 {
873 	static const uint32_t ptypes[] = {
874 		RTE_PTYPE_L2_ETHER,
875 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
876 		RTE_PTYPE_L4_FRAG,
877 		RTE_PTYPE_L4_ICMP,
878 		RTE_PTYPE_L4_NONFRAG,
879 		RTE_PTYPE_L4_SCTP,
880 		RTE_PTYPE_L4_TCP,
881 		RTE_PTYPE_L4_UDP,
882 		RTE_PTYPE_UNKNOWN
883 	};
884 	return ptypes;
885 }
886 
887 int
888 iavf_dev_link_update(struct rte_eth_dev *dev,
889 		    __rte_unused int wait_to_complete)
890 {
891 	struct rte_eth_link new_link;
892 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
893 
894 	memset(&new_link, 0, sizeof(new_link));
895 
896 	/* Only read status info stored in VF, and the info is updated
897 	 *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
898 	 */
899 	switch (vf->link_speed) {
900 	case 10:
901 		new_link.link_speed = ETH_SPEED_NUM_10M;
902 		break;
903 	case 100:
904 		new_link.link_speed = ETH_SPEED_NUM_100M;
905 		break;
906 	case 1000:
907 		new_link.link_speed = ETH_SPEED_NUM_1G;
908 		break;
909 	case 10000:
910 		new_link.link_speed = ETH_SPEED_NUM_10G;
911 		break;
912 	case 20000:
913 		new_link.link_speed = ETH_SPEED_NUM_20G;
914 		break;
915 	case 25000:
916 		new_link.link_speed = ETH_SPEED_NUM_25G;
917 		break;
918 	case 40000:
919 		new_link.link_speed = ETH_SPEED_NUM_40G;
920 		break;
921 	case 50000:
922 		new_link.link_speed = ETH_SPEED_NUM_50G;
923 		break;
924 	case 100000:
925 		new_link.link_speed = ETH_SPEED_NUM_100G;
926 		break;
927 	default:
928 		new_link.link_speed = ETH_SPEED_NUM_NONE;
929 		break;
930 	}
931 
932 	new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
933 	new_link.link_status = vf->link_up ? ETH_LINK_UP :
934 					     ETH_LINK_DOWN;
935 	new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
936 				ETH_LINK_SPEED_FIXED);
937 
938 	return rte_eth_linkstatus_set(dev, &new_link);
939 }
940 
941 static int
942 iavf_dev_promiscuous_enable(struct rte_eth_dev *dev)
943 {
944 	struct iavf_adapter *adapter =
945 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
946 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
947 
948 	return iavf_config_promisc(adapter,
949 				  true, vf->promisc_multicast_enabled);
950 }
951 
952 static int
953 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
954 {
955 	struct iavf_adapter *adapter =
956 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
957 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
958 
959 	return iavf_config_promisc(adapter,
960 				  false, vf->promisc_multicast_enabled);
961 }
962 
963 static int
964 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
965 {
966 	struct iavf_adapter *adapter =
967 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
968 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
969 
970 	return iavf_config_promisc(adapter,
971 				  vf->promisc_unicast_enabled, true);
972 }
973 
974 static int
975 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
976 {
977 	struct iavf_adapter *adapter =
978 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
979 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
980 
981 	return iavf_config_promisc(adapter,
982 				  vf->promisc_unicast_enabled, false);
983 }
984 
985 static int
986 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr,
987 		     __rte_unused uint32_t index,
988 		     __rte_unused uint32_t pool)
989 {
990 	struct iavf_adapter *adapter =
991 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
992 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
993 	int err;
994 
995 	if (rte_is_zero_ether_addr(addr)) {
996 		PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
997 		return -EINVAL;
998 	}
999 
1000 	err = iavf_add_del_eth_addr(adapter, addr, true);
1001 	if (err) {
1002 		PMD_DRV_LOG(ERR, "fail to add MAC address");
1003 		return -EIO;
1004 	}
1005 
1006 	vf->mac_num++;
1007 
1008 	return 0;
1009 }
1010 
1011 static void
1012 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
1013 {
1014 	struct iavf_adapter *adapter =
1015 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1016 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1017 	struct rte_ether_addr *addr;
1018 	int err;
1019 
1020 	addr = &dev->data->mac_addrs[index];
1021 
1022 	err = iavf_add_del_eth_addr(adapter, addr, false);
1023 	if (err)
1024 		PMD_DRV_LOG(ERR, "fail to delete MAC address");
1025 
1026 	vf->mac_num--;
1027 }
1028 
1029 static int
1030 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1031 {
1032 	struct iavf_adapter *adapter =
1033 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1034 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1035 	int err;
1036 
1037 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
1038 		err = iavf_add_del_vlan_v2(adapter, vlan_id, on);
1039 		if (err)
1040 			return -EIO;
1041 		return 0;
1042 	}
1043 
1044 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
1045 		return -ENOTSUP;
1046 
1047 	err = iavf_add_del_vlan(adapter, vlan_id, on);
1048 	if (err)
1049 		return -EIO;
1050 	return 0;
1051 }
1052 
1053 static void
1054 iavf_iterate_vlan_filters_v2(struct rte_eth_dev *dev, bool enable)
1055 {
1056 	struct rte_vlan_filter_conf *vfc = &dev->data->vlan_filter_conf;
1057 	struct iavf_adapter *adapter =
1058 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1059 	uint32_t i, j;
1060 	uint64_t ids;
1061 
1062 	for (i = 0; i < RTE_DIM(vfc->ids); i++) {
1063 		if (vfc->ids[i] == 0)
1064 			continue;
1065 
1066 		ids = vfc->ids[i];
1067 		for (j = 0; ids != 0 && j < 64; j++, ids >>= 1) {
1068 			if (ids & 1)
1069 				iavf_add_del_vlan_v2(adapter,
1070 						     64 * i + j, enable);
1071 		}
1072 	}
1073 }
1074 
1075 static int
1076 iavf_dev_vlan_offload_set_v2(struct rte_eth_dev *dev, int mask)
1077 {
1078 	struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1079 	struct iavf_adapter *adapter =
1080 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1081 	bool enable;
1082 	int err;
1083 
1084 	if (mask & ETH_VLAN_FILTER_MASK) {
1085 		enable = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER);
1086 
1087 		iavf_iterate_vlan_filters_v2(dev, enable);
1088 	}
1089 
1090 	if (mask & ETH_VLAN_STRIP_MASK) {
1091 		enable = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1092 
1093 		err = iavf_config_vlan_strip_v2(adapter, enable);
1094 		/* If not support, the stripping is already disabled by PF */
1095 		if (err == -ENOTSUP && !enable)
1096 			err = 0;
1097 		if (err)
1098 			return -EIO;
1099 	}
1100 
1101 	return 0;
1102 }
1103 
1104 static int
1105 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1106 {
1107 	struct iavf_adapter *adapter =
1108 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1109 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1110 	struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1111 	int err;
1112 
1113 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2)
1114 		return iavf_dev_vlan_offload_set_v2(dev, mask);
1115 
1116 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
1117 		return -ENOTSUP;
1118 
1119 	/* Vlan stripping setting */
1120 	if (mask & ETH_VLAN_STRIP_MASK) {
1121 		/* Enable or disable VLAN stripping */
1122 		if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1123 			err = iavf_enable_vlan_strip(adapter);
1124 		else
1125 			err = iavf_disable_vlan_strip(adapter);
1126 
1127 		if (err)
1128 			return -EIO;
1129 	}
1130 	return 0;
1131 }
1132 
1133 static int
1134 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
1135 			struct rte_eth_rss_reta_entry64 *reta_conf,
1136 			uint16_t reta_size)
1137 {
1138 	struct iavf_adapter *adapter =
1139 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1140 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1141 	uint8_t *lut;
1142 	uint16_t i, idx, shift;
1143 	int ret;
1144 
1145 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1146 		return -ENOTSUP;
1147 
1148 	if (reta_size != vf->vf_res->rss_lut_size) {
1149 		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1150 			"(%d) doesn't match the number of hardware can "
1151 			"support (%d)", reta_size, vf->vf_res->rss_lut_size);
1152 		return -EINVAL;
1153 	}
1154 
1155 	lut = rte_zmalloc("rss_lut", reta_size, 0);
1156 	if (!lut) {
1157 		PMD_DRV_LOG(ERR, "No memory can be allocated");
1158 		return -ENOMEM;
1159 	}
1160 	/* store the old lut table temporarily */
1161 	rte_memcpy(lut, vf->rss_lut, reta_size);
1162 
1163 	for (i = 0; i < reta_size; i++) {
1164 		idx = i / RTE_RETA_GROUP_SIZE;
1165 		shift = i % RTE_RETA_GROUP_SIZE;
1166 		if (reta_conf[idx].mask & (1ULL << shift))
1167 			lut[i] = reta_conf[idx].reta[shift];
1168 	}
1169 
1170 	rte_memcpy(vf->rss_lut, lut, reta_size);
1171 	/* send virtchnnl ops to configure rss*/
1172 	ret = iavf_configure_rss_lut(adapter);
1173 	if (ret) /* revert back */
1174 		rte_memcpy(vf->rss_lut, lut, reta_size);
1175 	rte_free(lut);
1176 
1177 	return ret;
1178 }
1179 
1180 static int
1181 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
1182 		       struct rte_eth_rss_reta_entry64 *reta_conf,
1183 		       uint16_t reta_size)
1184 {
1185 	struct iavf_adapter *adapter =
1186 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1187 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1188 	uint16_t i, idx, shift;
1189 
1190 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1191 		return -ENOTSUP;
1192 
1193 	if (reta_size != vf->vf_res->rss_lut_size) {
1194 		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1195 			"(%d) doesn't match the number of hardware can "
1196 			"support (%d)", reta_size, vf->vf_res->rss_lut_size);
1197 		return -EINVAL;
1198 	}
1199 
1200 	for (i = 0; i < reta_size; i++) {
1201 		idx = i / RTE_RETA_GROUP_SIZE;
1202 		shift = i % RTE_RETA_GROUP_SIZE;
1203 		if (reta_conf[idx].mask & (1ULL << shift))
1204 			reta_conf[idx].reta[shift] = vf->rss_lut[i];
1205 	}
1206 
1207 	return 0;
1208 }
1209 
1210 static int
1211 iavf_set_rss_key(struct iavf_adapter *adapter, uint8_t *key, uint8_t key_len)
1212 {
1213 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1214 
1215 	/* HENA setting, it is enabled by default, no change */
1216 	if (!key || key_len == 0) {
1217 		PMD_DRV_LOG(DEBUG, "No key to be configured");
1218 		return 0;
1219 	} else if (key_len != vf->vf_res->rss_key_size) {
1220 		PMD_DRV_LOG(ERR, "The size of hash key configured "
1221 			"(%d) doesn't match the size of hardware can "
1222 			"support (%d)", key_len,
1223 			vf->vf_res->rss_key_size);
1224 		return -EINVAL;
1225 	}
1226 
1227 	rte_memcpy(vf->rss_key, key, key_len);
1228 
1229 	return iavf_configure_rss_key(adapter);
1230 }
1231 
1232 static int
1233 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
1234 			struct rte_eth_rss_conf *rss_conf)
1235 {
1236 	struct iavf_adapter *adapter =
1237 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1238 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1239 	int ret;
1240 
1241 	adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf = *rss_conf;
1242 
1243 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1244 		return -ENOTSUP;
1245 
1246 	/* Set hash key. */
1247 	ret = iavf_set_rss_key(adapter, rss_conf->rss_key,
1248 			       rss_conf->rss_key_len);
1249 	if (ret)
1250 		return ret;
1251 
1252 	if (rss_conf->rss_hf == 0) {
1253 		vf->rss_hf = 0;
1254 		ret = iavf_set_hena(adapter, 0);
1255 
1256 		/* It is a workaround, temporarily allow error to be returned
1257 		 * due to possible lack of PF handling for hena = 0.
1258 		 */
1259 		if (ret)
1260 			PMD_DRV_LOG(WARNING, "fail to clean existing RSS, lack PF support");
1261 		return 0;
1262 	}
1263 
1264 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF) {
1265 		/* Clear existing RSS. */
1266 		ret = iavf_set_hena(adapter, 0);
1267 
1268 		/* It is a workaround, temporarily allow error to be returned
1269 		 * due to possible lack of PF handling for hena = 0.
1270 		 */
1271 		if (ret)
1272 			PMD_DRV_LOG(WARNING, "fail to clean existing RSS,"
1273 				    "lack PF support");
1274 
1275 		/* Set new RSS configuration. */
1276 		ret = iavf_rss_hash_set(adapter, rss_conf->rss_hf, true);
1277 		if (ret) {
1278 			PMD_DRV_LOG(ERR, "fail to set new RSS");
1279 			return ret;
1280 		}
1281 	}
1282 
1283 	return 0;
1284 }
1285 
1286 static int
1287 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1288 			  struct rte_eth_rss_conf *rss_conf)
1289 {
1290 	struct iavf_adapter *adapter =
1291 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1292 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1293 
1294 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1295 		return -ENOTSUP;
1296 
1297 	rss_conf->rss_hf = vf->rss_hf;
1298 
1299 	if (!rss_conf->rss_key)
1300 		return 0;
1301 
1302 	rss_conf->rss_key_len = vf->vf_res->rss_key_size;
1303 	rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
1304 
1305 	return 0;
1306 }
1307 
1308 static int
1309 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1310 {
1311 	uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
1312 	int ret = 0;
1313 
1314 	if (mtu < RTE_ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
1315 		return -EINVAL;
1316 
1317 	/* mtu setting is forbidden if port is start */
1318 	if (dev->data->dev_started) {
1319 		PMD_DRV_LOG(ERR, "port must be stopped before configuration");
1320 		return -EBUSY;
1321 	}
1322 
1323 	if (frame_size > IAVF_ETH_MAX_LEN)
1324 		dev->data->dev_conf.rxmode.offloads |=
1325 				DEV_RX_OFFLOAD_JUMBO_FRAME;
1326 	else
1327 		dev->data->dev_conf.rxmode.offloads &=
1328 				~DEV_RX_OFFLOAD_JUMBO_FRAME;
1329 
1330 	dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1331 
1332 	return ret;
1333 }
1334 
1335 static int
1336 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
1337 			     struct rte_ether_addr *mac_addr)
1338 {
1339 	struct iavf_adapter *adapter =
1340 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1341 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1342 	struct rte_ether_addr *perm_addr, *old_addr;
1343 	int ret;
1344 
1345 	old_addr = (struct rte_ether_addr *)hw->mac.addr;
1346 	perm_addr = (struct rte_ether_addr *)hw->mac.perm_addr;
1347 
1348 	/* If the MAC address is configured by host, skip the setting */
1349 	if (rte_is_valid_assigned_ether_addr(perm_addr))
1350 		return -EPERM;
1351 
1352 	ret = iavf_add_del_eth_addr(adapter, old_addr, false);
1353 	if (ret)
1354 		PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
1355 			    " %02X:%02X:%02X:%02X:%02X:%02X",
1356 			    old_addr->addr_bytes[0],
1357 			    old_addr->addr_bytes[1],
1358 			    old_addr->addr_bytes[2],
1359 			    old_addr->addr_bytes[3],
1360 			    old_addr->addr_bytes[4],
1361 			    old_addr->addr_bytes[5]);
1362 
1363 	ret = iavf_add_del_eth_addr(adapter, mac_addr, true);
1364 	if (ret)
1365 		PMD_DRV_LOG(ERR, "Fail to add new MAC:"
1366 			    " %02X:%02X:%02X:%02X:%02X:%02X",
1367 			    mac_addr->addr_bytes[0],
1368 			    mac_addr->addr_bytes[1],
1369 			    mac_addr->addr_bytes[2],
1370 			    mac_addr->addr_bytes[3],
1371 			    mac_addr->addr_bytes[4],
1372 			    mac_addr->addr_bytes[5]);
1373 
1374 	if (ret)
1375 		return -EIO;
1376 
1377 	rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
1378 	return 0;
1379 }
1380 
1381 static void
1382 iavf_stat_update_48(uint64_t *offset, uint64_t *stat)
1383 {
1384 	if (*stat >= *offset)
1385 		*stat = *stat - *offset;
1386 	else
1387 		*stat = (uint64_t)((*stat +
1388 			((uint64_t)1 << IAVF_48_BIT_WIDTH)) - *offset);
1389 
1390 	*stat &= IAVF_48_BIT_MASK;
1391 }
1392 
1393 static void
1394 iavf_stat_update_32(uint64_t *offset, uint64_t *stat)
1395 {
1396 	if (*stat >= *offset)
1397 		*stat = (uint64_t)(*stat - *offset);
1398 	else
1399 		*stat = (uint64_t)((*stat +
1400 			((uint64_t)1 << IAVF_32_BIT_WIDTH)) - *offset);
1401 }
1402 
1403 static void
1404 iavf_update_stats(struct iavf_vsi *vsi, struct virtchnl_eth_stats *nes)
1405 {
1406 	struct virtchnl_eth_stats *oes = &vsi->eth_stats_offset;
1407 
1408 	iavf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
1409 	iavf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
1410 	iavf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
1411 	iavf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
1412 	iavf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
1413 	iavf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
1414 	iavf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
1415 	iavf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
1416 	iavf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
1417 	iavf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
1418 	iavf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
1419 }
1420 
1421 static int
1422 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1423 {
1424 	struct iavf_adapter *adapter =
1425 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1426 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1427 	struct iavf_vsi *vsi = &vf->vsi;
1428 	struct virtchnl_eth_stats *pstats = NULL;
1429 	int ret;
1430 
1431 	ret = iavf_query_stats(adapter, &pstats);
1432 	if (ret == 0) {
1433 		uint8_t crc_stats_len = (dev->data->dev_conf.rxmode.offloads &
1434 					 DEV_RX_OFFLOAD_KEEP_CRC) ? 0 :
1435 					 RTE_ETHER_CRC_LEN;
1436 		iavf_update_stats(vsi, pstats);
1437 		stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
1438 				pstats->rx_broadcast - pstats->rx_discards;
1439 		stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
1440 						pstats->tx_unicast;
1441 		stats->imissed = pstats->rx_discards;
1442 		stats->oerrors = pstats->tx_errors + pstats->tx_discards;
1443 		stats->ibytes = pstats->rx_bytes;
1444 		stats->ibytes -= stats->ipackets * crc_stats_len;
1445 		stats->obytes = pstats->tx_bytes;
1446 	} else {
1447 		PMD_DRV_LOG(ERR, "Get statistics failed");
1448 	}
1449 	return ret;
1450 }
1451 
1452 static int
1453 iavf_dev_stats_reset(struct rte_eth_dev *dev)
1454 {
1455 	int ret;
1456 	struct iavf_adapter *adapter =
1457 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1458 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1459 	struct iavf_vsi *vsi = &vf->vsi;
1460 	struct virtchnl_eth_stats *pstats = NULL;
1461 
1462 	/* read stat values to clear hardware registers */
1463 	ret = iavf_query_stats(adapter, &pstats);
1464 	if (ret != 0)
1465 		return ret;
1466 
1467 	/* set stats offset base on current values */
1468 	vsi->eth_stats_offset = *pstats;
1469 
1470 	return 0;
1471 }
1472 
1473 static int iavf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1474 				      struct rte_eth_xstat_name *xstats_names,
1475 				      __rte_unused unsigned int limit)
1476 {
1477 	unsigned int i;
1478 
1479 	if (xstats_names != NULL)
1480 		for (i = 0; i < IAVF_NB_XSTATS; i++) {
1481 			snprintf(xstats_names[i].name,
1482 				sizeof(xstats_names[i].name),
1483 				"%s", rte_iavf_stats_strings[i].name);
1484 		}
1485 	return IAVF_NB_XSTATS;
1486 }
1487 
1488 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
1489 				 struct rte_eth_xstat *xstats, unsigned int n)
1490 {
1491 	int ret;
1492 	unsigned int i;
1493 	struct iavf_adapter *adapter =
1494 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1495 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1496 	struct iavf_vsi *vsi = &vf->vsi;
1497 	struct virtchnl_eth_stats *pstats = NULL;
1498 
1499 	if (n < IAVF_NB_XSTATS)
1500 		return IAVF_NB_XSTATS;
1501 
1502 	ret = iavf_query_stats(adapter, &pstats);
1503 	if (ret != 0)
1504 		return 0;
1505 
1506 	if (!xstats)
1507 		return 0;
1508 
1509 	iavf_update_stats(vsi, pstats);
1510 
1511 	/* loop over xstats array and values from pstats */
1512 	for (i = 0; i < IAVF_NB_XSTATS; i++) {
1513 		xstats[i].id = i;
1514 		xstats[i].value = *(uint64_t *)(((char *)pstats) +
1515 			rte_iavf_stats_strings[i].offset);
1516 	}
1517 
1518 	return IAVF_NB_XSTATS;
1519 }
1520 
1521 
1522 static int
1523 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1524 {
1525 	struct iavf_adapter *adapter =
1526 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1527 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1528 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1529 	uint16_t msix_intr;
1530 
1531 	msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1532 	if (msix_intr == IAVF_MISC_VEC_ID) {
1533 		PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1534 		IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1535 			       IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1536 			       IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1537 			       IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1538 	} else {
1539 		IAVF_WRITE_REG(hw,
1540 			       IAVF_VFINT_DYN_CTLN1
1541 				(msix_intr - IAVF_RX_VEC_START),
1542 			       IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1543 			       IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1544 			       IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1545 	}
1546 
1547 	IAVF_WRITE_FLUSH(hw);
1548 
1549 	rte_intr_ack(&pci_dev->intr_handle);
1550 
1551 	return 0;
1552 }
1553 
1554 static int
1555 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1556 {
1557 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1558 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1559 	uint16_t msix_intr;
1560 
1561 	msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1562 	if (msix_intr == IAVF_MISC_VEC_ID) {
1563 		PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1564 		return -EIO;
1565 	}
1566 
1567 	IAVF_WRITE_REG(hw,
1568 		      IAVF_VFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1569 		      0);
1570 
1571 	IAVF_WRITE_FLUSH(hw);
1572 	return 0;
1573 }
1574 
1575 static int
1576 iavf_check_vf_reset_done(struct iavf_hw *hw)
1577 {
1578 	int i, reset;
1579 
1580 	for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1581 		reset = IAVF_READ_REG(hw, IAVF_VFGEN_RSTAT) &
1582 			IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1583 		reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
1584 		if (reset == VIRTCHNL_VFR_VFACTIVE ||
1585 		    reset == VIRTCHNL_VFR_COMPLETED)
1586 			break;
1587 		rte_delay_ms(20);
1588 	}
1589 
1590 	if (i >= IAVF_RESET_WAIT_CNT)
1591 		return -1;
1592 
1593 	return 0;
1594 }
1595 
1596 static int
1597 iavf_lookup_proto_xtr_type(const char *flex_name)
1598 {
1599 	static struct {
1600 		const char *name;
1601 		enum iavf_proto_xtr_type type;
1602 	} xtr_type_map[] = {
1603 		{ "vlan",      IAVF_PROTO_XTR_VLAN      },
1604 		{ "ipv4",      IAVF_PROTO_XTR_IPV4      },
1605 		{ "ipv6",      IAVF_PROTO_XTR_IPV6      },
1606 		{ "ipv6_flow", IAVF_PROTO_XTR_IPV6_FLOW },
1607 		{ "tcp",       IAVF_PROTO_XTR_TCP       },
1608 		{ "ip_offset", IAVF_PROTO_XTR_IP_OFFSET },
1609 	};
1610 	uint32_t i;
1611 
1612 	for (i = 0; i < RTE_DIM(xtr_type_map); i++) {
1613 		if (strcmp(flex_name, xtr_type_map[i].name) == 0)
1614 			return xtr_type_map[i].type;
1615 	}
1616 
1617 	PMD_DRV_LOG(ERR, "wrong proto_xtr type, "
1618 		    "it should be: vlan|ipv4|ipv6|ipv6_flow|tcp|ip_offset");
1619 
1620 	return -1;
1621 }
1622 
1623 /**
1624  * Parse elem, the elem could be single number/range or '(' ')' group
1625  * 1) A single number elem, it's just a simple digit. e.g. 9
1626  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
1627  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
1628  *    Within group elem, '-' used for a range separator;
1629  *                       ',' used for a single number.
1630  */
1631 static int
1632 iavf_parse_queue_set(const char *input, int xtr_type,
1633 		     struct iavf_devargs *devargs)
1634 {
1635 	const char *str = input;
1636 	char *end = NULL;
1637 	uint32_t min, max;
1638 	uint32_t idx;
1639 
1640 	while (isblank(*str))
1641 		str++;
1642 
1643 	if (!isdigit(*str) && *str != '(')
1644 		return -1;
1645 
1646 	/* process single number or single range of number */
1647 	if (*str != '(') {
1648 		errno = 0;
1649 		idx = strtoul(str, &end, 10);
1650 		if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1651 			return -1;
1652 
1653 		while (isblank(*end))
1654 			end++;
1655 
1656 		min = idx;
1657 		max = idx;
1658 
1659 		/* process single <number>-<number> */
1660 		if (*end == '-') {
1661 			end++;
1662 			while (isblank(*end))
1663 				end++;
1664 			if (!isdigit(*end))
1665 				return -1;
1666 
1667 			errno = 0;
1668 			idx = strtoul(end, &end, 10);
1669 			if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1670 				return -1;
1671 
1672 			max = idx;
1673 			while (isblank(*end))
1674 				end++;
1675 		}
1676 
1677 		if (*end != ':')
1678 			return -1;
1679 
1680 		for (idx = RTE_MIN(min, max);
1681 		     idx <= RTE_MAX(min, max); idx++)
1682 			devargs->proto_xtr[idx] = xtr_type;
1683 
1684 		return 0;
1685 	}
1686 
1687 	/* process set within bracket */
1688 	str++;
1689 	while (isblank(*str))
1690 		str++;
1691 	if (*str == '\0')
1692 		return -1;
1693 
1694 	min = IAVF_MAX_QUEUE_NUM;
1695 	do {
1696 		/* go ahead to the first digit */
1697 		while (isblank(*str))
1698 			str++;
1699 		if (!isdigit(*str))
1700 			return -1;
1701 
1702 		/* get the digit value */
1703 		errno = 0;
1704 		idx = strtoul(str, &end, 10);
1705 		if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1706 			return -1;
1707 
1708 		/* go ahead to separator '-',',' and ')' */
1709 		while (isblank(*end))
1710 			end++;
1711 		if (*end == '-') {
1712 			if (min == IAVF_MAX_QUEUE_NUM)
1713 				min = idx;
1714 			else /* avoid continuous '-' */
1715 				return -1;
1716 		} else if (*end == ',' || *end == ')') {
1717 			max = idx;
1718 			if (min == IAVF_MAX_QUEUE_NUM)
1719 				min = idx;
1720 
1721 			for (idx = RTE_MIN(min, max);
1722 			     idx <= RTE_MAX(min, max); idx++)
1723 				devargs->proto_xtr[idx] = xtr_type;
1724 
1725 			min = IAVF_MAX_QUEUE_NUM;
1726 		} else {
1727 			return -1;
1728 		}
1729 
1730 		str = end + 1;
1731 	} while (*end != ')' && *end != '\0');
1732 
1733 	return 0;
1734 }
1735 
1736 static int
1737 iavf_parse_queue_proto_xtr(const char *queues, struct iavf_devargs *devargs)
1738 {
1739 	const char *queue_start;
1740 	uint32_t idx;
1741 	int xtr_type;
1742 	char flex_name[32];
1743 
1744 	while (isblank(*queues))
1745 		queues++;
1746 
1747 	if (*queues != '[') {
1748 		xtr_type = iavf_lookup_proto_xtr_type(queues);
1749 		if (xtr_type < 0)
1750 			return -1;
1751 
1752 		devargs->proto_xtr_dflt = xtr_type;
1753 
1754 		return 0;
1755 	}
1756 
1757 	queues++;
1758 	do {
1759 		while (isblank(*queues))
1760 			queues++;
1761 		if (*queues == '\0')
1762 			return -1;
1763 
1764 		queue_start = queues;
1765 
1766 		/* go across a complete bracket */
1767 		if (*queue_start == '(') {
1768 			queues += strcspn(queues, ")");
1769 			if (*queues != ')')
1770 				return -1;
1771 		}
1772 
1773 		/* scan the separator ':' */
1774 		queues += strcspn(queues, ":");
1775 		if (*queues++ != ':')
1776 			return -1;
1777 		while (isblank(*queues))
1778 			queues++;
1779 
1780 		for (idx = 0; ; idx++) {
1781 			if (isblank(queues[idx]) ||
1782 			    queues[idx] == ',' ||
1783 			    queues[idx] == ']' ||
1784 			    queues[idx] == '\0')
1785 				break;
1786 
1787 			if (idx > sizeof(flex_name) - 2)
1788 				return -1;
1789 
1790 			flex_name[idx] = queues[idx];
1791 		}
1792 		flex_name[idx] = '\0';
1793 		xtr_type = iavf_lookup_proto_xtr_type(flex_name);
1794 		if (xtr_type < 0)
1795 			return -1;
1796 
1797 		queues += idx;
1798 
1799 		while (isblank(*queues) || *queues == ',' || *queues == ']')
1800 			queues++;
1801 
1802 		if (iavf_parse_queue_set(queue_start, xtr_type, devargs) < 0)
1803 			return -1;
1804 	} while (*queues != '\0');
1805 
1806 	return 0;
1807 }
1808 
1809 static int
1810 iavf_handle_proto_xtr_arg(__rte_unused const char *key, const char *value,
1811 			  void *extra_args)
1812 {
1813 	struct iavf_devargs *devargs = extra_args;
1814 
1815 	if (!value || !extra_args)
1816 		return -EINVAL;
1817 
1818 	if (iavf_parse_queue_proto_xtr(value, devargs) < 0) {
1819 		PMD_DRV_LOG(ERR, "the proto_xtr's parameter is wrong : '%s'",
1820 			    value);
1821 		return -1;
1822 	}
1823 
1824 	return 0;
1825 }
1826 
1827 static int iavf_parse_devargs(struct rte_eth_dev *dev)
1828 {
1829 	struct iavf_adapter *ad =
1830 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1831 	struct rte_devargs *devargs = dev->device->devargs;
1832 	struct rte_kvargs *kvlist;
1833 	int ret;
1834 
1835 	if (!devargs)
1836 		return 0;
1837 
1838 	kvlist = rte_kvargs_parse(devargs->args, iavf_valid_args);
1839 	if (!kvlist) {
1840 		PMD_INIT_LOG(ERR, "invalid kvargs key\n");
1841 		return -EINVAL;
1842 	}
1843 
1844 	ad->devargs.proto_xtr_dflt = IAVF_PROTO_XTR_NONE;
1845 	memset(ad->devargs.proto_xtr, IAVF_PROTO_XTR_NONE,
1846 	       sizeof(ad->devargs.proto_xtr));
1847 
1848 	ret = rte_kvargs_process(kvlist, IAVF_PROTO_XTR_ARG,
1849 				 &iavf_handle_proto_xtr_arg, &ad->devargs);
1850 	if (ret)
1851 		goto bail;
1852 
1853 bail:
1854 	rte_kvargs_free(kvlist);
1855 	return ret;
1856 }
1857 
1858 static void
1859 iavf_init_proto_xtr(struct rte_eth_dev *dev)
1860 {
1861 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1862 	struct iavf_adapter *ad =
1863 			IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1864 	const struct iavf_proto_xtr_ol *xtr_ol;
1865 	bool proto_xtr_enable = false;
1866 	int offset;
1867 	uint16_t i;
1868 
1869 	vf->proto_xtr = rte_zmalloc("vf proto xtr",
1870 				    vf->vsi_res->num_queue_pairs, 0);
1871 	if (unlikely(!(vf->proto_xtr))) {
1872 		PMD_DRV_LOG(ERR, "no memory for setting up proto_xtr's table");
1873 		return;
1874 	}
1875 
1876 	for (i = 0; i < vf->vsi_res->num_queue_pairs; i++) {
1877 		vf->proto_xtr[i] = ad->devargs.proto_xtr[i] !=
1878 					IAVF_PROTO_XTR_NONE ?
1879 					ad->devargs.proto_xtr[i] :
1880 					ad->devargs.proto_xtr_dflt;
1881 
1882 		if (vf->proto_xtr[i] != IAVF_PROTO_XTR_NONE) {
1883 			uint8_t type = vf->proto_xtr[i];
1884 
1885 			iavf_proto_xtr_params[type].required = true;
1886 			proto_xtr_enable = true;
1887 		}
1888 	}
1889 
1890 	if (likely(!proto_xtr_enable))
1891 		return;
1892 
1893 	offset = rte_mbuf_dynfield_register(&iavf_proto_xtr_metadata_param);
1894 	if (unlikely(offset == -1)) {
1895 		PMD_DRV_LOG(ERR,
1896 			    "failed to extract protocol metadata, error %d",
1897 			    -rte_errno);
1898 		return;
1899 	}
1900 
1901 	PMD_DRV_LOG(DEBUG,
1902 		    "proto_xtr metadata offset in mbuf is : %d",
1903 		    offset);
1904 	rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = offset;
1905 
1906 	for (i = 0; i < RTE_DIM(iavf_proto_xtr_params); i++) {
1907 		xtr_ol = &iavf_proto_xtr_params[i];
1908 
1909 		uint8_t rxdid = iavf_proto_xtr_type_to_rxdid((uint8_t)i);
1910 
1911 		if (!xtr_ol->required)
1912 			continue;
1913 
1914 		if (!(vf->supported_rxdid & BIT(rxdid))) {
1915 			PMD_DRV_LOG(ERR,
1916 				    "rxdid[%u] is not supported in hardware",
1917 				    rxdid);
1918 			rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = -1;
1919 			break;
1920 		}
1921 
1922 		offset = rte_mbuf_dynflag_register(&xtr_ol->param);
1923 		if (unlikely(offset == -1)) {
1924 			PMD_DRV_LOG(ERR,
1925 				    "failed to register proto_xtr offload '%s', error %d",
1926 				    xtr_ol->param.name, -rte_errno);
1927 
1928 			rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = -1;
1929 			break;
1930 		}
1931 
1932 		PMD_DRV_LOG(DEBUG,
1933 			    "proto_xtr offload '%s' offset in mbuf is : %d",
1934 			    xtr_ol->param.name, offset);
1935 		*xtr_ol->ol_flag = 1ULL << offset;
1936 	}
1937 }
1938 
1939 static int
1940 iavf_init_vf(struct rte_eth_dev *dev)
1941 {
1942 	int err, bufsz;
1943 	struct iavf_adapter *adapter =
1944 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1945 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1946 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1947 
1948 	err = iavf_parse_devargs(dev);
1949 	if (err) {
1950 		PMD_INIT_LOG(ERR, "Failed to parse devargs");
1951 		goto err;
1952 	}
1953 
1954 	err = iavf_set_mac_type(hw);
1955 	if (err) {
1956 		PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1957 		goto err;
1958 	}
1959 
1960 	err = iavf_check_vf_reset_done(hw);
1961 	if (err) {
1962 		PMD_INIT_LOG(ERR, "VF is still resetting");
1963 		goto err;
1964 	}
1965 
1966 	iavf_init_adminq_parameter(hw);
1967 	err = iavf_init_adminq(hw);
1968 	if (err) {
1969 		PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1970 		goto err;
1971 	}
1972 
1973 	vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1974 	if (!vf->aq_resp) {
1975 		PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1976 		goto err_aq;
1977 	}
1978 	if (iavf_check_api_version(adapter) != 0) {
1979 		PMD_INIT_LOG(ERR, "check_api version failed");
1980 		goto err_api;
1981 	}
1982 
1983 	bufsz = sizeof(struct virtchnl_vf_resource) +
1984 		(IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1985 	vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1986 	if (!vf->vf_res) {
1987 		PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1988 		goto err_api;
1989 	}
1990 	if (iavf_get_vf_resource(adapter) != 0) {
1991 		PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1992 		goto err_alloc;
1993 	}
1994 	/* Allocate memort for RSS info */
1995 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1996 		vf->rss_key = rte_zmalloc("rss_key",
1997 					  vf->vf_res->rss_key_size, 0);
1998 		if (!vf->rss_key) {
1999 			PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
2000 			goto err_rss;
2001 		}
2002 		vf->rss_lut = rte_zmalloc("rss_lut",
2003 					  vf->vf_res->rss_lut_size, 0);
2004 		if (!vf->rss_lut) {
2005 			PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
2006 			goto err_rss;
2007 		}
2008 	}
2009 
2010 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
2011 		if (iavf_get_supported_rxdid(adapter) != 0) {
2012 			PMD_INIT_LOG(ERR, "failed to do get supported rxdid");
2013 			goto err_rss;
2014 		}
2015 	}
2016 
2017 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
2018 		if (iavf_get_vlan_offload_caps_v2(adapter) != 0) {
2019 			PMD_INIT_LOG(ERR, "failed to do get VLAN offload v2 capabilities");
2020 			goto err_rss;
2021 		}
2022 	}
2023 
2024 	iavf_init_proto_xtr(dev);
2025 
2026 	return 0;
2027 err_rss:
2028 	rte_free(vf->rss_key);
2029 	rte_free(vf->rss_lut);
2030 err_alloc:
2031 	rte_free(vf->vf_res);
2032 	vf->vsi_res = NULL;
2033 err_api:
2034 	rte_free(vf->aq_resp);
2035 err_aq:
2036 	iavf_shutdown_adminq(hw);
2037 err:
2038 	return -1;
2039 }
2040 
2041 /* Enable default admin queue interrupt setting */
2042 static inline void
2043 iavf_enable_irq0(struct iavf_hw *hw)
2044 {
2045 	/* Enable admin queue interrupt trigger */
2046 	IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1,
2047 		       IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
2048 
2049 	IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
2050 		       IAVF_VFINT_DYN_CTL01_INTENA_MASK |
2051 		       IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
2052 		       IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
2053 
2054 	IAVF_WRITE_FLUSH(hw);
2055 }
2056 
2057 static inline void
2058 iavf_disable_irq0(struct iavf_hw *hw)
2059 {
2060 	/* Disable all interrupt types */
2061 	IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1, 0);
2062 	IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
2063 		       IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
2064 	IAVF_WRITE_FLUSH(hw);
2065 }
2066 
2067 static void
2068 iavf_dev_interrupt_handler(void *param)
2069 {
2070 	struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2071 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2072 
2073 	iavf_disable_irq0(hw);
2074 
2075 	iavf_handle_virtchnl_msg(dev);
2076 
2077 	iavf_enable_irq0(hw);
2078 }
2079 
2080 static int
2081 iavf_dev_flow_ops_get(struct rte_eth_dev *dev,
2082 		      const struct rte_flow_ops **ops)
2083 {
2084 	if (!dev)
2085 		return -EINVAL;
2086 
2087 	*ops = &iavf_flow_ops;
2088 	return 0;
2089 }
2090 
2091 static void
2092 iavf_default_rss_disable(struct iavf_adapter *adapter)
2093 {
2094 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
2095 	int ret = 0;
2096 
2097 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF) {
2098 		/* Set hena = 0 to ask PF to cleanup all existing RSS. */
2099 		ret = iavf_set_hena(adapter, 0);
2100 		if (ret)
2101 			/* It is a workaround, temporarily allow error to be
2102 			 * returned due to possible lack of PF handling for
2103 			 * hena = 0.
2104 			 */
2105 			PMD_INIT_LOG(WARNING, "fail to disable default RSS,"
2106 				    "lack PF support");
2107 	}
2108 }
2109 
2110 static int
2111 iavf_dev_init(struct rte_eth_dev *eth_dev)
2112 {
2113 	struct iavf_adapter *adapter =
2114 		IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
2115 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
2116 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2117 	int ret = 0;
2118 
2119 	PMD_INIT_FUNC_TRACE();
2120 
2121 	/* assign ops func pointer */
2122 	eth_dev->dev_ops = &iavf_eth_dev_ops;
2123 	eth_dev->rx_queue_count = iavf_dev_rxq_count;
2124 	eth_dev->rx_descriptor_status = iavf_dev_rx_desc_status;
2125 	eth_dev->tx_descriptor_status = iavf_dev_tx_desc_status;
2126 	eth_dev->rx_pkt_burst = &iavf_recv_pkts;
2127 	eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
2128 	eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
2129 
2130 	/* For secondary processes, we don't initialise any further as primary
2131 	 * has already done this work. Only check if we need a different RX
2132 	 * and TX function.
2133 	 */
2134 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2135 		iavf_set_rx_function(eth_dev);
2136 		iavf_set_tx_function(eth_dev);
2137 		return 0;
2138 	}
2139 	rte_eth_copy_pci_info(eth_dev, pci_dev);
2140 	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
2141 
2142 	hw->vendor_id = pci_dev->id.vendor_id;
2143 	hw->device_id = pci_dev->id.device_id;
2144 	hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
2145 	hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
2146 	hw->bus.bus_id = pci_dev->addr.bus;
2147 	hw->bus.device = pci_dev->addr.devid;
2148 	hw->bus.func = pci_dev->addr.function;
2149 	hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
2150 	hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
2151 	adapter->eth_dev = eth_dev;
2152 	adapter->stopped = 1;
2153 
2154 	if (iavf_init_vf(eth_dev) != 0) {
2155 		PMD_INIT_LOG(ERR, "Init vf failed");
2156 		return -1;
2157 	}
2158 
2159 	/* set default ptype table */
2160 	adapter->ptype_tbl = iavf_get_default_ptype_table();
2161 
2162 	/* copy mac addr */
2163 	eth_dev->data->mac_addrs = rte_zmalloc(
2164 		"iavf_mac", RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX, 0);
2165 	if (!eth_dev->data->mac_addrs) {
2166 		PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
2167 			     " store MAC addresses",
2168 			     RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
2169 		return -ENOMEM;
2170 	}
2171 	/* If the MAC address is not configured by host,
2172 	 * generate a random one.
2173 	 */
2174 	if (!rte_is_valid_assigned_ether_addr(
2175 			(struct rte_ether_addr *)hw->mac.addr))
2176 		rte_eth_random_addr(hw->mac.addr);
2177 	rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
2178 			&eth_dev->data->mac_addrs[0]);
2179 
2180 	/* register callback func to eal lib */
2181 	rte_intr_callback_register(&pci_dev->intr_handle,
2182 				   iavf_dev_interrupt_handler,
2183 				   (void *)eth_dev);
2184 
2185 	/* enable uio intr after callback register */
2186 	rte_intr_enable(&pci_dev->intr_handle);
2187 
2188 	/* configure and enable device interrupt */
2189 	iavf_enable_irq0(hw);
2190 
2191 	ret = iavf_flow_init(adapter);
2192 	if (ret) {
2193 		PMD_INIT_LOG(ERR, "Failed to initialize flow");
2194 		return ret;
2195 	}
2196 
2197 	iavf_default_rss_disable(adapter);
2198 
2199 	return 0;
2200 }
2201 
2202 static int
2203 iavf_dev_close(struct rte_eth_dev *dev)
2204 {
2205 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2206 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2207 	struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2208 	struct iavf_adapter *adapter =
2209 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2210 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2211 	int ret;
2212 
2213 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2214 		return 0;
2215 
2216 	ret = iavf_dev_stop(dev);
2217 
2218 	iavf_flow_flush(dev, NULL);
2219 	iavf_flow_uninit(adapter);
2220 
2221 	/*
2222 	 * disable promiscuous mode before reset vf
2223 	 * it is a workaround solution when work with kernel driver
2224 	 * and it is not the normal way
2225 	 */
2226 	if (vf->promisc_unicast_enabled || vf->promisc_multicast_enabled)
2227 		iavf_config_promisc(adapter, false, false);
2228 
2229 	iavf_shutdown_adminq(hw);
2230 	/* disable uio intr before callback unregister */
2231 	rte_intr_disable(intr_handle);
2232 
2233 	/* unregister callback func from eal lib */
2234 	rte_intr_callback_unregister(intr_handle,
2235 				     iavf_dev_interrupt_handler, dev);
2236 	iavf_disable_irq0(hw);
2237 
2238 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2239 		if (vf->rss_lut) {
2240 			rte_free(vf->rss_lut);
2241 			vf->rss_lut = NULL;
2242 		}
2243 		if (vf->rss_key) {
2244 			rte_free(vf->rss_key);
2245 			vf->rss_key = NULL;
2246 		}
2247 	}
2248 
2249 	rte_free(vf->vf_res);
2250 	vf->vsi_res = NULL;
2251 	vf->vf_res = NULL;
2252 
2253 	rte_free(vf->aq_resp);
2254 	vf->aq_resp = NULL;
2255 
2256 	vf->vf_reset = false;
2257 
2258 	return ret;
2259 }
2260 
2261 static int
2262 iavf_dev_uninit(struct rte_eth_dev *dev)
2263 {
2264 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2265 		return -EPERM;
2266 
2267 	iavf_dev_close(dev);
2268 
2269 	return 0;
2270 }
2271 
2272 /*
2273  * Reset VF device only to re-initialize resources in PMD layer
2274  */
2275 static int
2276 iavf_dev_reset(struct rte_eth_dev *dev)
2277 {
2278 	int ret;
2279 
2280 	ret = iavf_dev_uninit(dev);
2281 	if (ret)
2282 		return ret;
2283 
2284 	return iavf_dev_init(dev);
2285 }
2286 
2287 static int
2288 iavf_dcf_cap_check_handler(__rte_unused const char *key,
2289 			   const char *value, __rte_unused void *opaque)
2290 {
2291 	if (strcmp(value, "dcf"))
2292 		return -1;
2293 
2294 	return 0;
2295 }
2296 
2297 static int
2298 iavf_dcf_cap_selected(struct rte_devargs *devargs)
2299 {
2300 	struct rte_kvargs *kvlist;
2301 	const char *key = "cap";
2302 	int ret = 0;
2303 
2304 	if (devargs == NULL)
2305 		return 0;
2306 
2307 	kvlist = rte_kvargs_parse(devargs->args, NULL);
2308 	if (kvlist == NULL)
2309 		return 0;
2310 
2311 	if (!rte_kvargs_count(kvlist, key))
2312 		goto exit;
2313 
2314 	/* dcf capability selected when there's a key-value pair: cap=dcf */
2315 	if (rte_kvargs_process(kvlist, key,
2316 			       iavf_dcf_cap_check_handler, NULL) < 0)
2317 		goto exit;
2318 
2319 	ret = 1;
2320 
2321 exit:
2322 	rte_kvargs_free(kvlist);
2323 	return ret;
2324 }
2325 
2326 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2327 			     struct rte_pci_device *pci_dev)
2328 {
2329 	if (iavf_dcf_cap_selected(pci_dev->device.devargs))
2330 		return 1;
2331 
2332 	return rte_eth_dev_pci_generic_probe(pci_dev,
2333 		sizeof(struct iavf_adapter), iavf_dev_init);
2334 }
2335 
2336 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
2337 {
2338 	return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
2339 }
2340 
2341 /* Adaptive virtual function driver struct */
2342 static struct rte_pci_driver rte_iavf_pmd = {
2343 	.id_table = pci_id_iavf_map,
2344 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
2345 	.probe = eth_iavf_pci_probe,
2346 	.remove = eth_iavf_pci_remove,
2347 };
2348 
2349 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
2350 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
2351 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
2352 RTE_PMD_REGISTER_PARAM_STRING(net_iavf, "cap=dcf");
2353 RTE_LOG_REGISTER(iavf_logtype_init, pmd.net.iavf.init, NOTICE);
2354 RTE_LOG_REGISTER(iavf_logtype_driver, pmd.net.iavf.driver, NOTICE);
2355 #ifdef RTE_ETHDEV_DEBUG_RX
2356 RTE_LOG_REGISTER(iavf_logtype_rx, pmd.net.iavf.rx, DEBUG);
2357 #endif
2358 #ifdef RTE_ETHDEV_DEBUG_TX
2359 RTE_LOG_REGISTER(iavf_logtype_tx, pmd.net.iavf.tx, DEBUG);
2360 #endif
2361