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