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;
22 bool first_update = false;
25 if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
26 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
30 vsi_ctx = hw->vsi_ctx[vsi_handle];
32 if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
34 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
36 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
40 hw->vsi_ctx[vsi_handle] = vsi_ctx;
44 new_vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
45 VIRTCHNL_DCF_VF_VSI_ID_S;
47 /* Redirect rules if vsi mapping table changes. */
48 if (!first_update && vsi_ctx->vsi_num != new_vsi_num) {
49 struct ice_flow_redirect rd;
51 memset(&rd, 0, sizeof(struct ice_flow_redirect));
52 rd.type = ICE_FLOW_REDIRECT_VSI;
53 rd.vsi_handle = vsi_handle;
54 rd.new_vsi_num = new_vsi_num;
55 ice_flow_redirect((struct ice_adapter *)hw->back, &rd);
57 vsi_ctx->vsi_num = new_vsi_num;
60 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
61 vsi_handle, vsi_ctx->vsi_num);
63 hw->vsi_ctx[vsi_handle] = NULL;
65 ice_free(hw, vsi_ctx);
67 PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
72 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
77 for (vf_id = 0; vf_id < num_vfs; vf_id++)
78 ice_dcf_update_vsi_ctx(hw, vf_id, vf_vsi_map[vf_id]);
82 ice_dcf_vsi_update_service_handler(void *param)
84 struct ice_dcf_hw *hw = param;
86 usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
88 rte_spinlock_lock(&vsi_update_lock);
90 if (!ice_dcf_handle_vsi_update_event(hw)) {
91 struct ice_dcf_adapter *dcf_ad =
92 container_of(hw, struct ice_dcf_adapter, real_hw);
94 ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
95 hw->num_vfs, hw->vf_vsi_map);
98 rte_spinlock_unlock(&vsi_update_lock);
104 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
105 uint8_t *msg, uint16_t msglen)
107 struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
110 if (msglen < sizeof(struct virtchnl_pf_event)) {
111 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
115 switch (pf_msg->event) {
116 case VIRTCHNL_EVENT_RESET_IMPENDING:
117 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
118 pthread_create(&thread, NULL,
119 ice_dcf_vsi_update_service_handler, dcf_hw);
121 case VIRTCHNL_EVENT_LINK_CHANGE:
122 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
124 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
125 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
127 case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
128 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
129 pf_msg->event_data.vf_vsi_map.vf_id,
130 pf_msg->event_data.vf_vsi_map.vsi_id);
131 pthread_create(&thread, NULL,
132 ice_dcf_vsi_update_service_handler, dcf_hw);
135 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
141 ice_dcf_init_parent_hw(struct ice_hw *hw)
143 struct ice_aqc_get_phy_caps_data *pcaps;
144 enum ice_status status;
146 status = ice_aq_get_fw_ver(hw, NULL);
150 status = ice_get_caps(hw);
154 hw->port_info = (struct ice_port_info *)
155 ice_malloc(hw, sizeof(*hw->port_info));
157 return ICE_ERR_NO_MEMORY;
159 /* set the back pointer to HW */
160 hw->port_info->hw = hw;
162 /* Initialize port_info struct with switch configuration data */
163 status = ice_get_initial_sw_cfg(hw);
165 goto err_unroll_alloc;
167 pcaps = (struct ice_aqc_get_phy_caps_data *)
168 ice_malloc(hw, sizeof(*pcaps));
170 status = ICE_ERR_NO_MEMORY;
171 goto err_unroll_alloc;
174 /* Initialize port_info struct with PHY capabilities */
175 status = ice_aq_get_phy_caps(hw->port_info, false,
176 ICE_AQC_REPORT_TOPO_CAP, pcaps, NULL);
179 goto err_unroll_alloc;
181 /* Initialize port_info struct with link information */
182 status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
184 goto err_unroll_alloc;
186 status = ice_init_fltr_mgmt_struct(hw);
188 goto err_unroll_alloc;
190 status = ice_init_hw_tbls(hw);
192 goto err_unroll_fltr_mgmt_struct;
195 "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
196 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
197 hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
202 err_unroll_fltr_mgmt_struct:
203 ice_cleanup_fltr_mgmt_struct(hw);
205 ice_free(hw, hw->port_info);
206 hw->port_info = NULL;
211 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
213 ice_cleanup_fltr_mgmt_struct(hw);
216 ice_free_hw_tbls(hw);
218 ice_free(hw, hw->port_info);
219 hw->port_info = NULL;
221 ice_clear_all_vsi_ctx(hw);
225 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
227 struct ice_dcf_adapter *dcf_adapter =
228 container_of(hw, struct ice_dcf_adapter, parent.hw);
230 /* TODO: check with DSN firstly by iAVF */
233 dcf_adapter->real_hw.vsi_id);
236 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
237 if (!access(pkg_name, 0))
241 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
242 if (!access(pkg_name, 0))
249 ice_dcf_load_pkg(struct ice_hw *hw)
251 char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
258 if (ice_dcf_request_pkg_name(hw, pkg_name)) {
259 PMD_INIT_LOG(ERR, "Failed to locate the package file");
263 PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
265 err = stat(pkg_name, &st);
267 PMD_INIT_LOG(ERR, "Failed to get file status");
271 buf_len = st.st_size;
272 pkg_buf = rte_malloc(NULL, buf_len, 0);
274 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
279 fp = fopen(pkg_name, "rb");
281 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
286 err = fread(pkg_buf, buf_len, 1, fp);
289 PMD_INIT_LOG(ERR, "failed to read package data");
294 err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
296 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
304 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
306 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
307 struct ice_adapter *parent_adapter = &adapter->parent;
308 struct ice_hw *parent_hw = &parent_adapter->hw;
309 struct ice_dcf_hw *hw = &adapter->real_hw;
310 const struct rte_ether_addr *mac;
313 parent_adapter->eth_dev = eth_dev;
314 parent_adapter->pf.adapter = parent_adapter;
315 parent_adapter->pf.dev_data = eth_dev->data;
316 parent_hw->back = parent_adapter;
317 parent_hw->mac_type = ICE_MAC_GENERIC;
318 parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
320 ice_init_lock(&parent_hw->adminq.sq_lock);
321 ice_init_lock(&parent_hw->adminq.rq_lock);
322 parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
323 parent_hw->aq_send_cmd_param = &adapter->real_hw;
324 parent_hw->dcf_enabled = true;
326 err = ice_dcf_init_parent_hw(parent_hw);
328 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
333 err = ice_dcf_load_pkg(parent_hw);
335 PMD_INIT_LOG(ERR, "failed to load package with error %d",
339 parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
341 err = ice_flow_init(parent_adapter);
343 PMD_INIT_LOG(ERR, "Failed to initialize flow");
347 ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
349 mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
350 if (rte_is_valid_assigned_ether_addr(mac))
351 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
353 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
355 eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
360 ice_dcf_uninit_parent_hw(parent_hw);
365 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
367 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
368 struct ice_adapter *parent_adapter = &adapter->parent;
369 struct ice_hw *parent_hw = &parent_adapter->hw;
371 eth_dev->data->mac_addrs = NULL;
373 ice_flow_uninit(parent_adapter);
374 ice_dcf_uninit_parent_hw(parent_hw);