vcl: allow more rx events on peek
[vpp.git] / src / plugins / avf / device.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vppinfra/ring.h>
20 #include <vlib/unix/unix.h>
21 #include <vlib/pci/pci.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/interface/rx_queue_funcs.h>
24 #include <vnet/interface/tx_queue_funcs.h>
25
26 #include <avf/avf.h>
27
28 #define AVF_MBOX_LEN 64
29 #define AVF_MBOX_BUF_SZ 4096
30 #define AVF_RXQ_SZ 512
31 #define AVF_TXQ_SZ 512
32 #define AVF_ITR_INT 250
33
34 #define PCI_VENDOR_ID_INTEL                     0x8086
35 #define PCI_DEVICE_ID_INTEL_AVF                 0x1889
36 #define PCI_DEVICE_ID_INTEL_X710_VF             0x154c
37 #define PCI_DEVICE_ID_INTEL_X722_VF             0x37cd
38
39 VLIB_REGISTER_LOG_CLASS (avf_log) = {
40   .class_name = "avf",
41 };
42
43 VLIB_REGISTER_LOG_CLASS (avf_stats_log) = {
44   .class_name = "avf",
45   .subclass_name = "stats",
46 };
47
48 avf_main_t avf_main;
49 void avf_delete_if (vlib_main_t * vm, avf_device_t * ad, int with_barrier);
50
51 static pci_device_id_t avf_pci_device_ids[] = {
52   {.vendor_id = PCI_VENDOR_ID_INTEL,.device_id = PCI_DEVICE_ID_INTEL_AVF},
53   {.vendor_id = PCI_VENDOR_ID_INTEL,.device_id = PCI_DEVICE_ID_INTEL_X710_VF},
54   {.vendor_id = PCI_VENDOR_ID_INTEL,.device_id = PCI_DEVICE_ID_INTEL_X722_VF},
55   {0},
56 };
57
58 const static char *virtchnl_event_names[] = {
59 #define _(v, n) [v] = #n,
60   foreach_virtchnl_event_code
61 #undef _
62 };
63
64 typedef enum
65 {
66   AVF_IRQ_STATE_DISABLED,
67   AVF_IRQ_STATE_ENABLED,
68   AVF_IRQ_STATE_WB_ON_ITR,
69 } avf_irq_state_t;
70
71 static inline void
72 avf_irq_0_set_state (avf_device_t * ad, avf_irq_state_t state)
73 {
74   u32 dyn_ctl0 = 0, icr0_ena = 0;
75
76   dyn_ctl0 |= (3 << 3);         /* 11b = No ITR update */
77
78   avf_reg_write (ad, AVFINT_ICR0_ENA1, icr0_ena);
79   avf_reg_write (ad, AVFINT_DYN_CTL0, dyn_ctl0);
80   avf_reg_flush (ad);
81
82   if (state == AVF_IRQ_STATE_DISABLED)
83     return;
84
85   dyn_ctl0 = 0;
86   icr0_ena = 0;
87
88   icr0_ena |= (1 << 30);        /* [30] Admin Queue Enable */
89
90   dyn_ctl0 |= (1 << 0);         /* [0] Interrupt Enable */
91   dyn_ctl0 |= (1 << 1);         /* [1] Clear PBA */
92   dyn_ctl0 |= (2 << 3);         /* [4:3] ITR Index, 11b = No ITR update */
93   dyn_ctl0 |= ((AVF_ITR_INT / 2) << 5); /* [16:5] ITR Interval in 2us steps */
94
95   avf_reg_write (ad, AVFINT_ICR0_ENA1, icr0_ena);
96   avf_reg_write (ad, AVFINT_DYN_CTL0, dyn_ctl0);
97   avf_reg_flush (ad);
98 }
99
100 static inline void
101 avf_irq_n_set_state (avf_device_t * ad, u8 line, avf_irq_state_t state)
102 {
103   u32 dyn_ctln = 0;
104
105   /* disable */
106   avf_reg_write (ad, AVFINT_DYN_CTLN (line), dyn_ctln);
107   avf_reg_flush (ad);
108
109   if (state == AVF_IRQ_STATE_DISABLED)
110     return;
111
112   dyn_ctln |= (1 << 1);         /* [1] Clear PBA */
113   if (state == AVF_IRQ_STATE_WB_ON_ITR)
114     {
115       /* minimal ITR interval, use ITR1 */
116       dyn_ctln |= (1 << 3);     /* [4:3] ITR Index */
117       dyn_ctln |= ((32 / 2) << 5);      /* [16:5] ITR Interval in 2us steps */
118       dyn_ctln |= (1 << 30);    /* [30] Writeback on ITR */
119     }
120   else
121     {
122       /* configured ITR interval, use ITR0 */
123       dyn_ctln |= (1 << 0);     /* [0] Interrupt Enable */
124       dyn_ctln |= ((AVF_ITR_INT / 2) << 5);     /* [16:5] ITR Interval in 2us steps */
125     }
126
127   avf_reg_write (ad, AVFINT_DYN_CTLN (line), dyn_ctln);
128   avf_reg_flush (ad);
129 }
130
131
132 clib_error_t *
133 avf_aq_desc_enq (vlib_main_t * vm, avf_device_t * ad, avf_aq_desc_t * dt,
134                  void *data, int len)
135 {
136   clib_error_t *err = 0;
137   avf_aq_desc_t *d, dc;
138   f64 t0, suspend_time = AVF_AQ_ENQ_SUSPEND_TIME;
139
140   d = &ad->atq[ad->atq_next_slot];
141   clib_memcpy_fast (d, dt, sizeof (avf_aq_desc_t));
142   d->flags |= AVF_AQ_F_RD | AVF_AQ_F_SI;
143   if (len)
144     d->datalen = len;
145   if (len)
146     {
147       u64 pa;
148       pa = ad->atq_bufs_pa + ad->atq_next_slot * AVF_MBOX_BUF_SZ;
149       d->addr_hi = (u32) (pa >> 32);
150       d->addr_lo = (u32) pa;
151       clib_memcpy_fast (ad->atq_bufs + ad->atq_next_slot * AVF_MBOX_BUF_SZ,
152                         data, len);
153       d->flags |= AVF_AQ_F_BUF;
154     }
155
156   if (ad->flags & AVF_DEVICE_F_ELOG)
157     clib_memcpy_fast (&dc, d, sizeof (avf_aq_desc_t));
158
159   ad->atq_next_slot = (ad->atq_next_slot + 1) % AVF_MBOX_LEN;
160   avf_reg_write (ad, AVF_ATQT, ad->atq_next_slot);
161   avf_reg_flush (ad);
162
163   t0 = vlib_time_now (vm);
164 retry:
165   vlib_process_suspend (vm, suspend_time);
166
167   if (((d->flags & AVF_AQ_F_DD) == 0) || ((d->flags & AVF_AQ_F_CMP) == 0))
168     {
169       f64 t = vlib_time_now (vm) - t0;
170       if (t > AVF_AQ_ENQ_MAX_WAIT_TIME)
171         {
172           avf_log_err (ad, "aq_desc_enq failed (timeout %.3fs)", t);
173           err = clib_error_return (0, "adminq enqueue timeout [opcode 0x%x]",
174                                    d->opcode);
175           goto done;
176         }
177       suspend_time *= 2;
178       goto retry;
179     }
180
181   clib_memcpy_fast (dt, d, sizeof (avf_aq_desc_t));
182   if (d->flags & AVF_AQ_F_ERR)
183     return clib_error_return (0, "adminq enqueue error [opcode 0x%x, retval "
184                               "%d]", d->opcode, d->retval);
185
186 done:
187   if (ad->flags & AVF_DEVICE_F_ELOG)
188     {
189       ELOG_TYPE_DECLARE (el) =
190         {
191           .format = "avf[%d] aq enq: s_flags 0x%x r_flags 0x%x opcode 0x%x "
192             "datalen %d retval %d",
193           .format_args = "i4i2i2i2i2i2",
194         };
195       struct
196         {
197           u32 dev_instance;
198           u16 s_flags;
199           u16 r_flags;
200           u16 opcode;
201           u16 datalen;
202           u16 retval;
203         } *ed;
204         ed = ELOG_DATA (&vlib_global_main.elog_main, el);
205         ed->dev_instance = ad->dev_instance;
206         ed->s_flags = dc.flags;
207         ed->r_flags = d->flags;
208         ed->opcode = dc.opcode;
209         ed->datalen = dc.datalen;
210         ed->retval = d->retval;
211     }
212
213   return err;
214 }
215
216 clib_error_t *
217 avf_cmd_rx_ctl_reg_write (vlib_main_t * vm, avf_device_t * ad, u32 reg,
218                           u32 val)
219 {
220   clib_error_t *err;
221   avf_aq_desc_t d = {.opcode = 0x207,.param1 = reg,.param3 = val };
222   err = avf_aq_desc_enq (vm, ad, &d, 0, 0);
223
224   if (ad->flags & AVF_DEVICE_F_ELOG)
225     {
226       ELOG_TYPE_DECLARE (el) =
227         {
228           .format = "avf[%d] rx ctl reg write: reg 0x%x val 0x%x ",
229           .format_args = "i4i4i4",
230         };
231       struct
232         {
233           u32 dev_instance;
234           u32 reg;
235           u32 val;
236         } *ed;
237         ed = ELOG_DATA (&vlib_global_main.elog_main, el);
238         ed->dev_instance = ad->dev_instance;
239         ed->reg = reg;
240         ed->val = val;
241     }
242   return err;
243 }
244
245 clib_error_t *
246 avf_rxq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 rxq_size)
247 {
248   clib_error_t *err;
249   avf_rxq_t *rxq;
250   u32 n_alloc, i;
251
252   vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES);
253   rxq = vec_elt_at_index (ad->rxqs, qid);
254   rxq->size = rxq_size;
255   rxq->next = 0;
256   rxq->descs = vlib_physmem_alloc_aligned_on_numa (vm, rxq->size *
257                                                    sizeof (avf_rx_desc_t),
258                                                    2 * CLIB_CACHE_LINE_BYTES,
259                                                    ad->numa_node);
260
261   rxq->buffer_pool_index =
262     vlib_buffer_pool_get_default_for_numa (vm, ad->numa_node);
263
264   if (rxq->descs == 0)
265     return vlib_physmem_last_error (vm);
266
267   if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) rxq->descs)))
268     return err;
269
270   clib_memset ((void *) rxq->descs, 0, rxq->size * sizeof (avf_rx_desc_t));
271   vec_validate_aligned (rxq->bufs, rxq->size, CLIB_CACHE_LINE_BYTES);
272   rxq->qrx_tail = ad->bar0 + AVF_QRX_TAIL (qid);
273
274   n_alloc = vlib_buffer_alloc_from_pool (vm, rxq->bufs, rxq->size - 8,
275                                          rxq->buffer_pool_index);
276
277   if (n_alloc == 0)
278     return clib_error_return (0, "buffer allocation error");
279
280   rxq->n_enqueued = n_alloc;
281   avf_rx_desc_t *d = rxq->descs;
282   for (i = 0; i < n_alloc; i++)
283     {
284       vlib_buffer_t *b = vlib_get_buffer (vm, rxq->bufs[i]);
285       if (ad->flags & AVF_DEVICE_F_VA_DMA)
286         d->qword[0] = vlib_buffer_get_va (b);
287       else
288         d->qword[0] = vlib_buffer_get_pa (vm, b);
289       d++;
290     }
291
292   return 0;
293 }
294
295 clib_error_t *
296 avf_txq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 txq_size)
297 {
298   clib_error_t *err;
299   avf_txq_t *txq;
300   u16 n;
301   u8 bpi = vlib_buffer_pool_get_default_for_numa (vm,
302                                                   ad->numa_node);
303
304   vec_validate_aligned (ad->txqs, qid, CLIB_CACHE_LINE_BYTES);
305   txq = vec_elt_at_index (ad->txqs, qid);
306   txq->size = txq_size;
307   txq->next = 0;
308   clib_spinlock_init (&txq->lock);
309
310   /* Prepare a placeholder buffer(s) to maintain a 1-1 relationship between
311    * bufs and descs when a context descriptor is added in descs. Worst case
312    * every second descriptor is context descriptor and due to b->ref_count
313    * being u8 we need one for each block of 510 descriptors */
314
315   n = (txq->size / 510) + 1;
316   vec_validate_aligned (txq->ph_bufs, n, CLIB_CACHE_LINE_BYTES);
317
318   if (!vlib_buffer_alloc_from_pool (vm, txq->ph_bufs, n, bpi))
319     return clib_error_return (0, "buffer allocation error");
320
321   txq->descs = vlib_physmem_alloc_aligned_on_numa (vm, txq->size *
322                                                    sizeof (avf_tx_desc_t),
323                                                    2 * CLIB_CACHE_LINE_BYTES,
324                                                    ad->numa_node);
325   if (txq->descs == 0)
326     return vlib_physmem_last_error (vm);
327
328   if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) txq->descs)))
329     return err;
330
331   vec_validate_aligned (txq->bufs, txq->size, CLIB_CACHE_LINE_BYTES);
332   txq->qtx_tail = ad->bar0 + AVF_QTX_TAIL (qid);
333
334   /* initialize ring of pending RS slots */
335   clib_ring_new_aligned (txq->rs_slots, 32, CLIB_CACHE_LINE_BYTES);
336
337   vec_validate_aligned (txq->tmp_descs, txq->size, CLIB_CACHE_LINE_BYTES);
338   vec_validate_aligned (txq->tmp_bufs, txq->size, CLIB_CACHE_LINE_BYTES);
339
340   return 0;
341 }
342
343 typedef struct
344 {
345   u16 vsi_id;
346   u16 flags;
347 } virtchnl_promisc_info_t;
348
349 void
350 avf_arq_slot_init (avf_device_t * ad, u16 slot)
351 {
352   avf_aq_desc_t *d;
353   u64 pa = ad->arq_bufs_pa + slot * AVF_MBOX_BUF_SZ;
354   d = &ad->arq[slot];
355   clib_memset (d, 0, sizeof (avf_aq_desc_t));
356   d->flags = AVF_AQ_F_BUF;
357   d->datalen = AVF_MBOX_BUF_SZ;
358   d->addr_hi = (u32) (pa >> 32);
359   d->addr_lo = (u32) pa;
360 }
361
362 static inline uword
363 avf_dma_addr (vlib_main_t * vm, avf_device_t * ad, void *p)
364 {
365   return (ad->flags & AVF_DEVICE_F_VA_DMA) ?
366     pointer_to_uword (p) : vlib_physmem_get_pa (vm, p);
367 }
368
369 static void
370 avf_adminq_init (vlib_main_t * vm, avf_device_t * ad)
371 {
372   u64 pa;
373   int i;
374
375   /* VF MailBox Transmit */
376   clib_memset (ad->atq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN);
377   ad->atq_bufs_pa = avf_dma_addr (vm, ad, ad->atq_bufs);
378
379   pa = avf_dma_addr (vm, ad, ad->atq);
380   avf_reg_write (ad, AVF_ATQT, 0);      /* Tail */
381   avf_reg_write (ad, AVF_ATQH, 0);      /* Head */
382   avf_reg_write (ad, AVF_ATQLEN, AVF_MBOX_LEN | (1ULL << 31));  /* len & ena */
383   avf_reg_write (ad, AVF_ATQBAL, (u32) pa);     /* Base Address Low */
384   avf_reg_write (ad, AVF_ATQBAH, (u32) (pa >> 32));     /* Base Address High */
385
386   /* VF MailBox Receive */
387   clib_memset (ad->arq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN);
388   ad->arq_bufs_pa = avf_dma_addr (vm, ad, ad->arq_bufs);
389
390   for (i = 0; i < AVF_MBOX_LEN; i++)
391     avf_arq_slot_init (ad, i);
392
393   pa = avf_dma_addr (vm, ad, ad->arq);
394
395   avf_reg_write (ad, AVF_ARQH, 0);      /* Head */
396   avf_reg_write (ad, AVF_ARQT, 0);      /* Head */
397   avf_reg_write (ad, AVF_ARQLEN, AVF_MBOX_LEN | (1ULL << 31));  /* len & ena */
398   avf_reg_write (ad, AVF_ARQBAL, (u32) pa);     /* Base Address Low */
399   avf_reg_write (ad, AVF_ARQBAH, (u32) (pa >> 32));     /* Base Address High */
400   avf_reg_write (ad, AVF_ARQT, AVF_MBOX_LEN - 1);       /* Tail */
401
402   ad->atq_next_slot = 0;
403   ad->arq_next_slot = 0;
404 }
405
406 clib_error_t *
407 avf_send_to_pf (vlib_main_t * vm, avf_device_t * ad, virtchnl_ops_t op,
408                 void *in, int in_len, void *out, int out_len)
409 {
410   clib_error_t *err;
411   avf_aq_desc_t *d, dt = {.opcode = 0x801,.v_opcode = op };
412   u32 head;
413   f64 t0, suspend_time = AVF_SEND_TO_PF_SUSPEND_TIME;
414
415   /* adminq operations should be only done from process node after device
416    * is initialized */
417   ASSERT ((ad->flags & AVF_DEVICE_F_INITIALIZED) == 0 ||
418           vlib_get_current_process_node_index (vm) == avf_process_node.index);
419
420   /* suppress interrupt in the next adminq receive slot
421      as we are going to wait for response
422      we only need interrupts when event is received */
423   d = &ad->arq[ad->arq_next_slot];
424   d->flags |= AVF_AQ_F_SI;
425
426   if ((err = avf_aq_desc_enq (vm, ad, &dt, in, in_len)))
427     return err;
428
429   t0 = vlib_time_now (vm);
430 retry:
431   head = avf_get_u32 (ad->bar0, AVF_ARQH);
432
433   if (ad->arq_next_slot == head)
434     {
435       f64 t = vlib_time_now (vm) - t0;
436       if (t > AVF_SEND_TO_PF_MAX_WAIT_TIME)
437         {
438           avf_log_err (ad, "send_to_pf failed (timeout %.3fs)", t);
439           return clib_error_return (0, "timeout");
440         }
441       vlib_process_suspend (vm, suspend_time);
442       suspend_time *= 2;
443       goto retry;
444     }
445
446   d = &ad->arq[ad->arq_next_slot];
447
448   if (d->v_opcode == VIRTCHNL_OP_EVENT)
449     {
450       void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ;
451       virtchnl_pf_event_t *e;
452
453       if ((d->datalen != sizeof (virtchnl_pf_event_t)) ||
454           ((d->flags & AVF_AQ_F_BUF) == 0))
455         return clib_error_return (0, "event message error");
456
457       vec_add2 (ad->events, e, 1);
458       clib_memcpy_fast (e, buf, sizeof (virtchnl_pf_event_t));
459       avf_arq_slot_init (ad, ad->arq_next_slot);
460       ad->arq_next_slot++;
461       /* reset timer */
462       t0 = vlib_time_now (vm);
463       suspend_time = AVF_SEND_TO_PF_SUSPEND_TIME;
464       goto retry;
465     }
466
467   if (d->v_opcode != op)
468     {
469       err = clib_error_return (0,
470                                "unexpected message received [v_opcode = %u, "
471                                "expected %u, v_retval %d]",
472                                d->v_opcode, op, d->v_retval);
473       goto done;
474     }
475
476   if (d->v_retval)
477     {
478       err = clib_error_return (0, "error [v_opcode = %u, v_retval %d]",
479                                d->v_opcode, d->v_retval);
480       goto done;
481     }
482
483   if (out_len && d->flags & AVF_AQ_F_BUF)
484     {
485       void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ;
486       clib_memcpy_fast (out, buf, out_len);
487     }
488
489   avf_arq_slot_init (ad, ad->arq_next_slot);
490   avf_reg_write (ad, AVF_ARQT, ad->arq_next_slot);
491   avf_reg_flush (ad);
492   ad->arq_next_slot = (ad->arq_next_slot + 1) % AVF_MBOX_LEN;
493
494 done:
495
496   if (ad->flags & AVF_DEVICE_F_ELOG)
497     {
498       ELOG_TYPE_DECLARE (el) =
499         {
500           .format = "avf[%d] send to pf: v_opcode %s (%d) v_retval 0x%x",
501           .format_args = "i4t4i4i4",
502           .n_enum_strings = VIRTCHNL_N_OPS,
503           .enum_strings = {
504 #define _(v, n) [v] = #n,
505               foreach_virtchnl_op
506 #undef _
507           },
508         };
509       struct
510         {
511           u32 dev_instance;
512           u32 v_opcode;
513           u32 v_opcode_val;
514           u32 v_retval;
515         } *ed;
516         ed = ELOG_DATA (&vlib_global_main.elog_main, el);
517         ed->dev_instance = ad->dev_instance;
518         ed->v_opcode = op;
519         ed->v_opcode_val = op;
520         ed->v_retval = d->v_retval;
521     }
522   return err;
523 }
524
525 clib_error_t *
526 avf_op_version (vlib_main_t * vm, avf_device_t * ad,
527                 virtchnl_version_info_t * ver)
528 {
529   clib_error_t *err = 0;
530   virtchnl_version_info_t myver = {
531     .major = VIRTCHNL_VERSION_MAJOR,
532     .minor = VIRTCHNL_VERSION_MINOR,
533   };
534
535   avf_log_debug (ad, "version: major %u minor %u", myver.major, myver.minor);
536
537   err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_VERSION, &myver,
538                         sizeof (virtchnl_version_info_t), ver,
539                         sizeof (virtchnl_version_info_t));
540
541   if (err)
542     return err;
543
544   return err;
545 }
546
547 clib_error_t *
548 avf_op_get_vf_resources (vlib_main_t * vm, avf_device_t * ad,
549                          virtchnl_vf_resource_t * res)
550 {
551   clib_error_t *err = 0;
552   u32 bitmap = (VIRTCHNL_VF_OFFLOAD_L2 | VIRTCHNL_VF_OFFLOAD_RSS_PF |
553                 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR | VIRTCHNL_VF_OFFLOAD_VLAN |
554                 VIRTCHNL_VF_OFFLOAD_RX_POLLING |
555                 VIRTCHNL_VF_CAP_ADV_LINK_SPEED | VIRTCHNL_VF_OFFLOAD_FDIR_PF |
556                 VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF | VIRTCHNL_VF_OFFLOAD_VLAN_V2);
557
558   avf_log_debug (ad, "get_vf_resources: bitmap 0x%x (%U)", bitmap,
559                  format_avf_vf_cap_flags, bitmap);
560   err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_VF_RESOURCES, &bitmap,
561                         sizeof (u32), res, sizeof (virtchnl_vf_resource_t));
562
563   if (err == 0)
564     {
565       int i;
566       avf_log_debug (ad,
567                      "get_vf_resources: num_vsis %u num_queue_pairs %u "
568                      "max_vectors %u max_mtu %u vf_cap_flags 0x%x (%U) "
569                      "rss_key_size %u rss_lut_size %u",
570                      res->num_vsis, res->num_queue_pairs, res->max_vectors,
571                      res->max_mtu, res->vf_cap_flags, format_avf_vf_cap_flags,
572                      res->vf_cap_flags, res->rss_key_size, res->rss_lut_size);
573       for (i = 0; i < res->num_vsis; i++)
574         avf_log_debug (
575           ad,
576           "get_vf_resources_vsi[%u]: vsi_id %u num_queue_pairs %u vsi_type %u "
577           "qset_handle %u default_mac_addr %U",
578           i, res->vsi_res[i].vsi_id, res->vsi_res[i].num_queue_pairs,
579           res->vsi_res[i].vsi_type, res->vsi_res[i].qset_handle,
580           format_ethernet_address, res->vsi_res[i].default_mac_addr);
581     }
582
583   return err;
584 }
585
586 clib_error_t *
587 avf_op_config_rss_lut (vlib_main_t * vm, avf_device_t * ad)
588 {
589   int msg_len = sizeof (virtchnl_rss_lut_t) + ad->rss_lut_size - 1;
590   int i;
591   u8 msg[msg_len];
592   virtchnl_rss_lut_t *rl;
593
594   clib_memset (msg, 0, msg_len);
595   rl = (virtchnl_rss_lut_t *) msg;
596   rl->vsi_id = ad->vsi_id;
597   rl->lut_entries = ad->rss_lut_size;
598   for (i = 0; i < ad->rss_lut_size; i++)
599     rl->lut[i] = i % ad->n_rx_queues;
600
601   avf_log_debug (ad, "config_rss_lut: vsi_id %u rss_lut_size %u lut 0x%U",
602                  rl->vsi_id, rl->lut_entries, format_hex_bytes_no_wrap,
603                  rl->lut, rl->lut_entries);
604
605   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_LUT, msg, msg_len, 0,
606                          0);
607 }
608
609 clib_error_t *
610 avf_op_config_rss_key (vlib_main_t * vm, avf_device_t * ad)
611 {
612   /* from DPDK i40e... */
613   static uint32_t rss_key_default[] = { 0x6b793944, 0x23504cb5, 0x5bea75b6,
614                                         0x309f4f12, 0x3dc0a2b8, 0x024ddcdf,
615                                         0x339b8ca0, 0x4c4af64a, 0x34fac605,
616                                         0x55d85839, 0x3a58997d, 0x2ec938e1,
617                                         0x66031581 };
618   int msg_len = sizeof (virtchnl_rss_key_t) + ad->rss_key_size - 1;
619   u8 msg[msg_len];
620   virtchnl_rss_key_t *rk;
621
622   if (sizeof (rss_key_default) != ad->rss_key_size)
623     return clib_error_create ("unsupported RSS key size (expected %d, got %d)",
624                               sizeof (rss_key_default), ad->rss_key_size);
625
626   clib_memset (msg, 0, msg_len);
627   rk = (virtchnl_rss_key_t *) msg;
628   rk->vsi_id = ad->vsi_id;
629   rk->key_len = ad->rss_key_size;
630   memcpy_s (rk->key, rk->key_len, rss_key_default, sizeof (rss_key_default));
631
632   avf_log_debug (ad, "config_rss_key: vsi_id %u rss_key_size %u key 0x%U",
633                  rk->vsi_id, rk->key_len, format_hex_bytes_no_wrap, rk->key,
634                  rk->key_len);
635
636   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_KEY, msg, msg_len, 0,
637                          0);
638 }
639
640 clib_error_t *
641 avf_op_disable_vlan_stripping (vlib_main_t * vm, avf_device_t * ad)
642 {
643   avf_log_debug (ad, "disable_vlan_stripping");
644
645   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, 0, 0, 0,
646                          0);
647 }
648
649 clib_error_t *
650 avf_op_config_promisc_mode (vlib_main_t * vm, avf_device_t * ad,
651                             int is_enable)
652 {
653   virtchnl_promisc_info_t pi = { 0 };
654
655   pi.vsi_id = ad->vsi_id;
656
657   if (is_enable)
658     pi.flags = FLAG_VF_UNICAST_PROMISC | FLAG_VF_MULTICAST_PROMISC;
659
660   avf_log_debug (ad, "config_promisc_mode: unicast %s multicast %s",
661                  pi.flags & FLAG_VF_UNICAST_PROMISC ? "on" : "off",
662                  pi.flags & FLAG_VF_MULTICAST_PROMISC ? "on" : "off");
663
664   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE, &pi,
665                          sizeof (virtchnl_promisc_info_t), 0, 0);
666 }
667
668
669 clib_error_t *
670 avf_op_config_vsi_queues (vlib_main_t * vm, avf_device_t * ad)
671 {
672   int i;
673   int n_qp = clib_max (vec_len (ad->rxqs), vec_len (ad->txqs));
674   int msg_len = sizeof (virtchnl_vsi_queue_config_info_t) + n_qp *
675     sizeof (virtchnl_queue_pair_info_t);
676   u8 msg[msg_len];
677   virtchnl_vsi_queue_config_info_t *ci;
678
679   clib_memset (msg, 0, msg_len);
680   ci = (virtchnl_vsi_queue_config_info_t *) msg;
681   ci->vsi_id = ad->vsi_id;
682   ci->num_queue_pairs = n_qp;
683
684   avf_log_debug (ad, "config_vsi_queues: vsi_id %u num_queue_pairs %u",
685                  ad->vsi_id, ci->num_queue_pairs);
686
687   for (i = 0; i < n_qp; i++)
688     {
689       virtchnl_txq_info_t *txq = &ci->qpair[i].txq;
690       virtchnl_rxq_info_t *rxq = &ci->qpair[i].rxq;
691
692       rxq->vsi_id = ad->vsi_id;
693       rxq->queue_id = i;
694       rxq->max_pkt_size = ETHERNET_MAX_PACKET_BYTES;
695       if (i < vec_len (ad->rxqs))
696         {
697           avf_rxq_t *q = vec_elt_at_index (ad->rxqs, i);
698           rxq->ring_len = q->size;
699           rxq->databuffer_size = vlib_buffer_get_default_data_size (vm);
700           rxq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs);
701           avf_reg_write (ad, AVF_QRX_TAIL (i), q->size - 1);
702         }
703       avf_log_debug (ad, "config_vsi_queues_rx[%u]: max_pkt_size %u "
704                      "ring_len %u databuffer_size %u dma_ring_addr 0x%llx",
705                      i, rxq->max_pkt_size, rxq->ring_len,
706                      rxq->databuffer_size, rxq->dma_ring_addr);
707
708       txq->vsi_id = ad->vsi_id;
709       txq->queue_id = i;
710       if (i < vec_len (ad->txqs))
711         {
712           avf_txq_t *q = vec_elt_at_index (ad->txqs, i);
713           txq->ring_len = q->size;
714           txq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs);
715         }
716       avf_log_debug (ad, "config_vsi_queues_tx[%u]: ring_len %u "
717                      "dma_ring_addr 0x%llx", i, txq->ring_len,
718                      txq->dma_ring_addr);
719     }
720
721   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_VSI_QUEUES, msg, msg_len,
722                          0, 0);
723 }
724
725 clib_error_t *
726 avf_op_config_irq_map (vlib_main_t * vm, avf_device_t * ad)
727 {
728   int msg_len = sizeof (virtchnl_irq_map_info_t) +
729     (ad->n_rx_irqs) * sizeof (virtchnl_vector_map_t);
730   u8 msg[msg_len];
731   virtchnl_irq_map_info_t *imi;
732
733   clib_memset (msg, 0, msg_len);
734   imi = (virtchnl_irq_map_info_t *) msg;
735   imi->num_vectors = ad->n_rx_irqs;
736
737   for (int i = 0; i < ad->n_rx_irqs; i++)
738     {
739       imi->vecmap[i].vector_id = i + 1;
740       imi->vecmap[i].vsi_id = ad->vsi_id;
741       if (ad->n_rx_irqs == ad->n_rx_queues)
742         imi->vecmap[i].rxq_map = 1 << i;
743       else
744         imi->vecmap[i].rxq_map = pow2_mask (ad->n_rx_queues);;
745
746       avf_log_debug (ad, "config_irq_map[%u/%u]: vsi_id %u vector_id %u "
747                      "rxq_map %u", i, ad->n_rx_irqs - 1, ad->vsi_id,
748                      imi->vecmap[i].vector_id, imi->vecmap[i].rxq_map);
749     }
750
751
752   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_IRQ_MAP, msg, msg_len, 0,
753                          0);
754 }
755
756 clib_error_t *
757 avf_op_add_del_eth_addr (vlib_main_t * vm, avf_device_t * ad, u8 count,
758                          u8 * macs, int is_add)
759 {
760   int msg_len =
761     sizeof (virtchnl_ether_addr_list_t) +
762     count * sizeof (virtchnl_ether_addr_t);
763   u8 msg[msg_len];
764   virtchnl_ether_addr_list_t *al;
765   int i;
766
767   clib_memset (msg, 0, msg_len);
768   al = (virtchnl_ether_addr_list_t *) msg;
769   al->vsi_id = ad->vsi_id;
770   al->num_elements = count;
771
772   avf_log_debug (ad, "add_del_eth_addr: vsi_id %u num_elements %u is_add %u",
773                  ad->vsi_id, al->num_elements, is_add);
774
775   for (i = 0; i < count; i++)
776     {
777       clib_memcpy_fast (&al->list[i].addr, macs + i * 6, 6);
778       avf_log_debug (ad, "add_del_eth_addr[%u]: %U", i,
779                      format_ethernet_address, &al->list[i].addr);
780     }
781   return avf_send_to_pf (vm, ad, is_add ? VIRTCHNL_OP_ADD_ETH_ADDR :
782                          VIRTCHNL_OP_DEL_ETH_ADDR, msg, msg_len, 0, 0);
783 }
784
785 clib_error_t *
786 avf_op_enable_queues (vlib_main_t * vm, avf_device_t * ad, u32 rx, u32 tx)
787 {
788   virtchnl_queue_select_t qs = { 0 };
789   int i = 0;
790   qs.vsi_id = ad->vsi_id;
791   qs.rx_queues = rx;
792   qs.tx_queues = tx;
793
794   avf_log_debug (ad, "enable_queues: vsi_id %u rx_queues %u tx_queues %u",
795                  ad->vsi_id, qs.rx_queues, qs.tx_queues);
796
797   while (rx)
798     {
799       if (rx & (1 << i))
800         {
801           avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
802           avf_reg_write (ad, AVF_QRX_TAIL (i), rxq->n_enqueued);
803           rx &= ~(1 << i);
804         }
805       i++;
806     }
807   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ENABLE_QUEUES, &qs,
808                          sizeof (virtchnl_queue_select_t), 0, 0);
809 }
810
811 clib_error_t *
812 avf_op_get_stats (vlib_main_t * vm, avf_device_t * ad,
813                   virtchnl_eth_stats_t * es)
814 {
815   virtchnl_queue_select_t qs = { 0 };
816   clib_error_t *err;
817   qs.vsi_id = ad->vsi_id;
818
819   err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_STATS, &qs,
820                         sizeof (virtchnl_queue_select_t), es,
821                         sizeof (virtchnl_eth_stats_t));
822
823   avf_stats_log_debug (ad, "get_stats: vsi_id %u\n  %U", ad->vsi_id,
824                        format_avf_eth_stats, es);
825
826   return err;
827 }
828
829 clib_error_t *
830 avf_op_get_offload_vlan_v2_caps (vlib_main_t *vm, avf_device_t *ad,
831                                  virtchnl_vlan_caps_t *vc)
832 {
833   clib_error_t *err;
834
835   err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS, 0, 0, vc,
836                         sizeof (virtchnl_vlan_caps_t));
837
838   avf_log_debug (ad, "get_offload_vlan_v2_caps:\n%U%U", format_white_space, 16,
839                  format_avf_vlan_caps, vc);
840
841   return err;
842 }
843
844 clib_error_t *
845 avf_op_disable_vlan_stripping_v2 (vlib_main_t *vm, avf_device_t *ad, u32 outer,
846                                   u32 inner)
847 {
848   virtchnl_vlan_setting_t vs = {
849     .outer_ethertype_setting = outer,
850     .inner_ethertype_setting = inner,
851     .vport_id = ad->vsi_id,
852   };
853
854   avf_log_debug (ad, "disable_vlan_stripping_v2: outer: %U, inner %U",
855                  format_avf_vlan_support, outer, format_avf_vlan_support,
856                  inner);
857
858   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2, &vs,
859                          sizeof (virtchnl_vlan_setting_t), 0, 0);
860 }
861
862 clib_error_t *
863 avf_device_reset (vlib_main_t * vm, avf_device_t * ad)
864 {
865   avf_aq_desc_t d = { 0 };
866   clib_error_t *error;
867   u32 rstat;
868   f64 t0, t = 0, suspend_time = AVF_RESET_SUSPEND_TIME;
869
870   avf_log_debug (ad, "reset");
871
872   d.opcode = 0x801;
873   d.v_opcode = VIRTCHNL_OP_RESET_VF;
874   if ((error = avf_aq_desc_enq (vm, ad, &d, 0, 0)))
875     return error;
876
877   t0 = vlib_time_now (vm);
878 retry:
879   vlib_process_suspend (vm, suspend_time);
880
881   rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
882
883   if (rstat == 2 || rstat == 3)
884     {
885       avf_log_debug (ad, "reset completed in %.3fs", t);
886       return 0;
887     }
888
889   t = vlib_time_now (vm) - t0;
890   if (t > AVF_RESET_MAX_WAIT_TIME)
891     {
892       avf_log_err (ad, "reset failed (timeout %.3fs)", t);
893       return clib_error_return (0, "reset failed (timeout)");
894     }
895
896   suspend_time *= 2;
897   goto retry;
898 }
899
900 clib_error_t *
901 avf_request_queues (vlib_main_t * vm, avf_device_t * ad, u16 num_queue_pairs)
902 {
903   virtchnl_vf_res_request_t res_req = { 0 };
904   clib_error_t *error;
905   u32 rstat;
906   f64 t0, t, suspend_time = AVF_RESET_SUSPEND_TIME;
907
908   res_req.num_queue_pairs = num_queue_pairs;
909
910   avf_log_debug (ad, "request_queues: num_queue_pairs %u", num_queue_pairs);
911
912   error = avf_send_to_pf (vm, ad, VIRTCHNL_OP_REQUEST_QUEUES, &res_req,
913                           sizeof (virtchnl_vf_res_request_t), &res_req,
914                           sizeof (virtchnl_vf_res_request_t));
915
916   /*
917    * if PF responds, the request failed
918    * else PF initializes restart and avf_send_to_pf returns an error
919    */
920   if (!error)
921     {
922       return clib_error_return (0, "requested more than %u queue pairs",
923                                 res_req.num_queue_pairs);
924     }
925
926   t0 = vlib_time_now (vm);
927 retry:
928   vlib_process_suspend (vm, suspend_time);
929   t = vlib_time_now (vm) - t0;
930
931   rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
932
933   if ((rstat == VIRTCHNL_VFR_COMPLETED) || (rstat == VIRTCHNL_VFR_VFACTIVE))
934     goto done;
935
936   if (t > AVF_RESET_MAX_WAIT_TIME)
937     {
938       avf_log_err (ad, "request queues failed (timeout %.3f seconds)", t);
939       return clib_error_return (0, "request queues failed (timeout)");
940     }
941
942   suspend_time *= 2;
943   goto retry;
944
945 done:
946   return NULL;
947 }
948
949 clib_error_t *
950 avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad,
951                  avf_create_if_args_t * args)
952 {
953   virtchnl_version_info_t ver = { 0 };
954   virtchnl_vf_resource_t res = { 0 };
955   clib_error_t *error;
956   int i, wb_on_itr;
957   u16 rxq_num, txq_num;
958
959   avf_adminq_init (vm, ad);
960
961   rxq_num = args->rxq_num ? args->rxq_num : 1;
962   txq_num = args->txq_num ? args->txq_num : vlib_get_n_threads ();
963
964   if ((error = avf_request_queues (vm, ad, clib_max (txq_num, rxq_num))))
965     {
966       /* we failed to get more queues, but still we want to proceed */
967       clib_error_free (error);
968
969       if ((error = avf_device_reset (vm, ad)))
970         return error;
971     }
972
973   avf_adminq_init (vm, ad);
974
975   /*
976    * OP_VERSION
977    */
978   if ((error = avf_op_version (vm, ad, &ver)))
979     return error;
980
981   if (ver.major != VIRTCHNL_VERSION_MAJOR ||
982       ver.minor != VIRTCHNL_VERSION_MINOR)
983     return clib_error_return (0, "incompatible protocol version "
984                               "(remote %d.%d)", ver.major, ver.minor);
985
986   /*
987    * OP_GET_VF_RESOURCES
988    */
989   if ((error = avf_op_get_vf_resources (vm, ad, &res)))
990     return error;
991
992   if (res.num_vsis != 1 || res.vsi_res[0].vsi_type != VIRTCHNL_VSI_SRIOV)
993     return clib_error_return (0, "unexpected GET_VF_RESOURCE reply received");
994
995   ad->vsi_id = res.vsi_res[0].vsi_id;
996   ad->cap_flags = res.vf_cap_flags;
997   ad->num_queue_pairs = res.num_queue_pairs;
998   ad->n_rx_queues = clib_min (rxq_num, res.num_queue_pairs);
999   ad->n_tx_queues = clib_min (txq_num, res.num_queue_pairs);
1000   ad->max_vectors = res.max_vectors;
1001   ad->max_mtu = res.max_mtu;
1002   ad->rss_key_size = res.rss_key_size;
1003   ad->rss_lut_size = res.rss_lut_size;
1004   ad->n_rx_irqs = ad->max_vectors > ad->n_rx_queues ? ad->n_rx_queues : 1;
1005
1006   if (ad->max_vectors > ad->n_rx_queues)
1007     ad->flags |= AVF_DEVICE_F_RX_INT;
1008
1009   wb_on_itr = (ad->cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) != 0;
1010
1011   clib_memcpy_fast (ad->hwaddr, res.vsi_res[0].default_mac_addr, 6);
1012
1013   if (args->rxq_num != 0 && ad->n_rx_queues != args->rxq_num)
1014     return clib_error_return (0,
1015                               "Number of requested RX queues (%u) is "
1016                               "higher than mumber of available queues (%u)",
1017                               args->rxq_num, ad->num_queue_pairs);
1018
1019   if (args->txq_num != 0 && ad->n_tx_queues != args->txq_num)
1020     return clib_error_return (0,
1021                               "Number of requested TX queues (%u) is "
1022                               "higher than mumber of available queues (%u)",
1023                               args->txq_num, ad->num_queue_pairs);
1024
1025   /*
1026    * Disable VLAN stripping
1027    */
1028   if (ad->cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2)
1029     {
1030       virtchnl_vlan_caps_t vc = {};
1031       u32 outer = VIRTCHNL_VLAN_UNSUPPORTED, inner = VIRTCHNL_VLAN_UNSUPPORTED;
1032       u32 mask = VIRTCHNL_VLAN_ETHERTYPE_8100;
1033
1034       if ((error = avf_op_get_offload_vlan_v2_caps (vm, ad, &vc)))
1035         return error;
1036
1037       outer = vc.offloads.stripping_support.outer & mask;
1038       inner = vc.offloads.stripping_support.inner & mask;
1039
1040       /* Check for ability to modify the VLAN setting */
1041       outer =
1042         vc.offloads.stripping_support.outer & VIRTCHNL_VLAN_TOGGLE ? outer : 0;
1043       inner =
1044         vc.offloads.stripping_support.inner & VIRTCHNL_VLAN_TOGGLE ? inner : 0;
1045
1046       if ((outer || inner) &&
1047           (error = avf_op_disable_vlan_stripping_v2 (vm, ad, outer, inner)))
1048         return error;
1049     }
1050   else if ((error = avf_op_disable_vlan_stripping (vm, ad)))
1051     return error;
1052
1053   /*
1054    * Init Queues
1055    */
1056   for (i = 0; i < ad->n_rx_queues; i++)
1057     if ((error = avf_rxq_init (vm, ad, i, args->rxq_size)))
1058       return error;
1059
1060   for (i = 0; i < ad->n_tx_queues; i++)
1061     if ((error = avf_txq_init (vm, ad, i, args->txq_size)))
1062       return error;
1063
1064   if ((ad->cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) &&
1065       (error = avf_op_config_rss_lut (vm, ad)))
1066     return error;
1067
1068   if ((ad->cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) &&
1069       (error = avf_op_config_rss_key (vm, ad)))
1070     return error;
1071
1072   if ((error = avf_op_config_vsi_queues (vm, ad)))
1073     return error;
1074
1075   if ((error = avf_op_config_irq_map (vm, ad)))
1076     return error;
1077
1078   avf_irq_0_set_state (ad, AVF_IRQ_STATE_ENABLED);
1079
1080   for (i = 0; i < ad->n_rx_irqs; i++)
1081     avf_irq_n_set_state (ad, i, wb_on_itr ? AVF_IRQ_STATE_WB_ON_ITR :
1082                          AVF_IRQ_STATE_ENABLED);
1083
1084   if ((error = avf_op_add_del_eth_addr (vm, ad, 1, ad->hwaddr, 1 /* add */ )))
1085     return error;
1086
1087   if ((error = avf_op_enable_queues (vm, ad, pow2_mask (ad->n_rx_queues),
1088                                      pow2_mask (ad->n_tx_queues))))
1089     return error;
1090
1091   ad->flags |= AVF_DEVICE_F_INITIALIZED;
1092   return error;
1093 }
1094
1095 void
1096 avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq)
1097 {
1098   vnet_main_t *vnm = vnet_get_main ();
1099   virtchnl_pf_event_t *e;
1100   u32 r;
1101
1102   if (ad->flags & AVF_DEVICE_F_ERROR)
1103     return;
1104
1105   if ((ad->flags & AVF_DEVICE_F_INITIALIZED) == 0)
1106     return;
1107
1108   ASSERT (ad->error == 0);
1109
1110   /* do not process device in reset state */
1111   r = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
1112   if (r != VIRTCHNL_VFR_VFACTIVE)
1113     return;
1114
1115   r = avf_get_u32 (ad->bar0, AVF_ARQLEN);
1116   if ((r & 0xf0000000) != (1ULL << 31))
1117     {
1118       ad->error = clib_error_return (0, "arq not enabled, arqlen = 0x%x", r);
1119       avf_log_err (ad, "error: %U", format_clib_error, ad->error);
1120       goto error;
1121     }
1122
1123   r = avf_get_u32 (ad->bar0, AVF_ATQLEN);
1124   if ((r & 0xf0000000) != (1ULL << 31))
1125     {
1126       ad->error = clib_error_return (0, "atq not enabled, atqlen = 0x%x", r);
1127       avf_log_err (ad, "error: %U", format_clib_error, ad->error);
1128       goto error;
1129     }
1130
1131   if (is_irq == 0)
1132     avf_op_get_stats (vm, ad, &ad->eth_stats);
1133
1134   vec_foreach (e, ad->events)
1135     {
1136       avf_log_debug (ad, "event: %s (%u) sev %d",
1137                      virtchnl_event_names[e->event], e->event, e->severity);
1138       if (e->event == VIRTCHNL_EVENT_LINK_CHANGE)
1139         {
1140           int link_up;
1141           virtchnl_link_speed_t speed = e->event_data.link_event.link_speed;
1142           u32 flags = 0;
1143           u32 mbps = 0;
1144
1145           if (ad->cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1146             link_up = e->event_data.link_event_adv.link_status;
1147           else
1148             link_up = e->event_data.link_event.link_status;
1149
1150           if (ad->cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1151             mbps = e->event_data.link_event_adv.link_speed;
1152           if (speed == VIRTCHNL_LINK_SPEED_40GB)
1153             mbps = 40000;
1154           else if (speed == VIRTCHNL_LINK_SPEED_25GB)
1155             mbps = 25000;
1156           else if (speed == VIRTCHNL_LINK_SPEED_10GB)
1157             mbps = 10000;
1158           else if (speed == VIRTCHNL_LINK_SPEED_5GB)
1159             mbps = 5000;
1160           else if (speed == VIRTCHNL_LINK_SPEED_2_5GB)
1161             mbps = 2500;
1162           else if (speed == VIRTCHNL_LINK_SPEED_1GB)
1163             mbps = 1000;
1164           else if (speed == VIRTCHNL_LINK_SPEED_100MB)
1165             mbps = 100;
1166
1167           avf_log_debug (ad, "event_link_change: status %d speed %u mbps",
1168                          link_up, mbps);
1169
1170           if (link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) == 0)
1171             {
1172               ad->flags |= AVF_DEVICE_F_LINK_UP;
1173               flags |= (VNET_HW_INTERFACE_FLAG_FULL_DUPLEX |
1174                         VNET_HW_INTERFACE_FLAG_LINK_UP);
1175               vnet_hw_interface_set_flags (vnm, ad->hw_if_index, flags);
1176               vnet_hw_interface_set_link_speed (
1177                 vnm, ad->hw_if_index,
1178                 (mbps == UINT32_MAX) ? UINT32_MAX : mbps * 1000);
1179               ad->link_speed = mbps;
1180             }
1181           else if (!link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) != 0)
1182             {
1183               ad->flags &= ~AVF_DEVICE_F_LINK_UP;
1184               ad->link_speed = 0;
1185             }
1186
1187           if (ad->flags & AVF_DEVICE_F_ELOG)
1188             {
1189               ELOG_TYPE_DECLARE (el) =
1190                 {
1191                   .format = "avf[%d] link change: link_status %d "
1192                     "link_speed %d mbps",
1193                   .format_args = "i4i1i4",
1194                 };
1195               struct
1196                 {
1197                   u32 dev_instance;
1198                   u8 link_status;
1199                   u32 link_speed;
1200                 } *ed;
1201                 ed = ELOG_DATA (&vlib_global_main.elog_main, el);
1202                 ed->dev_instance = ad->dev_instance;
1203                 ed->link_status = link_up;
1204                 ed->link_speed = mbps;
1205             }
1206         }
1207       else
1208         {
1209           if (ad->flags & AVF_DEVICE_F_ELOG)
1210             {
1211               ELOG_TYPE_DECLARE (el) =
1212                 {
1213                   .format = "avf[%d] unknown event: event %d severity %d",
1214                   .format_args = "i4i4i1i1",
1215                 };
1216               struct
1217                 {
1218                   u32 dev_instance;
1219                   u32 event;
1220                   u32 severity;
1221                 } *ed;
1222                 ed = ELOG_DATA (&vlib_global_main.elog_main, el);
1223                 ed->dev_instance = ad->dev_instance;
1224                 ed->event = e->event;
1225                 ed->severity = e->severity;
1226             }
1227         }
1228     }
1229   vec_reset_length (ad->events);
1230
1231   return;
1232
1233 error:
1234   ad->flags |= AVF_DEVICE_F_ERROR;
1235   ASSERT (ad->error != 0);
1236   vlib_log_err (avf_log.class, "%U", format_clib_error, ad->error);
1237 }
1238
1239 clib_error_t *
1240 avf_op_program_flow (vlib_main_t *vm, avf_device_t *ad, int is_create,
1241                      enum virthnl_adv_ops vc_op, u8 *rule, u32 rule_len,
1242                      u8 *program_status, u32 status_len)
1243 {
1244   virtchnl_ops_t op;
1245
1246   avf_log_debug (ad, "avf_op_program_flow: vsi_id %u is_create %u", ad->vsi_id,
1247                  is_create);
1248
1249   switch (vc_op)
1250     {
1251     case VIRTCHNL_ADV_OP_ADD_FDIR_FILTER:
1252     case VIRTCHNL_ADV_OP_DEL_FDIR_FILTER:
1253       op =
1254         is_create ? VIRTCHNL_OP_ADD_FDIR_FILTER : VIRTCHNL_OP_DEL_FDIR_FILTER;
1255       break;
1256     case VIRTCHNL_ADV_OP_ADD_RSS_CFG:
1257     case VIRTCHNL_ADV_OP_DEL_RSS_CFG:
1258       op = is_create ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
1259       break;
1260     default:
1261       return clib_error_return (0, "invalid virtchnl opcode");
1262       ;
1263     }
1264
1265   return avf_send_to_pf (vm, ad, op, rule, rule_len, program_status,
1266                          status_len);
1267 }
1268
1269 static void
1270 avf_process_handle_request (vlib_main_t * vm, avf_process_req_t * req)
1271 {
1272   avf_device_t *ad = avf_get_device (req->dev_instance);
1273
1274   if (req->type == AVF_PROCESS_REQ_ADD_DEL_ETH_ADDR)
1275     req->error = avf_op_add_del_eth_addr (vm, ad, 1, req->eth_addr,
1276                                           req->is_add);
1277   else if (req->type == AVF_PROCESS_REQ_CONFIG_PROMISC_MDDE)
1278     req->error = avf_op_config_promisc_mode (vm, ad, req->is_enable);
1279   else if (req->type == AVF_PROCESS_REQ_PROGRAM_FLOW)
1280     req->error = avf_op_program_flow (vm, ad, req->is_add, req->vc_op,
1281                                       req->rule, req->rule_len,
1282                                       req->program_status, req->status_len);
1283   else
1284     clib_panic ("BUG: unknown avf proceess request type");
1285
1286   if (req->calling_process_index != avf_process_node.index)
1287     vlib_process_signal_event (vm, req->calling_process_index, 0, 0);
1288 }
1289
1290 static clib_error_t *
1291 avf_process_request (vlib_main_t * vm, avf_process_req_t * req)
1292 {
1293   uword *event_data = 0;
1294   req->calling_process_index = vlib_get_current_process_node_index (vm);
1295
1296   if (req->calling_process_index != avf_process_node.index)
1297     {
1298       vlib_process_signal_event_pointer (vm, avf_process_node.index,
1299                                          AVF_PROCESS_EVENT_REQ, req);
1300
1301       vlib_process_wait_for_event_or_clock (vm, 5.0);
1302
1303       if (vlib_process_get_events (vm, &event_data) != 0)
1304         clib_panic ("avf process node failed to reply in 5 seconds");
1305       vec_free (event_data);
1306     }
1307   else
1308     avf_process_handle_request (vm, req);
1309
1310   return req->error;
1311 }
1312
1313 static u32
1314 avf_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
1315 {
1316   avf_process_req_t req;
1317   vlib_main_t *vm = vlib_get_main ();
1318   avf_device_t *ad = avf_get_device (hw->dev_instance);
1319   clib_error_t *err;
1320
1321   switch (flags)
1322     {
1323     case ETHERNET_INTERFACE_FLAG_DEFAULT_L3:
1324       ad->flags &= ~AVF_DEVICE_F_PROMISC;
1325       break;
1326     case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
1327       ad->flags |= AVF_DEVICE_F_PROMISC;
1328       break;
1329     default:
1330       return ~0;
1331     }
1332
1333   req.is_enable = ((ad->flags & AVF_DEVICE_F_PROMISC) != 0);
1334   req.type = AVF_PROCESS_REQ_CONFIG_PROMISC_MDDE;
1335   req.dev_instance = hw->dev_instance;
1336
1337   if ((err = avf_process_request (vm, &req)))
1338     {
1339       avf_log_err (ad, "error: %U", format_clib_error, err);
1340       clib_error_free (err);
1341       return ~0;
1342     }
1343   return 0;
1344 }
1345
1346 static uword
1347 avf_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1348 {
1349   avf_main_t *am = &avf_main;
1350   uword *event_data = 0, event_type;
1351   int enabled = 0, irq;
1352   f64 last_run_duration = 0;
1353   f64 last_periodic_time = 0;
1354   avf_device_t **dev_pointers = 0;
1355   u32 i;
1356
1357   while (1)
1358     {
1359       if (enabled)
1360         vlib_process_wait_for_event_or_clock (vm, 5.0 - last_run_duration);
1361       else
1362         vlib_process_wait_for_event (vm);
1363
1364       event_type = vlib_process_get_events (vm, &event_data);
1365       irq = 0;
1366
1367       switch (event_type)
1368         {
1369         case ~0:
1370           last_periodic_time = vlib_time_now (vm);
1371           break;
1372         case AVF_PROCESS_EVENT_START:
1373           enabled = 1;
1374           break;
1375         case AVF_PROCESS_EVENT_DELETE_IF:
1376           for (int i = 0; i < vec_len (event_data); i++)
1377             {
1378               avf_device_t *ad = avf_get_device (event_data[i]);
1379               avf_delete_if (vm, ad, /* with_barrier */ 1);
1380             }
1381           if (pool_elts (am->devices) < 1)
1382             enabled = 0;
1383           break;
1384         case AVF_PROCESS_EVENT_AQ_INT:
1385           irq = 1;
1386           break;
1387         case AVF_PROCESS_EVENT_REQ:
1388           for (int i = 0; i < vec_len (event_data); i++)
1389             avf_process_handle_request (vm, (void *) event_data[i]);
1390           break;
1391
1392         default:
1393           ASSERT (0);
1394         }
1395
1396       vec_reset_length (event_data);
1397
1398       if (enabled == 0)
1399         continue;
1400
1401       /* create local list of device pointers as device pool may grow
1402        * during suspend */
1403       vec_reset_length (dev_pointers);
1404       pool_foreach_index (i, am->devices)
1405         {
1406           vec_add1 (dev_pointers, avf_get_device (i));
1407         }
1408
1409       vec_foreach_index (i, dev_pointers)
1410         {
1411           avf_process_one_device (vm, dev_pointers[i], irq);
1412         };
1413       last_run_duration = vlib_time_now (vm) - last_periodic_time;
1414     }
1415   return 0;
1416 }
1417
1418 VLIB_REGISTER_NODE (avf_process_node)  = {
1419   .function = avf_process,
1420   .type = VLIB_NODE_TYPE_PROCESS,
1421   .name = "avf-process",
1422 };
1423
1424 static void
1425 avf_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
1426 {
1427   uword pd = vlib_pci_get_private_data (vm, h);
1428   avf_device_t *ad = avf_get_device (pd);
1429   u32 icr0;
1430
1431   icr0 = avf_reg_read (ad, AVFINT_ICR0);
1432
1433   if (ad->flags & AVF_DEVICE_F_ELOG)
1434     {
1435       ELOG_TYPE_DECLARE (el) =
1436         {
1437           .format = "avf[%d] irq 0: icr0 0x%x",
1438           .format_args = "i4i4",
1439         };
1440       struct
1441       {
1442         u32 dev_instance;
1443         u32 icr0;
1444       } *ed;
1445
1446       ed = ELOG_DATA (&vlib_global_main.elog_main, el);
1447       ed->dev_instance = ad->dev_instance;
1448       ed->icr0 = icr0;
1449     }
1450
1451   avf_irq_0_set_state (ad, AVF_IRQ_STATE_ENABLED);
1452
1453   /* bit 30 - Send/Receive Admin queue interrupt indication */
1454   if (icr0 & (1 << 30))
1455     vlib_process_signal_event (vm, avf_process_node.index,
1456                                AVF_PROCESS_EVENT_AQ_INT, 0);
1457 }
1458
1459 static void
1460 avf_irq_n_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
1461 {
1462   vnet_main_t *vnm = vnet_get_main ();
1463   uword pd = vlib_pci_get_private_data (vm, h);
1464   avf_device_t *ad = avf_get_device (pd);
1465   avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, line - 1);
1466
1467   if (ad->flags & AVF_DEVICE_F_ELOG)
1468     {
1469       ELOG_TYPE_DECLARE (el) =
1470         {
1471           .format = "avf[%d] irq %d: received",
1472           .format_args = "i4i2",
1473         };
1474       struct
1475       {
1476         u32 dev_instance;
1477         u16 line;
1478       } *ed;
1479
1480       ed = ELOG_DATA (&vlib_global_main.elog_main, el);
1481       ed->dev_instance = ad->dev_instance;
1482       ed->line = line;
1483     }
1484
1485   line--;
1486
1487   if (ad->flags & AVF_DEVICE_F_RX_INT && rxq->int_mode)
1488     vnet_hw_if_rx_queue_set_int_pending (vnm, rxq->queue_index);
1489   avf_irq_n_set_state (ad, line, AVF_IRQ_STATE_ENABLED);
1490 }
1491
1492 void
1493 avf_delete_if (vlib_main_t * vm, avf_device_t * ad, int with_barrier)
1494 {
1495   vnet_main_t *vnm = vnet_get_main ();
1496   avf_main_t *am = &avf_main;
1497   int i;
1498   u32 dev_instance;
1499
1500   ad->flags &= ~AVF_DEVICE_F_ADMIN_UP;
1501
1502   if (ad->hw_if_index)
1503     {
1504       if (with_barrier)
1505         vlib_worker_thread_barrier_sync (vm);
1506       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
1507       ethernet_delete_interface (vnm, ad->hw_if_index);
1508       if (with_barrier)
1509         vlib_worker_thread_barrier_release (vm);
1510     }
1511
1512   vlib_pci_device_close (vm, ad->pci_dev_handle);
1513
1514   vlib_physmem_free (vm, ad->atq);
1515   vlib_physmem_free (vm, ad->arq);
1516   vlib_physmem_free (vm, ad->atq_bufs);
1517   vlib_physmem_free (vm, ad->arq_bufs);
1518
1519   vec_foreach_index (i, ad->rxqs)
1520     {
1521       avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
1522       vlib_physmem_free (vm, (void *) rxq->descs);
1523       if (rxq->n_enqueued)
1524         vlib_buffer_free_from_ring (vm, rxq->bufs, rxq->next, rxq->size,
1525                                     rxq->n_enqueued);
1526       vec_free (rxq->bufs);
1527     }
1528   vec_free (ad->rxqs);
1529
1530   vec_foreach_index (i, ad->txqs)
1531     {
1532       avf_txq_t *txq = vec_elt_at_index (ad->txqs, i);
1533       vlib_physmem_free (vm, (void *) txq->descs);
1534       if (txq->n_enqueued)
1535         {
1536           u16 first = (txq->next - txq->n_enqueued) & (txq->size -1);
1537           vlib_buffer_free_from_ring (vm, txq->bufs, first, txq->size,
1538                                       txq->n_enqueued);
1539         }
1540       /* Free the placeholder buffer */
1541       vlib_buffer_free (vm, txq->ph_bufs, vec_len (txq->ph_bufs));
1542       vec_free (txq->ph_bufs);
1543       vec_free (txq->bufs);
1544       clib_ring_free (txq->rs_slots);
1545       vec_free (txq->tmp_bufs);
1546       vec_free (txq->tmp_descs);
1547       clib_spinlock_free (&txq->lock);
1548     }
1549   vec_free (ad->txqs);
1550   vec_free (ad->name);
1551
1552   clib_error_free (ad->error);
1553   dev_instance = ad->dev_instance;
1554   clib_memset (ad, 0, sizeof (*ad));
1555   pool_put_index (am->devices, dev_instance);
1556   clib_mem_free (ad);
1557 }
1558
1559 static u8
1560 avf_validate_queue_size (avf_create_if_args_t * args)
1561 {
1562   clib_error_t *error = 0;
1563
1564   args->rxq_size = (args->rxq_size == 0) ? AVF_RXQ_SZ : args->rxq_size;
1565   args->txq_size = (args->txq_size == 0) ? AVF_TXQ_SZ : args->txq_size;
1566
1567   if ((args->rxq_size > AVF_QUEUE_SZ_MAX)
1568       || (args->txq_size > AVF_QUEUE_SZ_MAX))
1569     {
1570       args->rv = VNET_API_ERROR_INVALID_VALUE;
1571       args->error =
1572         clib_error_return (error, "queue size must not be greater than %u",
1573                            AVF_QUEUE_SZ_MAX);
1574       return 1;
1575     }
1576   if ((args->rxq_size < AVF_QUEUE_SZ_MIN)
1577       || (args->txq_size < AVF_QUEUE_SZ_MIN))
1578     {
1579       args->rv = VNET_API_ERROR_INVALID_VALUE;
1580       args->error =
1581         clib_error_return (error, "queue size must not be smaller than %u",
1582                            AVF_QUEUE_SZ_MIN);
1583       return 1;
1584     }
1585   if ((args->rxq_size & (args->rxq_size - 1)) ||
1586       (args->txq_size & (args->txq_size - 1)))
1587     {
1588       args->rv = VNET_API_ERROR_INVALID_VALUE;
1589       args->error =
1590         clib_error_return (error, "queue size must be a power of two");
1591       return 1;
1592     }
1593   return 0;
1594 }
1595
1596 void
1597 avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args)
1598 {
1599   vnet_main_t *vnm = vnet_get_main ();
1600   vnet_eth_interface_registration_t eir = {};
1601   avf_main_t *am = &avf_main;
1602   avf_device_t *ad, **adp;
1603   vlib_pci_dev_handle_t h;
1604   clib_error_t *error = 0;
1605   int i;
1606
1607   /* check input args */
1608   if (avf_validate_queue_size (args) != 0)
1609     return;
1610
1611   pool_foreach (adp, am->devices)  {
1612         if ((*adp)->pci_addr.as_u32 == args->addr.as_u32)
1613       {
1614         args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
1615         args->error =
1616           clib_error_return (error, "%U: %s", format_vlib_pci_addr,
1617                              &args->addr, "pci address in use");
1618         return;
1619       }
1620   }
1621
1622   pool_get (am->devices, adp);
1623   adp[0] = ad = clib_mem_alloc_aligned (sizeof (avf_device_t),
1624                                         CLIB_CACHE_LINE_BYTES);
1625   clib_memset (ad, 0, sizeof (avf_device_t));
1626   ad->dev_instance = adp - am->devices;
1627   ad->per_interface_next_index = ~0;
1628   ad->name = vec_dup (args->name);
1629
1630   if (args->enable_elog)
1631     {
1632       ad->flags |= AVF_DEVICE_F_ELOG;
1633       avf_elog_init ();
1634     }
1635
1636   if ((error = vlib_pci_device_open (vm, &args->addr, avf_pci_device_ids,
1637                                      &h)))
1638     {
1639       pool_put (am->devices, adp);
1640       clib_mem_free (ad);
1641       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1642       args->error =
1643         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1644                            &args->addr);
1645       return;
1646     }
1647   ad->pci_dev_handle = h;
1648   ad->pci_addr = args->addr;
1649   ad->numa_node = vlib_pci_get_numa_node (vm, h);
1650
1651   vlib_pci_set_private_data (vm, h, ad->dev_instance);
1652
1653   if ((error = vlib_pci_bus_master_enable (vm, h)))
1654     goto error;
1655
1656   if ((error = vlib_pci_map_region (vm, h, 0, &ad->bar0)))
1657     goto error;
1658
1659   ad->atq = vlib_physmem_alloc_aligned_on_numa (vm, sizeof (avf_aq_desc_t) *
1660                                                 AVF_MBOX_LEN,
1661                                                 CLIB_CACHE_LINE_BYTES,
1662                                                 ad->numa_node);
1663   if (ad->atq == 0)
1664     {
1665       error = vlib_physmem_last_error (vm);
1666       goto error;
1667     }
1668
1669   if ((error = vlib_pci_map_dma (vm, h, ad->atq)))
1670     goto error;
1671
1672   ad->arq = vlib_physmem_alloc_aligned_on_numa (vm, sizeof (avf_aq_desc_t) *
1673                                                 AVF_MBOX_LEN,
1674                                                 CLIB_CACHE_LINE_BYTES,
1675                                                 ad->numa_node);
1676   if (ad->arq == 0)
1677     {
1678       error = vlib_physmem_last_error (vm);
1679       goto error;
1680     }
1681
1682   if ((error = vlib_pci_map_dma (vm, h, ad->arq)))
1683     goto error;
1684
1685   ad->atq_bufs = vlib_physmem_alloc_aligned_on_numa (vm, AVF_MBOX_BUF_SZ *
1686                                                      AVF_MBOX_LEN,
1687                                                      CLIB_CACHE_LINE_BYTES,
1688                                                      ad->numa_node);
1689   if (ad->atq_bufs == 0)
1690     {
1691       error = vlib_physmem_last_error (vm);
1692       goto error;
1693     }
1694
1695   if ((error = vlib_pci_map_dma (vm, h, ad->atq_bufs)))
1696     goto error;
1697
1698   ad->arq_bufs = vlib_physmem_alloc_aligned_on_numa (vm, AVF_MBOX_BUF_SZ *
1699                                                      AVF_MBOX_LEN,
1700                                                      CLIB_CACHE_LINE_BYTES,
1701                                                      ad->numa_node);
1702   if (ad->arq_bufs == 0)
1703     {
1704       error = vlib_physmem_last_error (vm);
1705       goto error;
1706     }
1707
1708   if ((error = vlib_pci_map_dma (vm, h, ad->arq_bufs)))
1709     goto error;
1710
1711   if (vlib_pci_supports_virtual_addr_dma (vm, h))
1712     ad->flags |= AVF_DEVICE_F_VA_DMA;
1713
1714   if ((error = avf_device_init (vm, am, ad, args)))
1715     goto error;
1716
1717   if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1718                                                &avf_irq_0_handler)))
1719     goto error;
1720
1721   if ((error = vlib_pci_register_msix_handler (vm, h, 1, ad->n_rx_irqs,
1722                                                &avf_irq_n_handler)))
1723     goto error;
1724
1725   if ((error = vlib_pci_enable_msix_irq (vm, h, 0, ad->n_rx_irqs + 1)))
1726     goto error;
1727
1728   if ((error = vlib_pci_intr_enable (vm, h)))
1729     goto error;
1730
1731   /* create interface */
1732   eir.dev_class_index = avf_device_class.index;
1733   eir.dev_instance = ad->dev_instance;
1734   eir.address = ad->hwaddr;
1735   eir.cb.flag_change = avf_flag_change;
1736   ad->hw_if_index = vnet_eth_register_interface (vnm, &eir);
1737
1738   ethernet_set_flags (vnm, ad->hw_if_index,
1739                       ETHERNET_INTERFACE_FLAG_DEFAULT_L3);
1740
1741   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
1742   args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
1743
1744   vnet_hw_if_set_input_node (vnm, ad->hw_if_index, avf_input_node.index);
1745
1746   /* set hw interface caps */
1747   vnet_hw_if_set_caps (vnm, ad->hw_if_index,
1748                        VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_MAC_FILTER |
1749                          VNET_HW_IF_CAP_TX_CKSUM | VNET_HW_IF_CAP_TCP_GSO);
1750
1751   for (i = 0; i < ad->n_rx_queues; i++)
1752     {
1753       u32 qi, fi;
1754       qi = vnet_hw_if_register_rx_queue (vnm, ad->hw_if_index, i,
1755                                          VNET_HW_IF_RXQ_THREAD_ANY);
1756
1757       if (ad->flags & AVF_DEVICE_F_RX_INT)
1758         {
1759           fi = vlib_pci_get_msix_file_index (vm, ad->pci_dev_handle, i + 1);
1760           vnet_hw_if_set_rx_queue_file_index (vnm, qi, fi);
1761         }
1762       ad->rxqs[i].queue_index = qi;
1763     }
1764
1765   for (i = 0; i < ad->n_tx_queues; i++)
1766     {
1767       u32 qi = vnet_hw_if_register_tx_queue (vnm, ad->hw_if_index, i);
1768       ad->txqs[i].queue_index = qi;
1769     }
1770
1771   for (i = 0; i < vlib_get_n_threads (); i++)
1772     {
1773       u32 qi = ad->txqs[i % ad->n_tx_queues].queue_index;
1774       vnet_hw_if_tx_queue_assign_thread (vnm, qi, i);
1775     }
1776
1777   vnet_hw_if_update_runtime_data (vnm, ad->hw_if_index);
1778
1779   if (pool_elts (am->devices) == 1)
1780     vlib_process_signal_event (vm, avf_process_node.index,
1781                                AVF_PROCESS_EVENT_START, 0);
1782
1783   return;
1784
1785 error:
1786   avf_delete_if (vm, ad, /* with_barrier */ 0);
1787   args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1788   args->error = clib_error_return (error, "pci-addr %U",
1789                                    format_vlib_pci_addr, &args->addr);
1790   avf_log_err (ad, "error: %U", format_clib_error, args->error);
1791 }
1792
1793 static clib_error_t *
1794 avf_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1795 {
1796   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1797   avf_device_t *ad = avf_get_device (hi->dev_instance);
1798   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1799
1800   if (ad->flags & AVF_DEVICE_F_ERROR)
1801     return clib_error_return (0, "device is in error state");
1802
1803   if (is_up)
1804     {
1805       vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
1806                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1807       ad->flags |= AVF_DEVICE_F_ADMIN_UP;
1808     }
1809   else
1810     {
1811       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
1812       ad->flags &= ~AVF_DEVICE_F_ADMIN_UP;
1813     }
1814   return 0;
1815 }
1816
1817 static clib_error_t *
1818 avf_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
1819                               vnet_hw_if_rx_mode mode)
1820 {
1821   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1822   avf_device_t *ad = avf_get_device (hw->dev_instance);
1823   avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
1824
1825   if (mode == VNET_HW_IF_RX_MODE_POLLING)
1826     {
1827       if (rxq->int_mode == 0)
1828         return 0;
1829       if (ad->cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1830         avf_irq_n_set_state (ad, qid, AVF_IRQ_STATE_WB_ON_ITR);
1831       else
1832         avf_irq_n_set_state (ad, qid, AVF_IRQ_STATE_ENABLED);
1833       rxq->int_mode = 0;
1834     }
1835   else
1836     {
1837       if (rxq->int_mode == 1)
1838         return 0;
1839       if (ad->n_rx_irqs != ad->n_rx_queues)
1840         return clib_error_return (0, "not enough interrupt lines");
1841       rxq->int_mode = 1;
1842       avf_irq_n_set_state (ad, qid, AVF_IRQ_STATE_ENABLED);
1843     }
1844
1845   return 0;
1846 }
1847
1848 static void
1849 avf_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1850                              u32 node_index)
1851 {
1852   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1853   avf_device_t *ad = avf_get_device (hw->dev_instance);
1854
1855   /* Shut off redirection */
1856   if (node_index == ~0)
1857     {
1858       ad->per_interface_next_index = node_index;
1859       return;
1860     }
1861
1862   ad->per_interface_next_index =
1863     vlib_node_add_next (vlib_get_main (), avf_input_node.index, node_index);
1864 }
1865
1866 static clib_error_t *
1867 avf_add_del_mac_address (vnet_hw_interface_t * hw,
1868                          const u8 * address, u8 is_add)
1869 {
1870   vlib_main_t *vm = vlib_get_main ();
1871   avf_process_req_t req;
1872
1873   req.dev_instance = hw->dev_instance;
1874   req.type = AVF_PROCESS_REQ_ADD_DEL_ETH_ADDR;
1875   req.is_add = is_add;
1876   clib_memcpy (req.eth_addr, address, 6);
1877
1878   return avf_process_request (vm, &req);
1879 }
1880
1881 static char *avf_tx_func_error_strings[] = {
1882 #define _(n,s) s,
1883   foreach_avf_tx_func_error
1884 #undef _
1885 };
1886
1887 static void
1888 avf_clear_hw_interface_counters (u32 instance)
1889 {
1890   avf_device_t *ad = avf_get_device (instance);
1891   clib_memcpy_fast (&ad->last_cleared_eth_stats,
1892                     &ad->eth_stats, sizeof (ad->eth_stats));
1893 }
1894
1895 clib_error_t *
1896 avf_program_flow (u32 dev_instance, int is_add, enum virthnl_adv_ops vc_op,
1897                   u8 *rule, u32 rule_len, u8 *program_status, u32 status_len)
1898 {
1899   vlib_main_t *vm = vlib_get_main ();
1900   avf_process_req_t req;
1901
1902   req.dev_instance = dev_instance;
1903   req.type = AVF_PROCESS_REQ_PROGRAM_FLOW;
1904   req.is_add = is_add;
1905   req.vc_op = vc_op;
1906   req.rule = rule;
1907   req.rule_len = rule_len;
1908   req.program_status = program_status;
1909   req.status_len = status_len;
1910
1911   return avf_process_request (vm, &req);
1912 }
1913
1914 VNET_DEVICE_CLASS (avf_device_class, ) = {
1915   .name = "Adaptive Virtual Function (AVF) interface",
1916   .clear_counters = avf_clear_hw_interface_counters,
1917   .format_device = format_avf_device,
1918   .format_device_name = format_avf_device_name,
1919   .admin_up_down_function = avf_interface_admin_up_down,
1920   .rx_mode_change_function = avf_interface_rx_mode_change,
1921   .rx_redirect_to_node = avf_set_interface_next_node,
1922   .mac_addr_add_del_function = avf_add_del_mac_address,
1923   .tx_function_n_errors = AVF_TX_N_ERROR,
1924   .tx_function_error_strings = avf_tx_func_error_strings,
1925   .flow_ops_function = avf_flow_ops_fn,
1926 };
1927
1928 clib_error_t *
1929 avf_init (vlib_main_t * vm)
1930 {
1931   avf_main_t *am = &avf_main;
1932   vlib_thread_main_t *tm = vlib_get_thread_main ();
1933
1934   vec_validate_aligned (am->per_thread_data, tm->n_vlib_mains - 1,
1935                         CLIB_CACHE_LINE_BYTES);
1936
1937   return 0;
1938 }
1939
1940 VLIB_INIT_FUNCTION (avf_init);