ab21147aca96b17f4cf803b120ea7f411d5c5784
[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   u16 queue_size = 0;
661   virtio_vring_t *vring;
662   struct vring vr;
663   u32 i = 0;
664   void *ptr = NULL;
665
666   queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
667   if (!virtio_pci_queue_size_valid (queue_size))
668     clib_warning ("queue size is not valid");
669
670   if (!is_pow2 (queue_size))
671     return clib_error_return (0, "ring size must be power of 2");
672
673   if (queue_size > 32768)
674     return clib_error_return (0, "ring size must be 32768 or lower");
675
676   if (queue_size == 0)
677     queue_size = 256;
678
679   if (queue_num % 2)
680     {
681       vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
682                             CLIB_CACHE_LINE_BYTES);
683       vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
684       clib_spinlock_init (&vring->lockp);
685     }
686   else
687     {
688       vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
689                             CLIB_CACHE_LINE_BYTES);
690       vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
691     }
692   i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
693   i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
694   ptr =
695     vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
696                                         vif->numa_node);
697   if (!ptr)
698     return vlib_physmem_last_error (vm);
699   clib_memset (ptr, 0, i);
700   vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
701   vring->desc = vr.desc;
702   vring->avail = vr.avail;
703   vring->used = vr.used;
704   vring->queue_id = queue_num;
705   vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
706
707   ASSERT (vring->buffers == 0);
708   vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
709   if (queue_num % 2)
710     {
711       virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
712                         queue_size);
713     }
714   else
715     {
716       virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
717                         queue_size);
718     }
719   vring->size = queue_size;
720   if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr))
721     return clib_error_return (0, "error in queue address setup");
722
723   vring->kick_fd = -1;
724   return error;
725 }
726
727 static void
728 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
729                            u64 req_features)
730 {
731   /*
732    * if features are not requested
733    * default: all supported features
734    */
735   u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
736     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
737     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
738     | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
739     | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
740     | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
741     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
742     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
743     | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
744     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
745     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
746     | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
747     | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
748     | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
749     | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
750     | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
751     | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
752     | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
753     | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
754
755   if (req_features == 0)
756     {
757       req_features = supported_features;
758     }
759
760   vif->features = req_features & vif->remote_features & supported_features;
761
762   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
763     {
764       virtio_net_config_t config;
765       virtio_pci_legacy_read_config (vm, vif, &config.mtu,
766                                      sizeof (config.mtu),
767                                      STRUCT_OFFSET_OF (virtio_net_config_t,
768                                                        mtu));
769       if (config.mtu < 64)
770         vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
771     }
772
773   vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
774 }
775
776 void
777 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
778 {
779   vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
780 }
781
782 int
783 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
784 {
785   u8 status = 0;
786
787   /*
788    * Reset the device
789    */
790   status = virtio_pci_legacy_reset (vm, vif);
791
792   /*
793    * Set the Acknowledge status bit
794    */
795   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
796
797   /*
798    * Set the Driver status bit
799    */
800   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
801
802   /*
803    * Read the status and verify it
804    */
805   status = virtio_pci_legacy_get_status (vm, vif);
806   if (!
807       ((status & VIRTIO_CONFIG_STATUS_ACK)
808        && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
809     return -1;
810   vif->status = status;
811
812   return 0;
813 }
814
815 clib_error_t *
816 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
817 {
818   clib_error_t *error = 0;
819   struct virtio_pci_cap cap;
820   u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
821   vlib_pci_dev_handle_t h = vif->pci_dev_handle;
822
823   if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
824     {
825       virtio_log_error (vif, "error in reading capabilty list position");
826       clib_error_return (error, "error in reading capabilty list position");
827     }
828   while (pos)
829     {
830       if ((error =
831            vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
832                                        sizeof (cap))))
833         {
834           virtio_log_error (vif, "%s [%2x]",
835                             "error in reading the capability at", pos);
836           clib_error_return (error,
837                              "error in reading the capability at [%2x]", pos);
838         }
839
840       if (cap.cap_vndr == PCI_CAP_ID_MSIX)
841         {
842           u16 flags, table_size, table_size_mask = 0x07FF;
843
844           if ((error =
845                vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
846                                            sizeof (flags))))
847             clib_error_return (error,
848                                "error in reading the capability at [%2x]",
849                                pos + 2);
850
851           table_size = flags & table_size_mask;
852           virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
853                             "msix interrupt vector table-size", table_size);
854
855           if (flags & PCI_MSIX_ENABLE)
856             {
857               virtio_log_debug (vif, "msix interrupt enabled");
858               vif->msix_enabled = VIRTIO_MSIX_ENABLED;
859             }
860           else
861             {
862               virtio_log_debug (vif, "msix interrupt disabled");
863               vif->msix_enabled = VIRTIO_MSIX_DISABLED;
864             }
865         }
866
867       if (cap.cap_vndr != PCI_CAP_ID_VNDR)
868         {
869           virtio_log_debug (vif, "[%2x] %s %2x ", pos,
870                             "skipping non VNDR cap id:", cap.cap_vndr);
871           goto next;
872         }
873
874       virtio_log_debug (vif,
875                         "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
876                         pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
877       switch (cap.cfg_type)
878         {
879         case VIRTIO_PCI_CAP_COMMON_CFG:
880           common_cfg = 1;
881           break;
882         case VIRTIO_PCI_CAP_NOTIFY_CFG:
883           notify_base = 1;
884           break;
885         case VIRTIO_PCI_CAP_DEVICE_CFG:
886           dev_cfg = 1;
887           break;
888         case VIRTIO_PCI_CAP_ISR_CFG:
889           isr = 1;
890           break;
891         case VIRTIO_PCI_CAP_PCI_CFG:
892           if (cap.bar == 0)
893             pci_cfg = 1;
894           break;
895         }
896     next:
897       pos = cap.cap_next;
898     }
899
900   if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
901     {
902       virtio_log_debug (vif, "legacy virtio pci device found");
903       return error;
904     }
905
906   if (!pci_cfg)
907     clib_error_return (error, "modern virtio pci device found");
908
909   virtio_log_debug (vif, "transitional virtio pci device found");
910   return error;
911 }
912
913 static clib_error_t *
914 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
915                         virtio_pci_create_if_args_t * args)
916 {
917   clib_error_t *error = 0;
918   vlib_thread_main_t *vtm = vlib_get_thread_main ();
919   u8 status = 0;
920
921   if ((error = virtio_pci_read_caps (vm, vif)))
922     clib_error_return (error, "Device is not supported");
923
924   if (virtio_pci_reset_device (vm, vif) < 0)
925     {
926       virtio_log_error (vif, "Failed to reset the device");
927       clib_error_return (error, "Failed to reset the device");
928     }
929   /*
930    * read device features and negotiate (user) requested features
931    */
932   virtio_pci_read_device_feature (vm, vif);
933   virtio_negotiate_features (vm, vif, args->features);
934
935   /*
936    * After FEATURE_OK, driver should not accept new feature bits
937    */
938   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
939   status = virtio_pci_legacy_get_status (vm, vif);
940   if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
941     {
942       virtio_log_error (vif,
943                         "error encountered: Device doesn't support requested features");
944       clib_error_return (error, "Device doesn't support requested features");
945     }
946   vif->status = status;
947
948   /*
949    * get or set the mac address
950    */
951   if (virtio_pci_get_mac (vm, vif))
952     {
953       f64 now = vlib_time_now (vm);
954       u32 rnd;
955       rnd = (u32) (now * 1e6);
956       rnd = random_u32 (&rnd);
957
958       memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
959       vif->mac_addr[0] = 2;
960       vif->mac_addr[1] = 0xfe;
961       virtio_pci_set_mac (vm, vif);
962     }
963
964   virtio_set_net_hdr_size (vif);
965
966   /*
967    * Initialize the virtqueues
968    */
969   if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
970     goto err;
971
972   for (int i = 0; i < vif->max_queue_pairs; i++)
973     {
974       if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
975         {
976           virtio_log_warning (vif, "%s (%u) %s", "error in rxq-queue",
977                               RX_QUEUE (i), "initialization");
978         }
979       else
980         {
981           vif->num_rxqs++;
982         }
983
984       if (i >= vtm->n_vlib_mains)
985         {
986           /*
987            * There is 1:1 mapping between tx queue and vpp worker thread.
988            * tx queue 0 is bind with thread index 0, tx queue 1 on thread
989            * index 1 and so on.
990            * Multiple worker threads can poll same tx queue when number of
991            * workers are more than tx queues. In this case, 1:N mapping
992            * between tx queue and vpp worker thread.
993            */
994           virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
995                             TX_QUEUE (i),
996                             "no VPP worker thread is available");
997           continue;
998         }
999
1000       if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
1001         {
1002           virtio_log_warning (vif, "%s (%u) %s", "error in txq-queue",
1003                               TX_QUEUE (i), "initialization");
1004         }
1005       else
1006         {
1007           vif->num_txqs++;
1008         }
1009     }
1010
1011   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1012     {
1013       if ((error =
1014            virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1015         {
1016           virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
1017                               vif->max_queue_pairs * 2, "initialization");
1018           if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1019             vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1020         }
1021     }
1022   else
1023     {
1024       virtio_log_debug (vif, "control queue is not available");
1025       vif->cxq_vring = NULL;
1026     }
1027
1028   /*
1029    * set the msix interrupts
1030    */
1031   if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1032     {
1033       if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
1034           VIRTIO_MSI_NO_VECTOR)
1035         virtio_log_warning (vif, "config vector 1 is not set");
1036       if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
1037           VIRTIO_MSI_NO_VECTOR)
1038         virtio_log_warning (vif, "queue vector 0 is not set");
1039     }
1040
1041   /*
1042    * set the driver status OK
1043    */
1044   virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1045   vif->status = virtio_pci_legacy_get_status (vm, vif);
1046 err:
1047   return error;
1048 }
1049
1050 void
1051 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1052 {
1053   vnet_main_t *vnm = vnet_get_main ();
1054   virtio_main_t *vim = &virtio_main;
1055   virtio_if_t *vif;
1056   vlib_pci_dev_handle_t h;
1057   clib_error_t *error = 0;
1058
1059   /* *INDENT-OFF* */
1060   pool_foreach (vif, vim->interfaces, ({
1061     if (vif->pci_addr.as_u32 == args->addr)
1062       {
1063         args->rv = VNET_API_ERROR_INVALID_VALUE;
1064         args->error =
1065           clib_error_return (error, "PCI address in use");
1066           vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1067                 format_vlib_pci_addr, &args->addr,
1068                 " PCI address in use");
1069         return;
1070       }
1071   }));
1072   /* *INDENT-ON* */
1073
1074   pool_get (vim->interfaces, vif);
1075   vif->dev_instance = vif - vim->interfaces;
1076   vif->per_interface_next_index = ~0;
1077   vif->pci_addr.as_u32 = args->addr;
1078
1079   if ((error =
1080        vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1081                              virtio_pci_device_ids, &h)))
1082     {
1083       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1084       args->error =
1085         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1086                            &vif->pci_addr);
1087       vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1088                 format_vlib_pci_addr, &vif->pci_addr,
1089                 "error encountered on pci device open");
1090       pool_put (vim->interfaces, vif);
1091       return;
1092     }
1093   vif->pci_dev_handle = h;
1094   vlib_pci_set_private_data (vm, h, vif->dev_instance);
1095   vif->numa_node = vlib_pci_get_numa_node (vm, h);
1096   vif->type = VIRTIO_IF_TYPE_PCI;
1097
1098   if ((error = vlib_pci_bus_master_enable (vm, h)))
1099     {
1100       virtio_log_error (vif, "error encountered on pci bus master enable");
1101       goto error;
1102     }
1103
1104   if ((error = vlib_pci_io_region (vm, h, 0)))
1105     {
1106       virtio_log_error (vif, "error encountered on pci io region");
1107       goto error;
1108     }
1109
1110   if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
1111     {
1112       if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1113                                                    &virtio_pci_irq_0_handler)))
1114         {
1115           virtio_log_error (vif,
1116                             "error encountered on pci register msix handler 0");
1117           goto error;
1118         }
1119       if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1120                                                    &virtio_pci_irq_1_handler)))
1121         {
1122           virtio_log_error (vif,
1123                             "error encountered on pci register msix handler 1");
1124           goto error;
1125         }
1126
1127       if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
1128         {
1129           virtio_log_error (vif, "error encountered on pci enable msix irq");
1130           goto error;
1131         }
1132       vif->support_int_mode = 1;
1133       virtio_log_debug (vif, "device supports msix interrupts");
1134     }
1135   else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1136     {
1137       /*
1138        * if msix table-size is 1, fall back to intX.
1139        */
1140       if ((error =
1141            vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1142         {
1143           virtio_log_error (vif,
1144                             "error encountered on pci register interrupt handler");
1145           goto error;
1146         }
1147       vif->support_int_mode = 1;
1148       virtio_log_debug (vif, "pci register interrupt handler");
1149     }
1150   else
1151     {
1152       /*
1153        * WARN: intX is showing some weird behaviour.
1154        * Please don't use interrupt mode with UIO driver.
1155        */
1156       vif->support_int_mode = 0;
1157       virtio_log_debug (vif, "driver is configured in poll mode only");
1158     }
1159
1160   if ((error = vlib_pci_intr_enable (vm, h)))
1161     {
1162       virtio_log_error (vif, "error encountered on pci interrupt enable");
1163       goto error;
1164     }
1165
1166   if ((error = virtio_pci_device_init (vm, vif, args)))
1167     {
1168       virtio_log_error (vif, "error encountered on device init");
1169       goto error;
1170     }
1171
1172   /* create interface */
1173   error = ethernet_register_interface (vnm, virtio_device_class.index,
1174                                        vif->dev_instance, vif->mac_addr,
1175                                        &vif->hw_if_index,
1176                                        virtio_pci_flag_change);
1177
1178   if (error)
1179     {
1180       virtio_log_error (vif,
1181                         "error encountered on ethernet register interface");
1182       goto error;
1183     }
1184
1185   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1186   vif->sw_if_index = sw->sw_if_index;
1187   args->sw_if_index = sw->sw_if_index;
1188
1189   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1190   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1191   vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1192                                     virtio_input_node.index);
1193   u32 i = 0;
1194   vec_foreach_index (i, vif->rxq_vrings)
1195   {
1196     vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1197     virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1198     /* Set default rx mode to POLLING */
1199     vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1200                                    VNET_HW_INTERFACE_RX_MODE_POLLING);
1201   }
1202   if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1203     {
1204       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1205       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1206                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1207     }
1208   else
1209     vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1210
1211   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1212     {
1213       if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) &&
1214           args->gso_enabled)
1215         {
1216           if (virtio_pci_enable_gso (vm, vif))
1217             {
1218               virtio_log_warning (vif, "gso is not enabled");
1219             }
1220           else
1221             {
1222               vif->gso_enabled = 1;
1223               hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
1224             }
1225         }
1226       if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1227         {
1228           if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1229             virtio_log_warning (vif, "multiqueue is not set");
1230         }
1231     }
1232   return;
1233
1234
1235 error:
1236   virtio_pci_delete_if (vm, vif);
1237   args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1238   args->error = error;
1239 }
1240
1241 int
1242 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1243 {
1244   vnet_main_t *vnm = vnet_get_main ();
1245   virtio_main_t *vim = &virtio_main;
1246   u32 i = 0;
1247
1248   if (vif->type != VIRTIO_IF_TYPE_PCI)
1249     return VNET_API_ERROR_INVALID_INTERFACE;
1250
1251   vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1252
1253   for (i = 0; i < vif->max_queue_pairs; i++)
1254     {
1255       virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1256       virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1257     }
1258
1259   if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1260     virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
1261
1262   virtio_pci_legacy_reset (vm, vif);
1263
1264   if (vif->hw_if_index)
1265     {
1266       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1267       vec_foreach_index (i, vif->rxq_vrings)
1268       {
1269         vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1270       }
1271       ethernet_delete_interface (vnm, vif->hw_if_index);
1272     }
1273
1274   vlib_pci_device_close (vm, vif->pci_dev_handle);
1275
1276   vec_foreach_index (i, vif->rxq_vrings)
1277   {
1278     virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1279     if (vring->kick_fd != -1)
1280       close (vring->kick_fd);
1281     if (vring->used)
1282       {
1283         virtio_free_rx_buffers (vm, vring);
1284       }
1285     vec_free (vring->buffers);
1286     vlib_physmem_free (vm, vring->desc);
1287   }
1288
1289   vec_foreach_index (i, vif->txq_vrings)
1290   {
1291     virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1292     if (vring->kick_fd != -1)
1293       close (vring->kick_fd);
1294     if (vring->used)
1295       {
1296         virtio_free_used_desc (vm, vring);
1297       }
1298     vec_free (vring->buffers);
1299     clib_spinlock_free (&vring->lockp);
1300     vlib_physmem_free (vm, vring->desc);
1301   }
1302
1303   if (vif->cxq_vring != NULL)
1304     {
1305       u16 last = vif->cxq_vring->last_used_idx;
1306       u16 n_left = vif->cxq_vring->used->idx - last;
1307       while (n_left)
1308         {
1309           last++;
1310           n_left--;
1311         }
1312
1313       vif->cxq_vring->last_used_idx = last;
1314       vlib_physmem_free (vm, vif->cxq_vring->desc);
1315     }
1316
1317   vec_free (vif->rxq_vrings);
1318   vec_free (vif->txq_vrings);
1319   vec_free (vif->cxq_vring);
1320
1321   clib_error_free (vif->error);
1322   memset (vif, 0, sizeof (*vif));
1323   pool_put (vim->interfaces, vif);
1324
1325   return 0;
1326 }
1327
1328 /*
1329  * fd.io coding-style-patch-verification: ON
1330  *
1331  * Local Variables:
1332  * eval: (c-set-style "gnu")
1333  * End:
1334  */