source: mainline/uspace/lib/virtio/virtio.c@ 3d135e9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3d135e9 was 3d135e9, checked in by Jakub Jermar <jakub@…>, 7 years ago

Implement virtio_net_send

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[2d4faf7]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 support
30 */
31
32#include "virtio-pci.h"
33
34#include <as.h>
35#include <align.h>
36#include <macros.h>
37
38#include <ddf/log.h>
[c1ba24a]39#include <libarch/barrier.h>
40
[3d135e9]41void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t num, uint16_t descno,
[c1ba24a]42 uint64_t addr, uint32_t len, uint16_t flags, uint16_t next)
43{
44 virtq_desc_t *d = &vdev->queues[num].desc[descno];
[9af56b6]45 pio_write_le64(&d->addr, addr);
46 pio_write_le32(&d->len, len);
47 pio_write_le16(&d->flags, flags);
48 pio_write_le16(&d->next, next);
[c1ba24a]49}
50
[3d135e9]51uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t num,
52 uint16_t descno)
53{
54 virtq_desc_t *d = &vdev->queues[num].desc[descno];
55 if (!(pio_read_le16(&d->flags) & VIRTQ_DESC_F_NEXT))
56 return (uint16_t) -1U;
57 return pio_read_le16(&d->next);
58}
59
[c1ba24a]60void virtio_virtq_produce_available(virtio_dev_t *vdev, uint16_t num,
61 uint16_t descno)
62{
63 virtq_t *q = &vdev->queues[num];
64
[9af56b6]65 uint16_t idx = pio_read_le16(&q->avail->idx);
66 pio_write_le16(&q->avail->ring[idx], descno);
[c1ba24a]67 write_barrier();
[9af56b6]68 pio_write_le16(&q->avail->idx, (idx + 1) % q->queue_size);
[c1ba24a]69 write_barrier();
[9af56b6]70 pio_write_le16(q->notify, num);
[c1ba24a]71}
[2d4faf7]72
[0180c67]73errno_t virtio_virtq_setup(virtio_dev_t *vdev, uint16_t num, uint16_t size)
[2d4faf7]74{
75 virtq_t *q = &vdev->queues[num];
76 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
77
78 /* Program the queue of our interest */
[9af56b6]79 pio_write_le16(&cfg->queue_select, num);
[2d4faf7]80
81 /* Trim the size of the queue as needed */
[ee0c03a]82 if (size > pio_read_16(&cfg->queue_size)) {
83 ddf_msg(LVL_ERROR, "Virtq %u: not enough descriptors", num);
84 return ENOMEM;
85 }
[9af56b6]86 pio_write_le16(&cfg->queue_size, size);
[ee0c03a]87 ddf_msg(LVL_NOTE, "Virtq %u: %u descriptors", num, (unsigned) size);
[2d4faf7]88
89 size_t avail_offset = 0;
90 size_t used_offset = 0;
91
92 /*
93 * Compute the size of the needed DMA memory and also the offsets of
94 * the individual components
95 */
96 size_t mem_size = sizeof(virtq_desc_t[size]);
97 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_avail_t));
98 avail_offset = mem_size;
99 mem_size += sizeof(virtq_avail_t) + sizeof(ioport16_t[size]) +
100 sizeof(ioport16_t);
101 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_used_t));
102 used_offset = mem_size;
103 mem_size += sizeof(virtq_used_t) + sizeof(virtq_used_elem_t[size]) +
104 sizeof(ioport16_t);
105
106 /*
[0180c67]107 * Allocate DMA memory for the virtqueues
[2d4faf7]108 */
109 q->virt = AS_AREA_ANY;
[5b5c286]110 errno_t rc = dmamem_map_anonymous(mem_size, 0,
[2d4faf7]111 AS_AREA_READ | AS_AREA_WRITE, 0, &q->phys, &q->virt);
112 if (rc != EOK) {
113 q->virt = NULL;
114 return rc;
115 }
116
117 q->size = mem_size;
118 q->queue_size = size;
119 q->desc = q->virt;
120 q->avail = q->virt + avail_offset;
121 q->used = q->virt + used_offset;
122
123 memset(q->virt, 0, q->size);
124
125 /*
126 * Write the configured addresses to device's common config
127 */
[9af56b6]128 pio_write_le64(&cfg->queue_desc, q->phys);
129 pio_write_le64(&cfg->queue_avail, q->phys + avail_offset);
130 pio_write_le64(&cfg->queue_used, q->phys + used_offset);
[2d4faf7]131
132 ddf_msg(LVL_NOTE, "DMA memory for virtq %d: virt=%p, phys=%p, size=%zu",
133 num, q->virt, (void *) q->phys, q->size);
134
[eda41a9e]135 /* Determine virtq's notification address */
136 q->notify = vdev->notify_base +
[9af56b6]137 pio_read_le16(&cfg->queue_notif_off) * vdev->notify_off_multiplier;
[eda41a9e]138
139 ddf_msg(LVL_NOTE, "notification register: %p", q->notify);
140
[cede6f8]141 /* Enable the queue */
142 pio_write_le16(&cfg->queue_enable, 1);
143 ddf_msg(LVL_NOTE, "virtq %d set", num);
144
[2d4faf7]145 return rc;
146}
147
148void virtio_virtq_teardown(virtio_dev_t *vdev, uint16_t num)
149{
[cede6f8]150 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
151
152 /* Disable the queue */
153 pio_write_le16(&cfg->queue_enable, 0);
154
[2d4faf7]155 virtq_t *q = &vdev->queues[num];
156 if (q->size)
157 dmamem_unmap_anonymous(q->virt);
158}
159
160/**
161 * Perform device initialization as described in section 3.1.1 of the
162 * specification, steps 1 - 6.
163 */
164errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
165{
166 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
167
168 /* 1. Reset the device */
169 uint8_t status = VIRTIO_DEV_STATUS_RESET;
170 pio_write_8(&cfg->device_status, status);
171
172 /* 2. Acknowledge we found the device */
173 status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
174 pio_write_8(&cfg->device_status, status);
175
176 /* 3. We know how to drive the device */
177 status |= VIRTIO_DEV_STATUS_DRIVER;
178 pio_write_8(&cfg->device_status, status);
179
180 /* 4. Read the offered feature flags */
[9af56b6]181 pio_write_le32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
182 uint32_t device_features = pio_read_le32(&cfg->device_feature);
[2d4faf7]183
184 ddf_msg(LVL_NOTE, "offered features %x", device_features);
185 features &= device_features;
186
187 if (!features)
188 return ENOTSUP;
189
190 /* 4. Write the accepted feature flags */
[9af56b6]191 pio_write_le32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
192 pio_write_le32(&cfg->driver_feature, features);
[2d4faf7]193
194 ddf_msg(LVL_NOTE, "accepted features %x", features);
195
196 /* 5. Set FEATURES_OK */
197 status |= VIRTIO_DEV_STATUS_FEATURES_OK;
198 pio_write_8(&cfg->device_status, status);
199
[eda41a9e]200 /* 6. Test if the device supports our feature subset */
[2d4faf7]201 status = pio_read_8(&cfg->device_status);
202 if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
203 return ENOTSUP;
204
205 return EOK;
206}
207
208/**
209 * Perform device initialization as described in section 3.1.1 of the
210 * specification, step 8 (go live).
211 */
212void virtio_device_setup_finalize(virtio_dev_t *vdev)
213{
214 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
215
216 /* 8. Go live */
217 uint8_t status = pio_read_8(&cfg->device_status);
218 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
219}
220
221void virtio_device_setup_fail(virtio_dev_t *vdev)
222{
223 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
224
225 uint8_t status = pio_read_8(&cfg->device_status);
226 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
227}
228
229/** @}
230 */
Note: See TracBrowser for help on using the repository browser.