blob: 63c8c54fdad55836db8bcb4ba5b3e57f90672182 [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>
18#include <linux/virtio_net.h>
19#include <linux/virtio_ring.h>
20#include <linux/vhost.h>
21#include <sys/eventfd.h>
22#if defined(__x86_64__)
23#include <sys/io.h>
24#endif
25
26#include <vppinfra/types.h>
27#include <vlib/vlib.h>
28#include <vlib/pci/pci.h>
29#include <vnet/ethernet/ethernet.h>
30#include <vpp/app/version.h>
31#include <vnet/ip/ip4_packet.h>
32#include <vnet/ip/ip6_packet.h>
33#include <vnet/devices/virtio/virtio.h>
34#include <vnet/devices/virtio/pci.h>
35
36#define PCI_VENDOR_ID_VIRTIO 0x1af4
37#define PCI_DEVICE_ID_VIRTIO_NIC 0x1000
38/* Doesn't support modern device */
39#define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041
40
41#define PCI_CAPABILITY_LIST 0x34
42#define PCI_CAP_ID_VNDR 0x09
43#define PCI_CAP_ID_MSIX 0x11
44
45#define PCI_MSIX_ENABLE 0x8000
46
47static u32 msix_enabled = 0;
48
49#define PCI_CONFIG_SIZE ((msix_enabled == VIRTIO_MSIX_ENABLED) ? \
50 24 : 20)
51
52static pci_device_id_t virtio_pci_device_ids[] = {
53 {
54 .vendor_id = PCI_VENDOR_ID_VIRTIO,
55 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
56 {
57 .vendor_id = PCI_VENDOR_ID_VIRTIO,
58 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
59 {0},
60};
61
62static void
63virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst,
64 int len, u32 addr)
65{
66 u32 size = 0;
67 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
68
69 while (len > 0)
70 {
71 if (len >= 4)
72 {
73 size = 4;
74 vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE + addr, dst);
75 }
76 else if (len >= 2)
77 {
78 size = 2;
79 vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE + addr, dst);
80 }
81 else
82 {
83 size = 1;
84 vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE + addr, dst);
85 }
86 dst = (u8 *) dst + size;
87 addr += size;
88 len -= size;
89 }
90}
91
92static void
93virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif,
94 void *src, int len, u32 addr)
95{
96 u32 size = 0;
97 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
98
99 while (len > 0)
100 {
101 if (len >= 4)
102 {
103 size = 4;
104 vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE + addr, src);
105 }
106 else if (len >= 2)
107 {
108 size = 2;
109 vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE + addr, src);
110 }
111 else
112 {
113 size = 1;
114 vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE + addr, src);
115 }
116 src = (u8 *) src + size;
117 addr += size;
118 len -= size;
119 }
120}
121
122static u64
123virtio_pci_legacy_get_features (vlib_main_t * vm, virtio_if_t * vif)
124{
125 u32 features;
126 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
127 &features);
128 return features;
129}
130
131static u32
132virtio_pci_legacy_set_features (vlib_main_t * vm, virtio_if_t * vif,
133 u64 features)
134{
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
190
191static void
192virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
193 u16 queue_id, void *p)
194{
195 u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
196 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);
200}
201
202static void
203virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
204 u16 queue_id)
205{
206 u32 src = 0;
207 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
208 &queue_id);
209 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
210}
211
212inline void
213virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
214 u16 queue_id)
215{
216 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
217 &queue_id);
218}
219
220/* Enable one vector (0) for Link State Intrerrupt */
221static u16
222virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
223 u16 vec)
224{
225 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
226 &vec);
227 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
228 &vec);
229 return vec;
230}
231
232static u16
233virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
234 u16 queue_id)
235{
236 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
237 &queue_id);
238 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
239 &vec);
240 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
241 &vec);
242 return vec;
243}
244
245static u32
246virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
247 u32 flags)
248{
249 return 0;
250}
251
252static clib_error_t *
253virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
254{
255 virtio_net_config_t config;
256 clib_error_t *error = 0;
257 u16 max_queue_pairs = 1;
258
259 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
260 {
261 virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
262 sizeof (config.max_virtqueue_pairs), 8);
263 max_queue_pairs = config.max_virtqueue_pairs;
264 }
265
266 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
267 clib_error_return (error, "max queue pair is %x", max_queue_pairs);
268
269 vif->max_queue_pairs = max_queue_pairs;
270 return error;
271}
272
273static void
274virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
275{
276 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
277 sizeof (vif->mac_addr), 0);
278}
279
280static u32
281virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
282{
283 if (vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
284 {
285 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
286 sizeof (vif->mac_addr), 0);
287 return 0;
288 }
289 return 1;
290}
291
292static u16
293virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
294{
295 /*
296 * Minimal driver: assumes link is up
297 */
298 u16 status = 1;
299 if (vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
300 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
301 6);
302 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 ();
309 virtio_main_t *vmxm = &virtio_main;
310 uword pd = vlib_pci_get_private_data (vm, h);
311 virtio_if_t *vif = pool_elt_at_index (vmxm->interfaces, pd);
312 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 ();
321 virtio_main_t *vmxm = &virtio_main;
322 uword pd = vlib_pci_get_private_data (vm, h);
323 virtio_if_t *vif = pool_elt_at_index (vmxm->interfaces, pd);
324
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{
341 virtio_main_t *vmxm = &virtio_main;
342 uword pd = vlib_pci_get_private_data (vm, h);
343 virtio_if_t *vif = pool_elt_at_index (vmxm->interfaces, pd);
344 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
418 u8 mac[6];
419 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
420 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
421 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
422 6);
423 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
424 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
425 /* offset to max_virtqueue */ 8);
426 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
427 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
428 10);
429 vlib_cli_output (vm, "mtu 0x%x", data_u16);
430
431 u32 i = PCI_CONFIG_SIZE + 12, a = 4;
432 i += a;
433 i &= ~a;
434 for (; i < 64; i += 4)
435 {
436 u32 data = 0;
437 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
438 vlib_cli_output (vm, "0x%lx", data);
439 }
440}
441
442static u8
443virtio_pci_queue_size_valid (u16 qsz)
444{
445 if (qsz < 64 || qsz > 4096)
446 return 0;
447 if ((qsz % 64) != 0)
448 return 0;
449 return 1;
450}
451
452clib_error_t *
453virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx)
454{
455 clib_error_t *error = 0;
456 u16 queue_size = 0;
457 virtio_vring_t *vring;
458 struct vring vr;
459 u32 i = 0;
460 void *ptr;
461
462 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, idx);
463 if (!virtio_pci_queue_size_valid (queue_size))
464 clib_warning ("queue size is not valid");
465
466 if (!is_pow2 (queue_size))
467 return clib_error_return (0, "ring size must be power of 2");
468
469 if (queue_size > 32768)
470 return clib_error_return (0, "ring size must be 32768 or lower");
471
472 if (queue_size == 0)
473 queue_size = 256;
474
475 vec_validate_aligned (vif->vrings, idx, CLIB_CACHE_LINE_BYTES);
476 vring = vec_elt_at_index (vif->vrings, idx);
477
478 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
479 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
480 ptr = vlib_physmem_alloc_aligned (vm, i, VIRTIO_PCI_VRING_ALIGN);
481 memset (ptr, 0, i);
482 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
483 vring->desc = vr.desc;
484 vring->avail = vr.avail;
485 vring->used = vr.used;
486 vring->queue_id = idx;
487 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
488
489 ASSERT (vring->buffers == 0);
490 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
491 ASSERT (vring->indirect_buffers == 0);
492 vec_validate_aligned (vring->indirect_buffers, queue_size,
493 CLIB_CACHE_LINE_BYTES);
494 if (idx % 2)
495 {
496 u32 n_alloc = 0;
497 do
498 {
499 if (n_alloc < queue_size)
500 n_alloc =
501 vlib_buffer_alloc (vm, vring->indirect_buffers + n_alloc,
502 queue_size - n_alloc);
503 }
504 while (n_alloc != queue_size);
505 vif->tx_ring_sz = queue_size;
506 }
507 else
508 vif->rx_ring_sz = queue_size;
509 vring->size = queue_size;
510
511 virtio_pci_legacy_setup_queue (vm, vif, idx, ptr);
512 vring->kick_fd = -1;
513
514 return error;
515}
516
517static void
518virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
519 u64 req_features)
520{
521 /*
522 * if features are not requested
523 * default: all supported features
524 */
525 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
526 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
527 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
528 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
529 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
530 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
531
532 if (req_features == 0)
533 {
534 req_features = supported_features;
535 }
536
537 vif->features = req_features & vif->remote_features & supported_features;
538
539 if (vif->
540 remote_features & vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
541 {
542 virtio_net_config_t config;
543 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
544 sizeof (config.mtu), 10);
545 if (config.mtu < 64)
546 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
547 }
548
549 vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
550}
551
552void
553virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
554{
555 vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
556}
557
558int
559virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
560{
561 u8 status = 0;
562
563 /*
564 * Reset the device
565 */
566 status = virtio_pci_legacy_reset (vm, vif);
567
568 /*
569 * Set the Acknowledge status bit
570 */
571 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
572
573 /*
574 * Set the Driver status bit
575 */
576 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
577
578 /*
579 * Read the status and verify it
580 */
581 status = virtio_pci_legacy_get_status (vm, vif);
582 if (!
583 ((status & VIRTIO_CONFIG_STATUS_ACK)
584 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
585 return -1;
586 vif->status = status;
587
588 return 0;
589}
590
591clib_error_t *
592virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
593{
594 clib_error_t *error = 0;
595 struct virtio_pci_cap cap;
596 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0;
597 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
598
599 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
600 clib_error_return (error, "error in reading capabilty list position");
601
602 while (pos)
603 {
604 if ((error =
605 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
606 sizeof (cap))))
607 clib_error_return (error, "error in reading the capability at [%2x]",
608 pos);
609
610 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
611 {
612 u16 flags;
613 if ((error =
614 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
615 sizeof (flags))))
616 clib_error_return (error,
617 "error in reading the capability at [%2x]",
618 pos + 2);
619
620 if (flags & PCI_MSIX_ENABLE)
621 msix_enabled = VIRTIO_MSIX_ENABLED;
622 else
623 msix_enabled = VIRTIO_MSIX_DISABLED;
624 }
625
626 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
627 {
628 clib_warning ("[%2x] skipping non VNDR cap id: %2x", pos,
629 cap.cap_vndr);
630 goto next;
631 }
632
633 switch (cap.cfg_type)
634 {
635 case VIRTIO_PCI_CAP_COMMON_CFG:
636 common_cfg = 1;
637 break;
638 case VIRTIO_PCI_CAP_NOTIFY_CFG:
639 notify_base = 1;
640 break;
641 case VIRTIO_PCI_CAP_DEVICE_CFG:
642 dev_cfg = 1;
643 break;
644 case VIRTIO_PCI_CAP_ISR_CFG:
645 isr = 1;
646 break;
647 }
648 next:
649 pos = cap.cap_next;
650 }
651
652 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
653 {
654 clib_warning ("no modern virtio pci device found");
655 return error;
656 }
657
658 return clib_error_return (error, "modern virtio pci device found");
659}
660
661static clib_error_t *
662virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
663 virtio_pci_create_if_args_t * args)
664{
665 clib_error_t *error = 0;
666 u8 status = 0;
667
668 virtio_pci_read_caps (vm, vif);
669
670 if (virtio_pci_reset_device (vm, vif) < 0)
671 clib_error_return (error, "Failed to reset the device");
672
673 /*
674 * read device features and negotiate (user) requested features
675 */
676 virtio_pci_read_device_feature (vm, vif);
677 virtio_negotiate_features (vm, vif, args->features);
678
679 /*
680 * After FEATURE_OK, driver should not accept new feature bits
681 */
682 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
683 status = virtio_pci_legacy_get_status (vm, vif);
684 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
685 clib_error_return (error, "Device doesn't support requested features");
686
687 vif->status = status;
688
689 if (virtio_pci_get_mac (vm, vif))
690 {
691 f64 now = vlib_time_now (vm);
692 u32 rnd;
693 rnd = (u32) (now * 1e6);
694 rnd = random_u32 (&rnd);
695
696 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
697 vif->mac_addr[0] = 2;
698 vif->mac_addr[1] = 0xfe;
699 virtio_pci_set_mac (vm, vif);
700 }
701
702 virtio_set_net_hdr_size (vif);
703
704 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
705 goto error;
706
707 if ((error = virtio_pci_vring_init (vm, vif, 0)))
708 goto error;
709
710 if ((error = virtio_pci_vring_init (vm, vif, 1)))
711 goto error;
712
713 if (msix_enabled == VIRTIO_MSIX_ENABLED)
714 {
715 virtio_pci_legacy_set_config_irq (vm, vif, VIRTIO_MSI_NO_VECTOR);
716 virtio_pci_legacy_set_queue_irq (vm, vif, VIRTIO_MSI_NO_VECTOR, 0);
717 }
718 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
719 vif->status = virtio_pci_legacy_get_status (vm, vif);
720error:
721 return error;
722}
723
724void
725virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
726{
727 vnet_main_t *vnm = vnet_get_main ();
728 virtio_main_t *vmxm = &virtio_main;
729 virtio_if_t *vif;
730 vlib_pci_dev_handle_t h;
731 clib_error_t *error = 0;
732
733 if (args->rxq_size == 0)
734 args->rxq_size = VIRTIO_NUM_RX_DESC;
735 if (args->txq_size == 0)
736 args->txq_size = VIRTIO_NUM_TX_DESC;
737
738 if (!virtio_pci_queue_size_valid (args->rxq_size) ||
739 !virtio_pci_queue_size_valid (args->txq_size))
740 {
741 args->rv = VNET_API_ERROR_INVALID_VALUE;
742 args->error =
743 clib_error_return (error,
744 "queue size must be <= 4096, >= 64, "
745 "and multiples of 64");
746 return;
747 }
748
749 /* *INDENT-OFF* */
750 pool_foreach (vif, vmxm->interfaces, ({
751 if (vif->pci_addr.as_u32 == args->addr)
752 {
753 args->rv = VNET_API_ERROR_INVALID_VALUE;
754 args->error =
755 clib_error_return (error, "PCI address in use");
756 return;
757 }
758 }));
759 /* *INDENT-ON* */
760
761 pool_get (vmxm->interfaces, vif);
762 vif->dev_instance = vif - vmxm->interfaces;
763 vif->per_interface_next_index = ~0;
764 vif->pci_addr.as_u32 = args->addr;
765
766 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
767 {
768 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
769 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
770 goto error;
771 }
772
773 if ((error =
774 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
775 virtio_pci_device_ids, &h)))
776 {
777 pool_put (vmxm->interfaces, vif);
778 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
779 args->error =
780 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
781 &vif->pci_addr);
782 return;
783 }
784 vif->pci_dev_handle = h;
785 vlib_pci_set_private_data (vm, h, vif->dev_instance);
786
787 if ((error = vlib_pci_bus_master_enable (vm, h)))
788 goto error;
789
790 if ((error = vlib_pci_io_region (vm, h, 0)))
791 goto error;
792
793 if ((error = virtio_pci_device_init (vm, vif, args)))
794 goto error;
795
796 if (msix_enabled == VIRTIO_MSIX_ENABLED)
797 {
798 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
799 &virtio_pci_irq_0_handler)))
800 goto error;
801
802 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
803 &virtio_pci_irq_1_handler)))
804 goto error;
805
806 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
807 goto error;
808 }
809 else
810 {
811 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler);
812 }
813
814 if ((error = vlib_pci_intr_enable (vm, h)))
815 goto error;
816
817 vif->type = VIRTIO_IF_TYPE_PCI;
818 /* create interface */
819 error = ethernet_register_interface (vnm, virtio_device_class.index,
820 vif->dev_instance, vif->mac_addr,
821 &vif->hw_if_index,
822 virtio_pci_flag_change);
823
824 if (error)
825 goto error;
826
827 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
828 vif->sw_if_index = sw->sw_if_index;
829 args->sw_if_index = sw->sw_if_index;
830
831 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
832 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
833 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
834 virtio_input_node.index);
835 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
836
837 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
838 {
839 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
840 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
841 VNET_HW_INTERFACE_FLAG_LINK_UP);
842 }
843 else
844 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
845 return;
846
847error:
848 virtio_pci_delete_if (vm, vif);
849 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
850 args->error = error;
851}
852
853int
854virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
855{
856 vnet_main_t *vnm = vnet_get_main ();
857 virtio_main_t *vmxm = &virtio_main;
858 u32 i = 0;
859
860 if (vif->type != VIRTIO_IF_TYPE_PCI)
861 return VNET_API_ERROR_INVALID_INTERFACE;
862
863 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
864
865 virtio_pci_legacy_del_queue (vm, vif, 0);
866 virtio_pci_legacy_del_queue (vm, vif, 1);
867
868 virtio_pci_legacy_reset (vm, vif);
869
870 if (vif->hw_if_index)
871 {
872 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
873 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0);
874 ethernet_delete_interface (vnm, vif->hw_if_index);
875 }
876
877 vlib_pci_device_close (vm, vif->pci_dev_handle);
878
879 vec_foreach_index (i, vif->vrings)
880 {
881 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, i);
882 if (vring->kick_fd != -1)
883 close (vring->kick_fd);
884 if (vring->used)
885 {
886 if ((i & 1) == 1)
887 virtio_free_used_desc (vm, vring);
888 else
889 virtio_free_rx_buffers (vm, vring);
890 }
891 if (vring->queue_id % 2)
892 {
893 vlib_buffer_free_no_next (vm, vring->indirect_buffers, vring->size);
894 }
895 vec_free (vring->buffers);
896 vec_free (vring->indirect_buffers);
897 vlib_physmem_free (vm, vring->desc);
898 }
899
900 vec_free (vif->vrings);
901
902 if (vif->fd != -1)
903 close (vif->fd);
904 if (vif->tap_fd != -1)
905 vif->tap_fd = -1;
906 clib_error_free (vif->error);
907 memset (vif, 0, sizeof (*vif));
908 pool_put (vmxm->interfaces, vif);
909
910 return 0;
911}
912
913/*
914 * fd.io coding-style-patch-verification: ON
915 *
916 * Local Variables:
917 * eval: (c-set-style "gnu")
918 * End:
919 */