vdpa/sfc: get queue notify area info
[dpdk.git] / drivers / vdpa / sfc / sfc_vdpa_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020-2021 Xilinx, Inc.
3  */
4
5 #include <pthread.h>
6 #include <unistd.h>
7 #include <sys/ioctl.h>
8
9 #include <rte_errno.h>
10 #include <rte_malloc.h>
11 #include <rte_vdpa.h>
12 #include <rte_vfio.h>
13 #include <rte_vhost.h>
14
15 #include <vdpa_driver.h>
16
17 #include "efx.h"
18 #include "sfc_vdpa_ops.h"
19 #include "sfc_vdpa.h"
20
21 /* These protocol features are needed to enable notifier ctrl */
22 #define SFC_VDPA_PROTOCOL_FEATURES \
23                 ((1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK) | \
24                  (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
25                  (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
26                  (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
27                  (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))
28
29 /*
30  * Set of features which are enabled by default.
31  * Protocol feature bit is needed to enable notification notifier ctrl.
32  */
33 #define SFC_VDPA_DEFAULT_FEATURES \
34                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
35
36 #define SFC_VDPA_MSIX_IRQ_SET_BUF_LEN \
37                 (sizeof(struct vfio_irq_set) + \
38                 sizeof(int) * (SFC_VDPA_MAX_QUEUE_PAIRS * 2 + 1))
39
40 /* It will be used for target VF when calling function is not PF */
41 #define SFC_VDPA_VF_NULL                0xFFFF
42
43 static int
44 sfc_vdpa_get_device_features(struct sfc_vdpa_ops_data *ops_data)
45 {
46         int rc;
47         uint64_t dev_features;
48         efx_nic_t *nic;
49
50         nic = sfc_vdpa_adapter_by_dev_handle(ops_data->dev_handle)->nic;
51
52         rc = efx_virtio_get_features(nic, EFX_VIRTIO_DEVICE_TYPE_NET,
53                                      &dev_features);
54         if (rc != 0) {
55                 sfc_vdpa_err(ops_data->dev_handle,
56                              "could not read device feature: %s",
57                              rte_strerror(rc));
58                 return rc;
59         }
60
61         ops_data->dev_features = dev_features;
62
63         sfc_vdpa_info(ops_data->dev_handle,
64                       "device supported virtio features : 0x%" PRIx64,
65                       ops_data->dev_features);
66
67         return 0;
68 }
69
70 static uint64_t
71 hva_to_gpa(int vid, uint64_t hva)
72 {
73         struct rte_vhost_memory *vhost_mem = NULL;
74         struct rte_vhost_mem_region *mem_reg = NULL;
75         uint32_t i;
76         uint64_t gpa = 0;
77
78         if (rte_vhost_get_mem_table(vid, &vhost_mem) < 0)
79                 goto error;
80
81         for (i = 0; i < vhost_mem->nregions; i++) {
82                 mem_reg = &vhost_mem->regions[i];
83
84                 if (hva >= mem_reg->host_user_addr &&
85                                 hva < mem_reg->host_user_addr + mem_reg->size) {
86                         gpa = (hva - mem_reg->host_user_addr) +
87                                 mem_reg->guest_phys_addr;
88                         break;
89                 }
90         }
91
92 error:
93         free(vhost_mem);
94         return gpa;
95 }
96
97 static int
98 sfc_vdpa_enable_vfio_intr(struct sfc_vdpa_ops_data *ops_data)
99 {
100         int rc;
101         int *irq_fd_ptr;
102         int vfio_dev_fd;
103         uint32_t i, num_vring;
104         struct rte_vhost_vring vring;
105         struct vfio_irq_set *irq_set;
106         struct rte_pci_device *pci_dev;
107         char irq_set_buf[SFC_VDPA_MSIX_IRQ_SET_BUF_LEN];
108         void *dev;
109
110         num_vring = rte_vhost_get_vring_num(ops_data->vid);
111         dev = ops_data->dev_handle;
112         vfio_dev_fd = sfc_vdpa_adapter_by_dev_handle(dev)->vfio_dev_fd;
113         pci_dev = sfc_vdpa_adapter_by_dev_handle(dev)->pdev;
114
115         irq_set = (struct vfio_irq_set *)irq_set_buf;
116         irq_set->argsz = sizeof(irq_set_buf);
117         irq_set->count = num_vring + 1;
118         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
119                          VFIO_IRQ_SET_ACTION_TRIGGER;
120         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
121         irq_set->start = 0;
122         irq_fd_ptr = (int *)&irq_set->data;
123         irq_fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] =
124                 rte_intr_fd_get(pci_dev->intr_handle);
125
126         for (i = 0; i < num_vring; i++) {
127                 rc = rte_vhost_get_vhost_vring(ops_data->vid, i, &vring);
128                 if (rc)
129                         return -1;
130
131                 irq_fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
132         }
133
134         rc = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
135         if (rc) {
136                 sfc_vdpa_err(ops_data->dev_handle,
137                              "error enabling MSI-X interrupts: %s",
138                              strerror(errno));
139                 return -1;
140         }
141
142         return 0;
143 }
144
145 static int
146 sfc_vdpa_disable_vfio_intr(struct sfc_vdpa_ops_data *ops_data)
147 {
148         int rc;
149         int vfio_dev_fd;
150         struct vfio_irq_set irq_set;
151         void *dev;
152
153         dev = ops_data->dev_handle;
154         vfio_dev_fd = sfc_vdpa_adapter_by_dev_handle(dev)->vfio_dev_fd;
155
156         irq_set.argsz = sizeof(irq_set);
157         irq_set.count = 0;
158         irq_set.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
159         irq_set.index = VFIO_PCI_MSIX_IRQ_INDEX;
160         irq_set.start = 0;
161
162         rc = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, &irq_set);
163         if (rc) {
164                 sfc_vdpa_err(ops_data->dev_handle,
165                              "error disabling MSI-X interrupts: %s",
166                              strerror(errno));
167                 return -1;
168         }
169
170         return 0;
171 }
172
173 static int
174 sfc_vdpa_get_vring_info(struct sfc_vdpa_ops_data *ops_data,
175                         int vq_num, struct sfc_vdpa_vring_info *vring)
176 {
177         int rc;
178         uint64_t gpa;
179         struct rte_vhost_vring vq;
180
181         rc = rte_vhost_get_vhost_vring(ops_data->vid, vq_num, &vq);
182         if (rc < 0) {
183                 sfc_vdpa_err(ops_data->dev_handle,
184                              "get vhost vring failed: %s", rte_strerror(rc));
185                 return rc;
186         }
187
188         gpa = hva_to_gpa(ops_data->vid, (uint64_t)(uintptr_t)vq.desc);
189         if (gpa == 0) {
190                 sfc_vdpa_err(ops_data->dev_handle,
191                              "fail to get GPA for descriptor ring.");
192                 return -1;
193         }
194         vring->desc = gpa;
195
196         gpa = hva_to_gpa(ops_data->vid, (uint64_t)(uintptr_t)vq.avail);
197         if (gpa == 0) {
198                 sfc_vdpa_err(ops_data->dev_handle,
199                              "fail to get GPA for available ring.");
200                 return -1;
201         }
202         vring->avail = gpa;
203
204         gpa = hva_to_gpa(ops_data->vid, (uint64_t)(uintptr_t)vq.used);
205         if (gpa == 0) {
206                 sfc_vdpa_err(ops_data->dev_handle,
207                              "fail to get GPA for used ring.");
208                 return -1;
209         }
210         vring->used = gpa;
211
212         vring->size = vq.size;
213
214         rc = rte_vhost_get_vring_base(ops_data->vid, vq_num,
215                                       &vring->last_avail_idx,
216                                       &vring->last_used_idx);
217
218         return rc;
219 }
220
221 static int
222 sfc_vdpa_virtq_start(struct sfc_vdpa_ops_data *ops_data, int vq_num)
223 {
224         int rc;
225         efx_virtio_vq_t *vq;
226         struct sfc_vdpa_vring_info vring;
227         efx_virtio_vq_cfg_t vq_cfg;
228         efx_virtio_vq_dyncfg_t vq_dyncfg;
229
230         vq = ops_data->vq_cxt[vq_num].vq;
231         if (vq == NULL)
232                 return -1;
233
234         rc = sfc_vdpa_get_vring_info(ops_data, vq_num, &vring);
235         if (rc < 0) {
236                 sfc_vdpa_err(ops_data->dev_handle,
237                              "get vring info failed: %s", rte_strerror(rc));
238                 goto fail_vring_info;
239         }
240
241         vq_cfg.evvc_target_vf = SFC_VDPA_VF_NULL;
242
243         /* even virtqueue for RX and odd for TX */
244         if (vq_num % 2) {
245                 vq_cfg.evvc_type = EFX_VIRTIO_VQ_TYPE_NET_TXQ;
246                 sfc_vdpa_info(ops_data->dev_handle,
247                               "configure virtqueue # %d (TXQ)", vq_num);
248         } else {
249                 vq_cfg.evvc_type = EFX_VIRTIO_VQ_TYPE_NET_RXQ;
250                 sfc_vdpa_info(ops_data->dev_handle,
251                               "configure virtqueue # %d (RXQ)", vq_num);
252         }
253
254         vq_cfg.evvc_vq_num = vq_num;
255         vq_cfg.evvc_desc_tbl_addr   = vring.desc;
256         vq_cfg.evvc_avail_ring_addr = vring.avail;
257         vq_cfg.evvc_used_ring_addr  = vring.used;
258         vq_cfg.evvc_vq_size = vring.size;
259
260         vq_dyncfg.evvd_vq_pidx = vring.last_used_idx;
261         vq_dyncfg.evvd_vq_cidx = vring.last_avail_idx;
262
263         /* MSI-X vector is function-relative */
264         vq_cfg.evvc_msix_vector = RTE_INTR_VEC_RXTX_OFFSET + vq_num;
265         if (ops_data->vdpa_context == SFC_VDPA_AS_VF)
266                 vq_cfg.evvc_pas_id = 0;
267         vq_cfg.evcc_features = ops_data->dev_features &
268                                ops_data->req_features;
269
270         /* Start virtqueue */
271         rc = efx_virtio_qstart(vq, &vq_cfg, &vq_dyncfg);
272         if (rc != 0) {
273                 /* destroy virtqueue */
274                 sfc_vdpa_err(ops_data->dev_handle,
275                              "virtqueue start failed: %s",
276                              rte_strerror(rc));
277                 efx_virtio_qdestroy(vq);
278                 goto fail_virtio_qstart;
279         }
280
281         sfc_vdpa_info(ops_data->dev_handle,
282                       "virtqueue started successfully for vq_num %d", vq_num);
283
284         ops_data->vq_cxt[vq_num].enable = B_TRUE;
285
286         return rc;
287
288 fail_virtio_qstart:
289 fail_vring_info:
290         return rc;
291 }
292
293 static int
294 sfc_vdpa_virtq_stop(struct sfc_vdpa_ops_data *ops_data, int vq_num)
295 {
296         int rc;
297         efx_virtio_vq_dyncfg_t vq_idx;
298         efx_virtio_vq_t *vq;
299
300         if (ops_data->vq_cxt[vq_num].enable != B_TRUE)
301                 return -1;
302
303         vq = ops_data->vq_cxt[vq_num].vq;
304         if (vq == NULL)
305                 return -1;
306
307         /* stop the vq */
308         rc = efx_virtio_qstop(vq, &vq_idx);
309         if (rc == 0) {
310                 ops_data->vq_cxt[vq_num].cidx = vq_idx.evvd_vq_cidx;
311                 ops_data->vq_cxt[vq_num].pidx = vq_idx.evvd_vq_pidx;
312         }
313         ops_data->vq_cxt[vq_num].enable = B_FALSE;
314
315         return rc;
316 }
317
318 static int
319 sfc_vdpa_configure(struct sfc_vdpa_ops_data *ops_data)
320 {
321         int rc, i;
322         int nr_vring;
323         int max_vring_cnt;
324         efx_virtio_vq_t *vq;
325         efx_nic_t *nic;
326         void *dev;
327
328         dev = ops_data->dev_handle;
329         nic = sfc_vdpa_adapter_by_dev_handle(dev)->nic;
330
331         SFC_EFX_ASSERT(ops_data->state == SFC_VDPA_STATE_INITIALIZED);
332
333         ops_data->state = SFC_VDPA_STATE_CONFIGURING;
334
335         nr_vring = rte_vhost_get_vring_num(ops_data->vid);
336         max_vring_cnt =
337                 (sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count * 2);
338
339         /* number of vring should not be more than supported max vq count */
340         if (nr_vring > max_vring_cnt) {
341                 sfc_vdpa_err(dev,
342                              "nr_vring (%d) is > max vring count (%d)",
343                              nr_vring, max_vring_cnt);
344                 goto fail_vring_num;
345         }
346
347         rc = sfc_vdpa_dma_map(ops_data, true);
348         if (rc) {
349                 sfc_vdpa_err(dev,
350                              "DMA map failed: %s", rte_strerror(rc));
351                 goto fail_dma_map;
352         }
353
354         for (i = 0; i < nr_vring; i++) {
355                 rc = efx_virtio_qcreate(nic, &vq);
356                 if ((rc != 0) || (vq == NULL)) {
357                         sfc_vdpa_err(dev,
358                                      "virtqueue create failed: %s",
359                                      rte_strerror(rc));
360                         goto fail_vq_create;
361                 }
362
363                 /* store created virtqueue context */
364                 ops_data->vq_cxt[i].vq = vq;
365         }
366
367         ops_data->vq_count = i;
368
369         ops_data->state = SFC_VDPA_STATE_CONFIGURED;
370
371         return 0;
372
373 fail_vq_create:
374         sfc_vdpa_dma_map(ops_data, false);
375
376 fail_dma_map:
377 fail_vring_num:
378         ops_data->state = SFC_VDPA_STATE_INITIALIZED;
379
380         return -1;
381 }
382
383 static void
384 sfc_vdpa_close(struct sfc_vdpa_ops_data *ops_data)
385 {
386         int i;
387
388         if (ops_data->state != SFC_VDPA_STATE_CONFIGURED)
389                 return;
390
391         ops_data->state = SFC_VDPA_STATE_CLOSING;
392
393         for (i = 0; i < ops_data->vq_count; i++) {
394                 if (ops_data->vq_cxt[i].vq == NULL)
395                         continue;
396
397                 efx_virtio_qdestroy(ops_data->vq_cxt[i].vq);
398         }
399
400         sfc_vdpa_dma_map(ops_data, false);
401
402         ops_data->state = SFC_VDPA_STATE_INITIALIZED;
403 }
404
405 static void
406 sfc_vdpa_stop(struct sfc_vdpa_ops_data *ops_data)
407 {
408         int i;
409         int rc;
410
411         if (ops_data->state != SFC_VDPA_STATE_STARTED)
412                 return;
413
414         ops_data->state = SFC_VDPA_STATE_STOPPING;
415
416         for (i = 0; i < ops_data->vq_count; i++) {
417                 rc = sfc_vdpa_virtq_stop(ops_data, i);
418                 if (rc != 0)
419                         continue;
420         }
421
422         sfc_vdpa_disable_vfio_intr(ops_data);
423
424         ops_data->state = SFC_VDPA_STATE_CONFIGURED;
425 }
426
427 static int
428 sfc_vdpa_start(struct sfc_vdpa_ops_data *ops_data)
429 {
430         int i, j;
431         int rc;
432
433         SFC_EFX_ASSERT(ops_data->state == SFC_VDPA_STATE_CONFIGURED);
434
435         sfc_vdpa_log_init(ops_data->dev_handle, "entry");
436
437         ops_data->state = SFC_VDPA_STATE_STARTING;
438
439         sfc_vdpa_log_init(ops_data->dev_handle, "enable interrupts");
440         rc = sfc_vdpa_enable_vfio_intr(ops_data);
441         if (rc < 0) {
442                 sfc_vdpa_err(ops_data->dev_handle,
443                              "vfio intr allocation failed: %s",
444                              rte_strerror(rc));
445                 goto fail_enable_vfio_intr;
446         }
447
448         rte_vhost_get_negotiated_features(ops_data->vid,
449                                           &ops_data->req_features);
450
451         sfc_vdpa_info(ops_data->dev_handle,
452                       "negotiated feature : 0x%" PRIx64,
453                       ops_data->req_features);
454
455         for (i = 0; i < ops_data->vq_count; i++) {
456                 sfc_vdpa_log_init(ops_data->dev_handle,
457                                   "starting vq# %d", i);
458                 rc = sfc_vdpa_virtq_start(ops_data, i);
459                 if (rc != 0)
460                         goto fail_vq_start;
461         }
462
463         ops_data->state = SFC_VDPA_STATE_STARTED;
464
465         sfc_vdpa_log_init(ops_data->dev_handle, "done");
466
467         return 0;
468
469 fail_vq_start:
470         /* stop already started virtqueues */
471         for (j = 0; j < i; j++)
472                 sfc_vdpa_virtq_stop(ops_data, j);
473         sfc_vdpa_disable_vfio_intr(ops_data);
474
475 fail_enable_vfio_intr:
476         ops_data->state = SFC_VDPA_STATE_CONFIGURED;
477
478         return rc;
479 }
480
481 static int
482 sfc_vdpa_get_queue_num(struct rte_vdpa_device *vdpa_dev, uint32_t *queue_num)
483 {
484         struct sfc_vdpa_ops_data *ops_data;
485         void *dev;
486
487         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
488         if (ops_data == NULL)
489                 return -1;
490
491         dev = ops_data->dev_handle;
492         *queue_num = sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count;
493
494         sfc_vdpa_info(dev, "vDPA ops get_queue_num :: supported queue num : %u",
495                       *queue_num);
496
497         return 0;
498 }
499
500 static int
501 sfc_vdpa_get_features(struct rte_vdpa_device *vdpa_dev, uint64_t *features)
502 {
503         struct sfc_vdpa_ops_data *ops_data;
504
505         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
506         if (ops_data == NULL)
507                 return -1;
508
509         *features = ops_data->drv_features;
510
511         sfc_vdpa_info(ops_data->dev_handle,
512                       "vDPA ops get_feature :: features : 0x%" PRIx64,
513                       *features);
514
515         return 0;
516 }
517
518 static int
519 sfc_vdpa_get_protocol_features(struct rte_vdpa_device *vdpa_dev,
520                                uint64_t *features)
521 {
522         struct sfc_vdpa_ops_data *ops_data;
523
524         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
525         if (ops_data == NULL)
526                 return -1;
527
528         *features = SFC_VDPA_PROTOCOL_FEATURES;
529
530         sfc_vdpa_info(ops_data->dev_handle,
531                       "vDPA ops get_protocol_feature :: features : 0x%" PRIx64,
532                       *features);
533
534         return 0;
535 }
536
537 static void *
538 sfc_vdpa_notify_ctrl(void *arg)
539 {
540         struct sfc_vdpa_ops_data *ops_data;
541         int vid;
542
543         ops_data = arg;
544         if (ops_data == NULL)
545                 return NULL;
546
547         sfc_vdpa_adapter_lock(ops_data->dev_handle);
548
549         vid = ops_data->vid;
550
551         if (rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true) != 0)
552                 sfc_vdpa_info(ops_data->dev_handle,
553                               "vDPA (%s): Notifier could not get configured",
554                               ops_data->vdpa_dev->device->name);
555
556         sfc_vdpa_adapter_unlock(ops_data->dev_handle);
557
558         return NULL;
559 }
560
561 static int
562 sfc_vdpa_setup_notify_ctrl(struct sfc_vdpa_ops_data *ops_data)
563 {
564         int ret;
565
566         ops_data->is_notify_thread_started = false;
567
568         /*
569          * Use rte_vhost_host_notifier_ctrl in a thread to avoid
570          * dead lock scenario when multiple VFs are used in single vdpa
571          * application and multiple VFs are passed to a single VM.
572          */
573         ret = pthread_create(&ops_data->notify_tid, NULL,
574                              sfc_vdpa_notify_ctrl, ops_data);
575         if (ret != 0) {
576                 sfc_vdpa_err(ops_data->dev_handle,
577                              "failed to create notify_ctrl thread: %s",
578                              rte_strerror(ret));
579                 return -1;
580         }
581         ops_data->is_notify_thread_started = true;
582
583         return 0;
584 }
585
586 static int
587 sfc_vdpa_dev_config(int vid)
588 {
589         struct rte_vdpa_device *vdpa_dev;
590         int rc;
591         struct sfc_vdpa_ops_data *ops_data;
592
593         vdpa_dev = rte_vhost_get_vdpa_device(vid);
594
595         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
596         if (ops_data == NULL) {
597                 sfc_vdpa_err(ops_data->dev_handle,
598                              "invalid vDPA device : %p, vid : %d",
599                              vdpa_dev, vid);
600                 return -1;
601         }
602
603         sfc_vdpa_log_init(ops_data->dev_handle, "entry");
604
605         ops_data->vid = vid;
606
607         sfc_vdpa_adapter_lock(ops_data->dev_handle);
608
609         sfc_vdpa_log_init(ops_data->dev_handle, "configuring");
610         rc = sfc_vdpa_configure(ops_data);
611         if (rc != 0)
612                 goto fail_vdpa_config;
613
614         sfc_vdpa_log_init(ops_data->dev_handle, "starting");
615         rc = sfc_vdpa_start(ops_data);
616         if (rc != 0)
617                 goto fail_vdpa_start;
618
619         rc = sfc_vdpa_setup_notify_ctrl(ops_data);
620         if (rc != 0)
621                 goto fail_vdpa_notify;
622
623         sfc_vdpa_adapter_unlock(ops_data->dev_handle);
624
625         sfc_vdpa_log_init(ops_data->dev_handle, "done");
626
627         return 0;
628
629 fail_vdpa_notify:
630         sfc_vdpa_stop(ops_data);
631
632 fail_vdpa_start:
633         sfc_vdpa_close(ops_data);
634
635 fail_vdpa_config:
636         sfc_vdpa_adapter_unlock(ops_data->dev_handle);
637
638         return -1;
639 }
640
641 static int
642 sfc_vdpa_dev_close(int vid)
643 {
644         int ret;
645         struct rte_vdpa_device *vdpa_dev;
646         struct sfc_vdpa_ops_data *ops_data;
647
648         vdpa_dev = rte_vhost_get_vdpa_device(vid);
649
650         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
651         if (ops_data == NULL) {
652                 sfc_vdpa_err(ops_data->dev_handle,
653                              "invalid vDPA device : %p, vid : %d",
654                              vdpa_dev, vid);
655                 return -1;
656         }
657
658         sfc_vdpa_adapter_lock(ops_data->dev_handle);
659         if (ops_data->is_notify_thread_started == true) {
660                 void *status;
661                 ret = pthread_cancel(ops_data->notify_tid);
662                 if (ret != 0) {
663                         sfc_vdpa_err(ops_data->dev_handle,
664                                      "failed to cancel notify_ctrl thread: %s",
665                                      rte_strerror(ret));
666                 }
667
668                 ret = pthread_join(ops_data->notify_tid, &status);
669                 if (ret != 0) {
670                         sfc_vdpa_err(ops_data->dev_handle,
671                                      "failed to join terminated notify_ctrl thread: %s",
672                                      rte_strerror(ret));
673                 }
674         }
675         ops_data->is_notify_thread_started = false;
676
677         sfc_vdpa_stop(ops_data);
678         sfc_vdpa_close(ops_data);
679
680         sfc_vdpa_adapter_unlock(ops_data->dev_handle);
681
682         return 0;
683 }
684
685 static int
686 sfc_vdpa_set_vring_state(int vid, int vring, int state)
687 {
688         RTE_SET_USED(vid);
689         RTE_SET_USED(vring);
690         RTE_SET_USED(state);
691
692         return -1;
693 }
694
695 static int
696 sfc_vdpa_set_features(int vid)
697 {
698         RTE_SET_USED(vid);
699
700         return -1;
701 }
702
703 static int
704 sfc_vdpa_get_vfio_device_fd(int vid)
705 {
706         struct rte_vdpa_device *vdpa_dev;
707         struct sfc_vdpa_ops_data *ops_data;
708         int vfio_dev_fd;
709         void *dev;
710
711         vdpa_dev = rte_vhost_get_vdpa_device(vid);
712
713         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
714         if (ops_data == NULL)
715                 return -1;
716
717         dev = ops_data->dev_handle;
718         vfio_dev_fd = sfc_vdpa_adapter_by_dev_handle(dev)->vfio_dev_fd;
719
720         sfc_vdpa_info(dev, "vDPA ops get_vfio_device_fd :: vfio fd : %d",
721                       vfio_dev_fd);
722
723         return vfio_dev_fd;
724 }
725
726 static int
727 sfc_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
728 {
729         int ret;
730         efx_nic_t *nic;
731         int vfio_dev_fd;
732         efx_rc_t rc;
733         unsigned int bar_offset;
734         struct rte_vdpa_device *vdpa_dev;
735         struct sfc_vdpa_ops_data *ops_data;
736         struct vfio_region_info reg = { .argsz = sizeof(reg) };
737         const efx_nic_cfg_t *encp;
738         int max_vring_cnt;
739         int64_t len;
740         void *dev;
741
742         vdpa_dev = rte_vhost_get_vdpa_device(vid);
743
744         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
745         if (ops_data == NULL)
746                 return -1;
747
748         dev = ops_data->dev_handle;
749
750         vfio_dev_fd = sfc_vdpa_adapter_by_dev_handle(dev)->vfio_dev_fd;
751         max_vring_cnt =
752                 (sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count * 2);
753
754         nic = sfc_vdpa_adapter_by_dev_handle(ops_data->dev_handle)->nic;
755         encp = efx_nic_cfg_get(nic);
756
757         if (qid >= max_vring_cnt) {
758                 sfc_vdpa_err(dev, "invalid qid : %d", qid);
759                 return -1;
760         }
761
762         if (ops_data->vq_cxt[qid].enable != B_TRUE) {
763                 sfc_vdpa_err(dev, "vq is not enabled");
764                 return -1;
765         }
766
767         rc = efx_virtio_get_doorbell_offset(ops_data->vq_cxt[qid].vq,
768                                             &bar_offset);
769         if (rc != 0) {
770                 sfc_vdpa_err(dev, "failed to get doorbell offset: %s",
771                              rte_strerror(rc));
772                 return rc;
773         }
774
775         reg.index = sfc_vdpa_adapter_by_dev_handle(dev)->mem_bar.esb_rid;
776         ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
777         if (ret != 0) {
778                 sfc_vdpa_err(dev, "could not get device region info: %s",
779                              strerror(errno));
780                 return ret;
781         }
782
783         *offset = reg.offset + bar_offset;
784
785         len = (1U << encp->enc_vi_window_shift) / 2;
786         if (len >= sysconf(_SC_PAGESIZE)) {
787                 *size = sysconf(_SC_PAGESIZE);
788         } else {
789                 sfc_vdpa_err(dev, "invalid VI window size : 0x%" PRIx64, len);
790                 return -1;
791         }
792
793         sfc_vdpa_info(dev, "vDPA ops get_notify_area :: offset : 0x%" PRIx64,
794                       *offset);
795
796         return 0;
797 }
798
799 static struct rte_vdpa_dev_ops sfc_vdpa_ops = {
800         .get_queue_num = sfc_vdpa_get_queue_num,
801         .get_features = sfc_vdpa_get_features,
802         .get_protocol_features = sfc_vdpa_get_protocol_features,
803         .dev_conf = sfc_vdpa_dev_config,
804         .dev_close = sfc_vdpa_dev_close,
805         .set_vring_state = sfc_vdpa_set_vring_state,
806         .set_features = sfc_vdpa_set_features,
807         .get_vfio_device_fd = sfc_vdpa_get_vfio_device_fd,
808         .get_notify_area = sfc_vdpa_get_notify_area,
809 };
810
811 struct sfc_vdpa_ops_data *
812 sfc_vdpa_device_init(void *dev_handle, enum sfc_vdpa_context context)
813 {
814         struct sfc_vdpa_ops_data *ops_data;
815         struct rte_pci_device *pci_dev;
816         int rc;
817
818         /* Create vDPA ops context */
819         ops_data = rte_zmalloc("vdpa", sizeof(struct sfc_vdpa_ops_data), 0);
820         if (ops_data == NULL)
821                 return NULL;
822
823         ops_data->vdpa_context = context;
824         ops_data->dev_handle = dev_handle;
825
826         pci_dev = sfc_vdpa_adapter_by_dev_handle(dev_handle)->pdev;
827
828         /* Register vDPA Device */
829         sfc_vdpa_log_init(dev_handle, "register vDPA device");
830         ops_data->vdpa_dev =
831                 rte_vdpa_register_device(&pci_dev->device, &sfc_vdpa_ops);
832         if (ops_data->vdpa_dev == NULL) {
833                 sfc_vdpa_err(dev_handle, "vDPA device registration failed");
834                 goto fail_register_device;
835         }
836
837         /* Read supported device features */
838         sfc_vdpa_log_init(dev_handle, "get device feature");
839         rc = sfc_vdpa_get_device_features(ops_data);
840         if (rc != 0)
841                 goto fail_get_dev_feature;
842
843         /* Driver features are superset of device supported feature
844          * and any additional features supported by the driver.
845          */
846         ops_data->drv_features =
847                 ops_data->dev_features | SFC_VDPA_DEFAULT_FEATURES;
848
849         ops_data->state = SFC_VDPA_STATE_INITIALIZED;
850
851         return ops_data;
852
853 fail_get_dev_feature:
854         rte_vdpa_unregister_device(ops_data->vdpa_dev);
855
856 fail_register_device:
857         rte_free(ops_data);
858         return NULL;
859 }
860
861 void
862 sfc_vdpa_device_fini(struct sfc_vdpa_ops_data *ops_data)
863 {
864         rte_vdpa_unregister_device(ops_data->vdpa_dev);
865
866         rte_free(ops_data);
867 }