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 | |
| 180 | |
| 181 | static void |
| 182 | virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 183 | u16 queue_id, void *p) |
| 184 | { |
| 185 | u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
| 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); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 194 | u16 queue_id) |
| 195 | { |
| 196 | u32 src = 0; |
| 197 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 198 | &queue_id); |
| 199 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src); |
| 200 | } |
| 201 | |
| 202 | inline void |
| 203 | virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 204 | u16 queue_id) |
| 205 | { |
| 206 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 207 | &queue_id); |
| 208 | } |
| 209 | |
| 210 | /* Enable one vector (0) for Link State Intrerrupt */ |
| 211 | static u16 |
| 212 | virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif, |
| 213 | u16 vec) |
| 214 | { |
| 215 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 216 | &vec); |
| 217 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 218 | &vec); |
| 219 | return vec; |
| 220 | } |
| 221 | |
| 222 | static u16 |
| 223 | virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec, |
| 224 | u16 queue_id) |
| 225 | { |
| 226 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 227 | &queue_id); |
| 228 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 229 | &vec); |
| 230 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 231 | &vec); |
| 232 | return vec; |
| 233 | } |
| 234 | |
| 235 | static u32 |
| 236 | virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, |
| 237 | u32 flags) |
| 238 | { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static clib_error_t * |
| 243 | virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 244 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 245 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 246 | virtio_net_config_t config; |
| 247 | clib_error_t *error = 0; |
| 248 | u16 max_queue_pairs = 1; |
| 249 | |
| 250 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 251 | { |
| 252 | virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs, |
| 253 | sizeof (config.max_virtqueue_pairs), 8); |
| 254 | max_queue_pairs = config.max_virtqueue_pairs; |
| 255 | } |
| 256 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 257 | 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] | 258 | if (max_queue_pairs < 1 || max_queue_pairs > 0x8000) |
| 259 | clib_error_return (error, "max queue pair is %x", max_queue_pairs); |
| 260 | |
| 261 | vif->max_queue_pairs = max_queue_pairs; |
| 262 | return error; |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 267 | { |
| 268 | virtio_pci_legacy_write_config (vm, vif, vif->mac_addr, |
| 269 | sizeof (vif->mac_addr), 0); |
| 270 | } |
| 271 | |
| 272 | static u32 |
| 273 | virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 274 | { |
| 275 | if (vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
| 276 | { |
| 277 | virtio_pci_legacy_read_config (vm, vif, vif->mac_addr, |
| 278 | sizeof (vif->mac_addr), 0); |
| 279 | return 0; |
| 280 | } |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | static u16 |
| 285 | virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif) |
| 286 | { |
| 287 | /* |
| 288 | * Minimal driver: assumes link is up |
| 289 | */ |
| 290 | u16 status = 1; |
| 291 | if (vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)) |
| 292 | virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */ |
| 293 | 6); |
| 294 | return status; |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line) |
| 299 | { |
| 300 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 301 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 302 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 303 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 304 | u16 qid = line; |
| 305 | |
| 306 | vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); |
| 307 | } |
| 308 | |
| 309 | static void |
| 310 | virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line) |
| 311 | { |
| 312 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 313 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 314 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 315 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 316 | |
| 317 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 318 | { |
| 319 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 320 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 321 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 326 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h) |
| 332 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 333 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 334 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 335 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 336 | u8 isr = 0; |
| 337 | u16 line = 0; |
| 338 | |
| 339 | isr = virtio_pci_legacy_get_isr (vm, vif); |
| 340 | |
| 341 | /* |
| 342 | * If the lower bit is set: look through the used rings of |
| 343 | * all virtqueues for the device, to see if any progress has |
| 344 | * been made by the device which requires servicing. |
| 345 | */ |
| 346 | if (isr & VIRTIO_PCI_ISR_INTR) |
| 347 | virtio_pci_irq_0_handler (vm, h, line); |
| 348 | |
| 349 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
| 350 | virtio_pci_irq_1_handler (vm, h, line); |
| 351 | } |
| 352 | |
| 353 | inline void |
| 354 | device_status (vlib_main_t * vm, virtio_if_t * vif) |
| 355 | { |
| 356 | struct status_struct |
| 357 | { |
| 358 | u8 bit; |
| 359 | char *str; |
| 360 | }; |
| 361 | struct status_struct *status_entry; |
| 362 | static struct status_struct status_array[] = { |
| 363 | #define _(s,b) { .str = #s, .bit = b, }, |
| 364 | foreach_virtio_config_status_flags |
| 365 | #undef _ |
| 366 | {.str = NULL} |
| 367 | }; |
| 368 | |
| 369 | vlib_cli_output (vm, " status 0x%x", vif->status); |
| 370 | |
| 371 | status_entry = (struct status_struct *) &status_array; |
| 372 | while (status_entry->str) |
| 373 | { |
| 374 | if (vif->status & status_entry->bit) |
| 375 | vlib_cli_output (vm, " %s (%x)", status_entry->str, |
| 376 | status_entry->bit); |
| 377 | status_entry++; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | inline void |
| 382 | debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif) |
| 383 | { |
| 384 | u32 data_u32; |
| 385 | u16 data_u16; |
| 386 | u8 data_u8; |
| 387 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES, |
| 388 | &data_u32); |
| 389 | vlib_cli_output (vm, "remote features 0x%lx", data_u32); |
| 390 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 391 | &data_u32); |
| 392 | vlib_cli_output (vm, "guest features 0x%lx", data_u32); |
| 393 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 394 | &data_u32); |
| 395 | vlib_cli_output (vm, "queue address 0x%lx", data_u32); |
| 396 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 397 | &data_u16); |
| 398 | vlib_cli_output (vm, "queue size 0x%x", data_u16); |
| 399 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 400 | &data_u16); |
| 401 | vlib_cli_output (vm, "queue select 0x%x", data_u16); |
| 402 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 403 | &data_u16); |
| 404 | vlib_cli_output (vm, "queue notify 0x%x", data_u16); |
| 405 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8); |
| 406 | vlib_cli_output (vm, "status 0x%x", data_u8); |
| 407 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8); |
| 408 | vlib_cli_output (vm, "isr 0x%x", data_u8); |
| 409 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 410 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 411 | { |
| 412 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 413 | &data_u16); |
| 414 | vlib_cli_output (vm, "config vector 0x%x", data_u16); |
| 415 | u16 queue_id = 0; |
| 416 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 417 | &queue_id); |
| 418 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 419 | &data_u16); |
| 420 | vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16); |
| 421 | } |
| 422 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 423 | u8 mac[6]; |
| 424 | virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0); |
| 425 | vlib_cli_output (vm, "mac %U", format_ethernet_address, mac); |
| 426 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */ |
| 427 | 6); |
| 428 | vlib_cli_output (vm, "link up/down status 0x%x", data_u16); |
| 429 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), |
| 430 | /* offset to max_virtqueue */ 8); |
| 431 | vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16); |
| 432 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */ |
| 433 | 10); |
| 434 | vlib_cli_output (vm, "mtu 0x%x", data_u16); |
| 435 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 436 | u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 437 | i += a; |
| 438 | i &= ~a; |
| 439 | for (; i < 64; i += 4) |
| 440 | { |
| 441 | u32 data = 0; |
| 442 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data); |
| 443 | vlib_cli_output (vm, "0x%lx", data); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static u8 |
| 448 | virtio_pci_queue_size_valid (u16 qsz) |
| 449 | { |
| 450 | if (qsz < 64 || qsz > 4096) |
| 451 | return 0; |
| 452 | if ((qsz % 64) != 0) |
| 453 | return 0; |
| 454 | return 1; |
| 455 | } |
| 456 | |
| 457 | clib_error_t * |
| 458 | virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx) |
| 459 | { |
| 460 | clib_error_t *error = 0; |
| 461 | u16 queue_size = 0; |
| 462 | virtio_vring_t *vring; |
| 463 | struct vring vr; |
| 464 | u32 i = 0; |
| 465 | void *ptr; |
| 466 | |
| 467 | queue_size = virtio_pci_legacy_get_queue_num (vm, vif, idx); |
| 468 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 469 | clib_warning ("queue size is not valid"); |
| 470 | |
| 471 | if (!is_pow2 (queue_size)) |
| 472 | return clib_error_return (0, "ring size must be power of 2"); |
| 473 | |
| 474 | if (queue_size > 32768) |
| 475 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 476 | |
| 477 | if (queue_size == 0) |
| 478 | queue_size = 256; |
| 479 | |
| 480 | vec_validate_aligned (vif->vrings, idx, CLIB_CACHE_LINE_BYTES); |
| 481 | vring = vec_elt_at_index (vif->vrings, idx); |
| 482 | |
| 483 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 484 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
| 485 | ptr = vlib_physmem_alloc_aligned (vm, i, VIRTIO_PCI_VRING_ALIGN); |
| 486 | memset (ptr, 0, i); |
| 487 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 488 | vring->desc = vr.desc; |
| 489 | vring->avail = vr.avail; |
| 490 | vring->used = vr.used; |
| 491 | vring->queue_id = idx; |
| 492 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 493 | |
| 494 | ASSERT (vring->buffers == 0); |
| 495 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
| 496 | ASSERT (vring->indirect_buffers == 0); |
| 497 | vec_validate_aligned (vring->indirect_buffers, queue_size, |
| 498 | CLIB_CACHE_LINE_BYTES); |
| 499 | if (idx % 2) |
| 500 | { |
| 501 | u32 n_alloc = 0; |
| 502 | do |
| 503 | { |
| 504 | if (n_alloc < queue_size) |
| 505 | n_alloc = |
| 506 | vlib_buffer_alloc (vm, vring->indirect_buffers + n_alloc, |
| 507 | queue_size - n_alloc); |
| 508 | } |
| 509 | while (n_alloc != queue_size); |
| 510 | vif->tx_ring_sz = queue_size; |
| 511 | } |
| 512 | else |
| 513 | vif->rx_ring_sz = queue_size; |
| 514 | vring->size = queue_size; |
| 515 | |
| 516 | virtio_pci_legacy_setup_queue (vm, vif, idx, ptr); |
| 517 | vring->kick_fd = -1; |
| 518 | |
| 519 | return error; |
| 520 | } |
| 521 | |
| 522 | static void |
| 523 | virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif, |
| 524 | u64 req_features) |
| 525 | { |
| 526 | /* |
| 527 | * if features are not requested |
| 528 | * default: all supported features |
| 529 | */ |
| 530 | u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_MTU) |
| 531 | | VIRTIO_FEATURE (VIRTIO_NET_F_MAC) |
| 532 | | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
| 533 | | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 534 | | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 535 | | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
| 536 | | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 537 | |
| 538 | if (req_features == 0) |
| 539 | { |
| 540 | req_features = supported_features; |
| 541 | } |
| 542 | |
| 543 | vif->features = req_features & vif->remote_features & supported_features; |
| 544 | |
| 545 | if (vif-> |
| 546 | remote_features & vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU)) |
| 547 | { |
| 548 | virtio_net_config_t config; |
| 549 | virtio_pci_legacy_read_config (vm, vif, &config.mtu, |
| 550 | sizeof (config.mtu), 10); |
| 551 | if (config.mtu < 64) |
| 552 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU); |
| 553 | } |
| 554 | |
| 555 | vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features); |
| 556 | } |
| 557 | |
| 558 | void |
| 559 | virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif) |
| 560 | { |
| 561 | vif->remote_features = virtio_pci_legacy_get_features (vm, vif); |
| 562 | } |
| 563 | |
| 564 | int |
| 565 | virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif) |
| 566 | { |
| 567 | u8 status = 0; |
| 568 | |
| 569 | /* |
| 570 | * Reset the device |
| 571 | */ |
| 572 | status = virtio_pci_legacy_reset (vm, vif); |
| 573 | |
| 574 | /* |
| 575 | * Set the Acknowledge status bit |
| 576 | */ |
| 577 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK); |
| 578 | |
| 579 | /* |
| 580 | * Set the Driver status bit |
| 581 | */ |
| 582 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER); |
| 583 | |
| 584 | /* |
| 585 | * Read the status and verify it |
| 586 | */ |
| 587 | status = virtio_pci_legacy_get_status (vm, vif); |
| 588 | if (! |
| 589 | ((status & VIRTIO_CONFIG_STATUS_ACK) |
| 590 | && (status & VIRTIO_CONFIG_STATUS_DRIVER))) |
| 591 | return -1; |
| 592 | vif->status = status; |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | clib_error_t * |
| 598 | virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif) |
| 599 | { |
| 600 | clib_error_t *error = 0; |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 601 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 602 | struct virtio_pci_cap cap; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 603 | 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] | 604 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 605 | |
| 606 | 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] | 607 | { |
| 608 | virtio_log_error (vim, vif, "error in reading capabilty list position"); |
| 609 | clib_error_return (error, "error in reading capabilty list position"); |
| 610 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 611 | while (pos) |
| 612 | { |
| 613 | if ((error = |
| 614 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap, |
| 615 | sizeof (cap)))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 616 | { |
| 617 | virtio_log_error (vim, vif, "%s [%2x]", |
| 618 | "error in reading the capability at", pos); |
| 619 | clib_error_return (error, |
| 620 | "error in reading the capability at [%2x]", pos); |
| 621 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 622 | |
| 623 | if (cap.cap_vndr == PCI_CAP_ID_MSIX) |
| 624 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 625 | u16 flags, table_size, table_size_mask = 0x07FF; |
| 626 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 627 | if ((error = |
| 628 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags, |
| 629 | sizeof (flags)))) |
| 630 | clib_error_return (error, |
| 631 | "error in reading the capability at [%2x]", |
| 632 | pos + 2); |
| 633 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 634 | table_size = flags & table_size_mask; |
| 635 | virtio_log_debug (vim, vif, "flags:0x%x %s 0x%x", flags, |
| 636 | "msix interrupt vector table-size", table_size); |
| 637 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 638 | if (flags & PCI_MSIX_ENABLE) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 639 | { |
| 640 | virtio_log_debug (vim, vif, "msix interrupt enabled"); |
| 641 | vif->msix_enabled = VIRTIO_MSIX_ENABLED; |
| 642 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 643 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 644 | { |
| 645 | virtio_log_debug (vim, vif, "msix interrupt disabled"); |
| 646 | vif->msix_enabled = VIRTIO_MSIX_DISABLED; |
| 647 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (cap.cap_vndr != PCI_CAP_ID_VNDR) |
| 651 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 652 | virtio_log_debug (vim, vif, "[%2x] %s %2x ", pos, |
| 653 | "skipping non VNDR cap id:", cap.cap_vndr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 654 | goto next; |
| 655 | } |
| 656 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 657 | virtio_log_debug (vim, vif, |
| 658 | "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u", |
| 659 | pos, cap.cfg_type, cap.bar, cap.offset, cap.length); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 660 | switch (cap.cfg_type) |
| 661 | { |
| 662 | case VIRTIO_PCI_CAP_COMMON_CFG: |
| 663 | common_cfg = 1; |
| 664 | break; |
| 665 | case VIRTIO_PCI_CAP_NOTIFY_CFG: |
| 666 | notify_base = 1; |
| 667 | break; |
| 668 | case VIRTIO_PCI_CAP_DEVICE_CFG: |
| 669 | dev_cfg = 1; |
| 670 | break; |
| 671 | case VIRTIO_PCI_CAP_ISR_CFG: |
| 672 | isr = 1; |
| 673 | break; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 674 | case VIRTIO_PCI_CAP_PCI_CFG: |
| 675 | if (cap.bar == 0) |
| 676 | pci_cfg = 1; |
| 677 | break; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 678 | } |
| 679 | next: |
| 680 | pos = cap.cap_next; |
| 681 | } |
| 682 | |
| 683 | if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0) |
| 684 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 685 | virtio_log_debug (vim, vif, "legacy virtio pci device found"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 686 | return error; |
| 687 | } |
| 688 | |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 689 | if (!pci_cfg) |
| 690 | clib_error_return (error, "modern virtio pci device found"); |
| 691 | |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 692 | virtio_log_debug (vim, vif, "transitional virtio pci device found"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 693 | return error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static clib_error_t * |
| 697 | virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, |
| 698 | virtio_pci_create_if_args_t * args) |
| 699 | { |
| 700 | clib_error_t *error = 0; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 701 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 702 | u8 status = 0; |
| 703 | |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 704 | if ((error = virtio_pci_read_caps (vm, vif))) |
| 705 | clib_error_return (error, "Device not supported"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 706 | |
| 707 | if (virtio_pci_reset_device (vm, vif) < 0) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 708 | { |
| 709 | virtio_log_error (vim, vif, "Failed to reset the device"); |
| 710 | clib_error_return (error, "Failed to reset the device"); |
| 711 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 712 | /* |
| 713 | * read device features and negotiate (user) requested features |
| 714 | */ |
| 715 | virtio_pci_read_device_feature (vm, vif); |
| 716 | virtio_negotiate_features (vm, vif, args->features); |
| 717 | |
| 718 | /* |
| 719 | * After FEATURE_OK, driver should not accept new feature bits |
| 720 | */ |
| 721 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK); |
| 722 | status = virtio_pci_legacy_get_status (vm, vif); |
| 723 | if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK)) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 724 | { |
| 725 | virtio_log_error (vim, vif, |
| 726 | "error encountered: Device doesn't support requested features"); |
| 727 | clib_error_return (error, "Device doesn't support requested features"); |
| 728 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 729 | vif->status = status; |
| 730 | |
| 731 | if (virtio_pci_get_mac (vm, vif)) |
| 732 | { |
| 733 | f64 now = vlib_time_now (vm); |
| 734 | u32 rnd; |
| 735 | rnd = (u32) (now * 1e6); |
| 736 | rnd = random_u32 (&rnd); |
| 737 | |
| 738 | memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd)); |
| 739 | vif->mac_addr[0] = 2; |
| 740 | vif->mac_addr[1] = 0xfe; |
| 741 | virtio_pci_set_mac (vm, vif); |
| 742 | } |
| 743 | |
| 744 | virtio_set_net_hdr_size (vif); |
| 745 | |
| 746 | if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif))) |
| 747 | goto error; |
| 748 | |
| 749 | if ((error = virtio_pci_vring_init (vm, vif, 0))) |
| 750 | goto error; |
| 751 | |
| 752 | if ((error = virtio_pci_vring_init (vm, vif, 1))) |
| 753 | goto error; |
| 754 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 755 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 756 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 757 | if (virtio_pci_legacy_set_config_irq (vm, vif, 1) == |
| 758 | VIRTIO_MSI_NO_VECTOR) |
| 759 | virtio_log_warning (vim, vif, "config vector 1 is not set"); |
| 760 | if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) == |
| 761 | VIRTIO_MSI_NO_VECTOR) |
| 762 | virtio_log_warning (vim, vif, "queue vector 0 is not set"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 763 | } |
| 764 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK); |
| 765 | vif->status = virtio_pci_legacy_get_status (vm, vif); |
| 766 | error: |
| 767 | return error; |
| 768 | } |
| 769 | |
| 770 | void |
| 771 | virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) |
| 772 | { |
| 773 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 774 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 775 | virtio_if_t *vif; |
| 776 | vlib_pci_dev_handle_t h; |
| 777 | clib_error_t *error = 0; |
| 778 | |
| 779 | if (args->rxq_size == 0) |
| 780 | args->rxq_size = VIRTIO_NUM_RX_DESC; |
| 781 | if (args->txq_size == 0) |
| 782 | args->txq_size = VIRTIO_NUM_TX_DESC; |
| 783 | |
| 784 | if (!virtio_pci_queue_size_valid (args->rxq_size) || |
| 785 | !virtio_pci_queue_size_valid (args->txq_size)) |
| 786 | { |
| 787 | args->rv = VNET_API_ERROR_INVALID_VALUE; |
| 788 | args->error = |
| 789 | clib_error_return (error, |
| 790 | "queue size must be <= 4096, >= 64, " |
| 791 | "and multiples of 64"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 792 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 793 | format_vlib_pci_addr, &args->addr, |
| 794 | "queue size must be <= 4096, >= 64, and multiples of 64"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 795 | return; |
| 796 | } |
| 797 | |
| 798 | /* *INDENT-OFF* */ |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 799 | pool_foreach (vif, vim->interfaces, ({ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 800 | if (vif->pci_addr.as_u32 == args->addr) |
| 801 | { |
| 802 | args->rv = VNET_API_ERROR_INVALID_VALUE; |
| 803 | args->error = |
| 804 | clib_error_return (error, "PCI address in use"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 805 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 806 | format_vlib_pci_addr, &args->addr, |
| 807 | " PCI address in use"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 808 | return; |
| 809 | } |
| 810 | })); |
| 811 | /* *INDENT-ON* */ |
| 812 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 813 | pool_get (vim->interfaces, vif); |
| 814 | vif->dev_instance = vif - vim->interfaces; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 815 | vif->per_interface_next_index = ~0; |
| 816 | vif->pci_addr.as_u32 = args->addr; |
| 817 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 818 | if ((error = |
| 819 | vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr, |
| 820 | virtio_pci_device_ids, &h))) |
| 821 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 822 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 823 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 824 | args->error = |
| 825 | clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, |
| 826 | &vif->pci_addr); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 827 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 828 | format_vlib_pci_addr, &vif->pci_addr, |
| 829 | "error encountered on pci device open"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | vif->pci_dev_handle = h; |
| 833 | vlib_pci_set_private_data (vm, h, vif->dev_instance); |
| 834 | |
| 835 | if ((error = vlib_pci_bus_master_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 836 | { |
| 837 | virtio_log_error (vim, vif, |
| 838 | "error encountered on pci bus master enable"); |
| 839 | goto error; |
| 840 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 841 | |
| 842 | if ((error = vlib_pci_io_region (vm, h, 0))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 843 | { |
| 844 | virtio_log_error (vim, vif, "error encountered on pci io region"); |
| 845 | goto error; |
| 846 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 847 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 848 | if (vlib_pci_get_num_msix_interrupts (vm, h) > 1) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 849 | { |
| 850 | if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, |
| 851 | &virtio_pci_irq_0_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 852 | { |
| 853 | virtio_log_error (vim, vif, |
| 854 | "error encountered on pci register msix handler 0"); |
| 855 | goto error; |
| 856 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 857 | if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1, |
| 858 | &virtio_pci_irq_1_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 859 | { |
| 860 | virtio_log_error (vim, vif, |
| 861 | "error encountered on pci register msix handler 1"); |
| 862 | goto error; |
| 863 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 864 | |
| 865 | if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 866 | { |
| 867 | virtio_log_error (vim, vif, |
| 868 | "error encountered on pci enable msix irq"); |
| 869 | goto error; |
| 870 | } |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 871 | vif->support_int_mode = 1; |
| 872 | virtio_log_debug (vim, vif, "device supports msix interrupts"); |
| 873 | } |
| 874 | else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1) |
| 875 | { |
| 876 | /* |
| 877 | * if msix table-size is 1, fall back to intX. |
| 878 | */ |
| 879 | if ((error = |
| 880 | vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler))) |
| 881 | { |
| 882 | virtio_log_error (vim, vif, |
| 883 | "error encountered on pci register interrupt handler"); |
| 884 | goto error; |
| 885 | } |
| 886 | vif->support_int_mode = 1; |
| 887 | virtio_log_debug (vim, vif, "pci register interrupt handler"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 888 | } |
| 889 | else |
| 890 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 891 | /* |
| 892 | * WARN: intX is showing some weird behaviour. |
| 893 | * Please don't use interrupt mode with UIO driver. |
| 894 | */ |
| 895 | vif->support_int_mode = 0; |
| 896 | virtio_log_debug (vim, vif, "driver is configured in poll mode only"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | if ((error = vlib_pci_intr_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 900 | { |
| 901 | virtio_log_error (vim, vif, |
| 902 | "error encountered on pci interrupt enable"); |
| 903 | goto error; |
| 904 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 905 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 906 | if ((error = virtio_pci_device_init (vm, vif, args))) |
| 907 | { |
| 908 | virtio_log_error (vim, vif, "error encountered on device init"); |
| 909 | goto error; |
| 910 | } |
| 911 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 912 | vif->type = VIRTIO_IF_TYPE_PCI; |
| 913 | /* create interface */ |
| 914 | error = ethernet_register_interface (vnm, virtio_device_class.index, |
| 915 | vif->dev_instance, vif->mac_addr, |
| 916 | &vif->hw_if_index, |
| 917 | virtio_pci_flag_change); |
| 918 | |
| 919 | if (error) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 920 | { |
| 921 | virtio_log_error (vim, vif, |
| 922 | "error encountered on ethernet register interface"); |
| 923 | goto error; |
| 924 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 925 | |
| 926 | vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 927 | vif->sw_if_index = sw->sw_if_index; |
| 928 | args->sw_if_index = sw->sw_if_index; |
| 929 | |
| 930 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 931 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 932 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 933 | virtio_input_node.index); |
| 934 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 935 | virtio_vring_set_numa_node (vm, vif, 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 936 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 937 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 938 | VNET_HW_INTERFACE_RX_MODE_POLLING); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 939 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 940 | { |
| 941 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 942 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 943 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 944 | } |
| 945 | else |
| 946 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 947 | return; |
| 948 | |
| 949 | error: |
| 950 | virtio_pci_delete_if (vm, vif); |
| 951 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 952 | args->error = error; |
| 953 | } |
| 954 | |
| 955 | int |
| 956 | virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) |
| 957 | { |
| 958 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 959 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 960 | u32 i = 0; |
| 961 | |
| 962 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 963 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 964 | |
| 965 | vlib_pci_intr_disable (vm, vif->pci_dev_handle); |
| 966 | |
| 967 | virtio_pci_legacy_del_queue (vm, vif, 0); |
| 968 | virtio_pci_legacy_del_queue (vm, vif, 1); |
| 969 | |
| 970 | virtio_pci_legacy_reset (vm, vif); |
| 971 | |
| 972 | if (vif->hw_if_index) |
| 973 | { |
| 974 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 975 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0); |
| 976 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 977 | } |
| 978 | |
| 979 | vlib_pci_device_close (vm, vif->pci_dev_handle); |
| 980 | |
| 981 | vec_foreach_index (i, vif->vrings) |
| 982 | { |
| 983 | virtio_vring_t *vring = vec_elt_at_index (vif->vrings, i); |
| 984 | if (vring->kick_fd != -1) |
| 985 | close (vring->kick_fd); |
| 986 | if (vring->used) |
| 987 | { |
| 988 | if ((i & 1) == 1) |
| 989 | virtio_free_used_desc (vm, vring); |
| 990 | else |
| 991 | virtio_free_rx_buffers (vm, vring); |
| 992 | } |
| 993 | if (vring->queue_id % 2) |
| 994 | { |
| 995 | vlib_buffer_free_no_next (vm, vring->indirect_buffers, vring->size); |
| 996 | } |
| 997 | vec_free (vring->buffers); |
| 998 | vec_free (vring->indirect_buffers); |
| 999 | vlib_physmem_free (vm, vring->desc); |
| 1000 | } |
| 1001 | |
| 1002 | vec_free (vif->vrings); |
| 1003 | |
| 1004 | if (vif->fd != -1) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1005 | vif->fd = -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1006 | if (vif->tap_fd != -1) |
| 1007 | vif->tap_fd = -1; |
| 1008 | clib_error_free (vif->error); |
| 1009 | memset (vif, 0, sizeof (*vif)); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1010 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * fd.io coding-style-patch-verification: ON |
| 1017 | * |
| 1018 | * Local Variables: |
| 1019 | * eval: (c-set-style "gnu") |
| 1020 | * End: |
| 1021 | */ |