blob: 5ba9f36de81d92132f7334719fd7c2faeb04a744 [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 Kazmi09a3bc52019-04-02 11:45:08 +0000489 vring->kick_fd = -1;
490
491 return error;
492}
493
494clib_error_t *
495virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
496{
497 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000498 u16 queue_size = 0;
499 virtio_vring_t *vring;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200500 vring_t vr;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000501 u32 i = 0;
502 void *ptr = NULL;
503
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000504 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000505 if (!virtio_pci_queue_size_valid (queue_size))
506 clib_warning ("queue size is not valid");
507
508 if (!is_pow2 (queue_size))
509 return clib_error_return (0, "ring size must be power of 2");
510
511 if (queue_size > 32768)
512 return clib_error_return (0, "ring size must be 32768 or lower");
513
514 if (queue_size == 0)
515 queue_size = 256;
516
517 if (queue_num % 2)
518 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000519 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
520 CLIB_CACHE_LINE_BYTES);
521 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
522 clib_spinlock_init (&vring->lockp);
523 }
524 else
525 {
526 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
527 CLIB_CACHE_LINE_BYTES);
528 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
529 }
530 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
531 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
532 ptr =
533 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
534 vif->numa_node);
535 if (!ptr)
536 return vlib_physmem_last_error (vm);
537 clib_memset (ptr, 0, i);
538 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
539 vring->desc = vr.desc;
540 vring->avail = vr.avail;
541 vring->used = vr.used;
542 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200543 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200544 vring->flow_table = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200545
546 ASSERT (vring->buffers == 0);
547 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000548 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200549 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100550 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000551 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100552 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200553 }
554 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000555 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100556 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000557 queue_size);
558 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200559 vring->size = queue_size;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000560 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200561 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200562
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200563 vring->kick_fd = -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200564 return error;
565}
566
567static void
568virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
569 u64 req_features)
570{
571 /*
572 * if features are not requested
573 * default: all supported features
574 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200575 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
576 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
577 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
578 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200579 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200580 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
581 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
582 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
583 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
584 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
585 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
586 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200587 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
588 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000589 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
590 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000591 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200592 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
593 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
594
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200595 if (vif->is_modern)
596 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
597
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200598 if (req_features == 0)
599 {
600 req_features = supported_features;
601 }
602
603 vif->features = req_features & vif->remote_features & supported_features;
604
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000605 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200606 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000607 u16 mtu = 0;
608 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
609
610 if (mtu < 64)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200611 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
612 }
613
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000614 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
615 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200616}
617
618void
619virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
620{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000621 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200622}
623
624int
625virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
626{
627 u8 status = 0;
628
629 /*
630 * Reset the device
631 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000632 status = vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200633
634 /*
635 * Set the Acknowledge status bit
636 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000637 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200638
639 /*
640 * Set the Driver status bit
641 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000642 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200643
644 /*
645 * Read the status and verify it
646 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000647 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200648 if ((status & VIRTIO_CONFIG_STATUS_ACK)
649 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
650 vif->status = status;
651 else
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200652 return -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200653
654 return 0;
655}
656
657clib_error_t *
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200658virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200659{
660 clib_error_t *error = 0;
Mohsin Kazmia7a22812020-08-31 17:17:16 +0200661 virtio_pci_cap_t cap;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200662 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200663 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
664
665 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000666 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100667 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200668 return clib_error_return (error,
669 "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000670 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200671 while (pos)
672 {
673 if ((error =
674 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
675 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000676 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100677 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000678 "error in reading the capability at", pos);
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200679 return clib_error_return (error,
680 "error in reading the capability at [%2x]",
681 pos);
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000682 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200683
684 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
685 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000686 u16 flags, table_size, table_size_mask = 0x07FF;
687
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200688 if ((error =
689 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
690 sizeof (flags))))
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200691 return clib_error_return (error,
692 "error in reading the capability at [%2x]",
693 pos + 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200694
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000695 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +0100696 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000697 "msix interrupt vector table-size", table_size);
698
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200699 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000700 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100701 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000702 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000703 vif->msix_table_size = table_size;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000704 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200705 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000706 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100707 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000708 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000709 vif->msix_table_size = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000710 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200711 }
712
713 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
714 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100715 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000716 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200717 goto next;
718 }
719
Damjan Marionf41244f2019-11-08 17:41:06 +0100720 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000721 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
722 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200723
Mohsin Kazmi0a507d72020-09-01 18:18:48 +0200724 if (cap.bar >= 0 && cap.bar <= 5)
Mohsin Kazmi133c91c12020-08-20 17:18:56 +0200725 {
726 vif->bar = bar[cap.bar];
727 vif->bar_id = cap.bar;
728 }
729 else
730 return clib_error_return (error, "invalid bar %u", cap.bar);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200731
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200732 switch (cap.cfg_type)
733 {
734 case VIRTIO_PCI_CAP_COMMON_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200735 vif->common_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200736 common_cfg = 1;
737 break;
738 case VIRTIO_PCI_CAP_NOTIFY_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200739 if ((error =
740 vlib_pci_read_write_config (vm, h, VLIB_READ,
741 pos + sizeof (cap),
742 &vif->notify_off_multiplier,
743 sizeof
744 (vif->notify_off_multiplier))))
745 {
746 virtio_log_error (vif, "notify off multiplier is not given");
747 }
748 else
749 {
750 virtio_log_debug (vif, "notify off multiplier is %u",
751 vif->notify_off_multiplier);
752 vif->notify_offset = cap.offset;
753 notify = 1;
754 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200755 break;
756 case VIRTIO_PCI_CAP_DEVICE_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200757 vif->device_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200758 dev_cfg = 1;
759 break;
760 case VIRTIO_PCI_CAP_ISR_CFG:
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200761 vif->isr_offset = cap.offset;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200762 isr = 1;
763 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000764 case VIRTIO_PCI_CAP_PCI_CFG:
765 if (cap.bar == 0)
766 pci_cfg = 1;
767 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200768 }
769 next:
770 pos = cap.cap_next;
771 }
772
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200773 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200774 {
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200775 vif->virtio_pci_func = &virtio_pci_legacy_func;
Damjan Marionf41244f2019-11-08 17:41:06 +0100776 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200777 return error;
778 }
779
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200780 vif->is_modern = 1;
781 vif->virtio_pci_func = &virtio_pci_modern_func;
782
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000783 if (!pci_cfg)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200784 virtio_log_debug (vif, "modern virtio pci device found");
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000785
Damjan Marionf41244f2019-11-08 17:41:06 +0100786 virtio_log_debug (vif, "transitional virtio pci device found");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000787 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200788}
789
790static clib_error_t *
791virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200792 virtio_pci_create_if_args_t * args, void **bar)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200793{
794 clib_error_t *error = 0;
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +0100795 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200796 u8 status = 0;
797
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200798 if ((error = virtio_pci_read_caps (vm, vif, bar)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000799 {
800 args->rv = VNET_API_ERROR_UNSUPPORTED;
801 virtio_log_error (vif, "Device is not supported");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200802 return clib_error_return (error, "Device is not supported");
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000803 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200804
805 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000806 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000807 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +0100808 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200809 return clib_error_return (error, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000810 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200811 /*
812 * read device features and negotiate (user) requested features
813 */
814 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +0000815 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
816 0)
817 {
818 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
819 "support VIRTIO_RING_F_INDIRECT_DESC features");
820 }
821 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
822 {
823 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
824 "support VIRTIO_NET_F_MRG_RXBUF features");
825 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200826 virtio_negotiate_features (vm, vif, args->features);
827
828 /*
829 * After FEATURE_OK, driver should not accept new feature bits
830 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000831 vif->virtio_pci_func->set_status (vm, vif,
832 VIRTIO_CONFIG_STATUS_FEATURES_OK);
833 status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200834 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000835 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000836 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +0100837 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000838 "error encountered: Device doesn't support requested features");
Mohsin Kazmic96e64b2020-09-01 17:12:50 +0200839 return clib_error_return (error,
840 "Device doesn't support requested features");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000841 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200842 vif->status = status;
843
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000844 /*
845 * get or set the mac address
846 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200847 if (virtio_pci_get_mac (vm, vif))
848 {
849 f64 now = vlib_time_now (vm);
850 u32 rnd;
851 rnd = (u32) (now * 1e6);
852 rnd = random_u32 (&rnd);
853
854 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
855 vif->mac_addr[0] = 2;
856 vif->mac_addr[1] = 0xfe;
857 virtio_pci_set_mac (vm, vif);
858 }
859
860 virtio_set_net_hdr_size (vif);
861
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000862 /*
863 * Initialize the virtqueues
864 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200865 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000866 {
867 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
868 goto err;
869 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200870
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000871 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
872 {
873 if (vif->msix_table_size <= vif->max_queue_pairs)
874 {
875 virtio_log_error (vif,
876 "error MSIX lines (%u) <= Number of RXQs (%u)",
877 vif->msix_table_size, vif->max_queue_pairs);
878 return clib_error_return (error,
879 "error MSIX lines (%u) <= Number of RXQs (%u)",
880 vif->msix_table_size,
881 vif->max_queue_pairs);
882 }
883 }
884
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000885 for (int i = 0; i < vif->max_queue_pairs; i++)
886 {
887 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200888 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000889 args->rv = VNET_API_ERROR_INIT_FAILED;
890 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
891 RX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000892 error =
893 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
894 RX_QUEUE (i), "initialization");
895 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200896 }
897 else
898 {
899 vif->num_rxqs++;
900 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200901
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +0100902 if (i >= vtm->n_vlib_mains)
903 {
904 /*
905 * There is 1:1 mapping between tx queue and vpp worker thread.
906 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
907 * index 1 and so on.
908 * Multiple worker threads can poll same tx queue when number of
909 * workers are more than tx queues. In this case, 1:N mapping
910 * between tx queue and vpp worker thread.
911 */
912 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
913 TX_QUEUE (i),
914 "no VPP worker thread is available");
915 continue;
916 }
917
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000918 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200919 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +0000920 args->rv = VNET_API_ERROR_INIT_FAILED;
921 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
922 TX_QUEUE (i), "initialization");
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000923 error =
924 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
925 TX_QUEUE (i), "initialization");
926 goto err;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200927 }
928 else
929 {
930 vif->num_txqs++;
931 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000932 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200933
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000934 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
935 {
936 if ((error =
937 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
938 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100939 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000940 vif->max_queue_pairs * 2, "initialization");
941 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
942 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
943 }
944 }
945 else
946 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100947 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000948 vif->cxq_vring = NULL;
949 }
950
951 /*
952 * set the msix interrupts
953 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000954 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200955 {
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000956 int i, j;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000957 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000958 VIRTIO_MSI_NO_VECTOR)
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000959 {
960 virtio_log_warning (vif, "config vector 0 is not set");
961 }
962 else
963 {
964 virtio_log_debug (vif, "config msix vector is set at 0");
965 }
966 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
967 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000968 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
969 RX_QUEUE (i)) ==
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +0000970 VIRTIO_MSI_NO_VECTOR)
971 {
972 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
973 RX_QUEUE (i), j);
974 }
975 else
976 {
977 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
978 RX_QUEUE (i), "msix vector is set at", j);
979 }
980 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200981 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000982
983 /*
984 * set the driver status OK
985 */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000986 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
987 vif->status = vif->virtio_pci_func->get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200988err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200989 return error;
990}
991
992void
993virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
994{
995 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000996 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200997 virtio_if_t *vif;
998 vlib_pci_dev_handle_t h;
999 clib_error_t *error = 0;
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001000 u32 interrupt_count = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001001
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001002 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001003 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001004 if (vif->pci_addr.as_u32 == args->addr)
1005 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001006 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001007 args->error =
1008 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001009 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1010 format_vlib_pci_addr, &args->addr,
1011 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001012 return;
1013 }
1014 }));
1015 /* *INDENT-ON* */
1016
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001017 pool_get (vim->interfaces, vif);
1018 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001019 vif->per_interface_next_index = ~0;
1020 vif->pci_addr.as_u32 = args->addr;
1021
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001022 if ((error =
1023 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1024 virtio_pci_device_ids, &h)))
1025 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001026 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1027 args->error =
1028 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1029 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001030 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1031 format_vlib_pci_addr, &vif->pci_addr,
1032 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001033 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001034 return;
1035 }
1036 vif->pci_dev_handle = h;
1037 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001038 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001039 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001040
1041 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001042 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001043 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001044 goto error;
1045 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001046
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001047 void *bar[6];
1048 for (u32 i = 0; i <= 5; i++)
1049 {
1050
1051 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1052 {
1053 virtio_log_debug (vif, "no pci map region for bar %u", i);
1054 }
1055 else
1056 {
1057 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1058 bar[i]);
1059 }
1060 }
1061
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001062 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001063 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001064 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001065 goto error;
1066 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001067
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001068 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1069 if (interrupt_count > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001070 {
1071 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001072 &virtio_pci_irq_config_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001073 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001074 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001075 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001076 "error encountered on pci register msix handler 0");
1077 goto error;
1078 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001079
1080 if ((error =
1081 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1082 &virtio_pci_irq_queue_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001083 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001084 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001085 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001086 "error encountered on pci register msix handler 1");
1087 goto error;
1088 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001089
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001090 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001091 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001092 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001093 goto error;
1094 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001095 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001096 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001097 }
Mohsin Kazmi8046fdc2020-07-15 11:54:10 +00001098 else if (interrupt_count == 1)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001099 {
1100 /*
1101 * if msix table-size is 1, fall back to intX.
1102 */
1103 if ((error =
1104 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1105 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001106 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001107 "error encountered on pci register interrupt handler");
1108 goto error;
1109 }
1110 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001111 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001112 }
1113 else
1114 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001115 /*
1116 * WARN: intX is showing some weird behaviour.
1117 * Please don't use interrupt mode with UIO driver.
1118 */
1119 vif->support_int_mode = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001120 virtio_log_debug (vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001121 }
1122
1123 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001124 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001125 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001126 goto error;
1127 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001128
Mohsin Kazmi379aac32020-08-20 10:25:12 +02001129 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001130 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001131 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001132 goto error;
1133 }
1134
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001135 /* create interface */
1136 error = ethernet_register_interface (vnm, virtio_device_class.index,
1137 vif->dev_instance, vif->mac_addr,
1138 &vif->hw_if_index,
1139 virtio_pci_flag_change);
1140
1141 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001142 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001143 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001144 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001145 "error encountered on ethernet register interface");
1146 goto error;
1147 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001148
1149 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1150 vif->sw_if_index = sw->sw_if_index;
1151 args->sw_if_index = sw->sw_if_index;
1152
1153 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1154 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1155 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1156 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001157 u32 i = 0;
1158 vec_foreach_index (i, vif->rxq_vrings)
1159 {
1160 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1161 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1162 /* Set default rx mode to POLLING */
1163 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1164 VNET_HW_INTERFACE_RX_MODE_POLLING);
1165 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001166 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1167 {
1168 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1169 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1170 VNET_HW_INTERFACE_FLAG_LINK_UP);
1171 }
1172 else
1173 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001174
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001175 virtio_pci_offloads (vm, vif, args->gso_enabled,
1176 args->checksum_offload_enabled);
1177
1178 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1179 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001180 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001181 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1182 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001183 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001184 return;
1185
1186error:
1187 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001188 if (args->rv == 0)
1189 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001190 args->error = error;
1191}
1192
1193int
1194virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1195{
1196 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001197 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001198 u32 i = 0;
1199
1200 if (vif->type != VIRTIO_IF_TYPE_PCI)
1201 return VNET_API_ERROR_INVALID_INTERFACE;
1202
1203 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1204
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001205 for (i = 0; i < vif->max_queue_pairs; i++)
1206 {
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001207 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1208 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001209 }
1210
1211 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
Mohsin Kazmia0a68332020-07-16 12:55:42 +00001212 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001213
Mohsin Kazmic79d7352020-09-01 17:45:56 +02001214 if (vif->virtio_pci_func)
1215 vif->virtio_pci_func->device_reset (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001216
1217 if (vif->hw_if_index)
1218 {
1219 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001220 vec_foreach_index (i, vif->rxq_vrings)
1221 {
1222 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1223 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001224 ethernet_delete_interface (vnm, vif->hw_if_index);
1225 }
1226
1227 vlib_pci_device_close (vm, vif->pci_dev_handle);
1228
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001229 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001230 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001231 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001232 if (vring->kick_fd != -1)
1233 close (vring->kick_fd);
1234 if (vring->used)
1235 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001236 virtio_free_rx_buffers (vm, vring);
1237 }
1238 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001239 vlib_physmem_free (vm, vring->desc);
1240 }
1241
1242 vec_foreach_index (i, vif->txq_vrings)
1243 {
1244 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1245 if (vring->kick_fd != -1)
1246 close (vring->kick_fd);
1247 if (vring->used)
1248 {
1249 virtio_free_used_desc (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001250 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001251 vec_free (vring->buffers);
Steven Luonge5584962019-06-25 13:47:06 -07001252 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001253 vlib_physmem_free (vm, vring->desc);
1254 }
1255
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001256 if (vif->cxq_vring != NULL)
1257 {
1258 u16 last = vif->cxq_vring->last_used_idx;
1259 u16 n_left = vif->cxq_vring->used->idx - last;
1260 while (n_left)
1261 {
1262 last++;
1263 n_left--;
1264 }
1265
1266 vif->cxq_vring->last_used_idx = last;
1267 vlib_physmem_free (vm, vif->cxq_vring->desc);
1268 }
1269
1270 vec_free (vif->rxq_vrings);
1271 vec_free (vif->txq_vrings);
1272 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001273
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001274 clib_error_free (vif->error);
1275 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001276 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001277
1278 return 0;
1279}
1280
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001281int
1282virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1283 int gso_enabled,
1284 int checksum_offload_enabled,
1285 int offloads_disabled)
1286{
1287 if (vif->type != VIRTIO_IF_TYPE_PCI)
1288 return VNET_API_ERROR_INVALID_INTERFACE;
1289
1290 if (gso_enabled)
1291 virtio_pci_offloads (vm, vif, 1, 0);
1292 else if (checksum_offload_enabled)
1293 virtio_pci_offloads (vm, vif, 0, 1);
1294 else if (offloads_disabled)
1295 virtio_pci_offloads (vm, vif, 0, 0);
1296
1297 return 0;
1298}
1299
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001300/*
1301 * fd.io coding-style-patch-verification: ON
1302 *
1303 * Local Variables:
1304 * eval: (c-set-style "gnu")
1305 * End:
1306 */