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