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. */
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);
229 struct virtchnl_pkg_info pkg_info;
230 struct dcf_virtchnl_cmd vc_cmd;
233 vc_cmd.v_op = VIRTCHNL_OP_DCF_GET_PKG_INFO;
234 vc_cmd.req_msglen = 0;
235 vc_cmd.req_msg = NULL;
236 vc_cmd.rsp_buflen = sizeof(pkg_info);
237 vc_cmd.rsp_msgbuf = (uint8_t *)&pkg_info;
239 if (ice_dcf_execute_virtchnl_cmd(&dcf_adapter->real_hw, &vc_cmd))
240 goto pkg_file_direct;
242 rte_memcpy(&dsn, pkg_info.dsn, sizeof(dsn));
244 snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
245 ICE_PKG_FILE_SEARCH_PATH_UPDATES "ice-%016llx.pkg",
246 (unsigned long long)dsn);
247 if (!access(pkg_name, 0))
250 snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
251 ICE_PKG_FILE_SEARCH_PATH_DEFAULT "ice-%016llx.pkg",
252 (unsigned long long)dsn);
253 if (!access(pkg_name, 0))
258 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
259 if (!access(pkg_name, 0))
263 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
264 if (!access(pkg_name, 0))
271 ice_dcf_load_pkg(struct ice_hw *hw)
273 char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
280 if (ice_dcf_request_pkg_name(hw, pkg_name)) {
281 PMD_INIT_LOG(ERR, "Failed to locate the package file");
285 PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
287 err = stat(pkg_name, &st);
289 PMD_INIT_LOG(ERR, "Failed to get file status");
293 buf_len = st.st_size;
294 pkg_buf = rte_malloc(NULL, buf_len, 0);
296 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
301 fp = fopen(pkg_name, "rb");
303 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
308 err = fread(pkg_buf, buf_len, 1, fp);
311 PMD_INIT_LOG(ERR, "failed to read package data");
316 err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
318 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
326 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
328 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
329 struct ice_adapter *parent_adapter = &adapter->parent;
330 struct ice_hw *parent_hw = &parent_adapter->hw;
331 struct ice_dcf_hw *hw = &adapter->real_hw;
332 const struct rte_ether_addr *mac;
335 parent_adapter->eth_dev = eth_dev;
336 parent_adapter->pf.adapter = parent_adapter;
337 parent_adapter->pf.dev_data = eth_dev->data;
338 parent_hw->back = parent_adapter;
339 parent_hw->mac_type = ICE_MAC_GENERIC;
340 parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
342 ice_init_lock(&parent_hw->adminq.sq_lock);
343 ice_init_lock(&parent_hw->adminq.rq_lock);
344 parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
345 parent_hw->aq_send_cmd_param = &adapter->real_hw;
346 parent_hw->dcf_enabled = true;
348 err = ice_dcf_init_parent_hw(parent_hw);
350 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
355 err = ice_dcf_load_pkg(parent_hw);
357 PMD_INIT_LOG(ERR, "failed to load package with error %d",
361 parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
363 err = ice_flow_init(parent_adapter);
365 PMD_INIT_LOG(ERR, "Failed to initialize flow");
369 ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
371 mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
372 if (rte_is_valid_assigned_ether_addr(mac))
373 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
375 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
377 eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
382 ice_dcf_uninit_parent_hw(parent_hw);
387 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
389 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
390 struct ice_adapter *parent_adapter = &adapter->parent;
391 struct ice_hw *parent_hw = &parent_adapter->hw;
393 eth_dev->data->mac_addrs = NULL;
395 ice_flow_uninit(parent_adapter);
396 ice_dcf_uninit_parent_hw(parent_hw);