Configure the virtqueues
[helenos.git] / uspace / lib / virtio / virtio-pci.h
blobc1f5cff7289f1a8d1d1e8d3fd0daebdf42635c81
1 /*
2 * Copyright (c) 2018 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @file VIRTIO PCI definitions
32 #ifndef _VIRTIO_PCI_H_
33 #define _VIRTIO_PCI_H_
35 #include <ddf/driver.h>
36 #include <pci_dev_iface.h>
37 #include <ddi.h>
39 #define VIRTIO_PCI_CAP_CAP_LEN(c) ((c) + 2)
40 #define VIRTIO_PCI_CAP_CFG_TYPE(c) ((c) + 3)
41 #define VIRTIO_PCI_CAP_BAR(c) ((c) + 4)
42 #define VIRTIO_PCI_CAP_OFFSET(c) ((c) + 8)
43 #define VIRTIO_PCI_CAP_LENGTH(c) ((c) + 12)
44 #define VIRTIO_PCI_CAP_END(c) ((c) + 16)
46 #define VIRTIO_PCI_CAP_COMMON_CFG 1
47 #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
48 #define VIRTIO_PCI_CAP_ISR_CFG 3
49 #define VIRTIO_PCI_CAP_DEVICE_CFG 4
50 #define VIRTIO_PCI_CAP_PCI_CFG 5
52 #define VIRTIO_DEV_STATUS_RESET 0
53 #define VIRTIO_DEV_STATUS_ACKNOWLEDGE 1
54 #define VIRTIO_DEV_STATUS_DRIVER 2
55 #define VIRTIO_DEV_STATUS_DRIVER_OK 4
56 #define VIRTIO_DEV_STATUS_FEATURES_OK 8
57 #define VIRTIO_DEV_STATUS_DEVICE_NEEDS_RESET 64
58 #define VIRTIO_DEV_STATUS_FAILED 128
60 /** Common configuration structure layout according to VIRTIO version 1.0 */
61 typedef struct virtio_pci_common_cfg {
62 ioport32_t device_feature_select;
63 const ioport32_t device_feature;
64 ioport32_t driver_feature_select;
65 ioport32_t driver_feature;
66 ioport16_t msix_config;
67 const ioport16_t num_queues;
68 ioport8_t device_status;
69 const ioport8_t config_generation;
70 ioport16_t queue_select;
71 ioport16_t queue_size;
72 ioport16_t queue_msix_vector;
73 ioport16_t queue_enable;
74 const ioport16_t queue_notif_off;
75 ioport64_t queue_desc;
76 ioport64_t queue_avail;
77 ioport64_t queue_used;
78 } virtio_pci_common_cfg_t;
80 /** The buffer continues in the next descriptor */
81 #define VIRTQ_DESC_F_NEXT 1
82 /** Device write-only buffer */
83 #define VIRTQ_DESC_F_WRITE 2
84 /** Buffer contains a list of buffer descriptors */
85 #define VIRTQ_DESC_F_INDIRECT 4
87 /** Virtqueue Descriptor structure as per VIRTIO version 1.0 */
88 typedef struct virtq_desc {
89 ioport64_t addr; /**< Buffer physical address */
90 ioport32_t len; /**< Buffer length */
91 ioport16_t flags; /**< Buffer flags */
92 ioport16_t next; /**< Continuation descriptor */
93 } virtq_desc_t;
95 #define VIRTQ_AVAIL_F_NO_INTERRUPT 1
97 /** Virtqueue Available Ring as per VIRTIO version 1.0 */
98 typedef struct virtq_avail {
99 ioport16_t flags;
100 ioport16_t idx;
101 ioport16_t ring[];
103 * We do not define the optional used_event member here in order to be
104 * able to define ring as a variable-length array.
106 } virtq_avail_t;
108 typedef struct virtq_used_elem {
109 ioport32_t id;
110 ioport32_t len;
111 } virtq_used_elem_t;
113 #define VIRTQ_USED_F_NO_NOTIFY 1
115 /** Virtqueue Used Ring as per VIRTIO version 1.0 */
116 typedef struct virtq_used {
117 ioport16_t flags;
118 ioport16_t idx;
119 virtq_used_elem_t ring[];
121 * We do not define the optional avail_event member here in order to be
122 * able to define ring as a variable-length array.
124 } virtq_used_t;
126 typedef struct {
127 void *virt;
128 uintptr_t phys;
129 size_t size;
132 * Size of the queue which determines the number of descriptors and
133 * DMA buffers.
135 size_t queue_size;
137 /** Virtual address of queue size virtq descriptors */
138 virtq_desc_t *desc;
139 /** Virtual address of the available ring */
140 virtq_avail_t *avail;
141 /** Virtual address of the used ring */
142 virtq_used_t *used;
145 * Queue-size-sized array of virtual addresses of the atcual DMA
146 * buffers.
148 void **buffers;
149 } virtq_t;
151 /** VIRTIO-device specific data associated with the NIC framework nic_t */
152 typedef struct {
153 struct {
154 bool mapped;
155 void *mapped_base;
156 size_t mapped_size;
157 } bar[PCI_BAR_COUNT];
159 /** Commong configuration structure */
160 virtio_pci_common_cfg_t *common_cfg;
162 /** Notification base address */
163 ioport16_t *notify_base;
164 /** Notification offset multiplier */
165 uint32_t notify_off_multiplier;
167 /** INT#x interrupt ISR register */
168 ioport8_t *isr;
170 /** Device-specific configuration */
171 void *device_cfg;
173 /** Virtqueues */
174 virtq_t *queues;
175 } virtio_dev_t;
177 extern errno_t virtio_virtq_setup(virtio_dev_t *, uint16_t, uint16_t, size_t,
178 uint16_t);
179 extern void virtio_virtq_teardown(virtio_dev_t *, uint16_t);
181 extern errno_t virtio_pci_dev_initialize(ddf_dev_t *, virtio_dev_t *);
182 extern errno_t virtio_pci_dev_cleanup(virtio_dev_t *);
184 #endif
186 /** @}