80f1cd7a27a9612376a41cc30d009e4a9c65eeaa
[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
59 /**
60  * Atomically reads the link status information from global
61  * structure rte_eth_dev.
62  *
63  * @param dev
64  *   - Pointer to the structure rte_eth_dev to read from.
65  *   - Pointer to the buffer to be saved with the link status.
66  *
67  * @return
68  *   - On success, zero.
69  *   - On failure, negative value.
70  */
71 static inline int
72 dpaa2_dev_atomic_read_link_status(struct rte_eth_dev *dev,
73                                   struct rte_eth_link *link)
74 {
75         struct rte_eth_link *dst = link;
76         struct rte_eth_link *src = &dev->data->dev_link;
77
78         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
79                                 *(uint64_t *)src) == 0)
80                 return -1;
81
82         return 0;
83 }
84
85 /**
86  * Atomically writes the link status information into global
87  * structure rte_eth_dev.
88  *
89  * @param dev
90  *   - Pointer to the structure rte_eth_dev to read from.
91  *   - Pointer to the buffer to be saved with the link status.
92  *
93  * @return
94  *   - On success, zero.
95  *   - On failure, negative value.
96  */
97 static inline int
98 dpaa2_dev_atomic_write_link_status(struct rte_eth_dev *dev,
99                                    struct rte_eth_link *link)
100 {
101         struct rte_eth_link *dst = &dev->data->dev_link;
102         struct rte_eth_link *src = link;
103
104         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
105                                 *(uint64_t *)src) == 0)
106                 return -1;
107
108         return 0;
109 }
110
111 static void
112 dpaa2_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
113 {
114         struct dpaa2_dev_priv *priv = dev->data->dev_private;
115
116         PMD_INIT_FUNC_TRACE();
117
118         dev_info->if_index = priv->hw_id;
119
120         dev_info->max_mac_addrs = priv->max_mac_filters;
121         dev_info->max_rx_pktlen = DPAA2_MAX_RX_PKT_LEN;
122         dev_info->min_rx_bufsize = DPAA2_MIN_RX_BUF_SIZE;
123         dev_info->max_rx_queues = (uint16_t)priv->nb_rx_queues;
124         dev_info->max_tx_queues = (uint16_t)priv->nb_tx_queues;
125         dev_info->rx_offload_capa =
126                 DEV_RX_OFFLOAD_IPV4_CKSUM |
127                 DEV_RX_OFFLOAD_UDP_CKSUM |
128                 DEV_RX_OFFLOAD_TCP_CKSUM |
129                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM;
130         dev_info->tx_offload_capa =
131                 DEV_TX_OFFLOAD_IPV4_CKSUM |
132                 DEV_TX_OFFLOAD_UDP_CKSUM |
133                 DEV_TX_OFFLOAD_TCP_CKSUM |
134                 DEV_TX_OFFLOAD_SCTP_CKSUM |
135                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
136         dev_info->speed_capa = ETH_LINK_SPEED_1G |
137                         ETH_LINK_SPEED_2_5G |
138                         ETH_LINK_SPEED_10G;
139 }
140
141 static int
142 dpaa2_alloc_rx_tx_queues(struct rte_eth_dev *dev)
143 {
144         struct dpaa2_dev_priv *priv = dev->data->dev_private;
145         uint16_t dist_idx;
146         uint32_t vq_id;
147         struct dpaa2_queue *mc_q, *mcq;
148         uint32_t tot_queues;
149         int i;
150         struct dpaa2_queue *dpaa2_q;
151
152         PMD_INIT_FUNC_TRACE();
153
154         tot_queues = priv->nb_rx_queues + priv->nb_tx_queues;
155         mc_q = rte_malloc(NULL, sizeof(struct dpaa2_queue) * tot_queues,
156                           RTE_CACHE_LINE_SIZE);
157         if (!mc_q) {
158                 PMD_INIT_LOG(ERR, "malloc failed for rx/tx queues\n");
159                 return -1;
160         }
161
162         for (i = 0; i < priv->nb_rx_queues; i++) {
163                 mc_q->dev = dev;
164                 priv->rx_vq[i] = mc_q++;
165                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
166                 dpaa2_q->q_storage = rte_malloc("dq_storage",
167                                         sizeof(struct queue_storage_info_t),
168                                         RTE_CACHE_LINE_SIZE);
169                 if (!dpaa2_q->q_storage)
170                         goto fail;
171
172                 memset(dpaa2_q->q_storage, 0,
173                        sizeof(struct queue_storage_info_t));
174                 if (dpaa2_alloc_dq_storage(dpaa2_q->q_storage))
175                         goto fail;
176         }
177
178         for (i = 0; i < priv->nb_tx_queues; i++) {
179                 mc_q->dev = dev;
180                 mc_q->flow_id = 0xffff;
181                 priv->tx_vq[i] = mc_q++;
182                 dpaa2_q = (struct dpaa2_queue *)priv->tx_vq[i];
183                 dpaa2_q->cscn = rte_malloc(NULL,
184                                            sizeof(struct qbman_result), 16);
185                 if (!dpaa2_q->cscn)
186                         goto fail_tx;
187         }
188
189         vq_id = 0;
190         for (dist_idx = 0; dist_idx < priv->num_dist_per_tc[DPAA2_DEF_TC];
191              dist_idx++) {
192                 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
193                 mcq->tc_index = DPAA2_DEF_TC;
194                 mcq->flow_id = dist_idx;
195                 vq_id++;
196         }
197
198         return 0;
199 fail_tx:
200         i -= 1;
201         while (i >= 0) {
202                 dpaa2_q = (struct dpaa2_queue *)priv->tx_vq[i];
203                 rte_free(dpaa2_q->cscn);
204                 priv->tx_vq[i--] = NULL;
205         }
206         i = priv->nb_rx_queues;
207 fail:
208         i -= 1;
209         mc_q = priv->rx_vq[0];
210         while (i >= 0) {
211                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
212                 dpaa2_free_dq_storage(dpaa2_q->q_storage);
213                 rte_free(dpaa2_q->q_storage);
214                 priv->rx_vq[i--] = NULL;
215         }
216         rte_free(mc_q);
217         return -1;
218 }
219
220 static int
221 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
222 {
223         struct rte_eth_dev_data *data = dev->data;
224         struct rte_eth_conf *eth_conf = &data->dev_conf;
225         int ret;
226
227         PMD_INIT_FUNC_TRACE();
228
229         /* Check for correct configuration */
230         if (eth_conf->rxmode.mq_mode != ETH_MQ_RX_RSS &&
231             data->nb_rx_queues > 1) {
232                 PMD_INIT_LOG(ERR, "Distribution is not enabled, "
233                             "but Rx queues more than 1\n");
234                 return -1;
235         }
236
237         if (eth_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) {
238                 /* Return in case number of Rx queues is 1 */
239                 if (data->nb_rx_queues == 1)
240                         return 0;
241                 ret = dpaa2_setup_flow_dist(dev,
242                                 eth_conf->rx_adv_conf.rss_conf.rss_hf);
243                 if (ret) {
244                         PMD_INIT_LOG(ERR, "unable to set flow distribution."
245                                      "please check queue config\n");
246                         return ret;
247                 }
248         }
249         return 0;
250 }
251
252 /* Function to setup RX flow information. It contains traffic class ID,
253  * flow ID, destination configuration etc.
254  */
255 static int
256 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
257                          uint16_t rx_queue_id,
258                          uint16_t nb_rx_desc __rte_unused,
259                          unsigned int socket_id __rte_unused,
260                          const struct rte_eth_rxconf *rx_conf __rte_unused,
261                          struct rte_mempool *mb_pool)
262 {
263         struct dpaa2_dev_priv *priv = dev->data->dev_private;
264         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
265         struct dpaa2_queue *dpaa2_q;
266         struct dpni_queue cfg;
267         uint8_t options = 0;
268         uint8_t flow_id;
269         uint32_t bpid;
270         int ret;
271
272         PMD_INIT_FUNC_TRACE();
273
274         PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, pool = %p, conf =%p",
275                      dev, rx_queue_id, mb_pool, rx_conf);
276
277         if (!priv->bp_list || priv->bp_list->mp != mb_pool) {
278                 bpid = mempool_to_bpid(mb_pool);
279                 ret = dpaa2_attach_bp_list(priv,
280                                            rte_dpaa2_bpid_info[bpid].bp_list);
281                 if (ret)
282                         return ret;
283         }
284         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
285         dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
286
287         /*Get the tc id and flow id from given VQ id*/
288         flow_id = rx_queue_id % priv->num_dist_per_tc[dpaa2_q->tc_index];
289         memset(&cfg, 0, sizeof(struct dpni_queue));
290
291         options = options | DPNI_QUEUE_OPT_USER_CTX;
292         cfg.user_context = (uint64_t)(dpaa2_q);
293
294         /*if ls2088 or rev2 device, enable the stashing */
295         if ((qbman_get_version() & 0xFFFF0000) > QMAN_REV_4000) {
296                 options |= DPNI_QUEUE_OPT_FLC;
297                 cfg.flc.stash_control = true;
298                 cfg.flc.value &= 0xFFFFFFFFFFFFFFC0;
299                 /* 00 00 00 - last 6 bit represent annotation, context stashing,
300                  * data stashing setting 01 01 00 (0x14) to enable
301                  * 1 line data, 1 line annotation
302                  */
303                 cfg.flc.value |= 0x14;
304         }
305         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
306                              dpaa2_q->tc_index, flow_id, options, &cfg);
307         if (ret) {
308                 PMD_INIT_LOG(ERR, "Error in setting the rx flow: = %d\n", ret);
309                 return -1;
310         }
311
312         dev->data->rx_queues[rx_queue_id] = dpaa2_q;
313         return 0;
314 }
315
316 static int
317 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
318                          uint16_t tx_queue_id,
319                          uint16_t nb_tx_desc __rte_unused,
320                          unsigned int socket_id __rte_unused,
321                          const struct rte_eth_txconf *tx_conf __rte_unused)
322 {
323         struct dpaa2_dev_priv *priv = dev->data->dev_private;
324         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
325                 priv->tx_vq[tx_queue_id];
326         struct fsl_mc_io *dpni = priv->hw;
327         struct dpni_queue tx_conf_cfg;
328         struct dpni_queue tx_flow_cfg;
329         uint8_t options = 0, flow_id;
330         uint32_t tc_id;
331         int ret;
332
333         PMD_INIT_FUNC_TRACE();
334
335         /* Return if queue already configured */
336         if (dpaa2_q->flow_id != 0xffff)
337                 return 0;
338
339         memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
340         memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
341
342         if (priv->num_tc == 1) {
343                 tc_id = 0;
344                 flow_id = tx_queue_id % priv->num_dist_per_tc[tc_id];
345         } else {
346                 tc_id = tx_queue_id;
347                 flow_id = 0;
348         }
349
350         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
351                              tc_id, flow_id, options, &tx_flow_cfg);
352         if (ret) {
353                 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
354                              "tc_id=%d, flow =%d ErrorCode = %x\n",
355                              tc_id, flow_id, -ret);
356                         return -1;
357         }
358
359         dpaa2_q->flow_id = flow_id;
360
361         if (tx_queue_id == 0) {
362                 /*Set tx-conf and error configuration*/
363                 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
364                                                     priv->token,
365                                                     DPNI_CONF_DISABLE);
366                 if (ret) {
367                         PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
368                                      " ErrorCode = %x", ret);
369                         return -1;
370                 }
371         }
372         dpaa2_q->tc_index = tc_id;
373
374         if (priv->flags & DPAA2_TX_CGR_SUPPORT) {
375                 struct dpni_congestion_notification_cfg cong_notif_cfg;
376
377                 cong_notif_cfg.units = DPNI_CONGESTION_UNIT_BYTES;
378                 /* Notify about congestion when the queue size is 32 KB */
379                 cong_notif_cfg.threshold_entry = CONG_ENTER_TX_THRESHOLD;
380                 /* Notify that the queue is not congested when the data in
381                  * the queue is below this thershold.
382                  */
383                 cong_notif_cfg.threshold_exit = CONG_EXIT_TX_THRESHOLD;
384                 cong_notif_cfg.message_ctx = 0;
385                 cong_notif_cfg.message_iova = (uint64_t)dpaa2_q->cscn;
386                 cong_notif_cfg.dest_cfg.dest_type = DPNI_DEST_NONE;
387                 cong_notif_cfg.notification_mode =
388                                          DPNI_CONG_OPT_WRITE_MEM_ON_ENTER |
389                                          DPNI_CONG_OPT_WRITE_MEM_ON_EXIT |
390                                          DPNI_CONG_OPT_COHERENT_WRITE;
391
392                 ret = dpni_set_congestion_notification(dpni, CMD_PRI_LOW,
393                                                        priv->token,
394                                                        DPNI_QUEUE_TX,
395                                                        tc_id,
396                                                        &cong_notif_cfg);
397                 if (ret) {
398                         PMD_INIT_LOG(ERR,
399                            "Error in setting tx congestion notification: = %d",
400                            -ret);
401                         return -ret;
402                 }
403         }
404         dev->data->tx_queues[tx_queue_id] = dpaa2_q;
405         return 0;
406 }
407
408 static void
409 dpaa2_dev_rx_queue_release(void *q __rte_unused)
410 {
411         PMD_INIT_FUNC_TRACE();
412 }
413
414 static void
415 dpaa2_dev_tx_queue_release(void *q __rte_unused)
416 {
417         PMD_INIT_FUNC_TRACE();
418 }
419
420 static const uint32_t *
421 dpaa2_supported_ptypes_get(struct rte_eth_dev *dev)
422 {
423         static const uint32_t ptypes[] = {
424                 /*todo -= add more types */
425                 RTE_PTYPE_L2_ETHER,
426                 RTE_PTYPE_L3_IPV4,
427                 RTE_PTYPE_L3_IPV4_EXT,
428                 RTE_PTYPE_L3_IPV6,
429                 RTE_PTYPE_L3_IPV6_EXT,
430                 RTE_PTYPE_L4_TCP,
431                 RTE_PTYPE_L4_UDP,
432                 RTE_PTYPE_L4_SCTP,
433                 RTE_PTYPE_L4_ICMP,
434                 RTE_PTYPE_UNKNOWN
435         };
436
437         if (dev->rx_pkt_burst == dpaa2_dev_prefetch_rx)
438                 return ptypes;
439         return NULL;
440 }
441
442 static int
443 dpaa2_dev_start(struct rte_eth_dev *dev)
444 {
445         struct rte_eth_dev_data *data = dev->data;
446         struct dpaa2_dev_priv *priv = data->dev_private;
447         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
448         struct dpni_queue cfg;
449         struct dpni_error_cfg   err_cfg;
450         uint16_t qdid;
451         struct dpni_queue_id qid;
452         struct dpaa2_queue *dpaa2_q;
453         int ret, i;
454
455         PMD_INIT_FUNC_TRACE();
456
457         ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
458         if (ret) {
459                 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
460                              ret, priv->hw_id);
461                 return ret;
462         }
463
464         ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
465                             DPNI_QUEUE_TX, &qdid);
466         if (ret) {
467                 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
468                 return ret;
469         }
470         priv->qdid = qdid;
471
472         for (i = 0; i < data->nb_rx_queues; i++) {
473                 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
474                 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
475                                      DPNI_QUEUE_RX, dpaa2_q->tc_index,
476                                        dpaa2_q->flow_id, &cfg, &qid);
477                 if (ret) {
478                         PMD_INIT_LOG(ERR, "Error to get flow "
479                                      "information Error code = %d\n", ret);
480                         return ret;
481                 }
482                 dpaa2_q->fqid = qid.fqid;
483         }
484
485         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
486                                DPNI_OFF_RX_L3_CSUM, true);
487         if (ret) {
488                 PMD_INIT_LOG(ERR, "Error to set RX l3 csum:Error = %d\n", ret);
489                 return ret;
490         }
491
492         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
493                                DPNI_OFF_RX_L4_CSUM, true);
494         if (ret) {
495                 PMD_INIT_LOG(ERR, "Error to get RX l4 csum:Error = %d\n", ret);
496                 return ret;
497         }
498
499         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
500                                DPNI_OFF_TX_L3_CSUM, true);
501         if (ret) {
502                 PMD_INIT_LOG(ERR, "Error to set TX l3 csum:Error = %d\n", ret);
503                 return ret;
504         }
505
506         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
507                                DPNI_OFF_TX_L4_CSUM, true);
508         if (ret) {
509                 PMD_INIT_LOG(ERR, "Error to get TX l4 csum:Error = %d\n", ret);
510                 return ret;
511         }
512
513         /*checksum errors, send them to normal path and set it in annotation */
514         err_cfg.errors = DPNI_ERROR_L3CE | DPNI_ERROR_L4CE;
515
516         err_cfg.error_action = DPNI_ERROR_ACTION_CONTINUE;
517         err_cfg.set_frame_annotation = true;
518
519         ret = dpni_set_errors_behavior(dpni, CMD_PRI_LOW,
520                                        priv->token, &err_cfg);
521         if (ret) {
522                 PMD_INIT_LOG(ERR, "Error to dpni_set_errors_behavior:"
523                              "code = %d\n", ret);
524                 return ret;
525         }
526
527         return 0;
528 }
529
530 /**
531  *  This routine disables all traffic on the adapter by issuing a
532  *  global reset on the MAC.
533  */
534 static void
535 dpaa2_dev_stop(struct rte_eth_dev *dev)
536 {
537         struct dpaa2_dev_priv *priv = dev->data->dev_private;
538         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
539         int ret;
540         struct rte_eth_link link;
541
542         PMD_INIT_FUNC_TRACE();
543
544         ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
545         if (ret) {
546                 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
547                              ret, priv->hw_id);
548                 return;
549         }
550
551         /* clear the recorded link status */
552         memset(&link, 0, sizeof(link));
553         dpaa2_dev_atomic_write_link_status(dev, &link);
554 }
555
556 static void
557 dpaa2_dev_close(struct rte_eth_dev *dev)
558 {
559         struct rte_eth_dev_data *data = dev->data;
560         struct dpaa2_dev_priv *priv = dev->data->dev_private;
561         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
562         int i, ret;
563         struct dpaa2_queue *dpaa2_q;
564
565         PMD_INIT_FUNC_TRACE();
566
567         for (i = 0; i < data->nb_tx_queues; i++) {
568                 dpaa2_q = (struct dpaa2_queue *)data->tx_queues[i];
569                 if (!dpaa2_q->cscn) {
570                         rte_free(dpaa2_q->cscn);
571                         dpaa2_q->cscn = NULL;
572                 }
573         }
574
575         /* Clean the device first */
576         ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
577         if (ret) {
578                 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
579                              " error code %d\n", ret);
580                 return;
581         }
582 }
583
584 static void
585 dpaa2_dev_promiscuous_enable(
586                 struct rte_eth_dev *dev)
587 {
588         int ret;
589         struct dpaa2_dev_priv *priv = dev->data->dev_private;
590         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
591
592         PMD_INIT_FUNC_TRACE();
593
594         if (dpni == NULL) {
595                 RTE_LOG(ERR, PMD, "dpni is NULL");
596                 return;
597         }
598
599         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
600         if (ret < 0)
601                 RTE_LOG(ERR, PMD, "Unable to enable promiscuous mode %d", ret);
602 }
603
604 static void
605 dpaa2_dev_promiscuous_disable(
606                 struct rte_eth_dev *dev)
607 {
608         int ret;
609         struct dpaa2_dev_priv *priv = dev->data->dev_private;
610         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
611
612         PMD_INIT_FUNC_TRACE();
613
614         if (dpni == NULL) {
615                 RTE_LOG(ERR, PMD, "dpni is NULL");
616                 return;
617         }
618
619         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, false);
620         if (ret < 0)
621                 RTE_LOG(ERR, PMD, "Unable to disable promiscuous mode %d", ret);
622 }
623
624 static int
625 dpaa2_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
626 {
627         int ret;
628         struct dpaa2_dev_priv *priv = dev->data->dev_private;
629         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
630         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
631
632         PMD_INIT_FUNC_TRACE();
633
634         if (dpni == NULL) {
635                 RTE_LOG(ERR, PMD, "dpni is NULL");
636                 return -EINVAL;
637         }
638
639         /* check that mtu is within the allowed range */
640         if ((mtu < ETHER_MIN_MTU) || (frame_size > DPAA2_MAX_RX_PKT_LEN))
641                 return -EINVAL;
642
643         /* Set the Max Rx frame length as 'mtu' +
644          * Maximum Ethernet header length
645          */
646         ret = dpni_set_max_frame_length(dpni, CMD_PRI_LOW, priv->token,
647                                         mtu + ETH_VLAN_HLEN);
648         if (ret) {
649                 PMD_DRV_LOG(ERR, "setting the max frame length failed");
650                 return -1;
651         }
652         PMD_DRV_LOG(INFO, "MTU is configured %d for the device\n", mtu);
653         return 0;
654 }
655
656 static
657 void dpaa2_dev_stats_get(struct rte_eth_dev *dev,
658                          struct rte_eth_stats *stats)
659 {
660         struct dpaa2_dev_priv *priv = dev->data->dev_private;
661         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
662         int32_t  retcode;
663         uint8_t page0 = 0, page1 = 1, page2 = 2;
664         union dpni_statistics value;
665
666         memset(&value, 0, sizeof(union dpni_statistics));
667
668         PMD_INIT_FUNC_TRACE();
669
670         if (!dpni) {
671                 RTE_LOG(ERR, PMD, "dpni is NULL");
672                 return;
673         }
674
675         if (!stats) {
676                 RTE_LOG(ERR, PMD, "stats is NULL");
677                 return;
678         }
679
680         /*Get Counters from page_0*/
681         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
682                                       page0, &value);
683         if (retcode)
684                 goto err;
685
686         stats->ipackets = value.page_0.ingress_all_frames;
687         stats->ibytes = value.page_0.ingress_all_bytes;
688
689         /*Get Counters from page_1*/
690         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
691                                       page1, &value);
692         if (retcode)
693                 goto err;
694
695         stats->opackets = value.page_1.egress_all_frames;
696         stats->obytes = value.page_1.egress_all_bytes;
697
698         /*Get Counters from page_2*/
699         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
700                                       page2, &value);
701         if (retcode)
702                 goto err;
703
704         stats->ierrors = value.page_2.ingress_discarded_frames;
705         stats->oerrors = value.page_2.egress_discarded_frames;
706         stats->imissed = value.page_2.ingress_nobuffer_discards;
707
708         return;
709
710 err:
711         RTE_LOG(ERR, PMD, "Operation not completed:Error Code = %d\n", retcode);
712         return;
713 };
714
715 static
716 void dpaa2_dev_stats_reset(struct rte_eth_dev *dev)
717 {
718         struct dpaa2_dev_priv *priv = dev->data->dev_private;
719         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
720         int32_t  retcode;
721
722         PMD_INIT_FUNC_TRACE();
723
724         if (dpni == NULL) {
725                 RTE_LOG(ERR, PMD, "dpni is NULL");
726                 return;
727         }
728
729         retcode =  dpni_reset_statistics(dpni, CMD_PRI_LOW, priv->token);
730         if (retcode)
731                 goto error;
732
733         return;
734
735 error:
736         RTE_LOG(ERR, PMD, "Operation not completed:Error Code = %d\n", retcode);
737         return;
738 };
739
740 /* return 0 means link status changed, -1 means not changed */
741 static int
742 dpaa2_dev_link_update(struct rte_eth_dev *dev,
743                         int wait_to_complete __rte_unused)
744 {
745         int ret;
746         struct dpaa2_dev_priv *priv = dev->data->dev_private;
747         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
748         struct rte_eth_link link, old;
749         struct dpni_link_state state = {0};
750
751         PMD_INIT_FUNC_TRACE();
752
753         if (dpni == NULL) {
754                 RTE_LOG(ERR, PMD, "error : dpni is NULL");
755                 return 0;
756         }
757         memset(&old, 0, sizeof(old));
758         dpaa2_dev_atomic_read_link_status(dev, &old);
759
760         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
761         if (ret < 0) {
762                 RTE_LOG(ERR, PMD, "error: dpni_get_link_state %d", ret);
763                 return -1;
764         }
765
766         if ((old.link_status == state.up) && (old.link_speed == state.rate)) {
767                 RTE_LOG(DEBUG, PMD, "No change in status\n");
768                 return -1;
769         }
770
771         memset(&link, 0, sizeof(struct rte_eth_link));
772         link.link_status = state.up;
773         link.link_speed = state.rate;
774
775         if (state.options & DPNI_LINK_OPT_HALF_DUPLEX)
776                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
777         else
778                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
779
780         dpaa2_dev_atomic_write_link_status(dev, &link);
781
782         if (link.link_status)
783                 PMD_DRV_LOG(INFO, "Port %d Link is Up\n", dev->data->port_id);
784         else
785                 PMD_DRV_LOG(INFO, "Port %d Link is Down\n", dev->data->port_id);
786         return 0;
787 }
788
789 static struct eth_dev_ops dpaa2_ethdev_ops = {
790         .dev_configure    = dpaa2_eth_dev_configure,
791         .dev_start            = dpaa2_dev_start,
792         .dev_stop             = dpaa2_dev_stop,
793         .dev_close            = dpaa2_dev_close,
794         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
795         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
796         .link_update       = dpaa2_dev_link_update,
797         .stats_get             = dpaa2_dev_stats_get,
798         .stats_reset       = dpaa2_dev_stats_reset,
799         .dev_infos_get     = dpaa2_dev_info_get,
800         .dev_supported_ptypes_get = dpaa2_supported_ptypes_get,
801         .mtu_set           = dpaa2_dev_mtu_set,
802         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
803         .rx_queue_release  = dpaa2_dev_rx_queue_release,
804         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
805         .tx_queue_release  = dpaa2_dev_tx_queue_release,
806 };
807
808 static int
809 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
810 {
811         struct rte_device *dev = eth_dev->device;
812         struct rte_dpaa2_device *dpaa2_dev;
813         struct fsl_mc_io *dpni_dev;
814         struct dpni_attr attr;
815         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
816         struct dpni_buffer_layout layout;
817         int i, ret, hw_id;
818
819         PMD_INIT_FUNC_TRACE();
820
821         /* For secondary processes, the primary has done all the work */
822         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
823                 return 0;
824
825         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
826
827         hw_id = dpaa2_dev->object_id;
828
829         dpni_dev = rte_malloc(NULL, sizeof(struct fsl_mc_io), 0);
830         if (!dpni_dev) {
831                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
832                 return -1;
833         }
834
835         dpni_dev->regs = rte_mcp_ptr_list[0];
836         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
837         if (ret) {
838                 PMD_INIT_LOG(ERR,
839                              "Failure in opening dpni@%d with err code %d\n",
840                              hw_id, ret);
841                 rte_free(dpni_dev);
842                 return -1;
843         }
844
845         /* Clean the device first */
846         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
847         if (ret) {
848                 PMD_INIT_LOG(ERR,
849                              "Failure cleaning dpni@%d with err code %d\n",
850                              hw_id, ret);
851                 goto init_err;
852         }
853
854         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
855         if (ret) {
856                 PMD_INIT_LOG(ERR,
857                              "Failure in get dpni@%d attribute, err code %d\n",
858                              hw_id, ret);
859                 goto init_err;
860         }
861
862         priv->num_tc = attr.num_tcs;
863         for (i = 0; i < attr.num_tcs; i++) {
864                 priv->num_dist_per_tc[i] = attr.num_queues;
865                 break;
866         }
867
868         /* Distribution is per Tc only,
869          * so choosing RX queues from default TC only
870          */
871         priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
872
873         if (attr.num_tcs == 1)
874                 priv->nb_tx_queues = attr.num_queues;
875         else
876                 priv->nb_tx_queues = attr.num_tcs;
877
878         PMD_INIT_LOG(DEBUG, "num_tc %d", priv->num_tc);
879         PMD_INIT_LOG(DEBUG, "nb_rx_queues %d", priv->nb_rx_queues);
880
881         priv->hw = dpni_dev;
882         priv->hw_id = hw_id;
883         priv->options = attr.options;
884         priv->max_mac_filters = attr.mac_filter_entries;
885         priv->max_vlan_filters = attr.vlan_filter_entries;
886         priv->flags = 0;
887
888         priv->flags |= DPAA2_TX_CGR_SUPPORT;
889         PMD_INIT_LOG(INFO, "Enable the tx congestion control support");
890
891         /* Allocate memory for hardware structure for queues */
892         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
893         if (ret) {
894                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
895                 goto init_err;
896         }
897
898         /* Allocate memory for storing MAC addresses */
899         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
900                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
901         if (eth_dev->data->mac_addrs == NULL) {
902                 PMD_INIT_LOG(ERR,
903                    "Failed to allocate %d bytes needed to store MAC addresses",
904                              ETHER_ADDR_LEN * attr.mac_filter_entries);
905                 ret = -ENOMEM;
906                 goto init_err;
907         }
908
909         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
910                                         priv->token,
911                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
912         if (ret) {
913                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:Err Code = %d\n",
914                              ret);
915                 goto init_err;
916         }
917
918         /* ... tx buffer layout ... */
919         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
920         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
921         layout.pass_frame_status = 1;
922         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
923                                      DPNI_QUEUE_TX, &layout);
924         if (ret) {
925                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer layout",
926                              ret);
927                 goto init_err;
928         }
929
930         /* ... tx-conf and error buffer layout ... */
931         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
932         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
933         layout.pass_frame_status = 1;
934         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
935                                      DPNI_QUEUE_TX_CONFIRM, &layout);
936         if (ret) {
937                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer layout",
938                              ret);
939                 goto init_err;
940         }
941
942         eth_dev->dev_ops = &dpaa2_ethdev_ops;
943         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
944
945         eth_dev->rx_pkt_burst = dpaa2_dev_prefetch_rx;
946         eth_dev->tx_pkt_burst = dpaa2_dev_tx;
947         rte_fslmc_vfio_dmamap();
948
949         return 0;
950 init_err:
951         dpaa2_dev_uninit(eth_dev);
952         return ret;
953 }
954
955 static int
956 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
957 {
958         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
959         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
960         int i, ret;
961         struct dpaa2_queue *dpaa2_q;
962
963         PMD_INIT_FUNC_TRACE();
964
965         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
966                 return -EPERM;
967
968         if (!dpni) {
969                 PMD_INIT_LOG(WARNING, "Already closed or not started");
970                 return -1;
971         }
972
973         dpaa2_dev_close(eth_dev);
974
975         if (priv->rx_vq[0]) {
976                 /* cleaning up queue storage */
977                 for (i = 0; i < priv->nb_rx_queues; i++) {
978                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
979                         if (dpaa2_q->q_storage)
980                                 rte_free(dpaa2_q->q_storage);
981                 }
982                 /*free the all queue memory */
983                 rte_free(priv->rx_vq[0]);
984                 priv->rx_vq[0] = NULL;
985         }
986
987         /* free memory for storing MAC addresses */
988         if (eth_dev->data->mac_addrs) {
989                 rte_free(eth_dev->data->mac_addrs);
990                 eth_dev->data->mac_addrs = NULL;
991         }
992
993         /* Close the device at underlying layer*/
994         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
995         if (ret) {
996                 PMD_INIT_LOG(ERR,
997                              "Failure closing dpni device with err code %d\n",
998                              ret);
999         }
1000
1001         /* Free the allocated memory for ethernet private data and dpni*/
1002         priv->hw = NULL;
1003         rte_free(dpni);
1004
1005         eth_dev->dev_ops = NULL;
1006         eth_dev->rx_pkt_burst = NULL;
1007         eth_dev->tx_pkt_burst = NULL;
1008
1009         return 0;
1010 }
1011
1012 static int
1013 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
1014                 struct rte_dpaa2_device *dpaa2_dev)
1015 {
1016         struct rte_eth_dev *eth_dev;
1017         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
1018
1019         int diag;
1020
1021         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
1022
1023         eth_dev = rte_eth_dev_allocate(ethdev_name);
1024         if (eth_dev == NULL)
1025                 return -ENOMEM;
1026
1027         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1028                 eth_dev->data->dev_private = rte_zmalloc(
1029                                                 "ethdev private structure",
1030                                                 sizeof(struct dpaa2_dev_priv),
1031                                                 RTE_CACHE_LINE_SIZE);
1032                 if (eth_dev->data->dev_private == NULL) {
1033                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
1034                                      " private port data\n");
1035                         rte_eth_dev_release_port(eth_dev);
1036                         return -ENOMEM;
1037                 }
1038         }
1039         eth_dev->device = &dpaa2_dev->device;
1040         dpaa2_dev->eth_dev = eth_dev;
1041         eth_dev->data->rx_mbuf_alloc_failed = 0;
1042
1043         /* Invoke PMD device initialization function */
1044         diag = dpaa2_dev_init(eth_dev);
1045         if (diag == 0)
1046                 return 0;
1047
1048         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1049                 rte_free(eth_dev->data->dev_private);
1050         rte_eth_dev_release_port(eth_dev);
1051         return diag;
1052 }
1053
1054 static int
1055 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
1056 {
1057         struct rte_eth_dev *eth_dev;
1058
1059         eth_dev = dpaa2_dev->eth_dev;
1060         dpaa2_dev_uninit(eth_dev);
1061
1062         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1063                 rte_free(eth_dev->data->dev_private);
1064         rte_eth_dev_release_port(eth_dev);
1065
1066         return 0;
1067 }
1068
1069 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
1070         .drv_type = DPAA2_MC_DPNI_DEVID,
1071         .probe = rte_dpaa2_probe,
1072         .remove = rte_dpaa2_remove,
1073 };
1074
1075 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);