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