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 |
| 113 | virtio_pci_legacy_get_features (vlib_main_t * vm, virtio_if_t * vif) |
| 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 |
| 122 | virtio_pci_legacy_set_features (vlib_main_t * vm, virtio_if_t * vif, |
| 123 | u64 features) |
| 124 | { |
| 125 | if ((features >> 32) != 0) |
| 126 | { |
| 127 | clib_warning ("only 32 bit features are allowed for legacy virtio!"); |
| 128 | } |
| 129 | u32 feature = 0, guest_features = (u32) features; |
| 130 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 131 | &guest_features); |
| 132 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 133 | &feature); |
| 134 | return feature; |
| 135 | } |
| 136 | |
| 137 | static u8 |
| 138 | virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif) |
| 139 | { |
| 140 | u8 status = 0; |
| 141 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status); |
| 142 | return status; |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status) |
| 147 | { |
| 148 | if (status != VIRTIO_CONFIG_STATUS_RESET) |
| 149 | status |= virtio_pci_legacy_get_status (vm, vif); |
| 150 | vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status); |
| 151 | } |
| 152 | |
| 153 | static u8 |
| 154 | virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif) |
| 155 | { |
| 156 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET); |
| 157 | return virtio_pci_legacy_get_status (vm, vif); |
| 158 | } |
| 159 | |
| 160 | static u8 |
| 161 | virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif) |
| 162 | { |
| 163 | u8 isr = 0; |
| 164 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr); |
| 165 | return isr; |
| 166 | } |
| 167 | |
| 168 | static u16 |
| 169 | virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif, |
| 170 | u16 queue_id) |
| 171 | { |
| 172 | u16 queue_num = 0; |
| 173 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 174 | &queue_id); |
| 175 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 176 | &queue_num); |
| 177 | return queue_num; |
| 178 | } |
| 179 | |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame^] | 180 | static int |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 181 | virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 182 | u16 queue_id, void *p) |
| 183 | { |
| 184 | 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^] | 185 | u32 addr2 = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 186 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 187 | &queue_id); |
| 188 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 189 | (u32 *) & addr); |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame^] | 190 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 191 | &addr2); |
| 192 | if ((u32) addr == addr2) |
| 193 | return 0; |
| 194 | return 1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void |
| 198 | virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 199 | u16 queue_id) |
| 200 | { |
| 201 | u32 src = 0; |
| 202 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 203 | &queue_id); |
| 204 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src); |
| 205 | } |
| 206 | |
| 207 | inline void |
| 208 | virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 209 | u16 queue_id) |
| 210 | { |
| 211 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 212 | &queue_id); |
| 213 | } |
| 214 | |
| 215 | /* Enable one vector (0) for Link State Intrerrupt */ |
| 216 | static u16 |
| 217 | virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif, |
| 218 | u16 vec) |
| 219 | { |
| 220 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 221 | &vec); |
| 222 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 223 | &vec); |
| 224 | return vec; |
| 225 | } |
| 226 | |
| 227 | static u16 |
| 228 | virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec, |
| 229 | u16 queue_id) |
| 230 | { |
| 231 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 232 | &queue_id); |
| 233 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 234 | &vec); |
| 235 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 236 | &vec); |
| 237 | return vec; |
| 238 | } |
| 239 | |
| 240 | static u32 |
| 241 | virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, |
| 242 | u32 flags) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static clib_error_t * |
| 248 | virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 249 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 250 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 251 | virtio_net_config_t config; |
| 252 | clib_error_t *error = 0; |
| 253 | u16 max_queue_pairs = 1; |
| 254 | |
| 255 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 256 | { |
| 257 | virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs, |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 258 | sizeof (config.max_virtqueue_pairs), |
| 259 | STRUCT_OFFSET_OF (virtio_net_config_t, |
| 260 | max_virtqueue_pairs)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 261 | max_queue_pairs = config.max_virtqueue_pairs; |
| 262 | } |
| 263 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 264 | virtio_log_debug (vim, vif, "max queue pair is %x", max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 265 | if (max_queue_pairs < 1 || max_queue_pairs > 0x8000) |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 266 | 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] | 267 | |
| 268 | vif->max_queue_pairs = max_queue_pairs; |
| 269 | return error; |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 274 | { |
| 275 | virtio_pci_legacy_write_config (vm, vif, vif->mac_addr, |
| 276 | sizeof (vif->mac_addr), 0); |
| 277 | } |
| 278 | |
| 279 | static u32 |
| 280 | virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 281 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 282 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 283 | { |
| 284 | virtio_pci_legacy_read_config (vm, vif, vif->mac_addr, |
| 285 | sizeof (vif->mac_addr), 0); |
| 286 | return 0; |
| 287 | } |
| 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | static u16 |
| 292 | virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif) |
| 293 | { |
| 294 | /* |
| 295 | * Minimal driver: assumes link is up |
| 296 | */ |
| 297 | u16 status = 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 298 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 299 | virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */ |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 300 | STRUCT_OFFSET_OF (virtio_net_config_t, |
| 301 | status)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 302 | return status; |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line) |
| 307 | { |
| 308 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 309 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 310 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 311 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 312 | u16 qid = line; |
| 313 | |
| 314 | vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); |
| 315 | } |
| 316 | |
| 317 | static void |
| 318 | virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line) |
| 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 | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 324 | |
| 325 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 326 | { |
| 327 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 328 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 329 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 334 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h) |
| 340 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 341 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 342 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 343 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 344 | u8 isr = 0; |
| 345 | u16 line = 0; |
| 346 | |
| 347 | isr = virtio_pci_legacy_get_isr (vm, vif); |
| 348 | |
| 349 | /* |
| 350 | * If the lower bit is set: look through the used rings of |
| 351 | * all virtqueues for the device, to see if any progress has |
| 352 | * been made by the device which requires servicing. |
| 353 | */ |
| 354 | if (isr & VIRTIO_PCI_ISR_INTR) |
| 355 | virtio_pci_irq_0_handler (vm, h, line); |
| 356 | |
| 357 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
| 358 | virtio_pci_irq_1_handler (vm, h, line); |
| 359 | } |
| 360 | |
| 361 | inline void |
| 362 | device_status (vlib_main_t * vm, virtio_if_t * vif) |
| 363 | { |
| 364 | struct status_struct |
| 365 | { |
| 366 | u8 bit; |
| 367 | char *str; |
| 368 | }; |
| 369 | struct status_struct *status_entry; |
| 370 | static struct status_struct status_array[] = { |
| 371 | #define _(s,b) { .str = #s, .bit = b, }, |
| 372 | foreach_virtio_config_status_flags |
| 373 | #undef _ |
| 374 | {.str = NULL} |
| 375 | }; |
| 376 | |
| 377 | vlib_cli_output (vm, " status 0x%x", vif->status); |
| 378 | |
| 379 | status_entry = (struct status_struct *) &status_array; |
| 380 | while (status_entry->str) |
| 381 | { |
| 382 | if (vif->status & status_entry->bit) |
| 383 | vlib_cli_output (vm, " %s (%x)", status_entry->str, |
| 384 | status_entry->bit); |
| 385 | status_entry++; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | inline void |
| 390 | debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif) |
| 391 | { |
| 392 | u32 data_u32; |
| 393 | u16 data_u16; |
| 394 | u8 data_u8; |
| 395 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES, |
| 396 | &data_u32); |
| 397 | vlib_cli_output (vm, "remote features 0x%lx", data_u32); |
| 398 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 399 | &data_u32); |
| 400 | vlib_cli_output (vm, "guest features 0x%lx", data_u32); |
| 401 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 402 | &data_u32); |
| 403 | vlib_cli_output (vm, "queue address 0x%lx", data_u32); |
| 404 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 405 | &data_u16); |
| 406 | vlib_cli_output (vm, "queue size 0x%x", data_u16); |
| 407 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 408 | &data_u16); |
| 409 | vlib_cli_output (vm, "queue select 0x%x", data_u16); |
| 410 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 411 | &data_u16); |
| 412 | vlib_cli_output (vm, "queue notify 0x%x", data_u16); |
| 413 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8); |
| 414 | vlib_cli_output (vm, "status 0x%x", data_u8); |
| 415 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8); |
| 416 | vlib_cli_output (vm, "isr 0x%x", data_u8); |
| 417 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 418 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 419 | { |
| 420 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 421 | &data_u16); |
| 422 | vlib_cli_output (vm, "config vector 0x%x", data_u16); |
| 423 | u16 queue_id = 0; |
| 424 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 425 | &queue_id); |
| 426 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 427 | &data_u16); |
| 428 | vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16); |
| 429 | } |
| 430 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 431 | u8 mac[6]; |
| 432 | virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0); |
| 433 | vlib_cli_output (vm, "mac %U", format_ethernet_address, mac); |
| 434 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */ |
| 435 | 6); |
| 436 | vlib_cli_output (vm, "link up/down status 0x%x", data_u16); |
| 437 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), |
| 438 | /* offset to max_virtqueue */ 8); |
| 439 | vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16); |
| 440 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */ |
| 441 | 10); |
| 442 | vlib_cli_output (vm, "mtu 0x%x", data_u16); |
| 443 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 444 | u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 445 | i += a; |
| 446 | i &= ~a; |
| 447 | for (; i < 64; i += 4) |
| 448 | { |
| 449 | u32 data = 0; |
| 450 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data); |
| 451 | vlib_cli_output (vm, "0x%lx", data); |
| 452 | } |
| 453 | } |
| 454 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 455 | struct virtio_ctrl_mq_status_hdr |
| 456 | { |
| 457 | struct virtio_net_ctrl_hdr ctrl; |
| 458 | struct virtio_net_ctrl_mq num_mqs; |
| 459 | virtio_net_ctrl_ack status; |
| 460 | }; |
| 461 | |
| 462 | static int |
| 463 | virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif, |
| 464 | u16 num_queues) |
| 465 | { |
| 466 | virtio_main_t *vim = &virtio_main; |
| 467 | virtio_vring_t *vring = vif->cxq_vring; |
| 468 | u32 buffer_index; |
| 469 | vlib_buffer_t *b; |
| 470 | u16 used, next, avail; |
| 471 | u16 sz = vring->size; |
| 472 | u16 mask = sz - 1; |
| 473 | struct virtio_ctrl_mq_status_hdr mq_hdr, result; |
| 474 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 475 | |
| 476 | mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ; |
| 477 | mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; |
| 478 | mq_hdr.status = VIRTIO_NET_ERR; |
| 479 | mq_hdr.num_mqs.virtqueue_pairs = num_queues; |
| 480 | |
| 481 | used = vring->desc_in_use; |
| 482 | next = vring->desc_next; |
| 483 | avail = vring->avail->idx; |
| 484 | struct vring_desc *d = &vring->desc[next]; |
| 485 | |
| 486 | if (vlib_buffer_alloc (vm, &buffer_index, 1)) |
| 487 | b = vlib_get_buffer (vm, buffer_index); |
| 488 | else |
| 489 | return VIRTIO_NET_ERR; |
| 490 | /* |
| 491 | * current_data may not be initialized with 0 and may contain |
| 492 | * previous offset. |
| 493 | */ |
| 494 | b->current_data = 0; |
| 495 | clib_memcpy (vlib_buffer_get_current (b), &mq_hdr, |
| 496 | sizeof (struct virtio_ctrl_mq_status_hdr)); |
| 497 | d->flags = VRING_DESC_F_NEXT; |
| 498 | d->addr = vlib_buffer_get_current_pa (vm, b); |
| 499 | d->len = sizeof (struct virtio_net_ctrl_hdr); |
| 500 | vring->avail->ring[avail & mask] = next; |
| 501 | avail++; |
| 502 | next = (next + 1) & mask; |
| 503 | d->next = next; |
| 504 | used++; |
| 505 | |
| 506 | d = &vring->desc[next]; |
| 507 | d->flags = VRING_DESC_F_NEXT; |
| 508 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
| 509 | STRUCT_OFFSET_OF (struct virtio_ctrl_mq_status_hdr, num_mqs); |
| 510 | d->len = sizeof (struct virtio_net_ctrl_mq); |
| 511 | next = (next + 1) & mask; |
| 512 | d->next = next; |
| 513 | used++; |
| 514 | |
| 515 | d = &vring->desc[next]; |
| 516 | d->flags = VRING_DESC_F_WRITE; |
| 517 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
| 518 | STRUCT_OFFSET_OF (struct virtio_ctrl_mq_status_hdr, status); |
| 519 | d->len = sizeof (mq_hdr.status); |
| 520 | next = (next + 1) & mask; |
| 521 | used++; |
| 522 | |
| 523 | CLIB_MEMORY_STORE_BARRIER (); |
| 524 | vring->avail->idx = avail; |
| 525 | vring->desc_next = next; |
| 526 | vring->desc_in_use = used; |
| 527 | |
| 528 | if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0) |
| 529 | { |
| 530 | virtio_kick (vm, vring, vif); |
| 531 | } |
| 532 | |
| 533 | clib_memset (&result, 0, sizeof (result)); |
| 534 | u16 last = vring->last_used_idx, n_left = 0; |
| 535 | n_left = vring->used->idx - last; |
| 536 | |
| 537 | while (n_left) |
| 538 | { |
| 539 | struct vring_used_elem *e = &vring->used->ring[last & mask]; |
| 540 | u16 slot = e->id; |
| 541 | |
| 542 | d = &vring->desc[slot]; |
| 543 | while (d->flags & VRING_DESC_F_NEXT) |
| 544 | { |
| 545 | used--; |
| 546 | slot = d->next; |
| 547 | d = &vring->desc[slot]; |
| 548 | } |
| 549 | used--; |
| 550 | last++; |
| 551 | n_left--; |
| 552 | } |
| 553 | vring->desc_in_use = used; |
| 554 | vring->last_used_idx = last; |
| 555 | |
| 556 | CLIB_MEMORY_BARRIER (); |
| 557 | clib_memcpy (&result, vlib_buffer_get_current (b), |
| 558 | sizeof (struct virtio_ctrl_mq_status_hdr)); |
| 559 | |
| 560 | virtio_log_debug (vim, vif, "multi-queue enable status on Ctrl queue : %u", |
| 561 | result.status); |
| 562 | status = result.status; |
| 563 | vlib_buffer_free (vm, &buffer_index, 1); |
| 564 | return status; |
| 565 | } |
| 566 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 567 | static u8 |
| 568 | virtio_pci_queue_size_valid (u16 qsz) |
| 569 | { |
| 570 | if (qsz < 64 || qsz > 4096) |
| 571 | return 0; |
| 572 | if ((qsz % 64) != 0) |
| 573 | return 0; |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | clib_error_t * |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 578 | virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif, |
| 579 | u16 queue_num) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 580 | { |
| 581 | clib_error_t *error = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 582 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 583 | u16 queue_size = 0; |
| 584 | virtio_vring_t *vring; |
| 585 | struct vring vr; |
| 586 | u32 i = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 587 | void *ptr = NULL; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 588 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 589 | queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 590 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 591 | clib_warning ("queue size is not valid"); |
| 592 | |
| 593 | if (!is_pow2 (queue_size)) |
| 594 | return clib_error_return (0, "ring size must be power of 2"); |
| 595 | |
| 596 | if (queue_size > 32768) |
| 597 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 598 | |
| 599 | if (queue_size == 0) |
| 600 | queue_size = 256; |
| 601 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 602 | vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); |
| 603 | vring = vec_elt_at_index (vif->cxq_vring, 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 604 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 605 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 606 | ptr = |
| 607 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 608 | vif->numa_node); |
| 609 | if (!ptr) |
| 610 | return vlib_physmem_last_error (vm); |
| 611 | clib_memset (ptr, 0, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 612 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 613 | vring->desc = vr.desc; |
| 614 | vring->avail = vr.avail; |
| 615 | vring->used = vr.used; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 616 | vring->queue_id = queue_num; |
| 617 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 618 | |
| 619 | ASSERT (vring->buffers == 0); |
| 620 | |
| 621 | vring->size = queue_size; |
| 622 | virtio_log_debug (vim, vif, "control-queue: number %u, size %u", queue_num, |
| 623 | queue_size); |
| 624 | virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr); |
| 625 | vring->kick_fd = -1; |
| 626 | |
| 627 | return error; |
| 628 | } |
| 629 | |
| 630 | clib_error_t * |
| 631 | virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num) |
| 632 | { |
| 633 | clib_error_t *error = 0; |
| 634 | virtio_main_t *vim = &virtio_main; |
| 635 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 636 | u16 queue_size = 0; |
| 637 | virtio_vring_t *vring; |
| 638 | struct vring vr; |
| 639 | u32 i = 0; |
| 640 | void *ptr = NULL; |
| 641 | |
| 642 | queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num); |
| 643 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 644 | clib_warning ("queue size is not valid"); |
| 645 | |
| 646 | if (!is_pow2 (queue_size)) |
| 647 | return clib_error_return (0, "ring size must be power of 2"); |
| 648 | |
| 649 | if (queue_size > 32768) |
| 650 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 651 | |
| 652 | if (queue_size == 0) |
| 653 | queue_size = 256; |
| 654 | |
| 655 | if (queue_num % 2) |
| 656 | { |
| 657 | if (TX_QUEUE_ACCESS (queue_num) > vtm->n_vlib_mains) |
| 658 | return error; |
| 659 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), |
| 660 | CLIB_CACHE_LINE_BYTES); |
| 661 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); |
| 662 | clib_spinlock_init (&vring->lockp); |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num), |
| 667 | CLIB_CACHE_LINE_BYTES); |
| 668 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); |
| 669 | } |
| 670 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 671 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
| 672 | ptr = |
| 673 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 674 | vif->numa_node); |
| 675 | if (!ptr) |
| 676 | return vlib_physmem_last_error (vm); |
| 677 | clib_memset (ptr, 0, i); |
| 678 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 679 | vring->desc = vr.desc; |
| 680 | vring->avail = vr.avail; |
| 681 | vring->used = vr.used; |
| 682 | vring->queue_id = queue_num; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 683 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 684 | |
| 685 | ASSERT (vring->buffers == 0); |
| 686 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
| 687 | ASSERT (vring->indirect_buffers == 0); |
| 688 | vec_validate_aligned (vring->indirect_buffers, queue_size, |
| 689 | CLIB_CACHE_LINE_BYTES); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 690 | if (queue_num % 2) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 691 | { |
| 692 | u32 n_alloc = 0; |
| 693 | do |
| 694 | { |
| 695 | if (n_alloc < queue_size) |
| 696 | n_alloc = |
| 697 | vlib_buffer_alloc (vm, vring->indirect_buffers + n_alloc, |
| 698 | queue_size - n_alloc); |
| 699 | } |
| 700 | while (n_alloc != queue_size); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 701 | virtio_log_debug (vim, vif, "tx-queue: number %u, size %u", queue_num, |
| 702 | queue_size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 703 | } |
| 704 | else |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 705 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 706 | virtio_log_debug (vim, vif, "rx-queue: number %u, size %u", queue_num, |
| 707 | queue_size); |
| 708 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 709 | vring->size = queue_size; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame^] | 710 | if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr)) |
| 711 | return clib_error_return (0, "error in queue address setup"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 712 | |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame^] | 713 | vring->kick_fd = -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 714 | return error; |
| 715 | } |
| 716 | |
| 717 | static void |
| 718 | virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif, |
| 719 | u64 req_features) |
| 720 | { |
| 721 | /* |
| 722 | * if features are not requested |
| 723 | * default: all supported features |
| 724 | */ |
| 725 | u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_MTU) |
| 726 | | VIRTIO_FEATURE (VIRTIO_NET_F_MAC) |
| 727 | | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
| 728 | | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 729 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
| 730 | | VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 731 | | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 732 | | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
| 733 | | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 734 | |
| 735 | if (req_features == 0) |
| 736 | { |
| 737 | req_features = supported_features; |
| 738 | } |
| 739 | |
| 740 | vif->features = req_features & vif->remote_features & supported_features; |
| 741 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 742 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 743 | { |
| 744 | virtio_net_config_t config; |
| 745 | virtio_pci_legacy_read_config (vm, vif, &config.mtu, |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 746 | sizeof (config.mtu), |
| 747 | STRUCT_OFFSET_OF (virtio_net_config_t, |
| 748 | mtu)); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 749 | if (config.mtu < 64) |
| 750 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU); |
| 751 | } |
| 752 | |
| 753 | vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features); |
| 754 | } |
| 755 | |
| 756 | void |
| 757 | virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif) |
| 758 | { |
| 759 | vif->remote_features = virtio_pci_legacy_get_features (vm, vif); |
| 760 | } |
| 761 | |
| 762 | int |
| 763 | virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif) |
| 764 | { |
| 765 | u8 status = 0; |
| 766 | |
| 767 | /* |
| 768 | * Reset the device |
| 769 | */ |
| 770 | status = virtio_pci_legacy_reset (vm, vif); |
| 771 | |
| 772 | /* |
| 773 | * Set the Acknowledge status bit |
| 774 | */ |
| 775 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK); |
| 776 | |
| 777 | /* |
| 778 | * Set the Driver status bit |
| 779 | */ |
| 780 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER); |
| 781 | |
| 782 | /* |
| 783 | * Read the status and verify it |
| 784 | */ |
| 785 | status = virtio_pci_legacy_get_status (vm, vif); |
| 786 | if (! |
| 787 | ((status & VIRTIO_CONFIG_STATUS_ACK) |
| 788 | && (status & VIRTIO_CONFIG_STATUS_DRIVER))) |
| 789 | return -1; |
| 790 | vif->status = status; |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | clib_error_t * |
| 796 | virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif) |
| 797 | { |
| 798 | clib_error_t *error = 0; |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 799 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 800 | struct virtio_pci_cap cap; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 801 | 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] | 802 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 803 | |
| 804 | 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] | 805 | { |
| 806 | virtio_log_error (vim, vif, "error in reading capabilty list position"); |
| 807 | clib_error_return (error, "error in reading capabilty list position"); |
| 808 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 809 | while (pos) |
| 810 | { |
| 811 | if ((error = |
| 812 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap, |
| 813 | sizeof (cap)))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 814 | { |
| 815 | virtio_log_error (vim, vif, "%s [%2x]", |
| 816 | "error in reading the capability at", pos); |
| 817 | clib_error_return (error, |
| 818 | "error in reading the capability at [%2x]", pos); |
| 819 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 820 | |
| 821 | if (cap.cap_vndr == PCI_CAP_ID_MSIX) |
| 822 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 823 | u16 flags, table_size, table_size_mask = 0x07FF; |
| 824 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 825 | if ((error = |
| 826 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags, |
| 827 | sizeof (flags)))) |
| 828 | clib_error_return (error, |
| 829 | "error in reading the capability at [%2x]", |
| 830 | pos + 2); |
| 831 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 832 | table_size = flags & table_size_mask; |
| 833 | virtio_log_debug (vim, vif, "flags:0x%x %s 0x%x", flags, |
| 834 | "msix interrupt vector table-size", table_size); |
| 835 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 836 | if (flags & PCI_MSIX_ENABLE) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 837 | { |
| 838 | virtio_log_debug (vim, vif, "msix interrupt enabled"); |
| 839 | vif->msix_enabled = VIRTIO_MSIX_ENABLED; |
| 840 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 841 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 842 | { |
| 843 | virtio_log_debug (vim, vif, "msix interrupt disabled"); |
| 844 | vif->msix_enabled = VIRTIO_MSIX_DISABLED; |
| 845 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (cap.cap_vndr != PCI_CAP_ID_VNDR) |
| 849 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 850 | virtio_log_debug (vim, vif, "[%2x] %s %2x ", pos, |
| 851 | "skipping non VNDR cap id:", cap.cap_vndr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 852 | goto next; |
| 853 | } |
| 854 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 855 | virtio_log_debug (vim, vif, |
| 856 | "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u", |
| 857 | pos, cap.cfg_type, cap.bar, cap.offset, cap.length); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 858 | switch (cap.cfg_type) |
| 859 | { |
| 860 | case VIRTIO_PCI_CAP_COMMON_CFG: |
| 861 | common_cfg = 1; |
| 862 | break; |
| 863 | case VIRTIO_PCI_CAP_NOTIFY_CFG: |
| 864 | notify_base = 1; |
| 865 | break; |
| 866 | case VIRTIO_PCI_CAP_DEVICE_CFG: |
| 867 | dev_cfg = 1; |
| 868 | break; |
| 869 | case VIRTIO_PCI_CAP_ISR_CFG: |
| 870 | isr = 1; |
| 871 | break; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 872 | case VIRTIO_PCI_CAP_PCI_CFG: |
| 873 | if (cap.bar == 0) |
| 874 | pci_cfg = 1; |
| 875 | break; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 876 | } |
| 877 | next: |
| 878 | pos = cap.cap_next; |
| 879 | } |
| 880 | |
| 881 | if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0) |
| 882 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 883 | virtio_log_debug (vim, vif, "legacy virtio pci device found"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 884 | return error; |
| 885 | } |
| 886 | |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 887 | if (!pci_cfg) |
| 888 | clib_error_return (error, "modern virtio pci device found"); |
| 889 | |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 890 | virtio_log_debug (vim, vif, "transitional virtio pci device found"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 891 | return error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static clib_error_t * |
| 895 | virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, |
| 896 | virtio_pci_create_if_args_t * args) |
| 897 | { |
| 898 | clib_error_t *error = 0; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 899 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 900 | u8 status = 0; |
| 901 | |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 902 | if ((error = virtio_pci_read_caps (vm, vif))) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 903 | clib_error_return (error, "Device is not supported"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 904 | |
| 905 | if (virtio_pci_reset_device (vm, vif) < 0) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 906 | { |
| 907 | virtio_log_error (vim, vif, "Failed to reset the device"); |
| 908 | clib_error_return (error, "Failed to reset the device"); |
| 909 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 910 | /* |
| 911 | * read device features and negotiate (user) requested features |
| 912 | */ |
| 913 | virtio_pci_read_device_feature (vm, vif); |
| 914 | virtio_negotiate_features (vm, vif, args->features); |
| 915 | |
| 916 | /* |
| 917 | * After FEATURE_OK, driver should not accept new feature bits |
| 918 | */ |
| 919 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK); |
| 920 | status = virtio_pci_legacy_get_status (vm, vif); |
| 921 | if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK)) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 922 | { |
| 923 | virtio_log_error (vim, vif, |
| 924 | "error encountered: Device doesn't support requested features"); |
| 925 | clib_error_return (error, "Device doesn't support requested features"); |
| 926 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 927 | vif->status = status; |
| 928 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 929 | /* |
| 930 | * get or set the mac address |
| 931 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 932 | if (virtio_pci_get_mac (vm, vif)) |
| 933 | { |
| 934 | f64 now = vlib_time_now (vm); |
| 935 | u32 rnd; |
| 936 | rnd = (u32) (now * 1e6); |
| 937 | rnd = random_u32 (&rnd); |
| 938 | |
| 939 | memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd)); |
| 940 | vif->mac_addr[0] = 2; |
| 941 | vif->mac_addr[1] = 0xfe; |
| 942 | virtio_pci_set_mac (vm, vif); |
| 943 | } |
| 944 | |
| 945 | virtio_set_net_hdr_size (vif); |
| 946 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 947 | /* |
| 948 | * Initialize the virtqueues |
| 949 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 950 | if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif))) |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 951 | goto err; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 952 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 953 | for (int i = 0; i < vif->max_queue_pairs; i++) |
| 954 | { |
| 955 | if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame^] | 956 | { |
| 957 | virtio_log_warning (vim, vif, "%s (%u) %s", "error in rxq-queue", |
| 958 | RX_QUEUE (i), "initialization"); |
| 959 | } |
| 960 | else |
| 961 | { |
| 962 | vif->num_rxqs++; |
| 963 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 964 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 965 | if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame^] | 966 | { |
| 967 | virtio_log_warning (vim, vif, "%s (%u) %s", "error in txq-queue", |
| 968 | TX_QUEUE (i), "initialization"); |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | vif->num_txqs++; |
| 973 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 974 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 975 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 976 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 977 | { |
| 978 | if ((error = |
| 979 | virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2))) |
| 980 | { |
| 981 | virtio_log_warning (vim, vif, "%s (%u) %s", |
| 982 | "error in control-queue", |
| 983 | vif->max_queue_pairs * 2, "initialization"); |
| 984 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 985 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ); |
| 986 | } |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | virtio_log_debug (vim, vif, "control queue is not available"); |
| 991 | vif->cxq_vring = NULL; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * set the msix interrupts |
| 996 | */ |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 997 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 998 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 999 | if (virtio_pci_legacy_set_config_irq (vm, vif, 1) == |
| 1000 | VIRTIO_MSI_NO_VECTOR) |
| 1001 | virtio_log_warning (vim, vif, "config vector 1 is not set"); |
| 1002 | if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) == |
| 1003 | VIRTIO_MSI_NO_VECTOR) |
| 1004 | virtio_log_warning (vim, vif, "queue vector 0 is not set"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1005 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* |
| 1008 | * set the driver status OK |
| 1009 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1010 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK); |
| 1011 | vif->status = virtio_pci_legacy_get_status (vm, vif); |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 1012 | err: |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1013 | return error; |
| 1014 | } |
| 1015 | |
| 1016 | void |
| 1017 | virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) |
| 1018 | { |
| 1019 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1020 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1021 | virtio_if_t *vif; |
| 1022 | vlib_pci_dev_handle_t h; |
| 1023 | clib_error_t *error = 0; |
| 1024 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1025 | /* *INDENT-OFF* */ |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1026 | pool_foreach (vif, vim->interfaces, ({ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1027 | if (vif->pci_addr.as_u32 == args->addr) |
| 1028 | { |
| 1029 | args->rv = VNET_API_ERROR_INVALID_VALUE; |
| 1030 | args->error = |
| 1031 | clib_error_return (error, "PCI address in use"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1032 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1033 | format_vlib_pci_addr, &args->addr, |
| 1034 | " PCI address in use"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1035 | return; |
| 1036 | } |
| 1037 | })); |
| 1038 | /* *INDENT-ON* */ |
| 1039 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1040 | pool_get (vim->interfaces, vif); |
| 1041 | vif->dev_instance = vif - vim->interfaces; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1042 | vif->per_interface_next_index = ~0; |
| 1043 | vif->pci_addr.as_u32 = args->addr; |
| 1044 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1045 | if ((error = |
| 1046 | vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr, |
| 1047 | virtio_pci_device_ids, &h))) |
| 1048 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1049 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1050 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1051 | args->error = |
| 1052 | clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, |
| 1053 | &vif->pci_addr); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1054 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1055 | format_vlib_pci_addr, &vif->pci_addr, |
| 1056 | "error encountered on pci device open"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | vif->pci_dev_handle = h; |
| 1060 | vlib_pci_set_private_data (vm, h, vif->dev_instance); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1061 | vif->numa_node = vlib_pci_get_numa_node (vm, h); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1062 | |
| 1063 | if ((error = vlib_pci_bus_master_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1064 | { |
| 1065 | virtio_log_error (vim, vif, |
| 1066 | "error encountered on pci bus master enable"); |
| 1067 | goto error; |
| 1068 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1069 | |
| 1070 | if ((error = vlib_pci_io_region (vm, h, 0))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1071 | { |
| 1072 | virtio_log_error (vim, vif, "error encountered on pci io region"); |
| 1073 | goto error; |
| 1074 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1075 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1076 | if (vlib_pci_get_num_msix_interrupts (vm, h) > 1) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1077 | { |
| 1078 | if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, |
| 1079 | &virtio_pci_irq_0_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1080 | { |
| 1081 | virtio_log_error (vim, vif, |
| 1082 | "error encountered on pci register msix handler 0"); |
| 1083 | goto error; |
| 1084 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1085 | if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1, |
| 1086 | &virtio_pci_irq_1_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1087 | { |
| 1088 | virtio_log_error (vim, vif, |
| 1089 | "error encountered on pci register msix handler 1"); |
| 1090 | goto error; |
| 1091 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1092 | |
| 1093 | if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1094 | { |
| 1095 | virtio_log_error (vim, vif, |
| 1096 | "error encountered on pci enable msix irq"); |
| 1097 | goto error; |
| 1098 | } |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1099 | vif->support_int_mode = 1; |
| 1100 | virtio_log_debug (vim, vif, "device supports msix interrupts"); |
| 1101 | } |
| 1102 | else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1) |
| 1103 | { |
| 1104 | /* |
| 1105 | * if msix table-size is 1, fall back to intX. |
| 1106 | */ |
| 1107 | if ((error = |
| 1108 | vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler))) |
| 1109 | { |
| 1110 | virtio_log_error (vim, vif, |
| 1111 | "error encountered on pci register interrupt handler"); |
| 1112 | goto error; |
| 1113 | } |
| 1114 | vif->support_int_mode = 1; |
| 1115 | virtio_log_debug (vim, vif, "pci register interrupt handler"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1116 | } |
| 1117 | else |
| 1118 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1119 | /* |
| 1120 | * WARN: intX is showing some weird behaviour. |
| 1121 | * Please don't use interrupt mode with UIO driver. |
| 1122 | */ |
| 1123 | vif->support_int_mode = 0; |
| 1124 | virtio_log_debug (vim, vif, "driver is configured in poll mode only"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | if ((error = vlib_pci_intr_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1128 | { |
| 1129 | virtio_log_error (vim, vif, |
| 1130 | "error encountered on pci interrupt enable"); |
| 1131 | goto error; |
| 1132 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1133 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1134 | if ((error = virtio_pci_device_init (vm, vif, args))) |
| 1135 | { |
| 1136 | virtio_log_error (vim, vif, "error encountered on device init"); |
| 1137 | goto error; |
| 1138 | } |
| 1139 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1140 | vif->type = VIRTIO_IF_TYPE_PCI; |
| 1141 | /* create interface */ |
| 1142 | error = ethernet_register_interface (vnm, virtio_device_class.index, |
| 1143 | vif->dev_instance, vif->mac_addr, |
| 1144 | &vif->hw_if_index, |
| 1145 | virtio_pci_flag_change); |
| 1146 | |
| 1147 | if (error) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1148 | { |
| 1149 | virtio_log_error (vim, vif, |
| 1150 | "error encountered on ethernet register interface"); |
| 1151 | goto error; |
| 1152 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1153 | |
| 1154 | vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 1155 | vif->sw_if_index = sw->sw_if_index; |
| 1156 | args->sw_if_index = sw->sw_if_index; |
| 1157 | |
| 1158 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 1159 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 1160 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 1161 | virtio_input_node.index); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1162 | u32 i = 0; |
| 1163 | vec_foreach_index (i, vif->rxq_vrings) |
| 1164 | { |
| 1165 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0); |
| 1166 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i)); |
| 1167 | /* Set default rx mode to POLLING */ |
| 1168 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i, |
| 1169 | VNET_HW_INTERFACE_RX_MODE_POLLING); |
| 1170 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1171 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 1172 | { |
| 1173 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 1174 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 1175 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 1176 | } |
| 1177 | else |
| 1178 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1179 | |
| 1180 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 1181 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))) |
| 1182 | { |
| 1183 | if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs)) |
| 1184 | virtio_log_warning (vim, vif, "multiqueue is not set"); |
| 1185 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1186 | return; |
| 1187 | |
| 1188 | error: |
| 1189 | virtio_pci_delete_if (vm, vif); |
| 1190 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1191 | args->error = error; |
| 1192 | } |
| 1193 | |
| 1194 | int |
| 1195 | virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) |
| 1196 | { |
| 1197 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1198 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1199 | u32 i = 0; |
| 1200 | |
| 1201 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1202 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1203 | |
| 1204 | vlib_pci_intr_disable (vm, vif->pci_dev_handle); |
| 1205 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1206 | for (i = 0; i < vif->max_queue_pairs; i++) |
| 1207 | { |
| 1208 | virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i)); |
| 1209 | virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i)); |
| 1210 | } |
| 1211 | |
| 1212 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 1213 | virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1214 | |
| 1215 | virtio_pci_legacy_reset (vm, vif); |
| 1216 | |
| 1217 | if (vif->hw_if_index) |
| 1218 | { |
| 1219 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1220 | vec_foreach_index (i, vif->rxq_vrings) |
| 1221 | { |
| 1222 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i); |
| 1223 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1224 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 1225 | } |
| 1226 | |
| 1227 | vlib_pci_device_close (vm, vif->pci_dev_handle); |
| 1228 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1229 | vec_foreach_index (i, vif->rxq_vrings) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1230 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1231 | virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1232 | if (vring->kick_fd != -1) |
| 1233 | close (vring->kick_fd); |
| 1234 | if (vring->used) |
| 1235 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1236 | virtio_free_rx_buffers (vm, vring); |
| 1237 | } |
| 1238 | vec_free (vring->buffers); |
| 1239 | vec_free (vring->indirect_buffers); |
| 1240 | vlib_physmem_free (vm, vring->desc); |
| 1241 | } |
| 1242 | |
| 1243 | vec_foreach_index (i, vif->txq_vrings) |
| 1244 | { |
| 1245 | virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); |
| 1246 | if (vring->kick_fd != -1) |
| 1247 | close (vring->kick_fd); |
| 1248 | if (vring->used) |
| 1249 | { |
| 1250 | virtio_free_used_desc (vm, vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1251 | } |
| 1252 | if (vring->queue_id % 2) |
| 1253 | { |
| 1254 | vlib_buffer_free_no_next (vm, vring->indirect_buffers, vring->size); |
| 1255 | } |
| 1256 | vec_free (vring->buffers); |
| 1257 | vec_free (vring->indirect_buffers); |
| 1258 | vlib_physmem_free (vm, vring->desc); |
| 1259 | } |
| 1260 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1261 | if (vif->cxq_vring != NULL) |
| 1262 | { |
| 1263 | u16 last = vif->cxq_vring->last_used_idx; |
| 1264 | u16 n_left = vif->cxq_vring->used->idx - last; |
| 1265 | while (n_left) |
| 1266 | { |
| 1267 | last++; |
| 1268 | n_left--; |
| 1269 | } |
| 1270 | |
| 1271 | vif->cxq_vring->last_used_idx = last; |
| 1272 | vlib_physmem_free (vm, vif->cxq_vring->desc); |
| 1273 | } |
| 1274 | |
| 1275 | vec_free (vif->rxq_vrings); |
| 1276 | vec_free (vif->txq_vrings); |
| 1277 | vec_free (vif->cxq_vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1278 | |
| 1279 | if (vif->fd != -1) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1280 | vif->fd = -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1281 | if (vif->tap_fd != -1) |
| 1282 | vif->tap_fd = -1; |
| 1283 | clib_error_free (vif->error); |
| 1284 | memset (vif, 0, sizeof (*vif)); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1285 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1286 | |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * fd.io coding-style-patch-verification: ON |
| 1292 | * |
| 1293 | * Local Variables: |
| 1294 | * eval: (c-set-style "gnu") |
| 1295 | * End: |
| 1296 | */ |