1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
6 #include <vnet/dev/dev.h>
7 #include <vnet/dev/pci.h>
8 #include <dev_ena/ena.h>
9 #include <dev_ena/ena_inlines.h>
10 #include <vnet/ethernet/ethernet.h>
11 #include <vnet/plugin/plugin.h>
12 #include <vpp/app/version.h>
14 static ena_aq_host_info_t host_info = {
15 .os_type = 3, /* DPDK */
16 .kernel_ver_str = VPP_BUILD_VER,
17 .os_dist_str = VPP_BUILD_VER,
27 .driver_supported_features = {
29 .rss_configurable_function_key = 1,
33 VLIB_REGISTER_LOG_CLASS (ena_log, static) = {
35 .subclass_name = "init",
38 #define _(f, n, s, d) \
39 { .name = #n, .desc = d, .severity = VL_COUNTER_SEVERITY_##s },
41 static vlib_error_desc_t ena_rx_node_counters[] = {
42 foreach_ena_rx_node_counter
44 static vlib_error_desc_t ena_tx_node_counters[] = {
45 foreach_ena_tx_node_counter
49 vnet_dev_node_t ena_rx_node = {
50 .error_counters = ena_rx_node_counters,
51 .n_error_counters = ARRAY_LEN (ena_rx_node_counters),
52 .format_trace = format_ena_rx_trace,
55 vnet_dev_node_t ena_tx_node = {
56 .error_counters = ena_tx_node_counters,
57 .n_error_counters = ARRAY_LEN (ena_tx_node_counters),
61 ena_deinit (vlib_main_t *vm, vnet_dev_t *dev)
63 ena_aenq_stop (vm, dev);
64 ena_aq_stop (vm, dev);
68 ena_alloc (vlib_main_t *vm, vnet_dev_t *dev)
70 ena_device_t *ed = vnet_dev_get_data (dev);
73 if ((rv = vnet_dev_dma_mem_alloc (vm, dev, 4096, 4096,
74 (void **) &ed->host_info)))
77 if ((rv = vnet_dev_dma_mem_alloc (vm, dev, sizeof (ena_mmio_resp_t), 0,
78 (void **) &ed->mmio_resp)))
81 if ((rv = ena_aq_olloc (vm, dev, ENA_ADMIN_QUEUE_DEPTH)))
84 if ((rv = ena_aenq_olloc (vm, dev, ENA_ASYNC_QUEUE_DEPTH)))
91 ena_free (vlib_main_t *vm, vnet_dev_t *dev)
93 ena_device_t *ed = vnet_dev_get_data (dev);
95 ena_aenq_free (vm, dev);
96 ena_aq_free (vm, dev);
98 vnet_dev_dma_mem_free (vm, dev, ed->host_info);
99 vnet_dev_dma_mem_free (vm, dev, ed->mmio_resp);
103 ena_init (vlib_main_t *vm, vnet_dev_t *dev)
105 ena_device_t *ed = vnet_dev_get_data (dev);
106 ena_aq_feat_host_attr_config_t host_attr = {};
107 vlib_pci_config_hdr_t pci_cfg_hdr;
108 vnet_dev_rv_t rv = VNET_DEV_OK;
110 vnet_dev_port_add_args_t port = {
113 .type = VNET_DEV_PORT_TYPE_ETHERNET,
116 .init = ena_port_init,
117 .start = ena_port_start,
118 .stop = ena_port_stop,
119 .config_change = ena_port_cfg_change,
120 .config_change_validate = ena_port_cfg_change_validate,
122 .data_size = sizeof (ena_port_t),
124 .rx_node = &ena_rx_node,
125 .tx_node = &ena_tx_node,
128 .data_size = sizeof (ena_rxq_t),
131 .size_is_power_of_two = 1,
134 .alloc = ena_rx_queue_alloc,
135 .start = ena_rx_queue_start,
136 .stop = ena_rx_queue_stop,
137 .free = ena_rx_queue_free,
142 .data_size = sizeof (ena_txq_t),
145 .size_is_power_of_two = 1,
148 .alloc = ena_tx_queue_alloc,
149 .start = ena_tx_queue_start,
150 .stop = ena_tx_queue_stop,
151 .free = ena_tx_queue_free,
156 if ((rv = vnet_dev_pci_read_config_header (vm, dev, &pci_cfg_hdr)))
159 log_debug (dev, "revision_id 0x%x", pci_cfg_hdr.revision_id);
161 ed->readless = (pci_cfg_hdr.revision_id & 1) == 0;
163 if ((rv = vnet_dev_pci_map_region (vm, dev, 0, &ed->reg_bar)))
166 if ((rv = ena_reg_reset (vm, dev, ENA_RESET_REASON_NORMAL)))
169 if ((rv = ena_aq_start (vm, dev)))
172 *ed->host_info = host_info;
173 ed->host_info->num_cpus = vlib_get_n_threads ();
174 ena_set_mem_addr (vm, dev, &host_attr.os_info_ba, ed->host_info);
176 if ((rv = ena_aq_set_feature (vm, dev, ENA_ADMIN_FEAT_ID_HOST_ATTR_CONFIG,
180 if ((rv = ena_aq_get_feature (vm, dev, ENA_ADMIN_FEAT_ID_DEVICE_ATTRIBUTES,
184 if (ena_aq_feature_is_supported (dev, ENA_ADMIN_FEAT_ID_MAX_QUEUES_EXT))
186 ena_aq_feat_max_queue_ext_t max_q_ext;
187 if ((rv = ena_aq_get_feature (vm, dev, ENA_ADMIN_FEAT_ID_MAX_QUEUES_EXT,
190 port.port.attr.max_rx_queues =
191 clib_min (max_q_ext.max_rx_cq_num, max_q_ext.max_rx_sq_num);
192 port.port.attr.max_tx_queues =
193 clib_min (max_q_ext.max_tx_cq_num, max_q_ext.max_tx_sq_num);
194 port.rx_queue.config.max_size =
195 clib_min (max_q_ext.max_rx_cq_depth, max_q_ext.max_rx_sq_depth);
196 port.tx_queue.config.max_size =
197 clib_min (max_q_ext.max_tx_cq_depth, max_q_ext.max_tx_sq_depth);
201 log_err (dev, "device doesn't support MAX_QUEUES_EXT");
202 return VNET_DEV_ERR_UNSUPPORTED_DEVICE_VER;
205 if ((rv = ena_aenq_start (vm, dev)))
208 port.port.attr.max_supported_rx_frame_size = ed->dev_attr.max_mtu;
210 if (ena_aq_feature_is_supported (dev, ENA_ADMIN_FEAT_ID_MTU))
211 port.port.attr.caps.change_max_rx_frame_size = 1;
213 vnet_dev_set_hw_addr_eth_mac (&port.port.attr.hw_addr,
214 ed->dev_attr.mac_addr);
216 return vnet_dev_port_add (vm, dev, 0, &port);
224 ena_probe (vlib_main_t *vm, vnet_dev_bus_index_t bus_index, void *dev_info)
226 vnet_dev_bus_pci_device_info_t *di = dev_info;
231 } ena_dev_types[] = {
232 { .device_id = 0x0ec2, .description = "Elastic Network Adapter (ENA) PF" },
233 { .device_id = 0xec20, .description = "Elastic Network Adapter (ENA) VF" },
236 if (di->vendor_id != 0x1d0f) /* AMAZON */
239 FOREACH_ARRAY_ELT (dt, ena_dev_types)
241 if (dt->device_id == di->device_id)
242 return format (0, "%s", dt->description);
248 VNET_DEV_REGISTER_DRIVER (ena) = {
251 .device_data_sz = sizeof (ena_device_t),
255 .deinit = ena_deinit,
257 .format_info = format_ena_dev_info,
262 VLIB_PLUGIN_REGISTER () = {
263 .version = VPP_BUILD_VER,
264 .description = "dev_ena",