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_update_pf_vsi_map(struct ice_hw *hw, uint16_t pf_vsi_idx,
85 struct ice_vsi_ctx *vsi_ctx;
87 if (unlikely(pf_vsi_idx >= ICE_MAX_VSI)) {
88 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", pf_vsi_idx);
92 vsi_ctx = hw->vsi_ctx[pf_vsi_idx];
95 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
98 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
103 vsi_ctx->vsi_num = pf_vsi_num;
104 hw->vsi_ctx[pf_vsi_idx] = vsi_ctx;
106 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
107 pf_vsi_idx, vsi_ctx->vsi_num);
111 ice_dcf_vsi_update_service_handler(void *param)
113 struct ice_dcf_hw *hw = param;
115 usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
117 rte_spinlock_lock(&vsi_update_lock);
119 if (!ice_dcf_handle_vsi_update_event(hw)) {
120 struct ice_dcf_adapter *dcf_ad =
121 container_of(hw, struct ice_dcf_adapter, real_hw);
123 ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
124 hw->num_vfs, hw->vf_vsi_map);
127 rte_spinlock_unlock(&vsi_update_lock);
133 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
134 uint8_t *msg, uint16_t msglen)
136 struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
139 if (msglen < sizeof(struct virtchnl_pf_event)) {
140 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
144 switch (pf_msg->event) {
145 case VIRTCHNL_EVENT_RESET_IMPENDING:
146 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
147 pthread_create(&thread, NULL,
148 ice_dcf_vsi_update_service_handler, dcf_hw);
150 case VIRTCHNL_EVENT_LINK_CHANGE:
151 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
153 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
154 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
156 case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
157 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
158 pf_msg->event_data.vf_vsi_map.vf_id,
159 pf_msg->event_data.vf_vsi_map.vsi_id);
160 pthread_create(&thread, NULL,
161 ice_dcf_vsi_update_service_handler, dcf_hw);
164 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
170 ice_dcf_init_parent_hw(struct ice_hw *hw)
172 struct ice_aqc_get_phy_caps_data *pcaps;
173 enum ice_status status;
175 status = ice_aq_get_fw_ver(hw, NULL);
179 status = ice_get_caps(hw);
183 hw->port_info = (struct ice_port_info *)
184 ice_malloc(hw, sizeof(*hw->port_info));
186 return ICE_ERR_NO_MEMORY;
188 /* set the back pointer to HW */
189 hw->port_info->hw = hw;
191 /* Initialize port_info struct with switch configuration data */
192 status = ice_get_initial_sw_cfg(hw);
194 goto err_unroll_alloc;
196 pcaps = (struct ice_aqc_get_phy_caps_data *)
197 ice_malloc(hw, sizeof(*pcaps));
199 status = ICE_ERR_NO_MEMORY;
200 goto err_unroll_alloc;
203 /* Initialize port_info struct with PHY capabilities */
204 status = ice_aq_get_phy_caps(hw->port_info, false,
205 ICE_AQC_REPORT_TOPO_CAP_MEDIA, pcaps, NULL);
208 goto err_unroll_alloc;
210 /* Initialize port_info struct with link information */
211 status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
213 goto err_unroll_alloc;
215 status = ice_init_fltr_mgmt_struct(hw);
217 goto err_unroll_alloc;
219 status = ice_init_hw_tbls(hw);
221 goto err_unroll_fltr_mgmt_struct;
224 "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
225 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
226 hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
231 err_unroll_fltr_mgmt_struct:
232 ice_cleanup_fltr_mgmt_struct(hw);
234 ice_free(hw, hw->port_info);
235 hw->port_info = NULL;
240 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
242 ice_cleanup_fltr_mgmt_struct(hw);
245 ice_free_hw_tbls(hw);
247 ice_free(hw, hw->port_info);
248 hw->port_info = NULL;
250 ice_clear_all_vsi_ctx(hw);
254 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
256 struct ice_dcf_adapter *dcf_adapter =
257 container_of(hw, struct ice_dcf_adapter, parent.hw);
258 struct virtchnl_pkg_info pkg_info;
259 struct dcf_virtchnl_cmd vc_cmd;
262 vc_cmd.v_op = VIRTCHNL_OP_DCF_GET_PKG_INFO;
263 vc_cmd.req_msglen = 0;
264 vc_cmd.req_msg = NULL;
265 vc_cmd.rsp_buflen = sizeof(pkg_info);
266 vc_cmd.rsp_msgbuf = (uint8_t *)&pkg_info;
268 if (ice_dcf_execute_virtchnl_cmd(&dcf_adapter->real_hw, &vc_cmd))
269 goto pkg_file_direct;
271 rte_memcpy(&dsn, pkg_info.dsn, sizeof(dsn));
273 snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
274 ICE_PKG_FILE_SEARCH_PATH_UPDATES "ice-%016llx.pkg",
275 (unsigned long long)dsn);
276 if (!access(pkg_name, 0))
279 snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
280 ICE_PKG_FILE_SEARCH_PATH_DEFAULT "ice-%016llx.pkg",
281 (unsigned long long)dsn);
282 if (!access(pkg_name, 0))
287 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
288 if (!access(pkg_name, 0))
292 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
293 if (!access(pkg_name, 0))
300 ice_dcf_load_pkg(struct ice_hw *hw)
302 char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
309 if (ice_dcf_request_pkg_name(hw, pkg_name)) {
310 PMD_INIT_LOG(ERR, "Failed to locate the package file");
314 PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
316 err = stat(pkg_name, &st);
318 PMD_INIT_LOG(ERR, "Failed to get file status");
322 buf_len = st.st_size;
323 pkg_buf = rte_malloc(NULL, buf_len, 0);
325 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
330 fp = fopen(pkg_name, "rb");
332 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
337 err = fread(pkg_buf, buf_len, 1, fp);
340 PMD_INIT_LOG(ERR, "failed to read package data");
345 err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
347 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
355 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
357 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
358 struct ice_adapter *parent_adapter = &adapter->parent;
359 struct ice_hw *parent_hw = &parent_adapter->hw;
360 struct ice_dcf_hw *hw = &adapter->real_hw;
361 const struct rte_ether_addr *mac;
364 parent_adapter->eth_dev = eth_dev;
365 parent_adapter->pf.adapter = parent_adapter;
366 parent_adapter->pf.dev_data = eth_dev->data;
367 /* create a dummy main_vsi */
368 parent_adapter->pf.main_vsi =
369 rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
370 if (!parent_adapter->pf.main_vsi)
372 parent_adapter->pf.main_vsi->adapter = parent_adapter;
373 parent_adapter->pf.adapter_stopped = 1;
375 parent_hw->back = parent_adapter;
376 parent_hw->mac_type = ICE_MAC_GENERIC;
377 parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
379 ice_init_lock(&parent_hw->adminq.sq_lock);
380 ice_init_lock(&parent_hw->adminq.rq_lock);
381 parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
382 parent_hw->aq_send_cmd_param = &adapter->real_hw;
383 parent_hw->dcf_enabled = true;
385 err = ice_dcf_init_parent_hw(parent_hw);
387 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
392 err = ice_dcf_load_pkg(parent_hw);
394 PMD_INIT_LOG(ERR, "failed to load package with error %d",
398 parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
400 parent_adapter->pf.main_vsi->idx = hw->num_vfs;
401 ice_dcf_update_pf_vsi_map(parent_hw,
402 parent_adapter->pf.main_vsi->idx, hw->pf_vsi_id);
404 ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
406 err = ice_flow_init(parent_adapter);
408 PMD_INIT_LOG(ERR, "Failed to initialize flow");
412 mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
413 if (rte_is_valid_assigned_ether_addr(mac))
414 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
416 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
418 eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
423 ice_dcf_uninit_parent_hw(parent_hw);
428 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
430 struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
431 struct ice_adapter *parent_adapter = &adapter->parent;
432 struct ice_hw *parent_hw = &parent_adapter->hw;
434 eth_dev->data->mac_addrs = NULL;
436 ice_flow_uninit(parent_adapter);
437 ice_dcf_uninit_parent_hw(parent_hw);