source: mainline/uspace/drv/nic/virtio-net/virtio-net.c@ 9ce251c7

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

moved to nic, fixed all except addressing

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