vdpa/mlx5: support queue update
[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         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
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*
82 ice_dcf_vsi_update_service_handler(void *param)
83 {
84         struct ice_dcf_hw *hw = param;
85
86         usleep(ICE_DCF_VSI_UPDATE_SERVICE_INTERVAL);
87
88         rte_spinlock_lock(&vsi_update_lock);
89
90         if (!ice_dcf_handle_vsi_update_event(hw)) {
91                 struct ice_dcf_adapter *dcf_ad =
92                         container_of(hw, struct ice_dcf_adapter, real_hw);
93
94                 ice_dcf_update_vf_vsi_map(&dcf_ad->parent.hw,
95                                           hw->num_vfs, hw->vf_vsi_map);
96         }
97
98         rte_spinlock_unlock(&vsi_update_lock);
99
100         return NULL;
101 }
102
103 void
104 ice_dcf_handle_pf_event_msg(struct ice_dcf_hw *dcf_hw,
105                             uint8_t *msg, uint16_t msglen)
106 {
107         struct virtchnl_pf_event *pf_msg = (struct virtchnl_pf_event *)msg;
108         pthread_t thread;
109
110         if (msglen < sizeof(struct virtchnl_pf_event)) {
111                 PMD_DRV_LOG(DEBUG, "Invalid event message length : %u", msglen);
112                 return;
113         }
114
115         switch (pf_msg->event) {
116         case VIRTCHNL_EVENT_RESET_IMPENDING:
117                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
118                 pthread_create(&thread, NULL,
119                                ice_dcf_vsi_update_service_handler, dcf_hw);
120                 break;
121         case VIRTCHNL_EVENT_LINK_CHANGE:
122                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
123                 break;
124         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
125                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
126                 break;
127         case VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE:
128                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_DCF_VSI_MAP_UPDATE event : VF%u with VSI num %u",
129                             pf_msg->event_data.vf_vsi_map.vf_id,
130                             pf_msg->event_data.vf_vsi_map.vsi_id);
131                 pthread_create(&thread, NULL,
132                                ice_dcf_vsi_update_service_handler, dcf_hw);
133                 break;
134         default:
135                 PMD_DRV_LOG(ERR, "Unknown event received %u", pf_msg->event);
136                 break;
137         }
138 }
139
140 static int
141 ice_dcf_init_parent_hw(struct ice_hw *hw)
142 {
143         struct ice_aqc_get_phy_caps_data *pcaps;
144         enum ice_status status;
145
146         status = ice_aq_get_fw_ver(hw, NULL);
147         if (status)
148                 return status;
149
150         status = ice_get_caps(hw);
151         if (status)
152                 return status;
153
154         hw->port_info = (struct ice_port_info *)
155                         ice_malloc(hw, sizeof(*hw->port_info));
156         if (!hw->port_info)
157                 return ICE_ERR_NO_MEMORY;
158
159         /* set the back pointer to HW */
160         hw->port_info->hw = hw;
161
162         /* Initialize port_info struct with switch configuration data */
163         status = ice_get_initial_sw_cfg(hw);
164         if (status)
165                 goto err_unroll_alloc;
166
167         pcaps = (struct ice_aqc_get_phy_caps_data *)
168                 ice_malloc(hw, sizeof(*pcaps));
169         if (!pcaps) {
170                 status = ICE_ERR_NO_MEMORY;
171                 goto err_unroll_alloc;
172         }
173
174         /* Initialize port_info struct with PHY capabilities */
175         status = ice_aq_get_phy_caps(hw->port_info, false,
176                                      ICE_AQC_REPORT_TOPO_CAP, pcaps, NULL);
177         ice_free(hw, pcaps);
178         if (status)
179                 goto err_unroll_alloc;
180
181         /* Initialize port_info struct with link information */
182         status = ice_aq_get_link_info(hw->port_info, false, NULL, NULL);
183         if (status)
184                 goto err_unroll_alloc;
185
186         status = ice_init_fltr_mgmt_struct(hw);
187         if (status)
188                 goto err_unroll_alloc;
189
190         status = ice_init_hw_tbls(hw);
191         if (status)
192                 goto err_unroll_fltr_mgmt_struct;
193
194         PMD_INIT_LOG(INFO,
195                      "firmware %d.%d.%d api %d.%d.%d build 0x%08x",
196                      hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch,
197                      hw->api_maj_ver, hw->api_min_ver, hw->api_patch,
198                      hw->fw_build);
199
200         return ICE_SUCCESS;
201
202 err_unroll_fltr_mgmt_struct:
203         ice_cleanup_fltr_mgmt_struct(hw);
204 err_unroll_alloc:
205         ice_free(hw, hw->port_info);
206         hw->port_info = NULL;
207
208         return status;
209 }
210
211 static void ice_dcf_uninit_parent_hw(struct ice_hw *hw)
212 {
213         ice_cleanup_fltr_mgmt_struct(hw);
214
215         ice_free_seg(hw);
216         ice_free_hw_tbls(hw);
217
218         ice_free(hw, hw->port_info);
219         hw->port_info = NULL;
220
221         ice_clear_all_vsi_ctx(hw);
222 }
223
224 static int
225 ice_dcf_request_pkg_name(struct ice_hw *hw, char *pkg_name)
226 {
227         struct ice_dcf_adapter *dcf_adapter =
228                         container_of(hw, struct ice_dcf_adapter, parent.hw);
229         struct virtchnl_pkg_info pkg_info;
230         struct dcf_virtchnl_cmd vc_cmd;
231         uint64_t dsn;
232
233         vc_cmd.v_op = VIRTCHNL_OP_DCF_GET_PKG_INFO;
234         vc_cmd.req_msglen = 0;
235         vc_cmd.req_msg = NULL;
236         vc_cmd.rsp_buflen = sizeof(pkg_info);
237         vc_cmd.rsp_msgbuf = (uint8_t *)&pkg_info;
238
239         if (ice_dcf_execute_virtchnl_cmd(&dcf_adapter->real_hw, &vc_cmd))
240                 goto pkg_file_direct;
241
242         rte_memcpy(&dsn, pkg_info.dsn, sizeof(dsn));
243
244         snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
245                  ICE_PKG_FILE_SEARCH_PATH_UPDATES "ice-%016llx.pkg",
246                  (unsigned long long)dsn);
247         if (!access(pkg_name, 0))
248                 return 0;
249
250         snprintf(pkg_name, ICE_MAX_PKG_FILENAME_SIZE,
251                  ICE_PKG_FILE_SEARCH_PATH_DEFAULT "ice-%016llx.pkg",
252                  (unsigned long long)dsn);
253         if (!access(pkg_name, 0))
254                 return 0;
255
256 pkg_file_direct:
257         snprintf(pkg_name,
258                  ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_UPDATES);
259         if (!access(pkg_name, 0))
260                 return 0;
261
262         snprintf(pkg_name,
263                  ICE_MAX_PKG_FILENAME_SIZE, "%s", ICE_PKG_FILE_DEFAULT);
264         if (!access(pkg_name, 0))
265                 return 0;
266
267         return -1;
268 }
269
270 static int
271 ice_dcf_load_pkg(struct ice_hw *hw)
272 {
273         char pkg_name[ICE_MAX_PKG_FILENAME_SIZE];
274         uint8_t *pkg_buf;
275         uint32_t buf_len;
276         struct stat st;
277         FILE *fp;
278         int err;
279
280         if (ice_dcf_request_pkg_name(hw, pkg_name)) {
281                 PMD_INIT_LOG(ERR, "Failed to locate the package file");
282                 return -ENOENT;
283         }
284
285         PMD_INIT_LOG(DEBUG, "DDP package name: %s", pkg_name);
286
287         err = stat(pkg_name, &st);
288         if (err) {
289                 PMD_INIT_LOG(ERR, "Failed to get file status");
290                 return err;
291         }
292
293         buf_len = st.st_size;
294         pkg_buf = rte_malloc(NULL, buf_len, 0);
295         if (!pkg_buf) {
296                 PMD_INIT_LOG(ERR, "failed to allocate buffer of size %u for package",
297                              buf_len);
298                 return -1;
299         }
300
301         fp = fopen(pkg_name, "rb");
302         if (!fp)  {
303                 PMD_INIT_LOG(ERR, "failed to open file: %s", pkg_name);
304                 err = -1;
305                 goto ret;
306         }
307
308         err = fread(pkg_buf, buf_len, 1, fp);
309         fclose(fp);
310         if (err != 1) {
311                 PMD_INIT_LOG(ERR, "failed to read package data");
312                 err = -1;
313                 goto ret;
314         }
315
316         err = ice_copy_and_init_pkg(hw, pkg_buf, buf_len);
317         if (err)
318                 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d", err);
319
320 ret:
321         rte_free(pkg_buf);
322         return err;
323 }
324
325 int
326 ice_dcf_init_parent_adapter(struct rte_eth_dev *eth_dev)
327 {
328         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
329         struct ice_adapter *parent_adapter = &adapter->parent;
330         struct ice_hw *parent_hw = &parent_adapter->hw;
331         struct ice_dcf_hw *hw = &adapter->real_hw;
332         const struct rte_ether_addr *mac;
333         int err;
334
335         parent_adapter->eth_dev = eth_dev;
336         parent_adapter->pf.adapter = parent_adapter;
337         parent_adapter->pf.dev_data = eth_dev->data;
338         /* create a dummy main_vsi */
339         parent_adapter->pf.main_vsi =
340                 rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
341         if (!parent_adapter->pf.main_vsi)
342                 return -ENOMEM;
343         parent_adapter->pf.main_vsi->adapter = parent_adapter;
344         parent_adapter->pf.adapter_stopped = 1;
345
346         parent_hw->back = parent_adapter;
347         parent_hw->mac_type = ICE_MAC_GENERIC;
348         parent_hw->vendor_id = ICE_INTEL_VENDOR_ID;
349
350         ice_init_lock(&parent_hw->adminq.sq_lock);
351         ice_init_lock(&parent_hw->adminq.rq_lock);
352         parent_hw->aq_send_cmd_fn = ice_dcf_send_aq_cmd;
353         parent_hw->aq_send_cmd_param = &adapter->real_hw;
354         parent_hw->dcf_enabled = true;
355
356         err = ice_dcf_init_parent_hw(parent_hw);
357         if (err) {
358                 PMD_INIT_LOG(ERR, "failed to init the DCF parent hardware with error %d",
359                              err);
360                 return err;
361         }
362
363         err = ice_dcf_load_pkg(parent_hw);
364         if (err) {
365                 PMD_INIT_LOG(ERR, "failed to load package with error %d",
366                              err);
367                 goto uninit_hw;
368         }
369         parent_adapter->active_pkg_type = ice_load_pkg_type(parent_hw);
370
371         err = ice_flow_init(parent_adapter);
372         if (err) {
373                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
374                 goto uninit_hw;
375         }
376
377         ice_dcf_update_vf_vsi_map(parent_hw, hw->num_vfs, hw->vf_vsi_map);
378
379         mac = (const struct rte_ether_addr *)hw->avf.mac.addr;
380         if (rte_is_valid_assigned_ether_addr(mac))
381                 rte_ether_addr_copy(mac, &parent_adapter->pf.dev_addr);
382         else
383                 rte_eth_random_addr(parent_adapter->pf.dev_addr.addr_bytes);
384
385         eth_dev->data->mac_addrs = &parent_adapter->pf.dev_addr;
386
387         return 0;
388
389 uninit_hw:
390         ice_dcf_uninit_parent_hw(parent_hw);
391         return err;
392 }
393
394 void
395 ice_dcf_uninit_parent_adapter(struct rte_eth_dev *eth_dev)
396 {
397         struct ice_dcf_adapter *adapter = eth_dev->data->dev_private;
398         struct ice_adapter *parent_adapter = &adapter->parent;
399         struct ice_hw *parent_hw = &parent_adapter->hw;
400
401         eth_dev->data->mac_addrs = NULL;
402
403         ice_flow_uninit(parent_adapter);
404         ice_dcf_uninit_parent_hw(parent_hw);
405 }