blob: 825cba1bb60316a0ddc7a380e6e504e10242568d [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
113virtio_pci_legacy_get_features (vlib_main_t * vm, virtio_if_t * vif)
114{
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
122virtio_pci_legacy_set_features (vlib_main_t * vm, virtio_if_t * vif,
123 u64 features)
124{
125 if ((features >> 32) != 0)
126 {
127 clib_warning ("only 32 bit features are allowed for legacy virtio!");
128 }
129 u32 feature = 0, guest_features = (u32) features;
130 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
131 &guest_features);
132 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
133 &feature);
134 return feature;
135}
136
137static u8
138virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif)
139{
140 u8 status = 0;
141 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
142 return status;
143}
144
145static void
146virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status)
147{
148 if (status != VIRTIO_CONFIG_STATUS_RESET)
149 status |= virtio_pci_legacy_get_status (vm, vif);
150 vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
151}
152
153static u8
154virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif)
155{
156 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET);
157 return virtio_pci_legacy_get_status (vm, vif);
158}
159
160static u8
161virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif)
162{
163 u8 isr = 0;
164 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr);
165 return isr;
166}
167
168static u16
169virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif,
170 u16 queue_id)
171{
172 u16 queue_num = 0;
173 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
174 &queue_id);
175 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
176 &queue_num);
177 return queue_num;
178}
179
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200180static int
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200181virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
182 u16 queue_id, void *p)
183{
184 u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200185 u32 addr2 = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200186 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
187 &queue_id);
188 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
189 (u32 *) & addr);
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200190 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
191 &addr2);
192 if ((u32) addr == addr2)
193 return 0;
194 return 1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200195}
196
197static void
198virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
199 u16 queue_id)
200{
201 u32 src = 0;
202 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
203 &queue_id);
204 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
205}
206
207inline void
208virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
209 u16 queue_id)
210{
211 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
212 &queue_id);
213}
214
215/* Enable one vector (0) for Link State Intrerrupt */
216static u16
217virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
218 u16 vec)
219{
220 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
221 &vec);
222 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
223 &vec);
224 return vec;
225}
226
227static u16
228virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
229 u16 queue_id)
230{
231 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
232 &queue_id);
233 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
234 &vec);
235 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
236 &vec);
237 return vec;
238}
239
240static u32
241virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
242 u32 flags)
243{
244 return 0;
245}
246
247static clib_error_t *
248virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
249{
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000250 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200251 virtio_net_config_t config;
252 clib_error_t *error = 0;
253 u16 max_queue_pairs = 1;
254
255 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
256 {
257 virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200258 sizeof (config.max_virtqueue_pairs),
259 STRUCT_OFFSET_OF (virtio_net_config_t,
260 max_virtqueue_pairs));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200261 max_queue_pairs = config.max_virtqueue_pairs;
262 }
263
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000264 virtio_log_debug (vim, vif, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200265 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200266 return clib_error_return (error, "max queue pair is %x", max_queue_pairs);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200267
268 vif->max_queue_pairs = max_queue_pairs;
269 return error;
270}
271
272static void
273virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
274{
275 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
276 sizeof (vif->mac_addr), 0);
277}
278
279static u32
280virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
281{
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000282 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200283 {
284 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
285 sizeof (vif->mac_addr), 0);
286 return 0;
287 }
288 return 1;
289}
290
291static u16
292virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
293{
294 /*
295 * Minimal driver: assumes link is up
296 */
297 u16 status = 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000298 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200299 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200300 STRUCT_OFFSET_OF (virtio_net_config_t,
301 status));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200302 return status;
303}
304
305static void
306virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
307{
308 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000309 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200310 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000311 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200312 u16 qid = line;
313
314 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
315}
316
317static void
318virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
319{
320 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000321 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200322 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000323 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200324
325 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
326 {
327 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
328 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
329 VNET_HW_INTERFACE_FLAG_LINK_UP);
330 }
331 else
332 {
333 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
334 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
335 }
336}
337
338static void
339virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
340{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000341 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200342 uword pd = vlib_pci_get_private_data (vm, h);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000343 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200344 u8 isr = 0;
345 u16 line = 0;
346
347 isr = virtio_pci_legacy_get_isr (vm, vif);
348
349 /*
350 * If the lower bit is set: look through the used rings of
351 * all virtqueues for the device, to see if any progress has
352 * been made by the device which requires servicing.
353 */
354 if (isr & VIRTIO_PCI_ISR_INTR)
355 virtio_pci_irq_0_handler (vm, h, line);
356
357 if (isr & VIRTIO_PCI_ISR_CONFIG)
358 virtio_pci_irq_1_handler (vm, h, line);
359}
360
361inline void
362device_status (vlib_main_t * vm, virtio_if_t * vif)
363{
364 struct status_struct
365 {
366 u8 bit;
367 char *str;
368 };
369 struct status_struct *status_entry;
370 static struct status_struct status_array[] = {
371#define _(s,b) { .str = #s, .bit = b, },
372 foreach_virtio_config_status_flags
373#undef _
374 {.str = NULL}
375 };
376
377 vlib_cli_output (vm, " status 0x%x", vif->status);
378
379 status_entry = (struct status_struct *) &status_array;
380 while (status_entry->str)
381 {
382 if (vif->status & status_entry->bit)
383 vlib_cli_output (vm, " %s (%x)", status_entry->str,
384 status_entry->bit);
385 status_entry++;
386 }
387}
388
389inline void
390debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif)
391{
392 u32 data_u32;
393 u16 data_u16;
394 u8 data_u8;
395 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
396 &data_u32);
397 vlib_cli_output (vm, "remote features 0x%lx", data_u32);
398 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
399 &data_u32);
400 vlib_cli_output (vm, "guest features 0x%lx", data_u32);
401 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
402 &data_u32);
403 vlib_cli_output (vm, "queue address 0x%lx", data_u32);
404 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
405 &data_u16);
406 vlib_cli_output (vm, "queue size 0x%x", data_u16);
407 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
408 &data_u16);
409 vlib_cli_output (vm, "queue select 0x%x", data_u16);
410 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
411 &data_u16);
412 vlib_cli_output (vm, "queue notify 0x%x", data_u16);
413 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8);
414 vlib_cli_output (vm, "status 0x%x", data_u8);
415 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8);
416 vlib_cli_output (vm, "isr 0x%x", data_u8);
417
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000418 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
419 {
420 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
421 &data_u16);
422 vlib_cli_output (vm, "config vector 0x%x", data_u16);
423 u16 queue_id = 0;
424 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
425 &queue_id);
426 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
427 &data_u16);
428 vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16);
429 }
430
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200431 u8 mac[6];
432 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
433 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
434 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
435 6);
436 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
437 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
438 /* offset to max_virtqueue */ 8);
439 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
440 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
441 10);
442 vlib_cli_output (vm, "mtu 0x%x", data_u16);
443
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000444 u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200445 i += a;
446 i &= ~a;
447 for (; i < 64; i += 4)
448 {
449 u32 data = 0;
450 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
451 vlib_cli_output (vm, "0x%lx", data);
452 }
453}
454
Mohsin Kazmi05493782019-05-01 14:26:17 +0200455struct virtio_ctrl_msg
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000456{
457 struct virtio_net_ctrl_hdr ctrl;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000458 virtio_net_ctrl_ack status;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200459 u8 data[1024];
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000460};
461
462static int
Mohsin Kazmi05493782019-05-01 14:26:17 +0200463virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
464 struct virtio_ctrl_msg *data, u32 len)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000465{
466 virtio_main_t *vim = &virtio_main;
467 virtio_vring_t *vring = vif->cxq_vring;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200468 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
469 struct virtio_ctrl_msg result;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000470 u32 buffer_index;
471 vlib_buffer_t *b;
472 u16 used, next, avail;
473 u16 sz = vring->size;
474 u16 mask = sz - 1;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000475
476 used = vring->desc_in_use;
477 next = vring->desc_next;
478 avail = vring->avail->idx;
479 struct vring_desc *d = &vring->desc[next];
480
481 if (vlib_buffer_alloc (vm, &buffer_index, 1))
482 b = vlib_get_buffer (vm, buffer_index);
483 else
484 return VIRTIO_NET_ERR;
485 /*
486 * current_data may not be initialized with 0 and may contain
487 * previous offset.
488 */
489 b->current_data = 0;
Mohsin Kazmi05493782019-05-01 14:26:17 +0200490 clib_memcpy (vlib_buffer_get_current (b), data,
491 sizeof (struct virtio_ctrl_msg));
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000492 d->flags = VRING_DESC_F_NEXT;
493 d->addr = vlib_buffer_get_current_pa (vm, b);
494 d->len = sizeof (struct virtio_net_ctrl_hdr);
495 vring->avail->ring[avail & mask] = next;
496 avail++;
497 next = (next + 1) & mask;
498 d->next = next;
499 used++;
500
501 d = &vring->desc[next];
502 d->flags = VRING_DESC_F_NEXT;
503 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200504 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
505 d->len = len;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000506 next = (next + 1) & mask;
507 d->next = next;
508 used++;
509
510 d = &vring->desc[next];
511 d->flags = VRING_DESC_F_WRITE;
512 d->addr = vlib_buffer_get_current_pa (vm, b) +
Mohsin Kazmi05493782019-05-01 14:26:17 +0200513 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
514 d->len = sizeof (data->status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000515 next = (next + 1) & mask;
516 used++;
517
518 CLIB_MEMORY_STORE_BARRIER ();
519 vring->avail->idx = avail;
520 vring->desc_next = next;
521 vring->desc_in_use = used;
522
523 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
524 {
525 virtio_kick (vm, vring, vif);
526 }
527
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000528 u16 last = vring->last_used_idx, n_left = 0;
529 n_left = vring->used->idx - last;
530
531 while (n_left)
532 {
533 struct vring_used_elem *e = &vring->used->ring[last & mask];
534 u16 slot = e->id;
535
536 d = &vring->desc[slot];
537 while (d->flags & VRING_DESC_F_NEXT)
538 {
539 used--;
540 slot = d->next;
541 d = &vring->desc[slot];
542 }
543 used--;
544 last++;
545 n_left--;
546 }
547 vring->desc_in_use = used;
548 vring->last_used_idx = last;
549
550 CLIB_MEMORY_BARRIER ();
551 clib_memcpy (&result, vlib_buffer_get_current (b),
Mohsin Kazmi05493782019-05-01 14:26:17 +0200552 sizeof (struct virtio_ctrl_msg));
553 virtio_log_debug (vim, vif, "ctrl-queue: status %u", result.status);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000554 status = result.status;
555 vlib_buffer_free (vm, &buffer_index, 1);
556 return status;
557}
558
Mohsin Kazmi05493782019-05-01 14:26:17 +0200559static int
560virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
561 u16 num_queues)
562{
563 virtio_main_t *vim = &virtio_main;
564 struct virtio_ctrl_msg mq_hdr;
565 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
566
567 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
568 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
569 mq_hdr.status = VIRTIO_NET_ERR;
570 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
571
572 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
573 virtio_log_debug (vim, vif, "multi-queue enable %u queues", num_queues);
574 return status;
575}
576
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200577static u8
578virtio_pci_queue_size_valid (u16 qsz)
579{
580 if (qsz < 64 || qsz > 4096)
581 return 0;
582 if ((qsz % 64) != 0)
583 return 0;
584 return 1;
585}
586
587clib_error_t *
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000588virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
589 u16 queue_num)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200590{
591 clib_error_t *error = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000592 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200593 u16 queue_size = 0;
594 virtio_vring_t *vring;
595 struct vring vr;
596 u32 i = 0;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000597 void *ptr = NULL;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200598
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000599 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200600 if (!virtio_pci_queue_size_valid (queue_size))
601 clib_warning ("queue size is not valid");
602
603 if (!is_pow2 (queue_size))
604 return clib_error_return (0, "ring size must be power of 2");
605
606 if (queue_size > 32768)
607 return clib_error_return (0, "ring size must be 32768 or lower");
608
609 if (queue_size == 0)
610 queue_size = 256;
611
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000612 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
613 vring = vec_elt_at_index (vif->cxq_vring, 0);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200614 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
615 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000616 ptr =
617 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
618 vif->numa_node);
619 if (!ptr)
620 return vlib_physmem_last_error (vm);
621 clib_memset (ptr, 0, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200622 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
623 vring->desc = vr.desc;
624 vring->avail = vr.avail;
625 vring->used = vr.used;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000626 vring->queue_id = queue_num;
627 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
628
629 ASSERT (vring->buffers == 0);
630
631 vring->size = queue_size;
632 virtio_log_debug (vim, vif, "control-queue: number %u, size %u", queue_num,
633 queue_size);
634 virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
635 vring->kick_fd = -1;
636
637 return error;
638}
639
640clib_error_t *
641virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
642{
643 clib_error_t *error = 0;
644 virtio_main_t *vim = &virtio_main;
645 vlib_thread_main_t *vtm = vlib_get_thread_main ();
646 u16 queue_size = 0;
647 virtio_vring_t *vring;
648 struct vring vr;
649 u32 i = 0;
650 void *ptr = NULL;
651
652 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
653 if (!virtio_pci_queue_size_valid (queue_size))
654 clib_warning ("queue size is not valid");
655
656 if (!is_pow2 (queue_size))
657 return clib_error_return (0, "ring size must be power of 2");
658
659 if (queue_size > 32768)
660 return clib_error_return (0, "ring size must be 32768 or lower");
661
662 if (queue_size == 0)
663 queue_size = 256;
664
665 if (queue_num % 2)
666 {
667 if (TX_QUEUE_ACCESS (queue_num) > vtm->n_vlib_mains)
668 return error;
669 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
670 CLIB_CACHE_LINE_BYTES);
671 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
672 clib_spinlock_init (&vring->lockp);
673 }
674 else
675 {
676 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
677 CLIB_CACHE_LINE_BYTES);
678 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
679 }
680 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
681 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
682 ptr =
683 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
684 vif->numa_node);
685 if (!ptr)
686 return vlib_physmem_last_error (vm);
687 clib_memset (ptr, 0, i);
688 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
689 vring->desc = vr.desc;
690 vring->avail = vr.avail;
691 vring->used = vr.used;
692 vring->queue_id = queue_num;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200693 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
694
695 ASSERT (vring->buffers == 0);
696 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
697 ASSERT (vring->indirect_buffers == 0);
698 vec_validate_aligned (vring->indirect_buffers, queue_size,
699 CLIB_CACHE_LINE_BYTES);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000700 if (queue_num % 2)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200701 {
702 u32 n_alloc = 0;
703 do
704 {
705 if (n_alloc < queue_size)
Mohsin Kazmi154a9032019-04-30 10:11:38 +0200706 n_alloc +=
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200707 vlib_buffer_alloc (vm, vring->indirect_buffers + n_alloc,
708 queue_size - n_alloc);
709 }
710 while (n_alloc != queue_size);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000711 virtio_log_debug (vim, vif, "tx-queue: number %u, size %u", queue_num,
712 queue_size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200713 }
714 else
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000715 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000716 virtio_log_debug (vim, vif, "rx-queue: number %u, size %u", queue_num,
717 queue_size);
718 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200719 vring->size = queue_size;
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200720 if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr))
721 return clib_error_return (0, "error in queue address setup");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200722
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200723 vring->kick_fd = -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200724 return error;
725}
726
727static void
728virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
729 u64 req_features)
730{
731 /*
732 * if features are not requested
733 * default: all supported features
734 */
735 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
736 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
737 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
738 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000739 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
740 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000741 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200742 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
743 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
744
745 if (req_features == 0)
746 {
747 req_features = supported_features;
748 }
749
750 vif->features = req_features & vif->remote_features & supported_features;
751
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000752 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200753 {
754 virtio_net_config_t config;
755 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200756 sizeof (config.mtu),
757 STRUCT_OFFSET_OF (virtio_net_config_t,
758 mtu));
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200759 if (config.mtu < 64)
760 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
761 }
762
763 vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
764}
765
766void
767virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
768{
769 vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
770}
771
772int
773virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
774{
775 u8 status = 0;
776
777 /*
778 * Reset the device
779 */
780 status = virtio_pci_legacy_reset (vm, vif);
781
782 /*
783 * Set the Acknowledge status bit
784 */
785 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
786
787 /*
788 * Set the Driver status bit
789 */
790 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
791
792 /*
793 * Read the status and verify it
794 */
795 status = virtio_pci_legacy_get_status (vm, vif);
796 if (!
797 ((status & VIRTIO_CONFIG_STATUS_ACK)
798 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
799 return -1;
800 vif->status = status;
801
802 return 0;
803}
804
805clib_error_t *
806virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
807{
808 clib_error_t *error = 0;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000809 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200810 struct virtio_pci_cap cap;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000811 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200812 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
813
814 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000815 {
816 virtio_log_error (vim, vif, "error in reading capabilty list position");
817 clib_error_return (error, "error in reading capabilty list position");
818 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200819 while (pos)
820 {
821 if ((error =
822 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
823 sizeof (cap))))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000824 {
825 virtio_log_error (vim, vif, "%s [%2x]",
826 "error in reading the capability at", pos);
827 clib_error_return (error,
828 "error in reading the capability at [%2x]", pos);
829 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200830
831 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
832 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000833 u16 flags, table_size, table_size_mask = 0x07FF;
834
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200835 if ((error =
836 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
837 sizeof (flags))))
838 clib_error_return (error,
839 "error in reading the capability at [%2x]",
840 pos + 2);
841
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000842 table_size = flags & table_size_mask;
843 virtio_log_debug (vim, vif, "flags:0x%x %s 0x%x", flags,
844 "msix interrupt vector table-size", table_size);
845
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200846 if (flags & PCI_MSIX_ENABLE)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000847 {
848 virtio_log_debug (vim, vif, "msix interrupt enabled");
849 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
850 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200851 else
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000852 {
853 virtio_log_debug (vim, vif, "msix interrupt disabled");
854 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
855 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200856 }
857
858 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
859 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000860 virtio_log_debug (vim, vif, "[%2x] %s %2x ", pos,
861 "skipping non VNDR cap id:", cap.cap_vndr);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200862 goto next;
863 }
864
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000865 virtio_log_debug (vim, vif,
866 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
867 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200868 switch (cap.cfg_type)
869 {
870 case VIRTIO_PCI_CAP_COMMON_CFG:
871 common_cfg = 1;
872 break;
873 case VIRTIO_PCI_CAP_NOTIFY_CFG:
874 notify_base = 1;
875 break;
876 case VIRTIO_PCI_CAP_DEVICE_CFG:
877 dev_cfg = 1;
878 break;
879 case VIRTIO_PCI_CAP_ISR_CFG:
880 isr = 1;
881 break;
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000882 case VIRTIO_PCI_CAP_PCI_CFG:
883 if (cap.bar == 0)
884 pci_cfg = 1;
885 break;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200886 }
887 next:
888 pos = cap.cap_next;
889 }
890
891 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
892 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000893 virtio_log_debug (vim, vif, "legacy virtio pci device found");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200894 return error;
895 }
896
Mohsin Kazmi0e2bc632019-01-30 13:36:57 +0000897 if (!pci_cfg)
898 clib_error_return (error, "modern virtio pci device found");
899
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000900 virtio_log_debug (vim, vif, "transitional virtio pci device found");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000901 return error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200902}
903
904static clib_error_t *
905virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
906 virtio_pci_create_if_args_t * args)
907{
908 clib_error_t *error = 0;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000909 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200910 u8 status = 0;
911
Mohsin Kazmi22c0ece2019-01-28 19:30:21 +0000912 if ((error = virtio_pci_read_caps (vm, vif)))
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000913 clib_error_return (error, "Device is not supported");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200914
915 if (virtio_pci_reset_device (vm, vif) < 0)
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000916 {
917 virtio_log_error (vim, vif, "Failed to reset the device");
918 clib_error_return (error, "Failed to reset the device");
919 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200920 /*
921 * read device features and negotiate (user) requested features
922 */
923 virtio_pci_read_device_feature (vm, vif);
924 virtio_negotiate_features (vm, vif, args->features);
925
926 /*
927 * After FEATURE_OK, driver should not accept new feature bits
928 */
929 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
930 status = virtio_pci_legacy_get_status (vm, vif);
931 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000932 {
933 virtio_log_error (vim, vif,
934 "error encountered: Device doesn't support requested features");
935 clib_error_return (error, "Device doesn't support requested features");
936 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200937 vif->status = status;
938
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000939 /*
940 * get or set the mac address
941 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200942 if (virtio_pci_get_mac (vm, vif))
943 {
944 f64 now = vlib_time_now (vm);
945 u32 rnd;
946 rnd = (u32) (now * 1e6);
947 rnd = random_u32 (&rnd);
948
949 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
950 vif->mac_addr[0] = 2;
951 vif->mac_addr[1] = 0xfe;
952 virtio_pci_set_mac (vm, vif);
953 }
954
955 virtio_set_net_hdr_size (vif);
956
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000957 /*
958 * Initialize the virtqueues
959 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200960 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
Mohsin Kazmi46f877e2019-04-08 10:36:18 +0200961 goto err;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200962
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000963 for (int i = 0; i < vif->max_queue_pairs; i++)
964 {
965 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200966 {
967 virtio_log_warning (vim, vif, "%s (%u) %s", "error in rxq-queue",
968 RX_QUEUE (i), "initialization");
969 }
970 else
971 {
972 vif->num_rxqs++;
973 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200974
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000975 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
Mohsin Kazmi43b512c2019-04-30 17:25:26 +0200976 {
977 virtio_log_warning (vim, vif, "%s (%u) %s", "error in txq-queue",
978 TX_QUEUE (i), "initialization");
979 }
980 else
981 {
982 vif->num_txqs++;
983 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000984 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200985
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000986 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
987 {
988 if ((error =
989 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
990 {
991 virtio_log_warning (vim, vif, "%s (%u) %s",
992 "error in control-queue",
993 vif->max_queue_pairs * 2, "initialization");
994 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
995 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
996 }
997 }
998 else
999 {
1000 virtio_log_debug (vim, vif, "control queue is not available");
1001 vif->cxq_vring = NULL;
1002 }
1003
1004 /*
1005 * set the msix interrupts
1006 */
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001007 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001008 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001009 if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
1010 VIRTIO_MSI_NO_VECTOR)
1011 virtio_log_warning (vim, vif, "config vector 1 is not set");
1012 if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
1013 VIRTIO_MSI_NO_VECTOR)
1014 virtio_log_warning (vim, vif, "queue vector 0 is not set");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001015 }
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001016
1017 /*
1018 * set the driver status OK
1019 */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001020 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1021 vif->status = virtio_pci_legacy_get_status (vm, vif);
Mohsin Kazmi46f877e2019-04-08 10:36:18 +02001022err:
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001023 return error;
1024}
1025
1026void
1027virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1028{
1029 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001030 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001031 virtio_if_t *vif;
1032 vlib_pci_dev_handle_t h;
1033 clib_error_t *error = 0;
1034
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001035 /* *INDENT-OFF* */
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001036 pool_foreach (vif, vim->interfaces, ({
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001037 if (vif->pci_addr.as_u32 == args->addr)
1038 {
1039 args->rv = VNET_API_ERROR_INVALID_VALUE;
1040 args->error =
1041 clib_error_return (error, "PCI address in use");
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001042 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1043 format_vlib_pci_addr, &args->addr,
1044 " PCI address in use");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001045 return;
1046 }
1047 }));
1048 /* *INDENT-ON* */
1049
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001050 pool_get (vim->interfaces, vif);
1051 vif->dev_instance = vif - vim->interfaces;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001052 vif->per_interface_next_index = ~0;
1053 vif->pci_addr.as_u32 = args->addr;
1054
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001055 if ((error =
1056 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1057 virtio_pci_device_ids, &h)))
1058 {
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001059 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001060 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1061 args->error =
1062 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1063 &vif->pci_addr);
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001064 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1065 format_vlib_pci_addr, &vif->pci_addr,
1066 "error encountered on pci device open");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001067 return;
1068 }
1069 vif->pci_dev_handle = h;
1070 vlib_pci_set_private_data (vm, h, vif->dev_instance);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001071 vif->numa_node = vlib_pci_get_numa_node (vm, h);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001072
1073 if ((error = vlib_pci_bus_master_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001074 {
1075 virtio_log_error (vim, vif,
1076 "error encountered on pci bus master enable");
1077 goto error;
1078 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001079
1080 if ((error = vlib_pci_io_region (vm, h, 0)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001081 {
1082 virtio_log_error (vim, vif, "error encountered on pci io region");
1083 goto error;
1084 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001085
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001086 if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001087 {
1088 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1089 &virtio_pci_irq_0_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001090 {
1091 virtio_log_error (vim, vif,
1092 "error encountered on pci register msix handler 0");
1093 goto error;
1094 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001095 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1096 &virtio_pci_irq_1_handler)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001097 {
1098 virtio_log_error (vim, vif,
1099 "error encountered on pci register msix handler 1");
1100 goto error;
1101 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001102
1103 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001104 {
1105 virtio_log_error (vim, vif,
1106 "error encountered on pci enable msix irq");
1107 goto error;
1108 }
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001109 vif->support_int_mode = 1;
1110 virtio_log_debug (vim, vif, "device supports msix interrupts");
1111 }
1112 else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1113 {
1114 /*
1115 * if msix table-size is 1, fall back to intX.
1116 */
1117 if ((error =
1118 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1119 {
1120 virtio_log_error (vim, vif,
1121 "error encountered on pci register interrupt handler");
1122 goto error;
1123 }
1124 vif->support_int_mode = 1;
1125 virtio_log_debug (vim, vif, "pci register interrupt handler");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001126 }
1127 else
1128 {
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001129 /*
1130 * WARN: intX is showing some weird behaviour.
1131 * Please don't use interrupt mode with UIO driver.
1132 */
1133 vif->support_int_mode = 0;
1134 virtio_log_debug (vim, vif, "driver is configured in poll mode only");
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001135 }
1136
1137 if ((error = vlib_pci_intr_enable (vm, h)))
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001138 {
1139 virtio_log_error (vim, vif,
1140 "error encountered on pci interrupt enable");
1141 goto error;
1142 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001143
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001144 if ((error = virtio_pci_device_init (vm, vif, args)))
1145 {
1146 virtio_log_error (vim, vif, "error encountered on device init");
1147 goto error;
1148 }
1149
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001150 vif->type = VIRTIO_IF_TYPE_PCI;
1151 /* create interface */
1152 error = ethernet_register_interface (vnm, virtio_device_class.index,
1153 vif->dev_instance, vif->mac_addr,
1154 &vif->hw_if_index,
1155 virtio_pci_flag_change);
1156
1157 if (error)
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001158 {
1159 virtio_log_error (vim, vif,
1160 "error encountered on ethernet register interface");
1161 goto error;
1162 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001163
1164 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1165 vif->sw_if_index = sw->sw_if_index;
1166 args->sw_if_index = sw->sw_if_index;
1167
1168 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1169 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1170 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1171 virtio_input_node.index);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001172 u32 i = 0;
1173 vec_foreach_index (i, vif->rxq_vrings)
1174 {
1175 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1176 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1177 /* Set default rx mode to POLLING */
1178 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1179 VNET_HW_INTERFACE_RX_MODE_POLLING);
1180 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001181 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1182 {
1183 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1184 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1185 VNET_HW_INTERFACE_FLAG_LINK_UP);
1186 }
1187 else
1188 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001189
1190 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1191 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
1192 {
1193 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1194 virtio_log_warning (vim, vif, "multiqueue is not set");
1195 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001196 return;
1197
1198error:
1199 virtio_pci_delete_if (vm, vif);
1200 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1201 args->error = error;
1202}
1203
1204int
1205virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1206{
1207 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001208 virtio_main_t *vim = &virtio_main;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001209 u32 i = 0;
1210
1211 if (vif->type != VIRTIO_IF_TYPE_PCI)
1212 return VNET_API_ERROR_INVALID_INTERFACE;
1213
1214 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1215
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001216 for (i = 0; i < vif->max_queue_pairs; i++)
1217 {
1218 virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1219 virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1220 }
1221
1222 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1223 virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001224
1225 virtio_pci_legacy_reset (vm, vif);
1226
1227 if (vif->hw_if_index)
1228 {
1229 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001230 vec_foreach_index (i, vif->rxq_vrings)
1231 {
1232 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1233 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001234 ethernet_delete_interface (vnm, vif->hw_if_index);
1235 }
1236
1237 vlib_pci_device_close (vm, vif->pci_dev_handle);
1238
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001239 vec_foreach_index (i, vif->rxq_vrings)
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001240 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001241 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001242 if (vring->kick_fd != -1)
1243 close (vring->kick_fd);
1244 if (vring->used)
1245 {
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001246 virtio_free_rx_buffers (vm, vring);
1247 }
1248 vec_free (vring->buffers);
1249 vec_free (vring->indirect_buffers);
1250 vlib_physmem_free (vm, vring->desc);
1251 }
1252
1253 vec_foreach_index (i, vif->txq_vrings)
1254 {
1255 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1256 if (vring->kick_fd != -1)
1257 close (vring->kick_fd);
1258 if (vring->used)
1259 {
1260 virtio_free_used_desc (vm, vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001261 }
1262 if (vring->queue_id % 2)
1263 {
1264 vlib_buffer_free_no_next (vm, vring->indirect_buffers, vring->size);
1265 }
1266 vec_free (vring->buffers);
1267 vec_free (vring->indirect_buffers);
1268 vlib_physmem_free (vm, vring->desc);
1269 }
1270
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +00001271 if (vif->cxq_vring != NULL)
1272 {
1273 u16 last = vif->cxq_vring->last_used_idx;
1274 u16 n_left = vif->cxq_vring->used->idx - last;
1275 while (n_left)
1276 {
1277 last++;
1278 n_left--;
1279 }
1280
1281 vif->cxq_vring->last_used_idx = last;
1282 vlib_physmem_free (vm, vif->cxq_vring->desc);
1283 }
1284
1285 vec_free (vif->rxq_vrings);
1286 vec_free (vif->txq_vrings);
1287 vec_free (vif->cxq_vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001288
1289 if (vif->fd != -1)
Mohsin Kazmib74fe322019-01-31 13:50:56 +00001290 vif->fd = -1;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001291 if (vif->tap_fd != -1)
1292 vif->tap_fd = -1;
1293 clib_error_free (vif->error);
1294 memset (vif, 0, sizeof (*vif));
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +00001295 pool_put (vim->interfaces, vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001296
1297 return 0;
1298}
1299
1300/*
1301 * fd.io coding-style-patch-verification: ON
1302 *
1303 * Local Variables:
1304 * eval: (c-set-style "gnu")
1305 * End:
1306 */