net/ice: handle PF initialization by DCF
[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 (v_op == VIRTCHNL_OP_EVENT) {
128                 if (hw->vc_event_msg_cb != NULL)
129                         hw->vc_event_msg_cb(hw,
130                                             info->msg_buf,
131                                             info->msg_len);
132                 return;
133         }
134
135         v_ret = rte_le_to_cpu_32(info->desc.cookie_low);
136
137         rte_spinlock_lock(&hw->vc_cmd_queue_lock);
138
139         TAILQ_FOREACH(cmd, &hw->vc_cmd_queue, next) {
140                 if (cmd->v_op == v_op && cmd->pending) {
141                         cmd->v_ret = v_ret;
142                         cmd->rsp_msglen = RTE_MIN(info->msg_len,
143                                                   cmd->rsp_buflen);
144                         if (likely(cmd->rsp_msglen != 0))
145                                 rte_memcpy(cmd->rsp_msgbuf, info->msg_buf,
146                                            cmd->rsp_msglen);
147
148                         /* prevent compiler reordering */
149                         rte_compiler_barrier();
150                         cmd->pending = 0;
151                         break;
152                 }
153         }
154
155         rte_spinlock_unlock(&hw->vc_cmd_queue_lock);
156 }
157
158 static void
159 ice_dcf_handle_virtchnl_msg(struct ice_dcf_hw *hw)
160 {
161         struct iavf_arq_event_info info;
162         uint16_t pending = 1;
163         int ret;
164
165         info.buf_len = ICE_DCF_AQ_BUF_SZ;
166         info.msg_buf = hw->arq_buf;
167
168         while (pending) {
169                 ret = iavf_clean_arq_element(&hw->avf, &info, &pending);
170                 if (ret != IAVF_SUCCESS)
171                         break;
172
173                 ice_dcf_aq_cmd_handle(hw, &info);
174         }
175 }
176
177 static int
178 ice_dcf_init_check_api_version(struct ice_dcf_hw *hw)
179 {
180 #define ICE_CPF_VIRTCHNL_VERSION_MAJOR_START    1
181 #define ICE_CPF_VIRTCHNL_VERSION_MINOR_START    1
182         struct virtchnl_version_info version, *pver;
183         int err;
184
185         version.major = VIRTCHNL_VERSION_MAJOR;
186         version.minor = VIRTCHNL_VERSION_MINOR;
187         err = ice_dcf_send_cmd_req_no_irq(hw, VIRTCHNL_OP_VERSION,
188                                           (uint8_t *)&version, sizeof(version));
189         if (err) {
190                 PMD_INIT_LOG(ERR, "Failed to send OP_VERSION");
191                 return err;
192         }
193
194         pver = &hw->virtchnl_version;
195         err = ice_dcf_recv_cmd_rsp_no_irq(hw, VIRTCHNL_OP_VERSION,
196                                           (uint8_t *)pver, sizeof(*pver), NULL);
197         if (err) {
198                 PMD_INIT_LOG(ERR, "Failed to get response of OP_VERSION");
199                 return -1;
200         }
201
202         PMD_INIT_LOG(DEBUG,
203                      "Peer PF API version: %u.%u", pver->major, pver->minor);
204
205         if (pver->major < ICE_CPF_VIRTCHNL_VERSION_MAJOR_START ||
206             (pver->major == ICE_CPF_VIRTCHNL_VERSION_MAJOR_START &&
207              pver->minor < ICE_CPF_VIRTCHNL_VERSION_MINOR_START)) {
208                 PMD_INIT_LOG(ERR,
209                              "VIRTCHNL API version should not be lower than (%u.%u)",
210                              ICE_CPF_VIRTCHNL_VERSION_MAJOR_START,
211                              ICE_CPF_VIRTCHNL_VERSION_MAJOR_START);
212                 return -1;
213         } else if (pver->major > VIRTCHNL_VERSION_MAJOR ||
214                    (pver->major == VIRTCHNL_VERSION_MAJOR &&
215                     pver->minor > VIRTCHNL_VERSION_MINOR)) {
216                 PMD_INIT_LOG(ERR,
217                              "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
218                              pver->major, pver->minor,
219                              VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR);
220                 return -1;
221         }
222
223         PMD_INIT_LOG(DEBUG, "Peer is supported PF host");
224
225         return 0;
226 }
227
228 static int
229 ice_dcf_get_vf_resource(struct ice_dcf_hw *hw)
230 {
231         uint32_t caps;
232         int err, i;
233
234         caps = VIRTCHNL_VF_OFFLOAD_WB_ON_ITR | VIRTCHNL_VF_OFFLOAD_RX_POLLING |
235                VIRTCHNL_VF_CAP_ADV_LINK_SPEED | VIRTCHNL_VF_CAP_DCF |
236                VF_BASE_MODE_OFFLOADS;
237
238         err = ice_dcf_send_cmd_req_no_irq(hw, VIRTCHNL_OP_GET_VF_RESOURCES,
239                                           (uint8_t *)&caps, sizeof(caps));
240         if (err) {
241                 PMD_DRV_LOG(ERR, "Failed to send msg OP_GET_VF_RESOURCE");
242                 return err;
243         }
244
245         err = ice_dcf_recv_cmd_rsp_no_irq(hw, VIRTCHNL_OP_GET_VF_RESOURCES,
246                                           (uint8_t *)hw->vf_res,
247                                           ICE_DCF_VF_RES_BUF_SZ, NULL);
248         if (err) {
249                 PMD_DRV_LOG(ERR, "Failed to get response of OP_GET_VF_RESOURCE");
250                 return -1;
251         }
252
253         iavf_vf_parse_hw_config(&hw->avf, hw->vf_res);
254
255         hw->vsi_res = NULL;
256         for (i = 0; i < hw->vf_res->num_vsis; i++) {
257                 if (hw->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
258                         hw->vsi_res = &hw->vf_res->vsi_res[i];
259         }
260
261         if (!hw->vsi_res) {
262                 PMD_DRV_LOG(ERR, "no LAN VSI found");
263                 return -1;
264         }
265
266         hw->vsi_id = hw->vsi_res->vsi_id;
267         PMD_DRV_LOG(DEBUG, "VSI ID is %u", hw->vsi_id);
268
269         return 0;
270 }
271
272 static int
273 ice_dcf_mode_disable(struct ice_dcf_hw *hw)
274 {
275         int err;
276
277         err = ice_dcf_send_cmd_req_no_irq(hw, VIRTCHNL_OP_DCF_DISABLE,
278                                           NULL, 0);
279         if (err) {
280                 PMD_DRV_LOG(ERR, "Failed to send msg OP_DCF_DISABLE");
281                 return err;
282         }
283
284         err = ice_dcf_recv_cmd_rsp_no_irq(hw, VIRTCHNL_OP_DCF_DISABLE,
285                                           hw->arq_buf, ICE_DCF_AQ_BUF_SZ, NULL);
286         if (err) {
287                 PMD_DRV_LOG(ERR,
288                             "Failed to get response of OP_DCF_DISABLE %d",
289                             err);
290                 return -1;
291         }
292
293         return 0;
294 }
295
296 static int
297 ice_dcf_check_reset_done(struct ice_dcf_hw *hw)
298 {
299 #define ICE_DCF_RESET_WAIT_CNT       50
300         struct iavf_hw *avf = &hw->avf;
301         int i, reset;
302
303         for (i = 0; i < ICE_DCF_RESET_WAIT_CNT; i++) {
304                 reset = IAVF_READ_REG(avf, IAVF_VFGEN_RSTAT) &
305                                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
306                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
307
308                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
309                     reset == VIRTCHNL_VFR_COMPLETED)
310                         break;
311
312                 rte_delay_ms(20);
313         }
314
315         if (i >= ICE_DCF_RESET_WAIT_CNT)
316                 return -1;
317
318         return 0;
319 }
320
321 static inline void
322 ice_dcf_enable_irq0(struct ice_dcf_hw *hw)
323 {
324         struct iavf_hw *avf = &hw->avf;
325
326         /* Enable admin queue interrupt trigger */
327         IAVF_WRITE_REG(avf, IAVF_VFINT_ICR0_ENA1,
328                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
329         IAVF_WRITE_REG(avf, IAVF_VFINT_DYN_CTL01,
330                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
331                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
332                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
333
334         IAVF_WRITE_FLUSH(avf);
335 }
336
337 static inline void
338 ice_dcf_disable_irq0(struct ice_dcf_hw *hw)
339 {
340         struct iavf_hw *avf = &hw->avf;
341
342         /* Disable all interrupt types */
343         IAVF_WRITE_REG(avf, IAVF_VFINT_ICR0_ENA1, 0);
344         IAVF_WRITE_REG(avf, IAVF_VFINT_DYN_CTL01,
345                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
346
347         IAVF_WRITE_FLUSH(avf);
348 }
349
350 static void
351 ice_dcf_dev_interrupt_handler(void *param)
352 {
353         struct ice_dcf_hw *hw = param;
354
355         ice_dcf_disable_irq0(hw);
356
357         ice_dcf_handle_virtchnl_msg(hw);
358
359         ice_dcf_enable_irq0(hw);
360 }
361
362 int
363 ice_dcf_execute_virtchnl_cmd(struct ice_dcf_hw *hw,
364                              struct dcf_virtchnl_cmd *cmd)
365 {
366         int i = 0;
367         int err;
368
369         if ((cmd->req_msg && !cmd->req_msglen) ||
370             (!cmd->req_msg && cmd->req_msglen) ||
371             (cmd->rsp_msgbuf && !cmd->rsp_buflen) ||
372             (!cmd->rsp_msgbuf && cmd->rsp_buflen))
373                 return -EINVAL;
374
375         rte_spinlock_lock(&hw->vc_cmd_send_lock);
376         ice_dcf_vc_cmd_set(hw, cmd);
377
378         err = ice_dcf_vc_cmd_send(hw, cmd);
379         if (err) {
380                 PMD_DRV_LOG(ERR, "fail to send cmd %d", cmd->v_op);
381                 goto ret;
382         }
383
384         do {
385                 if (!cmd->pending)
386                         break;
387
388                 rte_delay_ms(ICE_DCF_ARQ_CHECK_TIME);
389         } while (i++ < ICE_DCF_ARQ_MAX_RETRIES);
390
391         if (cmd->v_ret != IAVF_SUCCESS) {
392                 err = -1;
393                 PMD_DRV_LOG(ERR,
394                             "No response (%d times) or return failure (%d) for cmd %d",
395                             i, cmd->v_ret, cmd->v_op);
396         }
397
398 ret:
399         ice_dcf_aq_cmd_clear(hw, cmd);
400         rte_spinlock_unlock(&hw->vc_cmd_send_lock);
401         return err;
402 }
403
404 int
405 ice_dcf_send_aq_cmd(void *dcf_hw, struct ice_aq_desc *desc,
406                     void *buf, uint16_t buf_size)
407 {
408         struct dcf_virtchnl_cmd desc_cmd, buff_cmd;
409         struct ice_dcf_hw *hw = dcf_hw;
410         int err = 0;
411         int i = 0;
412
413         if ((buf && !buf_size) || (!buf && buf_size) ||
414             buf_size > ICE_DCF_AQ_BUF_SZ)
415                 return -EINVAL;
416
417         desc_cmd.v_op = VIRTCHNL_OP_DCF_CMD_DESC;
418         desc_cmd.req_msglen = sizeof(*desc);
419         desc_cmd.req_msg = (uint8_t *)desc;
420         desc_cmd.rsp_buflen = sizeof(*desc);
421         desc_cmd.rsp_msgbuf = (uint8_t *)desc;
422
423         if (buf == NULL)
424                 return ice_dcf_execute_virtchnl_cmd(hw, &desc_cmd);
425
426         desc->flags |= rte_cpu_to_le_16(ICE_AQ_FLAG_BUF);
427
428         buff_cmd.v_op = VIRTCHNL_OP_DCF_CMD_BUFF;
429         buff_cmd.req_msglen = buf_size;
430         buff_cmd.req_msg = buf;
431         buff_cmd.rsp_buflen = buf_size;
432         buff_cmd.rsp_msgbuf = buf;
433
434         rte_spinlock_lock(&hw->vc_cmd_send_lock);
435         ice_dcf_vc_cmd_set(hw, &desc_cmd);
436         ice_dcf_vc_cmd_set(hw, &buff_cmd);
437
438         if (ice_dcf_vc_cmd_send(hw, &desc_cmd) ||
439             ice_dcf_vc_cmd_send(hw, &buff_cmd)) {
440                 err = -1;
441                 PMD_DRV_LOG(ERR, "fail to send OP_DCF_CMD_DESC/BUFF");
442                 goto ret;
443         }
444
445         do {
446                 if ((!desc_cmd.pending && !buff_cmd.pending) ||
447                     (!desc_cmd.pending && desc_cmd.v_ret != IAVF_SUCCESS) ||
448                     (!buff_cmd.pending && buff_cmd.v_ret != IAVF_SUCCESS))
449                         break;
450
451                 rte_delay_ms(ICE_DCF_ARQ_CHECK_TIME);
452         } while (i++ < ICE_DCF_ARQ_MAX_RETRIES);
453
454         if (desc_cmd.v_ret != IAVF_SUCCESS || buff_cmd.v_ret != IAVF_SUCCESS) {
455                 err = -1;
456                 PMD_DRV_LOG(ERR,
457                             "No response (%d times) or return failure (desc: %d / buff: %d)",
458                             i, desc_cmd.v_ret, buff_cmd.v_ret);
459         }
460
461 ret:
462         ice_dcf_aq_cmd_clear(hw, &desc_cmd);
463         ice_dcf_aq_cmd_clear(hw, &buff_cmd);
464         rte_spinlock_unlock(&hw->vc_cmd_send_lock);
465
466         return err;
467 }
468
469 int
470 ice_dcf_init_hw(struct rte_eth_dev *eth_dev, struct ice_dcf_hw *hw)
471 {
472         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
473         int ret;
474
475         hw->avf.hw_addr = pci_dev->mem_resource[0].addr;
476         hw->avf.back = hw;
477
478         hw->avf.bus.bus_id = pci_dev->addr.bus;
479         hw->avf.bus.device = pci_dev->addr.devid;
480         hw->avf.bus.func = pci_dev->addr.function;
481
482         hw->avf.device_id = pci_dev->id.device_id;
483         hw->avf.vendor_id = pci_dev->id.vendor_id;
484         hw->avf.subsystem_device_id = pci_dev->id.subsystem_device_id;
485         hw->avf.subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
486
487         hw->avf.aq.num_arq_entries = ICE_DCF_AQ_LEN;
488         hw->avf.aq.num_asq_entries = ICE_DCF_AQ_LEN;
489         hw->avf.aq.arq_buf_size = ICE_DCF_AQ_BUF_SZ;
490         hw->avf.aq.asq_buf_size = ICE_DCF_AQ_BUF_SZ;
491
492         rte_spinlock_init(&hw->vc_cmd_send_lock);
493         rte_spinlock_init(&hw->vc_cmd_queue_lock);
494         TAILQ_INIT(&hw->vc_cmd_queue);
495
496         hw->arq_buf = rte_zmalloc("arq_buf", ICE_DCF_AQ_BUF_SZ, 0);
497         if (hw->arq_buf == NULL) {
498                 PMD_INIT_LOG(ERR, "unable to allocate AdminQ buffer memory");
499                 goto err;
500         }
501
502         ret = iavf_set_mac_type(&hw->avf);
503         if (ret) {
504                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", ret);
505                 goto err;
506         }
507
508         ret = ice_dcf_check_reset_done(hw);
509         if (ret) {
510                 PMD_INIT_LOG(ERR, "VF is still resetting");
511                 goto err;
512         }
513
514         ret = iavf_init_adminq(&hw->avf);
515         if (ret) {
516                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", ret);
517                 goto err;
518         }
519
520         if (ice_dcf_init_check_api_version(hw)) {
521                 PMD_INIT_LOG(ERR, "check_api version failed");
522                 goto err_api;
523         }
524
525         hw->vf_res = rte_zmalloc("vf_res", ICE_DCF_VF_RES_BUF_SZ, 0);
526         if (hw->vf_res == NULL) {
527                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
528                 goto err_api;
529         }
530
531         if (ice_dcf_get_vf_resource(hw)) {
532                 PMD_INIT_LOG(ERR, "Failed to get VF resource");
533                 goto err_alloc;
534         }
535
536         rte_intr_callback_register(&pci_dev->intr_handle,
537                                    ice_dcf_dev_interrupt_handler, hw);
538         rte_intr_enable(&pci_dev->intr_handle);
539         ice_dcf_enable_irq0(hw);
540
541         return 0;
542
543 err_alloc:
544         rte_free(hw->vf_res);
545 err_api:
546         iavf_shutdown_adminq(&hw->avf);
547 err:
548         rte_free(hw->arq_buf);
549
550         return -1;
551 }
552
553 void
554 ice_dcf_uninit_hw(struct rte_eth_dev *eth_dev, struct ice_dcf_hw *hw)
555 {
556         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
557         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
558
559         ice_dcf_disable_irq0(hw);
560         rte_intr_disable(intr_handle);
561         rte_intr_callback_unregister(intr_handle,
562                                      ice_dcf_dev_interrupt_handler, hw);
563
564         ice_dcf_mode_disable(hw);
565         iavf_shutdown_adminq(&hw->avf);
566
567         rte_free(hw->arq_buf);
568         rte_free(hw->vf_res);
569 }