4 * Copyright (C) Cavium Inc. 2017. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <rte_alarm.h>
40 #include <rte_branch_prediction.h>
41 #include <rte_debug.h>
42 #include <rte_devargs.h>
44 #include <rte_kvargs.h>
45 #include <rte_malloc.h>
46 #include <rte_prefetch.h>
49 #include "octeontx_ethdev.h"
50 #include "octeontx_logs.h"
52 struct octeontx_vdev_init_params {
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
67 /* Parse integer from integer argument */
69 parse_integer_arg(const char *key __rte_unused,
70 const char *value, void *extra_args)
72 int *i = (int *)extra_args;
76 octeontx_log_err("argument has to be positive.");
84 octeontx_parse_vdev_init_params(struct octeontx_vdev_init_params *params,
85 struct rte_vdev_device *dev)
87 struct rte_kvargs *kvlist = NULL;
90 static const char * const octeontx_vdev_valid_params[] = {
91 OCTEONTX_VDEV_NR_PORT_ARG,
95 const char *input_args = rte_vdev_device_args(dev);
101 kvlist = rte_kvargs_parse(input_args,
102 octeontx_vdev_valid_params);
106 ret = rte_kvargs_process(kvlist,
107 OCTEONTX_VDEV_NR_PORT_ARG,
115 rte_kvargs_free(kvlist);
120 octeontx_port_open(struct octeontx_nic *nic)
122 octeontx_mbox_bgx_port_conf_t bgx_port_conf;
127 PMD_INIT_FUNC_TRACE();
129 res = octeontx_bgx_port_open(nic->port_id, &bgx_port_conf);
131 octeontx_log_err("failed to open port %d", res);
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;
148 memcpy(&nic->mac_addr[0], &bgx_port_conf.macaddr[0], ETHER_ADDR_LEN);
150 octeontx_log_dbg("port opened %d", nic->port_id);
155 octeontx_port_close(struct octeontx_nic *nic)
157 PMD_INIT_FUNC_TRACE();
159 octeontx_bgx_port_close(nic->port_id);
160 octeontx_log_dbg("port closed %d", nic->port_id);
164 octeontx_port_promisc_set(struct octeontx_nic *nic, int en)
166 struct rte_eth_dev *dev;
170 PMD_INIT_FUNC_TRACE();
173 res = octeontx_bgx_port_promisc_set(nic->port_id, en);
175 octeontx_log_err("failed to set promiscuous mode %d",
178 /* Set proper flag for the mode */
179 dev->data->promiscuous = (en != 0) ? 1 : 0;
181 octeontx_log_dbg("port %d : promiscuous mode %s",
182 nic->port_id, en ? "set" : "unset");
186 octeontx_port_stats(struct octeontx_nic *nic, struct rte_eth_stats *stats)
188 octeontx_mbox_bgx_port_stats_t bgx_stats;
191 PMD_INIT_FUNC_TRACE();
193 res = octeontx_bgx_port_stats(nic->port_id, &bgx_stats);
195 octeontx_log_err("failed to get port stats %d", nic->port_id);
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;
205 octeontx_log_dbg("port%d stats inpkts=%" PRIx64 " outpkts=%" PRIx64 "",
206 nic->port_id, stats->ipackets, stats->opackets);
210 octeontx_port_stats_clr(struct octeontx_nic *nic)
212 PMD_INIT_FUNC_TRACE();
214 octeontx_bgx_port_stats_clr(nic->port_id);
218 devconf_set_default_sane_values(struct rte_event_dev_config *dev_conf,
219 struct rte_event_dev_info *info)
221 memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
222 dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
224 dev_conf->nb_event_ports = info->max_event_ports;
225 dev_conf->nb_event_queues = info->max_event_queues;
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;
239 octeontx_dev_configure(struct rte_eth_dev *dev)
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);
248 PMD_INIT_FUNC_TRACE();
251 if (!rte_eal_has_hugepages()) {
252 octeontx_log_err("huge page is not configured");
256 if (txmode->mq_mode) {
257 octeontx_log_err("tx mq_mode DCB or VMDq not supported");
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);
267 if (!rxmode->hw_strip_crc) {
268 PMD_INIT_LOG(NOTICE, "can't disable hw crc strip");
269 rxmode->hw_strip_crc = 1;
272 if (rxmode->hw_ip_checksum) {
273 PMD_INIT_LOG(NOTICE, "rxcksum not supported");
274 rxmode->hw_ip_checksum = 0;
277 if (rxmode->split_hdr_size) {
278 octeontx_log_err("rxmode does not support split header");
282 if (rxmode->hw_vlan_filter) {
283 octeontx_log_err("VLAN filter not supported");
287 if (rxmode->hw_vlan_extend) {
288 octeontx_log_err("VLAN extended not supported");
292 if (rxmode->enable_lro) {
293 octeontx_log_err("LRO not supported");
297 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
298 octeontx_log_err("setting link speed/duplex not supported");
302 if (conf->dcb_capability_en) {
303 octeontx_log_err("DCB enable not supported");
307 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
308 octeontx_log_err("flow director not supported");
312 nic->num_tx_queues = dev->data->nb_tx_queues;
314 ret = octeontx_pko_channel_open(nic->port_id * PKO_VF_NUM_DQ,
318 octeontx_log_err("failed to open channel %d no-of-txq %d",
319 nic->base_ochan, nic->num_tx_queues);
323 nic->pki.classifier_enable = false;
324 nic->pki.hash_enable = true;
325 nic->pki.initialized = false;
331 octeontx_dev_promisc_enable(struct rte_eth_dev *dev)
333 struct octeontx_nic *nic = octeontx_pmd_priv(dev);
335 PMD_INIT_FUNC_TRACE();
336 octeontx_port_promisc_set(nic, 1);
340 octeontx_dev_promisc_disable(struct rte_eth_dev *dev)
342 struct octeontx_nic *nic = octeontx_pmd_priv(dev);
344 PMD_INIT_FUNC_TRACE();
345 octeontx_port_promisc_set(nic, 0);
349 octeontx_atomic_write_link_status(struct rte_eth_dev *dev,
350 struct rte_eth_link *link)
352 struct rte_eth_link *dst = &dev->data->dev_link;
353 struct rte_eth_link *src = link;
355 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
356 *(uint64_t *)src) == 0)
363 octeontx_port_link_status(struct octeontx_nic *nic)
367 PMD_INIT_FUNC_TRACE();
368 res = octeontx_bgx_port_link_status(nic->port_id);
370 octeontx_log_err("failed to get port %d link status",
375 nic->link_up = (uint8_t)res;
376 octeontx_log_dbg("port %d link status %d", nic->port_id, nic->link_up);
382 * Return 0 means link status changed, -1 means not changed
385 octeontx_dev_link_update(struct rte_eth_dev *dev,
386 int wait_to_complete __rte_unused)
388 struct octeontx_nic *nic = octeontx_pmd_priv(dev);
389 struct rte_eth_link link;
393 PMD_INIT_FUNC_TRACE();
395 res = octeontx_port_link_status(nic);
397 octeontx_log_err("failed to request link status %d", res);
401 link.link_status = nic->link_up;
403 switch (nic->speed) {
404 case OCTEONTX_LINK_SPEED_SGMII:
405 link.link_speed = ETH_SPEED_NUM_1G;
408 case OCTEONTX_LINK_SPEED_XAUI:
409 link.link_speed = ETH_SPEED_NUM_10G;
412 case OCTEONTX_LINK_SPEED_RXAUI:
413 case OCTEONTX_LINK_SPEED_10G_R:
414 link.link_speed = ETH_SPEED_NUM_10G;
416 case OCTEONTX_LINK_SPEED_QSGMII:
417 link.link_speed = ETH_SPEED_NUM_5G;
419 case OCTEONTX_LINK_SPEED_40G_R:
420 link.link_speed = ETH_SPEED_NUM_40G;
423 case OCTEONTX_LINK_SPEED_RESERVE1:
424 case OCTEONTX_LINK_SPEED_RESERVE2:
426 octeontx_log_err("incorrect link speed %d", nic->speed);
430 link.link_duplex = ETH_LINK_AUTONEG;
431 link.link_autoneg = ETH_LINK_SPEED_AUTONEG;
433 return octeontx_atomic_write_link_status(dev, &link);
437 octeontx_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
439 struct octeontx_nic *nic = octeontx_pmd_priv(dev);
441 PMD_INIT_FUNC_TRACE();
442 octeontx_port_stats(nic, stats);
446 octeontx_dev_stats_reset(struct rte_eth_dev *dev)
448 struct octeontx_nic *nic = octeontx_pmd_priv(dev);
450 PMD_INIT_FUNC_TRACE();
451 octeontx_port_stats_clr(nic);
455 octeontx_dev_default_mac_addr_set(struct rte_eth_dev *dev,
456 struct ether_addr *addr)
458 struct octeontx_nic *nic = octeontx_pmd_priv(dev);
461 ret = octeontx_bgx_port_mac_set(nic->port_id, addr->addr_bytes);
463 octeontx_log_err("failed to set MAC address on port %d",
468 octeontx_dev_info(struct rte_eth_dev *dev,
469 struct rte_eth_dev_info *dev_info)
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 |
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;
487 dev_info->default_rxconf = (struct rte_eth_rxconf) {
492 dev_info->default_txconf = (struct rte_eth_txconf) {
495 ETH_TXQ_FLAGS_NOMULTSEGS |
496 ETH_TXQ_FLAGS_NOOFFLOADS |
497 ETH_TXQ_FLAGS_NOXSUMS,
500 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_MT_LOCKFREE;
503 /* Initialize and register driver with DPDK Application */
504 static const struct eth_dev_ops octeontx_dev_ops = {
505 .dev_configure = octeontx_dev_configure,
506 .dev_infos_get = octeontx_dev_info,
507 .promiscuous_enable = octeontx_dev_promisc_enable,
508 .promiscuous_disable = octeontx_dev_promisc_disable,
509 .link_update = octeontx_dev_link_update,
510 .stats_get = octeontx_dev_stats_get,
511 .stats_reset = octeontx_dev_stats_reset,
512 .mac_addr_set = octeontx_dev_default_mac_addr_set,
515 /* Create Ethdev interface per BGX LMAC ports */
517 octeontx_create(struct rte_vdev_device *dev, int port, uint8_t evdev,
521 char octtx_name[OCTEONTX_MAX_NAME_LEN];
522 struct octeontx_nic *nic = NULL;
523 struct rte_eth_dev *eth_dev = NULL;
524 struct rte_eth_dev_data *data = NULL;
525 const char *name = rte_vdev_device_name(dev);
527 PMD_INIT_FUNC_TRACE();
529 sprintf(octtx_name, "%s_%d", name, port);
530 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
531 eth_dev = rte_eth_dev_attach_secondary(octtx_name);
538 data = rte_zmalloc_socket(octtx_name, sizeof(*data), 0, socket_id);
540 octeontx_log_err("failed to allocate devdata");
545 nic = rte_zmalloc_socket(octtx_name, sizeof(*nic), 0, socket_id);
547 octeontx_log_err("failed to allocate nic structure");
555 res = octeontx_port_open(nic);
559 /* Rx side port configuration */
560 res = octeontx_pki_port_open(port);
562 octeontx_log_err("failed to open PKI port %d", port);
567 /* Reserve an ethdev entry */
568 eth_dev = rte_eth_dev_allocate(octtx_name);
569 if (eth_dev == NULL) {
570 octeontx_log_err("failed to allocate rte_eth_dev");
575 eth_dev->device = &dev->device;
576 eth_dev->intr_handle = NULL;
577 eth_dev->data->kdrv = RTE_KDRV_NONE;
578 eth_dev->data->numa_node = dev->device.numa_node;
580 rte_memcpy(data, (eth_dev)->data, sizeof(*data));
581 data->dev_private = nic;
583 data->port_id = eth_dev->data->port_id;
584 snprintf(data->name, sizeof(data->name), "%s", eth_dev->data->name);
589 data->dev_link.link_status = ETH_LINK_DOWN;
590 data->dev_started = 0;
591 data->promiscuous = 0;
592 data->all_multicast = 0;
593 data->scattered_rx = 0;
595 data->mac_addrs = rte_zmalloc_socket(octtx_name, ETHER_ADDR_LEN, 0,
597 if (data->mac_addrs == NULL) {
598 octeontx_log_err("failed to allocate memory for mac_addrs");
603 eth_dev->data = data;
604 eth_dev->dev_ops = &octeontx_dev_ops;
606 /* Finally save ethdev pointer to the NIC structure */
609 if (nic->port_id != data->port_id) {
610 octeontx_log_err("eth_dev->port_id (%d) is diff to orig (%d)",
611 data->port_id, nic->port_id);
616 /* Update port_id mac to eth_dev */
617 memcpy(data->mac_addrs, nic->mac_addr, ETHER_ADDR_LEN);
619 PMD_INIT_LOG(DEBUG, "ethdev info: ");
620 PMD_INIT_LOG(DEBUG, "port %d, port_ena %d ochan %d num_ochan %d tx_q %d",
621 nic->port_id, nic->port_ena,
622 nic->base_ochan, nic->num_ochans,
624 PMD_INIT_LOG(DEBUG, "speed %d mtu %d", nic->speed, nic->mtu);
626 return data->port_id;
630 octeontx_port_close(nic);
632 if (eth_dev != NULL) {
633 rte_free(eth_dev->data->mac_addrs);
636 rte_eth_dev_release_port(eth_dev);
642 /* Un initialize octeontx device */
644 octeontx_remove(struct rte_vdev_device *dev)
646 char octtx_name[OCTEONTX_MAX_NAME_LEN];
647 struct rte_eth_dev *eth_dev = NULL;
648 struct octeontx_nic *nic = NULL;
654 for (i = 0; i < OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT; i++) {
655 sprintf(octtx_name, "eth_octeontx_%d", i);
657 /* reserve an ethdev entry */
658 eth_dev = rte_eth_dev_allocated(octtx_name);
662 nic = octeontx_pmd_priv(eth_dev);
663 rte_event_dev_stop(nic->evdev);
664 PMD_INIT_LOG(INFO, "Closing octeontx device %s", octtx_name);
666 rte_free(eth_dev->data->mac_addrs);
667 rte_free(eth_dev->data->dev_private);
668 rte_free(eth_dev->data);
669 rte_eth_dev_release_port(eth_dev);
670 rte_event_dev_close(nic->evdev);
673 /* Free FC resource */
674 octeontx_pko_fc_free();
679 /* Initialize octeontx device */
681 octeontx_probe(struct rte_vdev_device *dev)
683 const char *dev_name;
684 static int probe_once;
685 uint8_t socket_id, qlist;
686 int tx_vfcnt, port_id, evdev, qnum, pnum, res, i;
687 struct rte_event_dev_config dev_conf;
688 const char *eventdev_name = "event_octeontx";
689 struct rte_event_dev_info info;
691 struct octeontx_vdev_init_params init_params = {
692 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT
695 dev_name = rte_vdev_device_name(dev);
696 res = octeontx_parse_vdev_init_params(&init_params, dev);
700 if (init_params.nr_port > OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT) {
701 octeontx_log_err("nr_port (%d) > max (%d)", init_params.nr_port,
702 OCTEONTX_VDEV_DEFAULT_MAX_NR_PORT);
706 PMD_INIT_LOG(DEBUG, "initializing %s pmd", dev_name);
708 socket_id = rte_socket_id();
710 tx_vfcnt = octeontx_pko_vf_count();
712 if (tx_vfcnt < init_params.nr_port) {
713 octeontx_log_err("not enough PKO (%d) for port number (%d)",
714 tx_vfcnt, init_params.nr_port);
717 evdev = rte_event_dev_get_dev_id(eventdev_name);
719 octeontx_log_err("eventdev %s not found", eventdev_name);
723 res = rte_event_dev_info_get(evdev, &info);
725 octeontx_log_err("failed to eventdev info %d", res);
729 PMD_INIT_LOG(DEBUG, "max_queue %d max_port %d",
730 info.max_event_queues, info.max_event_ports);
732 if (octeontx_pko_init_fc(tx_vfcnt))
735 devconf_set_default_sane_values(&dev_conf, &info);
736 res = rte_event_dev_configure(evdev, &dev_conf);
740 rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
742 rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
745 octeontx_log_err("too few event ports (%d) for event_q(%d)",
752 * We don't poll on event ports
753 * that do not have any queues assigned.
757 "reducing number of active event ports to %d", pnum);
759 for (i = 0; i < qnum; i++) {
760 res = rte_event_queue_setup(evdev, i, NULL);
762 octeontx_log_err("failed to setup event_q(%d): res %d",
768 for (i = 0; i < pnum; i++) {
769 res = rte_event_port_setup(evdev, i, NULL);
772 octeontx_log_err("failed to setup ev port(%d) res=%d",
776 /* Link one queue to one event port */
778 res = rte_event_port_link(evdev, i, &qlist, NULL, 1);
781 octeontx_log_err("failed to link port (%d): res=%d",
787 /* Create ethdev interface */
788 for (i = 0; i < init_params.nr_port; i++) {
789 port_id = octeontx_create(dev, i, evdev, socket_id);
791 octeontx_log_err("failed to create device %s",
797 PMD_INIT_LOG(INFO, "created ethdev %s for port %d", dev_name,
802 octeontx_log_err("interface %s not supported", dev_name);
803 octeontx_remove(dev);
812 octeontx_pko_fc_free();
816 static struct rte_vdev_driver octeontx_pmd_drv = {
817 .probe = octeontx_probe,
818 .remove = octeontx_remove,
821 RTE_PMD_REGISTER_VDEV(OCTEONTX_PMD, octeontx_pmd_drv);
822 RTE_PMD_REGISTER_ALIAS(OCTEONTX_PMD, eth_octeontx);
823 RTE_PMD_REGISTER_PARAM_STRING(OCTEONTX_PMD, "nr_port=<int> ");