2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Block-session component
|
|
|
|
* \author Christian Helmuth
|
2013-11-22 20:55:28 +00:00
|
|
|
* \author Stefan Kalkowski
|
2011-12-22 15:19:25 +00:00
|
|
|
* \date 2011-05-20
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-01-16 14:33:56 +00:00
|
|
|
* Copyright (C) 2011-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__BLOCK__COMPONENT_H_
|
|
|
|
#define _INCLUDE__BLOCK__COMPONENT_H_
|
|
|
|
|
2016-08-12 13:10:50 +00:00
|
|
|
#include <base/log.h>
|
|
|
|
#include <base/component.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
#include <base/allocator_avl.h>
|
|
|
|
#include <base/heap.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
#include <root/component.h>
|
|
|
|
#include <block/driver.h>
|
|
|
|
|
|
|
|
namespace Block {
|
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
2014-01-24 10:54:51 +00:00
|
|
|
class Session_component_base;
|
2013-11-22 20:55:28 +00:00
|
|
|
class Session_component;
|
|
|
|
class Root;
|
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-11-22 12:55:05 +00:00
|
|
|
|
2014-01-24 10:54:51 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
2014-02-06 14:23:17 +00:00
|
|
|
public Block::Driver_session
|
2013-11-22 20:55:28 +00:00
|
|
|
{
|
|
|
|
private:
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-08-12 13:10:50 +00:00
|
|
|
addr_t _rq_phys;
|
|
|
|
Signal_handler<Session_component> _sink_ack;
|
|
|
|
Signal_handler<Session_component> _sink_submit;
|
|
|
|
bool _req_queue_full;
|
|
|
|
bool _ack_queue_full;
|
|
|
|
Packet_descriptor _p_to_handle;
|
|
|
|
unsigned _p_in_fly;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-12-09 11:02:54 +00:00
|
|
|
/**
|
|
|
|
* Acknowledge a packet already handled
|
|
|
|
*/
|
|
|
|
inline void _ack_packet(Packet_descriptor &packet)
|
2013-11-22 20:55:28 +00:00
|
|
|
{
|
2013-12-09 11:02:54 +00:00
|
|
|
if (!tx_sink()->ready_to_ack())
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
error("not ready to ack!");
|
2013-12-09 11:02:54 +00:00
|
|
|
|
|
|
|
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 */
|
2016-05-31 16:53:57 +00:00
|
|
|
if (!packet.size() || !_range_check(_p_to_handle)) {
|
2013-12-09 11:02:54 +00:00
|
|
|
_ack_packet(_p_to_handle);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-12-09 11:02:54 +00:00
|
|
|
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();
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
2013-12-09 11:02:54 +00:00
|
|
|
} catch (Driver::Request_congestion) {
|
|
|
|
_req_queue_full = true;
|
|
|
|
} catch (Driver::Io_error) {
|
|
|
|
_ack_packet(_p_to_handle);
|
2013-11-22 20:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 11:02:54 +00:00
|
|
|
/**
|
2016-08-12 13:10:50 +00:00
|
|
|
* Called whenever a signal from the packet-stream interface triggered
|
2013-12-09 11:02:54 +00:00
|
|
|
*/
|
2016-08-12 13:10:50 +00:00
|
|
|
void _signal()
|
2013-12-09 11:02:54 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2014-01-08 14:05:01 +00:00
|
|
|
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());
|
2013-12-09 11:02:54 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 20:55:28 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2013-12-09 11:02:54 +00:00
|
|
|
*
|
|
|
|
* \param driver_factory factory to create and destroy driver objects
|
|
|
|
* \param ep entrypoint handling this session component
|
2016-08-12 13:10:50 +00:00
|
|
|
* \param buf_size size of packet-stream payload buffer
|
2013-11-22 20:55:28 +00:00
|
|
|
*/
|
2016-08-12 13:10:50 +00:00
|
|
|
Session_component(Driver_factory &driver_factory,
|
|
|
|
Genode::Entrypoint &ep,
|
2017-01-16 14:33:56 +00:00
|
|
|
Genode::Region_map &rm,
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
size_t buf_size)
|
2014-01-24 10:54:51 +00:00
|
|
|
: Session_component_base(driver_factory, buf_size),
|
2017-01-16 14:33:56 +00:00
|
|
|
Driver_session(rm, _rq_ds, ep.rpc_ep()),
|
2013-12-09 11:02:54 +00:00
|
|
|
_rq_phys(Dataspace_client(_rq_ds).phys_addr()),
|
2016-08-12 13:10:50 +00:00
|
|
|
_sink_ack(ep, *this, &Session_component::_signal),
|
|
|
|
_sink_submit(ep, *this, &Session_component::_signal),
|
2013-12-09 11:02:54 +00:00
|
|
|
_req_queue_full(false),
|
|
|
|
_p_in_fly(0)
|
2013-11-22 20:55:28 +00:00
|
|
|
{
|
|
|
|
_tx.sigh_ready_to_ack(_sink_ack);
|
|
|
|
_tx.sigh_packet_avail(_sink_submit);
|
2013-12-03 14:41:14 +00:00
|
|
|
|
2014-02-06 14:23:17 +00:00
|
|
|
_driver.session(this);
|
2013-11-22 20:55:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:23:17 +00:00
|
|
|
~Session_component() { _driver.session(nullptr); }
|
|
|
|
|
2013-11-22 20:55:28 +00:00
|
|
|
/**
|
2013-12-09 11:02:54 +00:00
|
|
|
* 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
|
2013-11-22 20:55:28 +00:00
|
|
|
*/
|
2014-02-06 14:23:17 +00:00
|
|
|
void ack_packet(Packet_descriptor &packet, bool success)
|
2013-11-22 20:55:28 +00:00
|
|
|
{
|
2013-12-09 11:02:54 +00:00
|
|
|
packet.succeeded(success);
|
|
|
|
_ack_packet(packet);
|
|
|
|
|
2014-01-08 14:05:01 +00:00
|
|
|
if (!_req_queue_full && !_ack_queue_full)
|
2013-12-09 11:02:54 +00:00
|
|
|
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 */
|
2016-08-12 13:10:50 +00:00
|
|
|
_signal();
|
2013-11-22 20:55:28 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 13:34:53 +00:00
|
|
|
|
|
|
|
/*******************************
|
|
|
|
** Block session interface **
|
|
|
|
*******************************/
|
|
|
|
|
|
|
|
void info(sector_t *blk_count, size_t *blk_size,
|
2013-11-22 20:55:28 +00:00
|
|
|
Operations *ops)
|
|
|
|
{
|
|
|
|
*blk_count = _driver.block_count();
|
|
|
|
*blk_size = _driver.block_size();
|
2013-11-22 23:24:44 +00:00
|
|
|
*ops = _driver.ops();
|
2013-11-22 20:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sync() { _driver.sync(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Root component, handling new session requests
|
|
|
|
*/
|
2014-01-13 12:44:33 +00:00
|
|
|
class Block::Root : public Genode::Root_component<Block::Session_component,
|
|
|
|
Single_client>
|
2013-11-22 20:55:28 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2014-01-13 12:44:33 +00:00
|
|
|
Driver_factory &_driver_factory;
|
2016-08-12 13:10:50 +00:00
|
|
|
Genode::Entrypoint &_ep;
|
2017-01-16 14:33:56 +00:00
|
|
|
Genode::Region_map &_rm;
|
2013-11-22 20:55:28 +00:00
|
|
|
|
|
|
|
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)
|
2017-05-08 12:32:03 +00:00
|
|
|
throw Insufficient_ram_quota();
|
2013-11-22 20:55:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
2016-08-12 13:10:50 +00:00
|
|
|
error("insufficient 'ram_quota', got ", ram_quota, ", need ",
|
|
|
|
tx_buf_size + session_size);
|
2017-05-08 12:32:03 +00:00
|
|
|
throw Insufficient_ram_quota();
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 10:54:51 +00:00
|
|
|
return new (md_alloc()) Session_component(_driver_factory,
|
2017-01-16 14:33:56 +00:00
|
|
|
_ep, _rm, tx_buf_size);
|
2013-11-22 20:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2013-12-09 11:02:54 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2014-01-13 12:44:33 +00:00
|
|
|
* \param ep entrypoint handling this root component
|
2013-12-09 11:02:54 +00:00
|
|
|
* \param md_alloc allocator to allocate session components
|
2017-01-16 14:33:56 +00:00
|
|
|
* \param rm region map
|
2013-12-09 11:02:54 +00:00
|
|
|
* \param driver_factory factory to create and destroy driver backend
|
|
|
|
*/
|
2016-08-12 13:10:50 +00:00
|
|
|
Root(Genode::Entrypoint &ep,
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
Allocator &md_alloc,
|
2017-01-16 14:33:56 +00:00
|
|
|
Genode::Region_map &rm,
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
Driver_factory &driver_factory)
|
|
|
|
:
|
|
|
|
Root_component(ep, md_alloc),
|
2017-01-16 14:33:56 +00:00
|
|
|
_driver_factory(driver_factory), _ep(ep), _rm(rm)
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
{ }
|
2013-11-22 20:55:28 +00:00
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
#endif /* _INCLUDE__BLOCK__COMPONENT_H_ */
|