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