Move desc free list functions to libvirtio
[helenos.git] / uspace / lib / virtio / virtio-pci.h
blob715237e0bd080b2b826c03300ffa8280c356bfc4
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>
38 #include <fibril_synch.h>
40 #define VIRTIO_PCI_CAP_CAP_LEN(c) ((c) + 2)
41 #define VIRTIO_PCI_CAP_CFG_TYPE(c) ((c) + 3)
42 #define VIRTIO_PCI_CAP_BAR(c) ((c) + 4)
43 #define VIRTIO_PCI_CAP_OFFSET(c) ((c) + 8)
44 #define VIRTIO_PCI_CAP_LENGTH(c) ((c) + 12)
45 #define VIRTIO_PCI_CAP_END(c) ((c) + 16)
47 #define VIRTIO_PCI_CAP_COMMON_CFG 1
48 #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
49 #define VIRTIO_PCI_CAP_ISR_CFG 3
50 #define VIRTIO_PCI_CAP_DEVICE_CFG 4
51 #define VIRTIO_PCI_CAP_PCI_CFG 5
53 #define VIRTIO_DEV_STATUS_RESET 0
54 #define VIRTIO_DEV_STATUS_ACKNOWLEDGE 1
55 #define VIRTIO_DEV_STATUS_DRIVER 2
56 #define VIRTIO_DEV_STATUS_DRIVER_OK 4
57 #define VIRTIO_DEV_STATUS_FEATURES_OK 8
58 #define VIRTIO_DEV_STATUS_DEVICE_NEEDS_RESET 64
59 #define VIRTIO_DEV_STATUS_FAILED 128
61 #define VIRTIO_FEATURES_0_31 0
63 /** Common configuration structure layout according to VIRTIO version 1.0 */
64 typedef struct virtio_pci_common_cfg {
65 ioport32_t device_feature_select;
66 const ioport32_t device_feature;
67 ioport32_t driver_feature_select;
68 ioport32_t driver_feature;
69 ioport16_t msix_config;
70 const ioport16_t num_queues;
71 ioport8_t device_status;
72 const ioport8_t config_generation;
73 ioport16_t queue_select;
74 ioport16_t queue_size;
75 ioport16_t queue_msix_vector;
76 ioport16_t queue_enable;
77 const ioport16_t queue_notif_off;
78 ioport64_t queue_desc;
79 ioport64_t queue_avail;
80 ioport64_t queue_used;
81 } virtio_pci_common_cfg_t;
83 /** The buffer continues in the next descriptor */
84 #define VIRTQ_DESC_F_NEXT 1
85 /** Device write-only buffer */
86 #define VIRTQ_DESC_F_WRITE 2
87 /** Buffer contains a list of buffer descriptors */
88 #define VIRTQ_DESC_F_INDIRECT 4
90 /** Virtqueue Descriptor structure as per VIRTIO version 1.0 */
91 typedef struct virtq_desc {
92 ioport64_t addr; /**< Buffer physical address */
93 ioport32_t len; /**< Buffer length */
94 ioport16_t flags; /**< Buffer flags */
95 ioport16_t next; /**< Continuation descriptor */
96 } virtq_desc_t;
98 #define VIRTQ_AVAIL_F_NO_INTERRUPT 1
100 /** Virtqueue Available Ring as per VIRTIO version 1.0 */
101 typedef struct virtq_avail {
102 ioport16_t flags;
103 ioport16_t idx;
104 ioport16_t ring[];
106 * We do not define the optional used_event member here in order to be
107 * able to define ring as a variable-length array.
109 } virtq_avail_t;
111 typedef struct virtq_used_elem {
112 ioport32_t id;
113 ioport32_t len;
114 } virtq_used_elem_t;
116 #define VIRTQ_USED_F_NO_NOTIFY 1
118 /** Virtqueue Used Ring as per VIRTIO version 1.0 */
119 typedef struct virtq_used {
120 ioport16_t flags;
121 ioport16_t idx;
122 virtq_used_elem_t ring[];
124 * We do not define the optional avail_event member here in order to be
125 * able to define ring as a variable-length array.
127 } virtq_used_t;
129 typedef struct {
130 void *virt;
131 uintptr_t phys;
132 size_t size;
134 /** Mutex protecting access to this virtqueue */
135 fibril_mutex_t lock;
138 * Size of the queue which determines the number of descriptors and
139 * DMA buffers.
141 size_t queue_size;
143 /** Virtual address of queue size virtq descriptors */
144 virtq_desc_t *desc;
145 /** Virtual address of the available ring */
146 virtq_avail_t *avail;
147 /** Virtual address of the used ring */
148 virtq_used_t *used;
149 uint16_t used_last_idx;
151 /** Address of the queue's notification register */
152 ioport16_t *notify;
153 } virtq_t;
155 /** VIRTIO-device specific data associated with the NIC framework nic_t */
156 typedef struct {
157 struct {
158 bool mapped;
159 uintptr_t phys_base;
160 void *mapped_base;
161 size_t mapped_size;
162 } bar[PCI_BAR_COUNT];
164 /** Commong configuration structure */
165 virtio_pci_common_cfg_t *common_cfg;
167 /** Notification base address */
168 void *notify_base;
169 /** Notification offset multiplier */
170 uint32_t notify_off_multiplier;
172 /** INT#x interrupt ISR register */
173 ioport8_t *isr;
174 uintptr_t isr_phys;
176 /** Device-specific configuration */
177 void *device_cfg;
179 /** Virtqueues */
180 virtq_t *queues;
181 } virtio_dev_t;
183 extern void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t, uint16_t,
184 uint64_t, uint32_t, uint16_t, uint16_t);
185 extern uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t,
186 uint16_t);
188 extern void virtio_create_desc_free_list(virtio_dev_t *, uint16_t, uint16_t,
189 uint16_t *);
190 extern uint16_t virtio_alloc_desc(virtio_dev_t *, uint16_t, uint16_t *);
191 extern void virtio_free_desc(virtio_dev_t *, uint16_t, uint16_t *, uint16_t);
193 extern void virtio_virtq_produce_available(virtio_dev_t *, uint16_t, uint16_t);
194 extern bool virtio_virtq_consume_used(virtio_dev_t *, uint16_t, uint16_t *,
195 uint32_t *);
197 extern errno_t virtio_virtq_setup(virtio_dev_t *, uint16_t, uint16_t);
198 extern void virtio_virtq_teardown(virtio_dev_t *, uint16_t);
200 extern errno_t virtio_device_setup_start(virtio_dev_t *, uint32_t);
201 extern void virtio_device_setup_fail(virtio_dev_t *);
202 extern void virtio_device_setup_finalize(virtio_dev_t *);
204 extern errno_t virtio_pci_dev_initialize(ddf_dev_t *, virtio_dev_t *);
205 extern errno_t virtio_pci_dev_cleanup(virtio_dev_t *);
207 #endif
209 /** @}