blob: c956e7b75fae9c791d84cd0a39dff754d77f9dad [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 Kazmib74fe322019-01-31 13:50:56 +000039#define PCI_CONFIG_SIZE(vif) ((vif->msix_enabled == VIRTIO_MSIX_ENABLED) ? \
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020040 24 : 20)
41
42static pci_device_id_t virtio_pci_device_ids[] = {
43 {
44 .vendor_id = PCI_VENDOR_ID_VIRTIO,
45 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
46 {
47 .vendor_id = PCI_VENDOR_ID_VIRTIO,
48 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
49 {0},
50};
51
52static void
53virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst,
54 int len, u32 addr)
55{
56 u32 size = 0;
57 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
58
59 while (len > 0)
60 {
61 if (len >= 4)
62 {
63 size = 4;
Mohsin Kazmib74fe322019-01-31 13:50:56 +000064 vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020065 }
66 else if (len >= 2)
67 {
68 size = 2;
Mohsin Kazmib74fe322019-01-31 13:50:56 +000069 vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020070 }
71 else
72 {
73 size = 1;
Mohsin Kazmib74fe322019-01-31 13:50:56 +000074 vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020075 }
76 dst = (u8 *) dst + size;
77 addr += size;
78 len -= size;
79 }
80}
81
82static void
83virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif,
84 void *src, int len, u32 addr)
85{
86 u32 size = 0;
87 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
88
89 while (len > 0)
90 {
91 if (len >= 4)
92 {
93 size = 4;
Mohsin Kazmib74fe322019-01-31 13:50:56 +000094 vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020095 }
96 else if (len >= 2)
97 {
98 size = 2;
Mohsin Kazmib74fe322019-01-31 13:50:56 +000099 vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200100 }
101 else
102 {
103 size = 1;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000104 vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200105 }
106 src = (u8 *) src + size;
107 addr += size;
108 len -= size;
109 }
110}
111
112static u64
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000113virtio_pci_legacy_get_host_features (vlib_main_t * vm, virtio_if_t * vif)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200114{
115 u32 features;
116 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
117 &features);
118 return features;
119}
120
121static u32
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000122virtio_pci_legacy_get_guest_features (vlib_main_t * vm, virtio_if_t * vif)
123{
124 u32 feature = 0;
125 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
126 &feature);
127 vif->features = feature;
128 return feature;
129}
130
131static u32
132virtio_pci_legacy_set_guest_features (vlib_main_t * vm, virtio_if_t * vif,
133 u64 features)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200134{
135 if ((features >> 32) != 0)
136 {
137 clib_warning ("only 32 bit features are allowed for legacy virtio!");
138 }
139 u32 feature = 0, guest_features = (u32) features;
140 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
141 &guest_features);
142 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
143 &feature);
144 return feature;
145}
146
147static u8
148virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif)
149{
150 u8 status = 0;
151 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
152 return status;
153}
154
155static void
156virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status)
157{
158 if (status != VIRTIO_CONFIG_STATUS_RESET)
159 status |= virtio_pci_legacy_get_status (vm, vif);
160 vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
161}
162
163static u8
164virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif)
165{
166 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET);
167 return virtio_pci_legacy_get_status (vm, vif);
168}
169
170static u8
171virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif)
172{
173 u8 isr = 0;
174 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr);
175 return isr;
176}
177
178static u16
179virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif,
180 u16 queue_id)
181{
182 u16 queue_num = 0;
183 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
184 &queue_id);
185 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
186 &queue_num);
187 return queue_num;
188}
189
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200190static int
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200191virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
192 u16 queue_id, void *p)
193{
194 u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200195 u32 addr2 = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200196 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
197 &queue_id);
198 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
199 (u32 *) & addr);
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200200 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
201 &addr2);
202 if ((u32) addr == addr2)
203 return 0;
204 return 1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200205}
206
207static void
208virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
209 u16 queue_id)
210{
211 u32 src = 0;
212 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
213 &queue_id);
214 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
215}
216
217inline void
218virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
219 u16 queue_id)
220{
221 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
222 &queue_id);
223}
224
225/* Enable one vector (0) for Link State Intrerrupt */
226static u16
227virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
228 u16 vec)
229{
230 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
231 &vec);
232 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
233 &vec);
234 return vec;
235}
236
237static u16
238virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
239 u16 queue_id)
240{
241 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
242 &queue_id);
243 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
244 &vec);
245 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
246 &vec);
247 return vec;
248}
249
250static u32
251virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
252 u32 flags)
253{
254 return 0;
255}
256
257static clib_error_t *
258virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
259{
260 virtio_net_config_t config;
261 clib_error_t *error = 0;
262 u16 max_queue_pairs = 1;
263
264 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
265 {
266 virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200267 sizeof (config.max_virtqueue_pairs),
268 STRUCT_OFFSET_OF (virtio_net_config_t,
269 max_virtqueue_pairs));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200270 max_queue_pairs = config.max_virtqueue_pairs;
271 }
272
Damjan Marionf41244f2019-11-08 17:41:06 +0100273 virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200274 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200275 return clib_error_return (error, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200276
277 vif->max_queue_pairs = max_queue_pairs;
278 return error;
279}
280
281static void
282virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
283{
284 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
285 sizeof (vif->mac_addr), 0);
286}
287
288static u32
289virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
290{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000291 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200292 {
293 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
294 sizeof (vif->mac_addr), 0);
295 return 0;
296 }
297 return 1;
298}
299
300static u16
301virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
302{
303 /*
304 * Minimal driver: assumes link is up
305 */
306 u16 status = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000307 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200308 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200309 STRUCT_OFFSET_OF (virtio_net_config_t,
310 status));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200311 return status;
312}
313
314static void
315virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
316{
317 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000318 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200319 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000320 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200321 u16 qid = line;
322
323 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
324}
325
326static void
327virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
328{
329 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000330 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200331 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000332 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200333
334 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
335 {
336 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
337 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
338 VNET_HW_INTERFACE_FLAG_LINK_UP);
339 }
340 else
341 {
342 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
343 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
344 }
345}
346
347static void
348virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
349{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000350 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200351 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000352 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200353 u8 isr = 0;
354 u16 line = 0;
355
356 isr = virtio_pci_legacy_get_isr (vm, vif);
357
358 /*
359 * If the lower bit is set: look through the used rings of
360 * all virtqueues for the device, to see if any progress has
361 * been made by the device which requires servicing.
362 */
363 if (isr & VIRTIO_PCI_ISR_INTR)
364 virtio_pci_irq_0_handler (vm, h, line);
365
366 if (isr & VIRTIO_PCI_ISR_CONFIG)
367 virtio_pci_irq_1_handler (vm, h, line);
368}
369
370inline void
371device_status (vlib_main_t * vm, virtio_if_t * vif)
372{
373 struct status_struct
374 {
375 u8 bit;
376 char *str;
377 };
378 struct status_struct *status_entry;
379 static struct status_struct status_array[] = {
380#define _(s,b) { .str = #s, .bit = b, },
381 foreach_virtio_config_status_flags
382#undef _
383 {.str = NULL}
384 };
385
386 vlib_cli_output (vm, " status 0x%x", vif->status);
387
388 status_entry = (struct status_struct *) &status_array;
389 while (status_entry->str)
390 {
391 if (vif->status & status_entry->bit)
392 vlib_cli_output (vm, " %s (%x)", status_entry->str,
393 status_entry->bit);
394 status_entry++;
395 }
396}
397
398inline void
399debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif)
400{
401 u32 data_u32;
402 u16 data_u16;
403 u8 data_u8;
404 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
405 &data_u32);
406 vlib_cli_output (vm, "remote features 0x%lx", data_u32);
407 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
408 &data_u32);
409 vlib_cli_output (vm, "guest features 0x%lx", data_u32);
410 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
411 &data_u32);
412 vlib_cli_output (vm, "queue address 0x%lx", data_u32);
413 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
414 &data_u16);
415 vlib_cli_output (vm, "queue size 0x%x", data_u16);
416 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
417 &data_u16);
418 vlib_cli_output (vm, "queue select 0x%x", data_u16);
419 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
420 &data_u16);
421 vlib_cli_output (vm, "queue notify 0x%x", data_u16);
422 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8);
423 vlib_cli_output (vm, "status 0x%x", data_u8);
424 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8);
425 vlib_cli_output (vm, "isr 0x%x", data_u8);
426
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000427 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
428 {
429 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
430 &data_u16);
431 vlib_cli_output (vm, "config vector 0x%x", data_u16);
432 u16 queue_id = 0;
433 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
434 &queue_id);
435 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
436 &data_u16);
437 vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16);
438 }
439
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200440 u8 mac[6];
441 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
442 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
443 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
444 6);
445 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
446 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
447 /* offset to max_virtqueue */ 8);
448 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
449 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
450 10);
451 vlib_cli_output (vm, "mtu 0x%x", data_u16);
452
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000453 u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200454 i += a;
455 i &= ~a;
456 for (; i < 64; i += 4)
457 {
458 u32 data = 0;
459 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
460 vlib_cli_output (vm, "0x%lx", data);
461 }
462}
463
Mohsin Kazmi05493782019-05-01 14:26:17 +0200464struct virtio_ctrl_msg
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000465{
466 struct virtio_net_ctrl_hdr ctrl;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000467 virtio_net_ctrl_ack status;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200468 u8 data[1024];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000469};
470
471static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200472virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
473 struct virtio_ctrl_msg *data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000474{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000475 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200476 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
477 struct virtio_ctrl_msg result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000478 u32 buffer_index;
479 vlib_buffer_t *b;
480 u16 used, next, avail;
481 u16 sz = vring->size;
482 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000483
484 used = vring->desc_in_use;
485 next = vring->desc_next;
486 avail = vring->avail->idx;
487 struct vring_desc *d = &vring->desc[next];
488
489 if (vlib_buffer_alloc (vm, &buffer_index, 1))
490 b = vlib_get_buffer (vm, buffer_index);
491 else
492 return VIRTIO_NET_ERR;
493 /*
494 * current_data may not be initialized with 0 and may contain
495 * previous offset.
496 */
497 b->current_data = 0;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200498 clib_memcpy (vlib_buffer_get_current (b), data,
499 sizeof (struct virtio_ctrl_msg));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000500 d->flags = VRING_DESC_F_NEXT;
501 d->addr = vlib_buffer_get_current_pa (vm, b);
502 d->len = sizeof (struct virtio_net_ctrl_hdr);
503 vring->avail->ring[avail & mask] = next;
504 avail++;
505 next = (next + 1) & mask;
506 d->next = next;
507 used++;
508
509 d = &vring->desc[next];
510 d->flags = VRING_DESC_F_NEXT;
511 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200512 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
513 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000514 next = (next + 1) & mask;
515 d->next = next;
516 used++;
517
518 d = &vring->desc[next];
519 d->flags = VRING_DESC_F_WRITE;
520 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200521 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
522 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000523 next = (next + 1) & mask;
524 used++;
525
526 CLIB_MEMORY_STORE_BARRIER ();
527 vring->avail->idx = avail;
528 vring->desc_next = next;
529 vring->desc_in_use = used;
530
531 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
532 {
533 virtio_kick (vm, vring, vif);
534 }
535
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000536 u16 last = vring->last_used_idx, n_left = 0;
537 n_left = vring->used->idx - last;
538
539 while (n_left)
540 {
541 struct vring_used_elem *e = &vring->used->ring[last & mask];
542 u16 slot = e->id;
543
544 d = &vring->desc[slot];
545 while (d->flags & VRING_DESC_F_NEXT)
546 {
547 used--;
548 slot = d->next;
549 d = &vring->desc[slot];
550 }
551 used--;
552 last++;
553 n_left--;
554 }
555 vring->desc_in_use = used;
556 vring->last_used_idx = last;
557
558 CLIB_MEMORY_BARRIER ();
559 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmi05493782019-05-01 14:26:17 +0200560 sizeof (struct virtio_ctrl_msg));
Damjan Marionf41244f2019-11-08 17:41:06 +0100561 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000562 status = result.status;
563 vlib_buffer_free (vm, &buffer_index, 1);
564 return status;
565}
566
Mohsin Kazmi05493782019-05-01 14:26:17 +0200567static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000568virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
569{
570 struct virtio_ctrl_msg offload_hdr;
571 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
572
573 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
574 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
575 offload_hdr.status = VIRTIO_NET_ERR;
576 u64 offloads = 0ULL;
577 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
578
579 status =
580 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
581 virtio_log_debug (vif, "disable offloads");
582 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
583 virtio_pci_legacy_get_guest_features (vm, vif);
584 return status;
585}
586
587static int
588virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
589{
590 struct virtio_ctrl_msg csum_offload_hdr;
591 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
592
593 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
594 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
595 csum_offload_hdr.status = VIRTIO_NET_ERR;
596 u64 offloads = 0ULL;
597 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
598 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
599
600 status =
601 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
602 virtio_log_debug (vif, "enable checksum offload");
603 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
604 virtio_pci_legacy_get_guest_features (vm, vif);
605 return status;
606}
607
608static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200609virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
610{
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200611 struct virtio_ctrl_msg gso_hdr;
612 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
613
614 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
615 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
616 gso_hdr.status = VIRTIO_NET_ERR;
617 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
618 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000619 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200620 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
621
622 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100623 virtio_log_debug (vif, "enable gso");
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000624 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
625 virtio_pci_legacy_get_guest_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200626 return status;
627}
628
629static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000630virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
631 int csum_offload_enabled)
632{
633 vnet_main_t *vnm = vnet_get_main ();
634 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
635
636 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
637 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
638 {
639 if (gso_enabled
640 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
641 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
642 {
643 if (virtio_pci_enable_gso (vm, vif))
644 {
645 virtio_log_warning (vif, "gso is not enabled");
646 }
647 else
648 {
649 vif->gso_enabled = 1;
650 vif->csum_offload_enabled = 0;
651 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
652 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
653 }
654 }
655 else if (csum_offload_enabled
656 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
657 {
658 if (virtio_pci_enable_checksum_offload (vm, vif))
659 {
660 virtio_log_warning (vif, "checksum offload is not enabled");
661 }
662 else
663 {
664 vif->csum_offload_enabled = 1;
665 vif->gso_enabled = 0;
666 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
667 hw->flags |=
668 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
669 }
670 }
671 else
672 {
673 if (virtio_pci_disable_offload (vm, vif))
674 {
675 virtio_log_warning (vif, "offloads are not disabled");
676 }
677 else
678 {
679 vif->csum_offload_enabled = 0;
680 vif->gso_enabled = 0;
681 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
682 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
683 }
684 }
685 }
686
687 return 0;
688}
689
690static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200691virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
692 u16 num_queues)
693{
Mohsin Kazmi05493782019-05-01 14:26:17 +0200694 struct virtio_ctrl_msg mq_hdr;
695 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
696
697 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
698 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
699 mq_hdr.status = VIRTIO_NET_ERR;
700 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
701
702 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100703 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200704 return status;
705}
706
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200707static u8
708virtio_pci_queue_size_valid (u16 qsz)
709{
710 if (qsz < 64 || qsz > 4096)
711 return 0;
712 if ((qsz % 64) != 0)
713 return 0;
714 return 1;
715}
716
717clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000718virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
719 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200720{
721 clib_error_t *error = 0;
722 u16 queue_size = 0;
723 virtio_vring_t *vring;
724 struct vring vr;
725 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000726 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200727
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000728 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200729 if (!virtio_pci_queue_size_valid (queue_size))
730 clib_warning ("queue size is not valid");
731
732 if (!is_pow2 (queue_size))
733 return clib_error_return (0, "ring size must be power of 2");
734
735 if (queue_size > 32768)
736 return clib_error_return (0, "ring size must be 32768 or lower");
737
738 if (queue_size == 0)
739 queue_size = 256;
740
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000741 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
742 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200743 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
744 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000745 ptr =
746 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
747 vif->numa_node);
748 if (!ptr)
749 return vlib_physmem_last_error (vm);
750 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200751 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
752 vring->desc = vr.desc;
753 vring->avail = vr.avail;
754 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000755 vring->queue_id = queue_num;
756 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
757
758 ASSERT (vring->buffers == 0);
759
760 vring->size = queue_size;
Damjan Marionf41244f2019-11-08 17:41:06 +0100761 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000762 queue_size);
763 virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
764 vring->kick_fd = -1;
765
766 return error;
767}
768
769clib_error_t *
770virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
771{
772 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000773 u16 queue_size = 0;
774 virtio_vring_t *vring;
775 struct vring vr;
776 u32 i = 0;
777 void *ptr = NULL;
778
779 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
780 if (!virtio_pci_queue_size_valid (queue_size))
781 clib_warning ("queue size is not valid");
782
783 if (!is_pow2 (queue_size))
784 return clib_error_return (0, "ring size must be power of 2");
785
786 if (queue_size > 32768)
787 return clib_error_return (0, "ring size must be 32768 or lower");
788
789 if (queue_size == 0)
790 queue_size = 256;
791
792 if (queue_num % 2)
793 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000794 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
795 CLIB_CACHE_LINE_BYTES);
796 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
797 clib_spinlock_init (&vring->lockp);
798 }
799 else
800 {
801 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
802 CLIB_CACHE_LINE_BYTES);
803 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
804 }
805 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
806 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
807 ptr =
808 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
809 vif->numa_node);
810 if (!ptr)
811 return vlib_physmem_last_error (vm);
812 clib_memset (ptr, 0, i);
813 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
814 vring->desc = vr.desc;
815 vring->avail = vr.avail;
816 vring->used = vr.used;
817 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200818 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
819
820 ASSERT (vring->buffers == 0);
821 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000822 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200823 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100824 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000825 queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200826 }
827 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000828 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100829 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000830 queue_size);
831 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200832 vring->size = queue_size;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200833 if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr))
834 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200835
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200836 vring->kick_fd = -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200837 return error;
838}
839
840static void
841virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
842 u64 req_features)
843{
844 /*
845 * if features are not requested
846 * default: all supported features
847 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200848 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
849 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
850 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
851 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200852 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200853 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
854 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
855 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
856 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
857 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
858 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
859 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200860 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
861 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000862 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
863 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000864 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200865 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
866 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
867
868 if (req_features == 0)
869 {
870 req_features = supported_features;
871 }
872
873 vif->features = req_features & vif->remote_features & supported_features;
874
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000875 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200876 {
877 virtio_net_config_t config;
878 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200879 sizeof (config.mtu),
880 STRUCT_OFFSET_OF (virtio_net_config_t,
881 mtu));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200882 if (config.mtu < 64)
883 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
884 }
885
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000886 vif->features =
887 virtio_pci_legacy_set_guest_features (vm, vif, vif->features);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200888}
889
890void
891virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
892{
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000893 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200894}
895
896int
897virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
898{
899 u8 status = 0;
900
901 /*
902 * Reset the device
903 */
904 status = virtio_pci_legacy_reset (vm, vif);
905
906 /*
907 * Set the Acknowledge status bit
908 */
909 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
910
911 /*
912 * Set the Driver status bit
913 */
914 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
915
916 /*
917 * Read the status and verify it
918 */
919 status = virtio_pci_legacy_get_status (vm, vif);
920 if (!
921 ((status & VIRTIO_CONFIG_STATUS_ACK)
922 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
923 return -1;
924 vif->status = status;
925
926 return 0;
927}
928
929clib_error_t *
930virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
931{
932 clib_error_t *error = 0;
933 struct virtio_pci_cap cap;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000934 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200935 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
936
937 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000938 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100939 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000940 clib_error_return (error, "error in reading capabilty list position");
941 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200942 while (pos)
943 {
944 if ((error =
945 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
946 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000947 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100948 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000949 "error in reading the capability at", pos);
950 clib_error_return (error,
951 "error in reading the capability at [%2x]", pos);
952 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200953
954 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
955 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000956 u16 flags, table_size, table_size_mask = 0x07FF;
957
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200958 if ((error =
959 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
960 sizeof (flags))))
961 clib_error_return (error,
962 "error in reading the capability at [%2x]",
963 pos + 2);
964
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000965 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +0100966 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000967 "msix interrupt vector table-size", table_size);
968
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200969 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000970 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100971 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000972 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
973 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200974 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000975 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100976 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000977 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
978 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200979 }
980
981 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
982 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100983 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000984 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200985 goto next;
986 }
987
Damjan Marionf41244f2019-11-08 17:41:06 +0100988 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000989 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
990 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200991 switch (cap.cfg_type)
992 {
993 case VIRTIO_PCI_CAP_COMMON_CFG:
994 common_cfg = 1;
995 break;
996 case VIRTIO_PCI_CAP_NOTIFY_CFG:
997 notify_base = 1;
998 break;
999 case VIRTIO_PCI_CAP_DEVICE_CFG:
1000 dev_cfg = 1;
1001 break;
1002 case VIRTIO_PCI_CAP_ISR_CFG:
1003 isr = 1;
1004 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001005 case VIRTIO_PCI_CAP_PCI_CFG:
1006 if (cap.bar == 0)
1007 pci_cfg = 1;
1008 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001009 }
1010 next:
1011 pos = cap.cap_next;
1012 }
1013
1014 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
1015 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001016 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001017 return error;
1018 }
1019
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001020 if (!pci_cfg)
1021 clib_error_return (error, "modern virtio pci device found");
1022
Damjan Marionf41244f2019-11-08 17:41:06 +01001023 virtio_log_debug (vif, "transitional virtio pci device found");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001024 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001025}
1026
1027static clib_error_t *
1028virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
1029 virtio_pci_create_if_args_t * args)
1030{
1031 clib_error_t *error = 0;
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +01001032 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001033 u8 status = 0;
1034
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001035 if ((error = virtio_pci_read_caps (vm, vif)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001036 clib_error_return (error, "Device is not supported");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001037
1038 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001039 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001040 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001041 clib_error_return (error, "Failed to reset the device");
1042 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001043 /*
1044 * read device features and negotiate (user) requested features
1045 */
1046 virtio_pci_read_device_feature (vm, vif);
1047 virtio_negotiate_features (vm, vif, args->features);
1048
1049 /*
1050 * After FEATURE_OK, driver should not accept new feature bits
1051 */
1052 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1053 status = virtio_pci_legacy_get_status (vm, vif);
1054 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001055 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001056 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001057 "error encountered: Device doesn't support requested features");
1058 clib_error_return (error, "Device doesn't support requested features");
1059 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001060 vif->status = status;
1061
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001062 /*
1063 * get or set the mac address
1064 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001065 if (virtio_pci_get_mac (vm, vif))
1066 {
1067 f64 now = vlib_time_now (vm);
1068 u32 rnd;
1069 rnd = (u32) (now * 1e6);
1070 rnd = random_u32 (&rnd);
1071
1072 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1073 vif->mac_addr[0] = 2;
1074 vif->mac_addr[1] = 0xfe;
1075 virtio_pci_set_mac (vm, vif);
1076 }
1077
1078 virtio_set_net_hdr_size (vif);
1079
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001080 /*
1081 * Initialize the virtqueues
1082 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001083 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001084 goto err;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001085
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001086 for (int i = 0; i < vif->max_queue_pairs; i++)
1087 {
1088 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001089 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001090 virtio_log_warning (vif, "%s (%u) %s", "error in rxq-queue",
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001091 RX_QUEUE (i), "initialization");
1092 }
1093 else
1094 {
1095 vif->num_rxqs++;
1096 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001097
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +01001098 if (i >= vtm->n_vlib_mains)
1099 {
1100 /*
1101 * There is 1:1 mapping between tx queue and vpp worker thread.
1102 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
1103 * index 1 and so on.
1104 * Multiple worker threads can poll same tx queue when number of
1105 * workers are more than tx queues. In this case, 1:N mapping
1106 * between tx queue and vpp worker thread.
1107 */
1108 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
1109 TX_QUEUE (i),
1110 "no VPP worker thread is available");
1111 continue;
1112 }
1113
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001114 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001115 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001116 virtio_log_warning (vif, "%s (%u) %s", "error in txq-queue",
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001117 TX_QUEUE (i), "initialization");
1118 }
1119 else
1120 {
1121 vif->num_txqs++;
1122 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001123 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001124
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001125 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1126 {
1127 if ((error =
1128 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1129 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001130 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001131 vif->max_queue_pairs * 2, "initialization");
1132 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1133 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1134 }
1135 }
1136 else
1137 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001138 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001139 vif->cxq_vring = NULL;
1140 }
1141
1142 /*
1143 * set the msix interrupts
1144 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001145 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001146 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001147 if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
1148 VIRTIO_MSI_NO_VECTOR)
Damjan Marionf41244f2019-11-08 17:41:06 +01001149 virtio_log_warning (vif, "config vector 1 is not set");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001150 if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
1151 VIRTIO_MSI_NO_VECTOR)
Damjan Marionf41244f2019-11-08 17:41:06 +01001152 virtio_log_warning (vif, "queue vector 0 is not set");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001153 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001154
1155 /*
1156 * set the driver status OK
1157 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001158 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1159 vif->status = virtio_pci_legacy_get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001160err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001161 return error;
1162}
1163
1164void
1165virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1166{
1167 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001168 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001169 virtio_if_t *vif;
1170 vlib_pci_dev_handle_t h;
1171 clib_error_t *error = 0;
1172
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001173 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001174 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001175 if (vif->pci_addr.as_u32 == args->addr)
1176 {
1177 args->rv = VNET_API_ERROR_INVALID_VALUE;
1178 args->error =
1179 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001180 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1181 format_vlib_pci_addr, &args->addr,
1182 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001183 return;
1184 }
1185 }));
1186 /* *INDENT-ON* */
1187
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001188 pool_get (vim->interfaces, vif);
1189 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001190 vif->per_interface_next_index = ~0;
1191 vif->pci_addr.as_u32 = args->addr;
1192
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001193 if ((error =
1194 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1195 virtio_pci_device_ids, &h)))
1196 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001197 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1198 args->error =
1199 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1200 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001201 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1202 format_vlib_pci_addr, &vif->pci_addr,
1203 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001204 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001205 return;
1206 }
1207 vif->pci_dev_handle = h;
1208 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001209 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001210 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001211
1212 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001213 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001214 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001215 goto error;
1216 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001217
1218 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001219 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001220 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001221 goto error;
1222 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001223
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001224 if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001225 {
1226 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1227 &virtio_pci_irq_0_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001228 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001229 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001230 "error encountered on pci register msix handler 0");
1231 goto error;
1232 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001233 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1234 &virtio_pci_irq_1_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001235 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001236 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001237 "error encountered on pci register msix handler 1");
1238 goto error;
1239 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001240
1241 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001242 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001243 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001244 goto error;
1245 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001246 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001247 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001248 }
1249 else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1250 {
1251 /*
1252 * if msix table-size is 1, fall back to intX.
1253 */
1254 if ((error =
1255 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1256 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001257 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001258 "error encountered on pci register interrupt handler");
1259 goto error;
1260 }
1261 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001262 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001263 }
1264 else
1265 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001266 /*
1267 * WARN: intX is showing some weird behaviour.
1268 * Please don't use interrupt mode with UIO driver.
1269 */
1270 vif->support_int_mode = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001271 virtio_log_debug (vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001272 }
1273
1274 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001275 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001276 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001277 goto error;
1278 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001279
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001280 if ((error = virtio_pci_device_init (vm, vif, args)))
1281 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001282 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001283 goto error;
1284 }
1285
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001286 /* create interface */
1287 error = ethernet_register_interface (vnm, virtio_device_class.index,
1288 vif->dev_instance, vif->mac_addr,
1289 &vif->hw_if_index,
1290 virtio_pci_flag_change);
1291
1292 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001293 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001294 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001295 "error encountered on ethernet register interface");
1296 goto error;
1297 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001298
1299 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1300 vif->sw_if_index = sw->sw_if_index;
1301 args->sw_if_index = sw->sw_if_index;
1302
1303 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1304 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1305 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1306 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001307 u32 i = 0;
1308 vec_foreach_index (i, vif->rxq_vrings)
1309 {
1310 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1311 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1312 /* Set default rx mode to POLLING */
1313 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1314 VNET_HW_INTERFACE_RX_MODE_POLLING);
1315 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001316 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1317 {
1318 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1319 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1320 VNET_HW_INTERFACE_FLAG_LINK_UP);
1321 }
1322 else
1323 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001324
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001325 virtio_pci_offloads (vm, vif, args->gso_enabled,
1326 args->checksum_offload_enabled);
1327
1328 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1329 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001330 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001331 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1332 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001333 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001334 return;
1335
1336error:
1337 virtio_pci_delete_if (vm, vif);
1338 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1339 args->error = error;
1340}
1341
1342int
1343virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1344{
1345 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001346 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001347 u32 i = 0;
1348
1349 if (vif->type != VIRTIO_IF_TYPE_PCI)
1350 return VNET_API_ERROR_INVALID_INTERFACE;
1351
1352 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1353
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001354 for (i = 0; i < vif->max_queue_pairs; i++)
1355 {
1356 virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1357 virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1358 }
1359
1360 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1361 virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001362
1363 virtio_pci_legacy_reset (vm, vif);
1364
1365 if (vif->hw_if_index)
1366 {
1367 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001368 vec_foreach_index (i, vif->rxq_vrings)
1369 {
1370 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1371 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001372 ethernet_delete_interface (vnm, vif->hw_if_index);
1373 }
1374
1375 vlib_pci_device_close (vm, vif->pci_dev_handle);
1376
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001377 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001378 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001379 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001380 if (vring->kick_fd != -1)
1381 close (vring->kick_fd);
1382 if (vring->used)
1383 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001384 virtio_free_rx_buffers (vm, vring);
1385 }
1386 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001387 vlib_physmem_free (vm, vring->desc);
1388 }
1389
1390 vec_foreach_index (i, vif->txq_vrings)
1391 {
1392 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1393 if (vring->kick_fd != -1)
1394 close (vring->kick_fd);
1395 if (vring->used)
1396 {
1397 virtio_free_used_desc (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001398 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001399 vec_free (vring->buffers);
Steven Luonge5584962019-06-25 13:47:06 -07001400 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001401 vlib_physmem_free (vm, vring->desc);
1402 }
1403
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001404 if (vif->cxq_vring != NULL)
1405 {
1406 u16 last = vif->cxq_vring->last_used_idx;
1407 u16 n_left = vif->cxq_vring->used->idx - last;
1408 while (n_left)
1409 {
1410 last++;
1411 n_left--;
1412 }
1413
1414 vif->cxq_vring->last_used_idx = last;
1415 vlib_physmem_free (vm, vif->cxq_vring->desc);
1416 }
1417
1418 vec_free (vif->rxq_vrings);
1419 vec_free (vif->txq_vrings);
1420 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001421
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001422 clib_error_free (vif->error);
1423 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001424 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001425
1426 return 0;
1427}
1428
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001429int
1430virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1431 int gso_enabled,
1432 int checksum_offload_enabled,
1433 int offloads_disabled)
1434{
1435 if (vif->type != VIRTIO_IF_TYPE_PCI)
1436 return VNET_API_ERROR_INVALID_INTERFACE;
1437
1438 if (gso_enabled)
1439 virtio_pci_offloads (vm, vif, 1, 0);
1440 else if (checksum_offload_enabled)
1441 virtio_pci_offloads (vm, vif, 0, 1);
1442 else if (offloads_disabled)
1443 virtio_pci_offloads (vm, vif, 0, 0);
1444
1445 return 0;
1446}
1447
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001448/*
1449 * fd.io coding-style-patch-verification: ON
1450 *
1451 * Local Variables:
1452 * eval: (c-set-style "gnu")
1453 * End:
1454 */