net/pfe: add packet types and basic statistics
[dpdk.git] / drivers / net / pfe / pfe_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 NXP
3  */
4
5 #include <sys/epoll.h>
6 #include <rte_kvargs.h>
7 #include <rte_ethdev_vdev.h>
8 #include <rte_bus_vdev.h>
9 #include <dpaa_of.h>
10
11 #include "pfe_logs.h"
12 #include "pfe_mod.h"
13
14 #define PFE_MAX_MACS 1 /*we can support upto 4 MACs per IF*/
15 #define PFE_VDEV_GEM_ID_ARG     "intf"
16
17 struct pfe_vdev_init_params {
18         int8_t  gem_id;
19 };
20 static struct pfe *g_pfe;
21 /* Supported Rx offloads */
22 static uint64_t dev_rx_offloads_sup =
23                 DEV_RX_OFFLOAD_IPV4_CKSUM |
24                 DEV_RX_OFFLOAD_UDP_CKSUM |
25                 DEV_RX_OFFLOAD_TCP_CKSUM;
26
27 /* Supported Tx offloads */
28 static uint64_t dev_tx_offloads_sup =
29                 DEV_TX_OFFLOAD_IPV4_CKSUM |
30                 DEV_TX_OFFLOAD_UDP_CKSUM |
31                 DEV_TX_OFFLOAD_TCP_CKSUM;
32
33 /* TODO: make pfe_svr a runtime option.
34  * Driver should be able to get the SVR
35  * information from HW.
36  */
37 unsigned int pfe_svr = SVR_LS1012A_REV1;
38 static void *cbus_emac_base[3];
39 static void *cbus_gpi_base[3];
40
41 int pfe_logtype_pmd;
42
43 /* pfe_gemac_init
44  */
45 static int
46 pfe_gemac_init(struct pfe_eth_priv_s *priv)
47 {
48         struct gemac_cfg cfg;
49
50         cfg.speed = SPEED_1000M;
51         cfg.duplex = DUPLEX_FULL;
52
53         gemac_set_config(priv->EMAC_baseaddr, &cfg);
54         gemac_allow_broadcast(priv->EMAC_baseaddr);
55         gemac_enable_1536_rx(priv->EMAC_baseaddr);
56         gemac_enable_stacked_vlan(priv->EMAC_baseaddr);
57         gemac_enable_pause_rx(priv->EMAC_baseaddr);
58         gemac_set_bus_width(priv->EMAC_baseaddr, 64);
59         gemac_enable_rx_checksum_offload(priv->EMAC_baseaddr);
60
61         return 0;
62 }
63
64 static void
65 pfe_soc_version_get(void)
66 {
67         FILE *svr_file = NULL;
68         unsigned int svr_ver = 0;
69
70         PMD_INIT_FUNC_TRACE();
71
72         svr_file = fopen(PFE_SOC_ID_FILE, "r");
73         if (!svr_file) {
74                 PFE_PMD_ERR("Unable to open SoC device");
75                 return; /* Not supported on this infra */
76         }
77
78         if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
79                 pfe_svr = svr_ver;
80         else
81                 PFE_PMD_ERR("Unable to read SoC device");
82
83         fclose(svr_file);
84 }
85
86 static int pfe_eth_start(struct pfe_eth_priv_s *priv)
87 {
88         gpi_enable(priv->GPI_baseaddr);
89         gemac_enable(priv->EMAC_baseaddr);
90
91         return 0;
92 }
93
94 static void
95 pfe_eth_flush_txQ(struct pfe_eth_priv_s *priv, int tx_q_num, int
96                   __rte_unused from_tx, __rte_unused int n_desc)
97 {
98         struct rte_mbuf *mbuf;
99         unsigned int flags;
100
101         /* Clean HIF and client queue */
102         while ((mbuf = hif_lib_tx_get_next_complete(&priv->client,
103                                                    tx_q_num, &flags,
104                                                    HIF_TX_DESC_NT))) {
105                 if (mbuf) {
106                         mbuf->next = NULL;
107                         mbuf->nb_segs = 1;
108                         rte_pktmbuf_free(mbuf);
109                 }
110         }
111 }
112
113
114 static void
115 pfe_eth_flush_tx(struct pfe_eth_priv_s *priv)
116 {
117         unsigned int ii;
118
119         for (ii = 0; ii < emac_txq_cnt; ii++)
120                 pfe_eth_flush_txQ(priv, ii, 0, 0);
121 }
122
123 static int
124 pfe_eth_event_handler(void *data, int event, __rte_unused int qno)
125 {
126         struct pfe_eth_priv_s *priv = data;
127
128         switch (event) {
129         case EVENT_TXDONE_IND:
130                 pfe_eth_flush_tx(priv);
131                 hif_lib_event_handler_start(&priv->client, EVENT_TXDONE_IND, 0);
132                 break;
133         case EVENT_HIGH_RX_WM:
134         default:
135                 break;
136         }
137
138         return 0;
139 }
140
141 static uint16_t
142 pfe_recv_pkts_on_intr(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
143 {
144         struct hif_client_rx_queue *queue = rxq;
145         struct pfe_eth_priv_s *priv = queue->priv;
146         struct epoll_event epoll_ev;
147         uint64_t ticks = 1;  /* 1 msec */
148         int ret;
149         int have_something, work_done;
150
151 #define RESET_STATUS (HIF_INT | HIF_RXPKT_INT)
152
153         /*TODO can we remove this cleanup from here?*/
154         pfe_tx_do_cleanup(priv->pfe);
155         have_something = pfe_hif_rx_process(priv->pfe, nb_pkts);
156         work_done = hif_lib_receive_pkt(rxq, priv->pfe->hif.shm->pool,
157                         rx_pkts, nb_pkts);
158
159         if (!have_something || !work_done) {
160                 writel(RESET_STATUS, HIF_INT_SRC);
161                 writel(readl(HIF_INT_ENABLE) | HIF_RXPKT_INT, HIF_INT_ENABLE);
162                 ret = epoll_wait(priv->pfe->hif.epoll_fd, &epoll_ev, 1, ticks);
163                 if (ret < 0 && errno != EINTR)
164                         PFE_PMD_ERR("epoll_wait fails with %d\n", errno);
165         }
166
167         return work_done;
168 }
169
170 static uint16_t
171 pfe_recv_pkts(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
172 {
173         struct hif_client_rx_queue *queue = rxq;
174         struct pfe_eth_priv_s *priv = queue->priv;
175         struct rte_mempool *pool;
176
177         /*TODO can we remove this cleanup from here?*/
178         pfe_tx_do_cleanup(priv->pfe);
179         pfe_hif_rx_process(priv->pfe, nb_pkts);
180         pool = priv->pfe->hif.shm->pool;
181
182         return hif_lib_receive_pkt(rxq, pool, rx_pkts, nb_pkts);
183 }
184
185 static uint16_t
186 pfe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
187 {
188         struct hif_client_tx_queue *queue = tx_queue;
189         struct pfe_eth_priv_s *priv = queue->priv;
190         struct rte_eth_stats *stats = &priv->stats;
191         int i;
192
193         for (i = 0; i < nb_pkts; i++) {
194                 if (tx_pkts[i]->nb_segs > 1) {
195                         struct rte_mbuf *mbuf;
196                         int j;
197
198                         hif_lib_xmit_pkt(&priv->client, queue->queue_id,
199                                 (void *)(size_t)rte_pktmbuf_iova(tx_pkts[i]),
200                                 tx_pkts[i]->buf_addr + tx_pkts[i]->data_off,
201                                 tx_pkts[i]->data_len, 0x0, HIF_FIRST_BUFFER,
202                                 tx_pkts[i]);
203
204                         mbuf = tx_pkts[i]->next;
205                         for (j = 0; j < (tx_pkts[i]->nb_segs - 2); j++) {
206                                 hif_lib_xmit_pkt(&priv->client, queue->queue_id,
207                                         (void *)(size_t)rte_pktmbuf_iova(mbuf),
208                                         mbuf->buf_addr + mbuf->data_off,
209                                         mbuf->data_len,
210                                         0x0, 0x0, mbuf);
211                                 mbuf = mbuf->next;
212                         }
213
214                         hif_lib_xmit_pkt(&priv->client, queue->queue_id,
215                                         (void *)(size_t)rte_pktmbuf_iova(mbuf),
216                                         mbuf->buf_addr + mbuf->data_off,
217                                         mbuf->data_len,
218                                         0x0, HIF_LAST_BUFFER | HIF_DATA_VALID,
219                                         mbuf);
220                 } else {
221                         hif_lib_xmit_pkt(&priv->client, queue->queue_id,
222                                 (void *)(size_t)rte_pktmbuf_iova(tx_pkts[i]),
223                                 tx_pkts[i]->buf_addr + tx_pkts[i]->data_off,
224                                 tx_pkts[i]->pkt_len, 0 /*ctrl*/,
225                                 HIF_FIRST_BUFFER | HIF_LAST_BUFFER |
226                                 HIF_DATA_VALID,
227                                 tx_pkts[i]);
228                 }
229                 stats->obytes += tx_pkts[i]->pkt_len;
230                 hif_tx_dma_start();
231         }
232         stats->opackets += nb_pkts;
233         pfe_tx_do_cleanup(priv->pfe);
234
235         return nb_pkts;
236 }
237
238 static uint16_t
239 pfe_dummy_xmit_pkts(__rte_unused void *tx_queue,
240                 __rte_unused struct rte_mbuf **tx_pkts,
241                 __rte_unused uint16_t nb_pkts)
242 {
243         return 0;
244 }
245
246 static uint16_t
247 pfe_dummy_recv_pkts(__rte_unused void *rxq,
248                 __rte_unused struct rte_mbuf **rx_pkts,
249                 __rte_unused uint16_t nb_pkts)
250 {
251         return 0;
252 }
253
254 static int
255 pfe_eth_open(struct rte_eth_dev *dev)
256 {
257         struct pfe_eth_priv_s *priv = dev->data->dev_private;
258         struct hif_client_s *client;
259         struct hif_shm *hif_shm;
260         int rc;
261
262         /* Register client driver with HIF */
263         client = &priv->client;
264
265         if (client->pfe) {
266                 hif_shm = client->pfe->hif.shm;
267                 /* TODO please remove the below code of if block, once we add
268                  * the proper cleanup in eth_close
269                  */
270                 if (!test_bit(PFE_CL_GEM0 + priv->id,
271                               &hif_shm->g_client_status[0])) {
272                         /* Register client driver with HIF */
273                         memset(client, 0, sizeof(*client));
274                         client->id = PFE_CL_GEM0 + priv->id;
275                         client->tx_qn = emac_txq_cnt;
276                         client->rx_qn = EMAC_RXQ_CNT;
277                         client->priv = priv;
278                         client->pfe = priv->pfe;
279                         client->port_id = dev->data->port_id;
280                         client->event_handler = pfe_eth_event_handler;
281
282                         client->tx_qsize = EMAC_TXQ_DEPTH;
283                         client->rx_qsize = EMAC_RXQ_DEPTH;
284
285                         rc = hif_lib_client_register(client);
286                         if (rc) {
287                                 PFE_PMD_ERR("hif_lib_client_register(%d)"
288                                             " failed", client->id);
289                                 goto err0;
290                         }
291                 } else {
292                         /* Freeing the packets if already exists */
293                         int ret = 0;
294                         struct rte_mbuf *rx_pkts[32];
295                         /* TODO multiqueue support */
296                         ret = hif_lib_receive_pkt(&client->rx_q[0],
297                                                   hif_shm->pool, rx_pkts, 32);
298                         while (ret) {
299                                 int i;
300                                 for (i = 0; i < ret; i++)
301                                         rte_pktmbuf_free(rx_pkts[i]);
302                                 ret = hif_lib_receive_pkt(&client->rx_q[0],
303                                                           hif_shm->pool,
304                                                           rx_pkts, 32);
305                         }
306                 }
307         } else {
308                 /* Register client driver with HIF */
309                 memset(client, 0, sizeof(*client));
310                 client->id = PFE_CL_GEM0 + priv->id;
311                 client->tx_qn = emac_txq_cnt;
312                 client->rx_qn = EMAC_RXQ_CNT;
313                 client->priv = priv;
314                 client->pfe = priv->pfe;
315                 client->port_id = dev->data->port_id;
316                 client->event_handler = pfe_eth_event_handler;
317
318                 client->tx_qsize = EMAC_TXQ_DEPTH;
319                 client->rx_qsize = EMAC_RXQ_DEPTH;
320
321                 rc = hif_lib_client_register(client);
322                 if (rc) {
323                         PFE_PMD_ERR("hif_lib_client_register(%d) failed",
324                                     client->id);
325                         goto err0;
326                 }
327         }
328         rc = pfe_eth_start(priv);
329         dev->rx_pkt_burst = &pfe_recv_pkts;
330         dev->tx_pkt_burst = &pfe_xmit_pkts;
331         /* If no prefetch is configured. */
332         if (getenv("PFE_INTR_SUPPORT")) {
333                 dev->rx_pkt_burst = &pfe_recv_pkts_on_intr;
334                 PFE_PMD_INFO("PFE INTERRUPT Mode enabled");
335         }
336
337
338 err0:
339         return rc;
340 }
341
342 static int
343 pfe_eth_open_cdev(struct pfe_eth_priv_s *priv)
344 {
345         int pfe_cdev_fd;
346
347         if (priv == NULL)
348                 return -1;
349
350         pfe_cdev_fd = open(PFE_CDEV_PATH, O_RDONLY);
351         if (pfe_cdev_fd < 0) {
352                 PFE_PMD_WARN("Unable to open PFE device file (%s).\n",
353                              PFE_CDEV_PATH);
354                 PFE_PMD_WARN("Link status update will not be available.\n");
355                 priv->link_fd = PFE_CDEV_INVALID_FD;
356                 return -1;
357         }
358
359         priv->link_fd = pfe_cdev_fd;
360
361         return 0;
362 }
363
364 static void
365 pfe_eth_close_cdev(struct pfe_eth_priv_s *priv)
366 {
367         if (priv == NULL)
368                 return;
369
370         if (priv->link_fd != PFE_CDEV_INVALID_FD) {
371                 close(priv->link_fd);
372                 priv->link_fd = PFE_CDEV_INVALID_FD;
373         }
374 }
375
376 static void
377 pfe_eth_stop(struct rte_eth_dev *dev/*, int wake*/)
378 {
379         struct pfe_eth_priv_s *priv = dev->data->dev_private;
380
381         gemac_disable(priv->EMAC_baseaddr);
382         gpi_disable(priv->GPI_baseaddr);
383
384         dev->rx_pkt_burst = &pfe_dummy_recv_pkts;
385         dev->tx_pkt_burst = &pfe_dummy_xmit_pkts;
386 }
387
388 static void
389 pfe_eth_exit(struct rte_eth_dev *dev, struct pfe *pfe)
390 {
391         PMD_INIT_FUNC_TRACE();
392
393         pfe_eth_stop(dev);
394         /* Close the device file for link status */
395         pfe_eth_close_cdev(dev->data->dev_private);
396
397         rte_free(dev->data->mac_addrs);
398         rte_eth_dev_release_port(dev);
399         pfe->nb_devs--;
400 }
401
402 static void
403 pfe_eth_close(struct rte_eth_dev *dev)
404 {
405         if (!dev)
406                 return;
407
408         if (!g_pfe)
409                 return;
410
411         pfe_eth_exit(dev, g_pfe);
412
413         if (g_pfe->nb_devs == 0) {
414                 pfe_hif_exit(g_pfe);
415                 pfe_hif_lib_exit(g_pfe);
416                 rte_free(g_pfe);
417                 g_pfe = NULL;
418         }
419 }
420
421 static int
422 pfe_eth_configure(struct rte_eth_dev *dev __rte_unused)
423 {
424         return 0;
425 }
426
427 static int
428 pfe_eth_info(struct rte_eth_dev *dev,
429                 struct rte_eth_dev_info *dev_info)
430 {
431         struct pfe_eth_priv_s *internals = dev->data->dev_private;
432
433         dev_info->if_index = internals->id;
434         dev_info->max_mac_addrs = PFE_MAX_MACS;
435         dev_info->max_rx_queues = dev->data->nb_rx_queues;
436         dev_info->max_tx_queues = dev->data->nb_tx_queues;
437         dev_info->min_rx_bufsize = HIF_RX_PKT_MIN_SIZE;
438         dev_info->rx_offload_capa = dev_rx_offloads_sup;
439         dev_info->tx_offload_capa = dev_tx_offloads_sup;
440         if (pfe_svr == SVR_LS1012A_REV1)
441                 dev_info->max_rx_pktlen = MAX_MTU_ON_REV1 + PFE_ETH_OVERHEAD;
442         else
443                 dev_info->max_rx_pktlen = JUMBO_FRAME_SIZE;
444
445         return 0;
446 }
447
448 /* Only first mb_pool given on first call of this API will be used
449  * in whole system, also nb_rx_desc and rx_conf are unused params
450  */
451 static int
452 pfe_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
453                 __rte_unused uint16_t nb_rx_desc,
454                 __rte_unused unsigned int socket_id,
455                 __rte_unused const struct rte_eth_rxconf *rx_conf,
456                 struct rte_mempool *mb_pool)
457 {
458         int rc = 0;
459         struct pfe *pfe;
460         struct pfe_eth_priv_s *priv = dev->data->dev_private;
461
462         pfe = priv->pfe;
463
464         if (queue_idx >= EMAC_RXQ_CNT) {
465                 PFE_PMD_ERR("Invalid queue idx = %d, Max queues = %d",
466                                 queue_idx, EMAC_RXQ_CNT);
467                 return -1;
468         }
469
470         if (!pfe->hif.setuped) {
471                 rc = pfe_hif_shm_init(pfe->hif.shm, mb_pool);
472                 if (rc) {
473                         PFE_PMD_ERR("Could not allocate buffer descriptors");
474                         return -1;
475                 }
476
477                 pfe->hif.shm->pool = mb_pool;
478                 if (pfe_hif_init_buffers(&pfe->hif)) {
479                         PFE_PMD_ERR("Could not initialize buffer descriptors");
480                         return -1;
481                 }
482                 hif_init();
483                 hif_rx_enable();
484                 hif_tx_enable();
485                 pfe->hif.setuped = 1;
486         }
487         dev->data->rx_queues[queue_idx] = &priv->client.rx_q[queue_idx];
488         priv->client.rx_q[queue_idx].queue_id = queue_idx;
489
490         return 0;
491 }
492
493 static void
494 pfe_rx_queue_release(void *q __rte_unused)
495 {
496         PMD_INIT_FUNC_TRACE();
497 }
498
499 static void
500 pfe_tx_queue_release(void *q __rte_unused)
501 {
502         PMD_INIT_FUNC_TRACE();
503 }
504
505 static int
506 pfe_tx_queue_setup(struct rte_eth_dev *dev,
507                    uint16_t queue_idx,
508                    __rte_unused uint16_t nb_desc,
509                    __rte_unused unsigned int socket_id,
510                    __rte_unused const struct rte_eth_txconf *tx_conf)
511 {
512         struct pfe_eth_priv_s *priv = dev->data->dev_private;
513
514         if (queue_idx >= emac_txq_cnt) {
515                 PFE_PMD_ERR("Invalid queue idx = %d, Max queues = %d",
516                                 queue_idx, emac_txq_cnt);
517                 return -1;
518         }
519         dev->data->tx_queues[queue_idx] = &priv->client.tx_q[queue_idx];
520         priv->client.tx_q[queue_idx].queue_id = queue_idx;
521         return 0;
522 }
523
524 static const uint32_t *
525 pfe_supported_ptypes_get(struct rte_eth_dev *dev)
526 {
527         static const uint32_t ptypes[] = {
528                 /*todo -= add more types */
529                 RTE_PTYPE_L2_ETHER,
530                 RTE_PTYPE_L3_IPV4,
531                 RTE_PTYPE_L3_IPV4_EXT,
532                 RTE_PTYPE_L3_IPV6,
533                 RTE_PTYPE_L3_IPV6_EXT,
534                 RTE_PTYPE_L4_TCP,
535                 RTE_PTYPE_L4_UDP,
536                 RTE_PTYPE_L4_SCTP
537         };
538
539         if (dev->rx_pkt_burst == pfe_recv_pkts ||
540                         dev->rx_pkt_burst == pfe_recv_pkts_on_intr)
541                 return ptypes;
542         return NULL;
543 }
544
545 static int
546 pfe_stats_get(struct rte_eth_dev *dev,
547               struct rte_eth_stats *stats)
548 {
549         struct pfe_eth_priv_s *priv = dev->data->dev_private;
550         struct rte_eth_stats *eth_stats = &priv->stats;
551
552         if (stats == NULL)
553                 return -1;
554
555         memset(stats, 0, sizeof(struct rte_eth_stats));
556
557         stats->ipackets = eth_stats->ipackets;
558         stats->ibytes = eth_stats->ibytes;
559         stats->opackets = eth_stats->opackets;
560         stats->obytes = eth_stats->obytes;
561
562         return 0;
563 }
564
565 static const struct eth_dev_ops ops = {
566         .dev_start = pfe_eth_open,
567         .dev_stop = pfe_eth_stop,
568         .dev_close = pfe_eth_close,
569         .dev_configure = pfe_eth_configure,
570         .dev_infos_get = pfe_eth_info,
571         .rx_queue_setup = pfe_rx_queue_setup,
572         .rx_queue_release  = pfe_rx_queue_release,
573         .tx_queue_setup = pfe_tx_queue_setup,
574         .tx_queue_release  = pfe_tx_queue_release,
575         .dev_supported_ptypes_get = pfe_supported_ptypes_get,
576         .stats_get            = pfe_stats_get,
577 };
578
579 static int
580 pfe_eth_init(struct rte_vdev_device *vdev, struct pfe *pfe, int id)
581 {
582         struct rte_eth_dev *eth_dev = NULL;
583         struct pfe_eth_priv_s *priv = NULL;
584         struct ls1012a_eth_platform_data *einfo;
585         struct ls1012a_pfe_platform_data *pfe_info;
586         int err;
587
588         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
589         if (eth_dev == NULL)
590                 return -ENOMEM;
591
592         /* Extract pltform data */
593         pfe_info = (struct ls1012a_pfe_platform_data *)&pfe->platform_data;
594         if (!pfe_info) {
595                 PFE_PMD_ERR("pfe missing additional platform data");
596                 err = -ENODEV;
597                 goto err0;
598         }
599
600         einfo = (struct ls1012a_eth_platform_data *)pfe_info->ls1012a_eth_pdata;
601
602         /* einfo never be NULL, but no harm in having this check */
603         if (!einfo) {
604                 PFE_PMD_ERR("pfe missing additional gemacs platform data");
605                 err = -ENODEV;
606                 goto err0;
607         }
608
609         priv = eth_dev->data->dev_private;
610         priv->ndev = eth_dev;
611         priv->id = einfo[id].gem_id;
612         priv->pfe = pfe;
613
614         pfe->eth.eth_priv[id] = priv;
615
616         /* Set the info in the priv to the current info */
617         priv->einfo = &einfo[id];
618         priv->EMAC_baseaddr = cbus_emac_base[id];
619         priv->PHY_baseaddr = cbus_emac_base[id];
620         priv->GPI_baseaddr = cbus_gpi_base[id];
621
622 #define HIF_GEMAC_TMUQ_BASE     6
623         priv->low_tmu_q = HIF_GEMAC_TMUQ_BASE + (id * 2);
624         priv->high_tmu_q = priv->low_tmu_q + 1;
625
626         rte_spinlock_init(&priv->lock);
627
628         /* Copy the station address into the dev structure, */
629         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr",
630                         ETHER_ADDR_LEN * PFE_MAX_MACS, 0);
631         if (eth_dev->data->mac_addrs == NULL) {
632                 PFE_PMD_ERR("Failed to allocate mem %d to store MAC addresses",
633                         ETHER_ADDR_LEN * PFE_MAX_MACS);
634                 err = -ENOMEM;
635                 goto err0;
636         }
637
638         eth_dev->data->mtu = 1500;
639         eth_dev->dev_ops = &ops;
640         pfe_eth_stop(eth_dev);
641         pfe_gemac_init(priv);
642
643         eth_dev->data->nb_rx_queues = 1;
644         eth_dev->data->nb_tx_queues = 1;
645
646         /* For link status, open the PFE CDEV; Error from this function
647          * is silently ignored; In case of error, the link status will not
648          * be available.
649          */
650         pfe_eth_open_cdev(priv);
651         rte_eth_dev_probing_finish(eth_dev);
652
653         return 0;
654 err0:
655         rte_eth_dev_release_port(eth_dev);
656         return err;
657 }
658
659 static int
660 pfe_get_gemac_if_proprties(struct pfe *pfe,
661                 __rte_unused const struct device_node *parent,
662                 unsigned int port, unsigned int if_cnt,
663                 struct ls1012a_pfe_platform_data *pdata)
664 {
665         const struct device_node *gem = NULL;
666         size_t size;
667         unsigned int ii = 0, phy_id = 0;
668         const u32 *addr;
669         const void *mac_addr;
670
671         for (ii = 0; ii < if_cnt; ii++) {
672                 gem = of_get_next_child(parent, gem);
673                 if (!gem)
674                         goto err;
675                 addr = of_get_property(gem, "reg", &size);
676                 if (addr && (rte_be_to_cpu_32((unsigned int)*addr) == port))
677                         break;
678         }
679
680         if (ii >= if_cnt) {
681                 PFE_PMD_ERR("Failed to find interface = %d", if_cnt);
682                 goto err;
683         }
684
685         pdata->ls1012a_eth_pdata[port].gem_id = port;
686
687         mac_addr = of_get_mac_address(gem);
688
689         if (mac_addr) {
690                 memcpy(pdata->ls1012a_eth_pdata[port].mac_addr, mac_addr,
691                        ETH_ALEN);
692         }
693
694         addr = of_get_property(gem, "fsl,mdio-mux-val", &size);
695         if (!addr) {
696                 PFE_PMD_ERR("Invalid mdio-mux-val....");
697         } else {
698                 phy_id = rte_be_to_cpu_32((unsigned int)*addr);
699                 pdata->ls1012a_eth_pdata[port].mdio_muxval = phy_id;
700         }
701         if (pdata->ls1012a_eth_pdata[port].phy_id < 32)
702                 pfe->mdio_muxval[pdata->ls1012a_eth_pdata[port].phy_id] =
703                          pdata->ls1012a_eth_pdata[port].mdio_muxval;
704
705         return 0;
706
707 err:
708         return -1;
709 }
710
711 /* Parse integer from integer argument */
712 static int
713 parse_integer_arg(const char *key __rte_unused,
714                 const char *value, void *extra_args)
715 {
716         int i;
717         char *end;
718         errno = 0;
719
720         i = strtol(value, &end, 10);
721         if (*end != 0 || errno != 0 || i < 0 || i > 1) {
722                 PFE_PMD_ERR("Supported Port IDS are 0 and 1");
723                 return -EINVAL;
724         }
725
726         *((uint32_t *)extra_args) = i;
727
728         return 0;
729 }
730
731 static int
732 pfe_parse_vdev_init_params(struct pfe_vdev_init_params *params,
733                            struct rte_vdev_device *dev)
734 {
735         struct rte_kvargs *kvlist = NULL;
736         int ret = 0;
737
738         static const char * const pfe_vdev_valid_params[] = {
739                 PFE_VDEV_GEM_ID_ARG,
740                 NULL
741         };
742
743         const char *input_args = rte_vdev_device_args(dev);
744
745         if (!input_args)
746                 return -1;
747
748         kvlist = rte_kvargs_parse(input_args, pfe_vdev_valid_params);
749         if (kvlist == NULL)
750                 return -1;
751
752         ret = rte_kvargs_process(kvlist,
753                                 PFE_VDEV_GEM_ID_ARG,
754                                 &parse_integer_arg,
755                                 &params->gem_id);
756         rte_kvargs_free(kvlist);
757         return ret;
758 }
759
760 static int
761 pmd_pfe_probe(struct rte_vdev_device *vdev)
762 {
763         const u32 *prop;
764         const struct device_node *np;
765         const char *name;
766         const uint32_t *addr;
767         uint64_t cbus_addr, ddr_size, cbus_size;
768         int rc = -1, fd = -1, gem_id;
769         unsigned int ii, interface_count = 0;
770         size_t size = 0;
771         struct pfe_vdev_init_params init_params = {
772                 .gem_id = -1
773         };
774
775         name = rte_vdev_device_name(vdev);
776         rc = pfe_parse_vdev_init_params(&init_params, vdev);
777         if (rc < 0)
778                 return -EINVAL;
779
780         RTE_LOG(INFO, PMD, "Initializing pmd_pfe for %s Given gem-id %d\n",
781                 name, init_params.gem_id);
782
783         if (g_pfe) {
784                 if (g_pfe->nb_devs >= g_pfe->max_intf) {
785                         PFE_PMD_ERR("PFE %d dev already created Max is %d",
786                                 g_pfe->nb_devs, g_pfe->max_intf);
787                         return -EINVAL;
788                 }
789                 goto eth_init;
790         }
791
792         g_pfe = rte_zmalloc(NULL, sizeof(*g_pfe), RTE_CACHE_LINE_SIZE);
793         if (g_pfe == NULL)
794                 return  -EINVAL;
795
796         /* Load the device-tree driver */
797         rc = of_init();
798         if (rc) {
799                 PFE_PMD_ERR("of_init failed with ret: %d", rc);
800                 goto err;
801         }
802
803         np = of_find_compatible_node(NULL, NULL, "fsl,pfe");
804         if (!np) {
805                 PFE_PMD_ERR("Invalid device node");
806                 rc = -EINVAL;
807                 goto err;
808         }
809
810         addr = of_get_address(np, 0, &cbus_size, NULL);
811         if (!addr) {
812                 PFE_PMD_ERR("of_get_address cannot return qman address\n");
813                 goto err;
814         }
815         cbus_addr = of_translate_address(np, addr);
816         if (!cbus_addr) {
817                 PFE_PMD_ERR("of_translate_address failed\n");
818                 goto err;
819         }
820
821         addr = of_get_address(np, 1, &ddr_size, NULL);
822         if (!addr) {
823                 PFE_PMD_ERR("of_get_address cannot return qman address\n");
824                 goto err;
825         }
826
827         g_pfe->ddr_phys_baseaddr = of_translate_address(np, addr);
828         if (!g_pfe->ddr_phys_baseaddr) {
829                 PFE_PMD_ERR("of_translate_address failed\n");
830                 goto err;
831         }
832
833         g_pfe->ddr_baseaddr = pfe_mem_ptov(g_pfe->ddr_phys_baseaddr);
834         g_pfe->ddr_size = ddr_size;
835         g_pfe->cbus_size = cbus_size;
836
837         fd = open("/dev/mem", O_RDWR);
838         g_pfe->cbus_baseaddr = mmap(NULL, cbus_size, PROT_READ | PROT_WRITE,
839                                         MAP_SHARED, fd, cbus_addr);
840         close(fd);
841         if (g_pfe->cbus_baseaddr == MAP_FAILED) {
842                 PFE_PMD_ERR("Can not map cbus base");
843                 rc = -EINVAL;
844                 goto err;
845         }
846
847         /* Read interface count */
848         prop = of_get_property(np, "fsl,pfe-num-interfaces", &size);
849         if (!prop) {
850                 PFE_PMD_ERR("Failed to read number of interfaces");
851                 rc = -ENXIO;
852                 goto err_prop;
853         }
854
855         interface_count = rte_be_to_cpu_32((unsigned int)*prop);
856         if (interface_count <= 0) {
857                 PFE_PMD_ERR("No ethernet interface count : %d",
858                                 interface_count);
859                 rc = -ENXIO;
860                 goto err_prop;
861         }
862         PFE_PMD_INFO("num interfaces = %d ", interface_count);
863
864         g_pfe->max_intf  = interface_count;
865         g_pfe->platform_data.ls1012a_mdio_pdata[0].phy_mask = 0xffffffff;
866
867         for (ii = 0; ii < interface_count; ii++) {
868                 pfe_get_gemac_if_proprties(g_pfe, np, ii, interface_count,
869                                            &g_pfe->platform_data);
870         }
871
872         pfe_lib_init(g_pfe->cbus_baseaddr, g_pfe->ddr_baseaddr,
873                      g_pfe->ddr_phys_baseaddr, g_pfe->ddr_size);
874
875         PFE_PMD_INFO("CLASS version: %x", readl(CLASS_VERSION));
876         PFE_PMD_INFO("TMU version: %x", readl(TMU_VERSION));
877
878         PFE_PMD_INFO("BMU1 version: %x", readl(BMU1_BASE_ADDR + BMU_VERSION));
879         PFE_PMD_INFO("BMU2 version: %x", readl(BMU2_BASE_ADDR + BMU_VERSION));
880
881         PFE_PMD_INFO("EGPI1 version: %x", readl(EGPI1_BASE_ADDR + GPI_VERSION));
882         PFE_PMD_INFO("EGPI2 version: %x", readl(EGPI2_BASE_ADDR + GPI_VERSION));
883         PFE_PMD_INFO("HGPI version: %x", readl(HGPI_BASE_ADDR + GPI_VERSION));
884
885         PFE_PMD_INFO("HIF version: %x", readl(HIF_VERSION));
886         PFE_PMD_INFO("HIF NOPCY version: %x", readl(HIF_NOCPY_VERSION));
887
888         cbus_emac_base[0] = EMAC1_BASE_ADDR;
889         cbus_emac_base[1] = EMAC2_BASE_ADDR;
890
891         cbus_gpi_base[0] = EGPI1_BASE_ADDR;
892         cbus_gpi_base[1] = EGPI2_BASE_ADDR;
893
894         rc = pfe_hif_lib_init(g_pfe);
895         if (rc < 0)
896                 goto err_hif_lib;
897
898         rc = pfe_hif_init(g_pfe);
899         if (rc < 0)
900                 goto err_hif;
901         pfe_soc_version_get();
902 eth_init:
903         if (init_params.gem_id < 0)
904                 gem_id = g_pfe->nb_devs;
905         else
906                 gem_id = init_params.gem_id;
907
908         RTE_LOG(INFO, PMD, "Init pmd_pfe for %s gem-id %d(given =%d)\n",
909                 name, gem_id, init_params.gem_id);
910
911         rc = pfe_eth_init(vdev, g_pfe, gem_id);
912         if (rc < 0)
913                 goto err_eth;
914         else
915                 g_pfe->nb_devs++;
916
917         return 0;
918
919 err_eth:
920         pfe_hif_exit(g_pfe);
921
922 err_hif:
923         pfe_hif_lib_exit(g_pfe);
924
925 err_hif_lib:
926 err_prop:
927         munmap(g_pfe->cbus_baseaddr, cbus_size);
928 err:
929         rte_free(g_pfe);
930         return rc;
931 }
932
933 static int
934 pmd_pfe_remove(struct rte_vdev_device *vdev)
935 {
936         const char *name;
937         struct rte_eth_dev *eth_dev = NULL;
938
939         name = rte_vdev_device_name(vdev);
940         if (name == NULL)
941                 return -EINVAL;
942
943         PFE_PMD_INFO("Closing eventdev sw device %s", name);
944
945         if (!g_pfe)
946                 return 0;
947
948         eth_dev = rte_eth_dev_allocated(name);
949         if (eth_dev == NULL)
950                 return -ENODEV;
951
952         pfe_eth_exit(eth_dev, g_pfe);
953         munmap(g_pfe->cbus_baseaddr, g_pfe->cbus_size);
954
955         if (g_pfe->nb_devs == 0) {
956                 pfe_hif_exit(g_pfe);
957                 pfe_hif_lib_exit(g_pfe);
958                 rte_free(g_pfe);
959                 g_pfe = NULL;
960         }
961         return 0;
962 }
963
964 static
965 struct rte_vdev_driver pmd_pfe_drv = {
966         .probe = pmd_pfe_probe,
967         .remove = pmd_pfe_remove,
968 };
969
970 RTE_PMD_REGISTER_VDEV(PFE_NAME_PMD, pmd_pfe_drv);
971 RTE_PMD_REGISTER_PARAM_STRING(PFE_NAME_PMD, PFE_VDEV_GEM_ID_ARG "=<int> ");
972
973 RTE_INIT(pfe_pmd_init_log)
974 {
975         pfe_logtype_pmd = rte_log_register("pmd.net.pfe");
976         if (pfe_logtype_pmd >= 0)
977                 rte_log_set_level(pfe_logtype_pmd, RTE_LOG_NOTICE);
978 }