net/ice: enable switch flow on DCF
[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 static __rte_always_inline void
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
23         if (unlikely(vsi_handle >= ICE_MAX_VSI)) {
24                 PMD_DRV_LOG(ERR, "Invalid vsi handle %u", vsi_handle);
25                 return;
26         }
27
28         vsi_ctx = hw->vsi_ctx[vsi_handle];
29
30         if (vsi_map & VIRTCHNL_DCF_VF_VSI_VALID) {
31                 if (!vsi_ctx) {
32                         vsi_ctx = ice_malloc(hw, sizeof(*vsi_ctx));
33                         if (!vsi_ctx) {
34                                 PMD_DRV_LOG(ERR, "No memory for vsi context %u",
35                                             vsi_handle);
36                                 return;
37                         }
38                 }
39
40                 vsi_ctx->vsi_num = (vsi_map & VIRTCHNL_DCF_VF_VSI_ID_M) >>
41                                               VIRTCHNL_DCF_VF_VSI_ID_S;
42                 hw->vsi_ctx[vsi_handle] = vsi_ctx;
43
44                 PMD_DRV_LOG(DEBUG, "VF%u is assigned with vsi number %u",
45                             vsi_handle, vsi_ctx->vsi_num);
46         } else {
47                 hw->vsi_ctx[vsi_handle] = NULL;
48
49                 ice_free(hw, vsi_ctx);
50
51                 PMD_DRV_LOG(NOTICE, "VF%u is disabled", vsi_handle);
52         }
53 }
54
55 static void
56 ice_dcf_update_vf_vsi_map(struct ice_hw *hw, uint16_t num_vfs,
57                           uint16_t *vf_vsi_map)
58 {
59         uint16_t vf_id;
60
61         for (vf_id = 0; vf_id < num_vfs; vf_id++)
62                 ice_dcf_update_vsi_ctx(hw, vf_id, vf_vsi_map[vf_id]);
63 }
64
65 static void*
66 ice_dcf_vsi_update_service_handler(void *param)
67 {
68         struct ice_dcf_hw *hw = param;
69
70         usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
71
72         rte_spinlock_lock(&vsi_update_lock);
73
74         if (!ice_dcf_handle_vsi_update_event(hw)) {
75                 struct ice_dcf_adapter *dcf_ad =
76                         container_of(hw, struct ice_dcf_adapter, real_hw);
77
78                 ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
79                                           hw->num_vfs, hw->vf_vsi_map);
80         }
81
82         rte_spinlock_unlock(&vsi_update_lock);
83
84         return NULL;
85 }
86
87 void
88 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
89                             uint8_t *msg, uint16_t msglen)
90 {
91         struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
92         pthread_t thread;
93
94         if (msglen < sizeof(struct virtchnl_pf_event)) {
95                 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
96                 return;
97         }
98
99         switch (pf_msg->event) {
100         case VIRTCHNL_EVENT_RESET_IMPENDING:
101                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
102                 pthread_create(&thread, NULL,
103                                ice_dcf_vsi_update_service_handler, dcf_hw);
104                 break;
105         case VIRTCHNL_EVENT_LINK_CHANGE:
106                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
107                 break;
108         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
109                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
110                 break;
111         case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
112                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
113                             pf_msg->event_data.vf_vsi_map.vf_id,
114                             pf_msg->event_data.vf_vsi_map.vsi_id);
115                 pthread_create(&thread, NULL,
116                                ice_dcf_vsi_update_service_handler, dcf_hw);
117                 break;
118         default:
119                 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
120                 break;
121         }
122 }
123
124 static int
125 ice_dcf_init_parent_hw(struct ice_hw *hw)
126 {
127         struct ice_aqc_get_phy_caps_data *pcaps;
128         enum ice_status status;
129
130         status = ice_aq_get_fw_ver(hw, NULL);
131         if (status)
132                 return status;
133
134         status = ice_get_caps(hw);
135         if (status)
136                 return status;
137
138         hw->port_info = (struct ice_port_info *)
139                         ice_malloc(hw, sizeof(*hw->port_info));
140         if (!hw->port_info)
141                 return ICE_ERR_NO_MEMORY;
142
143         /* set the back pointer to HW */
144         hw->port_info->hw = hw;
145
146         /* Initialize port_info struct with switch configuration data */
147         status = ice_get_initial_sw_cfg(hw);
148         if (status)
149                 goto err_unroll_alloc;
150
151         pcaps = (struct ice_aqc_get_phy_caps_data *)
152                 ice_malloc(hw, sizeof(*pcaps));
153         if (!pcaps) {
154                 status = ICE_ERR_NO_MEMORY;
155                 goto err_unroll_alloc;
156         }
157
158         /* Initialize port_info struct with PHY capabilities */
159         status = ice_aq_get_phy_caps(hw->port_info, false,
160                                      ICE_AQC_REPORT_TOPO_CAP, pcaps, NULL);
161         ice_free(hw, pcaps);
162         if (status)
163                 goto err_unroll_alloc;
164
165         /* Initialize port_info struct with link information */
166         status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
167         if (status)
168                 goto err_unroll_alloc;
169
170         status = ice_init_fltr_mgmt_struct(hw);
171         if (status)
172                 goto err_unroll_alloc;
173
174         status = ice_init_hw_tbls(hw);
175         if (status)
176                 goto err_unroll_fltr_mgmt_struct;
177
178         PMD_INIT_LOG(INFO,
179                      "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
180                      hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
181                      hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
182                      hw->fw_build);
183
184         return ICE_SUCCESS;
185
186 err_unroll_fltr_mgmt_struct:
187         ice_cleanup_fltr_mgmt_struct(hw);
188 err_unroll_alloc:
189         ice_free(hw, hw->port_info);
190         hw->port_info = NULL;
191
192         return status;
193 }
194
195 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
196 {
197         ice_cleanup_fltr_mgmt_struct(hw);
198
199         ice_free_seg(hw);
200         ice_free_hw_tbls(hw);
201
202         ice_free(hw, hw->port_info);
203         hw->port_info = NULL;
204
205         ice_clear_all_vsi_ctx(hw);
206 }
207
208 static int
209 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
210 {
211         struct ice_dcf_adapter *dcf_adapter =
212                         container_of(hw, struct ice_dcf_adapter, parent.hw);
213
214         /* TODO: check with DSN firstly by iAVF */
215         PMD_INIT_LOG(DEBUG,
216                      "DCF VSI_ID = %u",
217                      dcf_adapter->real_hw.vsi_id);
218
219         snprintf(pkg_name,
220                  ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
221         if (!access(pkg_name, 0))
222                 return 0;
223
224         snprintf(pkg_name,
225                  ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
226         if (!access(pkg_name, 0))
227                 return 0;
228
229         return -1;
230 }
231
232 static int
233 ice_dcf_load_pkg(struct ice_hw *hw)
234 {
235         char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
236         uint8_t *pkg_buf;
237         uint32_t buf_len;
238         struct stat st;
239         FILE *fp;
240         int err;
241
242         if (ice_dcf_request_pkg_name(hw, pkg_name)) {
243                 PMD_INIT_LOG(ERR, "Failed to locate the package file");
244                 return -ENOENT;
245         }
246
247         PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
248
249         err = stat(pkg_name, &st);
250         if (err) {
251                 PMD_INIT_LOG(ERR, "Failed to get file status");
252                 return err;
253         }
254
255         buf_len = st.st_size;
256         pkg_buf = rte_malloc(NULL, buf_len, 0);
257         if (!pkg_buf) {
258                 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
259                              buf_len);
260                 return -1;
261         }
262
263         fp = fopen(pkg_name, "rb");
264         if (!fp)  {
265                 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
266                 err = -1;
267                 goto ret;
268         }
269
270         err = fread(pkg_buf, buf_len, 1, fp);
271         fclose(fp);
272         if (err != 1) {
273                 PMD_INIT_LOG(ERR, "failed to read package data");
274                 err = -1;
275                 goto ret;
276         }
277
278         err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
279         if (err)
280                 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
281
282 ret:
283         rte_free(pkg_buf);
284         return err;
285 }
286
287 int
288 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
289 {
290         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
291         struct ice_adapter *parent_adapter = &adapter->parent;
292         struct ice_hw *parent_hw = &parent_adapter->hw;
293         struct ice_dcf_hw *hw = &adapter->real_hw;
294         const struct rte_ether_addr *mac;
295         int err;
296
297         parent_adapter->eth_dev = eth_dev;
298         parent_adapter->pf.adapter = parent_adapter;
299         parent_adapter->pf.dev_data = eth_dev->data;
300         parent_hw->back = parent_adapter;
301         parent_hw->mac_type = ICE_MAC_GENERIC;
302         parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
303
304         ice_init_lock(&parent_hw->adminq.sq_lock);
305         ice_init_lock(&parent_hw->adminq.rq_lock);
306         parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
307         parent_hw->aq_send_cmd_param = &adapter->real_hw;
308         parent_hw->dcf_enabled = true;
309
310         err = ice_dcf_init_parent_hw(parent_hw);
311         if (err) {
312                 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
313                              err);
314                 return err;
315         }
316
317         err = ice_dcf_load_pkg(parent_hw);
318         if (err) {
319                 PMD_INIT_LOG(ERR, "failed to load package with error %d",
320                              err);
321                 goto uninit_hw;
322         }
323         parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
324
325         err = ice_flow_init(parent_adapter);
326         if (err) {
327                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
328                 goto uninit_hw;
329         }
330
331         ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
332
333         mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
334         if (rte_is_valid_assigned_ether_addr(mac))
335                 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
336         else
337                 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
338
339         eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
340
341         return 0;
342
343 uninit_hw:
344         ice_dcf_uninit_parent_hw(parent_hw);
345         return err;
346 }
347
348 void
349 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
350 {
351         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
352         struct ice_adapter *parent_adapter = &adapter->parent;
353         struct ice_hw *parent_hw = &parent_adapter->hw;
354
355         eth_dev->data->mac_addrs = NULL;
356
357         ice_flow_uninit(parent_adapter);
358         ice_dcf_uninit_parent_hw(parent_hw);
359 }