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