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 | static ddf_dev_ops_t virtio_net_dev_ops;
|
---|
58 |
|
---|
59 | static errno_t virtio_net_dev_add(ddf_dev_t *dev);
|
---|
60 |
|
---|
61 | static driver_ops_t virtio_net_driver_ops = {
|
---|
62 | .dev_add = virtio_net_dev_add
|
---|
63 | };
|
---|
64 |
|
---|
65 | static driver_t virtio_net_driver = {
|
---|
66 | .name = NAME,
|
---|
67 | .driver_ops = &virtio_net_driver_ops
|
---|
68 | };
|
---|
69 |
|
---|
70 | static errno_t virtio_net_setup_bufs(unsigned int buffers, size_t size,
|
---|
71 | bool write, void *buf[], uintptr_t buf_p[])
|
---|
72 | {
|
---|
73 | /*
|
---|
74 | * Allocate all buffers at once in one large chung.
|
---|
75 | */
|
---|
76 | void *virt = AS_AREA_ANY;
|
---|
77 | uintptr_t phys;
|
---|
78 | errno_t rc = dmamem_map_anonymous(buffers * size, 0,
|
---|
79 | write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
|
---|
80 | if (rc != EOK)
|
---|
81 | return rc;
|
---|
82 |
|
---|
83 | ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Calculate addresses of the individual buffers for easy access.
|
---|
87 | */
|
---|
88 | for (unsigned i = 0; i < buffers; i++) {
|
---|
89 | buf[i] = virt + i * size;
|
---|
90 | buf_p[i] = phys + i * size;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return EOK;
|
---|
94 | }
|
---|
95 |
|
---|
96 | static void virtio_net_teardown_bufs(void *buf[])
|
---|
97 | {
|
---|
98 | if (buf[0]) {
|
---|
99 | dmamem_unmap_anonymous(buf[0]);
|
---|
100 | buf[0] = NULL;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | static void virtio_net_create_buf_free_list(virtio_dev_t *vdev, uint16_t num,
|
---|
105 | uint16_t size, uint16_t *head)
|
---|
106 | {
|
---|
107 | for (unsigned i = 0; i < size; i++) {
|
---|
108 | virtio_virtq_set_desc(vdev, num, i, 0, 0,
|
---|
109 | VIRTQ_DESC_F_NEXT, (i + 1 == size) ? -1U : i + 1);
|
---|
110 | }
|
---|
111 | *head = 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static errno_t virtio_net_initialize(ddf_dev_t *dev)
|
---|
115 | {
|
---|
116 | nic_t *nic_data = nic_create_and_bind(dev);
|
---|
117 | if (!nic_data)
|
---|
118 | return ENOMEM;
|
---|
119 |
|
---|
120 | virtio_net_t *virtio_net = calloc(1, sizeof(virtio_net_t));
|
---|
121 | if (!virtio_net) {
|
---|
122 | nic_unbind_and_destroy(dev);
|
---|
123 | return ENOMEM;
|
---|
124 | }
|
---|
125 |
|
---|
126 | nic_set_specific(nic_data, virtio_net);
|
---|
127 |
|
---|
128 | errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev);
|
---|
129 | if (rc != EOK)
|
---|
130 | return rc;
|
---|
131 |
|
---|
132 | virtio_dev_t *vdev = &virtio_net->virtio_dev;
|
---|
133 | virtio_pci_common_cfg_t *cfg = virtio_net->virtio_dev.common_cfg;
|
---|
134 | virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg;
|
---|
135 |
|
---|
136 | /* Reset the device and negotiate the feature bits */
|
---|
137 | rc = virtio_device_setup_start(vdev,
|
---|
138 | VIRTIO_NET_F_MAC | VIRTIO_NET_F_CTRL_VQ);
|
---|
139 | if (rc != EOK)
|
---|
140 | goto fail;
|
---|
141 |
|
---|
142 | /* Perform device-specific setup */
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Discover and configure the virtqueues
|
---|
146 | */
|
---|
147 | uint16_t num_queues = pio_read_le16(&cfg->num_queues);
|
---|
148 | if (num_queues != VIRTIO_NET_NUM_QUEUES) {
|
---|
149 | ddf_msg(LVL_NOTE, "Unsupported number of virtqueues: %u",
|
---|
150 | num_queues);
|
---|
151 | goto fail;
|
---|
152 | }
|
---|
153 |
|
---|
154 | vdev->queues = calloc(sizeof(virtq_t), num_queues);
|
---|
155 | if (!vdev->queues) {
|
---|
156 | rc = ENOMEM;
|
---|
157 | goto fail;
|
---|
158 | }
|
---|
159 |
|
---|
160 | rc = virtio_virtq_setup(vdev, RX_QUEUE_1, RX_BUFFERS);
|
---|
161 | if (rc != EOK)
|
---|
162 | goto fail;
|
---|
163 | rc = virtio_virtq_setup(vdev, TX_QUEUE_1, TX_BUFFERS);
|
---|
164 | if (rc != EOK)
|
---|
165 | goto fail;
|
---|
166 | rc = virtio_virtq_setup(vdev, CT_QUEUE_1, CT_BUFFERS);
|
---|
167 | if (rc != EOK)
|
---|
168 | goto fail;
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Setup DMA buffers
|
---|
172 | */
|
---|
173 | rc = virtio_net_setup_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
|
---|
174 | virtio_net->rx_buf, virtio_net->rx_buf_p);
|
---|
175 | if (rc != EOK)
|
---|
176 | goto fail;
|
---|
177 | rc = virtio_net_setup_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
|
---|
178 | virtio_net->tx_buf, virtio_net->tx_buf_p);
|
---|
179 | if (rc != EOK)
|
---|
180 | goto fail;
|
---|
181 | rc = virtio_net_setup_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
|
---|
182 | virtio_net->ct_buf, virtio_net->ct_buf_p);
|
---|
183 | if (rc != EOK)
|
---|
184 | goto fail;
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Give all RX buffers to the NIC
|
---|
188 | */
|
---|
189 | for (unsigned i = 0; i < RX_BUFFERS; i++) {
|
---|
190 | /*
|
---|
191 | * Associtate the buffer with the descriptor, set length and
|
---|
192 | * flags.
|
---|
193 | */
|
---|
194 | virtio_virtq_set_desc(vdev, RX_QUEUE_1, i,
|
---|
195 | virtio_net->rx_buf_p[i], RX_BUF_SIZE, VIRTQ_DESC_F_WRITE,
|
---|
196 | 0);
|
---|
197 | /*
|
---|
198 | * Put the set descriptor into the available ring of the RX
|
---|
199 | * queue.
|
---|
200 | */
|
---|
201 | virtio_virtq_produce_available(vdev, RX_QUEUE_1, i);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Put all TX and CT buffers on a free list
|
---|
206 | */
|
---|
207 | virtio_net_create_buf_free_list(vdev, TX_QUEUE_1, TX_BUFFERS,
|
---|
208 | &virtio_net->tx_free_head);
|
---|
209 | virtio_net_create_buf_free_list(vdev, CT_QUEUE_1, CT_BUFFERS,
|
---|
210 | &virtio_net->ct_free_head);
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * Read the MAC address
|
---|
214 | */
|
---|
215 | nic_address_t nic_addr;
|
---|
216 | for (unsigned i = 0; i < 6; i++)
|
---|
217 | nic_addr.address[i] = pio_read_8(&netcfg->mac[i]);
|
---|
218 | rc = nic_report_address(nic_data, &nic_addr);
|
---|
219 | if (rc != EOK)
|
---|
220 | goto fail;
|
---|
221 |
|
---|
222 | ddf_msg(LVL_NOTE, "MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
|
---|
223 | nic_addr.address[0], nic_addr.address[1], nic_addr.address[2],
|
---|
224 | nic_addr.address[3], nic_addr.address[4], nic_addr.address[5]);
|
---|
225 |
|
---|
226 | /* Go live */
|
---|
227 | virtio_device_setup_finalize(vdev);
|
---|
228 |
|
---|
229 | return EOK;
|
---|
230 |
|
---|
231 | fail:
|
---|
232 | virtio_net_teardown_bufs(virtio_net->rx_buf);
|
---|
233 | virtio_net_teardown_bufs(virtio_net->tx_buf);
|
---|
234 | virtio_net_teardown_bufs(virtio_net->ct_buf);
|
---|
235 |
|
---|
236 | virtio_device_setup_fail(vdev);
|
---|
237 | virtio_pci_dev_cleanup(vdev);
|
---|
238 | return rc;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static void virtio_net_uninitialize(ddf_dev_t *dev)
|
---|
242 | {
|
---|
243 | nic_t *nic = ddf_dev_data_get(dev);
|
---|
244 | virtio_net_t *virtio_net = (virtio_net_t *) nic_get_specific(nic);
|
---|
245 |
|
---|
246 | virtio_net_teardown_bufs(virtio_net->rx_buf);
|
---|
247 | virtio_net_teardown_bufs(virtio_net->tx_buf);
|
---|
248 | virtio_net_teardown_bufs(virtio_net->ct_buf);
|
---|
249 |
|
---|
250 | virtio_device_setup_fail(&virtio_net->virtio_dev);
|
---|
251 | virtio_pci_dev_cleanup(&virtio_net->virtio_dev);
|
---|
252 | }
|
---|
253 |
|
---|
254 | static errno_t virtio_net_dev_add(ddf_dev_t *dev)
|
---|
255 | {
|
---|
256 | ddf_msg(LVL_NOTE, "%s %s (handle = %zu)", __func__,
|
---|
257 | ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
|
---|
258 |
|
---|
259 | errno_t rc = virtio_net_initialize(dev);
|
---|
260 | if (rc != EOK)
|
---|
261 | return rc;
|
---|
262 |
|
---|
263 | ddf_fun_t *fun = ddf_fun_create(dev, fun_exposed, "port0");
|
---|
264 | if (fun == NULL) {
|
---|
265 | rc = ENOMEM;
|
---|
266 | goto error;
|
---|
267 | }
|
---|
268 | nic_t *nic = ddf_dev_data_get(dev);
|
---|
269 | nic_set_ddf_fun(nic, fun);
|
---|
270 | ddf_fun_set_ops(fun, &virtio_net_dev_ops);
|
---|
271 |
|
---|
272 | rc = ddf_fun_bind(fun);
|
---|
273 | if (rc != EOK) {
|
---|
274 | ddf_msg(LVL_ERROR, "Failed binding device function");
|
---|
275 | goto uninitialize;
|
---|
276 | }
|
---|
277 |
|
---|
278 | rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
|
---|
279 | if (rc != EOK) {
|
---|
280 | ddf_msg(LVL_ERROR, "Failed adding function to category");
|
---|
281 | goto unbind;
|
---|
282 | }
|
---|
283 |
|
---|
284 | ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
|
---|
285 | ddf_dev_get_name(dev));
|
---|
286 |
|
---|
287 | return EOK;
|
---|
288 |
|
---|
289 | unbind:
|
---|
290 | ddf_fun_unbind(fun);
|
---|
291 | uninitialize:
|
---|
292 | virtio_net_uninitialize(dev);
|
---|
293 | error:
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 | static nic_iface_t virtio_net_nic_iface;
|
---|
298 |
|
---|
299 | int main(void)
|
---|
300 | {
|
---|
301 | printf("%s: HelenOS virtio-net driver\n", NAME);
|
---|
302 |
|
---|
303 | if (nic_driver_init(NAME) != EOK)
|
---|
304 | return 1;
|
---|
305 |
|
---|
306 | nic_driver_implement(&virtio_net_driver_ops, &virtio_net_dev_ops,
|
---|
307 | &virtio_net_nic_iface);
|
---|
308 |
|
---|
309 | (void) ddf_log_init(NAME);
|
---|
310 | return ddf_driver_main(&virtio_net_driver);
|
---|
311 | }
|
---|