xref: /dpdk/drivers/net/iavf/iavf_ethdev.c (revision abdd3141)
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 <rte_ethdev_driver.h>
23 #include <rte_ethdev_pci.h>
24 #include <rte_malloc.h>
25 #include <rte_memzone.h>
26 #include <rte_dev.h>
27 
28 #include "iavf_log.h"
29 #include "base/iavf_prototype.h"
30 #include "base/iavf_adminq_cmd.h"
31 #include "base/iavf_type.h"
32 
33 #include "iavf.h"
34 #include "iavf_rxtx.h"
35 
36 static int iavf_dev_configure(struct rte_eth_dev *dev);
37 static int iavf_dev_start(struct rte_eth_dev *dev);
38 static void iavf_dev_stop(struct rte_eth_dev *dev);
39 static void iavf_dev_close(struct rte_eth_dev *dev);
40 static void iavf_dev_info_get(struct rte_eth_dev *dev,
41 			     struct rte_eth_dev_info *dev_info);
42 static const uint32_t *iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev);
43 static int iavf_dev_stats_get(struct rte_eth_dev *dev,
44 			     struct rte_eth_stats *stats);
45 static void iavf_dev_promiscuous_enable(struct rte_eth_dev *dev);
46 static void iavf_dev_promiscuous_disable(struct rte_eth_dev *dev);
47 static void iavf_dev_allmulticast_enable(struct rte_eth_dev *dev);
48 static void iavf_dev_allmulticast_disable(struct rte_eth_dev *dev);
49 static int iavf_dev_add_mac_addr(struct rte_eth_dev *dev,
50 				struct ether_addr *addr,
51 				uint32_t index,
52 				uint32_t pool);
53 static void iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index);
54 static int iavf_dev_vlan_filter_set(struct rte_eth_dev *dev,
55 				   uint16_t vlan_id, int on);
56 static int iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
57 static int iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
58 				   struct rte_eth_rss_reta_entry64 *reta_conf,
59 				   uint16_t reta_size);
60 static int iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
61 				  struct rte_eth_rss_reta_entry64 *reta_conf,
62 				  uint16_t reta_size);
63 static int iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
64 				   struct rte_eth_rss_conf *rss_conf);
65 static int iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
66 				     struct rte_eth_rss_conf *rss_conf);
67 static int iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
68 static int iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
69 					 struct ether_addr *mac_addr);
70 static int iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev,
71 					uint16_t queue_id);
72 static int iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev,
73 					 uint16_t queue_id);
74 
75 int iavf_logtype_init;
76 int iavf_logtype_driver;
77 
78 static const struct rte_pci_id pci_id_iavf_map[] = {
79 	{ RTE_PCI_DEVICE(IAVF_INTEL_VENDOR_ID, IAVF_DEV_ID_ADAPTIVE_VF) },
80 	{ .vendor_id = 0, /* sentinel */ },
81 };
82 
83 static const struct eth_dev_ops iavf_eth_dev_ops = {
84 	.dev_configure              = iavf_dev_configure,
85 	.dev_start                  = iavf_dev_start,
86 	.dev_stop                   = iavf_dev_stop,
87 	.dev_close                  = iavf_dev_close,
88 	.dev_infos_get              = iavf_dev_info_get,
89 	.dev_supported_ptypes_get   = iavf_dev_supported_ptypes_get,
90 	.link_update                = iavf_dev_link_update,
91 	.stats_get                  = iavf_dev_stats_get,
92 	.promiscuous_enable         = iavf_dev_promiscuous_enable,
93 	.promiscuous_disable        = iavf_dev_promiscuous_disable,
94 	.allmulticast_enable        = iavf_dev_allmulticast_enable,
95 	.allmulticast_disable       = iavf_dev_allmulticast_disable,
96 	.mac_addr_add               = iavf_dev_add_mac_addr,
97 	.mac_addr_remove            = iavf_dev_del_mac_addr,
98 	.vlan_filter_set            = iavf_dev_vlan_filter_set,
99 	.vlan_offload_set           = iavf_dev_vlan_offload_set,
100 	.rx_queue_start             = iavf_dev_rx_queue_start,
101 	.rx_queue_stop              = iavf_dev_rx_queue_stop,
102 	.tx_queue_start             = iavf_dev_tx_queue_start,
103 	.tx_queue_stop              = iavf_dev_tx_queue_stop,
104 	.rx_queue_setup             = iavf_dev_rx_queue_setup,
105 	.rx_queue_release           = iavf_dev_rx_queue_release,
106 	.tx_queue_setup             = iavf_dev_tx_queue_setup,
107 	.tx_queue_release           = iavf_dev_tx_queue_release,
108 	.mac_addr_set               = iavf_dev_set_default_mac_addr,
109 	.reta_update                = iavf_dev_rss_reta_update,
110 	.reta_query                 = iavf_dev_rss_reta_query,
111 	.rss_hash_update            = iavf_dev_rss_hash_update,
112 	.rss_hash_conf_get          = iavf_dev_rss_hash_conf_get,
113 	.rxq_info_get               = iavf_dev_rxq_info_get,
114 	.txq_info_get               = iavf_dev_txq_info_get,
115 	.rx_queue_count             = iavf_dev_rxq_count,
116 	.rx_descriptor_status       = iavf_dev_rx_desc_status,
117 	.tx_descriptor_status       = iavf_dev_tx_desc_status,
118 	.mtu_set                    = iavf_dev_mtu_set,
119 	.rx_queue_intr_enable       = iavf_dev_rx_queue_intr_enable,
120 	.rx_queue_intr_disable      = iavf_dev_rx_queue_intr_disable,
121 };
122 
123 static int
124 iavf_dev_configure(struct rte_eth_dev *dev)
125 {
126 	struct iavf_adapter *ad =
127 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
128 	struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(ad);
129 	struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
130 
131 	ad->rx_bulk_alloc_allowed = true;
132 #ifdef RTE_LIBRTE_IAVF_INC_VECTOR
133 	/* Initialize to TRUE. If any of Rx queues doesn't meet the
134 	 * vector Rx/Tx preconditions, it will be reset.
135 	 */
136 	ad->rx_vec_allowed = true;
137 	ad->tx_vec_allowed = true;
138 #else
139 	ad->rx_vec_allowed = false;
140 	ad->tx_vec_allowed = false;
141 #endif
142 
143 	/* Vlan stripping setting */
144 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN) {
145 		if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
146 			iavf_enable_vlan_strip(ad);
147 		else
148 			iavf_disable_vlan_strip(ad);
149 	}
150 	return 0;
151 }
152 
153 static int
154 iavf_init_rss(struct iavf_adapter *adapter)
155 {
156 	struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
157 	struct rte_eth_rss_conf *rss_conf;
158 	uint8_t i, j, nb_q;
159 	int ret;
160 
161 	rss_conf = &adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
162 	nb_q = RTE_MIN(adapter->eth_dev->data->nb_rx_queues,
163 		       IAVF_MAX_NUM_QUEUES);
164 
165 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF)) {
166 		PMD_DRV_LOG(DEBUG, "RSS is not supported");
167 		return -ENOTSUP;
168 	}
169 	if (adapter->eth_dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
170 		PMD_DRV_LOG(WARNING, "RSS is enabled by PF by default");
171 		/* set all lut items to default queue */
172 		for (i = 0; i < vf->vf_res->rss_lut_size; i++)
173 			vf->rss_lut[i] = 0;
174 		ret = iavf_configure_rss_lut(adapter);
175 		return ret;
176 	}
177 
178 	/* In IAVF, RSS enablement is set by PF driver. It is not supported
179 	 * to set based on rss_conf->rss_hf.
180 	 */
181 
182 	/* configure RSS key */
183 	if (!rss_conf->rss_key) {
184 		/* Calculate the default hash key */
185 		for (i = 0; i <= vf->vf_res->rss_key_size; i++)
186 			vf->rss_key[i] = (uint8_t)rte_rand();
187 	} else
188 		rte_memcpy(vf->rss_key, rss_conf->rss_key,
189 			   RTE_MIN(rss_conf->rss_key_len,
190 				   vf->vf_res->rss_key_size));
191 
192 	/* init RSS LUT table */
193 	for (i = 0, j = 0; i < vf->vf_res->rss_lut_size; i++, j++) {
194 		if (j >= nb_q)
195 			j = 0;
196 		vf->rss_lut[i] = j;
197 	}
198 	/* send virtchnnl ops to configure rss*/
199 	ret = iavf_configure_rss_lut(adapter);
200 	if (ret)
201 		return ret;
202 	ret = iavf_configure_rss_key(adapter);
203 	if (ret)
204 		return ret;
205 
206 	return 0;
207 }
208 
209 static int
210 iavf_init_rxq(struct rte_eth_dev *dev, struct iavf_rx_queue *rxq)
211 {
212 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
213 	struct rte_eth_dev_data *dev_data = dev->data;
214 	uint16_t buf_size, max_pkt_len, len;
215 
216 	buf_size = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
217 
218 	/* Calculate the maximum packet length allowed */
219 	len = rxq->rx_buf_len * IAVF_MAX_CHAINED_RX_BUFFERS;
220 	max_pkt_len = RTE_MIN(len, dev->data->dev_conf.rxmode.max_rx_pkt_len);
221 
222 	/* Check if the jumbo frame and maximum packet length are set
223 	 * correctly.
224 	 */
225 	if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
226 		if (max_pkt_len <= ETHER_MAX_LEN ||
227 		    max_pkt_len > IAVF_FRAME_SIZE_MAX) {
228 			PMD_DRV_LOG(ERR, "maximum packet length must be "
229 				    "larger than %u and smaller than %u, "
230 				    "as jumbo frame is enabled",
231 				    (uint32_t)ETHER_MAX_LEN,
232 				    (uint32_t)IAVF_FRAME_SIZE_MAX);
233 			return -EINVAL;
234 		}
235 	} else {
236 		if (max_pkt_len < ETHER_MIN_LEN ||
237 		    max_pkt_len > ETHER_MAX_LEN) {
238 			PMD_DRV_LOG(ERR, "maximum packet length must be "
239 				    "larger than %u and smaller than %u, "
240 				    "as jumbo frame is disabled",
241 				    (uint32_t)ETHER_MIN_LEN,
242 				    (uint32_t)ETHER_MAX_LEN);
243 			return -EINVAL;
244 		}
245 	}
246 
247 	rxq->max_pkt_len = max_pkt_len;
248 	if ((dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) ||
249 	    (rxq->max_pkt_len + 2 * IAVF_VLAN_TAG_SIZE) > buf_size) {
250 		dev_data->scattered_rx = 1;
251 	}
252 	IAVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
253 	IAVF_WRITE_FLUSH(hw);
254 
255 	return 0;
256 }
257 
258 static int
259 iavf_init_queues(struct rte_eth_dev *dev)
260 {
261 	struct iavf_rx_queue **rxq =
262 		(struct iavf_rx_queue **)dev->data->rx_queues;
263 	int i, ret = IAVF_SUCCESS;
264 
265 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
266 		if (!rxq[i] || !rxq[i]->q_set)
267 			continue;
268 		ret = iavf_init_rxq(dev, rxq[i]);
269 		if (ret != IAVF_SUCCESS)
270 			break;
271 	}
272 	/* set rx/tx function to vector/scatter/single-segment
273 	 * according to parameters
274 	 */
275 	iavf_set_rx_function(dev);
276 	iavf_set_tx_function(dev);
277 
278 	return ret;
279 }
280 
281 static int iavf_config_rx_queues_irqs(struct rte_eth_dev *dev,
282 				     struct rte_intr_handle *intr_handle)
283 {
284 	struct iavf_adapter *adapter =
285 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
286 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
287 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
288 	uint16_t interval, i;
289 	int vec;
290 
291 	if (rte_intr_cap_multiple(intr_handle) &&
292 	    dev->data->dev_conf.intr_conf.rxq) {
293 		if (rte_intr_efd_enable(intr_handle, dev->data->nb_rx_queues))
294 			return -1;
295 	}
296 
297 	if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
298 		intr_handle->intr_vec =
299 			rte_zmalloc("intr_vec",
300 				    dev->data->nb_rx_queues * sizeof(int), 0);
301 		if (!intr_handle->intr_vec) {
302 			PMD_DRV_LOG(ERR, "Failed to allocate %d rx intr_vec",
303 				    dev->data->nb_rx_queues);
304 			return -1;
305 		}
306 	}
307 
308 	if (!dev->data->dev_conf.intr_conf.rxq ||
309 	    !rte_intr_dp_is_en(intr_handle)) {
310 		/* Rx interrupt disabled, Map interrupt only for writeback */
311 		vf->nb_msix = 1;
312 		if (vf->vf_res->vf_cap_flags &
313 		    VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
314 			/* If WB_ON_ITR supports, enable it */
315 			vf->msix_base = IAVF_RX_VEC_START;
316 			IAVF_WRITE_REG(hw, IAVFINT_DYN_CTLN1(vf->msix_base - 1),
317 				      IAVFINT_DYN_CTLN1_ITR_INDX_MASK |
318 				      IAVFINT_DYN_CTLN1_WB_ON_ITR_MASK);
319 		} else {
320 			/* If no WB_ON_ITR offload flags, need to set
321 			 * interrupt for descriptor write back.
322 			 */
323 			vf->msix_base = IAVF_MISC_VEC_ID;
324 
325 			/* set ITR to max */
326 			interval = iavf_calc_itr_interval(
327 					IAVF_QUEUE_ITR_INTERVAL_MAX);
328 			IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01,
329 				      IAVFINT_DYN_CTL01_INTENA_MASK |
330 				      (IAVF_ITR_INDEX_DEFAULT <<
331 				       IAVFINT_DYN_CTL01_ITR_INDX_SHIFT) |
332 				      (interval <<
333 				       IAVFINT_DYN_CTL01_INTERVAL_SHIFT));
334 		}
335 		IAVF_WRITE_FLUSH(hw);
336 		/* map all queues to the same interrupt */
337 		for (i = 0; i < dev->data->nb_rx_queues; i++)
338 			vf->rxq_map[vf->msix_base] |= 1 << i;
339 	} else {
340 		if (!rte_intr_allow_others(intr_handle)) {
341 			vf->nb_msix = 1;
342 			vf->msix_base = IAVF_MISC_VEC_ID;
343 			for (i = 0; i < dev->data->nb_rx_queues; i++) {
344 				vf->rxq_map[vf->msix_base] |= 1 << i;
345 				intr_handle->intr_vec[i] = IAVF_MISC_VEC_ID;
346 			}
347 			PMD_DRV_LOG(DEBUG,
348 				    "vector %u are mapping to all Rx queues",
349 				    vf->msix_base);
350 		} else {
351 			/* If Rx interrupt is reuquired, and we can use
352 			 * multi interrupts, then the vec is from 1
353 			 */
354 			vf->nb_msix = RTE_MIN(vf->vf_res->max_vectors,
355 					      intr_handle->nb_efd);
356 			vf->msix_base = IAVF_RX_VEC_START;
357 			vec = IAVF_RX_VEC_START;
358 			for (i = 0; i < dev->data->nb_rx_queues; i++) {
359 				vf->rxq_map[vec] |= 1 << i;
360 				intr_handle->intr_vec[i] = vec++;
361 				if (vec >= vf->nb_msix)
362 					vec = IAVF_RX_VEC_START;
363 			}
364 			PMD_DRV_LOG(DEBUG,
365 				    "%u vectors are mapping to %u Rx queues",
366 				    vf->nb_msix, dev->data->nb_rx_queues);
367 		}
368 	}
369 
370 	if (iavf_config_irq_map(adapter)) {
371 		PMD_DRV_LOG(ERR, "config interrupt mapping failed");
372 		return -1;
373 	}
374 	return 0;
375 }
376 
377 static int
378 iavf_start_queues(struct rte_eth_dev *dev)
379 {
380 	struct iavf_rx_queue *rxq;
381 	struct iavf_tx_queue *txq;
382 	int i;
383 
384 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
385 		txq = dev->data->tx_queues[i];
386 		if (txq->tx_deferred_start)
387 			continue;
388 		if (iavf_dev_tx_queue_start(dev, i) != 0) {
389 			PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
390 			return -1;
391 		}
392 	}
393 
394 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
395 		rxq = dev->data->rx_queues[i];
396 		if (rxq->rx_deferred_start)
397 			continue;
398 		if (iavf_dev_rx_queue_start(dev, i) != 0) {
399 			PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
400 			return -1;
401 		}
402 	}
403 
404 	return 0;
405 }
406 
407 static int
408 iavf_dev_start(struct rte_eth_dev *dev)
409 {
410 	struct iavf_adapter *adapter =
411 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
412 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
413 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
414 	struct rte_intr_handle *intr_handle = dev->intr_handle;
415 
416 	PMD_INIT_FUNC_TRACE();
417 
418 	hw->adapter_stopped = 0;
419 
420 	vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
421 	vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
422 				      dev->data->nb_tx_queues);
423 
424 	if (iavf_init_queues(dev) != 0) {
425 		PMD_DRV_LOG(ERR, "failed to do Queue init");
426 		return -1;
427 	}
428 
429 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
430 		if (iavf_init_rss(adapter) != 0) {
431 			PMD_DRV_LOG(ERR, "configure rss failed");
432 			goto err_rss;
433 		}
434 	}
435 
436 	if (iavf_configure_queues(adapter) != 0) {
437 		PMD_DRV_LOG(ERR, "configure queues failed");
438 		goto err_queue;
439 	}
440 
441 	if (iavf_config_rx_queues_irqs(dev, intr_handle) != 0) {
442 		PMD_DRV_LOG(ERR, "configure irq failed");
443 		goto err_queue;
444 	}
445 	/* re-enable intr again, because efd assign may change */
446 	if (dev->data->dev_conf.intr_conf.rxq != 0) {
447 		rte_intr_disable(intr_handle);
448 		rte_intr_enable(intr_handle);
449 	}
450 
451 	/* Set all mac addrs */
452 	iavf_add_del_all_mac_addr(adapter, TRUE);
453 
454 	if (iavf_start_queues(dev) != 0) {
455 		PMD_DRV_LOG(ERR, "enable queues failed");
456 		goto err_mac;
457 	}
458 
459 	return 0;
460 
461 err_mac:
462 	iavf_add_del_all_mac_addr(adapter, FALSE);
463 err_queue:
464 err_rss:
465 	return -1;
466 }
467 
468 static void
469 iavf_dev_stop(struct rte_eth_dev *dev)
470 {
471 	struct iavf_adapter *adapter =
472 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
473 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
474 	struct rte_intr_handle *intr_handle = dev->intr_handle;
475 
476 	PMD_INIT_FUNC_TRACE();
477 
478 	if (hw->adapter_stopped == 1)
479 		return;
480 
481 	iavf_stop_queues(dev);
482 
483 	/* Disable the interrupt for Rx */
484 	rte_intr_efd_disable(intr_handle);
485 	/* Rx interrupt vector mapping free */
486 	if (intr_handle->intr_vec) {
487 		rte_free(intr_handle->intr_vec);
488 		intr_handle->intr_vec = NULL;
489 	}
490 
491 	/* remove all mac addrs */
492 	iavf_add_del_all_mac_addr(adapter, FALSE);
493 	hw->adapter_stopped = 1;
494 }
495 
496 static void
497 iavf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
498 {
499 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
500 
501 	memset(dev_info, 0, sizeof(*dev_info));
502 	dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
503 	dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
504 	dev_info->min_rx_bufsize = IAVF_BUF_SIZE_MIN;
505 	dev_info->max_rx_pktlen = IAVF_FRAME_SIZE_MAX;
506 	dev_info->hash_key_size = vf->vf_res->rss_key_size;
507 	dev_info->reta_size = vf->vf_res->rss_lut_size;
508 	dev_info->flow_type_rss_offloads = IAVF_RSS_OFFLOAD_ALL;
509 	dev_info->max_mac_addrs = IAVF_NUM_MACADDR_MAX;
510 	dev_info->rx_offload_capa =
511 		DEV_RX_OFFLOAD_VLAN_STRIP |
512 		DEV_RX_OFFLOAD_QINQ_STRIP |
513 		DEV_RX_OFFLOAD_IPV4_CKSUM |
514 		DEV_RX_OFFLOAD_UDP_CKSUM |
515 		DEV_RX_OFFLOAD_TCP_CKSUM |
516 		DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
517 		DEV_RX_OFFLOAD_SCATTER |
518 		DEV_RX_OFFLOAD_JUMBO_FRAME |
519 		DEV_RX_OFFLOAD_VLAN_FILTER;
520 	dev_info->tx_offload_capa =
521 		DEV_TX_OFFLOAD_VLAN_INSERT |
522 		DEV_TX_OFFLOAD_QINQ_INSERT |
523 		DEV_TX_OFFLOAD_IPV4_CKSUM |
524 		DEV_TX_OFFLOAD_UDP_CKSUM |
525 		DEV_TX_OFFLOAD_TCP_CKSUM |
526 		DEV_TX_OFFLOAD_SCTP_CKSUM |
527 		DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
528 		DEV_TX_OFFLOAD_TCP_TSO |
529 		DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
530 		DEV_TX_OFFLOAD_GRE_TNL_TSO |
531 		DEV_TX_OFFLOAD_IPIP_TNL_TSO |
532 		DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
533 		DEV_TX_OFFLOAD_MULTI_SEGS;
534 
535 	dev_info->default_rxconf = (struct rte_eth_rxconf) {
536 		.rx_free_thresh = IAVF_DEFAULT_RX_FREE_THRESH,
537 		.rx_drop_en = 0,
538 		.offloads = 0,
539 	};
540 
541 	dev_info->default_txconf = (struct rte_eth_txconf) {
542 		.tx_free_thresh = IAVF_DEFAULT_TX_FREE_THRESH,
543 		.tx_rs_thresh = IAVF_DEFAULT_TX_RS_THRESH,
544 		.offloads = 0,
545 	};
546 
547 	dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
548 		.nb_max = IAVF_MAX_RING_DESC,
549 		.nb_min = IAVF_MIN_RING_DESC,
550 		.nb_align = IAVF_ALIGN_RING_DESC,
551 	};
552 
553 	dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
554 		.nb_max = IAVF_MAX_RING_DESC,
555 		.nb_min = IAVF_MIN_RING_DESC,
556 		.nb_align = IAVF_ALIGN_RING_DESC,
557 	};
558 }
559 
560 static const uint32_t *
561 iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
562 {
563 	static const uint32_t ptypes[] = {
564 		RTE_PTYPE_L2_ETHER,
565 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
566 		RTE_PTYPE_L4_FRAG,
567 		RTE_PTYPE_L4_ICMP,
568 		RTE_PTYPE_L4_NONFRAG,
569 		RTE_PTYPE_L4_SCTP,
570 		RTE_PTYPE_L4_TCP,
571 		RTE_PTYPE_L4_UDP,
572 		RTE_PTYPE_UNKNOWN
573 	};
574 	return ptypes;
575 }
576 
577 int
578 iavf_dev_link_update(struct rte_eth_dev *dev,
579 		    __rte_unused int wait_to_complete)
580 {
581 	struct rte_eth_link new_link;
582 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
583 
584 	/* Only read status info stored in VF, and the info is updated
585 	 *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
586 	 */
587 	switch (vf->link_speed) {
588 	case VIRTCHNL_LINK_SPEED_100MB:
589 		new_link.link_speed = ETH_SPEED_NUM_100M;
590 		break;
591 	case VIRTCHNL_LINK_SPEED_1GB:
592 		new_link.link_speed = ETH_SPEED_NUM_1G;
593 		break;
594 	case VIRTCHNL_LINK_SPEED_10GB:
595 		new_link.link_speed = ETH_SPEED_NUM_10G;
596 		break;
597 	case VIRTCHNL_LINK_SPEED_20GB:
598 		new_link.link_speed = ETH_SPEED_NUM_20G;
599 		break;
600 	case VIRTCHNL_LINK_SPEED_25GB:
601 		new_link.link_speed = ETH_SPEED_NUM_25G;
602 		break;
603 	case VIRTCHNL_LINK_SPEED_40GB:
604 		new_link.link_speed = ETH_SPEED_NUM_40G;
605 		break;
606 	default:
607 		new_link.link_speed = ETH_SPEED_NUM_NONE;
608 		break;
609 	}
610 
611 	new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
612 	new_link.link_status = vf->link_up ? ETH_LINK_UP :
613 					     ETH_LINK_DOWN;
614 	new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
615 				ETH_LINK_SPEED_FIXED);
616 
617 	if (rte_atomic64_cmpset((uint64_t *)&dev->data->dev_link,
618 				*(uint64_t *)&dev->data->dev_link,
619 				*(uint64_t *)&new_link) == 0)
620 		return -1;
621 
622 	return 0;
623 }
624 
625 static void
626 iavf_dev_promiscuous_enable(struct rte_eth_dev *dev)
627 {
628 	struct iavf_adapter *adapter =
629 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
630 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
631 	int ret;
632 
633 	if (vf->promisc_unicast_enabled)
634 		return;
635 
636 	ret = iavf_config_promisc(adapter, TRUE, vf->promisc_multicast_enabled);
637 	if (!ret)
638 		vf->promisc_unicast_enabled = TRUE;
639 }
640 
641 static void
642 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
643 {
644 	struct iavf_adapter *adapter =
645 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
646 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
647 	int ret;
648 
649 	if (!vf->promisc_unicast_enabled)
650 		return;
651 
652 	ret = iavf_config_promisc(adapter, FALSE, vf->promisc_multicast_enabled);
653 	if (!ret)
654 		vf->promisc_unicast_enabled = FALSE;
655 }
656 
657 static void
658 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
659 {
660 	struct iavf_adapter *adapter =
661 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
662 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
663 	int ret;
664 
665 	if (vf->promisc_multicast_enabled)
666 		return;
667 
668 	ret = iavf_config_promisc(adapter, vf->promisc_unicast_enabled, TRUE);
669 	if (!ret)
670 		vf->promisc_multicast_enabled = TRUE;
671 }
672 
673 static void
674 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
675 {
676 	struct iavf_adapter *adapter =
677 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
678 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
679 	int ret;
680 
681 	if (!vf->promisc_multicast_enabled)
682 		return;
683 
684 	ret = iavf_config_promisc(adapter, vf->promisc_unicast_enabled, FALSE);
685 	if (!ret)
686 		vf->promisc_multicast_enabled = FALSE;
687 }
688 
689 static int
690 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr,
691 		     __rte_unused uint32_t index,
692 		     __rte_unused uint32_t pool)
693 {
694 	struct iavf_adapter *adapter =
695 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
696 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
697 	int err;
698 
699 	if (is_zero_ether_addr(addr)) {
700 		PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
701 		return -EINVAL;
702 	}
703 
704 	err = iavf_add_del_eth_addr(adapter, addr, TRUE);
705 	if (err) {
706 		PMD_DRV_LOG(ERR, "fail to add MAC address");
707 		return -EIO;
708 	}
709 
710 	vf->mac_num++;
711 
712 	return 0;
713 }
714 
715 static void
716 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
717 {
718 	struct iavf_adapter *adapter =
719 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
720 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
721 	struct ether_addr *addr;
722 	int err;
723 
724 	addr = &dev->data->mac_addrs[index];
725 
726 	err = iavf_add_del_eth_addr(adapter, addr, FALSE);
727 	if (err)
728 		PMD_DRV_LOG(ERR, "fail to delete MAC address");
729 
730 	vf->mac_num--;
731 }
732 
733 static int
734 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
735 {
736 	struct iavf_adapter *adapter =
737 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
738 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
739 	int err;
740 
741 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
742 		return -ENOTSUP;
743 
744 	err = iavf_add_del_vlan(adapter, vlan_id, on);
745 	if (err)
746 		return -EIO;
747 	return 0;
748 }
749 
750 static int
751 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
752 {
753 	struct iavf_adapter *adapter =
754 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
755 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
756 	struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
757 	int err;
758 
759 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
760 		return -ENOTSUP;
761 
762 	/* Vlan stripping setting */
763 	if (mask & ETH_VLAN_STRIP_MASK) {
764 		/* Enable or disable VLAN stripping */
765 		if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
766 			err = iavf_enable_vlan_strip(adapter);
767 		else
768 			err = iavf_disable_vlan_strip(adapter);
769 
770 		if (err)
771 			return -EIO;
772 	}
773 	return 0;
774 }
775 
776 static int
777 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
778 			struct rte_eth_rss_reta_entry64 *reta_conf,
779 			uint16_t reta_size)
780 {
781 	struct iavf_adapter *adapter =
782 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
783 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
784 	uint8_t *lut;
785 	uint16_t i, idx, shift;
786 	int ret;
787 
788 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
789 		return -ENOTSUP;
790 
791 	if (reta_size != vf->vf_res->rss_lut_size) {
792 		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
793 			"(%d) doesn't match the number of hardware can "
794 			"support (%d)", reta_size, vf->vf_res->rss_lut_size);
795 		return -EINVAL;
796 	}
797 
798 	lut = rte_zmalloc("rss_lut", reta_size, 0);
799 	if (!lut) {
800 		PMD_DRV_LOG(ERR, "No memory can be allocated");
801 		return -ENOMEM;
802 	}
803 	/* store the old lut table temporarily */
804 	rte_memcpy(lut, vf->rss_lut, reta_size);
805 
806 	for (i = 0; i < reta_size; i++) {
807 		idx = i / RTE_RETA_GROUP_SIZE;
808 		shift = i % RTE_RETA_GROUP_SIZE;
809 		if (reta_conf[idx].mask & (1ULL << shift))
810 			lut[i] = reta_conf[idx].reta[shift];
811 	}
812 
813 	rte_memcpy(vf->rss_lut, lut, reta_size);
814 	/* send virtchnnl ops to configure rss*/
815 	ret = iavf_configure_rss_lut(adapter);
816 	if (ret) /* revert back */
817 		rte_memcpy(vf->rss_lut, lut, reta_size);
818 	rte_free(lut);
819 
820 	return ret;
821 }
822 
823 static int
824 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
825 		       struct rte_eth_rss_reta_entry64 *reta_conf,
826 		       uint16_t reta_size)
827 {
828 	struct iavf_adapter *adapter =
829 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
830 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
831 	uint16_t i, idx, shift;
832 
833 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
834 		return -ENOTSUP;
835 
836 	if (reta_size != vf->vf_res->rss_lut_size) {
837 		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
838 			"(%d) doesn't match the number of hardware can "
839 			"support (%d)", reta_size, vf->vf_res->rss_lut_size);
840 		return -EINVAL;
841 	}
842 
843 	for (i = 0; i < reta_size; i++) {
844 		idx = i / RTE_RETA_GROUP_SIZE;
845 		shift = i % RTE_RETA_GROUP_SIZE;
846 		if (reta_conf[idx].mask & (1ULL << shift))
847 			reta_conf[idx].reta[shift] = vf->rss_lut[i];
848 	}
849 
850 	return 0;
851 }
852 
853 static int
854 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
855 			struct rte_eth_rss_conf *rss_conf)
856 {
857 	struct iavf_adapter *adapter =
858 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
859 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
860 
861 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
862 		return -ENOTSUP;
863 
864 	/* HENA setting, it is enabled by default, no change */
865 	if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
866 		PMD_DRV_LOG(DEBUG, "No key to be configured");
867 		return 0;
868 	} else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
869 		PMD_DRV_LOG(ERR, "The size of hash key configured "
870 			"(%d) doesn't match the size of hardware can "
871 			"support (%d)", rss_conf->rss_key_len,
872 			vf->vf_res->rss_key_size);
873 		return -EINVAL;
874 	}
875 
876 	rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
877 
878 	return iavf_configure_rss_key(adapter);
879 }
880 
881 static int
882 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
883 			  struct rte_eth_rss_conf *rss_conf)
884 {
885 	struct iavf_adapter *adapter =
886 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
887 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
888 
889 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
890 		return -ENOTSUP;
891 
892 	 /* Just set it to default value now. */
893 	rss_conf->rss_hf = IAVF_RSS_OFFLOAD_ALL;
894 
895 	if (!rss_conf->rss_key)
896 		return 0;
897 
898 	rss_conf->rss_key_len = vf->vf_res->rss_key_size;
899 	rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
900 
901 	return 0;
902 }
903 
904 static int
905 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
906 {
907 	uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
908 	int ret = 0;
909 
910 	if (mtu < ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
911 		return -EINVAL;
912 
913 	/* mtu setting is forbidden if port is start */
914 	if (dev->data->dev_started) {
915 		PMD_DRV_LOG(ERR, "port must be stopped before configuration");
916 		return -EBUSY;
917 	}
918 
919 	if (frame_size > ETHER_MAX_LEN)
920 		dev->data->dev_conf.rxmode.offloads |=
921 				DEV_RX_OFFLOAD_JUMBO_FRAME;
922 	else
923 		dev->data->dev_conf.rxmode.offloads &=
924 				~DEV_RX_OFFLOAD_JUMBO_FRAME;
925 
926 	dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
927 
928 	return ret;
929 }
930 
931 static int
932 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
933 			     struct ether_addr *mac_addr)
934 {
935 	struct iavf_adapter *adapter =
936 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
937 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
938 	struct ether_addr *perm_addr, *old_addr;
939 	int ret;
940 
941 	old_addr = (struct ether_addr *)hw->mac.addr;
942 	perm_addr = (struct ether_addr *)hw->mac.perm_addr;
943 
944 	if (is_same_ether_addr(mac_addr, old_addr))
945 		return 0;
946 
947 	/* If the MAC address is configured by host, skip the setting */
948 	if (is_valid_assigned_ether_addr(perm_addr))
949 		return -EPERM;
950 
951 	ret = iavf_add_del_eth_addr(adapter, old_addr, FALSE);
952 	if (ret)
953 		PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
954 			    " %02X:%02X:%02X:%02X:%02X:%02X",
955 			    old_addr->addr_bytes[0],
956 			    old_addr->addr_bytes[1],
957 			    old_addr->addr_bytes[2],
958 			    old_addr->addr_bytes[3],
959 			    old_addr->addr_bytes[4],
960 			    old_addr->addr_bytes[5]);
961 
962 	ret = iavf_add_del_eth_addr(adapter, mac_addr, TRUE);
963 	if (ret)
964 		PMD_DRV_LOG(ERR, "Fail to add new MAC:"
965 			    " %02X:%02X:%02X:%02X:%02X:%02X",
966 			    mac_addr->addr_bytes[0],
967 			    mac_addr->addr_bytes[1],
968 			    mac_addr->addr_bytes[2],
969 			    mac_addr->addr_bytes[3],
970 			    mac_addr->addr_bytes[4],
971 			    mac_addr->addr_bytes[5]);
972 
973 	if (ret)
974 		return -EIO;
975 
976 	ether_addr_copy(mac_addr, (struct ether_addr *)hw->mac.addr);
977 	return 0;
978 }
979 
980 static int
981 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
982 {
983 	struct iavf_adapter *adapter =
984 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
985 	struct virtchnl_eth_stats *pstats = NULL;
986 	int ret;
987 
988 	ret = iavf_query_stats(adapter, &pstats);
989 	if (ret == 0) {
990 		stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
991 						pstats->rx_broadcast;
992 		stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
993 						pstats->tx_unicast;
994 		stats->imissed = pstats->rx_discards;
995 		stats->oerrors = pstats->tx_errors + pstats->tx_discards;
996 		stats->ibytes = pstats->rx_bytes;
997 		stats->obytes = pstats->tx_bytes;
998 	} else {
999 		PMD_DRV_LOG(ERR, "Get statistics failed");
1000 	}
1001 	return -EIO;
1002 }
1003 
1004 static int
1005 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1006 {
1007 	struct iavf_adapter *adapter =
1008 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1009 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1010 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1011 	uint16_t msix_intr;
1012 
1013 	msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1014 	if (msix_intr == IAVF_MISC_VEC_ID) {
1015 		PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1016 		IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01,
1017 			      IAVFINT_DYN_CTL01_INTENA_MASK |
1018 			      IAVFINT_DYN_CTL01_ITR_INDX_MASK);
1019 	} else {
1020 		IAVF_WRITE_REG(hw,
1021 			      IAVFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1022 			      IAVFINT_DYN_CTLN1_INTENA_MASK |
1023 			      IAVFINT_DYN_CTLN1_ITR_INDX_MASK);
1024 	}
1025 
1026 	IAVF_WRITE_FLUSH(hw);
1027 
1028 	rte_intr_enable(&pci_dev->intr_handle);
1029 
1030 	return 0;
1031 }
1032 
1033 static int
1034 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1035 {
1036 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1037 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1038 	uint16_t msix_intr;
1039 
1040 	msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1041 	if (msix_intr == IAVF_MISC_VEC_ID) {
1042 		PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1043 		return -EIO;
1044 	}
1045 
1046 	IAVF_WRITE_REG(hw,
1047 		      IAVFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1048 		      0);
1049 
1050 	IAVF_WRITE_FLUSH(hw);
1051 	return 0;
1052 }
1053 
1054 static int
1055 iavf_check_vf_reset_done(struct iavf_hw *hw)
1056 {
1057 	int i, reset;
1058 
1059 	for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1060 		reset = IAVF_READ_REG(hw, IAVFGEN_RSTAT) &
1061 			IAVFGEN_RSTAT_VFR_STATE_MASK;
1062 		reset = reset >> IAVFGEN_RSTAT_VFR_STATE_SHIFT;
1063 		if (reset == VIRTCHNL_VFR_VFACTIVE ||
1064 		    reset == VIRTCHNL_VFR_COMPLETED)
1065 			break;
1066 		rte_delay_ms(20);
1067 	}
1068 
1069 	if (i >= IAVF_RESET_WAIT_CNT)
1070 		return -1;
1071 
1072 	return 0;
1073 }
1074 
1075 static int
1076 iavf_init_vf(struct rte_eth_dev *dev)
1077 {
1078 	int err, bufsz;
1079 	struct iavf_adapter *adapter =
1080 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1081 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1082 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1083 
1084 	err = iavf_set_mac_type(hw);
1085 	if (err) {
1086 		PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1087 		goto err;
1088 	}
1089 
1090 	err = iavf_check_vf_reset_done(hw);
1091 	if (err) {
1092 		PMD_INIT_LOG(ERR, "VF is still resetting");
1093 		goto err;
1094 	}
1095 
1096 	iavf_init_adminq_parameter(hw);
1097 	err = iavf_init_adminq(hw);
1098 	if (err) {
1099 		PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1100 		goto err;
1101 	}
1102 
1103 	vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1104 	if (!vf->aq_resp) {
1105 		PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1106 		goto err_aq;
1107 	}
1108 	if (iavf_check_api_version(adapter) != 0) {
1109 		PMD_INIT_LOG(ERR, "check_api version failed");
1110 		goto err_api;
1111 	}
1112 
1113 	bufsz = sizeof(struct virtchnl_vf_resource) +
1114 		(IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1115 	vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1116 	if (!vf->vf_res) {
1117 		PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1118 		goto err_api;
1119 	}
1120 	if (iavf_get_vf_resource(adapter) != 0) {
1121 		PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1122 		goto err_alloc;
1123 	}
1124 	/* Allocate memort for RSS info */
1125 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1126 		vf->rss_key = rte_zmalloc("rss_key",
1127 					  vf->vf_res->rss_key_size, 0);
1128 		if (!vf->rss_key) {
1129 			PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
1130 			goto err_rss;
1131 		}
1132 		vf->rss_lut = rte_zmalloc("rss_lut",
1133 					  vf->vf_res->rss_lut_size, 0);
1134 		if (!vf->rss_lut) {
1135 			PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1136 			goto err_rss;
1137 		}
1138 	}
1139 	return 0;
1140 err_rss:
1141 	rte_free(vf->rss_key);
1142 	rte_free(vf->rss_lut);
1143 err_alloc:
1144 	rte_free(vf->vf_res);
1145 	vf->vsi_res = NULL;
1146 err_api:
1147 	rte_free(vf->aq_resp);
1148 err_aq:
1149 	iavf_shutdown_adminq(hw);
1150 err:
1151 	return -1;
1152 }
1153 
1154 /* Enable default admin queue interrupt setting */
1155 static inline void
1156 iavf_enable_irq0(struct iavf_hw *hw)
1157 {
1158 	/* Enable admin queue interrupt trigger */
1159 	IAVF_WRITE_REG(hw, IAVFINT_ICR0_ENA1, IAVFINT_ICR0_ENA1_ADMINQ_MASK);
1160 
1161 	IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01, IAVFINT_DYN_CTL01_INTENA_MASK |
1162 		IAVFINT_DYN_CTL01_CLEARPBA_MASK | IAVFINT_DYN_CTL01_ITR_INDX_MASK);
1163 
1164 	IAVF_WRITE_FLUSH(hw);
1165 }
1166 
1167 static inline void
1168 iavf_disable_irq0(struct iavf_hw *hw)
1169 {
1170 	/* Disable all interrupt types */
1171 	IAVF_WRITE_REG(hw, IAVFINT_ICR0_ENA1, 0);
1172 	IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01,
1173 		      IAVFINT_DYN_CTL01_ITR_INDX_MASK);
1174 	IAVF_WRITE_FLUSH(hw);
1175 }
1176 
1177 static void
1178 iavf_dev_interrupt_handler(void *param)
1179 {
1180 	struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1181 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1182 
1183 	iavf_disable_irq0(hw);
1184 
1185 	iavf_handle_virtchnl_msg(dev);
1186 
1187 	iavf_enable_irq0(hw);
1188 }
1189 
1190 static int
1191 iavf_dev_init(struct rte_eth_dev *eth_dev)
1192 {
1193 	struct iavf_adapter *adapter =
1194 		IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1195 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1196 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1197 
1198 	PMD_INIT_FUNC_TRACE();
1199 
1200 	/* assign ops func pointer */
1201 	eth_dev->dev_ops = &iavf_eth_dev_ops;
1202 	eth_dev->rx_pkt_burst = &iavf_recv_pkts;
1203 	eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
1204 	eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
1205 
1206 	/* For secondary processes, we don't initialise any further as primary
1207 	 * has already done this work. Only check if we need a different RX
1208 	 * and TX function.
1209 	 */
1210 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1211 		iavf_set_rx_function(eth_dev);
1212 		iavf_set_tx_function(eth_dev);
1213 		return 0;
1214 	}
1215 	rte_eth_copy_pci_info(eth_dev, pci_dev);
1216 
1217 	hw->vendor_id = pci_dev->id.vendor_id;
1218 	hw->device_id = pci_dev->id.device_id;
1219 	hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1220 	hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1221 	hw->bus.bus_id = pci_dev->addr.bus;
1222 	hw->bus.device = pci_dev->addr.devid;
1223 	hw->bus.func = pci_dev->addr.function;
1224 	hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1225 	hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1226 	adapter->eth_dev = eth_dev;
1227 
1228 	if (iavf_init_vf(eth_dev) != 0) {
1229 		PMD_INIT_LOG(ERR, "Init vf failed");
1230 		return -1;
1231 	}
1232 
1233 	/* copy mac addr */
1234 	eth_dev->data->mac_addrs = rte_zmalloc(
1235 					"iavf_mac",
1236 					ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX,
1237 					0);
1238 	if (!eth_dev->data->mac_addrs) {
1239 		PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1240 			     " store MAC addresses",
1241 			     ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
1242 		return -ENOMEM;
1243 	}
1244 	/* If the MAC address is not configured by host,
1245 	 * generate a random one.
1246 	 */
1247 	if (!is_valid_assigned_ether_addr((struct ether_addr *)hw->mac.addr))
1248 		eth_random_addr(hw->mac.addr);
1249 	ether_addr_copy((struct ether_addr *)hw->mac.addr,
1250 			&eth_dev->data->mac_addrs[0]);
1251 
1252 	/* register callback func to eal lib */
1253 	rte_intr_callback_register(&pci_dev->intr_handle,
1254 				   iavf_dev_interrupt_handler,
1255 				   (void *)eth_dev);
1256 
1257 	/* enable uio intr after callback register */
1258 	rte_intr_enable(&pci_dev->intr_handle);
1259 
1260 	/* configure and enable device interrupt */
1261 	iavf_enable_irq0(hw);
1262 
1263 	return 0;
1264 }
1265 
1266 static void
1267 iavf_dev_close(struct rte_eth_dev *dev)
1268 {
1269 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1270 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1271 	struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1272 
1273 	iavf_dev_stop(dev);
1274 	iavf_shutdown_adminq(hw);
1275 	/* disable uio intr before callback unregister */
1276 	rte_intr_disable(intr_handle);
1277 
1278 	/* unregister callback func from eal lib */
1279 	rte_intr_callback_unregister(intr_handle,
1280 				     iavf_dev_interrupt_handler, dev);
1281 	iavf_disable_irq0(hw);
1282 }
1283 
1284 static int
1285 iavf_dev_uninit(struct rte_eth_dev *dev)
1286 {
1287 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1288 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1289 
1290 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1291 		return -EPERM;
1292 
1293 	dev->dev_ops = NULL;
1294 	dev->rx_pkt_burst = NULL;
1295 	dev->tx_pkt_burst = NULL;
1296 	if (hw->adapter_stopped == 0)
1297 		iavf_dev_close(dev);
1298 
1299 	rte_free(vf->vf_res);
1300 	vf->vsi_res = NULL;
1301 	vf->vf_res = NULL;
1302 
1303 	rte_free(vf->aq_resp);
1304 	vf->aq_resp = NULL;
1305 
1306 	if (vf->rss_lut) {
1307 		rte_free(vf->rss_lut);
1308 		vf->rss_lut = NULL;
1309 	}
1310 	if (vf->rss_key) {
1311 		rte_free(vf->rss_key);
1312 		vf->rss_key = NULL;
1313 	}
1314 
1315 	return 0;
1316 }
1317 
1318 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1319 			     struct rte_pci_device *pci_dev)
1320 {
1321 	return rte_eth_dev_pci_generic_probe(pci_dev,
1322 		sizeof(struct iavf_adapter), iavf_dev_init);
1323 }
1324 
1325 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
1326 {
1327 	return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
1328 }
1329 
1330 /* Adaptive virtual function driver struct */
1331 static struct rte_pci_driver rte_iavf_pmd = {
1332 	.id_table = pci_id_iavf_map,
1333 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
1334 		     RTE_PCI_DRV_IOVA_AS_VA,
1335 	.probe = eth_iavf_pci_probe,
1336 	.remove = eth_iavf_pci_remove,
1337 };
1338 
1339 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
1340 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
1341 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
1342 RTE_INIT(iavf_init_log)
1343 {
1344 	iavf_logtype_init = rte_log_register("pmd.net.iavf.init");
1345 	if (iavf_logtype_init >= 0)
1346 		rte_log_set_level(iavf_logtype_init, RTE_LOG_NOTICE);
1347 	iavf_logtype_driver = rte_log_register("pmd.net.iavf.driver");
1348 	if (iavf_logtype_driver >= 0)
1349 		rte_log_set_level(iavf_logtype_driver, RTE_LOG_NOTICE);
1350 }
1351 
1352 /* memory func for base code */
1353 enum iavf_status_code
1354 iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw *hw,
1355 		       struct iavf_dma_mem *mem,
1356 		       u64 size,
1357 		       u32 alignment)
1358 {
1359 	const struct rte_memzone *mz = NULL;
1360 	char z_name[RTE_MEMZONE_NAMESIZE];
1361 
1362 	if (!mem)
1363 		return IAVF_ERR_PARAM;
1364 
1365 	snprintf(z_name, sizeof(z_name), "iavf_dma_%"PRIu64, rte_rand());
1366 	mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY,
1367 			RTE_MEMZONE_IOVA_CONTIG, alignment, RTE_PGSIZE_2M);
1368 	if (!mz)
1369 		return IAVF_ERR_NO_MEMORY;
1370 
1371 	mem->size = size;
1372 	mem->va = mz->addr;
1373 	mem->pa = mz->phys_addr;
1374 	mem->zone = (const void *)mz;
1375 	PMD_DRV_LOG(DEBUG,
1376 		    "memzone %s allocated with physical address: %"PRIu64,
1377 		    mz->name, mem->pa);
1378 
1379 	return IAVF_SUCCESS;
1380 }
1381 
1382 enum iavf_status_code
1383 iavf_free_dma_mem_d(__rte_unused struct iavf_hw *hw,
1384 		   struct iavf_dma_mem *mem)
1385 {
1386 	if (!mem)
1387 		return IAVF_ERR_PARAM;
1388 
1389 	PMD_DRV_LOG(DEBUG,
1390 		    "memzone %s to be freed with physical address: %"PRIu64,
1391 		    ((const struct rte_memzone *)mem->zone)->name, mem->pa);
1392 	rte_memzone_free((const struct rte_memzone *)mem->zone);
1393 	mem->zone = NULL;
1394 	mem->va = NULL;
1395 	mem->pa = (u64)0;
1396 
1397 	return IAVF_SUCCESS;
1398 }
1399 
1400 enum iavf_status_code
1401 iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw *hw,
1402 			struct iavf_virt_mem *mem,
1403 			u32 size)
1404 {
1405 	if (!mem)
1406 		return IAVF_ERR_PARAM;
1407 
1408 	mem->size = size;
1409 	mem->va = rte_zmalloc("iavf", size, 0);
1410 
1411 	if (mem->va)
1412 		return IAVF_SUCCESS;
1413 	else
1414 		return IAVF_ERR_NO_MEMORY;
1415 }
1416 
1417 enum iavf_status_code
1418 iavf_free_virt_mem_d(__rte_unused struct iavf_hw *hw,
1419 		    struct iavf_virt_mem *mem)
1420 {
1421 	if (!mem)
1422 		return IAVF_ERR_PARAM;
1423 
1424 	rte_free(mem->va);
1425 	mem->va = NULL;
1426 
1427 	return IAVF_SUCCESS;
1428 }
1429