1/*-
2 * Copyright (c) 2018 VMware, Inc.
3 *
4 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
5 */
6
7/* VMCI Resource Access Control API. */
8
9#ifndef _VMCI_RESOURCE_H_
10#define _VMCI_RESOURCE_H_
11
12#include "vmci_defs.h"
13#include "vmci_hashtable.h"
14#include "vmci_kernel_if.h"
15
16#define RESOURCE_CONTAINER(ptr, type, member)				\
17	((type *)((char *)(ptr) - offsetof(type, member)))
18
19typedef void(*vmci_resource_free_cb)(void *resource);
20
21typedef enum {
22	VMCI_RESOURCE_TYPE_ANY,
23	VMCI_RESOURCE_TYPE_API,
24	VMCI_RESOURCE_TYPE_GROUP,
25	VMCI_RESOURCE_TYPE_DATAGRAM,
26	VMCI_RESOURCE_TYPE_DOORBELL,
27} vmci_resource_type;
28
29struct vmci_resource {
30	struct vmci_hash_entry	hash_entry;
31	vmci_resource_type	type;
32	/* Callback to free container object when refCount is 0. */
33	vmci_resource_free_cb	container_free_cb;
34	/* Container object reference. */
35	void			*container_object;
36};
37
38int	vmci_resource_init(void);
39void	vmci_resource_exit(void);
40void	vmci_resource_sync(void);
41
42vmci_id	vmci_resource_get_id(vmci_id context_id);
43
44int	vmci_resource_add(struct vmci_resource *resource,
45	    vmci_resource_type resource_type,
46	    struct vmci_handle resource_handle,
47	    vmci_resource_free_cb container_free_cb, void *container_object);
48void	vmci_resource_remove(struct vmci_handle resource_handle,
49	    vmci_resource_type resource_type);
50struct	vmci_resource *vmci_resource_get(struct vmci_handle resource_handle,
51	    vmci_resource_type resource_type);
52void	vmci_resource_hold(struct vmci_resource *resource);
53int	vmci_resource_release(struct vmci_resource *resource);
54struct	vmci_handle vmci_resource_handle(struct vmci_resource *resource);
55
56#endif /* !_VMCI_RESOURCE_H_ */
57