Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 9364ced in mainline


Ignore:
Timestamp:
2018-05-22T19:06:50Z (3 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master
Children:
c1ba24a
Parents:
5b5c286
git-author:
Jakub Jermar <jakub@…> (2018-05-20 10:35:33)
git-committer:
Jakub Jermar <jakub@…> (2018-05-22 19:06:50)
Message:

virtio-net: Setup DMA buffers

Location:
uspace/drv/nic/virtio-net
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/nic/virtio-net/virtio-net.c

    r5b5c286 r9364ced  
    3232#include <stdint.h>
    3333
     34#include <as.h>
    3435#include <ddf/driver.h>
    3536#include <ddf/log.h>
     
    4546#define VIRTIO_NET_NUM_QUEUES   3
    4647
    47 #define RECVQ1          0
    48 #define TRANSQ1         1
    49 #define CTRLQ1          2
    50 
    51 #define RECVQ_SIZE      8
    52 #define TRANSQ_SIZE     8
    53 #define CTRLQ_SIZE      4
     48#define RX_QUEUE_1      0
     49#define TX_QUEUE_1      1
     50#define CT_QUEUE_1      2
     51
     52#define BUFFER_SIZE     2048
     53#define RX_BUF_SIZE     BUFFER_SIZE
     54#define TX_BUF_SIZE     BUFFER_SIZE
     55#define CT_BUF_SIZE     BUFFER_SIZE
     56
     57
     58static errno_t virtio_net_setup_bufs(unsigned int buffers, size_t size,
     59    bool write, void *buf[], uintptr_t buf_p[])
     60{
     61        /*
     62         * Allocate all buffers at once in one large chung.
     63         */
     64        void *virt = AS_AREA_ANY;
     65        uintptr_t phys;
     66        errno_t rc = dmamem_map_anonymous(buffers * size, 0,
     67            write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
     68        if (rc != EOK)
     69                return rc;
     70
     71        ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
     72
     73        /*
     74         * Calculate addresses of the individual buffers for easy access.
     75         */
     76        for (unsigned i = 0; i < buffers; i++) {
     77                buf[i] = virt + i * size;
     78                buf_p[i] = phys + i * size;
     79        }
     80
     81        return EOK;
     82}
     83
     84static void virtio_net_teardown_bufs(void *buf[])
     85{
     86        if (buf[0]) {
     87                dmamem_unmap_anonymous(buf[0]);
     88                buf[0] = NULL;
     89        }
     90}
    5491
    5592static errno_t virtio_net_initialize(ddf_dev_t *dev)
     
    99136        }
    100137
    101         rc = virtio_virtq_setup(vdev, RECVQ1, RECVQ_SIZE);
    102         if (rc != EOK)
    103                 goto fail;
    104         rc = virtio_virtq_setup(vdev, TRANSQ1, TRANSQ_SIZE);
    105         if (rc != EOK)
    106                 goto fail;
    107         rc = virtio_virtq_setup(vdev, CTRLQ1, CTRLQ_SIZE);
     138        rc = virtio_virtq_setup(vdev, RX_QUEUE_1, RX_BUFFERS);
     139        if (rc != EOK)
     140                goto fail;
     141        rc = virtio_virtq_setup(vdev, TX_QUEUE_1, TX_BUFFERS);
     142        if (rc != EOK)
     143                goto fail;
     144        rc = virtio_virtq_setup(vdev, CT_QUEUE_1, CT_BUFFERS);
     145        if (rc != EOK)
     146                goto fail;
     147
     148        /*
     149         * Setup DMA buffers
     150         */
     151        rc = virtio_net_setup_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
     152            virtio_net->rx_buf, virtio_net->rx_buf_p);
     153        if (rc != EOK)
     154                goto fail;
     155        rc = virtio_net_setup_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
     156            virtio_net->tx_buf, virtio_net->tx_buf_p);
     157        if (rc != EOK)
     158                goto fail;
     159        rc = virtio_net_setup_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
     160            virtio_net->ct_buf, virtio_net->ct_buf_p);
    108161        if (rc != EOK)
    109162                goto fail;
     
    129182
    130183fail:
     184        virtio_net_teardown_bufs(virtio_net->rx_buf);
     185        virtio_net_teardown_bufs(virtio_net->tx_buf);
     186        virtio_net_teardown_bufs(virtio_net->ct_buf);
     187
    131188        virtio_device_setup_fail(vdev);
    132189        virtio_pci_dev_cleanup(vdev);
  • uspace/drv/nic/virtio-net/virtio-net.h

    r5b5c286 r9364ced  
    3232#include <virtio-pci.h>
    3333
     34#define RX_BUFFERS      8
     35#define TX_BUFFERS      8
     36#define CT_BUFFERS      4
     37
    3438/** Device handles packets with partial checksum. */
    3539#define VIRTIO_NET_F_CSUM               (1U << 0)
     
    4751typedef struct {
    4852        virtio_dev_t virtio_dev;
     53        void *rx_buf[RX_BUFFERS];
     54        uintptr_t rx_buf_p[RX_BUFFERS];
     55        void *tx_buf[TX_BUFFERS];
     56        uintptr_t tx_buf_p[TX_BUFFERS];
     57        void *ct_buf[CT_BUFFERS];
     58        uintptr_t ct_buf_p[CT_BUFFERS];
    4959} virtio_net_t;
    5060
Note: See TracChangeset for help on using the changeset viewer.