c92b86579be5b7ac1939243f13b0921f7757cf17
[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
53 #include "dpaa2_ethdev.h"
54
55 static struct rte_dpaa2_driver rte_dpaa2_pmd;
56
57 static void
58 dpaa2_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
59 {
60         struct dpaa2_dev_priv *priv = dev->data->dev_private;
61
62         PMD_INIT_FUNC_TRACE();
63
64         dev_info->if_index = priv->hw_id;
65
66         dev_info->max_mac_addrs = priv->max_mac_filters;
67         dev_info->max_rx_pktlen = DPAA2_MAX_RX_PKT_LEN;
68         dev_info->min_rx_bufsize = DPAA2_MIN_RX_BUF_SIZE;
69         dev_info->max_rx_queues = (uint16_t)priv->nb_rx_queues;
70         dev_info->max_tx_queues = (uint16_t)priv->nb_tx_queues;
71         dev_info->rx_offload_capa =
72                 DEV_RX_OFFLOAD_IPV4_CKSUM |
73                 DEV_RX_OFFLOAD_UDP_CKSUM |
74                 DEV_RX_OFFLOAD_TCP_CKSUM |
75                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM;
76         dev_info->tx_offload_capa =
77                 DEV_TX_OFFLOAD_IPV4_CKSUM |
78                 DEV_TX_OFFLOAD_UDP_CKSUM |
79                 DEV_TX_OFFLOAD_TCP_CKSUM |
80                 DEV_TX_OFFLOAD_SCTP_CKSUM |
81                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
82         dev_info->speed_capa = ETH_LINK_SPEED_1G |
83                         ETH_LINK_SPEED_2_5G |
84                         ETH_LINK_SPEED_10G;
85 }
86
87 static int
88 dpaa2_alloc_rx_tx_queues(struct rte_eth_dev *dev)
89 {
90         struct dpaa2_dev_priv *priv = dev->data->dev_private;
91         uint16_t dist_idx;
92         uint32_t vq_id;
93         struct dpaa2_queue *mc_q, *mcq;
94         uint32_t tot_queues;
95         int i;
96         struct dpaa2_queue *dpaa2_q;
97
98         PMD_INIT_FUNC_TRACE();
99
100         tot_queues = priv->nb_rx_queues + priv->nb_tx_queues;
101         mc_q = rte_malloc(NULL, sizeof(struct dpaa2_queue) * tot_queues,
102                           RTE_CACHE_LINE_SIZE);
103         if (!mc_q) {
104                 PMD_INIT_LOG(ERR, "malloc failed for rx/tx queues\n");
105                 return -1;
106         }
107
108         for (i = 0; i < priv->nb_rx_queues; i++) {
109                 mc_q->dev = dev;
110                 priv->rx_vq[i] = mc_q++;
111                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
112                 dpaa2_q->q_storage = rte_malloc("dq_storage",
113                                         sizeof(struct queue_storage_info_t),
114                                         RTE_CACHE_LINE_SIZE);
115                 if (!dpaa2_q->q_storage)
116                         goto fail;
117
118                 memset(dpaa2_q->q_storage, 0,
119                        sizeof(struct queue_storage_info_t));
120                 dpaa2_q->q_storage->dq_storage[0] = rte_malloc(NULL,
121                         DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
122                         RTE_CACHE_LINE_SIZE);
123         }
124
125         for (i = 0; i < priv->nb_tx_queues; i++) {
126                 mc_q->dev = dev;
127                 mc_q->flow_id = DPNI_NEW_FLOW_ID;
128                 priv->tx_vq[i] = mc_q++;
129         }
130
131         vq_id = 0;
132         for (dist_idx = 0; dist_idx < priv->num_dist_per_tc[DPAA2_DEF_TC];
133              dist_idx++) {
134                 mcq = (struct dpaa2_queue *)priv->rx_vq[vq_id];
135                 mcq->tc_index = DPAA2_DEF_TC;
136                 mcq->flow_id = dist_idx;
137                 vq_id++;
138         }
139
140         return 0;
141 fail:
142         i -= 1;
143         mc_q = priv->rx_vq[0];
144         while (i >= 0) {
145                 dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
146                 rte_free(dpaa2_q->q_storage->dq_storage[0]);
147                 rte_free(dpaa2_q->q_storage);
148                 priv->rx_vq[i--] = NULL;
149         }
150         rte_free(mc_q);
151         return -1;
152 }
153
154 static int
155 dpaa2_eth_dev_configure(struct rte_eth_dev *dev)
156 {
157         struct rte_eth_dev_data *data = dev->data;
158         struct rte_eth_conf *eth_conf = &data->dev_conf;
159         int ret;
160
161         PMD_INIT_FUNC_TRACE();
162
163         /* Check for correct configuration */
164         if (eth_conf->rxmode.mq_mode != ETH_MQ_RX_RSS &&
165             data->nb_rx_queues > 1) {
166                 PMD_INIT_LOG(ERR, "Distribution is not enabled, "
167                             "but Rx queues more than 1\n");
168                 return -1;
169         }
170
171         if (eth_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) {
172                 /* Return in case number of Rx queues is 1 */
173                 if (data->nb_rx_queues == 1)
174                         return 0;
175                 ret = dpaa2_setup_flow_dist(dev,
176                                 eth_conf->rx_adv_conf.rss_conf.rss_hf);
177                 if (ret) {
178                         PMD_INIT_LOG(ERR, "unable to set flow distribution."
179                                      "please check queue config\n");
180                         return ret;
181                 }
182         }
183         return 0;
184 }
185
186 /* Function to setup RX flow information. It contains traffic class ID,
187  * flow ID, destination configuration etc.
188  */
189 static int
190 dpaa2_dev_rx_queue_setup(struct rte_eth_dev *dev,
191                          uint16_t rx_queue_id,
192                          uint16_t nb_rx_desc __rte_unused,
193                          unsigned int socket_id __rte_unused,
194                          const struct rte_eth_rxconf *rx_conf __rte_unused,
195                          struct rte_mempool *mb_pool)
196 {
197         struct dpaa2_dev_priv *priv = dev->data->dev_private;
198         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
199         struct dpaa2_queue *dpaa2_q;
200         struct dpni_queue cfg;
201         uint8_t options = 0;
202         uint8_t flow_id;
203         uint32_t bpid;
204         int ret;
205
206         PMD_INIT_FUNC_TRACE();
207
208         PMD_INIT_LOG(DEBUG, "dev =%p, queue =%d, pool = %p, conf =%p",
209                      dev, rx_queue_id, mb_pool, rx_conf);
210
211         if (!priv->bp_list || priv->bp_list->mp != mb_pool) {
212                 bpid = mempool_to_bpid(mb_pool);
213                 ret = dpaa2_attach_bp_list(priv,
214                                            rte_dpaa2_bpid_info[bpid].bp_list);
215                 if (ret)
216                         return ret;
217         }
218         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[rx_queue_id];
219         dpaa2_q->mb_pool = mb_pool; /**< mbuf pool to populate RX ring. */
220
221         /*Get the tc id and flow id from given VQ id*/
222         flow_id = rx_queue_id % priv->num_dist_per_tc[dpaa2_q->tc_index];
223         memset(&cfg, 0, sizeof(struct dpni_queue));
224
225         options = options | DPNI_QUEUE_OPT_USER_CTX;
226         cfg.user_context = (uint64_t)(dpaa2_q);
227
228         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_RX,
229                              dpaa2_q->tc_index, flow_id, options, &cfg);
230         if (ret) {
231                 PMD_INIT_LOG(ERR, "Error in setting the rx flow: = %d\n", ret);
232                 return -1;
233         }
234
235         dev->data->rx_queues[rx_queue_id] = dpaa2_q;
236         return 0;
237 }
238
239 static int
240 dpaa2_dev_tx_queue_setup(struct rte_eth_dev *dev,
241                          uint16_t tx_queue_id,
242                          uint16_t nb_tx_desc __rte_unused,
243                          unsigned int socket_id __rte_unused,
244                          const struct rte_eth_txconf *tx_conf __rte_unused)
245 {
246         struct dpaa2_dev_priv *priv = dev->data->dev_private;
247         struct dpaa2_queue *dpaa2_q = (struct dpaa2_queue *)
248                 priv->tx_vq[tx_queue_id];
249         struct fsl_mc_io *dpni = priv->hw;
250         struct dpni_queue tx_conf_cfg;
251         struct dpni_queue tx_flow_cfg;
252         uint8_t options = 0, flow_id;
253         uint32_t tc_id;
254         int ret;
255
256         PMD_INIT_FUNC_TRACE();
257
258         /* Return if queue already configured */
259         if (dpaa2_q->flow_id != DPNI_NEW_FLOW_ID)
260                 return 0;
261
262         memset(&tx_conf_cfg, 0, sizeof(struct dpni_queue));
263         memset(&tx_flow_cfg, 0, sizeof(struct dpni_queue));
264
265         if (priv->num_tc == 1) {
266                 tc_id = 0;
267                 flow_id = tx_queue_id % priv->num_dist_per_tc[tc_id];
268         } else {
269                 tc_id = tx_queue_id;
270                 flow_id = 0;
271         }
272
273         ret = dpni_set_queue(dpni, CMD_PRI_LOW, priv->token, DPNI_QUEUE_TX,
274                              tc_id, flow_id, options, &tx_flow_cfg);
275         if (ret) {
276                 PMD_INIT_LOG(ERR, "Error in setting the tx flow: "
277                              "tc_id=%d, flow =%d ErrorCode = %x\n",
278                              tc_id, flow_id, -ret);
279                         return -1;
280         }
281
282         dpaa2_q->flow_id = flow_id;
283
284         if (tx_queue_id == 0) {
285                 /*Set tx-conf and error configuration*/
286                 ret = dpni_set_tx_confirmation_mode(dpni, CMD_PRI_LOW,
287                                                     priv->token,
288                                                     DPNI_CONF_DISABLE);
289                 if (ret) {
290                         PMD_INIT_LOG(ERR, "Error in set tx conf mode settings"
291                                      " ErrorCode = %x", ret);
292                         return -1;
293                 }
294         }
295         dpaa2_q->tc_index = tc_id;
296
297         dev->data->tx_queues[tx_queue_id] = dpaa2_q;
298         return 0;
299 }
300
301 static void
302 dpaa2_dev_rx_queue_release(void *q __rte_unused)
303 {
304         PMD_INIT_FUNC_TRACE();
305 }
306
307 static void
308 dpaa2_dev_tx_queue_release(void *q __rte_unused)
309 {
310         PMD_INIT_FUNC_TRACE();
311 }
312
313 static const uint32_t *
314 dpaa2_supported_ptypes_get(struct rte_eth_dev *dev)
315 {
316         static const uint32_t ptypes[] = {
317                 /*todo -= add more types */
318                 RTE_PTYPE_L2_ETHER,
319                 RTE_PTYPE_L3_IPV4,
320                 RTE_PTYPE_L3_IPV4_EXT,
321                 RTE_PTYPE_L3_IPV6,
322                 RTE_PTYPE_L3_IPV6_EXT,
323                 RTE_PTYPE_L4_TCP,
324                 RTE_PTYPE_L4_UDP,
325                 RTE_PTYPE_L4_SCTP,
326                 RTE_PTYPE_L4_ICMP,
327                 RTE_PTYPE_UNKNOWN
328         };
329
330         if (dev->rx_pkt_burst == dpaa2_dev_rx)
331                 return ptypes;
332         return NULL;
333 }
334
335 static int
336 dpaa2_dev_start(struct rte_eth_dev *dev)
337 {
338         struct rte_eth_dev_data *data = dev->data;
339         struct dpaa2_dev_priv *priv = data->dev_private;
340         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
341         struct dpni_queue cfg;
342         struct dpni_error_cfg   err_cfg;
343         uint16_t qdid;
344         struct dpni_queue_id qid;
345         struct dpaa2_queue *dpaa2_q;
346         int ret, i;
347
348         PMD_INIT_FUNC_TRACE();
349
350         ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
351         if (ret) {
352                 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
353                              ret, priv->hw_id);
354                 return ret;
355         }
356
357         ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
358                             DPNI_QUEUE_TX, &qdid);
359         if (ret) {
360                 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
361                 return ret;
362         }
363         priv->qdid = qdid;
364
365         for (i = 0; i < data->nb_rx_queues; i++) {
366                 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
367                 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
368                                      DPNI_QUEUE_RX, dpaa2_q->tc_index,
369                                        dpaa2_q->flow_id, &cfg, &qid);
370                 if (ret) {
371                         PMD_INIT_LOG(ERR, "Error to get flow "
372                                      "information Error code = %d\n", ret);
373                         return ret;
374                 }
375                 dpaa2_q->fqid = qid.fqid;
376         }
377
378         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
379                                DPNI_OFF_RX_L3_CSUM, true);
380         if (ret) {
381                 PMD_INIT_LOG(ERR, "Error to set RX l3 csum:Error = %d\n", ret);
382                 return ret;
383         }
384
385         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
386                                DPNI_OFF_RX_L4_CSUM, true);
387         if (ret) {
388                 PMD_INIT_LOG(ERR, "Error to get RX l4 csum:Error = %d\n", ret);
389                 return ret;
390         }
391
392         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
393                                DPNI_OFF_TX_L3_CSUM, true);
394         if (ret) {
395                 PMD_INIT_LOG(ERR, "Error to set TX l3 csum:Error = %d\n", ret);
396                 return ret;
397         }
398
399         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
400                                DPNI_OFF_TX_L4_CSUM, true);
401         if (ret) {
402                 PMD_INIT_LOG(ERR, "Error to get TX l4 csum:Error = %d\n", ret);
403                 return ret;
404         }
405
406         /*checksum errors, send them to normal path and set it in annotation */
407         err_cfg.errors = DPNI_ERROR_L3CE | DPNI_ERROR_L4CE;
408
409         err_cfg.error_action = DPNI_ERROR_ACTION_CONTINUE;
410         err_cfg.set_frame_annotation = true;
411
412         ret = dpni_set_errors_behavior(dpni, CMD_PRI_LOW,
413                                        priv->token, &err_cfg);
414         if (ret) {
415                 PMD_INIT_LOG(ERR, "Error to dpni_set_errors_behavior:"
416                              "code = %d\n", ret);
417                 return ret;
418         }
419
420         return 0;
421 }
422
423 /**
424  *  This routine disables all traffic on the adapter by issuing a
425  *  global reset on the MAC.
426  */
427 static void
428 dpaa2_dev_stop(struct rte_eth_dev *dev)
429 {
430         struct dpaa2_dev_priv *priv = dev->data->dev_private;
431         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
432         int ret;
433
434         PMD_INIT_FUNC_TRACE();
435
436         ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
437         if (ret) {
438                 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
439                              ret, priv->hw_id);
440                 return;
441         }
442 }
443
444 static void
445 dpaa2_dev_close(struct rte_eth_dev *dev)
446 {
447         struct dpaa2_dev_priv *priv = dev->data->dev_private;
448         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
449         int ret;
450
451         PMD_INIT_FUNC_TRACE();
452
453         /* Clean the device first */
454         ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
455         if (ret) {
456                 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
457                              " error code %d\n", ret);
458                 return;
459         }
460 }
461
462 static void
463 dpaa2_dev_promiscuous_enable(
464                 struct rte_eth_dev *dev)
465 {
466         int ret;
467         struct dpaa2_dev_priv *priv = dev->data->dev_private;
468         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
469
470         PMD_INIT_FUNC_TRACE();
471
472         if (dpni == NULL) {
473                 RTE_LOG(ERR, PMD, "dpni is NULL");
474                 return;
475         }
476
477         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
478         if (ret < 0)
479                 RTE_LOG(ERR, PMD, "Unable to enable promiscuous mode %d", ret);
480 }
481
482 static void
483 dpaa2_dev_promiscuous_disable(
484                 struct rte_eth_dev *dev)
485 {
486         int ret;
487         struct dpaa2_dev_priv *priv = dev->data->dev_private;
488         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
489
490         PMD_INIT_FUNC_TRACE();
491
492         if (dpni == NULL) {
493                 RTE_LOG(ERR, PMD, "dpni is NULL");
494                 return;
495         }
496
497         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, false);
498         if (ret < 0)
499                 RTE_LOG(ERR, PMD, "Unable to disable promiscuous mode %d", ret);
500 }
501
502 static int
503 dpaa2_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
504 {
505         int ret;
506         struct dpaa2_dev_priv *priv = dev->data->dev_private;
507         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
508         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
509
510         PMD_INIT_FUNC_TRACE();
511
512         if (dpni == NULL) {
513                 RTE_LOG(ERR, PMD, "dpni is NULL");
514                 return -EINVAL;
515         }
516
517         /* check that mtu is within the allowed range */
518         if ((mtu < ETHER_MIN_MTU) || (frame_size > DPAA2_MAX_RX_PKT_LEN))
519                 return -EINVAL;
520
521         /* Set the Max Rx frame length as 'mtu' +
522          * Maximum Ethernet header length
523          */
524         ret = dpni_set_max_frame_length(dpni, CMD_PRI_LOW, priv->token,
525                                         mtu + ETH_VLAN_HLEN);
526         if (ret) {
527                 PMD_DRV_LOG(ERR, "setting the max frame length failed");
528                 return -1;
529         }
530         PMD_DRV_LOG(INFO, "MTU is configured %d for the device\n", mtu);
531         return 0;
532 }
533
534 static struct eth_dev_ops dpaa2_ethdev_ops = {
535         .dev_configure    = dpaa2_eth_dev_configure,
536         .dev_start            = dpaa2_dev_start,
537         .dev_stop             = dpaa2_dev_stop,
538         .dev_close            = dpaa2_dev_close,
539         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
540         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
541         .dev_infos_get     = dpaa2_dev_info_get,
542         .dev_supported_ptypes_get = dpaa2_supported_ptypes_get,
543         .mtu_set           = dpaa2_dev_mtu_set,
544         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
545         .rx_queue_release  = dpaa2_dev_rx_queue_release,
546         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
547         .tx_queue_release  = dpaa2_dev_tx_queue_release,
548 };
549
550 static int
551 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
552 {
553         struct rte_device *dev = eth_dev->device;
554         struct rte_dpaa2_device *dpaa2_dev;
555         struct fsl_mc_io *dpni_dev;
556         struct dpni_attr attr;
557         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
558         struct dpni_buffer_layout layout;
559         int i, ret, hw_id;
560         int tot_size;
561
562         PMD_INIT_FUNC_TRACE();
563
564         /* For secondary processes, the primary has done all the work */
565         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
566                 return 0;
567
568         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
569
570         hw_id = dpaa2_dev->object_id;
571
572         dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
573         if (!dpni_dev) {
574                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
575                 return -1;
576         }
577
578         dpni_dev->regs = rte_mcp_ptr_list[0];
579         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
580         if (ret) {
581                 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
582                         " error code %d\n", hw_id, ret);
583                 return -1;
584         }
585
586         /* Clean the device first */
587         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
588         if (ret) {
589                 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
590                         " error code %d\n", hw_id, ret);
591                 return -1;
592         }
593
594         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
595         if (ret) {
596                 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
597                         " error code %d\n", hw_id, ret);
598                 return -1;
599         }
600
601         priv->num_tc = attr.num_tcs;
602         for (i = 0; i < attr.num_tcs; i++) {
603                 priv->num_dist_per_tc[i] = attr.num_queues;
604                 break;
605         }
606
607         /* Distribution is per Tc only,
608          * so choosing RX queues from default TC only
609          */
610         priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
611
612         if (attr.num_tcs == 1)
613                 priv->nb_tx_queues = attr.num_queues;
614         else
615                 priv->nb_tx_queues = attr.num_tcs;
616
617         PMD_INIT_LOG(DEBUG, "num_tc %d", priv->num_tc);
618         PMD_INIT_LOG(DEBUG, "nb_rx_queues %d", priv->nb_rx_queues);
619
620         priv->hw = dpni_dev;
621         priv->hw_id = hw_id;
622         priv->options = attr.options;
623         priv->max_mac_filters = attr.mac_filter_entries;
624         priv->max_vlan_filters = attr.vlan_filter_entries;
625         priv->flags = 0;
626
627         /* Allocate memory for hardware structure for queues */
628         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
629         if (ret) {
630                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
631                 return -ret;
632         }
633
634         /* Allocate memory for storing MAC addresses */
635         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
636                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
637         if (eth_dev->data->mac_addrs == NULL) {
638                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
639                                                 "store MAC addresses",
640                                 ETHER_ADDR_LEN * attr.mac_filter_entries);
641                 return -ENOMEM;
642         }
643
644         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
645                                         priv->token,
646                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
647         if (ret) {
648                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:"
649                                         " Error Code = %d\n", ret);
650                 return -ret;
651         }
652
653         /* ... rx buffer layout ... */
654         tot_size = DPAA2_HW_BUF_RESERVE + RTE_PKTMBUF_HEADROOM;
655         tot_size = RTE_ALIGN_CEIL(tot_size,
656                                   DPAA2_PACKET_LAYOUT_ALIGN);
657
658         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
659         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
660                                 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
661                                 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
662                                 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
663
664         layout.pass_frame_status = 1;
665         layout.data_head_room = tot_size
666                 - DPAA2_FD_PTA_SIZE - DPAA2_MBUF_HW_ANNOTATION;
667         layout.private_data_size = DPAA2_FD_PTA_SIZE;
668         layout.pass_parser_result = 1;
669         PMD_INIT_LOG(DEBUG, "Tot_size = %d, head room = %d, private = %d",
670                      tot_size, layout.data_head_room, layout.private_data_size);
671         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
672                                      DPNI_QUEUE_RX, &layout);
673         if (ret) {
674                 PMD_INIT_LOG(ERR, "Err(%d) in setting rx buffer layout", ret);
675                 return -1;
676         }
677
678         /* ... tx buffer layout ... */
679         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
680         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
681         layout.pass_frame_status = 1;
682         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
683                                      DPNI_QUEUE_TX, &layout);
684         if (ret) {
685                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer"
686                                   " layout", ret);
687                 return -1;
688         }
689
690         /* ... tx-conf and error buffer layout ... */
691         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
692         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
693         layout.pass_frame_status = 1;
694         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
695                                      DPNI_QUEUE_TX_CONFIRM, &layout);
696         if (ret) {
697                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer"
698                                   " layout", ret);
699                 return -1;
700         }
701
702         eth_dev->dev_ops = &dpaa2_ethdev_ops;
703         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
704
705         eth_dev->rx_pkt_burst = dpaa2_dev_rx;
706         eth_dev->tx_pkt_burst = dpaa2_dev_tx;
707         return 0;
708 }
709
710 static int
711 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
712 {
713         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
714         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
715         int i, ret;
716         struct dpaa2_queue *dpaa2_q;
717
718         PMD_INIT_FUNC_TRACE();
719
720         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
721                 return -EPERM;
722
723         if (!dpni) {
724                 PMD_INIT_LOG(WARNING, "Already closed or not started");
725                 return -1;
726         }
727
728         dpaa2_dev_close(eth_dev);
729
730         if (priv->rx_vq[0]) {
731                 /* cleaning up queue storage */
732                 for (i = 0; i < priv->nb_rx_queues; i++) {
733                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
734                         if (dpaa2_q->q_storage)
735                                 rte_free(dpaa2_q->q_storage);
736                 }
737                 /*free the all queue memory */
738                 rte_free(priv->rx_vq[0]);
739                 priv->rx_vq[0] = NULL;
740         }
741
742         /* Allocate memory for storing MAC addresses */
743         if (eth_dev->data->mac_addrs) {
744                 rte_free(eth_dev->data->mac_addrs);
745                 eth_dev->data->mac_addrs = NULL;
746         }
747
748         /*Close the device at underlying layer*/
749         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
750         if (ret) {
751                 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
752                         " error code %d\n", ret);
753         }
754
755         /*Free the allocated memory for ethernet private data and dpni*/
756         priv->hw = NULL;
757         free(dpni);
758
759         eth_dev->dev_ops = NULL;
760         eth_dev->rx_pkt_burst = NULL;
761         eth_dev->tx_pkt_burst = NULL;
762
763         return 0;
764 }
765
766 static int
767 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
768                 struct rte_dpaa2_device *dpaa2_dev)
769 {
770         struct rte_eth_dev *eth_dev;
771         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
772
773         int diag;
774
775         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
776
777         eth_dev = rte_eth_dev_allocate(ethdev_name);
778         if (eth_dev == NULL)
779                 return -ENOMEM;
780
781         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
782                 eth_dev->data->dev_private = rte_zmalloc(
783                                                 "ethdev private structure",
784                                                 sizeof(struct dpaa2_dev_priv),
785                                                 RTE_CACHE_LINE_SIZE);
786                 if (eth_dev->data->dev_private == NULL) {
787                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
788                                      " private port data\n");
789                         rte_eth_dev_release_port(eth_dev);
790                         return -ENOMEM;
791                 }
792         }
793         eth_dev->device = &dpaa2_dev->device;
794         dpaa2_dev->eth_dev = eth_dev;
795         eth_dev->data->rx_mbuf_alloc_failed = 0;
796
797         /* Invoke PMD device initialization function */
798         diag = dpaa2_dev_init(eth_dev);
799         if (diag == 0)
800                 return 0;
801
802         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
803                 rte_free(eth_dev->data->dev_private);
804         rte_eth_dev_release_port(eth_dev);
805         return diag;
806 }
807
808 static int
809 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
810 {
811         struct rte_eth_dev *eth_dev;
812
813         eth_dev = dpaa2_dev->eth_dev;
814         dpaa2_dev_uninit(eth_dev);
815
816         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
817                 rte_free(eth_dev->data->dev_private);
818         rte_eth_dev_release_port(eth_dev);
819
820         return 0;
821 }
822
823 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
824         .drv_type = DPAA2_MC_DPNI_DEVID,
825         .probe = rte_dpaa2_probe,
826         .remove = rte_dpaa2_remove,
827 };
828
829 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);