Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 Intel Corporation
3 : : */
4 : :
5 : : #include <uapi/linux/vfio.h>
6 : :
7 : : #include <unistd.h>
8 : : #include <pthread.h>
9 : : #include <fcntl.h>
10 : : #include <string.h>
11 : : #include <sys/ioctl.h>
12 : : #include <sys/epoll.h>
13 : : #include <linux/virtio_net.h>
14 : : #include <stdbool.h>
15 : :
16 : : #include <rte_eal_paging.h>
17 : : #include <rte_malloc.h>
18 : : #include <rte_memory.h>
19 : : #include <bus_pci_driver.h>
20 : : #include <rte_vhost.h>
21 : : #include <rte_vdpa.h>
22 : : #include <vdpa_driver.h>
23 : : #include <dev_vfio.h>
24 : : #include <rte_spinlock.h>
25 : : #include <rte_log.h>
26 : : #include <rte_kvargs.h>
27 : : #include <rte_devargs.h>
28 : : #include <rte_stdatomic.h>
29 : :
30 : : #include "base/ifcvf.h"
31 : :
32 : : /*
33 : : * RTE_MIN() cannot be used since braced-group within expression allowed
34 : : * only inside a function.
35 : : */
36 : : #define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
37 : :
38 [ - + ]: 305 : RTE_LOG_REGISTER(ifcvf_vdpa_logtype, pmd.vdpa.ifcvf, NOTICE);
39 : : #define RTE_LOGTYPE_IFCVF_VDPA ifcvf_vdpa_logtype
40 : : #define DRV_LOG(level, ...) \
41 : : RTE_LOG_LINE_PREFIX(level, IFCVF_VDPA, "%s(): ", __func__, __VA_ARGS__)
42 : :
43 : : #define IFCVF_USED_RING_LEN(size) \
44 : : ((size) * sizeof(struct vring_used_elem) + sizeof(uint16_t) * 3)
45 : :
46 : : #define IFCVF_VDPA_MODE "vdpa"
47 : : #define IFCVF_SW_FALLBACK_LM "sw-live-migration"
48 : :
49 : : #define THREAD_NAME_LEN 16
50 : :
51 : : static const char * const ifcvf_valid_arguments[] = {
52 : : IFCVF_VDPA_MODE,
53 : : IFCVF_SW_FALLBACK_LM,
54 : : NULL
55 : : };
56 : :
57 : : struct ifcvf_internal {
58 : : struct rte_pci_device *pdev;
59 : : struct ifcvf_hw hw;
60 : : int configured;
61 : : int vfio_container_fd;
62 : : int vfio_dev_fd;
63 : : rte_thread_t tid; /* thread for notify relay */
64 : : rte_thread_t intr_tid; /* thread for config space change interrupt relay */
65 : : int epfd;
66 : : int csc_epfd;
67 : : int vid;
68 : : struct rte_vdpa_device *vdev;
69 : : uint16_t max_queues;
70 : : uint64_t features;
71 : : rte_spinlock_t lock;
72 : : RTE_ATOMIC(bool) started;
73 : : RTE_ATOMIC(bool) dev_attached;
74 : : RTE_ATOMIC(bool) running;
75 : : bool sw_lm;
76 : : bool sw_fallback_running;
77 : : /* mediated vring for sw fallback */
78 : : struct vring m_vring[IFCVF_MAX_QUEUES * 2];
79 : : /* eventfd for used ring interrupt */
80 : : int intr_fd[IFCVF_MAX_QUEUES * 2];
81 : : };
82 : :
83 : : struct internal_list {
84 : : TAILQ_ENTRY(internal_list) next;
85 : : struct ifcvf_internal *internal;
86 : : };
87 : :
88 : : /* vdpa device info includes device features and devcic operation. */
89 : : struct rte_vdpa_dev_info {
90 : : uint64_t features;
91 : : struct rte_vdpa_dev_ops *ops;
92 : : };
93 : :
94 : : TAILQ_HEAD(internal_list_head, internal_list);
95 : : static struct internal_list_head internal_list =
96 : : TAILQ_HEAD_INITIALIZER(internal_list);
97 : :
98 : : static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
99 : :
100 : : static void update_used_ring(struct ifcvf_internal *internal, uint16_t qid);
101 : :
102 : : static struct internal_list *
103 : 0 : find_internal_resource_by_vdev(struct rte_vdpa_device *vdev)
104 : : {
105 : : int found = 0;
106 : : struct internal_list *list;
107 : :
108 : 0 : pthread_mutex_lock(&internal_list_lock);
109 : :
110 [ # # ]: 0 : TAILQ_FOREACH(list, &internal_list, next) {
111 [ # # ]: 0 : if (vdev == list->internal->vdev) {
112 : : found = 1;
113 : : break;
114 : : }
115 : : }
116 : :
117 : 0 : pthread_mutex_unlock(&internal_list_lock);
118 : :
119 [ # # ]: 0 : if (!found)
120 : 0 : return NULL;
121 : :
122 : : return list;
123 : : }
124 : :
125 : : static struct internal_list *
126 : 0 : find_internal_resource_by_pci_dev(struct rte_pci_device *pdev)
127 : : {
128 : : int found = 0;
129 : : struct internal_list *list;
130 : :
131 : 0 : pthread_mutex_lock(&internal_list_lock);
132 : :
133 [ # # ]: 0 : TAILQ_FOREACH(list, &internal_list, next) {
134 [ # # ]: 0 : if (!rte_pci_addr_cmp(&pdev->addr,
135 : 0 : &list->internal->pdev->addr)) {
136 : : found = 1;
137 : : break;
138 : : }
139 : : }
140 : :
141 : 0 : pthread_mutex_unlock(&internal_list_lock);
142 : :
143 [ # # ]: 0 : if (!found)
144 : 0 : return NULL;
145 : :
146 : : return list;
147 : : }
148 : :
149 : : static struct internal_list *
150 : 0 : find_internal_resource_by_rte_dev(struct rte_device *rte_dev)
151 : : {
152 : : int found = 0;
153 : : struct internal_list *list;
154 : :
155 : 0 : pthread_mutex_lock(&internal_list_lock);
156 : :
157 [ # # ]: 0 : TAILQ_FOREACH(list, &internal_list, next) {
158 [ # # ]: 0 : if (rte_dev == &list->internal->pdev->device) {
159 : : found = 1;
160 : : break;
161 : : }
162 : : }
163 : :
164 : 0 : pthread_mutex_unlock(&internal_list_lock);
165 : :
166 [ # # ]: 0 : if (!found)
167 : 0 : return NULL;
168 : :
169 : : return list;
170 : : }
171 : :
172 : : static int
173 : 0 : ifcvf_vfio_setup(struct ifcvf_internal *internal)
174 : : {
175 : 0 : struct rte_pci_device *dev = internal->pdev;
176 : 0 : char devname[RTE_DEV_NAME_MAX_LEN] = {0};
177 : : int i;
178 : :
179 : 0 : internal->vfio_dev_fd = -1;
180 : 0 : internal->vfio_container_fd = -1;
181 : :
182 : 0 : rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN);
183 : :
184 : 0 : internal->vfio_container_fd = dev_vfio_container_create();
185 [ # # ]: 0 : if (internal->vfio_container_fd < 0)
186 : : return -1;
187 : :
188 [ # # ]: 0 : if (dev_vfio_container_assign_device(internal->vfio_container_fd,
189 : : rte_pci_get_sysfs_path(), devname) < 0)
190 : 0 : goto err;
191 : :
192 [ # # ]: 0 : if (rte_pci_map_device(dev))
193 : 0 : goto err;
194 : :
195 : 0 : internal->vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
196 : :
197 [ # # ]: 0 : for (i = 0; i < RTE_MIN(PCI_MAX_RESOURCE, IFCVF_PCI_MAX_RESOURCE);
198 : 0 : i++) {
199 : 0 : internal->hw.mem_resource[i].addr =
200 : 0 : internal->pdev->mem_resource[i].addr;
201 : 0 : internal->hw.mem_resource[i].phys_addr =
202 : 0 : internal->pdev->mem_resource[i].phys_addr;
203 : 0 : internal->hw.mem_resource[i].len =
204 : 0 : internal->pdev->mem_resource[i].len;
205 : : }
206 : :
207 : : return 0;
208 : :
209 : 0 : err:
210 : 0 : dev_vfio_container_destroy(internal->vfio_container_fd);
211 : 0 : return -1;
212 : : }
213 : :
214 : : static int
215 : 0 : ifcvf_dma_map(struct ifcvf_internal *internal, bool do_map)
216 : : {
217 : : uint32_t i;
218 : : int ret;
219 : 0 : struct rte_vhost_memory *mem = NULL;
220 : : int vfio_container_fd;
221 : :
222 : 0 : ret = rte_vhost_get_mem_table(internal->vid, &mem);
223 [ # # ]: 0 : if (ret < 0) {
224 : 0 : DRV_LOG(ERR, "failed to get VM memory layout.");
225 : 0 : goto exit;
226 : : }
227 : :
228 : 0 : vfio_container_fd = internal->vfio_container_fd;
229 : :
230 [ # # ]: 0 : for (i = 0; i < mem->nregions; i++) {
231 : : struct rte_vhost_mem_region *reg;
232 : :
233 : : reg = &mem->regions[i];
234 [ # # ]: 0 : DRV_LOG(INFO, "%s, region %u: HVA 0x%" PRIx64 ", "
235 : : "GPA 0x%" PRIx64 ", size 0x%" PRIx64 ".",
236 : : do_map ? "DMA map" : "DMA unmap", i,
237 : : reg->host_user_addr, reg->guest_phys_addr, reg->size);
238 : :
239 [ # # ]: 0 : if (do_map) {
240 : 0 : ret = dev_vfio_container_dma_map(vfio_container_fd,
241 : : reg->host_user_addr, reg->guest_phys_addr,
242 : : reg->size);
243 [ # # ]: 0 : if (ret < 0) {
244 : 0 : DRV_LOG(ERR, "DMA map failed.");
245 : 0 : goto exit;
246 : : }
247 : : } else {
248 : 0 : ret = dev_vfio_container_dma_unmap(vfio_container_fd,
249 : : reg->host_user_addr, reg->guest_phys_addr,
250 : : reg->size);
251 [ # # ]: 0 : if (ret < 0) {
252 : 0 : DRV_LOG(ERR, "DMA unmap failed.");
253 : 0 : goto exit;
254 : : }
255 : : }
256 : : }
257 : :
258 : 0 : exit:
259 : 0 : free(mem);
260 : 0 : return ret;
261 : : }
262 : :
263 : : static uint64_t
264 : 0 : hva_to_gpa(int vid, uint64_t hva)
265 : : {
266 : 0 : struct rte_vhost_memory *mem = NULL;
267 : : struct rte_vhost_mem_region *reg;
268 : : uint32_t i;
269 : : uint64_t gpa = 0;
270 : :
271 [ # # ]: 0 : if (rte_vhost_get_mem_table(vid, &mem) < 0)
272 : 0 : goto exit;
273 : :
274 [ # # ]: 0 : for (i = 0; i < mem->nregions; i++) {
275 : : reg = &mem->regions[i];
276 : :
277 [ # # ]: 0 : if (hva >= reg->host_user_addr &&
278 [ # # ]: 0 : hva < reg->host_user_addr + reg->size) {
279 : 0 : gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
280 : 0 : break;
281 : : }
282 : : }
283 : :
284 : 0 : exit:
285 : 0 : free(mem);
286 : 0 : return gpa;
287 : : }
288 : :
289 : : static int
290 : 0 : vdpa_ifcvf_start(struct ifcvf_internal *internal)
291 : : {
292 : 0 : struct ifcvf_hw *hw = &internal->hw;
293 : : int i, nr_vring;
294 : : int vid;
295 : : struct rte_vhost_vring vq;
296 : : uint64_t gpa;
297 : :
298 : 0 : vid = internal->vid;
299 : 0 : nr_vring = rte_vhost_get_vring_num(vid);
300 : 0 : rte_vhost_get_negotiated_features(vid, &hw->req_features);
301 : :
302 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
303 [ # # ]: 0 : if (!hw->vring[i].enable)
304 : 0 : continue;
305 : 0 : rte_vhost_get_vhost_vring(vid, i, &vq);
306 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
307 [ # # ]: 0 : if (gpa == 0) {
308 : 0 : DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
309 : 0 : return -1;
310 : : }
311 : 0 : hw->vring[i].desc = gpa;
312 : :
313 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
314 [ # # ]: 0 : if (gpa == 0) {
315 : 0 : DRV_LOG(ERR, "Fail to get GPA for available ring.");
316 : 0 : return -1;
317 : : }
318 : 0 : hw->vring[i].avail = gpa;
319 : :
320 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
321 [ # # ]: 0 : if (gpa == 0) {
322 : 0 : DRV_LOG(ERR, "Fail to get GPA for used ring.");
323 : 0 : return -1;
324 : : }
325 : 0 : hw->vring[i].used = gpa;
326 : :
327 : 0 : hw->vring[i].size = vq.size;
328 : 0 : rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
329 : 0 : &hw->vring[i].last_used_idx);
330 : : }
331 : 0 : hw->nr_vring = i;
332 : :
333 : 0 : return ifcvf_start_hw(&internal->hw);
334 : : }
335 : :
336 : : static void
337 : 0 : vdpa_ifcvf_stop(struct ifcvf_internal *internal)
338 : : {
339 : 0 : struct ifcvf_hw *hw = &internal->hw;
340 : : uint32_t i;
341 : : int vid;
342 : 0 : uint64_t features = 0;
343 : 0 : uint64_t log_base = 0, log_size = 0;
344 : : uint64_t len;
345 : : u32 ring_state = 0;
346 : :
347 : 0 : vid = internal->vid;
348 : :
349 : : /* to make sure no packet is lost for blk device
350 : : * do not stop until last_avail_idx == last_used_idx
351 : : */
352 [ # # ]: 0 : if (internal->hw.device_type == IFCVF_BLK) {
353 [ # # ]: 0 : for (i = 0; i < hw->nr_vring; i++) {
354 : : do {
355 [ # # ]: 0 : if (hw->lm_cfg != NULL)
356 : 0 : ring_state = *(u32 *)(hw->lm_cfg +
357 : 0 : IFCVF_LM_RING_STATE_OFFSET +
358 : 0 : i * IFCVF_LM_CFG_SIZE);
359 : 0 : hw->vring[i].last_avail_idx =
360 : 0 : (u16)(ring_state & IFCVF_16_BIT_MASK);
361 : 0 : hw->vring[i].last_used_idx =
362 : 0 : (u16)(ring_state >> 16);
363 : 0 : usleep(10);
364 : 0 : } while (hw->vring[i].last_avail_idx !=
365 [ # # ]: 0 : hw->vring[i].last_used_idx);
366 : : }
367 : : }
368 : :
369 : 0 : ifcvf_stop_hw(hw);
370 : :
371 [ # # ]: 0 : for (i = 0; i < hw->nr_vring; i++)
372 : 0 : rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
373 : 0 : hw->vring[i].last_used_idx);
374 : :
375 [ # # ]: 0 : if (internal->sw_lm)
376 : 0 : return;
377 : :
378 : 0 : rte_vhost_get_negotiated_features(vid, &features);
379 [ # # ]: 0 : if (RTE_VHOST_NEED_LOG(features)) {
380 : 0 : ifcvf_disable_logging(hw);
381 : 0 : rte_vhost_get_log_base(internal->vid, &log_base, &log_size);
382 : 0 : dev_vfio_container_dma_unmap(internal->vfio_container_fd,
383 : : log_base, IFCVF_LOG_BASE, log_size);
384 : : /*
385 : : * IFCVF marks dirty memory pages for only packet buffer,
386 : : * SW helps to mark the used ring as dirty after device stops.
387 : : */
388 [ # # ]: 0 : for (i = 0; i < hw->nr_vring; i++) {
389 : 0 : len = IFCVF_USED_RING_LEN(hw->vring[i].size);
390 : 0 : rte_vhost_log_used_vring(vid, i, 0, len);
391 : : }
392 : : }
393 : : }
394 : :
395 : : #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
396 : : sizeof(int) * (IFCVF_MAX_QUEUES * 2 + 1))
397 : : static int
398 : 0 : vdpa_enable_vfio_intr(struct ifcvf_internal *internal, bool m_rx)
399 : : {
400 : : int ret;
401 : : uint32_t i, nr_vring;
402 : : char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
403 : : struct vfio_irq_set *irq_set;
404 : : int *fd_ptr;
405 : : struct rte_vhost_vring vring;
406 : : int fd;
407 : :
408 : 0 : vring.callfd = -1;
409 : :
410 : 0 : nr_vring = rte_vhost_get_vring_num(internal->vid);
411 [ # # ]: 0 : if (nr_vring > IFCVF_MAX_QUEUES * 2)
412 : : return -1;
413 : :
414 : : irq_set = (struct vfio_irq_set *)irq_set_buf;
415 : 0 : irq_set->argsz = sizeof(irq_set_buf);
416 : 0 : irq_set->count = nr_vring + 1;
417 : 0 : irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
418 : : VFIO_IRQ_SET_ACTION_TRIGGER;
419 : 0 : irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
420 : 0 : irq_set->start = 0;
421 : : fd_ptr = (int *)&irq_set->data;
422 : : /* The first interrupt is for the configure space change notification */
423 : 0 : fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] =
424 : 0 : rte_intr_fd_get(internal->pdev->intr_handle);
425 : :
426 [ # # ]: 0 : for (i = 0; i < nr_vring; i++)
427 : 0 : internal->intr_fd[i] = -1;
428 : :
429 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
430 : 0 : rte_vhost_get_vhost_vring(internal->vid, i, &vring);
431 : 0 : fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
432 [ # # ]: 0 : if (m_rx == true &&
433 [ # # # # ]: 0 : ((i & 1) == 0 || internal->hw.device_type == IFCVF_BLK)) {
434 : : /* For the net we only need to relay rx queue,
435 : : * which will change the mem of VM.
436 : : * For the blk we need to relay all the read cmd
437 : : * of each queue
438 : : */
439 : 0 : fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
440 [ # # ]: 0 : if (fd < 0) {
441 : 0 : DRV_LOG(ERR, "can't setup eventfd: %s",
442 : : strerror(errno));
443 : 0 : return -1;
444 : : }
445 : 0 : internal->intr_fd[i] = fd;
446 : 0 : fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = fd;
447 : : }
448 : : }
449 : :
450 : 0 : ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
451 [ # # ]: 0 : if (ret) {
452 : 0 : DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
453 : : strerror(errno));
454 : 0 : return -1;
455 : : }
456 : :
457 : : return 0;
458 : : }
459 : :
460 : : static int
461 : 0 : vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
462 : : {
463 : : int ret;
464 : : uint32_t i, nr_vring;
465 : : char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
466 : : struct vfio_irq_set *irq_set;
467 : :
468 : : irq_set = (struct vfio_irq_set *)irq_set_buf;
469 : 0 : irq_set->argsz = sizeof(irq_set_buf);
470 : 0 : irq_set->count = 0;
471 : 0 : irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
472 : 0 : irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
473 : 0 : irq_set->start = 0;
474 : :
475 : 0 : nr_vring = rte_vhost_get_vring_num(internal->vid);
476 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
477 [ # # ]: 0 : if (internal->intr_fd[i] >= 0)
478 : 0 : close(internal->intr_fd[i]);
479 : 0 : internal->intr_fd[i] = -1;
480 : : }
481 : :
482 : 0 : ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
483 [ # # ]: 0 : if (ret) {
484 : 0 : DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
485 : : strerror(errno));
486 : 0 : return -1;
487 : : }
488 : :
489 : : return 0;
490 : : }
491 : :
492 : : static uint32_t
493 : 0 : notify_relay(void *arg)
494 : : {
495 : : int i, kickfd, epfd, nfds = 0;
496 : : uint32_t qid, q_num;
497 : : struct epoll_event events[IFCVF_MAX_QUEUES * 2];
498 : : struct epoll_event ev;
499 : : uint64_t buf;
500 : : int nbytes;
501 : : struct rte_vhost_vring vring;
502 : : struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
503 : 0 : struct ifcvf_hw *hw = &internal->hw;
504 : :
505 : 0 : q_num = rte_vhost_get_vring_num(internal->vid);
506 : :
507 : 0 : epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
508 [ # # ]: 0 : if (epfd < 0) {
509 : 0 : DRV_LOG(ERR, "failed to create epoll instance.");
510 : 0 : return 1;
511 : : }
512 : 0 : internal->epfd = epfd;
513 : :
514 : 0 : vring.kickfd = -1;
515 [ # # ]: 0 : for (qid = 0; qid < q_num; qid++) {
516 [ # # ]: 0 : if (!hw->vring[qid].enable)
517 : 0 : continue;
518 : 0 : ev.events = EPOLLIN | EPOLLPRI;
519 : 0 : rte_vhost_get_vhost_vring(internal->vid, qid, &vring);
520 : 0 : ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
521 [ # # ]: 0 : if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
522 : 0 : DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
523 : 0 : return 1;
524 : : }
525 : : }
526 : :
527 : : for (;;) {
528 : 0 : nfds = epoll_wait(epfd, events, q_num, -1);
529 [ # # ]: 0 : if (nfds < 0) {
530 [ # # ]: 0 : if (errno == EINTR)
531 : 0 : continue;
532 : 0 : DRV_LOG(ERR, "epoll_wait return fail");
533 : 0 : return 1;
534 : : }
535 : :
536 [ # # ]: 0 : for (i = 0; i < nfds; i++) {
537 : 0 : qid = events[i].data.u32;
538 : 0 : kickfd = (uint32_t)(events[i].data.u64 >> 32);
539 : : do {
540 : 0 : nbytes = read(kickfd, &buf, 8);
541 [ # # ]: 0 : if (nbytes < 0) {
542 [ # # ]: 0 : if (errno == EINTR ||
543 : : errno == EWOULDBLOCK ||
544 : : errno == EAGAIN)
545 : : continue;
546 : 0 : DRV_LOG(INFO, "Error reading "
547 : : "kickfd: %s",
548 : : strerror(errno));
549 : : }
550 : : break;
551 : : } while (1);
552 : :
553 : 0 : ifcvf_notify_queue(hw, qid);
554 : : }
555 : : }
556 : :
557 : : return 0;
558 : : }
559 : :
560 : : static int
561 : 0 : setup_notify_relay(struct ifcvf_internal *internal)
562 : : {
563 : : char name[RTE_THREAD_INTERNAL_NAME_SIZE];
564 : : int ret;
565 : :
566 : 0 : snprintf(name, sizeof(name), "ifc-noti%d", internal->vid);
567 : 0 : ret = rte_thread_create_internal_control(&internal->tid, name,
568 : : notify_relay, internal);
569 [ # # ]: 0 : if (ret != 0) {
570 : 0 : DRV_LOG(ERR, "failed to create notify relay pthread.");
571 : 0 : return -1;
572 : : }
573 : :
574 : : return 0;
575 : : }
576 : :
577 : : static int
578 : 0 : unset_notify_relay(struct ifcvf_internal *internal)
579 : : {
580 [ # # ]: 0 : if (internal->tid.opaque_id != 0) {
581 : 0 : pthread_cancel((pthread_t)internal->tid.opaque_id);
582 : 0 : rte_thread_join(internal->tid, NULL);
583 : : }
584 : 0 : internal->tid.opaque_id = 0;
585 : :
586 [ # # ]: 0 : if (internal->epfd >= 0)
587 : 0 : close(internal->epfd);
588 : 0 : internal->epfd = -1;
589 : :
590 : 0 : return 0;
591 : : }
592 : :
593 : : static void
594 : 0 : virtio_interrupt_handler(struct ifcvf_internal *internal)
595 : : {
596 : 0 : int vid = internal->vid;
597 : : int ret;
598 : :
599 : 0 : ret = rte_vhost_backend_config_change(vid, 1);
600 [ # # ]: 0 : if (ret)
601 : 0 : DRV_LOG(ERR, "failed to notify the guest about configuration space change.");
602 : 0 : }
603 : :
604 : : static uint32_t
605 : 0 : intr_relay(void *arg)
606 : : {
607 : : struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
608 : : struct epoll_event csc_event;
609 : : struct epoll_event ev;
610 : : uint64_t buf;
611 : : int nbytes;
612 : : int csc_epfd, csc_val = 0;
613 : :
614 : 0 : csc_epfd = epoll_create(1);
615 [ # # ]: 0 : if (csc_epfd < 0) {
616 : 0 : DRV_LOG(ERR, "failed to create epoll for config space change.");
617 : 0 : return 1;
618 : : }
619 : :
620 : 0 : ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
621 : 0 : ev.data.fd = rte_intr_fd_get(internal->pdev->intr_handle);
622 [ # # ]: 0 : if (epoll_ctl(csc_epfd, EPOLL_CTL_ADD,
623 : 0 : rte_intr_fd_get(internal->pdev->intr_handle), &ev) < 0) {
624 : 0 : DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
625 : 0 : goto out;
626 : : }
627 : :
628 : 0 : internal->csc_epfd = csc_epfd;
629 : :
630 : : for (;;) {
631 : 0 : csc_val = epoll_wait(csc_epfd, &csc_event, 1, -1);
632 [ # # ]: 0 : if (csc_val < 0) {
633 [ # # ]: 0 : if (errno == EINTR)
634 : 0 : continue;
635 : 0 : DRV_LOG(ERR, "epoll_wait return fail.");
636 : 0 : goto out;
637 [ # # ]: 0 : } else if (csc_val == 0) {
638 : 0 : continue;
639 : : } else {
640 : : /* csc_val > 0 */
641 : 0 : nbytes = read(csc_event.data.fd, &buf, 8);
642 [ # # ]: 0 : if (nbytes < 0) {
643 [ # # ]: 0 : if (errno == EINTR ||
644 : : errno == EWOULDBLOCK ||
645 : : errno == EAGAIN)
646 : 0 : continue;
647 : 0 : DRV_LOG(ERR, "Error reading from file descriptor %d: %s",
648 : : csc_event.data.fd,
649 : : strerror(errno));
650 : 0 : goto out;
651 [ # # ]: 0 : } else if (nbytes == 0) {
652 : 0 : DRV_LOG(ERR, "Read nothing from file descriptor %d",
653 : : csc_event.data.fd);
654 : 0 : continue;
655 : : } else {
656 : 0 : virtio_interrupt_handler(internal);
657 : : }
658 : : }
659 : : }
660 : :
661 : 0 : out:
662 : : if (csc_epfd >= 0)
663 : 0 : close(csc_epfd);
664 : 0 : internal->csc_epfd = -1;
665 : :
666 : 0 : return 0;
667 : : }
668 : :
669 : : static int
670 : 0 : setup_intr_relay(struct ifcvf_internal *internal)
671 : : {
672 : : char name[RTE_THREAD_INTERNAL_NAME_SIZE];
673 : : int ret;
674 : :
675 : 0 : snprintf(name, sizeof(name), "ifc-int%d", internal->vid);
676 : 0 : ret = rte_thread_create_internal_control(&internal->intr_tid, name,
677 : : intr_relay, (void *)internal);
678 [ # # ]: 0 : if (ret) {
679 : 0 : DRV_LOG(ERR, "failed to create notify relay pthread.");
680 : 0 : return -1;
681 : : }
682 : : return 0;
683 : : }
684 : :
685 : : static void
686 : 0 : unset_intr_relay(struct ifcvf_internal *internal)
687 : : {
688 [ # # ]: 0 : if (internal->intr_tid.opaque_id != 0) {
689 : 0 : pthread_cancel((pthread_t)internal->intr_tid.opaque_id);
690 : 0 : rte_thread_join(internal->intr_tid, NULL);
691 : : }
692 : 0 : internal->intr_tid.opaque_id = 0;
693 : :
694 [ # # ]: 0 : if (internal->csc_epfd >= 0)
695 : 0 : close(internal->csc_epfd);
696 : 0 : internal->csc_epfd = -1;
697 : 0 : }
698 : :
699 : : static int
700 : 0 : update_datapath(struct ifcvf_internal *internal)
701 : : {
702 : : int ret;
703 : :
704 : 0 : rte_spinlock_lock(&internal->lock);
705 : :
706 [ # # ]: 0 : if (!rte_atomic_load_explicit(&internal->running, rte_memory_order_seq_cst) &&
707 [ # # ]: 0 : (rte_atomic_load_explicit(&internal->started, rte_memory_order_seq_cst) &&
708 [ # # ]: 0 : rte_atomic_load_explicit(&internal->dev_attached, rte_memory_order_seq_cst))) {
709 : 0 : ret = ifcvf_dma_map(internal, true);
710 [ # # ]: 0 : if (ret)
711 : 0 : goto err;
712 : :
713 : 0 : ret = vdpa_enable_vfio_intr(internal, false);
714 [ # # ]: 0 : if (ret)
715 : 0 : goto err;
716 : :
717 : 0 : ret = vdpa_ifcvf_start(internal);
718 [ # # ]: 0 : if (ret)
719 : 0 : goto err;
720 : :
721 : 0 : ret = setup_notify_relay(internal);
722 [ # # ]: 0 : if (ret)
723 : 0 : goto err;
724 : :
725 : 0 : ret = setup_intr_relay(internal);
726 [ # # ]: 0 : if (ret)
727 : 0 : goto err;
728 : :
729 : 0 : rte_atomic_store_explicit(&internal->running, true, rte_memory_order_seq_cst);
730 [ # # ]: 0 : } else if (rte_atomic_load_explicit(&internal->running, rte_memory_order_seq_cst) &&
731 [ # # ]: 0 : (!rte_atomic_load_explicit(&internal->started, rte_memory_order_seq_cst) ||
732 [ # # ]: 0 : !rte_atomic_load_explicit(&internal->dev_attached, rte_memory_order_seq_cst))) {
733 : 0 : unset_intr_relay(internal);
734 : :
735 : 0 : ret = unset_notify_relay(internal);
736 [ # # ]: 0 : if (ret)
737 : 0 : goto err;
738 : :
739 : 0 : vdpa_ifcvf_stop(internal);
740 : :
741 : 0 : ret = vdpa_disable_vfio_intr(internal);
742 [ # # ]: 0 : if (ret)
743 : 0 : goto err;
744 : :
745 : 0 : ret = ifcvf_dma_map(internal, false);
746 [ # # ]: 0 : if (ret)
747 : 0 : goto err;
748 : :
749 : 0 : rte_atomic_store_explicit(&internal->running, false, rte_memory_order_seq_cst);
750 : : }
751 : :
752 : : rte_spinlock_unlock(&internal->lock);
753 : 0 : return 0;
754 : 0 : err:
755 : : rte_spinlock_unlock(&internal->lock);
756 : 0 : return ret;
757 : : }
758 : :
759 : : static int
760 : 0 : m_ifcvf_start(struct ifcvf_internal *internal)
761 : : {
762 : 0 : struct ifcvf_hw *hw = &internal->hw;
763 : : uint32_t i, nr_vring;
764 : : int vid, ret;
765 : : struct rte_vhost_vring vq;
766 : : void *vring_buf;
767 : : uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
768 : : uint64_t size;
769 : : uint64_t gpa;
770 : :
771 : : memset(&vq, 0, sizeof(vq));
772 : 0 : vid = internal->vid;
773 : 0 : nr_vring = rte_vhost_get_vring_num(vid);
774 : 0 : rte_vhost_get_negotiated_features(vid, &hw->req_features);
775 : :
776 [ # # ]: 0 : for (i = 0; i < nr_vring; i++) {
777 : 0 : rte_vhost_get_vhost_vring(vid, i, &vq);
778 : :
779 : 0 : size = RTE_ALIGN_CEIL(vring_size(vq.size, rte_mem_page_size()),
780 : : rte_mem_page_size());
781 : 0 : vring_buf = rte_zmalloc("ifcvf", size, rte_mem_page_size());
782 : 0 : vring_init(&internal->m_vring[i], vq.size, vring_buf,
783 : : rte_mem_page_size());
784 : :
785 : 0 : ret = dev_vfio_container_dma_map(internal->vfio_container_fd,
786 : : (uint64_t)(uintptr_t)vring_buf, m_vring_iova, size);
787 [ # # ]: 0 : if (ret < 0) {
788 : 0 : DRV_LOG(ERR, "mediated vring DMA map failed.");
789 : 0 : goto error;
790 : : }
791 : :
792 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
793 [ # # ]: 0 : if (gpa == 0) {
794 : 0 : DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
795 : 0 : return -1;
796 : : }
797 : 0 : hw->vring[i].desc = gpa;
798 : :
799 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
800 [ # # ]: 0 : if (gpa == 0) {
801 : 0 : DRV_LOG(ERR, "Fail to get GPA for available ring.");
802 : 0 : return -1;
803 : : }
804 : 0 : hw->vring[i].avail = gpa;
805 : :
806 : : /* NET: Direct I/O for Tx queue, relay for Rx queue
807 : : * BLK: relay every queue
808 : : */
809 [ # # # # ]: 0 : if ((internal->hw.device_type == IFCVF_NET) && (i & 1)) {
810 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
811 [ # # ]: 0 : if (gpa == 0) {
812 : 0 : DRV_LOG(ERR, "Fail to get GPA for used ring.");
813 : 0 : return -1;
814 : : }
815 : 0 : hw->vring[i].used = gpa;
816 : : } else {
817 : 0 : hw->vring[i].used = m_vring_iova +
818 : 0 : (char *)internal->m_vring[i].used -
819 : 0 : (char *)internal->m_vring[i].desc;
820 : : }
821 : :
822 : 0 : hw->vring[i].size = vq.size;
823 : :
824 : 0 : rte_vhost_get_vring_base(vid, i,
825 : 0 : &internal->m_vring[i].avail->idx,
826 : 0 : &internal->m_vring[i].used->idx);
827 : :
828 : 0 : rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
829 : 0 : &hw->vring[i].last_used_idx);
830 : :
831 : 0 : m_vring_iova += size;
832 : : }
833 : 0 : hw->nr_vring = nr_vring;
834 : :
835 : 0 : return ifcvf_start_hw(&internal->hw);
836 : :
837 : : error:
838 [ # # ]: 0 : for (i = 0; i < nr_vring; i++)
839 : 0 : rte_free(internal->m_vring[i].desc);
840 : :
841 : : return -1;
842 : : }
843 : :
844 : : static int
845 : 0 : m_ifcvf_stop(struct ifcvf_internal *internal)
846 : : {
847 : : int vid;
848 : : uint32_t i;
849 : : struct rte_vhost_vring vq;
850 : 0 : struct ifcvf_hw *hw = &internal->hw;
851 : : uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
852 : : uint64_t size, len;
853 : : u32 ring_state = 0;
854 : :
855 : 0 : vid = internal->vid;
856 : :
857 : : /* to make sure no packet is lost for blk device
858 : : * do not stop until last_avail_idx == last_used_idx
859 : : */
860 [ # # ]: 0 : if (internal->hw.device_type == IFCVF_BLK) {
861 [ # # ]: 0 : for (i = 0; i < hw->nr_vring; i++) {
862 : : do {
863 [ # # ]: 0 : if (hw->lm_cfg != NULL)
864 : 0 : ring_state = *(u32 *)(hw->lm_cfg +
865 : 0 : IFCVF_LM_RING_STATE_OFFSET +
866 : 0 : i * IFCVF_LM_CFG_SIZE);
867 : 0 : hw->vring[i].last_avail_idx =
868 : 0 : (u16)(ring_state & IFCVF_16_BIT_MASK);
869 : 0 : hw->vring[i].last_used_idx =
870 : 0 : (u16)(ring_state >> 16);
871 : 0 : usleep(10);
872 : 0 : } while (hw->vring[i].last_avail_idx !=
873 [ # # ]: 0 : hw->vring[i].last_used_idx);
874 : : }
875 : : }
876 : :
877 : 0 : ifcvf_stop_hw(hw);
878 : :
879 [ # # ]: 0 : for (i = 0; i < hw->nr_vring; i++) {
880 : : /* synchronize remaining new used entries if any */
881 [ # # ]: 0 : if (internal->hw.device_type == IFCVF_NET) {
882 [ # # ]: 0 : if ((i & 1) == 0)
883 : 0 : update_used_ring(internal, i);
884 [ # # ]: 0 : } else if (internal->hw.device_type == IFCVF_BLK) {
885 : 0 : update_used_ring(internal, i);
886 : : }
887 : :
888 : 0 : rte_vhost_get_vhost_vring(vid, i, &vq);
889 : 0 : len = IFCVF_USED_RING_LEN(vq.size);
890 : 0 : rte_vhost_log_used_vring(vid, i, 0, len);
891 : :
892 : 0 : size = RTE_ALIGN_CEIL(vring_size(vq.size, rte_mem_page_size()),
893 : : rte_mem_page_size());
894 : 0 : dev_vfio_container_dma_unmap(internal->vfio_container_fd,
895 : 0 : (uint64_t)(uintptr_t)internal->m_vring[i].desc,
896 : : m_vring_iova, size);
897 : :
898 : 0 : rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
899 : 0 : hw->vring[i].last_used_idx);
900 : 0 : rte_free(internal->m_vring[i].desc);
901 : 0 : m_vring_iova += size;
902 : : }
903 : :
904 : 0 : return 0;
905 : : }
906 : :
907 : : static void
908 : 0 : update_used_ring(struct ifcvf_internal *internal, uint16_t qid)
909 : : {
910 : 0 : rte_vdpa_relay_vring_used(internal->vid, qid, &internal->m_vring[qid]);
911 : 0 : rte_vhost_vring_call(internal->vid, qid);
912 : 0 : }
913 : :
914 : : static uint32_t
915 : 0 : vring_relay(void *arg)
916 : : {
917 : : int i, vid, epfd, fd, nfds;
918 : : struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
919 : : struct rte_vhost_vring vring;
920 : : uint16_t qid, q_num;
921 : : struct epoll_event events[IFCVF_MAX_QUEUES * 4];
922 : : struct epoll_event ev;
923 : : int nbytes;
924 : : uint64_t buf;
925 : :
926 : 0 : vid = internal->vid;
927 : 0 : q_num = rte_vhost_get_vring_num(vid);
928 : :
929 : : /* add notify fd and interrupt fd to epoll */
930 : 0 : epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
931 [ # # ]: 0 : if (epfd < 0) {
932 : 0 : DRV_LOG(ERR, "failed to create epoll instance.");
933 : 0 : return 1;
934 : : }
935 : 0 : internal->epfd = epfd;
936 : :
937 : 0 : vring.kickfd = -1;
938 [ # # ]: 0 : for (qid = 0; qid < q_num; qid++) {
939 : 0 : ev.events = EPOLLIN | EPOLLPRI;
940 : 0 : rte_vhost_get_vhost_vring(vid, qid, &vring);
941 : 0 : ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
942 [ # # ]: 0 : if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
943 : 0 : DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
944 : 0 : return 1;
945 : : }
946 : : }
947 : :
948 [ # # ]: 0 : for (qid = 0; qid < q_num; qid += 1) {
949 [ # # # # ]: 0 : if ((internal->hw.device_type == IFCVF_NET) && (qid & 1))
950 : 0 : continue;
951 : 0 : ev.events = EPOLLIN | EPOLLPRI;
952 : : /* leave a flag to mark it's for interrupt */
953 : 0 : ev.data.u64 = 1 | qid << 1 |
954 : 0 : (uint64_t)internal->intr_fd[qid] << 32;
955 [ # # ]: 0 : if (epoll_ctl(epfd, EPOLL_CTL_ADD, internal->intr_fd[qid], &ev)
956 : : < 0) {
957 : 0 : DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
958 : 0 : return 1;
959 : : }
960 : 0 : update_used_ring(internal, qid);
961 : : }
962 : :
963 : : /* start relay with a first kick */
964 [ # # ]: 0 : for (qid = 0; qid < q_num; qid++)
965 : 0 : ifcvf_notify_queue(&internal->hw, qid);
966 : :
967 : : /* listen to the events and react accordingly */
968 : : for (;;) {
969 : 0 : nfds = epoll_wait(epfd, events, q_num * 2, -1);
970 [ # # ]: 0 : if (nfds < 0) {
971 [ # # ]: 0 : if (errno == EINTR)
972 : 0 : continue;
973 : 0 : DRV_LOG(ERR, "epoll_wait return fail.");
974 : 0 : return 1;
975 : : }
976 : :
977 [ # # ]: 0 : for (i = 0; i < nfds; i++) {
978 : 0 : fd = (uint32_t)(events[i].data.u64 >> 32);
979 : : do {
980 : 0 : nbytes = read(fd, &buf, 8);
981 [ # # ]: 0 : if (nbytes < 0) {
982 [ # # ]: 0 : if (errno == EINTR ||
983 : : errno == EWOULDBLOCK ||
984 : : errno == EAGAIN)
985 : : continue;
986 : 0 : DRV_LOG(INFO, "Error reading "
987 : : "kickfd: %s",
988 : : strerror(errno));
989 : : }
990 : : break;
991 : : } while (1);
992 : :
993 : 0 : qid = events[i].data.u32 >> 1;
994 : :
995 [ # # ]: 0 : if (events[i].data.u32 & 1)
996 : 0 : update_used_ring(internal, qid);
997 : : else
998 : 0 : ifcvf_notify_queue(&internal->hw, qid);
999 : : }
1000 : : }
1001 : :
1002 : : return 0;
1003 : : }
1004 : :
1005 : : static int
1006 : 0 : setup_vring_relay(struct ifcvf_internal *internal)
1007 : : {
1008 : : char name[RTE_THREAD_INTERNAL_NAME_SIZE];
1009 : : int ret;
1010 : :
1011 : 0 : snprintf(name, sizeof(name), "ifc-ring%d", internal->vid);
1012 : 0 : ret = rte_thread_create_internal_control(&internal->tid, name,
1013 : : vring_relay, internal);
1014 [ # # ]: 0 : if (ret != 0) {
1015 : 0 : DRV_LOG(ERR, "failed to create ring relay pthread.");
1016 : 0 : return -1;
1017 : : }
1018 : :
1019 : : return 0;
1020 : : }
1021 : :
1022 : : static int
1023 : 0 : unset_vring_relay(struct ifcvf_internal *internal)
1024 : : {
1025 [ # # ]: 0 : if (internal->tid.opaque_id != 0) {
1026 : 0 : pthread_cancel((pthread_t)internal->tid.opaque_id);
1027 : 0 : rte_thread_join(internal->tid, NULL);
1028 : : }
1029 : 0 : internal->tid.opaque_id = 0;
1030 : :
1031 [ # # ]: 0 : if (internal->epfd >= 0)
1032 : 0 : close(internal->epfd);
1033 : 0 : internal->epfd = -1;
1034 : :
1035 : 0 : return 0;
1036 : : }
1037 : :
1038 : : static int
1039 : 0 : ifcvf_sw_fallback_switchover(struct ifcvf_internal *internal)
1040 : : {
1041 : : int ret;
1042 : 0 : int vid = internal->vid;
1043 : :
1044 : : /* stop the direct IO data path */
1045 : 0 : unset_notify_relay(internal);
1046 : 0 : vdpa_ifcvf_stop(internal);
1047 : :
1048 : 0 : unset_intr_relay(internal);
1049 : :
1050 : 0 : vdpa_disable_vfio_intr(internal);
1051 : :
1052 : 0 : rte_atomic_store_explicit(&internal->running, false, rte_memory_order_seq_cst);
1053 : :
1054 : 0 : ret = rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, false);
1055 [ # # ]: 0 : if (ret && ret != -ENOTSUP)
1056 : 0 : goto error;
1057 : :
1058 : : /* set up interrupt for interrupt relay */
1059 : 0 : ret = vdpa_enable_vfio_intr(internal, true);
1060 [ # # ]: 0 : if (ret)
1061 : 0 : goto unmap;
1062 : :
1063 : : /* config the VF */
1064 : 0 : ret = m_ifcvf_start(internal);
1065 [ # # ]: 0 : if (ret)
1066 : 0 : goto unset_intr;
1067 : :
1068 : : /* set up vring relay thread */
1069 : 0 : ret = setup_vring_relay(internal);
1070 [ # # ]: 0 : if (ret)
1071 : 0 : goto stop_vf;
1072 : :
1073 : 0 : rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true);
1074 : :
1075 : 0 : internal->sw_fallback_running = true;
1076 : :
1077 : 0 : return 0;
1078 : :
1079 : : stop_vf:
1080 : 0 : m_ifcvf_stop(internal);
1081 : 0 : unset_intr:
1082 : 0 : vdpa_disable_vfio_intr(internal);
1083 : 0 : unmap:
1084 : 0 : ifcvf_dma_map(internal, false);
1085 : : error:
1086 : : return -1;
1087 : : }
1088 : :
1089 : : static int
1090 : 0 : ifcvf_dev_config(int vid)
1091 : : {
1092 : : struct rte_vdpa_device *vdev;
1093 : : struct internal_list *list;
1094 : : struct ifcvf_internal *internal;
1095 : : struct ifcvf_hw *hw;
1096 : : uint16_t i;
1097 : :
1098 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1099 : 0 : list = find_internal_resource_by_vdev(vdev);
1100 [ # # ]: 0 : if (list == NULL) {
1101 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1102 : 0 : return -1;
1103 : : }
1104 : :
1105 : 0 : internal = list->internal;
1106 : 0 : internal->vid = vid;
1107 : 0 : rte_atomic_store_explicit(&internal->dev_attached, true, rte_memory_order_seq_cst);
1108 [ # # ]: 0 : if (update_datapath(internal) < 0) {
1109 : 0 : DRV_LOG(ERR, "failed to update datapath for vDPA device %s",
1110 : : vdev->device->name);
1111 : 0 : rte_atomic_store_explicit(&internal->dev_attached, false, rte_memory_order_seq_cst);
1112 : 0 : return -1;
1113 : : }
1114 : :
1115 : : hw = &internal->hw;
1116 [ # # ]: 0 : for (i = 0; i < hw->nr_vring; i++) {
1117 [ # # ]: 0 : if (!hw->vring[i].enable)
1118 : 0 : continue;
1119 [ # # ]: 0 : if (rte_vhost_host_notifier_ctrl(vid, i, true) != 0)
1120 : 0 : DRV_LOG(NOTICE, "vDPA (%s): software relay is used.",
1121 : : vdev->device->name);
1122 : : }
1123 : :
1124 : 0 : internal->configured = 1;
1125 : 0 : DRV_LOG(INFO, "vDPA device %s is configured", vdev->device->name);
1126 : 0 : return 0;
1127 : : }
1128 : :
1129 : : static int
1130 : 0 : ifcvf_dev_close(int vid)
1131 : : {
1132 : : struct rte_vdpa_device *vdev;
1133 : : struct internal_list *list;
1134 : : struct ifcvf_internal *internal;
1135 : :
1136 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1137 : 0 : list = find_internal_resource_by_vdev(vdev);
1138 [ # # ]: 0 : if (list == NULL) {
1139 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1140 : 0 : return -1;
1141 : : }
1142 : :
1143 : 0 : internal = list->internal;
1144 : :
1145 [ # # ]: 0 : if (internal->sw_fallback_running) {
1146 : : /* unset ring relay */
1147 : 0 : unset_vring_relay(internal);
1148 : :
1149 : : /* reset VF */
1150 : 0 : m_ifcvf_stop(internal);
1151 : :
1152 : : /* remove interrupt setting */
1153 : 0 : vdpa_disable_vfio_intr(internal);
1154 : :
1155 : : /* unset DMA map for guest memory */
1156 : 0 : ifcvf_dma_map(internal, false);
1157 : :
1158 : 0 : internal->sw_fallback_running = false;
1159 : : } else {
1160 : 0 : rte_atomic_store_explicit(&internal->dev_attached, false, rte_memory_order_seq_cst);
1161 [ # # ]: 0 : if (update_datapath(internal) < 0) {
1162 : 0 : DRV_LOG(ERR, "failed to update datapath for vDPA device %s",
1163 : : vdev->device->name);
1164 : 0 : internal->configured = 0;
1165 : 0 : return -1;
1166 : : }
1167 : : }
1168 : :
1169 : 0 : internal->configured = 0;
1170 : 0 : return 0;
1171 : : }
1172 : :
1173 : : static int
1174 : 0 : ifcvf_set_features(int vid)
1175 : : {
1176 : 0 : uint64_t features = 0;
1177 : : struct rte_vdpa_device *vdev;
1178 : : struct internal_list *list;
1179 : : struct ifcvf_internal *internal;
1180 : 0 : uint64_t log_base = 0, log_size = 0;
1181 : :
1182 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1183 : 0 : list = find_internal_resource_by_vdev(vdev);
1184 [ # # ]: 0 : if (list == NULL) {
1185 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1186 : 0 : return -1;
1187 : : }
1188 : :
1189 : 0 : internal = list->internal;
1190 : 0 : rte_vhost_get_negotiated_features(vid, &features);
1191 : :
1192 [ # # ]: 0 : if (!RTE_VHOST_NEED_LOG(features))
1193 : : return 0;
1194 : :
1195 [ # # ]: 0 : if (internal->sw_lm) {
1196 : 0 : ifcvf_sw_fallback_switchover(internal);
1197 : : } else {
1198 : 0 : rte_vhost_get_log_base(vid, &log_base, &log_size);
1199 : 0 : dev_vfio_container_dma_map(internal->vfio_container_fd,
1200 : : log_base, IFCVF_LOG_BASE, log_size);
1201 : 0 : ifcvf_enable_logging(&internal->hw, IFCVF_LOG_BASE, log_size);
1202 : : }
1203 : :
1204 : : return 0;
1205 : : }
1206 : :
1207 : : static int
1208 : 0 : ifcvf_get_vfio_device_fd(int vid)
1209 : : {
1210 : : struct rte_vdpa_device *vdev;
1211 : : struct internal_list *list;
1212 : :
1213 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1214 : 0 : list = find_internal_resource_by_vdev(vdev);
1215 [ # # ]: 0 : if (list == NULL) {
1216 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1217 : 0 : return -1;
1218 : : }
1219 : :
1220 : 0 : return list->internal->vfio_dev_fd;
1221 : : }
1222 : :
1223 : : static int
1224 : 0 : ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
1225 : : {
1226 : : struct rte_vdpa_device *vdev;
1227 : : struct internal_list *list;
1228 : : struct ifcvf_internal *internal;
1229 : 0 : struct vfio_region_info reg = { .argsz = sizeof(reg) };
1230 : : int ret;
1231 : :
1232 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1233 : 0 : list = find_internal_resource_by_vdev(vdev);
1234 [ # # ]: 0 : if (list == NULL) {
1235 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1236 : 0 : return -1;
1237 : : }
1238 : :
1239 : 0 : internal = list->internal;
1240 : :
1241 : 0 : reg.index = ifcvf_get_notify_region(&internal->hw);
1242 : 0 : ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
1243 [ # # ]: 0 : if (ret) {
1244 : 0 : DRV_LOG(ERR, "Get not get device region info: %s",
1245 : : strerror(errno));
1246 : 0 : return -1;
1247 : : }
1248 : :
1249 : 0 : *offset = ifcvf_get_queue_notify_off(&internal->hw, qid) + reg.offset;
1250 : 0 : *size = 0x1000;
1251 : :
1252 : 0 : return 0;
1253 : : }
1254 : :
1255 : : static int
1256 : 0 : ifcvf_get_queue_num(struct rte_vdpa_device *vdev, uint32_t *queue_num)
1257 : : {
1258 : : struct internal_list *list;
1259 : :
1260 : 0 : list = find_internal_resource_by_vdev(vdev);
1261 [ # # ]: 0 : if (list == NULL) {
1262 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1263 : 0 : return -1;
1264 : : }
1265 : :
1266 : 0 : *queue_num = list->internal->max_queues;
1267 : :
1268 : 0 : return 0;
1269 : : }
1270 : :
1271 : : static int
1272 : 0 : ifcvf_get_vdpa_features(struct rte_vdpa_device *vdev, uint64_t *features)
1273 : : {
1274 : : struct internal_list *list;
1275 : :
1276 : 0 : list = find_internal_resource_by_vdev(vdev);
1277 [ # # ]: 0 : if (list == NULL) {
1278 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1279 : 0 : return -1;
1280 : : }
1281 : :
1282 : 0 : *features = list->internal->features;
1283 : :
1284 : 0 : return 0;
1285 : : }
1286 : :
1287 : : #define VDPA_SUPPORTED_PROTOCOL_FEATURES \
1288 : : (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
1289 : : 1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ | \
1290 : : 1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD | \
1291 : : 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER | \
1292 : : 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD | \
1293 : : 1ULL << VHOST_USER_PROTOCOL_F_MQ | \
1294 : : 1ULL << VHOST_USER_PROTOCOL_F_STATUS)
1295 : :
1296 : : #define VDPA_BLK_PROTOCOL_FEATURES \
1297 : : (1ULL << VHOST_USER_PROTOCOL_F_CONFIG)
1298 : :
1299 : : static int
1300 : 0 : ifcvf_get_protocol_features(struct rte_vdpa_device *vdev, uint64_t *features)
1301 : : {
1302 : : RTE_SET_USED(vdev);
1303 : :
1304 : 0 : *features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
1305 : 0 : return 0;
1306 : : }
1307 : :
1308 : : static int
1309 : 0 : ifcvf_config_vring(struct ifcvf_internal *internal, int vring)
1310 : : {
1311 : 0 : struct ifcvf_hw *hw = &internal->hw;
1312 : 0 : int vid = internal->vid;
1313 : : struct rte_vhost_vring vq;
1314 : : uint64_t gpa;
1315 : :
1316 [ # # ]: 0 : if (hw->vring[vring].enable) {
1317 : 0 : rte_vhost_get_vhost_vring(vid, vring, &vq);
1318 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
1319 [ # # ]: 0 : if (gpa == 0) {
1320 : 0 : DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
1321 : 0 : return -1;
1322 : : }
1323 : 0 : hw->vring[vring].desc = gpa;
1324 : :
1325 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
1326 [ # # ]: 0 : if (gpa == 0) {
1327 : 0 : DRV_LOG(ERR, "Fail to get GPA for available ring.");
1328 : 0 : return -1;
1329 : : }
1330 : 0 : hw->vring[vring].avail = gpa;
1331 : :
1332 : 0 : gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
1333 [ # # ]: 0 : if (gpa == 0) {
1334 : 0 : DRV_LOG(ERR, "Fail to get GPA for used ring.");
1335 : 0 : return -1;
1336 : : }
1337 : 0 : hw->vring[vring].used = gpa;
1338 : :
1339 : 0 : hw->vring[vring].size = vq.size;
1340 : 0 : rte_vhost_get_vring_base(vid, vring,
1341 : 0 : &hw->vring[vring].last_avail_idx,
1342 : 0 : &hw->vring[vring].last_used_idx);
1343 : 0 : ifcvf_enable_vring_hw(&internal->hw, vring);
1344 : : } else {
1345 : 0 : ifcvf_disable_vring_hw(&internal->hw, vring);
1346 : 0 : rte_vhost_set_vring_base(vid, vring,
1347 : 0 : hw->vring[vring].last_avail_idx,
1348 : 0 : hw->vring[vring].last_used_idx);
1349 : : }
1350 : :
1351 : : return 0;
1352 : : }
1353 : :
1354 : : static int
1355 : 0 : ifcvf_set_vring_state(int vid, int vring, int state)
1356 : : {
1357 : : struct rte_vdpa_device *vdev;
1358 : : struct internal_list *list;
1359 : : struct ifcvf_internal *internal;
1360 : : struct ifcvf_hw *hw;
1361 : 0 : bool enable = !!state;
1362 : : int ret = 0;
1363 : :
1364 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1365 : 0 : list = find_internal_resource_by_vdev(vdev);
1366 [ # # ]: 0 : if (list == NULL) {
1367 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1368 : 0 : return -1;
1369 : : }
1370 : :
1371 [ # # ]: 0 : DRV_LOG(INFO, "%s queue %d of vDPA device %s",
1372 : : enable ? "enable" : "disable", vring, vdev->device->name);
1373 : :
1374 : 0 : internal = list->internal;
1375 [ # # # # ]: 0 : if (vring < 0 || vring >= internal->max_queues * 2) {
1376 : 0 : DRV_LOG(ERR, "Vring index %d not correct", vring);
1377 : 0 : return -1;
1378 : : }
1379 : :
1380 : : hw = &internal->hw;
1381 : 0 : hw->vring[vring].enable = enable;
1382 : :
1383 [ # # ]: 0 : if (!internal->configured)
1384 : : return 0;
1385 : :
1386 : 0 : unset_notify_relay(internal);
1387 : :
1388 : 0 : ret = vdpa_enable_vfio_intr(internal, false);
1389 [ # # ]: 0 : if (ret) {
1390 : 0 : DRV_LOG(ERR, "failed to set vfio interrupt of vDPA device %s",
1391 : : vdev->device->name);
1392 : 0 : return ret;
1393 : : }
1394 : :
1395 : 0 : ret = ifcvf_config_vring(internal, vring);
1396 [ # # ]: 0 : if (ret) {
1397 : 0 : DRV_LOG(ERR, "failed to configure queue %d of vDPA device %s",
1398 : : vring, vdev->device->name);
1399 : 0 : return ret;
1400 : : }
1401 : :
1402 : 0 : ret = setup_notify_relay(internal);
1403 [ # # ]: 0 : if (ret) {
1404 : 0 : DRV_LOG(ERR, "failed to setup notify relay of vDPA device %s",
1405 : : vdev->device->name);
1406 : 0 : return ret;
1407 : : }
1408 : :
1409 : 0 : ret = rte_vhost_host_notifier_ctrl(vid, vring, enable);
1410 [ # # ]: 0 : if (ret) {
1411 : 0 : DRV_LOG(ERR, "vDPA device %s queue %d host notifier ctrl fail",
1412 : : vdev->device->name, vring);
1413 : 0 : return ret;
1414 : : }
1415 : :
1416 : : return 0;
1417 : : }
1418 : :
1419 : : static int
1420 : 0 : ifcvf_get_device_type(struct rte_vdpa_device *vdev,
1421 : : uint32_t *type)
1422 : : {
1423 : : struct ifcvf_internal *internal;
1424 : : struct internal_list *list;
1425 : 0 : struct rte_device *rte_dev = vdev->device;
1426 : :
1427 : 0 : list = find_internal_resource_by_rte_dev(rte_dev);
1428 [ # # ]: 0 : if (list == NULL) {
1429 : 0 : DRV_LOG(ERR, "Invalid rte device: %p", rte_dev);
1430 : 0 : return -1;
1431 : : }
1432 : :
1433 : 0 : internal = list->internal;
1434 : :
1435 [ # # ]: 0 : if (internal->hw.device_type == IFCVF_BLK)
1436 : 0 : *type = RTE_VHOST_VDPA_DEVICE_TYPE_BLK;
1437 : : else
1438 : 0 : *type = RTE_VHOST_VDPA_DEVICE_TYPE_NET;
1439 : :
1440 : : return 0;
1441 : : }
1442 : :
1443 : : static struct rte_vdpa_dev_ops ifcvf_net_ops = {
1444 : : .get_queue_num = ifcvf_get_queue_num,
1445 : : .get_features = ifcvf_get_vdpa_features,
1446 : : .get_protocol_features = ifcvf_get_protocol_features,
1447 : : .dev_conf = ifcvf_dev_config,
1448 : : .dev_close = ifcvf_dev_close,
1449 : : .set_vring_state = ifcvf_set_vring_state,
1450 : : .set_features = ifcvf_set_features,
1451 : : .migration_done = NULL,
1452 : : .get_vfio_device_fd = ifcvf_get_vfio_device_fd,
1453 : : .get_notify_area = ifcvf_get_notify_area,
1454 : : .get_dev_type = ifcvf_get_device_type,
1455 : : };
1456 : :
1457 : : static int
1458 : 0 : parse_bool(const char *key __rte_unused, const char *value, void *extra_args)
1459 : : {
1460 : 0 : char *endp = NULL;
1461 : :
1462 [ # # # # ]: 0 : if (value == NULL || extra_args == NULL || value[0] == '\0')
1463 : : return -EINVAL;
1464 : :
1465 : 0 : unsigned long n = strtoul(value, &endp, 0);
1466 [ # # # # ]: 0 : if (*endp != '\0' || n > 1)
1467 : : return -1;
1468 : :
1469 : 0 : *(bool *)extra_args = (n == 1);
1470 : 0 : return 0;
1471 : : }
1472 : :
1473 : : static int16_t
1474 : 0 : ifcvf_pci_get_device_type(struct rte_pci_device *pci_dev)
1475 : : {
1476 : 0 : uint16_t pci_device_id = pci_dev->id.device_id;
1477 : : uint16_t device_id;
1478 : :
1479 [ # # ]: 0 : if (pci_device_id < 0x1000 || pci_device_id > 0x107f) {
1480 : 0 : DRV_LOG(ERR, "Probe device is not a virtio device");
1481 : 0 : return -1;
1482 : : }
1483 : :
1484 [ # # ]: 0 : if (pci_device_id < 0x1040) {
1485 : : /* Transitional devices: use the PCI subsystem device id as
1486 : : * virtio device id, same as legacy driver always did.
1487 : : */
1488 : 0 : device_id = pci_dev->id.subsystem_device_id;
1489 : : } else {
1490 : : /* Modern devices: simply use PCI device id,
1491 : : * but start from 0x1040.
1492 : : */
1493 : 0 : device_id = pci_device_id - 0x1040;
1494 : : }
1495 : :
1496 : 0 : return device_id;
1497 : : }
1498 : :
1499 : : static int
1500 : 0 : ifcvf_blk_get_config(int vid, uint8_t *config, uint32_t size)
1501 : : {
1502 : : struct virtio_blk_config *dev_cfg;
1503 : : struct ifcvf_internal *internal;
1504 : : struct rte_vdpa_device *vdev;
1505 : : struct internal_list *list;
1506 : : uint32_t i;
1507 : : uint64_t capacity = 0;
1508 : : uint8_t *byte;
1509 : :
1510 [ # # ]: 0 : if (size < sizeof(struct virtio_blk_config)) {
1511 : 0 : DRV_LOG(ERR, "Invalid len: %u, required: %u",
1512 : : size, (uint32_t)sizeof(struct virtio_blk_config));
1513 : 0 : return -1;
1514 : : }
1515 : :
1516 : 0 : vdev = rte_vhost_get_vdpa_device(vid);
1517 [ # # ]: 0 : if (vdev == NULL) {
1518 : 0 : DRV_LOG(ERR, "Invalid vDPA device vid: %d", vid);
1519 : 0 : return -1;
1520 : : }
1521 : :
1522 : 0 : list = find_internal_resource_by_vdev(vdev);
1523 [ # # ]: 0 : if (list == NULL) {
1524 : 0 : DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1525 : 0 : return -1;
1526 : : }
1527 : :
1528 : 0 : internal = list->internal;
1529 : :
1530 [ # # ]: 0 : for (i = 0; i < sizeof(struct virtio_blk_config); i++)
1531 : 0 : config[i] = *((u8 *)internal->hw.blk_cfg + i);
1532 : :
1533 : 0 : dev_cfg = (struct virtio_blk_config *)internal->hw.blk_cfg;
1534 : :
1535 : : /* cannot read 64-bit register in one attempt, so read byte by byte. */
1536 [ # # ]: 0 : for (i = 0; i < sizeof(internal->hw.blk_cfg->capacity); i++) {
1537 : 0 : byte = (uint8_t *)&internal->hw.blk_cfg->capacity + i;
1538 : 0 : capacity |= (uint64_t)*byte << (i * 8);
1539 : : }
1540 : : /* The capacity is number of sectors in 512-byte.
1541 : : * So right shift 1 bit we get in K,
1542 : : * another right shift 10 bits we get in M,
1543 : : * right shift 10 more bits, we get in G.
1544 : : * To show capacity in G, we right shift 21 bits in total.
1545 : : */
1546 : 0 : DRV_LOG(DEBUG, "capacity : %"PRIu64"G", capacity >> 21);
1547 : :
1548 : 0 : DRV_LOG(DEBUG, "size_max : 0x%08x", dev_cfg->size_max);
1549 : 0 : DRV_LOG(DEBUG, "seg_max : 0x%08x", dev_cfg->seg_max);
1550 : 0 : DRV_LOG(DEBUG, "blk_size : 0x%08x", dev_cfg->blk_size);
1551 : 0 : DRV_LOG(DEBUG, "geometry");
1552 : 0 : DRV_LOG(DEBUG, " cylinders: %u", dev_cfg->geometry.cylinders);
1553 : 0 : DRV_LOG(DEBUG, " heads : %u", dev_cfg->geometry.heads);
1554 : 0 : DRV_LOG(DEBUG, " sectors : %u", dev_cfg->geometry.sectors);
1555 : 0 : DRV_LOG(DEBUG, "num_queues: 0x%08x", dev_cfg->num_queues);
1556 : :
1557 : 0 : DRV_LOG(DEBUG, "config: [%x] [%x] [%x] [%x] [%x] [%x] [%x] [%x]",
1558 : : config[0], config[1], config[2], config[3], config[4],
1559 : : config[5], config[6], config[7]);
1560 : 0 : return 0;
1561 : : }
1562 : :
1563 : : static int
1564 : 0 : ifcvf_blk_get_protocol_features(struct rte_vdpa_device *vdev,
1565 : : uint64_t *features)
1566 : : {
1567 : : RTE_SET_USED(vdev);
1568 : :
1569 : : *features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
1570 : 0 : *features |= VDPA_BLK_PROTOCOL_FEATURES;
1571 : 0 : return 0;
1572 : : }
1573 : :
1574 : : static struct rte_vdpa_dev_ops ifcvf_blk_ops = {
1575 : : .get_queue_num = ifcvf_get_queue_num,
1576 : : .get_features = ifcvf_get_vdpa_features,
1577 : : .set_features = ifcvf_set_features,
1578 : : .get_protocol_features = ifcvf_blk_get_protocol_features,
1579 : : .dev_conf = ifcvf_dev_config,
1580 : : .dev_close = ifcvf_dev_close,
1581 : : .set_vring_state = ifcvf_set_vring_state,
1582 : : .migration_done = NULL,
1583 : : .get_vfio_device_fd = ifcvf_get_vfio_device_fd,
1584 : : .get_notify_area = ifcvf_get_notify_area,
1585 : : .get_config = ifcvf_blk_get_config,
1586 : : .get_dev_type = ifcvf_get_device_type,
1587 : : };
1588 : :
1589 : : struct rte_vdpa_dev_info dev_info[] = {
1590 : : {
1591 : : .features = (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
1592 : : (1ULL << VIRTIO_NET_F_CTRL_VQ) |
1593 : : (1ULL << VIRTIO_NET_F_STATUS) |
1594 : : (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
1595 : : (1ULL << VHOST_F_LOG_ALL),
1596 : : .ops = &ifcvf_net_ops,
1597 : : },
1598 : : {
1599 : : .features = (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
1600 : : (1ULL << VHOST_F_LOG_ALL),
1601 : : .ops = &ifcvf_blk_ops,
1602 : : },
1603 : : };
1604 : :
1605 : : static int
1606 : 0 : ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1607 : : struct rte_pci_device *pci_dev)
1608 : : {
1609 : : uint64_t features;
1610 : : struct ifcvf_internal *internal = NULL;
1611 : : struct internal_list *list = NULL;
1612 : 0 : bool vdpa_mode = false;
1613 : 0 : bool sw_fallback_lm = false;
1614 : : struct rte_kvargs *kvlist = NULL;
1615 : : int ret = 0;
1616 : : int16_t device_id;
1617 : : uint64_t capacity = 0;
1618 : : uint8_t *byte;
1619 : : uint32_t i;
1620 : : uint16_t queue_pairs;
1621 : :
1622 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1623 : : return 0;
1624 : :
1625 [ # # ]: 0 : if (!pci_dev->device.devargs)
1626 : : return 1;
1627 : :
1628 : 0 : kvlist = rte_kvargs_parse(pci_dev->device.devargs->args,
1629 : : ifcvf_valid_arguments);
1630 [ # # ]: 0 : if (kvlist == NULL)
1631 : : return 1;
1632 : :
1633 : : /* probe only when vdpa mode is specified */
1634 [ # # ]: 0 : if (rte_kvargs_count(kvlist, IFCVF_VDPA_MODE) == 0) {
1635 : 0 : rte_kvargs_free(kvlist);
1636 : 0 : return 1;
1637 : : }
1638 : :
1639 : 0 : ret = rte_kvargs_process(kvlist, IFCVF_VDPA_MODE, &parse_bool,
1640 : : &vdpa_mode);
1641 [ # # # # ]: 0 : if (ret < 0 || !vdpa_mode) {
1642 : 0 : rte_kvargs_free(kvlist);
1643 : 0 : return 1;
1644 : : }
1645 : :
1646 : 0 : list = rte_zmalloc("ifcvf", sizeof(*list), 0);
1647 [ # # ]: 0 : if (list == NULL)
1648 : 0 : goto error;
1649 : :
1650 : 0 : internal = rte_zmalloc("ifcvf", sizeof(*internal), 0);
1651 [ # # ]: 0 : if (internal == NULL)
1652 : 0 : goto error;
1653 : :
1654 : 0 : internal->pdev = pci_dev;
1655 : : rte_spinlock_init(&internal->lock);
1656 : :
1657 [ # # ]: 0 : if (ifcvf_vfio_setup(internal) < 0) {
1658 : 0 : DRV_LOG(ERR, "failed to setup device %s", pci_dev->name);
1659 : 0 : goto error;
1660 : : }
1661 : :
1662 [ # # ]: 0 : if (ifcvf_init_hw(&internal->hw, internal->pdev) < 0) {
1663 : 0 : DRV_LOG(ERR, "failed to init device %s", pci_dev->name);
1664 : 0 : goto error;
1665 : : }
1666 : :
1667 : 0 : internal->configured = 0;
1668 : 0 : features = ifcvf_get_features(&internal->hw);
1669 : :
1670 : 0 : device_id = ifcvf_pci_get_device_type(pci_dev);
1671 [ # # ]: 0 : if (device_id < 0) {
1672 : 0 : DRV_LOG(ERR, "failed to get device %s type", pci_dev->name);
1673 : 0 : goto error;
1674 : : }
1675 : :
1676 [ # # ]: 0 : if (device_id == VIRTIO_ID_NET) {
1677 : 0 : internal->hw.device_type = IFCVF_NET;
1678 : : /*
1679 : : * ifc device always has CTRL_VQ,
1680 : : * and supports VIRTIO_NET_F_CTRL_VQ feature.
1681 : : */
1682 : 0 : queue_pairs = (internal->hw.common_cfg->num_queues - 1) / 2;
1683 : 0 : DRV_LOG(INFO, "%s support %u queue pairs", pci_dev->name,
1684 : : queue_pairs);
1685 : 0 : internal->max_queues = MIN(IFCVF_MAX_QUEUES, queue_pairs);
1686 : 0 : internal->features = features &
1687 : : ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
1688 : 0 : internal->features |= dev_info[IFCVF_NET].features;
1689 [ # # ]: 0 : } else if (device_id == VIRTIO_ID_BLOCK) {
1690 : 0 : internal->hw.device_type = IFCVF_BLK;
1691 : 0 : internal->features = features &
1692 : : ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
1693 : 0 : internal->features |= dev_info[IFCVF_BLK].features;
1694 : :
1695 : : /* cannot read 64-bit register in one attempt,
1696 : : * so read byte by byte.
1697 : : */
1698 [ # # ]: 0 : for (i = 0; i < sizeof(internal->hw.blk_cfg->capacity); i++) {
1699 : 0 : byte = (uint8_t *)&internal->hw.blk_cfg->capacity + i;
1700 : 0 : capacity |= (uint64_t)*byte << (i * 8);
1701 : : }
1702 : : /* The capacity is number of sectors in 512-byte.
1703 : : * So right shift 1 bit we get in K,
1704 : : * another right shift 10 bits we get in M,
1705 : : * right shift 10 more bits, we get in G.
1706 : : * To show capacity in G, we right shift 21 bits in total.
1707 : : */
1708 : 0 : DRV_LOG(DEBUG, "capacity : %"PRIu64"G", capacity >> 21);
1709 : :
1710 : 0 : DRV_LOG(DEBUG, "size_max : 0x%08x",
1711 : : internal->hw.blk_cfg->size_max);
1712 : 0 : DRV_LOG(DEBUG, "seg_max : 0x%08x",
1713 : : internal->hw.blk_cfg->seg_max);
1714 : 0 : DRV_LOG(DEBUG, "blk_size : 0x%08x",
1715 : : internal->hw.blk_cfg->blk_size);
1716 : 0 : DRV_LOG(DEBUG, "geometry");
1717 : 0 : DRV_LOG(DEBUG, " cylinders: %u",
1718 : : internal->hw.blk_cfg->geometry.cylinders);
1719 : 0 : DRV_LOG(DEBUG, " heads : %u",
1720 : : internal->hw.blk_cfg->geometry.heads);
1721 : 0 : DRV_LOG(DEBUG, " sectors : %u",
1722 : : internal->hw.blk_cfg->geometry.sectors);
1723 : 0 : DRV_LOG(DEBUG, "num_queues: 0x%08x",
1724 : : internal->hw.blk_cfg->num_queues);
1725 : :
1726 : 0 : internal->max_queues = MIN(IFCVF_MAX_QUEUES,
1727 : : internal->hw.blk_cfg->num_queues);
1728 : : }
1729 : :
1730 : 0 : list->internal = internal;
1731 : :
1732 [ # # ]: 0 : if (rte_kvargs_count(kvlist, IFCVF_SW_FALLBACK_LM)) {
1733 : 0 : ret = rte_kvargs_process(kvlist, IFCVF_SW_FALLBACK_LM,
1734 : : &parse_bool, &sw_fallback_lm);
1735 [ # # ]: 0 : if (ret < 0)
1736 : 0 : goto error;
1737 : : }
1738 : 0 : internal->sw_lm = sw_fallback_lm;
1739 [ # # # # ]: 0 : if (!internal->sw_lm && !internal->hw.lm_cfg) {
1740 : 0 : DRV_LOG(ERR, "Device %s does not support HW assist live migration, please enable sw-live-migration!",
1741 : : pci_dev->name);
1742 : 0 : goto error;
1743 : : }
1744 : :
1745 : 0 : pthread_mutex_lock(&internal_list_lock);
1746 : 0 : TAILQ_INSERT_TAIL(&internal_list, list, next);
1747 : 0 : pthread_mutex_unlock(&internal_list_lock);
1748 : :
1749 : 0 : internal->vdev = rte_vdpa_register_device(&pci_dev->device,
1750 : 0 : dev_info[internal->hw.device_type].ops);
1751 [ # # ]: 0 : if (internal->vdev == NULL) {
1752 : 0 : DRV_LOG(ERR, "failed to register device %s", pci_dev->name);
1753 : 0 : pthread_mutex_lock(&internal_list_lock);
1754 [ # # ]: 0 : TAILQ_REMOVE(&internal_list, list, next);
1755 : 0 : pthread_mutex_unlock(&internal_list_lock);
1756 : 0 : goto error;
1757 : : }
1758 : :
1759 : 0 : rte_atomic_store_explicit(&internal->started, true, rte_memory_order_seq_cst);
1760 [ # # ]: 0 : if (update_datapath(internal) < 0) {
1761 : 0 : DRV_LOG(ERR, "failed to update datapath %s", pci_dev->name);
1762 : 0 : rte_atomic_store_explicit(&internal->started, false, rte_memory_order_seq_cst);
1763 : 0 : rte_vdpa_unregister_device(internal->vdev);
1764 : 0 : pthread_mutex_lock(&internal_list_lock);
1765 [ # # ]: 0 : TAILQ_REMOVE(&internal_list, list, next);
1766 : 0 : pthread_mutex_unlock(&internal_list_lock);
1767 : 0 : goto error;
1768 : : }
1769 : :
1770 : 0 : rte_kvargs_free(kvlist);
1771 : 0 : return 0;
1772 : :
1773 : 0 : error:
1774 : 0 : rte_kvargs_free(kvlist);
1775 : 0 : rte_free(list);
1776 : 0 : rte_free(internal);
1777 : 0 : return -1;
1778 : : }
1779 : :
1780 : : static int
1781 : 0 : ifcvf_pci_remove(struct rte_pci_device *pci_dev)
1782 : : {
1783 : : struct ifcvf_internal *internal;
1784 : : struct internal_list *list;
1785 : :
1786 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1787 : : return 0;
1788 : :
1789 : 0 : list = find_internal_resource_by_pci_dev(pci_dev);
1790 [ # # ]: 0 : if (list == NULL) {
1791 : 0 : DRV_LOG(ERR, "Invalid device: %s", pci_dev->name);
1792 : 0 : return -1;
1793 : : }
1794 : :
1795 : 0 : internal = list->internal;
1796 : 0 : rte_atomic_store_explicit(&internal->started, false, rte_memory_order_seq_cst);
1797 [ # # ]: 0 : if (update_datapath(internal) < 0)
1798 : 0 : DRV_LOG(ERR, "failed to update datapath %s", pci_dev->name);
1799 : :
1800 : 0 : rte_pci_unmap_device(internal->pdev);
1801 : 0 : dev_vfio_container_destroy(internal->vfio_container_fd);
1802 : 0 : rte_vdpa_unregister_device(internal->vdev);
1803 : :
1804 : 0 : pthread_mutex_lock(&internal_list_lock);
1805 [ # # ]: 0 : TAILQ_REMOVE(&internal_list, list, next);
1806 : 0 : pthread_mutex_unlock(&internal_list_lock);
1807 : :
1808 : 0 : rte_free(list);
1809 : 0 : rte_free(internal);
1810 : :
1811 : 0 : return 0;
1812 : : }
1813 : :
1814 : : /*
1815 : : * IFCVF has the same vendor ID and device ID as virtio net PCI
1816 : : * device, with its specific subsystem vendor ID and device ID.
1817 : : */
1818 : : static const struct rte_pci_id pci_id_ifcvf_map[] = {
1819 : : { .class_id = RTE_CLASS_ANY_ID,
1820 : : .vendor_id = IFCVF_VENDOR_ID,
1821 : : .device_id = IFCVF_NET_MODERN_DEVICE_ID,
1822 : : .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1823 : : .subsystem_device_id = IFCVF_SUBSYS_DEVICE_ID,
1824 : : },
1825 : :
1826 : : { .class_id = RTE_CLASS_ANY_ID,
1827 : : .vendor_id = IFCVF_VENDOR_ID,
1828 : : .device_id = IFCVF_NET_TRANSITIONAL_DEVICE_ID,
1829 : : .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1830 : : .subsystem_device_id = IFCVF_SUBSYS_NET_DEVICE_ID,
1831 : : },
1832 : :
1833 : : { .class_id = RTE_CLASS_ANY_ID,
1834 : : .vendor_id = IFCVF_VENDOR_ID,
1835 : : .device_id = IFCVF_BLK_TRANSITIONAL_DEVICE_ID,
1836 : : .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1837 : : .subsystem_device_id = IFCVF_SUBSYS_BLK_DEVICE_ID,
1838 : : },
1839 : :
1840 : : { .class_id = RTE_CLASS_ANY_ID,
1841 : : .vendor_id = IFCVF_VENDOR_ID,
1842 : : .device_id = IFCVF_BLK_MODERN_DEVICE_ID,
1843 : : .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1844 : : .subsystem_device_id = IFCVF_SUBSYS_BLK_DEVICE_ID,
1845 : : },
1846 : :
1847 : : { .class_id = RTE_CLASS_ANY_ID,
1848 : : .vendor_id = IFCVF_VENDOR_ID,
1849 : : .device_id = IFCVF_BLK_MODERN_DEVICE_ID,
1850 : : .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1851 : : .subsystem_device_id = IFCVF_SUBSYS_DEFAULT_DEVICE_ID,
1852 : : }, /* virtio-blk devices with default subsystem IDs */
1853 : :
1854 : : { .vendor_id = 0, /* sentinel */
1855 : : },
1856 : : };
1857 : :
1858 : : static struct rte_pci_driver rte_ifcvf_vdpa = {
1859 : : .id_table = pci_id_ifcvf_map,
1860 : : .drv_flags = 0,
1861 : : .probe = ifcvf_pci_probe,
1862 : : .remove = ifcvf_pci_remove,
1863 : : };
1864 : :
1865 : 305 : RTE_PMD_REGISTER_PCI(net_ifcvf, rte_ifcvf_vdpa);
1866 : : RTE_PMD_REGISTER_PCI_TABLE(net_ifcvf, pci_id_ifcvf_map);
1867 : : RTE_PMD_REGISTER_KMOD_DEP(net_ifcvf, "* vfio-pci");
|