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