net/ice: add supported packet types query to DCF mode
[dpdk.git] / drivers / net / ice / ice_dcf_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #include <errno.h>
6 #include <stdbool.h>
7 #include <sys/queue.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #include <rte_interrupts.h>
12 #include <rte_debug.h>
13 #include <rte_pci.h>
14 #include <rte_atomic.h>
15 #include <rte_eal.h>
16 #include <rte_ether.h>
17 #include <ethdev_pci.h>
18 #include <rte_kvargs.h>
19 #include <rte_malloc.h>
20 #include <rte_memzone.h>
21 #include <rte_dev.h>
22
23 #include <iavf_devids.h>
24
25 #include "ice_generic_flow.h"
26 #include "ice_dcf_ethdev.h"
27 #include "ice_rxtx.h"
28
29 static int
30 ice_dcf_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
31                                 struct rte_eth_udp_tunnel *udp_tunnel);
32 static int
33 ice_dcf_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
34                                 struct rte_eth_udp_tunnel *udp_tunnel);
35
36 static int
37 ice_dcf_dev_init(struct rte_eth_dev *eth_dev);
38
39 static int
40 ice_dcf_dev_uninit(struct rte_eth_dev *eth_dev);
41
42 static uint16_t
43 ice_dcf_recv_pkts(__rte_unused void *rx_queue,
44                   __rte_unused struct rte_mbuf **bufs,
45                   __rte_unused uint16_t nb_pkts)
46 {
47         return 0;
48 }
49
50 static uint16_t
51 ice_dcf_xmit_pkts(__rte_unused void *tx_queue,
52                   __rte_unused struct rte_mbuf **bufs,
53                   __rte_unused uint16_t nb_pkts)
54 {
55         return 0;
56 }
57
58 static int
59 ice_dcf_init_rxq(struct rte_eth_dev *dev, struct ice_rx_queue *rxq)
60 {
61         struct ice_dcf_adapter *dcf_ad = dev->data->dev_private;
62         struct rte_eth_dev_data *dev_data = dev->data;
63         struct iavf_hw *hw = &dcf_ad->real_hw.avf;
64         uint16_t buf_size, max_pkt_len;
65
66         buf_size = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
67         rxq->rx_hdr_len = 0;
68         rxq->rx_buf_len = RTE_ALIGN(buf_size, (1 << ICE_RLAN_CTX_DBUF_S));
69         max_pkt_len = RTE_MIN(ICE_SUPPORT_CHAIN_NUM * rxq->rx_buf_len,
70                               dev->data->mtu + ICE_ETH_OVERHEAD);
71
72         /* Check maximum packet length is set correctly.  */
73         if (max_pkt_len <= RTE_ETHER_MIN_LEN ||
74             max_pkt_len > ICE_FRAME_SIZE_MAX) {
75                 PMD_DRV_LOG(ERR, "maximum packet length must be "
76                             "larger than %u and smaller than %u",
77                             (uint32_t)RTE_ETHER_MIN_LEN,
78                             (uint32_t)ICE_FRAME_SIZE_MAX);
79                 return -EINVAL;
80         }
81
82         rxq->max_pkt_len = max_pkt_len;
83         if ((dev_data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_SCATTER) ||
84             (rxq->max_pkt_len + 2 * RTE_VLAN_HLEN) > buf_size) {
85                 dev_data->scattered_rx = 1;
86         }
87         rxq->qrx_tail = hw->hw_addr + IAVF_QRX_TAIL1(rxq->queue_id);
88         IAVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
89         IAVF_WRITE_FLUSH(hw);
90
91         return 0;
92 }
93
94 static int
95 ice_dcf_init_rx_queues(struct rte_eth_dev *dev)
96 {
97         struct ice_rx_queue **rxq =
98                 (struct ice_rx_queue **)dev->data->rx_queues;
99         int i, ret;
100
101         for (i = 0; i < dev->data->nb_rx_queues; i++) {
102                 if (!rxq[i] || !rxq[i]->q_set)
103                         continue;
104                 ret = ice_dcf_init_rxq(dev, rxq[i]);
105                 if (ret)
106                         return ret;
107         }
108
109         ice_set_rx_function(dev);
110         ice_set_tx_function(dev);
111
112         return 0;
113 }
114
115 #define IAVF_MISC_VEC_ID                RTE_INTR_VEC_ZERO_OFFSET
116 #define IAVF_RX_VEC_START               RTE_INTR_VEC_RXTX_OFFSET
117
118 #define IAVF_ITR_INDEX_DEFAULT          0
119 #define IAVF_QUEUE_ITR_INTERVAL_DEFAULT 32 /* 32 us */
120 #define IAVF_QUEUE_ITR_INTERVAL_MAX     8160 /* 8160 us */
121
122 static inline uint16_t
123 iavf_calc_itr_interval(int16_t interval)
124 {
125         if (interval < 0 || interval > IAVF_QUEUE_ITR_INTERVAL_MAX)
126                 interval = IAVF_QUEUE_ITR_INTERVAL_DEFAULT;
127
128         /* Convert to hardware count, as writing each 1 represents 2 us */
129         return interval / 2;
130 }
131
132 static int
133 ice_dcf_config_rx_queues_irqs(struct rte_eth_dev *dev,
134                                      struct rte_intr_handle *intr_handle)
135 {
136         struct ice_dcf_adapter *adapter = dev->data->dev_private;
137         struct ice_dcf_hw *hw = &adapter->real_hw;
138         uint16_t interval, i;
139         int vec;
140
141         if (rte_intr_cap_multiple(intr_handle) &&
142             dev->data->dev_conf.intr_conf.rxq) {
143                 if (rte_intr_efd_enable(intr_handle, dev->data->nb_rx_queues))
144                         return -1;
145         }
146
147         if (rte_intr_dp_is_en(intr_handle)) {
148                 if (rte_intr_vec_list_alloc(intr_handle, "intr_vec",
149                                                    dev->data->nb_rx_queues)) {
150                         PMD_DRV_LOG(ERR, "Failed to allocate %d rx intr_vec",
151                                     dev->data->nb_rx_queues);
152                         return -1;
153                 }
154         }
155
156         if (!dev->data->dev_conf.intr_conf.rxq ||
157             !rte_intr_dp_is_en(intr_handle)) {
158                 /* Rx interrupt disabled, Map interrupt only for writeback */
159                 hw->nb_msix = 1;
160                 if (hw->vf_res->vf_cap_flags &
161                     VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
162                         /* If WB_ON_ITR supports, enable it */
163                         hw->msix_base = IAVF_RX_VEC_START;
164                         /* Set the ITR for index zero, to 2us to make sure that
165                          * we leave time for aggregation to occur, but don't
166                          * increase latency dramatically.
167                          */
168                         IAVF_WRITE_REG(&hw->avf,
169                                        IAVF_VFINT_DYN_CTLN1(hw->msix_base - 1),
170                                        (0 << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
171                                        IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
172                                        (2UL << IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT));
173                 } else {
174                         /* If no WB_ON_ITR offload flags, need to set
175                          * interrupt for descriptor write back.
176                          */
177                         hw->msix_base = IAVF_MISC_VEC_ID;
178
179                         /* set ITR to max */
180                         interval =
181                         iavf_calc_itr_interval(IAVF_QUEUE_ITR_INTERVAL_MAX);
182                         IAVF_WRITE_REG(&hw->avf, IAVF_VFINT_DYN_CTL01,
183                                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
184                                        (IAVF_ITR_INDEX_DEFAULT <<
185                                         IAVF_VFINT_DYN_CTL01_ITR_INDX_SHIFT) |
186                                        (interval <<
187                                         IAVF_VFINT_DYN_CTL01_INTERVAL_SHIFT));
188                 }
189                 IAVF_WRITE_FLUSH(&hw->avf);
190                 /* map all queues to the same interrupt */
191                 for (i = 0; i < dev->data->nb_rx_queues; i++)
192                         hw->rxq_map[hw->msix_base] |= 1 << i;
193         } else {
194                 if (!rte_intr_allow_others(intr_handle)) {
195                         hw->nb_msix = 1;
196                         hw->msix_base = IAVF_MISC_VEC_ID;
197                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
198                                 hw->rxq_map[hw->msix_base] |= 1 << i;
199                                 rte_intr_vec_list_index_set(intr_handle,
200                                                         i, IAVF_MISC_VEC_ID);
201                         }
202                         PMD_DRV_LOG(DEBUG,
203                                     "vector %u are mapping to all Rx queues",
204                                     hw->msix_base);
205                 } else {
206                         /* If Rx interrupt is required, and we can use
207                          * multi interrupts, then the vec is from 1
208                          */
209                         hw->nb_msix = RTE_MIN(hw->vf_res->max_vectors,
210                                       rte_intr_nb_efd_get(intr_handle));
211                         hw->msix_base = IAVF_MISC_VEC_ID;
212                         vec = IAVF_MISC_VEC_ID;
213                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
214                                 hw->rxq_map[vec] |= 1 << i;
215                                 rte_intr_vec_list_index_set(intr_handle,
216                                                                    i, vec++);
217                                 if (vec >= hw->nb_msix)
218                                         vec = IAVF_RX_VEC_START;
219                         }
220                         PMD_DRV_LOG(DEBUG,
221                                     "%u vectors are mapping to %u Rx queues",
222                                     hw->nb_msix, dev->data->nb_rx_queues);
223                 }
224         }
225
226         if (ice_dcf_config_irq_map(hw)) {
227                 PMD_DRV_LOG(ERR, "config interrupt mapping failed");
228                 return -1;
229         }
230         return 0;
231 }
232
233 static int
234 alloc_rxq_mbufs(struct ice_rx_queue *rxq)
235 {
236         volatile union ice_rx_flex_desc *rxd;
237         struct rte_mbuf *mbuf = NULL;
238         uint64_t dma_addr;
239         uint16_t i;
240
241         for (i = 0; i < rxq->nb_rx_desc; i++) {
242                 mbuf = rte_mbuf_raw_alloc(rxq->mp);
243                 if (unlikely(!mbuf)) {
244                         PMD_DRV_LOG(ERR, "Failed to allocate mbuf for RX");
245                         return -ENOMEM;
246                 }
247
248                 rte_mbuf_refcnt_set(mbuf, 1);
249                 mbuf->next = NULL;
250                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
251                 mbuf->nb_segs = 1;
252                 mbuf->port = rxq->port_id;
253
254                 dma_addr =
255                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
256
257                 rxd = &rxq->rx_ring[i];
258                 rxd->read.pkt_addr = dma_addr;
259                 rxd->read.hdr_addr = 0;
260 #ifndef RTE_LIBRTE_ICE_16BYTE_RX_DESC
261                 rxd->read.rsvd1 = 0;
262                 rxd->read.rsvd2 = 0;
263 #endif
264
265                 rxq->sw_ring[i].mbuf = (void *)mbuf;
266         }
267
268         return 0;
269 }
270
271 static int
272 ice_dcf_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
273 {
274         struct ice_dcf_adapter *ad = dev->data->dev_private;
275         struct iavf_hw *hw = &ad->real_hw.avf;
276         struct ice_rx_queue *rxq;
277         int err = 0;
278
279         if (rx_queue_id >= dev->data->nb_rx_queues)
280                 return -EINVAL;
281
282         rxq = dev->data->rx_queues[rx_queue_id];
283
284         err = alloc_rxq_mbufs(rxq);
285         if (err) {
286                 PMD_DRV_LOG(ERR, "Failed to allocate RX queue mbuf");
287                 return err;
288         }
289
290         rte_wmb();
291
292         /* Init the RX tail register. */
293         IAVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
294         IAVF_WRITE_FLUSH(hw);
295
296         /* Ready to switch the queue on */
297         err = ice_dcf_switch_queue(&ad->real_hw, rx_queue_id, true, true);
298         if (err) {
299                 PMD_DRV_LOG(ERR, "Failed to switch RX queue %u on",
300                             rx_queue_id);
301                 return err;
302         }
303
304         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
305
306         return 0;
307 }
308
309 static inline void
310 reset_rx_queue(struct ice_rx_queue *rxq)
311 {
312         uint16_t len;
313         uint32_t i;
314
315         if (!rxq)
316                 return;
317
318         len = rxq->nb_rx_desc + ICE_RX_MAX_BURST;
319
320         for (i = 0; i < len * sizeof(union ice_rx_flex_desc); i++)
321                 ((volatile char *)rxq->rx_ring)[i] = 0;
322
323         memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
324
325         for (i = 0; i < ICE_RX_MAX_BURST; i++)
326                 rxq->sw_ring[rxq->nb_rx_desc + i].mbuf = &rxq->fake_mbuf;
327
328         /* for rx bulk */
329         rxq->rx_nb_avail = 0;
330         rxq->rx_next_avail = 0;
331         rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
332
333         rxq->rx_tail = 0;
334         rxq->nb_rx_hold = 0;
335         rxq->pkt_first_seg = NULL;
336         rxq->pkt_last_seg = NULL;
337 }
338
339 static inline void
340 reset_tx_queue(struct ice_tx_queue *txq)
341 {
342         struct ice_tx_entry *txe;
343         uint32_t i, size;
344         uint16_t prev;
345
346         if (!txq) {
347                 PMD_DRV_LOG(DEBUG, "Pointer to txq is NULL");
348                 return;
349         }
350
351         txe = txq->sw_ring;
352         size = sizeof(struct ice_tx_desc) * txq->nb_tx_desc;
353         for (i = 0; i < size; i++)
354                 ((volatile char *)txq->tx_ring)[i] = 0;
355
356         prev = (uint16_t)(txq->nb_tx_desc - 1);
357         for (i = 0; i < txq->nb_tx_desc; i++) {
358                 txq->tx_ring[i].cmd_type_offset_bsz =
359                         rte_cpu_to_le_64(IAVF_TX_DESC_DTYPE_DESC_DONE);
360                 txe[i].mbuf =  NULL;
361                 txe[i].last_id = i;
362                 txe[prev].next_id = i;
363                 prev = i;
364         }
365
366         txq->tx_tail = 0;
367         txq->nb_tx_used = 0;
368
369         txq->last_desc_cleaned = txq->nb_tx_desc - 1;
370         txq->nb_tx_free = txq->nb_tx_desc - 1;
371
372         txq->tx_next_dd = txq->tx_rs_thresh - 1;
373         txq->tx_next_rs = txq->tx_rs_thresh - 1;
374 }
375
376 static int
377 ice_dcf_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
378 {
379         struct ice_dcf_adapter *ad = dev->data->dev_private;
380         struct ice_dcf_hw *hw = &ad->real_hw;
381         struct ice_rx_queue *rxq;
382         int err;
383
384         if (rx_queue_id >= dev->data->nb_rx_queues)
385                 return -EINVAL;
386
387         err = ice_dcf_switch_queue(hw, rx_queue_id, true, false);
388         if (err) {
389                 PMD_DRV_LOG(ERR, "Failed to switch RX queue %u off",
390                             rx_queue_id);
391                 return err;
392         }
393
394         rxq = dev->data->rx_queues[rx_queue_id];
395         rxq->rx_rel_mbufs(rxq);
396         reset_rx_queue(rxq);
397         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
398
399         return 0;
400 }
401
402 static int
403 ice_dcf_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
404 {
405         struct ice_dcf_adapter *ad = dev->data->dev_private;
406         struct iavf_hw *hw = &ad->real_hw.avf;
407         struct ice_tx_queue *txq;
408         int err = 0;
409
410         if (tx_queue_id >= dev->data->nb_tx_queues)
411                 return -EINVAL;
412
413         txq = dev->data->tx_queues[tx_queue_id];
414
415         /* Init the RX tail register. */
416         txq->qtx_tail = hw->hw_addr + IAVF_QTX_TAIL1(tx_queue_id);
417         IAVF_PCI_REG_WRITE(txq->qtx_tail, 0);
418         IAVF_WRITE_FLUSH(hw);
419
420         /* Ready to switch the queue on */
421         err = ice_dcf_switch_queue(&ad->real_hw, tx_queue_id, false, true);
422
423         if (err) {
424                 PMD_DRV_LOG(ERR, "Failed to switch TX queue %u on",
425                             tx_queue_id);
426                 return err;
427         }
428
429         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
430
431         return 0;
432 }
433
434 static int
435 ice_dcf_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
436 {
437         struct ice_dcf_adapter *ad = dev->data->dev_private;
438         struct ice_dcf_hw *hw = &ad->real_hw;
439         struct ice_tx_queue *txq;
440         int err;
441
442         if (tx_queue_id >= dev->data->nb_tx_queues)
443                 return -EINVAL;
444
445         err = ice_dcf_switch_queue(hw, tx_queue_id, false, false);
446         if (err) {
447                 PMD_DRV_LOG(ERR, "Failed to switch TX queue %u off",
448                             tx_queue_id);
449                 return err;
450         }
451
452         txq = dev->data->tx_queues[tx_queue_id];
453         txq->tx_rel_mbufs(txq);
454         reset_tx_queue(txq);
455         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
456
457         return 0;
458 }
459
460 static int
461 ice_dcf_start_queues(struct rte_eth_dev *dev)
462 {
463         struct ice_rx_queue *rxq;
464         struct ice_tx_queue *txq;
465         int nb_rxq = 0;
466         int nb_txq, i;
467
468         for (nb_txq = 0; nb_txq < dev->data->nb_tx_queues; nb_txq++) {
469                 txq = dev->data->tx_queues[nb_txq];
470                 if (txq->tx_deferred_start)
471                         continue;
472                 if (ice_dcf_tx_queue_start(dev, nb_txq) != 0) {
473                         PMD_DRV_LOG(ERR, "Fail to start queue %u", nb_txq);
474                         goto tx_err;
475                 }
476         }
477
478         for (nb_rxq = 0; nb_rxq < dev->data->nb_rx_queues; nb_rxq++) {
479                 rxq = dev->data->rx_queues[nb_rxq];
480                 if (rxq->rx_deferred_start)
481                         continue;
482                 if (ice_dcf_rx_queue_start(dev, nb_rxq) != 0) {
483                         PMD_DRV_LOG(ERR, "Fail to start queue %u", nb_rxq);
484                         goto rx_err;
485                 }
486         }
487
488         return 0;
489
490         /* stop the started queues if failed to start all queues */
491 rx_err:
492         for (i = 0; i < nb_rxq; i++)
493                 ice_dcf_rx_queue_stop(dev, i);
494 tx_err:
495         for (i = 0; i < nb_txq; i++)
496                 ice_dcf_tx_queue_stop(dev, i);
497
498         return -1;
499 }
500
501 static int
502 ice_dcf_dev_start(struct rte_eth_dev *dev)
503 {
504         struct ice_dcf_adapter *dcf_ad = dev->data->dev_private;
505         struct rte_intr_handle *intr_handle = dev->intr_handle;
506         struct ice_adapter *ad = &dcf_ad->parent;
507         struct ice_dcf_hw *hw = &dcf_ad->real_hw;
508         int ret;
509
510         if (hw->resetting) {
511                 PMD_DRV_LOG(ERR,
512                             "The DCF has been reset by PF, please reinit first");
513                 return -EIO;
514         }
515
516         if (hw->tm_conf.root && !hw->tm_conf.committed) {
517                 PMD_DRV_LOG(ERR,
518                         "please call hierarchy_commit() before starting the port");
519                 return -EIO;
520         }
521
522         ad->pf.adapter_stopped = 0;
523
524         hw->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
525                                       dev->data->nb_tx_queues);
526
527         ret = ice_dcf_init_rx_queues(dev);
528         if (ret) {
529                 PMD_DRV_LOG(ERR, "Fail to init queues");
530                 return ret;
531         }
532
533         if (hw->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
534                 ret = ice_dcf_init_rss(hw);
535                 if (ret) {
536                         PMD_DRV_LOG(ERR, "Failed to configure RSS");
537                         return ret;
538                 }
539         }
540
541         ret = ice_dcf_configure_queues(hw);
542         if (ret) {
543                 PMD_DRV_LOG(ERR, "Fail to config queues");
544                 return ret;
545         }
546
547         ret = ice_dcf_config_rx_queues_irqs(dev, intr_handle);
548         if (ret) {
549                 PMD_DRV_LOG(ERR, "Fail to config rx queues' irqs");
550                 return ret;
551         }
552
553         if (dev->data->dev_conf.intr_conf.rxq != 0) {
554                 rte_intr_disable(intr_handle);
555                 rte_intr_enable(intr_handle);
556         }
557
558         ret = ice_dcf_start_queues(dev);
559         if (ret) {
560                 PMD_DRV_LOG(ERR, "Failed to enable queues");
561                 return ret;
562         }
563
564         ret = ice_dcf_add_del_all_mac_addr(hw, true);
565         if (ret) {
566                 PMD_DRV_LOG(ERR, "Failed to add mac addr");
567                 return ret;
568         }
569
570         dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
571
572         return 0;
573 }
574
575 static void
576 ice_dcf_stop_queues(struct rte_eth_dev *dev)
577 {
578         struct ice_dcf_adapter *ad = dev->data->dev_private;
579         struct ice_dcf_hw *hw = &ad->real_hw;
580         struct ice_rx_queue *rxq;
581         struct ice_tx_queue *txq;
582         int ret, i;
583
584         /* Stop All queues */
585         ret = ice_dcf_disable_queues(hw);
586         if (ret)
587                 PMD_DRV_LOG(WARNING, "Fail to stop queues");
588
589         for (i = 0; i < dev->data->nb_tx_queues; i++) {
590                 txq = dev->data->tx_queues[i];
591                 if (!txq)
592                         continue;
593                 txq->tx_rel_mbufs(txq);
594                 reset_tx_queue(txq);
595                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
596         }
597         for (i = 0; i < dev->data->nb_rx_queues; i++) {
598                 rxq = dev->data->rx_queues[i];
599                 if (!rxq)
600                         continue;
601                 rxq->rx_rel_mbufs(rxq);
602                 reset_rx_queue(rxq);
603                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
604         }
605 }
606
607 static int
608 ice_dcf_dev_stop(struct rte_eth_dev *dev)
609 {
610         struct ice_dcf_adapter *dcf_ad = dev->data->dev_private;
611         struct rte_intr_handle *intr_handle = dev->intr_handle;
612         struct ice_adapter *ad = &dcf_ad->parent;
613         struct ice_dcf_hw *hw = &dcf_ad->real_hw;
614
615         if (ad->pf.adapter_stopped == 1) {
616                 PMD_DRV_LOG(DEBUG, "Port is already stopped");
617                 return 0;
618         }
619
620         /* Stop the VF representors for this device */
621         ice_dcf_vf_repr_stop_all(dcf_ad);
622
623         ice_dcf_stop_queues(dev);
624
625         rte_intr_efd_disable(intr_handle);
626         rte_intr_vec_list_free(intr_handle);
627
628         ice_dcf_add_del_all_mac_addr(&dcf_ad->real_hw, false);
629         dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
630         ad->pf.adapter_stopped = 1;
631         hw->tm_conf.committed = false;
632
633         return 0;
634 }
635
636 static int
637 ice_dcf_dev_configure(struct rte_eth_dev *dev)
638 {
639         struct ice_dcf_adapter *dcf_ad = dev->data->dev_private;
640         struct ice_adapter *ad = &dcf_ad->parent;
641
642         ad->rx_bulk_alloc_allowed = true;
643         ad->tx_simple_allowed = true;
644
645         if (dev->data->dev_conf.rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)
646                 dev->data->dev_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
647
648         return 0;
649 }
650
651 static int
652 ice_dcf_dev_info_get(struct rte_eth_dev *dev,
653                      struct rte_eth_dev_info *dev_info)
654 {
655         struct ice_dcf_adapter *adapter = dev->data->dev_private;
656         struct ice_dcf_hw *hw = &adapter->real_hw;
657
658         dev_info->max_mac_addrs = 1;
659         dev_info->max_rx_queues = hw->vsi_res->num_queue_pairs;
660         dev_info->max_tx_queues = hw->vsi_res->num_queue_pairs;
661         dev_info->min_rx_bufsize = ICE_BUF_SIZE_MIN;
662         dev_info->max_rx_pktlen = ICE_FRAME_SIZE_MAX;
663         dev_info->hash_key_size = hw->vf_res->rss_key_size;
664         dev_info->reta_size = hw->vf_res->rss_lut_size;
665         dev_info->flow_type_rss_offloads = ICE_RSS_OFFLOAD_ALL;
666         dev_info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
667
668         dev_info->rx_offload_capa =
669                 RTE_ETH_RX_OFFLOAD_VLAN_STRIP |
670                 RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
671                 RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
672                 RTE_ETH_RX_OFFLOAD_TCP_CKSUM |
673                 RTE_ETH_RX_OFFLOAD_OUTER_IPV4_CKSUM |
674                 RTE_ETH_RX_OFFLOAD_SCATTER |
675                 RTE_ETH_RX_OFFLOAD_VLAN_FILTER |
676                 RTE_ETH_RX_OFFLOAD_RSS_HASH;
677         dev_info->tx_offload_capa =
678                 RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
679                 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
680                 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
681                 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
682                 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM |
683                 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM |
684                 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM |
685                 RTE_ETH_TX_OFFLOAD_TCP_TSO |
686                 RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO |
687                 RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO |
688                 RTE_ETH_TX_OFFLOAD_IPIP_TNL_TSO |
689                 RTE_ETH_TX_OFFLOAD_GENEVE_TNL_TSO |
690                 RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
691
692         dev_info->default_rxconf = (struct rte_eth_rxconf) {
693                 .rx_thresh = {
694                         .pthresh = ICE_DEFAULT_RX_PTHRESH,
695                         .hthresh = ICE_DEFAULT_RX_HTHRESH,
696                         .wthresh = ICE_DEFAULT_RX_WTHRESH,
697                 },
698                 .rx_free_thresh = ICE_DEFAULT_RX_FREE_THRESH,
699                 .rx_drop_en = 0,
700                 .offloads = 0,
701         };
702
703         dev_info->default_txconf = (struct rte_eth_txconf) {
704                 .tx_thresh = {
705                         .pthresh = ICE_DEFAULT_TX_PTHRESH,
706                         .hthresh = ICE_DEFAULT_TX_HTHRESH,
707                         .wthresh = ICE_DEFAULT_TX_WTHRESH,
708                 },
709                 .tx_free_thresh = ICE_DEFAULT_TX_FREE_THRESH,
710                 .tx_rs_thresh = ICE_DEFAULT_TX_RSBIT_THRESH,
711                 .offloads = 0,
712         };
713
714         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
715                 .nb_max = ICE_MAX_RING_DESC,
716                 .nb_min = ICE_MIN_RING_DESC,
717                 .nb_align = ICE_ALIGN_RING_DESC,
718         };
719
720         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
721                 .nb_max = ICE_MAX_RING_DESC,
722                 .nb_min = ICE_MIN_RING_DESC,
723                 .nb_align = ICE_ALIGN_RING_DESC,
724         };
725
726         return 0;
727 }
728
729 static int
730 ice_dcf_dev_promiscuous_enable(__rte_unused struct rte_eth_dev *dev)
731 {
732         return 0;
733 }
734
735 static int
736 ice_dcf_dev_promiscuous_disable(__rte_unused struct rte_eth_dev *dev)
737 {
738         return 0;
739 }
740
741 static int
742 ice_dcf_dev_allmulticast_enable(__rte_unused struct rte_eth_dev *dev)
743 {
744         return 0;
745 }
746
747 static int
748 ice_dcf_dev_allmulticast_disable(__rte_unused struct rte_eth_dev *dev)
749 {
750         return 0;
751 }
752
753 static int
754 ice_dcf_dev_flow_ops_get(struct rte_eth_dev *dev,
755                          const struct rte_flow_ops **ops)
756 {
757         if (!dev)
758                 return -EINVAL;
759
760         *ops = &ice_flow_ops;
761         return 0;
762 }
763
764 static int
765 ice_dcf_dev_rss_reta_update(struct rte_eth_dev *dev,
766                         struct rte_eth_rss_reta_entry64 *reta_conf,
767                         uint16_t reta_size)
768 {
769         struct ice_dcf_adapter *adapter = dev->data->dev_private;
770         struct ice_dcf_hw *hw = &adapter->real_hw;
771         uint8_t *lut;
772         uint16_t i, idx, shift;
773         int ret;
774
775         if (!(hw->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
776                 return -ENOTSUP;
777
778         if (reta_size != hw->vf_res->rss_lut_size) {
779                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
780                         "(%d) doesn't match the number of hardware can "
781                         "support (%d)", reta_size, hw->vf_res->rss_lut_size);
782                 return -EINVAL;
783         }
784
785         lut = rte_zmalloc("rss_lut", reta_size, 0);
786         if (!lut) {
787                 PMD_DRV_LOG(ERR, "No memory can be allocated");
788                 return -ENOMEM;
789         }
790         /* store the old lut table temporarily */
791         rte_memcpy(lut, hw->rss_lut, reta_size);
792
793         for (i = 0; i < reta_size; i++) {
794                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
795                 shift = i % RTE_ETH_RETA_GROUP_SIZE;
796                 if (reta_conf[idx].mask & (1ULL << shift))
797                         lut[i] = reta_conf[idx].reta[shift];
798         }
799
800         rte_memcpy(hw->rss_lut, lut, reta_size);
801         /* send virtchnnl ops to configure rss*/
802         ret = ice_dcf_configure_rss_lut(hw);
803         if (ret) /* revert back */
804                 rte_memcpy(hw->rss_lut, lut, reta_size);
805         rte_free(lut);
806
807         return ret;
808 }
809
810 static int
811 ice_dcf_dev_rss_reta_query(struct rte_eth_dev *dev,
812                        struct rte_eth_rss_reta_entry64 *reta_conf,
813                        uint16_t reta_size)
814 {
815         struct ice_dcf_adapter *adapter = dev->data->dev_private;
816         struct ice_dcf_hw *hw = &adapter->real_hw;
817         uint16_t i, idx, shift;
818
819         if (!(hw->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
820                 return -ENOTSUP;
821
822         if (reta_size != hw->vf_res->rss_lut_size) {
823                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
824                         "(%d) doesn't match the number of hardware can "
825                         "support (%d)", reta_size, hw->vf_res->rss_lut_size);
826                 return -EINVAL;
827         }
828
829         for (i = 0; i < reta_size; i++) {
830                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
831                 shift = i % RTE_ETH_RETA_GROUP_SIZE;
832                 if (reta_conf[idx].mask & (1ULL << shift))
833                         reta_conf[idx].reta[shift] = hw->rss_lut[i];
834         }
835
836         return 0;
837 }
838
839 static int
840 ice_dcf_dev_rss_hash_update(struct rte_eth_dev *dev,
841                         struct rte_eth_rss_conf *rss_conf)
842 {
843         struct ice_dcf_adapter *adapter = dev->data->dev_private;
844         struct ice_dcf_hw *hw = &adapter->real_hw;
845
846         if (!(hw->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
847                 return -ENOTSUP;
848
849         /* HENA setting, it is enabled by default, no change */
850         if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
851                 PMD_DRV_LOG(DEBUG, "No key to be configured");
852                 return 0;
853         } else if (rss_conf->rss_key_len != hw->vf_res->rss_key_size) {
854                 PMD_DRV_LOG(ERR, "The size of hash key configured "
855                         "(%d) doesn't match the size of hardware can "
856                         "support (%d)", rss_conf->rss_key_len,
857                         hw->vf_res->rss_key_size);
858                 return -EINVAL;
859         }
860
861         rte_memcpy(hw->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
862
863         return ice_dcf_configure_rss_key(hw);
864 }
865
866 static int
867 ice_dcf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
868                           struct rte_eth_rss_conf *rss_conf)
869 {
870         struct ice_dcf_adapter *adapter = dev->data->dev_private;
871         struct ice_dcf_hw *hw = &adapter->real_hw;
872
873         if (!(hw->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
874                 return -ENOTSUP;
875
876         /* Just set it to default value now. */
877         rss_conf->rss_hf = ICE_RSS_OFFLOAD_ALL;
878
879         if (!rss_conf->rss_key)
880                 return 0;
881
882         rss_conf->rss_key_len = hw->vf_res->rss_key_size;
883         rte_memcpy(rss_conf->rss_key, hw->rss_key, rss_conf->rss_key_len);
884
885         return 0;
886 }
887
888 #define ICE_DCF_32_BIT_WIDTH (CHAR_BIT * 4)
889 #define ICE_DCF_48_BIT_WIDTH (CHAR_BIT * 6)
890 #define ICE_DCF_48_BIT_MASK  RTE_LEN2MASK(ICE_DCF_48_BIT_WIDTH, uint64_t)
891
892 static void
893 ice_dcf_stat_update_48(uint64_t *offset, uint64_t *stat)
894 {
895         if (*stat >= *offset)
896                 *stat = *stat - *offset;
897         else
898                 *stat = (uint64_t)((*stat +
899                         ((uint64_t)1 << ICE_DCF_48_BIT_WIDTH)) - *offset);
900
901         *stat &= ICE_DCF_48_BIT_MASK;
902 }
903
904 static void
905 ice_dcf_stat_update_32(uint64_t *offset, uint64_t *stat)
906 {
907         if (*stat >= *offset)
908                 *stat = (uint64_t)(*stat - *offset);
909         else
910                 *stat = (uint64_t)((*stat +
911                         ((uint64_t)1 << ICE_DCF_32_BIT_WIDTH)) - *offset);
912 }
913
914 static void
915 ice_dcf_update_stats(struct virtchnl_eth_stats *oes,
916                      struct virtchnl_eth_stats *nes)
917 {
918         ice_dcf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
919         ice_dcf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
920         ice_dcf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
921         ice_dcf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
922         ice_dcf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
923         ice_dcf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
924         ice_dcf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
925         ice_dcf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
926         ice_dcf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
927         ice_dcf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
928         ice_dcf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
929 }
930
931
932 static int
933 ice_dcf_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
934 {
935         struct ice_dcf_adapter *ad = dev->data->dev_private;
936         struct ice_dcf_hw *hw = &ad->real_hw;
937         struct virtchnl_eth_stats pstats;
938         int ret;
939
940         if (hw->resetting) {
941                 PMD_DRV_LOG(ERR,
942                             "The DCF has been reset by PF, please reinit first");
943                 return -EIO;
944         }
945
946         ret = ice_dcf_query_stats(hw, &pstats);
947         if (ret == 0) {
948                 ice_dcf_update_stats(&hw->eth_stats_offset, &pstats);
949                 stats->ipackets = pstats.rx_unicast + pstats.rx_multicast +
950                                 pstats.rx_broadcast - pstats.rx_discards;
951                 stats->opackets = pstats.tx_broadcast + pstats.tx_multicast +
952                                                 pstats.tx_unicast;
953                 stats->imissed = pstats.rx_discards;
954                 stats->oerrors = pstats.tx_errors + pstats.tx_discards;
955                 stats->ibytes = pstats.rx_bytes;
956                 stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
957                 stats->obytes = pstats.tx_bytes;
958         } else {
959                 PMD_DRV_LOG(ERR, "Get statistics failed");
960         }
961         return ret;
962 }
963
964 static int
965 ice_dcf_stats_reset(struct rte_eth_dev *dev)
966 {
967         struct ice_dcf_adapter *ad = dev->data->dev_private;
968         struct ice_dcf_hw *hw = &ad->real_hw;
969         struct virtchnl_eth_stats pstats;
970         int ret;
971
972         if (hw->resetting)
973                 return 0;
974
975         /* read stat values to clear hardware registers */
976         ret = ice_dcf_query_stats(hw, &pstats);
977         if (ret != 0)
978                 return ret;
979
980         /* set stats offset base on current values */
981         hw->eth_stats_offset = pstats;
982
983         return 0;
984 }
985
986 static void
987 ice_dcf_free_repr_info(struct ice_dcf_adapter *dcf_adapter)
988 {
989         if (dcf_adapter->repr_infos) {
990                 rte_free(dcf_adapter->repr_infos);
991                 dcf_adapter->repr_infos = NULL;
992         }
993 }
994
995 static int
996 ice_dcf_init_repr_info(struct ice_dcf_adapter *dcf_adapter)
997 {
998         dcf_adapter->repr_infos =
999                         rte_calloc("ice_dcf_rep_info",
1000                                    dcf_adapter->real_hw.num_vfs,
1001                                    sizeof(dcf_adapter->repr_infos[0]), 0);
1002         if (!dcf_adapter->repr_infos) {
1003                 PMD_DRV_LOG(ERR, "Failed to alloc memory for VF representors\n");
1004                 return -ENOMEM;
1005         }
1006
1007         return 0;
1008 }
1009
1010 static int
1011 ice_dcf_dev_close(struct rte_eth_dev *dev)
1012 {
1013         struct ice_dcf_adapter *adapter = dev->data->dev_private;
1014
1015         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1016                 return 0;
1017
1018         (void)ice_dcf_dev_stop(dev);
1019
1020         ice_free_queues(dev);
1021
1022         ice_dcf_free_repr_info(adapter);
1023         ice_dcf_uninit_parent_adapter(dev);
1024         ice_dcf_uninit_hw(dev, &adapter->real_hw);
1025
1026         return 0;
1027 }
1028
1029 int
1030 ice_dcf_link_update(struct rte_eth_dev *dev,
1031                     __rte_unused int wait_to_complete)
1032 {
1033         struct ice_dcf_adapter *ad = dev->data->dev_private;
1034         struct ice_dcf_hw *hw = &ad->real_hw;
1035         struct rte_eth_link new_link;
1036
1037         memset(&new_link, 0, sizeof(new_link));
1038
1039         /* Only read status info stored in VF, and the info is updated
1040          * when receive LINK_CHANGE event from PF by virtchnl.
1041          */
1042         switch (hw->link_speed) {
1043         case 10:
1044                 new_link.link_speed = RTE_ETH_SPEED_NUM_10M;
1045                 break;
1046         case 100:
1047                 new_link.link_speed = RTE_ETH_SPEED_NUM_100M;
1048                 break;
1049         case 1000:
1050                 new_link.link_speed = RTE_ETH_SPEED_NUM_1G;
1051                 break;
1052         case 10000:
1053                 new_link.link_speed = RTE_ETH_SPEED_NUM_10G;
1054                 break;
1055         case 20000:
1056                 new_link.link_speed = RTE_ETH_SPEED_NUM_20G;
1057                 break;
1058         case 25000:
1059                 new_link.link_speed = RTE_ETH_SPEED_NUM_25G;
1060                 break;
1061         case 40000:
1062                 new_link.link_speed = RTE_ETH_SPEED_NUM_40G;
1063                 break;
1064         case 50000:
1065                 new_link.link_speed = RTE_ETH_SPEED_NUM_50G;
1066                 break;
1067         case 100000:
1068                 new_link.link_speed = RTE_ETH_SPEED_NUM_100G;
1069                 break;
1070         default:
1071                 new_link.link_speed = RTE_ETH_SPEED_NUM_NONE;
1072                 break;
1073         }
1074
1075         new_link.link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
1076         new_link.link_status = hw->link_up ? RTE_ETH_LINK_UP :
1077                                              RTE_ETH_LINK_DOWN;
1078         new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1079                                 RTE_ETH_LINK_SPEED_FIXED);
1080
1081         return rte_eth_linkstatus_set(dev, &new_link);
1082 }
1083
1084 static int
1085 ice_dcf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu __rte_unused)
1086 {
1087         /* mtu setting is forbidden if port is start */
1088         if (dev->data->dev_started != 0) {
1089                 PMD_DRV_LOG(ERR, "port %d must be stopped before configuration",
1090                             dev->data->port_id);
1091                 return -EBUSY;
1092         }
1093
1094         return 0;
1095 }
1096
1097 bool
1098 ice_dcf_adminq_need_retry(struct ice_adapter *ad)
1099 {
1100         return ad->hw.dcf_enabled &&
1101                !__atomic_load_n(&ad->dcf_state_on, __ATOMIC_RELAXED);
1102 }
1103
1104 /* Add UDP tunneling port */
1105 static int
1106 ice_dcf_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
1107                                 struct rte_eth_udp_tunnel *udp_tunnel)
1108 {
1109         struct ice_dcf_adapter *adapter = dev->data->dev_private;
1110         struct ice_adapter *parent_adapter = &adapter->parent;
1111         struct ice_hw *parent_hw = &parent_adapter->hw;
1112         int ret = 0;
1113
1114         if (!udp_tunnel)
1115                 return -EINVAL;
1116
1117         switch (udp_tunnel->prot_type) {
1118         case RTE_ETH_TUNNEL_TYPE_VXLAN:
1119                 ret = ice_create_tunnel(parent_hw, TNL_VXLAN,
1120                                         udp_tunnel->udp_port);
1121                 break;
1122         case RTE_ETH_TUNNEL_TYPE_ECPRI:
1123                 ret = ice_create_tunnel(parent_hw, TNL_ECPRI,
1124                                         udp_tunnel->udp_port);
1125                 break;
1126         default:
1127                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
1128                 ret = -EINVAL;
1129                 break;
1130         }
1131
1132         return ret;
1133 }
1134
1135 /* Delete UDP tunneling port */
1136 static int
1137 ice_dcf_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
1138                                 struct rte_eth_udp_tunnel *udp_tunnel)
1139 {
1140         struct ice_dcf_adapter *adapter = dev->data->dev_private;
1141         struct ice_adapter *parent_adapter = &adapter->parent;
1142         struct ice_hw *parent_hw = &parent_adapter->hw;
1143         int ret = 0;
1144
1145         if (!udp_tunnel)
1146                 return -EINVAL;
1147
1148         switch (udp_tunnel->prot_type) {
1149         case RTE_ETH_TUNNEL_TYPE_VXLAN:
1150         case RTE_ETH_TUNNEL_TYPE_ECPRI:
1151                 ret = ice_destroy_tunnel(parent_hw, udp_tunnel->udp_port, 0);
1152                 break;
1153         default:
1154                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
1155                 ret = -EINVAL;
1156                 break;
1157         }
1158
1159         return ret;
1160 }
1161
1162 static int
1163 ice_dcf_tm_ops_get(struct rte_eth_dev *dev __rte_unused,
1164                 void *arg)
1165 {
1166         if (!arg)
1167                 return -EINVAL;
1168
1169         *(const void **)arg = &ice_dcf_tm_ops;
1170
1171         return 0;
1172 }
1173
1174 static inline void
1175 ice_dcf_reset_hw(struct rte_eth_dev *eth_dev, struct ice_dcf_hw *hw)
1176 {
1177         ice_dcf_uninit_hw(eth_dev, hw);
1178         ice_dcf_init_hw(eth_dev, hw);
1179 }
1180
1181 /* Check if reset has been triggered by PF */
1182 static inline bool
1183 ice_dcf_is_reset(struct rte_eth_dev *dev)
1184 {
1185         struct ice_dcf_adapter *ad = dev->data->dev_private;
1186         struct iavf_hw *hw = &ad->real_hw.avf;
1187
1188         return !(IAVF_READ_REG(hw, IAVF_VF_ARQLEN1) &
1189                  IAVF_VF_ARQLEN1_ARQENABLE_MASK);
1190 }
1191
1192 static int
1193 ice_dcf_dev_reset(struct rte_eth_dev *dev)
1194 {
1195         struct ice_dcf_adapter *ad = dev->data->dev_private;
1196         struct ice_dcf_hw *hw = &ad->real_hw;
1197         int ret;
1198
1199         if (ice_dcf_is_reset(dev)) {
1200                 if (!ad->real_hw.resetting)
1201                         ad->real_hw.resetting = true;
1202                 PMD_DRV_LOG(ERR, "The DCF has been reset by PF");
1203
1204                 /*
1205                  * Simply reset hw to trigger an additional DCF enable/disable
1206                  * cycle which help to workaround the issue that kernel driver
1207                  * may not clean up resource during previous reset.
1208                  */
1209                 ice_dcf_reset_hw(dev, hw);
1210         }
1211
1212         ret = ice_dcf_dev_uninit(dev);
1213         if (ret)
1214                 return ret;
1215
1216         ret = ice_dcf_dev_init(dev);
1217
1218         return ret;
1219 }
1220
1221 static const uint32_t *
1222 ice_dcf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1223 {
1224         static const uint32_t ptypes[] = {
1225                 RTE_PTYPE_L2_ETHER,
1226                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
1227                 RTE_PTYPE_L4_FRAG,
1228                 RTE_PTYPE_L4_ICMP,
1229                 RTE_PTYPE_L4_NONFRAG,
1230                 RTE_PTYPE_L4_SCTP,
1231                 RTE_PTYPE_L4_TCP,
1232                 RTE_PTYPE_L4_UDP,
1233                 RTE_PTYPE_UNKNOWN
1234         };
1235         return ptypes;
1236 }
1237
1238 static const struct eth_dev_ops ice_dcf_eth_dev_ops = {
1239         .dev_start                = ice_dcf_dev_start,
1240         .dev_stop                 = ice_dcf_dev_stop,
1241         .dev_close                = ice_dcf_dev_close,
1242         .dev_reset                = ice_dcf_dev_reset,
1243         .dev_configure            = ice_dcf_dev_configure,
1244         .dev_infos_get            = ice_dcf_dev_info_get,
1245         .dev_supported_ptypes_get = ice_dcf_dev_supported_ptypes_get,
1246         .rx_queue_setup           = ice_rx_queue_setup,
1247         .tx_queue_setup           = ice_tx_queue_setup,
1248         .rx_queue_release         = ice_dev_rx_queue_release,
1249         .tx_queue_release         = ice_dev_tx_queue_release,
1250         .rx_queue_start           = ice_dcf_rx_queue_start,
1251         .tx_queue_start           = ice_dcf_tx_queue_start,
1252         .rx_queue_stop            = ice_dcf_rx_queue_stop,
1253         .tx_queue_stop            = ice_dcf_tx_queue_stop,
1254         .link_update              = ice_dcf_link_update,
1255         .stats_get                = ice_dcf_stats_get,
1256         .stats_reset              = ice_dcf_stats_reset,
1257         .promiscuous_enable       = ice_dcf_dev_promiscuous_enable,
1258         .promiscuous_disable      = ice_dcf_dev_promiscuous_disable,
1259         .allmulticast_enable      = ice_dcf_dev_allmulticast_enable,
1260         .allmulticast_disable     = ice_dcf_dev_allmulticast_disable,
1261         .flow_ops_get             = ice_dcf_dev_flow_ops_get,
1262         .udp_tunnel_port_add      = ice_dcf_dev_udp_tunnel_port_add,
1263         .udp_tunnel_port_del      = ice_dcf_dev_udp_tunnel_port_del,
1264         .tm_ops_get               = ice_dcf_tm_ops_get,
1265         .reta_update              = ice_dcf_dev_rss_reta_update,
1266         .reta_query               = ice_dcf_dev_rss_reta_query,
1267         .rss_hash_update          = ice_dcf_dev_rss_hash_update,
1268         .rss_hash_conf_get        = ice_dcf_dev_rss_hash_conf_get,
1269         .tx_done_cleanup          = ice_tx_done_cleanup,
1270         .mtu_set                  = ice_dcf_dev_mtu_set,
1271 };
1272
1273 static int
1274 ice_dcf_dev_init(struct rte_eth_dev *eth_dev)
1275 {
1276         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
1277         struct ice_adapter *parent_adapter = &adapter->parent;
1278
1279         eth_dev->dev_ops = &ice_dcf_eth_dev_ops;
1280         eth_dev->rx_pkt_burst = ice_dcf_recv_pkts;
1281         eth_dev->tx_pkt_burst = ice_dcf_xmit_pkts;
1282
1283         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1284                 return 0;
1285
1286         adapter->real_hw.vc_event_msg_cb = ice_dcf_handle_pf_event_msg;
1287         if (ice_dcf_init_hw(eth_dev, &adapter->real_hw) != 0) {
1288                 PMD_INIT_LOG(ERR, "Failed to init DCF hardware");
1289                 __atomic_store_n(&parent_adapter->dcf_state_on, false,
1290                                  __ATOMIC_RELAXED);
1291                 return -1;
1292         }
1293
1294         __atomic_store_n(&parent_adapter->dcf_state_on, true, __ATOMIC_RELAXED);
1295
1296         if (ice_dcf_init_parent_adapter(eth_dev) != 0) {
1297                 PMD_INIT_LOG(ERR, "Failed to init DCF parent adapter");
1298                 ice_dcf_uninit_hw(eth_dev, &adapter->real_hw);
1299                 return -1;
1300         }
1301
1302         return 0;
1303 }
1304
1305 static int
1306 ice_dcf_dev_uninit(struct rte_eth_dev *eth_dev)
1307 {
1308         ice_dcf_dev_close(eth_dev);
1309
1310         return 0;
1311 }
1312
1313 static int
1314 ice_dcf_cap_check_handler(__rte_unused const char *key,
1315                           const char *value, __rte_unused void *opaque)
1316 {
1317         if (strcmp(value, "dcf"))
1318                 return -1;
1319
1320         return 0;
1321 }
1322
1323 static int
1324 ice_dcf_cap_selected(struct rte_devargs *devargs)
1325 {
1326         struct rte_kvargs *kvlist;
1327         const char *key = "cap";
1328         int ret = 0;
1329
1330         if (devargs == NULL)
1331                 return 0;
1332
1333         kvlist = rte_kvargs_parse(devargs->args, NULL);
1334         if (kvlist == NULL)
1335                 return 0;
1336
1337         if (!rte_kvargs_count(kvlist, key))
1338                 goto exit;
1339
1340         /* dcf capability selected when there's a key-value pair: cap=dcf */
1341         if (rte_kvargs_process(kvlist, key,
1342                                ice_dcf_cap_check_handler, NULL) < 0)
1343                 goto exit;
1344
1345         ret = 1;
1346
1347 exit:
1348         rte_kvargs_free(kvlist);
1349         return ret;
1350 }
1351
1352 static int
1353 eth_ice_dcf_pci_probe(__rte_unused struct rte_pci_driver *pci_drv,
1354                       struct rte_pci_device *pci_dev)
1355 {
1356         struct rte_eth_devargs eth_da = { .nb_representor_ports = 0 };
1357         struct ice_dcf_vf_repr_param repr_param;
1358         char repr_name[RTE_ETH_NAME_MAX_LEN];
1359         struct ice_dcf_adapter *dcf_adapter;
1360         struct rte_eth_dev *dcf_ethdev;
1361         uint16_t dcf_vsi_id;
1362         int i, ret;
1363
1364         if (!ice_dcf_cap_selected(pci_dev->device.devargs))
1365                 return 1;
1366
1367         ret = rte_eth_devargs_parse(pci_dev->device.devargs->args, &eth_da);
1368         if (ret)
1369                 return ret;
1370
1371         ret = rte_eth_dev_pci_generic_probe(pci_dev,
1372                                             sizeof(struct ice_dcf_adapter),
1373                                             ice_dcf_dev_init);
1374         if (ret || !eth_da.nb_representor_ports)
1375                 return ret;
1376         if (eth_da.type != RTE_ETH_REPRESENTOR_VF)
1377                 return -ENOTSUP;
1378
1379         dcf_ethdev = rte_eth_dev_allocated(pci_dev->device.name);
1380         if (dcf_ethdev == NULL)
1381                 return -ENODEV;
1382
1383         dcf_adapter = dcf_ethdev->data->dev_private;
1384         ret = ice_dcf_init_repr_info(dcf_adapter);
1385         if (ret)
1386                 return ret;
1387
1388         if (eth_da.nb_representor_ports > dcf_adapter->real_hw.num_vfs ||
1389             eth_da.nb_representor_ports >= RTE_MAX_ETHPORTS) {
1390                 PMD_DRV_LOG(ERR, "the number of port representors is too large: %u",
1391                             eth_da.nb_representor_ports);
1392                 ice_dcf_free_repr_info(dcf_adapter);
1393                 return -EINVAL;
1394         }
1395
1396         dcf_vsi_id = dcf_adapter->real_hw.vsi_id | VIRTCHNL_DCF_VF_VSI_VALID;
1397
1398         repr_param.dcf_eth_dev = dcf_ethdev;
1399         repr_param.switch_domain_id = 0;
1400
1401         for (i = 0; i < eth_da.nb_representor_ports; i++) {
1402                 uint16_t vf_id = eth_da.representor_ports[i];
1403                 struct rte_eth_dev *vf_rep_eth_dev;
1404
1405                 if (vf_id >= dcf_adapter->real_hw.num_vfs) {
1406                         PMD_DRV_LOG(ERR, "VF ID %u is out of range (0 ~ %u)",
1407                                     vf_id, dcf_adapter->real_hw.num_vfs - 1);
1408                         ret = -EINVAL;
1409                         break;
1410                 }
1411
1412                 if (dcf_adapter->real_hw.vf_vsi_map[vf_id] == dcf_vsi_id) {
1413                         PMD_DRV_LOG(ERR, "VF ID %u is DCF's ID.\n", vf_id);
1414                         ret = -EINVAL;
1415                         break;
1416                 }
1417
1418                 repr_param.vf_id = vf_id;
1419                 snprintf(repr_name, sizeof(repr_name), "net_%s_representor_%u",
1420                          pci_dev->device.name, vf_id);
1421                 ret = rte_eth_dev_create(&pci_dev->device, repr_name,
1422                                          sizeof(struct ice_dcf_vf_repr),
1423                                          NULL, NULL, ice_dcf_vf_repr_init,
1424                                          &repr_param);
1425                 if (ret) {
1426                         PMD_DRV_LOG(ERR, "failed to create DCF VF representor %s",
1427                                     repr_name);
1428                         break;
1429                 }
1430
1431                 vf_rep_eth_dev = rte_eth_dev_allocated(repr_name);
1432                 if (!vf_rep_eth_dev) {
1433                         PMD_DRV_LOG(ERR,
1434                                     "Failed to find the ethdev for DCF VF representor: %s",
1435                                     repr_name);
1436                         ret = -ENODEV;
1437                         break;
1438                 }
1439
1440                 dcf_adapter->repr_infos[vf_id].vf_rep_eth_dev = vf_rep_eth_dev;
1441                 dcf_adapter->num_reprs++;
1442         }
1443
1444         return ret;
1445 }
1446
1447 static int
1448 eth_ice_dcf_pci_remove(struct rte_pci_device *pci_dev)
1449 {
1450         struct rte_eth_dev *eth_dev;
1451
1452         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
1453         if (!eth_dev)
1454                 return 0;
1455
1456         if (eth_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)
1457                 return rte_eth_dev_pci_generic_remove(pci_dev,
1458                                                       ice_dcf_vf_repr_uninit);
1459         else
1460                 return rte_eth_dev_pci_generic_remove(pci_dev,
1461                                                       ice_dcf_dev_uninit);
1462 }
1463
1464 static const struct rte_pci_id pci_id_ice_dcf_map[] = {
1465         { RTE_PCI_DEVICE(IAVF_INTEL_VENDOR_ID, IAVF_DEV_ID_ADAPTIVE_VF) },
1466         { .vendor_id = 0, /* sentinel */ },
1467 };
1468
1469 static struct rte_pci_driver rte_ice_dcf_pmd = {
1470         .id_table = pci_id_ice_dcf_map,
1471         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1472         .probe = eth_ice_dcf_pci_probe,
1473         .remove = eth_ice_dcf_pci_remove,
1474 };
1475
1476 RTE_PMD_REGISTER_PCI(net_ice_dcf, rte_ice_dcf_pmd);
1477 RTE_PMD_REGISTER_PCI_TABLE(net_ice_dcf, pci_id_ice_dcf_map);
1478 RTE_PMD_REGISTER_KMOD_DEP(net_ice_dcf, "* igb_uio | vfio-pci");
1479 RTE_PMD_REGISTER_PARAM_STRING(net_ice_dcf, "cap=dcf");