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);
35 static const struct rte_pci_id pci_id_ionic_map[] = {
36 { RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_PF) },
37 { RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_VF) },
38 { RTE_PCI_DEVICE(IONIC_PENSANDO_VENDOR_ID, IONIC_DEV_ID_ETH_MGMT) },
39 { .vendor_id = 0, /* sentinel */ },
42 static const struct eth_dev_ops ionic_eth_dev_ops = {
43 .dev_infos_get = ionic_dev_info_get,
44 .dev_configure = ionic_dev_configure,
45 .mtu_set = ionic_dev_mtu_set,
46 .dev_start = ionic_dev_start,
47 .dev_stop = ionic_dev_stop,
48 .dev_close = ionic_dev_close,
49 .link_update = ionic_dev_link_update,
50 .dev_set_link_up = ionic_dev_set_link_up,
51 .dev_set_link_down = ionic_dev_set_link_down,
55 * Set device link up, enable tx.
58 ionic_dev_set_link_up(struct rte_eth_dev *eth_dev)
60 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
61 struct ionic_adapter *adapter = lif->adapter;
62 struct ionic_dev *idev = &adapter->idev;
67 ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_UP);
69 err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
71 IONIC_PRINT(WARNING, "Failed to bring port UP");
79 * Set device link down, disable tx.
82 ionic_dev_set_link_down(struct rte_eth_dev *eth_dev)
84 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
85 struct ionic_adapter *adapter = lif->adapter;
86 struct ionic_dev *idev = &adapter->idev;
91 ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_DOWN);
93 err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
95 IONIC_PRINT(WARNING, "Failed to bring port DOWN");
103 ionic_dev_link_update(struct rte_eth_dev *eth_dev,
104 int wait_to_complete __rte_unused)
106 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
107 struct ionic_adapter *adapter = lif->adapter;
108 struct rte_eth_link link;
113 memset(&link, 0, sizeof(link));
114 link.link_autoneg = ETH_LINK_AUTONEG;
116 if (!adapter->link_up) {
117 /* Interface is down */
118 link.link_status = ETH_LINK_DOWN;
119 link.link_duplex = ETH_LINK_HALF_DUPLEX;
120 link.link_speed = ETH_SPEED_NUM_NONE;
122 /* Interface is up */
123 link.link_status = ETH_LINK_UP;
124 link.link_duplex = ETH_LINK_FULL_DUPLEX;
125 switch (adapter->link_speed) {
127 link.link_speed = ETH_SPEED_NUM_10G;
130 link.link_speed = ETH_SPEED_NUM_25G;
133 link.link_speed = ETH_SPEED_NUM_40G;
136 link.link_speed = ETH_SPEED_NUM_50G;
139 link.link_speed = ETH_SPEED_NUM_100G;
142 link.link_speed = ETH_SPEED_NUM_NONE;
147 return rte_eth_linkstatus_set(eth_dev, &link);
151 * Interrupt handler triggered by NIC for handling
152 * specific interrupt.
155 * The address of parameter registered before.
161 ionic_dev_interrupt_handler(void *param)
163 struct ionic_adapter *adapter = (struct ionic_adapter *)param;
166 IONIC_PRINT(DEBUG, "->");
168 for (i = 0; i < adapter->nlifs; i++) {
169 if (adapter->lifs[i])
170 ionic_notifyq_handler(adapter->lifs[i], -1);
175 ionic_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
177 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
178 uint32_t max_frame_size;
184 * Note: mtu check against IONIC_MIN_MTU, IONIC_MAX_MTU
185 * is done by the the API.
189 * Max frame size is MTU + Ethernet header + VLAN + QinQ
190 * (plus ETHER_CRC_LEN if the adapter is able to keep CRC)
192 max_frame_size = mtu + RTE_ETHER_HDR_LEN + 4 + 4;
194 if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len < max_frame_size)
197 err = ionic_lif_change_mtu(lif, mtu);
205 ionic_dev_info_get(struct rte_eth_dev *eth_dev,
206 struct rte_eth_dev_info *dev_info)
208 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
209 struct ionic_adapter *adapter = lif->adapter;
210 struct ionic_identity *ident = &adapter->ident;
214 dev_info->max_rx_queues = (uint16_t)
215 ident->lif.eth.config.queue_count[IONIC_QTYPE_RXQ];
216 dev_info->max_tx_queues = (uint16_t)
217 ident->lif.eth.config.queue_count[IONIC_QTYPE_TXQ];
218 /* Also add ETHER_CRC_LEN if the adapter is able to keep CRC */
219 dev_info->min_rx_bufsize = IONIC_MIN_MTU + RTE_ETHER_HDR_LEN;
220 dev_info->max_rx_pktlen = IONIC_MAX_MTU + RTE_ETHER_HDR_LEN;
221 dev_info->max_mac_addrs = adapter->max_mac_addrs;
222 dev_info->min_mtu = IONIC_MIN_MTU;
223 dev_info->max_mtu = IONIC_MAX_MTU;
225 dev_info->speed_capa =
236 ionic_dev_configure(struct rte_eth_dev *eth_dev)
238 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
243 err = ionic_lif_configure(lif);
245 IONIC_PRINT(ERR, "Cannot configure LIF: %d", err);
252 static inline uint32_t
253 ionic_parse_link_speeds(uint16_t link_speeds)
255 if (link_speeds & ETH_LINK_SPEED_100G)
257 else if (link_speeds & ETH_LINK_SPEED_50G)
259 else if (link_speeds & ETH_LINK_SPEED_40G)
261 else if (link_speeds & ETH_LINK_SPEED_25G)
263 else if (link_speeds & ETH_LINK_SPEED_10G)
270 * Configure device link speed and setup link.
271 * It returns 0 on success.
274 ionic_dev_start(struct rte_eth_dev *eth_dev)
276 struct rte_eth_conf *dev_conf = ð_dev->data->dev_conf;
277 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
278 struct ionic_adapter *adapter = lif->adapter;
279 struct ionic_dev *idev = &adapter->idev;
280 uint32_t allowed_speeds;
286 ETH_LINK_SPEED_FIXED |
293 if (dev_conf->link_speeds & ~allowed_speeds) {
294 IONIC_PRINT(ERR, "Invalid link setting");
298 err = ionic_lif_start(lif);
300 IONIC_PRINT(ERR, "Cannot start LIF: %d", err);
304 if (eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
305 uint32_t speed = ionic_parse_link_speeds(dev_conf->link_speeds);
308 ionic_dev_cmd_port_speed(idev, speed);
311 ionic_dev_link_update(eth_dev, 0);
317 * Stop device: disable rx and tx functions to allow for reconfiguring.
320 ionic_dev_stop(struct rte_eth_dev *eth_dev)
322 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
327 err = ionic_lif_stop(lif);
329 IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
333 * Reset and stop device.
336 ionic_dev_close(struct rte_eth_dev *eth_dev)
338 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
343 err = ionic_lif_stop(lif);
345 IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
349 err = eth_ionic_dev_uninit(eth_dev);
351 IONIC_PRINT(ERR, "Cannot destroy LIF: %d", err);
357 eth_ionic_dev_init(struct rte_eth_dev *eth_dev, void *init_params)
359 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
360 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
361 struct ionic_adapter *adapter = (struct ionic_adapter *)init_params;
366 eth_dev->dev_ops = &ionic_eth_dev_ops;
368 /* Multi-process not supported, primary does initialization anyway */
369 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
372 rte_eth_copy_pci_info(eth_dev, pci_dev);
374 lif->index = adapter->nlifs;
375 lif->eth_dev = eth_dev;
376 lif->adapter = adapter;
377 adapter->lifs[adapter->nlifs] = lif;
379 IONIC_PRINT(DEBUG, "Up to %u MAC addresses supported",
380 adapter->max_mac_addrs);
382 /* Allocate memory for storing MAC addresses */
383 eth_dev->data->mac_addrs = rte_zmalloc("ionic",
384 RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs, 0);
386 if (eth_dev->data->mac_addrs == NULL) {
387 IONIC_PRINT(ERR, "Failed to allocate %u bytes needed to "
388 "store MAC addresses",
389 RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs);
394 err = ionic_lif_alloc(lif);
396 IONIC_PRINT(ERR, "Cannot allocate LIFs: %d, aborting",
401 err = ionic_lif_init(lif);
403 IONIC_PRINT(ERR, "Cannot init LIFs: %d, aborting", err);
407 /* Copy the MAC address */
408 rte_ether_addr_copy((struct rte_ether_addr *)lif->mac_addr,
409 ð_dev->data->mac_addrs[0]);
411 IONIC_PRINT(DEBUG, "Port %u initialized", eth_dev->data->port_id);
422 eth_ionic_dev_uninit(struct rte_eth_dev *eth_dev)
424 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
425 struct ionic_adapter *adapter = lif->adapter;
429 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
432 adapter->lifs[lif->index] = NULL;
434 ionic_lif_deinit(lif);
437 eth_dev->dev_ops = NULL;
443 ionic_configure_intr(struct ionic_adapter *adapter)
445 struct rte_pci_device *pci_dev = adapter->pci_dev;
446 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
449 IONIC_PRINT(DEBUG, "Configuring %u intrs", adapter->nintrs);
451 if (rte_intr_efd_enable(intr_handle, adapter->nintrs)) {
452 IONIC_PRINT(ERR, "Fail to create eventfd");
456 if (rte_intr_dp_is_en(intr_handle))
458 "Packet I/O interrupt on datapath is enabled");
460 if (!intr_handle->intr_vec) {
461 intr_handle->intr_vec = rte_zmalloc("intr_vec",
462 adapter->nintrs * sizeof(int), 0);
464 if (!intr_handle->intr_vec) {
465 IONIC_PRINT(ERR, "Failed to allocate %u vectors",
471 err = rte_intr_callback_register(intr_handle,
472 ionic_dev_interrupt_handler,
477 "Failure registering interrupts handler (%d)",
482 /* enable intr mapping */
483 err = rte_intr_enable(intr_handle);
486 IONIC_PRINT(ERR, "Failure enabling interrupts (%d)", err);
494 ionic_unconfigure_intr(struct ionic_adapter *adapter)
496 struct rte_pci_device *pci_dev = adapter->pci_dev;
497 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
499 rte_intr_disable(intr_handle);
501 rte_intr_callback_unregister(intr_handle,
502 ionic_dev_interrupt_handler,
507 eth_ionic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
508 struct rte_pci_device *pci_dev)
510 char name[RTE_ETH_NAME_MAX_LEN];
511 struct rte_mem_resource *resource;
512 struct ionic_adapter *adapter;
517 /* Check structs (trigger error at compilation time) */
518 ionic_struct_size_checks();
520 /* Multi-process not supported */
521 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
526 IONIC_PRINT(DEBUG, "Initializing device %s",
527 pci_dev->device.name);
529 adapter = rte_zmalloc("ionic", sizeof(*adapter), 0);
531 IONIC_PRINT(ERR, "OOM");
536 adapter->pci_dev = pci_dev;
539 hw->device_id = pci_dev->id.device_id;
540 hw->vendor_id = pci_dev->id.vendor_id;
542 err = ionic_init_mac(hw);
544 IONIC_PRINT(ERR, "Mac init failed: %d", err);
546 goto err_free_adapter;
549 adapter->is_mgmt_nic = (pci_dev->id.device_id == IONIC_DEV_ID_ETH_MGMT);
551 adapter->num_bars = 0;
552 for (i = 0; i < PCI_MAX_RESOURCE && i < IONIC_BARS_MAX; i++) {
553 resource = &pci_dev->mem_resource[i];
554 if (resource->phys_addr == 0 || resource->len == 0)
556 adapter->bars[adapter->num_bars].vaddr = resource->addr;
557 adapter->bars[adapter->num_bars].bus_addr = resource->phys_addr;
558 adapter->bars[adapter->num_bars].len = resource->len;
562 /* Discover ionic dev resources */
564 err = ionic_setup(adapter);
566 IONIC_PRINT(ERR, "Cannot setup device: %d, aborting", err);
567 goto err_free_adapter;
570 err = ionic_identify(adapter);
572 IONIC_PRINT(ERR, "Cannot identify device: %d, aborting",
574 goto err_free_adapter;
577 err = ionic_init(adapter);
579 IONIC_PRINT(ERR, "Cannot init device: %d, aborting", err);
580 goto err_free_adapter;
583 /* Configure the ports */
584 err = ionic_port_identify(adapter);
586 IONIC_PRINT(ERR, "Cannot identify port: %d, aborting",
588 goto err_free_adapter;
591 err = ionic_port_init(adapter);
593 IONIC_PRINT(ERR, "Cannot init port: %d, aborting", err);
594 goto err_free_adapter;
598 err = ionic_lif_identify(adapter);
600 IONIC_PRINT(ERR, "Cannot identify lif: %d, aborting", err);
601 goto err_free_adapter;
604 /* Allocate and init LIFs */
605 err = ionic_lifs_size(adapter);
607 IONIC_PRINT(ERR, "Cannot size LIFs: %d, aborting", err);
608 goto err_free_adapter;
611 adapter->max_mac_addrs = adapter->ident.lif.eth.max_ucast_filters;
614 for (i = 0; i < adapter->ident.dev.nlifs; i++) {
615 snprintf(name, sizeof(name), "net_%s_lif_%lu",
616 pci_dev->device.name, i);
618 err = rte_eth_dev_create(&pci_dev->device, name,
619 sizeof(struct ionic_lif),
621 eth_ionic_dev_init, adapter);
623 IONIC_PRINT(ERR, "Cannot create eth device for "
624 "ionic lif %s", name);
631 err = ionic_configure_intr(adapter);
634 IONIC_PRINT(ERR, "Failed to configure interrupts");
635 goto err_free_adapter;
647 eth_ionic_pci_remove(struct rte_pci_device *pci_dev __rte_unused)
649 char name[RTE_ETH_NAME_MAX_LEN];
650 struct ionic_adapter *adapter = NULL;
651 struct rte_eth_dev *eth_dev;
652 struct ionic_lif *lif;
655 /* Adapter lookup is using (the first) eth_dev name */
656 snprintf(name, sizeof(name), "net_%s_lif_0",
657 pci_dev->device.name);
659 eth_dev = rte_eth_dev_allocated(name);
661 lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
662 adapter = lif->adapter;
666 ionic_unconfigure_intr(adapter);
668 for (i = 0; i < adapter->nlifs; i++) {
669 lif = adapter->lifs[i];
670 rte_eth_dev_destroy(lif->eth_dev, eth_ionic_dev_uninit);
679 static struct rte_pci_driver rte_ionic_pmd = {
680 .id_table = pci_id_ionic_map,
681 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
682 .probe = eth_ionic_pci_probe,
683 .remove = eth_ionic_pci_remove,
686 RTE_PMD_REGISTER_PCI(net_ionic, rte_ionic_pmd);
687 RTE_PMD_REGISTER_PCI_TABLE(net_ionic, pci_id_ionic_map);
688 RTE_PMD_REGISTER_KMOD_DEP(net_ionic, "* igb_uio | uio_pci_generic | vfio-pci");
690 RTE_INIT(ionic_init_log)
692 ionic_logtype = rte_log_register("pmd.net.ionic");
693 if (ionic_logtype >= 0)
694 rte_log_set_level(ionic_logtype, RTE_LOG_NOTICE);