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