blob: 9b68b1cefbed234c5dd4c479aefba941245f4bfb [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 Kazmi05493782019-05-01 14:26:17 +0200198virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200199 virtio_ctrl_msg_t * data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000200{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000201 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200202 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
203 virtio_ctrl_msg_t result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000204 u32 buffer_index;
205 vlib_buffer_t *b;
206 u16 used, next, avail;
207 u16 sz = vring->size;
208 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000209
210 used = vring->desc_in_use;
211 next = vring->desc_next;
212 avail = vring->avail->idx;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200213 vring_desc_t *d = &vring->desc[next];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000214
215 if (vlib_buffer_alloc (vm, &buffer_index, 1))
216 b = vlib_get_buffer (vm, buffer_index);
217 else
218 return VIRTIO_NET_ERR;
219 /*
220 * current_data may not be initialized with 0 and may contain
221 * previous offset.
222 */
223 b->current_data = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200224 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000225 d->flags = VRING_DESC_F_NEXT;
226 d->addr = vlib_buffer_get_current_pa (vm, b);
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200227 d->len = sizeof (virtio_net_ctrl_hdr_t);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000228 vring->avail->ring[avail & mask] = next;
229 avail++;
230 next = (next + 1) & mask;
231 d->next = next;
232 used++;
233
234 d = &vring->desc[next];
235 d->flags = VRING_DESC_F_NEXT;
236 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200237 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200238 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000239 next = (next + 1) & mask;
240 d->next = next;
241 used++;
242
243 d = &vring->desc[next];
244 d->flags = VRING_DESC_F_WRITE;
245 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200246 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200247 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000248 next = (next + 1) & mask;
249 used++;
250
251 CLIB_MEMORY_STORE_BARRIER ();
252 vring->avail->idx = avail;
253 vring->desc_next = next;
254 vring->desc_in_use = used;
255
256 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
257 {
258 virtio_kick (vm, vring, vif);
259 }
260
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000261 u16 last = vring->last_used_idx, n_left = 0;
262 n_left = vring->used->idx - last;
263
264 while (n_left)
265 {
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200266 vring_used_elem_t *e = &vring->used->ring[last & mask];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000267 u16 slot = e->id;
268
269 d = &vring->desc[slot];
270 while (d->flags & VRING_DESC_F_NEXT)
271 {
272 used--;
273 slot = d->next;
274 d = &vring->desc[slot];
275 }
276 used--;
277 last++;
278 n_left--;
279 }
280 vring->desc_in_use = used;
281 vring->last_used_idx = last;
282
283 CLIB_MEMORY_BARRIER ();
284 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200285 sizeof (virtio_ctrl_msg_t));
Damjan Marionf41244f2019-11-08 17:41:06 +0100286 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000287 status = result.status;
288 vlib_buffer_free (vm, &buffer_index, 1);
289 return status;
290}
291
Mohsin Kazmi05493782019-05-01 14:26:17 +0200292static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000293virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
294{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200295 virtio_ctrl_msg_t offload_hdr;
296 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000297
298 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
299 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
300 offload_hdr.status = VIRTIO_NET_ERR;
301 u64 offloads = 0ULL;
302 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
303
304 status =
305 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
306 virtio_log_debug (vif, "disable offloads");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000307 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
308 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000309 return status;
310}
311
312static int
313virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
314{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200315 virtio_ctrl_msg_t csum_offload_hdr;
316 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000317
318 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
319 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
320 csum_offload_hdr.status = VIRTIO_NET_ERR;
321 u64 offloads = 0ULL;
322 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
323 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
324
325 status =
326 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
327 virtio_log_debug (vif, "enable checksum offload");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000328 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
329 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000330 return status;
331}
332
333static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200334virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
335{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200336 virtio_ctrl_msg_t gso_hdr;
337 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200338
339 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
340 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
341 gso_hdr.status = VIRTIO_NET_ERR;
342 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
343 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000344 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200345 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
346
347 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100348 virtio_log_debug (vif, "enable gso");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000349 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
350 vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200351 return status;
352}
353
354static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000355virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
356 int csum_offload_enabled)
357{
358 vnet_main_t *vnm = vnet_get_main ();
359 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
360
361 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
362 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
363 {
364 if (gso_enabled
365 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
366 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
367 {
368 if (virtio_pci_enable_gso (vm, vif))
369 {
370 virtio_log_warning (vif, "gso is not enabled");
371 }
372 else
373 {
374 vif->gso_enabled = 1;
375 vif->csum_offload_enabled = 0;
376 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
377 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
378 }
379 }
380 else if (csum_offload_enabled
381 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
382 {
383 if (virtio_pci_enable_checksum_offload (vm, vif))
384 {
385 virtio_log_warning (vif, "checksum offload is not enabled");
386 }
387 else
388 {
389 vif->csum_offload_enabled = 1;
390 vif->gso_enabled = 0;
391 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
392 hw->flags |=
393 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
394 }
395 }
396 else
397 {
398 if (virtio_pci_disable_offload (vm, vif))
399 {
400 virtio_log_warning (vif, "offloads are not disabled");
401 }
402 else
403 {
404 vif->csum_offload_enabled = 0;
405 vif->gso_enabled = 0;
406 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
407 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
408 }
409 }
410 }
411
412 return 0;
413}
414
415static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200416virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
417 u16 num_queues)
418{
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200419 virtio_ctrl_msg_t mq_hdr;
420 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200421
422 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
423 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
424 mq_hdr.status = VIRTIO_NET_ERR;
425 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
426
427 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100428 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200429 return status;
430}
431
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200432static u8
433virtio_pci_queue_size_valid (u16 qsz)
434{
435 if (qsz < 64 || qsz > 4096)
436 return 0;
437 if ((qsz % 64) != 0)
438 return 0;
439 return 1;
440}
441
442clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000443virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
444 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200445{
446 clib_error_t *error = 0;
447 u16 queue_size = 0;
448 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200449 vring_t vr;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200450 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000451 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200452
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000453 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200454 if (!virtio_pci_queue_size_valid (queue_size))
455 clib_warning ("queue size is not valid");
456
457 if (!is_pow2 (queue_size))
458 return clib_error_return (0, "ring size must be power of 2");
459
460 if (queue_size > 32768)
461 return clib_error_return (0, "ring size must be 32768 or lower");
462
463 if (queue_size == 0)
464 queue_size = 256;
465
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000466 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
467 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200468 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
469 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000470 ptr =
471 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
472 vif->numa_node);
473 if (!ptr)
474 return vlib_physmem_last_error (vm);
475 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200476 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
477 vring->desc = vr.desc;
478 vring->avail = vr.avail;
479 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000480 vring->queue_id = queue_num;
481 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
482
483 ASSERT (vring->buffers == 0);
484
485 vring->size = queue_size;
Damjan Marionf41244f2019-11-08 17:41:06 +0100486 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000487 queue_size);
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000488 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000489 vring->queue_notify_offset =
490 vif->notify_off_multiplier *
491 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
492 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
493 queue_num, vring->queue_notify_offset);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000494
495 return error;
496}
497
498clib_error_t *
499virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
500{
Benoît Ganneb6b484d2020-09-15 10:58:07 +0200501 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000502 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000503 u16 queue_size = 0;
504 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200505 vring_t vr;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000506 u32 i = 0;
507 void *ptr = NULL;
508
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000509 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000510 if (!virtio_pci_queue_size_valid (queue_size))
511 clib_warning ("queue size is not valid");
512
513 if (!is_pow2 (queue_size))
514 return clib_error_return (0, "ring size must be power of 2");
515
516 if (queue_size > 32768)
517 return clib_error_return (0, "ring size must be 32768 or lower");
518
519 if (queue_size == 0)
520 queue_size = 256;
521
522 if (queue_num % 2)
523 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000524 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
525 CLIB_CACHE_LINE_BYTES);
526 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
Benoît Ganneb6b484d2020-09-15 10:58:07 +0200527 if (vif->max_queue_pairs < vtm->n_vlib_mains)
528 clib_spinlock_init (&vring->lockp);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000529 }
530 else
531 {
532 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
533 CLIB_CACHE_LINE_BYTES);
534 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
535 }
536 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
537 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
538 ptr =
539 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
540 vif->numa_node);
541 if (!ptr)
542 return vlib_physmem_last_error (vm);
543 clib_memset (ptr, 0, i);
544 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
545 vring->desc = vr.desc;
546 vring->avail = vr.avail;
547 vring->used = vr.used;
548 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200549 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200550 vring->flow_table = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200551
552 ASSERT (vring->buffers == 0);
553 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000554 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200555 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100556 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000557 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100558 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200559 }
560 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000561 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100562 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000563 queue_size);
564 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200565 vring->size = queue_size;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000566 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200567 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200568
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000569 vring->queue_notify_offset =
570 vif->notify_off_multiplier *
571 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
572 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
573 queue_num, vring->queue_notify_offset);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200574 return error;
575}
576
577static void
578virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
579 u64 req_features)
580{
581 /*
582 * if features are not requested
583 * default: all supported features
584 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200585 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
586 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
587 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
588 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200589 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200590 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
591 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
592 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
593 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
594 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
595 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
596 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200597 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
598 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000599 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
600 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000601 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200602 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
603 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
604
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200605 if (vif->is_modern)
606 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
607
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200608 if (req_features == 0)
609 {
610 req_features = supported_features;
611 }
612
613 vif->features = req_features & vif->remote_features & supported_features;
614
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000615 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200616 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000617 u16 mtu = 0;
618 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
619
620 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200621 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
622 }
623
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000624 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
625 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200626}
627
628void
629virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
630{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000631 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200632}
633
634int
635virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
636{
637 u8 status = 0;
638
639 /*
640 * Reset the device
641 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000642 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200643
644 /*
645 * Set the Acknowledge status bit
646 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000647 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200648
649 /*
650 * Set the Driver status bit
651 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000652 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200653
654 /*
655 * Read the status and verify it
656 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000657 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200658 if ((status & VIRTIO_CONFIG_STATUS_ACK)
659 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
660 vif->status = status;
661 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200662 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200663
664 return 0;
665}
666
667clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200668virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200669{
670 clib_error_t *error = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200671 virtio_pci_cap_t cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200672 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200673 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
674
675 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000676 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100677 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200678 return clib_error_return (error,
679 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000680 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200681 while (pos)
682 {
683 if ((error =
684 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
685 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000686 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100687 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000688 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200689 return clib_error_return (error,
690 "error in reading the capability at [%2x]",
691 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000692 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200693
694 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
695 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000696 u16 flags, table_size, table_size_mask = 0x07FF;
697
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200698 if ((error =
699 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
700 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200701 return clib_error_return (error,
702 "error in reading the capability at [%2x]",
703 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200704
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000705 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +0100706 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000707 "msix interrupt vector table-size", table_size);
708
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200709 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000710 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100711 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000712 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000713 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000714 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200715 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000716 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100717 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000718 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000719 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000720 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200721 }
722
723 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
724 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100725 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000726 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200727 goto next;
728 }
729
Damjan Marionf41244f2019-11-08 17:41:06 +0100730 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000731 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
732 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200733
Mohsin Kazmi0a507d72020-09-01 18:18:48 +0200734 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +0200735 {
736 vif->bar = bar[cap.bar];
737 vif->bar_id = cap.bar;
738 }
739 else
740 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200741
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200742 switch (cap.cfg_type)
743 {
744 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200745 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200746 common_cfg = 1;
747 break;
748 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200749 if ((error =
750 vlib_pci_read_write_config (vm, h, VLIB_READ,
751 pos + sizeof (cap),
752 &vif->notify_off_multiplier,
753 sizeof
754 (vif->notify_off_multiplier))))
755 {
756 virtio_log_error (vif, "notify off multiplier is not given");
757 }
758 else
759 {
760 virtio_log_debug (vif, "notify off multiplier is %u",
761 vif->notify_off_multiplier);
762 vif->notify_offset = cap.offset;
763 notify = 1;
764 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200765 break;
766 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200767 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200768 dev_cfg = 1;
769 break;
770 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200771 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200772 isr = 1;
773 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000774 case VIRTIO_PCI_CAP_PCI_CFG:
775 if (cap.bar == 0)
776 pci_cfg = 1;
777 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200778 }
779 next:
780 pos = cap.cap_next;
781 }
782
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200783 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200784 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200785 vif->virtio_pci_func = &virtio_pci_legacy_func;
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000786 vif->notify_off_multiplier = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +0100787 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200788 return error;
789 }
790
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200791 vif->is_modern = 1;
792 vif->virtio_pci_func = &virtio_pci_modern_func;
793
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000794 if (!pci_cfg)
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000795 {
796 virtio_log_debug (vif, "modern virtio pci device found");
797 }
798 else
799 {
800 virtio_log_debug (vif, "transitional virtio pci device found");
801 }
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000802
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000803 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200804}
805
806static clib_error_t *
807virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200808 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200809{
810 clib_error_t *error = 0;
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +0100811 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200812 u8 status = 0;
813
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200814 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000815 {
816 args->rv = VNET_API_ERROR_UNSUPPORTED;
817 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200818 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000819 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200820
821 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000822 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000823 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +0100824 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200825 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000826 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200827 /*
828 * read device features and negotiate (user) requested features
829 */
830 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +0000831 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
832 0)
833 {
834 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
835 "support VIRTIO_RING_F_INDIRECT_DESC features");
836 }
837 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
838 {
839 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
840 "support VIRTIO_NET_F_MRG_RXBUF features");
841 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200842 virtio_negotiate_features (vm, vif, args->features);
843
844 /*
845 * After FEATURE_OK, driver should not accept new feature bits
846 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000847 vif->virtio_pci_func->set_status (vm, vif,
848 VIRTIO_CONFIG_STATUS_FEATURES_OK);
849 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200850 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000851 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000852 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +0100853 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000854 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200855 return clib_error_return (error,
856 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000857 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200858 vif->status = status;
859
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000860 /*
861 * get or set the mac address
862 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200863 if (virtio_pci_get_mac (vm, vif))
864 {
865 f64 now = vlib_time_now (vm);
866 u32 rnd;
867 rnd = (u32) (now * 1e6);
868 rnd = random_u32 (&rnd);
869
870 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
871 vif->mac_addr[0] = 2;
872 vif->mac_addr[1] = 0xfe;
873 virtio_pci_set_mac (vm, vif);
874 }
875
876 virtio_set_net_hdr_size (vif);
877
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000878 /*
879 * Initialize the virtqueues
880 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200881 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000882 {
883 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
884 goto err;
885 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200886
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000887 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
888 {
889 if (vif->msix_table_size <= vif->max_queue_pairs)
890 {
891 virtio_log_error (vif,
892 "error MSIX lines (%u) <= Number of RXQs (%u)",
893 vif->msix_table_size, vif->max_queue_pairs);
894 return clib_error_return (error,
895 "error MSIX lines (%u) <= Number of RXQs (%u)",
896 vif->msix_table_size,
897 vif->max_queue_pairs);
898 }
899 }
900
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000901 for (int i = 0; i < vif->max_queue_pairs; i++)
902 {
903 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200904 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000905 args->rv = VNET_API_ERROR_INIT_FAILED;
906 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
907 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000908 error =
909 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
910 RX_QUEUE (i), "initialization");
911 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200912 }
913 else
914 {
915 vif->num_rxqs++;
916 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200917
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +0100918 if (i >= vtm->n_vlib_mains)
919 {
920 /*
921 * There is 1:1 mapping between tx queue and vpp worker thread.
922 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
923 * index 1 and so on.
924 * Multiple worker threads can poll same tx queue when number of
925 * workers are more than tx queues. In this case, 1:N mapping
926 * between tx queue and vpp worker thread.
927 */
928 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
929 TX_QUEUE (i),
930 "no VPP worker thread is available");
931 continue;
932 }
933
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000934 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200935 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000936 args->rv = VNET_API_ERROR_INIT_FAILED;
937 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
938 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000939 error =
940 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
941 TX_QUEUE (i), "initialization");
942 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200943 }
944 else
945 {
946 vif->num_txqs++;
947 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000948 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200949
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000950 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
951 {
952 if ((error =
953 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
954 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100955 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000956 vif->max_queue_pairs * 2, "initialization");
957 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
958 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
959 }
960 }
961 else
962 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100963 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000964 vif->cxq_vring = NULL;
965 }
966
967 /*
968 * set the msix interrupts
969 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000970 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200971 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000972 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000973 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000974 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000975 {
976 virtio_log_warning (vif, "config vector 0 is not set");
977 }
978 else
979 {
980 virtio_log_debug (vif, "config msix vector is set at 0");
981 }
982 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
983 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000984 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
985 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000986 VIRTIO_MSI_NO_VECTOR)
987 {
988 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
989 RX_QUEUE (i), j);
990 }
991 else
992 {
993 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
994 RX_QUEUE (i), "msix vector is set at", j);
995 }
996 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200997 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000998
999 /*
1000 * set the driver status OK
1001 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001002 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1003 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001004err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001005 return error;
1006}
1007
1008void
1009virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1010{
1011 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001012 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001013 virtio_if_t *vif;
1014 vlib_pci_dev_handle_t h;
1015 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001016 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001017
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001018 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001019 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001020 if (vif->pci_addr.as_u32 == args->addr)
1021 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001022 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001023 args->error =
1024 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001025 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1026 format_vlib_pci_addr, &args->addr,
1027 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001028 return;
1029 }
1030 }));
1031 /* *INDENT-ON* */
1032
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001033 pool_get (vim->interfaces, vif);
1034 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001035 vif->per_interface_next_index = ~0;
1036 vif->pci_addr.as_u32 = args->addr;
1037
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001038 if ((error =
1039 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1040 virtio_pci_device_ids, &h)))
1041 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001042 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1043 args->error =
1044 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1045 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001046 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1047 format_vlib_pci_addr, &vif->pci_addr,
1048 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001049 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001050 return;
1051 }
1052 vif->pci_dev_handle = h;
1053 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001054 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001055 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001056
1057 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001058 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001059 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001060 goto error;
1061 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001062
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001063 void *bar[6];
1064 for (u32 i = 0; i <= 5; i++)
1065 {
1066
1067 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1068 {
1069 virtio_log_debug (vif, "no pci map region for bar %u", i);
1070 }
1071 else
1072 {
1073 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1074 bar[i]);
1075 }
1076 }
1077
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001078 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001079 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001080 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001081 goto error;
1082 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001083
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001084 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1085 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001086 {
1087 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001088 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001089 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001090 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001091 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001092 "error encountered on pci register msix handler 0");
1093 goto error;
1094 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001095
1096 if ((error =
1097 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1098 &virtio_pci_irq_queue_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001099 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001100 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001101 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001102 "error encountered on pci register msix handler 1");
1103 goto error;
1104 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001105
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001106 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001107 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001108 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001109 goto error;
1110 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001111 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001112 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001113 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001114 else if (interrupt_count == 1)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001115 {
1116 /*
1117 * if msix table-size is 1, fall back to intX.
1118 */
1119 if ((error =
1120 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1121 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001122 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001123 "error encountered on pci register interrupt handler");
1124 goto error;
1125 }
1126 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001127 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001128 }
1129 else
1130 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001131 /*
1132 * WARN: intX is showing some weird behaviour.
1133 * Please don't use interrupt mode with UIO driver.
1134 */
1135 vif->support_int_mode = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001136 virtio_log_debug (vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001137 }
1138
1139 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001140 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001141 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001142 goto error;
1143 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001144
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001145 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001146 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001147 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001148 goto error;
1149 }
1150
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001151 /* create interface */
1152 error = ethernet_register_interface (vnm, virtio_device_class.index,
1153 vif->dev_instance, vif->mac_addr,
1154 &vif->hw_if_index,
1155 virtio_pci_flag_change);
1156
1157 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001158 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001159 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001160 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001161 "error encountered on ethernet register interface");
1162 goto error;
1163 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001164
1165 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1166 vif->sw_if_index = sw->sw_if_index;
1167 args->sw_if_index = sw->sw_if_index;
1168
1169 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1170 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Mohsin Kazmie347acb2020-09-28 10:26:33 +00001171
1172 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1173 {
1174 error = virtio_set_packet_buffering (vif, args->buffering_size);
1175 if (error)
1176 {
1177 args->rv = VNET_API_ERROR_INIT_FAILED;
1178 virtio_log_error (vif,
1179 "error encountered during packet buffering init");
1180 goto error;
1181 }
1182 }
1183
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001184 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1185 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001186 u32 i = 0;
1187 vec_foreach_index (i, vif->rxq_vrings)
1188 {
1189 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1190 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1191 /* Set default rx mode to POLLING */
1192 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1193 VNET_HW_INTERFACE_RX_MODE_POLLING);
1194 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001195 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1196 {
1197 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1198 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1199 VNET_HW_INTERFACE_FLAG_LINK_UP);
1200 }
1201 else
1202 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001203
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001204 virtio_pci_offloads (vm, vif, args->gso_enabled,
1205 args->checksum_offload_enabled);
1206
1207 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1208 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001209 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001210 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1211 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001212 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001213 return;
1214
1215error:
1216 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001217 if (args->rv == 0)
1218 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001219 args->error = error;
1220}
1221
1222int
1223virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1224{
1225 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001226 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001227 u32 i = 0;
1228
1229 if (vif->type != VIRTIO_IF_TYPE_PCI)
1230 return VNET_API_ERROR_INVALID_INTERFACE;
1231
1232 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1233
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001234 for (i = 0; i < vif->max_queue_pairs; i++)
1235 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001236 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1237 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001238 }
1239
1240 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001241 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001242
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001243 if (vif->virtio_pci_func)
1244 vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001245
1246 if (vif->hw_if_index)
1247 {
1248 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001249 vec_foreach_index (i, vif->rxq_vrings)
1250 {
1251 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1252 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001253 ethernet_delete_interface (vnm, vif->hw_if_index);
1254 }
1255
1256 vlib_pci_device_close (vm, vif->pci_dev_handle);
1257
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001258 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001259 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001260 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001261 if (vring->used)
1262 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001263 virtio_free_rx_buffers (vm, vring);
1264 }
1265 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001266 vlib_physmem_free (vm, vring->desc);
1267 }
1268
1269 vec_foreach_index (i, vif->txq_vrings)
1270 {
1271 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001272 if (vring->used)
1273 {
1274 virtio_free_used_desc (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001275 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001276 vec_free (vring->buffers);
Steven Luonge5584962019-06-25 13:47:06 -07001277 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001278 vlib_physmem_free (vm, vring->desc);
1279 }
1280
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001281 if (vif->cxq_vring != NULL)
1282 {
1283 u16 last = vif->cxq_vring->last_used_idx;
1284 u16 n_left = vif->cxq_vring->used->idx - last;
1285 while (n_left)
1286 {
1287 last++;
1288 n_left--;
1289 }
1290
1291 vif->cxq_vring->last_used_idx = last;
1292 vlib_physmem_free (vm, vif->cxq_vring->desc);
1293 }
1294
1295 vec_free (vif->rxq_vrings);
1296 vec_free (vif->txq_vrings);
1297 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001298
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001299 clib_error_free (vif->error);
1300 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001301 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001302
1303 return 0;
1304}
1305
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001306int
1307virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1308 int gso_enabled,
1309 int checksum_offload_enabled,
1310 int offloads_disabled)
1311{
1312 if (vif->type != VIRTIO_IF_TYPE_PCI)
1313 return VNET_API_ERROR_INVALID_INTERFACE;
1314
1315 if (gso_enabled)
1316 virtio_pci_offloads (vm, vif, 1, 0);
1317 else if (checksum_offload_enabled)
1318 virtio_pci_offloads (vm, vif, 0, 1);
1319 else if (offloads_disabled)
1320 virtio_pci_offloads (vm, vif, 0, 0);
1321
1322 return 0;
1323}
1324
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001325/*
1326 * fd.io coding-style-patch-verification: ON
1327 *
1328 * Local Variables:
1329 * eval: (c-set-style "gnu")
1330 * End:
1331 */