avf: proper promisc handling
[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
24 #include <avf/avf.h>
25
26 #define AVF_MBOX_LEN 64
27 #define AVF_MBOX_BUF_SZ 512
28 #define AVF_RXQ_SZ 512
29 #define AVF_TXQ_SZ 512
30 #define AVF_ITR_INT 32
31
32 #define PCI_VENDOR_ID_INTEL                     0x8086
33 #define PCI_DEVICE_ID_INTEL_AVF                 0x1889
34 #define PCI_DEVICE_ID_INTEL_X710_VF             0x154c
35 #define PCI_DEVICE_ID_INTEL_X722_VF             0x37cd
36
37 avf_main_t avf_main;
38
39 static pci_device_id_t avf_pci_device_ids[] = {
40   {.vendor_id = PCI_VENDOR_ID_INTEL,.device_id = PCI_DEVICE_ID_INTEL_AVF},
41   {.vendor_id = PCI_VENDOR_ID_INTEL,.device_id = PCI_DEVICE_ID_INTEL_X710_VF},
42   {.vendor_id = PCI_VENDOR_ID_INTEL,.device_id = PCI_DEVICE_ID_INTEL_X722_VF},
43   {0},
44 };
45
46 const static char *virtchnl_event_names[] = {
47 #define _(v, n) [v] = #n,
48   foreach_virtchnl_event_code
49 #undef _
50 };
51
52 const static char *virtchnl_link_speed_str[] = {
53 #define _(v, n, s) [v] = s,
54   foreach_virtchnl_link_speed
55 #undef _
56 };
57
58 static inline void
59 avf_irq_0_disable (avf_device_t * ad)
60 {
61   u32 dyn_ctl0 = 0, icr0_ena = 0;
62
63   dyn_ctl0 |= (3 << 3);         /* 11b = No ITR update */
64
65   avf_reg_write (ad, AVFINT_ICR0_ENA1, icr0_ena);
66   avf_reg_write (ad, AVFINT_DYN_CTL0, dyn_ctl0);
67   avf_reg_flush (ad);
68 }
69
70 static inline void
71 avf_irq_0_enable (avf_device_t * ad)
72 {
73   u32 dyn_ctl0 = 0, icr0_ena = 0;
74
75   icr0_ena |= (1 << 30);        /* [30] Admin Queue Enable */
76
77   dyn_ctl0 |= (1 << 0);         /* [0] Interrupt Enable */
78   dyn_ctl0 |= (1 << 1);         /* [1] Clear PBA */
79   //dyn_ctl0 |= (3 << 3);               /* [4:3] ITR Index, 11b = No ITR update */
80   dyn_ctl0 |= ((AVF_ITR_INT / 2) << 5); /* [16:5] ITR Interval in 2us steps */
81
82   avf_irq_0_disable (ad);
83   avf_reg_write (ad, AVFINT_ICR0_ENA1, icr0_ena);
84   avf_reg_write (ad, AVFINT_DYN_CTL0, dyn_ctl0);
85   avf_reg_flush (ad);
86 }
87
88 static inline void
89 avf_irq_n_disable (avf_device_t * ad, u8 line)
90 {
91   u32 dyn_ctln = 0;
92
93   avf_reg_write (ad, AVFINT_DYN_CTLN (line), dyn_ctln);
94   avf_reg_flush (ad);
95 }
96
97 static inline void
98 avf_irq_n_enable (avf_device_t * ad, u8 line)
99 {
100   u32 dyn_ctln = 0;
101
102   dyn_ctln |= (1 << 0);         /* [0] Interrupt Enable */
103   dyn_ctln |= (1 << 1);         /* [1] Clear PBA */
104   dyn_ctln |= ((AVF_ITR_INT / 2) << 5); /* [16:5] ITR Interval in 2us steps */
105
106   avf_irq_n_disable (ad, line);
107   avf_reg_write (ad, AVFINT_DYN_CTLN (line), dyn_ctln);
108   avf_reg_flush (ad);
109 }
110
111
112 clib_error_t *
113 avf_aq_desc_enq (vlib_main_t * vm, avf_device_t * ad, avf_aq_desc_t * dt,
114                  void *data, int len)
115 {
116   clib_error_t *err = 0;
117   avf_aq_desc_t *d, dc;
118   f64 t0, suspend_time = AVF_AQ_ENQ_SUSPEND_TIME;
119
120   d = &ad->atq[ad->atq_next_slot];
121   clib_memcpy_fast (d, dt, sizeof (avf_aq_desc_t));
122   d->flags |= AVF_AQ_F_RD | AVF_AQ_F_SI;
123   if (len)
124     d->datalen = len;
125   if (len)
126     {
127       u64 pa;
128       pa = ad->atq_bufs_pa + ad->atq_next_slot * AVF_MBOX_BUF_SZ;
129       d->addr_hi = (u32) (pa >> 32);
130       d->addr_lo = (u32) pa;
131       clib_memcpy_fast (ad->atq_bufs + ad->atq_next_slot * AVF_MBOX_BUF_SZ,
132                         data, len);
133       d->flags |= AVF_AQ_F_BUF;
134     }
135
136   if (ad->flags & AVF_DEVICE_F_ELOG)
137     clib_memcpy_fast (&dc, d, sizeof (avf_aq_desc_t));
138
139   CLIB_MEMORY_BARRIER ();
140   ad->atq_next_slot = (ad->atq_next_slot + 1) % AVF_MBOX_LEN;
141   avf_reg_write (ad, AVF_ATQT, ad->atq_next_slot);
142   avf_reg_flush (ad);
143
144   t0 = vlib_time_now (vm);
145 retry:
146   vlib_process_suspend (vm, suspend_time);
147
148   if (((d->flags & AVF_AQ_F_DD) == 0) || ((d->flags & AVF_AQ_F_CMP) == 0))
149     {
150       f64 t = vlib_time_now (vm) - t0;
151       if (t > AVF_AQ_ENQ_MAX_WAIT_TIME)
152         {
153           avf_log_err (ad, "aq_desc_enq failed (timeout %.3fs)", t);
154           err = clib_error_return (0, "adminq enqueue timeout [opcode 0x%x]",
155                                    d->opcode);
156           goto done;
157         }
158       suspend_time *= 2;
159       goto retry;
160     }
161
162   clib_memcpy_fast (dt, d, sizeof (avf_aq_desc_t));
163   if (d->flags & AVF_AQ_F_ERR)
164     return clib_error_return (0, "adminq enqueue error [opcode 0x%x, retval "
165                               "%d]", d->opcode, d->retval);
166
167 done:
168   if (ad->flags & AVF_DEVICE_F_ELOG)
169     {
170       /* *INDENT-OFF* */
171       ELOG_TYPE_DECLARE (el) =
172         {
173           .format = "avf[%d] aq enq: s_flags 0x%x r_flags 0x%x opcode 0x%x "
174             "datalen %d retval %d",
175           .format_args = "i4i2i2i2i2i2",
176         };
177       struct
178         {
179           u32 dev_instance;
180           u16 s_flags;
181           u16 r_flags;
182           u16 opcode;
183           u16 datalen;
184           u16 retval;
185         } *ed;
186       ed = ELOG_DATA (&vm->elog_main, el);
187       ed->dev_instance = ad->dev_instance;
188       ed->s_flags = dc.flags;
189       ed->r_flags = d->flags;
190       ed->opcode = dc.opcode;
191       ed->datalen = dc.datalen;
192       ed->retval = d->retval;
193       /* *INDENT-ON* */
194     }
195
196   return err;
197 }
198
199 clib_error_t *
200 avf_cmd_rx_ctl_reg_write (vlib_main_t * vm, avf_device_t * ad, u32 reg,
201                           u32 val)
202 {
203   clib_error_t *err;
204   avf_aq_desc_t d = {.opcode = 0x207,.param1 = reg,.param3 = val };
205   err = avf_aq_desc_enq (vm, ad, &d, 0, 0);
206
207   if (ad->flags & AVF_DEVICE_F_ELOG)
208     {
209       /* *INDENT-OFF* */
210       ELOG_TYPE_DECLARE (el) =
211         {
212           .format = "avf[%d] rx ctl reg write: reg 0x%x val 0x%x ",
213           .format_args = "i4i4i4",
214         };
215       struct
216         {
217           u32 dev_instance;
218           u32 reg;
219           u32 val;
220         } *ed;
221       ed = ELOG_DATA (&vm->elog_main, el);
222       ed->dev_instance = ad->dev_instance;
223       ed->reg = reg;
224       ed->val = val;
225       /* *INDENT-ON* */
226     }
227   return err;
228 }
229
230 clib_error_t *
231 avf_rxq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 rxq_size)
232 {
233   clib_error_t *err;
234   avf_rxq_t *rxq;
235   u32 n_alloc, i;
236
237   vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES);
238   rxq = vec_elt_at_index (ad->rxqs, qid);
239   rxq->size = rxq_size;
240   rxq->next = 0;
241   rxq->descs = vlib_physmem_alloc_aligned_on_numa (vm, rxq->size *
242                                                    sizeof (avf_rx_desc_t),
243                                                    2 * CLIB_CACHE_LINE_BYTES,
244                                                    ad->numa_node);
245
246   rxq->buffer_pool_index =
247     vlib_buffer_pool_get_default_for_numa (vm, ad->numa_node);
248
249   if (rxq->descs == 0)
250     return vlib_physmem_last_error (vm);
251
252   if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) rxq->descs)))
253     return err;
254
255   clib_memset ((void *) rxq->descs, 0, rxq->size * sizeof (avf_rx_desc_t));
256   vec_validate_aligned (rxq->bufs, rxq->size, CLIB_CACHE_LINE_BYTES);
257   rxq->qrx_tail = ad->bar0 + AVF_QRX_TAIL (qid);
258
259   n_alloc = vlib_buffer_alloc_from_pool (vm, rxq->bufs, rxq->size - 8,
260                                          rxq->buffer_pool_index);
261
262   if (n_alloc == 0)
263     return clib_error_return (0, "buffer allocation error");
264
265   rxq->n_enqueued = n_alloc;
266   avf_rx_desc_t *d = rxq->descs;
267   for (i = 0; i < n_alloc; i++)
268     {
269       vlib_buffer_t *b = vlib_get_buffer (vm, rxq->bufs[i]);
270       if (ad->flags & AVF_DEVICE_F_VA_DMA)
271         d->qword[0] = vlib_buffer_get_va (b);
272       else
273         d->qword[0] = vlib_buffer_get_pa (vm, b);
274       d++;
275     }
276
277   ad->n_rx_queues = clib_min (ad->num_queue_pairs, qid + 1);
278   return 0;
279 }
280
281 clib_error_t *
282 avf_txq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 txq_size)
283 {
284   clib_error_t *err;
285   avf_txq_t *txq;
286
287   if (qid >= ad->num_queue_pairs)
288     {
289       qid = qid % ad->num_queue_pairs;
290       txq = vec_elt_at_index (ad->txqs, qid);
291       if (txq->lock == 0)
292         clib_spinlock_init (&txq->lock);
293       ad->flags |= AVF_DEVICE_F_SHARED_TXQ_LOCK;
294       return 0;
295     }
296
297   vec_validate_aligned (ad->txqs, qid, CLIB_CACHE_LINE_BYTES);
298   txq = vec_elt_at_index (ad->txqs, qid);
299   txq->size = txq_size;
300   txq->next = 0;
301   txq->descs = vlib_physmem_alloc_aligned_on_numa (vm, txq->size *
302                                                    sizeof (avf_tx_desc_t),
303                                                    2 * CLIB_CACHE_LINE_BYTES,
304                                                    ad->numa_node);
305   if (txq->descs == 0)
306     return vlib_physmem_last_error (vm);
307
308   if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) txq->descs)))
309     return err;
310
311   vec_validate_aligned (txq->bufs, txq->size, CLIB_CACHE_LINE_BYTES);
312   txq->qtx_tail = ad->bar0 + AVF_QTX_TAIL (qid);
313
314   /* initialize ring of pending RS slots */
315   clib_ring_new_aligned (txq->rs_slots, 32, CLIB_CACHE_LINE_BYTES);
316
317   ad->n_tx_queues = clib_min (ad->num_queue_pairs, qid + 1);
318   return 0;
319 }
320
321 typedef struct
322 {
323   u16 vsi_id;
324   u16 flags;
325 } virtchnl_promisc_info_t;
326
327 void
328 avf_arq_slot_init (avf_device_t * ad, u16 slot)
329 {
330   avf_aq_desc_t *d;
331   u64 pa = ad->arq_bufs_pa + slot * AVF_MBOX_BUF_SZ;
332   d = &ad->arq[slot];
333   clib_memset (d, 0, sizeof (avf_aq_desc_t));
334   d->flags = AVF_AQ_F_BUF;
335   d->datalen = AVF_MBOX_BUF_SZ;
336   d->addr_hi = (u32) (pa >> 32);
337   d->addr_lo = (u32) pa;
338 }
339
340 static inline uword
341 avf_dma_addr (vlib_main_t * vm, avf_device_t * ad, void *p)
342 {
343   return (ad->flags & AVF_DEVICE_F_VA_DMA) ?
344     pointer_to_uword (p) : vlib_physmem_get_pa (vm, p);
345 }
346
347 static void
348 avf_adminq_init (vlib_main_t * vm, avf_device_t * ad)
349 {
350   u64 pa;
351   int i;
352
353   /* VF MailBox Transmit */
354   clib_memset (ad->atq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN);
355   ad->atq_bufs_pa = avf_dma_addr (vm, ad, ad->atq_bufs);
356
357   pa = avf_dma_addr (vm, ad, ad->atq);
358   avf_reg_write (ad, AVF_ATQT, 0);      /* Tail */
359   avf_reg_write (ad, AVF_ATQH, 0);      /* Head */
360   avf_reg_write (ad, AVF_ATQLEN, AVF_MBOX_LEN | (1ULL << 31));  /* len & ena */
361   avf_reg_write (ad, AVF_ATQBAL, (u32) pa);     /* Base Address Low */
362   avf_reg_write (ad, AVF_ATQBAH, (u32) (pa >> 32));     /* Base Address High */
363
364   /* VF MailBox Receive */
365   clib_memset (ad->arq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN);
366   ad->arq_bufs_pa = avf_dma_addr (vm, ad, ad->arq_bufs);
367
368   for (i = 0; i < AVF_MBOX_LEN; i++)
369     avf_arq_slot_init (ad, i);
370
371   pa = avf_dma_addr (vm, ad, ad->arq);
372
373   avf_reg_write (ad, AVF_ARQH, 0);      /* Head */
374   avf_reg_write (ad, AVF_ARQT, 0);      /* Head */
375   avf_reg_write (ad, AVF_ARQLEN, AVF_MBOX_LEN | (1ULL << 31));  /* len & ena */
376   avf_reg_write (ad, AVF_ARQBAL, (u32) pa);     /* Base Address Low */
377   avf_reg_write (ad, AVF_ARQBAH, (u32) (pa >> 32));     /* Base Address High */
378   avf_reg_write (ad, AVF_ARQT, AVF_MBOX_LEN - 1);       /* Tail */
379
380   ad->atq_next_slot = 0;
381   ad->arq_next_slot = 0;
382 }
383
384 clib_error_t *
385 avf_send_to_pf (vlib_main_t * vm, avf_device_t * ad, virtchnl_ops_t op,
386                 void *in, int in_len, void *out, int out_len)
387 {
388   clib_error_t *err;
389   avf_aq_desc_t *d, dt = {.opcode = 0x801,.v_opcode = op };
390   u32 head;
391   f64 t0, suspend_time = AVF_SEND_TO_PF_SUSPEND_TIME;
392
393   /* suppress interrupt in the next adminq receive slot
394      as we are going to wait for response
395      we only need interrupts when event is received */
396   d = &ad->arq[ad->arq_next_slot];
397   d->flags |= AVF_AQ_F_SI;
398
399   if ((err = avf_aq_desc_enq (vm, ad, &dt, in, in_len)))
400     return err;
401
402   t0 = vlib_time_now (vm);
403 retry:
404   head = avf_get_u32 (ad->bar0, AVF_ARQH);
405
406   if (ad->arq_next_slot == head)
407     {
408       f64 t = vlib_time_now (vm) - t0;
409       if (t > AVF_SEND_TO_PF_MAX_WAIT_TIME)
410         {
411           avf_log_err (ad, "send_to_pf failed (timeout %.3fs)", t);
412           return clib_error_return (0, "timeout");
413         }
414       vlib_process_suspend (vm, suspend_time);
415       suspend_time *= 2;
416       goto retry;
417     }
418
419   d = &ad->arq[ad->arq_next_slot];
420
421   if (d->v_opcode == VIRTCHNL_OP_EVENT)
422     {
423       void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ;
424       virtchnl_pf_event_t *e;
425
426       if ((d->datalen != sizeof (virtchnl_pf_event_t)) ||
427           ((d->flags & AVF_AQ_F_BUF) == 0))
428         return clib_error_return (0, "event message error");
429
430       vec_add2 (ad->events, e, 1);
431       clib_memcpy_fast (e, buf, sizeof (virtchnl_pf_event_t));
432       avf_arq_slot_init (ad, ad->arq_next_slot);
433       ad->arq_next_slot++;
434       /* reset timer */
435       t0 = vlib_time_now (vm);
436       suspend_time = AVF_SEND_TO_PF_SUSPEND_TIME;
437       goto retry;
438     }
439
440   if (d->v_opcode != op)
441     {
442       err =
443         clib_error_return (0,
444                            "unexpected message receiver [v_opcode = %u, "
445                            "expected %u, v_retval %d]", d->v_opcode, op,
446                            d->v_retval);
447       goto done;
448     }
449
450   if (d->v_retval)
451     {
452       err = clib_error_return (0, "error [v_opcode = %u, v_retval %d]",
453                                d->v_opcode, d->v_retval);
454       goto done;
455     }
456
457   if (d->flags & AVF_AQ_F_BUF)
458     {
459       void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ;
460       clib_memcpy_fast (out, buf, out_len);
461     }
462
463   avf_arq_slot_init (ad, ad->arq_next_slot);
464   avf_reg_write (ad, AVF_ARQT, ad->arq_next_slot);
465   avf_reg_flush (ad);
466   ad->arq_next_slot = (ad->arq_next_slot + 1) % AVF_MBOX_LEN;
467
468 done:
469
470   if (ad->flags & AVF_DEVICE_F_ELOG)
471     {
472       /* *INDENT-OFF* */
473       ELOG_TYPE_DECLARE (el) =
474         {
475           .format = "avf[%d] send to pf: v_opcode %s (%d) v_retval 0x%x",
476           .format_args = "i4t4i4i4",
477           .n_enum_strings = VIRTCHNL_N_OPS,
478           .enum_strings = {
479 #define _(v, n) [v] = #n,
480               foreach_virtchnl_op
481 #undef _
482           },
483         };
484       struct
485         {
486           u32 dev_instance;
487           u32 v_opcode;
488           u32 v_opcode_val;
489           u32 v_retval;
490         } *ed;
491       ed = ELOG_DATA (&vm->elog_main, el);
492       ed->dev_instance = ad->dev_instance;
493       ed->v_opcode = op;
494       ed->v_opcode_val = op;
495       ed->v_retval = d->v_retval;
496       /* *INDENT-ON* */
497     }
498   return err;
499 }
500
501 clib_error_t *
502 avf_op_version (vlib_main_t * vm, avf_device_t * ad,
503                 virtchnl_version_info_t * ver)
504 {
505   clib_error_t *err = 0;
506   virtchnl_version_info_t myver = {
507     .major = VIRTCHNL_VERSION_MAJOR,
508     .minor = VIRTCHNL_VERSION_MINOR,
509   };
510
511   avf_log_debug (ad, "version: major %u minor %u", myver.major, myver.minor);
512
513   err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_VERSION, &myver,
514                         sizeof (virtchnl_version_info_t), ver,
515                         sizeof (virtchnl_version_info_t));
516
517   if (err)
518     return err;
519
520   return err;
521 }
522
523 clib_error_t *
524 avf_op_get_vf_resources (vlib_main_t * vm, avf_device_t * ad,
525                          virtchnl_vf_resource_t * res)
526 {
527   clib_error_t *err = 0;
528   u32 bitmap = (VIRTCHNL_VF_OFFLOAD_L2 | VIRTCHNL_VF_OFFLOAD_RSS_PF |
529                 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR | VIRTCHNL_VF_OFFLOAD_VLAN |
530                 VIRTCHNL_VF_OFFLOAD_RX_POLLING);
531
532   avf_log_debug (ad, "get_vf_reqources: bitmap 0x%x", bitmap);
533   err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_VF_RESOURCES, &bitmap,
534                         sizeof (u32), res, sizeof (virtchnl_vf_resource_t));
535
536   if (err == 0)
537     {
538       int i;
539       avf_log_debug (ad, "get_vf_reqources: num_vsis %u num_queue_pairs %u "
540                      "max_vectors %u max_mtu %u vf_offload_flags 0x%04x "
541                      "rss_key_size %u rss_lut_size %u",
542                      res->num_vsis, res->num_queue_pairs, res->max_vectors,
543                      res->max_mtu, res->vf_offload_flags, res->rss_key_size,
544                      res->rss_lut_size);
545       for (i = 0; i < res->num_vsis; i++)
546         avf_log_debug (ad, "get_vf_reqources_vsi[%u]: vsi_id %u "
547                        "num_queue_pairs %u vsi_type %u qset_handle %u "
548                        "default_mac_addr %U", i,
549                        res->vsi_res[i].vsi_id,
550                        res->vsi_res[i].num_queue_pairs,
551                        res->vsi_res[i].vsi_type,
552                        res->vsi_res[i].qset_handle,
553                        format_ethernet_address,
554                        res->vsi_res[i].default_mac_addr);
555     }
556
557   return err;
558 }
559
560 clib_error_t *
561 avf_op_config_rss_lut (vlib_main_t * vm, avf_device_t * ad)
562 {
563   int msg_len = sizeof (virtchnl_rss_lut_t) + ad->rss_lut_size - 1;
564   int i;
565   u8 msg[msg_len];
566   virtchnl_rss_lut_t *rl;
567
568   clib_memset (msg, 0, msg_len);
569   rl = (virtchnl_rss_lut_t *) msg;
570   rl->vsi_id = ad->vsi_id;
571   rl->lut_entries = ad->rss_lut_size;
572   for (i = 0; i < ad->rss_lut_size; i++)
573     rl->lut[i] = i % ad->n_rx_queues;
574
575   avf_log_debug (ad, "config_rss_lut: vsi_id %u rss_lut_size %u lut 0x%U",
576                  rl->vsi_id, rl->lut_entries, format_hex_bytes_no_wrap,
577                  rl->lut, rl->lut_entries);
578
579   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_LUT, msg, msg_len, 0,
580                          0);
581 }
582
583 clib_error_t *
584 avf_op_config_rss_key (vlib_main_t * vm, avf_device_t * ad)
585 {
586   int msg_len = sizeof (virtchnl_rss_key_t) + ad->rss_key_size - 1;
587   int i;
588   u8 msg[msg_len];
589   virtchnl_rss_key_t *rk;
590
591   clib_memset (msg, 0, msg_len);
592   rk = (virtchnl_rss_key_t *) msg;
593   rk->vsi_id = ad->vsi_id;
594   rk->key_len = ad->rss_key_size;
595   u32 seed = random_default_seed ();
596   for (i = 0; i < ad->rss_key_size; i++)
597     rk->key[i] = (u8) random_u32 (&seed);
598
599   avf_log_debug (ad, "config_rss_key: vsi_id %u rss_key_size %u key 0x%U",
600                  rk->vsi_id, rk->key_len, format_hex_bytes_no_wrap, rk->key,
601                  rk->key_len);
602
603   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_KEY, msg, msg_len, 0,
604                          0);
605 }
606
607 clib_error_t *
608 avf_op_disable_vlan_stripping (vlib_main_t * vm, avf_device_t * ad)
609 {
610   avf_log_debug (ad, "disable_vlan_stripping");
611
612   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, 0, 0, 0,
613                          0);
614 }
615
616 clib_error_t *
617 avf_config_promisc_mode (vlib_main_t * vm, avf_device_t * ad, int is_enable)
618 {
619   virtchnl_promisc_info_t pi = { 0 };
620
621   pi.vsi_id = ad->vsi_id;
622
623   if (is_enable)
624     pi.flags = FLAG_VF_UNICAST_PROMISC | FLAG_VF_MULTICAST_PROMISC;
625
626   avf_log_debug (ad, "config_promisc_mode: unicast %s multicast %s",
627                  pi.flags & FLAG_VF_UNICAST_PROMISC ? "on" : "off",
628                  pi.flags & FLAG_VF_MULTICAST_PROMISC ? "on" : "off");
629
630   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE, &pi,
631                          sizeof (virtchnl_promisc_info_t), 0, 0);
632 }
633
634
635 clib_error_t *
636 avf_op_config_vsi_queues (vlib_main_t * vm, avf_device_t * ad)
637 {
638   int i;
639   int n_qp = clib_max (vec_len (ad->rxqs), vec_len (ad->txqs));
640   int msg_len = sizeof (virtchnl_vsi_queue_config_info_t) + n_qp *
641     sizeof (virtchnl_queue_pair_info_t);
642   u8 msg[msg_len];
643   virtchnl_vsi_queue_config_info_t *ci;
644
645   clib_memset (msg, 0, msg_len);
646   ci = (virtchnl_vsi_queue_config_info_t *) msg;
647   ci->vsi_id = ad->vsi_id;
648   ci->num_queue_pairs = n_qp;
649
650   avf_log_debug (ad, "config_vsi_queues: vsi_id %u num_queue_pairs %u",
651                  ad->vsi_id, ci->num_queue_pairs);
652
653   for (i = 0; i < n_qp; i++)
654     {
655       virtchnl_txq_info_t *txq = &ci->qpair[i].txq;
656       virtchnl_rxq_info_t *rxq = &ci->qpair[i].rxq;
657
658       rxq->vsi_id = ad->vsi_id;
659       rxq->queue_id = i;
660       rxq->max_pkt_size = ETHERNET_MAX_PACKET_BYTES;
661       if (i < vec_len (ad->rxqs))
662         {
663           avf_rxq_t *q = vec_elt_at_index (ad->rxqs, i);
664           rxq->ring_len = q->size;
665           rxq->databuffer_size = vlib_buffer_get_default_data_size (vm);
666           rxq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs);
667           avf_reg_write (ad, AVF_QRX_TAIL (i), q->size - 1);
668         }
669       avf_log_debug (ad, "config_vsi_queues_rx[%u]: max_pkt_size %u "
670                      "ring_len %u databuffer_size %u dma_ring_addr 0x%llx",
671                      i, rxq->max_pkt_size, rxq->ring_len,
672                      rxq->databuffer_size, rxq->dma_ring_addr);
673
674       txq->vsi_id = ad->vsi_id;
675       txq->queue_id = i;
676       if (i < vec_len (ad->txqs))
677         {
678           avf_txq_t *q = vec_elt_at_index (ad->txqs, i);
679           txq->ring_len = q->size;
680           txq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs);
681         }
682       avf_log_debug (ad, "config_vsi_queues_tx[%u]: ring_len %u "
683                      "dma_ring_addr 0x%llx", i, txq->ring_len,
684                      txq->dma_ring_addr);
685     }
686
687   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_VSI_QUEUES, msg, msg_len,
688                          0, 0);
689 }
690
691 clib_error_t *
692 avf_op_config_irq_map (vlib_main_t * vm, avf_device_t * ad)
693 {
694   int count = 1;
695   int msg_len = sizeof (virtchnl_irq_map_info_t) +
696     count * sizeof (virtchnl_vector_map_t);
697   u8 msg[msg_len];
698   virtchnl_irq_map_info_t *imi;
699
700   clib_memset (msg, 0, msg_len);
701   imi = (virtchnl_irq_map_info_t *) msg;
702   imi->num_vectors = count;
703
704   imi->vecmap[0].vector_id = 1;
705   imi->vecmap[0].vsi_id = ad->vsi_id;
706   imi->vecmap[0].rxq_map = (1 << ad->n_rx_queues) - 1;
707   imi->vecmap[0].txq_map = (1 << ad->n_tx_queues) - 1;
708
709   avf_log_debug (ad, "config_irq_map: vsi_id %u vector_id %u rxq_map %u",
710                  ad->vsi_id, imi->vecmap[0].vector_id,
711                  imi->vecmap[0].rxq_map);
712
713   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_IRQ_MAP, msg, msg_len, 0,
714                          0);
715 }
716
717 clib_error_t *
718 avf_op_add_eth_addr (vlib_main_t * vm, avf_device_t * ad, u8 count, u8 * macs)
719 {
720   int msg_len =
721     sizeof (virtchnl_ether_addr_list_t) +
722     count * sizeof (virtchnl_ether_addr_t);
723   u8 msg[msg_len];
724   virtchnl_ether_addr_list_t *al;
725   int i;
726
727   clib_memset (msg, 0, msg_len);
728   al = (virtchnl_ether_addr_list_t *) msg;
729   al->vsi_id = ad->vsi_id;
730   al->num_elements = count;
731
732   avf_log_debug (ad, "add_eth_addr: vsi_id %u num_elements %u",
733                  ad->vsi_id, al->num_elements);
734
735   for (i = 0; i < count; i++)
736     {
737       clib_memcpy_fast (&al->list[i].addr, macs + i * 6, 6);
738       avf_log_debug (ad, "add_eth_addr[%u]: %U", i,
739                      format_ethernet_address, &al->list[i].addr);
740     }
741   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ADD_ETH_ADDR, msg, msg_len, 0,
742                          0);
743 }
744
745 clib_error_t *
746 avf_op_enable_queues (vlib_main_t * vm, avf_device_t * ad, u32 rx, u32 tx)
747 {
748   virtchnl_queue_select_t qs = { 0 };
749   int i = 0;
750   qs.vsi_id = ad->vsi_id;
751   qs.rx_queues = rx;
752   qs.tx_queues = tx;
753
754   avf_log_debug (ad, "enable_queues: vsi_id %u rx_queues %u tx_queues %u",
755                  ad->vsi_id, qs.rx_queues, qs.tx_queues);
756
757   while (rx)
758     {
759       if (rx & (1 << i))
760         {
761           avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
762           avf_reg_write (ad, AVF_QRX_TAIL (i), rxq->n_enqueued);
763           rx &= ~(1 << i);
764         }
765       i++;
766     }
767   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ENABLE_QUEUES, &qs,
768                          sizeof (virtchnl_queue_select_t), 0, 0);
769 }
770
771 clib_error_t *
772 avf_op_get_stats (vlib_main_t * vm, avf_device_t * ad,
773                   virtchnl_eth_stats_t * es)
774 {
775   virtchnl_queue_select_t qs = { 0 };
776   qs.vsi_id = ad->vsi_id;
777
778   avf_log_debug (ad, "get_stats: vsi_id %u", ad->vsi_id);
779
780   return avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_STATS,
781                          &qs, sizeof (virtchnl_queue_select_t),
782                          es, sizeof (virtchnl_eth_stats_t));
783 }
784
785 clib_error_t *
786 avf_device_reset (vlib_main_t * vm, avf_device_t * ad)
787 {
788   avf_aq_desc_t d = { 0 };
789   clib_error_t *error;
790   u32 rstat;
791   f64 t0, t = 0, suspend_time = AVF_RESET_SUSPEND_TIME;
792
793   avf_log_debug (ad, "reset");
794
795   d.opcode = 0x801;
796   d.v_opcode = VIRTCHNL_OP_RESET_VF;
797   if ((error = avf_aq_desc_enq (vm, ad, &d, 0, 0)))
798     return error;
799
800   t0 = vlib_time_now (vm);
801 retry:
802   vlib_process_suspend (vm, suspend_time);
803
804   rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
805
806   if (rstat == 2 || rstat == 3)
807     {
808       avf_log_debug (ad, "reset completed in %.3fs", t);
809       return 0;
810     }
811
812   t = vlib_time_now (vm) - t0;
813   if (t > AVF_RESET_MAX_WAIT_TIME)
814     {
815       avf_log_err (ad, "reset failed (timeout %.3fs)", t);
816       return clib_error_return (0, "reset failed (timeout)");
817     }
818
819   suspend_time *= 2;
820   goto retry;
821 }
822
823 clib_error_t *
824 avf_request_queues (vlib_main_t * vm, avf_device_t * ad, u16 num_queue_pairs)
825 {
826   virtchnl_vf_res_request_t res_req = { 0 };
827   clib_error_t *error;
828   u32 rstat;
829   f64 t0, t, suspend_time = AVF_RESET_SUSPEND_TIME;
830
831   res_req.num_queue_pairs = num_queue_pairs;
832
833   avf_log_debug (ad, "request_queues: num_queue_pairs %u", num_queue_pairs);
834
835   error = avf_send_to_pf (vm, ad, VIRTCHNL_OP_REQUEST_QUEUES, &res_req,
836                           sizeof (virtchnl_vf_res_request_t), &res_req,
837                           sizeof (virtchnl_vf_res_request_t));
838
839   /*
840    * if PF responds, the request failed
841    * else PF initializes restart and avf_send_to_pf returns an error
842    */
843   if (!error)
844     {
845       return clib_error_return (0, "requested more than %u queue pairs",
846                                 res_req.num_queue_pairs);
847     }
848
849   t0 = vlib_time_now (vm);
850 retry:
851   vlib_process_suspend (vm, suspend_time);
852   t = vlib_time_now (vm) - t0;
853
854   rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
855
856   if ((rstat == VIRTCHNL_VFR_COMPLETED) || (rstat == VIRTCHNL_VFR_VFACTIVE))
857     goto done;
858
859   if (t > AVF_RESET_MAX_WAIT_TIME)
860     {
861       avf_log_err (ad, "request queues failed (timeout %.3f seconds)", t);
862       return clib_error_return (0, "request queues failed (timeout)");
863     }
864
865   suspend_time *= 2;
866   goto retry;
867
868 done:
869   return NULL;
870 }
871
872 clib_error_t *
873 avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad,
874                  avf_create_if_args_t * args)
875 {
876   virtchnl_version_info_t ver = { 0 };
877   virtchnl_vf_resource_t res = { 0 };
878   clib_error_t *error;
879   vlib_thread_main_t *tm = vlib_get_thread_main ();
880   int i;
881
882   avf_adminq_init (vm, ad);
883
884   if ((error = avf_request_queues (vm, ad, clib_max (tm->n_vlib_mains,
885                                                      args->rxq_num))))
886     {
887       /* we failed to get more queues, but still we want to proceed */
888       clib_error_free (error);
889
890       if ((error = avf_device_reset (vm, ad)))
891         return error;
892     }
893
894   avf_adminq_init (vm, ad);
895
896   /*
897    * OP_VERSION
898    */
899   if ((error = avf_op_version (vm, ad, &ver)))
900     return error;
901
902   if (ver.major != VIRTCHNL_VERSION_MAJOR ||
903       ver.minor != VIRTCHNL_VERSION_MINOR)
904     return clib_error_return (0, "incompatible protocol version "
905                               "(remote %d.%d)", ver.major, ver.minor);
906
907   /*
908    * OP_GET_VF_RESOURCES
909    */
910   if ((error = avf_op_get_vf_resources (vm, ad, &res)))
911     return error;
912
913   if (res.num_vsis != 1 || res.vsi_res[0].vsi_type != VIRTCHNL_VSI_SRIOV)
914     return clib_error_return (0, "unexpected GET_VF_RESOURCE reply received");
915
916   ad->vsi_id = res.vsi_res[0].vsi_id;
917   ad->feature_bitmap = res.vf_offload_flags;
918   ad->num_queue_pairs = res.num_queue_pairs;
919   ad->max_vectors = res.max_vectors;
920   ad->max_mtu = res.max_mtu;
921   ad->rss_key_size = res.rss_key_size;
922   ad->rss_lut_size = res.rss_lut_size;
923
924   clib_memcpy_fast (ad->hwaddr, res.vsi_res[0].default_mac_addr, 6);
925
926   /*
927    * Disable VLAN stripping
928    */
929   if ((error = avf_op_disable_vlan_stripping (vm, ad)))
930     return error;
931
932   /*
933    * Init Queues
934    */
935   if (args->rxq_num == 0)
936     {
937       args->rxq_num = 1;
938     }
939   else if (args->rxq_num > ad->num_queue_pairs)
940     {
941       args->rxq_num = ad->num_queue_pairs;
942       avf_log_warn (ad, "Requested more rx queues than queue pairs available."
943                     "Using %u rx queues.", args->rxq_num);
944     }
945
946   for (i = 0; i < args->rxq_num; i++)
947     if ((error = avf_rxq_init (vm, ad, i, args->rxq_size)))
948       return error;
949
950   for (i = 0; i < tm->n_vlib_mains; i++)
951     if ((error = avf_txq_init (vm, ad, i, args->txq_size)))
952       return error;
953
954   if ((ad->feature_bitmap & VIRTCHNL_VF_OFFLOAD_RSS_PF) &&
955       (error = avf_op_config_rss_lut (vm, ad)))
956     return error;
957
958   if ((ad->feature_bitmap & VIRTCHNL_VF_OFFLOAD_RSS_PF) &&
959       (error = avf_op_config_rss_key (vm, ad)))
960     return error;
961
962   if ((error = avf_op_config_vsi_queues (vm, ad)))
963     return error;
964
965   if ((error = avf_op_config_irq_map (vm, ad)))
966     return error;
967
968   avf_irq_0_enable (ad);
969   for (i = 0; i < ad->n_rx_queues; i++)
970     avf_irq_n_enable (ad, i);
971
972   if ((error = avf_op_add_eth_addr (vm, ad, 1, ad->hwaddr)))
973     return error;
974
975   if ((error = avf_op_enable_queues (vm, ad, pow2_mask (ad->n_rx_queues),
976                                      pow2_mask (ad->n_tx_queues))))
977     return error;
978
979   ad->flags |= AVF_DEVICE_F_INITIALIZED;
980   return error;
981 }
982
983 void
984 avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq)
985 {
986   avf_main_t *am = &avf_main;
987   vnet_main_t *vnm = vnet_get_main ();
988   virtchnl_pf_event_t *e;
989   u32 r;
990
991   if (ad->flags & AVF_DEVICE_F_ERROR)
992     return;
993
994   if ((ad->flags & AVF_DEVICE_F_INITIALIZED) == 0)
995     return;
996
997   ASSERT (ad->error == 0);
998
999   /* do not process device in reset state */
1000   r = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
1001   if (r != VIRTCHNL_VFR_VFACTIVE)
1002     return;
1003
1004   r = avf_get_u32 (ad->bar0, AVF_ARQLEN);
1005   if ((r & 0xf0000000) != (1ULL << 31))
1006     {
1007       ad->error = clib_error_return (0, "arq not enabled, arqlen = 0x%x", r);
1008       avf_log_err (ad, "error: %U", format_clib_error, ad->error);
1009       goto error;
1010     }
1011
1012   r = avf_get_u32 (ad->bar0, AVF_ATQLEN);
1013   if ((r & 0xf0000000) != (1ULL << 31))
1014     {
1015       ad->error = clib_error_return (0, "atq not enabled, atqlen = 0x%x", r);
1016       avf_log_err (ad, "error: %U", format_clib_error, ad->error);
1017       goto error;
1018     }
1019
1020   if (is_irq == 0)
1021     avf_op_get_stats (vm, ad, &ad->eth_stats);
1022
1023   /* *INDENT-OFF* */
1024   vec_foreach (e, ad->events)
1025     {
1026       avf_log_debug (ad, "event: %s (%u) sev %d",
1027                      virtchnl_event_names[e->event], e->event, e->severity);
1028       if (e->event == VIRTCHNL_EVENT_LINK_CHANGE)
1029         {
1030           int link_up = e->event_data.link_event.link_status;
1031           virtchnl_link_speed_t speed = e->event_data.link_event.link_speed;
1032           u32 flags = 0;
1033           u32 kbps = 0;
1034
1035           avf_log_debug (ad, "event_link_change: status %d speed '%s' (%d)",
1036                          link_up,
1037                          speed < ARRAY_LEN (virtchnl_link_speed_str) ?
1038                          virtchnl_link_speed_str[speed] : "unknown", speed);
1039
1040           if (link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) == 0)
1041             {
1042               ad->flags |= AVF_DEVICE_F_LINK_UP;
1043               flags |= (VNET_HW_INTERFACE_FLAG_FULL_DUPLEX |
1044                         VNET_HW_INTERFACE_FLAG_LINK_UP);
1045               if (speed == VIRTCHNL_LINK_SPEED_40GB)
1046                 kbps = 40000000;
1047               else if (speed == VIRTCHNL_LINK_SPEED_25GB)
1048                 kbps = 25000000;
1049               else if (speed == VIRTCHNL_LINK_SPEED_10GB)
1050                 kbps = 10000000;
1051               else if (speed == VIRTCHNL_LINK_SPEED_5GB)
1052                 kbps = 5000000;
1053               else if (speed == VIRTCHNL_LINK_SPEED_2_5GB)
1054                 kbps = 2500000;
1055               else if (speed == VIRTCHNL_LINK_SPEED_1GB)
1056                 kbps = 1000000;
1057               else if (speed == VIRTCHNL_LINK_SPEED_100MB)
1058                 kbps = 100000;
1059               vnet_hw_interface_set_flags (vnm, ad->hw_if_index, flags);
1060               vnet_hw_interface_set_link_speed (vnm, ad->hw_if_index, kbps);
1061               ad->link_speed = speed;
1062             }
1063           else if (!link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) != 0)
1064             {
1065               ad->flags &= ~AVF_DEVICE_F_LINK_UP;
1066               ad->link_speed = 0;
1067             }
1068
1069           if (ad->flags & AVF_DEVICE_F_ELOG)
1070             {
1071               ELOG_TYPE_DECLARE (el) =
1072                 {
1073                   .format = "avf[%d] link change: link_status %d "
1074                     "link_speed %d",
1075                   .format_args = "i4i1i1",
1076                 };
1077               struct
1078                 {
1079                   u32 dev_instance;
1080                   u8 link_status;
1081                   u8 link_speed;
1082                 } *ed;
1083               ed = ELOG_DATA (&vm->elog_main, el);
1084               ed->dev_instance = ad->dev_instance;
1085               ed->link_status = link_up;
1086               ed->link_speed = speed;
1087             }
1088         }
1089       else
1090         {
1091           if (ad->flags & AVF_DEVICE_F_ELOG)
1092             {
1093               ELOG_TYPE_DECLARE (el) =
1094                 {
1095                   .format = "avf[%d] unknown event: event %d severity %d",
1096                   .format_args = "i4i4i1i1",
1097                 };
1098               struct
1099                 {
1100                   u32 dev_instance;
1101                   u32 event;
1102                   u32 severity;
1103                 } *ed;
1104               ed = ELOG_DATA (&vm->elog_main, el);
1105               ed->dev_instance = ad->dev_instance;
1106               ed->event = e->event;
1107               ed->severity = e->severity;
1108             }
1109         }
1110     }
1111   /* *INDENT-ON* */
1112   vec_reset_length (ad->events);
1113
1114   return;
1115
1116 error:
1117   ad->flags |= AVF_DEVICE_F_ERROR;
1118   ASSERT (ad->error != 0);
1119   vlib_log_err (am->log_class, "%U", format_clib_error, ad->error);
1120 }
1121
1122 static u32
1123 avf_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
1124 {
1125   vlib_main_t *vm = vlib_get_main ();
1126   avf_main_t *am = &avf_main;
1127   avf_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
1128   if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
1129     {
1130       clib_error_t *error;
1131       int promisc_enabled = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
1132       u32 new_flags = promisc_enabled ?
1133         ad->flags | AVF_DEVICE_F_PROMISC : ad->flags & ~AVF_DEVICE_F_PROMISC;
1134
1135       if (new_flags == ad->flags)
1136         return flags;
1137
1138       if ((error = avf_config_promisc_mode (vm, ad, promisc_enabled)))
1139         {
1140           avf_log_err (ad, "%s: %U", format_clib_error, error);
1141           clib_error_free (error);
1142           return 0;
1143         }
1144
1145       ad->flags = new_flags;
1146     }
1147   return 0;
1148 }
1149
1150 static uword
1151 avf_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1152 {
1153   avf_main_t *am = &avf_main;
1154   avf_device_t *ad;
1155   uword *event_data = 0, event_type;
1156   int enabled = 0, irq;
1157   f64 last_run_duration = 0;
1158   f64 last_periodic_time = 0;
1159
1160   while (1)
1161     {
1162       if (enabled)
1163         vlib_process_wait_for_event_or_clock (vm, 5.0 - last_run_duration);
1164       else
1165         vlib_process_wait_for_event (vm);
1166
1167       event_type = vlib_process_get_events (vm, &event_data);
1168       vec_reset_length (event_data);
1169       irq = 0;
1170
1171       switch (event_type)
1172         {
1173         case ~0:
1174           last_periodic_time = vlib_time_now (vm);
1175           break;
1176         case AVF_PROCESS_EVENT_START:
1177           enabled = 1;
1178           break;
1179         case AVF_PROCESS_EVENT_STOP:
1180           enabled = 0;
1181           continue;
1182         case AVF_PROCESS_EVENT_AQ_INT:
1183           irq = 1;
1184           break;
1185         default:
1186           ASSERT (0);
1187         }
1188
1189       /* *INDENT-OFF* */
1190       pool_foreach (ad, am->devices,
1191         {
1192           avf_process_one_device (vm, ad, irq);
1193         });
1194       /* *INDENT-ON* */
1195       last_run_duration = vlib_time_now (vm) - last_periodic_time;
1196     }
1197   return 0;
1198 }
1199
1200 /* *INDENT-OFF* */
1201 VLIB_REGISTER_NODE (avf_process_node, static)  = {
1202   .function = avf_process,
1203   .type = VLIB_NODE_TYPE_PROCESS,
1204   .name = "avf-process",
1205 };
1206 /* *INDENT-ON* */
1207
1208 static void
1209 avf_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
1210 {
1211   avf_main_t *am = &avf_main;
1212   uword pd = vlib_pci_get_private_data (vm, h);
1213   avf_device_t *ad = pool_elt_at_index (am->devices, pd);
1214   u32 icr0;
1215
1216   icr0 = avf_reg_read (ad, AVFINT_ICR0);
1217
1218   if (ad->flags & AVF_DEVICE_F_ELOG)
1219     {
1220       /* *INDENT-OFF* */
1221       ELOG_TYPE_DECLARE (el) =
1222         {
1223           .format = "avf[%d] irq 0: icr0 0x%x",
1224           .format_args = "i4i4",
1225         };
1226       /* *INDENT-ON* */
1227       struct
1228       {
1229         u32 dev_instance;
1230         u32 icr0;
1231       } *ed;
1232
1233       ed = ELOG_DATA (&vm->elog_main, el);
1234       ed->dev_instance = ad->dev_instance;
1235       ed->icr0 = icr0;
1236     }
1237
1238   avf_irq_0_enable (ad);
1239
1240   /* bit 30 - Send/Receive Admin queue interrupt indication */
1241   if (icr0 & (1 << 30))
1242     vlib_process_signal_event (vm, avf_process_node.index,
1243                                AVF_PROCESS_EVENT_AQ_INT, 0);
1244 }
1245
1246 static void
1247 avf_irq_n_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
1248 {
1249   vnet_main_t *vnm = vnet_get_main ();
1250   avf_main_t *am = &avf_main;
1251   uword pd = vlib_pci_get_private_data (vm, h);
1252   avf_device_t *ad = pool_elt_at_index (am->devices, pd);
1253   u16 qid;
1254   int i;
1255
1256   if (ad->flags & AVF_DEVICE_F_ELOG)
1257     {
1258       /* *INDENT-OFF* */
1259       ELOG_TYPE_DECLARE (el) =
1260         {
1261           .format = "avf[%d] irq %d: received",
1262           .format_args = "i4i2",
1263         };
1264       /* *INDENT-ON* */
1265       struct
1266       {
1267         u32 dev_instance;
1268         u16 line;
1269       } *ed;
1270
1271       ed = ELOG_DATA (&vm->elog_main, el);
1272       ed->dev_instance = ad->dev_instance;
1273       ed->line = line;
1274     }
1275
1276   qid = line - 1;
1277   if (vec_len (ad->rxqs) > qid && ad->rxqs[qid].int_mode != 0)
1278     vnet_device_input_set_interrupt_pending (vnm, ad->hw_if_index, qid);
1279   for (i = 0; i < vec_len (ad->rxqs); i++)
1280     avf_irq_n_enable (ad, i);
1281 }
1282
1283 void
1284 avf_delete_if (vlib_main_t * vm, avf_device_t * ad)
1285 {
1286   vnet_main_t *vnm = vnet_get_main ();
1287   avf_main_t *am = &avf_main;
1288   int i;
1289
1290   if (ad->hw_if_index)
1291     {
1292       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
1293       vnet_hw_interface_unassign_rx_thread (vnm, ad->hw_if_index, 0);
1294       ethernet_delete_interface (vnm, ad->hw_if_index);
1295     }
1296
1297   vlib_pci_device_close (vm, ad->pci_dev_handle);
1298
1299   vlib_physmem_free (vm, ad->atq);
1300   vlib_physmem_free (vm, ad->arq);
1301   vlib_physmem_free (vm, ad->atq_bufs);
1302   vlib_physmem_free (vm, ad->arq_bufs);
1303
1304   /* *INDENT-OFF* */
1305   vec_foreach_index (i, ad->rxqs)
1306     {
1307       avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
1308       vlib_physmem_free (vm, (void *) rxq->descs);
1309       if (rxq->n_enqueued)
1310         vlib_buffer_free_from_ring (vm, rxq->bufs, rxq->next, rxq->size,
1311                                     rxq->n_enqueued);
1312       vec_free (rxq->bufs);
1313     }
1314   /* *INDENT-ON* */
1315   vec_free (ad->rxqs);
1316
1317   /* *INDENT-OFF* */
1318   vec_foreach_index (i, ad->txqs)
1319     {
1320       avf_txq_t *txq = vec_elt_at_index (ad->txqs, i);
1321       vlib_physmem_free (vm, (void *) txq->descs);
1322       if (txq->n_enqueued)
1323         {
1324           u16 first = (txq->next - txq->n_enqueued) & (txq->size -1);
1325           vlib_buffer_free_from_ring (vm, txq->bufs, first, txq->size,
1326                                       txq->n_enqueued);
1327         }
1328       vec_free (txq->bufs);
1329       clib_ring_free (txq->rs_slots);
1330     }
1331   /* *INDENT-ON* */
1332   vec_free (ad->txqs);
1333   vec_free (ad->name);
1334
1335   clib_error_free (ad->error);
1336   clib_memset (ad, 0, sizeof (*ad));
1337   pool_put (am->devices, ad);
1338 }
1339
1340 void
1341 avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args)
1342 {
1343   vnet_main_t *vnm = vnet_get_main ();
1344   avf_main_t *am = &avf_main;
1345   avf_device_t *ad;
1346   vlib_pci_dev_handle_t h;
1347   clib_error_t *error = 0;
1348   int i;
1349
1350   /* check input args */
1351   args->rxq_size = (args->rxq_size == 0) ? AVF_RXQ_SZ : args->rxq_size;
1352   args->txq_size = (args->txq_size == 0) ? AVF_TXQ_SZ : args->txq_size;
1353
1354   if ((args->rxq_size & (args->rxq_size - 1))
1355       || (args->txq_size & (args->txq_size - 1)))
1356     {
1357       args->rv = VNET_API_ERROR_INVALID_VALUE;
1358       args->error =
1359         clib_error_return (error, "queue size must be a power of two");
1360       return;
1361     }
1362
1363   pool_get (am->devices, ad);
1364   ad->dev_instance = ad - am->devices;
1365   ad->per_interface_next_index = ~0;
1366   ad->name = vec_dup (args->name);
1367
1368   if (args->enable_elog)
1369     ad->flags |= AVF_DEVICE_F_ELOG;
1370
1371   if ((error = vlib_pci_device_open (vm, &args->addr, avf_pci_device_ids,
1372                                      &h)))
1373     {
1374       pool_put (am->devices, ad);
1375       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1376       args->error =
1377         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1378                            &args->addr);
1379       return;
1380     }
1381   ad->pci_dev_handle = h;
1382   ad->pci_addr = args->addr;
1383   ad->numa_node = vlib_pci_get_numa_node (vm, h);
1384
1385   vlib_pci_set_private_data (vm, h, ad->dev_instance);
1386
1387   if ((error = vlib_pci_bus_master_enable (vm, h)))
1388     goto error;
1389
1390   if ((error = vlib_pci_map_region (vm, h, 0, &ad->bar0)))
1391     goto error;
1392
1393   if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1394                                                &avf_irq_0_handler)))
1395     goto error;
1396
1397   if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1398                                                &avf_irq_n_handler)))
1399     goto error;
1400
1401   if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
1402     goto error;
1403
1404   ad->atq = vlib_physmem_alloc_aligned_on_numa (vm, sizeof (avf_aq_desc_t) *
1405                                                 AVF_MBOX_LEN,
1406                                                 CLIB_CACHE_LINE_BYTES,
1407                                                 ad->numa_node);
1408   if (ad->atq == 0)
1409     {
1410       error = vlib_physmem_last_error (vm);
1411       goto error;
1412     }
1413
1414   if ((error = vlib_pci_map_dma (vm, h, ad->atq)))
1415     goto error;
1416
1417   ad->arq = vlib_physmem_alloc_aligned_on_numa (vm, sizeof (avf_aq_desc_t) *
1418                                                 AVF_MBOX_LEN,
1419                                                 CLIB_CACHE_LINE_BYTES,
1420                                                 ad->numa_node);
1421   if (ad->arq == 0)
1422     {
1423       error = vlib_physmem_last_error (vm);
1424       goto error;
1425     }
1426
1427   if ((error = vlib_pci_map_dma (vm, h, ad->arq)))
1428     goto error;
1429
1430   ad->atq_bufs = vlib_physmem_alloc_aligned_on_numa (vm, AVF_MBOX_BUF_SZ *
1431                                                      AVF_MBOX_LEN,
1432                                                      CLIB_CACHE_LINE_BYTES,
1433                                                      ad->numa_node);
1434   if (ad->atq_bufs == 0)
1435     {
1436       error = vlib_physmem_last_error (vm);
1437       goto error;
1438     }
1439
1440   if ((error = vlib_pci_map_dma (vm, h, ad->atq_bufs)))
1441     goto error;
1442
1443   ad->arq_bufs = vlib_physmem_alloc_aligned_on_numa (vm, AVF_MBOX_BUF_SZ *
1444                                                      AVF_MBOX_LEN,
1445                                                      CLIB_CACHE_LINE_BYTES,
1446                                                      ad->numa_node);
1447   if (ad->arq_bufs == 0)
1448     {
1449       error = vlib_physmem_last_error (vm);
1450       goto error;
1451     }
1452
1453   if ((error = vlib_pci_map_dma (vm, h, ad->arq_bufs)))
1454     goto error;
1455
1456   if ((error = vlib_pci_intr_enable (vm, h)))
1457     goto error;
1458
1459   if (vlib_pci_supports_virtual_addr_dma (vm, h))
1460     ad->flags |= AVF_DEVICE_F_VA_DMA;
1461
1462   if ((error = avf_device_init (vm, am, ad, args)))
1463     goto error;
1464
1465   /* create interface */
1466   error = ethernet_register_interface (vnm, avf_device_class.index,
1467                                        ad->dev_instance, ad->hwaddr,
1468                                        &ad->hw_if_index, avf_flag_change);
1469
1470   if (error)
1471     goto error;
1472
1473   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
1474   args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
1475
1476   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, ad->hw_if_index);
1477   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1478   vnet_hw_interface_set_input_node (vnm, ad->hw_if_index,
1479                                     avf_input_node.index);
1480
1481   for (i = 0; i < ad->n_rx_queues; i++)
1482     vnet_hw_interface_assign_rx_thread (vnm, ad->hw_if_index, i, ~0);
1483
1484   if (pool_elts (am->devices) == 1)
1485     vlib_process_signal_event (vm, avf_process_node.index,
1486                                AVF_PROCESS_EVENT_START, 0);
1487
1488   return;
1489
1490 error:
1491   avf_delete_if (vm, ad);
1492   args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1493   args->error = clib_error_return (error, "pci-addr %U",
1494                                    format_vlib_pci_addr, &args->addr);
1495   avf_log_err (ad, "error: %U", format_clib_error, args->error);
1496 }
1497
1498 static clib_error_t *
1499 avf_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1500 {
1501   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1502   avf_main_t *am = &avf_main;
1503   avf_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
1504   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1505
1506   if (ad->flags & AVF_DEVICE_F_ERROR)
1507     return clib_error_return (0, "device is in error state");
1508
1509   if (is_up)
1510     {
1511       vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
1512                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
1513       ad->flags |= AVF_DEVICE_F_ADMIN_UP;
1514     }
1515   else
1516     {
1517       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
1518       ad->flags &= ~AVF_DEVICE_F_ADMIN_UP;
1519     }
1520   return 0;
1521 }
1522
1523 static clib_error_t *
1524 avf_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
1525                               vnet_hw_interface_rx_mode mode)
1526 {
1527   avf_main_t *am = &avf_main;
1528   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1529   avf_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
1530   avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
1531
1532   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
1533     rxq->int_mode = 0;
1534   else
1535     rxq->int_mode = 1;
1536
1537   return 0;
1538 }
1539
1540 static void
1541 avf_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1542                              u32 node_index)
1543 {
1544   avf_main_t *am = &avf_main;
1545   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1546   avf_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
1547
1548   /* Shut off redirection */
1549   if (node_index == ~0)
1550     {
1551       ad->per_interface_next_index = node_index;
1552       return;
1553     }
1554
1555   ad->per_interface_next_index =
1556     vlib_node_add_next (vlib_get_main (), avf_input_node.index, node_index);
1557 }
1558
1559 static char *avf_tx_func_error_strings[] = {
1560 #define _(n,s) s,
1561   foreach_avf_tx_func_error
1562 #undef _
1563 };
1564
1565 static void
1566 avf_clear_hw_interface_counters (u32 instance)
1567 {
1568   avf_main_t *am = &avf_main;
1569   avf_device_t *ad = vec_elt_at_index (am->devices, instance);
1570   clib_memcpy_fast (&ad->last_cleared_eth_stats,
1571                     &ad->eth_stats, sizeof (ad->eth_stats));
1572 }
1573
1574 /* *INDENT-OFF* */
1575 VNET_DEVICE_CLASS (avf_device_class,) =
1576 {
1577   .name = "Adaptive Virtual Function (AVF) interface",
1578   .clear_counters = avf_clear_hw_interface_counters,
1579   .format_device = format_avf_device,
1580   .format_device_name = format_avf_device_name,
1581   .admin_up_down_function = avf_interface_admin_up_down,
1582   .rx_mode_change_function = avf_interface_rx_mode_change,
1583   .rx_redirect_to_node = avf_set_interface_next_node,
1584   .tx_function_n_errors = AVF_TX_N_ERROR,
1585   .tx_function_error_strings = avf_tx_func_error_strings,
1586 };
1587 /* *INDENT-ON* */
1588
1589 clib_error_t *
1590 avf_init (vlib_main_t * vm)
1591 {
1592   avf_main_t *am = &avf_main;
1593   vlib_thread_main_t *tm = vlib_get_thread_main ();
1594
1595   vec_validate_aligned (am->per_thread_data, tm->n_vlib_mains - 1,
1596                         CLIB_CACHE_LINE_BYTES);
1597
1598   am->log_class = vlib_log_register_class ("avf", 0);
1599   vlib_log_debug (am->log_class, "initialized");
1600
1601   return 0;
1602 }
1603
1604 /* *INDENT-OFF* */
1605 VLIB_INIT_FUNCTION (avf_init) =
1606 {
1607   .runs_after = VLIB_INITS ("pci_bus_init"),
1608 };
1609 /* *INDENT-OFF* */
1610
1611 /*
1612  * fd.io coding-style-patch-verification: ON
1613  *
1614  * Local Variables:
1615  * eval: (c-set-style "gnu")
1616  * End:
1617  */