[033ebc25] | 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 |
|
---|
[1c40351] | 29 | #include "virtio-net.h"
|
---|
| 30 |
|
---|
[6413967] | 31 | #include <stdio.h>
|
---|
[69927fa] | 32 | #include <stdint.h>
|
---|
[6413967] | 33 |
|
---|
[9364ced] | 34 | #include <as.h>
|
---|
[6413967] | 35 | #include <ddf/driver.h>
|
---|
[fe96085] | 36 | #include <ddf/interrupt.h>
|
---|
[6413967] | 37 | #include <ddf/log.h>
|
---|
| 38 | #include <ops/nic.h>
|
---|
[69927fa] | 39 | #include <pci_dev_iface.h>
|
---|
[6a0f1309] | 40 | #include <nic/nic.h>
|
---|
[6413967] | 41 |
|
---|
| 42 | #include <nic.h>
|
---|
| 43 |
|
---|
[00192cde] | 44 | #include <virtio-pci.h>
|
---|
[6eab537d] | 45 | #include <pcapdump_iface.h>
|
---|
| 46 |
|
---|
[00192cde] | 47 | #define NAME "virtio-net"
|
---|
[69927fa] | 48 |
|
---|
[cbcb34c] | 49 | #define VIRTIO_NET_NUM_QUEUES 3
|
---|
| 50 |
|
---|
[9364ced] | 51 | #define RX_QUEUE_1 0
|
---|
| 52 | #define TX_QUEUE_1 1
|
---|
| 53 | #define CT_QUEUE_1 2
|
---|
[cbcb34c] | 54 |
|
---|
[9364ced] | 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 |
|
---|
[211b17a1] | 60 | static ddf_dev_ops_t virtio_net_dev_ops;
|
---|
| 61 |
|
---|
| 62 | static errno_t virtio_net_dev_add(ddf_dev_t *dev);
|
---|
| 63 |
|
---|
| 64 | static driver_ops_t virtio_net_driver_ops = {
|
---|
| 65 | .dev_add = virtio_net_dev_add
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | static driver_t virtio_net_driver = {
|
---|
| 69 | .name = NAME,
|
---|
| 70 | .driver_ops = &virtio_net_driver_ops
|
---|
| 71 | };
|
---|
[9364ced] | 72 |
|
---|
[b8ef198b] | 73 | static 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)) {
|
---|
[331d024] | 105 | virtio_free_desc(vdev, TX_QUEUE_1, &virtio_net->tx_free_head,
|
---|
| 106 | descno);
|
---|
[b8ef198b] | 107 | }
|
---|
| 108 | while (virtio_virtq_consume_used(vdev, CT_QUEUE_1, &descno, &len)) {
|
---|
[331d024] | 109 | virtio_free_desc(vdev, CT_QUEUE_1, &virtio_net->ct_free_head,
|
---|
| 110 | descno);
|
---|
[b8ef198b] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[fe96085] | 114 | static 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 |
|
---|
[1c40351] | 170 | static errno_t virtio_net_initialize(ddf_dev_t *dev)
|
---|
| 171 | {
|
---|
[fe96085] | 172 | nic_t *nic = nic_create_and_bind(dev);
|
---|
| 173 | if (!nic)
|
---|
[1c40351] | 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 |
|
---|
[fe96085] | 182 | nic_set_specific(nic, virtio_net);
|
---|
[1c40351] | 183 |
|
---|
| 184 | errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev);
|
---|
| 185 | if (rc != EOK)
|
---|
| 186 | return rc;
|
---|
| 187 |
|
---|
[cbcb34c] | 188 | virtio_dev_t *vdev = &virtio_net->virtio_dev;
|
---|
[1c40351] | 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 |
|
---|
[fe96085] | 192 | /*
|
---|
| 193 | * Register IRQ
|
---|
| 194 | */
|
---|
| 195 | rc = virtio_net_register_interrupt(dev);
|
---|
| 196 | if (rc != EOK)
|
---|
| 197 | goto fail;
|
---|
| 198 |
|
---|
[341df5f] | 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)
|
---|
[1d0620b] | 203 | goto fail;
|
---|
[1c40351] | 204 |
|
---|
[341df5f] | 205 | /* Perform device-specific setup */
|
---|
[cbcb34c] | 206 |
|
---|
| 207 | /*
|
---|
| 208 | * Discover and configure the virtqueues
|
---|
| 209 | */
|
---|
[9af56b6] | 210 | uint16_t num_queues = pio_read_le16(&cfg->num_queues);
|
---|
[cbcb34c] | 211 | if (num_queues != VIRTIO_NET_NUM_QUEUES) {
|
---|
| 212 | ddf_msg(LVL_NOTE, "Unsupported number of virtqueues: %u",
|
---|
| 213 | num_queues);
|
---|
[8fed3ef] | 214 | rc = ELIMIT;
|
---|
[cbcb34c] | 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 |
|
---|
[9364ced] | 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 | */
|
---|
[1e472ee] | 237 | rc = virtio_setup_dma_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
|
---|
[9364ced] | 238 | virtio_net->rx_buf, virtio_net->rx_buf_p);
|
---|
[cbcb34c] | 239 | if (rc != EOK)
|
---|
| 240 | goto fail;
|
---|
[1e472ee] | 241 | rc = virtio_setup_dma_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
|
---|
[9364ced] | 242 | virtio_net->tx_buf, virtio_net->tx_buf_p);
|
---|
[cbcb34c] | 243 | if (rc != EOK)
|
---|
| 244 | goto fail;
|
---|
[1e472ee] | 245 | rc = virtio_setup_dma_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
|
---|
[9364ced] | 246 | virtio_net->ct_buf, virtio_net->ct_buf_p);
|
---|
[cbcb34c] | 247 | if (rc != EOK)
|
---|
| 248 | goto fail;
|
---|
| 249 |
|
---|
[5dddac1] | 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 | */
|
---|
[3d135e9] | 258 | virtio_virtq_desc_set(vdev, RX_QUEUE_1, i,
|
---|
[5dddac1] | 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 |
|
---|
[b085bbaa] | 268 | /*
|
---|
| 269 | * Put all TX and CT buffers on a free list
|
---|
| 270 | */
|
---|
[331d024] | 271 | virtio_create_desc_free_list(vdev, TX_QUEUE_1, TX_BUFFERS,
|
---|
[b085bbaa] | 272 | &virtio_net->tx_free_head);
|
---|
[331d024] | 273 | virtio_create_desc_free_list(vdev, CT_QUEUE_1, CT_BUFFERS,
|
---|
[b085bbaa] | 274 | &virtio_net->ct_free_head);
|
---|
| 275 |
|
---|
[cbcb34c] | 276 | /*
|
---|
| 277 | * Read the MAC address
|
---|
| 278 | */
|
---|
[1c40351] | 279 | nic_address_t nic_addr;
|
---|
[6a0f1309] | 280 | for (unsigned i = 0; i < ETH_ADDR; i++)
|
---|
[cbcb34c] | 281 | nic_addr.address[i] = pio_read_8(&netcfg->mac[i]);
|
---|
[fe96085] | 282 | rc = nic_report_address(nic, &nic_addr);
|
---|
[1d0620b] | 283 | if (rc != EOK)
|
---|
| 284 | goto fail;
|
---|
[1c40351] | 285 |
|
---|
[ea6840d] | 286 | ddf_msg(LVL_NOTE, "MAC address: " PRIMAC, ARGSMAC(nic_addr.address));
|
---|
[1c40351] | 287 |
|
---|
[fe96085] | 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 |
|
---|
[341df5f] | 300 | /* Go live */
|
---|
| 301 | virtio_device_setup_finalize(vdev);
|
---|
[1c40351] | 302 |
|
---|
| 303 | return EOK;
|
---|
[1d0620b] | 304 |
|
---|
| 305 | fail:
|
---|
[1e472ee] | 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);
|
---|
[9364ced] | 309 |
|
---|
[341df5f] | 310 | virtio_device_setup_fail(vdev);
|
---|
| 311 | virtio_pci_dev_cleanup(vdev);
|
---|
[1d0620b] | 312 | return rc;
|
---|
[1c40351] | 313 | }
|
---|
| 314 |
|
---|
[211b17a1] | 315 | static 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 |
|
---|
[1e472ee] | 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);
|
---|
[211b17a1] | 323 |
|
---|
| 324 | virtio_device_setup_fail(&virtio_net->virtio_dev);
|
---|
| 325 | virtio_pci_dev_cleanup(&virtio_net->virtio_dev);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[121ee4fd] | 328 | static void virtio_net_send(nic_t *nic, void *data, size_t size)
|
---|
| 329 | {
|
---|
[3d135e9] | 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 |
|
---|
[331d024] | 338 | uint16_t descno = virtio_alloc_desc(vdev, TX_QUEUE_1,
|
---|
[3d135e9] | 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 |
|
---|
[b71ec66] | 346 | /* Setup the packet header */
|
---|
[3d135e9] | 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;
|
---|
[a1f173d] | 350 | hdr->num_buffers = 0;
|
---|
[3d135e9] | 351 |
|
---|
[b8ef198b] | 352 | /* Copy packet data into the buffer just past the header */
|
---|
[3d135e9] | 353 | memcpy(&hdr[1], data, size);
|
---|
[6eab537d] | 354 |
|
---|
[3d135e9] | 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,
|
---|
[4810acf] | 359 | virtio_net->tx_buf_p[descno], sizeof(virtio_net_hdr_t) + size, 0, 0);
|
---|
[3d135e9] | 360 | virtio_virtq_produce_available(vdev, TX_QUEUE_1, descno);
|
---|
[121ee4fd] | 361 | }
|
---|
| 362 |
|
---|
[5609d3c] | 363 | static 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 |
|
---|
[0d3bfb9e] | 383 | static 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 |
|
---|
[6413967] | 396 | static 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 |
|
---|
[1c40351] | 401 | errno_t rc = virtio_net_initialize(dev);
|
---|
[69927fa] | 402 | if (rc != EOK)
|
---|
| 403 | return rc;
|
---|
| 404 |
|
---|
[211b17a1] | 405 | ddf_fun_t *fun = ddf_fun_create(dev, fun_exposed, "port0");
|
---|
| 406 | if (fun == NULL) {
|
---|
| 407 | rc = ENOMEM;
|
---|
[8fed3ef] | 408 | goto uninitialize;
|
---|
[211b17a1] | 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 |
|
---|
[121ee4fd] | 414 | nic_set_send_frame_handler(nic, virtio_net_send);
|
---|
[5609d3c] | 415 | nic_set_filtering_change_handlers(nic, NULL,
|
---|
| 416 | virtio_net_on_multicast_mode_change,
|
---|
[0d3bfb9e] | 417 | virtio_net_on_broadcast_mode_change, NULL, NULL);
|
---|
| 418 |
|
---|
[211b17a1] | 419 | rc = ddf_fun_bind(fun);
|
---|
| 420 | if (rc != EOK) {
|
---|
| 421 | ddf_msg(LVL_ERROR, "Failed binding device function");
|
---|
[8fed3ef] | 422 | goto destroy;
|
---|
[211b17a1] | 423 | }
|
---|
[6413967] | 424 |
|
---|
[e55d2c1] | 425 | rc = nic_fun_add_to_cats(fun);
|
---|
[211b17a1] | 426 | if (rc != EOK) {
|
---|
[e55d2c1] | 427 | ddf_msg(LVL_ERROR, "Failed adding function to categories");
|
---|
| 428 | ddf_fun_unbind(fun);
|
---|
| 429 | return rc;
|
---|
[211b17a1] | 430 | }
|
---|
[6413967] | 431 |
|
---|
[211b17a1] | 432 | ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
|
---|
| 433 | ddf_dev_get_name(dev));
|
---|
[6413967] | 434 |
|
---|
[211b17a1] | 435 | return EOK;
|
---|
| 436 |
|
---|
[e55d2c1] | 437 | // unbind:
|
---|
| 438 | // ddf_fun_unbind(fun);
|
---|
[8fed3ef] | 439 | destroy:
|
---|
| 440 | ddf_fun_destroy(fun);
|
---|
[211b17a1] | 441 | uninitialize:
|
---|
| 442 | virtio_net_uninitialize(dev);
|
---|
| 443 | return rc;
|
---|
| 444 | }
|
---|
[6413967] | 445 |
|
---|
[0a087ae] | 446 | static errno_t virtio_net_get_device_info(ddf_fun_t *fun,
|
---|
| 447 | nic_device_info_t *info)
|
---|
| 448 | {
|
---|
[fe96085] | 449 | nic_t *nic = nic_get_from_ddf_fun(fun);
|
---|
| 450 | if (!nic)
|
---|
[0a087ae] | 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 |
|
---|
| 460 | static 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 |
|
---|
| 467 | static 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 |
|
---|
| 476 | static 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 | };
|
---|
[6413967] | 481 |
|
---|
[033ebc25] | 482 | int main(void)
|
---|
| 483 | {
|
---|
[6413967] | 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);
|
---|
[033ebc25] | 494 | }
|
---|