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