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