2 * Copyright (c) 2018 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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>
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
62 #define VIRTIO_FEATURES_32_63 1
64 #define VIRTIO_F_VERSION_1 1
66 /** Common configuration structure layout according to VIRTIO version 1.0 */
67 typedef struct virtio_pci_common_cfg
{
68 ioport32_t device_feature_select
;
69 const ioport32_t device_feature
;
70 ioport32_t driver_feature_select
;
71 ioport32_t driver_feature
;
72 ioport16_t msix_config
;
73 const ioport16_t num_queues
;
74 ioport8_t device_status
;
75 const ioport8_t config_generation
;
76 ioport16_t queue_select
;
77 ioport16_t queue_size
;
78 ioport16_t queue_msix_vector
;
79 ioport16_t queue_enable
;
80 const ioport16_t queue_notif_off
;
81 ioport64_t queue_desc
;
82 ioport64_t queue_avail
;
83 ioport64_t queue_used
;
84 } virtio_pci_common_cfg_t
;
86 /** The buffer continues in the next descriptor */
87 #define VIRTQ_DESC_F_NEXT 1
88 /** Device write-only buffer */
89 #define VIRTQ_DESC_F_WRITE 2
90 /** Buffer contains a list of buffer descriptors */
91 #define VIRTQ_DESC_F_INDIRECT 4
93 /** Virtqueue Descriptor structure as per VIRTIO version 1.0 */
94 typedef struct virtq_desc
{
95 ioport64_t addr
; /**< Buffer physical address */
96 ioport32_t len
; /**< Buffer length */
97 ioport16_t flags
; /**< Buffer flags */
98 ioport16_t next
; /**< Continuation descriptor */
101 #define VIRTQ_AVAIL_F_NO_INTERRUPT 1
103 /** Virtqueue Available Ring as per VIRTIO version 1.0 */
104 typedef struct virtq_avail
{
109 * We do not define the optional used_event member here in order to be
110 * able to define ring as a variable-length array.
114 typedef struct virtq_used_elem
{
119 #define VIRTQ_USED_F_NO_NOTIFY 1
121 /** Virtqueue Used Ring as per VIRTIO version 1.0 */
122 typedef struct virtq_used
{
125 virtq_used_elem_t ring
[];
127 * We do not define the optional avail_event member here in order to be
128 * able to define ring as a variable-length array.
137 /** Mutex protecting access to this virtqueue */
141 * Size of the queue which determines the number of descriptors and
146 /** Virtual address of queue size virtq descriptors */
148 /** Virtual address of the available ring */
149 virtq_avail_t
*avail
;
150 /** Virtual address of the used ring */
152 uint16_t used_last_idx
;
154 /** Address of the queue's notification register */
158 /** VIRTIO-device specific data associated with the NIC framework nic_t */
165 } bar
[PCI_BAR_COUNT
];
167 /** Commong configuration structure */
168 virtio_pci_common_cfg_t
*common_cfg
;
170 /** Notification base address */
172 /** Notification offset multiplier */
173 uint32_t notify_off_multiplier
;
175 /** INT#x interrupt ISR register */
179 /** Device-specific configuration */
186 extern errno_t
virtio_setup_dma_bufs(unsigned int, size_t, bool, void *[],
188 extern void virtio_teardown_dma_bufs(void *[]);
190 extern void virtio_virtq_desc_set(virtio_dev_t
*vdev
, uint16_t, uint16_t,
191 uint64_t, uint32_t, uint16_t, uint16_t);
192 extern uint16_t virtio_virtq_desc_get_next(virtio_dev_t
*vdev
, uint16_t,
195 extern void virtio_create_desc_free_list(virtio_dev_t
*, uint16_t, uint16_t,
197 extern uint16_t virtio_alloc_desc(virtio_dev_t
*, uint16_t, uint16_t *);
198 extern void virtio_free_desc(virtio_dev_t
*, uint16_t, uint16_t *, uint16_t);
200 extern void virtio_virtq_produce_available(virtio_dev_t
*, uint16_t, uint16_t);
201 extern bool virtio_virtq_consume_used(virtio_dev_t
*, uint16_t, uint16_t *,
204 extern errno_t
virtio_virtq_setup(virtio_dev_t
*, uint16_t, uint16_t);
205 extern void virtio_virtq_teardown(virtio_dev_t
*, uint16_t);
207 extern errno_t
virtio_device_setup_start(virtio_dev_t
*, uint32_t);
208 extern void virtio_device_setup_fail(virtio_dev_t
*);
209 extern void virtio_device_setup_finalize(virtio_dev_t
*);
211 extern errno_t
virtio_pci_dev_initialize(ddf_dev_t
*, virtio_dev_t
*);
212 extern errno_t
virtio_pci_dev_cleanup(virtio_dev_t
*);