1/*-
2 * Copyright (c) 2018 VMware, Inc.
3 *
4 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
5 *
6 * $FreeBSD$
7 */
8
9/* This file defines helper functions */
10
11#ifndef	_VMCI_KERNEL_IF_H_
12#define	_VMCI_KERNEL_IF_H_
13
14#include <sys/param.h>
15#include <sys/lock.h>
16#include <sys/mutex.h>
17#include <sys/queue.h>
18#include <sys/sema.h>
19
20#include "vmci_defs.h"
21
22#define VMCI_MEMORY_NORMAL		0x0
23#define VMCI_MEMORY_ATOMIC		0x1
24
25#define vmci_list(_l)			LIST_HEAD(, _l)
26#define vmci_list_item(_l)		LIST_ENTRY(_l)
27#define vmci_list_init(_l)		LIST_INIT(_l)
28#define vmci_list_empty(_l)		LIST_EMPTY(_l)
29#define vmci_list_first(_l)		LIST_FIRST(_l)
30#define vmci_list_next(e, f)		LIST_NEXT(e, f)
31#define vmci_list_insert(_l, _e, n)	LIST_INSERT_HEAD(_l, _e, n)
32#define vmci_list_remove(_e, n)		LIST_REMOVE(_e, n)
33#define vmci_list_scan(v, _l, n)	LIST_FOREACH(v, _l, n)
34#define vmci_list_scan_safe(_e, _l, n, t)				\
35	LIST_FOREACH_SAFE(_e, _l, n, t)
36#define vmci_list_swap(_l1, _l2, t, f)	LIST_SWAP(_l1, _l2, t, f)
37
38typedef unsigned short int vmci_io_port;
39typedef int vmci_io_handle;
40
41void	vmci_read_port_bytes(vmci_io_handle handle, vmci_io_port port,
42	    uint8_t *buffer, size_t buffer_length);
43
44typedef struct mtx vmci_lock;
45int	vmci_init_lock(vmci_lock *lock, char *name);
46void	vmci_cleanup_lock(vmci_lock *lock);
47void	vmci_grab_lock(vmci_lock *lock);
48void	vmci_release_lock(vmci_lock *lock);
49void	vmci_grab_lock_bh(vmci_lock *lock);
50void	vmci_release_lock_bh(vmci_lock *lock);
51
52void	*vmci_alloc_kernel_mem(size_t size, int flags);
53void	vmci_free_kernel_mem(void *ptr, size_t size);
54
55typedef struct sema vmci_event;
56typedef int (*vmci_event_release_cb)(void *client_data);
57void	vmci_create_event(vmci_event *event);
58void	vmci_destroy_event(vmci_event *event);
59void	vmci_signal_event(vmci_event *event);
60void	vmci_wait_on_event(vmci_event *event, vmci_event_release_cb release_cb,
61	    void *client_data);
62bool	vmci_wait_on_event_interruptible(vmci_event *event,
63	    vmci_event_release_cb release_cb, void *client_data);
64
65typedef void (vmci_work_fn)(void *data);
66bool	vmci_can_schedule_delayed_work(void);
67int	vmci_schedule_delayed_work(vmci_work_fn *work_fn, void *data);
68void	vmci_delayed_work_cb(void *context, int data);
69
70typedef struct mtx vmci_mutex;
71int	vmci_mutex_init(vmci_mutex *mutex, char *name);
72void	vmci_mutex_destroy(vmci_mutex *mutex);
73void	vmci_mutex_acquire(vmci_mutex *mutex);
74void	vmci_mutex_release(vmci_mutex *mutex);
75
76void	*vmci_alloc_queue(uint64_t size, uint32_t flags);
77void	vmci_free_queue(void *q, uint64_t size);
78
79typedef PPN *vmci_ppn_list;
80struct ppn_set {
81	uint64_t	num_produce_pages;
82	uint64_t	num_consume_pages;
83	vmci_ppn_list	produce_ppns;
84	vmci_ppn_list	consume_ppns;
85	bool		initialized;
86};
87
88int	vmci_alloc_ppn_set(void *produce_q, uint64_t num_produce_pages,
89	    void *consume_q, uint64_t num_consume_pages,
90	    struct ppn_set *ppn_set);
91void	vmci_free_ppn_set(struct ppn_set *ppn_set);
92int	vmci_populate_ppn_list(uint8_t *call_buf, const struct ppn_set *ppnset);
93
94#endif /* !_VMCI_KERNEL_IF_H_ */
95