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