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