xref: /haiku/headers/private/virtio/virtio.h (revision a5c0d1a80e18f50987966fda2005210092d7671b)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _VIRTIO_H_
6 #define _VIRTIO_H_
7 
8 
9 #include <device_manager.h>
10 #include <KernelExport.h>
11 
12 
13 #define VIRTIO_DEVICE_ID_NETWORK	1
14 #define VIRTIO_DEVICE_ID_BLOCK		2
15 #define VIRTIO_DEVICE_ID_CONSOLE	3
16 #define VIRTIO_DEVICE_ID_ENTROPY	4
17 #define VIRTIO_DEVICE_ID_BALLOON	5
18 #define VIRTIO_DEVICE_ID_IOMEMORY	6
19 #define VIRTIO_DEVICE_ID_RP_MESSAGE	7
20 #define VIRTIO_DEVICE_ID_SCSI		8
21 #define VIRTIO_DEVICE_ID_9P			9
22 #define VIRTIO_DEVICE_ID_RP_SERIAL	11
23 #define VIRTIO_DEVICE_ID_CAIF		12
24 #define VIRTIO_DEVICE_ID_GPU		16
25 #define VIRTIO_DEVICE_ID_INPUT		18
26 #define VIRTIO_DEVICE_ID_VSOCK		19
27 #define VIRTIO_DEVICE_ID_CRYPTO		20
28 
29 #define VIRTIO_FEATURE_TRANSPORT_MASK	((1 << 28) - 1)
30 
31 #define VIRTIO_FEATURE_NOTIFY_ON_EMPTY		(1 << 24)
32 #define VIRTIO_FEATURE_ANY_LAYOUT			(1 << 27)
33 #define VIRTIO_FEATURE_RING_INDIRECT_DESC	(1 << 28)
34 #define VIRTIO_FEATURE_RING_EVENT_IDX		(1 << 29)
35 #define VIRTIO_FEATURE_BAD_FEATURE			(1 << 30)
36 
37 #define VIRTIO_VIRTQUEUES_MAX_COUNT	8
38 
39 #define VIRTIO_CONFIG_STATUS_RESET	0x00
40 #define VIRTIO_CONFIG_STATUS_ACK	0x01
41 #define VIRTIO_CONFIG_STATUS_DRIVER	0x02
42 #define VIRTIO_CONFIG_STATUS_DRIVER_OK	0x04
43 #define VIRTIO_CONFIG_STATUS_FAILED	0x80
44 
45 // attributes:
46 
47 // node type
48 #define VIRTIO_BUS_TYPE_NAME "bus/virtio/v1"
49 // device type (uint16)
50 #define VIRTIO_DEVICE_TYPE_ITEM "virtio/type"
51 // alignment (uint16)
52 #define VIRTIO_VRING_ALIGNMENT_ITEM "virtio/vring_alignment"
53 
54 // sim cookie, issued by virtio bus manager
55 typedef void* virtio_sim;
56 // device cookie, issued by virtio bus manager
57 typedef void* virtio_device;
58 // queue cookie, issued by virtio bus manager
59 typedef void* virtio_queue;
60 // callback function for requests
61 typedef void (*virtio_callback_func)(void* driverCookie, void* cookie);
62 // callback function for interrupts
63 typedef void (*virtio_intr_func)(void* cookie);
64 
65 #define	VIRTIO_DEVICE_MODULE_NAME "bus_managers/virtio/device/v1"
66 
67 typedef struct {
68 	driver_module_info info;
69 
70 	status_t	(*queue_interrupt_handler)(virtio_sim sim, uint16 queue);
71 	status_t	(*config_interrupt_handler)(virtio_sim sim);
72 } virtio_for_controller_interface;
73 
74 #define VIRTIO_FOR_CONTROLLER_MODULE_NAME "bus_managers/virtio/controller/driver_v1"
75 
76 // Bus manager interface used by Virtio controller drivers.
77 typedef struct {
78 	driver_module_info info;
79 
80 	void (*set_sim)(void* cookie, virtio_sim sim);
81 	status_t (*read_host_features)(void* cookie, uint32* features);
82 	status_t (*write_guest_features)(void* cookie, uint32 features);
83 	uint8 (*get_status)(void* cookie);
84 	void (*set_status)(void* cookie, uint8 status);
85 	status_t (*read_device_config)(void* cookie, uint8 offset, void* buffer,
86 		size_t bufferSize);
87 	status_t (*write_device_config)(void* cookie, uint8 offset,
88 		const void* buffer, size_t bufferSize);
89 
90 	uint16	(*get_queue_ring_size)(void* cookie, uint16 queue);
91 	status_t (*setup_queue)(void* cookie, uint16 queue, phys_addr_t phy);
92 	status_t (*setup_interrupt)(void* cookie, uint16 queueCount);
93 	status_t (*free_interrupt)(void* cookie);
94 	void	(*notify_queue)(void* cookie, uint16 queue);
95 } virtio_sim_interface;
96 
97 
98 // bus manager device interface for peripheral driver
99 typedef struct {
100 	driver_module_info info;
101 
102 	status_t (*negotiate_features)(virtio_device cookie, uint32 supported,
103 		uint32* negotiated, const char* (*get_feature_name)(uint32));
104 
105 	status_t (*clear_feature)(virtio_device cookie, uint32 feature);
106 
107 	status_t (*read_device_config)(virtio_device cookie, uint8 offset,
108 		void* buffer, size_t bufferSize);
109 	status_t (*write_device_config)(virtio_device cookie, uint8 offset,
110 		const void* buffer, size_t bufferSize);
111 
112 	status_t (*alloc_queues)(virtio_device cookie, size_t count,
113 		virtio_queue* queues);
114 
115 	void (*free_queues)(virtio_device cookie);
116 
117 	status_t (*setup_interrupt)(virtio_device cookie,
118 		virtio_intr_func config_handler, void* driverCookie);
119 
120 	status_t (*free_interrupts)(virtio_device cookie);
121 
122 	status_t (*queue_setup_interrupt)(virtio_queue queue,
123 		virtio_callback_func handler, void* cookie);
124 
125 	status_t (*queue_request)(virtio_queue queue,
126 		const physical_entry* readEntry,
127 		const physical_entry* writtenEntry, void* cookie);
128 
129 	status_t (*queue_request_v)(virtio_queue queue,
130 		const physical_entry* vector,
131 		size_t readVectorCount, size_t writtenVectorCount,
132 		void* cookie);
133 
134 	bool (*queue_is_full)(virtio_queue queue);
135 
136 	bool (*queue_is_empty)(virtio_queue queue);
137 
138 	uint16 (*queue_size)(virtio_queue queue);
139 
140 	bool (*queue_dequeue)(virtio_queue queue, void** _cookie,
141 		uint32* _usedLength);
142 
143 } virtio_device_interface;
144 
145 
146 #endif	/* _VIRTIO_H_ */
147