net/dpaa2: add support for MAC address filtering
[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         if (!(priv->flags & DPAA2_RX_TAILDROP_OFF)) {
313                 struct dpni_taildrop taildrop;
314
315                 taildrop.enable = 1;
316                 /*enabling per rx queue congestion control */
317                 taildrop.threshold = CONG_THRESHOLD_RX_Q;
318                 taildrop.units = DPNI_CONGESTION_UNIT_BYTES;
319                 PMD_INIT_LOG(DEBUG, "Enabling Early Drop on queue = %d",
320                              rx_queue_id);
321                 ret = dpni_set_taildrop(dpni, CMD_PRI_LOW, priv->token,
322                                         DPNI_CP_QUEUE, DPNI_QUEUE_RX,
323                                         dpaa2_q->tc_index, flow_id, &taildrop);
324                 if (ret) {
325                         PMD_INIT_LOG(ERR, "Error in setting the rx flow"
326                                      " err : = %d\n", ret);
327                         return -1;
328                 }
329         }
330
331         dev->data->rx_queues[rx_queue_id] = dpaa2_q;
332         return 0;
333 }
334
335 static int
336 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
337                          uint16_t tx_queue_id,
338                          uint16_t nb_tx_desc __rte_unused,
339                          unsigned int socket_id __rte_unused,
340                          const struct rte_eth_txconf *tx_conf __rte_unused)
341 {
342         struct dpaa2_dev_priv *priv = dev->data->dev_private;
343         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
344                 priv->tx_vq[tx_queue_id];
345         struct fsl_mc_io *dpni = priv->hw;
346         struct dpni_queue tx_conf_cfg;
347         struct dpni_queue tx_flow_cfg;
348         uint8_t options = 0, flow_id;
349         uint32_t tc_id;
350         int ret;
351
352         PMD_INIT_FUNC_TRACE();
353
354         /* Return if queue already configured */
355         if (dpaa2_q->flow_id != 0xffff)
356                 return 0;
357
358         memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
359         memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
360
361         if (priv->num_tc == 1) {
362                 tc_id = 0;
363                 flow_id = tx_queue_id % priv->num_dist_per_tc[tc_id];
364         } else {
365                 tc_id = tx_queue_id;
366                 flow_id = 0;
367         }
368
369         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
370                              tc_id, flow_id, options, &tx_flow_cfg);
371         if (ret) {
372                 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
373                              "tc_id=%d, flow =%d ErrorCode = %x\n",
374                              tc_id, flow_id, -ret);
375                         return -1;
376         }
377
378         dpaa2_q->flow_id = flow_id;
379
380         if (tx_queue_id == 0) {
381                 /*Set tx-conf and error configuration*/
382                 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
383                                                     priv->token,
384                                                     DPNI_CONF_DISABLE);
385                 if (ret) {
386                         PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
387                                      " ErrorCode = %x", ret);
388                         return -1;
389                 }
390         }
391         dpaa2_q->tc_index = tc_id;
392
393         if (priv->flags & DPAA2_TX_CGR_SUPPORT) {
394                 struct dpni_congestion_notification_cfg cong_notif_cfg;
395
396                 cong_notif_cfg.units = DPNI_CONGESTION_UNIT_BYTES;
397                 /* Notify about congestion when the queue size is 32 KB */
398                 cong_notif_cfg.threshold_entry = CONG_ENTER_TX_THRESHOLD;
399                 /* Notify that the queue is not congested when the data in
400                  * the queue is below this thershold.
401                  */
402                 cong_notif_cfg.threshold_exit = CONG_EXIT_TX_THRESHOLD;
403                 cong_notif_cfg.message_ctx = 0;
404                 cong_notif_cfg.message_iova = (uint64_t)dpaa2_q->cscn;
405                 cong_notif_cfg.dest_cfg.dest_type = DPNI_DEST_NONE;
406                 cong_notif_cfg.notification_mode =
407                                          DPNI_CONG_OPT_WRITE_MEM_ON_ENTER |
408                                          DPNI_CONG_OPT_WRITE_MEM_ON_EXIT |
409                                          DPNI_CONG_OPT_COHERENT_WRITE;
410
411                 ret = dpni_set_congestion_notification(dpni, CMD_PRI_LOW,
412                                                        priv->token,
413                                                        DPNI_QUEUE_TX,
414                                                        tc_id,
415                                                        &cong_notif_cfg);
416                 if (ret) {
417                         PMD_INIT_LOG(ERR,
418                            "Error in setting tx congestion notification: = %d",
419                            -ret);
420                         return -ret;
421                 }
422         }
423         dev->data->tx_queues[tx_queue_id] = dpaa2_q;
424         return 0;
425 }
426
427 static void
428 dpaa2_dev_rx_queue_release(void *q __rte_unused)
429 {
430         PMD_INIT_FUNC_TRACE();
431 }
432
433 static void
434 dpaa2_dev_tx_queue_release(void *q __rte_unused)
435 {
436         PMD_INIT_FUNC_TRACE();
437 }
438
439 static const uint32_t *
440 dpaa2_supported_ptypes_get(struct rte_eth_dev *dev)
441 {
442         static const uint32_t ptypes[] = {
443                 /*todo -= add more types */
444                 RTE_PTYPE_L2_ETHER,
445                 RTE_PTYPE_L3_IPV4,
446                 RTE_PTYPE_L3_IPV4_EXT,
447                 RTE_PTYPE_L3_IPV6,
448                 RTE_PTYPE_L3_IPV6_EXT,
449                 RTE_PTYPE_L4_TCP,
450                 RTE_PTYPE_L4_UDP,
451                 RTE_PTYPE_L4_SCTP,
452                 RTE_PTYPE_L4_ICMP,
453                 RTE_PTYPE_UNKNOWN
454         };
455
456         if (dev->rx_pkt_burst == dpaa2_dev_prefetch_rx)
457                 return ptypes;
458         return NULL;
459 }
460
461 static int
462 dpaa2_dev_start(struct rte_eth_dev *dev)
463 {
464         struct rte_eth_dev_data *data = dev->data;
465         struct dpaa2_dev_priv *priv = data->dev_private;
466         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
467         struct dpni_queue cfg;
468         struct dpni_error_cfg   err_cfg;
469         uint16_t qdid;
470         struct dpni_queue_id qid;
471         struct dpaa2_queue *dpaa2_q;
472         int ret, i;
473
474         PMD_INIT_FUNC_TRACE();
475
476         ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
477         if (ret) {
478                 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
479                              ret, priv->hw_id);
480                 return ret;
481         }
482
483         ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
484                             DPNI_QUEUE_TX, &qdid);
485         if (ret) {
486                 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
487                 return ret;
488         }
489         priv->qdid = qdid;
490
491         for (i = 0; i < data->nb_rx_queues; i++) {
492                 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
493                 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
494                                      DPNI_QUEUE_RX, dpaa2_q->tc_index,
495                                        dpaa2_q->flow_id, &cfg, &qid);
496                 if (ret) {
497                         PMD_INIT_LOG(ERR, "Error to get flow "
498                                      "information Error code = %d\n", ret);
499                         return ret;
500                 }
501                 dpaa2_q->fqid = qid.fqid;
502         }
503
504         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
505                                DPNI_OFF_RX_L3_CSUM, true);
506         if (ret) {
507                 PMD_INIT_LOG(ERR, "Error to set RX l3 csum:Error = %d\n", ret);
508                 return ret;
509         }
510
511         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
512                                DPNI_OFF_RX_L4_CSUM, true);
513         if (ret) {
514                 PMD_INIT_LOG(ERR, "Error to get RX l4 csum:Error = %d\n", ret);
515                 return ret;
516         }
517
518         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
519                                DPNI_OFF_TX_L3_CSUM, true);
520         if (ret) {
521                 PMD_INIT_LOG(ERR, "Error to set TX l3 csum:Error = %d\n", ret);
522                 return ret;
523         }
524
525         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
526                                DPNI_OFF_TX_L4_CSUM, true);
527         if (ret) {
528                 PMD_INIT_LOG(ERR, "Error to get TX l4 csum:Error = %d\n", ret);
529                 return ret;
530         }
531
532         /*checksum errors, send them to normal path and set it in annotation */
533         err_cfg.errors = DPNI_ERROR_L3CE | DPNI_ERROR_L4CE;
534
535         err_cfg.error_action = DPNI_ERROR_ACTION_CONTINUE;
536         err_cfg.set_frame_annotation = true;
537
538         ret = dpni_set_errors_behavior(dpni, CMD_PRI_LOW,
539                                        priv->token, &err_cfg);
540         if (ret) {
541                 PMD_INIT_LOG(ERR, "Error to dpni_set_errors_behavior:"
542                              "code = %d\n", ret);
543                 return ret;
544         }
545
546         return 0;
547 }
548
549 /**
550  *  This routine disables all traffic on the adapter by issuing a
551  *  global reset on the MAC.
552  */
553 static void
554 dpaa2_dev_stop(struct rte_eth_dev *dev)
555 {
556         struct dpaa2_dev_priv *priv = dev->data->dev_private;
557         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
558         int ret;
559         struct rte_eth_link link;
560
561         PMD_INIT_FUNC_TRACE();
562
563         ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
564         if (ret) {
565                 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
566                              ret, priv->hw_id);
567                 return;
568         }
569
570         /* clear the recorded link status */
571         memset(&link, 0, sizeof(link));
572         dpaa2_dev_atomic_write_link_status(dev, &link);
573 }
574
575 static void
576 dpaa2_dev_close(struct rte_eth_dev *dev)
577 {
578         struct rte_eth_dev_data *data = dev->data;
579         struct dpaa2_dev_priv *priv = dev->data->dev_private;
580         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
581         int i, ret;
582         struct dpaa2_queue *dpaa2_q;
583
584         PMD_INIT_FUNC_TRACE();
585
586         for (i = 0; i < data->nb_tx_queues; i++) {
587                 dpaa2_q = (struct dpaa2_queue *)data->tx_queues[i];
588                 if (!dpaa2_q->cscn) {
589                         rte_free(dpaa2_q->cscn);
590                         dpaa2_q->cscn = NULL;
591                 }
592         }
593
594         /* Clean the device first */
595         ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
596         if (ret) {
597                 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
598                              " error code %d\n", ret);
599                 return;
600         }
601 }
602
603 static void
604 dpaa2_dev_promiscuous_enable(
605                 struct rte_eth_dev *dev)
606 {
607         int ret;
608         struct dpaa2_dev_priv *priv = dev->data->dev_private;
609         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
610
611         PMD_INIT_FUNC_TRACE();
612
613         if (dpni == NULL) {
614                 RTE_LOG(ERR, PMD, "dpni is NULL");
615                 return;
616         }
617
618         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
619         if (ret < 0)
620                 RTE_LOG(ERR, PMD, "Unable to enable promiscuous mode %d", ret);
621 }
622
623 static void
624 dpaa2_dev_promiscuous_disable(
625                 struct rte_eth_dev *dev)
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
631         PMD_INIT_FUNC_TRACE();
632
633         if (dpni == NULL) {
634                 RTE_LOG(ERR, PMD, "dpni is NULL");
635                 return;
636         }
637
638         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, false);
639         if (ret < 0)
640                 RTE_LOG(ERR, PMD, "Unable to disable promiscuous mode %d", ret);
641 }
642
643 static int
644 dpaa2_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
645 {
646         int ret;
647         struct dpaa2_dev_priv *priv = dev->data->dev_private;
648         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
649         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
650
651         PMD_INIT_FUNC_TRACE();
652
653         if (dpni == NULL) {
654                 RTE_LOG(ERR, PMD, "dpni is NULL");
655                 return -EINVAL;
656         }
657
658         /* check that mtu is within the allowed range */
659         if ((mtu < ETHER_MIN_MTU) || (frame_size > DPAA2_MAX_RX_PKT_LEN))
660                 return -EINVAL;
661
662         /* Set the Max Rx frame length as 'mtu' +
663          * Maximum Ethernet header length
664          */
665         ret = dpni_set_max_frame_length(dpni, CMD_PRI_LOW, priv->token,
666                                         mtu + ETH_VLAN_HLEN);
667         if (ret) {
668                 PMD_DRV_LOG(ERR, "setting the max frame length failed");
669                 return -1;
670         }
671         PMD_DRV_LOG(INFO, "MTU is configured %d for the device\n", mtu);
672         return 0;
673 }
674
675 static int
676 dpaa2_dev_add_mac_addr(struct rte_eth_dev *dev,
677                        struct ether_addr *addr,
678                        __rte_unused uint32_t index,
679                        __rte_unused uint32_t pool)
680 {
681         int ret;
682         struct dpaa2_dev_priv *priv = dev->data->dev_private;
683         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
684
685         PMD_INIT_FUNC_TRACE();
686
687         if (dpni == NULL) {
688                 RTE_LOG(ERR, PMD, "dpni is NULL");
689                 return -1;
690         }
691
692         ret = dpni_add_mac_addr(dpni, CMD_PRI_LOW,
693                                 priv->token, addr->addr_bytes);
694         if (ret)
695                 RTE_LOG(ERR, PMD, "error: Adding the MAC ADDR failed:"
696                         " err = %d", ret);
697         return 0;
698 }
699
700 static void
701 dpaa2_dev_remove_mac_addr(struct rte_eth_dev *dev,
702                           uint32_t index)
703 {
704         int ret;
705         struct dpaa2_dev_priv *priv = dev->data->dev_private;
706         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
707         struct rte_eth_dev_data *data = dev->data;
708         struct ether_addr *macaddr;
709
710         PMD_INIT_FUNC_TRACE();
711
712         macaddr = &data->mac_addrs[index];
713
714         if (dpni == NULL) {
715                 RTE_LOG(ERR, PMD, "dpni is NULL");
716                 return;
717         }
718
719         ret = dpni_remove_mac_addr(dpni, CMD_PRI_LOW,
720                                    priv->token, macaddr->addr_bytes);
721         if (ret)
722                 RTE_LOG(ERR, PMD, "error: Removing the MAC ADDR failed:"
723                         " err = %d", ret);
724 }
725
726 static void
727 dpaa2_dev_set_mac_addr(struct rte_eth_dev *dev,
728                        struct ether_addr *addr)
729 {
730         int ret;
731         struct dpaa2_dev_priv *priv = dev->data->dev_private;
732         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
733
734         PMD_INIT_FUNC_TRACE();
735
736         if (dpni == NULL) {
737                 RTE_LOG(ERR, PMD, "dpni is NULL");
738                 return;
739         }
740
741         ret = dpni_set_primary_mac_addr(dpni, CMD_PRI_LOW,
742                                         priv->token, addr->addr_bytes);
743
744         if (ret)
745                 RTE_LOG(ERR, PMD, "error: Setting the MAC ADDR failed %d", ret);
746 }
747 static
748 void dpaa2_dev_stats_get(struct rte_eth_dev *dev,
749                          struct rte_eth_stats *stats)
750 {
751         struct dpaa2_dev_priv *priv = dev->data->dev_private;
752         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
753         int32_t  retcode;
754         uint8_t page0 = 0, page1 = 1, page2 = 2;
755         union dpni_statistics value;
756
757         memset(&value, 0, sizeof(union dpni_statistics));
758
759         PMD_INIT_FUNC_TRACE();
760
761         if (!dpni) {
762                 RTE_LOG(ERR, PMD, "dpni is NULL");
763                 return;
764         }
765
766         if (!stats) {
767                 RTE_LOG(ERR, PMD, "stats is NULL");
768                 return;
769         }
770
771         /*Get Counters from page_0*/
772         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
773                                       page0, &value);
774         if (retcode)
775                 goto err;
776
777         stats->ipackets = value.page_0.ingress_all_frames;
778         stats->ibytes = value.page_0.ingress_all_bytes;
779
780         /*Get Counters from page_1*/
781         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
782                                       page1, &value);
783         if (retcode)
784                 goto err;
785
786         stats->opackets = value.page_1.egress_all_frames;
787         stats->obytes = value.page_1.egress_all_bytes;
788
789         /*Get Counters from page_2*/
790         retcode = dpni_get_statistics(dpni, CMD_PRI_LOW, priv->token,
791                                       page2, &value);
792         if (retcode)
793                 goto err;
794
795         /* Ingress drop frame count due to configured rules */
796         stats->ierrors = value.page_2.ingress_filtered_frames;
797         /* Ingress drop frame count due to error */
798         stats->ierrors += value.page_2.ingress_discarded_frames;
799
800         stats->oerrors = value.page_2.egress_discarded_frames;
801         stats->imissed = value.page_2.ingress_nobuffer_discards;
802
803         return;
804
805 err:
806         RTE_LOG(ERR, PMD, "Operation not completed:Error Code = %d\n", retcode);
807         return;
808 };
809
810 static
811 void dpaa2_dev_stats_reset(struct rte_eth_dev *dev)
812 {
813         struct dpaa2_dev_priv *priv = dev->data->dev_private;
814         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
815         int32_t  retcode;
816
817         PMD_INIT_FUNC_TRACE();
818
819         if (dpni == NULL) {
820                 RTE_LOG(ERR, PMD, "dpni is NULL");
821                 return;
822         }
823
824         retcode =  dpni_reset_statistics(dpni, CMD_PRI_LOW, priv->token);
825         if (retcode)
826                 goto error;
827
828         return;
829
830 error:
831         RTE_LOG(ERR, PMD, "Operation not completed:Error Code = %d\n", retcode);
832         return;
833 };
834
835 /* return 0 means link status changed, -1 means not changed */
836 static int
837 dpaa2_dev_link_update(struct rte_eth_dev *dev,
838                         int wait_to_complete __rte_unused)
839 {
840         int ret;
841         struct dpaa2_dev_priv *priv = dev->data->dev_private;
842         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
843         struct rte_eth_link link, old;
844         struct dpni_link_state state = {0};
845
846         PMD_INIT_FUNC_TRACE();
847
848         if (dpni == NULL) {
849                 RTE_LOG(ERR, PMD, "error : dpni is NULL");
850                 return 0;
851         }
852         memset(&old, 0, sizeof(old));
853         dpaa2_dev_atomic_read_link_status(dev, &old);
854
855         ret = dpni_get_link_state(dpni, CMD_PRI_LOW, priv->token, &state);
856         if (ret < 0) {
857                 RTE_LOG(ERR, PMD, "error: dpni_get_link_state %d", ret);
858                 return -1;
859         }
860
861         if ((old.link_status == state.up) && (old.link_speed == state.rate)) {
862                 RTE_LOG(DEBUG, PMD, "No change in status\n");
863                 return -1;
864         }
865
866         memset(&link, 0, sizeof(struct rte_eth_link));
867         link.link_status = state.up;
868         link.link_speed = state.rate;
869
870         if (state.options & DPNI_LINK_OPT_HALF_DUPLEX)
871                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
872         else
873                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
874
875         dpaa2_dev_atomic_write_link_status(dev, &link);
876
877         if (link.link_status)
878                 PMD_DRV_LOG(INFO, "Port %d Link is Up\n", dev->data->port_id);
879         else
880                 PMD_DRV_LOG(INFO, "Port %d Link is Down\n", dev->data->port_id);
881         return 0;
882 }
883
884 static struct eth_dev_ops dpaa2_ethdev_ops = {
885         .dev_configure    = dpaa2_eth_dev_configure,
886         .dev_start            = dpaa2_dev_start,
887         .dev_stop             = dpaa2_dev_stop,
888         .dev_close            = dpaa2_dev_close,
889         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
890         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
891         .link_update       = dpaa2_dev_link_update,
892         .stats_get             = dpaa2_dev_stats_get,
893         .stats_reset       = dpaa2_dev_stats_reset,
894         .dev_infos_get     = dpaa2_dev_info_get,
895         .dev_supported_ptypes_get = dpaa2_supported_ptypes_get,
896         .mtu_set           = dpaa2_dev_mtu_set,
897         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
898         .rx_queue_release  = dpaa2_dev_rx_queue_release,
899         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
900         .tx_queue_release  = dpaa2_dev_tx_queue_release,
901         .mac_addr_add         = dpaa2_dev_add_mac_addr,
902         .mac_addr_remove      = dpaa2_dev_remove_mac_addr,
903         .mac_addr_set         = dpaa2_dev_set_mac_addr,
904 };
905
906 static int
907 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
908 {
909         struct rte_device *dev = eth_dev->device;
910         struct rte_dpaa2_device *dpaa2_dev;
911         struct fsl_mc_io *dpni_dev;
912         struct dpni_attr attr;
913         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
914         struct dpni_buffer_layout layout;
915         int i, ret, hw_id;
916
917         PMD_INIT_FUNC_TRACE();
918
919         /* For secondary processes, the primary has done all the work */
920         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
921                 return 0;
922
923         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
924
925         hw_id = dpaa2_dev->object_id;
926
927         dpni_dev = rte_malloc(NULL, sizeof(struct fsl_mc_io), 0);
928         if (!dpni_dev) {
929                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
930                 return -1;
931         }
932
933         dpni_dev->regs = rte_mcp_ptr_list[0];
934         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
935         if (ret) {
936                 PMD_INIT_LOG(ERR,
937                              "Failure in opening dpni@%d with err code %d\n",
938                              hw_id, ret);
939                 rte_free(dpni_dev);
940                 return -1;
941         }
942
943         /* Clean the device first */
944         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
945         if (ret) {
946                 PMD_INIT_LOG(ERR,
947                              "Failure cleaning dpni@%d with err code %d\n",
948                              hw_id, ret);
949                 goto init_err;
950         }
951
952         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
953         if (ret) {
954                 PMD_INIT_LOG(ERR,
955                              "Failure in get dpni@%d attribute, err code %d\n",
956                              hw_id, ret);
957                 goto init_err;
958         }
959
960         priv->num_tc = attr.num_tcs;
961         for (i = 0; i < attr.num_tcs; i++) {
962                 priv->num_dist_per_tc[i] = attr.num_queues;
963                 break;
964         }
965
966         /* Distribution is per Tc only,
967          * so choosing RX queues from default TC only
968          */
969         priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
970
971         if (attr.num_tcs == 1)
972                 priv->nb_tx_queues = attr.num_queues;
973         else
974                 priv->nb_tx_queues = attr.num_tcs;
975
976         PMD_INIT_LOG(DEBUG, "num_tc %d", priv->num_tc);
977         PMD_INIT_LOG(DEBUG, "nb_rx_queues %d", priv->nb_rx_queues);
978
979         priv->hw = dpni_dev;
980         priv->hw_id = hw_id;
981         priv->options = attr.options;
982         priv->max_mac_filters = attr.mac_filter_entries;
983         priv->max_vlan_filters = attr.vlan_filter_entries;
984         priv->flags = 0;
985
986         priv->flags |= DPAA2_TX_CGR_SUPPORT;
987         PMD_INIT_LOG(INFO, "Enable the tx congestion control support");
988
989         /* Allocate memory for hardware structure for queues */
990         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
991         if (ret) {
992                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
993                 goto init_err;
994         }
995
996         /* Allocate memory for storing MAC addresses */
997         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
998                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
999         if (eth_dev->data->mac_addrs == NULL) {
1000                 PMD_INIT_LOG(ERR,
1001                    "Failed to allocate %d bytes needed to store MAC addresses",
1002                              ETHER_ADDR_LEN * attr.mac_filter_entries);
1003                 ret = -ENOMEM;
1004                 goto init_err;
1005         }
1006
1007         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
1008                                         priv->token,
1009                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
1010         if (ret) {
1011                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:Err Code = %d\n",
1012                              ret);
1013                 goto init_err;
1014         }
1015
1016         /* ... tx buffer layout ... */
1017         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
1018         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
1019         layout.pass_frame_status = 1;
1020         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
1021                                      DPNI_QUEUE_TX, &layout);
1022         if (ret) {
1023                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer layout",
1024                              ret);
1025                 goto init_err;
1026         }
1027
1028         /* ... tx-conf and error buffer layout ... */
1029         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
1030         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
1031         layout.pass_frame_status = 1;
1032         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
1033                                      DPNI_QUEUE_TX_CONFIRM, &layout);
1034         if (ret) {
1035                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer layout",
1036                              ret);
1037                 goto init_err;
1038         }
1039
1040         eth_dev->dev_ops = &dpaa2_ethdev_ops;
1041         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
1042
1043         eth_dev->rx_pkt_burst = dpaa2_dev_prefetch_rx;
1044         eth_dev->tx_pkt_burst = dpaa2_dev_tx;
1045         rte_fslmc_vfio_dmamap();
1046
1047         return 0;
1048 init_err:
1049         dpaa2_dev_uninit(eth_dev);
1050         return ret;
1051 }
1052
1053 static int
1054 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
1055 {
1056         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
1057         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
1058         int i, ret;
1059         struct dpaa2_queue *dpaa2_q;
1060
1061         PMD_INIT_FUNC_TRACE();
1062
1063         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1064                 return -EPERM;
1065
1066         if (!dpni) {
1067                 PMD_INIT_LOG(WARNING, "Already closed or not started");
1068                 return -1;
1069         }
1070
1071         dpaa2_dev_close(eth_dev);
1072
1073         if (priv->rx_vq[0]) {
1074                 /* cleaning up queue storage */
1075                 for (i = 0; i < priv->nb_rx_queues; i++) {
1076                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
1077                         if (dpaa2_q->q_storage)
1078                                 rte_free(dpaa2_q->q_storage);
1079                 }
1080                 /*free the all queue memory */
1081                 rte_free(priv->rx_vq[0]);
1082                 priv->rx_vq[0] = NULL;
1083         }
1084
1085         /* free memory for storing MAC addresses */
1086         if (eth_dev->data->mac_addrs) {
1087                 rte_free(eth_dev->data->mac_addrs);
1088                 eth_dev->data->mac_addrs = NULL;
1089         }
1090
1091         /* Close the device at underlying layer*/
1092         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
1093         if (ret) {
1094                 PMD_INIT_LOG(ERR,
1095                              "Failure closing dpni device with err code %d\n",
1096                              ret);
1097         }
1098
1099         /* Free the allocated memory for ethernet private data and dpni*/
1100         priv->hw = NULL;
1101         rte_free(dpni);
1102
1103         eth_dev->dev_ops = NULL;
1104         eth_dev->rx_pkt_burst = NULL;
1105         eth_dev->tx_pkt_burst = NULL;
1106
1107         return 0;
1108 }
1109
1110 static int
1111 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
1112                 struct rte_dpaa2_device *dpaa2_dev)
1113 {
1114         struct rte_eth_dev *eth_dev;
1115         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
1116
1117         int diag;
1118
1119         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
1120
1121         eth_dev = rte_eth_dev_allocate(ethdev_name);
1122         if (eth_dev == NULL)
1123                 return -ENOMEM;
1124
1125         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1126                 eth_dev->data->dev_private = rte_zmalloc(
1127                                                 "ethdev private structure",
1128                                                 sizeof(struct dpaa2_dev_priv),
1129                                                 RTE_CACHE_LINE_SIZE);
1130                 if (eth_dev->data->dev_private == NULL) {
1131                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
1132                                      " private port data\n");
1133                         rte_eth_dev_release_port(eth_dev);
1134                         return -ENOMEM;
1135                 }
1136         }
1137         eth_dev->device = &dpaa2_dev->device;
1138         dpaa2_dev->eth_dev = eth_dev;
1139         eth_dev->data->rx_mbuf_alloc_failed = 0;
1140
1141         /* Invoke PMD device initialization function */
1142         diag = dpaa2_dev_init(eth_dev);
1143         if (diag == 0)
1144                 return 0;
1145
1146         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1147                 rte_free(eth_dev->data->dev_private);
1148         rte_eth_dev_release_port(eth_dev);
1149         return diag;
1150 }
1151
1152 static int
1153 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
1154 {
1155         struct rte_eth_dev *eth_dev;
1156
1157         eth_dev = dpaa2_dev->eth_dev;
1158         dpaa2_dev_uninit(eth_dev);
1159
1160         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1161                 rte_free(eth_dev->data->dev_private);
1162         rte_eth_dev_release_port(eth_dev);
1163
1164         return 0;
1165 }
1166
1167 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
1168         .drv_type = DPAA2_MC_DPNI_DEVID,
1169         .probe = rte_dpaa2_probe,
1170         .remove = rte_dpaa2_remove,
1171 };
1172
1173 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);