Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __included_virtio_pci_h__ |
| 17 | #define __included_virtio_pci_h__ |
| 18 | |
| 19 | /* VirtIO ABI version, this must match exactly. */ |
| 20 | #define VIRTIO_PCI_ABI_VERSION 0 |
| 21 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 22 | /* VirtIO device IDs. */ |
| 23 | #define VIRTIO_ID_NETWORK 0x01 |
| 24 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 25 | /* |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 26 | * Vector value used to disable MSI for queue. |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 27 | * define in include/linux/virtio_pci.h |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 28 | */ |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 29 | #define VIRTIO_MSI_NO_VECTOR 0xFFFF |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 30 | |
| 31 | /* The bit of the ISR which indicates a device has an interrupt. */ |
| 32 | #define VIRTIO_PCI_ISR_INTR 0x1 |
| 33 | /* The bit of the ISR which indicates a device configuration change. */ |
| 34 | #define VIRTIO_PCI_ISR_CONFIG 0x2 |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 35 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 36 | /* Status byte for guest to report progress. */ |
| 37 | #define foreach_virtio_config_status_flags \ |
| 38 | _ (VIRTIO_CONFIG_STATUS_RESET, 0x00) \ |
| 39 | _ (VIRTIO_CONFIG_STATUS_ACK, 0x01) \ |
| 40 | _ (VIRTIO_CONFIG_STATUS_DRIVER, 0x02) \ |
| 41 | _ (VIRTIO_CONFIG_STATUS_DRIVER_OK, 0x04) \ |
| 42 | _ (VIRTIO_CONFIG_STATUS_FEATURES_OK, 0x08) \ |
| 43 | _ (VIRTIO_CONFIG_STATUS_DEVICE_NEEDS_RESET, 0x40) \ |
| 44 | _ (VIRTIO_CONFIG_STATUS_FAILED, 0x80) |
| 45 | |
| 46 | typedef enum |
| 47 | { |
| 48 | #define _(a, b) a = b, |
| 49 | foreach_virtio_config_status_flags |
| 50 | #undef _ |
| 51 | } virtio_config_status_flags_t; |
| 52 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 53 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 54 | #define VIRTIO_NET_S_LINK_UP 1 /* Link is up */ |
| 55 | #define VIRTIO_NET_S_ANNOUNCE 2 /* Announcement is needed */ |
| 56 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 57 | #define VIRTIO_NET_OK 0 |
| 58 | #define VIRTIO_NET_ERR 1 |
| 59 | |
| 60 | /* If multiqueue is provided by host, then we support it. */ |
| 61 | #define VIRTIO_NET_CTRL_MQ 4 |
| 62 | #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET 0 |
| 63 | #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN 1 |
| 64 | #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX 0x8000 |
| 65 | |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Control network offloads |
| 68 | * Reconfigures the network offloads that Guest can handle. |
| 69 | * Available with the VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit. |
| 70 | * Command data format matches the feature bit mask exactly. |
| 71 | * See VIRTIO_NET_F_GUEST_* for the list of offloads |
| 72 | * that can be enabled/disabled. |
| 73 | */ |
| 74 | #define VIRTIO_NET_CTRL_GUEST_OFFLOADS 5 |
| 75 | #define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET 0 |
| 76 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 77 | /* Common configuration */ |
| 78 | #define VIRTIO_PCI_CAP_COMMON_CFG 1 |
| 79 | /* Notifications */ |
| 80 | #define VIRTIO_PCI_CAP_NOTIFY_CFG 2 |
| 81 | /* ISR Status */ |
| 82 | #define VIRTIO_PCI_CAP_ISR_CFG 3 |
| 83 | /* Device specific configuration */ |
| 84 | #define VIRTIO_PCI_CAP_DEVICE_CFG 4 |
| 85 | /* PCI configuration access */ |
| 86 | #define VIRTIO_PCI_CAP_PCI_CFG 5 |
| 87 | |
| 88 | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 |
| 89 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 90 | #define VNET_VIRTIO_PCI_VRING_ALIGN 4096 |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 91 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 92 | typedef enum |
| 93 | { |
| 94 | VIRTIO_MSIX_NONE = 0, |
| 95 | VIRTIO_MSIX_DISABLED = 1, |
| 96 | VIRTIO_MSIX_ENABLED = 2 |
| 97 | } virtio_msix_status_t; |
| 98 | |
| 99 | /* This is the PCI capability header: */ |
| 100 | typedef struct |
| 101 | { |
| 102 | u8 cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */ |
| 103 | u8 cap_next; /* Generic PCI field: next ptr. */ |
| 104 | u8 cap_len; /* Generic PCI field: capability length */ |
| 105 | u8 cfg_type; /* Identifies the structure. */ |
| 106 | u8 bar; /* Where to find it. */ |
| 107 | u8 padding[3]; /* Pad to full dword. */ |
| 108 | u32 offset; /* Offset within bar. */ |
| 109 | u32 length; /* Length of the structure, in bytes. */ |
| 110 | } virtio_pci_cap_t; |
| 111 | |
| 112 | typedef struct |
| 113 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 114 | virtio_pci_cap_t cap; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 115 | u32 notify_off_multiplier; /* Multiplier for queue_notify_off. */ |
| 116 | } virtio_pci_notify_cap_t; |
| 117 | |
| 118 | /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */ |
| 119 | typedef struct |
| 120 | { |
| 121 | /* About the whole device. */ |
| 122 | u32 device_feature_select; /* read-write */ |
| 123 | u32 device_feature; /* read-only */ |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 124 | u32 driver_feature_select; /* read-write */ |
| 125 | u32 driver_feature; /* read-write */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 126 | u16 msix_config; /* read-write */ |
| 127 | u16 num_queues; /* read-only */ |
| 128 | u8 device_status; /* read-write */ |
| 129 | u8 config_generation; /* read-only */ |
| 130 | |
| 131 | /* About a specific virtqueue. */ |
| 132 | u16 queue_select; /* read-write */ |
| 133 | u16 queue_size; /* read-write, power of 2. */ |
| 134 | u16 queue_msix_vector; /* read-write */ |
| 135 | u16 queue_enable; /* read-write */ |
| 136 | u16 queue_notify_off; /* read-only */ |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 137 | u64 queue_desc; /* read-write */ |
| 138 | u64 queue_driver; /* read-write */ |
| 139 | u64 queue_device; /* read-write */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 140 | } virtio_pci_common_cfg_t; |
| 141 | |
| 142 | typedef struct |
| 143 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 144 | u8 mac[6]; |
| 145 | u16 status; |
| 146 | u16 max_virtqueue_pairs; |
| 147 | u16 mtu; |
| 148 | } virtio_net_config_t; |
| 149 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 150 | /* |
| 151 | * Control virtqueue data structures |
| 152 | * |
| 153 | * The control virtqueue expects a header in the first sg entry |
| 154 | * and an ack/status response in the last entry. Data for the |
| 155 | * command goes in between. |
| 156 | */ |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 157 | typedef CLIB_PACKED (struct |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 158 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 159 | u8 class; |
| 160 | u8 cmd; |
| 161 | }) virtio_net_ctrl_hdr_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 162 | |
| 163 | typedef u8 virtio_net_ctrl_ack_t; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 164 | |
| 165 | typedef struct |
| 166 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 167 | virtio_net_ctrl_hdr_t ctrl; |
| 168 | virtio_net_ctrl_ack_t status; |
| 169 | u8 data[1024]; |
| 170 | } virtio_ctrl_msg_t; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 171 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 172 | typedef struct _virtio_pci_func |
| 173 | { |
| 174 | void (*read_config) (vlib_main_t * vm, virtio_if_t * vif, void *dst, |
| 175 | int len, u32 addr); |
| 176 | void (*write_config) (vlib_main_t * vm, virtio_if_t * vif, void *src, |
| 177 | int len, u32 addr); |
| 178 | |
| 179 | u64 (*get_device_features) (vlib_main_t * vm, virtio_if_t * vif); |
| 180 | u64 (*get_driver_features) (vlib_main_t * vm, virtio_if_t * vif); |
| 181 | void (*set_driver_features) (vlib_main_t * vm, virtio_if_t * vif, |
| 182 | u64 features); |
| 183 | |
| 184 | u8 (*get_status) (vlib_main_t * vm, virtio_if_t * vif); |
| 185 | void (*set_status) (vlib_main_t * vm, virtio_if_t * vif, u8 status); |
| 186 | u8 (*device_reset) (vlib_main_t * vm, virtio_if_t * vif); |
| 187 | |
| 188 | u8 (*get_isr) (vlib_main_t * vm, virtio_if_t * vif); |
| 189 | |
| 190 | u16 (*get_queue_size) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id); |
| 191 | void (*set_queue_size) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id, |
| 192 | u16 queue_size); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 193 | u8 (*setup_queue) (vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, |
| 194 | vnet_virtio_vring_t *vring); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 195 | void (*del_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id); |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 196 | u16 (*get_queue_notify_off) (vlib_main_t * vm, virtio_if_t * vif, |
| 197 | u16 queue_id); |
| 198 | void (*notify_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id, |
| 199 | u16 queue_notify_offset); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 200 | |
| 201 | u16 (*set_config_irq) (vlib_main_t * vm, virtio_if_t * vif, u16 vec); |
| 202 | u16 (*set_queue_irq) (vlib_main_t * vm, virtio_if_t * vif, u16 vec, |
| 203 | u16 queue_id); |
| 204 | |
| 205 | void (*get_mac) (vlib_main_t * vm, virtio_if_t * vif); |
| 206 | void (*set_mac) (vlib_main_t * vm, virtio_if_t * vif); |
| 207 | u16 (*get_device_status) (vlib_main_t * vm, virtio_if_t * vif); |
| 208 | u16 (*get_max_queue_pairs) (vlib_main_t * vm, virtio_if_t * vif); |
| 209 | u16 (*get_mtu) (vlib_main_t * vm, virtio_if_t * vif); |
| 210 | void (*device_debug_config_space) (vlib_main_t * vm, virtio_if_t * vif); |
| 211 | } virtio_pci_func_t; |
| 212 | |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 213 | #define foreach_virtio_flags \ |
| 214 | _ (GSO, 0) \ |
| 215 | _ (CSUM_OFFLOAD, 1) \ |
| 216 | _ (GRO_COALESCE, 2) \ |
| 217 | _ (PACKED, 3) \ |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 218 | _ (IN_ORDER, 4) \ |
| 219 | _ (BUFFERING, 5) |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 220 | |
| 221 | typedef enum |
| 222 | { |
| 223 | #define _(a, b) VIRTIO_FLAG_##a = (1 << b), |
| 224 | foreach_virtio_flags |
| 225 | #undef _ |
| 226 | } virtio_flag_t; |
| 227 | |
Benoît Ganne | c04d8c4 | 2022-10-13 14:01:03 +0200 | [diff] [blame] | 228 | typedef enum |
| 229 | { |
| 230 | VIRTIO_BIND_NONE = 0, |
| 231 | VIRTIO_BIND_DEFAULT = 1, |
| 232 | VIRTIO_BIND_FORCE = 2, |
| 233 | } __clib_packed virtio_bind_t; |
| 234 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 235 | typedef struct |
| 236 | { |
| 237 | u32 addr; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 238 | /* return */ |
| 239 | i32 rv; |
| 240 | u32 sw_if_index; |
| 241 | u8 mac_addr_set; |
| 242 | u8 mac_addr[6]; |
| 243 | u64 features; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 244 | u8 gso_enabled; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 245 | u8 checksum_offload_enabled; |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 246 | u32 tx_queue_size; |
Benoît Ganne | c04d8c4 | 2022-10-13 14:01:03 +0200 | [diff] [blame] | 247 | virtio_bind_t bind; |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 248 | u32 buffering_size; |
Mohsin Kazmi | 518251b | 2020-09-01 17:17:44 +0000 | [diff] [blame] | 249 | u32 virtio_flags; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 250 | clib_error_t *error; |
| 251 | } virtio_pci_create_if_args_t; |
| 252 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 253 | extern const virtio_pci_func_t virtio_pci_legacy_func; |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 254 | extern const virtio_pci_func_t virtio_pci_modern_func; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 255 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 256 | extern void device_status (vlib_main_t * vm, virtio_if_t * vif); |
| 257 | void virtio_pci_create_if (vlib_main_t * vm, |
| 258 | virtio_pci_create_if_args_t * args); |
| 259 | int virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * ad); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 260 | int virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif, |
| 261 | int gso_enabled, |
| 262 | int checksum_offload_enabled, |
| 263 | int offloads_disabled); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 264 | #endif /* __included_virtio_pci_h__ */ |
| 265 | /* |
| 266 | * fd.io coding-style-patch-verification: ON |
| 267 | * |
| 268 | * Local Variables: |
| 269 | * eval: (c-set-style "gnu") |
| 270 | * End: |
| 271 | */ |