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