blob: b32f72bc66b9054d9786b246cb554f16a1708b29 [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>
Damjan Marion94100532020-11-06 23:25:57 +010027#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020028
29#define PCI_VENDOR_ID_VIRTIO 0x1af4
30#define PCI_DEVICE_ID_VIRTIO_NIC 0x1000
31/* Doesn't support modern device */
32#define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041
33
34#define PCI_CAPABILITY_LIST 0x34
35#define PCI_CAP_ID_VNDR 0x09
36#define PCI_CAP_ID_MSIX 0x11
37
38#define PCI_MSIX_ENABLE 0x8000
39
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020040static pci_device_id_t virtio_pci_device_ids[] = {
41 {
42 .vendor_id = PCI_VENDOR_ID_VIRTIO,
43 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
44 {
45 .vendor_id = PCI_VENDOR_ID_VIRTIO,
46 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
47 {0},
48};
49
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020050static u32
51virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
52 u32 flags)
53{
54 return 0;
55}
56
57static clib_error_t *
58virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
59{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020060 clib_error_t *error = 0;
61 u16 max_queue_pairs = 1;
62
63 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
64 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +000065 max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020066 }
67
Damjan Marionf41244f2019-11-08 17:41:06 +010068 virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020069 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
Mohsin Kazmi57d73212020-03-05 12:22:41 +000070 return clib_error_return (error, "max queue pair is %x,"
71 " should be in range [1, 0x8000]",
72 max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020073
74 vif->max_queue_pairs = max_queue_pairs;
75 return error;
76}
77
78static void
79virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
80{
Mohsin Kazmia0a68332020-07-16 12:55:42 +000081 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
82 vif->virtio_pci_func->set_mac (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020083}
84
85static u32
86virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
87{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000088 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020089 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +000090 vif->virtio_pci_func->get_mac (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020091 return 0;
92 }
93 return 1;
94}
95
96static u16
97virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
98{
99 /*
100 * Minimal driver: assumes link is up
101 */
102 u16 status = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000103 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000104 status = vif->virtio_pci_func->get_device_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200105 return status;
106}
107
108static void
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000109virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
110 u16 line)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200111{
112 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000113 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200114 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000115 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000116 line--;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200117 u16 qid = line;
118
Damjan Marion94100532020-11-06 23:25:57 +0100119 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
120 vnet_hw_if_rx_queue_set_int_pending (vnm, vring->queue_index);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200121}
122
123static void
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000124virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
125 u16 line)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200126{
127 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000128 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200129 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000130 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200131
132 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
133 {
134 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
135 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
136 VNET_HW_INTERFACE_FLAG_LINK_UP);
137 }
138 else
139 {
140 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
141 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
142 }
143}
144
145static void
146virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
147{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000148 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200149 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000150 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200151 u8 isr = 0;
152 u16 line = 0;
153
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000154 isr = vif->virtio_pci_func->get_isr (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200155
156 /*
157 * If the lower bit is set: look through the used rings of
158 * all virtqueues for the device, to see if any progress has
159 * been made by the device which requires servicing.
160 */
161 if (isr & VIRTIO_PCI_ISR_INTR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000162 {
163 for (; line < vif->num_rxqs; line++)
164 virtio_pci_irq_queue_handler (vm, h, (line + 1));
165 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200166
167 if (isr & VIRTIO_PCI_ISR_CONFIG)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000168 virtio_pci_irq_config_handler (vm, h, line);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200169}
170
171inline void
172device_status (vlib_main_t * vm, virtio_if_t * vif)
173{
174 struct status_struct
175 {
176 u8 bit;
177 char *str;
178 };
179 struct status_struct *status_entry;
180 static struct status_struct status_array[] = {
181#define _(s,b) { .str = #s, .bit = b, },
182 foreach_virtio_config_status_flags
183#undef _
184 {.str = NULL}
185 };
186
187 vlib_cli_output (vm, " status 0x%x", vif->status);
188
189 status_entry = (struct status_struct *) &status_array;
190 while (status_entry->str)
191 {
192 if (vif->status & status_entry->bit)
193 vlib_cli_output (vm, " %s (%x)", status_entry->str,
194 status_entry->bit);
195 status_entry++;
196 }
197}
198
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000199static int
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100200virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif,
201 virtio_ctrl_msg_t * data, u32 len)
202{
203 virtio_vring_t *vring = vif->cxq_vring;
204 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
205 virtio_ctrl_msg_t result;
206 u32 buffer_index;
207 vlib_buffer_t *b;
208 u16 used, next;
209 u16 sz = vring->size;
210 u16 flags = 0, first_desc_flags = 0;
211
212 used = vring->desc_in_use;
213 next = vring->desc_next;
214 vring_packed_desc_t *d = &vring->packed_desc[next];
215
216 if (vlib_buffer_alloc (vm, &buffer_index, 1))
217 b = vlib_get_buffer (vm, buffer_index);
218 else
219 return VIRTIO_NET_ERR;
220 /*
221 * current_data may not be initialized with 0 and may contain
222 * previous offset.
223 */
224 b->current_data = 0;
225 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
226
227 first_desc_flags = VRING_DESC_F_NEXT;
228 if (vring->avail_wrap_counter)
229 {
230 first_desc_flags |= VRING_DESC_F_AVAIL;
231 first_desc_flags &= ~VRING_DESC_F_USED;
232 }
233 else
234 {
235 first_desc_flags &= ~VRING_DESC_F_AVAIL;
236 first_desc_flags |= VRING_DESC_F_USED;
237 }
238 d->addr = vlib_buffer_get_current_pa (vm, b);
239 d->len = sizeof (virtio_net_ctrl_hdr_t);
240 d->id = next;
241
242 next++;
243 if (next >= sz)
244 {
245 next = 0;
246 vring->avail_wrap_counter ^= 1;
247 }
248 used++;
249
250 d = &vring->packed_desc[next];
251 flags = VRING_DESC_F_NEXT;
252 if (vring->avail_wrap_counter)
253 {
254 flags |= VRING_DESC_F_AVAIL;
255 flags &= ~VRING_DESC_F_USED;
256 }
257 else
258 {
259 flags &= ~VRING_DESC_F_AVAIL;
260 flags |= VRING_DESC_F_USED;
261 }
262 d->addr = vlib_buffer_get_current_pa (vm, b) +
263 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
264 d->len = len;
265 d->id = next;
266 d->flags = flags;
267
268 next++;
269 if (next >= sz)
270 {
271 next = 0;
272 vring->avail_wrap_counter ^= 1;
273 }
274 used++;
275
276 d = &vring->packed_desc[next];
277 flags = VRING_DESC_F_WRITE;
278 if (vring->avail_wrap_counter)
279 {
280 flags |= VRING_DESC_F_AVAIL;
281 flags &= ~VRING_DESC_F_USED;
282 }
283 else
284 {
285 flags &= ~VRING_DESC_F_AVAIL;
286 flags |= VRING_DESC_F_USED;
287 }
288 d->addr = vlib_buffer_get_current_pa (vm, b) +
289 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
290 d->len = sizeof (data->status);
291 d->id = next;
292 d->flags = flags;
293
294 next++;
295 if (next >= sz)
296 {
297 next = 0;
298 vring->avail_wrap_counter ^= 1;
299 }
300 used++;
301
302 CLIB_MEMORY_STORE_BARRIER ();
303 vring->packed_desc[vring->desc_next].flags = first_desc_flags;
304 vring->desc_next = next;
305 vring->desc_in_use = used;
306 CLIB_MEMORY_BARRIER ();
307 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
308 {
309 virtio_kick (vm, vring, vif);
310 }
311
312 u16 last = vring->last_used_idx;
313 d = &vring->packed_desc[last];
314 do
315 {
316 flags = d->flags;
317 }
318 while ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7)
319 || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15));
320
321 last += 3;
322 if (last >= vring->size)
323 {
324 last = last - vring->size;
325 vring->used_wrap_counter ^= 1;
326 }
327 vring->desc_in_use -= 3;
328 vring->last_used_idx = last;
329
330 CLIB_MEMORY_BARRIER ();
331 clib_memcpy (&result, vlib_buffer_get_current (b),
332 sizeof (virtio_ctrl_msg_t));
333 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
334 status = result.status;
335 vlib_buffer_free (vm, &buffer_index, 1);
336 return status;
337}
338
339static int
340virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif,
341 virtio_ctrl_msg_t * data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000342{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000343 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200344 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
345 virtio_ctrl_msg_t result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000346 u32 buffer_index;
347 vlib_buffer_t *b;
348 u16 used, next, avail;
349 u16 sz = vring->size;
350 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000351
352 used = vring->desc_in_use;
353 next = vring->desc_next;
354 avail = vring->avail->idx;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200355 vring_desc_t *d = &vring->desc[next];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000356
357 if (vlib_buffer_alloc (vm, &buffer_index, 1))
358 b = vlib_get_buffer (vm, buffer_index);
359 else
360 return VIRTIO_NET_ERR;
361 /*
362 * current_data may not be initialized with 0 and may contain
363 * previous offset.
364 */
365 b->current_data = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200366 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000367 d->flags = VRING_DESC_F_NEXT;
368 d->addr = vlib_buffer_get_current_pa (vm, b);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200369 d->len = sizeof (virtio_net_ctrl_hdr_t);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000370 vring->avail->ring[avail & mask] = next;
371 avail++;
372 next = (next + 1) & mask;
373 d->next = next;
374 used++;
375
376 d = &vring->desc[next];
377 d->flags = VRING_DESC_F_NEXT;
378 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200379 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200380 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000381 next = (next + 1) & mask;
382 d->next = next;
383 used++;
384
385 d = &vring->desc[next];
386 d->flags = VRING_DESC_F_WRITE;
387 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200388 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200389 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000390 next = (next + 1) & mask;
391 used++;
392
393 CLIB_MEMORY_STORE_BARRIER ();
394 vring->avail->idx = avail;
395 vring->desc_next = next;
396 vring->desc_in_use = used;
397
398 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
399 {
400 virtio_kick (vm, vring, vif);
401 }
402
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000403 u16 last = vring->last_used_idx, n_left = 0;
404 n_left = vring->used->idx - last;
405
406 while (n_left)
407 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200408 vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000409 u16 slot = e->id;
410
411 d = &vring->desc[slot];
412 while (d->flags & VRING_DESC_F_NEXT)
413 {
414 used--;
415 slot = d->next;
416 d = &vring->desc[slot];
417 }
418 used--;
419 last++;
420 n_left--;
421 }
422 vring->desc_in_use = used;
423 vring->last_used_idx = last;
424
425 CLIB_MEMORY_BARRIER ();
426 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200427 sizeof (virtio_ctrl_msg_t));
Damjan Marionf41244f2019-11-08 17:41:06 +0100428 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000429 status = result.status;
430 vlib_buffer_free (vm, &buffer_index, 1);
431 return status;
432}
433
Mohsin Kazmi05493782019-05-01 14:26:17 +0200434static int
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100435virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
436 virtio_ctrl_msg_t * data, u32 len)
437{
438 if (vif->is_packed)
439 return virtio_pci_send_ctrl_msg_packed (vm, vif, data, len);
440 else
441 return virtio_pci_send_ctrl_msg_split (vm, vif, data, len);
442}
443
444static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000445virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
446{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200447 virtio_ctrl_msg_t offload_hdr;
448 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000449
450 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
451 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
452 offload_hdr.status = VIRTIO_NET_ERR;
453 u64 offloads = 0ULL;
454 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
455
456 status =
457 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
458 virtio_log_debug (vif, "disable offloads");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000459 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
460 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000461 return status;
462}
463
464static int
465virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
466{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200467 virtio_ctrl_msg_t csum_offload_hdr;
468 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000469
470 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
471 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
472 csum_offload_hdr.status = VIRTIO_NET_ERR;
473 u64 offloads = 0ULL;
474 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
475 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
476
477 status =
478 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
479 virtio_log_debug (vif, "enable checksum offload");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000480 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
481 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000482 return status;
483}
484
485static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200486virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
487{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200488 virtio_ctrl_msg_t gso_hdr;
489 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200490
491 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
492 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
493 gso_hdr.status = VIRTIO_NET_ERR;
494 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
495 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000496 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200497 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
498
499 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100500 virtio_log_debug (vif, "enable gso");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000501 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
502 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200503 return status;
504}
505
506static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000507virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
508 int csum_offload_enabled)
509{
510 vnet_main_t *vnm = vnet_get_main ();
511 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
512
513 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
514 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
515 {
516 if (gso_enabled
517 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
518 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
519 {
520 if (virtio_pci_enable_gso (vm, vif))
521 {
522 virtio_log_warning (vif, "gso is not enabled");
523 }
524 else
525 {
526 vif->gso_enabled = 1;
527 vif->csum_offload_enabled = 0;
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100528 hw->caps |= VNET_HW_IF_CAP_TCP_GSO |
529 VNET_HW_IF_CAP_TX_TCP_CKSUM |
530 VNET_HW_IF_CAP_TX_UDP_CKSUM;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000531 }
532 }
533 else if (csum_offload_enabled
534 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
535 {
536 if (virtio_pci_enable_checksum_offload (vm, vif))
537 {
538 virtio_log_warning (vif, "checksum offload is not enabled");
539 }
540 else
541 {
542 vif->csum_offload_enabled = 1;
543 vif->gso_enabled = 0;
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100544 hw->caps &= ~VNET_HW_IF_CAP_TCP_GSO;
545 hw->caps |=
546 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000547 }
548 }
549 else
550 {
551 if (virtio_pci_disable_offload (vm, vif))
552 {
553 virtio_log_warning (vif, "offloads are not disabled");
554 }
555 else
556 {
557 vif->csum_offload_enabled = 0;
558 vif->gso_enabled = 0;
Damjan Mariond4f88cc2022-01-05 01:52:38 +0100559 hw->caps &=
560 ~(VNET_HW_IF_CAP_L4_TX_CKSUM | VNET_HW_IF_CAP_TCP_GSO);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000561 }
562 }
563 }
564
565 return 0;
566}
567
568static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200569virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
570 u16 num_queues)
571{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200572 virtio_ctrl_msg_t mq_hdr;
573 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200574
575 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
576 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
577 mq_hdr.status = VIRTIO_NET_ERR;
578 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
579
580 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100581 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200582 return status;
583}
584
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200585static u8
586virtio_pci_queue_size_valid (u16 qsz)
587{
588 if (qsz < 64 || qsz > 4096)
589 return 0;
590 if ((qsz % 64) != 0)
591 return 0;
592 return 1;
593}
594
595clib_error_t *
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100596virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
597 u16 queue_num)
598{
599 clib_error_t *error = 0;
600 u16 queue_size = 0;
601 virtio_vring_t *vring;
602 u32 i = 0;
603 void *ptr = NULL;
604
605 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
606
607 if (queue_size > 32768)
608 return clib_error_return (0, "ring size must be 32768 or lower");
609
610 if (queue_size == 0)
611 queue_size = 256;
612
613 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
614 vring = vec_elt_at_index (vif->cxq_vring, 0);
615
616 i =
617 (((queue_size * sizeof (vring_packed_desc_t)) +
618 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
619 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
620
621 ptr =
622 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
623 vif->numa_node);
624 if (!ptr)
625 return vlib_physmem_last_error (vm);
626 clib_memset (ptr, 0, i);
627
628 vring->packed_desc = ptr;
629
630 vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
631 vring->driver_event->off_wrap = 0;
632 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
633
634 vring->device_event =
635 ptr +
636 (((queue_size * sizeof (vring_packed_desc_t)) +
637 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
638 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
639 vring->device_event->off_wrap = 0;
640 vring->device_event->flags = 0;
641
642 vring->queue_id = queue_num;
643 vring->size = queue_size;
644 vring->avail_wrap_counter = 1;
645 vring->used_wrap_counter = 1;
646
647 ASSERT (vring->buffers == 0);
648
649 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
650 queue_size);
651 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring);
652 vring->queue_notify_offset =
653 vif->notify_off_multiplier *
654 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
655 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
656 queue_num, vring->queue_notify_offset);
657 return error;
658}
659
660clib_error_t *
661virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
662 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200663{
664 clib_error_t *error = 0;
665 u16 queue_size = 0;
666 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200667 vring_t vr;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200668 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000669 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200670
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000671 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200672 if (!virtio_pci_queue_size_valid (queue_size))
673 clib_warning ("queue size is not valid");
674
675 if (!is_pow2 (queue_size))
676 return clib_error_return (0, "ring size must be power of 2");
677
678 if (queue_size > 32768)
679 return clib_error_return (0, "ring size must be 32768 or lower");
680
681 if (queue_size == 0)
682 queue_size = 256;
683
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000684 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
685 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200686 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
687 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000688 ptr =
689 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
690 vif->numa_node);
691 if (!ptr)
692 return vlib_physmem_last_error (vm);
693 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200694 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
695 vring->desc = vr.desc;
696 vring->avail = vr.avail;
697 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000698 vring->queue_id = queue_num;
699 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
700
701 ASSERT (vring->buffers == 0);
702
703 vring->size = queue_size;
Damjan Marionf41244f2019-11-08 17:41:06 +0100704 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000705 queue_size);
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000706 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000707 vring->queue_notify_offset =
708 vif->notify_off_multiplier *
709 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
710 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
711 queue_num, vring->queue_notify_offset);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000712
713 return error;
714}
715
716clib_error_t *
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100717virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
718 u16 queue_num)
719{
720 if (vif->is_packed)
721 return virtio_pci_control_vring_packed_init (vm, vif, queue_num);
722 else
723 return virtio_pci_control_vring_split_init (vm, vif, queue_num);
724}
725
726clib_error_t *
727virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
728 u16 queue_num)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000729{
730 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000731 u16 queue_size = 0;
732 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200733 vring_t vr;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000734 u32 i = 0;
735 void *ptr = NULL;
736
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000737 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000738 if (!virtio_pci_queue_size_valid (queue_size))
739 clib_warning ("queue size is not valid");
740
741 if (!is_pow2 (queue_size))
742 return clib_error_return (0, "ring size must be power of 2");
743
744 if (queue_size > 32768)
745 return clib_error_return (0, "ring size must be 32768 or lower");
746
747 if (queue_size == 0)
748 queue_size = 256;
749
750 if (queue_num % 2)
751 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000752 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
753 CLIB_CACHE_LINE_BYTES);
754 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000755 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000756 }
757 else
758 {
759 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
760 CLIB_CACHE_LINE_BYTES);
761 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
762 }
763 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
764 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
765 ptr =
766 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
767 vif->numa_node);
768 if (!ptr)
769 return vlib_physmem_last_error (vm);
770 clib_memset (ptr, 0, i);
771 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
772 vring->desc = vr.desc;
773 vring->avail = vr.avail;
774 vring->used = vr.used;
775 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200776 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200777 vring->flow_table = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200778
779 ASSERT (vring->buffers == 0);
780 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000781 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200782 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100783 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000784 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100785 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200786 }
787 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000788 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100789 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000790 queue_size);
791 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200792 vring->size = queue_size;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000793 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200794 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200795
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000796 vring->queue_notify_offset =
797 vif->notify_off_multiplier *
798 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
799 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
800 queue_num, vring->queue_notify_offset);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200801 return error;
802}
803
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100804clib_error_t *
805virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
806 u16 queue_num)
807{
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100808 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));
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000827 clib_spinlock_init (&vring->lockp);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100828 }
829 else
830 {
831 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
832 CLIB_CACHE_LINE_BYTES);
833 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
834 }
835
836 i =
837 (((queue_size * sizeof (vring_packed_desc_t)) +
838 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
839 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
840
841 ptr =
842 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
843 vif->numa_node);
844 if (!ptr)
845 return vlib_physmem_last_error (vm);
846
847 clib_memset (ptr, 0, i);
848 vring->packed_desc = ptr;
849
850 vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
851 vring->driver_event->off_wrap = 0;
852 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
853
854 vring->device_event =
855 ptr +
856 (((queue_size * sizeof (vring_packed_desc_t)) +
857 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
858 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
859 vring->device_event->off_wrap = 0;
860 vring->device_event->flags = 0;
861
862 vring->queue_id = queue_num;
863
864 vring->avail_wrap_counter = 1;
865 vring->used_wrap_counter = 1;
866
867 ASSERT (vring->buffers == 0);
868 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
869 if (queue_num % 2)
870 {
871 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
872 queue_size);
873 clib_memset_u32 (vring->buffers, ~0, queue_size);
874 }
875 else
876 {
877 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
878 queue_size);
879 }
880 vring->size = queue_size;
881 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring))
882 return clib_error_return (0, "error in queue address setup");
883
884 vring->queue_notify_offset =
885 vif->notify_off_multiplier *
886 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
887 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
888 queue_num, vring->queue_notify_offset);
889
890 return error;
891}
892
893clib_error_t *
894virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
895{
896 if (vif->is_packed)
897 return virtio_pci_vring_packed_init (vm, vif, queue_num);
898 else
899 return virtio_pci_vring_split_init (vm, vif, queue_num);
900}
901
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200902static void
903virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
904 u64 req_features)
905{
906 /*
907 * if features are not requested
908 * default: all supported features
909 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200910 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
911 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
912 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
913 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200914 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200915 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
916 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
917 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
918 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
919 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
920 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
921 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200922 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
923 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000924 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
925 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000926 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200927 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
928 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
929
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200930 if (vif->is_modern)
931 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
932
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100933 if (vif->is_packed)
934 {
935 supported_features |=
936 (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) |
937 VIRTIO_FEATURE (VIRTIO_F_IN_ORDER));
938 }
939
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200940 if (req_features == 0)
941 {
942 req_features = supported_features;
943 }
944
945 vif->features = req_features & vif->remote_features & supported_features;
946
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000947 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200948 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000949 u16 mtu = 0;
950 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
951
952 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200953 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
954 }
955
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100956 if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0)
957 vif->is_packed = 0;
958
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000959 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
960 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200961}
962
963void
964virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
965{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000966 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200967}
968
969int
970virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
971{
972 u8 status = 0;
973
974 /*
975 * Reset the device
976 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000977 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200978
979 /*
980 * Set the Acknowledge status bit
981 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000982 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200983
984 /*
985 * Set the Driver status bit
986 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000987 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200988
989 /*
990 * Read the status and verify it
991 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000992 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200993 if ((status & VIRTIO_CONFIG_STATUS_ACK)
994 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
995 vif->status = status;
996 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200997 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200998
999 return 0;
1000}
1001
1002clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001003virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001004{
1005 clib_error_t *error = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +02001006 virtio_pci_cap_t cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001007 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001008 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
1009
1010 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001011 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001012 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001013 return clib_error_return (error,
1014 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001015 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001016 while (pos)
1017 {
1018 if ((error =
1019 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
1020 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001021 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001022 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001023 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001024 return clib_error_return (error,
1025 "error in reading the capability at [%2x]",
1026 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001027 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001028
1029 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
1030 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001031 u16 flags, table_size, table_size_mask = 0x07FF;
1032
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001033 if ((error =
1034 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
1035 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001036 return clib_error_return (error,
1037 "error in reading the capability at [%2x]",
1038 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001039
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001040 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +01001041 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001042 "msix interrupt vector table-size", table_size);
1043
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001044 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001045 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001046 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001047 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001048 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001049 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001050 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001051 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001052 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001053 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001054 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001055 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001056 }
1057
1058 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
1059 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001060 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001061 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001062 goto next;
1063 }
1064
Damjan Marionf41244f2019-11-08 17:41:06 +01001065 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001066 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
1067 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001068
Mohsin Kazmi0a507d72020-09-01 18:18:48 +02001069 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +02001070 {
1071 vif->bar = bar[cap.bar];
1072 vif->bar_id = cap.bar;
1073 }
1074 else
1075 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001076
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001077 switch (cap.cfg_type)
1078 {
1079 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001080 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001081 common_cfg = 1;
1082 break;
1083 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001084 if ((error =
1085 vlib_pci_read_write_config (vm, h, VLIB_READ,
1086 pos + sizeof (cap),
1087 &vif->notify_off_multiplier,
1088 sizeof
1089 (vif->notify_off_multiplier))))
1090 {
1091 virtio_log_error (vif, "notify off multiplier is not given");
1092 }
1093 else
1094 {
1095 virtio_log_debug (vif, "notify off multiplier is %u",
1096 vif->notify_off_multiplier);
1097 vif->notify_offset = cap.offset;
1098 notify = 1;
1099 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001100 break;
1101 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001102 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001103 dev_cfg = 1;
1104 break;
1105 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001106 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001107 isr = 1;
1108 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001109 case VIRTIO_PCI_CAP_PCI_CFG:
1110 if (cap.bar == 0)
1111 pci_cfg = 1;
1112 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001113 }
1114 next:
1115 pos = cap.cap_next;
1116 }
1117
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001118 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001119 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001120 vif->virtio_pci_func = &virtio_pci_legacy_func;
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001121 vif->notify_off_multiplier = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001122 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001123 return error;
1124 }
1125
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001126 vif->is_modern = 1;
1127 vif->virtio_pci_func = &virtio_pci_modern_func;
1128
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001129 if (!pci_cfg)
Mohsin Kazmi162a2962020-09-29 10:01:25 +00001130 {
1131 virtio_log_debug (vif, "modern virtio pci device found");
1132 }
1133 else
1134 {
1135 virtio_log_debug (vif, "transitional virtio pci device found");
1136 }
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001137
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001138 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001139}
1140
1141static clib_error_t *
1142virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001143 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001144{
1145 clib_error_t *error = 0;
1146 u8 status = 0;
1147
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001148 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001149 {
1150 args->rv = VNET_API_ERROR_UNSUPPORTED;
1151 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001152 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001153 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001154
1155 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001156 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001157 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001158 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001159 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001160 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001161 /*
1162 * read device features and negotiate (user) requested features
1163 */
1164 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +00001165 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1166 0)
1167 {
1168 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1169 "support VIRTIO_RING_F_INDIRECT_DESC features");
1170 }
1171 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1172 {
1173 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1174 "support VIRTIO_NET_F_MRG_RXBUF features");
1175 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001176 virtio_negotiate_features (vm, vif, args->features);
1177
1178 /*
1179 * After FEATURE_OK, driver should not accept new feature bits
1180 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001181 vif->virtio_pci_func->set_status (vm, vif,
1182 VIRTIO_CONFIG_STATUS_FEATURES_OK);
1183 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001184 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001185 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001186 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001187 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001188 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +02001189 return clib_error_return (error,
1190 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001191 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001192 vif->status = status;
1193
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001194 /*
1195 * get or set the mac address
1196 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001197 if (virtio_pci_get_mac (vm, vif))
1198 {
1199 f64 now = vlib_time_now (vm);
1200 u32 rnd;
1201 rnd = (u32) (now * 1e6);
1202 rnd = random_u32 (&rnd);
1203
1204 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1205 vif->mac_addr[0] = 2;
1206 vif->mac_addr[1] = 0xfe;
1207 virtio_pci_set_mac (vm, vif);
1208 }
1209
1210 virtio_set_net_hdr_size (vif);
1211
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001212 /*
1213 * Initialize the virtqueues
1214 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001215 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001216 {
1217 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1218 goto err;
1219 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001220
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001221 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1222 {
1223 if (vif->msix_table_size <= vif->max_queue_pairs)
1224 {
1225 virtio_log_error (vif,
1226 "error MSIX lines (%u) <= Number of RXQs (%u)",
1227 vif->msix_table_size, vif->max_queue_pairs);
1228 return clib_error_return (error,
1229 "error MSIX lines (%u) <= Number of RXQs (%u)",
1230 vif->msix_table_size,
1231 vif->max_queue_pairs);
1232 }
1233 }
1234
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001235 for (int i = 0; i < vif->max_queue_pairs; i++)
1236 {
1237 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001238 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001239 args->rv = VNET_API_ERROR_INIT_FAILED;
1240 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1241 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001242 error =
1243 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1244 RX_QUEUE (i), "initialization");
1245 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001246 }
1247 else
1248 {
1249 vif->num_rxqs++;
1250 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001251
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001252 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001253 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001254 args->rv = VNET_API_ERROR_INIT_FAILED;
1255 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1256 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001257 error =
1258 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1259 TX_QUEUE (i), "initialization");
1260 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001261 }
1262 else
1263 {
1264 vif->num_txqs++;
1265 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001266 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001267
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001268 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1269 {
1270 if ((error =
1271 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1272 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001273 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001274 vif->max_queue_pairs * 2, "initialization");
1275 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1276 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1277 }
1278 }
1279 else
1280 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001281 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001282 vif->cxq_vring = NULL;
1283 }
1284
1285 /*
1286 * set the msix interrupts
1287 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001288 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001289 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001290 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001291 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001292 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001293 {
1294 virtio_log_warning (vif, "config vector 0 is not set");
1295 }
1296 else
1297 {
1298 virtio_log_debug (vif, "config msix vector is set at 0");
1299 }
1300 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
1301 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001302 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
1303 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001304 VIRTIO_MSI_NO_VECTOR)
1305 {
1306 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
1307 RX_QUEUE (i), j);
1308 }
1309 else
1310 {
1311 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
1312 RX_QUEUE (i), "msix vector is set at", j);
1313 }
1314 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001315 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001316
1317 /*
1318 * set the driver status OK
1319 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001320 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1321 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001322err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001323 return error;
1324}
1325
1326void
1327virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1328{
1329 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001330 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001331 virtio_if_t *vif;
1332 vlib_pci_dev_handle_t h;
1333 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001334 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001335
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001336 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001337 pool_foreach (vif, vim->interfaces) {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001338 if (vif->pci_addr.as_u32 == args->addr)
1339 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001340 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001341 args->error =
1342 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001343 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1344 format_vlib_pci_addr, &args->addr,
1345 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001346 return;
1347 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001348 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001349 /* *INDENT-ON* */
1350
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001351 pool_get (vim->interfaces, vif);
1352 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001353 vif->per_interface_next_index = ~0;
1354 vif->pci_addr.as_u32 = args->addr;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001355 if (args->virtio_flags & VIRTIO_FLAG_PACKED)
1356 vif->is_packed = 1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001357
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001358 if ((error =
1359 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1360 virtio_pci_device_ids, &h)))
1361 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001362 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1363 args->error =
1364 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1365 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001366 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1367 format_vlib_pci_addr, &vif->pci_addr,
1368 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001369 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001370 return;
1371 }
1372 vif->pci_dev_handle = h;
1373 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001374 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001375 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001376
1377 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001378 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001379 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001380 goto error;
1381 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001382
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001383 void *bar[6];
1384 for (u32 i = 0; i <= 5; i++)
1385 {
1386
1387 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1388 {
1389 virtio_log_debug (vif, "no pci map region for bar %u", i);
1390 }
1391 else
1392 {
1393 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1394 bar[i]);
1395 }
1396 }
1397
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001398 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001399 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001400 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001401 goto error;
1402 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001403
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001404 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1405 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001406 {
1407 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001408 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001409 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001410 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001411 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001412 "error encountered on pci register msix handler 0");
1413 goto error;
1414 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001415
1416 if ((error =
1417 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1418 &virtio_pci_irq_queue_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001419 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001420 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001421 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001422 "error encountered on pci register msix handler 1");
1423 goto error;
1424 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001425
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001426 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001427 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001428 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001429 goto error;
1430 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001431 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001432 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001433 }
Mohsin Kazmi5db2f4a2021-02-22 18:27:57 +00001434 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001435 {
1436 /*
Mohsin Kazmi5db2f4a2021-02-22 18:27:57 +00001437 * WARN: performance will be sub-optimal.
1438 * Fall back to intX, if msix table-size is 1 or
1439 * if UIO driver is being used.
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001440 */
1441 if ((error =
1442 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1443 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001444 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001445 "error encountered on pci register interrupt handler");
1446 goto error;
1447 }
1448 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001449 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001450 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001451
1452 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001453 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001454 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001455 goto error;
1456 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001457
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001458 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001459 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001460 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001461 goto error;
1462 }
1463
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001464 /* create interface */
Damjan Marion5c954c42022-01-06 20:36:14 +01001465 vnet_eth_interface_registration_t eir = {};
1466 eir.dev_class_index = virtio_device_class.index;
1467 eir.dev_instance = vif->dev_instance;
1468 eir.address = vif->mac_addr;
1469 eir.cb.flag_change = virtio_pci_flag_change;
1470 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001471
1472 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1473 vif->sw_if_index = sw->sw_if_index;
1474 args->sw_if_index = sw->sw_if_index;
1475
1476 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001477 hw->caps |= VNET_HW_IF_CAP_INT_MODE;
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001478
1479 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1480 {
1481 error = virtio_set_packet_buffering (vif, args->buffering_size);
1482 if (error)
1483 {
1484 args->rv = VNET_API_ERROR_INIT_FAILED;
1485 virtio_log_error (vif,
1486 "error encountered during packet buffering init");
1487 goto error;
1488 }
1489 }
1490
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001491 virtio_pre_input_node_enable (vm, vif);
Damjan Marion94100532020-11-06 23:25:57 +01001492 virtio_vring_set_rx_queues (vm, vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001493 virtio_vring_set_tx_queues (vm, vif);
Damjan Marion94100532020-11-06 23:25:57 +01001494
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001495 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1496 {
1497 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1498 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1499 VNET_HW_INTERFACE_FLAG_LINK_UP);
1500 }
1501 else
1502 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001503
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001504 virtio_pci_offloads (vm, vif, args->gso_enabled,
1505 args->checksum_offload_enabled);
1506
1507 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1508 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001509 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001510 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1511 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001512 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001513 return;
1514
1515error:
1516 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001517 if (args->rv == 0)
1518 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001519 args->error = error;
1520}
1521
1522int
1523virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1524{
1525 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001526 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001527 u32 i = 0;
1528
1529 if (vif->type != VIRTIO_IF_TYPE_PCI)
1530 return VNET_API_ERROR_INVALID_INTERFACE;
1531
1532 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1533
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001534 for (i = 0; i < vif->max_queue_pairs; i++)
1535 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001536 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1537 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001538 }
1539
1540 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001541 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001542
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001543 if (vif->virtio_pci_func)
1544 vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001545
1546 if (vif->hw_if_index)
1547 {
1548 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001549 ethernet_delete_interface (vnm, vif->hw_if_index);
1550 }
1551
1552 vlib_pci_device_close (vm, vif->pci_dev_handle);
1553
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001554 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001555 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001556 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001557 if (vring->used)
1558 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001559 virtio_free_buffers (vm, vring);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001560 }
1561 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001562 vlib_physmem_free (vm, vring->desc);
1563 }
1564
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +00001565 virtio_pre_input_node_disable (vm, vif);
1566
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001567 vec_foreach_index (i, vif->txq_vrings)
1568 {
1569 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001570 if (vring->used)
1571 {
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001572 virtio_free_buffers (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001573 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001574 vec_free (vring->buffers);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +01001575 gro_flow_table_free (vring->flow_table);
1576 virtio_vring_buffering_free (vm, vring->buffering);
Steven Luonge5584962019-06-25 13:47:06 -07001577 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001578 vlib_physmem_free (vm, vring->desc);
1579 }
1580
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001581 if (vif->cxq_vring != NULL)
1582 {
1583 u16 last = vif->cxq_vring->last_used_idx;
1584 u16 n_left = vif->cxq_vring->used->idx - last;
1585 while (n_left)
1586 {
1587 last++;
1588 n_left--;
1589 }
1590
1591 vif->cxq_vring->last_used_idx = last;
1592 vlib_physmem_free (vm, vif->cxq_vring->desc);
1593 }
1594
1595 vec_free (vif->rxq_vrings);
1596 vec_free (vif->txq_vrings);
1597 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001598
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001599 clib_error_free (vif->error);
1600 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001601 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001602
1603 return 0;
1604}
1605
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001606int
1607virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1608 int gso_enabled,
1609 int checksum_offload_enabled,
1610 int offloads_disabled)
1611{
1612 if (vif->type != VIRTIO_IF_TYPE_PCI)
1613 return VNET_API_ERROR_INVALID_INTERFACE;
1614
1615 if (gso_enabled)
1616 virtio_pci_offloads (vm, vif, 1, 0);
1617 else if (checksum_offload_enabled)
1618 virtio_pci_offloads (vm, vif, 0, 1);
1619 else if (offloads_disabled)
1620 virtio_pci_offloads (vm, vif, 0, 0);
1621
1622 return 0;
1623}
1624
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001625/*
1626 * fd.io coding-style-patch-verification: ON
1627 *
1628 * Local Variables:
1629 * eval: (c-set-style "gnu")
1630 * End:
1631 */