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