virtio: refactor virtio-pci logging
[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
28 #define PCI_VENDOR_ID_VIRTIO                            0x1af4
29 #define PCI_DEVICE_ID_VIRTIO_NIC                        0x1000
30 /* Doesn't support modern device */
31 #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN                 0x1041
32
33 #define PCI_CAPABILITY_LIST     0x34
34 #define PCI_CAP_ID_VNDR         0x09
35 #define PCI_CAP_ID_MSIX         0x11
36
37 #define PCI_MSIX_ENABLE 0x8000
38
39 #define PCI_CONFIG_SIZE(vif) ((vif->msix_enabled == VIRTIO_MSIX_ENABLED) ? \
40   24 : 20)
41
42 static pci_device_id_t virtio_pci_device_ids[] = {
43   {
44    .vendor_id = PCI_VENDOR_ID_VIRTIO,
45    .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
46   {
47    .vendor_id = PCI_VENDOR_ID_VIRTIO,
48    .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
49   {0},
50 };
51
52 static void
53 virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst,
54                                int len, u32 addr)
55 {
56   u32 size = 0;
57   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
58
59   while (len > 0)
60     {
61       if (len >= 4)
62         {
63           size = 4;
64           vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
65         }
66       else if (len >= 2)
67         {
68           size = 2;
69           vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
70         }
71       else
72         {
73           size = 1;
74           vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
75         }
76       dst = (u8 *) dst + size;
77       addr += size;
78       len -= size;
79     }
80 }
81
82 static void
83 virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif,
84                                 void *src, int len, u32 addr)
85 {
86   u32 size = 0;
87   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
88
89   while (len > 0)
90     {
91       if (len >= 4)
92         {
93           size = 4;
94           vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
95         }
96       else if (len >= 2)
97         {
98           size = 2;
99           vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
100         }
101       else
102         {
103           size = 1;
104           vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
105         }
106       src = (u8 *) src + size;
107       addr += size;
108       len -= size;
109     }
110 }
111
112 static u64
113 virtio_pci_legacy_get_features (vlib_main_t * vm, virtio_if_t * vif)
114 {
115   u32 features;
116   vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
117                         &features);
118   return features;
119 }
120
121 static u32
122 virtio_pci_legacy_set_features (vlib_main_t * vm, virtio_if_t * vif,
123                                 u64 features)
124 {
125   if ((features >> 32) != 0)
126     {
127       clib_warning ("only 32 bit features are allowed for legacy virtio!");
128     }
129   u32 feature = 0, guest_features = (u32) features;
130   vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
131                          &guest_features);
132   vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
133                         &feature);
134   return feature;
135 }
136
137 static u8
138 virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif)
139 {
140   u8 status = 0;
141   vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
142   return status;
143 }
144
145 static void
146 virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status)
147 {
148   if (status != VIRTIO_CONFIG_STATUS_RESET)
149     status |= virtio_pci_legacy_get_status (vm, vif);
150   vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
151 }
152
153 static u8
154 virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif)
155 {
156   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET);
157   return virtio_pci_legacy_get_status (vm, vif);
158 }
159
160 static u8
161 virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif)
162 {
163   u8 isr = 0;
164   vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr);
165   return isr;
166 }
167
168 static u16
169 virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif,
170                                  u16 queue_id)
171 {
172   u16 queue_num = 0;
173   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
174                          &queue_id);
175   vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
176                         &queue_num);
177   return queue_num;
178 }
179
180 static int
181 virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
182                                u16 queue_id, void *p)
183 {
184   u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
185   u32 addr2 = 0;
186   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
187                          &queue_id);
188   vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
189                          (u32 *) & addr);
190   vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
191                         &addr2);
192   if ((u32) addr == addr2)
193     return 0;
194   return 1;
195 }
196
197 static void
198 virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
199                              u16 queue_id)
200 {
201   u32 src = 0;
202   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
203                          &queue_id);
204   vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
205 }
206
207 inline void
208 virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
209                                 u16 queue_id)
210 {
211   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
212                          &queue_id);
213 }
214
215 /* Enable one vector (0) for Link State Intrerrupt */
216 static u16
217 virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
218                                   u16 vec)
219 {
220   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
221                          &vec);
222   vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
223                         &vec);
224   return vec;
225 }
226
227 static u16
228 virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
229                                  u16 queue_id)
230 {
231   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
232                          &queue_id);
233   vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
234                          &vec);
235   vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
236                         &vec);
237   return vec;
238 }
239
240 static u32
241 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
242                         u32 flags)
243 {
244   return 0;
245 }
246
247 static clib_error_t *
248 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
249 {
250   virtio_net_config_t config;
251   clib_error_t *error = 0;
252   u16 max_queue_pairs = 1;
253
254   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
255     {
256       virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
257                                      sizeof (config.max_virtqueue_pairs),
258                                      STRUCT_OFFSET_OF (virtio_net_config_t,
259                                                        max_virtqueue_pairs));
260       max_queue_pairs = config.max_virtqueue_pairs;
261     }
262
263   virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
264   if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
265     return clib_error_return (error, "max queue pair is %x", max_queue_pairs);
266
267   vif->max_queue_pairs = max_queue_pairs;
268   return error;
269 }
270
271 static void
272 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
273 {
274   virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
275                                   sizeof (vif->mac_addr), 0);
276 }
277
278 static u32
279 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
280 {
281   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
282     {
283       virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
284                                      sizeof (vif->mac_addr), 0);
285       return 0;
286     }
287   return 1;
288 }
289
290 static u16
291 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
292 {
293   /*
294    * Minimal driver: assumes link is up
295    */
296   u16 status = 1;
297   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
298     virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status),   /* mac */
299                                    STRUCT_OFFSET_OF (virtio_net_config_t,
300                                                      status));
301   return status;
302 }
303
304 static void
305 virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
306 {
307   vnet_main_t *vnm = vnet_get_main ();
308   virtio_main_t *vim = &virtio_main;
309   uword pd = vlib_pci_get_private_data (vm, h);
310   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
311   u16 qid = line;
312
313   vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
314 }
315
316 static void
317 virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
318 {
319   vnet_main_t *vnm = vnet_get_main ();
320   virtio_main_t *vim = &virtio_main;
321   uword pd = vlib_pci_get_private_data (vm, h);
322   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
323
324   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
325     {
326       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
327       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
328                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
329     }
330   else
331     {
332       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
333       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
334     }
335 }
336
337 static void
338 virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
339 {
340   virtio_main_t *vim = &virtio_main;
341   uword pd = vlib_pci_get_private_data (vm, h);
342   virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
343   u8 isr = 0;
344   u16 line = 0;
345
346   isr = virtio_pci_legacy_get_isr (vm, vif);
347
348   /*
349    * If the lower bit is set: look through the used rings of
350    * all virtqueues for the device, to see if any progress has
351    * been made by the device which requires servicing.
352    */
353   if (isr & VIRTIO_PCI_ISR_INTR)
354     virtio_pci_irq_0_handler (vm, h, line);
355
356   if (isr & VIRTIO_PCI_ISR_CONFIG)
357     virtio_pci_irq_1_handler (vm, h, line);
358 }
359
360 inline void
361 device_status (vlib_main_t * vm, virtio_if_t * vif)
362 {
363   struct status_struct
364   {
365     u8 bit;
366     char *str;
367   };
368   struct status_struct *status_entry;
369   static struct status_struct status_array[] = {
370 #define _(s,b) { .str = #s, .bit = b, },
371     foreach_virtio_config_status_flags
372 #undef _
373     {.str = NULL}
374   };
375
376   vlib_cli_output (vm, "  status 0x%x", vif->status);
377
378   status_entry = (struct status_struct *) &status_array;
379   while (status_entry->str)
380     {
381       if (vif->status & status_entry->bit)
382         vlib_cli_output (vm, "    %s (%x)", status_entry->str,
383                          status_entry->bit);
384       status_entry++;
385     }
386 }
387
388 inline void
389 debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif)
390 {
391   u32 data_u32;
392   u16 data_u16;
393   u8 data_u8;
394   vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
395                         &data_u32);
396   vlib_cli_output (vm, "remote features 0x%lx", data_u32);
397   vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
398                         &data_u32);
399   vlib_cli_output (vm, "guest features 0x%lx", data_u32);
400   vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
401                         &data_u32);
402   vlib_cli_output (vm, "queue address 0x%lx", data_u32);
403   vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
404                         &data_u16);
405   vlib_cli_output (vm, "queue size 0x%x", data_u16);
406   vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
407                         &data_u16);
408   vlib_cli_output (vm, "queue select 0x%x", data_u16);
409   vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
410                         &data_u16);
411   vlib_cli_output (vm, "queue notify 0x%x", data_u16);
412   vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8);
413   vlib_cli_output (vm, "status 0x%x", data_u8);
414   vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8);
415   vlib_cli_output (vm, "isr 0x%x", data_u8);
416
417   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
418     {
419       vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
420                             &data_u16);
421       vlib_cli_output (vm, "config vector 0x%x", data_u16);
422       u16 queue_id = 0;
423       vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
424                              &queue_id);
425       vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
426                             &data_u16);
427       vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16);
428     }
429
430   u8 mac[6];
431   virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
432   vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
433   virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),      /* offset to status */
434                                  6);
435   vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
436   virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
437                                  /* offset to max_virtqueue */ 8);
438   vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
439   virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),      /* offset to mtu */
440                                  10);
441   vlib_cli_output (vm, "mtu 0x%x", data_u16);
442
443   u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4;
444   i += a;
445   i &= ~a;
446   for (; i < 64; i += 4)
447     {
448       u32 data = 0;
449       vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
450       vlib_cli_output (vm, "0x%lx", data);
451     }
452 }
453
454 struct virtio_ctrl_msg
455 {
456   struct virtio_net_ctrl_hdr ctrl;
457   virtio_net_ctrl_ack status;
458   u8 data[1024];
459 };
460
461 static int
462 virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
463                           struct virtio_ctrl_msg *data, u32 len)
464 {
465   virtio_vring_t *vring = vif->cxq_vring;
466   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
467   struct virtio_ctrl_msg result;
468   u32 buffer_index;
469   vlib_buffer_t *b;
470   u16 used, next, avail;
471   u16 sz = vring->size;
472   u16 mask = sz - 1;
473
474   used = vring->desc_in_use;
475   next = vring->desc_next;
476   avail = vring->avail->idx;
477   struct vring_desc *d = &vring->desc[next];
478
479   if (vlib_buffer_alloc (vm, &buffer_index, 1))
480     b = vlib_get_buffer (vm, buffer_index);
481   else
482     return VIRTIO_NET_ERR;
483   /*
484    * current_data may not be initialized with 0 and may contain
485    * previous offset.
486    */
487   b->current_data = 0;
488   clib_memcpy (vlib_buffer_get_current (b), data,
489                sizeof (struct virtio_ctrl_msg));
490   d->flags = VRING_DESC_F_NEXT;
491   d->addr = vlib_buffer_get_current_pa (vm, b);
492   d->len = sizeof (struct virtio_net_ctrl_hdr);
493   vring->avail->ring[avail & mask] = next;
494   avail++;
495   next = (next + 1) & mask;
496   d->next = next;
497   used++;
498
499   d = &vring->desc[next];
500   d->flags = VRING_DESC_F_NEXT;
501   d->addr = vlib_buffer_get_current_pa (vm, b) +
502     STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
503   d->len = len;
504   next = (next + 1) & mask;
505   d->next = next;
506   used++;
507
508   d = &vring->desc[next];
509   d->flags = VRING_DESC_F_WRITE;
510   d->addr = vlib_buffer_get_current_pa (vm, b) +
511     STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
512   d->len = sizeof (data->status);
513   next = (next + 1) & mask;
514   used++;
515
516   CLIB_MEMORY_STORE_BARRIER ();
517   vring->avail->idx = avail;
518   vring->desc_next = next;
519   vring->desc_in_use = used;
520
521   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
522     {
523       virtio_kick (vm, vring, vif);
524     }
525
526   u16 last = vring->last_used_idx, n_left = 0;
527   n_left = vring->used->idx - last;
528
529   while (n_left)
530     {
531       struct vring_used_elem *e = &vring->used->ring[last & mask];
532       u16 slot = e->id;
533
534       d = &vring->desc[slot];
535       while (d->flags & VRING_DESC_F_NEXT)
536         {
537           used--;
538           slot = d->next;
539           d = &vring->desc[slot];
540         }
541       used--;
542       last++;
543       n_left--;
544     }
545   vring->desc_in_use = used;
546   vring->last_used_idx = last;
547
548   CLIB_MEMORY_BARRIER ();
549   clib_memcpy (&result, vlib_buffer_get_current (b),
550                sizeof (struct virtio_ctrl_msg));
551   virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
552   status = result.status;
553   vlib_buffer_free (vm, &buffer_index, 1);
554   return status;
555 }
556
557 static int
558 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
559 {
560   struct virtio_ctrl_msg gso_hdr;
561   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
562
563   gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
564   gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
565   gso_hdr.status = VIRTIO_NET_ERR;
566   u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
567     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
568     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
569     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO);
570   clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
571
572   status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
573   virtio_log_debug (vif, "enable gso");
574   return status;
575 }
576
577 static int
578 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
579                               u16 num_queues)
580 {
581   struct virtio_ctrl_msg mq_hdr;
582   virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
583
584   mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
585   mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
586   mq_hdr.status = VIRTIO_NET_ERR;
587   clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
588
589   status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
590   virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
591   return status;
592 }
593
594 static u8
595 virtio_pci_queue_size_valid (u16 qsz)
596 {
597   if (qsz < 64 || qsz > 4096)
598     return 0;
599   if ((qsz % 64) != 0)
600     return 0;
601   return 1;
602 }
603
604 clib_error_t *
605 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
606                                u16 queue_num)
607 {
608   clib_error_t *error = 0;
609   u16 queue_size = 0;
610   virtio_vring_t *vring;
611   struct vring vr;
612   u32 i = 0;
613   void *ptr = NULL;
614
615   queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
616   if (!virtio_pci_queue_size_valid (queue_size))
617     clib_warning ("queue size is not valid");
618
619   if (!is_pow2 (queue_size))
620     return clib_error_return (0, "ring size must be power of 2");
621
622   if (queue_size > 32768)
623     return clib_error_return (0, "ring size must be 32768 or lower");
624
625   if (queue_size == 0)
626     queue_size = 256;
627
628   vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
629   vring = vec_elt_at_index (vif->cxq_vring, 0);
630   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
631   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
632   ptr =
633     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
634                                         vif->numa_node);
635   if (!ptr)
636     return vlib_physmem_last_error (vm);
637   clib_memset (ptr, 0, i);
638   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
639   vring->desc = vr.desc;
640   vring->avail = vr.avail;
641   vring->used = vr.used;
642   vring->queue_id = queue_num;
643   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
644
645   ASSERT (vring->buffers == 0);
646
647   vring->size = queue_size;
648   virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
649                     queue_size);
650   virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
651   vring->kick_fd = -1;
652
653   return error;
654 }
655
656 clib_error_t *
657 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
658 {
659   clib_error_t *error = 0;
660   vlib_thread_main_t *vtm = vlib_get_thread_main ();
661   u16 queue_size = 0;
662   virtio_vring_t *vring;
663   struct vring vr;
664   u32 i = 0;
665   void *ptr = NULL;
666
667   queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
668   if (!virtio_pci_queue_size_valid (queue_size))
669     clib_warning ("queue size is not valid");
670
671   if (!is_pow2 (queue_size))
672     return clib_error_return (0, "ring size must be power of 2");
673
674   if (queue_size > 32768)
675     return clib_error_return (0, "ring size must be 32768 or lower");
676
677   if (queue_size == 0)
678     queue_size = 256;
679
680   if (queue_num % 2)
681     {
682       if (TX_QUEUE_ACCESS (queue_num) > vtm->n_vlib_mains)
683         return error;
684       vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
685                             CLIB_CACHE_LINE_BYTES);
686       vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
687       clib_spinlock_init (&vring->lockp);
688     }
689   else
690     {
691       vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
692                             CLIB_CACHE_LINE_BYTES);
693       vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
694     }
695   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
696   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
697   ptr =
698     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
699                                         vif->numa_node);
700   if (!ptr)
701     return vlib_physmem_last_error (vm);
702   clib_memset (ptr, 0, i);
703   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
704   vring->desc = vr.desc;
705   vring->avail = vr.avail;
706   vring->used = vr.used;
707   vring->queue_id = queue_num;
708   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
709
710   ASSERT (vring->buffers == 0);
711   vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
712   if (queue_num % 2)
713     {
714       virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
715                         queue_size);
716     }
717   else
718     {
719       virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
720                         queue_size);
721     }
722   vring->size = queue_size;
723   if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr))
724     return clib_error_return (0, "error in queue address setup");
725
726   vring->kick_fd = -1;
727   return error;
728 }
729
730 static void
731 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
732                            u64 req_features)
733 {
734   /*
735    * if features are not requested
736    * default: all supported features
737    */
738   u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
739     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
740     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
741     | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
742     | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
743     | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
744     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
745     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
746     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
747     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
748     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
749     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
750     | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
751     | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
752     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
753     | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
754     | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
755     | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
756     | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
757
758   if (req_features == 0)
759     {
760       req_features = supported_features;
761     }
762
763   vif->features = req_features & vif->remote_features & supported_features;
764
765   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
766     {
767       virtio_net_config_t config;
768       virtio_pci_legacy_read_config (vm, vif, &config.mtu,
769                                      sizeof (config.mtu),
770                                      STRUCT_OFFSET_OF (virtio_net_config_t,
771                                                        mtu));
772       if (config.mtu < 64)
773         vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
774     }
775
776   vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
777 }
778
779 void
780 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
781 {
782   vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
783 }
784
785 int
786 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
787 {
788   u8 status = 0;
789
790   /*
791    * Reset the device
792    */
793   status = virtio_pci_legacy_reset (vm, vif);
794
795   /*
796    * Set the Acknowledge status bit
797    */
798   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
799
800   /*
801    * Set the Driver status bit
802    */
803   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
804
805   /*
806    * Read the status and verify it
807    */
808   status = virtio_pci_legacy_get_status (vm, vif);
809   if (!
810       ((status & VIRTIO_CONFIG_STATUS_ACK)
811        && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
812     return -1;
813   vif->status = status;
814
815   return 0;
816 }
817
818 clib_error_t *
819 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
820 {
821   clib_error_t *error = 0;
822   struct virtio_pci_cap cap;
823   u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
824   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
825
826   if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
827     {
828       virtio_log_error (vif, "error in reading capabilty list position");
829       clib_error_return (error, "error in reading capabilty list position");
830     }
831   while (pos)
832     {
833       if ((error =
834            vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
835                                        sizeof (cap))))
836         {
837           virtio_log_error (vif, "%s [%2x]",
838                             "error in reading the capability at", pos);
839           clib_error_return (error,
840                              "error in reading the capability at [%2x]", pos);
841         }
842
843       if (cap.cap_vndr == PCI_CAP_ID_MSIX)
844         {
845           u16 flags, table_size, table_size_mask = 0x07FF;
846
847           if ((error =
848                vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
849                                            sizeof (flags))))
850             clib_error_return (error,
851                                "error in reading the capability at [%2x]",
852                                pos + 2);
853
854           table_size = flags & table_size_mask;
855           virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
856                             "msix interrupt vector table-size", table_size);
857
858           if (flags & PCI_MSIX_ENABLE)
859             {
860               virtio_log_debug (vif, "msix interrupt enabled");
861               vif->msix_enabled = VIRTIO_MSIX_ENABLED;
862             }
863           else
864             {
865               virtio_log_debug (vif, "msix interrupt disabled");
866               vif->msix_enabled = VIRTIO_MSIX_DISABLED;
867             }
868         }
869
870       if (cap.cap_vndr != PCI_CAP_ID_VNDR)
871         {
872           virtio_log_debug (vif, "[%2x] %s %2x ", pos,
873                             "skipping non VNDR cap id:", cap.cap_vndr);
874           goto next;
875         }
876
877       virtio_log_debug (vif,
878                         "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
879                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
880       switch (cap.cfg_type)
881         {
882         case VIRTIO_PCI_CAP_COMMON_CFG:
883           common_cfg = 1;
884           break;
885         case VIRTIO_PCI_CAP_NOTIFY_CFG:
886           notify_base = 1;
887           break;
888         case VIRTIO_PCI_CAP_DEVICE_CFG:
889           dev_cfg = 1;
890           break;
891         case VIRTIO_PCI_CAP_ISR_CFG:
892           isr = 1;
893           break;
894         case VIRTIO_PCI_CAP_PCI_CFG:
895           if (cap.bar == 0)
896             pci_cfg = 1;
897           break;
898         }
899     next:
900       pos = cap.cap_next;
901     }
902
903   if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
904     {
905       virtio_log_debug (vif, "legacy virtio pci device found");
906       return error;
907     }
908
909   if (!pci_cfg)
910     clib_error_return (error, "modern virtio pci device found");
911
912   virtio_log_debug (vif, "transitional virtio pci device found");
913   return error;
914 }
915
916 static clib_error_t *
917 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
918                         virtio_pci_create_if_args_t * args)
919 {
920   clib_error_t *error = 0;
921   u8 status = 0;
922
923   if ((error = virtio_pci_read_caps (vm, vif)))
924     clib_error_return (error, "Device is not supported");
925
926   if (virtio_pci_reset_device (vm, vif) < 0)
927     {
928       virtio_log_error (vif, "Failed to reset the device");
929       clib_error_return (error, "Failed to reset the device");
930     }
931   /*
932    * read device features and negotiate (user) requested features
933    */
934   virtio_pci_read_device_feature (vm, vif);
935   virtio_negotiate_features (vm, vif, args->features);
936
937   /*
938    * After FEATURE_OK, driver should not accept new feature bits
939    */
940   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
941   status = virtio_pci_legacy_get_status (vm, vif);
942   if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
943     {
944       virtio_log_error (vif,
945                         "error encountered: Device doesn't support requested features");
946       clib_error_return (error, "Device doesn't support requested features");
947     }
948   vif->status = status;
949
950   /*
951    * get or set the mac address
952    */
953   if (virtio_pci_get_mac (vm, vif))
954     {
955       f64 now = vlib_time_now (vm);
956       u32 rnd;
957       rnd = (u32) (now * 1e6);
958       rnd = random_u32 (&rnd);
959
960       memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
961       vif->mac_addr[0] = 2;
962       vif->mac_addr[1] = 0xfe;
963       virtio_pci_set_mac (vm, vif);
964     }
965
966   virtio_set_net_hdr_size (vif);
967
968   /*
969    * Initialize the virtqueues
970    */
971   if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
972     goto err;
973
974   for (int i = 0; i < vif->max_queue_pairs; i++)
975     {
976       if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
977         {
978           virtio_log_warning (vif, "%s (%u) %s", "error in rxq-queue",
979                               RX_QUEUE (i), "initialization");
980         }
981       else
982         {
983           vif->num_rxqs++;
984         }
985
986       if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
987         {
988           virtio_log_warning (vif, "%s (%u) %s", "error in txq-queue",
989                               TX_QUEUE (i), "initialization");
990         }
991       else
992         {
993           vif->num_txqs++;
994         }
995     }
996
997   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
998     {
999       if ((error =
1000            virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1001         {
1002           virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
1003                               vif->max_queue_pairs * 2, "initialization");
1004           if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1005             vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1006         }
1007     }
1008   else
1009     {
1010       virtio_log_debug (vif, "control queue is not available");
1011       vif->cxq_vring = NULL;
1012     }
1013
1014   /*
1015    * set the msix interrupts
1016    */
1017   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1018     {
1019       if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
1020           VIRTIO_MSI_NO_VECTOR)
1021         virtio_log_warning (vif, "config vector 1 is not set");
1022       if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
1023           VIRTIO_MSI_NO_VECTOR)
1024         virtio_log_warning (vif, "queue vector 0 is not set");
1025     }
1026
1027   /*
1028    * set the driver status OK
1029    */
1030   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1031   vif->status = virtio_pci_legacy_get_status (vm, vif);
1032 err:
1033   return error;
1034 }
1035
1036 void
1037 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1038 {
1039   vnet_main_t *vnm = vnet_get_main ();
1040   virtio_main_t *vim = &virtio_main;
1041   virtio_if_t *vif;
1042   vlib_pci_dev_handle_t h;
1043   clib_error_t *error = 0;
1044
1045   /* *INDENT-OFF* */
1046   pool_foreach (vif, vim->interfaces, ({
1047     if (vif->pci_addr.as_u32 == args->addr)
1048       {
1049         args->rv = VNET_API_ERROR_INVALID_VALUE;
1050         args->error =
1051           clib_error_return (error, "PCI address in use");
1052           vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1053                 format_vlib_pci_addr, &args->addr,
1054                 " PCI address in use");
1055         return;
1056       }
1057   }));
1058   /* *INDENT-ON* */
1059
1060   pool_get (vim->interfaces, vif);
1061   vif->dev_instance = vif - vim->interfaces;
1062   vif->per_interface_next_index = ~0;
1063   vif->pci_addr.as_u32 = args->addr;
1064
1065   if ((error =
1066        vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1067                              virtio_pci_device_ids, &h)))
1068     {
1069       pool_put (vim->interfaces, vif);
1070       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1071       args->error =
1072         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1073                            &vif->pci_addr);
1074       vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1075                 format_vlib_pci_addr, &vif->pci_addr,
1076                 "error encountered on pci device open");
1077       return;
1078     }
1079   vif->pci_dev_handle = h;
1080   vlib_pci_set_private_data (vm, h, vif->dev_instance);
1081   vif->numa_node = vlib_pci_get_numa_node (vm, h);
1082   vif->type = VIRTIO_IF_TYPE_PCI;
1083
1084   if ((error = vlib_pci_bus_master_enable (vm, h)))
1085     {
1086       virtio_log_error (vif, "error encountered on pci bus master enable");
1087       goto error;
1088     }
1089
1090   if ((error = vlib_pci_io_region (vm, h, 0)))
1091     {
1092       virtio_log_error (vif, "error encountered on pci io region");
1093       goto error;
1094     }
1095
1096   if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
1097     {
1098       if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1099                                                    &virtio_pci_irq_0_handler)))
1100         {
1101           virtio_log_error (vif,
1102                             "error encountered on pci register msix handler 0");
1103           goto error;
1104         }
1105       if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1106                                                    &virtio_pci_irq_1_handler)))
1107         {
1108           virtio_log_error (vif,
1109                             "error encountered on pci register msix handler 1");
1110           goto error;
1111         }
1112
1113       if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
1114         {
1115           virtio_log_error (vif, "error encountered on pci enable msix irq");
1116           goto error;
1117         }
1118       vif->support_int_mode = 1;
1119       virtio_log_debug (vif, "device supports msix interrupts");
1120     }
1121   else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1122     {
1123       /*
1124        * if msix table-size is 1, fall back to intX.
1125        */
1126       if ((error =
1127            vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1128         {
1129           virtio_log_error (vif,
1130                             "error encountered on pci register interrupt handler");
1131           goto error;
1132         }
1133       vif->support_int_mode = 1;
1134       virtio_log_debug (vif, "pci register interrupt handler");
1135     }
1136   else
1137     {
1138       /*
1139        * WARN: intX is showing some weird behaviour.
1140        * Please don't use interrupt mode with UIO driver.
1141        */
1142       vif->support_int_mode = 0;
1143       virtio_log_debug (vif, "driver is configured in poll mode only");
1144     }
1145
1146   if ((error = vlib_pci_intr_enable (vm, h)))
1147     {
1148       virtio_log_error (vif, "error encountered on pci interrupt enable");
1149       goto error;
1150     }
1151
1152   if ((error = virtio_pci_device_init (vm, vif, args)))
1153     {
1154       virtio_log_error (vif, "error encountered on device init");
1155       goto error;
1156     }
1157
1158   /* create interface */
1159   error = ethernet_register_interface (vnm, virtio_device_class.index,
1160                                        vif->dev_instance, vif->mac_addr,
1161                                        &vif->hw_if_index,
1162                                        virtio_pci_flag_change);
1163
1164   if (error)
1165     {
1166       virtio_log_error (vif,
1167                         "error encountered on ethernet register interface");
1168       goto error;
1169     }
1170
1171   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1172   vif->sw_if_index = sw->sw_if_index;
1173   args->sw_if_index = sw->sw_if_index;
1174
1175   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1176   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1177   vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1178                                     virtio_input_node.index);
1179   u32 i = 0;
1180   vec_foreach_index (i, vif->rxq_vrings)
1181   {
1182     vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1183     virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1184     /* Set default rx mode to POLLING */
1185     vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1186                                    VNET_HW_INTERFACE_RX_MODE_POLLING);
1187   }
1188   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1189     {
1190       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1191       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1192                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1193     }
1194   else
1195     vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1196
1197   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1198     {
1199       if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) &&
1200           args->gso_enabled)
1201         {
1202           if (virtio_pci_enable_gso (vm, vif))
1203             {
1204               virtio_log_warning (vif, "gso is not enabled");
1205             }
1206           else
1207             {
1208               vif->gso_enabled = 1;
1209               hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
1210               vnm->interface_main.gso_interface_count++;
1211             }
1212         }
1213       if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1214         {
1215           if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1216             virtio_log_warning (vif, "multiqueue is not set");
1217         }
1218     }
1219   return;
1220
1221
1222 error:
1223   virtio_pci_delete_if (vm, vif);
1224   args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1225   args->error = error;
1226 }
1227
1228 int
1229 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1230 {
1231   vnet_main_t *vnm = vnet_get_main ();
1232   virtio_main_t *vim = &virtio_main;
1233   u32 i = 0;
1234
1235   if (vif->type != VIRTIO_IF_TYPE_PCI)
1236     return VNET_API_ERROR_INVALID_INTERFACE;
1237
1238   vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1239
1240   for (i = 0; i < vif->max_queue_pairs; i++)
1241     {
1242       virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1243       virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1244     }
1245
1246   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1247     virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
1248
1249   virtio_pci_legacy_reset (vm, vif);
1250
1251   if (vif->gso_enabled)
1252     vnm->interface_main.gso_interface_count--;
1253
1254   if (vif->hw_if_index)
1255     {
1256       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1257       vec_foreach_index (i, vif->rxq_vrings)
1258       {
1259         vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1260       }
1261       ethernet_delete_interface (vnm, vif->hw_if_index);
1262     }
1263
1264   vlib_pci_device_close (vm, vif->pci_dev_handle);
1265
1266   vec_foreach_index (i, vif->rxq_vrings)
1267   {
1268     virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1269     if (vring->kick_fd != -1)
1270       close (vring->kick_fd);
1271     if (vring->used)
1272       {
1273         virtio_free_rx_buffers (vm, vring);
1274       }
1275     vec_free (vring->buffers);
1276     vlib_physmem_free (vm, vring->desc);
1277   }
1278
1279   vec_foreach_index (i, vif->txq_vrings)
1280   {
1281     virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1282     if (vring->kick_fd != -1)
1283       close (vring->kick_fd);
1284     if (vring->used)
1285       {
1286         virtio_free_used_desc (vm, vring);
1287       }
1288     vec_free (vring->buffers);
1289     clib_spinlock_free (&vring->lockp);
1290     vlib_physmem_free (vm, vring->desc);
1291   }
1292
1293   if (vif->cxq_vring != NULL)
1294     {
1295       u16 last = vif->cxq_vring->last_used_idx;
1296       u16 n_left = vif->cxq_vring->used->idx - last;
1297       while (n_left)
1298         {
1299           last++;
1300           n_left--;
1301         }
1302
1303       vif->cxq_vring->last_used_idx = last;
1304       vlib_physmem_free (vm, vif->cxq_vring->desc);
1305     }
1306
1307   vec_free (vif->rxq_vrings);
1308   vec_free (vif->txq_vrings);
1309   vec_free (vif->cxq_vring);
1310
1311   clib_error_free (vif->error);
1312   memset (vif, 0, sizeof (*vif));
1313   pool_put (vim->interfaces, vif);
1314
1315   return 0;
1316 }
1317
1318 /*
1319  * fd.io coding-style-patch-verification: ON
1320  *
1321  * Local Variables:
1322  * eval: (c-set-style "gnu")
1323  * End:
1324  */