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