net/octeontx: add Rx queue setup and release ops
[dpdk.git] / drivers / net / octeontx / octeontx_ethdev.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium Inc. 2017. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium networks nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <rte_alarm.h>
40 #include <rte_branch_prediction.h>
41 #include <rte_debug.h>
42 #include <rte_devargs.h>
43 #include <rte_dev.h>
44 #include <rte_kvargs.h>
45 #include <rte_malloc.h>
46 #include <rte_prefetch.h>
47 #include <rte_vdev.h>
48
49 #include "octeontx_ethdev.h"
50 #include "octeontx_logs.h"
51
52 struct octeontx_vdev_init_params {
53         uint8_t nr_port;
54 };
55
56 enum octeontx_link_speed {
57         OCTEONTX_LINK_SPEED_SGMII,
58         OCTEONTX_LINK_SPEED_XAUI,
59         OCTEONTX_LINK_SPEED_RXAUI,
60         OCTEONTX_LINK_SPEED_10G_R,
61         OCTEONTX_LINK_SPEED_40G_R,
62         OCTEONTX_LINK_SPEED_RESERVE1,
63         OCTEONTX_LINK_SPEED_QSGMII,
64         OCTEONTX_LINK_SPEED_RESERVE2
65 };
66
67 /* Parse integer from integer argument */
68 static int
69 parse_integer_arg(const char *key __rte_unused,
70                 const char *value, void *extra_args)
71 {
72         int *i = (int *)extra_args;
73
74         *i = atoi(value);
75         if (*i < 0) {
76                 octeontx_log_err("argument has to be positive.");
77                 return -1;
78         }
79
80         return 0;
81 }
82
83 static int
84 octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
85                                 struct rte_vdev_device *dev)
86 {
87         struct rte_kvargs *kvlist = NULL;
88         int ret = 0;
89
90         static const char * const octeontx_vdev_valid_params[] = {
91                 OCTEONTX_VDEV_NR_PORT_ARG,
92                 NULL
93         };
94
95         const char *input_args = rte_vdev_device_args(dev);
96         if (params == NULL)
97                 return -EINVAL;
98
99
100         if (input_args) {
101                 kvlist = rte_kvargs_parse(input_args,
102                                 octeontx_vdev_valid_params);
103                 if (kvlist == NULL)
104                         return -1;
105
106                 ret = rte_kvargs_process(kvlist,
107                                         OCTEONTX_VDEV_NR_PORT_ARG,
108                                         &parse_integer_arg,
109                                         &params->nr_port);
110                 if (ret < 0)
111                         goto free_kvlist;
112         }
113
114 free_kvlist:
115         rte_kvargs_free(kvlist);
116         return ret;
117 }
118
119 static int
120 octeontx_port_open(struct octeontx_nic *nic)
121 {
122         octeontx_mbox_bgx_port_conf_t bgx_port_conf;
123         int res;
124
125         res = 0;
126
127         PMD_INIT_FUNC_TRACE();
128
129         res = octeontx_bgx_port_open(nic->port_id, &bgx_port_conf);
130         if (res < 0) {
131                 octeontx_log_err("failed to open port %d", res);
132                 return res;
133         }
134
135         nic->node = bgx_port_conf.node;
136         nic->port_ena = bgx_port_conf.enable;
137         nic->base_ichan = bgx_port_conf.base_chan;
138         nic->base_ochan = bgx_port_conf.base_chan;
139         nic->num_ichans = bgx_port_conf.num_chans;
140         nic->num_ochans = bgx_port_conf.num_chans;
141         nic->mtu = bgx_port_conf.mtu;
142         nic->bpen = bgx_port_conf.bpen;
143         nic->fcs_strip = bgx_port_conf.fcs_strip;
144         nic->bcast_mode = bgx_port_conf.bcast_mode;
145         nic->mcast_mode = bgx_port_conf.mcast_mode;
146         nic->speed      = bgx_port_conf.mode;
147
148         memcpy(&nic->mac_addr[0], &bgx_port_conf.macaddr[0], ETHER_ADDR_LEN);
149
150         octeontx_log_dbg("port opened %d", nic->port_id);
151         return res;
152 }
153
154 static void
155 octeontx_port_close(struct octeontx_nic *nic)
156 {
157         PMD_INIT_FUNC_TRACE();
158
159         octeontx_bgx_port_close(nic->port_id);
160         octeontx_log_dbg("port closed %d", nic->port_id);
161 }
162
163 static void
164 octeontx_port_promisc_set(struct octeontx_nic *nic, int en)
165 {
166         struct rte_eth_dev *dev;
167         int res;
168
169         res = 0;
170         PMD_INIT_FUNC_TRACE();
171         dev = nic->dev;
172
173         res = octeontx_bgx_port_promisc_set(nic->port_id, en);
174         if (res < 0)
175                 octeontx_log_err("failed to set promiscuous mode %d",
176                                 nic->port_id);
177
178         /* Set proper flag for the mode */
179         dev->data->promiscuous = (en != 0) ? 1 : 0;
180
181         octeontx_log_dbg("port %d : promiscuous mode %s",
182                         nic->port_id, en ? "set" : "unset");
183 }
184
185 static void
186 octeontx_port_stats(struct octeontx_nic *nic, struct rte_eth_stats *stats)
187 {
188         octeontx_mbox_bgx_port_stats_t bgx_stats;
189         int res;
190
191         PMD_INIT_FUNC_TRACE();
192
193         res = octeontx_bgx_port_stats(nic->port_id, &bgx_stats);
194         if (res < 0)
195                 octeontx_log_err("failed to get port stats %d", nic->port_id);
196
197         stats->ipackets = bgx_stats.rx_packets;
198         stats->ibytes = bgx_stats.rx_bytes;
199         stats->imissed = bgx_stats.rx_dropped;
200         stats->ierrors = bgx_stats.rx_errors;
201         stats->opackets = bgx_stats.tx_packets;
202         stats->obytes = bgx_stats.tx_bytes;
203         stats->oerrors = bgx_stats.tx_errors;
204
205         octeontx_log_dbg("port%d stats inpkts=%" PRIx64 " outpkts=%" PRIx64 "",
206                         nic->port_id, stats->ipackets, stats->opackets);
207 }
208
209 static void
210 octeontx_port_stats_clr(struct octeontx_nic *nic)
211 {
212         PMD_INIT_FUNC_TRACE();
213
214         octeontx_bgx_port_stats_clr(nic->port_id);
215 }
216
217 static inline void
218 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
219                                 struct rte_event_dev_info *info)
220 {
221         memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
222         dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
223
224         dev_conf->nb_event_ports = info->max_event_ports;
225         dev_conf->nb_event_queues = info->max_event_queues;
226
227         dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
228         dev_conf->nb_event_port_dequeue_depth =
229                         info->max_event_port_dequeue_depth;
230         dev_conf->nb_event_port_enqueue_depth =
231                         info->max_event_port_enqueue_depth;
232         dev_conf->nb_event_port_enqueue_depth =
233                         info->max_event_port_enqueue_depth;
234         dev_conf->nb_events_limit =
235                         info->max_num_events;
236 }
237
238 static int
239 octeontx_dev_configure(struct rte_eth_dev *dev)
240 {
241         struct rte_eth_dev_data *data = dev->data;
242         struct rte_eth_conf *conf = &data->dev_conf;
243         struct rte_eth_rxmode *rxmode = &conf->rxmode;
244         struct rte_eth_txmode *txmode = &conf->txmode;
245         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
246         int ret;
247
248         PMD_INIT_FUNC_TRACE();
249         RTE_SET_USED(conf);
250
251         if (!rte_eal_has_hugepages()) {
252                 octeontx_log_err("huge page is not configured");
253                 return -EINVAL;
254         }
255
256         if (txmode->mq_mode) {
257                 octeontx_log_err("tx mq_mode DCB or VMDq not supported");
258                 return -EINVAL;
259         }
260
261         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
262                 rxmode->mq_mode != ETH_MQ_RX_RSS) {
263                 octeontx_log_err("unsupported rx qmode %d", rxmode->mq_mode);
264                 return -EINVAL;
265         }
266
267         if (!rxmode->hw_strip_crc) {
268                 PMD_INIT_LOG(NOTICE, "can't disable hw crc strip");
269                 rxmode->hw_strip_crc = 1;
270         }
271
272         if (rxmode->hw_ip_checksum) {
273                 PMD_INIT_LOG(NOTICE, "rxcksum not supported");
274                 rxmode->hw_ip_checksum = 0;
275         }
276
277         if (rxmode->split_hdr_size) {
278                 octeontx_log_err("rxmode does not support split header");
279                 return -EINVAL;
280         }
281
282         if (rxmode->hw_vlan_filter) {
283                 octeontx_log_err("VLAN filter not supported");
284                 return -EINVAL;
285         }
286
287         if (rxmode->hw_vlan_extend) {
288                 octeontx_log_err("VLAN extended not supported");
289                 return -EINVAL;
290         }
291
292         if (rxmode->enable_lro) {
293                 octeontx_log_err("LRO not supported");
294                 return -EINVAL;
295         }
296
297         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
298                 octeontx_log_err("setting link speed/duplex not supported");
299                 return -EINVAL;
300         }
301
302         if (conf->dcb_capability_en) {
303                 octeontx_log_err("DCB enable not supported");
304                 return -EINVAL;
305         }
306
307         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
308                 octeontx_log_err("flow director not supported");
309                 return -EINVAL;
310         }
311
312         nic->num_tx_queues = dev->data->nb_tx_queues;
313
314         ret = octeontx_pko_channel_open(nic->port_id * PKO_VF_NUM_DQ,
315                                         nic->num_tx_queues,
316                                         nic->base_ochan);
317         if (ret) {
318                 octeontx_log_err("failed to open channel %d no-of-txq %d",
319                            nic->base_ochan, nic->num_tx_queues);
320                 return -EFAULT;
321         }
322
323         nic->pki.classifier_enable = false;
324         nic->pki.hash_enable = true;
325         nic->pki.initialized = false;
326
327         return 0;
328 }
329
330 static void
331 octeontx_dev_promisc_enable(struct rte_eth_dev *dev)
332 {
333         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
334
335         PMD_INIT_FUNC_TRACE();
336         octeontx_port_promisc_set(nic, 1);
337 }
338
339 static void
340 octeontx_dev_promisc_disable(struct rte_eth_dev *dev)
341 {
342         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
343
344         PMD_INIT_FUNC_TRACE();
345         octeontx_port_promisc_set(nic, 0);
346 }
347
348 static inline int
349 octeontx_atomic_write_link_status(struct rte_eth_dev *dev,
350                                   struct rte_eth_link *link)
351 {
352         struct rte_eth_link *dst = &dev->data->dev_link;
353         struct rte_eth_link *src = link;
354
355         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
356                 *(uint64_t *)src) == 0)
357                 return -1;
358
359         return 0;
360 }
361
362 static int
363 octeontx_port_link_status(struct octeontx_nic *nic)
364 {
365         int res;
366
367         PMD_INIT_FUNC_TRACE();
368         res = octeontx_bgx_port_link_status(nic->port_id);
369         if (res < 0) {
370                 octeontx_log_err("failed to get port %d link status",
371                                 nic->port_id);
372                 return res;
373         }
374
375         nic->link_up = (uint8_t)res;
376         octeontx_log_dbg("port %d link status %d", nic->port_id, nic->link_up);
377
378         return res;
379 }
380
381 /*
382  * Return 0 means link status changed, -1 means not changed
383  */
384 static int
385 octeontx_dev_link_update(struct rte_eth_dev *dev,
386                          int wait_to_complete __rte_unused)
387 {
388         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
389         struct rte_eth_link link;
390         int res;
391
392         res = 0;
393         PMD_INIT_FUNC_TRACE();
394
395         res = octeontx_port_link_status(nic);
396         if (res < 0) {
397                 octeontx_log_err("failed to request link status %d", res);
398                 return res;
399         }
400
401         link.link_status = nic->link_up;
402
403         switch (nic->speed) {
404         case OCTEONTX_LINK_SPEED_SGMII:
405                 link.link_speed = ETH_SPEED_NUM_1G;
406                 break;
407
408         case OCTEONTX_LINK_SPEED_XAUI:
409                 link.link_speed = ETH_SPEED_NUM_10G;
410                 break;
411
412         case OCTEONTX_LINK_SPEED_RXAUI:
413         case OCTEONTX_LINK_SPEED_10G_R:
414                 link.link_speed = ETH_SPEED_NUM_10G;
415                 break;
416         case OCTEONTX_LINK_SPEED_QSGMII:
417                 link.link_speed = ETH_SPEED_NUM_5G;
418                 break;
419         case OCTEONTX_LINK_SPEED_40G_R:
420                 link.link_speed = ETH_SPEED_NUM_40G;
421                 break;
422
423         case OCTEONTX_LINK_SPEED_RESERVE1:
424         case OCTEONTX_LINK_SPEED_RESERVE2:
425         default:
426                 octeontx_log_err("incorrect link speed %d", nic->speed);
427                 break;
428         }
429
430         link.link_duplex = ETH_LINK_AUTONEG;
431         link.link_autoneg = ETH_LINK_SPEED_AUTONEG;
432
433         return octeontx_atomic_write_link_status(dev, &link);
434 }
435
436 static void
437 octeontx_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
438 {
439         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
440
441         PMD_INIT_FUNC_TRACE();
442         octeontx_port_stats(nic, stats);
443 }
444
445 static void
446 octeontx_dev_stats_reset(struct rte_eth_dev *dev)
447 {
448         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
449
450         PMD_INIT_FUNC_TRACE();
451         octeontx_port_stats_clr(nic);
452 }
453
454 static void
455 octeontx_dev_default_mac_addr_set(struct rte_eth_dev *dev,
456                                         struct ether_addr *addr)
457 {
458         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
459         int ret;
460
461         ret = octeontx_bgx_port_mac_set(nic->port_id, addr->addr_bytes);
462         if (ret != 0)
463                 octeontx_log_err("failed to set MAC address on port %d",
464                                 nic->port_id);
465 }
466
467 static void
468 octeontx_dev_info(struct rte_eth_dev *dev,
469                 struct rte_eth_dev_info *dev_info)
470 {
471         RTE_SET_USED(dev);
472
473         /* Autonegotiation may be disabled */
474         dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
475         dev_info->speed_capa |= ETH_LINK_SPEED_10M | ETH_LINK_SPEED_100M |
476                         ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
477                         ETH_LINK_SPEED_40G;
478
479         dev_info->driver_name = RTE_STR(rte_octeontx_pmd);
480         dev_info->max_mac_addrs = 1;
481         dev_info->max_rx_pktlen = PKI_MAX_PKTLEN;
482         dev_info->max_rx_queues = 1;
483         dev_info->max_tx_queues = PKO_MAX_NUM_DQ;
484         dev_info->min_rx_bufsize = 0;
485         dev_info->pci_dev = NULL;
486
487         dev_info->default_rxconf = (struct rte_eth_rxconf) {
488                 .rx_free_thresh = 0,
489                 .rx_drop_en = 0,
490         };
491
492         dev_info->default_txconf = (struct rte_eth_txconf) {
493                 .tx_free_thresh = 0,
494                 .txq_flags =
495                         ETH_TXQ_FLAGS_NOMULTSEGS |
496                         ETH_TXQ_FLAGS_NOOFFLOADS |
497                         ETH_TXQ_FLAGS_NOXSUMS,
498         };
499
500         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MT_LOCKFREE;
501 }
502
503 static int
504 octeontx_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
505                                 uint16_t nb_desc, unsigned int socket_id,
506                                 const struct rte_eth_rxconf *rx_conf,
507                                 struct rte_mempool *mb_pool)
508 {
509         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
510         struct rte_mempool_ops *mp_ops = NULL;
511         struct octeontx_rxq *rxq = NULL;
512         pki_pktbuf_cfg_t pktbuf_conf;
513         pki_hash_cfg_t pki_hash;
514         pki_qos_cfg_t pki_qos;
515         uintptr_t pool;
516         int ret, port;
517         uint8_t gaura;
518         unsigned int ev_queues = (nic->ev_queues * nic->port_id) + qidx;
519         unsigned int ev_ports = (nic->ev_ports * nic->port_id) + qidx;
520
521         RTE_SET_USED(nb_desc);
522
523         memset(&pktbuf_conf, 0, sizeof(pktbuf_conf));
524         memset(&pki_hash, 0, sizeof(pki_hash));
525         memset(&pki_qos, 0, sizeof(pki_qos));
526
527         mp_ops = rte_mempool_get_ops(mb_pool->ops_index);
528         if (strcmp(mp_ops->name, "octeontx_fpavf")) {
529                 octeontx_log_err("failed to find octeontx_fpavf mempool");
530                 return -ENOTSUP;
531         }
532
533         /* Handle forbidden configurations */
534         if (nic->pki.classifier_enable) {
535                 octeontx_log_err("cannot setup queue %d. "
536                                         "Classifier option unsupported", qidx);
537                 return -EINVAL;
538         }
539
540         port = nic->port_id;
541
542         /* Rx deferred start is not supported */
543         if (rx_conf->rx_deferred_start) {
544                 octeontx_log_err("rx deferred start not supported");
545                 return -EINVAL;
546         }
547
548         /* Verify queue index */
549         if (qidx >= dev->data->nb_rx_queues) {
550                 octeontx_log_err("QID %d not supporteded (0 - %d available)\n",
551                                 qidx, (dev->data->nb_rx_queues - 1));
552                 return -ENOTSUP;
553         }
554
555         /* Socket id check */
556         if (socket_id != (unsigned int)SOCKET_ID_ANY &&
557                         socket_id != (unsigned int)nic->node)
558                 PMD_RX_LOG(INFO, "socket_id expected %d, configured %d",
559                                                 socket_id, nic->node);
560
561         /* Allocating rx queue data structure */
562         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct octeontx_rxq),
563                                  RTE_CACHE_LINE_SIZE, nic->node);
564         if (rxq == NULL) {
565                 octeontx_log_err("failed to allocate rxq=%d", qidx);
566                 return -ENOMEM;
567         }
568
569         if (!nic->pki.initialized) {
570                 pktbuf_conf.port_type = 0;
571                 pki_hash.port_type = 0;
572                 pki_qos.port_type = 0;
573
574                 pktbuf_conf.mmask.f_wqe_skip = 1;
575                 pktbuf_conf.mmask.f_first_skip = 1;
576                 pktbuf_conf.mmask.f_later_skip = 1;
577                 pktbuf_conf.mmask.f_mbuff_size = 1;
578                 pktbuf_conf.mmask.f_cache_mode = 1;
579
580                 pktbuf_conf.wqe_skip = OCTTX_PACKET_WQE_SKIP;
581                 pktbuf_conf.first_skip = OCTTX_PACKET_FIRST_SKIP;
582                 pktbuf_conf.later_skip = OCTTX_PACKET_LATER_SKIP;
583                 pktbuf_conf.mbuff_size = (mb_pool->elt_size -
584                                         RTE_PKTMBUF_HEADROOM -
585                                         sizeof(struct rte_mbuf));
586
587                 pktbuf_conf.cache_mode = PKI_OPC_MODE_STF2_STT;
588
589                 ret = octeontx_pki_port_pktbuf_config(port, &pktbuf_conf);
590                 if (ret != 0) {
591                         octeontx_log_err("fail to configure pktbuf for port %d",
592                                         port);
593                         rte_free(rxq);
594                         return ret;
595                 }
596                 PMD_RX_LOG(DEBUG, "Port %d Rx pktbuf configured:\n"
597                                 "\tmbuf_size:\t0x%0x\n"
598                                 "\twqe_skip:\t0x%0x\n"
599                                 "\tfirst_skip:\t0x%0x\n"
600                                 "\tlater_skip:\t0x%0x\n"
601                                 "\tcache_mode:\t%s\n",
602                                 port,
603                                 pktbuf_conf.mbuff_size,
604                                 pktbuf_conf.wqe_skip,
605                                 pktbuf_conf.first_skip,
606                                 pktbuf_conf.later_skip,
607                                 (pktbuf_conf.cache_mode ==
608                                                 PKI_OPC_MODE_STT) ?
609                                 "STT" :
610                                 (pktbuf_conf.cache_mode ==
611                                                 PKI_OPC_MODE_STF) ?
612                                 "STF" :
613                                 (pktbuf_conf.cache_mode ==
614                                                 PKI_OPC_MODE_STF1_STT) ?
615                                 "STF1_STT" : "STF2_STT");
616
617                 if (nic->pki.hash_enable) {
618                         pki_hash.tag_dlc = 1;
619                         pki_hash.tag_slc = 1;
620                         pki_hash.tag_dlf = 1;
621                         pki_hash.tag_slf = 1;
622                         octeontx_pki_port_hash_config(port, &pki_hash);
623                 }
624
625                 pool = (uintptr_t)mb_pool->pool_id;
626
627                 /* Get the gpool Id */
628                 gaura = octeontx_fpa_bufpool_gpool(pool);
629
630                 pki_qos.qpg_qos = PKI_QPG_QOS_NONE;
631                 pki_qos.num_entry = 1;
632                 pki_qos.drop_policy = 0;
633                 pki_qos.tag_type = 2L;
634                 pki_qos.qos_entry[0].port_add = 0;
635                 pki_qos.qos_entry[0].gaura = gaura;
636                 pki_qos.qos_entry[0].ggrp_ok = ev_queues;
637                 pki_qos.qos_entry[0].ggrp_bad = ev_queues;
638                 pki_qos.qos_entry[0].grptag_bad = 0;
639                 pki_qos.qos_entry[0].grptag_ok = 0;
640
641                 ret = octeontx_pki_port_create_qos(port, &pki_qos);
642                 if (ret < 0) {
643                         octeontx_log_err("failed to create QOS port=%d, q=%d",
644                                         port, qidx);
645                         rte_free(rxq);
646                         return ret;
647                 }
648                 nic->pki.initialized = true;
649         }
650
651         rxq->port_id = nic->port_id;
652         rxq->eth_dev = dev;
653         rxq->queue_id = qidx;
654         rxq->evdev = nic->evdev;
655         rxq->ev_queues = ev_queues;
656         rxq->ev_ports = ev_ports;
657
658         dev->data->rx_queues[qidx] = rxq;
659         dev->data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
660         return 0;
661 }
662
663 static void
664 octeontx_dev_rx_queue_release(void *rxq)
665 {
666         rte_free(rxq);
667 }
668
669 /* Initialize and register driver with DPDK Application */
670 static const struct eth_dev_ops octeontx_dev_ops = {
671         .dev_configure           = octeontx_dev_configure,
672         .dev_infos_get           = octeontx_dev_info,
673         .promiscuous_enable      = octeontx_dev_promisc_enable,
674         .promiscuous_disable     = octeontx_dev_promisc_disable,
675         .link_update             = octeontx_dev_link_update,
676         .stats_get               = octeontx_dev_stats_get,
677         .stats_reset             = octeontx_dev_stats_reset,
678         .mac_addr_set            = octeontx_dev_default_mac_addr_set,
679         .rx_queue_setup          = octeontx_dev_rx_queue_setup,
680         .rx_queue_release        = octeontx_dev_rx_queue_release,
681 };
682
683 /* Create Ethdev interface per BGX LMAC ports */
684 static int
685 octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
686                         int socket_id)
687 {
688         int res;
689         char octtx_name[OCTEONTX_MAX_NAME_LEN];
690         struct octeontx_nic *nic = NULL;
691         struct rte_eth_dev *eth_dev = NULL;
692         struct rte_eth_dev_data *data = NULL;
693         const char *name = rte_vdev_device_name(dev);
694
695         PMD_INIT_FUNC_TRACE();
696
697         sprintf(octtx_name, "%s_%d", name, port);
698         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
699                 eth_dev = rte_eth_dev_attach_secondary(octtx_name);
700                 if (eth_dev == NULL)
701                         return -ENODEV;
702
703                 return 0;
704         }
705
706         data = rte_zmalloc_socket(octtx_name, sizeof(*data), 0, socket_id);
707         if (data == NULL) {
708                 octeontx_log_err("failed to allocate devdata");
709                 res = -ENOMEM;
710                 goto err;
711         }
712
713         nic = rte_zmalloc_socket(octtx_name, sizeof(*nic), 0, socket_id);
714         if (nic == NULL) {
715                 octeontx_log_err("failed to allocate nic structure");
716                 res = -ENOMEM;
717                 goto err;
718         }
719
720         nic->port_id = port;
721         nic->evdev = evdev;
722
723         res = octeontx_port_open(nic);
724         if (res < 0)
725                 goto err;
726
727         /* Rx side port configuration */
728         res = octeontx_pki_port_open(port);
729         if (res != 0) {
730                 octeontx_log_err("failed to open PKI port %d", port);
731                 res = -ENODEV;
732                 goto err;
733         }
734
735         /* Reserve an ethdev entry */
736         eth_dev = rte_eth_dev_allocate(octtx_name);
737         if (eth_dev == NULL) {
738                 octeontx_log_err("failed to allocate rte_eth_dev");
739                 res = -ENOMEM;
740                 goto err;
741         }
742
743         eth_dev->device = &dev->device;
744         eth_dev->intr_handle = NULL;
745         eth_dev->data->kdrv = RTE_KDRV_NONE;
746         eth_dev->data->numa_node = dev->device.numa_node;
747
748         rte_memcpy(data, (eth_dev)->data, sizeof(*data));
749         data->dev_private = nic;
750
751         data->port_id = eth_dev->data->port_id;
752         snprintf(data->name, sizeof(data->name), "%s", eth_dev->data->name);
753
754         nic->ev_queues = 1;
755         nic->ev_ports = 1;
756
757         data->dev_link.link_status = ETH_LINK_DOWN;
758         data->dev_started = 0;
759         data->promiscuous = 0;
760         data->all_multicast = 0;
761         data->scattered_rx = 0;
762
763         data->mac_addrs = rte_zmalloc_socket(octtx_name, ETHER_ADDR_LEN, 0,
764                                                         socket_id);
765         if (data->mac_addrs == NULL) {
766                 octeontx_log_err("failed to allocate memory for mac_addrs");
767                 res = -ENOMEM;
768                 goto err;
769         }
770
771         eth_dev->data = data;
772         eth_dev->dev_ops = &octeontx_dev_ops;
773
774         /* Finally save ethdev pointer to the NIC structure */
775         nic->dev = eth_dev;
776
777         if (nic->port_id != data->port_id) {
778                 octeontx_log_err("eth_dev->port_id (%d) is diff to orig (%d)",
779                                 data->port_id, nic->port_id);
780                 res = -EINVAL;
781                 goto err;
782         }
783
784         /* Update port_id mac to eth_dev */
785         memcpy(data->mac_addrs, nic->mac_addr, ETHER_ADDR_LEN);
786
787         PMD_INIT_LOG(DEBUG, "ethdev info: ");
788         PMD_INIT_LOG(DEBUG, "port %d, port_ena %d ochan %d num_ochan %d tx_q %d",
789                                 nic->port_id, nic->port_ena,
790                                 nic->base_ochan, nic->num_ochans,
791                                 nic->num_tx_queues);
792         PMD_INIT_LOG(DEBUG, "speed %d mtu %d", nic->speed, nic->mtu);
793
794         return data->port_id;
795
796 err:
797         if (port)
798                 octeontx_port_close(nic);
799
800         if (eth_dev != NULL) {
801                 rte_free(eth_dev->data->mac_addrs);
802                 rte_free(data);
803                 rte_free(nic);
804                 rte_eth_dev_release_port(eth_dev);
805         }
806
807         return res;
808 }
809
810 /* Un initialize octeontx device */
811 static int
812 octeontx_remove(struct rte_vdev_device *dev)
813 {
814         char octtx_name[OCTEONTX_MAX_NAME_LEN];
815         struct rte_eth_dev *eth_dev = NULL;
816         struct octeontx_nic *nic = NULL;
817         int i;
818
819         if (dev == NULL)
820                 return -EINVAL;
821
822         for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
823                 sprintf(octtx_name, "eth_octeontx_%d", i);
824
825                 /* reserve an ethdev entry */
826                 eth_dev = rte_eth_dev_allocated(octtx_name);
827                 if (eth_dev == NULL)
828                         return -ENODEV;
829
830                 nic = octeontx_pmd_priv(eth_dev);
831                 rte_event_dev_stop(nic->evdev);
832                 PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
833
834                 rte_free(eth_dev->data->mac_addrs);
835                 rte_free(eth_dev->data->dev_private);
836                 rte_free(eth_dev->data);
837                 rte_eth_dev_release_port(eth_dev);
838                 rte_event_dev_close(nic->evdev);
839         }
840
841         /* Free FC resource */
842         octeontx_pko_fc_free();
843
844         return 0;
845 }
846
847 /* Initialize octeontx device */
848 static int
849 octeontx_probe(struct rte_vdev_device *dev)
850 {
851         const char *dev_name;
852         static int probe_once;
853         uint8_t socket_id, qlist;
854         int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
855         struct rte_event_dev_config dev_conf;
856         const char *eventdev_name = "event_octeontx";
857         struct rte_event_dev_info info;
858
859         struct octeontx_vdev_init_params init_params = {
860                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
861         };
862
863         dev_name = rte_vdev_device_name(dev);
864         res = octeontx_parse_vdev_init_params(&init_params, dev);
865         if (res < 0)
866                 return -EINVAL;
867
868         if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
869                 octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
870                                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
871                 return -ENOTSUP;
872         }
873
874         PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
875
876         socket_id = rte_socket_id();
877
878         tx_vfcnt = octeontx_pko_vf_count();
879
880         if (tx_vfcnt < init_params.nr_port) {
881                 octeontx_log_err("not enough PKO (%d) for port number (%d)",
882                                 tx_vfcnt, init_params.nr_port);
883                 return -EINVAL;
884         }
885         evdev = rte_event_dev_get_dev_id(eventdev_name);
886         if (evdev < 0) {
887                 octeontx_log_err("eventdev %s not found", eventdev_name);
888                 return -ENODEV;
889         }
890
891         res = rte_event_dev_info_get(evdev, &info);
892         if (res < 0) {
893                 octeontx_log_err("failed to eventdev info %d", res);
894                 return -EINVAL;
895         }
896
897         PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
898                         info.max_event_queues, info.max_event_ports);
899
900         if (octeontx_pko_init_fc(tx_vfcnt))
901                 return -ENOMEM;
902
903         devconf_set_default_sane_values(&dev_conf, &info);
904         res = rte_event_dev_configure(evdev, &dev_conf);
905         if (res < 0)
906                 goto parse_error;
907
908         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
909                         (uint32_t *)&pnum);
910         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
911                         (uint32_t *)&qnum);
912         if (pnum < qnum) {
913                 octeontx_log_err("too few event ports (%d) for event_q(%d)",
914                                 pnum, qnum);
915                 res = -EINVAL;
916                 goto parse_error;
917         }
918         if (pnum > qnum) {
919                 /*
920                  * We don't poll on event ports
921                  * that do not have any queues assigned.
922                  */
923                 pnum = qnum;
924                 PMD_INIT_LOG(INFO,
925                         "reducing number of active event ports to %d", pnum);
926         }
927         for (i = 0; i < qnum; i++) {
928                 res = rte_event_queue_setup(evdev, i, NULL);
929                 if (res < 0) {
930                         octeontx_log_err("failed to setup event_q(%d): res %d",
931                                         i, res);
932                         goto parse_error;
933                 }
934         }
935
936         for (i = 0; i < pnum; i++) {
937                 res = rte_event_port_setup(evdev, i, NULL);
938                 if (res < 0) {
939                         res = -ENODEV;
940                         octeontx_log_err("failed to setup ev port(%d) res=%d",
941                                                 i, res);
942                         goto parse_error;
943                 }
944                 /* Link one queue to one event port */
945                 qlist = i;
946                 res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
947                 if (res < 0) {
948                         res = -ENODEV;
949                         octeontx_log_err("failed to link port (%d): res=%d",
950                                         i, res);
951                         goto parse_error;
952                 }
953         }
954
955         /* Create ethdev interface */
956         for (i = 0; i < init_params.nr_port; i++) {
957                 port_id = octeontx_create(dev, i, evdev, socket_id);
958                 if (port_id < 0) {
959                         octeontx_log_err("failed to create device %s",
960                                         dev_name);
961                         res = -ENODEV;
962                         goto parse_error;
963                 }
964
965                 PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
966                                         port_id);
967         }
968
969         if (probe_once) {
970                 octeontx_log_err("interface %s not supported", dev_name);
971                 octeontx_remove(dev);
972                 res = -ENOTSUP;
973                 goto parse_error;
974         }
975         probe_once = 1;
976
977         return 0;
978
979 parse_error:
980         octeontx_pko_fc_free();
981         return res;
982 }
983
984 static struct rte_vdev_driver octeontx_pmd_drv = {
985         .probe = octeontx_probe,
986         .remove = octeontx_remove,
987 };
988
989 RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
990 RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
991 RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");