source: mainline/uspace/drv/nic/virtio-net/virtio-net.c@ 2dac5a9

Last change on this file since 2dac5a9 was 2dac5a9, checked in by Nataliia Korop <n.corop08@…>, 10 months ago

framework added to virtio-net

  • Property mode set to 100644
File size: 12.8 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/interrupt.h>
37#include <ddf/log.h>
38#include <ops/nic.h>
39#include <pci_dev_iface.h>
40#include <nic/nic.h>
41
42#include <nic.h>
43
44#include <virtio-pci.h>
45#include "pcapdump_iface.h"
46#include "pcap_iface.h"
47#define NAME "virtio-net"
48
49#define VIRTIO_NET_NUM_QUEUES 3
50
51#define RX_QUEUE_1 0
52#define TX_QUEUE_1 1
53#define CT_QUEUE_1 2
54
55#define BUFFER_SIZE 2048
56#define RX_BUF_SIZE BUFFER_SIZE
57#define TX_BUF_SIZE BUFFER_SIZE
58#define CT_BUF_SIZE BUFFER_SIZE
59
60static ddf_dev_ops_t virtio_net_dev_ops;
61
62static errno_t virtio_net_dev_add(ddf_dev_t *dev);
63
64static driver_ops_t virtio_net_driver_ops = {
65 .dev_add = virtio_net_dev_add
66};
67
68static driver_t virtio_net_driver = {
69 .name = NAME,
70 .driver_ops = &virtio_net_driver_ops
71};
72
73/** VirtIO net IRQ handler.
74 *
75 * @param icall IRQ event notification
76 * @param arg Argument (nic_t *)
77 */
78static void virtio_net_irq_handler(ipc_call_t *icall, void *arg)
79{
80 nic_t *nic = (nic_t *)arg;
81 virtio_net_t *virtio_net = nic_get_specific(nic);
82 virtio_dev_t *vdev = &virtio_net->virtio_dev;
83
84 uint16_t descno;
85 uint32_t len;
86 while (virtio_virtq_consume_used(vdev, RX_QUEUE_1, &descno, &len)) {
87 virtio_net_hdr_t *hdr =
88 (virtio_net_hdr_t *) virtio_net->rx_buf[descno];
89 if (len <= sizeof(*hdr)) {
90 ddf_msg(LVL_WARN,
91 "RX data length too short, packet dropped");
92 virtio_virtq_produce_available(vdev, RX_QUEUE_1,
93 descno);
94 continue;
95 }
96
97 nic_frame_t *frame = nic_alloc_frame(nic, len - sizeof(*hdr));
98 if (frame) {
99 memcpy(frame->data, &hdr[1], len - sizeof(*hdr));
100 pcapdump_packet(nic_get_pcap_iface(nic), frame->data, frame->size);
101 nic_received_frame(nic, frame);
102 } else {
103 ddf_msg(LVL_WARN,
104 "Cannot allocate RX frame, packet dropped");
105 }
106
107 virtio_virtq_produce_available(vdev, RX_QUEUE_1, descno);
108 }
109
110 while (virtio_virtq_consume_used(vdev, TX_QUEUE_1, &descno, &len)) {
111 virtio_free_desc(vdev, TX_QUEUE_1, &virtio_net->tx_free_head,
112 descno);
113 }
114 while (virtio_virtq_consume_used(vdev, CT_QUEUE_1, &descno, &len)) {
115 virtio_free_desc(vdev, CT_QUEUE_1, &virtio_net->ct_free_head,
116 descno);
117 }
118}
119
120static errno_t virtio_net_register_interrupt(ddf_dev_t *dev)
121{
122 nic_t *nic = ddf_dev_data_get(dev);
123 virtio_net_t *virtio_net = nic_get_specific(nic);
124 virtio_dev_t *vdev = &virtio_net->virtio_dev;
125
126 hw_res_list_parsed_t res;
127 hw_res_list_parsed_init(&res);
128
129 errno_t rc = nic_get_resources(nic, &res);
130 if (rc != EOK)
131 return rc;
132
133 if (res.irqs.count < 1) {
134 hw_res_list_parsed_clean(&res);
135 rc = EINVAL;
136 return rc;
137 }
138
139 virtio_net->irq = res.irqs.irqs[0];
140 hw_res_list_parsed_clean(&res);
141
142 irq_pio_range_t pio_ranges[] = {
143 {
144 .base = vdev->isr_phys,
145 .size = sizeof(vdev->isr_phys),
146 }
147 };
148
149 irq_cmd_t irq_commands[] = {
150 {
151 .cmd = CMD_PIO_READ_8,
152 .addr = (void *) vdev->isr_phys,
153 .dstarg = 2
154 },
155 {
156 .cmd = CMD_PREDICATE,
157 .value = 1,
158 .srcarg = 2
159 },
160 {
161 .cmd = CMD_ACCEPT
162 }
163 };
164
165 irq_code_t irq_code = {
166 .rangecount = sizeof(pio_ranges) / sizeof(irq_pio_range_t),
167 .ranges = pio_ranges,
168 .cmdcount = sizeof(irq_commands) / sizeof(irq_cmd_t),
169 .cmds = irq_commands
170 };
171
172 return register_interrupt_handler(dev, virtio_net->irq,
173 virtio_net_irq_handler, (void *)nic, &irq_code,
174 &virtio_net->irq_handle);
175}
176
177static errno_t virtio_net_initialize(ddf_dev_t *dev)
178{
179 nic_t *nic = nic_create_and_bind(dev);
180 if (!nic)
181 return ENOMEM;
182
183 virtio_net_t *virtio_net = calloc(1, sizeof(virtio_net_t));
184 if (!virtio_net) {
185 nic_unbind_and_destroy(dev);
186 return ENOMEM;
187 }
188
189 nic_set_specific(nic, virtio_net);
190
191 errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev);
192 if (rc != EOK)
193 return rc;
194
195 virtio_dev_t *vdev = &virtio_net->virtio_dev;
196 virtio_pci_common_cfg_t *cfg = virtio_net->virtio_dev.common_cfg;
197 virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg;
198
199 /*
200 * Register IRQ
201 */
202 rc = virtio_net_register_interrupt(dev);
203 if (rc != EOK)
204 goto fail;
205
206 /* Reset the device and negotiate the feature bits */
207 rc = virtio_device_setup_start(vdev,
208 VIRTIO_NET_F_MAC | VIRTIO_NET_F_CTRL_VQ);
209 if (rc != EOK)
210 goto fail;
211
212 /* Perform device-specific setup */
213
214 /*
215 * Discover and configure the virtqueues
216 */
217 uint16_t num_queues = pio_read_le16(&cfg->num_queues);
218 if (num_queues != VIRTIO_NET_NUM_QUEUES) {
219 ddf_msg(LVL_NOTE, "Unsupported number of virtqueues: %u",
220 num_queues);
221 rc = ELIMIT;
222 goto fail;
223 }
224
225 vdev->queues = calloc(sizeof(virtq_t), num_queues);
226 if (!vdev->queues) {
227 rc = ENOMEM;
228 goto fail;
229 }
230
231 rc = virtio_virtq_setup(vdev, RX_QUEUE_1, RX_BUFFERS);
232 if (rc != EOK)
233 goto fail;
234 rc = virtio_virtq_setup(vdev, TX_QUEUE_1, TX_BUFFERS);
235 if (rc != EOK)
236 goto fail;
237 rc = virtio_virtq_setup(vdev, CT_QUEUE_1, CT_BUFFERS);
238 if (rc != EOK)
239 goto fail;
240
241 /*
242 * Setup DMA buffers
243 */
244 rc = virtio_setup_dma_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
245 virtio_net->rx_buf, virtio_net->rx_buf_p);
246 if (rc != EOK)
247 goto fail;
248 rc = virtio_setup_dma_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
249 virtio_net->tx_buf, virtio_net->tx_buf_p);
250 if (rc != EOK)
251 goto fail;
252 rc = virtio_setup_dma_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
253 virtio_net->ct_buf, virtio_net->ct_buf_p);
254 if (rc != EOK)
255 goto fail;
256
257 /*
258 * Give all RX buffers to the NIC
259 */
260 for (unsigned i = 0; i < RX_BUFFERS; i++) {
261 /*
262 * Associtate the buffer with the descriptor, set length and
263 * flags.
264 */
265 virtio_virtq_desc_set(vdev, RX_QUEUE_1, i,
266 virtio_net->rx_buf_p[i], RX_BUF_SIZE, VIRTQ_DESC_F_WRITE,
267 0);
268 /*
269 * Put the set descriptor into the available ring of the RX
270 * queue.
271 */
272 virtio_virtq_produce_available(vdev, RX_QUEUE_1, i);
273 }
274
275 /*
276 * Put all TX and CT buffers on a free list
277 */
278 virtio_create_desc_free_list(vdev, TX_QUEUE_1, TX_BUFFERS,
279 &virtio_net->tx_free_head);
280 virtio_create_desc_free_list(vdev, CT_QUEUE_1, CT_BUFFERS,
281 &virtio_net->ct_free_head);
282
283 /*
284 * Read the MAC address
285 */
286 nic_address_t nic_addr;
287 for (unsigned i = 0; i < ETH_ADDR; i++)
288 nic_addr.address[i] = pio_read_8(&netcfg->mac[i]);
289 rc = nic_report_address(nic, &nic_addr);
290 if (rc != EOK)
291 goto fail;
292
293 ddf_msg(LVL_NOTE, "MAC address: " PRIMAC, ARGSMAC(nic_addr.address));
294
295 /*
296 * Enable IRQ
297 */
298 rc = hw_res_enable_interrupt(ddf_dev_parent_sess_get(dev),
299 virtio_net->irq);
300 if (rc != EOK) {
301 ddf_msg(LVL_NOTE, "Failed to enable interrupt");
302 goto fail;
303 }
304
305 ddf_msg(LVL_NOTE, "Registered IRQ %d", virtio_net->irq);
306
307 /* Go live */
308 virtio_device_setup_finalize(vdev);
309
310 return EOK;
311
312fail:
313 virtio_teardown_dma_bufs(virtio_net->rx_buf);
314 virtio_teardown_dma_bufs(virtio_net->tx_buf);
315 virtio_teardown_dma_bufs(virtio_net->ct_buf);
316
317 virtio_device_setup_fail(vdev);
318 virtio_pci_dev_cleanup(vdev);
319 return rc;
320}
321
322static void virtio_net_uninitialize(ddf_dev_t *dev)
323{
324 nic_t *nic = ddf_dev_data_get(dev);
325 virtio_net_t *virtio_net = (virtio_net_t *) nic_get_specific(nic);
326
327 virtio_teardown_dma_bufs(virtio_net->rx_buf);
328 virtio_teardown_dma_bufs(virtio_net->tx_buf);
329 virtio_teardown_dma_bufs(virtio_net->ct_buf);
330
331 virtio_device_setup_fail(&virtio_net->virtio_dev);
332 virtio_pci_dev_cleanup(&virtio_net->virtio_dev);
333}
334
335static void virtio_net_send(nic_t *nic, void *data, size_t size)
336{
337 virtio_net_t *virtio_net = nic_get_specific(nic);
338 virtio_dev_t *vdev = &virtio_net->virtio_dev;
339
340 if (size > sizeof(virtio_net) + TX_BUF_SIZE) {
341 ddf_msg(LVL_WARN, "TX data too big, frame dropped");
342 return;
343 }
344
345 uint16_t descno = virtio_alloc_desc(vdev, TX_QUEUE_1,
346 &virtio_net->tx_free_head);
347 if (descno == (uint16_t) -1U) {
348 ddf_msg(LVL_WARN, "No TX buffers available, frame dropped");
349 return;
350 }
351 assert(descno < TX_BUFFERS);
352
353 /* Setup the packet header */
354 virtio_net_hdr_t *hdr = (virtio_net_hdr_t *) virtio_net->tx_buf[descno];
355 memset(hdr, 0, sizeof(virtio_net_hdr_t));
356 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
357 hdr->num_buffers = 0;
358
359 /* Copy packet data into the buffer just past the header */
360 memcpy(&hdr[1], data, size);
361 pcapdump_packet(nic_get_pcap_iface(nic), data, size);
362 /*
363 * Set the descriptor, put it into the virtqueue and notify the device
364 */
365 virtio_virtq_desc_set(vdev, TX_QUEUE_1, descno,
366 virtio_net->tx_buf_p[descno], sizeof(virtio_net_hdr_t) + size, 0, 0);
367 virtio_virtq_produce_available(vdev, TX_QUEUE_1, descno);
368}
369
370static errno_t virtio_net_on_multicast_mode_change(nic_t *nic,
371 nic_multicast_mode_t new_mode, const nic_address_t *address_list,
372 size_t address_count)
373{
374 switch (new_mode) {
375 case NIC_MULTICAST_BLOCKED:
376 nic_report_hw_filtering(nic, -1, 0, -1);
377 return EOK;
378 case NIC_MULTICAST_LIST:
379 nic_report_hw_filtering(nic, -1, 0, -1);
380 return EOK;
381 case NIC_MULTICAST_PROMISC:
382 nic_report_hw_filtering(nic, -1, 0, -1);
383 return EOK;
384 default:
385 return ENOTSUP;
386 }
387 return EOK;
388}
389
390static errno_t virtio_net_on_broadcast_mode_change(nic_t *nic,
391 nic_broadcast_mode_t new_mode)
392{
393 switch (new_mode) {
394 case NIC_BROADCAST_BLOCKED:
395 return ENOTSUP;
396 case NIC_BROADCAST_ACCEPTED:
397 return EOK;
398 default:
399 return ENOTSUP;
400 }
401}
402
403static errno_t virtio_net_dev_add(ddf_dev_t *dev)
404{
405 ddf_msg(LVL_NOTE, "%s %s (handle = %zu)", __func__,
406 ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
407
408 errno_t rc = virtio_net_initialize(dev);
409 if (rc != EOK)
410 return rc;
411
412 ddf_fun_t *fun = ddf_fun_create(dev, fun_exposed, "port0");
413 if (fun == NULL) {
414 rc = ENOMEM;
415 goto uninitialize;
416 }
417 nic_t *nic = ddf_dev_data_get(dev);
418 nic_set_ddf_fun(nic, fun);
419 ddf_fun_set_ops(fun, &virtio_net_dev_ops);
420
421 nic_set_send_frame_handler(nic, virtio_net_send);
422 nic_set_filtering_change_handlers(nic, NULL,
423 virtio_net_on_multicast_mode_change,
424 virtio_net_on_broadcast_mode_change, NULL, NULL);
425
426 rc = ddf_fun_bind(fun);
427 if (rc != EOK) {
428 ddf_msg(LVL_ERROR, "Failed binding device function");
429 goto destroy;
430 }
431
432 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
433 if (rc != EOK) {
434 ddf_msg(LVL_ERROR, "Failed adding function to category");
435 goto unbind;
436 }
437
438 ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
439 ddf_dev_get_name(dev));
440
441 errno_t pcap_rc = pcapdump_init(nic_get_pcap_iface(nic));
442
443 if (pcap_rc != EOK) {
444 printf("Failed creating pcapdump port\n");
445 }
446 rc = ddf_fun_add_to_category(fun, "pcap");
447 if (rc != EOK)
448 goto unbind;
449
450 return EOK;
451
452unbind:
453 ddf_fun_unbind(fun);
454destroy:
455 ddf_fun_destroy(fun);
456uninitialize:
457 virtio_net_uninitialize(dev);
458 return rc;
459}
460
461static errno_t virtio_net_get_device_info(ddf_fun_t *fun,
462 nic_device_info_t *info)
463{
464 nic_t *nic = nic_get_from_ddf_fun(fun);
465 if (!nic)
466 return ENOENT;
467
468 str_cpy(info->vendor_name, sizeof(info->vendor_name), "Red Hat, Inc.");
469 str_cpy(info->model_name, sizeof(info->model_name),
470 "Virtio network device");
471
472 return EOK;
473}
474
475static errno_t virtio_net_get_cable_state(ddf_fun_t *fun,
476 nic_cable_state_t *state)
477{
478 *state = NIC_CS_PLUGGED;
479 return EOK;
480}
481
482static errno_t virtio_net_get_operation_mode(ddf_fun_t *fun, int *speed,
483 nic_channel_mode_t *duplex, nic_role_t *role)
484{
485 *speed = 1000;
486 *duplex = NIC_CM_FULL_DUPLEX;
487 *role = NIC_ROLE_UNKNOWN;
488 return EOK;
489}
490
491static nic_iface_t virtio_net_nic_iface = {
492 .get_device_info = virtio_net_get_device_info,
493 .get_cable_state = virtio_net_get_cable_state,
494 .get_operation_mode = virtio_net_get_operation_mode,
495};
496
497int main(void)
498{
499 printf("%s: HelenOS virtio-net driver\n", NAME);
500
501 if (nic_driver_init(NAME) != EOK)
502 return 1;
503
504 nic_driver_implement(&virtio_net_driver_ops, &virtio_net_dev_ops,
505 &virtio_net_nic_iface);
506
507 (void) ddf_log_init(NAME);
508 return ddf_driver_main(&virtio_net_driver);
509}
Note: See TracBrowser for help on using the repository browser.