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