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