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,
52 .mac_addr_add = ionic_dev_add_mac,
53 .mac_addr_remove = ionic_dev_remove_mac,
54 .mac_addr_set = ionic_dev_set_mac,
55 .vlan_filter_set = ionic_dev_vlan_filter_set,
56 .promiscuous_enable = ionic_dev_promiscuous_enable,
57 .promiscuous_disable = ionic_dev_promiscuous_disable,
58 .allmulticast_enable = ionic_dev_allmulticast_enable,
59 .allmulticast_disable = ionic_dev_allmulticast_disable,
63 * Set device link up, enable tx.
66 ionic_dev_set_link_up(struct rte_eth_dev *eth_dev)
68 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
69 struct ionic_adapter *adapter = lif->adapter;
70 struct ionic_dev *idev = &adapter->idev;
75 ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_UP);
77 err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
79 IONIC_PRINT(WARNING, "Failed to bring port UP");
87 * Set device link down, disable tx.
90 ionic_dev_set_link_down(struct rte_eth_dev *eth_dev)
92 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
93 struct ionic_adapter *adapter = lif->adapter;
94 struct ionic_dev *idev = &adapter->idev;
99 ionic_dev_cmd_port_state(idev, IONIC_PORT_ADMIN_STATE_DOWN);
101 err = ionic_dev_cmd_wait_check(idev, IONIC_DEVCMD_TIMEOUT);
103 IONIC_PRINT(WARNING, "Failed to bring port DOWN");
111 ionic_dev_link_update(struct rte_eth_dev *eth_dev,
112 int wait_to_complete __rte_unused)
114 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
115 struct ionic_adapter *adapter = lif->adapter;
116 struct rte_eth_link link;
121 memset(&link, 0, sizeof(link));
122 link.link_autoneg = ETH_LINK_AUTONEG;
124 if (!adapter->link_up) {
125 /* Interface is down */
126 link.link_status = ETH_LINK_DOWN;
127 link.link_duplex = ETH_LINK_HALF_DUPLEX;
128 link.link_speed = ETH_SPEED_NUM_NONE;
130 /* Interface is up */
131 link.link_status = ETH_LINK_UP;
132 link.link_duplex = ETH_LINK_FULL_DUPLEX;
133 switch (adapter->link_speed) {
135 link.link_speed = ETH_SPEED_NUM_10G;
138 link.link_speed = ETH_SPEED_NUM_25G;
141 link.link_speed = ETH_SPEED_NUM_40G;
144 link.link_speed = ETH_SPEED_NUM_50G;
147 link.link_speed = ETH_SPEED_NUM_100G;
150 link.link_speed = ETH_SPEED_NUM_NONE;
155 return rte_eth_linkstatus_set(eth_dev, &link);
159 * Interrupt handler triggered by NIC for handling
160 * specific interrupt.
163 * The address of parameter registered before.
169 ionic_dev_interrupt_handler(void *param)
171 struct ionic_adapter *adapter = (struct ionic_adapter *)param;
174 IONIC_PRINT(DEBUG, "->");
176 for (i = 0; i < adapter->nlifs; i++) {
177 if (adapter->lifs[i])
178 ionic_notifyq_handler(adapter->lifs[i], -1);
183 ionic_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
185 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
186 uint32_t max_frame_size;
192 * Note: mtu check against IONIC_MIN_MTU, IONIC_MAX_MTU
193 * is done by the the API.
197 * Max frame size is MTU + Ethernet header + VLAN + QinQ
198 * (plus ETHER_CRC_LEN if the adapter is able to keep CRC)
200 max_frame_size = mtu + RTE_ETHER_HDR_LEN + 4 + 4;
202 if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len < max_frame_size)
205 err = ionic_lif_change_mtu(lif, mtu);
213 ionic_dev_info_get(struct rte_eth_dev *eth_dev,
214 struct rte_eth_dev_info *dev_info)
216 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
217 struct ionic_adapter *adapter = lif->adapter;
218 struct ionic_identity *ident = &adapter->ident;
222 dev_info->max_rx_queues = (uint16_t)
223 ident->lif.eth.config.queue_count[IONIC_QTYPE_RXQ];
224 dev_info->max_tx_queues = (uint16_t)
225 ident->lif.eth.config.queue_count[IONIC_QTYPE_TXQ];
226 /* Also add ETHER_CRC_LEN if the adapter is able to keep CRC */
227 dev_info->min_rx_bufsize = IONIC_MIN_MTU + RTE_ETHER_HDR_LEN;
228 dev_info->max_rx_pktlen = IONIC_MAX_MTU + RTE_ETHER_HDR_LEN;
229 dev_info->max_mac_addrs = adapter->max_mac_addrs;
230 dev_info->min_mtu = IONIC_MIN_MTU;
231 dev_info->max_mtu = IONIC_MAX_MTU;
233 dev_info->speed_capa =
244 ionic_dev_configure(struct rte_eth_dev *eth_dev)
246 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
251 err = ionic_lif_configure(lif);
253 IONIC_PRINT(ERR, "Cannot configure LIF: %d", err);
260 static inline uint32_t
261 ionic_parse_link_speeds(uint16_t link_speeds)
263 if (link_speeds & ETH_LINK_SPEED_100G)
265 else if (link_speeds & ETH_LINK_SPEED_50G)
267 else if (link_speeds & ETH_LINK_SPEED_40G)
269 else if (link_speeds & ETH_LINK_SPEED_25G)
271 else if (link_speeds & ETH_LINK_SPEED_10G)
278 * Configure device link speed and setup link.
279 * It returns 0 on success.
282 ionic_dev_start(struct rte_eth_dev *eth_dev)
284 struct rte_eth_conf *dev_conf = ð_dev->data->dev_conf;
285 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
286 struct ionic_adapter *adapter = lif->adapter;
287 struct ionic_dev *idev = &adapter->idev;
288 uint32_t allowed_speeds;
294 ETH_LINK_SPEED_FIXED |
301 if (dev_conf->link_speeds & ~allowed_speeds) {
302 IONIC_PRINT(ERR, "Invalid link setting");
306 err = ionic_lif_start(lif);
308 IONIC_PRINT(ERR, "Cannot start LIF: %d", err);
312 if (eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
313 uint32_t speed = ionic_parse_link_speeds(dev_conf->link_speeds);
316 ionic_dev_cmd_port_speed(idev, speed);
319 ionic_dev_link_update(eth_dev, 0);
325 * Stop device: disable rx and tx functions to allow for reconfiguring.
328 ionic_dev_stop(struct rte_eth_dev *eth_dev)
330 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
335 err = ionic_lif_stop(lif);
337 IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
341 * Reset and stop device.
344 ionic_dev_close(struct rte_eth_dev *eth_dev)
346 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
351 err = ionic_lif_stop(lif);
353 IONIC_PRINT(ERR, "Cannot stop LIF: %d", err);
357 err = eth_ionic_dev_uninit(eth_dev);
359 IONIC_PRINT(ERR, "Cannot destroy LIF: %d", err);
365 eth_ionic_dev_init(struct rte_eth_dev *eth_dev, void *init_params)
367 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
368 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
369 struct ionic_adapter *adapter = (struct ionic_adapter *)init_params;
374 eth_dev->dev_ops = &ionic_eth_dev_ops;
376 /* Multi-process not supported, primary does initialization anyway */
377 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
380 rte_eth_copy_pci_info(eth_dev, pci_dev);
382 lif->index = adapter->nlifs;
383 lif->eth_dev = eth_dev;
384 lif->adapter = adapter;
385 adapter->lifs[adapter->nlifs] = lif;
387 IONIC_PRINT(DEBUG, "Up to %u MAC addresses supported",
388 adapter->max_mac_addrs);
390 /* Allocate memory for storing MAC addresses */
391 eth_dev->data->mac_addrs = rte_zmalloc("ionic",
392 RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs, 0);
394 if (eth_dev->data->mac_addrs == NULL) {
395 IONIC_PRINT(ERR, "Failed to allocate %u bytes needed to "
396 "store MAC addresses",
397 RTE_ETHER_ADDR_LEN * adapter->max_mac_addrs);
402 err = ionic_lif_alloc(lif);
404 IONIC_PRINT(ERR, "Cannot allocate LIFs: %d, aborting",
409 err = ionic_lif_init(lif);
411 IONIC_PRINT(ERR, "Cannot init LIFs: %d, aborting", err);
415 /* Copy the MAC address */
416 rte_ether_addr_copy((struct rte_ether_addr *)lif->mac_addr,
417 ð_dev->data->mac_addrs[0]);
419 IONIC_PRINT(DEBUG, "Port %u initialized", eth_dev->data->port_id);
430 eth_ionic_dev_uninit(struct rte_eth_dev *eth_dev)
432 struct ionic_lif *lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
433 struct ionic_adapter *adapter = lif->adapter;
437 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
440 adapter->lifs[lif->index] = NULL;
442 ionic_lif_deinit(lif);
445 eth_dev->dev_ops = NULL;
451 ionic_configure_intr(struct ionic_adapter *adapter)
453 struct rte_pci_device *pci_dev = adapter->pci_dev;
454 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
457 IONIC_PRINT(DEBUG, "Configuring %u intrs", adapter->nintrs);
459 if (rte_intr_efd_enable(intr_handle, adapter->nintrs)) {
460 IONIC_PRINT(ERR, "Fail to create eventfd");
464 if (rte_intr_dp_is_en(intr_handle))
466 "Packet I/O interrupt on datapath is enabled");
468 if (!intr_handle->intr_vec) {
469 intr_handle->intr_vec = rte_zmalloc("intr_vec",
470 adapter->nintrs * sizeof(int), 0);
472 if (!intr_handle->intr_vec) {
473 IONIC_PRINT(ERR, "Failed to allocate %u vectors",
479 err = rte_intr_callback_register(intr_handle,
480 ionic_dev_interrupt_handler,
485 "Failure registering interrupts handler (%d)",
490 /* enable intr mapping */
491 err = rte_intr_enable(intr_handle);
494 IONIC_PRINT(ERR, "Failure enabling interrupts (%d)", err);
502 ionic_unconfigure_intr(struct ionic_adapter *adapter)
504 struct rte_pci_device *pci_dev = adapter->pci_dev;
505 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
507 rte_intr_disable(intr_handle);
509 rte_intr_callback_unregister(intr_handle,
510 ionic_dev_interrupt_handler,
515 eth_ionic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
516 struct rte_pci_device *pci_dev)
518 char name[RTE_ETH_NAME_MAX_LEN];
519 struct rte_mem_resource *resource;
520 struct ionic_adapter *adapter;
525 /* Check structs (trigger error at compilation time) */
526 ionic_struct_size_checks();
528 /* Multi-process not supported */
529 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
534 IONIC_PRINT(DEBUG, "Initializing device %s",
535 pci_dev->device.name);
537 adapter = rte_zmalloc("ionic", sizeof(*adapter), 0);
539 IONIC_PRINT(ERR, "OOM");
544 adapter->pci_dev = pci_dev;
547 hw->device_id = pci_dev->id.device_id;
548 hw->vendor_id = pci_dev->id.vendor_id;
550 err = ionic_init_mac(hw);
552 IONIC_PRINT(ERR, "Mac init failed: %d", err);
554 goto err_free_adapter;
557 adapter->is_mgmt_nic = (pci_dev->id.device_id == IONIC_DEV_ID_ETH_MGMT);
559 adapter->num_bars = 0;
560 for (i = 0; i < PCI_MAX_RESOURCE && i < IONIC_BARS_MAX; i++) {
561 resource = &pci_dev->mem_resource[i];
562 if (resource->phys_addr == 0 || resource->len == 0)
564 adapter->bars[adapter->num_bars].vaddr = resource->addr;
565 adapter->bars[adapter->num_bars].bus_addr = resource->phys_addr;
566 adapter->bars[adapter->num_bars].len = resource->len;
570 /* Discover ionic dev resources */
572 err = ionic_setup(adapter);
574 IONIC_PRINT(ERR, "Cannot setup device: %d, aborting", err);
575 goto err_free_adapter;
578 err = ionic_identify(adapter);
580 IONIC_PRINT(ERR, "Cannot identify device: %d, aborting",
582 goto err_free_adapter;
585 err = ionic_init(adapter);
587 IONIC_PRINT(ERR, "Cannot init device: %d, aborting", err);
588 goto err_free_adapter;
591 /* Configure the ports */
592 err = ionic_port_identify(adapter);
594 IONIC_PRINT(ERR, "Cannot identify port: %d, aborting",
596 goto err_free_adapter;
599 err = ionic_port_init(adapter);
601 IONIC_PRINT(ERR, "Cannot init port: %d, aborting", err);
602 goto err_free_adapter;
606 err = ionic_lif_identify(adapter);
608 IONIC_PRINT(ERR, "Cannot identify lif: %d, aborting", err);
609 goto err_free_adapter;
612 /* Allocate and init LIFs */
613 err = ionic_lifs_size(adapter);
615 IONIC_PRINT(ERR, "Cannot size LIFs: %d, aborting", err);
616 goto err_free_adapter;
619 adapter->max_mac_addrs = adapter->ident.lif.eth.max_ucast_filters;
622 for (i = 0; i < adapter->ident.dev.nlifs; i++) {
623 snprintf(name, sizeof(name), "net_%s_lif_%lu",
624 pci_dev->device.name, i);
626 err = rte_eth_dev_create(&pci_dev->device, name,
627 sizeof(struct ionic_lif),
629 eth_ionic_dev_init, adapter);
631 IONIC_PRINT(ERR, "Cannot create eth device for "
632 "ionic lif %s", name);
639 err = ionic_configure_intr(adapter);
642 IONIC_PRINT(ERR, "Failed to configure interrupts");
643 goto err_free_adapter;
655 eth_ionic_pci_remove(struct rte_pci_device *pci_dev __rte_unused)
657 char name[RTE_ETH_NAME_MAX_LEN];
658 struct ionic_adapter *adapter = NULL;
659 struct rte_eth_dev *eth_dev;
660 struct ionic_lif *lif;
663 /* Adapter lookup is using (the first) eth_dev name */
664 snprintf(name, sizeof(name), "net_%s_lif_0",
665 pci_dev->device.name);
667 eth_dev = rte_eth_dev_allocated(name);
669 lif = IONIC_ETH_DEV_TO_LIF(eth_dev);
670 adapter = lif->adapter;
674 ionic_unconfigure_intr(adapter);
676 for (i = 0; i < adapter->nlifs; i++) {
677 lif = adapter->lifs[i];
678 rte_eth_dev_destroy(lif->eth_dev, eth_ionic_dev_uninit);
687 static struct rte_pci_driver rte_ionic_pmd = {
688 .id_table = pci_id_ionic_map,
689 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
690 .probe = eth_ionic_pci_probe,
691 .remove = eth_ionic_pci_remove,
694 RTE_PMD_REGISTER_PCI(net_ionic, rte_ionic_pmd);
695 RTE_PMD_REGISTER_PCI_TABLE(net_ionic, pci_id_ionic_map);
696 RTE_PMD_REGISTER_KMOD_DEP(net_ionic, "* igb_uio | uio_pci_generic | vfio-pci");
698 RTE_INIT(ionic_init_log)
700 ionic_logtype = rte_log_register("pmd.net.ionic");
701 if (ionic_logtype >= 0)
702 rte_log_set_level(ionic_logtype, RTE_LOG_NOTICE);