net/dpaa2: enable Rx and Tx operations
[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
480 static int
481 dpaa2_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
482 {
483         int ret;
484         struct dpaa2_dev_priv *priv = dev->data->dev_private;
485         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
486         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
487
488         PMD_INIT_FUNC_TRACE();
489
490         if (dpni == NULL) {
491                 RTE_LOG(ERR, PMD, "dpni is NULL");
492                 return -EINVAL;
493         }
494
495         /* check that mtu is within the allowed range */
496         if ((mtu < ETHER_MIN_MTU) || (frame_size > DPAA2_MAX_RX_PKT_LEN))
497                 return -EINVAL;
498
499         /* Set the Max Rx frame length as 'mtu' +
500          * Maximum Ethernet header length
501          */
502         ret = dpni_set_max_frame_length(dpni, CMD_PRI_LOW, priv->token,
503                                         mtu + ETH_VLAN_HLEN);
504         if (ret) {
505                 PMD_DRV_LOG(ERR, "setting the max frame length failed");
506                 return -1;
507         }
508         PMD_DRV_LOG(INFO, "MTU is configured %d for the device\n", mtu);
509         return 0;
510 }
511
512 static struct eth_dev_ops dpaa2_ethdev_ops = {
513         .dev_configure    = dpaa2_eth_dev_configure,
514         .dev_start            = dpaa2_dev_start,
515         .dev_stop             = dpaa2_dev_stop,
516         .dev_close            = dpaa2_dev_close,
517         .promiscuous_enable   = dpaa2_dev_promiscuous_enable,
518         .promiscuous_disable  = dpaa2_dev_promiscuous_disable,
519         .dev_infos_get     = dpaa2_dev_info_get,
520         .mtu_set           = dpaa2_dev_mtu_set,
521         .rx_queue_setup    = dpaa2_dev_rx_queue_setup,
522         .rx_queue_release  = dpaa2_dev_rx_queue_release,
523         .tx_queue_setup    = dpaa2_dev_tx_queue_setup,
524         .tx_queue_release  = dpaa2_dev_tx_queue_release,
525 };
526
527 static int
528 dpaa2_dev_init(struct rte_eth_dev *eth_dev)
529 {
530         struct rte_device *dev = eth_dev->device;
531         struct rte_dpaa2_device *dpaa2_dev;
532         struct fsl_mc_io *dpni_dev;
533         struct dpni_attr attr;
534         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
535         struct dpni_buffer_layout layout;
536         int i, ret, hw_id;
537         int tot_size;
538
539         PMD_INIT_FUNC_TRACE();
540
541         /* For secondary processes, the primary has done all the work */
542         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
543                 return 0;
544
545         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
546
547         hw_id = dpaa2_dev->object_id;
548
549         dpni_dev = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
550         if (!dpni_dev) {
551                 PMD_INIT_LOG(ERR, "malloc failed for dpni device\n");
552                 return -1;
553         }
554
555         dpni_dev->regs = rte_mcp_ptr_list[0];
556         ret = dpni_open(dpni_dev, CMD_PRI_LOW, hw_id, &priv->token);
557         if (ret) {
558                 PMD_INIT_LOG(ERR, "Failure in opening dpni@%d device with"
559                         " error code %d\n", hw_id, ret);
560                 return -1;
561         }
562
563         /* Clean the device first */
564         ret = dpni_reset(dpni_dev, CMD_PRI_LOW, priv->token);
565         if (ret) {
566                 PMD_INIT_LOG(ERR, "Failure cleaning dpni@%d device with"
567                         " error code %d\n", hw_id, ret);
568                 return -1;
569         }
570
571         ret = dpni_get_attributes(dpni_dev, CMD_PRI_LOW, priv->token, &attr);
572         if (ret) {
573                 PMD_INIT_LOG(ERR, "Failure in getting dpni@%d attribute, "
574                         " error code %d\n", hw_id, ret);
575                 return -1;
576         }
577
578         priv->num_tc = attr.num_tcs;
579         for (i = 0; i < attr.num_tcs; i++) {
580                 priv->num_dist_per_tc[i] = attr.num_queues;
581                 break;
582         }
583
584         /* Distribution is per Tc only,
585          * so choosing RX queues from default TC only
586          */
587         priv->nb_rx_queues = priv->num_dist_per_tc[DPAA2_DEF_TC];
588
589         if (attr.num_tcs == 1)
590                 priv->nb_tx_queues = attr.num_queues;
591         else
592                 priv->nb_tx_queues = attr.num_tcs;
593
594         PMD_INIT_LOG(DEBUG, "num_tc %d", priv->num_tc);
595         PMD_INIT_LOG(DEBUG, "nb_rx_queues %d", priv->nb_rx_queues);
596
597         priv->hw = dpni_dev;
598         priv->hw_id = hw_id;
599         priv->options = attr.options;
600         priv->max_mac_filters = attr.mac_filter_entries;
601         priv->max_vlan_filters = attr.vlan_filter_entries;
602         priv->flags = 0;
603
604         /* Allocate memory for hardware structure for queues */
605         ret = dpaa2_alloc_rx_tx_queues(eth_dev);
606         if (ret) {
607                 PMD_INIT_LOG(ERR, "dpaa2_alloc_rx_tx_queuesFailed\n");
608                 return -ret;
609         }
610
611         /* Allocate memory for storing MAC addresses */
612         eth_dev->data->mac_addrs = rte_zmalloc("dpni",
613                 ETHER_ADDR_LEN * attr.mac_filter_entries, 0);
614         if (eth_dev->data->mac_addrs == NULL) {
615                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
616                                                 "store MAC addresses",
617                                 ETHER_ADDR_LEN * attr.mac_filter_entries);
618                 return -ENOMEM;
619         }
620
621         ret = dpni_get_primary_mac_addr(dpni_dev, CMD_PRI_LOW,
622                                         priv->token,
623                         (uint8_t *)(eth_dev->data->mac_addrs[0].addr_bytes));
624         if (ret) {
625                 PMD_INIT_LOG(ERR, "DPNI get mac address failed:"
626                                         " Error Code = %d\n", ret);
627                 return -ret;
628         }
629
630         /* ... rx buffer layout ... */
631         tot_size = DPAA2_HW_BUF_RESERVE + RTE_PKTMBUF_HEADROOM;
632         tot_size = RTE_ALIGN_CEIL(tot_size,
633                                   DPAA2_PACKET_LAYOUT_ALIGN);
634
635         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
636         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
637                                 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
638                                 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
639                                 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
640
641         layout.pass_frame_status = 1;
642         layout.data_head_room = tot_size
643                 - DPAA2_FD_PTA_SIZE - DPAA2_MBUF_HW_ANNOTATION;
644         layout.private_data_size = DPAA2_FD_PTA_SIZE;
645         layout.pass_parser_result = 1;
646         PMD_INIT_LOG(DEBUG, "Tot_size = %d, head room = %d, private = %d",
647                      tot_size, layout.data_head_room, layout.private_data_size);
648         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
649                                      DPNI_QUEUE_RX, &layout);
650         if (ret) {
651                 PMD_INIT_LOG(ERR, "Err(%d) in setting rx buffer layout", ret);
652                 return -1;
653         }
654
655         /* ... tx buffer layout ... */
656         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
657         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
658         layout.pass_frame_status = 1;
659         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
660                                      DPNI_QUEUE_TX, &layout);
661         if (ret) {
662                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx buffer"
663                                   " layout", ret);
664                 return -1;
665         }
666
667         /* ... tx-conf and error buffer layout ... */
668         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
669         layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
670         layout.pass_frame_status = 1;
671         ret = dpni_set_buffer_layout(dpni_dev, CMD_PRI_LOW, priv->token,
672                                      DPNI_QUEUE_TX_CONFIRM, &layout);
673         if (ret) {
674                 PMD_INIT_LOG(ERR, "Error (%d) in setting tx-conf buffer"
675                                   " layout", ret);
676                 return -1;
677         }
678
679         eth_dev->dev_ops = &dpaa2_ethdev_ops;
680         eth_dev->data->drv_name = rte_dpaa2_pmd.driver.name;
681
682         eth_dev->rx_pkt_burst = dpaa2_dev_rx;
683         eth_dev->tx_pkt_burst = dpaa2_dev_tx;
684         return 0;
685 }
686
687 static int
688 dpaa2_dev_uninit(struct rte_eth_dev *eth_dev)
689 {
690         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
691         struct fsl_mc_io *dpni = (struct fsl_mc_io *)priv->hw;
692         int i, ret;
693         struct dpaa2_queue *dpaa2_q;
694
695         PMD_INIT_FUNC_TRACE();
696
697         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
698                 return -EPERM;
699
700         if (!dpni) {
701                 PMD_INIT_LOG(WARNING, "Already closed or not started");
702                 return -1;
703         }
704
705         dpaa2_dev_close(eth_dev);
706
707         if (priv->rx_vq[0]) {
708                 /* cleaning up queue storage */
709                 for (i = 0; i < priv->nb_rx_queues; i++) {
710                         dpaa2_q = (struct dpaa2_queue *)priv->rx_vq[i];
711                         if (dpaa2_q->q_storage)
712                                 rte_free(dpaa2_q->q_storage);
713                 }
714                 /*free the all queue memory */
715                 rte_free(priv->rx_vq[0]);
716                 priv->rx_vq[0] = NULL;
717         }
718
719         /* Allocate memory for storing MAC addresses */
720         if (eth_dev->data->mac_addrs) {
721                 rte_free(eth_dev->data->mac_addrs);
722                 eth_dev->data->mac_addrs = NULL;
723         }
724
725         /*Close the device at underlying layer*/
726         ret = dpni_close(dpni, CMD_PRI_LOW, priv->token);
727         if (ret) {
728                 PMD_INIT_LOG(ERR, "Failure closing dpni device with"
729                         " error code %d\n", ret);
730         }
731
732         /*Free the allocated memory for ethernet private data and dpni*/
733         priv->hw = NULL;
734         free(dpni);
735
736         eth_dev->dev_ops = NULL;
737         eth_dev->rx_pkt_burst = NULL;
738         eth_dev->tx_pkt_burst = NULL;
739
740         return 0;
741 }
742
743 static int
744 rte_dpaa2_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
745                 struct rte_dpaa2_device *dpaa2_dev)
746 {
747         struct rte_eth_dev *eth_dev;
748         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
749
750         int diag;
751
752         sprintf(ethdev_name, "dpni-%d", dpaa2_dev->object_id);
753
754         eth_dev = rte_eth_dev_allocate(ethdev_name);
755         if (eth_dev == NULL)
756                 return -ENOMEM;
757
758         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
759                 eth_dev->data->dev_private = rte_zmalloc(
760                                                 "ethdev private structure",
761                                                 sizeof(struct dpaa2_dev_priv),
762                                                 RTE_CACHE_LINE_SIZE);
763                 if (eth_dev->data->dev_private == NULL) {
764                         PMD_INIT_LOG(CRIT, "Cannot allocate memzone for"
765                                      " private port data\n");
766                         rte_eth_dev_release_port(eth_dev);
767                         return -ENOMEM;
768                 }
769         }
770         eth_dev->device = &dpaa2_dev->device;
771         dpaa2_dev->eth_dev = eth_dev;
772         eth_dev->data->rx_mbuf_alloc_failed = 0;
773
774         /* Invoke PMD device initialization function */
775         diag = dpaa2_dev_init(eth_dev);
776         if (diag == 0)
777                 return 0;
778
779         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
780                 rte_free(eth_dev->data->dev_private);
781         rte_eth_dev_release_port(eth_dev);
782         return diag;
783 }
784
785 static int
786 rte_dpaa2_remove(struct rte_dpaa2_device *dpaa2_dev)
787 {
788         struct rte_eth_dev *eth_dev;
789
790         eth_dev = dpaa2_dev->eth_dev;
791         dpaa2_dev_uninit(eth_dev);
792
793         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
794                 rte_free(eth_dev->data->dev_private);
795         rte_eth_dev_release_port(eth_dev);
796
797         return 0;
798 }
799
800 static struct rte_dpaa2_driver rte_dpaa2_pmd = {
801         .drv_type = DPAA2_MC_DPNI_DEVID,
802         .probe = rte_dpaa2_probe,
803         .remove = rte_dpaa2_remove,
804 };
805
806 RTE_PMD_REGISTER_DPAA2(net_dpaa2, rte_dpaa2_pmd);