xref: /dpdk/drivers/net/iavf/iavf_ethdev.c (revision 92c9ba90)
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 	dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
502 	dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
503 	dev_info->min_rx_bufsize = IAVF_BUF_SIZE_MIN;
504 	dev_info->max_rx_pktlen = IAVF_FRAME_SIZE_MAX;
505 	dev_info->hash_key_size = vf->vf_res->rss_key_size;
506 	dev_info->reta_size = vf->vf_res->rss_lut_size;
507 	dev_info->flow_type_rss_offloads = IAVF_RSS_OFFLOAD_ALL;
508 	dev_info->max_mac_addrs = IAVF_NUM_MACADDR_MAX;
509 	dev_info->rx_offload_capa =
510 		DEV_RX_OFFLOAD_VLAN_STRIP |
511 		DEV_RX_OFFLOAD_QINQ_STRIP |
512 		DEV_RX_OFFLOAD_IPV4_CKSUM |
513 		DEV_RX_OFFLOAD_UDP_CKSUM |
514 		DEV_RX_OFFLOAD_TCP_CKSUM |
515 		DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
516 		DEV_RX_OFFLOAD_SCATTER |
517 		DEV_RX_OFFLOAD_JUMBO_FRAME |
518 		DEV_RX_OFFLOAD_VLAN_FILTER;
519 	dev_info->tx_offload_capa =
520 		DEV_TX_OFFLOAD_VLAN_INSERT |
521 		DEV_TX_OFFLOAD_QINQ_INSERT |
522 		DEV_TX_OFFLOAD_IPV4_CKSUM |
523 		DEV_TX_OFFLOAD_UDP_CKSUM |
524 		DEV_TX_OFFLOAD_TCP_CKSUM |
525 		DEV_TX_OFFLOAD_SCTP_CKSUM |
526 		DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
527 		DEV_TX_OFFLOAD_TCP_TSO |
528 		DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
529 		DEV_TX_OFFLOAD_GRE_TNL_TSO |
530 		DEV_TX_OFFLOAD_IPIP_TNL_TSO |
531 		DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
532 		DEV_TX_OFFLOAD_MULTI_SEGS;
533 
534 	dev_info->default_rxconf = (struct rte_eth_rxconf) {
535 		.rx_free_thresh = IAVF_DEFAULT_RX_FREE_THRESH,
536 		.rx_drop_en = 0,
537 		.offloads = 0,
538 	};
539 
540 	dev_info->default_txconf = (struct rte_eth_txconf) {
541 		.tx_free_thresh = IAVF_DEFAULT_TX_FREE_THRESH,
542 		.tx_rs_thresh = IAVF_DEFAULT_TX_RS_THRESH,
543 		.offloads = 0,
544 	};
545 
546 	dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
547 		.nb_max = IAVF_MAX_RING_DESC,
548 		.nb_min = IAVF_MIN_RING_DESC,
549 		.nb_align = IAVF_ALIGN_RING_DESC,
550 	};
551 
552 	dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
553 		.nb_max = IAVF_MAX_RING_DESC,
554 		.nb_min = IAVF_MIN_RING_DESC,
555 		.nb_align = IAVF_ALIGN_RING_DESC,
556 	};
557 }
558 
559 static const uint32_t *
560 iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
561 {
562 	static const uint32_t ptypes[] = {
563 		RTE_PTYPE_L2_ETHER,
564 		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
565 		RTE_PTYPE_L4_FRAG,
566 		RTE_PTYPE_L4_ICMP,
567 		RTE_PTYPE_L4_NONFRAG,
568 		RTE_PTYPE_L4_SCTP,
569 		RTE_PTYPE_L4_TCP,
570 		RTE_PTYPE_L4_UDP,
571 		RTE_PTYPE_UNKNOWN
572 	};
573 	return ptypes;
574 }
575 
576 int
577 iavf_dev_link_update(struct rte_eth_dev *dev,
578 		    __rte_unused int wait_to_complete)
579 {
580 	struct rte_eth_link new_link;
581 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
582 
583 	/* Only read status info stored in VF, and the info is updated
584 	 *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
585 	 */
586 	switch (vf->link_speed) {
587 	case VIRTCHNL_LINK_SPEED_100MB:
588 		new_link.link_speed = ETH_SPEED_NUM_100M;
589 		break;
590 	case VIRTCHNL_LINK_SPEED_1GB:
591 		new_link.link_speed = ETH_SPEED_NUM_1G;
592 		break;
593 	case VIRTCHNL_LINK_SPEED_10GB:
594 		new_link.link_speed = ETH_SPEED_NUM_10G;
595 		break;
596 	case VIRTCHNL_LINK_SPEED_20GB:
597 		new_link.link_speed = ETH_SPEED_NUM_20G;
598 		break;
599 	case VIRTCHNL_LINK_SPEED_25GB:
600 		new_link.link_speed = ETH_SPEED_NUM_25G;
601 		break;
602 	case VIRTCHNL_LINK_SPEED_40GB:
603 		new_link.link_speed = ETH_SPEED_NUM_40G;
604 		break;
605 	default:
606 		new_link.link_speed = ETH_SPEED_NUM_NONE;
607 		break;
608 	}
609 
610 	new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
611 	new_link.link_status = vf->link_up ? ETH_LINK_UP :
612 					     ETH_LINK_DOWN;
613 	new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
614 				ETH_LINK_SPEED_FIXED);
615 
616 	if (rte_atomic64_cmpset((uint64_t *)&dev->data->dev_link,
617 				*(uint64_t *)&dev->data->dev_link,
618 				*(uint64_t *)&new_link) == 0)
619 		return -1;
620 
621 	return 0;
622 }
623 
624 static void
625 iavf_dev_promiscuous_enable(struct rte_eth_dev *dev)
626 {
627 	struct iavf_adapter *adapter =
628 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
629 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
630 	int ret;
631 
632 	if (vf->promisc_unicast_enabled)
633 		return;
634 
635 	ret = iavf_config_promisc(adapter, TRUE, vf->promisc_multicast_enabled);
636 	if (!ret)
637 		vf->promisc_unicast_enabled = TRUE;
638 }
639 
640 static void
641 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
642 {
643 	struct iavf_adapter *adapter =
644 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
645 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
646 	int ret;
647 
648 	if (!vf->promisc_unicast_enabled)
649 		return;
650 
651 	ret = iavf_config_promisc(adapter, FALSE, vf->promisc_multicast_enabled);
652 	if (!ret)
653 		vf->promisc_unicast_enabled = FALSE;
654 }
655 
656 static void
657 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
658 {
659 	struct iavf_adapter *adapter =
660 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
661 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
662 	int ret;
663 
664 	if (vf->promisc_multicast_enabled)
665 		return;
666 
667 	ret = iavf_config_promisc(adapter, vf->promisc_unicast_enabled, TRUE);
668 	if (!ret)
669 		vf->promisc_multicast_enabled = TRUE;
670 }
671 
672 static void
673 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
674 {
675 	struct iavf_adapter *adapter =
676 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
677 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
678 	int ret;
679 
680 	if (!vf->promisc_multicast_enabled)
681 		return;
682 
683 	ret = iavf_config_promisc(adapter, vf->promisc_unicast_enabled, FALSE);
684 	if (!ret)
685 		vf->promisc_multicast_enabled = FALSE;
686 }
687 
688 static int
689 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr,
690 		     __rte_unused uint32_t index,
691 		     __rte_unused uint32_t pool)
692 {
693 	struct iavf_adapter *adapter =
694 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
695 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
696 	int err;
697 
698 	if (is_zero_ether_addr(addr)) {
699 		PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
700 		return -EINVAL;
701 	}
702 
703 	err = iavf_add_del_eth_addr(adapter, addr, TRUE);
704 	if (err) {
705 		PMD_DRV_LOG(ERR, "fail to add MAC address");
706 		return -EIO;
707 	}
708 
709 	vf->mac_num++;
710 
711 	return 0;
712 }
713 
714 static void
715 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
716 {
717 	struct iavf_adapter *adapter =
718 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
719 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
720 	struct ether_addr *addr;
721 	int err;
722 
723 	addr = &dev->data->mac_addrs[index];
724 
725 	err = iavf_add_del_eth_addr(adapter, addr, FALSE);
726 	if (err)
727 		PMD_DRV_LOG(ERR, "fail to delete MAC address");
728 
729 	vf->mac_num--;
730 }
731 
732 static int
733 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
734 {
735 	struct iavf_adapter *adapter =
736 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
737 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
738 	int err;
739 
740 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
741 		return -ENOTSUP;
742 
743 	err = iavf_add_del_vlan(adapter, vlan_id, on);
744 	if (err)
745 		return -EIO;
746 	return 0;
747 }
748 
749 static int
750 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
751 {
752 	struct iavf_adapter *adapter =
753 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
754 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
755 	struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
756 	int err;
757 
758 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
759 		return -ENOTSUP;
760 
761 	/* Vlan stripping setting */
762 	if (mask & ETH_VLAN_STRIP_MASK) {
763 		/* Enable or disable VLAN stripping */
764 		if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
765 			err = iavf_enable_vlan_strip(adapter);
766 		else
767 			err = iavf_disable_vlan_strip(adapter);
768 
769 		if (err)
770 			return -EIO;
771 	}
772 	return 0;
773 }
774 
775 static int
776 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
777 			struct rte_eth_rss_reta_entry64 *reta_conf,
778 			uint16_t reta_size)
779 {
780 	struct iavf_adapter *adapter =
781 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
782 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
783 	uint8_t *lut;
784 	uint16_t i, idx, shift;
785 	int ret;
786 
787 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
788 		return -ENOTSUP;
789 
790 	if (reta_size != vf->vf_res->rss_lut_size) {
791 		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
792 			"(%d) doesn't match the number of hardware can "
793 			"support (%d)", reta_size, vf->vf_res->rss_lut_size);
794 		return -EINVAL;
795 	}
796 
797 	lut = rte_zmalloc("rss_lut", reta_size, 0);
798 	if (!lut) {
799 		PMD_DRV_LOG(ERR, "No memory can be allocated");
800 		return -ENOMEM;
801 	}
802 	/* store the old lut table temporarily */
803 	rte_memcpy(lut, vf->rss_lut, reta_size);
804 
805 	for (i = 0; i < reta_size; i++) {
806 		idx = i / RTE_RETA_GROUP_SIZE;
807 		shift = i % RTE_RETA_GROUP_SIZE;
808 		if (reta_conf[idx].mask & (1ULL << shift))
809 			lut[i] = reta_conf[idx].reta[shift];
810 	}
811 
812 	rte_memcpy(vf->rss_lut, lut, reta_size);
813 	/* send virtchnnl ops to configure rss*/
814 	ret = iavf_configure_rss_lut(adapter);
815 	if (ret) /* revert back */
816 		rte_memcpy(vf->rss_lut, lut, reta_size);
817 	rte_free(lut);
818 
819 	return ret;
820 }
821 
822 static int
823 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
824 		       struct rte_eth_rss_reta_entry64 *reta_conf,
825 		       uint16_t reta_size)
826 {
827 	struct iavf_adapter *adapter =
828 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
829 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
830 	uint16_t i, idx, shift;
831 
832 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
833 		return -ENOTSUP;
834 
835 	if (reta_size != vf->vf_res->rss_lut_size) {
836 		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
837 			"(%d) doesn't match the number of hardware can "
838 			"support (%d)", reta_size, vf->vf_res->rss_lut_size);
839 		return -EINVAL;
840 	}
841 
842 	for (i = 0; i < reta_size; i++) {
843 		idx = i / RTE_RETA_GROUP_SIZE;
844 		shift = i % RTE_RETA_GROUP_SIZE;
845 		if (reta_conf[idx].mask & (1ULL << shift))
846 			reta_conf[idx].reta[shift] = vf->rss_lut[i];
847 	}
848 
849 	return 0;
850 }
851 
852 static int
853 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
854 			struct rte_eth_rss_conf *rss_conf)
855 {
856 	struct iavf_adapter *adapter =
857 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
858 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
859 
860 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
861 		return -ENOTSUP;
862 
863 	/* HENA setting, it is enabled by default, no change */
864 	if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
865 		PMD_DRV_LOG(DEBUG, "No key to be configured");
866 		return 0;
867 	} else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
868 		PMD_DRV_LOG(ERR, "The size of hash key configured "
869 			"(%d) doesn't match the size of hardware can "
870 			"support (%d)", rss_conf->rss_key_len,
871 			vf->vf_res->rss_key_size);
872 		return -EINVAL;
873 	}
874 
875 	rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
876 
877 	return iavf_configure_rss_key(adapter);
878 }
879 
880 static int
881 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
882 			  struct rte_eth_rss_conf *rss_conf)
883 {
884 	struct iavf_adapter *adapter =
885 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
886 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
887 
888 	if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
889 		return -ENOTSUP;
890 
891 	 /* Just set it to default value now. */
892 	rss_conf->rss_hf = IAVF_RSS_OFFLOAD_ALL;
893 
894 	if (!rss_conf->rss_key)
895 		return 0;
896 
897 	rss_conf->rss_key_len = vf->vf_res->rss_key_size;
898 	rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
899 
900 	return 0;
901 }
902 
903 static int
904 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
905 {
906 	uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
907 	int ret = 0;
908 
909 	if (mtu < ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
910 		return -EINVAL;
911 
912 	/* mtu setting is forbidden if port is start */
913 	if (dev->data->dev_started) {
914 		PMD_DRV_LOG(ERR, "port must be stopped before configuration");
915 		return -EBUSY;
916 	}
917 
918 	if (frame_size > ETHER_MAX_LEN)
919 		dev->data->dev_conf.rxmode.offloads |=
920 				DEV_RX_OFFLOAD_JUMBO_FRAME;
921 	else
922 		dev->data->dev_conf.rxmode.offloads &=
923 				~DEV_RX_OFFLOAD_JUMBO_FRAME;
924 
925 	dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
926 
927 	return ret;
928 }
929 
930 static int
931 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
932 			     struct ether_addr *mac_addr)
933 {
934 	struct iavf_adapter *adapter =
935 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
936 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
937 	struct ether_addr *perm_addr, *old_addr;
938 	int ret;
939 
940 	old_addr = (struct ether_addr *)hw->mac.addr;
941 	perm_addr = (struct ether_addr *)hw->mac.perm_addr;
942 
943 	if (is_same_ether_addr(mac_addr, old_addr))
944 		return 0;
945 
946 	/* If the MAC address is configured by host, skip the setting */
947 	if (is_valid_assigned_ether_addr(perm_addr))
948 		return -EPERM;
949 
950 	ret = iavf_add_del_eth_addr(adapter, old_addr, FALSE);
951 	if (ret)
952 		PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
953 			    " %02X:%02X:%02X:%02X:%02X:%02X",
954 			    old_addr->addr_bytes[0],
955 			    old_addr->addr_bytes[1],
956 			    old_addr->addr_bytes[2],
957 			    old_addr->addr_bytes[3],
958 			    old_addr->addr_bytes[4],
959 			    old_addr->addr_bytes[5]);
960 
961 	ret = iavf_add_del_eth_addr(adapter, mac_addr, TRUE);
962 	if (ret)
963 		PMD_DRV_LOG(ERR, "Fail to add new MAC:"
964 			    " %02X:%02X:%02X:%02X:%02X:%02X",
965 			    mac_addr->addr_bytes[0],
966 			    mac_addr->addr_bytes[1],
967 			    mac_addr->addr_bytes[2],
968 			    mac_addr->addr_bytes[3],
969 			    mac_addr->addr_bytes[4],
970 			    mac_addr->addr_bytes[5]);
971 
972 	if (ret)
973 		return -EIO;
974 
975 	ether_addr_copy(mac_addr, (struct ether_addr *)hw->mac.addr);
976 	return 0;
977 }
978 
979 static int
980 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
981 {
982 	struct iavf_adapter *adapter =
983 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
984 	struct virtchnl_eth_stats *pstats = NULL;
985 	int ret;
986 
987 	ret = iavf_query_stats(adapter, &pstats);
988 	if (ret == 0) {
989 		stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
990 						pstats->rx_broadcast;
991 		stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
992 						pstats->tx_unicast;
993 		stats->imissed = pstats->rx_discards;
994 		stats->oerrors = pstats->tx_errors + pstats->tx_discards;
995 		stats->ibytes = pstats->rx_bytes;
996 		stats->obytes = pstats->tx_bytes;
997 	} else {
998 		PMD_DRV_LOG(ERR, "Get statistics failed");
999 	}
1000 	return -EIO;
1001 }
1002 
1003 static int
1004 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1005 {
1006 	struct iavf_adapter *adapter =
1007 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1008 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1009 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1010 	uint16_t msix_intr;
1011 
1012 	msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1013 	if (msix_intr == IAVF_MISC_VEC_ID) {
1014 		PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1015 		IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01,
1016 			      IAVFINT_DYN_CTL01_INTENA_MASK |
1017 			      IAVFINT_DYN_CTL01_CLEARPBA_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_CTL01_CLEARPBA_MASK |
1024 			      IAVFINT_DYN_CTLN1_ITR_INDX_MASK);
1025 	}
1026 
1027 	IAVF_WRITE_FLUSH(hw);
1028 
1029 	rte_intr_enable(&pci_dev->intr_handle);
1030 
1031 	return 0;
1032 }
1033 
1034 static int
1035 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1036 {
1037 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1038 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1039 	uint16_t msix_intr;
1040 
1041 	msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1042 	if (msix_intr == IAVF_MISC_VEC_ID) {
1043 		PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1044 		return -EIO;
1045 	}
1046 
1047 	IAVF_WRITE_REG(hw,
1048 		      IAVFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1049 		      0);
1050 
1051 	IAVF_WRITE_FLUSH(hw);
1052 	return 0;
1053 }
1054 
1055 static int
1056 iavf_check_vf_reset_done(struct iavf_hw *hw)
1057 {
1058 	int i, reset;
1059 
1060 	for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1061 		reset = IAVF_READ_REG(hw, IAVFGEN_RSTAT) &
1062 			IAVFGEN_RSTAT_VFR_STATE_MASK;
1063 		reset = reset >> IAVFGEN_RSTAT_VFR_STATE_SHIFT;
1064 		if (reset == VIRTCHNL_VFR_VFACTIVE ||
1065 		    reset == VIRTCHNL_VFR_COMPLETED)
1066 			break;
1067 		rte_delay_ms(20);
1068 	}
1069 
1070 	if (i >= IAVF_RESET_WAIT_CNT)
1071 		return -1;
1072 
1073 	return 0;
1074 }
1075 
1076 static int
1077 iavf_init_vf(struct rte_eth_dev *dev)
1078 {
1079 	int err, bufsz;
1080 	struct iavf_adapter *adapter =
1081 		IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1082 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1083 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1084 
1085 	err = iavf_set_mac_type(hw);
1086 	if (err) {
1087 		PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1088 		goto err;
1089 	}
1090 
1091 	err = iavf_check_vf_reset_done(hw);
1092 	if (err) {
1093 		PMD_INIT_LOG(ERR, "VF is still resetting");
1094 		goto err;
1095 	}
1096 
1097 	iavf_init_adminq_parameter(hw);
1098 	err = iavf_init_adminq(hw);
1099 	if (err) {
1100 		PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1101 		goto err;
1102 	}
1103 
1104 	vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1105 	if (!vf->aq_resp) {
1106 		PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1107 		goto err_aq;
1108 	}
1109 	if (iavf_check_api_version(adapter) != 0) {
1110 		PMD_INIT_LOG(ERR, "check_api version failed");
1111 		goto err_api;
1112 	}
1113 
1114 	bufsz = sizeof(struct virtchnl_vf_resource) +
1115 		(IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1116 	vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1117 	if (!vf->vf_res) {
1118 		PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1119 		goto err_api;
1120 	}
1121 	if (iavf_get_vf_resource(adapter) != 0) {
1122 		PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1123 		goto err_alloc;
1124 	}
1125 	/* Allocate memort for RSS info */
1126 	if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1127 		vf->rss_key = rte_zmalloc("rss_key",
1128 					  vf->vf_res->rss_key_size, 0);
1129 		if (!vf->rss_key) {
1130 			PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
1131 			goto err_rss;
1132 		}
1133 		vf->rss_lut = rte_zmalloc("rss_lut",
1134 					  vf->vf_res->rss_lut_size, 0);
1135 		if (!vf->rss_lut) {
1136 			PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1137 			goto err_rss;
1138 		}
1139 	}
1140 	return 0;
1141 err_rss:
1142 	rte_free(vf->rss_key);
1143 	rte_free(vf->rss_lut);
1144 err_alloc:
1145 	rte_free(vf->vf_res);
1146 	vf->vsi_res = NULL;
1147 err_api:
1148 	rte_free(vf->aq_resp);
1149 err_aq:
1150 	iavf_shutdown_adminq(hw);
1151 err:
1152 	return -1;
1153 }
1154 
1155 /* Enable default admin queue interrupt setting */
1156 static inline void
1157 iavf_enable_irq0(struct iavf_hw *hw)
1158 {
1159 	/* Enable admin queue interrupt trigger */
1160 	IAVF_WRITE_REG(hw, IAVFINT_ICR0_ENA1, IAVFINT_ICR0_ENA1_ADMINQ_MASK);
1161 
1162 	IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01, IAVFINT_DYN_CTL01_INTENA_MASK |
1163 		IAVFINT_DYN_CTL01_CLEARPBA_MASK | IAVFINT_DYN_CTL01_ITR_INDX_MASK);
1164 
1165 	IAVF_WRITE_FLUSH(hw);
1166 }
1167 
1168 static inline void
1169 iavf_disable_irq0(struct iavf_hw *hw)
1170 {
1171 	/* Disable all interrupt types */
1172 	IAVF_WRITE_REG(hw, IAVFINT_ICR0_ENA1, 0);
1173 	IAVF_WRITE_REG(hw, IAVFINT_DYN_CTL01,
1174 		      IAVFINT_DYN_CTL01_ITR_INDX_MASK);
1175 	IAVF_WRITE_FLUSH(hw);
1176 }
1177 
1178 static void
1179 iavf_dev_interrupt_handler(void *param)
1180 {
1181 	struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1182 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1183 
1184 	iavf_disable_irq0(hw);
1185 
1186 	iavf_handle_virtchnl_msg(dev);
1187 
1188 	iavf_enable_irq0(hw);
1189 }
1190 
1191 static int
1192 iavf_dev_init(struct rte_eth_dev *eth_dev)
1193 {
1194 	struct iavf_adapter *adapter =
1195 		IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1196 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1197 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1198 
1199 	PMD_INIT_FUNC_TRACE();
1200 
1201 	/* assign ops func pointer */
1202 	eth_dev->dev_ops = &iavf_eth_dev_ops;
1203 	eth_dev->rx_pkt_burst = &iavf_recv_pkts;
1204 	eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
1205 	eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
1206 
1207 	/* For secondary processes, we don't initialise any further as primary
1208 	 * has already done this work. Only check if we need a different RX
1209 	 * and TX function.
1210 	 */
1211 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1212 		iavf_set_rx_function(eth_dev);
1213 		iavf_set_tx_function(eth_dev);
1214 		return 0;
1215 	}
1216 	rte_eth_copy_pci_info(eth_dev, pci_dev);
1217 
1218 	hw->vendor_id = pci_dev->id.vendor_id;
1219 	hw->device_id = pci_dev->id.device_id;
1220 	hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1221 	hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1222 	hw->bus.bus_id = pci_dev->addr.bus;
1223 	hw->bus.device = pci_dev->addr.devid;
1224 	hw->bus.func = pci_dev->addr.function;
1225 	hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1226 	hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1227 	adapter->eth_dev = eth_dev;
1228 
1229 	if (iavf_init_vf(eth_dev) != 0) {
1230 		PMD_INIT_LOG(ERR, "Init vf failed");
1231 		return -1;
1232 	}
1233 
1234 	/* copy mac addr */
1235 	eth_dev->data->mac_addrs = rte_zmalloc(
1236 					"iavf_mac",
1237 					ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX,
1238 					0);
1239 	if (!eth_dev->data->mac_addrs) {
1240 		PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1241 			     " store MAC addresses",
1242 			     ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
1243 		return -ENOMEM;
1244 	}
1245 	/* If the MAC address is not configured by host,
1246 	 * generate a random one.
1247 	 */
1248 	if (!is_valid_assigned_ether_addr((struct ether_addr *)hw->mac.addr))
1249 		eth_random_addr(hw->mac.addr);
1250 	ether_addr_copy((struct ether_addr *)hw->mac.addr,
1251 			&eth_dev->data->mac_addrs[0]);
1252 
1253 	/* register callback func to eal lib */
1254 	rte_intr_callback_register(&pci_dev->intr_handle,
1255 				   iavf_dev_interrupt_handler,
1256 				   (void *)eth_dev);
1257 
1258 	/* enable uio intr after callback register */
1259 	rte_intr_enable(&pci_dev->intr_handle);
1260 
1261 	/* configure and enable device interrupt */
1262 	iavf_enable_irq0(hw);
1263 
1264 	return 0;
1265 }
1266 
1267 static void
1268 iavf_dev_close(struct rte_eth_dev *dev)
1269 {
1270 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1271 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1272 	struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1273 
1274 	iavf_dev_stop(dev);
1275 	iavf_shutdown_adminq(hw);
1276 	/* disable uio intr before callback unregister */
1277 	rte_intr_disable(intr_handle);
1278 
1279 	/* unregister callback func from eal lib */
1280 	rte_intr_callback_unregister(intr_handle,
1281 				     iavf_dev_interrupt_handler, dev);
1282 	iavf_disable_irq0(hw);
1283 }
1284 
1285 static int
1286 iavf_dev_uninit(struct rte_eth_dev *dev)
1287 {
1288 	struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1289 	struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1290 
1291 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1292 		return -EPERM;
1293 
1294 	dev->dev_ops = NULL;
1295 	dev->rx_pkt_burst = NULL;
1296 	dev->tx_pkt_burst = NULL;
1297 	if (hw->adapter_stopped == 0)
1298 		iavf_dev_close(dev);
1299 
1300 	rte_free(vf->vf_res);
1301 	vf->vsi_res = NULL;
1302 	vf->vf_res = NULL;
1303 
1304 	rte_free(vf->aq_resp);
1305 	vf->aq_resp = NULL;
1306 
1307 	if (vf->rss_lut) {
1308 		rte_free(vf->rss_lut);
1309 		vf->rss_lut = NULL;
1310 	}
1311 	if (vf->rss_key) {
1312 		rte_free(vf->rss_key);
1313 		vf->rss_key = NULL;
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1320 			     struct rte_pci_device *pci_dev)
1321 {
1322 	return rte_eth_dev_pci_generic_probe(pci_dev,
1323 		sizeof(struct iavf_adapter), iavf_dev_init);
1324 }
1325 
1326 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
1327 {
1328 	return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
1329 }
1330 
1331 /* Adaptive virtual function driver struct */
1332 static struct rte_pci_driver rte_iavf_pmd = {
1333 	.id_table = pci_id_iavf_map,
1334 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
1335 		     RTE_PCI_DRV_IOVA_AS_VA,
1336 	.probe = eth_iavf_pci_probe,
1337 	.remove = eth_iavf_pci_remove,
1338 };
1339 
1340 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
1341 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
1342 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
1343 RTE_INIT(iavf_init_log)
1344 {
1345 	iavf_logtype_init = rte_log_register("pmd.net.iavf.init");
1346 	if (iavf_logtype_init >= 0)
1347 		rte_log_set_level(iavf_logtype_init, RTE_LOG_NOTICE);
1348 	iavf_logtype_driver = rte_log_register("pmd.net.iavf.driver");
1349 	if (iavf_logtype_driver >= 0)
1350 		rte_log_set_level(iavf_logtype_driver, RTE_LOG_NOTICE);
1351 }
1352 
1353 /* memory func for base code */
1354 enum iavf_status_code
1355 iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw *hw,
1356 		       struct iavf_dma_mem *mem,
1357 		       u64 size,
1358 		       u32 alignment)
1359 {
1360 	const struct rte_memzone *mz = NULL;
1361 	char z_name[RTE_MEMZONE_NAMESIZE];
1362 
1363 	if (!mem)
1364 		return IAVF_ERR_PARAM;
1365 
1366 	snprintf(z_name, sizeof(z_name), "iavf_dma_%"PRIu64, rte_rand());
1367 	mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY,
1368 			RTE_MEMZONE_IOVA_CONTIG, alignment, RTE_PGSIZE_2M);
1369 	if (!mz)
1370 		return IAVF_ERR_NO_MEMORY;
1371 
1372 	mem->size = size;
1373 	mem->va = mz->addr;
1374 	mem->pa = mz->phys_addr;
1375 	mem->zone = (const void *)mz;
1376 	PMD_DRV_LOG(DEBUG,
1377 		    "memzone %s allocated with physical address: %"PRIu64,
1378 		    mz->name, mem->pa);
1379 
1380 	return IAVF_SUCCESS;
1381 }
1382 
1383 enum iavf_status_code
1384 iavf_free_dma_mem_d(__rte_unused struct iavf_hw *hw,
1385 		   struct iavf_dma_mem *mem)
1386 {
1387 	if (!mem)
1388 		return IAVF_ERR_PARAM;
1389 
1390 	PMD_DRV_LOG(DEBUG,
1391 		    "memzone %s to be freed with physical address: %"PRIu64,
1392 		    ((const struct rte_memzone *)mem->zone)->name, mem->pa);
1393 	rte_memzone_free((const struct rte_memzone *)mem->zone);
1394 	mem->zone = NULL;
1395 	mem->va = NULL;
1396 	mem->pa = (u64)0;
1397 
1398 	return IAVF_SUCCESS;
1399 }
1400 
1401 enum iavf_status_code
1402 iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw *hw,
1403 			struct iavf_virt_mem *mem,
1404 			u32 size)
1405 {
1406 	if (!mem)
1407 		return IAVF_ERR_PARAM;
1408 
1409 	mem->size = size;
1410 	mem->va = rte_zmalloc("iavf", size, 0);
1411 
1412 	if (mem->va)
1413 		return IAVF_SUCCESS;
1414 	else
1415 		return IAVF_ERR_NO_MEMORY;
1416 }
1417 
1418 enum iavf_status_code
1419 iavf_free_virt_mem_d(__rte_unused struct iavf_hw *hw,
1420 		    struct iavf_virt_mem *mem)
1421 {
1422 	if (!mem)
1423 		return IAVF_ERR_PARAM;
1424 
1425 	rte_free(mem->va);
1426 	mem->va = NULL;
1427 
1428 	return IAVF_SUCCESS;
1429 }
1430