source: mainline/uspace/drv/nic/virtio-net/virtio-net.c@ 9364ced

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

virtio-net: Setup DMA buffers

  • Property mode set to 100644
File size: 5.9 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#include "virtio-net.h"
30
31#include <stdio.h>
32#include <stdint.h>
33
34#include <as.h>
35#include <ddf/driver.h>
36#include <ddf/log.h>
37#include <ops/nic.h>
38#include <pci_dev_iface.h>
39
40#include <nic.h>
41
42#include <virtio-pci.h>
43
44#define NAME "virtio-net"
45
46#define VIRTIO_NET_NUM_QUEUES 3
47
48#define RX_QUEUE_1 0
49#define TX_QUEUE_1 1
50#define CT_QUEUE_1 2
51
52#define BUFFER_SIZE 2048
53#define RX_BUF_SIZE BUFFER_SIZE
54#define TX_BUF_SIZE BUFFER_SIZE
55#define CT_BUF_SIZE BUFFER_SIZE
56
57
58static errno_t virtio_net_setup_bufs(unsigned int buffers, size_t size,
59 bool write, void *buf[], uintptr_t buf_p[])
60{
61 /*
62 * Allocate all buffers at once in one large chung.
63 */
64 void *virt = AS_AREA_ANY;
65 uintptr_t phys;
66 errno_t rc = dmamem_map_anonymous(buffers * size, 0,
67 write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
68 if (rc != EOK)
69 return rc;
70
71 ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
72
73 /*
74 * Calculate addresses of the individual buffers for easy access.
75 */
76 for (unsigned i = 0; i < buffers; i++) {
77 buf[i] = virt + i * size;
78 buf_p[i] = phys + i * size;
79 }
80
81 return EOK;
82}
83
84static void virtio_net_teardown_bufs(void *buf[])
85{
86 if (buf[0]) {
87 dmamem_unmap_anonymous(buf[0]);
88 buf[0] = NULL;
89 }
90}
91
92static errno_t virtio_net_initialize(ddf_dev_t *dev)
93{
94 nic_t *nic_data = nic_create_and_bind(dev);
95 if (!nic_data)
96 return ENOMEM;
97
98 virtio_net_t *virtio_net = calloc(1, sizeof(virtio_net_t));
99 if (!virtio_net) {
100 nic_unbind_and_destroy(dev);
101 return ENOMEM;
102 }
103
104 nic_set_specific(nic_data, virtio_net);
105
106 errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev);
107 if (rc != EOK)
108 return rc;
109
110 virtio_dev_t *vdev = &virtio_net->virtio_dev;
111 virtio_pci_common_cfg_t *cfg = virtio_net->virtio_dev.common_cfg;
112 virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg;
113
114 /* Reset the device and negotiate the feature bits */
115 rc = virtio_device_setup_start(vdev,
116 VIRTIO_NET_F_MAC | VIRTIO_NET_F_CTRL_VQ);
117 if (rc != EOK)
118 goto fail;
119
120 /* Perform device-specific setup */
121
122 /*
123 * Discover and configure the virtqueues
124 */
125 uint16_t num_queues = pio_read_16(&cfg->num_queues);
126 if (num_queues != VIRTIO_NET_NUM_QUEUES) {
127 ddf_msg(LVL_NOTE, "Unsupported number of virtqueues: %u",
128 num_queues);
129 goto fail;
130 }
131
132 vdev->queues = calloc(sizeof(virtq_t), num_queues);
133 if (!vdev->queues) {
134 rc = ENOMEM;
135 goto fail;
136 }
137
138 rc = virtio_virtq_setup(vdev, RX_QUEUE_1, RX_BUFFERS);
139 if (rc != EOK)
140 goto fail;
141 rc = virtio_virtq_setup(vdev, TX_QUEUE_1, TX_BUFFERS);
142 if (rc != EOK)
143 goto fail;
144 rc = virtio_virtq_setup(vdev, CT_QUEUE_1, CT_BUFFERS);
145 if (rc != EOK)
146 goto fail;
147
148 /*
149 * Setup DMA buffers
150 */
151 rc = virtio_net_setup_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
152 virtio_net->rx_buf, virtio_net->rx_buf_p);
153 if (rc != EOK)
154 goto fail;
155 rc = virtio_net_setup_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
156 virtio_net->tx_buf, virtio_net->tx_buf_p);
157 if (rc != EOK)
158 goto fail;
159 rc = virtio_net_setup_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
160 virtio_net->ct_buf, virtio_net->ct_buf_p);
161 if (rc != EOK)
162 goto fail;
163
164 /*
165 * Read the MAC address
166 */
167 nic_address_t nic_addr;
168 for (unsigned i = 0; i < 6; i++)
169 nic_addr.address[i] = pio_read_8(&netcfg->mac[i]);
170 rc = nic_report_address(nic_data, &nic_addr);
171 if (rc != EOK)
172 goto fail;
173
174 ddf_msg(LVL_NOTE, "MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
175 nic_addr.address[0], nic_addr.address[1], nic_addr.address[2],
176 nic_addr.address[3], nic_addr.address[4], nic_addr.address[5]);
177
178 /* Go live */
179 virtio_device_setup_finalize(vdev);
180
181 return EOK;
182
183fail:
184 virtio_net_teardown_bufs(virtio_net->rx_buf);
185 virtio_net_teardown_bufs(virtio_net->tx_buf);
186 virtio_net_teardown_bufs(virtio_net->ct_buf);
187
188 virtio_device_setup_fail(vdev);
189 virtio_pci_dev_cleanup(vdev);
190 return rc;
191}
192
193static errno_t virtio_net_dev_add(ddf_dev_t *dev)
194{
195 ddf_msg(LVL_NOTE, "%s %s (handle = %zu)", __func__,
196 ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
197
198 errno_t rc = virtio_net_initialize(dev);
199 if (rc != EOK)
200 return rc;
201
202 return ENOTSUP;
203}
204
205static ddf_dev_ops_t virtio_net_dev_ops;
206
207static driver_ops_t virtio_net_driver_ops = {
208 .dev_add = virtio_net_dev_add
209};
210
211static driver_t virtio_net_driver = {
212 .name = NAME,
213 .driver_ops = &virtio_net_driver_ops
214};
215
216static nic_iface_t virtio_net_nic_iface;
217
218int main(void)
219{
220 printf("%s: HelenOS virtio-net driver\n", NAME);
221
222 if (nic_driver_init(NAME) != EOK)
223 return 1;
224
225 nic_driver_implement(&virtio_net_driver_ops, &virtio_net_dev_ops,
226 &virtio_net_nic_iface);
227
228 (void) ddf_log_init(NAME);
229 return ddf_driver_main(&virtio_net_driver);
230}
Note: See TracBrowser for help on using the repository browser.