/
component.h
307 lines (259 loc) · 8.52 KB
/
component.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* \brief Block-session component
* \author Christian Helmuth
* \author Stefan Kalkowski
* \date 2011-05-20
*/
/*
* Copyright (C) 2011-2013 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _INCLUDE__BLOCK__COMPONENT_H_
#define _INCLUDE__BLOCK__COMPONENT_H_
#include <root/component.h>
#include <os/signal_rpc_dispatcher.h>
#include <os/server.h>
#include <block/driver.h>
namespace Block {
using namespace Genode;
class Session_component_base;
class Session_component;
class Root;
};
/**
* We have a hen and egg situation that makes this base class necessary.
* The Block::Session_rpc_object construction depends on a dataspace for
* the packet stream. The dataspace on the other hand is constructed by
* the driver, which is created on demand when creating a session.
* When creating the driver, and dataspace outside the Session_component
* constructor within _create_session of the root component, we would have
* to destroy the driver and dataspace within the destructor body of
* Session_component, which will lead to problems, because the packet stream
* destructors will be called after the shared memory already vanished.
*/
class Block::Session_component_base
{
protected:
Driver_factory &_driver_factory;
Driver &_driver;
Ram_dataspace_capability _rq_ds;
Session_component_base(Driver_factory &factory, size_t tx_buf_size)
: _driver_factory(factory),
_driver(*factory.create()),
_rq_ds(_driver.alloc_dma_buffer(tx_buf_size)) {}
~Session_component_base()
{
_driver.free_dma_buffer(_rq_ds);
_driver_factory.destroy(&_driver);
}
};
class Block::Session_component : public Block::Session_component_base,
public Block::Driver_session
{
private:
addr_t _rq_phys;
Signal_rpc_member<Session_component> _sink_ack;
Signal_rpc_member<Session_component> _sink_submit;
bool _req_queue_full;
bool _ack_queue_full;
Packet_descriptor _p_to_handle;
unsigned _p_in_fly;
/**
* Acknowledge a packet already handled
*/
inline void _ack_packet(Packet_descriptor &packet)
{
if (!tx_sink()->ready_to_ack())
PERR("Not ready to ack!");
tx_sink()->acknowledge_packet(packet);
_p_in_fly--;
}
/**
* Range check packet request
*/
inline bool _range_check(Packet_descriptor &p) {
return p.block_number() + p.block_count() - 1
< _driver.block_count(); }
/**
* Handle a single request
*/
void _handle_packet(Packet_descriptor packet)
{
_p_to_handle = packet;
_p_to_handle.succeeded(false);
/* ignore invalid packets */
if (!packet.valid() || !_range_check(_p_to_handle)) {
_ack_packet(_p_to_handle);
return;
}
try {
switch (_p_to_handle.operation()) {
case Block::Packet_descriptor::READ:
if (_driver.dma_enabled())
_driver.read_dma(packet.block_number(),
packet.block_count(),
_rq_phys + packet.offset(),
_p_to_handle);
else
_driver.read(packet.block_number(),
packet.block_count(),
tx_sink()->packet_content(packet),
_p_to_handle);
break;
case Block::Packet_descriptor::WRITE:
if (_driver.dma_enabled())
_driver.write_dma(packet.block_number(),
packet.block_count(),
_rq_phys + packet.offset(),
_p_to_handle);
else
_driver.write(packet.block_number(),
packet.block_count(),
tx_sink()->packet_content(packet),
_p_to_handle);
break;
default:
throw Driver::Io_error();
}
} catch (Driver::Request_congestion) {
_req_queue_full = true;
} catch (Driver::Io_error) {
_ack_packet(_p_to_handle);
}
}
/**
* Triggered when a packet was placed into the empty submit queue
*/
void _packet_avail(unsigned)
{
/*
* as long as more packets are available, and we're able to ack
* them, and the driver's request queue isn't full,
* direct the packet request to the driver backend
*/
for (_ack_queue_full = (_p_in_fly >= tx_sink()->ack_slots_free());
!_req_queue_full && !_ack_queue_full
&& tx_sink()->packet_avail();
_ack_queue_full = (++_p_in_fly >= tx_sink()->ack_slots_free()))
_handle_packet(tx_sink()->get_packet());
}
/**
* Triggered when an ack got removed from the full ack queue
*/
void _ready_to_ack(unsigned) { _packet_avail(0); }
public:
/**
* Constructor
*
* \param rq_ds shared dataspace for packet stream
* \param driver block driver backend
* \param driver_factory factory to create and destroy driver objects
* \param ep entrypoint handling this session component
*/
Session_component(Driver_factory &driver_factory,
Server::Entrypoint &ep, size_t buf_size)
: Session_component_base(driver_factory, buf_size),
Driver_session(_rq_ds, ep.rpc_ep()),
_rq_phys(Dataspace_client(_rq_ds).phys_addr()),
_sink_ack(ep, *this, &Session_component::_ready_to_ack),
_sink_submit(ep, *this, &Session_component::_packet_avail),
_req_queue_full(false),
_p_in_fly(0)
{
_tx.sigh_ready_to_ack(_sink_ack);
_tx.sigh_packet_avail(_sink_submit);
_driver.session(this);
}
~Session_component() { _driver.session(nullptr); }
/**
* Acknowledges a packet processed by the driver to the client
*
* \param packet the packet to acknowledge
* \param success indicated whether the processing was successful
*
* \throw Ack_congestion
*/
void ack_packet(Packet_descriptor &packet, bool success)
{
packet.succeeded(success);
_ack_packet(packet);
if (!_req_queue_full && !_ack_queue_full)
return;
/*
* when the driver's request queue was full,
* handle last unprocessed packet taken out of submit queue
*/
if (_req_queue_full) {
_req_queue_full = false;
_handle_packet(_p_to_handle);
}
/* resume packet processing */
_packet_avail(0);
}
/*******************************
** Block session interface **
*******************************/
void info(sector_t *blk_count, size_t *blk_size,
Operations *ops)
{
*blk_count = _driver.block_count();
*blk_size = _driver.block_size();
*ops = _driver.ops();
}
void sync() { _driver.sync(); }
};
/**
* Root component, handling new session requests
*/
class Block::Root : public Genode::Root_component<Block::Session_component,
Single_client>
{
private:
Driver_factory &_driver_factory;
Server::Entrypoint &_ep;
protected:
/**
* Always returns the singleton block-session component
*/
Session_component *_create_session(const char *args)
{
size_t ram_quota =
Arg_string::find_arg(args, "ram_quota" ).ulong_value(0);
size_t tx_buf_size =
Arg_string::find_arg(args, "tx_buf_size").ulong_value(0);
/* delete ram quota by the memory needed for the session */
size_t session_size = max((size_t)4096,
sizeof(Session_component)
+ sizeof(Allocator_avl));
if (ram_quota < session_size)
throw Root::Quota_exceeded();
/*
* Check if donated ram quota suffices for both
* communication buffers. Also check both sizes separately
* to handle a possible overflow of the sum of both sizes.
*/
if (tx_buf_size > ram_quota - session_size) {
PERR("insufficient 'ram_quota', got %zd, need %zd",
ram_quota, tx_buf_size + session_size);
throw Root::Quota_exceeded();
}
return new (md_alloc()) Session_component(_driver_factory,
_ep, tx_buf_size);
}
public:
/**
* Constructor
*
* \param ep entrypoint handling this root component
* \param md_alloc allocator to allocate session components
* \param driver_factory factory to create and destroy driver backend
* \param receiver signal receiver managing signals of the client
*/
Root(Server::Entrypoint &ep, Allocator *md_alloc,
Driver_factory &driver_factory)
: Root_component(&ep.rpc_ep(), md_alloc),
_driver_factory(driver_factory), _ep(ep) { }
};
#endif /* _INCLUDE__BLOCK__COMPONENT_H_ */