Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2023 Corigine, Inc.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include <uapi/linux/vfio.h>
7 : :
8 : : #include <pthread.h>
9 : : #include <sys/epoll.h>
10 : : #include <sys/ioctl.h>
11 : : #include <unistd.h>
12 : :
13 : : #include <nfp_common_pci.h>
14 : : #include <nfp_dev.h>
15 : : #include <dev_vfio.h>
16 : : #include <rte_eal_paging.h>
17 : : #include <rte_malloc.h>
18 : : #include <vdpa_driver.h>
19 : :
20 : : #include "nfp_vdpa_core.h"
21 : : #include "nfp_vdpa_log.h"
22 : :
23 : : #define NFP_VDPA_DRIVER_NAME nfp_vdpa
24 : :
25 : : #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
26 : : sizeof(int) * (NFP_VDPA_MAX_QUEUES * 2 + 1))
27 : :
28 : : #define NFP_VDPA_USED_RING_LEN(size) \
29 : : ((size) * sizeof(struct vring_used_elem) + sizeof(struct vring_used))
30 : :
31 : : #define EPOLL_DATA_INTR 1
32 : :
33 : : struct nfp_vdpa_dev {
34 : : struct rte_pci_device *pci_dev;
35 : : struct rte_vdpa_device *vdev;
36 : : struct nfp_vdpa_hw hw;
37 : :
38 : : int vfio_container_fd;
39 : : int vfio_dev_fd;
40 : :
41 : : rte_thread_t tid; /**< Thread for notify relay */
42 : : int epoll_fd;
43 : :
44 : : int vid;
45 : : uint16_t max_queues;
46 : : RTE_ATOMIC(uint32_t) started;
47 : : RTE_ATOMIC(uint32_t) dev_attached;
48 : : RTE_ATOMIC(uint32_t) running;
49 : : rte_spinlock_t lock;
50 : :
51 : : /** Eventfd for used ring interrupt */
52 : : int intr_fd[NFP_VDPA_MAX_QUEUES * 2];
53 : : };
54 : :
55 : : struct nfp_vdpa_dev_node {
56 : : TAILQ_ENTRY(nfp_vdpa_dev_node) next;
57 : : struct nfp_vdpa_dev *device;
58 : : };
59 : :
60 : : TAILQ_HEAD(vdpa_dev_list_head, nfp_vdpa_dev_node);
61 : :
62 : : static struct vdpa_dev_list_head vdpa_dev_list =
63 : : TAILQ_HEAD_INITIALIZER(vdpa_dev_list);
64 : :
65 : : static pthread_mutex_t vdpa_list_lock = PTHREAD_MUTEX_INITIALIZER;
66 : :
67 : : static struct nfp_vdpa_dev_node *
68 : 0 : nfp_vdpa_find_node_by_vdev(struct rte_vdpa_device *vdev)
69 : : {
70 : : bool found = false;
71 : : struct nfp_vdpa_dev_node *node;
72 : :
73 : 0 : pthread_mutex_lock(&vdpa_list_lock);
74 : :
75 [ # # ]: 0 : TAILQ_FOREACH(node, &vdpa_dev_list, next) {
76 [ # # ]: 0 : if (vdev == node->device->vdev) {
77 : : found = true;
78 : : break;
79 : : }
80 : : }
81 : :
82 : 0 : pthread_mutex_unlock(&vdpa_list_lock);
83 : :
84 [ # # ]: 0 : if (found)
85 : 0 : return node;
86 : :
87 : : return NULL;
88 : : }
89 : :
90 : : static struct nfp_vdpa_dev_node *
91 : 0 : nfp_vdpa_find_node_by_pdev(struct rte_pci_device *pdev)
92 : : {
93 : : bool found = false;
94 : : struct nfp_vdpa_dev_node *node;
95 : :
96 : 0 : pthread_mutex_lock(&vdpa_list_lock);
97 : :
98 [ # # ]: 0 : TAILQ_FOREACH(node, &vdpa_dev_list, next) {
99 [ # # ]: 0 : if (pdev == node->device->pci_dev) {
100 : : found = true;
101 : : break;
102 : : }
103 : : }
104 : :
105 : 0 : pthread_mutex_unlock(&vdpa_list_lock);
106 : :
107 [ # # ]: 0 : if (found)
108 : 0 : return node;
109 : :
110 : : return NULL;
111 : : }
112 : :
113 : : static int
114 : 0 : nfp_vdpa_vfio_setup(struct nfp_vdpa_dev *device)
115 : : {
116 : : int ret;
117 : 0 : char dev_name[RTE_DEV_NAME_MAX_LEN] = {0};
118 : 0 : struct rte_pci_device *pci_dev = device->pci_dev;
119 : :
120 : 0 : rte_pci_device_name(&pci_dev->addr, dev_name, RTE_DEV_NAME_MAX_LEN);
121 : :
122 : 0 : device->vfio_container_fd = dev_vfio_container_create();
123 [ # # ]: 0 : if (device->vfio_container_fd < 0)
124 : : return -1;
125 : :
126 : 0 : ret = dev_vfio_container_assign_device(device->vfio_container_fd,
127 : : rte_pci_get_sysfs_path(), dev_name);
128 [ # # ]: 0 : if (ret < 0)
129 : 0 : goto container_destroy;
130 : :
131 : 0 : DRV_VDPA_LOG(DEBUG, "container_fd=%d", device->vfio_container_fd);
132 : :
133 : 0 : ret = rte_pci_map_device(pci_dev);
134 [ # # ]: 0 : if (ret != 0)
135 : 0 : goto container_destroy;
136 : :
137 : 0 : device->vfio_dev_fd = rte_intr_dev_fd_get(pci_dev->intr_handle);
138 : :
139 : 0 : return 0;
140 : :
141 : 0 : container_destroy:
142 : 0 : dev_vfio_container_destroy(device->vfio_container_fd);
143 : :
144 : 0 : return -1;
145 : : }
146 : :
147 : : static void
148 : : nfp_vdpa_vfio_teardown(struct nfp_vdpa_dev *device)
149 : : {
150 : 0 : rte_pci_unmap_device(device->pci_dev);
151 : 0 : dev_vfio_container_destroy(device->vfio_container_fd);
152 : 0 : }
153 : :
154 : : static int
155 : 0 : nfp_vdpa_dma_do_unmap(struct rte_vhost_memory *mem,
156 : : uint32_t times,
157 : : int vfio_container_fd)
158 : : {
159 : : uint32_t i;
160 : : int ret = 0;
161 : : struct rte_vhost_mem_region *region;
162 : :
163 [ # # ]: 0 : for (i = 0; i < times; i++) {
164 : : region = &mem->regions[i];
165 : :
166 : 0 : ret = dev_vfio_container_dma_unmap(vfio_container_fd,
167 : : region->host_user_addr, region->guest_phys_addr,
168 : : region->size);
169 [ # # ]: 0 : if (ret < 0) {
170 : : /* Here should not return, even error happened. */
171 : 0 : DRV_VDPA_LOG(ERR, "DMA unmap failed. Times: %u.", i);
172 : : }
173 : : }
174 : :
175 : 0 : return ret;
176 : : }
177 : :
178 : : static int
179 : 0 : nfp_vdpa_dma_do_map(struct rte_vhost_memory *mem,
180 : : uint32_t times,
181 : : int vfio_container_fd)
182 : : {
183 : : int ret;
184 : : uint32_t i;
185 : : struct rte_vhost_mem_region *region;
186 : :
187 [ # # ]: 0 : for (i = 0; i < times; i++) {
188 : : region = &mem->regions[i];
189 : :
190 : 0 : ret = dev_vfio_container_dma_map(vfio_container_fd,
191 : : region->host_user_addr, region->guest_phys_addr,
192 : : region->size);
193 [ # # ]: 0 : if (ret < 0) {
194 : 0 : DRV_VDPA_LOG(ERR, "DMA map failed.");
195 : 0 : nfp_vdpa_dma_do_unmap(mem, i, vfio_container_fd);
196 : 0 : return ret;
197 : : }
198 : : }
199 : :
200 : : return 0;
201 : : }
202 : :
203 : : static int
204 : 0 : nfp_vdpa_dma_map(struct nfp_vdpa_dev *device,
205 : : bool do_map)
206 : : {
207 : : int ret;
208 : : int vfio_container_fd;
209 : 0 : struct rte_vhost_memory *mem = NULL;
210 : :
211 : 0 : ret = rte_vhost_get_mem_table(device->vid, &mem);
212 [ # # ]: 0 : if (ret < 0) {
213 : 0 : DRV_VDPA_LOG(ERR, "Failed to get memory layout.");
214 : 0 : return ret;
215 : : }
216 : :
217 : 0 : vfio_container_fd = device->vfio_container_fd;
218 : 0 : DRV_VDPA_LOG(DEBUG, "The vfio_container_fd %d.", vfio_container_fd);
219 : :
220 [ # # ]: 0 : if (do_map)
221 : 0 : ret = nfp_vdpa_dma_do_map(mem, mem->nregions, vfio_container_fd);
222 : : else
223 : 0 : ret = nfp_vdpa_dma_do_unmap(mem, mem->nregions, vfio_container_fd);
224 : :
225 : 0 : free(mem);
226 : :
227 : 0 : return ret;
228 : : }
229 : :
230 : : static uint64_t
231 : 0 : nfp_vdpa_qva_to_gpa(int vid,
232 : : uint64_t qva)
233 : : {
234 : : int ret;
235 : : uint32_t i;
236 : : uint64_t gpa = 0;
237 : 0 : struct rte_vhost_memory *mem = NULL;
238 : : struct rte_vhost_mem_region *region;
239 : :
240 : 0 : ret = rte_vhost_get_mem_table(vid, &mem);
241 [ # # ]: 0 : if (ret < 0) {
242 : 0 : DRV_VDPA_LOG(ERR, "Failed to get memory layout.");
243 : 0 : return gpa;
244 : : }
245 : :
246 [ # # ]: 0 : for (i = 0; i < mem->nregions; i++) {
247 : : region = &mem->regions[i];
248 : :
249 [ # # ]: 0 : if (qva >= region->host_user_addr &&
250 [ # # ]: 0 : qva < region->host_user_addr + region->size) {
251 : 0 : gpa = qva - region->host_user_addr + region->guest_phys_addr;
252 : 0 : break;
253 : : }
254 : : }
255 : :
256 : 0 : free(mem);
257 : :
258 : 0 : return gpa;
259 : : }
260 : :
261 : : static void
262 : 0 : nfp_vdpa_relay_vring_free(struct nfp_vdpa_dev *device,
263 : : uint16_t vring_index)
264 : : {
265 : : uint16_t i;
266 : : uint64_t size;
267 : : struct rte_vhost_vring vring;
268 : : uint64_t m_vring_iova = NFP_VDPA_RELAY_VRING;
269 : :
270 [ # # ]: 0 : for (i = 0; i < vring_index; i++) {
271 : 0 : rte_vhost_get_vhost_vring(device->vid, i, &vring);
272 : :
273 : 0 : size = RTE_ALIGN_CEIL(vring_size(vring.size, rte_mem_page_size()),
274 : : rte_mem_page_size());
275 : 0 : dev_vfio_container_dma_unmap(device->vfio_container_fd,
276 : 0 : (uint64_t)(uintptr_t)device->hw.m_vring[i].desc,
277 : : m_vring_iova, size);
278 : :
279 : 0 : rte_free(device->hw.m_vring[i].desc);
280 : 0 : m_vring_iova += size;
281 : : }
282 : 0 : }
283 : :
284 : : static int
285 : 0 : nfp_vdpa_relay_vring_alloc(struct nfp_vdpa_dev *device)
286 : : {
287 : : int ret;
288 : : uint16_t i;
289 : : uint64_t size;
290 : : void *vring_buf;
291 : : uint64_t page_size;
292 : : struct rte_vhost_vring vring;
293 : : struct nfp_vdpa_hw *vdpa_hw = &device->hw;
294 : : uint64_t m_vring_iova = NFP_VDPA_RELAY_VRING;
295 : :
296 : 0 : page_size = rte_mem_page_size();
297 : :
298 [ # # ]: 0 : for (i = 0; i < vdpa_hw->nr_vring; i++) {
299 : 0 : rte_vhost_get_vhost_vring(device->vid, i, &vring);
300 : :
301 : 0 : size = RTE_ALIGN_CEIL(vring_size(vring.size, page_size), page_size);
302 : 0 : vring_buf = rte_zmalloc("nfp_vdpa_relay", size, page_size);
303 [ # # ]: 0 : if (vring_buf == NULL)
304 : 0 : goto vring_free_all;
305 : :
306 : 0 : vring_init(&vdpa_hw->m_vring[i], vring.size, vring_buf, page_size);
307 : :
308 : 0 : ret = dev_vfio_container_dma_map(device->vfio_container_fd,
309 : : (uint64_t)(uintptr_t)vring_buf, m_vring_iova, size);
310 [ # # ]: 0 : if (ret != 0) {
311 : 0 : DRV_VDPA_LOG(ERR, "vDPA vring relay dma map failed.");
312 : 0 : goto vring_free_one;
313 : : }
314 : :
315 : 0 : m_vring_iova += size;
316 : : }
317 : :
318 : : return 0;
319 : :
320 : : vring_free_one:
321 : 0 : rte_free(device->hw.m_vring[i].desc);
322 : 0 : vring_free_all:
323 : 0 : nfp_vdpa_relay_vring_free(device, i);
324 : :
325 : 0 : return -ENOSPC;
326 : : }
327 : :
328 : : static int
329 : 0 : nfp_vdpa_start(struct nfp_vdpa_dev *device,
330 : : bool relay)
331 : : {
332 : : int ret;
333 : : int vid;
334 : : uint16_t i;
335 : : uint64_t gpa;
336 : : uint16_t size;
337 : : struct rte_vhost_vring vring;
338 : 0 : struct nfp_vdpa_hw *vdpa_hw = &device->hw;
339 : : uint64_t m_vring_iova = NFP_VDPA_RELAY_VRING;
340 : :
341 : 0 : vid = device->vid;
342 : 0 : vdpa_hw->nr_vring = rte_vhost_get_vring_num(vid);
343 : :
344 : 0 : ret = rte_vhost_get_negotiated_features(vid, &vdpa_hw->req_features);
345 [ # # ]: 0 : if (ret != 0)
346 : : return ret;
347 : :
348 [ # # ]: 0 : if (relay) {
349 : 0 : ret = nfp_vdpa_relay_vring_alloc(device);
350 [ # # ]: 0 : if (ret != 0)
351 : : return ret;
352 : : }
353 : :
354 [ # # ]: 0 : for (i = 0; i < vdpa_hw->nr_vring; i++) {
355 : 0 : ret = rte_vhost_get_vhost_vring(vid, i, &vring);
356 [ # # ]: 0 : if (ret != 0)
357 : 0 : goto relay_vring_free;
358 : :
359 : 0 : gpa = nfp_vdpa_qva_to_gpa(vid, (uint64_t)(uintptr_t)vring.desc);
360 [ # # ]: 0 : if (gpa == 0) {
361 : 0 : DRV_VDPA_LOG(ERR, "Fail to get GPA for descriptor ring.");
362 : 0 : goto relay_vring_free;
363 : : }
364 : :
365 : 0 : vdpa_hw->vring[i].desc = gpa;
366 : :
367 : 0 : gpa = nfp_vdpa_qva_to_gpa(vid, (uint64_t)(uintptr_t)vring.avail);
368 [ # # ]: 0 : if (gpa == 0) {
369 : 0 : DRV_VDPA_LOG(ERR, "Fail to get GPA for available ring.");
370 : 0 : goto relay_vring_free;
371 : : }
372 : :
373 : 0 : vdpa_hw->vring[i].avail = gpa;
374 : :
375 : : /* Direct I/O for Tx queue, relay for Rx queue */
376 [ # # # # ]: 0 : if (relay && ((i & 1) == 0)) {
377 : 0 : vdpa_hw->vring[i].used = m_vring_iova +
378 : 0 : (char *)vdpa_hw->m_vring[i].used -
379 : 0 : (char *)vdpa_hw->m_vring[i].desc;
380 : :
381 : 0 : ret = rte_vhost_get_vring_base(vid, i,
382 : 0 : &vdpa_hw->m_vring[i].avail->idx,
383 : 0 : &vdpa_hw->m_vring[i].used->idx);
384 [ # # ]: 0 : if (ret != 0)
385 : 0 : goto relay_vring_free;
386 : : } else {
387 : 0 : gpa = nfp_vdpa_qva_to_gpa(vid, (uint64_t)(uintptr_t)vring.used);
388 [ # # ]: 0 : if (gpa == 0) {
389 : 0 : DRV_VDPA_LOG(ERR, "Fail to get GPA for used ring.");
390 : 0 : goto relay_vring_free;
391 : : }
392 : :
393 : 0 : vdpa_hw->vring[i].used = gpa;
394 : : }
395 : :
396 : 0 : vdpa_hw->vring[i].size = vring.size;
397 : :
398 [ # # ]: 0 : if (relay) {
399 : 0 : size = RTE_ALIGN_CEIL(vring_size(vring.size,
400 : : rte_mem_page_size()), rte_mem_page_size());
401 : 0 : m_vring_iova += size;
402 : : }
403 : :
404 : 0 : ret = rte_vhost_get_vring_base(vid, i,
405 : : &vdpa_hw->vring[i].last_avail_idx,
406 : : &vdpa_hw->vring[i].last_used_idx);
407 [ # # ]: 0 : if (ret != 0)
408 : 0 : goto relay_vring_free;
409 : : }
410 : :
411 [ # # ]: 0 : if (relay)
412 : 0 : return nfp_vdpa_relay_hw_start(&device->hw, vid);
413 : : else
414 : 0 : return nfp_vdpa_hw_start(&device->hw, vid);
415 : :
416 : 0 : relay_vring_free:
417 [ # # ]: 0 : if (relay)
418 : 0 : nfp_vdpa_relay_vring_free(device, vdpa_hw->nr_vring);
419 : :
420 : : return -EFAULT;
421 : : }
422 : :
423 : : static void
424 : 0 : nfp_vdpa_update_used_ring(struct nfp_vdpa_dev *dev,
425 : : uint16_t qid)
426 : : {
427 : 0 : rte_vdpa_relay_vring_used(dev->vid, qid, &dev->hw.m_vring[qid]);
428 : 0 : rte_vhost_vring_call(dev->vid, qid);
429 : 0 : }
430 : :
431 : : static void
432 : 0 : nfp_vdpa_relay_stop(struct nfp_vdpa_dev *device)
433 : : {
434 : : int vid;
435 : : uint32_t i;
436 : : uint64_t len;
437 : : struct rte_vhost_vring vring;
438 : 0 : struct nfp_vdpa_hw *vdpa_hw = &device->hw;
439 : :
440 : 0 : nfp_vdpa_hw_stop(vdpa_hw);
441 : :
442 : 0 : vid = device->vid;
443 [ # # ]: 0 : for (i = 0; i < vdpa_hw->nr_vring; i++) {
444 : : /* Synchronize remaining new used entries if any */
445 [ # # ]: 0 : if ((i & 1) == 0)
446 : 0 : nfp_vdpa_update_used_ring(device, i);
447 : :
448 : 0 : rte_vhost_get_vhost_vring(vid, i, &vring);
449 : 0 : len = NFP_VDPA_USED_RING_LEN(vring.size);
450 : 0 : vdpa_hw->vring[i].last_avail_idx = vring.avail->idx;
451 : 0 : vdpa_hw->vring[i].last_used_idx = vring.used->idx;
452 : :
453 : 0 : rte_vhost_set_vring_base(vid, i,
454 : : vdpa_hw->vring[i].last_avail_idx,
455 : : vdpa_hw->vring[i].last_used_idx);
456 : :
457 : 0 : rte_vhost_log_used_vring(vid, i, 0, len);
458 : :
459 [ # # ]: 0 : if (vring.used->idx != vring.avail->idx)
460 : 0 : rte_atomic_store_explicit(
461 : : (unsigned short __rte_atomic *)&vring.used->idx,
462 : : vring.avail->idx, rte_memory_order_release);
463 : : }
464 : :
465 : 0 : nfp_vdpa_relay_vring_free(device, vdpa_hw->nr_vring);
466 : 0 : }
467 : :
468 : : static void
469 : 0 : nfp_vdpa_stop(struct nfp_vdpa_dev *device,
470 : : bool relay)
471 : : {
472 : : int vid;
473 : : uint32_t i;
474 : 0 : struct nfp_vdpa_hw *vdpa_hw = &device->hw;
475 : :
476 : 0 : nfp_vdpa_hw_stop(vdpa_hw);
477 : :
478 : 0 : vid = device->vid;
479 [ # # ]: 0 : if (relay)
480 : 0 : nfp_vdpa_relay_stop(device);
481 : : else
482 [ # # ]: 0 : for (i = 0; i < vdpa_hw->nr_vring; i++)
483 : 0 : rte_vhost_set_vring_base(vid, i,
484 : 0 : vdpa_hw->vring[i].last_avail_idx,
485 : 0 : vdpa_hw->vring[i].last_used_idx);
486 : :
487 : 0 : }
488 : :
489 : : static int
490 : 0 : nfp_vdpa_enable_vfio_intr(struct nfp_vdpa_dev *device,
491 : : bool relay)
492 : : {
493 : : int fd;
494 : : int ret;
495 : : uint16_t i;
496 : : int *fd_ptr;
497 : : uint16_t nr_vring;
498 : : struct vfio_irq_set *irq_set;
499 : : struct rte_vhost_vring vring;
500 : : char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
501 : :
502 : 0 : nr_vring = rte_vhost_get_vring_num(device->vid);
503 : :
504 : : irq_set = (struct vfio_irq_set *)irq_set_buf;
505 : 0 : irq_set->argsz = sizeof(irq_set_buf);
506 : 0 : irq_set->count = nr_vring + 1;
507 : 0 : irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
508 : 0 : irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
509 : 0 : irq_set->start = 0;
510 : :
511 : : fd_ptr = (int *)&irq_set->data;
512 : 0 : fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = rte_intr_fd_get(device->pci_dev->intr_handle);
513 : :
514 [ # # ]: 0 : for (i = 0; i < nr_vring; i++)
515 : 0 : device->intr_fd[i] = -1;
516 : :
517 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
518 : 0 : rte_vhost_get_vhost_vring(device->vid, i, &vring);
519 : 0 : fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
520 : : }
521 : :
522 [ # # ]: 0 : if (relay) {
523 [ # # ]: 0 : for (i = 0; i < nr_vring; i += 2) {
524 : 0 : fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
525 [ # # ]: 0 : if (fd < 0) {
526 : 0 : DRV_VDPA_LOG(ERR, "Can't setup eventfd.");
527 : 0 : return -EINVAL;
528 : : }
529 : :
530 : 0 : device->intr_fd[i] = fd;
531 : 0 : fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = fd;
532 : : }
533 : : }
534 : :
535 : 0 : ret = ioctl(device->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
536 [ # # ]: 0 : if (ret != 0) {
537 : 0 : DRV_VDPA_LOG(ERR, "Error enabling MSI-X interrupts.");
538 : 0 : return -EIO;
539 : : }
540 : :
541 : : return 0;
542 : : }
543 : :
544 : : static int
545 : 0 : nfp_vdpa_disable_vfio_intr(struct nfp_vdpa_dev *device)
546 : : {
547 : : int ret;
548 : : struct vfio_irq_set *irq_set;
549 : : char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
550 : :
551 : : irq_set = (struct vfio_irq_set *)irq_set_buf;
552 : 0 : irq_set->argsz = sizeof(irq_set_buf);
553 : 0 : irq_set->count = 0;
554 : 0 : irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
555 : 0 : irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
556 : 0 : irq_set->start = 0;
557 : :
558 : 0 : ret = ioctl(device->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
559 [ # # ]: 0 : if (ret != 0) {
560 : 0 : DRV_VDPA_LOG(ERR, "Error disabling MSI-X interrupts.");
561 : 0 : return -EIO;
562 : : }
563 : :
564 : : return 0;
565 : : }
566 : :
567 : : static void
568 : 0 : nfp_vdpa_read_kickfd(int kickfd)
569 : : {
570 : : int bytes;
571 : : uint64_t buf;
572 : :
573 : : for (;;) {
574 : 0 : bytes = read(kickfd, &buf, 8);
575 [ # # ]: 0 : if (bytes >= 0)
576 : : break;
577 : :
578 [ # # ]: 0 : if (errno != EINTR && errno != EWOULDBLOCK &&
579 : : errno != EAGAIN) {
580 : 0 : DRV_VDPA_LOG(ERR, "Error reading kickfd.");
581 : 0 : break;
582 : : }
583 : : }
584 : 0 : }
585 : :
586 : : static int
587 : 0 : nfp_vdpa_notify_epoll_ctl(uint32_t queue_num,
588 : : struct nfp_vdpa_dev *device)
589 : : {
590 : : int ret;
591 : : uint32_t qid;
592 : :
593 [ # # ]: 0 : for (qid = 0; qid < queue_num; qid++) {
594 : : struct epoll_event ev;
595 : : struct rte_vhost_vring vring;
596 : :
597 : 0 : ev.events = EPOLLIN | EPOLLPRI;
598 : 0 : rte_vhost_get_vhost_vring(device->vid, qid, &vring);
599 : 0 : ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
600 : 0 : ret = epoll_ctl(device->epoll_fd, EPOLL_CTL_ADD, vring.kickfd, &ev);
601 [ # # ]: 0 : if (ret < 0) {
602 : 0 : DRV_VDPA_LOG(ERR, "Epoll add error for queue %d.", qid);
603 : 0 : return ret;
604 : : }
605 : : }
606 : :
607 : : return 0;
608 : : }
609 : :
610 : : static int
611 : 0 : nfp_vdpa_notify_epoll_wait(uint32_t queue_num,
612 : : struct nfp_vdpa_dev *device)
613 : : {
614 : : int i;
615 : : int fds;
616 : : int kickfd;
617 : : uint32_t qid;
618 : : struct epoll_event events[NFP_VDPA_MAX_QUEUES * 2];
619 : :
620 : : for (;;) {
621 : 0 : fds = epoll_wait(device->epoll_fd, events, queue_num, -1);
622 [ # # ]: 0 : if (fds < 0) {
623 [ # # ]: 0 : if (errno == EINTR)
624 : 0 : continue;
625 : :
626 : 0 : DRV_VDPA_LOG(ERR, "Epoll wait fail.");
627 : : return -EACCES;
628 : : }
629 : :
630 : 0 : for (i = 0; i < fds; i++) {
631 : 0 : qid = events[i].data.u32;
632 : 0 : kickfd = (uint32_t)(events[i].data.u64 >> 32);
633 : :
634 : 0 : nfp_vdpa_read_kickfd(kickfd);
635 : 0 : nfp_vdpa_notify_queue(&device->hw, qid);
636 : : }
637 : : }
638 : :
639 : : return 0;
640 : : }
641 : :
642 : : static uint32_t
643 : 0 : nfp_vdpa_notify_relay(void *arg)
644 : : {
645 : : int ret;
646 : : int epoll_fd;
647 : : uint32_t queue_num;
648 : : struct nfp_vdpa_dev *device = arg;
649 : :
650 : 0 : epoll_fd = epoll_create(NFP_VDPA_MAX_QUEUES * 2);
651 [ # # ]: 0 : if (epoll_fd < 0) {
652 : 0 : DRV_VDPA_LOG(ERR, "Failed to create epoll instance.");
653 : 0 : return 1;
654 : : }
655 : :
656 : 0 : device->epoll_fd = epoll_fd;
657 : :
658 : 0 : queue_num = rte_vhost_get_vring_num(device->vid);
659 : :
660 : 0 : ret = nfp_vdpa_notify_epoll_ctl(queue_num, device);
661 [ # # ]: 0 : if (ret != 0)
662 : 0 : goto notify_exit;
663 : :
664 : 0 : ret = nfp_vdpa_notify_epoll_wait(queue_num, device);
665 [ # # ]: 0 : if (ret != 0)
666 : 0 : goto notify_exit;
667 : :
668 : : return 0;
669 : :
670 : 0 : notify_exit:
671 : 0 : close(device->epoll_fd);
672 : 0 : device->epoll_fd = -1;
673 : :
674 : 0 : return 1;
675 : : }
676 : :
677 : : static int
678 : 0 : nfp_vdpa_setup_notify_relay(struct nfp_vdpa_dev *device)
679 : : {
680 : : int ret;
681 : : char name[RTE_THREAD_INTERNAL_NAME_SIZE];
682 : :
683 : 0 : snprintf(name, sizeof(name), "nfp-noti%d", device->vid);
684 : 0 : ret = rte_thread_create_internal_control(&device->tid, name,
685 : : nfp_vdpa_notify_relay, (void *)device);
686 [ # # ]: 0 : if (ret != 0) {
687 : 0 : DRV_VDPA_LOG(ERR, "Failed to create notify relay pthread.");
688 : 0 : return -1;
689 : : }
690 : :
691 : : return 0;
692 : : }
693 : :
694 : : static void
695 : 0 : nfp_vdpa_unset_notify_relay(struct nfp_vdpa_dev *device)
696 : : {
697 [ # # ]: 0 : if (device->tid.opaque_id != 0) {
698 : 0 : pthread_cancel((pthread_t)device->tid.opaque_id);
699 : 0 : rte_thread_join(device->tid, NULL);
700 : 0 : device->tid.opaque_id = 0;
701 : : }
702 : :
703 [ # # ]: 0 : if (device->epoll_fd >= 0) {
704 : 0 : close(device->epoll_fd);
705 : 0 : device->epoll_fd = -1;
706 : : }
707 : 0 : }
708 : :
709 : : static int
710 : 0 : update_datapath(struct nfp_vdpa_dev *device)
711 : : {
712 : : int ret;
713 : :
714 : 0 : rte_spinlock_lock(&device->lock);
715 : :
716 [ # # ]: 0 : if ((rte_atomic_load_explicit(&device->running, rte_memory_order_relaxed) == 0) &&
717 [ # # ]: 0 : (rte_atomic_load_explicit(&device->started,
718 : 0 : rte_memory_order_relaxed) != 0) &&
719 [ # # ]: 0 : (rte_atomic_load_explicit(&device->dev_attached,
720 : : rte_memory_order_relaxed) != 0)) {
721 : 0 : ret = nfp_vdpa_dma_map(device, true);
722 [ # # ]: 0 : if (ret != 0)
723 : 0 : goto unlock_exit;
724 : :
725 : 0 : ret = nfp_vdpa_enable_vfio_intr(device, false);
726 [ # # ]: 0 : if (ret != 0)
727 : 0 : goto dma_map_rollback;
728 : :
729 : 0 : ret = nfp_vdpa_start(device, false);
730 [ # # ]: 0 : if (ret != 0)
731 : 0 : goto disable_vfio_intr;
732 : :
733 : 0 : ret = nfp_vdpa_setup_notify_relay(device);
734 [ # # ]: 0 : if (ret != 0)
735 : 0 : goto vdpa_stop;
736 : :
737 : 0 : rte_atomic_store_explicit(&device->running, 1, rte_memory_order_relaxed);
738 [ # # ]: 0 : } else if ((rte_atomic_load_explicit(&device->running, rte_memory_order_relaxed) != 0) &&
739 [ # # ]: 0 : ((rte_atomic_load_explicit(&device->started,
740 : 0 : rte_memory_order_relaxed) != 0) ||
741 [ # # ]: 0 : (rte_atomic_load_explicit(&device->dev_attached,
742 : : rte_memory_order_relaxed) != 0))) {
743 : 0 : nfp_vdpa_unset_notify_relay(device);
744 : :
745 : 0 : nfp_vdpa_stop(device, false);
746 : :
747 : 0 : ret = nfp_vdpa_disable_vfio_intr(device);
748 [ # # ]: 0 : if (ret != 0)
749 : 0 : goto unlock_exit;
750 : :
751 : 0 : ret = nfp_vdpa_dma_map(device, false);
752 [ # # ]: 0 : if (ret != 0)
753 : 0 : goto unlock_exit;
754 : :
755 : 0 : rte_atomic_store_explicit(&device->running, 0, rte_memory_order_relaxed);
756 : : }
757 : :
758 : : rte_spinlock_unlock(&device->lock);
759 : 0 : return 0;
760 : :
761 : : vdpa_stop:
762 : 0 : nfp_vdpa_stop(device, false);
763 : 0 : disable_vfio_intr:
764 : 0 : nfp_vdpa_disable_vfio_intr(device);
765 : 0 : dma_map_rollback:
766 : 0 : nfp_vdpa_dma_map(device, false);
767 : 0 : unlock_exit:
768 : : rte_spinlock_unlock(&device->lock);
769 : 0 : return ret;
770 : : }
771 : :
772 : : static int
773 : 0 : nfp_vdpa_vring_epoll_ctl(uint32_t queue_num,
774 : : struct nfp_vdpa_dev *device)
775 : : {
776 : : int ret;
777 : : uint32_t qid;
778 : : struct epoll_event ev;
779 : : struct rte_vhost_vring vring;
780 : :
781 [ # # ]: 0 : for (qid = 0; qid < queue_num; qid++) {
782 : 0 : ev.events = EPOLLIN | EPOLLPRI;
783 : 0 : rte_vhost_get_vhost_vring(device->vid, qid, &vring);
784 : 0 : ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
785 : 0 : ret = epoll_ctl(device->epoll_fd, EPOLL_CTL_ADD, vring.kickfd, &ev);
786 [ # # ]: 0 : if (ret < 0) {
787 : 0 : DRV_VDPA_LOG(ERR, "Epoll add error for queue %u.", qid);
788 : 0 : return ret;
789 : : }
790 : : }
791 : :
792 : : /* vDPA driver interrupt */
793 [ # # ]: 0 : for (qid = 0; qid < queue_num; qid += 2) {
794 : 0 : ev.events = EPOLLIN | EPOLLPRI;
795 : : /* Leave a flag to mark it's for interrupt */
796 : 0 : ev.data.u64 = EPOLL_DATA_INTR | qid << 1 |
797 : 0 : (uint64_t)device->intr_fd[qid] << 32;
798 : 0 : ret = epoll_ctl(device->epoll_fd, EPOLL_CTL_ADD,
799 : : device->intr_fd[qid], &ev);
800 [ # # ]: 0 : if (ret < 0) {
801 : 0 : DRV_VDPA_LOG(ERR, "Epoll add error for queue %u.", qid);
802 : 0 : return ret;
803 : : }
804 : :
805 : 0 : nfp_vdpa_update_used_ring(device, qid);
806 : : }
807 : :
808 : : return 0;
809 : : }
810 : :
811 : : static int
812 : 0 : nfp_vdpa_vring_epoll_wait(uint32_t queue_num,
813 : : struct nfp_vdpa_dev *device)
814 : : {
815 : : int i;
816 : : int fds;
817 : : int kickfd;
818 : : uint32_t qid;
819 : : struct epoll_event events[NFP_VDPA_MAX_QUEUES * 2];
820 : :
821 : : for (;;) {
822 : 0 : fds = epoll_wait(device->epoll_fd, events, queue_num * 2, -1);
823 [ # # ]: 0 : if (fds < 0) {
824 [ # # ]: 0 : if (errno == EINTR)
825 : 0 : continue;
826 : :
827 : 0 : DRV_VDPA_LOG(ERR, "Epoll wait fail.");
828 : : return -EACCES;
829 : : }
830 : :
831 : 0 : for (i = 0; i < fds; i++) {
832 : 0 : qid = events[i].data.u32 >> 1;
833 : 0 : kickfd = (uint32_t)(events[i].data.u64 >> 32);
834 : :
835 : 0 : nfp_vdpa_read_kickfd(kickfd);
836 [ # # ]: 0 : if ((events[i].data.u32 & EPOLL_DATA_INTR) != 0) {
837 : 0 : nfp_vdpa_update_used_ring(device, qid);
838 : 0 : nfp_vdpa_irq_unmask(&device->hw);
839 : : } else {
840 : 0 : nfp_vdpa_notify_queue(&device->hw, qid);
841 : : }
842 : : }
843 : : }
844 : :
845 : : return 0;
846 : : }
847 : :
848 : : static uint32_t
849 : 0 : nfp_vdpa_vring_relay(void *arg)
850 : : {
851 : : int ret;
852 : : int epoll_fd;
853 : : uint16_t queue_id;
854 : : uint32_t queue_num;
855 : : struct nfp_vdpa_dev *device = arg;
856 : :
857 : 0 : epoll_fd = epoll_create(NFP_VDPA_MAX_QUEUES * 2);
858 [ # # ]: 0 : if (epoll_fd < 0) {
859 : 0 : DRV_VDPA_LOG(ERR, "failed to create epoll instance.");
860 : 0 : return 1;
861 : : }
862 : :
863 : 0 : device->epoll_fd = epoll_fd;
864 : :
865 : 0 : queue_num = rte_vhost_get_vring_num(device->vid);
866 : :
867 : 0 : ret = nfp_vdpa_vring_epoll_ctl(queue_num, device);
868 [ # # ]: 0 : if (ret != 0)
869 : 0 : goto notify_exit;
870 : :
871 : : /* Start relay with a first kick */
872 [ # # ]: 0 : for (queue_id = 0; queue_id < queue_num; queue_id++)
873 : 0 : nfp_vdpa_notify_queue(&device->hw, queue_id);
874 : :
875 : 0 : ret = nfp_vdpa_vring_epoll_wait(queue_num, device);
876 [ # # ]: 0 : if (ret != 0)
877 : 0 : goto notify_exit;
878 : :
879 : : return 0;
880 : :
881 : 0 : notify_exit:
882 : 0 : close(device->epoll_fd);
883 : 0 : device->epoll_fd = -1;
884 : :
885 : 0 : return 1;
886 : : }
887 : :
888 : : static int
889 : 0 : nfp_vdpa_setup_vring_relay(struct nfp_vdpa_dev *device)
890 : : {
891 : : int ret;
892 : : char name[RTE_THREAD_INTERNAL_NAME_SIZE];
893 : :
894 : 0 : snprintf(name, sizeof(name), "nfp_vring%d", device->vid);
895 : 0 : ret = rte_thread_create_internal_control(&device->tid, name,
896 : : nfp_vdpa_vring_relay, (void *)device);
897 [ # # ]: 0 : if (ret != 0) {
898 : 0 : DRV_VDPA_LOG(ERR, "Failed to create vring relay pthread.");
899 : 0 : return -EPERM;
900 : : }
901 : :
902 : : return 0;
903 : : }
904 : :
905 : : static int
906 : 0 : nfp_vdpa_sw_fallback(struct nfp_vdpa_dev *device)
907 : : {
908 : : int ret;
909 : 0 : int vid = device->vid;
910 : :
911 : : /* Stop the direct IO data path */
912 : 0 : nfp_vdpa_unset_notify_relay(device);
913 : 0 : nfp_vdpa_disable_vfio_intr(device);
914 : :
915 : 0 : ret = rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, false);
916 [ # # ]: 0 : if ((ret != 0) && (ret != -ENOTSUP)) {
917 : 0 : DRV_VDPA_LOG(ERR, "Unset the host notifier failed.");
918 : 0 : goto error;
919 : : }
920 : :
921 : : /* Setup interrupt for vring relay */
922 : 0 : ret = nfp_vdpa_enable_vfio_intr(device, true);
923 [ # # ]: 0 : if (ret != 0)
924 : 0 : goto error;
925 : :
926 : : /* Config the VF */
927 : 0 : ret = nfp_vdpa_start(device, true);
928 [ # # ]: 0 : if (ret != 0)
929 : 0 : goto unset_intr;
930 : :
931 : : /* Setup vring relay thread */
932 : 0 : ret = nfp_vdpa_setup_vring_relay(device);
933 [ # # ]: 0 : if (ret != 0)
934 : 0 : goto stop_vf;
935 : :
936 : 0 : device->hw.sw_fallback_running = true;
937 : :
938 : 0 : return 0;
939 : :
940 : : stop_vf:
941 : 0 : nfp_vdpa_stop(device, true);
942 : 0 : unset_intr:
943 : 0 : nfp_vdpa_disable_vfio_intr(device);
944 : : error:
945 : : return ret;
946 : : }
947 : :
948 : : static int
949 : 0 : nfp_vdpa_dev_config(int vid)
950 : : {
951 : : int ret;
952 : : struct nfp_vdpa_dev *device;
953 : : struct rte_vdpa_device *vdev;
954 : : struct nfp_vdpa_dev_node *node;
955 : :
956 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
957 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
958 [ # # ]: 0 : if (node == NULL) {
959 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p.", vdev);
960 : 0 : return -ENODEV;
961 : : }
962 : :
963 : 0 : device = node->device;
964 : 0 : device->vid = vid;
965 : 0 : rte_atomic_store_explicit(&device->dev_attached, 1, rte_memory_order_relaxed);
966 : 0 : update_datapath(device);
967 : :
968 : 0 : ret = rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true);
969 [ # # ]: 0 : if (ret != 0)
970 : 0 : DRV_VDPA_LOG(INFO, "vDPA (%s): software relay is used.",
971 : : vdev->device->name);
972 : :
973 : : return 0;
974 : : }
975 : :
976 : : static int
977 : 0 : nfp_vdpa_dev_close(int vid)
978 : : {
979 : : struct nfp_vdpa_dev *device;
980 : : struct rte_vdpa_device *vdev;
981 : : struct nfp_vdpa_dev_node *node;
982 : :
983 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
984 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
985 [ # # ]: 0 : if (node == NULL) {
986 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p.", vdev);
987 : 0 : return -ENODEV;
988 : : }
989 : :
990 : 0 : device = node->device;
991 [ # # ]: 0 : if (device->hw.sw_fallback_running) {
992 : : /* Reset VF */
993 : 0 : nfp_vdpa_stop(device, true);
994 : :
995 : : /* Remove interrupt setting */
996 : 0 : nfp_vdpa_disable_vfio_intr(device);
997 : :
998 : : /* Unset DMA map for guest memory */
999 : 0 : nfp_vdpa_dma_map(device, false);
1000 : :
1001 : 0 : device->hw.sw_fallback_running = false;
1002 : :
1003 : 0 : rte_atomic_store_explicit(&device->dev_attached, 0,
1004 : : rte_memory_order_relaxed);
1005 : 0 : rte_atomic_store_explicit(&device->running, 0,
1006 : : rte_memory_order_relaxed);
1007 : : } else {
1008 : 0 : rte_atomic_store_explicit(&device->dev_attached, 0,
1009 : : rte_memory_order_relaxed);
1010 : 0 : update_datapath(device);
1011 : : }
1012 : :
1013 : : return 0;
1014 : : }
1015 : :
1016 : : static int
1017 : 0 : nfp_vdpa_get_vfio_device_fd(int vid)
1018 : : {
1019 : : struct rte_vdpa_device *vdev;
1020 : : struct nfp_vdpa_dev_node *node;
1021 : :
1022 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1023 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
1024 [ # # ]: 0 : if (node == NULL) {
1025 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p.", vdev);
1026 : 0 : return -ENODEV;
1027 : : }
1028 : :
1029 : 0 : return node->device->vfio_dev_fd;
1030 : : }
1031 : :
1032 : : static int
1033 : 0 : nfp_vdpa_get_notify_area(int vid,
1034 : : int qid,
1035 : : uint64_t *offset,
1036 : : uint64_t *size)
1037 : : {
1038 : : int ret;
1039 : : struct nfp_vdpa_dev *device;
1040 : : struct rte_vdpa_device *vdev;
1041 : : struct nfp_vdpa_dev_node *node;
1042 : 0 : struct vfio_region_info region = {
1043 : : .argsz = sizeof(region)
1044 : : };
1045 : :
1046 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1047 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
1048 [ # # ]: 0 : if (node == NULL) {
1049 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p", vdev);
1050 : 0 : return -ENODEV;
1051 : : }
1052 : :
1053 : 0 : device = node->device;
1054 : 0 : region.index = device->hw.notify_region;
1055 : :
1056 : 0 : ret = ioctl(device->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®ion);
1057 [ # # ]: 0 : if (ret != 0) {
1058 : 0 : DRV_VDPA_LOG(ERR, "Get not get device region info.");
1059 : 0 : return -EIO;
1060 : : }
1061 : :
1062 : 0 : *offset = nfp_vdpa_get_queue_notify_offset(&device->hw, qid) + region.offset;
1063 : 0 : *size = NFP_VDPA_NOTIFY_ADDR_INTERVAL;
1064 : :
1065 : 0 : return 0;
1066 : : }
1067 : :
1068 : : static int
1069 : 0 : nfp_vdpa_get_queue_num(struct rte_vdpa_device *vdev,
1070 : : uint32_t *queue_num)
1071 : : {
1072 : : struct nfp_vdpa_dev_node *node;
1073 : :
1074 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
1075 [ # # ]: 0 : if (node == NULL) {
1076 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p.", vdev);
1077 : 0 : return -ENODEV;
1078 : : }
1079 : :
1080 : 0 : *queue_num = node->device->max_queues;
1081 : :
1082 : 0 : return 0;
1083 : : }
1084 : :
1085 : : static int
1086 : 0 : nfp_vdpa_get_vdpa_features(struct rte_vdpa_device *vdev,
1087 : : uint64_t *features)
1088 : : {
1089 : : struct nfp_vdpa_dev_node *node;
1090 : :
1091 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
1092 [ # # ]: 0 : if (node == NULL) {
1093 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p", vdev);
1094 : 0 : return -ENODEV;
1095 : : }
1096 : :
1097 : 0 : *features = node->device->hw.features;
1098 : :
1099 : 0 : return 0;
1100 : : }
1101 : :
1102 : : static int
1103 : 0 : nfp_vdpa_get_protocol_features(struct rte_vdpa_device *vdev __rte_unused,
1104 : : uint64_t *features)
1105 : : {
1106 : 0 : *features = 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD |
1107 : : 1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK |
1108 : : 1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ |
1109 : : 1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD |
1110 : : 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER;
1111 : :
1112 : 0 : return 0;
1113 : : }
1114 : :
1115 : : static int
1116 : 0 : nfp_vdpa_set_features(int32_t vid)
1117 : : {
1118 : : int ret;
1119 : 0 : uint64_t features = 0;
1120 : : struct nfp_vdpa_dev *device;
1121 : : struct rte_vdpa_device *vdev;
1122 : : struct nfp_vdpa_dev_node *node;
1123 : :
1124 : 0 : DRV_VDPA_LOG(DEBUG, "Start vid=%d.", vid);
1125 : :
1126 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1127 : 0 : node = nfp_vdpa_find_node_by_vdev(vdev);
1128 [ # # ]: 0 : if (node == NULL) {
1129 : 0 : DRV_VDPA_LOG(ERR, "Invalid vDPA device: %p.", vdev);
1130 : 0 : return -ENODEV;
1131 : : }
1132 : :
1133 : 0 : rte_vhost_get_negotiated_features(vid, &features);
1134 : :
1135 [ # # ]: 0 : if (RTE_VHOST_NEED_LOG(features) == 0)
1136 : : return 0;
1137 : :
1138 : 0 : device = node->device;
1139 [ # # ]: 0 : if (device->hw.sw_lm) {
1140 : 0 : ret = nfp_vdpa_sw_fallback(device);
1141 [ # # ]: 0 : if (ret != 0) {
1142 : 0 : DRV_VDPA_LOG(ERR, "Software fallback start failed.");
1143 : 0 : return -1;
1144 : : }
1145 : : }
1146 : :
1147 : : return 0;
1148 : : }
1149 : :
1150 : : static int
1151 : 0 : nfp_vdpa_set_vring_state(int vid,
1152 : : int vring,
1153 : : int state)
1154 : : {
1155 : 0 : DRV_VDPA_LOG(DEBUG, "Start vid=%d, vring=%d, state=%d.", vid, vring, state);
1156 : 0 : return 0;
1157 : : }
1158 : :
1159 : : struct rte_vdpa_dev_ops nfp_vdpa_ops = {
1160 : : .get_queue_num = nfp_vdpa_get_queue_num,
1161 : : .get_features = nfp_vdpa_get_vdpa_features,
1162 : : .get_protocol_features = nfp_vdpa_get_protocol_features,
1163 : : .dev_conf = nfp_vdpa_dev_config,
1164 : : .dev_close = nfp_vdpa_dev_close,
1165 : : .set_vring_state = nfp_vdpa_set_vring_state,
1166 : : .set_features = nfp_vdpa_set_features,
1167 : : .get_vfio_device_fd = nfp_vdpa_get_vfio_device_fd,
1168 : : .get_notify_area = nfp_vdpa_get_notify_area,
1169 : : };
1170 : :
1171 : : static int
1172 : 0 : nfp_vdpa_pci_probe(struct rte_pci_device *pci_dev)
1173 : : {
1174 : : int ret;
1175 : : struct nfp_vdpa_dev *device;
1176 : : struct nfp_vdpa_dev_node *node;
1177 : :
1178 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1179 : : return 0;
1180 : :
1181 : 0 : node = calloc(1, sizeof(*node));
1182 [ # # ]: 0 : if (node == NULL)
1183 : : return -ENOMEM;
1184 : :
1185 : 0 : device = calloc(1, sizeof(*device));
1186 [ # # ]: 0 : if (device == NULL)
1187 : 0 : goto free_node;
1188 : :
1189 : 0 : device->pci_dev = pci_dev;
1190 : :
1191 : 0 : ret = nfp_vdpa_vfio_setup(device);
1192 [ # # ]: 0 : if (ret != 0)
1193 : 0 : goto free_device;
1194 : :
1195 : 0 : ret = nfp_vdpa_hw_init(&device->hw, pci_dev);
1196 [ # # ]: 0 : if (ret != 0)
1197 : 0 : goto vfio_teardown;
1198 : :
1199 : 0 : device->max_queues = NFP_VDPA_MAX_QUEUES;
1200 : :
1201 : 0 : device->vdev = rte_vdpa_register_device(&pci_dev->device, &nfp_vdpa_ops);
1202 [ # # ]: 0 : if (device->vdev == NULL) {
1203 : 0 : DRV_VDPA_LOG(ERR, "Failed to register device %s.", pci_dev->name);
1204 : 0 : goto vfio_teardown;
1205 : : }
1206 : :
1207 : 0 : node->device = device;
1208 : 0 : pthread_mutex_lock(&vdpa_list_lock);
1209 : 0 : TAILQ_INSERT_TAIL(&vdpa_dev_list, node, next);
1210 : 0 : pthread_mutex_unlock(&vdpa_list_lock);
1211 : :
1212 : : rte_spinlock_init(&device->lock);
1213 : 0 : rte_atomic_store_explicit(&device->started, 1, rte_memory_order_relaxed);
1214 : 0 : update_datapath(device);
1215 : :
1216 : 0 : return 0;
1217 : :
1218 : 0 : vfio_teardown:
1219 : : nfp_vdpa_vfio_teardown(device);
1220 : 0 : free_device:
1221 : 0 : free(device);
1222 : 0 : free_node:
1223 : 0 : free(node);
1224 : :
1225 : 0 : return -1;
1226 : : }
1227 : :
1228 : : static int
1229 : 0 : nfp_vdpa_pci_remove(struct rte_pci_device *pci_dev)
1230 : : {
1231 : : struct nfp_vdpa_dev *device;
1232 : : struct nfp_vdpa_dev_node *node;
1233 : :
1234 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1235 : : return 0;
1236 : :
1237 : 0 : node = nfp_vdpa_find_node_by_pdev(pci_dev);
1238 [ # # ]: 0 : if (node == NULL) {
1239 : 0 : DRV_VDPA_LOG(ERR, "Invalid device: %s.", pci_dev->name);
1240 : 0 : return -ENODEV;
1241 : : }
1242 : :
1243 : 0 : device = node->device;
1244 : :
1245 : 0 : rte_atomic_store_explicit(&device->started, 0, rte_memory_order_relaxed);
1246 : 0 : update_datapath(device);
1247 : :
1248 : 0 : pthread_mutex_lock(&vdpa_list_lock);
1249 [ # # ]: 0 : TAILQ_REMOVE(&vdpa_dev_list, node, next);
1250 : 0 : pthread_mutex_unlock(&vdpa_list_lock);
1251 : :
1252 : 0 : rte_vdpa_unregister_device(device->vdev);
1253 : : nfp_vdpa_vfio_teardown(device);
1254 : :
1255 : 0 : free(device);
1256 : 0 : free(node);
1257 : :
1258 : 0 : return 0;
1259 : : }
1260 : :
1261 : : static const struct rte_pci_id pci_id_nfp_vdpa_map[] = {
1262 : : {
1263 : : RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
1264 : : PCI_DEVICE_ID_NFP6000_VF_NIC)
1265 : : },
1266 : : {
1267 : : .vendor_id = 0,
1268 : : },
1269 : : };
1270 : :
1271 : : static struct nfp_class_driver nfp_vdpa = {
1272 : : .drv_class = NFP_CLASS_VDPA,
1273 : : .name = RTE_STR(NFP_VDPA_DRIVER_NAME),
1274 : : .id_table = pci_id_nfp_vdpa_map,
1275 : : .drv_flags = RTE_PCI_DRV_INTR_LSC,
1276 : : .probe = nfp_vdpa_pci_probe,
1277 : : .remove = nfp_vdpa_pci_remove,
1278 : : };
1279 : :
1280 : 305 : RTE_INIT(nfp_vdpa_init)
1281 : : {
1282 : 305 : nfp_class_driver_register(&nfp_vdpa);
1283 : 305 : }
1284 : :
1285 : : RTE_PMD_REGISTER_PCI_TABLE(NFP_VDPA_DRIVER_NAME, pci_id_nfp_vdpa_map);
1286 : : RTE_PMD_REGISTER_KMOD_DEP(NFP_VDPA_DRIVER_NAME, "* vfio-pci");
|