11 #include <l4/cxx/unique_ptr> 16 #include <l4/l4virtio/virtio.h> 17 #include <l4/l4virtio/virtio_block.h> 18 #include <l4/l4virtio/server/l4virtio> 19 #include <l4/sys/cxx/ipc_epiface> 28 template<
typename Ds_data>
44 Data_block() =
default;
69 rp.
start(_mem_list, _request, &data);
71 unsigned total = data.len;
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)
184 while (_rp.next(_mem_list, &_data) && _todo_blocks > 0)
187 if (_todo_blocks > 0 && _data.len > 0)
188 *(
static_cast<l4_uint8_t *
>(_data.addr) + _data.len - 1) = status;
192 else if (_data.len > 0)
193 *(
static_cast<l4_uint8_t *
>(_data.addr)) = status;
217 Virtqueue::Request _request;
219 unsigned _todo_blocks;
226 Block_features() =
default;
230 CXX_BITFIELD_MEMBER( 1, 1, size_max, raw);
232 CXX_BITFIELD_MEMBER( 2, 2, seg_max, raw);
234 CXX_BITFIELD_MEMBER( 4, 4, geometry, raw);
236 CXX_BITFIELD_MEMBER( 5, 5, ro, raw);
238 CXX_BITFIELD_MEMBER( 6, 6, blk_size, raw);
240 CXX_BITFIELD_MEMBER( 9, 9, flush, raw);
242 CXX_BITFIELD_MEMBER(10, 10, topology, raw);
244 CXX_BITFIELD_MEMBER(11, 11, config_wce, raw);
246 CXX_BITFIELD_MEMBER(13, 13, discard, raw);
248 CXX_BITFIELD_MEMBER(14, 14, write_zeroes, raw);
257 template <
typename Ds_data>
260 public L4::Epiface_t<Block_dev<Ds_data>, L4virtio::Device>
263 class Irq_object :
public L4::Irqep_t<Irq_object>
276 Irq_object _irq_handler;
282 Dev_config_t<l4virtio_block_config_t> _dev_config;
288 Block_features negotiated_features()
const 289 {
return _dev_config.negotiated_features(0); }
291 Block_features device_features()
const 292 {
return _dev_config.host_features(0); }
294 void set_device_features(Block_features df)
295 { _dev_config.host_features(0) = df.raw; }
308 _dev_config.priv_config()->size_max = sz;
309 Block_features df = device_features();
310 df.size_max() =
true;
311 set_device_features(df);
313 _max_block_size = sz;
322 _dev_config.priv_config()->seg_max = sz;
323 Block_features df = device_features();
325 set_device_features(df);
334 pc->geometry.cylinders = cylinders;
335 pc->geometry.heads = heads;
336 pc->geometry.sectors = sectors;
337 Block_features df = device_features();
338 df.geometry() =
true;
339 set_device_features(df);
350 _dev_config.priv_config()->blk_size = sz;
351 Block_features df = device_features();
352 df.blk_size() =
true;
353 set_device_features(df);
370 pc->topology.physical_block_exp = physical_block_exp;
371 pc->topology.alignment_offset = alignment_offset;
372 pc->topology.min_io_size = min_io_size;
373 pc->topology.opt_io_size = opt_io_size;
374 Block_features df = device_features();
375 df.topology() =
true;
376 set_device_features(df);
382 Block_features df = device_features();
384 set_device_features(df);
394 pc->writeback = writeback;
395 Block_features df = device_features();
396 df.config_wce() =
true;
397 set_device_features(df);
407 return pc->writeback;
422 pc->max_discard_sectors = max_discard_sectors;
423 pc->max_discard_seg = max_discard_seg;
424 pc->discard_sector_alignment = discard_sector_alignment;
425 Block_features df = device_features();
427 set_device_features(df);
443 pc->max_write_zeroes_sectors = max_write_zeroes_sectors;
444 pc->max_write_zeroes_seg = max_write_zeroes_seg;
445 pc->write_zeroes_may_unmap = write_zeroes_may_unmap;
446 Block_features df = device_features();
447 df.write_zeroes() =
true;
448 set_device_features(df);
463 _irq_handler(this), _vq_max(queue_size),
466 this->reset_queue_config(0, queue_size);
468 Block_features df(0);
469 df.ring_indirect_desc() =
true;
471 set_device_features(df);
473 _dev_config.priv_config()->capacity = capacity;
491 virtual bool process_request(cxx::unique_ptr<Request> &&req) = 0;
496 virtual void reset_device() = 0;
510 virtual bool queue_stopped() = 0;
526 if (_dev_config.status().failed())
529 if (req->release_request(&_queue, status, sz) < 0)
530 this->device_error();
534 _kick_guest_irq->trigger();
541 if (idx == 0 && this->setup_queue(&_queue, 0, _vq_max))
558 char const *service = 0)
582 return L4::Epiface::server_iface();
587 if (!_queue.
ready() || queue_stopped())
590 while (!_dev_config.status().failed())
598 cxx::unique_ptr<Request>
599 cur{
new Request(r, &(this->_mem_info), _vq_max, _max_block_size)};
601 if (!process_request(cxx::move(cur)))
606 this->device_error();
618 void register_single_driver_irq()
629 L4Re::chkcap(server_iface()->
template rcv_cap<L4::Irq>(0)));
637 _dev_config.reset_queue(0, _vq_max);
638 _dev_config.reset_hdr();
Abstract interface for object registries.
void set_config_wce(l4_uint8_t writeback)
Sets cache mode and enables the the writeback toggle.
bool has_more() const
Are there more chained descriptors ?
Descriptor in the descriptor table.
L4::Cap< void > register_obj(L4::Registry_iface *registry, char const *service=0)
Attach device to an object registry.
Virtqueue implementation for the device.
void set_size_max(l4_uint32_t sz)
Sets the maximum size of any single segment reported to client.
Server-side L4-VIRTIO device stub.
Unique_cap / Unique_del_cap.
l4virtio_block_header_t const & header() const
Return the block request header.
Interface for server-loop related functions.
Request finished successfully.
unsigned short int l4_uint16_t
Unsigned 16bit value.
Block_dev(l4_uint32_t vendor, unsigned queue_size, l4_uint64_t capacity, bool read_only)
Create a new virtio block device.
void set_seg_max(l4_uint32_t sz)
Sets the maximum number of segments in a request that is reported to client.
Exception used by Queue to indicate descriptor errors.
void set_geometry(l4_uint16_t cylinders, l4_uint8_t heads, l4_uint8_t sectors)
Set disk geometry that is reported to the client.
int reconfig_queue(unsigned idx)
callback for client queue-config request
bool ready() const
Test if this queue is in working state.
Device configuration for block devices.
void start(DESC_MAN *dm, Virtqueue *ring, Virtqueue::Head_desc const &request, ARGS... args)
Start processing a new request.
unsigned data_size() const
Compute the total size of the data in the request.
void consumed(Head_desc const &r, l4_uint32_t len=0)
Put the given descriptor into the used ring.
void disable()
Completely disable the queue.
Request next_avail()
Get the next available descriptor from the available ring.
Encapsulate the state for processing a VIRTIO request.
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.
Exception for an abstract runtime error.
Base class for virtio block devices.
void set_flush()
Enables the flush command.
L4::Detail::Unique_cap_impl< T, Smart_cap_auto< L4_FP_ALL_SPACES > > Unique_cap
Unique capability that implements automatic free and unmap of the capability selector.
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.
Data_block next_block()
Return next block in scatter-gather list.
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.
bool next(DESC_MAN *dm, ARGS... args)
Switch to the next descriptor in a descriptor chain.
Ptr< void > addr
Address stored in descriptor.
virtual bool reset_client()
The client requests reinitialisation of the connection.
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
A request to read or write data.
T * local(Ptr< T > p) const
Get the local address for driver address p.
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
unsigned char l4_uint8_t
Unsigned 8bit value.
Type for device feature bitmap.
virtual L4::Cap< L4::Irq > register_irq_obj(L4::Epiface *o)=0
Register o as server-side object for asynchronous IRQs.
bool has_more()
Check if the request contains more data blocks.
Invalid size of memory block.
Cap< T > cap_cast(Cap< F > const &c)
static_cast for capabilities.
unsigned long long l4_uint64_t
Unsigned 64bit value.
L4-VIRTIO Transport C++ API.
l4_uint8_t get_writeback()
Get the writeback field from the configuration space.
C++ interface for capabilities.
void set_blk_size(l4_uint32_t sz)
Sets block disk size to be reported to the client.
l4_uint32_t len
Length of described buffer.
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...
struct l4virtio_block_header_t l4virtio_block_header_t
Header structure of a request for a block device.
unsigned int l4_uint32_t
Unsigned 32bit value.
void set_topology(l4_uint8_t physical_block_exp, l4_uint8_t alignment_offset, l4_uint32_t min_io_size, l4_uint32_t opt_io_size)
Sets the I/O alignment information reported back to the client.