2 * Copyright (c) 2018 VMware, Inc.
4 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
7 /* Driver for VMware Virtual Machine Communication Interface (VMCI) device. */
10 __FBSDID("$FreeBSD$");
12 #include <sys/types.h>
14 #include <sys/kernel.h>
15 #include <sys/malloc.h>
16 #include <sys/module.h>
18 #include <sys/systm.h>
20 #include <dev/pci/pcireg.h>
21 #include <dev/pci/pcivar.h>
23 #include <machine/bus.h>
26 #include "vmci_doorbell.h"
27 #include "vmci_driver.h"
28 #include "vmci_kernel_defs.h"
29 #include "vmci_queue_pair.h"
31 static int vmci_probe(device_t);
32 static int vmci_attach(device_t);
33 static int vmci_detach(device_t);
34 static int vmci_shutdown(device_t);
36 static int vmci_map_bars(struct vmci_softc *);
37 static void vmci_unmap_bars(struct vmci_softc *);
39 static int vmci_config_capabilities(struct vmci_softc *);
41 static int vmci_dma_malloc_int(struct vmci_softc *, bus_size_t,
42 bus_size_t, struct vmci_dma_alloc *);
43 static void vmci_dma_free_int(struct vmci_softc *,
44 struct vmci_dma_alloc *);
46 static int vmci_config_interrupts(struct vmci_softc *);
47 static int vmci_config_interrupt(struct vmci_softc *);
48 static int vmci_check_intr_cnt(struct vmci_softc *);
49 static int vmci_allocate_interrupt_resources(struct vmci_softc *);
50 static int vmci_setup_interrupts(struct vmci_softc *);
51 static void vmci_dismantle_interrupts(struct vmci_softc *);
52 static void vmci_interrupt(void *);
53 static void vmci_interrupt_bm(void *);
54 static void dispatch_datagrams(void *, int);
55 static void process_bitmap(void *, int);
57 static void vmci_delayed_work_fn_cb(void *context, int data);
59 static device_method_t vmci_methods[] = {
60 /* Device interface. */
61 DEVMETHOD(device_probe, vmci_probe),
62 DEVMETHOD(device_attach, vmci_attach),
63 DEVMETHOD(device_detach, vmci_detach),
64 DEVMETHOD(device_shutdown, vmci_shutdown),
69 static driver_t vmci_driver = {
70 "vmci", vmci_methods, sizeof(struct vmci_softc)
73 static devclass_t vmci_devclass;
74 DRIVER_MODULE(vmci, pci, vmci_driver, vmci_devclass, 0, 0);
75 MODULE_VERSION(vmci, VMCI_VERSION);
81 { VMCI_VMWARE_VENDOR_ID, VMCI_VMWARE_DEVICE_ID,
82 "VMware Virtual Machine Communication Interface" },
84 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, vmci, vmci_ids,
87 MODULE_DEPEND(vmci, pci, 1, 1, 1);
89 static struct vmci_softc *vmci_sc;
91 #define LGPFX "vmci: "
93 * Allocate a buffer for incoming datagrams globally to avoid repeated
94 * allocation in the interrupt handler's atomic context.
96 static uint8_t *data_buffer = NULL;
97 static uint32_t data_buffer_size = VMCI_MAX_DG_SIZE;
99 struct vmci_delayed_work_info {
100 vmci_work_fn *work_fn;
102 vmci_list_item(vmci_delayed_work_info) entry;
106 *------------------------------------------------------------------------------
110 * Probe to see if the VMCI device is present.
113 * BUS_PROBE_DEFAULT if device exists, ENXIO otherwise.
118 *------------------------------------------------------------------------------
122 vmci_probe(device_t dev)
125 if (pci_get_vendor(dev) == vmci_ids[0].vendor &&
126 pci_get_device(dev) == vmci_ids[0].device) {
127 device_set_desc(dev, vmci_ids[0].desc);
129 return (BUS_PROBE_DEFAULT);
136 *------------------------------------------------------------------------------
140 * Attach VMCI device to the system after vmci_probe() has been called and
141 * the device has been detected.
144 * 0 if success, ENXIO otherwise.
149 *------------------------------------------------------------------------------
153 vmci_attach(device_t dev)
155 struct vmci_softc *sc;
158 sc = device_get_softc(dev);
163 sc->vmci_num_intr = 0;
164 for (i = 0; i < VMCI_MAX_INTRS; i++) {
165 sc->vmci_intrs[i].vmci_irq = NULL;
166 sc->vmci_intrs[i].vmci_handler = NULL;
169 TASK_INIT(&sc->vmci_interrupt_dq_task, 0, dispatch_datagrams, sc);
170 TASK_INIT(&sc->vmci_interrupt_bm_task, 0, process_bitmap, sc);
172 TASK_INIT(&sc->vmci_delayed_work_task, 0, vmci_delayed_work_fn_cb, sc);
174 pci_enable_busmaster(dev);
176 mtx_init(&sc->vmci_spinlock, "VMCI Spinlock", NULL, MTX_SPIN);
177 mtx_init(&sc->vmci_delayed_work_lock, "VMCI Delayed Work Lock",
180 error = vmci_map_bars(sc);
182 VMCI_LOG_ERROR(LGPFX"Failed to map PCI BARs.\n");
186 error = vmci_config_capabilities(sc);
188 VMCI_LOG_ERROR(LGPFX"Failed to configure capabilities.\n");
192 vmci_list_init(&sc->vmci_delayed_work_infos);
194 vmci_components_init();
196 error = vmci_qp_guest_endpoints_init();
198 VMCI_LOG_ERROR(LGPFX"vmci_qp_guest_endpoints_init failed.\n");
202 error = vmci_config_interrupts(sc);
204 VMCI_LOG_ERROR(LGPFX"Failed to enable interrupts.\n");
216 *------------------------------------------------------------------------------
220 * Detach the VMCI device.
228 *------------------------------------------------------------------------------
232 vmci_detach(device_t dev)
234 struct vmci_softc *sc;
236 sc = device_get_softc(dev);
238 vmci_qp_guest_endpoints_exit();
241 vmci_dismantle_interrupts(sc);
243 vmci_components_cleanup();
245 taskqueue_drain(taskqueue_thread, &sc->vmci_delayed_work_task);
246 mtx_destroy(&sc->vmci_delayed_work_lock);
248 if (sc->vmci_res0 != NULL)
249 bus_space_write_4(sc->vmci_iot0, sc->vmci_ioh0,
250 VMCI_CONTROL_ADDR, VMCI_CONTROL_RESET);
252 if (sc->vmci_notifications_bitmap.dma_vaddr != NULL)
253 vmci_dma_free(&sc->vmci_notifications_bitmap);
257 mtx_destroy(&sc->vmci_spinlock);
259 pci_disable_busmaster(dev);
265 *------------------------------------------------------------------------------
269 * This function is called during system shutdown. We don't do anything.
277 *------------------------------------------------------------------------------
281 vmci_shutdown(device_t dev)
288 *------------------------------------------------------------------------------
292 * Maps the PCI I/O and MMIO BARs.
295 * 0 on success, ENXIO otherwise.
300 *------------------------------------------------------------------------------
304 vmci_map_bars(struct vmci_softc *sc)
308 /* Map the PCI I/O BAR: BAR0 */
310 sc->vmci_res0 = bus_alloc_resource_any(sc->vmci_dev, SYS_RES_IOPORT,
312 if (sc->vmci_res0 == NULL) {
313 VMCI_LOG_ERROR(LGPFX"Could not map: BAR0\n");
317 sc->vmci_iot0 = rman_get_bustag(sc->vmci_res0);
318 sc->vmci_ioh0 = rman_get_bushandle(sc->vmci_res0);
319 sc->vmci_ioaddr = rman_get_start(sc->vmci_res0);
321 /* Map the PCI MMIO BAR: BAR1 */
323 sc->vmci_res1 = bus_alloc_resource_any(sc->vmci_dev, SYS_RES_MEMORY,
325 if (sc->vmci_res1 == NULL) {
326 VMCI_LOG_ERROR(LGPFX"Could not map: BAR1\n");
330 sc->vmci_iot1 = rman_get_bustag(sc->vmci_res1);
331 sc->vmci_ioh1 = rman_get_bushandle(sc->vmci_res1);
337 *------------------------------------------------------------------------------
341 * Unmaps the VMCI PCI I/O and MMIO BARs.
349 *------------------------------------------------------------------------------
353 vmci_unmap_bars(struct vmci_softc *sc)
357 if (sc->vmci_res0 != NULL) {
359 bus_release_resource(sc->vmci_dev, SYS_RES_IOPORT, rid,
361 sc->vmci_res0 = NULL;
364 if (sc->vmci_res1 != NULL) {
366 bus_release_resource(sc->vmci_dev, SYS_RES_MEMORY, rid,
368 sc->vmci_res1 = NULL;
373 *------------------------------------------------------------------------------
375 * vmci_config_capabilities --
377 * Check the VMCI device capabilities and configure the device accordingly.
380 * 0 if success, ENODEV otherwise.
383 * Device capabilities are enabled.
385 *------------------------------------------------------------------------------
389 vmci_config_capabilities(struct vmci_softc *sc)
391 unsigned long bitmap_PPN;
395 * Verify that the VMCI device supports the capabilities that we
396 * need. Datagrams are necessary and notifications will be used
397 * if the device supports it.
399 sc->capabilities = bus_space_read_4(sc->vmci_iot0, sc->vmci_ioh0,
402 if ((sc->capabilities & VMCI_CAPS_DATAGRAM) == 0) {
403 VMCI_LOG_ERROR(LGPFX"VMCI device does not support "
408 if (sc->capabilities & VMCI_CAPS_NOTIFICATIONS) {
409 sc->capabilities = VMCI_CAPS_DATAGRAM;
410 error = vmci_dma_malloc(PAGE_SIZE, 1,
411 &sc->vmci_notifications_bitmap);
413 VMCI_LOG_ERROR(LGPFX"Failed to alloc memory for "
414 "notification bitmap.\n");
416 memset(sc->vmci_notifications_bitmap.dma_vaddr, 0,
418 sc->capabilities |= VMCI_CAPS_NOTIFICATIONS;
421 sc->capabilities = VMCI_CAPS_DATAGRAM;
423 /* Let the host know which capabilities we intend to use. */
424 bus_space_write_4(sc->vmci_iot0, sc->vmci_ioh0,
425 VMCI_CAPS_ADDR, sc->capabilities);
428 * Register notification bitmap with device if that capability is
431 if (sc->capabilities & VMCI_CAPS_NOTIFICATIONS) {
433 sc->vmci_notifications_bitmap.dma_paddr >> PAGE_SHIFT;
434 vmci_register_notification_bitmap(bitmap_PPN);
437 /* Check host capabilities. */
438 if (!vmci_check_host_capabilities())
445 *------------------------------------------------------------------------------
449 * Callback to receive mapping information resulting from the load of a
450 * bus_dmamap_t via bus_dmamap_load()
458 *------------------------------------------------------------------------------
462 vmci_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
464 bus_addr_t *baddr = arg;
467 *baddr = segs->ds_addr;
471 *------------------------------------------------------------------------------
473 * vmci_dma_malloc_int --
475 * Internal function that allocates DMA memory.
479 * ENOMEM if insufficient memory.
480 * EINPROGRESS if mapping is deferred.
481 * EINVAL if the request was invalid.
484 * DMA memory is allocated.
486 *------------------------------------------------------------------------------
490 vmci_dma_malloc_int(struct vmci_softc *sc, bus_size_t size, bus_size_t align,
491 struct vmci_dma_alloc *dma)
495 bzero(dma, sizeof(struct vmci_dma_alloc));
497 error = bus_dma_tag_create(bus_get_dma_tag(vmci_sc->vmci_dev),
498 align, 0, /* alignment, bounds */
499 BUS_SPACE_MAXADDR, /* lowaddr */
500 BUS_SPACE_MAXADDR, /* highaddr */
501 NULL, NULL, /* filter, filterarg */
504 size, /* maxsegsize */
505 BUS_DMA_ALLOCNOW, /* flags */
507 NULL, /* lockfuncarg */
510 VMCI_LOG_ERROR(LGPFX"bus_dma_tag_create failed: %d\n", error);
514 error = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
515 BUS_DMA_ZERO | BUS_DMA_NOWAIT, &dma->dma_map);
517 VMCI_LOG_ERROR(LGPFX"bus_dmamem_alloc failed: %d\n", error);
521 error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
522 size, vmci_dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
524 VMCI_LOG_ERROR(LGPFX"bus_dmamap_load failed: %d\n", error);
528 dma->dma_size = size;
538 *------------------------------------------------------------------------------
542 * This function is a wrapper around vmci_dma_malloc_int for callers
543 * outside of this module. Since we only support a single VMCI device, this
544 * wrapper provides access to the device softc structure.
548 * ENOMEM if insufficient memory.
549 * EINPROGRESS if mapping is deferred.
550 * EINVAL if the request was invalid.
553 * DMA memory is allocated.
555 *------------------------------------------------------------------------------
559 vmci_dma_malloc(bus_size_t size, bus_size_t align, struct vmci_dma_alloc *dma)
562 return (vmci_dma_malloc_int(vmci_sc, size, align, dma));
566 *------------------------------------------------------------------------------
568 * vmci_dma_free_int --
570 * Internal function that frees DMA memory.
578 *------------------------------------------------------------------------------
582 vmci_dma_free_int(struct vmci_softc *sc, struct vmci_dma_alloc *dma)
585 if (dma->dma_tag != NULL) {
586 if (dma->dma_paddr != 0) {
587 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
588 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
589 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
592 if (dma->dma_vaddr != NULL)
593 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr,
596 bus_dma_tag_destroy(dma->dma_tag);
598 bzero(dma, sizeof(struct vmci_dma_alloc));
602 *------------------------------------------------------------------------------
606 * This function is a wrapper around vmci_dma_free_int for callers outside
607 * of this module. Since we only support a single VMCI device, this wrapper
608 * provides access to the device softc structure.
616 *------------------------------------------------------------------------------
620 vmci_dma_free(struct vmci_dma_alloc *dma)
623 vmci_dma_free_int(vmci_sc, dma);
627 *------------------------------------------------------------------------------
629 * vmci_config_interrupts --
631 * Configures and enables interrupts. Try to configure MSI-X. If this fails,
632 * try to configure MSI. If even this fails, try legacy interrupts.
636 * ENOMEM if insufficient memory.
637 * ENODEV if the device doesn't support interrupts.
638 * ENXIO if the device configuration failed.
641 * Interrupts get enabled if successful.
643 *------------------------------------------------------------------------------
647 vmci_config_interrupts(struct vmci_softc *sc)
651 data_buffer = malloc(data_buffer_size, M_DEVBUF, M_ZERO | M_NOWAIT);
652 if (data_buffer == NULL)
655 sc->vmci_intr_type = VMCI_INTR_TYPE_MSIX;
656 error = vmci_config_interrupt(sc);
658 sc->vmci_intr_type = VMCI_INTR_TYPE_MSI;
659 error = vmci_config_interrupt(sc);
662 sc->vmci_intr_type = VMCI_INTR_TYPE_INTX;
663 error = vmci_config_interrupt(sc);
668 /* Enable specific interrupt bits. */
669 if (sc->capabilities & VMCI_CAPS_NOTIFICATIONS)
670 bus_space_write_4(sc->vmci_iot0, sc->vmci_ioh0,
671 VMCI_IMR_ADDR, VMCI_IMR_DATAGRAM | VMCI_IMR_NOTIFICATION);
673 bus_space_write_4(sc->vmci_iot0, sc->vmci_ioh0,
674 VMCI_IMR_ADDR, VMCI_IMR_DATAGRAM);
676 /* Enable interrupts. */
677 bus_space_write_4(sc->vmci_iot0, sc->vmci_ioh0,
678 VMCI_CONTROL_ADDR, VMCI_CONTROL_INT_ENABLE);
684 *------------------------------------------------------------------------------
686 * vmci_config_interrupt --
688 * Check the number of interrupts supported, allocate resources and setup
693 * ENOMEM if insufficient memory.
694 * ENODEV if the device doesn't support interrupts.
695 * ENXIO if the device configuration failed.
698 * Resources get allocated and interrupts get setup (but not enabled) if
701 *------------------------------------------------------------------------------
705 vmci_config_interrupt(struct vmci_softc *sc)
709 error = vmci_check_intr_cnt(sc);
713 error = vmci_allocate_interrupt_resources(sc);
717 error = vmci_setup_interrupts(sc);
725 *------------------------------------------------------------------------------
727 * vmci_check_intr_cnt --
729 * Check the number of interrupts supported by the device and ask PCI bus
730 * to allocate appropriate number of interrupts.
734 * ENODEV if the device doesn't support any interrupts.
735 * ENXIO if the device configuration failed.
738 * Resources get allocated on success.
740 *------------------------------------------------------------------------------
744 vmci_check_intr_cnt(struct vmci_softc *sc)
747 if (sc->vmci_intr_type == VMCI_INTR_TYPE_INTX) {
748 sc->vmci_num_intr = 1;
753 * Make sure that the device supports the required number of MSI/MSI-X
754 * messages. We try for 2 MSI-X messages but 1 is good too. We need at
755 * least 1 MSI message.
757 sc->vmci_num_intr = (sc->vmci_intr_type == VMCI_INTR_TYPE_MSIX) ?
758 pci_msix_count(sc->vmci_dev) : pci_msi_count(sc->vmci_dev);
760 if (!sc->vmci_num_intr) {
761 VMCI_LOG_ERROR(LGPFX"Device does not support any interrupt"
766 sc->vmci_num_intr = (sc->vmci_intr_type == VMCI_INTR_TYPE_MSIX) ?
768 if (sc->vmci_intr_type == VMCI_INTR_TYPE_MSIX) {
769 if (pci_alloc_msix(sc->vmci_dev, &sc->vmci_num_intr))
771 } else if (sc->vmci_intr_type == VMCI_INTR_TYPE_MSI) {
772 if (pci_alloc_msi(sc->vmci_dev, &sc->vmci_num_intr))
780 *------------------------------------------------------------------------------
782 * vmci_allocate_interrupt_resources --
784 * Allocate resources necessary for interrupts.
787 * 0 if success, ENXIO otherwise.
790 * Resources get allocated on success.
792 *------------------------------------------------------------------------------
796 vmci_allocate_interrupt_resources(struct vmci_softc *sc)
798 struct resource *irq;
802 flags |= (sc->vmci_num_intr == 1) ? RF_SHAREABLE : 0;
803 rid = (sc->vmci_intr_type == VMCI_INTR_TYPE_INTX) ? 0 : 1;
805 for (i = 0; i < sc->vmci_num_intr; i++, rid++) {
806 irq = bus_alloc_resource_any(sc->vmci_dev, SYS_RES_IRQ, &rid,
810 sc->vmci_intrs[i].vmci_irq = irq;
811 sc->vmci_intrs[i].vmci_rid = rid;
818 *------------------------------------------------------------------------------
820 * vmci_setup_interrupts --
822 * Sets up the interrupts.
825 * 0 if success, appropriate error code from bus_setup_intr otherwise.
828 * Interrupt handler gets attached.
830 *------------------------------------------------------------------------------
834 vmci_setup_interrupts(struct vmci_softc *sc)
836 struct vmci_interrupt *intr;
839 flags = INTR_TYPE_NET | INTR_MPSAFE;
840 if (sc->vmci_num_intr > 1)
843 intr = &sc->vmci_intrs[0];
844 error = bus_setup_intr(sc->vmci_dev, intr->vmci_irq, flags, NULL,
845 vmci_interrupt, NULL, &intr->vmci_handler);
849 if (sc->vmci_num_intr == 2) {
850 bus_describe_intr(sc->vmci_dev, intr->vmci_irq,
851 intr->vmci_handler, "dg");
852 intr = &sc->vmci_intrs[1];
853 error = bus_setup_intr(sc->vmci_dev, intr->vmci_irq, flags,
854 NULL, vmci_interrupt_bm, NULL, &intr->vmci_handler);
857 bus_describe_intr(sc->vmci_dev, intr->vmci_irq,
858 intr->vmci_handler, "bm");
865 *------------------------------------------------------------------------------
869 * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
870 * interrupt (vector VMCI_INTR_DATAGRAM).
878 *------------------------------------------------------------------------------
882 vmci_interrupt(void *arg)
885 if (vmci_sc->vmci_num_intr == 2)
886 taskqueue_enqueue(taskqueue_swi,
887 &vmci_sc->vmci_interrupt_dq_task);
891 icr = inl(vmci_sc->vmci_ioaddr + VMCI_ICR_ADDR);
892 if (icr == 0 || icr == 0xffffffff)
894 if (icr & VMCI_ICR_DATAGRAM) {
895 taskqueue_enqueue(taskqueue_swi,
896 &vmci_sc->vmci_interrupt_dq_task);
897 icr &= ~VMCI_ICR_DATAGRAM;
899 if (icr & VMCI_ICR_NOTIFICATION) {
900 taskqueue_enqueue(taskqueue_swi,
901 &vmci_sc->vmci_interrupt_bm_task);
902 icr &= ~VMCI_ICR_NOTIFICATION;
905 VMCI_LOG_INFO(LGPFX"Ignoring unknown interrupt "
911 *------------------------------------------------------------------------------
913 * vmci_interrupt_bm --
915 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
916 * which is for the notification bitmap. Will only get called if we are
917 * using MSI-X with exclusive vectors.
925 *------------------------------------------------------------------------------
929 vmci_interrupt_bm(void *arg)
932 ASSERT(vmci_sc->vmci_num_intr == 2);
933 taskqueue_enqueue(taskqueue_swi, &vmci_sc->vmci_interrupt_bm_task);
937 *------------------------------------------------------------------------------
939 * dispatch_datagrams --
941 * Reads and dispatches incoming datagrams.
947 * Reads data from the device.
949 *------------------------------------------------------------------------------
953 dispatch_datagrams(void *context, int data)
956 if (data_buffer == NULL)
957 VMCI_LOG_INFO(LGPFX"dispatch_datagrams(): no buffer "
960 vmci_read_datagrams_from_port((vmci_io_handle) 0,
961 vmci_sc->vmci_ioaddr + VMCI_DATA_IN_ADDR,
962 data_buffer, data_buffer_size);
966 *------------------------------------------------------------------------------
970 * Scans the notification bitmap for raised flags, clears them and handles
979 *------------------------------------------------------------------------------
983 process_bitmap(void *context, int data)
986 if (vmci_sc->vmci_notifications_bitmap.dma_vaddr == NULL)
987 VMCI_LOG_INFO(LGPFX"process_bitmaps(): no bitmap present");
989 vmci_scan_notification_bitmap(
990 vmci_sc->vmci_notifications_bitmap.dma_vaddr);
994 *------------------------------------------------------------------------------
996 * vmci_dismantle_interrupts --
998 * Releases resources, detaches the interrupt handler and drains the task
1005 * No more interrupts.
1007 *------------------------------------------------------------------------------
1011 vmci_dismantle_interrupts(struct vmci_softc *sc)
1013 struct vmci_interrupt *intr;
1016 for (i = 0; i < sc->vmci_num_intr; i++) {
1017 intr = &sc->vmci_intrs[i];
1018 if (intr->vmci_handler != NULL) {
1019 bus_teardown_intr(sc->vmci_dev, intr->vmci_irq,
1020 intr->vmci_handler);
1021 intr->vmci_handler = NULL;
1023 if (intr->vmci_irq != NULL) {
1024 bus_release_resource(sc->vmci_dev, SYS_RES_IRQ,
1025 intr->vmci_rid, intr->vmci_irq);
1026 intr->vmci_irq = NULL;
1027 intr->vmci_rid = -1;
1031 if ((sc->vmci_intr_type != VMCI_INTR_TYPE_INTX) &&
1032 (sc->vmci_num_intr))
1033 pci_release_msi(sc->vmci_dev);
1035 taskqueue_drain(taskqueue_swi, &sc->vmci_interrupt_dq_task);
1036 taskqueue_drain(taskqueue_swi, &sc->vmci_interrupt_bm_task);
1038 if (data_buffer != NULL)
1039 free(data_buffer, M_DEVBUF);
1043 *------------------------------------------------------------------------------
1045 * vmci_delayed_work_fn_cb --
1047 * Callback function that executes the queued up delayed work functions.
1055 *------------------------------------------------------------------------------
1059 vmci_delayed_work_fn_cb(void *context, int data)
1061 vmci_list(vmci_delayed_work_info) temp_list;
1063 vmci_list_init(&temp_list);
1066 * Swap vmci_delayed_work_infos list with the empty temp_list while
1067 * holding a lock. vmci_delayed_work_infos would then be an empty list
1068 * and temp_list would contain the elements from the original
1069 * vmci_delayed_work_infos. Finally, iterate through temp_list
1070 * executing the delayed callbacks.
1073 mtx_lock(&vmci_sc->vmci_delayed_work_lock);
1074 vmci_list_swap(&temp_list, &vmci_sc->vmci_delayed_work_infos,
1075 vmci_delayed_work_info, entry);
1076 mtx_unlock(&vmci_sc->vmci_delayed_work_lock);
1078 while (!vmci_list_empty(&temp_list)) {
1079 struct vmci_delayed_work_info *delayed_work_info =
1080 vmci_list_first(&temp_list);
1082 delayed_work_info->work_fn(delayed_work_info->data);
1084 vmci_list_remove(delayed_work_info, entry);
1085 vmci_free_kernel_mem(delayed_work_info,
1086 sizeof(*delayed_work_info));
1091 *------------------------------------------------------------------------------
1093 * vmci_schedule_delayed_work_fn --
1095 * Schedule the specified callback.
1098 * 0 if success, error code otherwise.
1103 *------------------------------------------------------------------------------
1107 vmci_schedule_delayed_work_fn(vmci_work_fn *work_fn, void *data)
1109 struct vmci_delayed_work_info *delayed_work_info;
1111 delayed_work_info = vmci_alloc_kernel_mem(sizeof(*delayed_work_info),
1112 VMCI_MEMORY_ATOMIC);
1114 if (!delayed_work_info)
1115 return (VMCI_ERROR_NO_MEM);
1117 delayed_work_info->work_fn = work_fn;
1118 delayed_work_info->data = data;
1119 mtx_lock(&vmci_sc->vmci_delayed_work_lock);
1120 vmci_list_insert(&vmci_sc->vmci_delayed_work_infos,
1121 delayed_work_info, entry);
1122 mtx_unlock(&vmci_sc->vmci_delayed_work_lock);
1124 taskqueue_enqueue(taskqueue_thread,
1125 &vmci_sc->vmci_delayed_work_task);
1127 return (VMCI_SUCCESS);
1131 *------------------------------------------------------------------------------
1133 * vmci_send_datagram --
1135 * VM to hypervisor call mechanism.
1138 * The result of the hypercall.
1143 *------------------------------------------------------------------------------
1147 vmci_send_datagram(struct vmci_datagram *dg)
1152 return (VMCI_ERROR_INVALID_ARGS);
1155 * Need to acquire spinlock on the device because
1156 * the datagram data may be spread over multiple pages and the monitor
1157 * may interleave device user rpc calls from multiple VCPUs. Acquiring
1158 * the spinlock precludes that possibility. Disabling interrupts to
1159 * avoid incoming datagrams during a "rep out" and possibly landing up
1162 mtx_lock_spin(&vmci_sc->vmci_spinlock);
1165 * Send the datagram and retrieve the return value from the result
1168 __asm__ __volatile__(
1172 : "d"(vmci_sc->vmci_ioaddr + VMCI_DATA_OUT_ADDR),
1173 "c"(VMCI_DG_SIZE(dg)), "S"(dg)
1177 * XXX: Should read result high port as well when updating handlers to
1181 result = bus_space_read_4(vmci_sc->vmci_iot0,
1182 vmci_sc->vmci_ioh0, VMCI_RESULT_LOW_ADDR);
1183 mtx_unlock_spin(&vmci_sc->vmci_spinlock);