xref: /f-stack/dpdk/drivers/net/ice/ice_dcf_parent.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <pthread.h>
7 #include <unistd.h>
8 
9 #include <rte_spinlock.h>
10 
11 #include "ice_dcf_ethdev.h"
12 #include "ice_generic_flow.h"
13 
14 #define ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL	100000 /* us */
15 static rte_spinlock_t vsi_update_lock = RTE_SPINLOCK_INITIALIZER;
16 
17 static __rte_always_inline void
ice_dcf_update_vsi_ctx(struct ice_hw * hw,uint16_t vsi_handle,uint16_t vsi_map)18 ice_dcf_update_vsi_ctx(struct ice_hw *hw, uint16_t vsi_handle,
19 		       uint16_t vsi_map)
20 {
21 	struct ice_vsi_ctx *vsi_ctx;
22 	bool first_update = false;
23 	uint16_t new_vsi_num;
24 
25 	if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
26 		PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
27 		return;
28 	}
29 
30 	vsi_ctx = hw->vsi_ctx[vsi_handle];
31 
32 	if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
33 		if (!vsi_ctx) {
34 			vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
35 			if (!vsi_ctx) {
36 				PMD_DRV_LOG(ERR, "No memory for vsi context %u",
37 					    vsi_handle);
38 				return;
39 			}
40 			hw->vsi_ctx[vsi_handle] = vsi_ctx;
41 			first_update = true;
42 		}
43 
44 		new_vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
45 			VIRTCHNL_DCF_VF_VSI_ID_S;
46 
47 		/* Redirect rules if vsi mapping table changes. */
48 		if (!first_update) {
49 			struct ice_flow_redirect rd;
50 
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);
56 		} else {
57 			vsi_ctx->vsi_num = new_vsi_num;
58 		}
59 
60 		PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
61 			    vsi_handle, vsi_ctx->vsi_num);
62 	} else {
63 		hw->vsi_ctx[vsi_handle] = NULL;
64 
65 		ice_free(hw, vsi_ctx);
66 
67 		PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
68 	}
69 }
70 
71 static void
ice_dcf_update_vf_vsi_map(struct ice_hw * hw,uint16_t num_vfs,uint16_t * vf_vsi_map)72 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
73 			  uint16_t *vf_vsi_map)
74 {
75 	uint16_t vf_id;
76 
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]);
79 }
80 
81 static void
ice_dcf_update_pf_vsi_map(struct ice_hw * hw,uint16_t pf_vsi_idx,uint16_t pf_vsi_num)82 ice_dcf_update_pf_vsi_map(struct ice_hw *hw, uint16_t pf_vsi_idx,
83 			uint16_t pf_vsi_num)
84 {
85 	struct ice_vsi_ctx *vsi_ctx;
86 
87 	if (unlikely(pf_vsi_idx >= ICE_MAX_VSI)) {
88 		PMD_DRV_LOG(ERR, "Invalid vsi handle %u", pf_vsi_idx);
89 		return;
90 	}
91 
92 	vsi_ctx = hw->vsi_ctx[pf_vsi_idx];
93 
94 	if (!vsi_ctx)
95 		vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
96 
97 	if (!vsi_ctx) {
98 		PMD_DRV_LOG(ERR, "No memory for vsi context %u",
99 				pf_vsi_idx);
100 		return;
101 	}
102 
103 	vsi_ctx->vsi_num = pf_vsi_num;
104 	hw->vsi_ctx[pf_vsi_idx] = vsi_ctx;
105 
106 	PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
107 			pf_vsi_idx, vsi_ctx->vsi_num);
108 }
109 
110 static void*
ice_dcf_vsi_update_service_handler(void * param)111 ice_dcf_vsi_update_service_handler(void *param)
112 {
113 	struct ice_dcf_hw *hw = param;
114 
115 	usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
116 
117 	rte_spinlock_lock(&vsi_update_lock);
118 
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);
122 
123 		ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
124 					  hw->num_vfs, hw->vf_vsi_map);
125 	}
126 
127 	rte_spinlock_unlock(&vsi_update_lock);
128 
129 	return NULL;
130 }
131 
132 void
ice_dcf_handle_pf_event_msg(struct ice_dcf_hw * dcf_hw,uint8_t * msg,uint16_t msglen)133 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
134 			    uint8_t *msg, uint16_t msglen)
135 {
136 	struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
137 	pthread_t thread;
138 
139 	if (msglen < sizeof(struct virtchnl_pf_event)) {
140 		PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
141 		return;
142 	}
143 
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);
149 		break;
150 	case VIRTCHNL_EVENT_LINK_CHANGE:
151 		PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
152 		break;
153 	case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
154 		PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
155 		break;
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);
162 		break;
163 	default:
164 		PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
165 		break;
166 	}
167 }
168 
169 static int
ice_dcf_init_parent_hw(struct ice_hw * hw)170 ice_dcf_init_parent_hw(struct ice_hw *hw)
171 {
172 	struct ice_aqc_get_phy_caps_data *pcaps;
173 	enum ice_status status;
174 
175 	status = ice_aq_get_fw_ver(hw, NULL);
176 	if (status)
177 		return status;
178 
179 	status = ice_get_caps(hw);
180 	if (status)
181 		return status;
182 
183 	hw->port_info = (struct ice_port_info *)
184 			ice_malloc(hw, sizeof(*hw->port_info));
185 	if (!hw->port_info)
186 		return ICE_ERR_NO_MEMORY;
187 
188 	/* set the back pointer to HW */
189 	hw->port_info->hw = hw;
190 
191 	/* Initialize port_info struct with switch configuration data */
192 	status = ice_get_initial_sw_cfg(hw);
193 	if (status)
194 		goto err_unroll_alloc;
195 
196 	pcaps = (struct ice_aqc_get_phy_caps_data *)
197 		ice_malloc(hw, sizeof(*pcaps));
198 	if (!pcaps) {
199 		status = ICE_ERR_NO_MEMORY;
200 		goto err_unroll_alloc;
201 	}
202 
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, pcaps, NULL);
206 	ice_free(hw, pcaps);
207 	if (status)
208 		goto err_unroll_alloc;
209 
210 	/* Initialize port_info struct with link information */
211 	status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
212 	if (status)
213 		goto err_unroll_alloc;
214 
215 	status = ice_init_fltr_mgmt_struct(hw);
216 	if (status)
217 		goto err_unroll_alloc;
218 
219 	status = ice_init_hw_tbls(hw);
220 	if (status)
221 		goto err_unroll_fltr_mgmt_struct;
222 
223 	PMD_INIT_LOG(INFO,
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,
227 		     hw->fw_build);
228 
229 	return ICE_SUCCESS;
230 
231 err_unroll_fltr_mgmt_struct:
232 	ice_cleanup_fltr_mgmt_struct(hw);
233 err_unroll_alloc:
234 	ice_free(hw, hw->port_info);
235 	hw->port_info = NULL;
236 
237 	return status;
238 }
239 
ice_dcf_uninit_parent_hw(struct ice_hw * hw)240 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
241 {
242 	ice_cleanup_fltr_mgmt_struct(hw);
243 
244 	ice_free_seg(hw);
245 	ice_free_hw_tbls(hw);
246 
247 	ice_free(hw, hw->port_info);
248 	hw->port_info = NULL;
249 
250 	ice_clear_all_vsi_ctx(hw);
251 }
252 
253 static int
ice_dcf_request_pkg_name(struct ice_hw * hw,char * pkg_name)254 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
255 {
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;
260 	uint64_t dsn;
261 
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;
267 
268 	if (ice_dcf_execute_virtchnl_cmd(&dcf_adapter->real_hw, &vc_cmd))
269 		goto pkg_file_direct;
270 
271 	rte_memcpy(&dsn, pkg_info.dsn, sizeof(dsn));
272 
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))
277 		return 0;
278 
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))
283 		return 0;
284 
285 pkg_file_direct:
286 	snprintf(pkg_name,
287 		 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
288 	if (!access(pkg_name, 0))
289 		return 0;
290 
291 	snprintf(pkg_name,
292 		 ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
293 	if (!access(pkg_name, 0))
294 		return 0;
295 
296 	return -1;
297 }
298 
299 static int
ice_dcf_load_pkg(struct ice_hw * hw)300 ice_dcf_load_pkg(struct ice_hw *hw)
301 {
302 	char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
303 	uint8_t *pkg_buf;
304 	uint32_t buf_len;
305 	struct stat st;
306 	FILE *fp;
307 	int err;
308 
309 	if (ice_dcf_request_pkg_name(hw, pkg_name)) {
310 		PMD_INIT_LOG(ERR, "Failed to locate the package file");
311 		return -ENOENT;
312 	}
313 
314 	PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
315 
316 	err = stat(pkg_name, &st);
317 	if (err) {
318 		PMD_INIT_LOG(ERR, "Failed to get file status");
319 		return err;
320 	}
321 
322 	buf_len = st.st_size;
323 	pkg_buf = rte_malloc(NULL, buf_len, 0);
324 	if (!pkg_buf) {
325 		PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
326 			     buf_len);
327 		return -1;
328 	}
329 
330 	fp = fopen(pkg_name, "rb");
331 	if (!fp)  {
332 		PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
333 		err = -1;
334 		goto ret;
335 	}
336 
337 	err = fread(pkg_buf, buf_len, 1, fp);
338 	fclose(fp);
339 	if (err != 1) {
340 		PMD_INIT_LOG(ERR, "failed to read package data");
341 		err = -1;
342 		goto ret;
343 	}
344 
345 	err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
346 	if (err)
347 		PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
348 
349 ret:
350 	rte_free(pkg_buf);
351 	return err;
352 }
353 
354 int
ice_dcf_init_parent_adapter(struct rte_eth_dev * eth_dev)355 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
356 {
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;
362 	int err;
363 
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)
371 		return -ENOMEM;
372 	parent_adapter->pf.main_vsi->adapter = parent_adapter;
373 	parent_adapter->pf.adapter_stopped = 1;
374 
375 	parent_hw->back = parent_adapter;
376 	parent_hw->mac_type = ICE_MAC_GENERIC;
377 	parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
378 
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;
384 
385 	err = ice_dcf_init_parent_hw(parent_hw);
386 	if (err) {
387 		PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
388 			     err);
389 		return err;
390 	}
391 
392 	err = ice_dcf_load_pkg(parent_hw);
393 	if (err) {
394 		PMD_INIT_LOG(ERR, "failed to load package with error %d",
395 			     err);
396 		goto uninit_hw;
397 	}
398 	parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
399 
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);
403 
404 	ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
405 
406 	err = ice_flow_init(parent_adapter);
407 	if (err) {
408 		PMD_INIT_LOG(ERR, "Failed to initialize flow");
409 		goto uninit_hw;
410 	}
411 
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);
415 	else
416 		rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
417 
418 	eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
419 
420 	return 0;
421 
422 uninit_hw:
423 	ice_dcf_uninit_parent_hw(parent_hw);
424 	return err;
425 }
426 
427 void
ice_dcf_uninit_parent_adapter(struct rte_eth_dev * eth_dev)428 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
429 {
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;
433 
434 	eth_dev->data->mac_addrs = NULL;
435 
436 	ice_flow_uninit(parent_adapter);
437 	ice_dcf_uninit_parent_hw(parent_hw);
438 }
439