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