source: mainline/uspace/lib/virtio/virtio-pci.h@ eec201d

Last change on this file since eec201d was 1e472ee, checked in by Jakub Jermar <jakub@…>, 7 years ago

Move DMA buffers allocation functions to libvirtio

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (c) 2018 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @file VIRTIO PCI definitions
30 */
31
32#ifndef _VIRTIO_PCI_H_
33#define _VIRTIO_PCI_H_
34
35#include <ddf/driver.h>
36#include <pci_dev_iface.h>
37#include <ddi.h>
38#include <fibril_synch.h>
39
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)
46
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
52
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
60
61#define VIRTIO_FEATURES_0_31 0
62
63/** Common configuration structure layout according to VIRTIO version 1.0 */
64typedef 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;
82
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
89
90/** Virtqueue Descriptor structure as per VIRTIO version 1.0 */
91typedef 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;
97
98#define VIRTQ_AVAIL_F_NO_INTERRUPT 1
99
100/** Virtqueue Available Ring as per VIRTIO version 1.0 */
101typedef struct virtq_avail {
102 ioport16_t flags;
103 ioport16_t idx;
104 ioport16_t ring[];
105 /*
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.
108 */
109} virtq_avail_t;
110
111typedef struct virtq_used_elem {
112 ioport32_t id;
113 ioport32_t len;
114} virtq_used_elem_t;
115
116#define VIRTQ_USED_F_NO_NOTIFY 1
117
118/** Virtqueue Used Ring as per VIRTIO version 1.0 */
119typedef struct virtq_used {
120 ioport16_t flags;
121 ioport16_t idx;
122 virtq_used_elem_t ring[];
123 /*
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.
126 */
127} virtq_used_t;
128
129typedef struct {
130 void *virt;
131 uintptr_t phys;
132 size_t size;
133
134 /** Mutex protecting access to this virtqueue */
135 fibril_mutex_t lock;
136
137 /**
138 * Size of the queue which determines the number of descriptors and
139 * DMA buffers.
140 */
141 size_t queue_size;
142
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;
150
151 /** Address of the queue's notification register */
152 ioport16_t *notify;
153} virtq_t;
154
155/** VIRTIO-device specific data associated with the NIC framework nic_t */
156typedef 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];
163
164 /** Commong configuration structure */
165 virtio_pci_common_cfg_t *common_cfg;
166
167 /** Notification base address */
168 void *notify_base;
169 /** Notification offset multiplier */
170 uint32_t notify_off_multiplier;
171
172 /** INT#x interrupt ISR register */
173 ioport8_t *isr;
174 uintptr_t isr_phys;
175
176 /** Device-specific configuration */
177 void *device_cfg;
178
179 /** Virtqueues */
180 virtq_t *queues;
181} virtio_dev_t;
182
183extern errno_t virtio_setup_dma_bufs(unsigned int, size_t, bool, void *[],
184 uintptr_t []);
185extern void virtio_teardown_dma_bufs(void *[]);
186
187extern void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t, uint16_t,
188 uint64_t, uint32_t, uint16_t, uint16_t);
189extern uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t,
190 uint16_t);
191
192extern void virtio_create_desc_free_list(virtio_dev_t *, uint16_t, uint16_t,
193 uint16_t *);
194extern uint16_t virtio_alloc_desc(virtio_dev_t *, uint16_t, uint16_t *);
195extern void virtio_free_desc(virtio_dev_t *, uint16_t, uint16_t *, uint16_t);
196
197extern void virtio_virtq_produce_available(virtio_dev_t *, uint16_t, uint16_t);
198extern bool virtio_virtq_consume_used(virtio_dev_t *, uint16_t, uint16_t *,
199 uint32_t *);
200
201extern errno_t virtio_virtq_setup(virtio_dev_t *, uint16_t, uint16_t);
202extern void virtio_virtq_teardown(virtio_dev_t *, uint16_t);
203
204extern errno_t virtio_device_setup_start(virtio_dev_t *, uint32_t);
205extern void virtio_device_setup_fail(virtio_dev_t *);
206extern void virtio_device_setup_finalize(virtio_dev_t *);
207
208extern errno_t virtio_pci_dev_initialize(ddf_dev_t *, virtio_dev_t *);
209extern errno_t virtio_pci_dev_cleanup(virtio_dev_t *);
210
211#endif
212
213/** @}
214 */
Note: See TracBrowser for help on using the repository browser.