xref: /haiku/src/add-ons/kernel/bus_managers/virtio/VirtioPrivate.h (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1 /*
2  * Copyright 2013, Jérôme Duval, korli@users.berlios.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef VIRTIO_PRIVATE_H
6 #define VIRTIO_PRIVATE_H
7 
8 
9 #include <new>
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include <lock.h>
14 #include <util/AutoLock.h>
15 #include <virtio.h>
16 
17 #include "virtio_ring.h"
18 
19 
20 //#define VIRTIO_TRACE
21 #ifdef VIRTIO_TRACE
22 #	define TRACE(x...)		dprintf("\33[33mvirtio:\33[0m " x)
23 #else
24 #	define TRACE(x...)
25 #endif
26 #define TRACE_ALWAYS(x...)	dprintf("\33[33mvirtio:\33[0m " x)
27 #define ERROR(x...)			TRACE_ALWAYS(x)
28 #define CALLED() 			TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
29 
30 
31 #define VIRTIO_SIM_MODULE_NAME		"bus_managers/virtio/sim/driver_v1"
32 
33 
34 class VirtioDevice;
35 class VirtioQueue;
36 
37 extern device_manager_info *gDeviceManager;
38 
39 
40 class VirtioDevice {
41 public:
42 								VirtioDevice(device_node *node);
43 								~VirtioDevice();
44 
45 			status_t			InitCheck();
46 			uint32				ID() const { return fID; }
47 
48 			status_t 			NegociateFeatures(uint32 supported,
49 									uint32* negociated,
50 									const char* (*get_feature_name)(uint32));
51 
52 			status_t			ReadDeviceConfig(uint8 offset, void* buffer,
53 									size_t bufferSize);
54 			status_t			WriteDeviceConfig(uint8 offset,
55 									const void* buffer, size_t bufferSize);
56 
57 			status_t			AllocateQueues(size_t count,
58 									virtio_queue *queues);
59 			status_t			SetupInterrupt(virtio_intr_func config_handler,
60 									void *driverCookie);
61 
62 			uint16				Alignment() const { return fAlignment; }
63 			uint32				Features() const { return fFeatures; }
64 
65 			void*				DriverCookie() { return fDriverCookie; }
66 
67 			status_t			SetupQueue(uint16 queueNumber,
68 									phys_addr_t physAddr);
69 			void				NotifyQueue(uint16 queueNumber);
70 
71 			status_t			QueueInterrupt(uint16 queueNumber);
72 			status_t			ConfigInterrupt();
73 
74 private:
75 			void				DumpFeatures(const char* title,
76 									uint32 features,
77 									const char* (*get_feature_name)(uint32));
78 
79 
80 			device_node *		fNode;
81 			uint32				fID;
82 			virtio_sim_interface *fController;
83 			void *				fCookie;
84 			status_t			fStatus;
85 			VirtioQueue**		fQueues;
86 			size_t				fQueueCount;
87 			uint32				fFeatures;
88 			uint16				fAlignment;
89 
90 			virtio_intr_func	fConfigHandler;
91 			void* 				fDriverCookie;
92 };
93 
94 
95 class TransferDescriptor;
96 
97 
98 class VirtioQueue {
99 public:
100 								VirtioQueue(VirtioDevice *device,
101 									uint16 queueNumber, uint16 ringSize);
102 								~VirtioQueue();
103 			status_t			InitCheck() { return fStatus; }
104 
105 			void				NotifyHost();
106 			status_t			Interrupt();
107 
108 			bool				IsFull() const { return fRingFree == 0; }
109 			bool				IsEmpty() const { return fRingFree == fRingSize; }
110 			uint16				Size() const { return fRingSize; }
111 
112 			VirtioDevice*		Device() { return fDevice; }
113 
114 			status_t			QueueRequest(const physical_entry* vector,
115 									size_t readVectorCount,
116 									size_t writtenVectorCount,
117 									virtio_callback_func callback,
118 									void *callbackCookie);
119 			status_t			QueueRequestIndirect(
120 									const physical_entry* vector,
121 									size_t readVectorCount,
122 									size_t writtenVectorCount,
123 									virtio_callback_func callback,
124 									void *callbackCookie);
125 			void				EnableInterrupt();
126 			void				DisableInterrupt();
127 
128 private:
129 			void				UpdateAvailable(uint16 index);
130 			uint16				QueueVector(uint16 insertIndex,
131 									struct vring_desc *desc,
132 									const physical_entry* vector,
133 									size_t readVectorCount,
134 									size_t writtenVectorCount);
135 			void				Finish();
136 
137 			VirtioDevice*		fDevice;
138 			uint16				fQueueNumber;
139 			uint16				fRingSize;
140 			uint16				fRingFree;
141 
142 			struct vring		fRing;
143 			uint16				fRingHeadIndex;
144 			uint16				fRingUsedIndex;
145 			status_t			fStatus;
146 			size_t 				fAreaSize;
147 			area_id				fArea;
148 
149 			uint16				fIndirectMaxSize;
150 
151 			TransferDescriptor**	fDescriptors;
152 };
153 
154 #endif // VIRTIO_PRIVATE_H
155