d6adbbc938261dce13a4473a188588d9511ff415
[dpdk.git] / drivers / net / octeontx / octeontx_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include <rte_alarm.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_debug.h>
15 #include <rte_devargs.h>
16 #include <rte_dev.h>
17 #include <rte_kvargs.h>
18 #include <rte_malloc.h>
19 #include <rte_mbuf_pool_ops.h>
20 #include <rte_prefetch.h>
21 #include <rte_bus_vdev.h>
22
23 #include "octeontx_ethdev.h"
24 #include "octeontx_rxtx.h"
25 #include "octeontx_logs.h"
26
27 struct evdev_priv_data {
28         OFFLOAD_FLAGS; /*Sequence should not be changed */
29 } __rte_cache_aligned;
30
31 struct octeontx_vdev_init_params {
32         uint8_t nr_port;
33 };
34
35 uint16_t
36 rte_octeontx_pchan_map[OCTEONTX_MAX_BGX_PORTS][OCTEONTX_MAX_LMAC_PER_BGX];
37
38 enum octeontx_link_speed {
39         OCTEONTX_LINK_SPEED_SGMII,
40         OCTEONTX_LINK_SPEED_XAUI,
41         OCTEONTX_LINK_SPEED_RXAUI,
42         OCTEONTX_LINK_SPEED_10G_R,
43         OCTEONTX_LINK_SPEED_40G_R,
44         OCTEONTX_LINK_SPEED_RESERVE1,
45         OCTEONTX_LINK_SPEED_QSGMII,
46         OCTEONTX_LINK_SPEED_RESERVE2
47 };
48
49 int otx_net_logtype_mbox;
50 int otx_net_logtype_init;
51 int otx_net_logtype_driver;
52
53 RTE_INIT(otx_net_init_log)
54 {
55         otx_net_logtype_mbox = rte_log_register("pmd.net.octeontx.mbox");
56         if (otx_net_logtype_mbox >= 0)
57                 rte_log_set_level(otx_net_logtype_mbox, RTE_LOG_NOTICE);
58
59         otx_net_logtype_init = rte_log_register("pmd.net.octeontx.init");
60         if (otx_net_logtype_init >= 0)
61                 rte_log_set_level(otx_net_logtype_init, RTE_LOG_NOTICE);
62
63         otx_net_logtype_driver = rte_log_register("pmd.net.octeontx.driver");
64         if (otx_net_logtype_driver >= 0)
65                 rte_log_set_level(otx_net_logtype_driver, RTE_LOG_NOTICE);
66 }
67
68 /* Parse integer from integer argument */
69 static int
70 parse_integer_arg(const char *key __rte_unused,
71                 const char *value, void *extra_args)
72 {
73         int *i = (int *)extra_args;
74
75         *i = atoi(value);
76         if (*i < 0) {
77                 octeontx_log_err("argument has to be positive.");
78                 return -1;
79         }
80
81         return 0;
82 }
83
84 static int
85 octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
86                                 struct rte_vdev_device *dev)
87 {
88         struct rte_kvargs *kvlist = NULL;
89         int ret = 0;
90
91         static const char * const octeontx_vdev_valid_params[] = {
92                 OCTEONTX_VDEV_NR_PORT_ARG,
93                 NULL
94         };
95
96         const char *input_args = rte_vdev_device_args(dev);
97         if (params == NULL)
98                 return -EINVAL;
99
100
101         if (input_args) {
102                 kvlist = rte_kvargs_parse(input_args,
103                                 octeontx_vdev_valid_params);
104                 if (kvlist == NULL)
105                         return -1;
106
107                 ret = rte_kvargs_process(kvlist,
108                                         OCTEONTX_VDEV_NR_PORT_ARG,
109                                         &parse_integer_arg,
110                                         &params->nr_port);
111                 if (ret < 0)
112                         goto free_kvlist;
113         }
114
115 free_kvlist:
116         rte_kvargs_free(kvlist);
117         return ret;
118 }
119
120 static int
121 octeontx_port_open(struct octeontx_nic *nic)
122 {
123         octeontx_mbox_bgx_port_conf_t bgx_port_conf;
124         int res;
125
126         res = 0;
127         memset(&bgx_port_conf, 0x0, sizeof(bgx_port_conf));
128         PMD_INIT_FUNC_TRACE();
129
130         res = octeontx_bgx_port_open(nic->port_id, &bgx_port_conf);
131         if (res < 0) {
132                 octeontx_log_err("failed to open port %d", res);
133                 return res;
134         }
135
136         nic->node = bgx_port_conf.node;
137         nic->port_ena = bgx_port_conf.enable;
138         nic->base_ichan = bgx_port_conf.base_chan;
139         nic->base_ochan = bgx_port_conf.base_chan;
140         nic->num_ichans = bgx_port_conf.num_chans;
141         nic->num_ochans = bgx_port_conf.num_chans;
142         nic->mtu = bgx_port_conf.mtu;
143         nic->bpen = bgx_port_conf.bpen;
144         nic->fcs_strip = bgx_port_conf.fcs_strip;
145         nic->bcast_mode = bgx_port_conf.bcast_mode;
146         nic->mcast_mode = bgx_port_conf.mcast_mode;
147         nic->speed      = bgx_port_conf.mode;
148
149         memcpy(&nic->mac_addr[0], &bgx_port_conf.macaddr[0],
150                 RTE_ETHER_ADDR_LEN);
151
152         octeontx_log_dbg("port opened %d", nic->port_id);
153         return res;
154 }
155
156 static void
157 octeontx_port_close(struct octeontx_nic *nic)
158 {
159         PMD_INIT_FUNC_TRACE();
160
161         octeontx_bgx_port_close(nic->port_id);
162         octeontx_log_dbg("port closed %d", nic->port_id);
163 }
164
165 static int
166 octeontx_port_start(struct octeontx_nic *nic)
167 {
168         PMD_INIT_FUNC_TRACE();
169
170         return octeontx_bgx_port_start(nic->port_id);
171 }
172
173 static int
174 octeontx_port_stop(struct octeontx_nic *nic)
175 {
176         PMD_INIT_FUNC_TRACE();
177
178         return octeontx_bgx_port_stop(nic->port_id);
179 }
180
181 static int
182 octeontx_port_promisc_set(struct octeontx_nic *nic, int en)
183 {
184         struct rte_eth_dev *dev;
185         int res;
186
187         res = 0;
188         PMD_INIT_FUNC_TRACE();
189         dev = nic->dev;
190
191         res = octeontx_bgx_port_promisc_set(nic->port_id, en);
192         if (res < 0) {
193                 octeontx_log_err("failed to set promiscuous mode %d",
194                                 nic->port_id);
195                 return res;
196         }
197
198         /* Set proper flag for the mode */
199         dev->data->promiscuous = (en != 0) ? 1 : 0;
200
201         octeontx_log_dbg("port %d : promiscuous mode %s",
202                         nic->port_id, en ? "set" : "unset");
203
204         return 0;
205 }
206
207 static int
208 octeontx_port_stats(struct octeontx_nic *nic, struct rte_eth_stats *stats)
209 {
210         octeontx_mbox_bgx_port_stats_t bgx_stats;
211         int res;
212
213         PMD_INIT_FUNC_TRACE();
214
215         res = octeontx_bgx_port_stats(nic->port_id, &bgx_stats);
216         if (res < 0) {
217                 octeontx_log_err("failed to get port stats %d", nic->port_id);
218                 return res;
219         }
220
221         stats->ipackets = bgx_stats.rx_packets;
222         stats->ibytes = bgx_stats.rx_bytes;
223         stats->imissed = bgx_stats.rx_dropped;
224         stats->ierrors = bgx_stats.rx_errors;
225         stats->opackets = bgx_stats.tx_packets;
226         stats->obytes = bgx_stats.tx_bytes;
227         stats->oerrors = bgx_stats.tx_errors;
228
229         octeontx_log_dbg("port%d stats inpkts=%" PRIx64 " outpkts=%" PRIx64 "",
230                         nic->port_id, stats->ipackets, stats->opackets);
231
232         return 0;
233 }
234
235 static int
236 octeontx_port_stats_clr(struct octeontx_nic *nic)
237 {
238         PMD_INIT_FUNC_TRACE();
239
240         return octeontx_bgx_port_stats_clr(nic->port_id);
241 }
242
243 static inline void
244 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
245                                 struct rte_event_dev_info *info)
246 {
247         memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
248         dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
249
250         dev_conf->nb_event_ports = info->max_event_ports;
251         dev_conf->nb_event_queues = info->max_event_queues;
252
253         dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
254         dev_conf->nb_event_port_dequeue_depth =
255                         info->max_event_port_dequeue_depth;
256         dev_conf->nb_event_port_enqueue_depth =
257                         info->max_event_port_enqueue_depth;
258         dev_conf->nb_event_port_enqueue_depth =
259                         info->max_event_port_enqueue_depth;
260         dev_conf->nb_events_limit =
261                         info->max_num_events;
262 }
263
264 static uint16_t
265 octeontx_tx_offload_flags(struct rte_eth_dev *eth_dev)
266 {
267         struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
268         uint16_t flags = 0;
269
270         /* Created function for supoorting future offloads */
271         if (nic->tx_offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
272                 flags |= OCCTX_TX_MULTI_SEG_F;
273
274         return flags;
275 }
276
277 static uint16_t
278 octeontx_rx_offload_flags(struct rte_eth_dev *eth_dev)
279 {
280         struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
281         struct rte_eth_dev_data *data = eth_dev->data;
282         struct rte_eth_conf *conf = &data->dev_conf;
283         struct rte_eth_rxmode *rxmode = &conf->rxmode;
284         uint16_t flags = 0;
285
286         if (rxmode->mq_mode == ETH_MQ_RX_RSS)
287                 flags |= OCCTX_RX_OFFLOAD_RSS_F;
288
289         if (nic->rx_offloads & DEV_RX_OFFLOAD_SCATTER) {
290                 flags |= OCCTX_RX_MULTI_SEG_F;
291                 eth_dev->data->scattered_rx = 1;
292                 /* If scatter mode is enabled, TX should also be in multi
293                  * seg mode, else memory leak will occur
294                  */
295                 nic->tx_offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
296         }
297
298         return flags;
299 }
300
301 static int
302 octeontx_dev_configure(struct rte_eth_dev *dev)
303 {
304         struct rte_eth_dev_data *data = dev->data;
305         struct rte_eth_conf *conf = &data->dev_conf;
306         struct rte_eth_rxmode *rxmode = &conf->rxmode;
307         struct rte_eth_txmode *txmode = &conf->txmode;
308         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
309         int ret;
310
311         PMD_INIT_FUNC_TRACE();
312         RTE_SET_USED(conf);
313
314         if (!rte_eal_has_hugepages()) {
315                 octeontx_log_err("huge page is not configured");
316                 return -EINVAL;
317         }
318
319         if (txmode->mq_mode) {
320                 octeontx_log_err("tx mq_mode DCB or VMDq not supported");
321                 return -EINVAL;
322         }
323
324         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
325                 rxmode->mq_mode != ETH_MQ_RX_RSS) {
326                 octeontx_log_err("unsupported rx qmode %d", rxmode->mq_mode);
327                 return -EINVAL;
328         }
329
330         if (!(txmode->offloads & DEV_TX_OFFLOAD_MT_LOCKFREE)) {
331                 PMD_INIT_LOG(NOTICE, "cant disable lockfree tx");
332                 txmode->offloads |= DEV_TX_OFFLOAD_MT_LOCKFREE;
333         }
334
335         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
336                 octeontx_log_err("setting link speed/duplex not supported");
337                 return -EINVAL;
338         }
339
340         if (conf->dcb_capability_en) {
341                 octeontx_log_err("DCB enable not supported");
342                 return -EINVAL;
343         }
344
345         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
346                 octeontx_log_err("flow director not supported");
347                 return -EINVAL;
348         }
349
350         nic->num_tx_queues = dev->data->nb_tx_queues;
351
352         ret = octeontx_pko_channel_open(nic->pko_vfid * PKO_VF_NUM_DQ,
353                                         nic->num_tx_queues,
354                                         nic->base_ochan);
355         if (ret) {
356                 octeontx_log_err("failed to open channel %d no-of-txq %d",
357                            nic->base_ochan, nic->num_tx_queues);
358                 return -EFAULT;
359         }
360
361         nic->pki.classifier_enable = false;
362         nic->pki.hash_enable = true;
363         nic->pki.initialized = false;
364
365         nic->rx_offloads |= rxmode->offloads;
366         nic->tx_offloads |= txmode->offloads;
367         nic->rx_offload_flags |= octeontx_rx_offload_flags(dev);
368         nic->tx_offload_flags |= octeontx_tx_offload_flags(dev);
369
370         return 0;
371 }
372
373 static void
374 octeontx_dev_close(struct rte_eth_dev *dev)
375 {
376         struct octeontx_txq *txq = NULL;
377         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
378         unsigned int i;
379         int ret;
380
381         PMD_INIT_FUNC_TRACE();
382
383         rte_event_dev_close(nic->evdev);
384
385         ret = octeontx_pko_channel_close(nic->base_ochan);
386         if (ret < 0) {
387                 octeontx_log_err("failed to close channel %d VF%d %d %d",
388                              nic->base_ochan, nic->port_id, nic->num_tx_queues,
389                              ret);
390         }
391         /* Free txq resources for this port */
392         for (i = 0; i < nic->num_tx_queues; i++) {
393                 txq = dev->data->tx_queues[i];
394                 if (!txq)
395                         continue;
396
397                 rte_free(txq);
398         }
399
400         /* Free MAC address table */
401         rte_free(dev->data->mac_addrs);
402         dev->data->mac_addrs = NULL;
403
404         dev->tx_pkt_burst = NULL;
405         dev->rx_pkt_burst = NULL;
406 }
407
408 static int
409 octeontx_recheck_rx_offloads(struct octeontx_rxq *rxq)
410 {
411         struct rte_eth_dev *eth_dev = rxq->eth_dev;
412         struct octeontx_nic *nic = octeontx_pmd_priv(eth_dev);
413         struct rte_eth_dev_data *data = eth_dev->data;
414         struct rte_pktmbuf_pool_private *mbp_priv;
415         struct evdev_priv_data *evdev_priv;
416         struct rte_eventdev *dev;
417         uint32_t buffsz;
418
419         /* Get rx buffer size */
420         mbp_priv = rte_mempool_get_priv(rxq->pool);
421         buffsz = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
422
423         /* Setup scatter mode if needed by jumbo */
424         if (data->dev_conf.rxmode.max_rx_pkt_len > buffsz) {
425                 nic->rx_offloads |= DEV_RX_OFFLOAD_SCATTER;
426                 nic->rx_offload_flags |= octeontx_rx_offload_flags(eth_dev);
427                 nic->tx_offload_flags |= octeontx_tx_offload_flags(eth_dev);
428         }
429
430         /* Sharing offload flags via eventdev priv region */
431         dev = &rte_eventdevs[rxq->evdev];
432         evdev_priv = dev->data->dev_private;
433         evdev_priv->rx_offload_flags = nic->rx_offload_flags;
434         evdev_priv->tx_offload_flags = nic->tx_offload_flags;
435
436         return 0;
437 }
438
439 static int
440 octeontx_dev_start(struct rte_eth_dev *dev)
441 {
442         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
443         struct octeontx_rxq *rxq;
444         int ret = 0, i;
445
446         /* Rechecking if any new offload set to update
447          * rx/tx burst function pointer accordingly.
448          */
449         for (i = 0; i < dev->data->nb_rx_queues; i++) {
450                 rxq = dev->data->rx_queues[i];
451                 octeontx_recheck_rx_offloads(rxq);
452         }
453
454         PMD_INIT_FUNC_TRACE();
455         /*
456          * Tx start
457          */
458         octeontx_set_tx_function(dev);
459         ret = octeontx_pko_channel_start(nic->base_ochan);
460         if (ret < 0) {
461                 octeontx_log_err("fail to conf VF%d no. txq %d chan %d ret %d",
462                            nic->port_id, nic->num_tx_queues, nic->base_ochan,
463                            ret);
464                 goto error;
465         }
466
467         /*
468          * Rx start
469          */
470         dev->rx_pkt_burst = octeontx_recv_pkts;
471         ret = octeontx_pki_port_start(nic->port_id);
472         if (ret < 0) {
473                 octeontx_log_err("fail to start Rx on port %d", nic->port_id);
474                 goto channel_stop_error;
475         }
476
477         /*
478          * Start port
479          */
480         ret = octeontx_port_start(nic);
481         if (ret < 0) {
482                 octeontx_log_err("failed start port %d", ret);
483                 goto pki_port_stop_error;
484         }
485
486         PMD_TX_LOG(DEBUG, "pko: start channel %d no.of txq %d port %d",
487                         nic->base_ochan, nic->num_tx_queues, nic->port_id);
488
489         ret = rte_event_dev_start(nic->evdev);
490         if (ret < 0) {
491                 octeontx_log_err("failed to start evdev: ret (%d)", ret);
492                 goto pki_port_stop_error;
493         }
494
495         /* Success */
496         return ret;
497
498 pki_port_stop_error:
499         octeontx_pki_port_stop(nic->port_id);
500 channel_stop_error:
501         octeontx_pko_channel_stop(nic->base_ochan);
502 error:
503         return ret;
504 }
505
506 static void
507 octeontx_dev_stop(struct rte_eth_dev *dev)
508 {
509         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
510         int ret;
511
512         PMD_INIT_FUNC_TRACE();
513
514         rte_event_dev_stop(nic->evdev);
515
516         ret = octeontx_port_stop(nic);
517         if (ret < 0) {
518                 octeontx_log_err("failed to req stop port %d res=%d",
519                                         nic->port_id, ret);
520                 return;
521         }
522
523         ret = octeontx_pki_port_stop(nic->port_id);
524         if (ret < 0) {
525                 octeontx_log_err("failed to stop pki port %d res=%d",
526                                         nic->port_id, ret);
527                 return;
528         }
529
530         ret = octeontx_pko_channel_stop(nic->base_ochan);
531         if (ret < 0) {
532                 octeontx_log_err("failed to stop channel %d VF%d %d %d",
533                              nic->base_ochan, nic->port_id, nic->num_tx_queues,
534                              ret);
535                 return;
536         }
537 }
538
539 static int
540 octeontx_dev_promisc_enable(struct rte_eth_dev *dev)
541 {
542         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
543
544         PMD_INIT_FUNC_TRACE();
545         return octeontx_port_promisc_set(nic, 1);
546 }
547
548 static int
549 octeontx_dev_promisc_disable(struct rte_eth_dev *dev)
550 {
551         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
552
553         PMD_INIT_FUNC_TRACE();
554         return octeontx_port_promisc_set(nic, 0);
555 }
556
557 static int
558 octeontx_port_link_status(struct octeontx_nic *nic)
559 {
560         int res;
561
562         PMD_INIT_FUNC_TRACE();
563         res = octeontx_bgx_port_link_status(nic->port_id);
564         if (res < 0) {
565                 octeontx_log_err("failed to get port %d link status",
566                                 nic->port_id);
567                 return res;
568         }
569
570         nic->link_up = (uint8_t)res;
571         octeontx_log_dbg("port %d link status %d", nic->port_id, nic->link_up);
572
573         return res;
574 }
575
576 /*
577  * Return 0 means link status changed, -1 means not changed
578  */
579 static int
580 octeontx_dev_link_update(struct rte_eth_dev *dev,
581                          int wait_to_complete __rte_unused)
582 {
583         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
584         struct rte_eth_link link;
585         int res;
586
587         PMD_INIT_FUNC_TRACE();
588
589         res = octeontx_port_link_status(nic);
590         if (res < 0) {
591                 octeontx_log_err("failed to request link status %d", res);
592                 return res;
593         }
594
595         link.link_status = nic->link_up;
596
597         switch (nic->speed) {
598         case OCTEONTX_LINK_SPEED_SGMII:
599                 link.link_speed = ETH_SPEED_NUM_1G;
600                 break;
601
602         case OCTEONTX_LINK_SPEED_XAUI:
603                 link.link_speed = ETH_SPEED_NUM_10G;
604                 break;
605
606         case OCTEONTX_LINK_SPEED_RXAUI:
607         case OCTEONTX_LINK_SPEED_10G_R:
608                 link.link_speed = ETH_SPEED_NUM_10G;
609                 break;
610         case OCTEONTX_LINK_SPEED_QSGMII:
611                 link.link_speed = ETH_SPEED_NUM_5G;
612                 break;
613         case OCTEONTX_LINK_SPEED_40G_R:
614                 link.link_speed = ETH_SPEED_NUM_40G;
615                 break;
616
617         case OCTEONTX_LINK_SPEED_RESERVE1:
618         case OCTEONTX_LINK_SPEED_RESERVE2:
619         default:
620                 link.link_speed = ETH_SPEED_NUM_NONE;
621                 octeontx_log_err("incorrect link speed %d", nic->speed);
622                 break;
623         }
624
625         link.link_duplex = ETH_LINK_FULL_DUPLEX;
626         link.link_autoneg = ETH_LINK_AUTONEG;
627
628         return rte_eth_linkstatus_set(dev, &link);
629 }
630
631 static int
632 octeontx_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
633 {
634         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
635
636         PMD_INIT_FUNC_TRACE();
637         return octeontx_port_stats(nic, stats);
638 }
639
640 static int
641 octeontx_dev_stats_reset(struct rte_eth_dev *dev)
642 {
643         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
644
645         PMD_INIT_FUNC_TRACE();
646         return octeontx_port_stats_clr(nic);
647 }
648
649 static void
650 octeontx_dev_mac_addr_del(struct rte_eth_dev *dev, uint32_t index)
651 {
652         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
653         int ret;
654
655         ret = octeontx_bgx_port_mac_del(nic->port_id, index);
656         if (ret != 0)
657                 octeontx_log_err("failed to del MAC address filter on port %d",
658                                  nic->port_id);
659 }
660
661 static int
662 octeontx_dev_mac_addr_add(struct rte_eth_dev *dev,
663                           struct rte_ether_addr *mac_addr,
664                           uint32_t index,
665                           __rte_unused uint32_t vmdq)
666 {
667         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
668         int ret;
669
670         ret = octeontx_bgx_port_mac_add(nic->port_id, mac_addr->addr_bytes,
671                                         index);
672         if (ret < 0) {
673                 octeontx_log_err("failed to add MAC address filter on port %d",
674                                  nic->port_id);
675                 return ret;
676         }
677
678         return 0;
679 }
680
681 static int
682 octeontx_dev_default_mac_addr_set(struct rte_eth_dev *dev,
683                                         struct rte_ether_addr *addr)
684 {
685         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
686         int ret;
687
688         ret = octeontx_bgx_port_mac_set(nic->port_id, addr->addr_bytes);
689         if (ret == 0) {
690                 /* Update same mac address to BGX CAM table */
691                 ret = octeontx_bgx_port_mac_add(nic->port_id, addr->addr_bytes,
692                                                 0);
693         }
694         if (ret < 0) {
695                 octeontx_log_err("failed to set MAC address on port %d",
696                                  nic->port_id);
697         }
698
699         return ret;
700 }
701
702 static int
703 octeontx_dev_info(struct rte_eth_dev *dev,
704                 struct rte_eth_dev_info *dev_info)
705 {
706         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
707
708         /* Autonegotiation may be disabled */
709         dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
710         dev_info->speed_capa |= ETH_LINK_SPEED_10M | ETH_LINK_SPEED_100M |
711                         ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
712                         ETH_LINK_SPEED_40G;
713
714         dev_info->max_mac_addrs =
715                                 octeontx_bgx_port_mac_entries_get(nic->port_id);
716         dev_info->max_rx_pktlen = PKI_MAX_PKTLEN;
717         dev_info->max_rx_queues = 1;
718         dev_info->max_tx_queues = PKO_MAX_NUM_DQ;
719         dev_info->min_rx_bufsize = 0;
720
721         dev_info->default_rxconf = (struct rte_eth_rxconf) {
722                 .rx_free_thresh = 0,
723                 .rx_drop_en = 0,
724                 .offloads = OCTEONTX_RX_OFFLOADS,
725         };
726
727         dev_info->default_txconf = (struct rte_eth_txconf) {
728                 .tx_free_thresh = 0,
729                 .offloads = OCTEONTX_TX_OFFLOADS,
730         };
731
732         dev_info->rx_offload_capa = OCTEONTX_RX_OFFLOADS;
733         dev_info->tx_offload_capa = OCTEONTX_TX_OFFLOADS;
734         dev_info->rx_queue_offload_capa = OCTEONTX_RX_OFFLOADS;
735         dev_info->tx_queue_offload_capa = OCTEONTX_TX_OFFLOADS;
736
737         return 0;
738 }
739
740 static void
741 octeontx_dq_info_getter(octeontx_dq_t *dq, void *out)
742 {
743         ((octeontx_dq_t *)out)->lmtline_va = dq->lmtline_va;
744         ((octeontx_dq_t *)out)->ioreg_va = dq->ioreg_va;
745         ((octeontx_dq_t *)out)->fc_status_va = dq->fc_status_va;
746 }
747
748 static int
749 octeontx_vf_start_tx_queue(struct rte_eth_dev *dev, struct octeontx_nic *nic,
750                                 uint16_t qidx)
751 {
752         struct octeontx_txq *txq;
753         int res;
754
755         PMD_INIT_FUNC_TRACE();
756
757         if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
758                 return 0;
759
760         txq = dev->data->tx_queues[qidx];
761
762         res = octeontx_pko_channel_query_dqs(nic->base_ochan,
763                                                 &txq->dq,
764                                                 sizeof(octeontx_dq_t),
765                                                 txq->queue_id,
766                                                 octeontx_dq_info_getter);
767         if (res < 0) {
768                 res = -EFAULT;
769                 goto close_port;
770         }
771
772         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
773         return res;
774
775 close_port:
776         (void)octeontx_port_stop(nic);
777         octeontx_pko_channel_stop(nic->base_ochan);
778         octeontx_pko_channel_close(nic->base_ochan);
779         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
780         return res;
781 }
782
783 static int
784 octeontx_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t qidx)
785 {
786         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
787
788         PMD_INIT_FUNC_TRACE();
789         qidx = qidx % PKO_VF_NUM_DQ;
790         return octeontx_vf_start_tx_queue(dev, nic, qidx);
791 }
792
793 static inline int
794 octeontx_vf_stop_tx_queue(struct rte_eth_dev *dev, struct octeontx_nic *nic,
795                           uint16_t qidx)
796 {
797         int ret = 0;
798
799         RTE_SET_USED(nic);
800         PMD_INIT_FUNC_TRACE();
801
802         if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
803                 return 0;
804
805         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
806         return ret;
807 }
808
809 static int
810 octeontx_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx)
811 {
812         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
813
814         PMD_INIT_FUNC_TRACE();
815         qidx = qidx % PKO_VF_NUM_DQ;
816
817         return octeontx_vf_stop_tx_queue(dev, nic, qidx);
818 }
819
820 static void
821 octeontx_dev_tx_queue_release(void *tx_queue)
822 {
823         struct octeontx_txq *txq = tx_queue;
824         int res;
825
826         PMD_INIT_FUNC_TRACE();
827
828         if (txq) {
829                 res = octeontx_dev_tx_queue_stop(txq->eth_dev, txq->queue_id);
830                 if (res < 0)
831                         octeontx_log_err("failed stop tx_queue(%d)\n",
832                                    txq->queue_id);
833
834                 rte_free(txq);
835         }
836 }
837
838 static int
839 octeontx_dev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
840                             uint16_t nb_desc, unsigned int socket_id,
841                             const struct rte_eth_txconf *tx_conf __rte_unused)
842 {
843         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
844         struct octeontx_txq *txq = NULL;
845         uint16_t dq_num;
846         int res = 0;
847
848         RTE_SET_USED(nb_desc);
849         RTE_SET_USED(socket_id);
850
851         dq_num = (nic->pko_vfid * PKO_VF_NUM_DQ) + qidx;
852
853         /* Socket id check */
854         if (socket_id != (unsigned int)SOCKET_ID_ANY &&
855                         socket_id != (unsigned int)nic->node)
856                 PMD_TX_LOG(INFO, "socket_id expected %d, configured %d",
857                                                 socket_id, nic->node);
858
859         /* Free memory prior to re-allocation if needed. */
860         if (dev->data->tx_queues[qidx] != NULL) {
861                 PMD_TX_LOG(DEBUG, "freeing memory prior to re-allocation %d",
862                                 qidx);
863                 octeontx_dev_tx_queue_release(dev->data->tx_queues[qidx]);
864                 dev->data->tx_queues[qidx] = NULL;
865         }
866
867         /* Allocating tx queue data structure */
868         txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct octeontx_txq),
869                                  RTE_CACHE_LINE_SIZE, nic->node);
870         if (txq == NULL) {
871                 octeontx_log_err("failed to allocate txq=%d", qidx);
872                 res = -ENOMEM;
873                 goto err;
874         }
875
876         txq->eth_dev = dev;
877         txq->queue_id = dq_num;
878         dev->data->tx_queues[qidx] = txq;
879         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
880
881         res = octeontx_pko_channel_query_dqs(nic->base_ochan,
882                                                 &txq->dq,
883                                                 sizeof(octeontx_dq_t),
884                                                 txq->queue_id,
885                                                 octeontx_dq_info_getter);
886         if (res < 0) {
887                 res = -EFAULT;
888                 goto err;
889         }
890
891         PMD_TX_LOG(DEBUG, "[%d]:[%d] txq=%p nb_desc=%d lmtline=%p ioreg_va=%p fc_status_va=%p",
892                         qidx, txq->queue_id, txq, nb_desc, txq->dq.lmtline_va,
893                         txq->dq.ioreg_va,
894                         txq->dq.fc_status_va);
895
896         return res;
897
898 err:
899         if (txq)
900                 rte_free(txq);
901
902         return res;
903 }
904
905 static int
906 octeontx_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
907                                 uint16_t nb_desc, unsigned int socket_id,
908                                 const struct rte_eth_rxconf *rx_conf,
909                                 struct rte_mempool *mb_pool)
910 {
911         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
912         struct rte_mempool_ops *mp_ops = NULL;
913         struct octeontx_rxq *rxq = NULL;
914         pki_pktbuf_cfg_t pktbuf_conf;
915         pki_hash_cfg_t pki_hash;
916         pki_qos_cfg_t pki_qos;
917         uintptr_t pool;
918         int ret, port;
919         uint16_t gaura;
920         unsigned int ev_queues = (nic->ev_queues * nic->port_id) + qidx;
921         unsigned int ev_ports = (nic->ev_ports * nic->port_id) + qidx;
922
923         RTE_SET_USED(nb_desc);
924
925         memset(&pktbuf_conf, 0, sizeof(pktbuf_conf));
926         memset(&pki_hash, 0, sizeof(pki_hash));
927         memset(&pki_qos, 0, sizeof(pki_qos));
928
929         mp_ops = rte_mempool_get_ops(mb_pool->ops_index);
930         if (strcmp(mp_ops->name, "octeontx_fpavf")) {
931                 octeontx_log_err("failed to find octeontx_fpavf mempool");
932                 return -ENOTSUP;
933         }
934
935         /* Handle forbidden configurations */
936         if (nic->pki.classifier_enable) {
937                 octeontx_log_err("cannot setup queue %d. "
938                                         "Classifier option unsupported", qidx);
939                 return -EINVAL;
940         }
941
942         port = nic->port_id;
943
944         /* Rx deferred start is not supported */
945         if (rx_conf->rx_deferred_start) {
946                 octeontx_log_err("rx deferred start not supported");
947                 return -EINVAL;
948         }
949
950         /* Verify queue index */
951         if (qidx >= dev->data->nb_rx_queues) {
952                 octeontx_log_err("QID %d not supporteded (0 - %d available)\n",
953                                 qidx, (dev->data->nb_rx_queues - 1));
954                 return -ENOTSUP;
955         }
956
957         /* Socket id check */
958         if (socket_id != (unsigned int)SOCKET_ID_ANY &&
959                         socket_id != (unsigned int)nic->node)
960                 PMD_RX_LOG(INFO, "socket_id expected %d, configured %d",
961                                                 socket_id, nic->node);
962
963         /* Allocating rx queue data structure */
964         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct octeontx_rxq),
965                                  RTE_CACHE_LINE_SIZE, nic->node);
966         if (rxq == NULL) {
967                 octeontx_log_err("failed to allocate rxq=%d", qidx);
968                 return -ENOMEM;
969         }
970
971         if (!nic->pki.initialized) {
972                 pktbuf_conf.port_type = 0;
973                 pki_hash.port_type = 0;
974                 pki_qos.port_type = 0;
975
976                 pktbuf_conf.mmask.f_wqe_skip = 1;
977                 pktbuf_conf.mmask.f_first_skip = 1;
978                 pktbuf_conf.mmask.f_later_skip = 1;
979                 pktbuf_conf.mmask.f_mbuff_size = 1;
980                 pktbuf_conf.mmask.f_cache_mode = 1;
981
982                 pktbuf_conf.wqe_skip = OCTTX_PACKET_WQE_SKIP;
983                 pktbuf_conf.first_skip = OCTTX_PACKET_FIRST_SKIP(mb_pool);
984                 pktbuf_conf.later_skip = OCTTX_PACKET_LATER_SKIP;
985                 pktbuf_conf.mbuff_size = (mb_pool->elt_size -
986                                         RTE_PKTMBUF_HEADROOM -
987                                         rte_pktmbuf_priv_size(mb_pool) -
988                                         sizeof(struct rte_mbuf));
989
990                 pktbuf_conf.cache_mode = PKI_OPC_MODE_STF2_STT;
991
992                 ret = octeontx_pki_port_pktbuf_config(port, &pktbuf_conf);
993                 if (ret != 0) {
994                         octeontx_log_err("fail to configure pktbuf for port %d",
995                                         port);
996                         rte_free(rxq);
997                         return ret;
998                 }
999                 PMD_RX_LOG(DEBUG, "Port %d Rx pktbuf configured:\n"
1000                                 "\tmbuf_size:\t0x%0x\n"
1001                                 "\twqe_skip:\t0x%0x\n"
1002                                 "\tfirst_skip:\t0x%0x\n"
1003                                 "\tlater_skip:\t0x%0x\n"
1004                                 "\tcache_mode:\t%s\n",
1005                                 port,
1006                                 pktbuf_conf.mbuff_size,
1007                                 pktbuf_conf.wqe_skip,
1008                                 pktbuf_conf.first_skip,
1009                                 pktbuf_conf.later_skip,
1010                                 (pktbuf_conf.cache_mode ==
1011                                                 PKI_OPC_MODE_STT) ?
1012                                 "STT" :
1013                                 (pktbuf_conf.cache_mode ==
1014                                                 PKI_OPC_MODE_STF) ?
1015                                 "STF" :
1016                                 (pktbuf_conf.cache_mode ==
1017                                                 PKI_OPC_MODE_STF1_STT) ?
1018                                 "STF1_STT" : "STF2_STT");
1019
1020                 if (nic->pki.hash_enable) {
1021                         pki_hash.tag_dlc = 1;
1022                         pki_hash.tag_slc = 1;
1023                         pki_hash.tag_dlf = 1;
1024                         pki_hash.tag_slf = 1;
1025                         pki_hash.tag_prt = 1;
1026                         octeontx_pki_port_hash_config(port, &pki_hash);
1027                 }
1028
1029                 pool = (uintptr_t)mb_pool->pool_id;
1030
1031                 /* Get the gaura Id */
1032                 gaura = octeontx_fpa_bufpool_gaura(pool);
1033
1034                 pki_qos.qpg_qos = PKI_QPG_QOS_NONE;
1035                 pki_qos.num_entry = 1;
1036                 pki_qos.drop_policy = 0;
1037                 pki_qos.tag_type = 0L;
1038                 pki_qos.qos_entry[0].port_add = 0;
1039                 pki_qos.qos_entry[0].gaura = gaura;
1040                 pki_qos.qos_entry[0].ggrp_ok = ev_queues;
1041                 pki_qos.qos_entry[0].ggrp_bad = ev_queues;
1042                 pki_qos.qos_entry[0].grptag_bad = 0;
1043                 pki_qos.qos_entry[0].grptag_ok = 0;
1044
1045                 ret = octeontx_pki_port_create_qos(port, &pki_qos);
1046                 if (ret < 0) {
1047                         octeontx_log_err("failed to create QOS port=%d, q=%d",
1048                                         port, qidx);
1049                         rte_free(rxq);
1050                         return ret;
1051                 }
1052                 nic->pki.initialized = true;
1053         }
1054
1055         rxq->port_id = nic->port_id;
1056         rxq->eth_dev = dev;
1057         rxq->queue_id = qidx;
1058         rxq->evdev = nic->evdev;
1059         rxq->ev_queues = ev_queues;
1060         rxq->ev_ports = ev_ports;
1061         rxq->pool = mb_pool;
1062
1063         octeontx_recheck_rx_offloads(rxq);
1064         dev->data->rx_queues[qidx] = rxq;
1065         dev->data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
1066         return 0;
1067 }
1068
1069 static void
1070 octeontx_dev_rx_queue_release(void *rxq)
1071 {
1072         rte_free(rxq);
1073 }
1074
1075 static const uint32_t *
1076 octeontx_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1077 {
1078         static const uint32_t ptypes[] = {
1079                 RTE_PTYPE_L3_IPV4,
1080                 RTE_PTYPE_L3_IPV4_EXT,
1081                 RTE_PTYPE_L3_IPV6,
1082                 RTE_PTYPE_L3_IPV6_EXT,
1083                 RTE_PTYPE_L4_TCP,
1084                 RTE_PTYPE_L4_UDP,
1085                 RTE_PTYPE_L4_FRAG,
1086                 RTE_PTYPE_UNKNOWN
1087         };
1088
1089         if (dev->rx_pkt_burst == octeontx_recv_pkts)
1090                 return ptypes;
1091
1092         return NULL;
1093 }
1094
1095 static int
1096 octeontx_pool_ops(struct rte_eth_dev *dev, const char *pool)
1097 {
1098         RTE_SET_USED(dev);
1099
1100         if (!strcmp(pool, "octeontx_fpavf"))
1101                 return 0;
1102
1103         return -ENOTSUP;
1104 }
1105
1106 /* Initialize and register driver with DPDK Application */
1107 static const struct eth_dev_ops octeontx_dev_ops = {
1108         .dev_configure           = octeontx_dev_configure,
1109         .dev_infos_get           = octeontx_dev_info,
1110         .dev_close               = octeontx_dev_close,
1111         .dev_start               = octeontx_dev_start,
1112         .dev_stop                = octeontx_dev_stop,
1113         .promiscuous_enable      = octeontx_dev_promisc_enable,
1114         .promiscuous_disable     = octeontx_dev_promisc_disable,
1115         .link_update             = octeontx_dev_link_update,
1116         .stats_get               = octeontx_dev_stats_get,
1117         .stats_reset             = octeontx_dev_stats_reset,
1118         .mac_addr_remove         = octeontx_dev_mac_addr_del,
1119         .mac_addr_add            = octeontx_dev_mac_addr_add,
1120         .mac_addr_set            = octeontx_dev_default_mac_addr_set,
1121         .tx_queue_start          = octeontx_dev_tx_queue_start,
1122         .tx_queue_stop           = octeontx_dev_tx_queue_stop,
1123         .tx_queue_setup          = octeontx_dev_tx_queue_setup,
1124         .tx_queue_release        = octeontx_dev_tx_queue_release,
1125         .rx_queue_setup          = octeontx_dev_rx_queue_setup,
1126         .rx_queue_release        = octeontx_dev_rx_queue_release,
1127         .dev_supported_ptypes_get = octeontx_dev_supported_ptypes_get,
1128         .pool_ops_supported      = octeontx_pool_ops,
1129 };
1130
1131 /* Create Ethdev interface per BGX LMAC ports */
1132 static int
1133 octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
1134                         int socket_id)
1135 {
1136         int res;
1137         size_t pko_vfid;
1138         char octtx_name[OCTEONTX_MAX_NAME_LEN];
1139         struct octeontx_nic *nic = NULL;
1140         struct rte_eth_dev *eth_dev = NULL;
1141         struct rte_eth_dev_data *data;
1142         const char *name = rte_vdev_device_name(dev);
1143         int max_entries;
1144
1145         PMD_INIT_FUNC_TRACE();
1146
1147         sprintf(octtx_name, "%s_%d", name, port);
1148         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1149                 eth_dev = rte_eth_dev_attach_secondary(octtx_name);
1150                 if (eth_dev == NULL)
1151                         return -ENODEV;
1152
1153                 eth_dev->dev_ops = &octeontx_dev_ops;
1154                 eth_dev->device = &dev->device;
1155                 octeontx_set_tx_function(eth_dev);
1156                 eth_dev->rx_pkt_burst = octeontx_recv_pkts;
1157                 rte_eth_dev_probing_finish(eth_dev);
1158                 return 0;
1159         }
1160
1161         /* Reserve an ethdev entry */
1162         eth_dev = rte_eth_dev_allocate(octtx_name);
1163         if (eth_dev == NULL) {
1164                 octeontx_log_err("failed to allocate rte_eth_dev");
1165                 res = -ENOMEM;
1166                 goto err;
1167         }
1168         data = eth_dev->data;
1169
1170         nic = rte_zmalloc_socket(octtx_name, sizeof(*nic), 0, socket_id);
1171         if (nic == NULL) {
1172                 octeontx_log_err("failed to allocate nic structure");
1173                 res = -ENOMEM;
1174                 goto err;
1175         }
1176         data->dev_private = nic;
1177         pko_vfid = octeontx_pko_get_vfid();
1178
1179         if (pko_vfid == SIZE_MAX) {
1180                 octeontx_log_err("failed to get pko vfid");
1181                 res = -ENODEV;
1182                 goto err;
1183         }
1184
1185         nic->pko_vfid = pko_vfid;
1186         nic->port_id = port;
1187         nic->evdev = evdev;
1188
1189         res = octeontx_port_open(nic);
1190         if (res < 0)
1191                 goto err;
1192
1193         /* Rx side port configuration */
1194         res = octeontx_pki_port_open(port);
1195         if (res != 0) {
1196                 octeontx_log_err("failed to open PKI port %d", port);
1197                 res = -ENODEV;
1198                 goto err;
1199         }
1200
1201         eth_dev->device = &dev->device;
1202         eth_dev->intr_handle = NULL;
1203         eth_dev->data->kdrv = RTE_KDRV_NONE;
1204         eth_dev->data->numa_node = dev->device.numa_node;
1205
1206         data->port_id = eth_dev->data->port_id;
1207
1208         nic->ev_queues = 1;
1209         nic->ev_ports = 1;
1210
1211         data->dev_link.link_status = ETH_LINK_DOWN;
1212         data->dev_started = 0;
1213         data->promiscuous = 0;
1214         data->all_multicast = 0;
1215         data->scattered_rx = 0;
1216
1217         /* Get maximum number of supported MAC entries */
1218         max_entries = octeontx_bgx_port_mac_entries_get(nic->port_id);
1219         if (max_entries < 0) {
1220                 octeontx_log_err("Failed to get max entries for mac addr");
1221                 res = -ENOTSUP;
1222                 goto err;
1223         }
1224
1225         data->mac_addrs = rte_zmalloc_socket(octtx_name, max_entries *
1226                                              RTE_ETHER_ADDR_LEN, 0,
1227                                                         socket_id);
1228         if (data->mac_addrs == NULL) {
1229                 octeontx_log_err("failed to allocate memory for mac_addrs");
1230                 res = -ENOMEM;
1231                 goto err;
1232         }
1233
1234         eth_dev->dev_ops = &octeontx_dev_ops;
1235
1236         /* Finally save ethdev pointer to the NIC structure */
1237         nic->dev = eth_dev;
1238
1239         if (nic->port_id != data->port_id) {
1240                 octeontx_log_err("eth_dev->port_id (%d) is diff to orig (%d)",
1241                                 data->port_id, nic->port_id);
1242                 res = -EINVAL;
1243                 goto free_mac_addrs;
1244         }
1245
1246         /* Update port_id mac to eth_dev */
1247         memcpy(data->mac_addrs, nic->mac_addr, RTE_ETHER_ADDR_LEN);
1248
1249         /* Update same mac address to BGX CAM table at index 0 */
1250         octeontx_bgx_port_mac_add(nic->port_id, nic->mac_addr, 0);
1251
1252         PMD_INIT_LOG(DEBUG, "ethdev info: ");
1253         PMD_INIT_LOG(DEBUG, "port %d, port_ena %d ochan %d num_ochan %d tx_q %d",
1254                                 nic->port_id, nic->port_ena,
1255                                 nic->base_ochan, nic->num_ochans,
1256                                 nic->num_tx_queues);
1257         PMD_INIT_LOG(DEBUG, "speed %d mtu %d", nic->speed, nic->mtu);
1258
1259         rte_octeontx_pchan_map[(nic->base_ochan >> 8) & 0x7]
1260                 [(nic->base_ochan >> 4) & 0xF] = data->port_id;
1261
1262         rte_eth_dev_probing_finish(eth_dev);
1263         return data->port_id;
1264
1265 free_mac_addrs:
1266         rte_free(data->mac_addrs);
1267 err:
1268         if (nic)
1269                 octeontx_port_close(nic);
1270
1271         rte_eth_dev_release_port(eth_dev);
1272
1273         return res;
1274 }
1275
1276 /* Un initialize octeontx device */
1277 static int
1278 octeontx_remove(struct rte_vdev_device *dev)
1279 {
1280         char octtx_name[OCTEONTX_MAX_NAME_LEN];
1281         struct rte_eth_dev *eth_dev = NULL;
1282         struct octeontx_nic *nic = NULL;
1283         int i;
1284
1285         if (dev == NULL)
1286                 return -EINVAL;
1287
1288         for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
1289                 sprintf(octtx_name, "eth_octeontx_%d", i);
1290
1291                 /* reserve an ethdev entry */
1292                 eth_dev = rte_eth_dev_allocated(octtx_name);
1293                 if (eth_dev == NULL)
1294                         return -ENODEV;
1295
1296                 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1297                         rte_eth_dev_release_port(eth_dev);
1298                         continue;
1299                 }
1300
1301                 nic = octeontx_pmd_priv(eth_dev);
1302                 rte_event_dev_stop(nic->evdev);
1303                 PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
1304
1305                 rte_eth_dev_release_port(eth_dev);
1306                 rte_event_dev_close(nic->evdev);
1307         }
1308
1309         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1310                 return 0;
1311
1312         /* Free FC resource */
1313         octeontx_pko_fc_free();
1314
1315         return 0;
1316 }
1317
1318 /* Initialize octeontx device */
1319 static int
1320 octeontx_probe(struct rte_vdev_device *dev)
1321 {
1322         const char *dev_name;
1323         static int probe_once;
1324         uint8_t socket_id, qlist;
1325         int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
1326         struct rte_event_dev_config dev_conf;
1327         const char *eventdev_name = "event_octeontx";
1328         struct rte_event_dev_info info;
1329         struct rte_eth_dev *eth_dev;
1330
1331         struct octeontx_vdev_init_params init_params = {
1332                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
1333         };
1334
1335         dev_name = rte_vdev_device_name(dev);
1336
1337         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1338             strlen(rte_vdev_device_args(dev)) == 0) {
1339                 eth_dev = rte_eth_dev_attach_secondary(dev_name);
1340                 if (!eth_dev) {
1341                         PMD_INIT_LOG(ERR, "Failed to probe %s", dev_name);
1342                         return -1;
1343                 }
1344                 /* TODO: request info from primary to set up Rx and Tx */
1345                 eth_dev->dev_ops = &octeontx_dev_ops;
1346                 eth_dev->device = &dev->device;
1347                 rte_eth_dev_probing_finish(eth_dev);
1348                 return 0;
1349         }
1350
1351         res = octeontx_parse_vdev_init_params(&init_params, dev);
1352         if (res < 0)
1353                 return -EINVAL;
1354
1355         if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
1356                 octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
1357                                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
1358                 return -ENOTSUP;
1359         }
1360
1361         PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
1362
1363         socket_id = rte_socket_id();
1364
1365         tx_vfcnt = octeontx_pko_vf_count();
1366
1367         if (tx_vfcnt < init_params.nr_port) {
1368                 octeontx_log_err("not enough PKO (%d) for port number (%d)",
1369                                 tx_vfcnt, init_params.nr_port);
1370                 return -EINVAL;
1371         }
1372         evdev = rte_event_dev_get_dev_id(eventdev_name);
1373         if (evdev < 0) {
1374                 octeontx_log_err("eventdev %s not found", eventdev_name);
1375                 return -ENODEV;
1376         }
1377
1378         res = rte_event_dev_info_get(evdev, &info);
1379         if (res < 0) {
1380                 octeontx_log_err("failed to eventdev info %d", res);
1381                 return -EINVAL;
1382         }
1383
1384         PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
1385                         info.max_event_queues, info.max_event_ports);
1386
1387         if (octeontx_pko_init_fc(tx_vfcnt))
1388                 return -ENOMEM;
1389
1390         devconf_set_default_sane_values(&dev_conf, &info);
1391         res = rte_event_dev_configure(evdev, &dev_conf);
1392         if (res < 0)
1393                 goto parse_error;
1394
1395         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
1396                         (uint32_t *)&pnum);
1397         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
1398                         (uint32_t *)&qnum);
1399         if (pnum < qnum) {
1400                 octeontx_log_err("too few event ports (%d) for event_q(%d)",
1401                                 pnum, qnum);
1402                 res = -EINVAL;
1403                 goto parse_error;
1404         }
1405
1406         /* Enable all queues available */
1407         for (i = 0; i < qnum; i++) {
1408                 res = rte_event_queue_setup(evdev, i, NULL);
1409                 if (res < 0) {
1410                         octeontx_log_err("failed to setup event_q(%d): res %d",
1411                                         i, res);
1412                         goto parse_error;
1413                 }
1414         }
1415
1416         /* Enable all ports available */
1417         for (i = 0; i < pnum; i++) {
1418                 res = rte_event_port_setup(evdev, i, NULL);
1419                 if (res < 0) {
1420                         res = -ENODEV;
1421                         octeontx_log_err("failed to setup ev port(%d) res=%d",
1422                                                 i, res);
1423                         goto parse_error;
1424                 }
1425         }
1426
1427         /*
1428          * Do 1:1 links for ports & queues. All queues would be mapped to
1429          * one port. If there are more ports than queues, then some ports
1430          * won't be linked to any queue.
1431          */
1432         for (i = 0; i < qnum; i++) {
1433                 /* Link one queue to one event port */
1434                 qlist = i;
1435                 res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
1436                 if (res < 0) {
1437                         res = -ENODEV;
1438                         octeontx_log_err("failed to link port (%d): res=%d",
1439                                         i, res);
1440                         goto parse_error;
1441                 }
1442         }
1443
1444         /* Create ethdev interface */
1445         for (i = 0; i < init_params.nr_port; i++) {
1446                 port_id = octeontx_create(dev, i, evdev, socket_id);
1447                 if (port_id < 0) {
1448                         octeontx_log_err("failed to create device %s",
1449                                         dev_name);
1450                         res = -ENODEV;
1451                         goto parse_error;
1452                 }
1453
1454                 PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
1455                                         port_id);
1456         }
1457
1458         if (probe_once) {
1459                 octeontx_log_err("interface %s not supported", dev_name);
1460                 octeontx_remove(dev);
1461                 res = -ENOTSUP;
1462                 goto parse_error;
1463         }
1464         rte_mbuf_set_platform_mempool_ops("octeontx_fpavf");
1465         probe_once = 1;
1466
1467         return 0;
1468
1469 parse_error:
1470         octeontx_pko_fc_free();
1471         return res;
1472 }
1473
1474 static struct rte_vdev_driver octeontx_pmd_drv = {
1475         .probe = octeontx_probe,
1476         .remove = octeontx_remove,
1477 };
1478
1479 RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
1480 RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
1481 RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");