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 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 18 | |
| 19 | #include <vppinfra/types.h> |
| 20 | #include <vlib/vlib.h> |
| 21 | #include <vlib/pci/pci.h> |
| 22 | #include <vnet/ethernet/ethernet.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 23 | #include <vnet/ip/ip4_packet.h> |
| 24 | #include <vnet/ip/ip6_packet.h> |
| 25 | #include <vnet/devices/virtio/virtio.h> |
| 26 | #include <vnet/devices/virtio/pci.h> |
| 27 | |
| 28 | #define PCI_VENDOR_ID_VIRTIO 0x1af4 |
| 29 | #define PCI_DEVICE_ID_VIRTIO_NIC 0x1000 |
| 30 | /* Doesn't support modern device */ |
| 31 | #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041 |
| 32 | |
| 33 | #define PCI_CAPABILITY_LIST 0x34 |
| 34 | #define PCI_CAP_ID_VNDR 0x09 |
| 35 | #define PCI_CAP_ID_MSIX 0x11 |
| 36 | |
| 37 | #define PCI_MSIX_ENABLE 0x8000 |
| 38 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 39 | #define PCI_CONFIG_SIZE(vif) ((vif->msix_enabled == VIRTIO_MSIX_ENABLED) ? \ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 40 | 24 : 20) |
| 41 | |
| 42 | static pci_device_id_t virtio_pci_device_ids[] = { |
| 43 | { |
| 44 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 45 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC}, |
| 46 | { |
| 47 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 48 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN}, |
| 49 | {0}, |
| 50 | }; |
| 51 | |
| 52 | static void |
| 53 | virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst, |
| 54 | int len, u32 addr) |
| 55 | { |
| 56 | u32 size = 0; |
| 57 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 58 | |
| 59 | while (len > 0) |
| 60 | { |
| 61 | if (len >= 4) |
| 62 | { |
| 63 | size = 4; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 64 | vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 65 | } |
| 66 | else if (len >= 2) |
| 67 | { |
| 68 | size = 2; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 69 | vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 70 | } |
| 71 | else |
| 72 | { |
| 73 | size = 1; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 74 | vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 75 | } |
| 76 | dst = (u8 *) dst + size; |
| 77 | addr += size; |
| 78 | len -= size; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif, |
| 84 | void *src, int len, u32 addr) |
| 85 | { |
| 86 | u32 size = 0; |
| 87 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 88 | |
| 89 | while (len > 0) |
| 90 | { |
| 91 | if (len >= 4) |
| 92 | { |
| 93 | size = 4; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 94 | vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 95 | } |
| 96 | else if (len >= 2) |
| 97 | { |
| 98 | size = 2; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 99 | vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 100 | } |
| 101 | else |
| 102 | { |
| 103 | size = 1; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 104 | vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 105 | } |
| 106 | src = (u8 *) src + size; |
| 107 | addr += size; |
| 108 | len -= size; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static u64 |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 113 | virtio_pci_legacy_get_host_features (vlib_main_t * vm, virtio_if_t * vif) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 114 | { |
| 115 | u32 features; |
| 116 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES, |
| 117 | &features); |
| 118 | return features; |
| 119 | } |
| 120 | |
| 121 | static u32 |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 122 | virtio_pci_legacy_get_guest_features (vlib_main_t * vm, virtio_if_t * vif) |
| 123 | { |
| 124 | u32 feature = 0; |
| 125 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 126 | &feature); |
| 127 | vif->features = feature; |
| 128 | return feature; |
| 129 | } |
| 130 | |
| 131 | static u32 |
| 132 | virtio_pci_legacy_set_guest_features (vlib_main_t * vm, virtio_if_t * vif, |
| 133 | u64 features) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 134 | { |
| 135 | if ((features >> 32) != 0) |
| 136 | { |
| 137 | clib_warning ("only 32 bit features are allowed for legacy virtio!"); |
| 138 | } |
| 139 | u32 feature = 0, guest_features = (u32) features; |
| 140 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 141 | &guest_features); |
| 142 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 143 | &feature); |
| 144 | return feature; |
| 145 | } |
| 146 | |
| 147 | static u8 |
| 148 | virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif) |
| 149 | { |
| 150 | u8 status = 0; |
| 151 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status); |
| 152 | return status; |
| 153 | } |
| 154 | |
| 155 | static void |
| 156 | virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status) |
| 157 | { |
| 158 | if (status != VIRTIO_CONFIG_STATUS_RESET) |
| 159 | status |= virtio_pci_legacy_get_status (vm, vif); |
| 160 | vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status); |
| 161 | } |
| 162 | |
| 163 | static u8 |
| 164 | virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif) |
| 165 | { |
| 166 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET); |
| 167 | return virtio_pci_legacy_get_status (vm, vif); |
| 168 | } |
| 169 | |
| 170 | static u8 |
| 171 | virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif) |
| 172 | { |
| 173 | u8 isr = 0; |
| 174 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr); |
| 175 | return isr; |
| 176 | } |
| 177 | |
| 178 | static u16 |
| 179 | virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif, |
| 180 | u16 queue_id) |
| 181 | { |
| 182 | u16 queue_num = 0; |
| 183 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 184 | &queue_id); |
| 185 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 186 | &queue_num); |
| 187 | return queue_num; |
| 188 | } |
| 189 | |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 190 | static int |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 191 | virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 192 | u16 queue_id, void *p) |
| 193 | { |
| 194 | u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 195 | u32 addr2 = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 196 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 197 | &queue_id); |
| 198 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 199 | (u32 *) & addr); |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 200 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 201 | &addr2); |
| 202 | if ((u32) addr == addr2) |
| 203 | return 0; |
| 204 | return 1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void |
| 208 | virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 209 | u16 queue_id) |
| 210 | { |
| 211 | u32 src = 0; |
| 212 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 213 | &queue_id); |
| 214 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src); |
| 215 | } |
| 216 | |
| 217 | inline void |
| 218 | virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 219 | u16 queue_id) |
| 220 | { |
| 221 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 222 | &queue_id); |
| 223 | } |
| 224 | |
| 225 | /* Enable one vector (0) for Link State Intrerrupt */ |
| 226 | static u16 |
| 227 | virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif, |
| 228 | u16 vec) |
| 229 | { |
| 230 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 231 | &vec); |
| 232 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 233 | &vec); |
| 234 | return vec; |
| 235 | } |
| 236 | |
| 237 | static u16 |
| 238 | virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec, |
| 239 | u16 queue_id) |
| 240 | { |
| 241 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 242 | &queue_id); |
| 243 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 244 | &vec); |
| 245 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 246 | &vec); |
| 247 | return vec; |
| 248 | } |
| 249 | |
| 250 | static u32 |
| 251 | virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, |
| 252 | u32 flags) |
| 253 | { |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static clib_error_t * |
| 258 | virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 259 | { |
| 260 | virtio_net_config_t config; |
| 261 | clib_error_t *error = 0; |
| 262 | u16 max_queue_pairs = 1; |
| 263 | |
| 264 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 265 | { |
| 266 | virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs, |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 267 | sizeof (config.max_virtqueue_pairs), |
| 268 | STRUCT_OFFSET_OF (virtio_net_config_t, |
| 269 | max_virtqueue_pairs)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 270 | max_queue_pairs = config.max_virtqueue_pairs; |
| 271 | } |
| 272 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 273 | virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 274 | if (max_queue_pairs < 1 || max_queue_pairs > 0x8000) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 275 | return clib_error_return (error, "max queue pair is %x," |
| 276 | " should be in range [1, 0x8000]", |
| 277 | max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 278 | |
| 279 | vif->max_queue_pairs = max_queue_pairs; |
| 280 | return error; |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 285 | { |
| 286 | virtio_pci_legacy_write_config (vm, vif, vif->mac_addr, |
| 287 | sizeof (vif->mac_addr), 0); |
| 288 | } |
| 289 | |
| 290 | static u32 |
| 291 | virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 292 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 293 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 294 | { |
| 295 | virtio_pci_legacy_read_config (vm, vif, vif->mac_addr, |
| 296 | sizeof (vif->mac_addr), 0); |
| 297 | return 0; |
| 298 | } |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | static u16 |
| 303 | virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif) |
| 304 | { |
| 305 | /* |
| 306 | * Minimal driver: assumes link is up |
| 307 | */ |
| 308 | u16 status = 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 309 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 310 | virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */ |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 311 | STRUCT_OFFSET_OF (virtio_net_config_t, |
| 312 | status)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 313 | return status; |
| 314 | } |
| 315 | |
| 316 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 317 | virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 318 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 319 | { |
| 320 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 321 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 322 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 323 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 324 | line--; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 325 | u16 qid = line; |
| 326 | |
| 327 | vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); |
| 328 | } |
| 329 | |
| 330 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 331 | virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 332 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 333 | { |
| 334 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 335 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 336 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 337 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 338 | |
| 339 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 340 | { |
| 341 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 342 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 343 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 348 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void |
| 353 | virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h) |
| 354 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 355 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 356 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 357 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 358 | u8 isr = 0; |
| 359 | u16 line = 0; |
| 360 | |
| 361 | isr = virtio_pci_legacy_get_isr (vm, vif); |
| 362 | |
| 363 | /* |
| 364 | * If the lower bit is set: look through the used rings of |
| 365 | * all virtqueues for the device, to see if any progress has |
| 366 | * been made by the device which requires servicing. |
| 367 | */ |
| 368 | if (isr & VIRTIO_PCI_ISR_INTR) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 369 | { |
| 370 | for (; line < vif->num_rxqs; line++) |
| 371 | virtio_pci_irq_queue_handler (vm, h, (line + 1)); |
| 372 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 373 | |
| 374 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 375 | virtio_pci_irq_config_handler (vm, h, line); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | inline void |
| 379 | device_status (vlib_main_t * vm, virtio_if_t * vif) |
| 380 | { |
| 381 | struct status_struct |
| 382 | { |
| 383 | u8 bit; |
| 384 | char *str; |
| 385 | }; |
| 386 | struct status_struct *status_entry; |
| 387 | static struct status_struct status_array[] = { |
| 388 | #define _(s,b) { .str = #s, .bit = b, }, |
| 389 | foreach_virtio_config_status_flags |
| 390 | #undef _ |
| 391 | {.str = NULL} |
| 392 | }; |
| 393 | |
| 394 | vlib_cli_output (vm, " status 0x%x", vif->status); |
| 395 | |
| 396 | status_entry = (struct status_struct *) &status_array; |
| 397 | while (status_entry->str) |
| 398 | { |
| 399 | if (vif->status & status_entry->bit) |
| 400 | vlib_cli_output (vm, " %s (%x)", status_entry->str, |
| 401 | status_entry->bit); |
| 402 | status_entry++; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | inline void |
| 407 | debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif) |
| 408 | { |
| 409 | u32 data_u32; |
| 410 | u16 data_u16; |
| 411 | u8 data_u8; |
| 412 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES, |
| 413 | &data_u32); |
| 414 | vlib_cli_output (vm, "remote features 0x%lx", data_u32); |
| 415 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 416 | &data_u32); |
| 417 | vlib_cli_output (vm, "guest features 0x%lx", data_u32); |
| 418 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 419 | &data_u32); |
| 420 | vlib_cli_output (vm, "queue address 0x%lx", data_u32); |
| 421 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 422 | &data_u16); |
| 423 | vlib_cli_output (vm, "queue size 0x%x", data_u16); |
| 424 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 425 | &data_u16); |
| 426 | vlib_cli_output (vm, "queue select 0x%x", data_u16); |
| 427 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 428 | &data_u16); |
| 429 | vlib_cli_output (vm, "queue notify 0x%x", data_u16); |
| 430 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8); |
| 431 | vlib_cli_output (vm, "status 0x%x", data_u8); |
| 432 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8); |
| 433 | vlib_cli_output (vm, "isr 0x%x", data_u8); |
| 434 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 435 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 436 | { |
| 437 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 438 | &data_u16); |
| 439 | vlib_cli_output (vm, "config vector 0x%x", data_u16); |
| 440 | u16 queue_id = 0; |
| 441 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 442 | &queue_id); |
| 443 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 444 | &data_u16); |
| 445 | vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16); |
| 446 | } |
| 447 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 448 | u8 mac[6]; |
| 449 | virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0); |
| 450 | vlib_cli_output (vm, "mac %U", format_ethernet_address, mac); |
| 451 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */ |
| 452 | 6); |
| 453 | vlib_cli_output (vm, "link up/down status 0x%x", data_u16); |
| 454 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), |
| 455 | /* offset to max_virtqueue */ 8); |
| 456 | vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16); |
| 457 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */ |
| 458 | 10); |
| 459 | vlib_cli_output (vm, "mtu 0x%x", data_u16); |
| 460 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 461 | u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 462 | i += a; |
| 463 | i &= ~a; |
| 464 | for (; i < 64; i += 4) |
| 465 | { |
| 466 | u32 data = 0; |
| 467 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data); |
| 468 | vlib_cli_output (vm, "0x%lx", data); |
| 469 | } |
| 470 | } |
| 471 | |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 472 | struct virtio_ctrl_msg |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 473 | { |
| 474 | struct virtio_net_ctrl_hdr ctrl; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 475 | virtio_net_ctrl_ack status; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 476 | u8 data[1024]; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | static int |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 480 | virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif, |
| 481 | struct virtio_ctrl_msg *data, u32 len) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 482 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 483 | virtio_vring_t *vring = vif->cxq_vring; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 484 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 485 | struct virtio_ctrl_msg result; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 486 | u32 buffer_index; |
| 487 | vlib_buffer_t *b; |
| 488 | u16 used, next, avail; |
| 489 | u16 sz = vring->size; |
| 490 | u16 mask = sz - 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 491 | |
| 492 | used = vring->desc_in_use; |
| 493 | next = vring->desc_next; |
| 494 | avail = vring->avail->idx; |
| 495 | struct vring_desc *d = &vring->desc[next]; |
| 496 | |
| 497 | if (vlib_buffer_alloc (vm, &buffer_index, 1)) |
| 498 | b = vlib_get_buffer (vm, buffer_index); |
| 499 | else |
| 500 | return VIRTIO_NET_ERR; |
| 501 | /* |
| 502 | * current_data may not be initialized with 0 and may contain |
| 503 | * previous offset. |
| 504 | */ |
| 505 | b->current_data = 0; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 506 | clib_memcpy (vlib_buffer_get_current (b), data, |
| 507 | sizeof (struct virtio_ctrl_msg)); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 508 | d->flags = VRING_DESC_F_NEXT; |
| 509 | d->addr = vlib_buffer_get_current_pa (vm, b); |
| 510 | d->len = sizeof (struct virtio_net_ctrl_hdr); |
| 511 | vring->avail->ring[avail & mask] = next; |
| 512 | avail++; |
| 513 | next = (next + 1) & mask; |
| 514 | d->next = next; |
| 515 | used++; |
| 516 | |
| 517 | d = &vring->desc[next]; |
| 518 | d->flags = VRING_DESC_F_NEXT; |
| 519 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 520 | STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data); |
| 521 | d->len = len; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 522 | next = (next + 1) & mask; |
| 523 | d->next = next; |
| 524 | used++; |
| 525 | |
| 526 | d = &vring->desc[next]; |
| 527 | d->flags = VRING_DESC_F_WRITE; |
| 528 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 529 | STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status); |
| 530 | d->len = sizeof (data->status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 531 | next = (next + 1) & mask; |
| 532 | used++; |
| 533 | |
| 534 | CLIB_MEMORY_STORE_BARRIER (); |
| 535 | vring->avail->idx = avail; |
| 536 | vring->desc_next = next; |
| 537 | vring->desc_in_use = used; |
| 538 | |
| 539 | if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0) |
| 540 | { |
| 541 | virtio_kick (vm, vring, vif); |
| 542 | } |
| 543 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 544 | u16 last = vring->last_used_idx, n_left = 0; |
| 545 | n_left = vring->used->idx - last; |
| 546 | |
| 547 | while (n_left) |
| 548 | { |
| 549 | struct vring_used_elem *e = &vring->used->ring[last & mask]; |
| 550 | u16 slot = e->id; |
| 551 | |
| 552 | d = &vring->desc[slot]; |
| 553 | while (d->flags & VRING_DESC_F_NEXT) |
| 554 | { |
| 555 | used--; |
| 556 | slot = d->next; |
| 557 | d = &vring->desc[slot]; |
| 558 | } |
| 559 | used--; |
| 560 | last++; |
| 561 | n_left--; |
| 562 | } |
| 563 | vring->desc_in_use = used; |
| 564 | vring->last_used_idx = last; |
| 565 | |
| 566 | CLIB_MEMORY_BARRIER (); |
| 567 | clib_memcpy (&result, vlib_buffer_get_current (b), |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 568 | sizeof (struct virtio_ctrl_msg)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 569 | virtio_log_debug (vif, "ctrl-queue: status %u", result.status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 570 | status = result.status; |
| 571 | vlib_buffer_free (vm, &buffer_index, 1); |
| 572 | return status; |
| 573 | } |
| 574 | |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 575 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 576 | virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 577 | { |
| 578 | struct virtio_ctrl_msg offload_hdr; |
| 579 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 580 | |
| 581 | offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 582 | offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 583 | offload_hdr.status = VIRTIO_NET_ERR; |
| 584 | u64 offloads = 0ULL; |
| 585 | clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads)); |
| 586 | |
| 587 | status = |
| 588 | virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads)); |
| 589 | virtio_log_debug (vif, "disable offloads"); |
| 590 | vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif); |
| 591 | virtio_pci_legacy_get_guest_features (vm, vif); |
| 592 | return status; |
| 593 | } |
| 594 | |
| 595 | static int |
| 596 | virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 597 | { |
| 598 | struct virtio_ctrl_msg csum_offload_hdr; |
| 599 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 600 | |
| 601 | csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 602 | csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 603 | csum_offload_hdr.status = VIRTIO_NET_ERR; |
| 604 | u64 offloads = 0ULL; |
| 605 | offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM); |
| 606 | clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads)); |
| 607 | |
| 608 | status = |
| 609 | virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads)); |
| 610 | virtio_log_debug (vif, "enable checksum offload"); |
| 611 | vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif); |
| 612 | virtio_pci_legacy_get_guest_features (vm, vif); |
| 613 | return status; |
| 614 | } |
| 615 | |
| 616 | static int |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 617 | virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif) |
| 618 | { |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 619 | struct virtio_ctrl_msg gso_hdr; |
| 620 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 621 | |
| 622 | gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 623 | gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 624 | gso_hdr.status = VIRTIO_NET_ERR; |
| 625 | u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 626 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 627 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 628 | clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads)); |
| 629 | |
| 630 | status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 631 | virtio_log_debug (vif, "enable gso"); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 632 | vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif); |
| 633 | virtio_pci_legacy_get_guest_features (vm, vif); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 634 | return status; |
| 635 | } |
| 636 | |
| 637 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 638 | virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, |
| 639 | int csum_offload_enabled) |
| 640 | { |
| 641 | vnet_main_t *vnm = vnet_get_main (); |
| 642 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 643 | |
| 644 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 645 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))) |
| 646 | { |
| 647 | if (gso_enabled |
| 648 | && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | |
| 649 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)))) |
| 650 | { |
| 651 | if (virtio_pci_enable_gso (vm, vif)) |
| 652 | { |
| 653 | virtio_log_warning (vif, "gso is not enabled"); |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | vif->gso_enabled = 1; |
| 658 | vif->csum_offload_enabled = 0; |
| 659 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO | |
| 660 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD; |
| 661 | } |
| 662 | } |
| 663 | else if (csum_offload_enabled |
| 664 | && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))) |
| 665 | { |
| 666 | if (virtio_pci_enable_checksum_offload (vm, vif)) |
| 667 | { |
| 668 | virtio_log_warning (vif, "checksum offload is not enabled"); |
| 669 | } |
| 670 | else |
| 671 | { |
| 672 | vif->csum_offload_enabled = 1; |
| 673 | vif->gso_enabled = 0; |
| 674 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 675 | hw->flags |= |
| 676 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD; |
| 677 | } |
| 678 | } |
| 679 | else |
| 680 | { |
| 681 | if (virtio_pci_disable_offload (vm, vif)) |
| 682 | { |
| 683 | virtio_log_warning (vif, "offloads are not disabled"); |
| 684 | } |
| 685 | else |
| 686 | { |
| 687 | vif->csum_offload_enabled = 0; |
| 688 | vif->gso_enabled = 0; |
| 689 | hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO | |
| 690 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static int |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 699 | virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif, |
| 700 | u16 num_queues) |
| 701 | { |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 702 | struct virtio_ctrl_msg mq_hdr; |
| 703 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 704 | |
| 705 | mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ; |
| 706 | mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; |
| 707 | mq_hdr.status = VIRTIO_NET_ERR; |
| 708 | clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues)); |
| 709 | |
| 710 | status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 711 | virtio_log_debug (vif, "multi-queue enable %u queues", num_queues); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 712 | return status; |
| 713 | } |
| 714 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 715 | static u8 |
| 716 | virtio_pci_queue_size_valid (u16 qsz) |
| 717 | { |
| 718 | if (qsz < 64 || qsz > 4096) |
| 719 | return 0; |
| 720 | if ((qsz % 64) != 0) |
| 721 | return 0; |
| 722 | return 1; |
| 723 | } |
| 724 | |
| 725 | clib_error_t * |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 726 | virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif, |
| 727 | u16 queue_num) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 728 | { |
| 729 | clib_error_t *error = 0; |
| 730 | u16 queue_size = 0; |
| 731 | virtio_vring_t *vring; |
| 732 | struct vring vr; |
| 733 | u32 i = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 734 | void *ptr = NULL; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 735 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 736 | queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 737 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 738 | clib_warning ("queue size is not valid"); |
| 739 | |
| 740 | if (!is_pow2 (queue_size)) |
| 741 | return clib_error_return (0, "ring size must be power of 2"); |
| 742 | |
| 743 | if (queue_size > 32768) |
| 744 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 745 | |
| 746 | if (queue_size == 0) |
| 747 | queue_size = 256; |
| 748 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 749 | vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); |
| 750 | vring = vec_elt_at_index (vif->cxq_vring, 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 751 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 752 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 753 | ptr = |
| 754 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 755 | vif->numa_node); |
| 756 | if (!ptr) |
| 757 | return vlib_physmem_last_error (vm); |
| 758 | clib_memset (ptr, 0, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 759 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 760 | vring->desc = vr.desc; |
| 761 | vring->avail = vr.avail; |
| 762 | vring->used = vr.used; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 763 | vring->queue_id = queue_num; |
| 764 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 765 | |
| 766 | ASSERT (vring->buffers == 0); |
| 767 | |
| 768 | vring->size = queue_size; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 769 | virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 770 | queue_size); |
| 771 | virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr); |
| 772 | vring->kick_fd = -1; |
| 773 | |
| 774 | return error; |
| 775 | } |
| 776 | |
| 777 | clib_error_t * |
| 778 | virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num) |
| 779 | { |
| 780 | clib_error_t *error = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 781 | u16 queue_size = 0; |
| 782 | virtio_vring_t *vring; |
| 783 | struct vring vr; |
| 784 | u32 i = 0; |
| 785 | void *ptr = NULL; |
| 786 | |
| 787 | queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num); |
| 788 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 789 | clib_warning ("queue size is not valid"); |
| 790 | |
| 791 | if (!is_pow2 (queue_size)) |
| 792 | return clib_error_return (0, "ring size must be power of 2"); |
| 793 | |
| 794 | if (queue_size > 32768) |
| 795 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 796 | |
| 797 | if (queue_size == 0) |
| 798 | queue_size = 256; |
| 799 | |
| 800 | if (queue_num % 2) |
| 801 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 802 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), |
| 803 | CLIB_CACHE_LINE_BYTES); |
| 804 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); |
| 805 | clib_spinlock_init (&vring->lockp); |
| 806 | } |
| 807 | else |
| 808 | { |
| 809 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num), |
| 810 | CLIB_CACHE_LINE_BYTES); |
| 811 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); |
| 812 | } |
| 813 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 814 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
| 815 | ptr = |
| 816 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 817 | vif->numa_node); |
| 818 | if (!ptr) |
| 819 | return vlib_physmem_last_error (vm); |
| 820 | clib_memset (ptr, 0, i); |
| 821 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 822 | vring->desc = vr.desc; |
| 823 | vring->avail = vr.avail; |
| 824 | vring->used = vr.used; |
| 825 | vring->queue_id = queue_num; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 826 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame^] | 827 | vring->flow_table = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 828 | |
| 829 | ASSERT (vring->buffers == 0); |
| 830 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 831 | if (queue_num % 2) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 832 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 833 | virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 834 | queue_size); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 835 | clib_memset_u32 (vring->buffers, ~0, queue_size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 836 | } |
| 837 | else |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 838 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 839 | virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 840 | queue_size); |
| 841 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 842 | vring->size = queue_size; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 843 | if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr)) |
| 844 | return clib_error_return (0, "error in queue address setup"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 845 | |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 846 | vring->kick_fd = -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 847 | return error; |
| 848 | } |
| 849 | |
| 850 | static void |
| 851 | virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif, |
| 852 | u64 req_features) |
| 853 | { |
| 854 | /* |
| 855 | * if features are not requested |
| 856 | * default: all supported features |
| 857 | */ |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 858 | u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) |
| 859 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 860 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
| 861 | | VIRTIO_FEATURE (VIRTIO_NET_F_MTU) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 862 | | VIRTIO_FEATURE (VIRTIO_NET_F_MAC) |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 863 | | VIRTIO_FEATURE (VIRTIO_NET_F_GSO) |
| 864 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
| 865 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6) |
| 866 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) |
| 867 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
| 868 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6) |
| 869 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 870 | | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
| 871 | | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 872 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
| 873 | | VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 874 | | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 875 | | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
| 876 | | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 877 | |
| 878 | if (req_features == 0) |
| 879 | { |
| 880 | req_features = supported_features; |
| 881 | } |
| 882 | |
| 883 | vif->features = req_features & vif->remote_features & supported_features; |
| 884 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 885 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 886 | { |
| 887 | virtio_net_config_t config; |
| 888 | virtio_pci_legacy_read_config (vm, vif, &config.mtu, |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 889 | sizeof (config.mtu), |
| 890 | STRUCT_OFFSET_OF (virtio_net_config_t, |
| 891 | mtu)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 892 | if (config.mtu < 64) |
| 893 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU); |
| 894 | } |
| 895 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 896 | vif->features = |
| 897 | virtio_pci_legacy_set_guest_features (vm, vif, vif->features); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void |
| 901 | virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif) |
| 902 | { |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 903 | vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | int |
| 907 | virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif) |
| 908 | { |
| 909 | u8 status = 0; |
| 910 | |
| 911 | /* |
| 912 | * Reset the device |
| 913 | */ |
| 914 | status = virtio_pci_legacy_reset (vm, vif); |
| 915 | |
| 916 | /* |
| 917 | * Set the Acknowledge status bit |
| 918 | */ |
| 919 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK); |
| 920 | |
| 921 | /* |
| 922 | * Set the Driver status bit |
| 923 | */ |
| 924 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER); |
| 925 | |
| 926 | /* |
| 927 | * Read the status and verify it |
| 928 | */ |
| 929 | status = virtio_pci_legacy_get_status (vm, vif); |
| 930 | if (! |
| 931 | ((status & VIRTIO_CONFIG_STATUS_ACK) |
| 932 | && (status & VIRTIO_CONFIG_STATUS_DRIVER))) |
| 933 | return -1; |
| 934 | vif->status = status; |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | clib_error_t * |
| 940 | virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif) |
| 941 | { |
| 942 | clib_error_t *error = 0; |
| 943 | struct virtio_pci_cap cap; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 944 | u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 945 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 946 | |
| 947 | if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 948 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 949 | virtio_log_error (vif, "error in reading capabilty list position"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 950 | clib_error_return (error, "error in reading capabilty list position"); |
| 951 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 952 | while (pos) |
| 953 | { |
| 954 | if ((error = |
| 955 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap, |
| 956 | sizeof (cap)))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 957 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 958 | virtio_log_error (vif, "%s [%2x]", |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 959 | "error in reading the capability at", pos); |
| 960 | clib_error_return (error, |
| 961 | "error in reading the capability at [%2x]", pos); |
| 962 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 963 | |
| 964 | if (cap.cap_vndr == PCI_CAP_ID_MSIX) |
| 965 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 966 | u16 flags, table_size, table_size_mask = 0x07FF; |
| 967 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 968 | if ((error = |
| 969 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags, |
| 970 | sizeof (flags)))) |
| 971 | clib_error_return (error, |
| 972 | "error in reading the capability at [%2x]", |
| 973 | pos + 2); |
| 974 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 975 | table_size = flags & table_size_mask; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 976 | virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 977 | "msix interrupt vector table-size", table_size); |
| 978 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 979 | if (flags & PCI_MSIX_ENABLE) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 980 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 981 | virtio_log_debug (vif, "msix interrupt enabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 982 | vif->msix_enabled = VIRTIO_MSIX_ENABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 983 | vif->msix_table_size = table_size; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 984 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 985 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 986 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 987 | virtio_log_debug (vif, "msix interrupt disabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 988 | vif->msix_enabled = VIRTIO_MSIX_DISABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 989 | vif->msix_table_size = 0; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 990 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if (cap.cap_vndr != PCI_CAP_ID_VNDR) |
| 994 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 995 | virtio_log_debug (vif, "[%2x] %s %2x ", pos, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 996 | "skipping non VNDR cap id:", cap.cap_vndr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 997 | goto next; |
| 998 | } |
| 999 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1000 | virtio_log_debug (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1001 | "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u", |
| 1002 | pos, cap.cfg_type, cap.bar, cap.offset, cap.length); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1003 | switch (cap.cfg_type) |
| 1004 | { |
| 1005 | case VIRTIO_PCI_CAP_COMMON_CFG: |
| 1006 | common_cfg = 1; |
| 1007 | break; |
| 1008 | case VIRTIO_PCI_CAP_NOTIFY_CFG: |
| 1009 | notify_base = 1; |
| 1010 | break; |
| 1011 | case VIRTIO_PCI_CAP_DEVICE_CFG: |
| 1012 | dev_cfg = 1; |
| 1013 | break; |
| 1014 | case VIRTIO_PCI_CAP_ISR_CFG: |
| 1015 | isr = 1; |
| 1016 | break; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 1017 | case VIRTIO_PCI_CAP_PCI_CFG: |
| 1018 | if (cap.bar == 0) |
| 1019 | pci_cfg = 1; |
| 1020 | break; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1021 | } |
| 1022 | next: |
| 1023 | pos = cap.cap_next; |
| 1024 | } |
| 1025 | |
| 1026 | if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0) |
| 1027 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1028 | virtio_log_debug (vif, "legacy virtio pci device found"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1029 | return error; |
| 1030 | } |
| 1031 | |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 1032 | if (!pci_cfg) |
| 1033 | clib_error_return (error, "modern virtio pci device found"); |
| 1034 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1035 | virtio_log_debug (vif, "transitional virtio pci device found"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1036 | return error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static clib_error_t * |
| 1040 | virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, |
| 1041 | virtio_pci_create_if_args_t * args) |
| 1042 | { |
| 1043 | clib_error_t *error = 0; |
Mohsin Kazmi | edf3b4b | 2019-10-30 15:00:04 +0100 | [diff] [blame] | 1044 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1045 | u8 status = 0; |
| 1046 | |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 1047 | if ((error = virtio_pci_read_caps (vm, vif))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1048 | { |
| 1049 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 1050 | virtio_log_error (vif, "Device is not supported"); |
| 1051 | clib_error_return (error, "Device is not supported"); |
| 1052 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1053 | |
| 1054 | if (virtio_pci_reset_device (vm, vif) < 0) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1055 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1056 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1057 | virtio_log_error (vif, "Failed to reset the device"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1058 | clib_error_return (error, "Failed to reset the device"); |
| 1059 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1060 | /* |
| 1061 | * read device features and negotiate (user) requested features |
| 1062 | */ |
| 1063 | virtio_pci_read_device_feature (vm, vif); |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 1064 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 1065 | 0) |
| 1066 | { |
| 1067 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 1068 | "support VIRTIO_RING_F_INDIRECT_DESC features"); |
| 1069 | } |
| 1070 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 1071 | { |
| 1072 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 1073 | "support VIRTIO_NET_F_MRG_RXBUF features"); |
| 1074 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1075 | virtio_negotiate_features (vm, vif, args->features); |
| 1076 | |
| 1077 | /* |
| 1078 | * After FEATURE_OK, driver should not accept new feature bits |
| 1079 | */ |
| 1080 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK); |
| 1081 | status = virtio_pci_legacy_get_status (vm, vif); |
| 1082 | if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK)) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1083 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1084 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1085 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1086 | "error encountered: Device doesn't support requested features"); |
| 1087 | clib_error_return (error, "Device doesn't support requested features"); |
| 1088 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1089 | vif->status = status; |
| 1090 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | * get or set the mac address |
| 1093 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1094 | if (virtio_pci_get_mac (vm, vif)) |
| 1095 | { |
| 1096 | f64 now = vlib_time_now (vm); |
| 1097 | u32 rnd; |
| 1098 | rnd = (u32) (now * 1e6); |
| 1099 | rnd = random_u32 (&rnd); |
| 1100 | |
| 1101 | memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd)); |
| 1102 | vif->mac_addr[0] = 2; |
| 1103 | vif->mac_addr[1] = 0xfe; |
| 1104 | virtio_pci_set_mac (vm, vif); |
| 1105 | } |
| 1106 | |
| 1107 | virtio_set_net_hdr_size (vif); |
| 1108 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1109 | /* |
| 1110 | * Initialize the virtqueues |
| 1111 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1112 | if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1113 | { |
| 1114 | args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY; |
| 1115 | goto err; |
| 1116 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1117 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1118 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 1119 | { |
| 1120 | if (vif->msix_table_size <= vif->max_queue_pairs) |
| 1121 | { |
| 1122 | virtio_log_error (vif, |
| 1123 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 1124 | vif->msix_table_size, vif->max_queue_pairs); |
| 1125 | return clib_error_return (error, |
| 1126 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 1127 | vif->msix_table_size, |
| 1128 | vif->max_queue_pairs); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1132 | for (int i = 0; i < vif->max_queue_pairs; i++) |
| 1133 | { |
| 1134 | if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1135 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1136 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 1137 | virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue", |
| 1138 | RX_QUEUE (i), "initialization"); |
| 1139 | clib_error_return (error, "%s (%u) %s", "error in rxq-queue", |
| 1140 | RX_QUEUE (i), "initialization"); |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1141 | } |
| 1142 | else |
| 1143 | { |
| 1144 | vif->num_rxqs++; |
| 1145 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1146 | |
Mohsin Kazmi | edf3b4b | 2019-10-30 15:00:04 +0100 | [diff] [blame] | 1147 | if (i >= vtm->n_vlib_mains) |
| 1148 | { |
| 1149 | /* |
| 1150 | * There is 1:1 mapping between tx queue and vpp worker thread. |
| 1151 | * tx queue 0 is bind with thread index 0, tx queue 1 on thread |
| 1152 | * index 1 and so on. |
| 1153 | * Multiple worker threads can poll same tx queue when number of |
| 1154 | * workers are more than tx queues. In this case, 1:N mapping |
| 1155 | * between tx queue and vpp worker thread. |
| 1156 | */ |
| 1157 | virtio_log_debug (vif, "%s %u, %s", "tx-queue: number", |
| 1158 | TX_QUEUE (i), |
| 1159 | "no VPP worker thread is available"); |
| 1160 | continue; |
| 1161 | } |
| 1162 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1163 | if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1164 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1165 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 1166 | virtio_log_error (vif, "%s (%u) %s", "error in txq-queue", |
| 1167 | TX_QUEUE (i), "initialization"); |
| 1168 | clib_error_return (error, "%s (%u) %s", "error in txq-queue", |
| 1169 | TX_QUEUE (i), "initialization"); |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1170 | } |
| 1171 | else |
| 1172 | { |
| 1173 | vif->num_txqs++; |
| 1174 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1175 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1176 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1177 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 1178 | { |
| 1179 | if ((error = |
| 1180 | virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2))) |
| 1181 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1182 | virtio_log_warning (vif, "%s (%u) %s", "error in control-queue", |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1183 | vif->max_queue_pairs * 2, "initialization"); |
| 1184 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 1185 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ); |
| 1186 | } |
| 1187 | } |
| 1188 | else |
| 1189 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1190 | virtio_log_debug (vif, "control queue is not available"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1191 | vif->cxq_vring = NULL; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * set the msix interrupts |
| 1196 | */ |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1197 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1198 | { |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1199 | int i, j; |
| 1200 | if (virtio_pci_legacy_set_config_irq (vm, vif, 0) == |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1201 | VIRTIO_MSI_NO_VECTOR) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1202 | { |
| 1203 | virtio_log_warning (vif, "config vector 0 is not set"); |
| 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | virtio_log_debug (vif, "config msix vector is set at 0"); |
| 1208 | } |
| 1209 | for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++) |
| 1210 | { |
| 1211 | if (virtio_pci_legacy_set_queue_irq (vm, vif, j, RX_QUEUE (i)) == |
| 1212 | VIRTIO_MSI_NO_VECTOR) |
| 1213 | { |
| 1214 | virtio_log_warning (vif, "queue (%u) vector is not set at %u", |
| 1215 | RX_QUEUE (i), j); |
| 1216 | } |
| 1217 | else |
| 1218 | { |
| 1219 | virtio_log_debug (vif, "%s (%u) %s %u", "queue", |
| 1220 | RX_QUEUE (i), "msix vector is set at", j); |
| 1221 | } |
| 1222 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1223 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1224 | |
| 1225 | /* |
| 1226 | * set the driver status OK |
| 1227 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1228 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK); |
| 1229 | vif->status = virtio_pci_legacy_get_status (vm, vif); |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 1230 | err: |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1231 | return error; |
| 1232 | } |
| 1233 | |
| 1234 | void |
| 1235 | virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) |
| 1236 | { |
| 1237 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1238 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1239 | virtio_if_t *vif; |
| 1240 | vlib_pci_dev_handle_t h; |
| 1241 | clib_error_t *error = 0; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1242 | u32 interrupt_count = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1243 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1244 | /* *INDENT-OFF* */ |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1245 | pool_foreach (vif, vim->interfaces, ({ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1246 | if (vif->pci_addr.as_u32 == args->addr) |
| 1247 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1248 | args->rv = VNET_API_ERROR_ADDRESS_IN_USE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1249 | args->error = |
| 1250 | clib_error_return (error, "PCI address in use"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1251 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1252 | format_vlib_pci_addr, &args->addr, |
| 1253 | " PCI address in use"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1254 | return; |
| 1255 | } |
| 1256 | })); |
| 1257 | /* *INDENT-ON* */ |
| 1258 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1259 | pool_get (vim->interfaces, vif); |
| 1260 | vif->dev_instance = vif - vim->interfaces; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1261 | vif->per_interface_next_index = ~0; |
| 1262 | vif->pci_addr.as_u32 = args->addr; |
| 1263 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1264 | if ((error = |
| 1265 | vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr, |
| 1266 | virtio_pci_device_ids, &h))) |
| 1267 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1268 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1269 | args->error = |
| 1270 | clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, |
| 1271 | &vif->pci_addr); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1272 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1273 | format_vlib_pci_addr, &vif->pci_addr, |
| 1274 | "error encountered on pci device open"); |
Benoît Ganne | 8f01183 | 2019-11-20 14:32:38 +0100 | [diff] [blame] | 1275 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1276 | return; |
| 1277 | } |
| 1278 | vif->pci_dev_handle = h; |
| 1279 | vlib_pci_set_private_data (vm, h, vif->dev_instance); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1280 | vif->numa_node = vlib_pci_get_numa_node (vm, h); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1281 | vif->type = VIRTIO_IF_TYPE_PCI; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1282 | |
| 1283 | if ((error = vlib_pci_bus_master_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1284 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1285 | virtio_log_error (vif, "error encountered on pci bus master enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1286 | goto error; |
| 1287 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1288 | |
| 1289 | if ((error = vlib_pci_io_region (vm, h, 0))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1290 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1291 | virtio_log_error (vif, "error encountered on pci io region"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1292 | goto error; |
| 1293 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1294 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1295 | interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h); |
| 1296 | if (interrupt_count > 1) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1297 | { |
| 1298 | if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1299 | &virtio_pci_irq_config_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1300 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1301 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1302 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1303 | "error encountered on pci register msix handler 0"); |
| 1304 | goto error; |
| 1305 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1306 | |
| 1307 | if ((error = |
| 1308 | vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1), |
| 1309 | &virtio_pci_irq_queue_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1310 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1311 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1312 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1313 | "error encountered on pci register msix handler 1"); |
| 1314 | goto error; |
| 1315 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1316 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1317 | if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1318 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1319 | virtio_log_error (vif, "error encountered on pci enable msix irq"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1320 | goto error; |
| 1321 | } |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1322 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1323 | virtio_log_debug (vif, "device supports msix interrupts"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1324 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1325 | else if (interrupt_count == 1) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1326 | { |
| 1327 | /* |
| 1328 | * if msix table-size is 1, fall back to intX. |
| 1329 | */ |
| 1330 | if ((error = |
| 1331 | vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler))) |
| 1332 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1333 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1334 | "error encountered on pci register interrupt handler"); |
| 1335 | goto error; |
| 1336 | } |
| 1337 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1338 | virtio_log_debug (vif, "pci register interrupt handler"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1339 | } |
| 1340 | else |
| 1341 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1342 | /* |
| 1343 | * WARN: intX is showing some weird behaviour. |
| 1344 | * Please don't use interrupt mode with UIO driver. |
| 1345 | */ |
| 1346 | vif->support_int_mode = 0; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1347 | virtio_log_debug (vif, "driver is configured in poll mode only"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if ((error = vlib_pci_intr_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1351 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1352 | virtio_log_error (vif, "error encountered on pci interrupt enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1353 | goto error; |
| 1354 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1355 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1356 | if ((error = virtio_pci_device_init (vm, vif, args))) |
| 1357 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1358 | virtio_log_error (vif, "error encountered on device init"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1359 | goto error; |
| 1360 | } |
| 1361 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1362 | /* create interface */ |
| 1363 | error = ethernet_register_interface (vnm, virtio_device_class.index, |
| 1364 | vif->dev_instance, vif->mac_addr, |
| 1365 | &vif->hw_if_index, |
| 1366 | virtio_pci_flag_change); |
| 1367 | |
| 1368 | if (error) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1369 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1370 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1371 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1372 | "error encountered on ethernet register interface"); |
| 1373 | goto error; |
| 1374 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1375 | |
| 1376 | vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 1377 | vif->sw_if_index = sw->sw_if_index; |
| 1378 | args->sw_if_index = sw->sw_if_index; |
| 1379 | |
| 1380 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 1381 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 1382 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 1383 | virtio_input_node.index); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1384 | u32 i = 0; |
| 1385 | vec_foreach_index (i, vif->rxq_vrings) |
| 1386 | { |
| 1387 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0); |
| 1388 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i)); |
| 1389 | /* Set default rx mode to POLLING */ |
| 1390 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i, |
| 1391 | VNET_HW_INTERFACE_RX_MODE_POLLING); |
| 1392 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1393 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 1394 | { |
| 1395 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 1396 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 1397 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 1398 | } |
| 1399 | else |
| 1400 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1401 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1402 | virtio_pci_offloads (vm, vif, args->gso_enabled, |
| 1403 | args->checksum_offload_enabled); |
| 1404 | |
| 1405 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 1406 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1407 | { |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1408 | if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs)) |
| 1409 | virtio_log_warning (vif, "multiqueue is not set"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1410 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1411 | return; |
| 1412 | |
| 1413 | error: |
| 1414 | virtio_pci_delete_if (vm, vif); |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1415 | if (args->rv == 0) |
| 1416 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1417 | args->error = error; |
| 1418 | } |
| 1419 | |
| 1420 | int |
| 1421 | virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) |
| 1422 | { |
| 1423 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1424 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1425 | u32 i = 0; |
| 1426 | |
| 1427 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1428 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1429 | |
| 1430 | vlib_pci_intr_disable (vm, vif->pci_dev_handle); |
| 1431 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1432 | for (i = 0; i < vif->max_queue_pairs; i++) |
| 1433 | { |
| 1434 | virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i)); |
| 1435 | virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i)); |
| 1436 | } |
| 1437 | |
| 1438 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 1439 | virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1440 | |
| 1441 | virtio_pci_legacy_reset (vm, vif); |
| 1442 | |
| 1443 | if (vif->hw_if_index) |
| 1444 | { |
| 1445 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1446 | vec_foreach_index (i, vif->rxq_vrings) |
| 1447 | { |
| 1448 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i); |
| 1449 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1450 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 1451 | } |
| 1452 | |
| 1453 | vlib_pci_device_close (vm, vif->pci_dev_handle); |
| 1454 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1455 | vec_foreach_index (i, vif->rxq_vrings) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1456 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1457 | virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1458 | if (vring->kick_fd != -1) |
| 1459 | close (vring->kick_fd); |
| 1460 | if (vring->used) |
| 1461 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1462 | virtio_free_rx_buffers (vm, vring); |
| 1463 | } |
| 1464 | vec_free (vring->buffers); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1465 | vlib_physmem_free (vm, vring->desc); |
| 1466 | } |
| 1467 | |
| 1468 | vec_foreach_index (i, vif->txq_vrings) |
| 1469 | { |
| 1470 | virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); |
| 1471 | if (vring->kick_fd != -1) |
| 1472 | close (vring->kick_fd); |
| 1473 | if (vring->used) |
| 1474 | { |
| 1475 | virtio_free_used_desc (vm, vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1476 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1477 | vec_free (vring->buffers); |
Steven Luong | e558496 | 2019-06-25 13:47:06 -0700 | [diff] [blame] | 1478 | clib_spinlock_free (&vring->lockp); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1479 | vlib_physmem_free (vm, vring->desc); |
| 1480 | } |
| 1481 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1482 | if (vif->cxq_vring != NULL) |
| 1483 | { |
| 1484 | u16 last = vif->cxq_vring->last_used_idx; |
| 1485 | u16 n_left = vif->cxq_vring->used->idx - last; |
| 1486 | while (n_left) |
| 1487 | { |
| 1488 | last++; |
| 1489 | n_left--; |
| 1490 | } |
| 1491 | |
| 1492 | vif->cxq_vring->last_used_idx = last; |
| 1493 | vlib_physmem_free (vm, vif->cxq_vring->desc); |
| 1494 | } |
| 1495 | |
| 1496 | vec_free (vif->rxq_vrings); |
| 1497 | vec_free (vif->txq_vrings); |
| 1498 | vec_free (vif->cxq_vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1499 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1500 | clib_error_free (vif->error); |
| 1501 | memset (vif, 0, sizeof (*vif)); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1502 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1507 | int |
| 1508 | virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif, |
| 1509 | int gso_enabled, |
| 1510 | int checksum_offload_enabled, |
| 1511 | int offloads_disabled) |
| 1512 | { |
| 1513 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1514 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1515 | |
| 1516 | if (gso_enabled) |
| 1517 | virtio_pci_offloads (vm, vif, 1, 0); |
| 1518 | else if (checksum_offload_enabled) |
| 1519 | virtio_pci_offloads (vm, vif, 0, 1); |
| 1520 | else if (offloads_disabled) |
| 1521 | virtio_pci_offloads (vm, vif, 0, 0); |
| 1522 | |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1526 | /* |
| 1527 | * fd.io coding-style-patch-verification: ON |
| 1528 | * |
| 1529 | * Local Variables: |
| 1530 | * eval: (c-set-style "gnu") |
| 1531 | * End: |
| 1532 | */ |