1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2 * Copyright(c) 2018-2019 Pensando Systems, Inc. All rights reserved.
6 #include <rte_bus_pci.h>
7 #include <rte_ethdev.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_malloc.h>
10 #include <rte_ethdev_pci.h>
12 #include "ionic_logs.h"
14 #include "ionic_dev.h"
15 #include "ionic_mac_api.h"
16 #include "ionic_lif.h"
17 #include "ionic_ethdev.h"
19 static int eth_ionic_dev_init(struct rte_eth_dev *eth_dev, void *init_params);
20 static int eth_ionic_dev_uninit(struct rte_eth_dev *eth_dev);
21 static int ionic_dev_info_get(struct rte_eth_dev *eth_dev,
22 struct rte_eth_dev_info *dev_info);
23 static int ionic_dev_configure(struct rte_eth_dev *dev);
24 static int ionic_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
25 static int ionic_dev_start(struct rte_eth_dev *dev);
26 static void ionic_dev_stop(struct rte_eth_dev *dev);
27 static void ionic_dev_close(struct rte_eth_dev *dev);
28 static int ionic_dev_set_link_up(struct rte_eth_dev *dev);
29 static int ionic_dev_set_link_down(struct rte_eth_dev *dev);
30 static int ionic_dev_link_update(struct rte_eth_dev *eth_dev,
31 int wait_to_complete);
32 static int ionic_flow_ctrl_get(struct rte_eth_dev *eth_dev,
33 struct rte_eth_fc_conf *fc_conf);
34 static int ionic_flow_ctrl_set(struct rte_eth_dev *eth_dev,
35 struct rte_eth_fc_conf *fc_conf);
39 static const struct rte_pci_id pci_id_ionic_map[] = {
40 { RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_PF) },
41 { RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_VF) },
42 { RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_MGMT) },
43 { .vendor_id = 0, /* sentinel */ },
46 static const struct eth_dev_ops ionic_eth_dev_ops = {
47 .dev_infos_get = ionic_dev_info_get,
48 .dev_configure = ionic_dev_configure,
49 .mtu_set = ionic_dev_mtu_set,
50 .dev_start = ionic_dev_start,
51 .dev_stop = ionic_dev_stop,
52 .dev_close = ionic_dev_close,
53 .link_update = ionic_dev_link_update,
54 .dev_set_link_up = ionic_dev_set_link_up,
55 .dev_set_link_down = ionic_dev_set_link_down,
56 .mac_addr_add = ionic_dev_add_mac,
57 .mac_addr_remove = ionic_dev_remove_mac,
58 .mac_addr_set = ionic_dev_set_mac,
59 .vlan_filter_set = ionic_dev_vlan_filter_set,
60 .promiscuous_enable = ionic_dev_promiscuous_enable,
61 .promiscuous_disable = ionic_dev_promiscuous_disable,
62 .allmulticast_enable = ionic_dev_allmulticast_enable,
63 .allmulticast_disable = ionic_dev_allmulticast_disable,
64 .flow_ctrl_get = ionic_flow_ctrl_get,
65 .flow_ctrl_set = ionic_flow_ctrl_set,
69 * Set device link up, enable tx.
72 ionic_dev_set_link_up(struct rte_eth_dev *eth_dev)
74 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
75 struct ionic_adapter *adapter = lif->adapter;
76 struct ionic_dev *idev = &adapter->idev;
81 ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_UP);
83 err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
85 IONIC_PRINT(WARNING, "Failed to bring port UP");
93 * Set device link down, disable tx.
96 ionic_dev_set_link_down(struct rte_eth_dev *eth_dev)
98 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
99 struct ionic_adapter *adapter = lif->adapter;
100 struct ionic_dev *idev = &adapter->idev;
105 ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_DOWN);
107 err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
109 IONIC_PRINT(WARNING, "Failed to bring port DOWN");
117 ionic_dev_link_update(struct rte_eth_dev *eth_dev,
118 int wait_to_complete __rte_unused)
120 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
121 struct ionic_adapter *adapter = lif->adapter;
122 struct rte_eth_link link;
127 memset(&link, 0, sizeof(link));
128 link.link_autoneg = ETH_LINK_AUTONEG;
130 if (!adapter->link_up) {
131 /* Interface is down */
132 link.link_status = ETH_LINK_DOWN;
133 link.link_duplex = ETH_LINK_HALF_DUPLEX;
134 link.link_speed = ETH_SPEED_NUM_NONE;
136 /* Interface is up */
137 link.link_status = ETH_LINK_UP;
138 link.link_duplex = ETH_LINK_FULL_DUPLEX;
139 switch (adapter->link_speed) {
141 link.link_speed = ETH_SPEED_NUM_10G;
144 link.link_speed = ETH_SPEED_NUM_25G;
147 link.link_speed = ETH_SPEED_NUM_40G;
150 link.link_speed = ETH_SPEED_NUM_50G;
153 link.link_speed = ETH_SPEED_NUM_100G;
156 link.link_speed = ETH_SPEED_NUM_NONE;
161 return rte_eth_linkstatus_set(eth_dev, &link);
165 * Interrupt handler triggered by NIC for handling
166 * specific interrupt.
169 * The address of parameter registered before.
175 ionic_dev_interrupt_handler(void *param)
177 struct ionic_adapter *adapter = (struct ionic_adapter *)param;
180 IONIC_PRINT(DEBUG, "->");
182 for (i = 0; i < adapter->nlifs; i++) {
183 if (adapter->lifs[i])
184 ionic_notifyq_handler(adapter->lifs[i], -1);
189 ionic_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
191 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
192 uint32_t max_frame_size;
198 * Note: mtu check against IONIC_MIN_MTU, IONIC_MAX_MTU
199 * is done by the the API.
203 * Max frame size is MTU + Ethernet header + VLAN + QinQ
204 * (plus ETHER_CRC_LEN if the adapter is able to keep CRC)
206 max_frame_size = mtu + RTE_ETHER_HDR_LEN + 4 + 4;
208 if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len < max_frame_size)
211 err = ionic_lif_change_mtu(lif, mtu);
219 ionic_dev_info_get(struct rte_eth_dev *eth_dev,
220 struct rte_eth_dev_info *dev_info)
222 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
223 struct ionic_adapter *adapter = lif->adapter;
224 struct ionic_identity *ident = &adapter->ident;
228 dev_info->max_rx_queues = (uint16_t)
229 ident->lif.eth.config.queue_count[IONIC_QTYPE_RXQ];
230 dev_info->max_tx_queues = (uint16_t)
231 ident->lif.eth.config.queue_count[IONIC_QTYPE_TXQ];
232 /* Also add ETHER_CRC_LEN if the adapter is able to keep CRC */
233 dev_info->min_rx_bufsize = IONIC_MIN_MTU + RTE_ETHER_HDR_LEN;
234 dev_info->max_rx_pktlen = IONIC_MAX_MTU + RTE_ETHER_HDR_LEN;
235 dev_info->max_mac_addrs = adapter->max_mac_addrs;
236 dev_info->min_mtu = IONIC_MIN_MTU;
237 dev_info->max_mtu = IONIC_MAX_MTU;
239 dev_info->speed_capa =
250 ionic_flow_ctrl_get(struct rte_eth_dev *eth_dev,
251 struct rte_eth_fc_conf *fc_conf)
253 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
254 struct ionic_adapter *adapter = lif->adapter;
255 struct ionic_dev *idev = &adapter->idev;
257 if (idev->port_info) {
258 fc_conf->autoneg = idev->port_info->config.an_enable;
260 if (idev->port_info->config.pause_type)
261 fc_conf->mode = RTE_FC_FULL;
263 fc_conf->mode = RTE_FC_NONE;
270 ionic_flow_ctrl_set(struct rte_eth_dev *eth_dev,
271 struct rte_eth_fc_conf *fc_conf)
273 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
274 struct ionic_adapter *adapter = lif->adapter;
275 struct ionic_dev *idev = &adapter->idev;
276 uint8_t pause_type = IONIC_PORT_PAUSE_TYPE_NONE;
279 switch (fc_conf->mode) {
281 pause_type = IONIC_PORT_PAUSE_TYPE_NONE;
284 pause_type = IONIC_PORT_PAUSE_TYPE_LINK;
286 case RTE_FC_RX_PAUSE:
287 case RTE_FC_TX_PAUSE:
291 an_enable = fc_conf->autoneg;
293 ionic_dev_cmd_port_pause(idev, pause_type);
294 ionic_dev_cmd_port_autoneg(idev, an_enable);
300 ionic_dev_configure(struct rte_eth_dev *eth_dev)
302 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
307 err = ionic_lif_configure(lif);
309 IONIC_PRINT(ERR, "Cannot configure LIF: %d", err);
316 static inline uint32_t
317 ionic_parse_link_speeds(uint16_t link_speeds)
319 if (link_speeds & ETH_LINK_SPEED_100G)
321 else if (link_speeds & ETH_LINK_SPEED_50G)
323 else if (link_speeds & ETH_LINK_SPEED_40G)
325 else if (link_speeds & ETH_LINK_SPEED_25G)
327 else if (link_speeds & ETH_LINK_SPEED_10G)
334 * Configure device link speed and setup link.
335 * It returns 0 on success.
338 ionic_dev_start(struct rte_eth_dev *eth_dev)
340 struct rte_eth_conf *dev_conf = ð_dev->data->dev_conf;
341 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
342 struct ionic_adapter *adapter = lif->adapter;
343 struct ionic_dev *idev = &adapter->idev;
344 uint32_t allowed_speeds;
350 ETH_LINK_SPEED_FIXED |
357 if (dev_conf->link_speeds & ~allowed_speeds) {
358 IONIC_PRINT(ERR, "Invalid link setting");
362 err = ionic_lif_start(lif);
364 IONIC_PRINT(ERR, "Cannot start LIF: %d", err);
368 if (eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
369 uint32_t speed = ionic_parse_link_speeds(dev_conf->link_speeds);
372 ionic_dev_cmd_port_speed(idev, speed);
375 ionic_dev_link_update(eth_dev, 0);
381 * Stop device: disable rx and tx functions to allow for reconfiguring.
384 ionic_dev_stop(struct rte_eth_dev *eth_dev)
386 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
391 err = ionic_lif_stop(lif);
393 IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
397 * Reset and stop device.
400 ionic_dev_close(struct rte_eth_dev *eth_dev)
402 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
407 err = ionic_lif_stop(lif);
409 IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
413 err = eth_ionic_dev_uninit(eth_dev);
415 IONIC_PRINT(ERR, "Cannot destroy LIF: %d", err);
421 eth_ionic_dev_init(struct rte_eth_dev *eth_dev, void *init_params)
423 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
424 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
425 struct ionic_adapter *adapter = (struct ionic_adapter *)init_params;
430 eth_dev->dev_ops = &ionic_eth_dev_ops;
432 /* Multi-process not supported, primary does initialization anyway */
433 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
436 rte_eth_copy_pci_info(eth_dev, pci_dev);
438 lif->index = adapter->nlifs;
439 lif->eth_dev = eth_dev;
440 lif->adapter = adapter;
441 adapter->lifs[adapter->nlifs] = lif;
443 IONIC_PRINT(DEBUG, "Up to %u MAC addresses supported",
444 adapter->max_mac_addrs);
446 /* Allocate memory for storing MAC addresses */
447 eth_dev->data->mac_addrs = rte_zmalloc("ionic",
448 RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs, 0);
450 if (eth_dev->data->mac_addrs == NULL) {
451 IONIC_PRINT(ERR, "Failed to allocate %u bytes needed to "
452 "store MAC addresses",
453 RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs);
458 err = ionic_lif_alloc(lif);
460 IONIC_PRINT(ERR, "Cannot allocate LIFs: %d, aborting",
465 err = ionic_lif_init(lif);
467 IONIC_PRINT(ERR, "Cannot init LIFs: %d, aborting", err);
471 /* Copy the MAC address */
472 rte_ether_addr_copy((struct rte_ether_addr *)lif->mac_addr,
473 ð_dev->data->mac_addrs[0]);
475 IONIC_PRINT(DEBUG, "Port %u initialized", eth_dev->data->port_id);
486 eth_ionic_dev_uninit(struct rte_eth_dev *eth_dev)
488 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
489 struct ionic_adapter *adapter = lif->adapter;
493 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
496 adapter->lifs[lif->index] = NULL;
498 ionic_lif_deinit(lif);
501 eth_dev->dev_ops = NULL;
507 ionic_configure_intr(struct ionic_adapter *adapter)
509 struct rte_pci_device *pci_dev = adapter->pci_dev;
510 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
513 IONIC_PRINT(DEBUG, "Configuring %u intrs", adapter->nintrs);
515 if (rte_intr_efd_enable(intr_handle, adapter->nintrs)) {
516 IONIC_PRINT(ERR, "Fail to create eventfd");
520 if (rte_intr_dp_is_en(intr_handle))
522 "Packet I/O interrupt on datapath is enabled");
524 if (!intr_handle->intr_vec) {
525 intr_handle->intr_vec = rte_zmalloc("intr_vec",
526 adapter->nintrs * sizeof(int), 0);
528 if (!intr_handle->intr_vec) {
529 IONIC_PRINT(ERR, "Failed to allocate %u vectors",
535 err = rte_intr_callback_register(intr_handle,
536 ionic_dev_interrupt_handler,
541 "Failure registering interrupts handler (%d)",
546 /* enable intr mapping */
547 err = rte_intr_enable(intr_handle);
550 IONIC_PRINT(ERR, "Failure enabling interrupts (%d)", err);
558 ionic_unconfigure_intr(struct ionic_adapter *adapter)
560 struct rte_pci_device *pci_dev = adapter->pci_dev;
561 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
563 rte_intr_disable(intr_handle);
565 rte_intr_callback_unregister(intr_handle,
566 ionic_dev_interrupt_handler,
571 eth_ionic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
572 struct rte_pci_device *pci_dev)
574 char name[RTE_ETH_NAME_MAX_LEN];
575 struct rte_mem_resource *resource;
576 struct ionic_adapter *adapter;
581 /* Check structs (trigger error at compilation time) */
582 ionic_struct_size_checks();
584 /* Multi-process not supported */
585 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
590 IONIC_PRINT(DEBUG, "Initializing device %s",
591 pci_dev->device.name);
593 adapter = rte_zmalloc("ionic", sizeof(*adapter), 0);
595 IONIC_PRINT(ERR, "OOM");
600 adapter->pci_dev = pci_dev;
603 hw->device_id = pci_dev->id.device_id;
604 hw->vendor_id = pci_dev->id.vendor_id;
606 err = ionic_init_mac(hw);
608 IONIC_PRINT(ERR, "Mac init failed: %d", err);
610 goto err_free_adapter;
613 adapter->is_mgmt_nic = (pci_dev->id.device_id == IONIC_DEV_ID_ETH_MGMT);
615 adapter->num_bars = 0;
616 for (i = 0; i < PCI_MAX_RESOURCE && i < IONIC_BARS_MAX; i++) {
617 resource = &pci_dev->mem_resource[i];
618 if (resource->phys_addr == 0 || resource->len == 0)
620 adapter->bars[adapter->num_bars].vaddr = resource->addr;
621 adapter->bars[adapter->num_bars].bus_addr = resource->phys_addr;
622 adapter->bars[adapter->num_bars].len = resource->len;
626 /* Discover ionic dev resources */
628 err = ionic_setup(adapter);
630 IONIC_PRINT(ERR, "Cannot setup device: %d, aborting", err);
631 goto err_free_adapter;
634 err = ionic_identify(adapter);
636 IONIC_PRINT(ERR, "Cannot identify device: %d, aborting",
638 goto err_free_adapter;
641 err = ionic_init(adapter);
643 IONIC_PRINT(ERR, "Cannot init device: %d, aborting", err);
644 goto err_free_adapter;
647 /* Configure the ports */
648 err = ionic_port_identify(adapter);
650 IONIC_PRINT(ERR, "Cannot identify port: %d, aborting",
652 goto err_free_adapter;
655 err = ionic_port_init(adapter);
657 IONIC_PRINT(ERR, "Cannot init port: %d, aborting", err);
658 goto err_free_adapter;
662 err = ionic_lif_identify(adapter);
664 IONIC_PRINT(ERR, "Cannot identify lif: %d, aborting", err);
665 goto err_free_adapter;
668 /* Allocate and init LIFs */
669 err = ionic_lifs_size(adapter);
671 IONIC_PRINT(ERR, "Cannot size LIFs: %d, aborting", err);
672 goto err_free_adapter;
675 adapter->max_mac_addrs = adapter->ident.lif.eth.max_ucast_filters;
678 for (i = 0; i < adapter->ident.dev.nlifs; i++) {
679 snprintf(name, sizeof(name), "net_%s_lif_%lu",
680 pci_dev->device.name, i);
682 err = rte_eth_dev_create(&pci_dev->device, name,
683 sizeof(struct ionic_lif),
685 eth_ionic_dev_init, adapter);
687 IONIC_PRINT(ERR, "Cannot create eth device for "
688 "ionic lif %s", name);
695 err = ionic_configure_intr(adapter);
698 IONIC_PRINT(ERR, "Failed to configure interrupts");
699 goto err_free_adapter;
711 eth_ionic_pci_remove(struct rte_pci_device *pci_dev __rte_unused)
713 char name[RTE_ETH_NAME_MAX_LEN];
714 struct ionic_adapter *adapter = NULL;
715 struct rte_eth_dev *eth_dev;
716 struct ionic_lif *lif;
719 /* Adapter lookup is using (the first) eth_dev name */
720 snprintf(name, sizeof(name), "net_%s_lif_0",
721 pci_dev->device.name);
723 eth_dev = rte_eth_dev_allocated(name);
725 lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
726 adapter = lif->adapter;
730 ionic_unconfigure_intr(adapter);
732 for (i = 0; i < adapter->nlifs; i++) {
733 lif = adapter->lifs[i];
734 rte_eth_dev_destroy(lif->eth_dev, eth_ionic_dev_uninit);
743 static struct rte_pci_driver rte_ionic_pmd = {
744 .id_table = pci_id_ionic_map,
745 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
746 .probe = eth_ionic_pci_probe,
747 .remove = eth_ionic_pci_remove,
750 RTE_PMD_REGISTER_PCI(net_ionic, rte_ionic_pmd);
751 RTE_PMD_REGISTER_PCI_TABLE(net_ionic, pci_id_ionic_map);
752 RTE_PMD_REGISTER_KMOD_DEP(net_ionic, "* igb_uio | uio_pci_generic | vfio-pci");
754 RTE_INIT(ionic_init_log)
756 ionic_logtype = rte_log_register("pmd.net.ionic");
757 if (ionic_logtype >= 0)
758 rte_log_set_level(ionic_logtype, RTE_LOG_NOTICE);