706cf88bb4246f3c97f5420c85196e2ab7b48dc7
[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_rxtx.h"
51 #include "octeontx_logs.h"
52
53 struct octeontx_vdev_init_params {
54         uint8_t nr_port;
55 };
56
57 enum octeontx_link_speed {
58         OCTEONTX_LINK_SPEED_SGMII,
59         OCTEONTX_LINK_SPEED_XAUI,
60         OCTEONTX_LINK_SPEED_RXAUI,
61         OCTEONTX_LINK_SPEED_10G_R,
62         OCTEONTX_LINK_SPEED_40G_R,
63         OCTEONTX_LINK_SPEED_RESERVE1,
64         OCTEONTX_LINK_SPEED_QSGMII,
65         OCTEONTX_LINK_SPEED_RESERVE2
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
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], ETHER_ADDR_LEN);
150
151         octeontx_log_dbg("port opened %d", nic->port_id);
152         return res;
153 }
154
155 static void
156 octeontx_port_close(struct octeontx_nic *nic)
157 {
158         PMD_INIT_FUNC_TRACE();
159
160         octeontx_bgx_port_close(nic->port_id);
161         octeontx_log_dbg("port closed %d", nic->port_id);
162 }
163
164 static int
165 octeontx_port_stop(struct octeontx_nic *nic)
166 {
167         PMD_INIT_FUNC_TRACE();
168
169         return octeontx_bgx_port_stop(nic->port_id);
170 }
171
172 static void
173 octeontx_port_promisc_set(struct octeontx_nic *nic, int en)
174 {
175         struct rte_eth_dev *dev;
176         int res;
177
178         res = 0;
179         PMD_INIT_FUNC_TRACE();
180         dev = nic->dev;
181
182         res = octeontx_bgx_port_promisc_set(nic->port_id, en);
183         if (res < 0)
184                 octeontx_log_err("failed to set promiscuous mode %d",
185                                 nic->port_id);
186
187         /* Set proper flag for the mode */
188         dev->data->promiscuous = (en != 0) ? 1 : 0;
189
190         octeontx_log_dbg("port %d : promiscuous mode %s",
191                         nic->port_id, en ? "set" : "unset");
192 }
193
194 static void
195 octeontx_port_stats(struct octeontx_nic *nic, struct rte_eth_stats *stats)
196 {
197         octeontx_mbox_bgx_port_stats_t bgx_stats;
198         int res;
199
200         PMD_INIT_FUNC_TRACE();
201
202         res = octeontx_bgx_port_stats(nic->port_id, &bgx_stats);
203         if (res < 0)
204                 octeontx_log_err("failed to get port stats %d", nic->port_id);
205
206         stats->ipackets = bgx_stats.rx_packets;
207         stats->ibytes = bgx_stats.rx_bytes;
208         stats->imissed = bgx_stats.rx_dropped;
209         stats->ierrors = bgx_stats.rx_errors;
210         stats->opackets = bgx_stats.tx_packets;
211         stats->obytes = bgx_stats.tx_bytes;
212         stats->oerrors = bgx_stats.tx_errors;
213
214         octeontx_log_dbg("port%d stats inpkts=%" PRIx64 " outpkts=%" PRIx64 "",
215                         nic->port_id, stats->ipackets, stats->opackets);
216 }
217
218 static void
219 octeontx_port_stats_clr(struct octeontx_nic *nic)
220 {
221         PMD_INIT_FUNC_TRACE();
222
223         octeontx_bgx_port_stats_clr(nic->port_id);
224 }
225
226 static inline void
227 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
228                                 struct rte_event_dev_info *info)
229 {
230         memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
231         dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
232
233         dev_conf->nb_event_ports = info->max_event_ports;
234         dev_conf->nb_event_queues = info->max_event_queues;
235
236         dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
237         dev_conf->nb_event_port_dequeue_depth =
238                         info->max_event_port_dequeue_depth;
239         dev_conf->nb_event_port_enqueue_depth =
240                         info->max_event_port_enqueue_depth;
241         dev_conf->nb_event_port_enqueue_depth =
242                         info->max_event_port_enqueue_depth;
243         dev_conf->nb_events_limit =
244                         info->max_num_events;
245 }
246
247 static int
248 octeontx_dev_configure(struct rte_eth_dev *dev)
249 {
250         struct rte_eth_dev_data *data = dev->data;
251         struct rte_eth_conf *conf = &data->dev_conf;
252         struct rte_eth_rxmode *rxmode = &conf->rxmode;
253         struct rte_eth_txmode *txmode = &conf->txmode;
254         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
255         int ret;
256
257         PMD_INIT_FUNC_TRACE();
258         RTE_SET_USED(conf);
259
260         if (!rte_eal_has_hugepages()) {
261                 octeontx_log_err("huge page is not configured");
262                 return -EINVAL;
263         }
264
265         if (txmode->mq_mode) {
266                 octeontx_log_err("tx mq_mode DCB or VMDq not supported");
267                 return -EINVAL;
268         }
269
270         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
271                 rxmode->mq_mode != ETH_MQ_RX_RSS) {
272                 octeontx_log_err("unsupported rx qmode %d", rxmode->mq_mode);
273                 return -EINVAL;
274         }
275
276         if (!rxmode->hw_strip_crc) {
277                 PMD_INIT_LOG(NOTICE, "can't disable hw crc strip");
278                 rxmode->hw_strip_crc = 1;
279         }
280
281         if (rxmode->hw_ip_checksum) {
282                 PMD_INIT_LOG(NOTICE, "rxcksum not supported");
283                 rxmode->hw_ip_checksum = 0;
284         }
285
286         if (rxmode->split_hdr_size) {
287                 octeontx_log_err("rxmode does not support split header");
288                 return -EINVAL;
289         }
290
291         if (rxmode->hw_vlan_filter) {
292                 octeontx_log_err("VLAN filter not supported");
293                 return -EINVAL;
294         }
295
296         if (rxmode->hw_vlan_extend) {
297                 octeontx_log_err("VLAN extended not supported");
298                 return -EINVAL;
299         }
300
301         if (rxmode->enable_lro) {
302                 octeontx_log_err("LRO not supported");
303                 return -EINVAL;
304         }
305
306         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
307                 octeontx_log_err("setting link speed/duplex not supported");
308                 return -EINVAL;
309         }
310
311         if (conf->dcb_capability_en) {
312                 octeontx_log_err("DCB enable not supported");
313                 return -EINVAL;
314         }
315
316         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
317                 octeontx_log_err("flow director not supported");
318                 return -EINVAL;
319         }
320
321         nic->num_tx_queues = dev->data->nb_tx_queues;
322
323         ret = octeontx_pko_channel_open(nic->port_id * PKO_VF_NUM_DQ,
324                                         nic->num_tx_queues,
325                                         nic->base_ochan);
326         if (ret) {
327                 octeontx_log_err("failed to open channel %d no-of-txq %d",
328                            nic->base_ochan, nic->num_tx_queues);
329                 return -EFAULT;
330         }
331
332         nic->pki.classifier_enable = false;
333         nic->pki.hash_enable = true;
334         nic->pki.initialized = false;
335
336         return 0;
337 }
338
339 static void
340 octeontx_dev_promisc_enable(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, 1);
346 }
347
348 static void
349 octeontx_dev_promisc_disable(struct rte_eth_dev *dev)
350 {
351         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
352
353         PMD_INIT_FUNC_TRACE();
354         octeontx_port_promisc_set(nic, 0);
355 }
356
357 static inline int
358 octeontx_atomic_write_link_status(struct rte_eth_dev *dev,
359                                   struct rte_eth_link *link)
360 {
361         struct rte_eth_link *dst = &dev->data->dev_link;
362         struct rte_eth_link *src = link;
363
364         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
365                 *(uint64_t *)src) == 0)
366                 return -1;
367
368         return 0;
369 }
370
371 static int
372 octeontx_port_link_status(struct octeontx_nic *nic)
373 {
374         int res;
375
376         PMD_INIT_FUNC_TRACE();
377         res = octeontx_bgx_port_link_status(nic->port_id);
378         if (res < 0) {
379                 octeontx_log_err("failed to get port %d link status",
380                                 nic->port_id);
381                 return res;
382         }
383
384         nic->link_up = (uint8_t)res;
385         octeontx_log_dbg("port %d link status %d", nic->port_id, nic->link_up);
386
387         return res;
388 }
389
390 /*
391  * Return 0 means link status changed, -1 means not changed
392  */
393 static int
394 octeontx_dev_link_update(struct rte_eth_dev *dev,
395                          int wait_to_complete __rte_unused)
396 {
397         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
398         struct rte_eth_link link;
399         int res;
400
401         res = 0;
402         PMD_INIT_FUNC_TRACE();
403
404         res = octeontx_port_link_status(nic);
405         if (res < 0) {
406                 octeontx_log_err("failed to request link status %d", res);
407                 return res;
408         }
409
410         link.link_status = nic->link_up;
411
412         switch (nic->speed) {
413         case OCTEONTX_LINK_SPEED_SGMII:
414                 link.link_speed = ETH_SPEED_NUM_1G;
415                 break;
416
417         case OCTEONTX_LINK_SPEED_XAUI:
418                 link.link_speed = ETH_SPEED_NUM_10G;
419                 break;
420
421         case OCTEONTX_LINK_SPEED_RXAUI:
422         case OCTEONTX_LINK_SPEED_10G_R:
423                 link.link_speed = ETH_SPEED_NUM_10G;
424                 break;
425         case OCTEONTX_LINK_SPEED_QSGMII:
426                 link.link_speed = ETH_SPEED_NUM_5G;
427                 break;
428         case OCTEONTX_LINK_SPEED_40G_R:
429                 link.link_speed = ETH_SPEED_NUM_40G;
430                 break;
431
432         case OCTEONTX_LINK_SPEED_RESERVE1:
433         case OCTEONTX_LINK_SPEED_RESERVE2:
434         default:
435                 octeontx_log_err("incorrect link speed %d", nic->speed);
436                 break;
437         }
438
439         link.link_duplex = ETH_LINK_AUTONEG;
440         link.link_autoneg = ETH_LINK_SPEED_AUTONEG;
441
442         return octeontx_atomic_write_link_status(dev, &link);
443 }
444
445 static void
446 octeontx_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
447 {
448         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
449
450         PMD_INIT_FUNC_TRACE();
451         octeontx_port_stats(nic, stats);
452 }
453
454 static void
455 octeontx_dev_stats_reset(struct rte_eth_dev *dev)
456 {
457         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
458
459         PMD_INIT_FUNC_TRACE();
460         octeontx_port_stats_clr(nic);
461 }
462
463 static void
464 octeontx_dev_default_mac_addr_set(struct rte_eth_dev *dev,
465                                         struct ether_addr *addr)
466 {
467         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
468         int ret;
469
470         ret = octeontx_bgx_port_mac_set(nic->port_id, addr->addr_bytes);
471         if (ret != 0)
472                 octeontx_log_err("failed to set MAC address on port %d",
473                                 nic->port_id);
474 }
475
476 static void
477 octeontx_dev_info(struct rte_eth_dev *dev,
478                 struct rte_eth_dev_info *dev_info)
479 {
480         RTE_SET_USED(dev);
481
482         /* Autonegotiation may be disabled */
483         dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
484         dev_info->speed_capa |= ETH_LINK_SPEED_10M | ETH_LINK_SPEED_100M |
485                         ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
486                         ETH_LINK_SPEED_40G;
487
488         dev_info->driver_name = RTE_STR(rte_octeontx_pmd);
489         dev_info->max_mac_addrs = 1;
490         dev_info->max_rx_pktlen = PKI_MAX_PKTLEN;
491         dev_info->max_rx_queues = 1;
492         dev_info->max_tx_queues = PKO_MAX_NUM_DQ;
493         dev_info->min_rx_bufsize = 0;
494         dev_info->pci_dev = NULL;
495
496         dev_info->default_rxconf = (struct rte_eth_rxconf) {
497                 .rx_free_thresh = 0,
498                 .rx_drop_en = 0,
499         };
500
501         dev_info->default_txconf = (struct rte_eth_txconf) {
502                 .tx_free_thresh = 0,
503                 .txq_flags =
504                         ETH_TXQ_FLAGS_NOMULTSEGS |
505                         ETH_TXQ_FLAGS_NOOFFLOADS |
506                         ETH_TXQ_FLAGS_NOXSUMS,
507         };
508
509         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MT_LOCKFREE;
510 }
511
512 static void
513 octeontx_dq_info_getter(octeontx_dq_t *dq, void *out)
514 {
515         ((octeontx_dq_t *)out)->lmtline_va = dq->lmtline_va;
516         ((octeontx_dq_t *)out)->ioreg_va = dq->ioreg_va;
517         ((octeontx_dq_t *)out)->fc_status_va = dq->fc_status_va;
518 }
519
520 static int
521 octeontx_vf_start_tx_queue(struct rte_eth_dev *dev, struct octeontx_nic *nic,
522                                 uint16_t qidx)
523 {
524         struct octeontx_txq *txq;
525         int res;
526
527         PMD_INIT_FUNC_TRACE();
528
529         if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
530                 return 0;
531
532         txq = dev->data->tx_queues[qidx];
533
534         res = octeontx_pko_channel_query_dqs(nic->base_ochan,
535                                                 &txq->dq,
536                                                 sizeof(octeontx_dq_t),
537                                                 txq->queue_id,
538                                                 octeontx_dq_info_getter);
539         if (res < 0) {
540                 res = -EFAULT;
541                 goto close_port;
542         }
543
544         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
545         return res;
546
547 close_port:
548         (void)octeontx_port_stop(nic);
549         octeontx_pko_channel_stop(nic->base_ochan);
550         octeontx_pko_channel_close(nic->base_ochan);
551         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
552         return res;
553 }
554
555 static int
556 octeontx_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t qidx)
557 {
558         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
559
560         PMD_INIT_FUNC_TRACE();
561         qidx = qidx % PKO_VF_NUM_DQ;
562         return octeontx_vf_start_tx_queue(dev, nic, qidx);
563 }
564
565 static inline int
566 octeontx_vf_stop_tx_queue(struct rte_eth_dev *dev, struct octeontx_nic *nic,
567                           uint16_t qidx)
568 {
569         int ret = 0;
570
571         RTE_SET_USED(nic);
572         PMD_INIT_FUNC_TRACE();
573
574         if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
575                 return 0;
576
577         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
578         return ret;
579 }
580
581 static int
582 octeontx_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx)
583 {
584         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
585
586         PMD_INIT_FUNC_TRACE();
587         qidx = qidx % PKO_VF_NUM_DQ;
588
589         return octeontx_vf_stop_tx_queue(dev, nic, qidx);
590 }
591
592 static void
593 octeontx_dev_tx_queue_release(void *tx_queue)
594 {
595         struct octeontx_txq *txq = tx_queue;
596         int res;
597
598         PMD_INIT_FUNC_TRACE();
599
600         if (txq) {
601                 res = octeontx_dev_tx_queue_stop(txq->eth_dev, txq->queue_id);
602                 if (res < 0)
603                         octeontx_log_err("failed stop tx_queue(%d)\n",
604                                    txq->queue_id);
605
606                 rte_free(txq);
607         }
608 }
609
610 static int
611 octeontx_dev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
612                             uint16_t nb_desc, unsigned int socket_id,
613                             const struct rte_eth_txconf *tx_conf)
614 {
615         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
616         struct octeontx_txq *txq = NULL;
617         uint16_t dq_num;
618         int res = 0;
619
620         RTE_SET_USED(nb_desc);
621         RTE_SET_USED(socket_id);
622         RTE_SET_USED(tx_conf);
623
624         dq_num = (nic->port_id * PKO_VF_NUM_DQ) + qidx;
625
626         /* Socket id check */
627         if (socket_id != (unsigned int)SOCKET_ID_ANY &&
628                         socket_id != (unsigned int)nic->node)
629                 PMD_TX_LOG(INFO, "socket_id expected %d, configured %d",
630                                                 socket_id, nic->node);
631
632         /* Free memory prior to re-allocation if needed. */
633         if (dev->data->tx_queues[qidx] != NULL) {
634                 PMD_TX_LOG(DEBUG, "freeing memory prior to re-allocation %d",
635                                 qidx);
636                 octeontx_dev_tx_queue_release(dev->data->tx_queues[qidx]);
637                 dev->data->tx_queues[qidx] = NULL;
638         }
639
640         /* Allocating tx queue data structure */
641         txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct octeontx_txq),
642                                  RTE_CACHE_LINE_SIZE, nic->node);
643         if (txq == NULL) {
644                 octeontx_log_err("failed to allocate txq=%d", qidx);
645                 res = -ENOMEM;
646                 goto err;
647         }
648
649         txq->eth_dev = dev;
650         txq->queue_id = dq_num;
651         dev->data->tx_queues[qidx] = txq;
652         dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
653
654         res = octeontx_pko_channel_query_dqs(nic->base_ochan,
655                                                 &txq->dq,
656                                                 sizeof(octeontx_dq_t),
657                                                 txq->queue_id,
658                                                 octeontx_dq_info_getter);
659         if (res < 0) {
660                 res = -EFAULT;
661                 goto err;
662         }
663
664         PMD_TX_LOG(DEBUG, "[%d]:[%d] txq=%p nb_desc=%d lmtline=%p ioreg_va=%p fc_status_va=%p",
665                         qidx, txq->queue_id, txq, nb_desc, txq->dq.lmtline_va,
666                         txq->dq.ioreg_va,
667                         txq->dq.fc_status_va);
668
669         return res;
670
671 err:
672         if (txq)
673                 rte_free(txq);
674
675         return res;
676 }
677
678 static int
679 octeontx_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx,
680                                 uint16_t nb_desc, unsigned int socket_id,
681                                 const struct rte_eth_rxconf *rx_conf,
682                                 struct rte_mempool *mb_pool)
683 {
684         struct octeontx_nic *nic = octeontx_pmd_priv(dev);
685         struct rte_mempool_ops *mp_ops = NULL;
686         struct octeontx_rxq *rxq = NULL;
687         pki_pktbuf_cfg_t pktbuf_conf;
688         pki_hash_cfg_t pki_hash;
689         pki_qos_cfg_t pki_qos;
690         uintptr_t pool;
691         int ret, port;
692         uint8_t gaura;
693         unsigned int ev_queues = (nic->ev_queues * nic->port_id) + qidx;
694         unsigned int ev_ports = (nic->ev_ports * nic->port_id) + qidx;
695
696         RTE_SET_USED(nb_desc);
697
698         memset(&pktbuf_conf, 0, sizeof(pktbuf_conf));
699         memset(&pki_hash, 0, sizeof(pki_hash));
700         memset(&pki_qos, 0, sizeof(pki_qos));
701
702         mp_ops = rte_mempool_get_ops(mb_pool->ops_index);
703         if (strcmp(mp_ops->name, "octeontx_fpavf")) {
704                 octeontx_log_err("failed to find octeontx_fpavf mempool");
705                 return -ENOTSUP;
706         }
707
708         /* Handle forbidden configurations */
709         if (nic->pki.classifier_enable) {
710                 octeontx_log_err("cannot setup queue %d. "
711                                         "Classifier option unsupported", qidx);
712                 return -EINVAL;
713         }
714
715         port = nic->port_id;
716
717         /* Rx deferred start is not supported */
718         if (rx_conf->rx_deferred_start) {
719                 octeontx_log_err("rx deferred start not supported");
720                 return -EINVAL;
721         }
722
723         /* Verify queue index */
724         if (qidx >= dev->data->nb_rx_queues) {
725                 octeontx_log_err("QID %d not supporteded (0 - %d available)\n",
726                                 qidx, (dev->data->nb_rx_queues - 1));
727                 return -ENOTSUP;
728         }
729
730         /* Socket id check */
731         if (socket_id != (unsigned int)SOCKET_ID_ANY &&
732                         socket_id != (unsigned int)nic->node)
733                 PMD_RX_LOG(INFO, "socket_id expected %d, configured %d",
734                                                 socket_id, nic->node);
735
736         /* Allocating rx queue data structure */
737         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct octeontx_rxq),
738                                  RTE_CACHE_LINE_SIZE, nic->node);
739         if (rxq == NULL) {
740                 octeontx_log_err("failed to allocate rxq=%d", qidx);
741                 return -ENOMEM;
742         }
743
744         if (!nic->pki.initialized) {
745                 pktbuf_conf.port_type = 0;
746                 pki_hash.port_type = 0;
747                 pki_qos.port_type = 0;
748
749                 pktbuf_conf.mmask.f_wqe_skip = 1;
750                 pktbuf_conf.mmask.f_first_skip = 1;
751                 pktbuf_conf.mmask.f_later_skip = 1;
752                 pktbuf_conf.mmask.f_mbuff_size = 1;
753                 pktbuf_conf.mmask.f_cache_mode = 1;
754
755                 pktbuf_conf.wqe_skip = OCTTX_PACKET_WQE_SKIP;
756                 pktbuf_conf.first_skip = OCTTX_PACKET_FIRST_SKIP;
757                 pktbuf_conf.later_skip = OCTTX_PACKET_LATER_SKIP;
758                 pktbuf_conf.mbuff_size = (mb_pool->elt_size -
759                                         RTE_PKTMBUF_HEADROOM -
760                                         sizeof(struct rte_mbuf));
761
762                 pktbuf_conf.cache_mode = PKI_OPC_MODE_STF2_STT;
763
764                 ret = octeontx_pki_port_pktbuf_config(port, &pktbuf_conf);
765                 if (ret != 0) {
766                         octeontx_log_err("fail to configure pktbuf for port %d",
767                                         port);
768                         rte_free(rxq);
769                         return ret;
770                 }
771                 PMD_RX_LOG(DEBUG, "Port %d Rx pktbuf configured:\n"
772                                 "\tmbuf_size:\t0x%0x\n"
773                                 "\twqe_skip:\t0x%0x\n"
774                                 "\tfirst_skip:\t0x%0x\n"
775                                 "\tlater_skip:\t0x%0x\n"
776                                 "\tcache_mode:\t%s\n",
777                                 port,
778                                 pktbuf_conf.mbuff_size,
779                                 pktbuf_conf.wqe_skip,
780                                 pktbuf_conf.first_skip,
781                                 pktbuf_conf.later_skip,
782                                 (pktbuf_conf.cache_mode ==
783                                                 PKI_OPC_MODE_STT) ?
784                                 "STT" :
785                                 (pktbuf_conf.cache_mode ==
786                                                 PKI_OPC_MODE_STF) ?
787                                 "STF" :
788                                 (pktbuf_conf.cache_mode ==
789                                                 PKI_OPC_MODE_STF1_STT) ?
790                                 "STF1_STT" : "STF2_STT");
791
792                 if (nic->pki.hash_enable) {
793                         pki_hash.tag_dlc = 1;
794                         pki_hash.tag_slc = 1;
795                         pki_hash.tag_dlf = 1;
796                         pki_hash.tag_slf = 1;
797                         octeontx_pki_port_hash_config(port, &pki_hash);
798                 }
799
800                 pool = (uintptr_t)mb_pool->pool_id;
801
802                 /* Get the gpool Id */
803                 gaura = octeontx_fpa_bufpool_gpool(pool);
804
805                 pki_qos.qpg_qos = PKI_QPG_QOS_NONE;
806                 pki_qos.num_entry = 1;
807                 pki_qos.drop_policy = 0;
808                 pki_qos.tag_type = 2L;
809                 pki_qos.qos_entry[0].port_add = 0;
810                 pki_qos.qos_entry[0].gaura = gaura;
811                 pki_qos.qos_entry[0].ggrp_ok = ev_queues;
812                 pki_qos.qos_entry[0].ggrp_bad = ev_queues;
813                 pki_qos.qos_entry[0].grptag_bad = 0;
814                 pki_qos.qos_entry[0].grptag_ok = 0;
815
816                 ret = octeontx_pki_port_create_qos(port, &pki_qos);
817                 if (ret < 0) {
818                         octeontx_log_err("failed to create QOS port=%d, q=%d",
819                                         port, qidx);
820                         rte_free(rxq);
821                         return ret;
822                 }
823                 nic->pki.initialized = true;
824         }
825
826         rxq->port_id = nic->port_id;
827         rxq->eth_dev = dev;
828         rxq->queue_id = qidx;
829         rxq->evdev = nic->evdev;
830         rxq->ev_queues = ev_queues;
831         rxq->ev_ports = ev_ports;
832
833         dev->data->rx_queues[qidx] = rxq;
834         dev->data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
835         return 0;
836 }
837
838 static void
839 octeontx_dev_rx_queue_release(void *rxq)
840 {
841         rte_free(rxq);
842 }
843
844 static const uint32_t *
845 octeontx_dev_supported_ptypes_get(struct rte_eth_dev *dev)
846 {
847         static const uint32_t ptypes[] = {
848                 RTE_PTYPE_L3_IPV4,
849                 RTE_PTYPE_L3_IPV4_EXT,
850                 RTE_PTYPE_L3_IPV6,
851                 RTE_PTYPE_L3_IPV6_EXT,
852                 RTE_PTYPE_L4_TCP,
853                 RTE_PTYPE_L4_UDP,
854                 RTE_PTYPE_L4_FRAG,
855                 RTE_PTYPE_UNKNOWN
856         };
857
858         if (dev->rx_pkt_burst == octeontx_recv_pkts)
859                 return ptypes;
860
861         return NULL;
862 }
863
864 /* Initialize and register driver with DPDK Application */
865 static const struct eth_dev_ops octeontx_dev_ops = {
866         .dev_configure           = octeontx_dev_configure,
867         .dev_infos_get           = octeontx_dev_info,
868         .promiscuous_enable      = octeontx_dev_promisc_enable,
869         .promiscuous_disable     = octeontx_dev_promisc_disable,
870         .link_update             = octeontx_dev_link_update,
871         .stats_get               = octeontx_dev_stats_get,
872         .stats_reset             = octeontx_dev_stats_reset,
873         .mac_addr_set            = octeontx_dev_default_mac_addr_set,
874         .tx_queue_start          = octeontx_dev_tx_queue_start,
875         .tx_queue_stop           = octeontx_dev_tx_queue_stop,
876         .tx_queue_setup          = octeontx_dev_tx_queue_setup,
877         .tx_queue_release        = octeontx_dev_tx_queue_release,
878         .rx_queue_setup          = octeontx_dev_rx_queue_setup,
879         .rx_queue_release        = octeontx_dev_rx_queue_release,
880         .dev_supported_ptypes_get = octeontx_dev_supported_ptypes_get,
881 };
882
883 /* Create Ethdev interface per BGX LMAC ports */
884 static int
885 octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
886                         int socket_id)
887 {
888         int res;
889         char octtx_name[OCTEONTX_MAX_NAME_LEN];
890         struct octeontx_nic *nic = NULL;
891         struct rte_eth_dev *eth_dev = NULL;
892         struct rte_eth_dev_data *data = NULL;
893         const char *name = rte_vdev_device_name(dev);
894
895         PMD_INIT_FUNC_TRACE();
896
897         sprintf(octtx_name, "%s_%d", name, port);
898         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
899                 eth_dev = rte_eth_dev_attach_secondary(octtx_name);
900                 if (eth_dev == NULL)
901                         return -ENODEV;
902
903                 return 0;
904         }
905
906         data = rte_zmalloc_socket(octtx_name, sizeof(*data), 0, socket_id);
907         if (data == NULL) {
908                 octeontx_log_err("failed to allocate devdata");
909                 res = -ENOMEM;
910                 goto err;
911         }
912
913         nic = rte_zmalloc_socket(octtx_name, sizeof(*nic), 0, socket_id);
914         if (nic == NULL) {
915                 octeontx_log_err("failed to allocate nic structure");
916                 res = -ENOMEM;
917                 goto err;
918         }
919
920         nic->port_id = port;
921         nic->evdev = evdev;
922
923         res = octeontx_port_open(nic);
924         if (res < 0)
925                 goto err;
926
927         /* Rx side port configuration */
928         res = octeontx_pki_port_open(port);
929         if (res != 0) {
930                 octeontx_log_err("failed to open PKI port %d", port);
931                 res = -ENODEV;
932                 goto err;
933         }
934
935         /* Reserve an ethdev entry */
936         eth_dev = rte_eth_dev_allocate(octtx_name);
937         if (eth_dev == NULL) {
938                 octeontx_log_err("failed to allocate rte_eth_dev");
939                 res = -ENOMEM;
940                 goto err;
941         }
942
943         eth_dev->device = &dev->device;
944         eth_dev->intr_handle = NULL;
945         eth_dev->data->kdrv = RTE_KDRV_NONE;
946         eth_dev->data->numa_node = dev->device.numa_node;
947
948         rte_memcpy(data, (eth_dev)->data, sizeof(*data));
949         data->dev_private = nic;
950
951         data->port_id = eth_dev->data->port_id;
952         snprintf(data->name, sizeof(data->name), "%s", eth_dev->data->name);
953
954         nic->ev_queues = 1;
955         nic->ev_ports = 1;
956
957         data->dev_link.link_status = ETH_LINK_DOWN;
958         data->dev_started = 0;
959         data->promiscuous = 0;
960         data->all_multicast = 0;
961         data->scattered_rx = 0;
962
963         data->mac_addrs = rte_zmalloc_socket(octtx_name, ETHER_ADDR_LEN, 0,
964                                                         socket_id);
965         if (data->mac_addrs == NULL) {
966                 octeontx_log_err("failed to allocate memory for mac_addrs");
967                 res = -ENOMEM;
968                 goto err;
969         }
970
971         eth_dev->data = data;
972         eth_dev->dev_ops = &octeontx_dev_ops;
973
974         /* Finally save ethdev pointer to the NIC structure */
975         nic->dev = eth_dev;
976
977         if (nic->port_id != data->port_id) {
978                 octeontx_log_err("eth_dev->port_id (%d) is diff to orig (%d)",
979                                 data->port_id, nic->port_id);
980                 res = -EINVAL;
981                 goto err;
982         }
983
984         /* Update port_id mac to eth_dev */
985         memcpy(data->mac_addrs, nic->mac_addr, ETHER_ADDR_LEN);
986
987         PMD_INIT_LOG(DEBUG, "ethdev info: ");
988         PMD_INIT_LOG(DEBUG, "port %d, port_ena %d ochan %d num_ochan %d tx_q %d",
989                                 nic->port_id, nic->port_ena,
990                                 nic->base_ochan, nic->num_ochans,
991                                 nic->num_tx_queues);
992         PMD_INIT_LOG(DEBUG, "speed %d mtu %d", nic->speed, nic->mtu);
993
994         return data->port_id;
995
996 err:
997         if (port)
998                 octeontx_port_close(nic);
999
1000         if (eth_dev != NULL) {
1001                 rte_free(eth_dev->data->mac_addrs);
1002                 rte_free(data);
1003                 rte_free(nic);
1004                 rte_eth_dev_release_port(eth_dev);
1005         }
1006
1007         return res;
1008 }
1009
1010 /* Un initialize octeontx device */
1011 static int
1012 octeontx_remove(struct rte_vdev_device *dev)
1013 {
1014         char octtx_name[OCTEONTX_MAX_NAME_LEN];
1015         struct rte_eth_dev *eth_dev = NULL;
1016         struct octeontx_nic *nic = NULL;
1017         int i;
1018
1019         if (dev == NULL)
1020                 return -EINVAL;
1021
1022         for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
1023                 sprintf(octtx_name, "eth_octeontx_%d", i);
1024
1025                 /* reserve an ethdev entry */
1026                 eth_dev = rte_eth_dev_allocated(octtx_name);
1027                 if (eth_dev == NULL)
1028                         return -ENODEV;
1029
1030                 nic = octeontx_pmd_priv(eth_dev);
1031                 rte_event_dev_stop(nic->evdev);
1032                 PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
1033
1034                 rte_free(eth_dev->data->mac_addrs);
1035                 rte_free(eth_dev->data->dev_private);
1036                 rte_free(eth_dev->data);
1037                 rte_eth_dev_release_port(eth_dev);
1038                 rte_event_dev_close(nic->evdev);
1039         }
1040
1041         /* Free FC resource */
1042         octeontx_pko_fc_free();
1043
1044         return 0;
1045 }
1046
1047 /* Initialize octeontx device */
1048 static int
1049 octeontx_probe(struct rte_vdev_device *dev)
1050 {
1051         const char *dev_name;
1052         static int probe_once;
1053         uint8_t socket_id, qlist;
1054         int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
1055         struct rte_event_dev_config dev_conf;
1056         const char *eventdev_name = "event_octeontx";
1057         struct rte_event_dev_info info;
1058
1059         struct octeontx_vdev_init_params init_params = {
1060                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
1061         };
1062
1063         dev_name = rte_vdev_device_name(dev);
1064         res = octeontx_parse_vdev_init_params(&init_params, dev);
1065         if (res < 0)
1066                 return -EINVAL;
1067
1068         if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
1069                 octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
1070                                 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
1071                 return -ENOTSUP;
1072         }
1073
1074         PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
1075
1076         socket_id = rte_socket_id();
1077
1078         tx_vfcnt = octeontx_pko_vf_count();
1079
1080         if (tx_vfcnt < init_params.nr_port) {
1081                 octeontx_log_err("not enough PKO (%d) for port number (%d)",
1082                                 tx_vfcnt, init_params.nr_port);
1083                 return -EINVAL;
1084         }
1085         evdev = rte_event_dev_get_dev_id(eventdev_name);
1086         if (evdev < 0) {
1087                 octeontx_log_err("eventdev %s not found", eventdev_name);
1088                 return -ENODEV;
1089         }
1090
1091         res = rte_event_dev_info_get(evdev, &info);
1092         if (res < 0) {
1093                 octeontx_log_err("failed to eventdev info %d", res);
1094                 return -EINVAL;
1095         }
1096
1097         PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
1098                         info.max_event_queues, info.max_event_ports);
1099
1100         if (octeontx_pko_init_fc(tx_vfcnt))
1101                 return -ENOMEM;
1102
1103         devconf_set_default_sane_values(&dev_conf, &info);
1104         res = rte_event_dev_configure(evdev, &dev_conf);
1105         if (res < 0)
1106                 goto parse_error;
1107
1108         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
1109                         (uint32_t *)&pnum);
1110         rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
1111                         (uint32_t *)&qnum);
1112         if (pnum < qnum) {
1113                 octeontx_log_err("too few event ports (%d) for event_q(%d)",
1114                                 pnum, qnum);
1115                 res = -EINVAL;
1116                 goto parse_error;
1117         }
1118         if (pnum > qnum) {
1119                 /*
1120                  * We don't poll on event ports
1121                  * that do not have any queues assigned.
1122                  */
1123                 pnum = qnum;
1124                 PMD_INIT_LOG(INFO,
1125                         "reducing number of active event ports to %d", pnum);
1126         }
1127         for (i = 0; i < qnum; i++) {
1128                 res = rte_event_queue_setup(evdev, i, NULL);
1129                 if (res < 0) {
1130                         octeontx_log_err("failed to setup event_q(%d): res %d",
1131                                         i, res);
1132                         goto parse_error;
1133                 }
1134         }
1135
1136         for (i = 0; i < pnum; i++) {
1137                 res = rte_event_port_setup(evdev, i, NULL);
1138                 if (res < 0) {
1139                         res = -ENODEV;
1140                         octeontx_log_err("failed to setup ev port(%d) res=%d",
1141                                                 i, res);
1142                         goto parse_error;
1143                 }
1144                 /* Link one queue to one event port */
1145                 qlist = i;
1146                 res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
1147                 if (res < 0) {
1148                         res = -ENODEV;
1149                         octeontx_log_err("failed to link port (%d): res=%d",
1150                                         i, res);
1151                         goto parse_error;
1152                 }
1153         }
1154
1155         /* Create ethdev interface */
1156         for (i = 0; i < init_params.nr_port; i++) {
1157                 port_id = octeontx_create(dev, i, evdev, socket_id);
1158                 if (port_id < 0) {
1159                         octeontx_log_err("failed to create device %s",
1160                                         dev_name);
1161                         res = -ENODEV;
1162                         goto parse_error;
1163                 }
1164
1165                 PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
1166                                         port_id);
1167         }
1168
1169         if (probe_once) {
1170                 octeontx_log_err("interface %s not supported", dev_name);
1171                 octeontx_remove(dev);
1172                 res = -ENOTSUP;
1173                 goto parse_error;
1174         }
1175         probe_once = 1;
1176
1177         return 0;
1178
1179 parse_error:
1180         octeontx_pko_fc_free();
1181         return res;
1182 }
1183
1184 static struct rte_vdev_driver octeontx_pmd_drv = {
1185         .probe = octeontx_probe,
1186         .remove = octeontx_remove,
1187 };
1188
1189 RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
1190 RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
1191 RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");