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