blob: 6c6edbc3836f0eec1ee0b1f01ad61b20f548c46a [file] [log] [blame]
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001/*
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 Kazmid6c15af2018-10-23 18:00:47 +020018
19#include <vppinfra/types.h>
20#include <vlib/vlib.h>
21#include <vlib/pci/pci.h>
22#include <vnet/ethernet/ethernet.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020023#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 Kazmid6c15af2018-10-23 18:00:47 +020039static 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 Kazmid6c15af2018-10-23 18:00:47 +020049static u32
50virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
51 u32 flags)
52{
53 return 0;
54}
55
56static clib_error_t *
57virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
58{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020059 clib_error_t *error = 0;
60 u16 max_queue_pairs = 1;
61
62 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
63 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +000064 max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020065 }
66
Damjan Marionf41244f2019-11-08 17:41:06 +010067 virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020068 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
Mohsin Kazmi57d73212020-03-05 12:22:41 +000069 return clib_error_return (error, "max queue pair is %x,"
70 " should be in range [1, 0x8000]",
71 max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020072
73 vif->max_queue_pairs = max_queue_pairs;
74 return error;
75}
76
77static void
78virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
79{
Mohsin Kazmia0a68332020-07-16 12:55:42 +000080 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
81 vif->virtio_pci_func->set_mac (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020082}
83
84static u32
85virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
86{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000087 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020088 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +000089 vif->virtio_pci_func->get_mac (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020090 return 0;
91 }
92 return 1;
93}
94
95static u16
96virtio_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 Kazmi09a3bc52019-04-02 11:45:08 +0000102 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000103 status = vif->virtio_pci_func->get_device_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200104 return status;
105}
106
107static void
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000108virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
109 u16 line)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200110{
111 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000112 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200113 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000114 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000115 line--;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200116 u16 qid = line;
117
118 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
119}
120
121static void
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000122virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
123 u16 line)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200124{
125 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000126 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200127 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000128 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200129
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
143static void
144virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
145{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000146 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200147 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000148 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200149 u8 isr = 0;
150 u16 line = 0;
151
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000152 isr = vif->virtio_pci_func->get_isr (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200153
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 Kazmi8046fdc2020-07-15 11:54:10 +0000160 {
161 for (; line < vif->num_rxqs; line++)
162 virtio_pci_irq_queue_handler (vm, h, (line + 1));
163 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200164
165 if (isr & VIRTIO_PCI_ISR_CONFIG)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000166 virtio_pci_irq_config_handler (vm, h, line);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200167}
168
169inline void
170device_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 Kazmi05493782019-05-01 14:26:17 +0200197struct virtio_ctrl_msg
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000198{
199 struct virtio_net_ctrl_hdr ctrl;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000200 virtio_net_ctrl_ack status;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200201 u8 data[1024];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000202};
203
204static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200205virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
206 struct virtio_ctrl_msg *data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000207{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000208 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200209 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
210 struct virtio_ctrl_msg result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000211 u32 buffer_index;
212 vlib_buffer_t *b;
213 u16 used, next, avail;
214 u16 sz = vring->size;
215 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000216
217 used = vring->desc_in_use;
218 next = vring->desc_next;
219 avail = vring->avail->idx;
220 struct vring_desc *d = &vring->desc[next];
221
222 if (vlib_buffer_alloc (vm, &buffer_index, 1))
223 b = vlib_get_buffer (vm, buffer_index);
224 else
225 return VIRTIO_NET_ERR;
226 /*
227 * current_data may not be initialized with 0 and may contain
228 * previous offset.
229 */
230 b->current_data = 0;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200231 clib_memcpy (vlib_buffer_get_current (b), data,
232 sizeof (struct virtio_ctrl_msg));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000233 d->flags = VRING_DESC_F_NEXT;
234 d->addr = vlib_buffer_get_current_pa (vm, b);
235 d->len = sizeof (struct virtio_net_ctrl_hdr);
236 vring->avail->ring[avail & mask] = next;
237 avail++;
238 next = (next + 1) & mask;
239 d->next = next;
240 used++;
241
242 d = &vring->desc[next];
243 d->flags = VRING_DESC_F_NEXT;
244 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200245 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
246 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000247 next = (next + 1) & mask;
248 d->next = next;
249 used++;
250
251 d = &vring->desc[next];
252 d->flags = VRING_DESC_F_WRITE;
253 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200254 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
255 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000256 next = (next + 1) & mask;
257 used++;
258
259 CLIB_MEMORY_STORE_BARRIER ();
260 vring->avail->idx = avail;
261 vring->desc_next = next;
262 vring->desc_in_use = used;
263
264 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
265 {
266 virtio_kick (vm, vring, vif);
267 }
268
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000269 u16 last = vring->last_used_idx, n_left = 0;
270 n_left = vring->used->idx - last;
271
272 while (n_left)
273 {
274 struct vring_used_elem *e = &vring->used->ring[last & mask];
275 u16 slot = e->id;
276
277 d = &vring->desc[slot];
278 while (d->flags & VRING_DESC_F_NEXT)
279 {
280 used--;
281 slot = d->next;
282 d = &vring->desc[slot];
283 }
284 used--;
285 last++;
286 n_left--;
287 }
288 vring->desc_in_use = used;
289 vring->last_used_idx = last;
290
291 CLIB_MEMORY_BARRIER ();
292 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmi05493782019-05-01 14:26:17 +0200293 sizeof (struct virtio_ctrl_msg));
Damjan Marionf41244f2019-11-08 17:41:06 +0100294 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000295 status = result.status;
296 vlib_buffer_free (vm, &buffer_index, 1);
297 return status;
298}
299
Mohsin Kazmi05493782019-05-01 14:26:17 +0200300static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000301virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
302{
303 struct virtio_ctrl_msg offload_hdr;
304 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
305
306 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
307 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
308 offload_hdr.status = VIRTIO_NET_ERR;
309 u64 offloads = 0ULL;
310 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
311
312 status =
313 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
314 virtio_log_debug (vif, "disable offloads");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000315 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
316 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000317 return status;
318}
319
320static int
321virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
322{
323 struct virtio_ctrl_msg csum_offload_hdr;
324 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
325
326 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
327 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
328 csum_offload_hdr.status = VIRTIO_NET_ERR;
329 u64 offloads = 0ULL;
330 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
331 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
332
333 status =
334 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
335 virtio_log_debug (vif, "enable checksum offload");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000336 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
337 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000338 return status;
339}
340
341static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200342virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
343{
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200344 struct virtio_ctrl_msg gso_hdr;
345 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
346
347 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
348 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
349 gso_hdr.status = VIRTIO_NET_ERR;
350 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
351 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000352 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200353 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
354
355 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100356 virtio_log_debug (vif, "enable gso");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000357 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
358 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200359 return status;
360}
361
362static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000363virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
364 int csum_offload_enabled)
365{
366 vnet_main_t *vnm = vnet_get_main ();
367 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
368
369 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
370 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
371 {
372 if (gso_enabled
373 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
374 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
375 {
376 if (virtio_pci_enable_gso (vm, vif))
377 {
378 virtio_log_warning (vif, "gso is not enabled");
379 }
380 else
381 {
382 vif->gso_enabled = 1;
383 vif->csum_offload_enabled = 0;
384 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
385 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
386 }
387 }
388 else if (csum_offload_enabled
389 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
390 {
391 if (virtio_pci_enable_checksum_offload (vm, vif))
392 {
393 virtio_log_warning (vif, "checksum offload is not enabled");
394 }
395 else
396 {
397 vif->csum_offload_enabled = 1;
398 vif->gso_enabled = 0;
399 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
400 hw->flags |=
401 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
402 }
403 }
404 else
405 {
406 if (virtio_pci_disable_offload (vm, vif))
407 {
408 virtio_log_warning (vif, "offloads are not disabled");
409 }
410 else
411 {
412 vif->csum_offload_enabled = 0;
413 vif->gso_enabled = 0;
414 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
415 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
416 }
417 }
418 }
419
420 return 0;
421}
422
423static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200424virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
425 u16 num_queues)
426{
Mohsin Kazmi05493782019-05-01 14:26:17 +0200427 struct virtio_ctrl_msg mq_hdr;
428 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
429
430 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
431 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
432 mq_hdr.status = VIRTIO_NET_ERR;
433 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
434
435 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100436 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200437 return status;
438}
439
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200440static u8
441virtio_pci_queue_size_valid (u16 qsz)
442{
443 if (qsz < 64 || qsz > 4096)
444 return 0;
445 if ((qsz % 64) != 0)
446 return 0;
447 return 1;
448}
449
450clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000451virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
452 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200453{
454 clib_error_t *error = 0;
455 u16 queue_size = 0;
456 virtio_vring_t *vring;
457 struct vring vr;
458 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000459 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200460
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000461 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200462 if (!virtio_pci_queue_size_valid (queue_size))
463 clib_warning ("queue size is not valid");
464
465 if (!is_pow2 (queue_size))
466 return clib_error_return (0, "ring size must be power of 2");
467
468 if (queue_size > 32768)
469 return clib_error_return (0, "ring size must be 32768 or lower");
470
471 if (queue_size == 0)
472 queue_size = 256;
473
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000474 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
475 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200476 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
477 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000478 ptr =
479 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
480 vif->numa_node);
481 if (!ptr)
482 return vlib_physmem_last_error (vm);
483 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200484 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
485 vring->desc = vr.desc;
486 vring->avail = vr.avail;
487 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000488 vring->queue_id = queue_num;
489 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
490
491 ASSERT (vring->buffers == 0);
492
493 vring->size = queue_size;
Damjan Marionf41244f2019-11-08 17:41:06 +0100494 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000495 queue_size);
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000496 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000497 vring->kick_fd = -1;
498
499 return error;
500}
501
502clib_error_t *
503virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
504{
505 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000506 u16 queue_size = 0;
507 virtio_vring_t *vring;
508 struct vring vr;
509 u32 i = 0;
510 void *ptr = NULL;
511
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000512 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000513 if (!virtio_pci_queue_size_valid (queue_size))
514 clib_warning ("queue size is not valid");
515
516 if (!is_pow2 (queue_size))
517 return clib_error_return (0, "ring size must be power of 2");
518
519 if (queue_size > 32768)
520 return clib_error_return (0, "ring size must be 32768 or lower");
521
522 if (queue_size == 0)
523 queue_size = 256;
524
525 if (queue_num % 2)
526 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000527 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
528 CLIB_CACHE_LINE_BYTES);
529 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
530 clib_spinlock_init (&vring->lockp);
531 }
532 else
533 {
534 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
535 CLIB_CACHE_LINE_BYTES);
536 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
537 }
538 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
539 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
540 ptr =
541 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
542 vif->numa_node);
543 if (!ptr)
544 return vlib_physmem_last_error (vm);
545 clib_memset (ptr, 0, i);
546 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
547 vring->desc = vr.desc;
548 vring->avail = vr.avail;
549 vring->used = vr.used;
550 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200551 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200552 vring->flow_table = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200553
554 ASSERT (vring->buffers == 0);
555 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000556 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200557 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100558 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000559 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100560 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200561 }
562 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000563 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100564 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000565 queue_size);
566 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200567 vring->size = queue_size;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000568 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200569 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200570
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200571 vring->kick_fd = -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200572 return error;
573}
574
575static void
576virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
577 u64 req_features)
578{
579 /*
580 * if features are not requested
581 * default: all supported features
582 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200583 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
584 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
585 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
586 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200587 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200588 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
589 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
590 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
591 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
592 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
593 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
594 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200595 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
596 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000597 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
598 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000599 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200600 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
601 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
602
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200603 if (vif->is_modern)
604 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
605
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200606 if (req_features == 0)
607 {
608 req_features = supported_features;
609 }
610
611 vif->features = req_features & vif->remote_features & supported_features;
612
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000613 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200614 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000615 u16 mtu = 0;
616 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
617
618 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200619 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
620 }
621
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000622 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
623 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200624}
625
626void
627virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
628{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000629 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200630}
631
632int
633virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
634{
635 u8 status = 0;
636
637 /*
638 * Reset the device
639 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000640 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200641
642 /*
643 * Set the Acknowledge status bit
644 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000645 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200646
647 /*
648 * Set the Driver status bit
649 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000650 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200651
652 /*
653 * Read the status and verify it
654 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000655 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200656 if ((status & VIRTIO_CONFIG_STATUS_ACK)
657 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
658 vif->status = status;
659 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200660 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200661
662 return 0;
663}
664
665clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200666virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200667{
668 clib_error_t *error = 0;
669 struct virtio_pci_cap cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200670 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200671 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
672
673 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000674 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100675 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200676 return clib_error_return (error,
677 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000678 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200679 while (pos)
680 {
681 if ((error =
682 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
683 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000684 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100685 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000686 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200687 return clib_error_return (error,
688 "error in reading the capability at [%2x]",
689 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000690 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200691
692 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
693 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000694 u16 flags, table_size, table_size_mask = 0x07FF;
695
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200696 if ((error =
697 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
698 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200699 return clib_error_return (error,
700 "error in reading the capability at [%2x]",
701 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200702
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000703 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +0100704 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000705 "msix interrupt vector table-size", table_size);
706
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200707 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000708 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100709 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000710 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000711 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000712 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200713 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000714 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100715 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000716 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000717 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000718 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200719 }
720
721 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
722 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100723 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000724 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200725 goto next;
726 }
727
Damjan Marionf41244f2019-11-08 17:41:06 +0100728 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000729 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
730 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200731
Mohsin Kazmi0a507d72020-09-01 18:18:48 +0200732 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +0200733 {
734 vif->bar = bar[cap.bar];
735 vif->bar_id = cap.bar;
736 }
737 else
738 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200739
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200740 switch (cap.cfg_type)
741 {
742 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200743 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200744 common_cfg = 1;
745 break;
746 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200747 if ((error =
748 vlib_pci_read_write_config (vm, h, VLIB_READ,
749 pos + sizeof (cap),
750 &vif->notify_off_multiplier,
751 sizeof
752 (vif->notify_off_multiplier))))
753 {
754 virtio_log_error (vif, "notify off multiplier is not given");
755 }
756 else
757 {
758 virtio_log_debug (vif, "notify off multiplier is %u",
759 vif->notify_off_multiplier);
760 vif->notify_offset = cap.offset;
761 notify = 1;
762 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200763 break;
764 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200765 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200766 dev_cfg = 1;
767 break;
768 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200769 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200770 isr = 1;
771 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000772 case VIRTIO_PCI_CAP_PCI_CFG:
773 if (cap.bar == 0)
774 pci_cfg = 1;
775 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200776 }
777 next:
778 pos = cap.cap_next;
779 }
780
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200781 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200782 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200783 vif->virtio_pci_func = &virtio_pci_legacy_func;
Damjan Marionf41244f2019-11-08 17:41:06 +0100784 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200785 return error;
786 }
787
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200788 vif->is_modern = 1;
789 vif->virtio_pci_func = &virtio_pci_modern_func;
790
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000791 if (!pci_cfg)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200792 virtio_log_debug (vif, "modern virtio pci device found");
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000793
Damjan Marionf41244f2019-11-08 17:41:06 +0100794 virtio_log_debug (vif, "transitional virtio pci device found");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000795 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200796}
797
798static clib_error_t *
799virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200800 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200801{
802 clib_error_t *error = 0;
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +0100803 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200804 u8 status = 0;
805
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200806 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000807 {
808 args->rv = VNET_API_ERROR_UNSUPPORTED;
809 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200810 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000811 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200812
813 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000814 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000815 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +0100816 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200817 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000818 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200819 /*
820 * read device features and negotiate (user) requested features
821 */
822 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +0000823 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
824 0)
825 {
826 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
827 "support VIRTIO_RING_F_INDIRECT_DESC features");
828 }
829 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
830 {
831 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
832 "support VIRTIO_NET_F_MRG_RXBUF features");
833 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200834 virtio_negotiate_features (vm, vif, args->features);
835
836 /*
837 * After FEATURE_OK, driver should not accept new feature bits
838 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000839 vif->virtio_pci_func->set_status (vm, vif,
840 VIRTIO_CONFIG_STATUS_FEATURES_OK);
841 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200842 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000843 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000844 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +0100845 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000846 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200847 return clib_error_return (error,
848 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000849 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200850 vif->status = status;
851
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000852 /*
853 * get or set the mac address
854 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200855 if (virtio_pci_get_mac (vm, vif))
856 {
857 f64 now = vlib_time_now (vm);
858 u32 rnd;
859 rnd = (u32) (now * 1e6);
860 rnd = random_u32 (&rnd);
861
862 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
863 vif->mac_addr[0] = 2;
864 vif->mac_addr[1] = 0xfe;
865 virtio_pci_set_mac (vm, vif);
866 }
867
868 virtio_set_net_hdr_size (vif);
869
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000870 /*
871 * Initialize the virtqueues
872 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200873 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000874 {
875 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
876 goto err;
877 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200878
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000879 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
880 {
881 if (vif->msix_table_size <= vif->max_queue_pairs)
882 {
883 virtio_log_error (vif,
884 "error MSIX lines (%u) <= Number of RXQs (%u)",
885 vif->msix_table_size, vif->max_queue_pairs);
886 return clib_error_return (error,
887 "error MSIX lines (%u) <= Number of RXQs (%u)",
888 vif->msix_table_size,
889 vif->max_queue_pairs);
890 }
891 }
892
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000893 for (int i = 0; i < vif->max_queue_pairs; i++)
894 {
895 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200896 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000897 args->rv = VNET_API_ERROR_INIT_FAILED;
898 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
899 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000900 error =
901 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
902 RX_QUEUE (i), "initialization");
903 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200904 }
905 else
906 {
907 vif->num_rxqs++;
908 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200909
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +0100910 if (i >= vtm->n_vlib_mains)
911 {
912 /*
913 * There is 1:1 mapping between tx queue and vpp worker thread.
914 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
915 * index 1 and so on.
916 * Multiple worker threads can poll same tx queue when number of
917 * workers are more than tx queues. In this case, 1:N mapping
918 * between tx queue and vpp worker thread.
919 */
920 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
921 TX_QUEUE (i),
922 "no VPP worker thread is available");
923 continue;
924 }
925
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000926 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200927 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000928 args->rv = VNET_API_ERROR_INIT_FAILED;
929 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
930 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000931 error =
932 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
933 TX_QUEUE (i), "initialization");
934 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200935 }
936 else
937 {
938 vif->num_txqs++;
939 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000940 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200941
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000942 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
943 {
944 if ((error =
945 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
946 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100947 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000948 vif->max_queue_pairs * 2, "initialization");
949 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
950 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
951 }
952 }
953 else
954 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100955 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000956 vif->cxq_vring = NULL;
957 }
958
959 /*
960 * set the msix interrupts
961 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000962 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200963 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000964 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000965 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000966 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000967 {
968 virtio_log_warning (vif, "config vector 0 is not set");
969 }
970 else
971 {
972 virtio_log_debug (vif, "config msix vector is set at 0");
973 }
974 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
975 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000976 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
977 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000978 VIRTIO_MSI_NO_VECTOR)
979 {
980 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
981 RX_QUEUE (i), j);
982 }
983 else
984 {
985 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
986 RX_QUEUE (i), "msix vector is set at", j);
987 }
988 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200989 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000990
991 /*
992 * set the driver status OK
993 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000994 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
995 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200996err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200997 return error;
998}
999
1000void
1001virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1002{
1003 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001004 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001005 virtio_if_t *vif;
1006 vlib_pci_dev_handle_t h;
1007 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001008 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001009
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001010 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001011 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001012 if (vif->pci_addr.as_u32 == args->addr)
1013 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001014 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001015 args->error =
1016 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001017 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1018 format_vlib_pci_addr, &args->addr,
1019 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001020 return;
1021 }
1022 }));
1023 /* *INDENT-ON* */
1024
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001025 pool_get (vim->interfaces, vif);
1026 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001027 vif->per_interface_next_index = ~0;
1028 vif->pci_addr.as_u32 = args->addr;
1029
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001030 if ((error =
1031 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1032 virtio_pci_device_ids, &h)))
1033 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001034 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1035 args->error =
1036 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1037 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001038 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1039 format_vlib_pci_addr, &vif->pci_addr,
1040 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001041 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001042 return;
1043 }
1044 vif->pci_dev_handle = h;
1045 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001046 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001047 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001048
1049 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001050 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001051 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001052 goto error;
1053 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001054
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001055 void *bar[6];
1056 for (u32 i = 0; i <= 5; i++)
1057 {
1058
1059 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1060 {
1061 virtio_log_debug (vif, "no pci map region for bar %u", i);
1062 }
1063 else
1064 {
1065 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1066 bar[i]);
1067 }
1068 }
1069
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001070 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001071 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001072 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001073 goto error;
1074 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001075
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001076 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1077 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001078 {
1079 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001080 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001081 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001082 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001083 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001084 "error encountered on pci register msix handler 0");
1085 goto error;
1086 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001087
1088 if ((error =
1089 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1090 &virtio_pci_irq_queue_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001091 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001092 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001093 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001094 "error encountered on pci register msix handler 1");
1095 goto error;
1096 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001097
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001098 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001099 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001100 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001101 goto error;
1102 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001103 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001104 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001105 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001106 else if (interrupt_count == 1)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001107 {
1108 /*
1109 * if msix table-size is 1, fall back to intX.
1110 */
1111 if ((error =
1112 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1113 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001114 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001115 "error encountered on pci register interrupt handler");
1116 goto error;
1117 }
1118 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001119 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001120 }
1121 else
1122 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001123 /*
1124 * WARN: intX is showing some weird behaviour.
1125 * Please don't use interrupt mode with UIO driver.
1126 */
1127 vif->support_int_mode = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001128 virtio_log_debug (vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001129 }
1130
1131 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001132 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001133 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001134 goto error;
1135 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001136
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001137 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001138 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001139 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001140 goto error;
1141 }
1142
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001143 /* create interface */
1144 error = ethernet_register_interface (vnm, virtio_device_class.index,
1145 vif->dev_instance, vif->mac_addr,
1146 &vif->hw_if_index,
1147 virtio_pci_flag_change);
1148
1149 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001150 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001151 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001152 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001153 "error encountered on ethernet register interface");
1154 goto error;
1155 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001156
1157 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1158 vif->sw_if_index = sw->sw_if_index;
1159 args->sw_if_index = sw->sw_if_index;
1160
1161 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1162 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1163 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1164 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001165 u32 i = 0;
1166 vec_foreach_index (i, vif->rxq_vrings)
1167 {
1168 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1169 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1170 /* Set default rx mode to POLLING */
1171 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1172 VNET_HW_INTERFACE_RX_MODE_POLLING);
1173 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001174 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1175 {
1176 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1177 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1178 VNET_HW_INTERFACE_FLAG_LINK_UP);
1179 }
1180 else
1181 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001182
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001183 virtio_pci_offloads (vm, vif, args->gso_enabled,
1184 args->checksum_offload_enabled);
1185
1186 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1187 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001188 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001189 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1190 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001191 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001192 return;
1193
1194error:
1195 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001196 if (args->rv == 0)
1197 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001198 args->error = error;
1199}
1200
1201int
1202virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1203{
1204 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001205 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001206 u32 i = 0;
1207
1208 if (vif->type != VIRTIO_IF_TYPE_PCI)
1209 return VNET_API_ERROR_INVALID_INTERFACE;
1210
1211 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1212
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001213 for (i = 0; i < vif->max_queue_pairs; i++)
1214 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001215 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1216 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001217 }
1218
1219 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001220 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001221
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001222 if (vif->virtio_pci_func)
1223 vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001224
1225 if (vif->hw_if_index)
1226 {
1227 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001228 vec_foreach_index (i, vif->rxq_vrings)
1229 {
1230 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1231 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001232 ethernet_delete_interface (vnm, vif->hw_if_index);
1233 }
1234
1235 vlib_pci_device_close (vm, vif->pci_dev_handle);
1236
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001237 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001238 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001239 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001240 if (vring->kick_fd != -1)
1241 close (vring->kick_fd);
1242 if (vring->used)
1243 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001244 virtio_free_rx_buffers (vm, vring);
1245 }
1246 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001247 vlib_physmem_free (vm, vring->desc);
1248 }
1249
1250 vec_foreach_index (i, vif->txq_vrings)
1251 {
1252 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1253 if (vring->kick_fd != -1)
1254 close (vring->kick_fd);
1255 if (vring->used)
1256 {
1257 virtio_free_used_desc (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001258 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001259 vec_free (vring->buffers);
Steven Luonge5584962019-06-25 13:47:06 -07001260 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001261 vlib_physmem_free (vm, vring->desc);
1262 }
1263
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001264 if (vif->cxq_vring != NULL)
1265 {
1266 u16 last = vif->cxq_vring->last_used_idx;
1267 u16 n_left = vif->cxq_vring->used->idx - last;
1268 while (n_left)
1269 {
1270 last++;
1271 n_left--;
1272 }
1273
1274 vif->cxq_vring->last_used_idx = last;
1275 vlib_physmem_free (vm, vif->cxq_vring->desc);
1276 }
1277
1278 vec_free (vif->rxq_vrings);
1279 vec_free (vif->txq_vrings);
1280 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001281
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001282 clib_error_free (vif->error);
1283 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001284 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001285
1286 return 0;
1287}
1288
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001289int
1290virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1291 int gso_enabled,
1292 int checksum_offload_enabled,
1293 int offloads_disabled)
1294{
1295 if (vif->type != VIRTIO_IF_TYPE_PCI)
1296 return VNET_API_ERROR_INVALID_INTERFACE;
1297
1298 if (gso_enabled)
1299 virtio_pci_offloads (vm, vif, 1, 0);
1300 else if (checksum_offload_enabled)
1301 virtio_pci_offloads (vm, vif, 0, 1);
1302 else if (offloads_disabled)
1303 virtio_pci_offloads (vm, vif, 0, 0);
1304
1305 return 0;
1306}
1307
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001308/*
1309 * fd.io coding-style-patch-verification: ON
1310 *
1311 * Local Variables:
1312 * eval: (c-set-style "gnu")
1313 * End:
1314 */