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