Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 18 | |
| 19 | #include <vppinfra/types.h> |
| 20 | #include <vlib/vlib.h> |
| 21 | #include <vlib/pci/pci.h> |
| 22 | #include <vnet/ethernet/ethernet.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 23 | #include <vnet/ip/ip4_packet.h> |
| 24 | #include <vnet/ip/ip6_packet.h> |
| 25 | #include <vnet/devices/virtio/virtio.h> |
| 26 | #include <vnet/devices/virtio/pci.h> |
| 27 | |
| 28 | #define PCI_VENDOR_ID_VIRTIO 0x1af4 |
| 29 | #define PCI_DEVICE_ID_VIRTIO_NIC 0x1000 |
| 30 | /* Doesn't support modern device */ |
| 31 | #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041 |
| 32 | |
| 33 | #define PCI_CAPABILITY_LIST 0x34 |
| 34 | #define PCI_CAP_ID_VNDR 0x09 |
| 35 | #define PCI_CAP_ID_MSIX 0x11 |
| 36 | |
| 37 | #define PCI_MSIX_ENABLE 0x8000 |
| 38 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 39 | static pci_device_id_t virtio_pci_device_ids[] = { |
| 40 | { |
| 41 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 42 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC}, |
| 43 | { |
| 44 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 45 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN}, |
| 46 | {0}, |
| 47 | }; |
| 48 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 49 | static u32 |
| 50 | virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, |
| 51 | u32 flags) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static clib_error_t * |
| 57 | virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 58 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 59 | clib_error_t *error = 0; |
| 60 | u16 max_queue_pairs = 1; |
| 61 | |
| 62 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 63 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 64 | 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] | 65 | } |
| 66 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 67 | virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 68 | if (max_queue_pairs < 1 || max_queue_pairs > 0x8000) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 69 | return clib_error_return (error, "max queue pair is %x," |
| 70 | " should be in range [1, 0x8000]", |
| 71 | max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 72 | |
| 73 | vif->max_queue_pairs = max_queue_pairs; |
| 74 | return error; |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 79 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 80 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
| 81 | vif->virtio_pci_func->set_mac (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static u32 |
| 85 | virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 86 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 87 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 88 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 89 | vif->virtio_pci_func->get_mac (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | static u16 |
| 96 | virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif) |
| 97 | { |
| 98 | /* |
| 99 | * Minimal driver: assumes link is up |
| 100 | */ |
| 101 | u16 status = 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 102 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 103 | status = vif->virtio_pci_func->get_device_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 104 | return status; |
| 105 | } |
| 106 | |
| 107 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 108 | virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 109 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 110 | { |
| 111 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 112 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 113 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 114 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 115 | line--; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 116 | u16 qid = line; |
| 117 | |
| 118 | vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); |
| 119 | } |
| 120 | |
| 121 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 122 | virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 123 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 124 | { |
| 125 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 126 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 127 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 128 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 129 | |
| 130 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 131 | { |
| 132 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 133 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 134 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 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 | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 198 | virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif, |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 199 | virtio_ctrl_msg_t * data, u32 len) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 200 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 201 | virtio_vring_t *vring = vif->cxq_vring; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 202 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
| 203 | virtio_ctrl_msg_t result; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 204 | u32 buffer_index; |
| 205 | vlib_buffer_t *b; |
| 206 | u16 used, next, avail; |
| 207 | u16 sz = vring->size; |
| 208 | u16 mask = sz - 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 209 | |
| 210 | used = vring->desc_in_use; |
| 211 | next = vring->desc_next; |
| 212 | avail = vring->avail->idx; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 213 | vring_desc_t *d = &vring->desc[next]; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 214 | |
| 215 | if (vlib_buffer_alloc (vm, &buffer_index, 1)) |
| 216 | b = vlib_get_buffer (vm, buffer_index); |
| 217 | else |
| 218 | return VIRTIO_NET_ERR; |
| 219 | /* |
| 220 | * current_data may not be initialized with 0 and may contain |
| 221 | * previous offset. |
| 222 | */ |
| 223 | b->current_data = 0; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 224 | 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] | 225 | d->flags = VRING_DESC_F_NEXT; |
| 226 | d->addr = vlib_buffer_get_current_pa (vm, b); |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 227 | d->len = sizeof (virtio_net_ctrl_hdr_t); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 228 | vring->avail->ring[avail & mask] = next; |
| 229 | avail++; |
| 230 | next = (next + 1) & mask; |
| 231 | d->next = next; |
| 232 | used++; |
| 233 | |
| 234 | d = &vring->desc[next]; |
| 235 | d->flags = VRING_DESC_F_NEXT; |
| 236 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 237 | STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 238 | d->len = len; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 239 | next = (next + 1) & mask; |
| 240 | d->next = next; |
| 241 | used++; |
| 242 | |
| 243 | d = &vring->desc[next]; |
| 244 | d->flags = VRING_DESC_F_WRITE; |
| 245 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 246 | STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 247 | d->len = sizeof (data->status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 248 | next = (next + 1) & mask; |
| 249 | used++; |
| 250 | |
| 251 | CLIB_MEMORY_STORE_BARRIER (); |
| 252 | vring->avail->idx = avail; |
| 253 | vring->desc_next = next; |
| 254 | vring->desc_in_use = used; |
| 255 | |
| 256 | if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0) |
| 257 | { |
| 258 | virtio_kick (vm, vring, vif); |
| 259 | } |
| 260 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 261 | u16 last = vring->last_used_idx, n_left = 0; |
| 262 | n_left = vring->used->idx - last; |
| 263 | |
| 264 | while (n_left) |
| 265 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 266 | vring_used_elem_t *e = &vring->used->ring[last & mask]; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 267 | u16 slot = e->id; |
| 268 | |
| 269 | d = &vring->desc[slot]; |
| 270 | while (d->flags & VRING_DESC_F_NEXT) |
| 271 | { |
| 272 | used--; |
| 273 | slot = d->next; |
| 274 | d = &vring->desc[slot]; |
| 275 | } |
| 276 | used--; |
| 277 | last++; |
| 278 | n_left--; |
| 279 | } |
| 280 | vring->desc_in_use = used; |
| 281 | vring->last_used_idx = last; |
| 282 | |
| 283 | CLIB_MEMORY_BARRIER (); |
| 284 | clib_memcpy (&result, vlib_buffer_get_current (b), |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 285 | sizeof (virtio_ctrl_msg_t)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 286 | virtio_log_debug (vif, "ctrl-queue: status %u", result.status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 287 | status = result.status; |
| 288 | vlib_buffer_free (vm, &buffer_index, 1); |
| 289 | return status; |
| 290 | } |
| 291 | |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 292 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 293 | virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 294 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 295 | virtio_ctrl_msg_t offload_hdr; |
| 296 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 297 | |
| 298 | offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 299 | offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 300 | offload_hdr.status = VIRTIO_NET_ERR; |
| 301 | u64 offloads = 0ULL; |
| 302 | clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads)); |
| 303 | |
| 304 | status = |
| 305 | virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads)); |
| 306 | virtio_log_debug (vif, "disable offloads"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 307 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 308 | vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 309 | return status; |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 314 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 315 | virtio_ctrl_msg_t csum_offload_hdr; |
| 316 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 317 | |
| 318 | csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 319 | csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 320 | csum_offload_hdr.status = VIRTIO_NET_ERR; |
| 321 | u64 offloads = 0ULL; |
| 322 | offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM); |
| 323 | clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads)); |
| 324 | |
| 325 | status = |
| 326 | virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads)); |
| 327 | virtio_log_debug (vif, "enable checksum offload"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 328 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 329 | vif->features = vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 330 | return status; |
| 331 | } |
| 332 | |
| 333 | static int |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 334 | virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif) |
| 335 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 336 | virtio_ctrl_msg_t gso_hdr; |
| 337 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 338 | |
| 339 | gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 340 | gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 341 | gso_hdr.status = VIRTIO_NET_ERR; |
| 342 | u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 343 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 344 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 345 | clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads)); |
| 346 | |
| 347 | status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 348 | virtio_log_debug (vif, "enable gso"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 349 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 350 | vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 351 | return status; |
| 352 | } |
| 353 | |
| 354 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 355 | virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, |
| 356 | int csum_offload_enabled) |
| 357 | { |
| 358 | vnet_main_t *vnm = vnet_get_main (); |
| 359 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 360 | |
| 361 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 362 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))) |
| 363 | { |
| 364 | if (gso_enabled |
| 365 | && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | |
| 366 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)))) |
| 367 | { |
| 368 | if (virtio_pci_enable_gso (vm, vif)) |
| 369 | { |
| 370 | virtio_log_warning (vif, "gso is not enabled"); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | vif->gso_enabled = 1; |
| 375 | vif->csum_offload_enabled = 0; |
| 376 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO | |
| 377 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD; |
| 378 | } |
| 379 | } |
| 380 | else if (csum_offload_enabled |
| 381 | && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))) |
| 382 | { |
| 383 | if (virtio_pci_enable_checksum_offload (vm, vif)) |
| 384 | { |
| 385 | virtio_log_warning (vif, "checksum offload is not enabled"); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | vif->csum_offload_enabled = 1; |
| 390 | vif->gso_enabled = 0; |
| 391 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 392 | hw->flags |= |
| 393 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD; |
| 394 | } |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | if (virtio_pci_disable_offload (vm, vif)) |
| 399 | { |
| 400 | virtio_log_warning (vif, "offloads are not disabled"); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | vif->csum_offload_enabled = 0; |
| 405 | vif->gso_enabled = 0; |
| 406 | hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO | |
| 407 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 416 | virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif, |
| 417 | u16 num_queues) |
| 418 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 419 | virtio_ctrl_msg_t mq_hdr; |
| 420 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 421 | |
| 422 | mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ; |
| 423 | mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; |
| 424 | mq_hdr.status = VIRTIO_NET_ERR; |
| 425 | clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues)); |
| 426 | |
| 427 | 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] | 428 | virtio_log_debug (vif, "multi-queue enable %u queues", num_queues); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 429 | return status; |
| 430 | } |
| 431 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 432 | static u8 |
| 433 | virtio_pci_queue_size_valid (u16 qsz) |
| 434 | { |
| 435 | if (qsz < 64 || qsz > 4096) |
| 436 | return 0; |
| 437 | if ((qsz % 64) != 0) |
| 438 | return 0; |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | clib_error_t * |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 443 | virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif, |
| 444 | u16 queue_num) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 445 | { |
| 446 | clib_error_t *error = 0; |
| 447 | u16 queue_size = 0; |
| 448 | virtio_vring_t *vring; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 449 | vring_t vr; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 450 | u32 i = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 451 | void *ptr = NULL; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 452 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 453 | 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] | 454 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 455 | clib_warning ("queue size is not valid"); |
| 456 | |
| 457 | if (!is_pow2 (queue_size)) |
| 458 | return clib_error_return (0, "ring size must be power of 2"); |
| 459 | |
| 460 | if (queue_size > 32768) |
| 461 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 462 | |
| 463 | if (queue_size == 0) |
| 464 | queue_size = 256; |
| 465 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 466 | vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); |
| 467 | vring = vec_elt_at_index (vif->cxq_vring, 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 468 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 469 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 470 | ptr = |
| 471 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 472 | vif->numa_node); |
| 473 | if (!ptr) |
| 474 | return vlib_physmem_last_error (vm); |
| 475 | clib_memset (ptr, 0, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 476 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 477 | vring->desc = vr.desc; |
| 478 | vring->avail = vr.avail; |
| 479 | vring->used = vr.used; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 480 | vring->queue_id = queue_num; |
| 481 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 482 | |
| 483 | ASSERT (vring->buffers == 0); |
| 484 | |
| 485 | vring->size = queue_size; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 486 | virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 487 | queue_size); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 488 | vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 489 | vring->kick_fd = -1; |
| 490 | |
| 491 | return error; |
| 492 | } |
| 493 | |
| 494 | clib_error_t * |
| 495 | virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num) |
| 496 | { |
Benoît Ganne | b6b484d | 2020-09-15 10:58:07 +0200 | [diff] [blame^] | 497 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 498 | clib_error_t *error = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 499 | u16 queue_size = 0; |
| 500 | virtio_vring_t *vring; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 501 | vring_t vr; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 502 | u32 i = 0; |
| 503 | void *ptr = NULL; |
| 504 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 505 | 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] | 506 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 507 | clib_warning ("queue size is not valid"); |
| 508 | |
| 509 | if (!is_pow2 (queue_size)) |
| 510 | return clib_error_return (0, "ring size must be power of 2"); |
| 511 | |
| 512 | if (queue_size > 32768) |
| 513 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 514 | |
| 515 | if (queue_size == 0) |
| 516 | queue_size = 256; |
| 517 | |
| 518 | if (queue_num % 2) |
| 519 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 520 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), |
| 521 | CLIB_CACHE_LINE_BYTES); |
| 522 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); |
Benoît Ganne | b6b484d | 2020-09-15 10:58:07 +0200 | [diff] [blame^] | 523 | if (vif->max_queue_pairs < vtm->n_vlib_mains) |
| 524 | clib_spinlock_init (&vring->lockp); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 525 | } |
| 526 | else |
| 527 | { |
| 528 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num), |
| 529 | CLIB_CACHE_LINE_BYTES); |
| 530 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); |
| 531 | } |
| 532 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 533 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
| 534 | ptr = |
| 535 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 536 | vif->numa_node); |
| 537 | if (!ptr) |
| 538 | return vlib_physmem_last_error (vm); |
| 539 | clib_memset (ptr, 0, i); |
| 540 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 541 | vring->desc = vr.desc; |
| 542 | vring->avail = vr.avail; |
| 543 | vring->used = vr.used; |
| 544 | vring->queue_id = queue_num; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 545 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 546 | vring->flow_table = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 547 | |
| 548 | ASSERT (vring->buffers == 0); |
| 549 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 550 | if (queue_num % 2) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 551 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 552 | virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 553 | queue_size); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 554 | clib_memset_u32 (vring->buffers, ~0, queue_size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 555 | } |
| 556 | else |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 557 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 558 | virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 559 | queue_size); |
| 560 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 561 | vring->size = queue_size; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 562 | if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr)) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 563 | return clib_error_return (0, "error in queue address setup"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 564 | |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 565 | vring->kick_fd = -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 566 | return error; |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif, |
| 571 | u64 req_features) |
| 572 | { |
| 573 | /* |
| 574 | * if features are not requested |
| 575 | * default: all supported features |
| 576 | */ |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 577 | u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) |
| 578 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 579 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
| 580 | | VIRTIO_FEATURE (VIRTIO_NET_F_MTU) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 581 | | VIRTIO_FEATURE (VIRTIO_NET_F_MAC) |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 582 | | VIRTIO_FEATURE (VIRTIO_NET_F_GSO) |
| 583 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
| 584 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6) |
| 585 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) |
| 586 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
| 587 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6) |
| 588 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 589 | | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
| 590 | | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 591 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
| 592 | | VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 593 | | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 594 | | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
| 595 | | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 596 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 597 | if (vif->is_modern) |
| 598 | supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 599 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 600 | if (req_features == 0) |
| 601 | { |
| 602 | req_features = supported_features; |
| 603 | } |
| 604 | |
| 605 | vif->features = req_features & vif->remote_features & supported_features; |
| 606 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 607 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 608 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 609 | u16 mtu = 0; |
| 610 | mtu = vif->virtio_pci_func->get_mtu (vm, vif); |
| 611 | |
| 612 | if (mtu < 64) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 613 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU); |
| 614 | } |
| 615 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 616 | vif->virtio_pci_func->set_driver_features (vm, vif, vif->features); |
| 617 | vif->features = vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void |
| 621 | virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif) |
| 622 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 623 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | int |
| 627 | virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif) |
| 628 | { |
| 629 | u8 status = 0; |
| 630 | |
| 631 | /* |
| 632 | * Reset the device |
| 633 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 634 | status = vif->virtio_pci_func->device_reset (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 635 | |
| 636 | /* |
| 637 | * Set the Acknowledge status bit |
| 638 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 639 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 640 | |
| 641 | /* |
| 642 | * Set the Driver status bit |
| 643 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 644 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 645 | |
| 646 | /* |
| 647 | * Read the status and verify it |
| 648 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 649 | status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 650 | if ((status & VIRTIO_CONFIG_STATUS_ACK) |
| 651 | && (status & VIRTIO_CONFIG_STATUS_DRIVER)) |
| 652 | vif->status = status; |
| 653 | else |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 654 | return -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | clib_error_t * |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 660 | 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] | 661 | { |
| 662 | clib_error_t *error = 0; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 663 | virtio_pci_cap_t cap; |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 664 | 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] | 665 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 666 | |
| 667 | 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] | 668 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 669 | virtio_log_error (vif, "error in reading capabilty list position"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 670 | return clib_error_return (error, |
| 671 | "error in reading capabilty list position"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 672 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 673 | while (pos) |
| 674 | { |
| 675 | if ((error = |
| 676 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap, |
| 677 | sizeof (cap)))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 678 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 679 | virtio_log_error (vif, "%s [%2x]", |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 680 | "error in reading the capability at", pos); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 681 | return clib_error_return (error, |
| 682 | "error in reading the capability at [%2x]", |
| 683 | pos); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 684 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 685 | |
| 686 | if (cap.cap_vndr == PCI_CAP_ID_MSIX) |
| 687 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 688 | u16 flags, table_size, table_size_mask = 0x07FF; |
| 689 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 690 | if ((error = |
| 691 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags, |
| 692 | sizeof (flags)))) |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 693 | return clib_error_return (error, |
| 694 | "error in reading the capability at [%2x]", |
| 695 | pos + 2); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 696 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 697 | table_size = flags & table_size_mask; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 698 | virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 699 | "msix interrupt vector table-size", table_size); |
| 700 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 701 | if (flags & PCI_MSIX_ENABLE) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 702 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 703 | virtio_log_debug (vif, "msix interrupt enabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 704 | vif->msix_enabled = VIRTIO_MSIX_ENABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 705 | vif->msix_table_size = table_size; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 706 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 707 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 708 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 709 | virtio_log_debug (vif, "msix interrupt disabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 710 | vif->msix_enabled = VIRTIO_MSIX_DISABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 711 | vif->msix_table_size = 0; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 712 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | if (cap.cap_vndr != PCI_CAP_ID_VNDR) |
| 716 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 717 | virtio_log_debug (vif, "[%2x] %s %2x ", pos, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 718 | "skipping non VNDR cap id:", cap.cap_vndr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 719 | goto next; |
| 720 | } |
| 721 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 722 | virtio_log_debug (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 723 | "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u", |
| 724 | pos, cap.cfg_type, cap.bar, cap.offset, cap.length); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 725 | |
Mohsin Kazmi | 0a507d7 | 2020-09-01 18:18:48 +0200 | [diff] [blame] | 726 | if (cap.bar >= 0 && cap.bar <= 5) |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame] | 727 | { |
| 728 | vif->bar = bar[cap.bar]; |
| 729 | vif->bar_id = cap.bar; |
| 730 | } |
| 731 | else |
| 732 | return clib_error_return (error, "invalid bar %u", cap.bar); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 733 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 734 | switch (cap.cfg_type) |
| 735 | { |
| 736 | case VIRTIO_PCI_CAP_COMMON_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 737 | vif->common_offset = cap.offset; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 738 | common_cfg = 1; |
| 739 | break; |
| 740 | case VIRTIO_PCI_CAP_NOTIFY_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 741 | if ((error = |
| 742 | vlib_pci_read_write_config (vm, h, VLIB_READ, |
| 743 | pos + sizeof (cap), |
| 744 | &vif->notify_off_multiplier, |
| 745 | sizeof |
| 746 | (vif->notify_off_multiplier)))) |
| 747 | { |
| 748 | virtio_log_error (vif, "notify off multiplier is not given"); |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | virtio_log_debug (vif, "notify off multiplier is %u", |
| 753 | vif->notify_off_multiplier); |
| 754 | vif->notify_offset = cap.offset; |
| 755 | notify = 1; |
| 756 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 757 | break; |
| 758 | case VIRTIO_PCI_CAP_DEVICE_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 759 | vif->device_offset = cap.offset; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 760 | dev_cfg = 1; |
| 761 | break; |
| 762 | case VIRTIO_PCI_CAP_ISR_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 763 | vif->isr_offset = cap.offset; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 764 | isr = 1; |
| 765 | break; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 766 | case VIRTIO_PCI_CAP_PCI_CFG: |
| 767 | if (cap.bar == 0) |
| 768 | pci_cfg = 1; |
| 769 | break; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 770 | } |
| 771 | next: |
| 772 | pos = cap.cap_next; |
| 773 | } |
| 774 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 775 | if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 776 | { |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 777 | vif->virtio_pci_func = &virtio_pci_legacy_func; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 778 | virtio_log_debug (vif, "legacy virtio pci device found"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 779 | return error; |
| 780 | } |
| 781 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 782 | vif->is_modern = 1; |
| 783 | vif->virtio_pci_func = &virtio_pci_modern_func; |
| 784 | |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 785 | if (!pci_cfg) |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 786 | virtio_log_debug (vif, "modern virtio pci device found"); |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 787 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 788 | virtio_log_debug (vif, "transitional virtio pci device found"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 789 | return error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static clib_error_t * |
| 793 | virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 794 | virtio_pci_create_if_args_t * args, void **bar) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 795 | { |
| 796 | clib_error_t *error = 0; |
Mohsin Kazmi | edf3b4b | 2019-10-30 15:00:04 +0100 | [diff] [blame] | 797 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 798 | u8 status = 0; |
| 799 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 800 | if ((error = virtio_pci_read_caps (vm, vif, bar))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 801 | { |
| 802 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 803 | virtio_log_error (vif, "Device is not supported"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 804 | return clib_error_return (error, "Device is not supported"); |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 805 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 806 | |
| 807 | if (virtio_pci_reset_device (vm, vif) < 0) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 808 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 809 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 810 | virtio_log_error (vif, "Failed to reset the device"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 811 | return clib_error_return (error, "Failed to reset the device"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 812 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 813 | /* |
| 814 | * read device features and negotiate (user) requested features |
| 815 | */ |
| 816 | virtio_pci_read_device_feature (vm, vif); |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 817 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 818 | 0) |
| 819 | { |
| 820 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 821 | "support VIRTIO_RING_F_INDIRECT_DESC features"); |
| 822 | } |
| 823 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 824 | { |
| 825 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 826 | "support VIRTIO_NET_F_MRG_RXBUF features"); |
| 827 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 828 | virtio_negotiate_features (vm, vif, args->features); |
| 829 | |
| 830 | /* |
| 831 | * After FEATURE_OK, driver should not accept new feature bits |
| 832 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 833 | vif->virtio_pci_func->set_status (vm, vif, |
| 834 | VIRTIO_CONFIG_STATUS_FEATURES_OK); |
| 835 | status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 836 | if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK)) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 837 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 838 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 839 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 840 | "error encountered: Device doesn't support requested features"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 841 | return clib_error_return (error, |
| 842 | "Device doesn't support requested features"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 843 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 844 | vif->status = status; |
| 845 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 846 | /* |
| 847 | * get or set the mac address |
| 848 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 849 | if (virtio_pci_get_mac (vm, vif)) |
| 850 | { |
| 851 | f64 now = vlib_time_now (vm); |
| 852 | u32 rnd; |
| 853 | rnd = (u32) (now * 1e6); |
| 854 | rnd = random_u32 (&rnd); |
| 855 | |
| 856 | memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd)); |
| 857 | vif->mac_addr[0] = 2; |
| 858 | vif->mac_addr[1] = 0xfe; |
| 859 | virtio_pci_set_mac (vm, vif); |
| 860 | } |
| 861 | |
| 862 | virtio_set_net_hdr_size (vif); |
| 863 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 864 | /* |
| 865 | * Initialize the virtqueues |
| 866 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 867 | if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 868 | { |
| 869 | args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY; |
| 870 | goto err; |
| 871 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 872 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 873 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 874 | { |
| 875 | if (vif->msix_table_size <= vif->max_queue_pairs) |
| 876 | { |
| 877 | virtio_log_error (vif, |
| 878 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 879 | vif->msix_table_size, vif->max_queue_pairs); |
| 880 | return clib_error_return (error, |
| 881 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 882 | vif->msix_table_size, |
| 883 | vif->max_queue_pairs); |
| 884 | } |
| 885 | } |
| 886 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 887 | for (int i = 0; i < vif->max_queue_pairs; i++) |
| 888 | { |
| 889 | if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 890 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 891 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 892 | virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue", |
| 893 | RX_QUEUE (i), "initialization"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 894 | error = |
| 895 | clib_error_return (error, "%s (%u) %s", "error in rxq-queue", |
| 896 | RX_QUEUE (i), "initialization"); |
| 897 | goto err; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 898 | } |
| 899 | else |
| 900 | { |
| 901 | vif->num_rxqs++; |
| 902 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 903 | |
Mohsin Kazmi | edf3b4b | 2019-10-30 15:00:04 +0100 | [diff] [blame] | 904 | if (i >= vtm->n_vlib_mains) |
| 905 | { |
| 906 | /* |
| 907 | * There is 1:1 mapping between tx queue and vpp worker thread. |
| 908 | * tx queue 0 is bind with thread index 0, tx queue 1 on thread |
| 909 | * index 1 and so on. |
| 910 | * Multiple worker threads can poll same tx queue when number of |
| 911 | * workers are more than tx queues. In this case, 1:N mapping |
| 912 | * between tx queue and vpp worker thread. |
| 913 | */ |
| 914 | virtio_log_debug (vif, "%s %u, %s", "tx-queue: number", |
| 915 | TX_QUEUE (i), |
| 916 | "no VPP worker thread is available"); |
| 917 | continue; |
| 918 | } |
| 919 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 920 | if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 921 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 922 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 923 | virtio_log_error (vif, "%s (%u) %s", "error in txq-queue", |
| 924 | TX_QUEUE (i), "initialization"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 925 | error = |
| 926 | clib_error_return (error, "%s (%u) %s", "error in txq-queue", |
| 927 | TX_QUEUE (i), "initialization"); |
| 928 | goto err; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 929 | } |
| 930 | else |
| 931 | { |
| 932 | vif->num_txqs++; |
| 933 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 934 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 935 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 936 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 937 | { |
| 938 | if ((error = |
| 939 | virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2))) |
| 940 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 941 | virtio_log_warning (vif, "%s (%u) %s", "error in control-queue", |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 942 | vif->max_queue_pairs * 2, "initialization"); |
| 943 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 944 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ); |
| 945 | } |
| 946 | } |
| 947 | else |
| 948 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 949 | virtio_log_debug (vif, "control queue is not available"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 950 | vif->cxq_vring = NULL; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * set the msix interrupts |
| 955 | */ |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 956 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 957 | { |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 958 | int i, j; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 959 | if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) == |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 960 | VIRTIO_MSI_NO_VECTOR) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 961 | { |
| 962 | virtio_log_warning (vif, "config vector 0 is not set"); |
| 963 | } |
| 964 | else |
| 965 | { |
| 966 | virtio_log_debug (vif, "config msix vector is set at 0"); |
| 967 | } |
| 968 | for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++) |
| 969 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 970 | if (vif->virtio_pci_func->set_queue_irq (vm, vif, j, |
| 971 | RX_QUEUE (i)) == |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 972 | VIRTIO_MSI_NO_VECTOR) |
| 973 | { |
| 974 | virtio_log_warning (vif, "queue (%u) vector is not set at %u", |
| 975 | RX_QUEUE (i), j); |
| 976 | } |
| 977 | else |
| 978 | { |
| 979 | virtio_log_debug (vif, "%s (%u) %s %u", "queue", |
| 980 | RX_QUEUE (i), "msix vector is set at", j); |
| 981 | } |
| 982 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 983 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 984 | |
| 985 | /* |
| 986 | * set the driver status OK |
| 987 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 988 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK); |
| 989 | vif->status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 990 | err: |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 991 | return error; |
| 992 | } |
| 993 | |
| 994 | void |
| 995 | virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) |
| 996 | { |
| 997 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 998 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 999 | virtio_if_t *vif; |
| 1000 | vlib_pci_dev_handle_t h; |
| 1001 | clib_error_t *error = 0; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1002 | u32 interrupt_count = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1003 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1004 | /* *INDENT-OFF* */ |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1005 | pool_foreach (vif, vim->interfaces, ({ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1006 | if (vif->pci_addr.as_u32 == args->addr) |
| 1007 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1008 | args->rv = VNET_API_ERROR_ADDRESS_IN_USE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1009 | args->error = |
| 1010 | clib_error_return (error, "PCI address in use"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1011 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1012 | format_vlib_pci_addr, &args->addr, |
| 1013 | " PCI address in use"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1014 | return; |
| 1015 | } |
| 1016 | })); |
| 1017 | /* *INDENT-ON* */ |
| 1018 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1019 | pool_get (vim->interfaces, vif); |
| 1020 | vif->dev_instance = vif - vim->interfaces; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1021 | vif->per_interface_next_index = ~0; |
| 1022 | vif->pci_addr.as_u32 = args->addr; |
| 1023 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1024 | if ((error = |
| 1025 | vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr, |
| 1026 | virtio_pci_device_ids, &h))) |
| 1027 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1028 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1029 | args->error = |
| 1030 | clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, |
| 1031 | &vif->pci_addr); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1032 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1033 | format_vlib_pci_addr, &vif->pci_addr, |
| 1034 | "error encountered on pci device open"); |
Benoît Ganne | 8f01183 | 2019-11-20 14:32:38 +0100 | [diff] [blame] | 1035 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1036 | return; |
| 1037 | } |
| 1038 | vif->pci_dev_handle = h; |
| 1039 | vlib_pci_set_private_data (vm, h, vif->dev_instance); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1040 | vif->numa_node = vlib_pci_get_numa_node (vm, h); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1041 | vif->type = VIRTIO_IF_TYPE_PCI; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1042 | |
| 1043 | if ((error = vlib_pci_bus_master_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1044 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1045 | virtio_log_error (vif, "error encountered on pci bus master enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1046 | goto error; |
| 1047 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1048 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1049 | void *bar[6]; |
| 1050 | for (u32 i = 0; i <= 5; i++) |
| 1051 | { |
| 1052 | |
| 1053 | if ((error = vlib_pci_map_region (vm, h, i, &bar[i]))) |
| 1054 | { |
| 1055 | virtio_log_debug (vif, "no pci map region for bar %u", i); |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | virtio_log_debug (vif, "pci map region for bar %u at %p", i, |
| 1060 | bar[i]); |
| 1061 | } |
| 1062 | } |
| 1063 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1064 | if ((error = vlib_pci_io_region (vm, h, 0))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1065 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1066 | virtio_log_error (vif, "error encountered on pci io region"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1067 | goto error; |
| 1068 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1069 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1070 | interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h); |
| 1071 | if (interrupt_count > 1) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1072 | { |
| 1073 | if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1074 | &virtio_pci_irq_config_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1075 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1076 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1077 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1078 | "error encountered on pci register msix handler 0"); |
| 1079 | goto error; |
| 1080 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1081 | |
| 1082 | if ((error = |
| 1083 | vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1), |
| 1084 | &virtio_pci_irq_queue_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1085 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1086 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1087 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1088 | "error encountered on pci register msix handler 1"); |
| 1089 | goto error; |
| 1090 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1091 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1092 | if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1093 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1094 | virtio_log_error (vif, "error encountered on pci enable msix irq"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1095 | goto error; |
| 1096 | } |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1097 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1098 | virtio_log_debug (vif, "device supports msix interrupts"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1099 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1100 | else if (interrupt_count == 1) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1101 | { |
| 1102 | /* |
| 1103 | * if msix table-size is 1, fall back to intX. |
| 1104 | */ |
| 1105 | if ((error = |
| 1106 | vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler))) |
| 1107 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1108 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1109 | "error encountered on pci register interrupt handler"); |
| 1110 | goto error; |
| 1111 | } |
| 1112 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1113 | virtio_log_debug (vif, "pci register interrupt handler"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1114 | } |
| 1115 | else |
| 1116 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1117 | /* |
| 1118 | * WARN: intX is showing some weird behaviour. |
| 1119 | * Please don't use interrupt mode with UIO driver. |
| 1120 | */ |
| 1121 | vif->support_int_mode = 0; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1122 | virtio_log_debug (vif, "driver is configured in poll mode only"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | if ((error = vlib_pci_intr_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1126 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1127 | virtio_log_error (vif, "error encountered on pci interrupt enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1128 | goto error; |
| 1129 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1130 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1131 | if ((error = virtio_pci_device_init (vm, vif, args, bar))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1132 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1133 | virtio_log_error (vif, "error encountered on device init"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1134 | goto error; |
| 1135 | } |
| 1136 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1137 | /* create interface */ |
| 1138 | error = ethernet_register_interface (vnm, virtio_device_class.index, |
| 1139 | vif->dev_instance, vif->mac_addr, |
| 1140 | &vif->hw_if_index, |
| 1141 | virtio_pci_flag_change); |
| 1142 | |
| 1143 | if (error) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1144 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1145 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1146 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1147 | "error encountered on ethernet register interface"); |
| 1148 | goto error; |
| 1149 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1150 | |
| 1151 | vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 1152 | vif->sw_if_index = sw->sw_if_index; |
| 1153 | args->sw_if_index = sw->sw_if_index; |
| 1154 | |
| 1155 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 1156 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 1157 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 1158 | virtio_input_node.index); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1159 | u32 i = 0; |
| 1160 | vec_foreach_index (i, vif->rxq_vrings) |
| 1161 | { |
| 1162 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0); |
| 1163 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i)); |
| 1164 | /* Set default rx mode to POLLING */ |
| 1165 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i, |
| 1166 | VNET_HW_INTERFACE_RX_MODE_POLLING); |
| 1167 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1168 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 1169 | { |
| 1170 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 1171 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 1172 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 1173 | } |
| 1174 | else |
| 1175 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1176 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1177 | virtio_pci_offloads (vm, vif, args->gso_enabled, |
| 1178 | args->checksum_offload_enabled); |
| 1179 | |
| 1180 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 1181 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1182 | { |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1183 | if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs)) |
| 1184 | virtio_log_warning (vif, "multiqueue is not set"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1185 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1186 | return; |
| 1187 | |
| 1188 | error: |
| 1189 | virtio_pci_delete_if (vm, vif); |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1190 | if (args->rv == 0) |
| 1191 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1192 | args->error = error; |
| 1193 | } |
| 1194 | |
| 1195 | int |
| 1196 | virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) |
| 1197 | { |
| 1198 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1199 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1200 | u32 i = 0; |
| 1201 | |
| 1202 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1203 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1204 | |
| 1205 | vlib_pci_intr_disable (vm, vif->pci_dev_handle); |
| 1206 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1207 | for (i = 0; i < vif->max_queue_pairs; i++) |
| 1208 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1209 | vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i)); |
| 1210 | vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i)); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1214 | vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1215 | |
Mohsin Kazmi | c79d735 | 2020-09-01 17:45:56 +0200 | [diff] [blame] | 1216 | if (vif->virtio_pci_func) |
| 1217 | vif->virtio_pci_func->device_reset (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1218 | |
| 1219 | if (vif->hw_if_index) |
| 1220 | { |
| 1221 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1222 | vec_foreach_index (i, vif->rxq_vrings) |
| 1223 | { |
| 1224 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i); |
| 1225 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1226 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 1227 | } |
| 1228 | |
| 1229 | vlib_pci_device_close (vm, vif->pci_dev_handle); |
| 1230 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1231 | vec_foreach_index (i, vif->rxq_vrings) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1232 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1233 | virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1234 | if (vring->kick_fd != -1) |
| 1235 | close (vring->kick_fd); |
| 1236 | if (vring->used) |
| 1237 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1238 | virtio_free_rx_buffers (vm, vring); |
| 1239 | } |
| 1240 | vec_free (vring->buffers); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1241 | vlib_physmem_free (vm, vring->desc); |
| 1242 | } |
| 1243 | |
| 1244 | vec_foreach_index (i, vif->txq_vrings) |
| 1245 | { |
| 1246 | virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); |
| 1247 | if (vring->kick_fd != -1) |
| 1248 | close (vring->kick_fd); |
| 1249 | if (vring->used) |
| 1250 | { |
| 1251 | virtio_free_used_desc (vm, vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1252 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1253 | vec_free (vring->buffers); |
Steven Luong | e558496 | 2019-06-25 13:47:06 -0700 | [diff] [blame] | 1254 | clib_spinlock_free (&vring->lockp); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1255 | vlib_physmem_free (vm, vring->desc); |
| 1256 | } |
| 1257 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1258 | if (vif->cxq_vring != NULL) |
| 1259 | { |
| 1260 | u16 last = vif->cxq_vring->last_used_idx; |
| 1261 | u16 n_left = vif->cxq_vring->used->idx - last; |
| 1262 | while (n_left) |
| 1263 | { |
| 1264 | last++; |
| 1265 | n_left--; |
| 1266 | } |
| 1267 | |
| 1268 | vif->cxq_vring->last_used_idx = last; |
| 1269 | vlib_physmem_free (vm, vif->cxq_vring->desc); |
| 1270 | } |
| 1271 | |
| 1272 | vec_free (vif->rxq_vrings); |
| 1273 | vec_free (vif->txq_vrings); |
| 1274 | vec_free (vif->cxq_vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1275 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1276 | clib_error_free (vif->error); |
| 1277 | memset (vif, 0, sizeof (*vif)); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1278 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1283 | int |
| 1284 | virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif, |
| 1285 | int gso_enabled, |
| 1286 | int checksum_offload_enabled, |
| 1287 | int offloads_disabled) |
| 1288 | { |
| 1289 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1290 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1291 | |
| 1292 | if (gso_enabled) |
| 1293 | virtio_pci_offloads (vm, vif, 1, 0); |
| 1294 | else if (checksum_offload_enabled) |
| 1295 | virtio_pci_offloads (vm, vif, 0, 1); |
| 1296 | else if (offloads_disabled) |
| 1297 | virtio_pci_offloads (vm, vif, 0, 0); |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1302 | /* |
| 1303 | * fd.io coding-style-patch-verification: ON |
| 1304 | * |
| 1305 | * Local Variables: |
| 1306 | * eval: (c-set-style "gnu") |
| 1307 | * End: |
| 1308 | */ |