f4565b69061cc17973750790ae03c557422ede98
[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 struct eth_dev_ops dpaa2_ethdev_ops = {
441         .dev_configure    = dpaa2_eth_dev_configure,
442         .dev_start            = dpaa2_dev_start,
443         .dev_stop             = dpaa2_dev_stop,
444         .dev_close            = dpaa2_dev_close,
445         .dev_infos_get     = dpaa2_dev_info_get,
446         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
447         .rx_queue_release  = dpaa2_dev_rx_queue_release,
448         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
449         .tx_queue_release  = dpaa2_dev_tx_queue_release,
450 };
451
452 static int
453 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
454 {
455         struct rte_device *dev = eth_dev->device;
456         struct rte_dpaa2_device *dpaa2_dev;
457         struct fsl_mc_io *dpni_dev;
458         struct dpni_attr attr;
459         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
460         struct dpni_buffer_layout layout;
461         int i, ret, hw_id;
462         int tot_size;
463
464         PMD_INIT_FUNC_TRACE();
465
466         /* For secondary processes, the primary has done all the work */
467         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
468                 return 0;
469
470         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
471
472         hw_id = dpaa2_dev->object_id;
473
474         dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
475         if (!dpni_dev) {
476                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
477                 return -1;
478         }
479
480         dpni_dev->regs = rte_mcp_ptr_list[0];
481         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
482         if (ret) {
483                 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
484                         " error code %d\n", hw_id, ret);
485                 return -1;
486         }
487
488         /* Clean the device first */
489         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
490         if (ret) {
491                 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
492                         " error code %d\n", hw_id, ret);
493                 return -1;
494         }
495
496         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
497         if (ret) {
498                 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
499                         " error code %d\n", hw_id, ret);
500                 return -1;
501         }
502
503         priv->num_tc = attr.num_tcs;
504         for (i = 0; i < attr.num_tcs; i++) {
505                 priv->num_dist_per_tc[i] = attr.num_queues;
506                 break;
507         }
508
509         /* Distribution is per Tc only,
510          * so choosing RX queues from default TC only
511          */
512         priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
513
514         if (attr.num_tcs == 1)
515                 priv->nb_tx_queues = attr.num_queues;
516         else
517                 priv->nb_tx_queues = attr.num_tcs;
518
519         PMD_INIT_LOG(DEBUG, "num_tc %d", priv->num_tc);
520         PMD_INIT_LOG(DEBUG, "nb_rx_queues %d", priv->nb_rx_queues);
521
522         priv->hw = dpni_dev;
523         priv->hw_id = hw_id;
524         priv->options = attr.options;
525         priv->max_mac_filters = attr.mac_filter_entries;
526         priv->max_vlan_filters = attr.vlan_filter_entries;
527         priv->flags = 0;
528
529         /* Allocate memory for hardware structure for queues */
530         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
531         if (ret) {
532                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
533                 return -ret;
534         }
535
536         /* Allocate memory for storing MAC addresses */
537         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
538                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
539         if (eth_dev->data->mac_addrs == NULL) {
540                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
541                                                 "store MAC addresses",
542                                 ETHER_ADDR_LEN * attr.mac_filter_entries);
543                 return -ENOMEM;
544         }
545
546         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
547                                         priv->token,
548                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
549         if (ret) {
550                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:"
551                                         " Error Code = %d\n", ret);
552                 return -ret;
553         }
554
555         /* ... rx buffer layout ... */
556         tot_size = DPAA2_HW_BUF_RESERVE + RTE_PKTMBUF_HEADROOM;
557         tot_size = RTE_ALIGN_CEIL(tot_size,
558                                   DPAA2_PACKET_LAYOUT_ALIGN);
559
560         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
561         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
562                                 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
563                                 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
564                                 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
565
566         layout.pass_frame_status = 1;
567         layout.data_head_room = tot_size
568                 - DPAA2_FD_PTA_SIZE - DPAA2_MBUF_HW_ANNOTATION;
569         layout.private_data_size = DPAA2_FD_PTA_SIZE;
570         layout.pass_parser_result = 1;
571         PMD_INIT_LOG(DEBUG, "Tot_size = %d, head room = %d, private = %d",
572                      tot_size, layout.data_head_room, layout.private_data_size);
573         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
574                                      DPNI_QUEUE_RX, &layout);
575         if (ret) {
576                 PMD_INIT_LOG(ERR, "Err(%d) in setting rx buffer layout", ret);
577                 return -1;
578         }
579
580         /* ... tx buffer layout ... */
581         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
582         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
583         layout.pass_frame_status = 1;
584         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
585                                      DPNI_QUEUE_TX, &layout);
586         if (ret) {
587                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer"
588                                   " layout", ret);
589                 return -1;
590         }
591
592         /* ... tx-conf and error buffer layout ... */
593         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
594         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
595         layout.pass_frame_status = 1;
596         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
597                                      DPNI_QUEUE_TX_CONFIRM, &layout);
598         if (ret) {
599                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer"
600                                   " layout", ret);
601                 return -1;
602         }
603
604         eth_dev->dev_ops = &dpaa2_ethdev_ops;
605         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
606
607         return 0;
608 }
609
610 static int
611 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
612 {
613         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
614         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
615         int i, ret;
616         struct dpaa2_queue *dpaa2_q;
617
618         PMD_INIT_FUNC_TRACE();
619
620         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
621                 return -EPERM;
622
623         if (!dpni) {
624                 PMD_INIT_LOG(WARNING, "Already closed or not started");
625                 return -1;
626         }
627
628         dpaa2_dev_close(eth_dev);
629
630         if (priv->rx_vq[0]) {
631                 /* cleaning up queue storage */
632                 for (i = 0; i < priv->nb_rx_queues; i++) {
633                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
634                         if (dpaa2_q->q_storage)
635                                 rte_free(dpaa2_q->q_storage);
636                 }
637                 /*free the all queue memory */
638                 rte_free(priv->rx_vq[0]);
639                 priv->rx_vq[0] = NULL;
640         }
641
642         /* Allocate memory for storing MAC addresses */
643         if (eth_dev->data->mac_addrs) {
644                 rte_free(eth_dev->data->mac_addrs);
645                 eth_dev->data->mac_addrs = NULL;
646         }
647
648         /*Close the device at underlying layer*/
649         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
650         if (ret) {
651                 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
652                         " error code %d\n", ret);
653         }
654
655         /*Free the allocated memory for ethernet private data and dpni*/
656         priv->hw = NULL;
657         free(dpni);
658
659         eth_dev->dev_ops = NULL;
660
661         return 0;
662 }
663
664 static int
665 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
666                 struct rte_dpaa2_device *dpaa2_dev)
667 {
668         struct rte_eth_dev *eth_dev;
669         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
670
671         int diag;
672
673         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
674
675         eth_dev = rte_eth_dev_allocate(ethdev_name);
676         if (eth_dev == NULL)
677                 return -ENOMEM;
678
679         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
680                 eth_dev->data->dev_private = rte_zmalloc(
681                                                 "ethdev private structure",
682                                                 sizeof(struct dpaa2_dev_priv),
683                                                 RTE_CACHE_LINE_SIZE);
684                 if (eth_dev->data->dev_private == NULL) {
685                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
686                                      " private port data\n");
687                         rte_eth_dev_release_port(eth_dev);
688                         return -ENOMEM;
689                 }
690         }
691         eth_dev->device = &dpaa2_dev->device;
692         dpaa2_dev->eth_dev = eth_dev;
693         eth_dev->data->rx_mbuf_alloc_failed = 0;
694
695         /* Invoke PMD device initialization function */
696         diag = dpaa2_dev_init(eth_dev);
697         if (diag == 0)
698                 return 0;
699
700         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
701                 rte_free(eth_dev->data->dev_private);
702         rte_eth_dev_release_port(eth_dev);
703         return diag;
704 }
705
706 static int
707 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
708 {
709         struct rte_eth_dev *eth_dev;
710
711         eth_dev = dpaa2_dev->eth_dev;
712         dpaa2_dev_uninit(eth_dev);
713
714         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
715                 rte_free(eth_dev->data->dev_private);
716         rte_eth_dev_release_port(eth_dev);
717
718         return 0;
719 }
720
721 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
722         .drv_type = DPAA2_MC_DPNI_DEVID,
723         .probe = rte_dpaa2_probe,
724         .remove = rte_dpaa2_remove,
725 };
726
727 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);