Changeset 1c40351 in mainline
- Timestamp:
- 2018-05-22T19:06:50Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1d0620b
- Parents:
- f175d22
- git-author:
- Jakub Jermar <jakub@…> (2018-04-23 21:00:57)
- git-committer:
- Jakub Jermar <jakub@…> (2018-05-22 19:06:50)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/virtio-net/virtio-net.c
rf175d22 r1c40351 27 27 */ 28 28 29 #include "virtio-net.h" 30 29 31 #include <stdio.h> 30 32 #include <stdint.h> … … 41 43 #define NAME "virtio-net" 42 44 45 static errno_t virtio_net_initialize(ddf_dev_t *dev) 46 { 47 nic_t *nic_data = nic_create_and_bind(dev); 48 if (!nic_data) 49 return ENOMEM; 50 51 virtio_net_t *virtio_net = calloc(1, sizeof(virtio_net_t)); 52 if (!virtio_net) { 53 nic_unbind_and_destroy(dev); 54 return ENOMEM; 55 } 56 57 nic_set_specific(nic_data, virtio_net); 58 59 errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev); 60 if (rc != EOK) 61 return rc; 62 63 virtio_pci_common_cfg_t *cfg = virtio_net->virtio_dev.common_cfg; 64 virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg; 65 66 /* 67 * Perform device initialization as described in section 3.1.1 of the 68 * specification. 69 */ 70 71 /* 1. Reset the device */ 72 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_RESET); 73 74 /* 2. Acknowledge we found the device */ 75 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_ACKNOWLEDGE); 76 77 /* 3. We know how to drive the device */ 78 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_DRIVER); 79 80 /* 4. Read the offered feature flags */ 81 pio_write_32(&cfg->device_feature_select, VIRTIO_NET_F_SELECT_PAGE_0); 82 uint32_t features = pio_read_32(&cfg->device_feature); 83 84 ddf_msg(LVL_NOTE, "offered features %x", features); 85 features &= (1U << VIRTIO_NET_F_MAC); 86 87 if (!features) { 88 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_FAILED); 89 return ENOTSUP; 90 } 91 92 /* 4. Write the accepted feature flags */ 93 pio_write_32(&cfg->driver_feature_select, VIRTIO_NET_F_SELECT_PAGE_0); 94 pio_write_32(&cfg->driver_feature, features); 95 96 /* 5. Set FEATURES_OK */ 97 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_FEATURES_OK); 98 99 /* 6. Test if the device supports our feature subset */ 100 uint8_t status = pio_read_8(&cfg->device_status); 101 if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK)) { 102 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_FAILED); 103 return ENOTSUP; 104 } 105 106 /* 7. Perform device-specific setup */ 107 nic_address_t nic_addr; 108 for (unsigned i = 0; i < 6; i++) 109 nic_addr.address[i] = netcfg->mac[i]; 110 rc = nic_report_address(nic_data, &nic_addr); 111 if (rc != EOK) { 112 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_FAILED); 113 return rc; 114 } 115 116 ddf_msg(LVL_NOTE, "MAC address: %02x:%02x:%02x:%02x:%02x:%02x", 117 nic_addr.address[0], nic_addr.address[1], 118 nic_addr.address[2], nic_addr.address[3], 119 nic_addr.address[4], nic_addr.address[5]); 120 121 /* 8. Go live */ 122 pio_write_8(&cfg->device_status, VIRTIO_DEV_STATUS_DRIVER_OK); 123 124 return EOK; 125 } 126 43 127 static errno_t virtio_net_dev_add(ddf_dev_t *dev) 44 128 { … … 46 130 ddf_dev_get_name(dev), ddf_dev_get_handle(dev)); 47 131 48 // XXX: this will be part of the nic data 49 virtio_dev_t virtio_dev; 50 errno_t rc = virtio_pci_dev_initialize(dev, &virtio_dev); 132 errno_t rc = virtio_net_initialize(dev); 51 133 if (rc != EOK) 52 134 return rc;
Note:
See TracChangeset
for help on using the changeset viewer.