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

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

add_to_cat func in lib

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