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