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