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 struct ice_dcf_reset_event_param {
18 struct ice_dcf_hw *dcf_hw;
20 bool vfr; /* VF reset event */
21 uint16_t vf_id; /* The reset VF ID */
24 static __rte_always_inline void
25 ice_dcf_update_vsi_ctx(struct ice_hw *hw, uint16_t vsi_handle,
28 struct ice_vsi_ctx *vsi_ctx;
29 bool first_update = false;
32 if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
33 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
37 vsi_ctx = hw->vsi_ctx[vsi_handle];
39 if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
41 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
43 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
47 hw->vsi_ctx[vsi_handle] = vsi_ctx;
51 new_vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
52 VIRTCHNL_DCF_VF_VSI_ID_S;
54 /* Redirect rules if vsi mapping table changes. */
56 struct ice_flow_redirect rd;
58 memset(&rd, 0, sizeof(struct ice_flow_redirect));
59 rd.type = ICE_FLOW_REDIRECT_VSI;
60 rd.vsi_handle = vsi_handle;
61 rd.new_vsi_num = new_vsi_num;
62 ice_flow_redirect((struct ice_adapter *)hw->back, &rd);
64 vsi_ctx->vsi_num = new_vsi_num;
67 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
68 vsi_handle, vsi_ctx->vsi_num);
70 hw->vsi_ctx[vsi_handle] = NULL;
72 ice_free(hw, vsi_ctx);
74 PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
79 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
84 for (vf_id = 0; vf_id < num_vfs; vf_id++)
85 ice_dcf_update_vsi_ctx(hw, vf_id, vf_vsi_map[vf_id]);
89 ice_dcf_update_pf_vsi_map(struct ice_hw *hw, uint16_t pf_vsi_idx,
92 struct ice_vsi_ctx *vsi_ctx;
94 if (unlikely(pf_vsi_idx >= ICE_MAX_VSI)) {
95 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", pf_vsi_idx);
99 vsi_ctx = hw->vsi_ctx[pf_vsi_idx];
102 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
105 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
110 vsi_ctx->vsi_num = pf_vsi_num;
111 hw->vsi_ctx[pf_vsi_idx] = vsi_ctx;
113 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
114 pf_vsi_idx, vsi_ctx->vsi_num);
118 ice_dcf_vsi_update_service_handler(void *param)
120 struct ice_dcf_reset_event_param *reset_param = param;
121 struct ice_dcf_hw *hw = reset_param->dcf_hw;
122 struct ice_dcf_adapter *adapter;
124 usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
126 rte_spinlock_lock(&vsi_update_lock);
128 adapter = container_of(hw, struct ice_dcf_adapter, real_hw);
130 if (!ice_dcf_handle_vsi_update_event(hw))
131 ice_dcf_update_vf_vsi_map(&adapter->parent.hw,
132 hw->num_vfs, hw->vf_vsi_map);
134 if (reset_param->vfr && adapter->repr_infos) {
135 struct rte_eth_dev *vf_rep_eth_dev =
136 adapter->repr_infos[reset_param->vf_id].vf_rep_eth_dev;
137 if (vf_rep_eth_dev && vf_rep_eth_dev->data->dev_started) {
138 PMD_DRV_LOG(DEBUG, "VF%u representor is resetting",
140 ice_dcf_vf_repr_init_vlan(vf_rep_eth_dev);
144 rte_spinlock_unlock(&vsi_update_lock);
152 start_vsi_reset_thread(struct ice_dcf_hw *dcf_hw, bool vfr, uint16_t vf_id)
154 struct ice_dcf_reset_event_param *param;
158 param = malloc(sizeof(*param));
160 PMD_DRV_LOG(ERR, "Failed to allocate the memory for reset handling");
164 param->dcf_hw = dcf_hw;
166 param->vf_id = vf_id;
168 ret = pthread_create(&thread, NULL,
169 ice_dcf_vsi_update_service_handler, param);
171 PMD_DRV_LOG(ERR, "Failed to start the thread for reset handling");
177 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
178 uint8_t *msg, uint16_t msglen)
180 struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
182 if (msglen < sizeof(struct virtchnl_pf_event)) {
183 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
187 switch (pf_msg->event) {
188 case VIRTCHNL_EVENT_RESET_IMPENDING:
189 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
190 start_vsi_reset_thread(dcf_hw, false, 0);
192 case VIRTCHNL_EVENT_LINK_CHANGE:
193 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
195 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
196 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
198 case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
199 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
200 pf_msg->event_data.vf_vsi_map.vf_id,
201 pf_msg->event_data.vf_vsi_map.vsi_id);
202 start_vsi_reset_thread(dcf_hw, true,
203 pf_msg->event_data.vf_vsi_map.vf_id);
206 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
212 ice_dcf_init_parent_hw(struct ice_hw *hw)
214 struct ice_aqc_get_phy_caps_data *pcaps;
215 enum ice_status status;
217 status = ice_aq_get_fw_ver(hw, NULL);
221 status = ice_get_caps(hw);
225 hw->port_info = (struct ice_port_info *)
226 ice_malloc(hw, sizeof(*hw->port_info));
228 return ICE_ERR_NO_MEMORY;
230 /* set the back pointer to HW */
231 hw->port_info->hw = hw;
233 /* Initialize port_info struct with switch configuration data */
234 status = ice_get_initial_sw_cfg(hw);
236 goto err_unroll_alloc;
238 pcaps = (struct ice_aqc_get_phy_caps_data *)
239 ice_malloc(hw, sizeof(*pcaps));
241 status = ICE_ERR_NO_MEMORY;
242 goto err_unroll_alloc;
245 /* Initialize port_info struct with PHY capabilities */
246 status = ice_aq_get_phy_caps(hw->port_info, false,
247 ICE_AQC_REPORT_TOPO_CAP_MEDIA, pcaps, NULL);
250 goto err_unroll_alloc;
252 /* Initialize port_info struct with link information */
253 status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
255 goto err_unroll_alloc;
257 status = ice_init_fltr_mgmt_struct(hw);
259 goto err_unroll_alloc;
261 status = ice_init_hw_tbls(hw);
263 goto err_unroll_fltr_mgmt_struct;
266 "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
267 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
268 hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
273 err_unroll_fltr_mgmt_struct:
274 ice_cleanup_fltr_mgmt_struct(hw);
276 ice_free(hw, hw->port_info);
277 hw->port_info = NULL;
282 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
284 ice_cleanup_fltr_mgmt_struct(hw);
287 ice_free_hw_tbls(hw);
289 ice_free(hw, hw->port_info);
290 hw->port_info = NULL;
292 ice_clear_all_vsi_ctx(hw);
296 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
298 struct ice_dcf_adapter *dcf_adapter =
299 container_of(hw, struct ice_dcf_adapter, parent.hw);
300 struct virtchnl_pkg_info pkg_info;
301 struct dcf_virtchnl_cmd vc_cmd;
304 vc_cmd.v_op = VIRTCHNL_OP_DCF_GET_PKG_INFO;
305 vc_cmd.req_msglen = 0;
306 vc_cmd.req_msg = NULL;
307 vc_cmd.rsp_buflen = sizeof(pkg_info);
308 vc_cmd.rsp_msgbuf = (uint8_t *)&pkg_info;
310 if (ice_dcf_execute_virtchnl_cmd(&dcf_adapter->real_hw, &vc_cmd))
311 goto pkg_file_direct;
313 rte_memcpy(&dsn, pkg_info.dsn, sizeof(dsn));
315 snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
316 ICE_PKG_FILE_SEARCH_PATH_UPDATES "ice-%016llx.pkg",
317 (unsigned long long)dsn);
318 if (!access(pkg_name, 0))
321 snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
322 ICE_PKG_FILE_SEARCH_PATH_DEFAULT "ice-%016llx.pkg",
323 (unsigned long long)dsn);
324 if (!access(pkg_name, 0))
329 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
330 if (!access(pkg_name, 0))
334 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
335 if (!access(pkg_name, 0))
342 ice_dcf_load_pkg(struct ice_hw *hw)
344 char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
351 if (ice_dcf_request_pkg_name(hw, pkg_name)) {
352 PMD_INIT_LOG(ERR, "Failed to locate the package file");
356 PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
358 err = stat(pkg_name, &st);
360 PMD_INIT_LOG(ERR, "Failed to get file status");
364 buf_len = st.st_size;
365 pkg_buf = rte_malloc(NULL, buf_len, 0);
367 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
372 fp = fopen(pkg_name, "rb");
374 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
379 err = fread(pkg_buf, buf_len, 1, fp);
382 PMD_INIT_LOG(ERR, "failed to read package data");
387 err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
389 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
397 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
399 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
400 struct ice_adapter *parent_adapter = &adapter->parent;
401 struct ice_hw *parent_hw = &parent_adapter->hw;
402 struct ice_dcf_hw *hw = &adapter->real_hw;
403 const struct rte_ether_addr *mac;
406 parent_adapter->eth_dev = eth_dev;
407 parent_adapter->pf.adapter = parent_adapter;
408 parent_adapter->pf.dev_data = eth_dev->data;
409 /* create a dummy main_vsi */
410 parent_adapter->pf.main_vsi =
411 rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
412 if (!parent_adapter->pf.main_vsi)
414 parent_adapter->pf.main_vsi->adapter = parent_adapter;
415 parent_adapter->pf.adapter_stopped = 1;
417 parent_hw->back = parent_adapter;
418 parent_hw->mac_type = ICE_MAC_GENERIC;
419 parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
421 ice_init_lock(&parent_hw->adminq.sq_lock);
422 ice_init_lock(&parent_hw->adminq.rq_lock);
423 parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
424 parent_hw->aq_send_cmd_param = &adapter->real_hw;
425 parent_hw->dcf_enabled = true;
427 err = ice_dcf_init_parent_hw(parent_hw);
429 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
434 err = ice_dcf_load_pkg(parent_hw);
436 PMD_INIT_LOG(ERR, "failed to load package with error %d",
440 parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
442 parent_adapter->pf.main_vsi->idx = hw->num_vfs;
443 ice_dcf_update_pf_vsi_map(parent_hw,
444 parent_adapter->pf.main_vsi->idx, hw->pf_vsi_id);
446 ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
448 err = ice_flow_init(parent_adapter);
450 PMD_INIT_LOG(ERR, "Failed to initialize flow");
454 mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
455 if (rte_is_valid_assigned_ether_addr(mac))
456 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
458 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
460 eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
465 ice_dcf_uninit_parent_hw(parent_hw);
470 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
472 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
473 struct ice_adapter *parent_adapter = &adapter->parent;
474 struct ice_hw *parent_hw = &parent_adapter->hw;
476 eth_dev->data->mac_addrs = NULL;
478 ice_flow_uninit(parent_adapter);
479 ice_dcf_uninit_parent_hw(parent_hw);