10#include <l4/cxx/unique_ptr>
15#include <l4/l4virtio/virtio.h>
16#include <l4/l4virtio/virtio_block.h>
17#include <l4/l4virtio/server/l4virtio>
18#include <l4/sys/cxx/ipc_epiface>
27template<
typename Ds_data>
43 Data_block() =
default;
71 rp.
start(_mem_list, _request, &data);
77 rp.
next(_mem_list, &data);
87 if (total < Header_size + 1)
90 return total - Header_size - 1;
99 while (_data.len == 0 && _rp.has_more())
100 _rp.next(_mem_list, &_data);
103 return (_data.len > 1 || _rp.has_more());
122 "No more data blocks in virtio request");
124 if (_todo_blocks == 0)
128 _rp.next(_mem_list, &_data);
131 if (_data.len > _max_block_size)
140 _data.addr =
static_cast<char *
>(_data.addr) + out.len;
155 : _mem_list(mem_list),
157 _todo_blocks(max_blocks),
158 _max_block_size(max_block_size)
161 _rp.
start(mem_list, _request, &_data);
164 if (_data.len < Header_size)
169 _data.addr =
static_cast<char *
>(_data.addr) + Header_size;
170 _data.len -= Header_size;
173 if (!_rp.
has_more() && _data.len == 0)
186 while (_rp.
next(_mem_list, &_data) && _todo_blocks > 0)
189 catch (Bad_descriptor
const &)
195 if (_todo_blocks > 0 && _data.len > 0)
196 *(
static_cast<l4_uint8_t *
>(_data.addr) + _data.len - 1) = status;
200 else if (_data.len > 0)
201 *(
static_cast<l4_uint8_t *
>(_data.addr)) = status;
206 queue->consumed(_request, sz);
216 Driver_mem_list_t<Ds_data> *_mem_list;
220 Request_processor _rp;
225 Virtqueue::Request _request;
227 unsigned _todo_blocks;
232struct Block_features :
public Dev_config::Features
234 Block_features() =
default;
238 CXX_BITFIELD_MEMBER( 1, 1, size_max,
raw);
240 CXX_BITFIELD_MEMBER( 2, 2, seg_max,
raw);
242 CXX_BITFIELD_MEMBER( 4, 4, geometry,
raw);
244 CXX_BITFIELD_MEMBER( 5, 5, ro,
raw);
246 CXX_BITFIELD_MEMBER( 6, 6, blk_size,
raw);
248 CXX_BITFIELD_MEMBER( 9, 9, flush,
raw);
250 CXX_BITFIELD_MEMBER(10, 10, topology,
raw);
252 CXX_BITFIELD_MEMBER(11, 11, config_wce,
raw);
254 CXX_BITFIELD_MEMBER(12, 12, mq,
raw);
256 CXX_BITFIELD_MEMBER(13, 13, discard,
raw);
258 CXX_BITFIELD_MEMBER(14, 14, write_zeroes,
raw);
267template <
typename Ds_data>
275 Dev_config_t<l4virtio_block_config_t> _dev_config;
281 Block_features negotiated_features()
const
282 {
return _dev_config.negotiated_features(0); }
284 Block_features device_features()
const
285 {
return _dev_config.host_features(0); }
287 void set_device_features(Block_features df)
288 { _dev_config.host_features(0) = df.raw; }
301 _dev_config.priv_config()->size_max = sz;
302 Block_features df = device_features();
303 df.size_max() =
true;
304 set_device_features(df);
306 _max_block_size = sz;
315 _dev_config.priv_config()->seg_max = sz;
316 Block_features df = device_features();
318 set_device_features(df);
327 pc->geometry.cylinders = cylinders;
328 pc->geometry.heads = heads;
329 pc->geometry.sectors = sectors;
330 Block_features df = device_features();
331 df.geometry() =
true;
332 set_device_features(df);
343 _dev_config.priv_config()->blk_size = sz;
344 Block_features df = device_features();
345 df.blk_size() =
true;
346 set_device_features(df);
363 pc->topology.physical_block_exp = physical_block_exp;
364 pc->topology.alignment_offset = alignment_offset;
365 pc->topology.min_io_size = min_io_size;
366 pc->topology.opt_io_size = opt_io_size;
367 Block_features df = device_features();
368 df.topology() =
true;
369 set_device_features(df);
375 Block_features df = device_features();
377 set_device_features(df);
387 pc->writeback = writeback;
388 Block_features df = device_features();
389 df.config_wce() =
true;
390 set_device_features(df);
400 return pc->writeback;
415 pc->max_discard_sectors = max_discard_sectors;
416 pc->max_discard_seg = max_discard_seg;
417 pc->discard_sector_alignment = discard_sector_alignment;
418 Block_features df = device_features();
420 set_device_features(df);
436 pc->max_write_zeroes_sectors = max_write_zeroes_sectors;
437 pc->max_write_zeroes_seg = max_write_zeroes_seg;
438 pc->write_zeroes_may_unmap = write_zeroes_may_unmap;
439 Block_features df = device_features();
440 df.write_zeroes() =
true;
441 set_device_features(df);
458 bool read_only, L4Re::Util::Dbg msg_user = L4Re::Util::Dbg{},
459 L4Re::Util::Dbg msg_dev = L4Re::Util::Dbg{2})
466 Block_features df(0);
467 df.ring_indirect_desc() =
true;
469 set_device_features(df);
473 _dev_config.priv_config()->capacity = capacity;
500 if (_dev_config.status().fail_state() || !_queue.ready())
503 if (req->release_request(&_queue, status, sz) < 0)
510 _kick_guest_irq->trigger();
517 if (idx == 0 && this->
setup_queue(&_queue, 0, _vq_max))
526 _dev_config.reset_queue(0, _vq_max);
527 _dev_config.reset_hdr();
532 bool check_for_new_requests()
546 cxx::unique_ptr<Request> req;
551 if (_dev_config.status().fail_state())
554 auto r = _queue.next_avail();
560 cxx::unique_ptr<Request> cur{
561 new Request(r, &(this->
_mem_info), _vq_max, _max_block_size)};
563 req = cxx::move(cur);
578 L4Re::chkcap(this->server_iface()->
template rcv_cap<L4::Irq>(0)));
586 _kick_guest_irq->trigger();
601template <
typename Ds_data>
604 L4::Epiface_t<Block_dev<Ds_data>, L4virtio::Device>
607 class Irq_object :
public L4::Irqep_t<Irq_object>
610 Irq_object(Block_dev<Ds_data> *parent) : _parent(parent) {}
618 Block_dev<Ds_data> *_parent;
620 Irq_object _irq_handler;
623 L4::Epiface *irq_iface()
624 {
return &_irq_handler; }
628 bool read_only, L4Re::Util::Dbg msg_user = L4Re::Util::Dbg{},
629 L4Re::Util::Dbg msg_dev = L4Re::Util::Dbg{2})
630 :
Block_dev_base<Ds_data>(vendor, queue_size, capacity, read_only,
645 L4::Cap<void> register_obj(L4::Registry_iface *registry,
646 char const *service = 0)
659 L4::Cap<void> register_obj(L4::Registry_iface *registry,
660 L4::Cap<L4::Rcv_endpoint> ep)
667 typedef Block_request<Ds_data> Request;
682 virtual bool process_request(cxx::unique_ptr<Request> &&req) = 0;
685 L4::Ipc_svr::Server_iface *
server_iface()
const override
697 if (!this->process_request(cxx::move(req)))
virtual L4::Cap< L4::Irq > register_irq_obj(L4::Epiface *o)=0
Register o as server-side object for asynchronous IRQs.
virtual L4::Cap< void > register_obj(L4::Epiface *o, char const *service)=0
Register an L4::Epiface for an IPC gate available in the applications environment under the name serv...
Exception for an abstract runtime error.
Base class for virtio block devices.
void set_write_zeroes(l4_uint32_t max_write_zeroes_sectors, l4_uint32_t max_write_zeroes_seg, l4_uint8_t write_zeroes_may_unmap)
Sets constraints for and enables the write zeroes command.
Block_dev_base(l4_uint32_t vendor, unsigned queue_size, l4_uint64_t capacity, bool read_only, L4Re::Util::Dbg msg_user=L4Re::Util::Dbg{}, L4Re::Util::Dbg msg_dev=L4Re::Util::Dbg{2})
Create a new virtio block device.
virtual bool queue_stopped()=0
Return true, if the queues should not be processed further.
l4_uint8_t get_writeback()
Get the writeback field from the configuration space.
cxx::unique_ptr< Request > get_request()
Return one request if available.
void set_blk_size(l4_uint32_t sz)
Sets block disk size to be reported to the client.
void set_config_wce(l4_uint8_t writeback)
Sets cache mode and enables the writeback toggle.
void set_flush()
Enables the flush command.
void set_size_max(l4_uint32_t sz)
Sets the maximum size of any single segment reported to client.
virtual void reset_device()=0
Reset the actual hardware device.
void set_discard(l4_uint32_t max_discard_sectors, l4_uint32_t max_discard_seg, l4_uint32_t discard_sector_alignment)
Sets constraints for and enables the discard command.
void reset() override
reset callback, called for doing a device reset
void set_geometry(l4_uint16_t cylinders, l4_uint8_t heads, l4_uint8_t sectors)
Set disk geometry that is reported to the client.
void set_seg_max(l4_uint32_t sz)
Sets the maximum number of segments in a request that is reported to client.
void set_topology(l4_uint8_t physical_block_exp, l4_uint8_t alignment_offset, l4_uint16_t min_io_size, l4_uint32_t opt_io_size)
Sets the I/O alignment information reported back to the client.
void finalize_request(cxx::unique_ptr< Request > req, unsigned sz, l4_uint8_t status=L4VIRTIO_BLOCK_S_OK)
Releases resources related to a request and notifies the client.
int reconfig_queue(unsigned idx) override
callback for client queue-config request
A request to read or write data.
unsigned data_size() const
Compute the total size of the data in the request.
l4virtio_block_header_t const & header() const
Return the block request header.
bool has_more()
Check if the request contains more data blocks.
Data_block next_block()
Return next block in scatter-gather list.
void add_irq_status(l4_uint32_t status)
Adds irq status bit.
Status status() const
Get current device status (trusted).
Server-side L4-VIRTIO device stub.
virtual void trigger_driver_config_irq()=0
Device_t(Dev_config *dev_config, L4Re::Util::Dbg msg_user=L4Re::Util::Dbg{}, L4Re::Util::Dbg msg_dev=L4Re::Util::Dbg{2})
virtual void register_single_driver_irq()
bool setup_queue(Virtqueue *q, unsigned qn, unsigned num_max)
void reset_queue_config(unsigned idx, unsigned num_max, bool inc_generation=false, unsigned device_notify_index=0)
virtual L4::Cap< L4::Irq > device_notify_irq() const
virtual bool check_queues()=0
List of driver memory regions assigned to a single L4-VIRTIO transport instance.
Region of driver memory, that shall be managed locally.
T * local(Ptr< T > p) const
Get the local address for driver address p.
Encapsulate the state for processing a VIRTIO request.
bool next(DESC_MAN *dm, ARGS... args)
Switch to the next descriptor in a descriptor chain.
bool has_more() const
Are there more chained descriptors?
void start(DESC_MAN *dm, Virtqueue *ring, Virtqueue::Head_desc const &request, ARGS... args)
Start processing a new request.
Virtqueue implementation for the device.
bool desc_avail() const
Test for available descriptors.
Descriptor in the descriptor table.
l4_uint32_t len
Length of described buffer.
Ptr< void > addr
Address stored in descriptor.
bool no_notify_guest() const
Get the no IRQ flag of this queue.
bool ready() const
Test if this queue is in working state.
unsigned char l4_uint8_t
Unsigned 8bit value.
unsigned int l4_uint32_t
Unsigned 32bit value.
unsigned short int l4_uint16_t
Unsigned 16bit value.
unsigned long long l4_uint64_t
Unsigned 64bit value.
@ L4_EEXIST
Already exists.
@ L4_EINVAL
Invalid argument.
struct l4virtio_block_header_t l4virtio_block_header_t
Header structure of a request for a block device.
@ L4VIRTIO_BLOCK_S_OK
Request finished successfully.
@ L4VIRTIO_FEATURE_VERSION_1
Virtio protocol version 1 supported. Must be 1 for L4virtio.
@ L4VIRTIO_ID_BLOCK
General block device.
@ L4VIRTIO_IRQ_STATUS_VRING
VRING IRQ pending flag.
@ L4VIRTIO_IRQ_STATUS_CONFIG
CONFIG IRQ pending flag.
L4::Detail::Unique_cap_impl< T, L4Re::Util::Smart_cap_auto< L4_FP_ALL_SPACES > > Unique_cap
Unique capability that implements automatic free and unmap of the capability slot.
T chkcap(T &&cap, char const *extra="", l4_ret_t err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
l4_ret_t chksys(l4_ret_t err, char const *extra="", l4_ret_t ret=0)
Generate C++ exception on error.
Cap< T > cap_cast(Cap< F > const &c) noexcept
static_cast for capabilities.
L4-VIRTIO Transport C++ API.
Server_iface * server_iface() const
Get pointer to server interface at which the object is currently registered.
Exception used by Queue to indicate descriptor errors.
@ Bad_size
Invalid size of memory block.
l4_uint32_t raw
The raw value of the features bitmap.
constexpr fail_state_bfm_t::Val fail_state() const
Get the fail_state bits (6 to 7) of raw.
Device configuration for block devices.
Unique_cap / Unique_del_cap.