1d2699bbb36fbe6efe7b8105764877ae9295ea10
[vpp.git] / src / vnet / devices / virtio / pci.c
1 /*
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
19 #include <vppinfra/types.h>
20 #include <vlib/vlib.h>
21 #include <vlib/pci/pci.h>
22 #include <vnet/ethernet/ethernet.h>
23 #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 #include <vnet/interface/rx_queue_funcs.h>
28
29 #define PCI_VENDOR_ID_VIRTIO                            0x1af4
30 #define PCI_DEVICE_ID_VIRTIO_NIC                        0x1000
31 /* Doesn't support modern device */
32 #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN                 0x1041
33
34 #define PCI_CAPABILITY_LIST     0x34
35 #define PCI_CAP_ID_VNDR         0x09
36 #define PCI_CAP_ID_MSIX         0x11
37
38 #define PCI_MSIX_ENABLE 0x8000
39
40 static pci_device_id_t virtio_pci_device_ids[] = {
41   {
42    .vendor_id = PCI_VENDOR_ID_VIRTIO,
43    .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
44   {
45    .vendor_id = PCI_VENDOR_ID_VIRTIO,
46    .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
47   {0},
48 };
49
50 static u32
51 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
52                         u32 flags)
53 {
54   return 0;
55 }
56
57 static clib_error_t *
58 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
59 {
60   clib_error_t *error = 0;
61   u16 max_queue_pairs = 1;
62
63   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
64     {
65       max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif);
66     }
67
68   virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
69   if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
70     return clib_error_return (error, "max queue pair is %x,"
71                               " should be in range [1, 0x8000]",
72                               max_queue_pairs);
73
74   vif->max_queue_pairs = max_queue_pairs;
75   return error;
76 }
77
78 static void
79 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
80 {
81   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
82     vif->virtio_pci_func->set_mac (vm, vif);
83 }
84
85 static u32
86 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
87 {
88   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
89     {
90       vif->virtio_pci_func->get_mac (vm, vif);
91       return 0;
92     }
93   return 1;
94 }
95
96 static u16
97 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
98 {
99   /*
100    * Minimal driver: assumes link is up
101    */
102   u16 status = 1;
103   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
104     status = vif->virtio_pci_func->get_device_status (vm, vif);
105   return status;
106 }
107
108 static void
109 virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
110                               u16 line)
111 {
112   vnet_main_t *vnm = vnet_get_main ();
113   virtio_main_t *vim = &virtio_main;
114   uword pd = vlib_pci_get_private_data (vm, h);
115   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
116   line--;
117   u16 qid = line;
118
119   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
120   vnet_hw_if_rx_queue_set_int_pending (vnm, vring->queue_index);
121 }
122
123 static void
124 virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
125                                u16 line)
126 {
127   vnet_main_t *vnm = vnet_get_main ();
128   virtio_main_t *vim = &virtio_main;
129   uword pd = vlib_pci_get_private_data (vm, h);
130   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
131
132   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
133     {
134       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
135                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
136     }
137   else
138     {
139       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
140     }
141 }
142
143 static void
144 virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
145 {
146   virtio_main_t *vim = &virtio_main;
147   uword pd = vlib_pci_get_private_data (vm, h);
148   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
149   u8 isr = 0;
150   u16 line = 0;
151
152   isr = vif->virtio_pci_func->get_isr (vm, vif);
153
154   /*
155    * If the lower bit is set: look through the used rings of
156    * all virtqueues for the device, to see if any progress has
157    * been made by the device which requires servicing.
158    */
159   if (isr & VIRTIO_PCI_ISR_INTR)
160     {
161       for (; line < vif->num_rxqs; line++)
162         virtio_pci_irq_queue_handler (vm, h, (line + 1));
163     }
164
165   if (isr & VIRTIO_PCI_ISR_CONFIG)
166     virtio_pci_irq_config_handler (vm, h, line);
167 }
168
169 inline void
170 device_status (vlib_main_t * vm, virtio_if_t * vif)
171 {
172   struct status_struct
173   {
174     u8 bit;
175     char *str;
176   };
177   struct status_struct *status_entry;
178   static struct status_struct status_array[] = {
179 #define _(s,b) { .str = #s, .bit = b, },
180     foreach_virtio_config_status_flags
181 #undef _
182     {.str = NULL}
183   };
184
185   vlib_cli_output (vm, "  status 0x%x", vif->status);
186
187   status_entry = (struct status_struct *) &status_array;
188   while (status_entry->str)
189     {
190       if (vif->status & status_entry->bit)
191         vlib_cli_output (vm, "    %s (%x)", status_entry->str,
192                          status_entry->bit);
193       status_entry++;
194     }
195 }
196
197 static int
198 virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif,
199                                  virtio_ctrl_msg_t * data, u32 len)
200 {
201   virtio_vring_t *vring = vif->cxq_vring;
202   virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
203   virtio_ctrl_msg_t result;
204   u32 buffer_index;
205   vlib_buffer_t *b;
206   u16 used, next;
207   u16 sz = vring->size;
208   u16 flags = 0, first_desc_flags = 0;
209
210   used = vring->desc_in_use;
211   next = vring->desc_next;
212   vring_packed_desc_t *d = &vring->packed_desc[next];
213
214   if (vlib_buffer_alloc (vm, &buffer_index, 1))
215     b = vlib_get_buffer (vm, buffer_index);
216   else
217     return VIRTIO_NET_ERR;
218   /*
219    * current_data may not be initialized with 0 and may contain
220    * previous offset.
221    */
222   b->current_data = 0;
223   clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
224
225   first_desc_flags = VRING_DESC_F_NEXT;
226   if (vring->avail_wrap_counter)
227     {
228       first_desc_flags |= VRING_DESC_F_AVAIL;
229       first_desc_flags &= ~VRING_DESC_F_USED;
230     }
231   else
232     {
233       first_desc_flags &= ~VRING_DESC_F_AVAIL;
234       first_desc_flags |= VRING_DESC_F_USED;
235     }
236   d->addr = vlib_buffer_get_current_pa (vm, b);
237   d->len = sizeof (virtio_net_ctrl_hdr_t);
238   d->id = next;
239
240   next++;
241   if (next >= sz)
242     {
243       next = 0;
244       vring->avail_wrap_counter ^= 1;
245     }
246   used++;
247
248   d = &vring->packed_desc[next];
249   flags = VRING_DESC_F_NEXT;
250   if (vring->avail_wrap_counter)
251     {
252       flags |= VRING_DESC_F_AVAIL;
253       flags &= ~VRING_DESC_F_USED;
254     }
255   else
256     {
257       flags &= ~VRING_DESC_F_AVAIL;
258       flags |= VRING_DESC_F_USED;
259     }
260   d->addr = vlib_buffer_get_current_pa (vm, b) +
261     STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
262   d->len = len;
263   d->id = next;
264   d->flags = flags;
265
266   next++;
267   if (next >= sz)
268     {
269       next = 0;
270       vring->avail_wrap_counter ^= 1;
271     }
272   used++;
273
274   d = &vring->packed_desc[next];
275   flags = VRING_DESC_F_WRITE;
276   if (vring->avail_wrap_counter)
277     {
278       flags |= VRING_DESC_F_AVAIL;
279       flags &= ~VRING_DESC_F_USED;
280     }
281   else
282     {
283       flags &= ~VRING_DESC_F_AVAIL;
284       flags |= VRING_DESC_F_USED;
285     }
286   d->addr = vlib_buffer_get_current_pa (vm, b) +
287     STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
288   d->len = sizeof (data->status);
289   d->id = next;
290   d->flags = flags;
291
292   next++;
293   if (next >= sz)
294     {
295       next = 0;
296       vring->avail_wrap_counter ^= 1;
297     }
298   used++;
299
300   CLIB_MEMORY_STORE_BARRIER ();
301   vring->packed_desc[vring->desc_next].flags = first_desc_flags;
302   vring->desc_next = next;
303   vring->desc_in_use = used;
304   CLIB_MEMORY_BARRIER ();
305   if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
306     {
307       virtio_kick (vm, vring, vif);
308     }
309
310   u16 last = vring->last_used_idx;
311   d = &vring->packed_desc[last];
312   do
313     {
314       flags = d->flags;
315     }
316   while ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7)
317          || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15));
318
319   last += 3;
320   if (last >= vring->size)
321     {
322       last = last - vring->size;
323       vring->used_wrap_counter ^= 1;
324     }
325   vring->desc_in_use -= 3;
326   vring->last_used_idx = last;
327
328   CLIB_MEMORY_BARRIER ();
329   clib_memcpy (&result, vlib_buffer_get_current (b),
330                sizeof (virtio_ctrl_msg_t));
331   virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
332   status = result.status;
333   vlib_buffer_free (vm, &buffer_index, 1);
334   return status;
335 }
336
337 static int
338 virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif,
339                                 virtio_ctrl_msg_t * data, u32 len)
340 {
341   virtio_vring_t *vring = vif->cxq_vring;
342   virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
343   virtio_ctrl_msg_t result;
344   u32 buffer_index;
345   vlib_buffer_t *b;
346   u16 used, next, avail;
347   u16 sz = vring->size;
348   u16 mask = sz - 1;
349
350   used = vring->desc_in_use;
351   next = vring->desc_next;
352   avail = vring->avail->idx;
353   vring_desc_t *d = &vring->desc[next];
354
355   if (vlib_buffer_alloc (vm, &buffer_index, 1))
356     b = vlib_get_buffer (vm, buffer_index);
357   else
358     return VIRTIO_NET_ERR;
359   /*
360    * current_data may not be initialized with 0 and may contain
361    * previous offset.
362    */
363   b->current_data = 0;
364   clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
365   d->flags = VRING_DESC_F_NEXT;
366   d->addr = vlib_buffer_get_current_pa (vm, b);
367   d->len = sizeof (virtio_net_ctrl_hdr_t);
368   vring->avail->ring[avail & mask] = next;
369   avail++;
370   next = (next + 1) & mask;
371   d->next = next;
372   used++;
373
374   d = &vring->desc[next];
375   d->flags = VRING_DESC_F_NEXT;
376   d->addr = vlib_buffer_get_current_pa (vm, b) +
377     STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
378   d->len = len;
379   next = (next + 1) & mask;
380   d->next = next;
381   used++;
382
383   d = &vring->desc[next];
384   d->flags = VRING_DESC_F_WRITE;
385   d->addr = vlib_buffer_get_current_pa (vm, b) +
386     STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
387   d->len = sizeof (data->status);
388   next = (next + 1) & mask;
389   used++;
390
391   CLIB_MEMORY_STORE_BARRIER ();
392   vring->avail->idx = avail;
393   vring->desc_next = next;
394   vring->desc_in_use = used;
395
396   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
397     {
398       virtio_kick (vm, vring, vif);
399     }
400
401   u16 last = vring->last_used_idx, n_left = 0;
402   n_left = vring->used->idx - last;
403
404   while (n_left)
405     {
406       vring_used_elem_t *e = &vring->used->ring[last & mask];
407       u16 slot = e->id;
408
409       d = &vring->desc[slot];
410       while (d->flags & VRING_DESC_F_NEXT)
411         {
412           used--;
413           slot = d->next;
414           d = &vring->desc[slot];
415         }
416       used--;
417       last++;
418       n_left--;
419     }
420   vring->desc_in_use = used;
421   vring->last_used_idx = last;
422
423   CLIB_MEMORY_BARRIER ();
424   clib_memcpy (&result, vlib_buffer_get_current (b),
425                sizeof (virtio_ctrl_msg_t));
426   virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
427   status = result.status;
428   vlib_buffer_free (vm, &buffer_index, 1);
429   return status;
430 }
431
432 static int
433 virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
434                           virtio_ctrl_msg_t * data, u32 len)
435 {
436   if (vif->is_packed)
437     return virtio_pci_send_ctrl_msg_packed (vm, vif, data, len);
438   else
439     return virtio_pci_send_ctrl_msg_split (vm, vif, data, len);
440 }
441
442 static int
443 virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
444 {
445   virtio_ctrl_msg_t offload_hdr;
446   virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
447
448   offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
449   offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
450   offload_hdr.status = VIRTIO_NET_ERR;
451   u64 offloads = 0ULL;
452   clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
453
454   status =
455     virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
456   virtio_log_debug (vif, "disable offloads");
457   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
458   vif->virtio_pci_func->get_driver_features (vm, vif);
459   return status;
460 }
461
462 static int
463 virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
464 {
465   virtio_ctrl_msg_t csum_offload_hdr;
466   virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
467
468   csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
469   csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
470   csum_offload_hdr.status = VIRTIO_NET_ERR;
471   u64 offloads = 0ULL;
472   offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
473   clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
474
475   status =
476     virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
477   virtio_log_debug (vif, "enable checksum offload");
478   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
479   vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
480   return status;
481 }
482
483 static int
484 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
485 {
486   virtio_ctrl_msg_t gso_hdr;
487   virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
488
489   gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
490   gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
491   gso_hdr.status = VIRTIO_NET_ERR;
492   u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
493     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
494     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
495   clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
496
497   status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
498   virtio_log_debug (vif, "enable gso");
499   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
500   vif->virtio_pci_func->get_driver_features (vm, vif);
501   return status;
502 }
503
504 static int
505 virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
506                      int csum_offload_enabled)
507 {
508   vnet_main_t *vnm = vnet_get_main ();
509   vnet_hw_if_caps_change_t cc = {};
510
511   if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
512       (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
513     {
514       if (gso_enabled
515           && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
516                                VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
517         {
518           if (virtio_pci_enable_gso (vm, vif))
519             {
520               virtio_log_warning (vif, "gso is not enabled");
521             }
522           else
523             {
524               vif->gso_enabled = 1;
525               vif->csum_offload_enabled = 1;
526               cc.val = cc.mask = VNET_HW_IF_CAP_TCP_GSO |
527                                  VNET_HW_IF_CAP_TX_TCP_CKSUM |
528                                  VNET_HW_IF_CAP_TX_UDP_CKSUM;
529             }
530         }
531       else if (csum_offload_enabled
532                && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
533         {
534           if (virtio_pci_enable_checksum_offload (vm, vif))
535             {
536               virtio_log_warning (vif, "checksum offload is not enabled");
537             }
538           else
539             {
540               vif->csum_offload_enabled = 1;
541               vif->gso_enabled = 0;
542               cc.val =
543                 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
544               cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
545                         VNET_HW_IF_CAP_TX_UDP_CKSUM;
546             }
547         }
548       else
549         {
550           if (virtio_pci_disable_offload (vm, vif))
551             {
552               virtio_log_warning (vif, "offloads are not disabled");
553             }
554           else
555             {
556               vif->csum_offload_enabled = 0;
557               vif->gso_enabled = 0;
558               cc.val = 0;
559               cc.mask = VNET_HW_IF_CAP_L4_TX_CKSUM | VNET_HW_IF_CAP_TCP_GSO;
560             }
561         }
562     }
563
564   if (cc.mask)
565     vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
566
567   return 0;
568 }
569
570 static int
571 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
572                               u16 num_queues)
573 {
574   virtio_ctrl_msg_t mq_hdr;
575   virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
576
577   mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
578   mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
579   mq_hdr.status = VIRTIO_NET_ERR;
580   clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
581
582   status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
583   virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
584   return status;
585 }
586
587 static u8
588 virtio_pci_queue_size_valid (u16 qsz)
589 {
590   if (qsz < 64 || qsz > 4096)
591     return 0;
592   if ((qsz % 64) != 0)
593     return 0;
594   return 1;
595 }
596
597 clib_error_t *
598 virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
599                                       u16 queue_num)
600 {
601   clib_error_t *error = 0;
602   u16 queue_size = 0;
603   virtio_vring_t *vring;
604   u32 i = 0;
605   void *ptr = NULL;
606
607   queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
608
609   if (queue_size > 32768)
610     return clib_error_return (0, "ring size must be 32768 or lower");
611
612   if (queue_size == 0)
613     queue_size = 256;
614
615   vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
616   vring = vec_elt_at_index (vif->cxq_vring, 0);
617
618   i =
619     (((queue_size * sizeof (vring_packed_desc_t)) +
620       sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
621       1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
622
623   ptr =
624     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
625                                         vif->numa_node);
626   if (!ptr)
627     return vlib_physmem_last_error (vm);
628   clib_memset (ptr, 0, i);
629
630   vring->packed_desc = ptr;
631
632   vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
633   vring->driver_event->off_wrap = 0;
634   vring->driver_event->flags = VRING_EVENT_F_DISABLE;
635
636   vring->device_event =
637     ptr +
638     (((queue_size * sizeof (vring_packed_desc_t)) +
639       sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
640       1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
641   vring->device_event->off_wrap = 0;
642   vring->device_event->flags = 0;
643
644   vring->queue_id = queue_num;
645   vring->size = queue_size;
646   vring->avail_wrap_counter = 1;
647   vring->used_wrap_counter = 1;
648
649   ASSERT (vring->buffers == 0);
650
651   virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
652                     queue_size);
653   vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring);
654   vring->queue_notify_offset =
655     vif->notify_off_multiplier *
656     vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
657   virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
658                     queue_num, vring->queue_notify_offset);
659   return error;
660 }
661
662 clib_error_t *
663 virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
664                                      u16 queue_num)
665 {
666   clib_error_t *error = 0;
667   u16 queue_size = 0;
668   virtio_vring_t *vring;
669   vring_t vr;
670   u32 i = 0;
671   void *ptr = NULL;
672
673   queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
674   if (!virtio_pci_queue_size_valid (queue_size))
675     clib_warning ("queue size is not valid");
676
677   if (!is_pow2 (queue_size))
678     return clib_error_return (0, "ring size must be power of 2");
679
680   if (queue_size > 32768)
681     return clib_error_return (0, "ring size must be 32768 or lower");
682
683   if (queue_size == 0)
684     queue_size = 256;
685
686   vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
687   vring = vec_elt_at_index (vif->cxq_vring, 0);
688   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
689   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
690   ptr =
691     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
692                                         vif->numa_node);
693   if (!ptr)
694     return vlib_physmem_last_error (vm);
695   clib_memset (ptr, 0, i);
696   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
697   vring->desc = vr.desc;
698   vring->avail = vr.avail;
699   vring->used = vr.used;
700   vring->queue_id = queue_num;
701   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
702
703   ASSERT (vring->buffers == 0);
704
705   vring->size = queue_size;
706   virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
707                     queue_size);
708   vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
709   vring->queue_notify_offset =
710     vif->notify_off_multiplier *
711     vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
712   virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
713                     queue_num, vring->queue_notify_offset);
714
715   return error;
716 }
717
718 clib_error_t *
719 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
720                                u16 queue_num)
721 {
722   if (vif->is_packed)
723     return virtio_pci_control_vring_packed_init (vm, vif, queue_num);
724   else
725     return virtio_pci_control_vring_split_init (vm, vif, queue_num);
726 }
727
728 clib_error_t *
729 virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
730                              u16 queue_num)
731 {
732   clib_error_t *error = 0;
733   u16 queue_size = 0;
734   virtio_vring_t *vring;
735   vring_t vr;
736   u32 i = 0;
737   void *ptr = NULL;
738
739   queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
740   if (!virtio_pci_queue_size_valid (queue_size))
741     clib_warning ("queue size is not valid");
742
743   if (!is_pow2 (queue_size))
744     return clib_error_return (0, "ring size must be power of 2");
745
746   if (queue_size > 32768)
747     return clib_error_return (0, "ring size must be 32768 or lower");
748
749   if (queue_size == 0)
750     queue_size = 256;
751
752   if (queue_num % 2)
753     {
754       vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
755                             CLIB_CACHE_LINE_BYTES);
756       vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
757       clib_spinlock_init (&vring->lockp);
758     }
759   else
760     {
761       vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
762                             CLIB_CACHE_LINE_BYTES);
763       vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
764     }
765   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
766   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
767   ptr =
768     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
769                                         vif->numa_node);
770   if (!ptr)
771     return vlib_physmem_last_error (vm);
772   clib_memset (ptr, 0, i);
773   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
774   vring->desc = vr.desc;
775   vring->avail = vr.avail;
776   vring->used = vr.used;
777   vring->queue_id = queue_num;
778   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
779   vring->flow_table = 0;
780
781   ASSERT (vring->buffers == 0);
782   vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
783   if (queue_num % 2)
784     {
785       virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
786                         queue_size);
787       clib_memset_u32 (vring->buffers, ~0, queue_size);
788     }
789   else
790     {
791       virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
792                         queue_size);
793     }
794   vring->size = queue_size;
795   if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
796     return clib_error_return (0, "error in queue address setup");
797
798   vring->queue_notify_offset =
799     vif->notify_off_multiplier *
800     vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
801   virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
802                     queue_num, vring->queue_notify_offset);
803   return error;
804 }
805
806 clib_error_t *
807 virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
808                               u16 queue_num)
809 {
810   clib_error_t *error = 0;
811   u16 queue_size = 0;
812   virtio_vring_t *vring;
813   u32 i = 0;
814   void *ptr = NULL;
815
816   queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
817
818   if (queue_size > 32768)
819     return clib_error_return (0, "ring size must be 32768 or lower");
820
821   if (queue_size == 0)
822     queue_size = 256;
823
824   if (queue_num % 2)
825     {
826       vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
827                             CLIB_CACHE_LINE_BYTES);
828       vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
829       clib_spinlock_init (&vring->lockp);
830     }
831   else
832     {
833       vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
834                             CLIB_CACHE_LINE_BYTES);
835       vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
836     }
837
838   i =
839     (((queue_size * sizeof (vring_packed_desc_t)) +
840       sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
841       1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
842
843   ptr =
844     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
845                                         vif->numa_node);
846   if (!ptr)
847     return vlib_physmem_last_error (vm);
848
849   clib_memset (ptr, 0, i);
850   vring->packed_desc = ptr;
851
852   vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
853   vring->driver_event->off_wrap = 0;
854   vring->driver_event->flags = VRING_EVENT_F_DISABLE;
855
856   vring->device_event =
857     ptr +
858     (((queue_size * sizeof (vring_packed_desc_t)) +
859       sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
860       1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
861   vring->device_event->off_wrap = 0;
862   vring->device_event->flags = 0;
863
864   vring->queue_id = queue_num;
865
866   vring->avail_wrap_counter = 1;
867   vring->used_wrap_counter = 1;
868
869   ASSERT (vring->buffers == 0);
870   vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
871   if (queue_num % 2)
872     {
873       virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
874                         queue_size);
875       clib_memset_u32 (vring->buffers, ~0, queue_size);
876     }
877   else
878     {
879       virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
880                         queue_size);
881     }
882   vring->size = queue_size;
883   if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring))
884     return clib_error_return (0, "error in queue address setup");
885
886   vring->queue_notify_offset =
887     vif->notify_off_multiplier *
888     vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
889   virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
890                     queue_num, vring->queue_notify_offset);
891
892   return error;
893 }
894
895 clib_error_t *
896 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
897 {
898   if (vif->is_packed)
899     return virtio_pci_vring_packed_init (vm, vif, queue_num);
900   else
901     return virtio_pci_vring_split_init (vm, vif, queue_num);
902 }
903
904 static void
905 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
906                            u64 req_features)
907 {
908   /*
909    * if features are not requested
910    * default: all supported features
911    */
912   u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
913     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
914     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
915     | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
916     | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
917     | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
918     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
919     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
920     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
921     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
922     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
923     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
924     | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
925     | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
926     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
927     | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
928     | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
929     | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
930     | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
931
932   if (vif->is_modern)
933     supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
934
935   if (vif->is_packed)
936     {
937       supported_features |=
938         (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) |
939          VIRTIO_FEATURE (VIRTIO_F_IN_ORDER));
940     }
941
942   if (req_features == 0)
943     {
944       req_features = supported_features;
945     }
946
947   vif->features = req_features & vif->remote_features & supported_features;
948
949   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
950     {
951       u16 mtu = 0;
952       mtu = vif->virtio_pci_func->get_mtu (vm, vif);
953
954       if (mtu < 64)
955         vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
956     }
957
958   if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0)
959     vif->is_packed = 0;
960
961   vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
962   vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
963 }
964
965 void
966 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
967 {
968   vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
969 }
970
971 int
972 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
973 {
974   u8 status = 0;
975
976   /*
977    * Reset the device
978    */
979   status = vif->virtio_pci_func->device_reset (vm, vif);
980
981   /*
982    * Set the Acknowledge status bit
983    */
984   vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
985
986   /*
987    * Set the Driver status bit
988    */
989   vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
990
991   /*
992    * Read the status and verify it
993    */
994   status = vif->virtio_pci_func->get_status (vm, vif);
995   if ((status & VIRTIO_CONFIG_STATUS_ACK)
996       && (status & VIRTIO_CONFIG_STATUS_DRIVER))
997     vif->status = status;
998   else
999     return -1;
1000
1001   return 0;
1002 }
1003
1004 clib_error_t *
1005 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
1006 {
1007   clib_error_t *error = 0;
1008   virtio_pci_cap_t cap;
1009   u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
1010   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
1011
1012   if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
1013     {
1014       virtio_log_error (vif, "error in reading capabilty list position");
1015       return clib_error_return (error,
1016                                 "error in reading capabilty list position");
1017     }
1018   while (pos)
1019     {
1020       if ((error =
1021            vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
1022                                        sizeof (cap))))
1023         {
1024           virtio_log_error (vif, "%s [%2x]",
1025                             "error in reading the capability at", pos);
1026           return clib_error_return (error,
1027                                     "error in reading the capability at [%2x]",
1028                                     pos);
1029         }
1030
1031       if (cap.cap_vndr == PCI_CAP_ID_MSIX)
1032         {
1033           u16 flags, table_size, table_size_mask = 0x07FF;
1034
1035           if ((error =
1036                vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
1037                                            sizeof (flags))))
1038             return clib_error_return (error,
1039                                       "error in reading the capability at [%2x]",
1040                                       pos + 2);
1041
1042           table_size = flags & table_size_mask;
1043           virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
1044                             "msix interrupt vector table-size", table_size);
1045
1046           if (flags & PCI_MSIX_ENABLE)
1047             {
1048               virtio_log_debug (vif, "msix interrupt enabled");
1049               vif->msix_enabled = VIRTIO_MSIX_ENABLED;
1050               vif->msix_table_size = table_size;
1051             }
1052           else
1053             {
1054               virtio_log_debug (vif, "msix interrupt disabled");
1055               vif->msix_enabled = VIRTIO_MSIX_DISABLED;
1056               vif->msix_table_size = 0;
1057             }
1058         }
1059
1060       if (cap.cap_vndr != PCI_CAP_ID_VNDR)
1061         {
1062           virtio_log_debug (vif, "[%2x] %s %2x ", pos,
1063                             "skipping non VNDR cap id:", cap.cap_vndr);
1064           goto next;
1065         }
1066
1067       virtio_log_debug (vif,
1068                         "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
1069                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
1070
1071       if (cap.bar >= 0 && cap.bar <= 5)
1072         {
1073           vif->bar = bar[cap.bar];
1074           vif->bar_id = cap.bar;
1075         }
1076       else
1077         return clib_error_return (error, "invalid bar %u", cap.bar);
1078
1079       switch (cap.cfg_type)
1080         {
1081         case VIRTIO_PCI_CAP_COMMON_CFG:
1082           vif->common_offset = cap.offset;
1083           common_cfg = 1;
1084           break;
1085         case VIRTIO_PCI_CAP_NOTIFY_CFG:
1086           if ((error =
1087                vlib_pci_read_write_config (vm, h, VLIB_READ,
1088                                            pos + sizeof (cap),
1089                                            &vif->notify_off_multiplier,
1090                                            sizeof
1091                                            (vif->notify_off_multiplier))))
1092             {
1093               virtio_log_error (vif, "notify off multiplier is not given");
1094             }
1095           else
1096             {
1097               virtio_log_debug (vif, "notify off multiplier is %u",
1098                                 vif->notify_off_multiplier);
1099               vif->notify_offset = cap.offset;
1100               notify = 1;
1101             }
1102           break;
1103         case VIRTIO_PCI_CAP_DEVICE_CFG:
1104           vif->device_offset = cap.offset;
1105           dev_cfg = 1;
1106           break;
1107         case VIRTIO_PCI_CAP_ISR_CFG:
1108           vif->isr_offset = cap.offset;
1109           isr = 1;
1110           break;
1111         case VIRTIO_PCI_CAP_PCI_CFG:
1112           if (cap.bar == 0)
1113             pci_cfg = 1;
1114           break;
1115         }
1116     next:
1117       pos = cap.cap_next;
1118     }
1119
1120   if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
1121     {
1122       vif->virtio_pci_func = &virtio_pci_legacy_func;
1123       vif->notify_off_multiplier = 0;
1124       virtio_log_debug (vif, "legacy virtio pci device found");
1125       return error;
1126     }
1127
1128   vif->is_modern = 1;
1129   vif->virtio_pci_func = &virtio_pci_modern_func;
1130
1131   if (!pci_cfg)
1132     {
1133       virtio_log_debug (vif, "modern virtio pci device found");
1134     }
1135   else
1136     {
1137       virtio_log_debug (vif, "transitional virtio pci device found");
1138     }
1139
1140   return error;
1141 }
1142
1143 static clib_error_t *
1144 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
1145                         virtio_pci_create_if_args_t * args, void **bar)
1146 {
1147   clib_error_t *error = 0;
1148   u8 status = 0;
1149
1150   if ((error = virtio_pci_read_caps (vm, vif, bar)))
1151     {
1152       args->rv = VNET_API_ERROR_UNSUPPORTED;
1153       virtio_log_error (vif, "Device is not supported");
1154       return clib_error_return (error, "Device is not supported");
1155     }
1156
1157   if (virtio_pci_reset_device (vm, vif) < 0)
1158     {
1159       args->rv = VNET_API_ERROR_INIT_FAILED;
1160       virtio_log_error (vif, "Failed to reset the device");
1161       return clib_error_return (error, "Failed to reset the device");
1162     }
1163   /*
1164    * read device features and negotiate (user) requested features
1165    */
1166   virtio_pci_read_device_feature (vm, vif);
1167   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1168       0)
1169     {
1170       virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1171                           "support VIRTIO_RING_F_INDIRECT_DESC features");
1172     }
1173   if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1174     {
1175       virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1176                           "support VIRTIO_NET_F_MRG_RXBUF features");
1177     }
1178   virtio_negotiate_features (vm, vif, args->features);
1179
1180   /*
1181    * After FEATURE_OK, driver should not accept new feature bits
1182    */
1183   vif->virtio_pci_func->set_status (vm, vif,
1184                                     VIRTIO_CONFIG_STATUS_FEATURES_OK);
1185   status = vif->virtio_pci_func->get_status (vm, vif);
1186   if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
1187     {
1188       args->rv = VNET_API_ERROR_UNSUPPORTED;
1189       virtio_log_error (vif,
1190                         "error encountered: Device doesn't support requested features");
1191       return clib_error_return (error,
1192                                 "Device doesn't support requested features");
1193     }
1194   vif->status = status;
1195
1196   /*
1197    * get or set the mac address
1198    */
1199   if (virtio_pci_get_mac (vm, vif))
1200     {
1201       f64 now = vlib_time_now (vm);
1202       u32 rnd;
1203       rnd = (u32) (now * 1e6);
1204       rnd = random_u32 (&rnd);
1205
1206       memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1207       vif->mac_addr[0] = 2;
1208       vif->mac_addr[1] = 0xfe;
1209       virtio_pci_set_mac (vm, vif);
1210     }
1211
1212   virtio_set_net_hdr_size (vif);
1213
1214   /*
1215    * Initialize the virtqueues
1216    */
1217   if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
1218     {
1219       args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1220       goto err;
1221     }
1222
1223   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1224     {
1225       if (vif->msix_table_size <= vif->max_queue_pairs)
1226         {
1227           virtio_log_error (vif,
1228                             "error MSIX lines (%u) <= Number of RXQs (%u)",
1229                             vif->msix_table_size, vif->max_queue_pairs);
1230           return clib_error_return (error,
1231                                     "error MSIX lines (%u) <= Number of RXQs (%u)",
1232                                     vif->msix_table_size,
1233                                     vif->max_queue_pairs);
1234         }
1235     }
1236
1237   for (int i = 0; i < vif->max_queue_pairs; i++)
1238     {
1239       if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
1240         {
1241           args->rv = VNET_API_ERROR_INIT_FAILED;
1242           virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1243                             RX_QUEUE (i), "initialization");
1244           error =
1245             clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1246                                RX_QUEUE (i), "initialization");
1247           goto err;
1248         }
1249       else
1250         {
1251           vif->num_rxqs++;
1252         }
1253
1254       if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
1255         {
1256           args->rv = VNET_API_ERROR_INIT_FAILED;
1257           virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1258                             TX_QUEUE (i), "initialization");
1259           error =
1260             clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1261                                TX_QUEUE (i), "initialization");
1262           goto err;
1263         }
1264       else
1265         {
1266           vif->num_txqs++;
1267         }
1268     }
1269
1270   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1271     {
1272       if ((error =
1273            virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1274         {
1275           virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
1276                               vif->max_queue_pairs * 2, "initialization");
1277           if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1278             vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1279         }
1280     }
1281   else
1282     {
1283       virtio_log_debug (vif, "control queue is not available");
1284       vif->cxq_vring = NULL;
1285     }
1286
1287   /*
1288    * set the msix interrupts
1289    */
1290   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1291     {
1292       int i, j;
1293       if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
1294           VIRTIO_MSI_NO_VECTOR)
1295         {
1296           virtio_log_warning (vif, "config vector 0 is not set");
1297         }
1298       else
1299         {
1300           virtio_log_debug (vif, "config msix vector is set at 0");
1301         }
1302       for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
1303         {
1304           if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
1305                                                    RX_QUEUE (i)) ==
1306               VIRTIO_MSI_NO_VECTOR)
1307             {
1308               virtio_log_warning (vif, "queue (%u) vector is not set at %u",
1309                                   RX_QUEUE (i), j);
1310             }
1311           else
1312             {
1313               virtio_log_debug (vif, "%s (%u) %s %u", "queue",
1314                                 RX_QUEUE (i), "msix vector is set at", j);
1315             }
1316         }
1317     }
1318
1319   /*
1320    * set the driver status OK
1321    */
1322   vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1323   vif->status = vif->virtio_pci_func->get_status (vm, vif);
1324 err:
1325   return error;
1326 }
1327
1328 void
1329 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1330 {
1331   vnet_main_t *vnm = vnet_get_main ();
1332   virtio_main_t *vim = &virtio_main;
1333   virtio_if_t *vif;
1334   vlib_pci_dev_handle_t h;
1335   clib_error_t *error = 0;
1336   u32 interrupt_count = 0;
1337
1338   /* *INDENT-OFF* */
1339   pool_foreach (vif, vim->interfaces)  {
1340     if (vif->pci_addr.as_u32 == args->addr)
1341       {
1342         args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
1343         args->error =
1344           clib_error_return (error, "PCI address in use");
1345           vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1346                 format_vlib_pci_addr, &args->addr,
1347                 " PCI address in use");
1348         return;
1349       }
1350   }
1351   /* *INDENT-ON* */
1352
1353   pool_get (vim->interfaces, vif);
1354   vif->dev_instance = vif - vim->interfaces;
1355   vif->per_interface_next_index = ~0;
1356   vif->pci_addr.as_u32 = args->addr;
1357   if (args->virtio_flags & VIRTIO_FLAG_PACKED)
1358     vif->is_packed = 1;
1359
1360   if ((error =
1361        vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1362                              virtio_pci_device_ids, &h)))
1363     {
1364       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1365       args->error =
1366         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1367                            &vif->pci_addr);
1368       vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1369                 format_vlib_pci_addr, &vif->pci_addr,
1370                 "error encountered on pci device open");
1371       pool_put (vim->interfaces, vif);
1372       return;
1373     }
1374   vif->pci_dev_handle = h;
1375   vlib_pci_set_private_data (vm, h, vif->dev_instance);
1376   vif->numa_node = vlib_pci_get_numa_node (vm, h);
1377   vif->type = VIRTIO_IF_TYPE_PCI;
1378
1379   if ((error = vlib_pci_bus_master_enable (vm, h)))
1380     {
1381       virtio_log_error (vif, "error encountered on pci bus master enable");
1382       goto error;
1383     }
1384
1385   void *bar[6];
1386   for (u32 i = 0; i <= 5; i++)
1387     {
1388
1389       if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1390         {
1391           virtio_log_debug (vif, "no pci map region for bar %u", i);
1392         }
1393       else
1394         {
1395           virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1396                             bar[i]);
1397         }
1398     }
1399
1400   if ((error = vlib_pci_io_region (vm, h, 0)))
1401     {
1402       virtio_log_error (vif, "error encountered on pci io region");
1403       goto error;
1404     }
1405
1406   interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1407   if (interrupt_count > 1)
1408     {
1409       if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1410                                                    &virtio_pci_irq_config_handler)))
1411         {
1412           args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1413           virtio_log_error (vif,
1414                             "error encountered on pci register msix handler 0");
1415           goto error;
1416         }
1417
1418       if ((error =
1419            vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1420                                            &virtio_pci_irq_queue_handler)))
1421         {
1422           args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1423           virtio_log_error (vif,
1424                             "error encountered on pci register msix handler 1");
1425           goto error;
1426         }
1427
1428       if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
1429         {
1430           virtio_log_error (vif, "error encountered on pci enable msix irq");
1431           goto error;
1432         }
1433       vif->support_int_mode = 1;
1434       virtio_log_debug (vif, "device supports msix interrupts");
1435     }
1436   else
1437     {
1438       /*
1439        * WARN: performance will be sub-optimal.
1440        * Fall back to intX, if msix table-size is 1 or
1441        * if UIO driver is being used.
1442        */
1443       if ((error =
1444            vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1445         {
1446           virtio_log_error (vif,
1447                             "error encountered on pci register interrupt handler");
1448           goto error;
1449         }
1450       vif->support_int_mode = 1;
1451       virtio_log_debug (vif, "pci register interrupt handler");
1452     }
1453
1454   if ((error = vlib_pci_intr_enable (vm, h)))
1455     {
1456       virtio_log_error (vif, "error encountered on pci interrupt enable");
1457       goto error;
1458     }
1459
1460   if ((error = virtio_pci_device_init (vm, vif, args, bar)))
1461     {
1462       virtio_log_error (vif, "error encountered on device init");
1463       goto error;
1464     }
1465
1466   /* create interface */
1467   vnet_eth_interface_registration_t eir = {};
1468   eir.dev_class_index = virtio_device_class.index;
1469   eir.dev_instance = vif->dev_instance;
1470   eir.address = vif->mac_addr;
1471   eir.cb.flag_change = virtio_pci_flag_change;
1472   vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
1473
1474   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1475   vif->sw_if_index = sw->sw_if_index;
1476   args->sw_if_index = sw->sw_if_index;
1477
1478   vnet_hw_if_set_caps (vnm, vif->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
1479
1480   if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1481     {
1482       error = virtio_set_packet_buffering (vif, args->buffering_size);
1483       if (error)
1484         {
1485           args->rv = VNET_API_ERROR_INIT_FAILED;
1486           virtio_log_error (vif,
1487                             "error encountered during packet buffering init");
1488           goto error;
1489         }
1490     }
1491
1492   virtio_pre_input_node_enable (vm, vif);
1493   virtio_vring_set_rx_queues (vm, vif);
1494   virtio_vring_set_tx_queues (vm, vif);
1495
1496   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1497     {
1498       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1499                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1500     }
1501   else
1502     vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1503
1504   virtio_pci_offloads (vm, vif, args->gso_enabled,
1505                        args->checksum_offload_enabled);
1506
1507   if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1508       (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
1509     {
1510       if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1511         virtio_log_warning (vif, "multiqueue is not set");
1512     }
1513   return;
1514
1515 error:
1516   virtio_pci_delete_if (vm, vif);
1517   if (args->rv == 0)
1518     args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1519   args->error = error;
1520 }
1521
1522 int
1523 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1524 {
1525   vnet_main_t *vnm = vnet_get_main ();
1526   virtio_main_t *vim = &virtio_main;
1527   u32 i = 0;
1528
1529   if (vif->type != VIRTIO_IF_TYPE_PCI)
1530     return VNET_API_ERROR_INVALID_INTERFACE;
1531
1532   vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1533
1534   for (i = 0; i < vif->max_queue_pairs; i++)
1535     {
1536       vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1537       vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
1538     }
1539
1540   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1541     vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
1542
1543   if (vif->virtio_pci_func)
1544     vif->virtio_pci_func->device_reset (vm, vif);
1545
1546   if (vif->hw_if_index)
1547     {
1548       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1549       ethernet_delete_interface (vnm, vif->hw_if_index);
1550     }
1551
1552   vlib_pci_device_close (vm, vif->pci_dev_handle);
1553
1554   vec_foreach_index (i, vif->rxq_vrings)
1555   {
1556     virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1557     if (vring->used)
1558       {
1559         virtio_free_buffers (vm, vring);
1560       }
1561     vec_free (vring->buffers);
1562     vlib_physmem_free (vm, vring->desc);
1563   }
1564
1565   virtio_pre_input_node_disable (vm, vif);
1566
1567   vec_foreach_index (i, vif->txq_vrings)
1568   {
1569     virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1570     if (vring->used)
1571       {
1572         virtio_free_buffers (vm, vring);
1573       }
1574     vec_free (vring->buffers);
1575     gro_flow_table_free (vring->flow_table);
1576     virtio_vring_buffering_free (vm, vring->buffering);
1577     clib_spinlock_free (&vring->lockp);
1578     vlib_physmem_free (vm, vring->desc);
1579   }
1580
1581   if (vif->cxq_vring != NULL)
1582     {
1583       u16 last = vif->cxq_vring->last_used_idx;
1584       u16 n_left = vif->cxq_vring->used->idx - last;
1585       while (n_left)
1586         {
1587           last++;
1588           n_left--;
1589         }
1590
1591       vif->cxq_vring->last_used_idx = last;
1592       vlib_physmem_free (vm, vif->cxq_vring->desc);
1593     }
1594
1595   vec_free (vif->rxq_vrings);
1596   vec_free (vif->txq_vrings);
1597   vec_free (vif->cxq_vring);
1598
1599   clib_error_free (vif->error);
1600   memset (vif, 0, sizeof (*vif));
1601   pool_put (vim->interfaces, vif);
1602
1603   return 0;
1604 }
1605
1606 int
1607 virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1608                                     int gso_enabled,
1609                                     int checksum_offload_enabled,
1610                                     int offloads_disabled)
1611 {
1612   if (vif->type != VIRTIO_IF_TYPE_PCI)
1613     return VNET_API_ERROR_INVALID_INTERFACE;
1614
1615   if (gso_enabled)
1616     virtio_pci_offloads (vm, vif, 1, 0);
1617   else if (checksum_offload_enabled)
1618     virtio_pci_offloads (vm, vif, 0, 1);
1619   else if (offloads_disabled)
1620     virtio_pci_offloads (vm, vif, 0, 0);
1621
1622   return 0;
1623 }
1624
1625 /*
1626  * fd.io coding-style-patch-verification: ON
1627  *
1628  * Local Variables:
1629  * eval: (c-set-style "gnu")
1630  * End:
1631  */