72c9e67792e0ee032f6c40101c8a0dbe3bd16ea6
[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         PMD_INIT_FUNC_TRACE();
1141
1142         if (dpni == NULL) {
1143                 RTE_LOG(ERR, PMD, "dpni is NULL\n");
1144                 return 0;
1145         }
1146         memset(&old, 0, sizeof(old));
1147         dpaa2_dev_atomic_read_link_status(dev, &old);
1148
1149         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1150         if (ret < 0) {
1151                 RTE_LOG(ERR, PMD, "error: dpni_get_link_state %d\n", ret);
1152                 return -1;
1153         }
1154
1155         if ((old.link_status == state.up) && (old.link_speed == state.rate)) {
1156                 RTE_LOG(DEBUG, PMD, "No change in status\n");
1157                 return -1;
1158         }
1159
1160         memset(&link, 0, sizeof(struct rte_eth_link));
1161         link.link_status = state.up;
1162         link.link_speed = state.rate;
1163
1164         if (state.options & DPNI_LINK_OPT_HALF_DUPLEX)
1165                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
1166         else
1167                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
1168
1169         dpaa2_dev_atomic_write_link_status(dev, &link);
1170
1171         if (link.link_status)
1172                 PMD_DRV_LOG(INFO, "Port %d Link is Up\n", dev->data->port_id);
1173         else
1174                 PMD_DRV_LOG(INFO, "Port %d Link is Down\n", dev->data->port_id);
1175         return 0;
1176 }
1177
1178 /**
1179  * Toggle the DPNI to enable, if not already enabled.
1180  * This is not strictly PHY up/down - it is more of logical toggling.
1181  */
1182 static int
1183 dpaa2_dev_set_link_up(struct rte_eth_dev *dev)
1184 {
1185         int ret = -EINVAL;
1186         struct dpaa2_dev_priv *priv;
1187         struct fsl_mc_io *dpni;
1188         int en = 0;
1189
1190         PMD_INIT_FUNC_TRACE();
1191
1192         priv = dev->data->dev_private;
1193         dpni = (struct fsl_mc_io *)priv->hw;
1194
1195         if (dpni == NULL) {
1196                 RTE_LOG(ERR, PMD, "DPNI is NULL\n");
1197                 return ret;
1198         }
1199
1200         /* Check if DPNI is currently enabled */
1201         ret = dpni_is_enabled(dpni, CMD_PRI_LOW, priv->token, &en);
1202         if (ret) {
1203                 /* Unable to obtain dpni status; Not continuing */
1204                 PMD_DRV_LOG(ERR, "Interface Link UP failed (%d)", ret);
1205                 return -EINVAL;
1206         }
1207
1208         /* Enable link if not already enabled */
1209         if (!en) {
1210                 ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
1211                 if (ret) {
1212                         PMD_DRV_LOG(ERR, "Interface Link UP failed (%d)", ret);
1213                         return -EINVAL;
1214                 }
1215         }
1216         /* changing tx burst function to start enqueues */
1217         dev->tx_pkt_burst = dpaa2_dev_tx;
1218         dev->data->dev_link.link_status = 1;
1219
1220         PMD_DRV_LOG(INFO, "Port %d Link UP successful", dev->data->port_id);
1221         return ret;
1222 }
1223
1224 /**
1225  * Toggle the DPNI to disable, if not already disabled.
1226  * This is not strictly PHY up/down - it is more of logical toggling.
1227  */
1228 static int
1229 dpaa2_dev_set_link_down(struct rte_eth_dev *dev)
1230 {
1231         int ret = -EINVAL;
1232         struct dpaa2_dev_priv *priv;
1233         struct fsl_mc_io *dpni;
1234         int dpni_enabled = 0;
1235         int retries = 10;
1236
1237         PMD_INIT_FUNC_TRACE();
1238
1239         priv = dev->data->dev_private;
1240         dpni = (struct fsl_mc_io *)priv->hw;
1241
1242         if (dpni == NULL) {
1243                 RTE_LOG(ERR, PMD, "Device has not yet been configured\n");
1244                 return ret;
1245         }
1246
1247         /*changing  tx burst function to avoid any more enqueues */
1248         dev->tx_pkt_burst = dummy_dev_tx;
1249
1250         /* Loop while dpni_disable() attempts to drain the egress FQs
1251          * and confirm them back to us.
1252          */
1253         do {
1254                 ret = dpni_disable(dpni, 0, priv->token);
1255                 if (ret) {
1256                         PMD_DRV_LOG(ERR, "dpni disable failed (%d)", ret);
1257                         return ret;
1258                 }
1259                 ret = dpni_is_enabled(dpni, 0, priv->token, &dpni_enabled);
1260                 if (ret) {
1261                         PMD_DRV_LOG(ERR, "dpni_is_enabled failed (%d)", ret);
1262                         return ret;
1263                 }
1264                 if (dpni_enabled)
1265                         /* Allow the MC some slack */
1266                         rte_delay_us(100 * 1000);
1267         } while (dpni_enabled && --retries);
1268
1269         if (!retries) {
1270                 PMD_DRV_LOG(WARNING, "Retry count exceeded disabling DPNI\n");
1271                 /* todo- we may have to manually cleanup queues.
1272                  */
1273         } else {
1274                 PMD_DRV_LOG(INFO, "Port %d Link DOWN successful",
1275                             dev->data->port_id);
1276         }
1277
1278         dev->data->dev_link.link_status = 0;
1279
1280         return ret;
1281 }
1282
1283 static int
1284 dpaa2_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1285 {
1286         int ret = -EINVAL;
1287         struct dpaa2_dev_priv *priv;
1288         struct fsl_mc_io *dpni;
1289         struct dpni_link_state state = {0};
1290
1291         PMD_INIT_FUNC_TRACE();
1292
1293         priv = dev->data->dev_private;
1294         dpni = (struct fsl_mc_io *)priv->hw;
1295
1296         if (dpni == NULL || fc_conf == NULL) {
1297                 RTE_LOG(ERR, PMD, "device not configured\n");
1298                 return ret;
1299         }
1300
1301         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1302         if (ret) {
1303                 RTE_LOG(ERR, PMD, "error: dpni_get_link_state %d\n", ret);
1304                 return ret;
1305         }
1306
1307         memset(fc_conf, 0, sizeof(struct rte_eth_fc_conf));
1308         if (state.options & DPNI_LINK_OPT_PAUSE) {
1309                 /* DPNI_LINK_OPT_PAUSE set
1310                  *  if ASYM_PAUSE not set,
1311                  *      RX Side flow control (handle received Pause frame)
1312                  *      TX side flow control (send Pause frame)
1313                  *  if ASYM_PAUSE set,
1314                  *      RX Side flow control (handle received Pause frame)
1315                  *      No TX side flow control (send Pause frame disabled)
1316                  */
1317                 if (!(state.options & DPNI_LINK_OPT_ASYM_PAUSE))
1318                         fc_conf->mode = RTE_FC_FULL;
1319                 else
1320                         fc_conf->mode = RTE_FC_RX_PAUSE;
1321         } else {
1322                 /* DPNI_LINK_OPT_PAUSE not set
1323                  *  if ASYM_PAUSE set,
1324                  *      TX side flow control (send Pause frame)
1325                  *      No RX side flow control (No action on pause frame rx)
1326                  *  if ASYM_PAUSE not set,
1327                  *      Flow control disabled
1328                  */
1329                 if (state.options & DPNI_LINK_OPT_ASYM_PAUSE)
1330                         fc_conf->mode = RTE_FC_TX_PAUSE;
1331                 else
1332                         fc_conf->mode = RTE_FC_NONE;
1333         }
1334
1335         return ret;
1336 }
1337
1338 static int
1339 dpaa2_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1340 {
1341         int ret = -EINVAL;
1342         struct dpaa2_dev_priv *priv;
1343         struct fsl_mc_io *dpni;
1344         struct dpni_link_state state = {0};
1345         struct dpni_link_cfg cfg = {0};
1346
1347         PMD_INIT_FUNC_TRACE();
1348
1349         priv = dev->data->dev_private;
1350         dpni = (struct fsl_mc_io *)priv->hw;
1351
1352         if (dpni == NULL) {
1353                 RTE_LOG(ERR, PMD, "dpni is NULL\n");
1354                 return ret;
1355         }
1356
1357         /* It is necessary to obtain the current state before setting fc_conf
1358          * as MC would return error in case rate, autoneg or duplex values are
1359          * different.
1360          */
1361         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
1362         if (ret) {
1363                 RTE_LOG(ERR, PMD, "Unable to get link state (err=%d)\n", ret);
1364                 return -1;
1365         }
1366
1367         /* Disable link before setting configuration */
1368         dpaa2_dev_set_link_down(dev);
1369
1370         /* Based on fc_conf, update cfg */
1371         cfg.rate = state.rate;
1372         cfg.options = state.options;
1373
1374         /* update cfg with fc_conf */
1375         switch (fc_conf->mode) {
1376         case RTE_FC_FULL:
1377                 /* Full flow control;
1378                  * OPT_PAUSE set, ASYM_PAUSE not set
1379                  */
1380                 cfg.options |= DPNI_LINK_OPT_PAUSE;
1381                 cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
1382                 break;
1383         case RTE_FC_TX_PAUSE:
1384                 /* Enable RX flow control
1385                  * OPT_PAUSE not set;
1386                  * ASYM_PAUSE set;
1387                  */
1388                 cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
1389                 cfg.options &= ~DPNI_LINK_OPT_PAUSE;
1390                 break;
1391         case RTE_FC_RX_PAUSE:
1392                 /* Enable TX Flow control
1393                  * OPT_PAUSE set
1394                  * ASYM_PAUSE set
1395                  */
1396                 cfg.options |= DPNI_LINK_OPT_PAUSE;
1397                 cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
1398                 break;
1399         case RTE_FC_NONE:
1400                 /* Disable Flow control
1401                  * OPT_PAUSE not set
1402                  * ASYM_PAUSE not set
1403                  */
1404                 cfg.options &= ~DPNI_LINK_OPT_PAUSE;
1405                 cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
1406                 break;
1407         default:
1408                 RTE_LOG(ERR, PMD, "Incorrect Flow control flag (%d)\n",
1409                         fc_conf->mode);
1410                 return -1;
1411         }
1412
1413         ret = dpni_set_link_cfg(dpni, CMD_PRI_LOW, priv->token, &cfg);
1414         if (ret)
1415                 RTE_LOG(ERR, PMD,
1416                         "Unable to set Link configuration (err=%d)\n",
1417                         ret);
1418
1419         /* Enable link */
1420         dpaa2_dev_set_link_up(dev);
1421
1422         return ret;
1423 }
1424
1425 static struct eth_dev_ops dpaa2_ethdev_ops = {
1426         .dev_configure    = dpaa2_eth_dev_configure,
1427         .dev_start            = dpaa2_dev_start,
1428         .dev_stop             = dpaa2_dev_stop,
1429         .dev_close            = dpaa2_dev_close,
1430         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
1431         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
1432         .allmulticast_enable  = dpaa2_dev_allmulticast_enable,
1433         .allmulticast_disable = dpaa2_dev_allmulticast_disable,
1434         .dev_set_link_up      = dpaa2_dev_set_link_up,
1435         .dev_set_link_down    = dpaa2_dev_set_link_down,
1436         .link_update       = dpaa2_dev_link_update,
1437         .stats_get             = dpaa2_dev_stats_get,
1438         .stats_reset       = dpaa2_dev_stats_reset,
1439         .fw_version_get    = dpaa2_fw_version_get,
1440         .dev_infos_get     = dpaa2_dev_info_get,
1441         .dev_supported_ptypes_get = dpaa2_supported_ptypes_get,
1442         .mtu_set           = dpaa2_dev_mtu_set,
1443         .vlan_filter_set      = dpaa2_vlan_filter_set,
1444         .vlan_offload_set     = dpaa2_vlan_offload_set,
1445         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
1446         .rx_queue_release  = dpaa2_dev_rx_queue_release,
1447         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
1448         .tx_queue_release  = dpaa2_dev_tx_queue_release,
1449         .flow_ctrl_get        = dpaa2_flow_ctrl_get,
1450         .flow_ctrl_set        = dpaa2_flow_ctrl_set,
1451         .mac_addr_add         = dpaa2_dev_add_mac_addr,
1452         .mac_addr_remove      = dpaa2_dev_remove_mac_addr,
1453         .mac_addr_set         = dpaa2_dev_set_mac_addr,
1454 };
1455
1456 static int
1457 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
1458 {
1459         struct rte_device *dev = eth_dev->device;
1460         struct rte_dpaa2_device *dpaa2_dev;
1461         struct fsl_mc_io *dpni_dev;
1462         struct dpni_attr attr;
1463         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
1464         struct dpni_buffer_layout layout;
1465         int ret, hw_id;
1466
1467         PMD_INIT_FUNC_TRACE();
1468
1469         /* For secondary processes, the primary has done all the work */
1470         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1471                 return 0;
1472
1473         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
1474
1475         hw_id = dpaa2_dev->object_id;
1476
1477         dpni_dev = rte_malloc(NULL, sizeof(struct fsl_mc_io), 0);
1478         if (!dpni_dev) {
1479                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
1480                 return -1;
1481         }
1482
1483         dpni_dev->regs = rte_mcp_ptr_list[0];
1484         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
1485         if (ret) {
1486                 PMD_INIT_LOG(ERR,
1487                              "Failure in opening dpni@%d with err code %d\n",
1488                              hw_id, ret);
1489                 rte_free(dpni_dev);
1490                 return -1;
1491         }
1492
1493         /* Clean the device first */
1494         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
1495         if (ret) {
1496                 PMD_INIT_LOG(ERR,
1497                              "Failure cleaning dpni@%d with err code %d\n",
1498                              hw_id, ret);
1499                 goto init_err;
1500         }
1501
1502         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
1503         if (ret) {
1504                 PMD_INIT_LOG(ERR,
1505                              "Failure in get dpni@%d attribute, err code %d\n",
1506                              hw_id, ret);
1507                 goto init_err;
1508         }
1509
1510         priv->num_rx_tc = attr.num_rx_tcs;
1511
1512         /* Resetting the "num_rx_queues" to equal number of queues in first TC
1513          * as only one TC is supported on Rx Side. Once Multiple TCs will be
1514          * in use for Rx processing then this will be changed or removed.
1515          */
1516         priv->nb_rx_queues = attr.num_queues;
1517
1518         /* Using number of TX queues as number of TX TCs */
1519         priv->nb_tx_queues = attr.num_tx_tcs;
1520
1521         PMD_DRV_LOG(DEBUG, "RX-TC= %d, nb_rx_queues= %d, nb_tx_queues=%d",
1522                     priv->num_tc, priv->nb_rx_queues, priv->nb_tx_queues);
1523
1524         priv->hw = dpni_dev;
1525         priv->hw_id = hw_id;
1526         priv->options = attr.options;
1527         priv->max_mac_filters = attr.mac_filter_entries;
1528         priv->max_vlan_filters = attr.vlan_filter_entries;
1529         priv->flags = 0;
1530
1531         /* Allocate memory for hardware structure for queues */
1532         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
1533         if (ret) {
1534                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
1535                 goto init_err;
1536         }
1537
1538         /* Allocate memory for storing MAC addresses */
1539         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
1540                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
1541         if (eth_dev->data->mac_addrs == NULL) {
1542                 PMD_INIT_LOG(ERR,
1543                    "Failed to allocate %d bytes needed to store MAC addresses",
1544                              ETHER_ADDR_LEN * attr.mac_filter_entries);
1545                 ret = -ENOMEM;
1546                 goto init_err;
1547         }
1548
1549         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
1550                                         priv->token,
1551                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
1552         if (ret) {
1553                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:Err Code = %d\n",
1554                              ret);
1555                 goto init_err;
1556         }
1557
1558         /* ... tx buffer layout ... */
1559         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
1560         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
1561         layout.pass_frame_status = 1;
1562         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
1563                                      DPNI_QUEUE_TX, &layout);
1564         if (ret) {
1565                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer layout",
1566                              ret);
1567                 goto init_err;
1568         }
1569
1570         /* ... tx-conf and error buffer layout ... */
1571         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
1572         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
1573         layout.pass_frame_status = 1;
1574         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
1575                                      DPNI_QUEUE_TX_CONFIRM, &layout);
1576         if (ret) {
1577                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer layout",
1578                              ret);
1579                 goto init_err;
1580         }
1581
1582         eth_dev->dev_ops = &dpaa2_ethdev_ops;
1583         eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
1584
1585         eth_dev->rx_pkt_burst = dpaa2_dev_prefetch_rx;
1586         eth_dev->tx_pkt_burst = dpaa2_dev_tx;
1587         rte_fslmc_vfio_dmamap();
1588
1589         return 0;
1590 init_err:
1591         dpaa2_dev_uninit(eth_dev);
1592         return ret;
1593 }
1594
1595 static int
1596 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
1597 {
1598         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
1599         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1600         int i, ret;
1601         struct dpaa2_queue *dpaa2_q;
1602
1603         PMD_INIT_FUNC_TRACE();
1604
1605         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1606                 return 0;
1607
1608         if (!dpni) {
1609                 PMD_INIT_LOG(WARNING, "Already closed or not started");
1610                 return -1;
1611         }
1612
1613         dpaa2_dev_close(eth_dev);
1614
1615         if (priv->rx_vq[0]) {
1616                 /* cleaning up queue storage */
1617                 for (i = 0; i < priv->nb_rx_queues; i++) {
1618                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
1619                         if (dpaa2_q->q_storage)
1620                                 rte_free(dpaa2_q->q_storage);
1621                 }
1622                 /*free the all queue memory */
1623                 rte_free(priv->rx_vq[0]);
1624                 priv->rx_vq[0] = NULL;
1625         }
1626
1627         /* free memory for storing MAC addresses */
1628         if (eth_dev->data->mac_addrs) {
1629                 rte_free(eth_dev->data->mac_addrs);
1630                 eth_dev->data->mac_addrs = NULL;
1631         }
1632
1633         /* Close the device at underlying layer*/
1634         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
1635         if (ret) {
1636                 PMD_INIT_LOG(ERR,
1637                              "Failure closing dpni device with err code %d\n",
1638                              ret);
1639         }
1640
1641         /* Free the allocated memory for ethernet private data and dpni*/
1642         priv->hw = NULL;
1643         rte_free(dpni);
1644
1645         eth_dev->dev_ops = NULL;
1646         eth_dev->rx_pkt_burst = NULL;
1647         eth_dev->tx_pkt_burst = NULL;
1648
1649         return 0;
1650 }
1651
1652 static int
1653 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv,
1654                 struct rte_dpaa2_device *dpaa2_dev)
1655 {
1656         struct rte_eth_dev *eth_dev;
1657         int diag;
1658
1659         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1660                 eth_dev = rte_eth_dev_allocate(dpaa2_dev->device.name);
1661                 if (!eth_dev)
1662                         return -ENODEV;
1663                 eth_dev->data->dev_private = rte_zmalloc(
1664                                                 "ethdev private structure",
1665                                                 sizeof(struct dpaa2_dev_priv),
1666                                                 RTE_CACHE_LINE_SIZE);
1667                 if (eth_dev->data->dev_private == NULL) {
1668                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
1669                                      " private port data\n");
1670                         rte_eth_dev_release_port(eth_dev);
1671                         return -ENOMEM;
1672                 }
1673         } else {
1674                 eth_dev = rte_eth_dev_attach_secondary(dpaa2_dev->device.name);
1675                 if (!eth_dev)
1676                         return -ENODEV;
1677         }
1678
1679         eth_dev->device = &dpaa2_dev->device;
1680         eth_dev->device->driver = &dpaa2_drv->driver;
1681
1682         dpaa2_dev->eth_dev = eth_dev;
1683         eth_dev->data->rx_mbuf_alloc_failed = 0;
1684
1685         /* Invoke PMD device initialization function */
1686         diag = dpaa2_dev_init(eth_dev);
1687         if (diag == 0)
1688                 return 0;
1689
1690         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1691                 rte_free(eth_dev->data->dev_private);
1692         rte_eth_dev_release_port(eth_dev);
1693         return diag;
1694 }
1695
1696 static int
1697 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
1698 {
1699         struct rte_eth_dev *eth_dev;
1700
1701         eth_dev = dpaa2_dev->eth_dev;
1702         dpaa2_dev_uninit(eth_dev);
1703
1704         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1705                 rte_free(eth_dev->data->dev_private);
1706         rte_eth_dev_release_port(eth_dev);
1707
1708         return 0;
1709 }
1710
1711 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
1712         .drv_type = DPAA2_ETH,
1713         .probe = rte_dpaa2_probe,
1714         .remove = rte_dpaa2_remove,
1715 };
1716
1717 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);