net/dpaa2: enable optional timestamp in mbuf
[dpdk.git] / drivers / net / dpaa2 / dpaa2_ethdev.c
1 /* * SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016 NXP
5  *
6  */
7
8 #include <time.h>
9 #include <net/if.h>
10
11 #include <rte_mbuf.h>
12 #include <rte_ethdev_driver.h>
13 #include <rte_malloc.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_cycles.h>
17 #include <rte_kvargs.h>
18 #include <rte_dev.h>
19 #include <rte_fslmc.h>
20
21 #include "dpaa2_pmd_logs.h"
22 #include <fslmc_vfio.h>
23 #include <dpaa2_hw_pvt.h>
24 #include <dpaa2_hw_mempool.h>
25 #include <dpaa2_hw_dpio.h>
26 #include <mc/fsl_dpmng.h>
27 #include "dpaa2_ethdev.h"
28 #include <fsl_qbman_debug.h>
29
30 /* Supported Rx offloads */
31 static uint64_t dev_rx_offloads_sup =
32                 DEV_RX_OFFLOAD_VLAN_STRIP |
33                 DEV_RX_OFFLOAD_IPV4_CKSUM |
34                 DEV_RX_OFFLOAD_UDP_CKSUM |
35                 DEV_RX_OFFLOAD_TCP_CKSUM |
36                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
37                 DEV_RX_OFFLOAD_VLAN_FILTER |
38                 DEV_RX_OFFLOAD_JUMBO_FRAME;
39
40 /* Rx offloads which cannot be disabled */
41 static uint64_t dev_rx_offloads_nodis =
42                 DEV_RX_OFFLOAD_SCATTER;
43
44 /* Supported Tx offloads */
45 static uint64_t dev_tx_offloads_sup =
46                 DEV_TX_OFFLOAD_VLAN_INSERT |
47                 DEV_TX_OFFLOAD_IPV4_CKSUM |
48                 DEV_TX_OFFLOAD_UDP_CKSUM |
49                 DEV_TX_OFFLOAD_TCP_CKSUM |
50                 DEV_TX_OFFLOAD_SCTP_CKSUM |
51                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
52
53 /* Tx offloads which cannot be disabled */
54 static uint64_t dev_tx_offloads_nodis =
55                 DEV_TX_OFFLOAD_MULTI_SEGS |
56                 DEV_TX_OFFLOAD_MT_LOCKFREE |
57                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
58
59 /* enable timestamp in mbuf */
60 enum pmd_dpaa2_ts dpaa2_enable_ts;
61
62 struct rte_dpaa2_xstats_name_off {
63         char name[RTE_ETH_XSTATS_NAME_SIZE];
64         uint8_t page_id; /* dpni statistics page id */
65         uint8_t stats_id; /* stats id in the given page */
66 };
67
68 static const struct rte_dpaa2_xstats_name_off dpaa2_xstats_strings[] = {
69         {"ingress_multicast_frames", 0, 2},
70         {"ingress_multicast_bytes", 0, 3},
71         {"ingress_broadcast_frames", 0, 4},
72         {"ingress_broadcast_bytes", 0, 5},
73         {"egress_multicast_frames", 1, 2},
74         {"egress_multicast_bytes", 1, 3},
75         {"egress_broadcast_frames", 1, 4},
76         {"egress_broadcast_bytes", 1, 5},
77         {"ingress_filtered_frames", 2, 0},
78         {"ingress_discarded_frames", 2, 1},
79         {"ingress_nobuffer_discards", 2, 2},
80         {"egress_discarded_frames", 2, 3},
81         {"egress_confirmed_frames", 2, 4},
82 };
83
84 static struct rte_dpaa2_driver rte_dpaa2_pmd;
85 static int dpaa2_dev_uninit(struct rte_eth_dev *eth_dev);
86 static int dpaa2_dev_link_update(struct rte_eth_dev *dev,
87                                  int wait_to_complete);
88 static int dpaa2_dev_set_link_up(struct rte_eth_dev *dev);
89 static int dpaa2_dev_set_link_down(struct rte_eth_dev *dev);
90 static int dpaa2_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
91
92 int dpaa2_logtype_pmd;
93
94 __rte_experimental void
95 rte_pmd_dpaa2_set_timestamp(enum pmd_dpaa2_ts enable)
96 {
97         dpaa2_enable_ts = enable;
98 }
99
100 static int
101 dpaa2_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
102 {
103         int ret;
104         struct dpaa2_dev_priv *priv = dev->data->dev_private;
105         struct fsl_mc_io *dpni = priv->hw;
106
107         PMD_INIT_FUNC_TRACE();
108
109         if (dpni == NULL) {
110                 DPAA2_PMD_ERR("dpni is NULL");
111                 return -1;
112         }
113
114         if (on)
115                 ret = dpni_add_vlan_id(dpni, CMD_PRI_LOW,
116                                        priv->token, vlan_id);
117         else
118                 ret = dpni_remove_vlan_id(dpni, CMD_PRI_LOW,
119                                           priv->token, vlan_id);
120
121         if (ret < 0)
122                 DPAA2_PMD_ERR("ret = %d Unable to add/rem vlan %d hwid =%d",
123                               ret, vlan_id, priv->hw_id);
124
125         return ret;
126 }
127
128 static int
129 dpaa2_vlan_offload_set(struct rte_eth_dev *dev, int mask)
130 {
131         struct dpaa2_dev_priv *priv = dev->data->dev_private;
132         struct fsl_mc_io *dpni = priv->hw;
133         int ret;
134
135         PMD_INIT_FUNC_TRACE();
136
137         if (mask & ETH_VLAN_FILTER_MASK) {
138                 /* VLAN Filter not avaialble */
139                 if (!priv->max_vlan_filters) {
140                         DPAA2_PMD_INFO("VLAN filter not available");
141                         goto next_mask;
142                 }
143
144                 if (dev->data->dev_conf.rxmode.offloads &
145                         DEV_RX_OFFLOAD_VLAN_FILTER)
146                         ret = dpni_enable_vlan_filter(dpni, CMD_PRI_LOW,
147                                                       priv->token, true);
148                 else
149                         ret = dpni_enable_vlan_filter(dpni, CMD_PRI_LOW,
150                                                       priv->token, false);
151                 if (ret < 0)
152                         DPAA2_PMD_INFO("Unable to set vlan filter = %d", ret);
153         }
154 next_mask:
155         if (mask & ETH_VLAN_EXTEND_MASK) {
156                 if (dev->data->dev_conf.rxmode.offloads &
157                         DEV_RX_OFFLOAD_VLAN_EXTEND)
158                         DPAA2_PMD_INFO("VLAN extend offload not supported");
159         }
160
161         return 0;
162 }
163
164 static int
165 dpaa2_fw_version_get(struct rte_eth_dev *dev,
166                      char *fw_version,
167                      size_t fw_size)
168 {
169         int ret;
170         struct dpaa2_dev_priv *priv = dev->data->dev_private;
171         struct fsl_mc_io *dpni = priv->hw;
172         struct mc_soc_version mc_plat_info = {0};
173         struct mc_version mc_ver_info = {0};
174
175         PMD_INIT_FUNC_TRACE();
176
177         if (mc_get_soc_version(dpni, CMD_PRI_LOW, &mc_plat_info))
178                 DPAA2_PMD_WARN("\tmc_get_soc_version failed");
179
180         if (mc_get_version(dpni, CMD_PRI_LOW, &mc_ver_info))
181                 DPAA2_PMD_WARN("\tmc_get_version failed");
182
183         ret = snprintf(fw_version, fw_size,
184                        "%x-%d.%d.%d",
185                        mc_plat_info.svr,
186                        mc_ver_info.major,
187                        mc_ver_info.minor,
188                        mc_ver_info.revision);
189
190         ret += 1; /* add the size of '\0' */
191         if (fw_size < (uint32_t)ret)
192                 return ret;
193         else
194                 return 0;
195 }
196
197 static void
198 dpaa2_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
199 {
200         struct dpaa2_dev_priv *priv = dev->data->dev_private;
201
202         PMD_INIT_FUNC_TRACE();
203
204         dev_info->if_index = priv->hw_id;
205
206         dev_info->max_mac_addrs = priv->max_mac_filters;
207         dev_info->max_rx_pktlen = DPAA2_MAX_RX_PKT_LEN;
208         dev_info->min_rx_bufsize = DPAA2_MIN_RX_BUF_SIZE;
209         dev_info->max_rx_queues = (uint16_t)priv->nb_rx_queues;
210         dev_info->max_tx_queues = (uint16_t)priv->nb_tx_queues;
211         dev_info->rx_offload_capa = dev_rx_offloads_sup |
212                                         dev_rx_offloads_nodis;
213         dev_info->tx_offload_capa = dev_tx_offloads_sup |
214                                         dev_tx_offloads_nodis;
215         dev_info->speed_capa = ETH_LINK_SPEED_1G |
216                         ETH_LINK_SPEED_2_5G |
217                         ETH_LINK_SPEED_10G;
218
219         dev_info->max_hash_mac_addrs = 0;
220         dev_info->max_vfs = 0;
221         dev_info->max_vmdq_pools = ETH_16_POOLS;
222         dev_info->flow_type_rss_offloads = DPAA2_RSS_OFFLOAD_ALL;
223 }
224
225 static int
226 dpaa2_alloc_rx_tx_queues(struct rte_eth_dev *dev)
227 {
228         struct dpaa2_dev_priv *priv = dev->data->dev_private;
229         uint16_t dist_idx;
230         uint32_t vq_id;
231         struct dpaa2_queue *mc_q, *mcq;
232         uint32_t tot_queues;
233         int i;
234         struct dpaa2_queue *dpaa2_q;
235
236         PMD_INIT_FUNC_TRACE();
237
238         tot_queues = priv->nb_rx_queues + priv->nb_tx_queues;
239         mc_q = rte_malloc(NULL, sizeof(struct dpaa2_queue) * tot_queues,
240                           RTE_CACHE_LINE_SIZE);
241         if (!mc_q) {
242                 DPAA2_PMD_ERR("Memory allocation failed for rx/tx queues");
243                 return -1;
244         }
245
246         for (i = 0; i < priv->nb_rx_queues; i++) {
247                 mc_q->dev = dev;
248                 priv->rx_vq[i] = mc_q++;
249                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
250                 dpaa2_q->q_storage = rte_malloc("dq_storage",
251                                         sizeof(struct queue_storage_info_t),
252                                         RTE_CACHE_LINE_SIZE);
253                 if (!dpaa2_q->q_storage)
254                         goto fail;
255
256                 memset(dpaa2_q->q_storage, 0,
257                        sizeof(struct queue_storage_info_t));
258                 if (dpaa2_alloc_dq_storage(dpaa2_q->q_storage))
259                         goto fail;
260         }
261
262         for (i = 0; i < priv->nb_tx_queues; i++) {
263                 mc_q->dev = dev;
264                 mc_q->flow_id = 0xffff;
265                 priv->tx_vq[i] = mc_q++;
266                 dpaa2_q = (struct dpaa2_queue *)priv->tx_vq[i];
267                 dpaa2_q->cscn = rte_malloc(NULL,
268                                            sizeof(struct qbman_result), 16);
269                 if (!dpaa2_q->cscn)
270                         goto fail_tx;
271         }
272
273         vq_id = 0;
274         for (dist_idx = 0; dist_idx < priv->nb_rx_queues; dist_idx++) {
275                 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
276                 mcq->tc_index = DPAA2_DEF_TC;
277                 mcq->flow_id = dist_idx;
278                 vq_id++;
279         }
280
281         return 0;
282 fail_tx:
283         i -= 1;
284         while (i >= 0) {
285                 dpaa2_q = (struct dpaa2_queue *)priv->tx_vq[i];
286                 rte_free(dpaa2_q->cscn);
287                 priv->tx_vq[i--] = NULL;
288         }
289         i = priv->nb_rx_queues;
290 fail:
291         i -= 1;
292         mc_q = priv->rx_vq[0];
293         while (i >= 0) {
294                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
295                 dpaa2_free_dq_storage(dpaa2_q->q_storage);
296                 rte_free(dpaa2_q->q_storage);
297                 priv->rx_vq[i--] = NULL;
298         }
299         rte_free(mc_q);
300         return -1;
301 }
302
303 static void
304 dpaa2_free_rx_tx_queues(struct rte_eth_dev *dev)
305 {
306         struct dpaa2_dev_priv *priv = dev->data->dev_private;
307         struct dpaa2_queue *dpaa2_q;
308         int i;
309
310         PMD_INIT_FUNC_TRACE();
311
312         /* Queue allocation base */
313         if (priv->rx_vq[0]) {
314                 /* cleaning up queue storage */
315                 for (i = 0; i < priv->nb_rx_queues; i++) {
316                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
317                         if (dpaa2_q->q_storage)
318                                 rte_free(dpaa2_q->q_storage);
319                 }
320                 /* cleanup tx queue cscn */
321                 for (i = 0; i < priv->nb_tx_queues; i++) {
322                         dpaa2_q = (struct dpaa2_queue *)priv->tx_vq[i];
323                         rte_free(dpaa2_q->cscn);
324                 }
325                 /*free memory for all queues (RX+TX) */
326                 rte_free(priv->rx_vq[0]);
327                 priv->rx_vq[0] = NULL;
328         }
329 }
330
331 static int
332 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
333 {
334         struct dpaa2_dev_priv *priv = dev->data->dev_private;
335         struct fsl_mc_io *dpni = priv->hw;
336         struct rte_eth_conf *eth_conf = &dev->data->dev_conf;
337         uint64_t rx_offloads = eth_conf->rxmode.offloads;
338         uint64_t tx_offloads = eth_conf->txmode.offloads;
339         int rx_l3_csum_offload = false;
340         int rx_l4_csum_offload = false;
341         int tx_l3_csum_offload = false;
342         int tx_l4_csum_offload = false;
343         int ret;
344
345         PMD_INIT_FUNC_TRACE();
346
347         /* Rx offloads validation */
348         if (dev_rx_offloads_nodis & ~rx_offloads) {
349                 DPAA2_PMD_WARN(
350                 "Rx offloads non configurable - requested 0x%" PRIx64
351                 " ignored 0x%" PRIx64,
352                         rx_offloads, dev_rx_offloads_nodis);
353         }
354
355         /* Tx offloads validation */
356         if (dev_tx_offloads_nodis & ~tx_offloads) {
357                 DPAA2_PMD_WARN(
358                 "Tx offloads non configurable - requested 0x%" PRIx64
359                 " ignored 0x%" PRIx64,
360                         tx_offloads, dev_tx_offloads_nodis);
361         }
362
363         if (rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
364                 if (eth_conf->rxmode.max_rx_pkt_len <= DPAA2_MAX_RX_PKT_LEN) {
365                         ret = dpni_set_max_frame_length(dpni, CMD_PRI_LOW,
366                                 priv->token, eth_conf->rxmode.max_rx_pkt_len);
367                         if (ret) {
368                                 DPAA2_PMD_ERR(
369                                         "Unable to set mtu. check config");
370                                 return ret;
371                         }
372                 } else {
373                         return -1;
374                 }
375         }
376
377         if (eth_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) {
378                 ret = dpaa2_setup_flow_dist(dev,
379                                 eth_conf->rx_adv_conf.rss_conf.rss_hf);
380                 if (ret) {
381                         DPAA2_PMD_ERR("Unable to set flow distribution."
382                                       "Check queue config");
383                         return ret;
384                 }
385         }
386
387         if (rx_offloads & DEV_RX_OFFLOAD_IPV4_CKSUM)
388                 rx_l3_csum_offload = true;
389
390         if ((rx_offloads & DEV_RX_OFFLOAD_UDP_CKSUM) ||
391                 (rx_offloads & DEV_RX_OFFLOAD_TCP_CKSUM))
392                 rx_l4_csum_offload = true;
393
394         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
395                                DPNI_OFF_RX_L3_CSUM, rx_l3_csum_offload);
396         if (ret) {
397                 DPAA2_PMD_ERR("Error to set RX l3 csum:Error = %d", ret);
398                 return ret;
399         }
400
401         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
402                                DPNI_OFF_RX_L4_CSUM, rx_l4_csum_offload);
403         if (ret) {
404                 DPAA2_PMD_ERR("Error to get RX l4 csum:Error = %d", ret);
405                 return ret;
406         }
407
408         if (tx_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
409                 tx_l3_csum_offload = true;
410
411         if ((tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM) ||
412                 (tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM) ||
413                 (tx_offloads & DEV_TX_OFFLOAD_SCTP_CKSUM))
414                 tx_l4_csum_offload = true;
415
416         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
417                                DPNI_OFF_TX_L3_CSUM, tx_l3_csum_offload);
418         if (ret) {
419                 DPAA2_PMD_ERR("Error to set TX l3 csum:Error = %d", ret);
420                 return ret;
421         }
422
423         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
424                                DPNI_OFF_TX_L4_CSUM, tx_l4_csum_offload);
425         if (ret) {
426                 DPAA2_PMD_ERR("Error to get TX l4 csum:Error = %d", ret);
427                 return ret;
428         }
429
430         /* Enabling hash results in FD requires setting DPNI_FLCTYPE_HASH in
431          * dpni_set_offload API. Setting this FLCTYPE for DPNI sets the FD[SC]
432          * to 0 for LS2 in the hardware thus disabling data/annotation
433          * stashing. For LX2 this is fixed in hardware and thus hash result and
434          * parse results can be received in FD using this option.
435          */
436         if (dpaa2_svr_family == SVR_LX2160A) {
437                 ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
438                                        DPNI_FLCTYPE_HASH, true);
439                 if (ret) {
440                         DPAA2_PMD_ERR("Error setting FLCTYPE: Err = %d", ret);
441                         return ret;
442                 }
443         }
444
445         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
446                 dpaa2_vlan_offload_set(dev, ETH_VLAN_FILTER_MASK);
447
448         /* update the current status */
449         dpaa2_dev_link_update(dev, 0);
450
451         return 0;
452 }
453
454 /* Function to setup RX flow information. It contains traffic class ID,
455  * flow ID, destination configuration etc.
456  */
457 static int
458 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
459                          uint16_t rx_queue_id,
460                          uint16_t nb_rx_desc __rte_unused,
461                          unsigned int socket_id __rte_unused,
462                          const struct rte_eth_rxconf *rx_conf __rte_unused,
463                          struct rte_mempool *mb_pool)
464 {
465         struct dpaa2_dev_priv *priv = dev->data->dev_private;
466         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
467         struct dpaa2_queue *dpaa2_q;
468         struct dpni_queue cfg;
469         uint8_t options = 0;
470         uint8_t flow_id;
471         uint32_t bpid;
472         int ret;
473
474         PMD_INIT_FUNC_TRACE();
475
476         DPAA2_PMD_DEBUG("dev =%p, queue =%d, pool = %p, conf =%p",
477                         dev, rx_queue_id, mb_pool, rx_conf);
478
479         if (!priv->bp_list || priv->bp_list->mp != mb_pool) {
480                 bpid = mempool_to_bpid(mb_pool);
481                 ret = dpaa2_attach_bp_list(priv,
482                                            rte_dpaa2_bpid_info[bpid].bp_list);
483                 if (ret)
484                         return ret;
485         }
486         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
487         dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
488
489         /*Get the flow id from given VQ id*/
490         flow_id = rx_queue_id % priv->nb_rx_queues;
491         memset(&cfg, 0, sizeof(struct dpni_queue));
492
493         options = options | DPNI_QUEUE_OPT_USER_CTX;
494         cfg.user_context = (size_t)(dpaa2_q);
495
496         /*if ls2088 or rev2 device, enable the stashing */
497
498         if ((dpaa2_svr_family & 0xffff0000) != SVR_LS2080A) {
499                 options |= DPNI_QUEUE_OPT_FLC;
500                 cfg.flc.stash_control = true;
501                 cfg.flc.value &= 0xFFFFFFFFFFFFFFC0;
502                 /* 00 00 00 - last 6 bit represent annotation, context stashing,
503                  * data stashing setting 01 01 00 (0x14)
504                  * (in following order ->DS AS CS)
505                  * to enable 1 line data, 1 line annotation.
506                  * For LX2, this setting should be 01 00 00 (0x10)
507                  */
508                 if ((dpaa2_svr_family & 0xffff0000) == SVR_LX2160A)
509                         cfg.flc.value |= 0x10;
510                 else
511                         cfg.flc.value |= 0x14;
512         }
513         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
514                              dpaa2_q->tc_index, flow_id, options, &cfg);
515         if (ret) {
516                 DPAA2_PMD_ERR("Error in setting the rx flow: = %d", ret);
517                 return -1;
518         }
519
520         if (!(priv->flags & DPAA2_RX_TAILDROP_OFF)) {
521                 struct dpni_taildrop taildrop;
522
523                 taildrop.enable = 1;
524                 /*enabling per rx queue congestion control */
525                 taildrop.threshold = CONG_THRESHOLD_RX_Q;
526                 taildrop.units = DPNI_CONGESTION_UNIT_BYTES;
527                 taildrop.oal = CONG_RX_OAL;
528                 DPAA2_PMD_DEBUG("Enabling Early Drop on queue = %d",
529                                 rx_queue_id);
530                 ret = dpni_set_taildrop(dpni, CMD_PRI_LOW, priv->token,
531                                         DPNI_CP_QUEUE, DPNI_QUEUE_RX,
532                                         dpaa2_q->tc_index, flow_id, &taildrop);
533                 if (ret) {
534                         DPAA2_PMD_ERR("Error in setting taildrop. err=(%d)",
535                                       ret);
536                         return -1;
537                 }
538         }
539
540         dev->data->rx_queues[rx_queue_id] = dpaa2_q;
541         return 0;
542 }
543
544 static int
545 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
546                          uint16_t tx_queue_id,
547                          uint16_t nb_tx_desc __rte_unused,
548                          unsigned int socket_id __rte_unused,
549                          const struct rte_eth_txconf *tx_conf __rte_unused)
550 {
551         struct dpaa2_dev_priv *priv = dev->data->dev_private;
552         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
553                 priv->tx_vq[tx_queue_id];
554         struct fsl_mc_io *dpni = priv->hw;
555         struct dpni_queue tx_conf_cfg;
556         struct dpni_queue tx_flow_cfg;
557         uint8_t options = 0, flow_id;
558         uint32_t tc_id;
559         int ret;
560
561         PMD_INIT_FUNC_TRACE();
562
563         /* Return if queue already configured */
564         if (dpaa2_q->flow_id != 0xffff) {
565                 dev->data->tx_queues[tx_queue_id] = dpaa2_q;
566                 return 0;
567         }
568
569         memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
570         memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
571
572         tc_id = tx_queue_id;
573         flow_id = 0;
574
575         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
576                              tc_id, flow_id, options, &tx_flow_cfg);
577         if (ret) {
578                 DPAA2_PMD_ERR("Error in setting the tx flow: "
579                               "tc_id=%d, flow=%d err=%d",
580                               tc_id, flow_id, ret);
581                         return -1;
582         }
583
584         dpaa2_q->flow_id = flow_id;
585
586         if (tx_queue_id == 0) {
587                 /*Set tx-conf and error configuration*/
588                 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
589                                                     priv->token,
590                                                     DPNI_CONF_DISABLE);
591                 if (ret) {
592                         DPAA2_PMD_ERR("Error in set tx conf mode settings: "
593                                       "err=%d", ret);
594                         return -1;
595                 }
596         }
597         dpaa2_q->tc_index = tc_id;
598
599         if (!(priv->flags & DPAA2_TX_CGR_OFF)) {
600                 struct dpni_congestion_notification_cfg cong_notif_cfg;
601
602                 cong_notif_cfg.units = DPNI_CONGESTION_UNIT_FRAMES;
603                 cong_notif_cfg.threshold_entry = CONG_ENTER_TX_THRESHOLD;
604                 /* Notify that the queue is not congested when the data in
605                  * the queue is below this thershold.
606                  */
607                 cong_notif_cfg.threshold_exit = CONG_EXIT_TX_THRESHOLD;
608                 cong_notif_cfg.message_ctx = 0;
609                 cong_notif_cfg.message_iova =
610                                 (size_t)DPAA2_VADDR_TO_IOVA(dpaa2_q->cscn);
611                 cong_notif_cfg.dest_cfg.dest_type = DPNI_DEST_NONE;
612                 cong_notif_cfg.notification_mode =
613                                          DPNI_CONG_OPT_WRITE_MEM_ON_ENTER |
614                                          DPNI_CONG_OPT_WRITE_MEM_ON_EXIT |
615                                          DPNI_CONG_OPT_COHERENT_WRITE;
616
617                 ret = dpni_set_congestion_notification(dpni, CMD_PRI_LOW,
618                                                        priv->token,
619                                                        DPNI_QUEUE_TX,
620                                                        tc_id,
621                                                        &cong_notif_cfg);
622                 if (ret) {
623                         DPAA2_PMD_ERR(
624                            "Error in setting tx congestion notification: "
625                            "err=%d", ret);
626                         return -ret;
627                 }
628         }
629         dev->data->tx_queues[tx_queue_id] = dpaa2_q;
630         return 0;
631 }
632
633 static void
634 dpaa2_dev_rx_queue_release(void *q __rte_unused)
635 {
636         PMD_INIT_FUNC_TRACE();
637 }
638
639 static void
640 dpaa2_dev_tx_queue_release(void *q __rte_unused)
641 {
642         PMD_INIT_FUNC_TRACE();
643 }
644
645 static uint32_t
646 dpaa2_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
647 {
648         int32_t ret;
649         struct dpaa2_dev_priv *priv = dev->data->dev_private;
650         struct dpaa2_queue *dpaa2_q;
651         struct qbman_swp *swp;
652         struct qbman_fq_query_np_rslt state;
653         uint32_t frame_cnt = 0;
654
655         PMD_INIT_FUNC_TRACE();
656
657         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
658                 ret = dpaa2_affine_qbman_swp();
659                 if (ret) {
660                         DPAA2_PMD_ERR("Failure in affining portal");
661                         return -EINVAL;
662                 }
663         }
664         swp = DPAA2_PER_LCORE_PORTAL;
665
666         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
667
668         if (qbman_fq_query_state(swp, dpaa2_q->fqid, &state) == 0) {
669                 frame_cnt = qbman_fq_state_frame_count(&state);
670                 DPAA2_PMD_DEBUG("RX frame count for q(%d) is %u",
671                                 rx_queue_id, frame_cnt);
672         }
673         return frame_cnt;
674 }
675
676 static const uint32_t *
677 dpaa2_supported_ptypes_get(struct rte_eth_dev *dev)
678 {
679         static const uint32_t ptypes[] = {
680                 /*todo -= add more types */
681                 RTE_PTYPE_L2_ETHER,
682                 RTE_PTYPE_L3_IPV4,
683                 RTE_PTYPE_L3_IPV4_EXT,
684                 RTE_PTYPE_L3_IPV6,
685                 RTE_PTYPE_L3_IPV6_EXT,
686                 RTE_PTYPE_L4_TCP,
687                 RTE_PTYPE_L4_UDP,
688                 RTE_PTYPE_L4_SCTP,
689                 RTE_PTYPE_L4_ICMP,
690                 RTE_PTYPE_UNKNOWN
691         };
692
693         if (dev->rx_pkt_burst == dpaa2_dev_prefetch_rx)
694                 return ptypes;
695         return NULL;
696 }
697
698 /**
699  * Dpaa2 link Interrupt handler
700  *
701  * @param param
702  *  The address of parameter (struct rte_eth_dev *) regsitered before.
703  *
704  * @return
705  *  void
706  */
707 static void
708 dpaa2_interrupt_handler(void *param)
709 {
710         struct rte_eth_dev *dev = param;
711         struct dpaa2_dev_priv *priv = dev->data->dev_private;
712         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
713         int ret;
714         int irq_index = DPNI_IRQ_INDEX;
715         unsigned int status = 0, clear = 0;
716
717         PMD_INIT_FUNC_TRACE();
718
719         if (dpni == NULL) {
720                 DPAA2_PMD_ERR("dpni is NULL");
721                 return;
722         }
723
724         ret = dpni_get_irq_status(dpni, CMD_PRI_LOW, priv->token,
725                                   irq_index, &status);
726         if (unlikely(ret)) {
727                 DPAA2_PMD_ERR("Can't get irq status (err %d)", ret);
728                 clear = 0xffffffff;
729                 goto out;
730         }
731
732         if (status & DPNI_IRQ_EVENT_LINK_CHANGED) {
733                 clear = DPNI_IRQ_EVENT_LINK_CHANGED;
734                 dpaa2_dev_link_update(dev, 0);
735                 /* calling all the apps registered for link status event */
736                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
737                                               NULL);
738         }
739 out:
740         ret = dpni_clear_irq_status(dpni, CMD_PRI_LOW, priv->token,
741                                     irq_index, clear);
742         if (unlikely(ret))
743                 DPAA2_PMD_ERR("Can't clear irq status (err %d)", ret);
744 }
745
746 static int
747 dpaa2_eth_setup_irqs(struct rte_eth_dev *dev, int enable)
748 {
749         int err = 0;
750         struct dpaa2_dev_priv *priv = dev->data->dev_private;
751         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
752         int irq_index = DPNI_IRQ_INDEX;
753         unsigned int mask = DPNI_IRQ_EVENT_LINK_CHANGED;
754
755         PMD_INIT_FUNC_TRACE();
756
757         err = dpni_set_irq_mask(dpni, CMD_PRI_LOW, priv->token,
758                                 irq_index, mask);
759         if (err < 0) {
760                 DPAA2_PMD_ERR("Error: dpni_set_irq_mask():%d (%s)", err,
761                               strerror(-err));
762                 return err;
763         }
764
765         err = dpni_set_irq_enable(dpni, CMD_PRI_LOW, priv->token,
766                                   irq_index, enable);
767         if (err < 0)
768                 DPAA2_PMD_ERR("Error: dpni_set_irq_enable():%d (%s)", err,
769                               strerror(-err));
770
771         return err;
772 }
773
774 static int
775 dpaa2_dev_start(struct rte_eth_dev *dev)
776 {
777         struct rte_device *rdev = dev->device;
778         struct rte_dpaa2_device *dpaa2_dev;
779         struct rte_eth_dev_data *data = dev->data;
780         struct dpaa2_dev_priv *priv = data->dev_private;
781         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
782         struct dpni_queue cfg;
783         struct dpni_error_cfg   err_cfg;
784         uint16_t qdid;
785         struct dpni_queue_id qid;
786         struct dpaa2_queue *dpaa2_q;
787         int ret, i;
788         struct rte_intr_handle *intr_handle;
789
790         dpaa2_dev = container_of(rdev, struct rte_dpaa2_device, device);
791         intr_handle = &dpaa2_dev->intr_handle;
792
793         PMD_INIT_FUNC_TRACE();
794
795         ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
796         if (ret) {
797                 DPAA2_PMD_ERR("Failure in enabling dpni %d device: err=%d",
798                               priv->hw_id, ret);
799                 return ret;
800         }
801
802         /* Power up the phy. Needed to make the link go UP */
803         dpaa2_dev_set_link_up(dev);
804
805         ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
806                             DPNI_QUEUE_TX, &qdid);
807         if (ret) {
808                 DPAA2_PMD_ERR("Error in getting qdid: err=%d", ret);
809                 return ret;
810         }
811         priv->qdid = qdid;
812
813         for (i = 0; i < data->nb_rx_queues; i++) {
814                 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
815                 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
816                                      DPNI_QUEUE_RX, dpaa2_q->tc_index,
817                                        dpaa2_q->flow_id, &cfg, &qid);
818                 if (ret) {
819                         DPAA2_PMD_ERR("Error in getting flow information: "
820                                       "err=%d", ret);
821                         return ret;
822                 }
823                 dpaa2_q->fqid = qid.fqid;
824         }
825
826         /*checksum errors, send them to normal path and set it in annotation */
827         err_cfg.errors = DPNI_ERROR_L3CE | DPNI_ERROR_L4CE;
828
829         err_cfg.error_action = DPNI_ERROR_ACTION_CONTINUE;
830         err_cfg.set_frame_annotation = true;
831
832         ret = dpni_set_errors_behavior(dpni, CMD_PRI_LOW,
833                                        priv->token, &err_cfg);
834         if (ret) {
835                 DPAA2_PMD_ERR("Error to dpni_set_errors_behavior: code = %d",
836                               ret);
837                 return ret;
838         }
839
840         /* if the interrupts were configured on this devices*/
841         if (intr_handle && (intr_handle->fd) &&
842             (dev->data->dev_conf.intr_conf.lsc != 0)) {
843                 /* Registering LSC interrupt handler */
844                 rte_intr_callback_register(intr_handle,
845                                            dpaa2_interrupt_handler,
846                                            (void *)dev);
847
848                 /* enable vfio intr/eventfd mapping
849                  * Interrupt index 0 is required, so we can not use
850                  * rte_intr_enable.
851                  */
852                 rte_dpaa2_intr_enable(intr_handle, DPNI_IRQ_INDEX);
853
854                 /* enable dpni_irqs */
855                 dpaa2_eth_setup_irqs(dev, 1);
856         }
857
858         return 0;
859 }
860
861 /**
862  *  This routine disables all traffic on the adapter by issuing a
863  *  global reset on the MAC.
864  */
865 static void
866 dpaa2_dev_stop(struct rte_eth_dev *dev)
867 {
868         struct dpaa2_dev_priv *priv = dev->data->dev_private;
869         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
870         int ret;
871         struct rte_eth_link link;
872         struct rte_intr_handle *intr_handle = dev->intr_handle;
873
874         PMD_INIT_FUNC_TRACE();
875
876         /* reset interrupt callback  */
877         if (intr_handle && (intr_handle->fd) &&
878             (dev->data->dev_conf.intr_conf.lsc != 0)) {
879                 /*disable dpni irqs */
880                 dpaa2_eth_setup_irqs(dev, 0);
881
882                 /* disable vfio intr before callback unregister */
883                 rte_dpaa2_intr_disable(intr_handle, DPNI_IRQ_INDEX);
884
885                 /* Unregistering LSC interrupt handler */
886                 rte_intr_callback_unregister(intr_handle,
887                                              dpaa2_interrupt_handler,
888                                              (void *)dev);
889         }
890
891         dpaa2_dev_set_link_down(dev);
892
893         ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
894         if (ret) {
895                 DPAA2_PMD_ERR("Failure (ret %d) in disabling dpni %d dev",
896                               ret, priv->hw_id);
897                 return;
898         }
899
900         /* clear the recorded link status */
901         memset(&link, 0, sizeof(link));
902         rte_eth_linkstatus_set(dev, &link);
903 }
904
905 static void
906 dpaa2_dev_close(struct rte_eth_dev *dev)
907 {
908         struct dpaa2_dev_priv *priv = dev->data->dev_private;
909         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
910         int ret;
911         struct rte_eth_link link;
912
913         PMD_INIT_FUNC_TRACE();
914
915         /* Clean the device first */
916         ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
917         if (ret) {
918                 DPAA2_PMD_ERR("Failure cleaning dpni device: err=%d", ret);
919                 return;
920         }
921
922         memset(&link, 0, sizeof(link));
923         rte_eth_linkstatus_set(dev, &link);
924 }
925
926 static void
927 dpaa2_dev_promiscuous_enable(
928                 struct rte_eth_dev *dev)
929 {
930         int ret;
931         struct dpaa2_dev_priv *priv = dev->data->dev_private;
932         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
933
934         PMD_INIT_FUNC_TRACE();
935
936         if (dpni == NULL) {
937                 DPAA2_PMD_ERR("dpni is NULL");
938                 return;
939         }
940
941         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
942         if (ret < 0)
943                 DPAA2_PMD_ERR("Unable to enable U promisc mode %d", ret);
944
945         ret = dpni_set_multicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
946         if (ret < 0)
947                 DPAA2_PMD_ERR("Unable to enable M promisc mode %d", ret);
948 }
949
950 static void
951 dpaa2_dev_promiscuous_disable(
952                 struct rte_eth_dev *dev)
953 {
954         int ret;
955         struct dpaa2_dev_priv *priv = dev->data->dev_private;
956         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
957
958         PMD_INIT_FUNC_TRACE();
959
960         if (dpni == NULL) {
961                 DPAA2_PMD_ERR("dpni is NULL");
962                 return;
963         }
964
965         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, false);
966         if (ret < 0)
967                 DPAA2_PMD_ERR("Unable to disable U promisc mode %d", ret);
968
969         if (dev->data->all_multicast == 0) {
970                 ret = dpni_set_multicast_promisc(dpni, CMD_PRI_LOW,
971                                                  priv->token, false);
972                 if (ret < 0)
973                         DPAA2_PMD_ERR("Unable to disable M promisc mode %d",
974                                       ret);
975         }
976 }
977
978 static void
979 dpaa2_dev_allmulticast_enable(
980                 struct rte_eth_dev *dev)
981 {
982         int ret;
983         struct dpaa2_dev_priv *priv = dev->data->dev_private;
984         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
985
986         PMD_INIT_FUNC_TRACE();
987
988         if (dpni == NULL) {
989                 DPAA2_PMD_ERR("dpni is NULL");
990                 return;
991         }
992
993         ret = dpni_set_multicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
994         if (ret < 0)
995                 DPAA2_PMD_ERR("Unable to enable multicast mode %d", ret);
996 }
997
998 static void
999 dpaa2_dev_allmulticast_disable(struct rte_eth_dev *dev)
1000 {
1001         int ret;
1002         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1003         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1004
1005         PMD_INIT_FUNC_TRACE();
1006
1007         if (dpni == NULL) {
1008                 DPAA2_PMD_ERR("dpni is NULL");
1009                 return;
1010         }
1011
1012         /* must remain on for all promiscuous */
1013         if (dev->data->promiscuous == 1)
1014                 return;
1015
1016         ret = dpni_set_multicast_promisc(dpni, CMD_PRI_LOW, priv->token, false);
1017         if (ret < 0)
1018                 DPAA2_PMD_ERR("Unable to disable multicast mode %d", ret);
1019 }
1020
1021 static int
1022 dpaa2_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1023 {
1024         int ret;
1025         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1026         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1027         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN
1028                                 + VLAN_TAG_SIZE;
1029
1030         PMD_INIT_FUNC_TRACE();
1031
1032         if (dpni == NULL) {
1033                 DPAA2_PMD_ERR("dpni is NULL");
1034                 return -EINVAL;
1035         }
1036
1037         /* check that mtu is within the allowed range */
1038         if ((mtu < ETHER_MIN_MTU) || (frame_size > DPAA2_MAX_RX_PKT_LEN))
1039                 return -EINVAL;
1040
1041         if (frame_size > ETHER_MAX_LEN)
1042                 dev->data->dev_conf.rxmode.offloads &=
1043                                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
1044         else
1045                 dev->data->dev_conf.rxmode.offloads &=
1046                                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1047
1048         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1049
1050         /* Set the Max Rx frame length as 'mtu' +
1051          * Maximum Ethernet header length
1052          */
1053         ret = dpni_set_max_frame_length(dpni, CMD_PRI_LOW, priv->token,
1054                                         frame_size);
1055         if (ret) {
1056                 DPAA2_PMD_ERR("Setting the max frame length failed");
1057                 return -1;
1058         }
1059         DPAA2_PMD_INFO("MTU configured for the device: %d", mtu);
1060         return 0;
1061 }
1062
1063 static int
1064 dpaa2_dev_add_mac_addr(struct rte_eth_dev *dev,
1065                        struct ether_addr *addr,
1066                        __rte_unused uint32_t index,
1067                        __rte_unused uint32_t pool)
1068 {
1069         int ret;
1070         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1071         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1072
1073         PMD_INIT_FUNC_TRACE();
1074
1075         if (dpni == NULL) {
1076                 DPAA2_PMD_ERR("dpni is NULL");
1077                 return -1;
1078         }
1079
1080         ret = dpni_add_mac_addr(dpni, CMD_PRI_LOW,
1081                                 priv->token, addr->addr_bytes);
1082         if (ret)
1083                 DPAA2_PMD_ERR(
1084                         "error: Adding the MAC ADDR failed: err = %d", ret);
1085         return 0;
1086 }
1087
1088 static void
1089 dpaa2_dev_remove_mac_addr(struct rte_eth_dev *dev,
1090                           uint32_t index)
1091 {
1092         int ret;
1093         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1094         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1095         struct rte_eth_dev_data *data = dev->data;
1096         struct ether_addr *macaddr;
1097
1098         PMD_INIT_FUNC_TRACE();
1099
1100         macaddr = &data->mac_addrs[index];
1101
1102         if (dpni == NULL) {
1103                 DPAA2_PMD_ERR("dpni is NULL");
1104                 return;
1105         }
1106
1107         ret = dpni_remove_mac_addr(dpni, CMD_PRI_LOW,
1108                                    priv->token, macaddr->addr_bytes);
1109         if (ret)
1110                 DPAA2_PMD_ERR(
1111                         "error: Removing the MAC ADDR failed: err = %d", ret);
1112 }
1113
1114 static int
1115 dpaa2_dev_set_mac_addr(struct rte_eth_dev *dev,
1116                        struct ether_addr *addr)
1117 {
1118         int ret;
1119         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1120         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1121
1122         PMD_INIT_FUNC_TRACE();
1123
1124         if (dpni == NULL) {
1125                 DPAA2_PMD_ERR("dpni is NULL");
1126                 return -EINVAL;
1127         }
1128
1129         ret = dpni_set_primary_mac_addr(dpni, CMD_PRI_LOW,
1130                                         priv->token, addr->addr_bytes);
1131
1132         if (ret)
1133                 DPAA2_PMD_ERR(
1134                         "error: Setting the MAC ADDR failed %d", ret);
1135
1136         return ret;
1137 }
1138
1139 static
1140 int dpaa2_dev_stats_get(struct rte_eth_dev *dev,
1141                          struct rte_eth_stats *stats)
1142 {
1143         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1144         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1145         int32_t  retcode;
1146         uint8_t page0 = 0, page1 = 1, page2 = 2;
1147         union dpni_statistics value;
1148         int i;
1149         struct dpaa2_queue *dpaa2_rxq, *dpaa2_txq;
1150
1151         memset(&value, 0, sizeof(union dpni_statistics));
1152
1153         PMD_INIT_FUNC_TRACE();
1154
1155         if (!dpni) {
1156                 DPAA2_PMD_ERR("dpni is NULL");
1157                 return -EINVAL;
1158         }
1159
1160         if (!stats) {
1161                 DPAA2_PMD_ERR("stats is NULL");
1162                 return -EINVAL;
1163         }
1164
1165         /*Get Counters from page_0*/
1166         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1167                                       page0, 0, &value);
1168         if (retcode)
1169                 goto err;
1170
1171         stats->ipackets = value.page_0.ingress_all_frames;
1172         stats->ibytes = value.page_0.ingress_all_bytes;
1173
1174         /*Get Counters from page_1*/
1175         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1176                                       page1, 0, &value);
1177         if (retcode)
1178                 goto err;
1179
1180         stats->opackets = value.page_1.egress_all_frames;
1181         stats->obytes = value.page_1.egress_all_bytes;
1182
1183         /*Get Counters from page_2*/
1184         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1185                                       page2, 0, &value);
1186         if (retcode)
1187                 goto err;
1188
1189         /* Ingress drop frame count due to configured rules */
1190         stats->ierrors = value.page_2.ingress_filtered_frames;
1191         /* Ingress drop frame count due to error */
1192         stats->ierrors += value.page_2.ingress_discarded_frames;
1193
1194         stats->oerrors = value.page_2.egress_discarded_frames;
1195         stats->imissed = value.page_2.ingress_nobuffer_discards;
1196
1197         /* Fill in per queue stats */
1198         for (i = 0; (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) &&
1199                 (i < priv->nb_rx_queues || i < priv->nb_tx_queues); ++i) {
1200                 dpaa2_rxq = (struct dpaa2_queue *)priv->rx_vq[i];
1201                 dpaa2_txq = (struct dpaa2_queue *)priv->tx_vq[i];
1202                 if (dpaa2_rxq)
1203                         stats->q_ipackets[i] = dpaa2_rxq->rx_pkts;
1204                 if (dpaa2_txq)
1205                         stats->q_opackets[i] = dpaa2_txq->tx_pkts;
1206
1207                 /* Byte counting is not implemented */
1208                 stats->q_ibytes[i]   = 0;
1209                 stats->q_obytes[i]   = 0;
1210         }
1211
1212         return 0;
1213
1214 err:
1215         DPAA2_PMD_ERR("Operation not completed:Error Code = %d", retcode);
1216         return retcode;
1217 };
1218
1219 static int
1220 dpaa2_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1221                      unsigned int n)
1222 {
1223         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1224         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1225         int32_t  retcode;
1226         union dpni_statistics value[3] = {};
1227         unsigned int i = 0, num = RTE_DIM(dpaa2_xstats_strings);
1228
1229         if (n < num)
1230                 return num;
1231
1232         if (xstats == NULL)
1233                 return 0;
1234
1235         /* Get Counters from page_0*/
1236         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1237                                       0, 0, &value[0]);
1238         if (retcode)
1239                 goto err;
1240
1241         /* Get Counters from page_1*/
1242         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1243                                       1, 0, &value[1]);
1244         if (retcode)
1245                 goto err;
1246
1247         /* Get Counters from page_2*/
1248         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1249                                       2, 0, &value[2]);
1250         if (retcode)
1251                 goto err;
1252
1253         for (i = 0; i < num; i++) {
1254                 xstats[i].id = i;
1255                 xstats[i].value = value[dpaa2_xstats_strings[i].page_id].
1256                         raw.counter[dpaa2_xstats_strings[i].stats_id];
1257         }
1258         return i;
1259 err:
1260         DPAA2_PMD_ERR("Error in obtaining extended stats (%d)", retcode);
1261         return retcode;
1262 }
1263
1264 static int
1265 dpaa2_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1266                        struct rte_eth_xstat_name *xstats_names,
1267                        unsigned int limit)
1268 {
1269         unsigned int i, stat_cnt = RTE_DIM(dpaa2_xstats_strings);
1270
1271         if (limit < stat_cnt)
1272                 return stat_cnt;
1273
1274         if (xstats_names != NULL)
1275                 for (i = 0; i < stat_cnt; i++)
1276                         snprintf(xstats_names[i].name,
1277                                  sizeof(xstats_names[i].name),
1278                                  "%s",
1279                                  dpaa2_xstats_strings[i].name);
1280
1281         return stat_cnt;
1282 }
1283
1284 static int
1285 dpaa2_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
1286                        uint64_t *values, unsigned int n)
1287 {
1288         unsigned int i, stat_cnt = RTE_DIM(dpaa2_xstats_strings);
1289         uint64_t values_copy[stat_cnt];
1290
1291         if (!ids) {
1292                 struct dpaa2_dev_priv *priv = dev->data->dev_private;
1293                 struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1294                 int32_t  retcode;
1295                 union dpni_statistics value[3] = {};
1296
1297                 if (n < stat_cnt)
1298                         return stat_cnt;
1299
1300                 if (!values)
1301                         return 0;
1302
1303                 /* Get Counters from page_0*/
1304                 retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1305                                               0, 0, &value[0]);
1306                 if (retcode)
1307                         return 0;
1308
1309                 /* Get Counters from page_1*/
1310                 retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1311                                               1, 0, &value[1]);
1312                 if (retcode)
1313                         return 0;
1314
1315                 /* Get Counters from page_2*/
1316                 retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
1317                                               2, 0, &value[2]);
1318                 if (retcode)
1319                         return 0;
1320
1321                 for (i = 0; i < stat_cnt; i++) {
1322                         values[i] = value[dpaa2_xstats_strings[i].page_id].
1323                                 raw.counter[dpaa2_xstats_strings[i].stats_id];
1324                 }
1325                 return stat_cnt;
1326         }
1327
1328         dpaa2_xstats_get_by_id(dev, NULL, values_copy, stat_cnt);
1329
1330         for (i = 0; i < n; i++) {
1331                 if (ids[i] >= stat_cnt) {
1332                         DPAA2_PMD_ERR("xstats id value isn't valid");
1333                         return -1;
1334                 }
1335                 values[i] = values_copy[ids[i]];
1336         }
1337         return n;
1338 }
1339
1340 static int
1341 dpaa2_xstats_get_names_by_id(
1342         struct rte_eth_dev *dev,
1343         struct rte_eth_xstat_name *xstats_names,
1344         const uint64_t *ids,
1345         unsigned int limit)
1346 {
1347         unsigned int i, stat_cnt = RTE_DIM(dpaa2_xstats_strings);
1348         struct rte_eth_xstat_name xstats_names_copy[stat_cnt];
1349
1350         if (!ids)
1351                 return dpaa2_xstats_get_names(dev, xstats_names, limit);
1352
1353         dpaa2_xstats_get_names(dev, xstats_names_copy, limit);
1354
1355         for (i = 0; i < limit; i++) {
1356                 if (ids[i] >= stat_cnt) {
1357                         DPAA2_PMD_ERR("xstats id value isn't valid");
1358                         return -1;
1359                 }
1360                 strcpy(xstats_names[i].name, xstats_names_copy[ids[i]].name);
1361         }
1362         return limit;
1363 }
1364
1365 static void
1366 dpaa2_dev_stats_reset(struct rte_eth_dev *dev)
1367 {
1368         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1369         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1370         int32_t  retcode;
1371         int i;
1372         struct dpaa2_queue *dpaa2_q;
1373
1374         PMD_INIT_FUNC_TRACE();
1375
1376         if (dpni == NULL) {
1377                 DPAA2_PMD_ERR("dpni is NULL");
1378                 return;
1379         }
1380
1381         retcode =  dpni_reset_statistics(dpni, CMD_PRI_LOW, priv->token);
1382         if (retcode)
1383                 goto error;
1384
1385         /* Reset the per queue stats in dpaa2_queue structure */
1386         for (i = 0; i < priv->nb_rx_queues; i++) {
1387                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
1388                 if (dpaa2_q)
1389                         dpaa2_q->rx_pkts = 0;
1390         }
1391
1392         for (i = 0; i < priv->nb_tx_queues; i++) {
1393                 dpaa2_q = (struct dpaa2_queue *)priv->tx_vq[i];
1394                 if (dpaa2_q)
1395                         dpaa2_q->tx_pkts = 0;
1396         }
1397
1398         return;
1399
1400 error:
1401         DPAA2_PMD_ERR("Operation not completed:Error Code = %d", retcode);
1402         return;
1403 };
1404
1405 /* return 0 means link status changed, -1 means not changed */
1406 static int
1407 dpaa2_dev_link_update(struct rte_eth_dev *dev,
1408                         int wait_to_complete __rte_unused)
1409 {
1410         int ret;
1411         struct dpaa2_dev_priv *priv = dev->data->dev_private;
1412         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1413         struct rte_eth_link link;
1414         struct dpni_link_state state = {0};
1415
1416         if (dpni == NULL) {
1417                 DPAA2_PMD_ERR("dpni is NULL");
1418                 return 0;
1419         }
1420
1421         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1422         if (ret < 0) {
1423                 DPAA2_PMD_DEBUG("error: dpni_get_link_state %d", ret);
1424                 return -1;
1425         }
1426
1427         memset(&link, 0, sizeof(struct rte_eth_link));
1428         link.link_status = state.up;
1429         link.link_speed = state.rate;
1430
1431         if (state.options & DPNI_LINK_OPT_HALF_DUPLEX)
1432                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
1433         else
1434                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1435
1436         ret = rte_eth_linkstatus_set(dev, &link);
1437         if (ret == -1)
1438                 DPAA2_PMD_DEBUG("No change in status");
1439         else
1440                 DPAA2_PMD_INFO("Port %d Link is %s\n", dev->data->port_id,
1441                                link.link_status ? "Up" : "Down");
1442
1443         return ret;
1444 }
1445
1446 /**
1447  * Toggle the DPNI to enable, if not already enabled.
1448  * This is not strictly PHY up/down - it is more of logical toggling.
1449  */
1450 static int
1451 dpaa2_dev_set_link_up(struct rte_eth_dev *dev)
1452 {
1453         int ret = -EINVAL;
1454         struct dpaa2_dev_priv *priv;
1455         struct fsl_mc_io *dpni;
1456         int en = 0;
1457         struct dpni_link_state state = {0};
1458
1459         priv = dev->data->dev_private;
1460         dpni = (struct fsl_mc_io *)priv->hw;
1461
1462         if (dpni == NULL) {
1463                 DPAA2_PMD_ERR("dpni is NULL");
1464                 return ret;
1465         }
1466
1467         /* Check if DPNI is currently enabled */
1468         ret = dpni_is_enabled(dpni, CMD_PRI_LOW, priv->token, &en);
1469         if (ret) {
1470                 /* Unable to obtain dpni status; Not continuing */
1471                 DPAA2_PMD_ERR("Interface Link UP failed (%d)", ret);
1472                 return -EINVAL;
1473         }
1474
1475         /* Enable link if not already enabled */
1476         if (!en) {
1477                 ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
1478                 if (ret) {
1479                         DPAA2_PMD_ERR("Interface Link UP failed (%d)", ret);
1480                         return -EINVAL;
1481                 }
1482         }
1483         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1484         if (ret < 0) {
1485                 DPAA2_PMD_DEBUG("Unable to get link state (%d)", ret);
1486                 return -1;
1487         }
1488
1489         /* changing tx burst function to start enqueues */
1490         dev->tx_pkt_burst = dpaa2_dev_tx;
1491         dev->data->dev_link.link_status = state.up;
1492
1493         if (state.up)
1494                 DPAA2_PMD_INFO("Port %d Link is Up", dev->data->port_id);
1495         else
1496                 DPAA2_PMD_INFO("Port %d Link is Down", dev->data->port_id);
1497         return ret;
1498 }
1499
1500 /**
1501  * Toggle the DPNI to disable, if not already disabled.
1502  * This is not strictly PHY up/down - it is more of logical toggling.
1503  */
1504 static int
1505 dpaa2_dev_set_link_down(struct rte_eth_dev *dev)
1506 {
1507         int ret = -EINVAL;
1508         struct dpaa2_dev_priv *priv;
1509         struct fsl_mc_io *dpni;
1510         int dpni_enabled = 0;
1511         int retries = 10;
1512
1513         PMD_INIT_FUNC_TRACE();
1514
1515         priv = dev->data->dev_private;
1516         dpni = (struct fsl_mc_io *)priv->hw;
1517
1518         if (dpni == NULL) {
1519                 DPAA2_PMD_ERR("Device has not yet been configured");
1520                 return ret;
1521         }
1522
1523         /*changing  tx burst function to avoid any more enqueues */
1524         dev->tx_pkt_burst = dummy_dev_tx;
1525
1526         /* Loop while dpni_disable() attempts to drain the egress FQs
1527          * and confirm them back to us.
1528          */
1529         do {
1530                 ret = dpni_disable(dpni, 0, priv->token);
1531                 if (ret) {
1532                         DPAA2_PMD_ERR("dpni disable failed (%d)", ret);
1533                         return ret;
1534                 }
1535                 ret = dpni_is_enabled(dpni, 0, priv->token, &dpni_enabled);
1536                 if (ret) {
1537                         DPAA2_PMD_ERR("dpni enable check failed (%d)", ret);
1538                         return ret;
1539                 }
1540                 if (dpni_enabled)
1541                         /* Allow the MC some slack */
1542                         rte_delay_us(100 * 1000);
1543         } while (dpni_enabled && --retries);
1544
1545         if (!retries) {
1546                 DPAA2_PMD_WARN("Retry count exceeded disabling dpni");
1547                 /* todo- we may have to manually cleanup queues.
1548                  */
1549         } else {
1550                 DPAA2_PMD_INFO("Port %d Link DOWN successful",
1551                                dev->data->port_id);
1552         }
1553
1554         dev->data->dev_link.link_status = 0;
1555
1556         return ret;
1557 }
1558
1559 static int
1560 dpaa2_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1561 {
1562         int ret = -EINVAL;
1563         struct dpaa2_dev_priv *priv;
1564         struct fsl_mc_io *dpni;
1565         struct dpni_link_state state = {0};
1566
1567         PMD_INIT_FUNC_TRACE();
1568
1569         priv = dev->data->dev_private;
1570         dpni = (struct fsl_mc_io *)priv->hw;
1571
1572         if (dpni == NULL || fc_conf == NULL) {
1573                 DPAA2_PMD_ERR("device not configured");
1574                 return ret;
1575         }
1576
1577         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1578         if (ret) {
1579                 DPAA2_PMD_ERR("error: dpni_get_link_state %d", ret);
1580                 return ret;
1581         }
1582
1583         memset(fc_conf, 0, sizeof(struct rte_eth_fc_conf));
1584         if (state.options & DPNI_LINK_OPT_PAUSE) {
1585                 /* DPNI_LINK_OPT_PAUSE set
1586                  *  if ASYM_PAUSE not set,
1587                  *      RX Side flow control (handle received Pause frame)
1588                  *      TX side flow control (send Pause frame)
1589                  *  if ASYM_PAUSE set,
1590                  *      RX Side flow control (handle received Pause frame)
1591                  *      No TX side flow control (send Pause frame disabled)
1592                  */
1593                 if (!(state.options & DPNI_LINK_OPT_ASYM_PAUSE))
1594                         fc_conf->mode = RTE_FC_FULL;
1595                 else
1596                         fc_conf->mode = RTE_FC_RX_PAUSE;
1597         } else {
1598                 /* DPNI_LINK_OPT_PAUSE not set
1599                  *  if ASYM_PAUSE set,
1600                  *      TX side flow control (send Pause frame)
1601                  *      No RX side flow control (No action on pause frame rx)
1602                  *  if ASYM_PAUSE not set,
1603                  *      Flow control disabled
1604                  */
1605                 if (state.options & DPNI_LINK_OPT_ASYM_PAUSE)
1606                         fc_conf->mode = RTE_FC_TX_PAUSE;
1607                 else
1608                         fc_conf->mode = RTE_FC_NONE;
1609         }
1610
1611         return ret;
1612 }
1613
1614 static int
1615 dpaa2_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1616 {
1617         int ret = -EINVAL;
1618         struct dpaa2_dev_priv *priv;
1619         struct fsl_mc_io *dpni;
1620         struct dpni_link_state state = {0};
1621         struct dpni_link_cfg cfg = {0};
1622
1623         PMD_INIT_FUNC_TRACE();
1624
1625         priv = dev->data->dev_private;
1626         dpni = (struct fsl_mc_io *)priv->hw;
1627
1628         if (dpni == NULL) {
1629                 DPAA2_PMD_ERR("dpni is NULL");
1630                 return ret;
1631         }
1632
1633         /* It is necessary to obtain the current state before setting fc_conf
1634          * as MC would return error in case rate, autoneg or duplex values are
1635          * different.
1636          */
1637         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1638         if (ret) {
1639                 DPAA2_PMD_ERR("Unable to get link state (err=%d)", ret);
1640                 return -1;
1641         }
1642
1643         /* Disable link before setting configuration */
1644         dpaa2_dev_set_link_down(dev);
1645
1646         /* Based on fc_conf, update cfg */
1647         cfg.rate = state.rate;
1648         cfg.options = state.options;
1649
1650         /* update cfg with fc_conf */
1651         switch (fc_conf->mode) {
1652         case RTE_FC_FULL:
1653                 /* Full flow control;
1654                  * OPT_PAUSE set, ASYM_PAUSE not set
1655                  */
1656                 cfg.options |= DPNI_LINK_OPT_PAUSE;
1657                 cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
1658                 break;
1659         case RTE_FC_TX_PAUSE:
1660                 /* Enable RX flow control
1661                  * OPT_PAUSE not set;
1662                  * ASYM_PAUSE set;
1663                  */
1664                 cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
1665                 cfg.options &= ~DPNI_LINK_OPT_PAUSE;
1666                 break;
1667         case RTE_FC_RX_PAUSE:
1668                 /* Enable TX Flow control
1669                  * OPT_PAUSE set
1670                  * ASYM_PAUSE set
1671                  */
1672                 cfg.options |= DPNI_LINK_OPT_PAUSE;
1673                 cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
1674                 break;
1675         case RTE_FC_NONE:
1676                 /* Disable Flow control
1677                  * OPT_PAUSE not set
1678                  * ASYM_PAUSE not set
1679                  */
1680                 cfg.options &= ~DPNI_LINK_OPT_PAUSE;
1681                 cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
1682                 break;
1683         default:
1684                 DPAA2_PMD_ERR("Incorrect Flow control flag (%d)",
1685                               fc_conf->mode);
1686                 return -1;
1687         }
1688
1689         ret = dpni_set_link_cfg(dpni, CMD_PRI_LOW, priv->token, &cfg);
1690         if (ret)
1691                 DPAA2_PMD_ERR("Unable to set Link configuration (err=%d)",
1692                               ret);
1693
1694         /* Enable link */
1695         dpaa2_dev_set_link_up(dev);
1696
1697         return ret;
1698 }
1699
1700 static int
1701 dpaa2_dev_rss_hash_update(struct rte_eth_dev *dev,
1702                           struct rte_eth_rss_conf *rss_conf)
1703 {
1704         struct rte_eth_dev_data *data = dev->data;
1705         struct rte_eth_conf *eth_conf = &data->dev_conf;
1706         int ret;
1707
1708         PMD_INIT_FUNC_TRACE();
1709
1710         if (rss_conf->rss_hf) {
1711                 ret = dpaa2_setup_flow_dist(dev, rss_conf->rss_hf);
1712                 if (ret) {
1713                         DPAA2_PMD_ERR("Unable to set flow dist");
1714                         return ret;
1715                 }
1716         } else {
1717                 ret = dpaa2_remove_flow_dist(dev, 0);
1718                 if (ret) {
1719                         DPAA2_PMD_ERR("Unable to remove flow dist");
1720                         return ret;
1721                 }
1722         }
1723         eth_conf->rx_adv_conf.rss_conf.rss_hf = rss_conf->rss_hf;
1724         return 0;
1725 }
1726
1727 static int
1728 dpaa2_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1729                             struct rte_eth_rss_conf *rss_conf)
1730 {
1731         struct rte_eth_dev_data *data = dev->data;
1732         struct rte_eth_conf *eth_conf = &data->dev_conf;
1733
1734         /* dpaa2 does not support rss_key, so length should be 0*/
1735         rss_conf->rss_key_len = 0;
1736         rss_conf->rss_hf = eth_conf->rx_adv_conf.rss_conf.rss_hf;
1737         return 0;
1738 }
1739
1740 int dpaa2_eth_eventq_attach(const struct rte_eth_dev *dev,
1741                 int eth_rx_queue_id,
1742                 uint16_t dpcon_id,
1743                 const struct rte_event_eth_rx_adapter_queue_conf *queue_conf)
1744 {
1745         struct dpaa2_dev_priv *eth_priv = dev->data->dev_private;
1746         struct fsl_mc_io *dpni = (struct fsl_mc_io *)eth_priv->hw;
1747         struct dpaa2_queue *dpaa2_ethq = eth_priv->rx_vq[eth_rx_queue_id];
1748         uint8_t flow_id = dpaa2_ethq->flow_id;
1749         struct dpni_queue cfg;
1750         uint8_t options;
1751         int ret;
1752
1753         if (queue_conf->ev.sched_type == RTE_SCHED_TYPE_PARALLEL)
1754                 dpaa2_ethq->cb = dpaa2_dev_process_parallel_event;
1755         else if (queue_conf->ev.sched_type == RTE_SCHED_TYPE_ATOMIC)
1756                 dpaa2_ethq->cb = dpaa2_dev_process_atomic_event;
1757         else
1758                 return -EINVAL;
1759
1760         memset(&cfg, 0, sizeof(struct dpni_queue));
1761         options = DPNI_QUEUE_OPT_DEST;
1762         cfg.destination.type = DPNI_DEST_DPCON;
1763         cfg.destination.id = dpcon_id;
1764         cfg.destination.priority = queue_conf->ev.priority;
1765
1766         if (queue_conf->ev.sched_type == RTE_SCHED_TYPE_ATOMIC) {
1767                 options |= DPNI_QUEUE_OPT_HOLD_ACTIVE;
1768                 cfg.destination.hold_active = 1;
1769         }
1770
1771         options |= DPNI_QUEUE_OPT_USER_CTX;
1772         cfg.user_context = (size_t)(dpaa2_ethq);
1773
1774         ret = dpni_set_queue(dpni, CMD_PRI_LOW, eth_priv->token, DPNI_QUEUE_RX,
1775                              dpaa2_ethq->tc_index, flow_id, options, &cfg);
1776         if (ret) {
1777                 DPAA2_PMD_ERR("Error in dpni_set_queue: ret: %d", ret);
1778                 return ret;
1779         }
1780
1781         memcpy(&dpaa2_ethq->ev, &queue_conf->ev, sizeof(struct rte_event));
1782
1783         return 0;
1784 }
1785
1786 int dpaa2_eth_eventq_detach(const struct rte_eth_dev *dev,
1787                 int eth_rx_queue_id)
1788 {
1789         struct dpaa2_dev_priv *eth_priv = dev->data->dev_private;
1790         struct fsl_mc_io *dpni = (struct fsl_mc_io *)eth_priv->hw;
1791         struct dpaa2_queue *dpaa2_ethq = eth_priv->rx_vq[eth_rx_queue_id];
1792         uint8_t flow_id = dpaa2_ethq->flow_id;
1793         struct dpni_queue cfg;
1794         uint8_t options;
1795         int ret;
1796
1797         memset(&cfg, 0, sizeof(struct dpni_queue));
1798         options = DPNI_QUEUE_OPT_DEST;
1799         cfg.destination.type = DPNI_DEST_NONE;
1800
1801         ret = dpni_set_queue(dpni, CMD_PRI_LOW, eth_priv->token, DPNI_QUEUE_RX,
1802                              dpaa2_ethq->tc_index, flow_id, options, &cfg);
1803         if (ret)
1804                 DPAA2_PMD_ERR("Error in dpni_set_queue: ret: %d", ret);
1805
1806         return ret;
1807 }
1808
1809 static struct eth_dev_ops dpaa2_ethdev_ops = {
1810         .dev_configure    = dpaa2_eth_dev_configure,
1811         .dev_start            = dpaa2_dev_start,
1812         .dev_stop             = dpaa2_dev_stop,
1813         .dev_close            = dpaa2_dev_close,
1814         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
1815         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
1816         .allmulticast_enable  = dpaa2_dev_allmulticast_enable,
1817         .allmulticast_disable = dpaa2_dev_allmulticast_disable,
1818         .dev_set_link_up      = dpaa2_dev_set_link_up,
1819         .dev_set_link_down    = dpaa2_dev_set_link_down,
1820         .link_update       = dpaa2_dev_link_update,
1821         .stats_get             = dpaa2_dev_stats_get,
1822         .xstats_get            = dpaa2_dev_xstats_get,
1823         .xstats_get_by_id     = dpaa2_xstats_get_by_id,
1824         .xstats_get_names_by_id = dpaa2_xstats_get_names_by_id,
1825         .xstats_get_names      = dpaa2_xstats_get_names,
1826         .stats_reset       = dpaa2_dev_stats_reset,
1827         .xstats_reset         = dpaa2_dev_stats_reset,
1828         .fw_version_get    = dpaa2_fw_version_get,
1829         .dev_infos_get     = dpaa2_dev_info_get,
1830         .dev_supported_ptypes_get = dpaa2_supported_ptypes_get,
1831         .mtu_set           = dpaa2_dev_mtu_set,
1832         .vlan_filter_set      = dpaa2_vlan_filter_set,
1833         .vlan_offload_set     = dpaa2_vlan_offload_set,
1834         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
1835         .rx_queue_release  = dpaa2_dev_rx_queue_release,
1836         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
1837         .tx_queue_release  = dpaa2_dev_tx_queue_release,
1838         .rx_queue_count       = dpaa2_dev_rx_queue_count,
1839         .flow_ctrl_get        = dpaa2_flow_ctrl_get,
1840         .flow_ctrl_set        = dpaa2_flow_ctrl_set,
1841         .mac_addr_add         = dpaa2_dev_add_mac_addr,
1842         .mac_addr_remove      = dpaa2_dev_remove_mac_addr,
1843         .mac_addr_set         = dpaa2_dev_set_mac_addr,
1844         .rss_hash_update      = dpaa2_dev_rss_hash_update,
1845         .rss_hash_conf_get    = dpaa2_dev_rss_hash_conf_get,
1846 };
1847
1848 /* Populate the mac address from physically available (u-boot/firmware) and/or
1849  * one set by higher layers like MC (restool) etc.
1850  * Returns the table of MAC entries (multiple entries)
1851  */
1852 static int
1853 populate_mac_addr(struct fsl_mc_io *dpni_dev, struct dpaa2_dev_priv *priv,
1854                   struct ether_addr *mac_entry)
1855 {
1856         int ret;
1857         struct ether_addr phy_mac, prime_mac;
1858
1859         memset(&phy_mac, 0, sizeof(struct ether_addr));
1860         memset(&prime_mac, 0, sizeof(struct ether_addr));
1861
1862         /* Get the physical device MAC address */
1863         ret = dpni_get_port_mac_addr(dpni_dev, CMD_PRI_LOW, priv->token,
1864                                      phy_mac.addr_bytes);
1865         if (ret) {
1866                 DPAA2_PMD_ERR("DPNI get physical port MAC failed: %d", ret);
1867                 goto cleanup;
1868         }
1869
1870         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW, priv->token,
1871                                         prime_mac.addr_bytes);
1872         if (ret) {
1873                 DPAA2_PMD_ERR("DPNI get Prime port MAC failed: %d", ret);
1874                 goto cleanup;
1875         }
1876
1877         /* Now that both MAC have been obtained, do:
1878          *  if not_empty_mac(phy) && phy != Prime, overwrite prime with Phy
1879          *     and return phy
1880          *  If empty_mac(phy), return prime.
1881          *  if both are empty, create random MAC, set as prime and return
1882          */
1883         if (!is_zero_ether_addr(&phy_mac)) {
1884                 /* If the addresses are not same, overwrite prime */
1885                 if (!is_same_ether_addr(&phy_mac, &prime_mac)) {
1886                         ret = dpni_set_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
1887                                                         priv->token,
1888                                                         phy_mac.addr_bytes);
1889                         if (ret) {
1890                                 DPAA2_PMD_ERR("Unable to set MAC Address: %d",
1891                                               ret);
1892                                 goto cleanup;
1893                         }
1894                         memcpy(&prime_mac, &phy_mac, sizeof(struct ether_addr));
1895                 }
1896         } else if (is_zero_ether_addr(&prime_mac)) {
1897                 /* In case phys and prime, both are zero, create random MAC */
1898                 eth_random_addr(prime_mac.addr_bytes);
1899                 ret = dpni_set_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
1900                                                 priv->token,
1901                                                 prime_mac.addr_bytes);
1902                 if (ret) {
1903                         DPAA2_PMD_ERR("Unable to set MAC Address: %d", ret);
1904                         goto cleanup;
1905                 }
1906         }
1907
1908         /* prime_mac the final MAC address */
1909         memcpy(mac_entry, &prime_mac, sizeof(struct ether_addr));
1910         return 0;
1911
1912 cleanup:
1913         return -1;
1914 }
1915
1916 static int
1917 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
1918 {
1919         struct rte_device *dev = eth_dev->device;
1920         struct rte_dpaa2_device *dpaa2_dev;
1921         struct fsl_mc_io *dpni_dev;
1922         struct dpni_attr attr;
1923         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
1924         struct dpni_buffer_layout layout;
1925         int ret, hw_id;
1926
1927         PMD_INIT_FUNC_TRACE();
1928
1929         /* For secondary processes, the primary has done all the work */
1930         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1931                 /* In case of secondary, only burst and ops API need to be
1932                  * plugged.
1933                  */
1934                 eth_dev->dev_ops = &dpaa2_ethdev_ops;
1935                 eth_dev->rx_pkt_burst = dpaa2_dev_prefetch_rx;
1936                 eth_dev->tx_pkt_burst = dpaa2_dev_tx;
1937                 return 0;
1938         }
1939
1940         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
1941
1942         hw_id = dpaa2_dev->object_id;
1943
1944         dpni_dev = rte_malloc(NULL, sizeof(struct fsl_mc_io), 0);
1945         if (!dpni_dev) {
1946                 DPAA2_PMD_ERR("Memory allocation failed for dpni device");
1947                 return -1;
1948         }
1949
1950         dpni_dev->regs = rte_mcp_ptr_list[0];
1951         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
1952         if (ret) {
1953                 DPAA2_PMD_ERR(
1954                              "Failure in opening dpni@%d with err code %d",
1955                              hw_id, ret);
1956                 rte_free(dpni_dev);
1957                 return -1;
1958         }
1959
1960         /* Clean the device first */
1961         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
1962         if (ret) {
1963                 DPAA2_PMD_ERR("Failure cleaning dpni@%d with err code %d",
1964                               hw_id, ret);
1965                 goto init_err;
1966         }
1967
1968         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
1969         if (ret) {
1970                 DPAA2_PMD_ERR(
1971                              "Failure in get dpni@%d attribute, err code %d",
1972                              hw_id, ret);
1973                 goto init_err;
1974         }
1975
1976         priv->num_rx_tc = attr.num_rx_tcs;
1977
1978         /* Resetting the "num_rx_queues" to equal number of queues in first TC
1979          * as only one TC is supported on Rx Side. Once Multiple TCs will be
1980          * in use for Rx processing then this will be changed or removed.
1981          */
1982         priv->nb_rx_queues = attr.num_queues;
1983
1984         /* Using number of TX queues as number of TX TCs */
1985         priv->nb_tx_queues = attr.num_tx_tcs;
1986
1987         DPAA2_PMD_DEBUG("RX-TC= %d, nb_rx_queues= %d, nb_tx_queues=%d",
1988                         priv->num_rx_tc, priv->nb_rx_queues,
1989                         priv->nb_tx_queues);
1990
1991         priv->hw = dpni_dev;
1992         priv->hw_id = hw_id;
1993         priv->options = attr.options;
1994         priv->max_mac_filters = attr.mac_filter_entries;
1995         priv->max_vlan_filters = attr.vlan_filter_entries;
1996         priv->flags = 0;
1997
1998         /* Allocate memory for hardware structure for queues */
1999         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
2000         if (ret) {
2001                 DPAA2_PMD_ERR("Queue allocation Failed");
2002                 goto init_err;
2003         }
2004
2005         /* Allocate memory for storing MAC addresses.
2006          * Table of mac_filter_entries size is allocated so that RTE ether lib
2007          * can add MAC entries when rte_eth_dev_mac_addr_add is called.
2008          */
2009         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
2010                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
2011         if (eth_dev->data->mac_addrs == NULL) {
2012                 DPAA2_PMD_ERR(
2013                    "Failed to allocate %d bytes needed to store MAC addresses",
2014                    ETHER_ADDR_LEN * attr.mac_filter_entries);
2015                 ret = -ENOMEM;
2016                 goto init_err;
2017         }
2018
2019         ret = populate_mac_addr(dpni_dev, priv, &eth_dev->data->mac_addrs[0]);
2020         if (ret) {
2021                 DPAA2_PMD_ERR("Unable to fetch MAC Address for device");
2022                 rte_free(eth_dev->data->mac_addrs);
2023                 eth_dev->data->mac_addrs = NULL;
2024                 goto init_err;
2025         }
2026
2027         /* ... tx buffer layout ... */
2028         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
2029         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
2030         layout.pass_frame_status = 1;
2031         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
2032                                      DPNI_QUEUE_TX, &layout);
2033         if (ret) {
2034                 DPAA2_PMD_ERR("Error (%d) in setting tx buffer layout", ret);
2035                 goto init_err;
2036         }
2037
2038         /* ... tx-conf and error buffer layout ... */
2039         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
2040         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
2041         layout.pass_frame_status = 1;
2042         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
2043                                      DPNI_QUEUE_TX_CONFIRM, &layout);
2044         if (ret) {
2045                 DPAA2_PMD_ERR("Error (%d) in setting tx-conf buffer layout",
2046                              ret);
2047                 goto init_err;
2048         }
2049
2050         eth_dev->dev_ops = &dpaa2_ethdev_ops;
2051
2052         eth_dev->rx_pkt_burst = dpaa2_dev_prefetch_rx;
2053         eth_dev->tx_pkt_burst = dpaa2_dev_tx;
2054
2055         RTE_LOG(INFO, PMD, "%s: netdev created\n", eth_dev->data->name);
2056         return 0;
2057 init_err:
2058         dpaa2_dev_uninit(eth_dev);
2059         return ret;
2060 }
2061
2062 static int
2063 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
2064 {
2065         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
2066         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
2067         int ret;
2068
2069         PMD_INIT_FUNC_TRACE();
2070
2071         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2072                 return 0;
2073
2074         if (!dpni) {
2075                 DPAA2_PMD_WARN("Already closed or not started");
2076                 return -1;
2077         }
2078
2079         dpaa2_dev_close(eth_dev);
2080
2081         dpaa2_free_rx_tx_queues(eth_dev);
2082
2083         /* Close the device at underlying layer*/
2084         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
2085         if (ret) {
2086                 DPAA2_PMD_ERR(
2087                              "Failure closing dpni device with err code %d",
2088                              ret);
2089         }
2090
2091         /* Free the allocated memory for ethernet private data and dpni*/
2092         priv->hw = NULL;
2093         rte_free(dpni);
2094
2095         eth_dev->dev_ops = NULL;
2096         eth_dev->rx_pkt_burst = NULL;
2097         eth_dev->tx_pkt_burst = NULL;
2098
2099         DPAA2_PMD_INFO("%s: netdev deleted", eth_dev->data->name);
2100         return 0;
2101 }
2102
2103 static int
2104 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv,
2105                 struct rte_dpaa2_device *dpaa2_dev)
2106 {
2107         struct rte_eth_dev *eth_dev;
2108         int diag;
2109
2110         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2111                 eth_dev = rte_eth_dev_allocate(dpaa2_dev->device.name);
2112                 if (!eth_dev)
2113                         return -ENODEV;
2114                 eth_dev->data->dev_private = rte_zmalloc(
2115                                                 "ethdev private structure",
2116                                                 sizeof(struct dpaa2_dev_priv),
2117                                                 RTE_CACHE_LINE_SIZE);
2118                 if (eth_dev->data->dev_private == NULL) {
2119                         DPAA2_PMD_CRIT(
2120                                 "Unable to allocate memory for private data");
2121                         rte_eth_dev_release_port(eth_dev);
2122                         return -ENOMEM;
2123                 }
2124         } else {
2125                 eth_dev = rte_eth_dev_attach_secondary(dpaa2_dev->device.name);
2126                 if (!eth_dev)
2127                         return -ENODEV;
2128         }
2129
2130         eth_dev->device = &dpaa2_dev->device;
2131
2132         dpaa2_dev->eth_dev = eth_dev;
2133         eth_dev->data->rx_mbuf_alloc_failed = 0;
2134
2135         if (dpaa2_drv->drv_flags & RTE_DPAA2_DRV_INTR_LSC)
2136                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
2137
2138         /* Invoke PMD device initialization function */
2139         diag = dpaa2_dev_init(eth_dev);
2140         if (diag == 0) {
2141                 rte_eth_dev_probing_finish(eth_dev);
2142                 return 0;
2143         }
2144
2145         rte_eth_dev_release_port(eth_dev);
2146         return diag;
2147 }
2148
2149 static int
2150 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
2151 {
2152         struct rte_eth_dev *eth_dev;
2153
2154         eth_dev = dpaa2_dev->eth_dev;
2155         dpaa2_dev_uninit(eth_dev);
2156
2157         rte_eth_dev_release_port(eth_dev);
2158
2159         return 0;
2160 }
2161
2162 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
2163         .drv_flags = RTE_DPAA2_DRV_INTR_LSC | RTE_DPAA2_DRV_IOVA_AS_VA,
2164         .drv_type = DPAA2_ETH,
2165         .probe = rte_dpaa2_probe,
2166         .remove = rte_dpaa2_remove,
2167 };
2168
2169 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);
2170
2171 RTE_INIT(dpaa2_pmd_init_log)
2172 {
2173         dpaa2_logtype_pmd = rte_log_register("pmd.net.dpaa2");
2174         if (dpaa2_logtype_pmd >= 0)
2175                 rte_log_set_level(dpaa2_logtype_pmd, RTE_LOG_NOTICE);
2176 }