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