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