c8e433239bf68a8516235e5cc3452f0215c38d5d
[dpdk.git] / drivers / net / ice / ice_dcf_parent.c
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 struct ice_dcf_reset_event_param {
18         struct ice_dcf_hw *dcf_hw;
19
20         bool vfr; /* VF reset event */
21         uint16_t vf_id; /* The reset VF ID */
22 };
23
24 static __rte_always_inline void
25 ice_dcf_update_vsi_ctx(struct ice_hw *hw, uint16_t vsi_handle,
26                        uint16_t vsi_map)
27 {
28         struct ice_vsi_ctx *vsi_ctx;
29         bool first_update = false;
30         uint16_t new_vsi_num;
31
32         if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
33                 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
34                 return;
35         }
36
37         vsi_ctx = hw->vsi_ctx[vsi_handle];
38
39         if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
40                 if (!vsi_ctx) {
41                         vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
42                         if (!vsi_ctx) {
43                                 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
44                                             vsi_handle);
45                                 return;
46                         }
47                         hw->vsi_ctx[vsi_handle] = vsi_ctx;
48                         first_update = true;
49                 }
50
51                 new_vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
52                         VIRTCHNL_DCF_VF_VSI_ID_S;
53
54                 /* Redirect rules if vsi mapping table changes. */
55                 if (!first_update) {
56                         struct ice_flow_redirect rd;
57
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);
63                 } else {
64                         vsi_ctx->vsi_num = new_vsi_num;
65                 }
66
67                 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
68                             vsi_handle, vsi_ctx->vsi_num);
69         } else {
70                 hw->vsi_ctx[vsi_handle] = NULL;
71
72                 ice_free(hw, vsi_ctx);
73
74                 PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
75         }
76 }
77
78 static void
79 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
80                           uint16_t *vf_vsi_map)
81 {
82         uint16_t vf_id;
83
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]);
86 }
87
88 static void
89 ice_dcf_update_pf_vsi_map(struct ice_hw *hw, uint16_t pf_vsi_idx,
90                         uint16_t pf_vsi_num)
91 {
92         struct ice_vsi_ctx *vsi_ctx;
93
94         if (unlikely(pf_vsi_idx >= ICE_MAX_VSI)) {
95                 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", pf_vsi_idx);
96                 return;
97         }
98
99         vsi_ctx = hw->vsi_ctx[pf_vsi_idx];
100
101         if (!vsi_ctx)
102                 vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
103
104         if (!vsi_ctx) {
105                 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
106                                 pf_vsi_idx);
107                 return;
108         }
109
110         vsi_ctx->vsi_num = pf_vsi_num;
111         hw->vsi_ctx[pf_vsi_idx] = vsi_ctx;
112
113         PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
114                         pf_vsi_idx, vsi_ctx->vsi_num);
115 }
116
117 static void*
118 ice_dcf_vsi_update_service_handler(void *param)
119 {
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;
123
124         rte_delay_us(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
125
126         rte_spinlock_lock(&vsi_update_lock);
127
128         adapter = container_of(hw, struct ice_dcf_adapter, real_hw);
129
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);
133
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",
139                                     reset_param->vf_id);
140                         ice_dcf_vf_repr_init_vlan(vf_rep_eth_dev);
141                 }
142         }
143
144         rte_spinlock_unlock(&vsi_update_lock);
145
146         free(param);
147
148         return NULL;
149 }
150
151 static void
152 start_vsi_reset_thread(struct ice_dcf_hw *dcf_hw, bool vfr, uint16_t vf_id)
153 {
154 #define THREAD_NAME_LEN 16
155         struct ice_dcf_reset_event_param *param;
156         char name[THREAD_NAME_LEN];
157         pthread_t thread;
158         int ret;
159
160         param = malloc(sizeof(*param));
161         if (!param) {
162                 PMD_DRV_LOG(ERR, "Failed to allocate the memory for reset handling");
163                 return;
164         }
165
166         param->dcf_hw = dcf_hw;
167         param->vfr = vfr;
168         param->vf_id = vf_id;
169
170         snprintf(name, sizeof(name), "ice-reset-%u", vf_id);
171         ret = rte_ctrl_thread_create(&thread, name, NULL,
172                                      ice_dcf_vsi_update_service_handler, param);
173         if (ret != 0) {
174                 PMD_DRV_LOG(ERR, "Failed to start the thread for reset handling");
175                 free(param);
176         }
177 }
178
179 void
180 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
181                             uint8_t *msg, uint16_t msglen)
182 {
183         struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
184
185         if (msglen < sizeof(struct virtchnl_pf_event)) {
186                 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
187                 return;
188         }
189
190         switch (pf_msg->event) {
191         case VIRTCHNL_EVENT_RESET_IMPENDING:
192                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
193                 start_vsi_reset_thread(dcf_hw, false, 0);
194                 break;
195         case VIRTCHNL_EVENT_LINK_CHANGE:
196                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
197                 break;
198         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
199                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
200                 break;
201         case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
202                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
203                             pf_msg->event_data.vf_vsi_map.vf_id,
204                             pf_msg->event_data.vf_vsi_map.vsi_id);
205                 start_vsi_reset_thread(dcf_hw, true,
206                                        pf_msg->event_data.vf_vsi_map.vf_id);
207                 break;
208         default:
209                 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
210                 break;
211         }
212 }
213
214 static int
215 ice_dcf_init_parent_hw(struct ice_hw *hw)
216 {
217         struct ice_aqc_get_phy_caps_data *pcaps;
218         enum ice_status status;
219
220         status = ice_aq_get_fw_ver(hw, NULL);
221         if (status)
222                 return status;
223
224         status = ice_get_caps(hw);
225         if (status)
226                 return status;
227
228         hw->port_info = (struct ice_port_info *)
229                         ice_malloc(hw, sizeof(*hw->port_info));
230         if (!hw->port_info)
231                 return ICE_ERR_NO_MEMORY;
232
233         /* set the back pointer to HW */
234         hw->port_info->hw = hw;
235
236         /* Initialize port_info struct with switch configuration data */
237         status = ice_get_initial_sw_cfg(hw);
238         if (status)
239                 goto err_unroll_alloc;
240
241         pcaps = (struct ice_aqc_get_phy_caps_data *)
242                 ice_malloc(hw, sizeof(*pcaps));
243         if (!pcaps) {
244                 status = ICE_ERR_NO_MEMORY;
245                 goto err_unroll_alloc;
246         }
247
248         /* Initialize port_info struct with PHY capabilities */
249         status = ice_aq_get_phy_caps(hw->port_info, false,
250                                      ICE_AQC_REPORT_TOPO_CAP_MEDIA, pcaps, NULL);
251         ice_free(hw, pcaps);
252         if (status)
253                 goto err_unroll_alloc;
254
255         /* Initialize port_info struct with link information */
256         status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
257         if (status)
258                 goto err_unroll_alloc;
259
260         status = ice_init_fltr_mgmt_struct(hw);
261         if (status)
262                 goto err_unroll_alloc;
263
264         status = ice_init_hw_tbls(hw);
265         if (status)
266                 goto err_unroll_fltr_mgmt_struct;
267
268         PMD_INIT_LOG(INFO,
269                      "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
270                      hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
271                      hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
272                      hw->fw_build);
273
274         return ICE_SUCCESS;
275
276 err_unroll_fltr_mgmt_struct:
277         ice_cleanup_fltr_mgmt_struct(hw);
278 err_unroll_alloc:
279         ice_free(hw, hw->port_info);
280         hw->port_info = NULL;
281
282         return status;
283 }
284
285 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
286 {
287         ice_cleanup_fltr_mgmt_struct(hw);
288
289         ice_free_seg(hw);
290         ice_free_hw_tbls(hw);
291
292         ice_free(hw, hw->port_info);
293         hw->port_info = NULL;
294
295         ice_clear_all_vsi_ctx(hw);
296 }
297
298 static int
299 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
300 {
301         struct ice_dcf_adapter *dcf_adapter =
302                         container_of(hw, struct ice_dcf_adapter, parent.hw);
303         struct virtchnl_pkg_info pkg_info;
304         struct dcf_virtchnl_cmd vc_cmd;
305         uint64_t dsn;
306
307         vc_cmd.v_op = VIRTCHNL_OP_DCF_GET_PKG_INFO;
308         vc_cmd.req_msglen = 0;
309         vc_cmd.req_msg = NULL;
310         vc_cmd.rsp_buflen = sizeof(pkg_info);
311         vc_cmd.rsp_msgbuf = (uint8_t *)&pkg_info;
312
313         if (ice_dcf_execute_virtchnl_cmd(&dcf_adapter->real_hw, &vc_cmd))
314                 goto pkg_file_direct;
315
316         rte_memcpy(&dsn, pkg_info.dsn, sizeof(dsn));
317
318         snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
319                  ICE_PKG_FILE_SEARCH_PATH_UPDATES "ice-%016llx.pkg",
320                  (unsigned long long)dsn);
321         if (!ice_access(pkg_name, 0))
322                 return 0;
323
324         snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
325                  ICE_PKG_FILE_SEARCH_PATH_DEFAULT "ice-%016llx.pkg",
326                  (unsigned long long)dsn);
327         if (!ice_access(pkg_name, 0))
328                 return 0;
329
330 pkg_file_direct:
331         snprintf(pkg_name,
332                  ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
333         if (!ice_access(pkg_name, 0))
334                 return 0;
335
336         snprintf(pkg_name,
337                  ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
338         if (!ice_access(pkg_name, 0))
339                 return 0;
340
341         return -1;
342 }
343
344 static int
345 ice_dcf_load_pkg(struct ice_hw *hw)
346 {
347         char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
348         uint8_t *pkg_buf;
349         uint32_t buf_len;
350         struct stat st;
351         FILE *fp;
352         int err;
353
354         if (ice_dcf_request_pkg_name(hw, pkg_name)) {
355                 PMD_INIT_LOG(ERR, "Failed to locate the package file");
356                 return -ENOENT;
357         }
358
359         PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
360
361         err = stat(pkg_name, &st);
362         if (err) {
363                 PMD_INIT_LOG(ERR, "Failed to get file status");
364                 return err;
365         }
366
367         buf_len = st.st_size;
368         pkg_buf = rte_malloc(NULL, buf_len, 0);
369         if (!pkg_buf) {
370                 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
371                              buf_len);
372                 return -1;
373         }
374
375         fp = fopen(pkg_name, "rb");
376         if (!fp)  {
377                 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
378                 err = -1;
379                 goto ret;
380         }
381
382         err = fread(pkg_buf, buf_len, 1, fp);
383         fclose(fp);
384         if (err != 1) {
385                 PMD_INIT_LOG(ERR, "failed to read package data");
386                 err = -1;
387                 goto ret;
388         }
389
390         err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
391         if (err)
392                 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
393
394 ret:
395         rte_free(pkg_buf);
396         return err;
397 }
398
399 int
400 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
401 {
402         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
403         struct ice_adapter *parent_adapter = &adapter->parent;
404         struct ice_hw *parent_hw = &parent_adapter->hw;
405         struct ice_dcf_hw *hw = &adapter->real_hw;
406         const struct rte_ether_addr *mac;
407         int err;
408
409         parent_adapter->eth_dev = eth_dev;
410         parent_adapter->pf.adapter = parent_adapter;
411         parent_adapter->pf.dev_data = eth_dev->data;
412         /* create a dummy main_vsi */
413         parent_adapter->pf.main_vsi =
414                 rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
415         if (!parent_adapter->pf.main_vsi)
416                 return -ENOMEM;
417         parent_adapter->pf.main_vsi->adapter = parent_adapter;
418         parent_adapter->pf.adapter_stopped = 1;
419
420         parent_hw->back = parent_adapter;
421         parent_hw->mac_type = ICE_MAC_GENERIC;
422         parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
423
424         ice_init_lock(&parent_hw->adminq.sq_lock);
425         ice_init_lock(&parent_hw->adminq.rq_lock);
426         parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
427         parent_hw->aq_send_cmd_param = &adapter->real_hw;
428         parent_hw->dcf_enabled = true;
429
430         err = ice_dcf_init_parent_hw(parent_hw);
431         if (err) {
432                 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
433                              err);
434                 return err;
435         }
436
437         err = ice_dcf_load_pkg(parent_hw);
438         if (err) {
439                 PMD_INIT_LOG(ERR, "failed to load package with error %d",
440                              err);
441                 goto uninit_hw;
442         }
443         parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
444
445         parent_adapter->pf.main_vsi->idx = hw->num_vfs;
446         ice_dcf_update_pf_vsi_map(parent_hw,
447                         parent_adapter->pf.main_vsi->idx, hw->pf_vsi_id);
448
449         ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
450
451         err = ice_flow_init(parent_adapter);
452         if (err) {
453                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
454                 goto uninit_hw;
455         }
456
457         mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
458         if (rte_is_valid_assigned_ether_addr(mac))
459                 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
460         else
461                 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
462
463         eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
464
465         return 0;
466
467 uninit_hw:
468         ice_dcf_uninit_parent_hw(parent_hw);
469         return err;
470 }
471
472 void
473 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
474 {
475         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
476         struct ice_adapter *parent_adapter = &adapter->parent;
477         struct ice_hw *parent_hw = &parent_adapter->hw;
478
479         eth_dev->data->mac_addrs = NULL;
480
481         ice_flow_uninit(parent_adapter);
482         ice_dcf_uninit_parent_hw(parent_hw);
483 }