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