net/ice: add DCF hardware initialization
[dpdk.git] / drivers / net / ice / ice_dcf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
15
16 #include <rte_pci.h>
17 #include <rte_atomic.h>
18 #include <rte_eal.h>
19 #include <rte_ether.h>
20 #include <rte_ethdev_driver.h>
21 #include <rte_ethdev_pci.h>
22 #include <rte_malloc.h>
23 #include <rte_memzone.h>
24 #include <rte_dev.h>
25
26 #include "ice_dcf.h"
27
28 #define ICE_DCF_AQ_LEN     32
29 #define ICE_DCF_AQ_BUF_SZ  4096
30
31 #define ICE_DCF_ARQ_MAX_RETRIES 200
32 #define ICE_DCF_ARQ_CHECK_TIME  2   /* msecs */
33
34 #define ICE_DCF_VF_RES_BUF_SZ   \
35         (sizeof(struct virtchnl_vf_resource) +  \
36                 IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource))
37
38 static __rte_always_inline int
39 ice_dcf_send_cmd_req_no_irq(struct ice_dcf_hw *hw, enum virtchnl_ops op,
40                             uint8_t *req_msg, uint16_t req_msglen)
41 {
42         return iavf_aq_send_msg_to_pf(&hw->avf, op, IAVF_SUCCESS,
43                                       req_msg, req_msglen, NULL);
44 }
45
46 static int
47 ice_dcf_recv_cmd_rsp_no_irq(struct ice_dcf_hw *hw, enum virtchnl_ops op,
48                             uint8_t *rsp_msgbuf, uint16_t rsp_buflen,
49                             uint16_t *rsp_msglen)
50 {
51         struct iavf_arq_event_info event;
52         enum virtchnl_ops v_op;
53         int i = 0;
54         int err;
55
56         event.buf_len = rsp_buflen;
57         event.msg_buf = rsp_msgbuf;
58
59         do {
60                 err = iavf_clean_arq_element(&hw->avf, &event, NULL);
61                 if (err != IAVF_SUCCESS)
62                         goto again;
63
64                 v_op = rte_le_to_cpu_32(event.desc.cookie_high);
65                 if (v_op != op)
66                         goto again;
67
68                 if (rsp_msglen != NULL)
69                         *rsp_msglen = event.msg_len;
70                 return rte_le_to_cpu_32(event.desc.cookie_low);
71
72 again:
73                 rte_delay_ms(ICE_DCF_ARQ_CHECK_TIME);
74         } while (i++ < ICE_DCF_ARQ_MAX_RETRIES);
75
76         return -EIO;
77 }
78
79 static __rte_always_inline void
80 ice_dcf_aq_cmd_clear(struct ice_dcf_hw *hw, struct dcf_virtchnl_cmd *cmd)
81 {
82         rte_spinlock_lock(&hw->vc_cmd_queue_lock);
83
84         TAILQ_REMOVE(&hw->vc_cmd_queue, cmd, next);
85
86         rte_spinlock_unlock(&hw->vc_cmd_queue_lock);
87 }
88
89 static __rte_always_inline void
90 ice_dcf_vc_cmd_set(struct ice_dcf_hw *hw, struct dcf_virtchnl_cmd *cmd)
91 {
92         cmd->v_ret = IAVF_ERR_NOT_READY;
93         cmd->rsp_msglen = 0;
94         cmd->pending = 1;
95
96         rte_spinlock_lock(&hw->vc_cmd_queue_lock);
97
98         TAILQ_INSERT_TAIL(&hw->vc_cmd_queue, cmd, next);
99
100         rte_spinlock_unlock(&hw->vc_cmd_queue_lock);
101 }
102
103 static __rte_always_inline int
104 ice_dcf_vc_cmd_send(struct ice_dcf_hw *hw, struct dcf_virtchnl_cmd *cmd)
105 {
106         return iavf_aq_send_msg_to_pf(&hw->avf,
107                                       cmd->v_op, IAVF_SUCCESS,
108                                       cmd->req_msg, cmd->req_msglen, NULL);
109 }
110
111 static __rte_always_inline void
112 ice_dcf_aq_cmd_handle(struct ice_dcf_hw *hw, struct iavf_arq_event_info *info)
113 {
114         struct dcf_virtchnl_cmd *cmd;
115         enum virtchnl_ops v_op;
116         enum iavf_status v_ret;
117         uint16_t aq_op;
118
119         aq_op = rte_le_to_cpu_16(info->desc.opcode);
120         if (unlikely(aq_op != iavf_aqc_opc_send_msg_to_vf)) {
121                 PMD_DRV_LOG(ERR,
122                             "Request %u is not supported yet", aq_op);
123                 return;
124         }
125
126         v_op = rte_le_to_cpu_32(info->desc.cookie_high);
127         if (unlikely(v_op == VIRTCHNL_OP_EVENT))
128                 return;
129
130         v_ret = rte_le_to_cpu_32(info->desc.cookie_low);
131
132         rte_spinlock_lock(&hw->vc_cmd_queue_lock);
133
134         TAILQ_FOREACH(cmd, &hw->vc_cmd_queue, next) {
135                 if (cmd->v_op == v_op && cmd->pending) {
136                         cmd->v_ret = v_ret;
137                         cmd->rsp_msglen = RTE_MIN(info->msg_len,
138                                                   cmd->rsp_buflen);
139                         if (likely(cmd->rsp_msglen != 0))
140                                 rte_memcpy(cmd->rsp_msgbuf, info->msg_buf,
141                                            cmd->rsp_msglen);
142
143                         /* prevent compiler reordering */
144                         rte_compiler_barrier();
145                         cmd->pending = 0;
146                         break;
147                 }
148         }
149
150         rte_spinlock_unlock(&hw->vc_cmd_queue_lock);
151 }
152
153 static void
154 ice_dcf_handle_virtchnl_msg(struct ice_dcf_hw *hw)
155 {
156         struct iavf_arq_event_info info;
157         uint16_t pending = 1;
158         int ret;
159
160         info.buf_len = ICE_DCF_AQ_BUF_SZ;
161         info.msg_buf = hw->arq_buf;
162
163         while (pending) {
164                 ret = iavf_clean_arq_element(&hw->avf, &info, &pending);
165                 if (ret != IAVF_SUCCESS)
166                         break;
167
168                 ice_dcf_aq_cmd_handle(hw, &info);
169         }
170 }
171
172 static int
173 ice_dcf_init_check_api_version(struct ice_dcf_hw *hw)
174 {
175 #define ICE_CPF_VIRTCHNL_VERSION_MAJOR_START    1
176 #define ICE_CPF_VIRTCHNL_VERSION_MINOR_START    1
177         struct virtchnl_version_info version, *pver;
178         int err;
179
180         version.major = VIRTCHNL_VERSION_MAJOR;
181         version.minor = VIRTCHNL_VERSION_MINOR;
182         err = ice_dcf_send_cmd_req_no_irq(hw, VIRTCHNL_OP_VERSION,
183                                           (uint8_t *)&version, sizeof(version));
184         if (err) {
185                 PMD_INIT_LOG(ERR, "Failed to send OP_VERSION");
186                 return err;
187         }
188
189         pver = &hw->virtchnl_version;
190         err = ice_dcf_recv_cmd_rsp_no_irq(hw, VIRTCHNL_OP_VERSION,
191                                           (uint8_t *)pver, sizeof(*pver), NULL);
192         if (err) {
193                 PMD_INIT_LOG(ERR, "Failed to get response of OP_VERSION");
194                 return -1;
195         }
196
197         PMD_INIT_LOG(DEBUG,
198                      "Peer PF API version: %u.%u", pver->major, pver->minor);
199
200         if (pver->major < ICE_CPF_VIRTCHNL_VERSION_MAJOR_START ||
201             (pver->major == ICE_CPF_VIRTCHNL_VERSION_MAJOR_START &&
202              pver->minor < ICE_CPF_VIRTCHNL_VERSION_MINOR_START)) {
203                 PMD_INIT_LOG(ERR,
204                              "VIRTCHNL API version should not be lower than (%u.%u)",
205                              ICE_CPF_VIRTCHNL_VERSION_MAJOR_START,
206                              ICE_CPF_VIRTCHNL_VERSION_MAJOR_START);
207                 return -1;
208         } else if (pver->major > VIRTCHNL_VERSION_MAJOR ||
209                    (pver->major == VIRTCHNL_VERSION_MAJOR &&
210                     pver->minor > VIRTCHNL_VERSION_MINOR)) {
211                 PMD_INIT_LOG(ERR,
212                              "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
213                              pver->major, pver->minor,
214                              VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR);
215                 return -1;
216         }
217
218         PMD_INIT_LOG(DEBUG, "Peer is supported PF host");
219
220         return 0;
221 }
222
223 static int
224 ice_dcf_get_vf_resource(struct ice_dcf_hw *hw)
225 {
226         uint32_t caps;
227         int err, i;
228
229         caps = VIRTCHNL_VF_OFFLOAD_WB_ON_ITR | VIRTCHNL_VF_OFFLOAD_RX_POLLING |
230                VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
231                VF_BASE_MODE_OFFLOADS;
232
233         err = ice_dcf_send_cmd_req_no_irq(hw, VIRTCHNL_OP_GET_VF_RESOURCES,
234                                           (uint8_t *)&caps, sizeof(caps));
235         if (err) {
236                 PMD_DRV_LOG(ERR, "Failed to send msg OP_GET_VF_RESOURCE");
237                 return err;
238         }
239
240         err = ice_dcf_recv_cmd_rsp_no_irq(hw, VIRTCHNL_OP_GET_VF_RESOURCES,
241                                           (uint8_t *)hw->vf_res,
242                                           ICE_DCF_VF_RES_BUF_SZ, NULL);
243         if (err) {
244                 PMD_DRV_LOG(ERR, "Failed to get response of OP_GET_VF_RESOURCE");
245                 return -1;
246         }
247
248         iavf_vf_parse_hw_config(&hw->avf, hw->vf_res);
249
250         hw->vsi_res = NULL;
251         for (i = 0; i < hw->vf_res->num_vsis; i++) {
252                 if (hw->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
253                         hw->vsi_res = &hw->vf_res->vsi_res[i];
254         }
255
256         if (!hw->vsi_res) {
257                 PMD_DRV_LOG(ERR, "no LAN VSI found");
258                 return -1;
259         }
260
261         hw->vsi_id = hw->vsi_res->vsi_id;
262         PMD_DRV_LOG(DEBUG, "VSI ID is %u", hw->vsi_id);
263
264         return 0;
265 }
266
267 static int
268 ice_dcf_check_reset_done(struct ice_dcf_hw *hw)
269 {
270 #define ICE_DCF_RESET_WAIT_CNT       50
271         struct iavf_hw *avf = &hw->avf;
272         int i, reset;
273
274         for (i = 0; i < ICE_DCF_RESET_WAIT_CNT; i++) {
275                 reset = IAVF_READ_REG(avf, IAVF_VFGEN_RSTAT) &
276                                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
277                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
278
279                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
280                     reset == VIRTCHNL_VFR_COMPLETED)
281                         break;
282
283                 rte_delay_ms(20);
284         }
285
286         if (i >= ICE_DCF_RESET_WAIT_CNT)
287                 return -1;
288
289         return 0;
290 }
291
292 static inline void
293 ice_dcf_enable_irq0(struct ice_dcf_hw *hw)
294 {
295         struct iavf_hw *avf = &hw->avf;
296
297         /* Enable admin queue interrupt trigger */
298         IAVF_WRITE_REG(avf, IAVF_VFINT_ICR0_ENA1,
299                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
300         IAVF_WRITE_REG(avf, IAVF_VFINT_DYN_CTL01,
301                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
302                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
303                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
304
305         IAVF_WRITE_FLUSH(avf);
306 }
307
308 static inline void
309 ice_dcf_disable_irq0(struct ice_dcf_hw *hw)
310 {
311         struct iavf_hw *avf = &hw->avf;
312
313         /* Disable all interrupt types */
314         IAVF_WRITE_REG(avf, IAVF_VFINT_ICR0_ENA1, 0);
315         IAVF_WRITE_REG(avf, IAVF_VFINT_DYN_CTL01,
316                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
317
318         IAVF_WRITE_FLUSH(avf);
319 }
320
321 static void
322 ice_dcf_dev_interrupt_handler(void *param)
323 {
324         struct ice_dcf_hw *hw = param;
325
326         ice_dcf_disable_irq0(hw);
327
328         ice_dcf_handle_virtchnl_msg(hw);
329
330         ice_dcf_enable_irq0(hw);
331 }
332
333 int
334 ice_dcf_execute_virtchnl_cmd(struct ice_dcf_hw *hw,
335                              struct dcf_virtchnl_cmd *cmd)
336 {
337         int i = 0;
338         int err;
339
340         if ((cmd->req_msg && !cmd->req_msglen) ||
341             (!cmd->req_msg && cmd->req_msglen) ||
342             (cmd->rsp_msgbuf && !cmd->rsp_buflen) ||
343             (!cmd->rsp_msgbuf && cmd->rsp_buflen))
344                 return -EINVAL;
345
346         rte_spinlock_lock(&hw->vc_cmd_send_lock);
347         ice_dcf_vc_cmd_set(hw, cmd);
348
349         err = ice_dcf_vc_cmd_send(hw, cmd);
350         if (err) {
351                 PMD_DRV_LOG(ERR, "fail to send cmd %d", cmd->v_op);
352                 goto ret;
353         }
354
355         do {
356                 if (!cmd->pending)
357                         break;
358
359                 rte_delay_ms(ICE_DCF_ARQ_CHECK_TIME);
360         } while (i++ < ICE_DCF_ARQ_MAX_RETRIES);
361
362         if (cmd->v_ret != IAVF_SUCCESS) {
363                 err = -1;
364                 PMD_DRV_LOG(ERR,
365                             "No response (%d times) or return failure (%d) for cmd %d",
366                             i, cmd->v_ret, cmd->v_op);
367         }
368
369 ret:
370         ice_dcf_aq_cmd_clear(hw, cmd);
371         rte_spinlock_unlock(&hw->vc_cmd_send_lock);
372         return err;
373 }
374
375 int
376 ice_dcf_init_hw(struct rte_eth_dev *eth_dev, struct ice_dcf_hw *hw)
377 {
378         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
379         int ret;
380
381         hw->avf.hw_addr = pci_dev->mem_resource[0].addr;
382         hw->avf.back = hw;
383
384         hw->avf.bus.bus_id = pci_dev->addr.bus;
385         hw->avf.bus.device = pci_dev->addr.devid;
386         hw->avf.bus.func = pci_dev->addr.function;
387
388         hw->avf.device_id = pci_dev->id.device_id;
389         hw->avf.vendor_id = pci_dev->id.vendor_id;
390         hw->avf.subsystem_device_id = pci_dev->id.subsystem_device_id;
391         hw->avf.subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
392
393         hw->avf.aq.num_arq_entries = ICE_DCF_AQ_LEN;
394         hw->avf.aq.num_asq_entries = ICE_DCF_AQ_LEN;
395         hw->avf.aq.arq_buf_size = ICE_DCF_AQ_BUF_SZ;
396         hw->avf.aq.asq_buf_size = ICE_DCF_AQ_BUF_SZ;
397
398         rte_spinlock_init(&hw->vc_cmd_send_lock);
399         rte_spinlock_init(&hw->vc_cmd_queue_lock);
400         TAILQ_INIT(&hw->vc_cmd_queue);
401
402         hw->arq_buf = rte_zmalloc("arq_buf", ICE_DCF_AQ_BUF_SZ, 0);
403         if (hw->arq_buf == NULL) {
404                 PMD_INIT_LOG(ERR, "unable to allocate AdminQ buffer memory");
405                 goto err;
406         }
407
408         ret = iavf_set_mac_type(&hw->avf);
409         if (ret) {
410                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", ret);
411                 goto err;
412         }
413
414         ret = ice_dcf_check_reset_done(hw);
415         if (ret) {
416                 PMD_INIT_LOG(ERR, "VF is still resetting");
417                 goto err;
418         }
419
420         ret = iavf_init_adminq(&hw->avf);
421         if (ret) {
422                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", ret);
423                 goto err;
424         }
425
426         if (ice_dcf_init_check_api_version(hw)) {
427                 PMD_INIT_LOG(ERR, "check_api version failed");
428                 goto err_api;
429         }
430
431         hw->vf_res = rte_zmalloc("vf_res", ICE_DCF_VF_RES_BUF_SZ, 0);
432         if (hw->vf_res == NULL) {
433                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
434                 goto err_api;
435         }
436
437         if (ice_dcf_get_vf_resource(hw)) {
438                 PMD_INIT_LOG(ERR, "Failed to get VF resource");
439                 goto err_alloc;
440         }
441
442         rte_intr_callback_register(&pci_dev->intr_handle,
443                                    ice_dcf_dev_interrupt_handler, hw);
444         rte_intr_enable(&pci_dev->intr_handle);
445         ice_dcf_enable_irq0(hw);
446
447         return 0;
448
449 err_alloc:
450         rte_free(hw->vf_res);
451 err_api:
452         iavf_shutdown_adminq(&hw->avf);
453 err:
454         rte_free(hw->arq_buf);
455
456         return -1;
457 }
458
459 void
460 ice_dcf_uninit_hw(struct rte_eth_dev *eth_dev, struct ice_dcf_hw *hw)
461 {
462         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
463         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
464
465         ice_dcf_disable_irq0(hw);
466         rte_intr_disable(intr_handle);
467         rte_intr_callback_unregister(intr_handle,
468                                      ice_dcf_dev_interrupt_handler, hw);
469
470         iavf_shutdown_adminq(&hw->avf);
471
472         rte_free(hw->arq_buf);
473         rte_free(hw->vf_res);
474 }