1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
9 #include <rte_spinlock.h>
11 #include "ice_dcf_ethdev.h"
13 #define ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL 100000 /* us */
14 static rte_spinlock_t vsi_update_lock = RTE_SPINLOCK_INITIALIZER;
16 static __rte_always_inline void
17 ice_dcf_update_vsi_ctx(struct ice_hw *hw, uint16_t vsi_handle,
20 struct ice_vsi_ctx *vsi_ctx;
22 if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
23 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
27 vsi_ctx = hw->vsi_ctx[vsi_handle];
29 if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
31 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
33 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
39 vsi_ctx->vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
40 VIRTCHNL_DCF_VF_VSI_ID_S;
41 hw->vsi_ctx[vsi_handle] = vsi_ctx;
43 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
44 vsi_handle, vsi_ctx->vsi_num);
46 hw->vsi_ctx[vsi_handle] = NULL;
48 ice_free(hw, vsi_ctx);
50 PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
55 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
60 for (vf_id = 0; vf_id < num_vfs; vf_id++)
61 ice_dcf_update_vsi_ctx(hw, vf_id, vf_vsi_map[vf_id]);
65 ice_dcf_vsi_update_service_handler(void *param)
67 struct ice_dcf_hw *hw = param;
69 usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
71 rte_spinlock_lock(&vsi_update_lock);
73 if (!ice_dcf_handle_vsi_update_event(hw)) {
74 struct ice_dcf_adapter *dcf_ad =
75 container_of(hw, struct ice_dcf_adapter, real_hw);
77 ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
78 hw->num_vfs, hw->vf_vsi_map);
81 rte_spinlock_unlock(&vsi_update_lock);
87 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
88 uint8_t *msg, uint16_t msglen)
90 struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
93 if (msglen < sizeof(struct virtchnl_pf_event)) {
94 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
98 switch (pf_msg->event) {
99 case VIRTCHNL_EVENT_RESET_IMPENDING:
100 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
101 pthread_create(&thread, NULL,
102 ice_dcf_vsi_update_service_handler, dcf_hw);
104 case VIRTCHNL_EVENT_LINK_CHANGE:
105 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
107 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
108 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
110 case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
111 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
112 pf_msg->event_data.vf_vsi_map.vf_id,
113 pf_msg->event_data.vf_vsi_map.vsi_id);
114 pthread_create(&thread, NULL,
115 ice_dcf_vsi_update_service_handler, dcf_hw);
118 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
124 ice_dcf_init_parent_hw(struct ice_hw *hw)
126 struct ice_aqc_get_phy_caps_data *pcaps;
127 enum ice_status status;
129 status = ice_aq_get_fw_ver(hw, NULL);
133 status = ice_get_caps(hw);
137 hw->port_info = (struct ice_port_info *)
138 ice_malloc(hw, sizeof(*hw->port_info));
140 return ICE_ERR_NO_MEMORY;
142 /* set the back pointer to HW */
143 hw->port_info->hw = hw;
145 /* Initialize port_info struct with switch configuration data */
146 status = ice_get_initial_sw_cfg(hw);
148 goto err_unroll_alloc;
150 pcaps = (struct ice_aqc_get_phy_caps_data *)
151 ice_malloc(hw, sizeof(*pcaps));
153 status = ICE_ERR_NO_MEMORY;
154 goto err_unroll_alloc;
157 /* Initialize port_info struct with PHY capabilities */
158 status = ice_aq_get_phy_caps(hw->port_info, false,
159 ICE_AQC_REPORT_TOPO_CAP, pcaps, NULL);
162 goto err_unroll_alloc;
164 /* Initialize port_info struct with link information */
165 status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
167 goto err_unroll_alloc;
169 status = ice_init_fltr_mgmt_struct(hw);
171 goto err_unroll_alloc;
173 status = ice_init_hw_tbls(hw);
175 goto err_unroll_fltr_mgmt_struct;
178 "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
179 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
180 hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
185 err_unroll_fltr_mgmt_struct:
186 ice_cleanup_fltr_mgmt_struct(hw);
188 ice_free(hw, hw->port_info);
189 hw->port_info = NULL;
194 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
196 ice_cleanup_fltr_mgmt_struct(hw);
199 ice_free_hw_tbls(hw);
201 ice_free(hw, hw->port_info);
202 hw->port_info = NULL;
204 ice_clear_all_vsi_ctx(hw);
208 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
210 struct ice_dcf_adapter *dcf_adapter =
211 container_of(hw, struct ice_dcf_adapter, parent.hw);
213 /* TODO: check with DSN firstly by iAVF */
216 dcf_adapter->real_hw.vsi_id);
219 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
220 if (!access(pkg_name, 0))
224 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
225 if (!access(pkg_name, 0))
232 ice_dcf_load_pkg(struct ice_hw *hw)
234 char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
241 if (ice_dcf_request_pkg_name(hw, pkg_name)) {
242 PMD_INIT_LOG(ERR, "Failed to locate the package file");
246 PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
248 err = stat(pkg_name, &st);
250 PMD_INIT_LOG(ERR, "Failed to get file status");
254 buf_len = st.st_size;
255 pkg_buf = rte_malloc(NULL, buf_len, 0);
257 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
262 fp = fopen(pkg_name, "rb");
264 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
269 err = fread(pkg_buf, buf_len, 1, fp);
272 PMD_INIT_LOG(ERR, "failed to read package data");
277 err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
279 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
287 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
289 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
290 struct ice_adapter *parent_adapter = &adapter->parent;
291 struct ice_hw *parent_hw = &parent_adapter->hw;
292 struct ice_dcf_hw *hw = &adapter->real_hw;
293 const struct rte_ether_addr *mac;
296 parent_adapter->eth_dev = eth_dev;
297 parent_adapter->pf.adapter = parent_adapter;
298 parent_adapter->pf.dev_data = eth_dev->data;
299 parent_hw->back = parent_adapter;
300 parent_hw->mac_type = ICE_MAC_GENERIC;
301 parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
303 ice_init_lock(&parent_hw->adminq.sq_lock);
304 ice_init_lock(&parent_hw->adminq.rq_lock);
305 parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
306 parent_hw->aq_send_cmd_param = &adapter->real_hw;
307 parent_hw->dcf_enabled = true;
309 err = ice_dcf_init_parent_hw(parent_hw);
311 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
316 err = ice_dcf_load_pkg(parent_hw);
318 PMD_INIT_LOG(ERR, "failed to load package with error %d",
322 parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
324 ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
326 mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
327 if (rte_is_valid_assigned_ether_addr(mac))
328 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
330 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
332 eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
337 ice_dcf_uninit_parent_hw(parent_hw);
342 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
344 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
345 struct ice_adapter *parent_adapter = &adapter->parent;
346 struct ice_hw *parent_hw = &parent_adapter->hw;
348 eth_dev->data->mac_addrs = NULL;
350 ice_dcf_uninit_parent_hw(parent_hw);