blob: c7398c95fe83a24be40b4617d1b832ffc678485f [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 Kazmi57d73212020-03-05 12:22:41 +0000275 return clib_error_return (error, "max queue pair is %x,"
276 " should be in range [1, 0x8000]",
277 max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200278
279 vif->max_queue_pairs = max_queue_pairs;
280 return error;
281}
282
283static void
284virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
285{
286 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
287 sizeof (vif->mac_addr), 0);
288}
289
290static u32
291virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
292{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000293 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200294 {
295 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
296 sizeof (vif->mac_addr), 0);
297 return 0;
298 }
299 return 1;
300}
301
302static u16
303virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
304{
305 /*
306 * Minimal driver: assumes link is up
307 */
308 u16 status = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000309 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200310 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200311 STRUCT_OFFSET_OF (virtio_net_config_t,
312 status));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200313 return status;
314}
315
316static void
317virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
318{
319 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000320 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200321 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000322 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200323 u16 qid = line;
324
325 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
326}
327
328static void
329virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
330{
331 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000332 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200333 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000334 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200335
336 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
337 {
338 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
339 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
340 VNET_HW_INTERFACE_FLAG_LINK_UP);
341 }
342 else
343 {
344 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
345 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
346 }
347}
348
349static void
350virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
351{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000352 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200353 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000354 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200355 u8 isr = 0;
356 u16 line = 0;
357
358 isr = virtio_pci_legacy_get_isr (vm, vif);
359
360 /*
361 * If the lower bit is set: look through the used rings of
362 * all virtqueues for the device, to see if any progress has
363 * been made by the device which requires servicing.
364 */
365 if (isr & VIRTIO_PCI_ISR_INTR)
366 virtio_pci_irq_0_handler (vm, h, line);
367
368 if (isr & VIRTIO_PCI_ISR_CONFIG)
369 virtio_pci_irq_1_handler (vm, h, line);
370}
371
372inline void
373device_status (vlib_main_t * vm, virtio_if_t * vif)
374{
375 struct status_struct
376 {
377 u8 bit;
378 char *str;
379 };
380 struct status_struct *status_entry;
381 static struct status_struct status_array[] = {
382#define _(s,b) { .str = #s, .bit = b, },
383 foreach_virtio_config_status_flags
384#undef _
385 {.str = NULL}
386 };
387
388 vlib_cli_output (vm, " status 0x%x", vif->status);
389
390 status_entry = (struct status_struct *) &status_array;
391 while (status_entry->str)
392 {
393 if (vif->status & status_entry->bit)
394 vlib_cli_output (vm, " %s (%x)", status_entry->str,
395 status_entry->bit);
396 status_entry++;
397 }
398}
399
400inline void
401debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif)
402{
403 u32 data_u32;
404 u16 data_u16;
405 u8 data_u8;
406 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
407 &data_u32);
408 vlib_cli_output (vm, "remote features 0x%lx", data_u32);
409 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
410 &data_u32);
411 vlib_cli_output (vm, "guest features 0x%lx", data_u32);
412 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
413 &data_u32);
414 vlib_cli_output (vm, "queue address 0x%lx", data_u32);
415 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
416 &data_u16);
417 vlib_cli_output (vm, "queue size 0x%x", data_u16);
418 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
419 &data_u16);
420 vlib_cli_output (vm, "queue select 0x%x", data_u16);
421 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
422 &data_u16);
423 vlib_cli_output (vm, "queue notify 0x%x", data_u16);
424 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8);
425 vlib_cli_output (vm, "status 0x%x", data_u8);
426 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8);
427 vlib_cli_output (vm, "isr 0x%x", data_u8);
428
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000429 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
430 {
431 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
432 &data_u16);
433 vlib_cli_output (vm, "config vector 0x%x", data_u16);
434 u16 queue_id = 0;
435 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
436 &queue_id);
437 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
438 &data_u16);
439 vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16);
440 }
441
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200442 u8 mac[6];
443 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
444 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
445 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
446 6);
447 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
448 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
449 /* offset to max_virtqueue */ 8);
450 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
451 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
452 10);
453 vlib_cli_output (vm, "mtu 0x%x", data_u16);
454
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000455 u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200456 i += a;
457 i &= ~a;
458 for (; i < 64; i += 4)
459 {
460 u32 data = 0;
461 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
462 vlib_cli_output (vm, "0x%lx", data);
463 }
464}
465
Mohsin Kazmi05493782019-05-01 14:26:17 +0200466struct virtio_ctrl_msg
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000467{
468 struct virtio_net_ctrl_hdr ctrl;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000469 virtio_net_ctrl_ack status;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200470 u8 data[1024];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000471};
472
473static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200474virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
475 struct virtio_ctrl_msg *data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000476{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000477 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200478 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
479 struct virtio_ctrl_msg result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000480 u32 buffer_index;
481 vlib_buffer_t *b;
482 u16 used, next, avail;
483 u16 sz = vring->size;
484 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000485
486 used = vring->desc_in_use;
487 next = vring->desc_next;
488 avail = vring->avail->idx;
489 struct vring_desc *d = &vring->desc[next];
490
491 if (vlib_buffer_alloc (vm, &buffer_index, 1))
492 b = vlib_get_buffer (vm, buffer_index);
493 else
494 return VIRTIO_NET_ERR;
495 /*
496 * current_data may not be initialized with 0 and may contain
497 * previous offset.
498 */
499 b->current_data = 0;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200500 clib_memcpy (vlib_buffer_get_current (b), data,
501 sizeof (struct virtio_ctrl_msg));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000502 d->flags = VRING_DESC_F_NEXT;
503 d->addr = vlib_buffer_get_current_pa (vm, b);
504 d->len = sizeof (struct virtio_net_ctrl_hdr);
505 vring->avail->ring[avail & mask] = next;
506 avail++;
507 next = (next + 1) & mask;
508 d->next = next;
509 used++;
510
511 d = &vring->desc[next];
512 d->flags = VRING_DESC_F_NEXT;
513 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200514 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
515 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000516 next = (next + 1) & mask;
517 d->next = next;
518 used++;
519
520 d = &vring->desc[next];
521 d->flags = VRING_DESC_F_WRITE;
522 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200523 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
524 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000525 next = (next + 1) & mask;
526 used++;
527
528 CLIB_MEMORY_STORE_BARRIER ();
529 vring->avail->idx = avail;
530 vring->desc_next = next;
531 vring->desc_in_use = used;
532
533 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
534 {
535 virtio_kick (vm, vring, vif);
536 }
537
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000538 u16 last = vring->last_used_idx, n_left = 0;
539 n_left = vring->used->idx - last;
540
541 while (n_left)
542 {
543 struct vring_used_elem *e = &vring->used->ring[last & mask];
544 u16 slot = e->id;
545
546 d = &vring->desc[slot];
547 while (d->flags & VRING_DESC_F_NEXT)
548 {
549 used--;
550 slot = d->next;
551 d = &vring->desc[slot];
552 }
553 used--;
554 last++;
555 n_left--;
556 }
557 vring->desc_in_use = used;
558 vring->last_used_idx = last;
559
560 CLIB_MEMORY_BARRIER ();
561 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmi05493782019-05-01 14:26:17 +0200562 sizeof (struct virtio_ctrl_msg));
Damjan Marionf41244f2019-11-08 17:41:06 +0100563 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000564 status = result.status;
565 vlib_buffer_free (vm, &buffer_index, 1);
566 return status;
567}
568
Mohsin Kazmi05493782019-05-01 14:26:17 +0200569static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000570virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
571{
572 struct virtio_ctrl_msg offload_hdr;
573 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
574
575 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
576 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
577 offload_hdr.status = VIRTIO_NET_ERR;
578 u64 offloads = 0ULL;
579 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
580
581 status =
582 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
583 virtio_log_debug (vif, "disable offloads");
584 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
585 virtio_pci_legacy_get_guest_features (vm, vif);
586 return status;
587}
588
589static int
590virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
591{
592 struct virtio_ctrl_msg csum_offload_hdr;
593 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
594
595 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
596 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
597 csum_offload_hdr.status = VIRTIO_NET_ERR;
598 u64 offloads = 0ULL;
599 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
600 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
601
602 status =
603 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
604 virtio_log_debug (vif, "enable checksum offload");
605 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
606 virtio_pci_legacy_get_guest_features (vm, vif);
607 return status;
608}
609
610static int
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200611virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
612{
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200613 struct virtio_ctrl_msg gso_hdr;
614 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
615
616 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
617 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
618 gso_hdr.status = VIRTIO_NET_ERR;
619 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
620 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000621 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200622 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
623
624 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
Damjan Marionf41244f2019-11-08 17:41:06 +0100625 virtio_log_debug (vif, "enable gso");
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000626 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
627 virtio_pci_legacy_get_guest_features (vm, vif);
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200628 return status;
629}
630
631static int
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000632virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
633 int csum_offload_enabled)
634{
635 vnet_main_t *vnm = vnet_get_main ();
636 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
637
638 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
639 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
640 {
641 if (gso_enabled
642 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
643 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
644 {
645 if (virtio_pci_enable_gso (vm, vif))
646 {
647 virtio_log_warning (vif, "gso is not enabled");
648 }
649 else
650 {
651 vif->gso_enabled = 1;
652 vif->csum_offload_enabled = 0;
653 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
654 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
655 }
656 }
657 else if (csum_offload_enabled
658 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
659 {
660 if (virtio_pci_enable_checksum_offload (vm, vif))
661 {
662 virtio_log_warning (vif, "checksum offload is not enabled");
663 }
664 else
665 {
666 vif->csum_offload_enabled = 1;
667 vif->gso_enabled = 0;
668 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
669 hw->flags |=
670 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
671 }
672 }
673 else
674 {
675 if (virtio_pci_disable_offload (vm, vif))
676 {
677 virtio_log_warning (vif, "offloads are not disabled");
678 }
679 else
680 {
681 vif->csum_offload_enabled = 0;
682 vif->gso_enabled = 0;
683 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
684 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
685 }
686 }
687 }
688
689 return 0;
690}
691
692static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200693virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
694 u16 num_queues)
695{
Mohsin Kazmi05493782019-05-01 14:26:17 +0200696 struct virtio_ctrl_msg mq_hdr;
697 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
698
699 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
700 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
701 mq_hdr.status = VIRTIO_NET_ERR;
702 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
703
704 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
Damjan Marionf41244f2019-11-08 17:41:06 +0100705 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
Mohsin Kazmi05493782019-05-01 14:26:17 +0200706 return status;
707}
708
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200709static u8
710virtio_pci_queue_size_valid (u16 qsz)
711{
712 if (qsz < 64 || qsz > 4096)
713 return 0;
714 if ((qsz % 64) != 0)
715 return 0;
716 return 1;
717}
718
719clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000720virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
721 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200722{
723 clib_error_t *error = 0;
724 u16 queue_size = 0;
725 virtio_vring_t *vring;
726 struct vring vr;
727 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000728 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200729
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000730 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200731 if (!virtio_pci_queue_size_valid (queue_size))
732 clib_warning ("queue size is not valid");
733
734 if (!is_pow2 (queue_size))
735 return clib_error_return (0, "ring size must be power of 2");
736
737 if (queue_size > 32768)
738 return clib_error_return (0, "ring size must be 32768 or lower");
739
740 if (queue_size == 0)
741 queue_size = 256;
742
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000743 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
744 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200745 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
746 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000747 ptr =
748 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
749 vif->numa_node);
750 if (!ptr)
751 return vlib_physmem_last_error (vm);
752 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200753 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
754 vring->desc = vr.desc;
755 vring->avail = vr.avail;
756 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000757 vring->queue_id = queue_num;
758 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
759
760 ASSERT (vring->buffers == 0);
761
762 vring->size = queue_size;
Damjan Marionf41244f2019-11-08 17:41:06 +0100763 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000764 queue_size);
765 virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
766 vring->kick_fd = -1;
767
768 return error;
769}
770
771clib_error_t *
772virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
773{
774 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000775 u16 queue_size = 0;
776 virtio_vring_t *vring;
777 struct vring vr;
778 u32 i = 0;
779 void *ptr = NULL;
780
781 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
782 if (!virtio_pci_queue_size_valid (queue_size))
783 clib_warning ("queue size is not valid");
784
785 if (!is_pow2 (queue_size))
786 return clib_error_return (0, "ring size must be power of 2");
787
788 if (queue_size > 32768)
789 return clib_error_return (0, "ring size must be 32768 or lower");
790
791 if (queue_size == 0)
792 queue_size = 256;
793
794 if (queue_num % 2)
795 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000796 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
797 CLIB_CACHE_LINE_BYTES);
798 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
799 clib_spinlock_init (&vring->lockp);
800 }
801 else
802 {
803 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
804 CLIB_CACHE_LINE_BYTES);
805 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
806 }
807 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
808 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
809 ptr =
810 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
811 vif->numa_node);
812 if (!ptr)
813 return vlib_physmem_last_error (vm);
814 clib_memset (ptr, 0, i);
815 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
816 vring->desc = vr.desc;
817 vring->avail = vr.avail;
818 vring->used = vr.used;
819 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200820 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
821
822 ASSERT (vring->buffers == 0);
823 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000824 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200825 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100826 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000827 queue_size);
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100828 clib_memset_u32 (vring->buffers, ~0, queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200829 }
830 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000831 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100832 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000833 queue_size);
834 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200835 vring->size = queue_size;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200836 if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr))
837 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200838
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200839 vring->kick_fd = -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200840 return error;
841}
842
843static void
844virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
845 u64 req_features)
846{
847 /*
848 * if features are not requested
849 * default: all supported features
850 */
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200851 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
852 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
853 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
854 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200855 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200856 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
857 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
858 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
859 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
860 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
861 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
862 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200863 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
864 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000865 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
866 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000867 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200868 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
869 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
870
871 if (req_features == 0)
872 {
873 req_features = supported_features;
874 }
875
876 vif->features = req_features & vif->remote_features & supported_features;
877
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000878 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200879 {
880 virtio_net_config_t config;
881 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200882 sizeof (config.mtu),
883 STRUCT_OFFSET_OF (virtio_net_config_t,
884 mtu));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200885 if (config.mtu < 64)
886 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
887 }
888
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000889 vif->features =
890 virtio_pci_legacy_set_guest_features (vm, vif, vif->features);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200891}
892
893void
894virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
895{
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000896 vif->remote_features = virtio_pci_legacy_get_host_features (vm, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200897}
898
899int
900virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
901{
902 u8 status = 0;
903
904 /*
905 * Reset the device
906 */
907 status = virtio_pci_legacy_reset (vm, vif);
908
909 /*
910 * Set the Acknowledge status bit
911 */
912 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
913
914 /*
915 * Set the Driver status bit
916 */
917 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
918
919 /*
920 * Read the status and verify it
921 */
922 status = virtio_pci_legacy_get_status (vm, vif);
923 if (!
924 ((status & VIRTIO_CONFIG_STATUS_ACK)
925 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
926 return -1;
927 vif->status = status;
928
929 return 0;
930}
931
932clib_error_t *
933virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
934{
935 clib_error_t *error = 0;
936 struct virtio_pci_cap cap;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000937 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200938 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
939
940 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000941 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100942 virtio_log_error (vif, "error in reading capabilty list position");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000943 clib_error_return (error, "error in reading capabilty list position");
944 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200945 while (pos)
946 {
947 if ((error =
948 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
949 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000950 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100951 virtio_log_error (vif, "%s [%2x]",
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000952 "error in reading the capability at", pos);
953 clib_error_return (error,
954 "error in reading the capability at [%2x]", pos);
955 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200956
957 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
958 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000959 u16 flags, table_size, table_size_mask = 0x07FF;
960
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200961 if ((error =
962 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
963 sizeof (flags))))
964 clib_error_return (error,
965 "error in reading the capability at [%2x]",
966 pos + 2);
967
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000968 table_size = flags & table_size_mask;
Damjan Marionf41244f2019-11-08 17:41:06 +0100969 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000970 "msix interrupt vector table-size", table_size);
971
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200972 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000973 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100974 virtio_log_debug (vif, "msix interrupt enabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000975 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
976 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200977 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000978 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100979 virtio_log_debug (vif, "msix interrupt disabled");
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000980 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
981 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200982 }
983
984 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
985 {
Damjan Marionf41244f2019-11-08 17:41:06 +0100986 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000987 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200988 goto next;
989 }
990
Damjan Marionf41244f2019-11-08 17:41:06 +0100991 virtio_log_debug (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000992 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
993 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200994 switch (cap.cfg_type)
995 {
996 case VIRTIO_PCI_CAP_COMMON_CFG:
997 common_cfg = 1;
998 break;
999 case VIRTIO_PCI_CAP_NOTIFY_CFG:
1000 notify_base = 1;
1001 break;
1002 case VIRTIO_PCI_CAP_DEVICE_CFG:
1003 dev_cfg = 1;
1004 break;
1005 case VIRTIO_PCI_CAP_ISR_CFG:
1006 isr = 1;
1007 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001008 case VIRTIO_PCI_CAP_PCI_CFG:
1009 if (cap.bar == 0)
1010 pci_cfg = 1;
1011 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001012 }
1013 next:
1014 pos = cap.cap_next;
1015 }
1016
1017 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
1018 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001019 virtio_log_debug (vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001020 return error;
1021 }
1022
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +00001023 if (!pci_cfg)
1024 clib_error_return (error, "modern virtio pci device found");
1025
Damjan Marionf41244f2019-11-08 17:41:06 +01001026 virtio_log_debug (vif, "transitional virtio pci device found");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001027 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001028}
1029
1030static clib_error_t *
1031virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
1032 virtio_pci_create_if_args_t * args)
1033{
1034 clib_error_t *error = 0;
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +01001035 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001036 u8 status = 0;
1037
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +00001038 if ((error = virtio_pci_read_caps (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001039 {
1040 args->rv = VNET_API_ERROR_UNSUPPORTED;
1041 virtio_log_error (vif, "Device is not supported");
1042 clib_error_return (error, "Device is not supported");
1043 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001044
1045 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001046 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001047 args->rv = VNET_API_ERROR_INIT_FAILED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001048 virtio_log_error (vif, "Failed to reset the device");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001049 clib_error_return (error, "Failed to reset the device");
1050 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001051 /*
1052 * read device features and negotiate (user) requested features
1053 */
1054 virtio_pci_read_device_feature (vm, vif);
Mohsin Kazmice215992020-04-10 14:57:06 +00001055 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1056 0)
1057 {
1058 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1059 "support VIRTIO_RING_F_INDIRECT_DESC features");
1060 }
1061 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1062 {
1063 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1064 "support VIRTIO_NET_F_MRG_RXBUF features");
1065 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001066 virtio_negotiate_features (vm, vif, args->features);
1067
1068 /*
1069 * After FEATURE_OK, driver should not accept new feature bits
1070 */
1071 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
1072 status = virtio_pci_legacy_get_status (vm, vif);
1073 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001074 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001075 args->rv = VNET_API_ERROR_UNSUPPORTED;
Damjan Marionf41244f2019-11-08 17:41:06 +01001076 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001077 "error encountered: Device doesn't support requested features");
1078 clib_error_return (error, "Device doesn't support requested features");
1079 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001080 vif->status = status;
1081
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001082 /*
1083 * get or set the mac address
1084 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001085 if (virtio_pci_get_mac (vm, vif))
1086 {
1087 f64 now = vlib_time_now (vm);
1088 u32 rnd;
1089 rnd = (u32) (now * 1e6);
1090 rnd = random_u32 (&rnd);
1091
1092 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1093 vif->mac_addr[0] = 2;
1094 vif->mac_addr[1] = 0xfe;
1095 virtio_pci_set_mac (vm, vif);
1096 }
1097
1098 virtio_set_net_hdr_size (vif);
1099
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001100 /*
1101 * Initialize the virtqueues
1102 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001103 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001104 {
1105 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1106 goto err;
1107 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001108
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001109 for (int i = 0; i < vif->max_queue_pairs; i++)
1110 {
1111 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001112 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001113 args->rv = VNET_API_ERROR_INIT_FAILED;
1114 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1115 RX_QUEUE (i), "initialization");
1116 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1117 RX_QUEUE (i), "initialization");
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001118 }
1119 else
1120 {
1121 vif->num_rxqs++;
1122 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001123
Mohsin Kazmiedf3b4b2019-10-30 15:00:04 +01001124 if (i >= vtm->n_vlib_mains)
1125 {
1126 /*
1127 * There is 1:1 mapping between tx queue and vpp worker thread.
1128 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
1129 * index 1 and so on.
1130 * Multiple worker threads can poll same tx queue when number of
1131 * workers are more than tx queues. In this case, 1:N mapping
1132 * between tx queue and vpp worker thread.
1133 */
1134 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
1135 TX_QUEUE (i),
1136 "no VPP worker thread is available");
1137 continue;
1138 }
1139
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001140 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001141 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001142 args->rv = VNET_API_ERROR_INIT_FAILED;
1143 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1144 TX_QUEUE (i), "initialization");
1145 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1146 TX_QUEUE (i), "initialization");
Mohsin Kazmi43b512c2019-04-30 17:25:26 +02001147 }
1148 else
1149 {
1150 vif->num_txqs++;
1151 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001152 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001153
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001154 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1155 {
1156 if ((error =
1157 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1158 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001159 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001160 vif->max_queue_pairs * 2, "initialization");
1161 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1162 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1163 }
1164 }
1165 else
1166 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001167 virtio_log_debug (vif, "control queue is not available");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001168 vif->cxq_vring = NULL;
1169 }
1170
1171 /*
1172 * set the msix interrupts
1173 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001174 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001175 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001176 if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
1177 VIRTIO_MSI_NO_VECTOR)
Damjan Marionf41244f2019-11-08 17:41:06 +01001178 virtio_log_warning (vif, "config vector 1 is not set");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001179 if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
1180 VIRTIO_MSI_NO_VECTOR)
Damjan Marionf41244f2019-11-08 17:41:06 +01001181 virtio_log_warning (vif, "queue vector 0 is not set");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001182 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001183
1184 /*
1185 * set the driver status OK
1186 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001187 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1188 vif->status = virtio_pci_legacy_get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001189err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001190 return error;
1191}
1192
1193void
1194virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
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 virtio_if_t *vif;
1199 vlib_pci_dev_handle_t h;
1200 clib_error_t *error = 0;
1201
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001202 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001203 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001204 if (vif->pci_addr.as_u32 == args->addr)
1205 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001206 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001207 args->error =
1208 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001209 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1210 format_vlib_pci_addr, &args->addr,
1211 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001212 return;
1213 }
1214 }));
1215 /* *INDENT-ON* */
1216
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001217 pool_get (vim->interfaces, vif);
1218 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001219 vif->per_interface_next_index = ~0;
1220 vif->pci_addr.as_u32 = args->addr;
1221
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001222 if ((error =
1223 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1224 virtio_pci_device_ids, &h)))
1225 {
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001226 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1227 args->error =
1228 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1229 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001230 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1231 format_vlib_pci_addr, &vif->pci_addr,
1232 "error encountered on pci device open");
Benoît Ganne8f011832019-11-20 14:32:38 +01001233 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001234 return;
1235 }
1236 vif->pci_dev_handle = h;
1237 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001238 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Damjan Marionf41244f2019-11-08 17:41:06 +01001239 vif->type = VIRTIO_IF_TYPE_PCI;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001240
1241 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001242 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001243 virtio_log_error (vif, "error encountered on pci bus master enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001244 goto error;
1245 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001246
1247 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001248 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001249 virtio_log_error (vif, "error encountered on pci io region");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001250 goto error;
1251 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001252
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001253 if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001254 {
1255 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1256 &virtio_pci_irq_0_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001257 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001258 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001259 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001260 "error encountered on pci register msix handler 0");
1261 goto error;
1262 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001263 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1264 &virtio_pci_irq_1_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001265 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001266 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001267 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001268 "error encountered on pci register msix handler 1");
1269 goto error;
1270 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001271
1272 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001273 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001274 virtio_log_error (vif, "error encountered on pci enable msix irq");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001275 goto error;
1276 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001277 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001278 virtio_log_debug (vif, "device supports msix interrupts");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001279 }
1280 else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1281 {
1282 /*
1283 * if msix table-size is 1, fall back to intX.
1284 */
1285 if ((error =
1286 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1287 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001288 virtio_log_error (vif,
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001289 "error encountered on pci register interrupt handler");
1290 goto error;
1291 }
1292 vif->support_int_mode = 1;
Damjan Marionf41244f2019-11-08 17:41:06 +01001293 virtio_log_debug (vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001294 }
1295 else
1296 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001297 /*
1298 * WARN: intX is showing some weird behaviour.
1299 * Please don't use interrupt mode with UIO driver.
1300 */
1301 vif->support_int_mode = 0;
Damjan Marionf41244f2019-11-08 17:41:06 +01001302 virtio_log_debug (vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001303 }
1304
1305 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001306 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001307 virtio_log_error (vif, "error encountered on pci interrupt enable");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001308 goto error;
1309 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001310
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001311 if ((error = virtio_pci_device_init (vm, vif, args)))
1312 {
Damjan Marionf41244f2019-11-08 17:41:06 +01001313 virtio_log_error (vif, "error encountered on device init");
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001314 goto error;
1315 }
1316
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001317 /* create interface */
1318 error = ethernet_register_interface (vnm, virtio_device_class.index,
1319 vif->dev_instance, vif->mac_addr,
1320 &vif->hw_if_index,
1321 virtio_pci_flag_change);
1322
1323 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001324 {
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001325 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
Damjan Marionf41244f2019-11-08 17:41:06 +01001326 virtio_log_error (vif,
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001327 "error encountered on ethernet register interface");
1328 goto error;
1329 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001330
1331 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1332 vif->sw_if_index = sw->sw_if_index;
1333 args->sw_if_index = sw->sw_if_index;
1334
1335 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1336 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1337 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1338 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001339 u32 i = 0;
1340 vec_foreach_index (i, vif->rxq_vrings)
1341 {
1342 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1343 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1344 /* Set default rx mode to POLLING */
1345 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1346 VNET_HW_INTERFACE_RX_MODE_POLLING);
1347 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001348 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1349 {
1350 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1351 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1352 VNET_HW_INTERFACE_FLAG_LINK_UP);
1353 }
1354 else
1355 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001356
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001357 virtio_pci_offloads (vm, vif, args->gso_enabled,
1358 args->checksum_offload_enabled);
1359
1360 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1361 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001362 {
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001363 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1364 virtio_log_warning (vif, "multiqueue is not set");
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001365 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001366 return;
1367
1368error:
1369 virtio_pci_delete_if (vm, vif);
Mohsin Kazmi57d73212020-03-05 12:22:41 +00001370 if (args->rv == 0)
1371 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001372 args->error = error;
1373}
1374
1375int
1376virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1377{
1378 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001379 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001380 u32 i = 0;
1381
1382 if (vif->type != VIRTIO_IF_TYPE_PCI)
1383 return VNET_API_ERROR_INVALID_INTERFACE;
1384
1385 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1386
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001387 for (i = 0; i < vif->max_queue_pairs; i++)
1388 {
1389 virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1390 virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1391 }
1392
1393 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1394 virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001395
1396 virtio_pci_legacy_reset (vm, vif);
1397
1398 if (vif->hw_if_index)
1399 {
1400 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001401 vec_foreach_index (i, vif->rxq_vrings)
1402 {
1403 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1404 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001405 ethernet_delete_interface (vnm, vif->hw_if_index);
1406 }
1407
1408 vlib_pci_device_close (vm, vif->pci_dev_handle);
1409
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001410 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001411 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001412 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001413 if (vring->kick_fd != -1)
1414 close (vring->kick_fd);
1415 if (vring->used)
1416 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001417 virtio_free_rx_buffers (vm, vring);
1418 }
1419 vec_free (vring->buffers);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001420 vlib_physmem_free (vm, vring->desc);
1421 }
1422
1423 vec_foreach_index (i, vif->txq_vrings)
1424 {
1425 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1426 if (vring->kick_fd != -1)
1427 close (vring->kick_fd);
1428 if (vring->used)
1429 {
1430 virtio_free_used_desc (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001431 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001432 vec_free (vring->buffers);
Steven Luonge5584962019-06-25 13:47:06 -07001433 clib_spinlock_free (&vring->lockp);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001434 vlib_physmem_free (vm, vring->desc);
1435 }
1436
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001437 if (vif->cxq_vring != NULL)
1438 {
1439 u16 last = vif->cxq_vring->last_used_idx;
1440 u16 n_left = vif->cxq_vring->used->idx - last;
1441 while (n_left)
1442 {
1443 last++;
1444 n_left--;
1445 }
1446
1447 vif->cxq_vring->last_used_idx = last;
1448 vlib_physmem_free (vm, vif->cxq_vring->desc);
1449 }
1450
1451 vec_free (vif->rxq_vrings);
1452 vec_free (vif->txq_vrings);
1453 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001454
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001455 clib_error_free (vif->error);
1456 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001457 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001458
1459 return 0;
1460}
1461
Mohsin Kazmi6d4af892020-01-03 15:11:53 +00001462int
1463virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1464 int gso_enabled,
1465 int checksum_offload_enabled,
1466 int offloads_disabled)
1467{
1468 if (vif->type != VIRTIO_IF_TYPE_PCI)
1469 return VNET_API_ERROR_INVALID_INTERFACE;
1470
1471 if (gso_enabled)
1472 virtio_pci_offloads (vm, vif, 1, 0);
1473 else if (checksum_offload_enabled)
1474 virtio_pci_offloads (vm, vif, 0, 1);
1475 else if (offloads_disabled)
1476 virtio_pci_offloads (vm, vif, 0, 0);
1477
1478 return 0;
1479}
1480
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001481/*
1482 * fd.io coding-style-patch-verification: ON
1483 *
1484 * Local Variables:
1485 * eval: (c-set-style "gnu")
1486 * End:
1487 */