a3b7020fa71151d6e8f4a816d68c46d1b0f7e7d5
[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 int
314 dpaa2_dev_start(struct rte_eth_dev *dev)
315 {
316         struct rte_eth_dev_data *data = dev->data;
317         struct dpaa2_dev_priv *priv = data->dev_private;
318         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
319         struct dpni_queue cfg;
320         struct dpni_error_cfg   err_cfg;
321         uint16_t qdid;
322         struct dpni_queue_id qid;
323         struct dpaa2_queue *dpaa2_q;
324         int ret, i;
325
326         PMD_INIT_FUNC_TRACE();
327
328         ret = dpni_enable(dpni, CMD_PRI_LOW, priv->token);
329         if (ret) {
330                 PMD_INIT_LOG(ERR, "Failure %d in enabling dpni %d device\n",
331                              ret, priv->hw_id);
332                 return ret;
333         }
334
335         ret = dpni_get_qdid(dpni, CMD_PRI_LOW, priv->token,
336                             DPNI_QUEUE_TX, &qdid);
337         if (ret) {
338                 PMD_INIT_LOG(ERR, "Error to get qdid:ErrorCode = %d\n", ret);
339                 return ret;
340         }
341         priv->qdid = qdid;
342
343         for (i = 0; i < data->nb_rx_queues; i++) {
344                 dpaa2_q = (struct dpaa2_queue *)data->rx_queues[i];
345                 ret = dpni_get_queue(dpni, CMD_PRI_LOW, priv->token,
346                                      DPNI_QUEUE_RX, dpaa2_q->tc_index,
347                                        dpaa2_q->flow_id, &cfg, &qid);
348                 if (ret) {
349                         PMD_INIT_LOG(ERR, "Error to get flow "
350                                      "information Error code = %d\n", ret);
351                         return ret;
352                 }
353                 dpaa2_q->fqid = qid.fqid;
354         }
355
356         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
357                                DPNI_OFF_RX_L3_CSUM, true);
358         if (ret) {
359                 PMD_INIT_LOG(ERR, "Error to set RX l3 csum:Error = %d\n", ret);
360                 return ret;
361         }
362
363         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
364                                DPNI_OFF_RX_L4_CSUM, true);
365         if (ret) {
366                 PMD_INIT_LOG(ERR, "Error to get RX l4 csum:Error = %d\n", ret);
367                 return ret;
368         }
369
370         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
371                                DPNI_OFF_TX_L3_CSUM, true);
372         if (ret) {
373                 PMD_INIT_LOG(ERR, "Error to set TX l3 csum:Error = %d\n", ret);
374                 return ret;
375         }
376
377         ret = dpni_set_offload(dpni, CMD_PRI_LOW, priv->token,
378                                DPNI_OFF_TX_L4_CSUM, true);
379         if (ret) {
380                 PMD_INIT_LOG(ERR, "Error to get TX l4 csum:Error = %d\n", ret);
381                 return ret;
382         }
383
384         /*checksum errors, send them to normal path and set it in annotation */
385         err_cfg.errors = DPNI_ERROR_L3CE | DPNI_ERROR_L4CE;
386
387         err_cfg.error_action = DPNI_ERROR_ACTION_CONTINUE;
388         err_cfg.set_frame_annotation = true;
389
390         ret = dpni_set_errors_behavior(dpni, CMD_PRI_LOW,
391                                        priv->token, &err_cfg);
392         if (ret) {
393                 PMD_INIT_LOG(ERR, "Error to dpni_set_errors_behavior:"
394                              "code = %d\n", ret);
395                 return ret;
396         }
397
398         return 0;
399 }
400
401 /**
402  *  This routine disables all traffic on the adapter by issuing a
403  *  global reset on the MAC.
404  */
405 static void
406 dpaa2_dev_stop(struct rte_eth_dev *dev)
407 {
408         struct dpaa2_dev_priv *priv = dev->data->dev_private;
409         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
410         int ret;
411
412         PMD_INIT_FUNC_TRACE();
413
414         ret = dpni_disable(dpni, CMD_PRI_LOW, priv->token);
415         if (ret) {
416                 PMD_INIT_LOG(ERR, "Failure (ret %d) in disabling dpni %d dev\n",
417                              ret, priv->hw_id);
418                 return;
419         }
420 }
421
422 static void
423 dpaa2_dev_close(struct rte_eth_dev *dev)
424 {
425         struct dpaa2_dev_priv *priv = dev->data->dev_private;
426         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
427         int ret;
428
429         PMD_INIT_FUNC_TRACE();
430
431         /* Clean the device first */
432         ret = dpni_reset(dpni, CMD_PRI_LOW, priv->token);
433         if (ret) {
434                 PMD_INIT_LOG(ERR, "Failure cleaning dpni device with"
435                              " error code %d\n", ret);
436                 return;
437         }
438 }
439
440 static void
441 dpaa2_dev_promiscuous_enable(
442                 struct rte_eth_dev *dev)
443 {
444         int ret;
445         struct dpaa2_dev_priv *priv = dev->data->dev_private;
446         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
447
448         PMD_INIT_FUNC_TRACE();
449
450         if (dpni == NULL) {
451                 RTE_LOG(ERR, PMD, "dpni is NULL");
452                 return;
453         }
454
455         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, true);
456         if (ret < 0)
457                 RTE_LOG(ERR, PMD, "Unable to enable promiscuous mode %d", ret);
458 }
459
460 static void
461 dpaa2_dev_promiscuous_disable(
462                 struct rte_eth_dev *dev)
463 {
464         int ret;
465         struct dpaa2_dev_priv *priv = dev->data->dev_private;
466         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
467
468         PMD_INIT_FUNC_TRACE();
469
470         if (dpni == NULL) {
471                 RTE_LOG(ERR, PMD, "dpni is NULL");
472                 return;
473         }
474
475         ret = dpni_set_unicast_promisc(dpni, CMD_PRI_LOW, priv->token, false);
476         if (ret < 0)
477                 RTE_LOG(ERR, PMD, "Unable to disable promiscuous mode %d", ret);
478 }
479 static struct eth_dev_ops dpaa2_ethdev_ops = {
480         .dev_configure    = dpaa2_eth_dev_configure,
481         .dev_start            = dpaa2_dev_start,
482         .dev_stop             = dpaa2_dev_stop,
483         .dev_close            = dpaa2_dev_close,
484         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
485         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
486         .dev_infos_get     = dpaa2_dev_info_get,
487         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
488         .rx_queue_release  = dpaa2_dev_rx_queue_release,
489         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
490         .tx_queue_release  = dpaa2_dev_tx_queue_release,
491 };
492
493 static int
494 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
495 {
496         struct rte_device *dev = eth_dev->device;
497         struct rte_dpaa2_device *dpaa2_dev;
498         struct fsl_mc_io *dpni_dev;
499         struct dpni_attr attr;
500         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
501         struct dpni_buffer_layout layout;
502         int i, ret, hw_id;
503         int tot_size;
504
505         PMD_INIT_FUNC_TRACE();
506
507         /* For secondary processes, the primary has done all the work */
508         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
509                 return 0;
510
511         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
512
513         hw_id = dpaa2_dev->object_id;
514
515         dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
516         if (!dpni_dev) {
517                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
518                 return -1;
519         }
520
521         dpni_dev->regs = rte_mcp_ptr_list[0];
522         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
523         if (ret) {
524                 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
525                         " error code %d\n", hw_id, ret);
526                 return -1;
527         }
528
529         /* Clean the device first */
530         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
531         if (ret) {
532                 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
533                         " error code %d\n", hw_id, ret);
534                 return -1;
535         }
536
537         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
538         if (ret) {
539                 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
540                         " error code %d\n", hw_id, ret);
541                 return -1;
542         }
543
544         priv->num_tc = attr.num_tcs;
545         for (i = 0; i < attr.num_tcs; i++) {
546                 priv->num_dist_per_tc[i] = attr.num_queues;
547                 break;
548         }
549
550         /* Distribution is per Tc only,
551          * so choosing RX queues from default TC only
552          */
553         priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
554
555         if (attr.num_tcs == 1)
556                 priv->nb_tx_queues = attr.num_queues;
557         else
558                 priv->nb_tx_queues = attr.num_tcs;
559
560         PMD_INIT_LOG(DEBUG, "num_tc %d", priv->num_tc);
561         PMD_INIT_LOG(DEBUG, "nb_rx_queues %d", priv->nb_rx_queues);
562
563         priv->hw = dpni_dev;
564         priv->hw_id = hw_id;
565         priv->options = attr.options;
566         priv->max_mac_filters = attr.mac_filter_entries;
567         priv->max_vlan_filters = attr.vlan_filter_entries;
568         priv->flags = 0;
569
570         /* Allocate memory for hardware structure for queues */
571         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
572         if (ret) {
573                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
574                 return -ret;
575         }
576
577         /* Allocate memory for storing MAC addresses */
578         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
579                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
580         if (eth_dev->data->mac_addrs == NULL) {
581                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
582                                                 "store MAC addresses",
583                                 ETHER_ADDR_LEN * attr.mac_filter_entries);
584                 return -ENOMEM;
585         }
586
587         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
588                                         priv->token,
589                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
590         if (ret) {
591                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:"
592                                         " Error Code = %d\n", ret);
593                 return -ret;
594         }
595
596         /* ... rx buffer layout ... */
597         tot_size = DPAA2_HW_BUF_RESERVE + RTE_PKTMBUF_HEADROOM;
598         tot_size = RTE_ALIGN_CEIL(tot_size,
599                                   DPAA2_PACKET_LAYOUT_ALIGN);
600
601         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
602         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
603                                 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
604                                 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
605                                 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
606
607         layout.pass_frame_status = 1;
608         layout.data_head_room = tot_size
609                 - DPAA2_FD_PTA_SIZE - DPAA2_MBUF_HW_ANNOTATION;
610         layout.private_data_size = DPAA2_FD_PTA_SIZE;
611         layout.pass_parser_result = 1;
612         PMD_INIT_LOG(DEBUG, "Tot_size = %d, head room = %d, private = %d",
613                      tot_size, layout.data_head_room, layout.private_data_size);
614         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
615                                      DPNI_QUEUE_RX, &layout);
616         if (ret) {
617                 PMD_INIT_LOG(ERR, "Err(%d) in setting rx buffer layout", ret);
618                 return -1;
619         }
620
621         /* ... tx buffer layout ... */
622         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
623         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
624         layout.pass_frame_status = 1;
625         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
626                                      DPNI_QUEUE_TX, &layout);
627         if (ret) {
628                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer"
629                                   " layout", ret);
630                 return -1;
631         }
632
633         /* ... tx-conf and error buffer layout ... */
634         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
635         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
636         layout.pass_frame_status = 1;
637         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
638                                      DPNI_QUEUE_TX_CONFIRM, &layout);
639         if (ret) {
640                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer"
641                                   " layout", ret);
642                 return -1;
643         }
644
645         eth_dev->dev_ops = &dpaa2_ethdev_ops;
646         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
647
648         return 0;
649 }
650
651 static int
652 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
653 {
654         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
655         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
656         int i, ret;
657         struct dpaa2_queue *dpaa2_q;
658
659         PMD_INIT_FUNC_TRACE();
660
661         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
662                 return -EPERM;
663
664         if (!dpni) {
665                 PMD_INIT_LOG(WARNING, "Already closed or not started");
666                 return -1;
667         }
668
669         dpaa2_dev_close(eth_dev);
670
671         if (priv->rx_vq[0]) {
672                 /* cleaning up queue storage */
673                 for (i = 0; i < priv->nb_rx_queues; i++) {
674                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
675                         if (dpaa2_q->q_storage)
676                                 rte_free(dpaa2_q->q_storage);
677                 }
678                 /*free the all queue memory */
679                 rte_free(priv->rx_vq[0]);
680                 priv->rx_vq[0] = NULL;
681         }
682
683         /* Allocate memory for storing MAC addresses */
684         if (eth_dev->data->mac_addrs) {
685                 rte_free(eth_dev->data->mac_addrs);
686                 eth_dev->data->mac_addrs = NULL;
687         }
688
689         /*Close the device at underlying layer*/
690         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
691         if (ret) {
692                 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
693                         " error code %d\n", ret);
694         }
695
696         /*Free the allocated memory for ethernet private data and dpni*/
697         priv->hw = NULL;
698         free(dpni);
699
700         eth_dev->dev_ops = NULL;
701
702         return 0;
703 }
704
705 static int
706 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
707                 struct rte_dpaa2_device *dpaa2_dev)
708 {
709         struct rte_eth_dev *eth_dev;
710         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
711
712         int diag;
713
714         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
715
716         eth_dev = rte_eth_dev_allocate(ethdev_name);
717         if (eth_dev == NULL)
718                 return -ENOMEM;
719
720         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
721                 eth_dev->data->dev_private = rte_zmalloc(
722                                                 "ethdev private structure",
723                                                 sizeof(struct dpaa2_dev_priv),
724                                                 RTE_CACHE_LINE_SIZE);
725                 if (eth_dev->data->dev_private == NULL) {
726                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
727                                      " private port data\n");
728                         rte_eth_dev_release_port(eth_dev);
729                         return -ENOMEM;
730                 }
731         }
732         eth_dev->device = &dpaa2_dev->device;
733         dpaa2_dev->eth_dev = eth_dev;
734         eth_dev->data->rx_mbuf_alloc_failed = 0;
735
736         /* Invoke PMD device initialization function */
737         diag = dpaa2_dev_init(eth_dev);
738         if (diag == 0)
739                 return 0;
740
741         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
742                 rte_free(eth_dev->data->dev_private);
743         rte_eth_dev_release_port(eth_dev);
744         return diag;
745 }
746
747 static int
748 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
749 {
750         struct rte_eth_dev *eth_dev;
751
752         eth_dev = dpaa2_dev->eth_dev;
753         dpaa2_dev_uninit(eth_dev);
754
755         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
756                 rte_free(eth_dev->data->dev_private);
757         rte_eth_dev_release_port(eth_dev);
758
759         return 0;
760 }
761
762 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
763         .drv_type = DPAA2_MC_DPNI_DEVID,
764         .probe = rte_dpaa2_probe,
765         .remove = rte_dpaa2_remove,
766 };
767
768 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);