blob: 785ca4839c5c556e053bd8a2a5049c5e94ff836c [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 Kazmi09a3bc52019-04-02 11:45:08 +0000197static int
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100198virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif,
199 virtio_ctrl_msg_t * data, u32 len)
200{
201 virtio_vring_t *vring = vif->cxq_vring;
202 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
203 virtio_ctrl_msg_t result;
204 u32 buffer_index;
205 vlib_buffer_t *b;
206 u16 used, next;
207 u16 sz = vring->size;
208 u16 flags = 0, first_desc_flags = 0;
209
210 used = vring->desc_in_use;
211 next = vring->desc_next;
212 vring_packed_desc_t *d = &vring->packed_desc[next];
213
214 if (vlib_buffer_alloc (vm, &buffer_index, 1))
215 b = vlib_get_buffer (vm, buffer_index);
216 else
217 return VIRTIO_NET_ERR;
218 /*
219 * current_data may not be initialized with 0 and may contain
220 * previous offset.
221 */
222 b->current_data = 0;
223 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
224
225 first_desc_flags = VRING_DESC_F_NEXT;
226 if (vring->avail_wrap_counter)
227 {
228 first_desc_flags |= VRING_DESC_F_AVAIL;
229 first_desc_flags &= ~VRING_DESC_F_USED;
230 }
231 else
232 {
233 first_desc_flags &= ~VRING_DESC_F_AVAIL;
234 first_desc_flags |= VRING_DESC_F_USED;
235 }
236 d->addr = vlib_buffer_get_current_pa (vm, b);
237 d->len = sizeof (virtio_net_ctrl_hdr_t);
238 d->id = next;
239
240 next++;
241 if (next >= sz)
242 {
243 next = 0;
244 vring->avail_wrap_counter ^= 1;
245 }
246 used++;
247
248 d = &vring->packed_desc[next];
249 flags = VRING_DESC_F_NEXT;
250 if (vring->avail_wrap_counter)
251 {
252 flags |= VRING_DESC_F_AVAIL;
253 flags &= ~VRING_DESC_F_USED;
254 }
255 else
256 {
257 flags &= ~VRING_DESC_F_AVAIL;
258 flags |= VRING_DESC_F_USED;
259 }
260 d->addr = vlib_buffer_get_current_pa (vm, b) +
261 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
262 d->len = len;
263 d->id = next;
264 d->flags = flags;
265
266 next++;
267 if (next >= sz)
268 {
269 next = 0;
270 vring->avail_wrap_counter ^= 1;
271 }
272 used++;
273
274 d = &vring->packed_desc[next];
275 flags = VRING_DESC_F_WRITE;
276 if (vring->avail_wrap_counter)
277 {
278 flags |= VRING_DESC_F_AVAIL;
279 flags &= ~VRING_DESC_F_USED;
280 }
281 else
282 {
283 flags &= ~VRING_DESC_F_AVAIL;
284 flags |= VRING_DESC_F_USED;
285 }
286 d->addr = vlib_buffer_get_current_pa (vm, b) +
287 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
288 d->len = sizeof (data->status);
289 d->id = next;
290 d->flags = flags;
291
292 next++;
293 if (next >= sz)
294 {
295 next = 0;
296 vring->avail_wrap_counter ^= 1;
297 }
298 used++;
299
300 CLIB_MEMORY_STORE_BARRIER ();
301 vring->packed_desc[vring->desc_next].flags = first_desc_flags;
302 vring->desc_next = next;
303 vring->desc_in_use = used;
304 CLIB_MEMORY_BARRIER ();
305 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
306 {
307 virtio_kick (vm, vring, vif);
308 }
309
310 u16 last = vring->last_used_idx;
311 d = &vring->packed_desc[last];
312 do
313 {
314 flags = d->flags;
315 }
316 while ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7)
317 || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15));
318
319 last += 3;
320 if (last >= vring->size)
321 {
322 last = last - vring->size;
323 vring->used_wrap_counter ^= 1;
324 }
325 vring->desc_in_use -= 3;
326 vring->last_used_idx = last;
327
328 CLIB_MEMORY_BARRIER ();
329 clib_memcpy (&result, vlib_buffer_get_current (b),
330 sizeof (virtio_ctrl_msg_t));
331 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
332 status = result.status;
333 vlib_buffer_free (vm, &buffer_index, 1);
334 return status;
335}
336
337static int
338virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif,
339 virtio_ctrl_msg_t * data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000340{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000341 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200342 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
343 virtio_ctrl_msg_t result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000344 u32 buffer_index;
345 vlib_buffer_t *b;
346 u16 used, next, avail;
347 u16 sz = vring->size;
348 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000349
350 used = vring->desc_in_use;
351 next = vring->desc_next;
352 avail = vring->avail->idx;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200353 vring_desc_t *d = &vring->desc[next];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000354
355 if (vlib_buffer_alloc (vm, &buffer_index, 1))
356 b = vlib_get_buffer (vm, buffer_index);
357 else
358 return VIRTIO_NET_ERR;
359 /*
360 * current_data may not be initialized with 0 and may contain
361 * previous offset.
362 */
363 b->current_data = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200364 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000365 d->flags = VRING_DESC_F_NEXT;
366 d->addr = vlib_buffer_get_current_pa (vm, b);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200367 d->len = sizeof (virtio_net_ctrl_hdr_t);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000368 vring->avail->ring[avail & mask] = next;
369 avail++;
370 next = (next + 1) & mask;
371 d->next = next;
372 used++;
373
374 d = &vring->desc[next];
375 d->flags = VRING_DESC_F_NEXT;
376 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200377 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200378 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000379 next = (next + 1) & mask;
380 d->next = next;
381 used++;
382
383 d = &vring->desc[next];
384 d->flags = VRING_DESC_F_WRITE;
385 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200386 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200387 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000388 next = (next + 1) & mask;
389 used++;
390
391 CLIB_MEMORY_STORE_BARRIER ();
392 vring->avail->idx = avail;
393 vring->desc_next = next;
394 vring->desc_in_use = used;
395
396 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
397 {
398 virtio_kick (vm, vring, vif);
399 }
400
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000401 u16 last = vring->last_used_idx, n_left = 0;
402 n_left = vring->used->idx - last;
403
404 while (n_left)
405 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200406 vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000407 u16 slot = e->id;
408
409 d = &vring->desc[slot];
410 while (d->flags & VRING_DESC_F_NEXT)
411 {
412 used--;
413 slot = d->next;
414 d = &vring->desc[slot];
415 }
416 used--;
417 last++;
418 n_left--;
419 }
420 vring->desc_in_use = used;
421 vring->last_used_idx = last;
422
423 CLIB_MEMORY_BARRIER ();
424 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200425 sizeof (virtio_ctrl_msg_t));
Damjan Marionf41244f2019-11-08 17:41:06 +0100426 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000427 status = result.status;
428 vlib_buffer_free (vm, &buffer_index, 1);
429 return status;
430}
431
Mohsin Kazmi05493782019-05-01 14:26:17 +0200432static int
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100433virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
434 virtio_ctrl_msg_t * data, u32 len)
435{
436 if (vif->is_packed)
437 return virtio_pci_send_ctrl_msg_packed (vm, vif, data, len);
438 else
439 return virtio_pci_send_ctrl_msg_split (vm, vif, data, len);
440}
441
442static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000443virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
444{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200445 virtio_ctrl_msg_t offload_hdr;
446 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000447
448 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
449 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
450 offload_hdr.status = VIRTIO_NET_ERR;
451 u64 offloads = 0ULL;
452 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
453
454 status =
455 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
456 virtio_log_debug (vif, "disable offloads");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000457 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
458 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000459 return status;
460}
461
462static int
463virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
464{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200465 virtio_ctrl_msg_t csum_offload_hdr;
466 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000467
468 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
469 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
470 csum_offload_hdr.status = VIRTIO_NET_ERR;
471 u64 offloads = 0ULL;
472 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
473 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
474
475 status =
476 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
477 virtio_log_debug (vif, "enable checksum offload");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000478 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
479 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000480 return status;
481}
482
483static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200484virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
485{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200486 virtio_ctrl_msg_t gso_hdr;
487 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200488
489 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
490 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
491 gso_hdr.status = VIRTIO_NET_ERR;
492 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
493 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000494 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200495 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
496
497 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100498 virtio_log_debug (vif, "enable gso");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000499 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
500 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200501 return status;
502}
503
504static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000505virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
506 int csum_offload_enabled)
507{
508 vnet_main_t *vnm = vnet_get_main ();
509 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
510
511 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
512 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
513 {
514 if (gso_enabled
515 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
516 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
517 {
518 if (virtio_pci_enable_gso (vm, vif))
519 {
520 virtio_log_warning (vif, "gso is not enabled");
521 }
522 else
523 {
524 vif->gso_enabled = 1;
525 vif->csum_offload_enabled = 0;
526 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
527 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
528 }
529 }
530 else if (csum_offload_enabled
531 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
532 {
533 if (virtio_pci_enable_checksum_offload (vm, vif))
534 {
535 virtio_log_warning (vif, "checksum offload is not enabled");
536 }
537 else
538 {
539 vif->csum_offload_enabled = 1;
540 vif->gso_enabled = 0;
541 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
542 hw->flags |=
543 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
544 }
545 }
546 else
547 {
548 if (virtio_pci_disable_offload (vm, vif))
549 {
550 virtio_log_warning (vif, "offloads are not disabled");
551 }
552 else
553 {
554 vif->csum_offload_enabled = 0;
555 vif->gso_enabled = 0;
556 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
557 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
558 }
559 }
560 }
561
562 return 0;
563}
564
565static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200566virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
567 u16 num_queues)
568{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200569 virtio_ctrl_msg_t mq_hdr;
570 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200571
572 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
573 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
574 mq_hdr.status = VIRTIO_NET_ERR;
575 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
576
577 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100578 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200579 return status;
580}
581
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200582static u8
583virtio_pci_queue_size_valid (u16 qsz)
584{
585 if (qsz < 64 || qsz > 4096)
586 return 0;
587 if ((qsz % 64) != 0)
588 return 0;
589 return 1;
590}
591
592clib_error_t *
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100593virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
594 u16 queue_num)
595{
596 clib_error_t *error = 0;
597 u16 queue_size = 0;
598 virtio_vring_t *vring;
599 u32 i = 0;
600 void *ptr = NULL;
601
602 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
603
604 if (queue_size > 32768)
605 return clib_error_return (0, "ring size must be 32768 or lower");
606
607 if (queue_size == 0)
608 queue_size = 256;
609
610 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
611 vring = vec_elt_at_index (vif->cxq_vring, 0);
612
613 i =
614 (((queue_size * sizeof (vring_packed_desc_t)) +
615 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
616 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
617
618 ptr =
619 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
620 vif->numa_node);
621 if (!ptr)
622 return vlib_physmem_last_error (vm);
623 clib_memset (ptr, 0, i);
624
625 vring->packed_desc = ptr;
626
627 vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
628 vring->driver_event->off_wrap = 0;
629 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
630
631 vring->device_event =
632 ptr +
633 (((queue_size * sizeof (vring_packed_desc_t)) +
634 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
635 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
636 vring->device_event->off_wrap = 0;
637 vring->device_event->flags = 0;
638
639 vring->queue_id = queue_num;
640 vring->size = queue_size;
641 vring->avail_wrap_counter = 1;
642 vring->used_wrap_counter = 1;
643
644 ASSERT (vring->buffers == 0);
645
646 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
647 queue_size);
648 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring);
649 vring->queue_notify_offset =
650 vif->notify_off_multiplier *
651 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
652 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
653 queue_num, vring->queue_notify_offset);
654 return error;
655}
656
657clib_error_t *
658virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
659 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200660{
661 clib_error_t *error = 0;
662 u16 queue_size = 0;
663 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200664 vring_t vr;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200665 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000666 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200667
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000668 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200669 if (!virtio_pci_queue_size_valid (queue_size))
670 clib_warning ("queue size is not valid");
671
672 if (!is_pow2 (queue_size))
673 return clib_error_return (0, "ring size must be power of 2");
674
675 if (queue_size > 32768)
676 return clib_error_return (0, "ring size must be 32768 or lower");
677
678 if (queue_size == 0)
679 queue_size = 256;
680
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000681 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
682 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200683 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
684 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000685 ptr =
686 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
687 vif->numa_node);
688 if (!ptr)
689 return vlib_physmem_last_error (vm);
690 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200691 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
692 vring->desc = vr.desc;
693 vring->avail = vr.avail;
694 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000695 vring->queue_id = queue_num;
696 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
697
698 ASSERT (vring->buffers == 0);
699
700 vring->size = queue_size;
Damjan Marionf41244f2019-11-08 17:41:06 +0100701 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000702 queue_size);
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000703 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000704 vring->queue_notify_offset =
705 vif->notify_off_multiplier *
706 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
707 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
708 queue_num, vring->queue_notify_offset);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000709
710 return error;
711}
712
713clib_error_t *
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100714virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
715 u16 queue_num)
716{
717 if (vif->is_packed)
718 return virtio_pci_control_vring_packed_init (vm, vif, queue_num);
719 else
720 return virtio_pci_control_vring_split_init (vm, vif, queue_num);
721}
722
723clib_error_t *
724virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
725 u16 queue_num)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000726{
Benoît Ganneb6b484d2020-09-15 10:58:07 +0200727 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000728 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000729 u16 queue_size = 0;
730 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200731 vring_t vr;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000732 u32 i = 0;
733 void *ptr = NULL;
734
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000735 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000736 if (!virtio_pci_queue_size_valid (queue_size))
737 clib_warning ("queue size is not valid");
738
739 if (!is_pow2 (queue_size))
740 return clib_error_return (0, "ring size must be power of 2");
741
742 if (queue_size > 32768)
743 return clib_error_return (0, "ring size must be 32768 or lower");
744
745 if (queue_size == 0)
746 queue_size = 256;
747
748 if (queue_num % 2)
749 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000750 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
751 CLIB_CACHE_LINE_BYTES);
752 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Benoît Ganneb6b484d2020-09-15 10:58:07 +0200753 if (vif->max_queue_pairs < vtm->n_vlib_mains)
754 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000755 }
756 else
757 {
758 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
759 CLIB_CACHE_LINE_BYTES);
760 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
761 }
762 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
763 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
764 ptr =
765 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
766 vif->numa_node);
767 if (!ptr)
768 return vlib_physmem_last_error (vm);
769 clib_memset (ptr, 0, i);
770 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
771 vring->desc = vr.desc;
772 vring->avail = vr.avail;
773 vring->used = vr.used;
774 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200775 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200776 vring->flow_table = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200777
778 ASSERT (vring->buffers == 0);
779 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000780 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200781 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100782 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000783 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100784 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200785 }
786 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000787 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100788 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000789 queue_size);
790 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200791 vring->size = queue_size;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000792 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200793 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200794
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000795 vring->queue_notify_offset =
796 vif->notify_off_multiplier *
797 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
798 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
799 queue_num, vring->queue_notify_offset);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200800 return error;
801}
802
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100803clib_error_t *
804virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
805 u16 queue_num)
806{
807 vlib_thread_main_t *vtm = vlib_get_thread_main ();
808 clib_error_t *error = 0;
809 u16 queue_size = 0;
810 virtio_vring_t *vring;
811 u32 i = 0;
812 void *ptr = NULL;
813
814 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
815
816 if (queue_size > 32768)
817 return clib_error_return (0, "ring size must be 32768 or lower");
818
819 if (queue_size == 0)
820 queue_size = 256;
821
822 if (queue_num % 2)
823 {
824 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
825 CLIB_CACHE_LINE_BYTES);
826 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
827 if (vif->max_queue_pairs < vtm->n_vlib_mains)
828 clib_spinlock_init (&vring->lockp);
829 }
830 else
831 {
832 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
833 CLIB_CACHE_LINE_BYTES);
834 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
835 }
836
837 i =
838 (((queue_size * sizeof (vring_packed_desc_t)) +
839 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
840 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
841
842 ptr =
843 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
844 vif->numa_node);
845 if (!ptr)
846 return vlib_physmem_last_error (vm);
847
848 clib_memset (ptr, 0, i);
849 vring->packed_desc = ptr;
850
851 vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
852 vring->driver_event->off_wrap = 0;
853 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
854
855 vring->device_event =
856 ptr +
857 (((queue_size * sizeof (vring_packed_desc_t)) +
858 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
859 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
860 vring->device_event->off_wrap = 0;
861 vring->device_event->flags = 0;
862
863 vring->queue_id = queue_num;
864
865 vring->avail_wrap_counter = 1;
866 vring->used_wrap_counter = 1;
867
868 ASSERT (vring->buffers == 0);
869 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
870 if (queue_num % 2)
871 {
872 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
873 queue_size);
874 clib_memset_u32 (vring->buffers, ~0, queue_size);
875 }
876 else
877 {
878 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
879 queue_size);
880 }
881 vring->size = queue_size;
882 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring))
883 return clib_error_return (0, "error in queue address setup");
884
885 vring->queue_notify_offset =
886 vif->notify_off_multiplier *
887 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
888 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
889 queue_num, vring->queue_notify_offset);
890
891 return error;
892}
893
894clib_error_t *
895virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
896{
897 if (vif->is_packed)
898 return virtio_pci_vring_packed_init (vm, vif, queue_num);
899 else
900 return virtio_pci_vring_split_init (vm, vif, queue_num);
901}
902
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200903static void
904virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
905 u64 req_features)
906{
907 /*
908 * if features are not requested
909 * default: all supported features
910 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200911 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
912 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
913 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
914 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200915 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200916 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
917 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
918 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
919 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
920 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
921 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
922 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200923 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
924 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000925 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
926 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000927 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200928 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
929 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
930
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200931 if (vif->is_modern)
932 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
933
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100934 if (vif->is_packed)
935 {
936 supported_features |=
937 (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) |
938 VIRTIO_FEATURE (VIRTIO_F_IN_ORDER));
939 }
940
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200941 if (req_features == 0)
942 {
943 req_features = supported_features;
944 }
945
946 vif->features = req_features & vif->remote_features & supported_features;
947
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000948 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200949 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000950 u16 mtu = 0;
951 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
952
953 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200954 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
955 }
956
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100957 if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0)
958 vif->is_packed = 0;
959
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000960 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
961 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200962}
963
964void
965virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
966{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000967 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200968}
969
970int
971virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
972{
973 u8 status = 0;
974
975 /*
976 * Reset the device
977 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000978 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200979
980 /*
981 * Set the Acknowledge status bit
982 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000983 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200984
985 /*
986 * Set the Driver status bit
987 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000988 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200989
990 /*
991 * Read the status and verify it
992 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000993 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200994 if ((status & VIRTIO_CONFIG_STATUS_ACK)
995 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
996 vif->status = status;
997 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200998 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200999
1000 return 0;
1001}
1002
1003clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001004virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001005{
1006 clib_error_t *error = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001007 virtio_pci_cap_t cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001008 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001009 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
1010
1011 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001012 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001013 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001014 return clib_error_return (error,
1015 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001016 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001017 while (pos)
1018 {
1019 if ((error =
1020 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
1021 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001022 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001023 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001024 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001025 return clib_error_return (error,
1026 "error in reading the capability at [%2x]",
1027 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001028 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001029
1030 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
1031 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001032 u16 flags, table_size, table_size_mask = 0x07FF;
1033
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001034 if ((error =
1035 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
1036 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001037 return clib_error_return (error,
1038 "error in reading the capability at [%2x]",
1039 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001040
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001041 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +01001042 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001043 "msix interrupt vector table-size", table_size);
1044
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001045 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001046 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001047 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001048 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001049 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001050 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001051 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001052 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001053 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001054 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001055 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001056 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001057 }
1058
1059 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
1060 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001061 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001062 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001063 goto next;
1064 }
1065
Damjan Marionf41244f2019-11-08 17:41:06 +01001066 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001067 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
1068 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001069
Mohsin Kazmi0a507d72020-09-01 18:18:48 +02001070 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +02001071 {
1072 vif->bar = bar[cap.bar];
1073 vif->bar_id = cap.bar;
1074 }
1075 else
1076 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001077
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001078 switch (cap.cfg_type)
1079 {
1080 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001081 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001082 common_cfg = 1;
1083 break;
1084 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001085 if ((error =
1086 vlib_pci_read_write_config (vm, h, VLIB_READ,
1087 pos + sizeof (cap),
1088 &vif->notify_off_multiplier,
1089 sizeof
1090 (vif->notify_off_multiplier))))
1091 {
1092 virtio_log_error (vif, "notify off multiplier is not given");
1093 }
1094 else
1095 {
1096 virtio_log_debug (vif, "notify off multiplier is %u",
1097 vif->notify_off_multiplier);
1098 vif->notify_offset = cap.offset;
1099 notify = 1;
1100 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001101 break;
1102 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001103 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001104 dev_cfg = 1;
1105 break;
1106 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001107 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001108 isr = 1;
1109 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001110 case VIRTIO_PCI_CAP_PCI_CFG:
1111 if (cap.bar == 0)
1112 pci_cfg = 1;
1113 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001114 }
1115 next:
1116 pos = cap.cap_next;
1117 }
1118
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001119 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001120 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001121 vif->virtio_pci_func = &virtio_pci_legacy_func;
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001122 vif->notify_off_multiplier = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001123 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001124 return error;
1125 }
1126
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001127 vif->is_modern = 1;
1128 vif->virtio_pci_func = &virtio_pci_modern_func;
1129
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001130 if (!pci_cfg)
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001131 {
1132 virtio_log_debug (vif, "modern virtio pci device found");
1133 }
1134 else
1135 {
1136 virtio_log_debug (vif, "transitional virtio pci device found");
1137 }
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001138
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001139 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001140}
1141
1142static clib_error_t *
1143virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001144 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001145{
1146 clib_error_t *error = 0;
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +01001147 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001148 u8 status = 0;
1149
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001150 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001151 {
1152 args->rv = VNET_API_ERROR_UNSUPPORTED;
1153 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001154 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001155 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001156
1157 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001158 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001159 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001160 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001161 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001162 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001163 /*
1164 * read device features and negotiate (user) requested features
1165 */
1166 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +00001167 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1168 0)
1169 {
1170 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1171 "support VIRTIO_RING_F_INDIRECT_DESC features");
1172 }
1173 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1174 {
1175 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1176 "support VIRTIO_NET_F_MRG_RXBUF features");
1177 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001178 virtio_negotiate_features (vm, vif, args->features);
1179
1180 /*
1181 * After FEATURE_OK, driver should not accept new feature bits
1182 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001183 vif->virtio_pci_func->set_status (vm, vif,
1184 VIRTIO_CONFIG_STATUS_FEATURES_OK);
1185 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001186 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001187 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001188 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001189 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001190 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001191 return clib_error_return (error,
1192 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001193 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001194 vif->status = status;
1195
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001196 /*
1197 * get or set the mac address
1198 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001199 if (virtio_pci_get_mac (vm, vif))
1200 {
1201 f64 now = vlib_time_now (vm);
1202 u32 rnd;
1203 rnd = (u32) (now * 1e6);
1204 rnd = random_u32 (&rnd);
1205
1206 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1207 vif->mac_addr[0] = 2;
1208 vif->mac_addr[1] = 0xfe;
1209 virtio_pci_set_mac (vm, vif);
1210 }
1211
1212 virtio_set_net_hdr_size (vif);
1213
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001214 /*
1215 * Initialize the virtqueues
1216 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001217 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001218 {
1219 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1220 goto err;
1221 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001222
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001223 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1224 {
1225 if (vif->msix_table_size <= vif->max_queue_pairs)
1226 {
1227 virtio_log_error (vif,
1228 "error MSIX lines (%u) <= Number of RXQs (%u)",
1229 vif->msix_table_size, vif->max_queue_pairs);
1230 return clib_error_return (error,
1231 "error MSIX lines (%u) <= Number of RXQs (%u)",
1232 vif->msix_table_size,
1233 vif->max_queue_pairs);
1234 }
1235 }
1236
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001237 for (int i = 0; i < vif->max_queue_pairs; i++)
1238 {
1239 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001240 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001241 args->rv = VNET_API_ERROR_INIT_FAILED;
1242 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1243 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001244 error =
1245 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1246 RX_QUEUE (i), "initialization");
1247 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001248 }
1249 else
1250 {
1251 vif->num_rxqs++;
1252 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001253
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +01001254 if (i >= vtm->n_vlib_mains)
1255 {
1256 /*
1257 * There is 1:1 mapping between tx queue and vpp worker thread.
1258 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
1259 * index 1 and so on.
1260 * Multiple worker threads can poll same tx queue when number of
1261 * workers are more than tx queues. In this case, 1:N mapping
1262 * between tx queue and vpp worker thread.
1263 */
1264 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
1265 TX_QUEUE (i),
1266 "no VPP worker thread is available");
1267 continue;
1268 }
1269
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001270 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001271 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001272 args->rv = VNET_API_ERROR_INIT_FAILED;
1273 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1274 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001275 error =
1276 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1277 TX_QUEUE (i), "initialization");
1278 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001279 }
1280 else
1281 {
1282 vif->num_txqs++;
1283 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001284 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001285
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001286 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1287 {
1288 if ((error =
1289 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1290 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001291 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001292 vif->max_queue_pairs * 2, "initialization");
1293 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1294 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1295 }
1296 }
1297 else
1298 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001299 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001300 vif->cxq_vring = NULL;
1301 }
1302
1303 /*
1304 * set the msix interrupts
1305 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001306 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001307 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001308 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001309 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001310 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001311 {
1312 virtio_log_warning (vif, "config vector 0 is not set");
1313 }
1314 else
1315 {
1316 virtio_log_debug (vif, "config msix vector is set at 0");
1317 }
1318 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
1319 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001320 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
1321 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001322 VIRTIO_MSI_NO_VECTOR)
1323 {
1324 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
1325 RX_QUEUE (i), j);
1326 }
1327 else
1328 {
1329 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
1330 RX_QUEUE (i), "msix vector is set at", j);
1331 }
1332 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001333 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001334
1335 /*
1336 * set the driver status OK
1337 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001338 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1339 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001340err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001341 return error;
1342}
1343
1344void
1345virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1346{
1347 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001348 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001349 virtio_if_t *vif;
1350 vlib_pci_dev_handle_t h;
1351 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001352 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001353
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001354 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001355 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001356 if (vif->pci_addr.as_u32 == args->addr)
1357 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001358 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001359 args->error =
1360 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001361 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1362 format_vlib_pci_addr, &args->addr,
1363 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001364 return;
1365 }
1366 }));
1367 /* *INDENT-ON* */
1368
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001369 pool_get (vim->interfaces, vif);
1370 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001371 vif->per_interface_next_index = ~0;
1372 vif->pci_addr.as_u32 = args->addr;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001373 if (args->virtio_flags & VIRTIO_FLAG_PACKED)
1374 vif->is_packed = 1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001375
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001376 if ((error =
1377 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1378 virtio_pci_device_ids, &h)))
1379 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001380 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1381 args->error =
1382 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1383 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001384 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1385 format_vlib_pci_addr, &vif->pci_addr,
1386 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001387 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001388 return;
1389 }
1390 vif->pci_dev_handle = h;
1391 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001392 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001393 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001394
1395 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001396 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001397 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001398 goto error;
1399 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001400
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001401 void *bar[6];
1402 for (u32 i = 0; i <= 5; i++)
1403 {
1404
1405 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1406 {
1407 virtio_log_debug (vif, "no pci map region for bar %u", i);
1408 }
1409 else
1410 {
1411 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1412 bar[i]);
1413 }
1414 }
1415
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001416 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001417 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001418 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001419 goto error;
1420 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001421
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001422 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1423 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001424 {
1425 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001426 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001427 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001428 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001429 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001430 "error encountered on pci register msix handler 0");
1431 goto error;
1432 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001433
1434 if ((error =
1435 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1436 &virtio_pci_irq_queue_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001437 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001438 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001439 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001440 "error encountered on pci register msix handler 1");
1441 goto error;
1442 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001443
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001444 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001445 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001446 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001447 goto error;
1448 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001449 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001450 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001451 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001452 else if (interrupt_count == 1)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001453 {
1454 /*
1455 * if msix table-size is 1, fall back to intX.
1456 */
1457 if ((error =
1458 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1459 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001460 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001461 "error encountered on pci register interrupt handler");
1462 goto error;
1463 }
1464 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001465 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001466 }
1467 else
1468 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001469 /*
1470 * WARN: intX is showing some weird behaviour.
1471 * Please don't use interrupt mode with UIO driver.
1472 */
1473 vif->support_int_mode = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001474 virtio_log_debug (vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001475 }
1476
1477 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001478 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001479 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001480 goto error;
1481 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001482
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001483 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001484 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001485 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001486 goto error;
1487 }
1488
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001489 /* create interface */
1490 error = ethernet_register_interface (vnm, virtio_device_class.index,
1491 vif->dev_instance, vif->mac_addr,
1492 &vif->hw_if_index,
1493 virtio_pci_flag_change);
1494
1495 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001496 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001497 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001498 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001499 "error encountered on ethernet register interface");
1500 goto error;
1501 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001502
1503 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1504 vif->sw_if_index = sw->sw_if_index;
1505 args->sw_if_index = sw->sw_if_index;
1506
1507 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1508 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001509
1510 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1511 {
1512 error = virtio_set_packet_buffering (vif, args->buffering_size);
1513 if (error)
1514 {
1515 args->rv = VNET_API_ERROR_INIT_FAILED;
1516 virtio_log_error (vif,
1517 "error encountered during packet buffering init");
1518 goto error;
1519 }
1520 }
1521
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001522 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1523 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001524 u32 i = 0;
1525 vec_foreach_index (i, vif->rxq_vrings)
1526 {
1527 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1528 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1529 /* Set default rx mode to POLLING */
1530 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
Damjan Marioneabd4242020-10-07 20:59:07 +02001531 VNET_HW_IF_RX_MODE_POLLING);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001532 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001533 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1534 {
1535 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1536 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1537 VNET_HW_INTERFACE_FLAG_LINK_UP);
1538 }
1539 else
1540 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001541
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001542 virtio_pci_offloads (vm, vif, args->gso_enabled,
1543 args->checksum_offload_enabled);
1544
1545 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1546 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001547 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001548 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1549 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001550 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001551 return;
1552
1553error:
1554 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001555 if (args->rv == 0)
1556 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001557 args->error = error;
1558}
1559
1560int
1561virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1562{
1563 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001564 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001565 u32 i = 0;
1566
1567 if (vif->type != VIRTIO_IF_TYPE_PCI)
1568 return VNET_API_ERROR_INVALID_INTERFACE;
1569
1570 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1571
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001572 for (i = 0; i < vif->max_queue_pairs; i++)
1573 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001574 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1575 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001576 }
1577
1578 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001579 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001580
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001581 if (vif->virtio_pci_func)
1582 vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001583
1584 if (vif->hw_if_index)
1585 {
1586 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001587 vec_foreach_index (i, vif->rxq_vrings)
1588 {
1589 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1590 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001591 ethernet_delete_interface (vnm, vif->hw_if_index);
1592 }
1593
1594 vlib_pci_device_close (vm, vif->pci_dev_handle);
1595
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001596 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001597 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001598 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001599 if (vring->used)
1600 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001601 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001602 }
1603 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001604 vlib_physmem_free (vm, vring->desc);
1605 }
1606
1607 vec_foreach_index (i, vif->txq_vrings)
1608 {
1609 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001610 if (vring->used)
1611 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001612 virtio_free_buffers (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001613 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001614 vec_free (vring->buffers);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001615 gro_flow_table_free (vring->flow_table);
1616 virtio_vring_buffering_free (vm, vring->buffering);
Steven Luonge5584962019-06-25 13:47:06 -07001617 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001618 vlib_physmem_free (vm, vring->desc);
1619 }
1620
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001621 if (vif->cxq_vring != NULL)
1622 {
1623 u16 last = vif->cxq_vring->last_used_idx;
1624 u16 n_left = vif->cxq_vring->used->idx - last;
1625 while (n_left)
1626 {
1627 last++;
1628 n_left--;
1629 }
1630
1631 vif->cxq_vring->last_used_idx = last;
1632 vlib_physmem_free (vm, vif->cxq_vring->desc);
1633 }
1634
1635 vec_free (vif->rxq_vrings);
1636 vec_free (vif->txq_vrings);
1637 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001638
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001639 clib_error_free (vif->error);
1640 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001641 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001642
1643 return 0;
1644}
1645
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001646int
1647virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1648 int gso_enabled,
1649 int checksum_offload_enabled,
1650 int offloads_disabled)
1651{
1652 if (vif->type != VIRTIO_IF_TYPE_PCI)
1653 return VNET_API_ERROR_INVALID_INTERFACE;
1654
1655 if (gso_enabled)
1656 virtio_pci_offloads (vm, vif, 1, 0);
1657 else if (checksum_offload_enabled)
1658 virtio_pci_offloads (vm, vif, 0, 1);
1659 else if (offloads_disabled)
1660 virtio_pci_offloads (vm, vif, 0, 0);
1661
1662 return 0;
1663}
1664
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001665/*
1666 * fd.io coding-style-patch-verification: ON
1667 *
1668 * Local Variables:
1669 * eval: (c-set-style "gnu")
1670 * End:
1671 */