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