16#include <l4/re/util/object_registry>
21#include <l4/l4virtio/client/l4virtio>
22#include <l4/l4virtio/l4virtio>
23#include <l4/l4virtio/virtqueue>
24#include <l4/l4virtio/virtio_block.h>
31namespace L4virtio {
namespace Driver {
39 typedef std::function<void(
unsigned char)> Callback;
49 Request() : tail(Virtqueue::Eoq), callback(0) {}
61 explicit Handle(
l4_uint16_t descno) : head(descno) {}
64 Handle() : head(Virtqueue::Eoq) {}
65 bool valid()
const {
return head != Virtqueue::Eoq; }
103 if (_config->num_queues != 1)
114 l4_uint64_t const status_offset = header_offset + queuesz * Header_size;
119 totalsz += usermem_offset;
125 "Allocate queue dataspace capability");
129 "Allocate memory for virtio structures");
130 _queue_ds = _ds.get();
134 if (qds->size() < totalsz)
144 "Attach dataspace for virtio structures");
148 "Register queue dataspace with device");
150 _queue.init_queue(queuesz, _queue_region.get());
152 config_queue(0, queuesz, devaddr, devaddr + _queue.avail_offset(),
153 devaddr + _queue.used_offset());
155 _header_addr = devaddr + header_offset;
159 _status_addr = devaddr + status_offset;
160 _status = _queue_region.get() + status_offset;
162 user_devaddr =
Ptr<void>(devaddr + usermem_offset);
164 *userdata = _queue_region.get() + usermem_offset;
167 _pending.assign(queuesz, Request());
170 _config->driver_features_map[0] = fmask0;
171 _config->driver_features_map[1] = fmask1;
195 if (descno == Virtqueue::Eoq)
196 return Handle(Virtqueue::Eoq);
199 Request &req = _pending[descno];
209 desc.
len = Header_size;
213 req.callback = callback;
232 if (descno == Virtqueue::Eoq)
235 Request &req = _pending[handle.head];
245 if (_headers[handle.head].type > 0)
268 auto descno = _queue.alloc_descriptor();
269 if (descno == Virtqueue::Eoq)
272 Request &req = _pending[handle.head];
286 send(_queue, handle.head);
309 auto descno = _queue.alloc_descriptor();
310 if (descno == Virtqueue::Eoq)
324 _pending[handle.head].tail = descno;
327 unsigned char status = _status[descno];
328 free_request(handle);
343 void free_request(Handle handle)
345 if (handle.head != Virtqueue::Eoq
346 && _pending[handle.head].tail != Virtqueue::Eoq)
348 _pending[handle.head].tail = Virtqueue::Eoq;
360 descno != Virtqueue::Eoq;
361 descno = _queue.find_next_used()
364 if (descno >= _queue.num() || _pending[descno].tail == Virtqueue::Eoq)
367 unsigned char status = _status[descno];
368 free_request(
Handle(descno));
370 if (_pending[descno].callback)
371 _pending[descno].callback(status);
382 unsigned char *_status;
386 std::vector<Request> _pending;
bit manipulation functions
static Env const * env() noexcept
Returns the initial environment for the current task.
@ Continuous
Allocate physically contiguous memory.
@ Pinned
Deprecated, use L4Re::Dma_space instead.
C++ interface for capabilities.
Handle to an ongoing request.
Simple class for accessing a virtio block device synchronously.
int send_request(Handle handle)
Process request asynchronously.
int add_block(Handle handle, Ptr< void > addr, l4_uint32_t size)
Add a data block to a request that has already been set up.
int process_request(Handle handle)
Process request synchronously.
l4virtio_block_config_t const & device_config() const
Return a reference to the device configuration.
void process_used_queue()
Process and free all items in the used queue.
void setup_device(L4::Cap< L4virtio::Device > srvcap, l4_size_t usermem, void **userdata, Ptr< void > &user_devaddr, L4::Cap< L4Re::Dataspace > qds=L4::Cap< L4Re::Dataspace >(), l4_uint32_t fmask0=-1U, l4_uint32_t fmask1=-1U)
Establish a connection to the device and set up shared memory.
Handle start_request(l4_uint64_t sector, l4_uint32_t type, Callback callback)
Start the setup of a new request.
Client-side implementation for a general virtio device.
int max_queue_size(int num) const
Maximum queue size allowed by the device.
void driver_connect(L4::Cap< L4virtio::Device > srvcap, bool manage_notify=true)
Contacts the device and starts the initial handshake.
void send(Virtqueue &queue, l4_uint16_t descno)
Send a request to the device.
int register_ds(L4::Cap< L4Re::Dataspace > ds, l4_umword_t offset, l4_umword_t size, l4_uint64_t *devaddr)
Share a dataspace with the device.
int config_queue(int num, unsigned size, l4_uint64_t desc_addr, l4_uint64_t avail_addr, l4_uint64_t used_addr)
Send the virtqueue configuration to the device.
int driver_acknowledge()
Finalize handshake with the device.
int send_and_wait(Virtqueue &queue, l4_uint16_t descno)
Send a request to the device and wait for it to be processed.
Driver-side implementation of a Virtqueue.
void free_descriptor(l4_uint16_t head, l4_uint16_t tail)
Free a chained list of descriptors in the descriptor queue.
Pointer used in virtio descriptors.
Descriptor in the descriptor table.
l4_uint16_t next
Index of the next chained descriptor.
l4_uint32_t len
Length of described buffer.
Flags flags
Descriptor flags.
Ptr< void > addr
Address stored in descriptor.
Common factory related definitions.
unsigned int l4_size_t
Unsigned size type.
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_EINVAL
Invalid argument.
@ L4_ENODEV
No such thing.
l4_addr_t l4_round_page(l4_addr_t address) L4_NOTHROW
Round address up to the next page.
#define L4_PAGESHIFT
Size of a page, log2-based.
l4_addr_t l4_round_size(l4_addr_t value, unsigned char bits) L4_NOTHROW
Round value up to the next alignment with bits size.
@ L4VIRTIO_BLOCK_S_IOERR
IO error on device.
@ L4VIRTIO_BLOCK_S_UNSUPP
Operation is not supported.
@ L4VIRTIO_BLOCK_S_OK
Request finished successfully.
@ L4VIRTIO_ID_BLOCK
General block device.
atomic operations header and generic implementations
Unique_cap< T > make_unique_cap()
Allocate a capability slot and wrap it in an Unique_cap.
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.
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
T chkcap(T &&cap, char const *extra="", long err=-L4_ENOMEM)
Check for valid capability or raise C++ exception.
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
L4-VIRTIO Transport C++ API.
Semaphore class definition.
@ RW
Readable and writable region.
@ Search_addr
Search for a suitable address range.
constexpr next_bfm_t::Val next() const
Get the next bits (0 to 0) of raw.
constexpr write_bfm_t::Val write() const
Get the write bits (1 to 1) of raw.
l4_uint16_t raw
raw flags value of a virtio descriptor.
Device configuration for block devices.
Unique_cap / Unique_del_cap.