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