Changeset 341df5f in mainline


Ignore:
Timestamp:
2018-05-22T19:06:50Z (6 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2d4faf7
Parents:
cbcb34c
git-author:
Jakub Jermar <jakub@…> (2018-05-05 20:07:00)
git-committer:
Jakub Jermar <jakub@…> (2018-05-22 19:06:50)
Message:

Factor our generic virtio device initialization

Location:
uspace
Files:
4 edited

Legend:

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

    rcbcb34c r341df5f  
    7575        virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg;
    7676
    77         /*
    78          * Perform device initialization as described in section 3.1.1 of the
    79          * specification.
    80          */
     77        /* Reset the device and negotiate the feature bits */
     78        rc = virtio_device_setup_start(vdev,
     79            VIRTIO_NET_F_MAC | VIRTIO_NET_F_CTRL_VQ);
     80        if (rc != EOK)
     81                goto fail;
    8182
    82         /* 1. Reset the device */
    83         uint8_t status = VIRTIO_DEV_STATUS_RESET;
    84         pio_write_8(&cfg->device_status, status);
    85 
    86         /* 2. Acknowledge we found the device */
    87         status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
    88         pio_write_8(&cfg->device_status, status);
    89 
    90         /* 3. We know how to drive the device */
    91         status |= VIRTIO_DEV_STATUS_DRIVER;
    92         pio_write_8(&cfg->device_status, status);
    93 
    94         /* 4. Read the offered feature flags */
    95         pio_write_32(&cfg->device_feature_select, VIRTIO_NET_F_SELECT_PAGE_0);
    96         uint32_t features = pio_read_32(&cfg->device_feature);
    97 
    98         ddf_msg(LVL_NOTE, "offered features %x", features);
    99         features &= (1U << VIRTIO_NET_F_MAC) | (1U << VIRTIO_NET_F_CTRL_VQ);
    100 
    101         if (!features) {
    102                 rc = ENOTSUP;
    103                 goto fail;
    104         }
    105 
    106         /* 4. Write the accepted feature flags */
    107         pio_write_32(&cfg->driver_feature_select, VIRTIO_NET_F_SELECT_PAGE_0);
    108         pio_write_32(&cfg->driver_feature, features);
    109 
    110         ddf_msg(LVL_NOTE, "accepted features %x", features);
    111 
    112         /* 5. Set FEATURES_OK */
    113         status |= VIRTIO_DEV_STATUS_FEATURES_OK;
    114         pio_write_8(&cfg->device_status, status);
    115 
    116         /* 6. Test if the device supports our feature subset */
    117         status = pio_read_8(&cfg->device_status);
    118         if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK)) {
    119                 rc = ENOTSUP;
    120                 goto fail;
    121         }
    122 
    123         /* 7. Perform device-specific setup */
     83        /* Perform device-specific setup */
    12484
    12585        /*
     
    164124            nic_addr.address[3], nic_addr.address[4], nic_addr.address[5]);
    165125
    166         /* 8. Go live */
    167         status |= VIRTIO_DEV_STATUS_DRIVER_OK;
    168         pio_write_8(&cfg->device_status, status);
     126        /* Go live */
     127        virtio_device_setup_finalize(vdev);
    169128
    170129        return EOK;
    171130
    172131fail:
    173         status |= VIRTIO_DEV_STATUS_FAILED;
    174         pio_write_8(&cfg->device_status, status);
    175         virtio_pci_dev_cleanup(&virtio_net->virtio_dev);
     132        virtio_device_setup_fail(vdev);
     133        virtio_pci_dev_cleanup(vdev);
    176134        return rc;
    177135}
  • uspace/drv/nic/virtio-net/virtio-net.h

    rcbcb34c r341df5f  
    3232#include <virtio-pci.h>
    3333
    34 #define VIRTIO_NET_F_SELECT_PAGE_0      0
    35 
    3634/** Device handles packets with partial checksum. */
    37 #define VIRTIO_NET_F_CSUM               0
     35#define VIRTIO_NET_F_CSUM               (1U << 0)
    3836/** Driver handles packets with partial checksum. */
    39 #define VIRTIO_NET_F_GUEST_CSUM         2
     37#define VIRTIO_NET_F_GUEST_CSUM         (1U << 2)
    4038/** Device has given MAC address. */
    41 #define VIRTIO_NET_F_MAC                5
     39#define VIRTIO_NET_F_MAC                (1U << 5)
    4240/** Control channel is available */
    43 #define VIRTIO_NET_F_CTRL_VQ            17
     41#define VIRTIO_NET_F_CTRL_VQ            (1U << 17)
    4442
    4543typedef struct {
  • uspace/lib/virtio/virtio-pci.c

    rcbcb34c r341df5f  
    272272}
    273273
     274/**
     275 * Perform device initialization as described in section 3.1.1 of the
     276 * specification, steps 1 - 6.
     277 */
     278errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
     279{
     280        virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
     281
     282        /* 1. Reset the device */
     283        uint8_t status = VIRTIO_DEV_STATUS_RESET;
     284        pio_write_8(&cfg->device_status, status);
     285
     286        /* 2. Acknowledge we found the device */
     287        status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
     288        pio_write_8(&cfg->device_status, status);
     289
     290        /* 3. We know how to drive the device */
     291        status |= VIRTIO_DEV_STATUS_DRIVER;
     292        pio_write_8(&cfg->device_status, status);
     293
     294        /* 4. Read the offered feature flags */
     295        pio_write_32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
     296        uint32_t device_features = pio_read_32(&cfg->device_feature);
     297
     298        ddf_msg(LVL_NOTE, "offered features %x", device_features);
     299        features &= device_features;
     300
     301        if (!features)
     302                return ENOTSUP;
     303
     304        /* 4. Write the accepted feature flags */
     305        pio_write_32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
     306        pio_write_32(&cfg->driver_feature, features);
     307
     308        ddf_msg(LVL_NOTE, "accepted features %x", features);
     309
     310        /* 5. Set FEATURES_OK */
     311        status |= VIRTIO_DEV_STATUS_FEATURES_OK;
     312        pio_write_8(&cfg->device_status, status);
     313
     314        /* 6. Test if the device supports our feature subset */
     315        status = pio_read_8(&cfg->device_status);
     316        if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
     317                return ENOTSUP;
     318
     319        return EOK;
     320}
     321
     322/**
     323 * Perform device initialization as described in section 3.1.1 of the
     324 * specification, step 8 (go live).
     325 */
     326void virtio_device_setup_finalize(virtio_dev_t *vdev)
     327{
     328        virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
     329
     330        /* 8. Go live */
     331        uint8_t status = pio_read_8(&cfg->device_status);
     332        pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
     333}
     334
     335void virtio_device_setup_fail(virtio_dev_t *vdev)
     336{
     337        virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
     338
     339        uint8_t status = pio_read_8(&cfg->device_status);
     340        pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
     341}
     342
    274343errno_t virtio_pci_dev_initialize(ddf_dev_t *dev, virtio_dev_t *vdev)
    275344{
  • uspace/lib/virtio/virtio-pci.h

    rcbcb34c r341df5f  
    5757#define VIRTIO_DEV_STATUS_DEVICE_NEEDS_RESET    64
    5858#define VIRTIO_DEV_STATUS_FAILED                128
     59
     60#define VIRTIO_FEATURES_0_31    0
    5961
    6062/** Common configuration structure layout according to VIRTIO version 1.0 */
     
    179181extern void virtio_virtq_teardown(virtio_dev_t *, uint16_t);
    180182
     183extern errno_t virtio_device_setup_start(virtio_dev_t *, uint32_t);
     184extern void virtio_device_setup_fail(virtio_dev_t *);
     185extern void virtio_device_setup_finalize(virtio_dev_t *);
     186
    181187extern errno_t virtio_pci_dev_initialize(ddf_dev_t *, virtio_dev_t *);
    182188extern errno_t virtio_pci_dev_cleanup(virtio_dev_t *);
Note: See TracChangeset for help on using the changeset viewer.