1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
9 #include <rte_spinlock.h>
11 #include "ice_dcf_ethdev.h"
12 #include "ice_generic_flow.h"
14 #define ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL 100000 /* us */
15 static rte_spinlock_t vsi_update_lock = RTE_SPINLOCK_INITIALIZER;
17 static __rte_always_inline void
18 ice_dcf_update_vsi_ctx(struct ice_hw *hw, uint16_t vsi_handle,
21 struct ice_vsi_ctx *vsi_ctx;
23 if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
24 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
28 vsi_ctx = hw->vsi_ctx[vsi_handle];
30 if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
32 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
34 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
40 vsi_ctx->vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
41 VIRTCHNL_DCF_VF_VSI_ID_S;
42 hw->vsi_ctx[vsi_handle] = vsi_ctx;
44 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
45 vsi_handle, vsi_ctx->vsi_num);
47 hw->vsi_ctx[vsi_handle] = NULL;
49 ice_free(hw, vsi_ctx);
51 PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
56 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
61 for (vf_id = 0; vf_id < num_vfs; vf_id++)
62 ice_dcf_update_vsi_ctx(hw, vf_id, vf_vsi_map[vf_id]);
66 ice_dcf_vsi_update_service_handler(void *param)
68 struct ice_dcf_hw *hw = param;
70 usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
72 rte_spinlock_lock(&vsi_update_lock);
74 if (!ice_dcf_handle_vsi_update_event(hw)) {
75 struct ice_dcf_adapter *dcf_ad =
76 container_of(hw, struct ice_dcf_adapter, real_hw);
78 ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
79 hw->num_vfs, hw->vf_vsi_map);
82 rte_spinlock_unlock(&vsi_update_lock);
88 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
89 uint8_t *msg, uint16_t msglen)
91 struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
94 if (msglen < sizeof(struct virtchnl_pf_event)) {
95 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
99 switch (pf_msg->event) {
100 case VIRTCHNL_EVENT_RESET_IMPENDING:
101 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
102 pthread_create(&thread, NULL,
103 ice_dcf_vsi_update_service_handler, dcf_hw);
105 case VIRTCHNL_EVENT_LINK_CHANGE:
106 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
108 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
109 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
111 case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
112 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
113 pf_msg->event_data.vf_vsi_map.vf_id,
114 pf_msg->event_data.vf_vsi_map.vsi_id);
115 pthread_create(&thread, NULL,
116 ice_dcf_vsi_update_service_handler, dcf_hw);
119 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
125 ice_dcf_init_parent_hw(struct ice_hw *hw)
127 struct ice_aqc_get_phy_caps_data *pcaps;
128 enum ice_status status;
130 status = ice_aq_get_fw_ver(hw, NULL);
134 status = ice_get_caps(hw);
138 hw->port_info = (struct ice_port_info *)
139 ice_malloc(hw, sizeof(*hw->port_info));
141 return ICE_ERR_NO_MEMORY;
143 /* set the back pointer to HW */
144 hw->port_info->hw = hw;
146 /* Initialize port_info struct with switch configuration data */
147 status = ice_get_initial_sw_cfg(hw);
149 goto err_unroll_alloc;
151 pcaps = (struct ice_aqc_get_phy_caps_data *)
152 ice_malloc(hw, sizeof(*pcaps));
154 status = ICE_ERR_NO_MEMORY;
155 goto err_unroll_alloc;
158 /* Initialize port_info struct with PHY capabilities */
159 status = ice_aq_get_phy_caps(hw->port_info, false,
160 ICE_AQC_REPORT_TOPO_CAP, pcaps, NULL);
163 goto err_unroll_alloc;
165 /* Initialize port_info struct with link information */
166 status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
168 goto err_unroll_alloc;
170 status = ice_init_fltr_mgmt_struct(hw);
172 goto err_unroll_alloc;
174 status = ice_init_hw_tbls(hw);
176 goto err_unroll_fltr_mgmt_struct;
179 "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
180 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
181 hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
186 err_unroll_fltr_mgmt_struct:
187 ice_cleanup_fltr_mgmt_struct(hw);
189 ice_free(hw, hw->port_info);
190 hw->port_info = NULL;
195 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
197 ice_cleanup_fltr_mgmt_struct(hw);
200 ice_free_hw_tbls(hw);
202 ice_free(hw, hw->port_info);
203 hw->port_info = NULL;
205 ice_clear_all_vsi_ctx(hw);
209 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
211 struct ice_dcf_adapter *dcf_adapter =
212 container_of(hw, struct ice_dcf_adapter, parent.hw);
214 /* TODO: check with DSN firstly by iAVF */
217 dcf_adapter->real_hw.vsi_id);
220 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
221 if (!access(pkg_name, 0))
225 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
226 if (!access(pkg_name, 0))
233 ice_dcf_load_pkg(struct ice_hw *hw)
235 char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
242 if (ice_dcf_request_pkg_name(hw, pkg_name)) {
243 PMD_INIT_LOG(ERR, "Failed to locate the package file");
247 PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
249 err = stat(pkg_name, &st);
251 PMD_INIT_LOG(ERR, "Failed to get file status");
255 buf_len = st.st_size;
256 pkg_buf = rte_malloc(NULL, buf_len, 0);
258 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
263 fp = fopen(pkg_name, "rb");
265 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
270 err = fread(pkg_buf, buf_len, 1, fp);
273 PMD_INIT_LOG(ERR, "failed to read package data");
278 err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
280 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
288 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
290 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
291 struct ice_adapter *parent_adapter = &adapter->parent;
292 struct ice_hw *parent_hw = &parent_adapter->hw;
293 struct ice_dcf_hw *hw = &adapter->real_hw;
294 const struct rte_ether_addr *mac;
297 parent_adapter->eth_dev = eth_dev;
298 parent_adapter->pf.adapter = parent_adapter;
299 parent_adapter->pf.dev_data = eth_dev->data;
300 parent_hw->back = parent_adapter;
301 parent_hw->mac_type = ICE_MAC_GENERIC;
302 parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
304 ice_init_lock(&parent_hw->adminq.sq_lock);
305 ice_init_lock(&parent_hw->adminq.rq_lock);
306 parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
307 parent_hw->aq_send_cmd_param = &adapter->real_hw;
308 parent_hw->dcf_enabled = true;
310 err = ice_dcf_init_parent_hw(parent_hw);
312 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
317 err = ice_dcf_load_pkg(parent_hw);
319 PMD_INIT_LOG(ERR, "failed to load package with error %d",
323 parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
325 err = ice_flow_init(parent_adapter);
327 PMD_INIT_LOG(ERR, "Failed to initialize flow");
331 ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
333 mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
334 if (rte_is_valid_assigned_ether_addr(mac))
335 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
337 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
339 eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
344 ice_dcf_uninit_parent_hw(parent_hw);
349 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
351 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
352 struct ice_adapter *parent_adapter = &adapter->parent;
353 struct ice_hw *parent_hw = &parent_adapter->hw;
355 eth_dev->data->mac_addrs = NULL;
357 ice_flow_uninit(parent_adapter);
358 ice_dcf_uninit_parent_hw(parent_hw);