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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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>
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 8160
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
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},
47 avf_irq_0_disable (avf_device_t * ad)
49 u32 dyn_ctl0 = 0, icr0_ena = 0;
51 dyn_ctl0 |= (3 << 3); /* 11b = No ITR update */
53 avf_reg_write (ad, AVFINT_ICR0_ENA1, icr0_ena);
54 avf_reg_write (ad, AVFINT_DYN_CTL0, dyn_ctl0);
59 avf_irq_0_enable (avf_device_t * ad)
61 u32 dyn_ctl0 = 0, icr0_ena = 0;
63 icr0_ena |= (1 << 30); /* [30] Admin Queue Enable */
65 dyn_ctl0 |= (1 << 0); /* [0] Interrupt Enable */
66 dyn_ctl0 |= (1 << 1); /* [1] Clear PBA */
67 //dyn_ctl0 |= (3 << 3); /* [4:3] ITR Index, 11b = No ITR update */
68 dyn_ctl0 |= ((AVF_ITR_INT / 2) << 5); /* [16:5] ITR Interval in 2us steps */
70 avf_irq_0_disable (ad);
71 avf_reg_write (ad, AVFINT_ICR0_ENA1, icr0_ena);
72 avf_reg_write (ad, AVFINT_DYN_CTL0, dyn_ctl0);
77 avf_irq_n_disable (avf_device_t * ad, u8 line)
81 avf_reg_write (ad, AVFINT_DYN_CTLN (line), dyn_ctln);
86 avf_irq_n_enable (avf_device_t * ad, u8 line)
90 dyn_ctln |= (1 << 0); /* [0] Interrupt Enable */
91 dyn_ctln |= (1 << 1); /* [1] Clear PBA */
92 dyn_ctln |= ((AVF_ITR_INT / 2) << 5); /* [16:5] ITR Interval in 2us steps */
94 avf_irq_n_disable (ad, line);
95 avf_reg_write (ad, AVFINT_DYN_CTLN (line), dyn_ctln);
101 avf_aq_desc_enq (vlib_main_t * vm, avf_device_t * ad, avf_aq_desc_t * dt,
104 avf_main_t *am = &avf_main;
105 clib_error_t *err = 0;
106 avf_aq_desc_t *d, dc;
109 d = &ad->atq[ad->atq_next_slot];
110 clib_memcpy_fast (d, dt, sizeof (avf_aq_desc_t));
111 d->flags |= AVF_AQ_F_RD | AVF_AQ_F_SI;
117 pa = ad->atq_bufs_pa + ad->atq_next_slot * AVF_MBOX_BUF_SZ;
118 d->addr_hi = (u32) (pa >> 32);
119 d->addr_lo = (u32) pa;
120 clib_memcpy_fast (ad->atq_bufs + ad->atq_next_slot * AVF_MBOX_BUF_SZ,
122 d->flags |= AVF_AQ_F_BUF;
125 if (ad->flags & AVF_DEVICE_F_ELOG)
126 clib_memcpy_fast (&dc, d, sizeof (avf_aq_desc_t));
128 CLIB_MEMORY_BARRIER ();
129 vlib_log_debug (am->log_class, "%U", format_hexdump, data, len);
130 ad->atq_next_slot = (ad->atq_next_slot + 1) % AVF_MBOX_LEN;
131 avf_reg_write (ad, AVF_ATQT, ad->atq_next_slot);
135 vlib_process_suspend (vm, 10e-6);
137 if (((d->flags & AVF_AQ_F_DD) == 0) || ((d->flags & AVF_AQ_F_CMP) == 0))
141 err = clib_error_return (0, "adminq enqueue timeout [opcode 0x%x]",
148 clib_memcpy_fast (dt, d, sizeof (avf_aq_desc_t));
149 if (d->flags & AVF_AQ_F_ERR)
150 return clib_error_return (0, "adminq enqueue error [opcode 0x%x, retval "
151 "%d]", d->opcode, d->retval);
154 if (ad->flags & AVF_DEVICE_F_ELOG)
157 ELOG_TYPE_DECLARE (el) =
159 .format = "avf[%d] aq enq: s_flags 0x%x r_flags 0x%x opcode 0x%x "
160 "datalen %d retval %d",
161 .format_args = "i4i2i2i2i2i2",
172 ed = ELOG_DATA (&vm->elog_main, el);
173 ed->dev_instance = ad->dev_instance;
174 ed->s_flags = dc.flags;
175 ed->r_flags = d->flags;
176 ed->opcode = dc.opcode;
177 ed->datalen = dc.datalen;
178 ed->retval = d->retval;
186 avf_cmd_rx_ctl_reg_write (vlib_main_t * vm, avf_device_t * ad, u32 reg,
190 avf_aq_desc_t d = {.opcode = 0x207,.param1 = reg,.param3 = val };
191 err = avf_aq_desc_enq (vm, ad, &d, 0, 0);
193 if (ad->flags & AVF_DEVICE_F_ELOG)
196 ELOG_TYPE_DECLARE (el) =
198 .format = "avf[%d] rx ctl reg write: reg 0x%x val 0x%x ",
199 .format_args = "i4i4i4",
207 ed = ELOG_DATA (&vm->elog_main, el);
208 ed->dev_instance = ad->dev_instance;
217 avf_rxq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 rxq_size)
223 vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES);
224 rxq = vec_elt_at_index (ad->rxqs, qid);
225 rxq->size = rxq_size;
227 rxq->descs = vlib_physmem_alloc_aligned (vm, rxq->size *
228 sizeof (avf_rx_desc_t),
229 2 * CLIB_CACHE_LINE_BYTES);
231 return vlib_physmem_last_error (vm);
233 if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) rxq->descs)))
236 clib_memset ((void *) rxq->descs, 0, rxq->size * sizeof (avf_rx_desc_t));
237 vec_validate_aligned (rxq->bufs, rxq->size, CLIB_CACHE_LINE_BYTES);
238 rxq->qrx_tail = ad->bar0 + AVF_QRX_TAIL (qid);
240 n_alloc = vlib_buffer_alloc (vm, rxq->bufs, rxq->size - 8);
243 return clib_error_return (0, "buffer allocation error");
245 rxq->n_enqueued = n_alloc;
246 avf_rx_desc_t *d = rxq->descs;
247 for (i = 0; i < n_alloc; i++)
249 vlib_buffer_t *b = vlib_get_buffer (vm, rxq->bufs[i]);
250 if (ad->flags & AVF_DEVICE_F_VA_DMA)
251 d->qword[0] = vlib_buffer_get_va (b);
253 d->qword[0] = vlib_buffer_get_pa (vm, b);
257 ad->n_rx_queues = clib_min (ad->num_queue_pairs, qid + 1);
262 avf_txq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 txq_size)
267 if (qid >= ad->num_queue_pairs)
269 qid = qid % ad->num_queue_pairs;
270 txq = vec_elt_at_index (ad->txqs, qid);
272 clib_spinlock_init (&txq->lock);
273 ad->flags |= AVF_DEVICE_F_SHARED_TXQ_LOCK;
277 vec_validate_aligned (ad->txqs, qid, CLIB_CACHE_LINE_BYTES);
278 txq = vec_elt_at_index (ad->txqs, qid);
279 txq->size = txq_size;
281 txq->descs = vlib_physmem_alloc_aligned (vm, txq->size *
282 sizeof (avf_tx_desc_t),
283 2 * CLIB_CACHE_LINE_BYTES);
285 return vlib_physmem_last_error (vm);
287 if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) txq->descs)))
290 vec_validate_aligned (txq->bufs, txq->size, CLIB_CACHE_LINE_BYTES);
291 txq->qtx_tail = ad->bar0 + AVF_QTX_TAIL (qid);
293 /* initialize ring of pending RS slots */
294 clib_ring_new_aligned (txq->rs_slots, 32, CLIB_CACHE_LINE_BYTES);
296 ad->n_tx_queues = clib_min (ad->num_queue_pairs, qid + 1);
304 } virtchnl_promisc_info_t;
307 avf_arq_slot_init (avf_device_t * ad, u16 slot)
310 u64 pa = ad->arq_bufs_pa + slot * AVF_MBOX_BUF_SZ;
312 clib_memset (d, 0, sizeof (avf_aq_desc_t));
313 d->flags = AVF_AQ_F_BUF;
314 d->datalen = AVF_MBOX_BUF_SZ;
315 d->addr_hi = (u32) (pa >> 32);
316 d->addr_lo = (u32) pa;
320 avf_dma_addr (vlib_main_t * vm, avf_device_t * ad, void *p)
322 return (ad->flags & AVF_DEVICE_F_VA_DMA) ?
323 pointer_to_uword (p) : vlib_physmem_get_pa (vm, p);
327 avf_adminq_init (vlib_main_t * vm, avf_device_t * ad)
332 /* VF MailBox Transmit */
333 clib_memset (ad->atq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN);
334 ad->atq_bufs_pa = avf_dma_addr (vm, ad, ad->atq_bufs);
336 pa = avf_dma_addr (vm, ad, ad->atq);
337 avf_reg_write (ad, AVF_ATQT, 0); /* Tail */
338 avf_reg_write (ad, AVF_ATQH, 0); /* Head */
339 avf_reg_write (ad, AVF_ATQLEN, AVF_MBOX_LEN | (1ULL << 31)); /* len & ena */
340 avf_reg_write (ad, AVF_ATQBAL, (u32) pa); /* Base Address Low */
341 avf_reg_write (ad, AVF_ATQBAH, (u32) (pa >> 32)); /* Base Address High */
343 /* VF MailBox Receive */
344 clib_memset (ad->arq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN);
345 ad->arq_bufs_pa = avf_dma_addr (vm, ad, ad->arq_bufs);
347 for (i = 0; i < AVF_MBOX_LEN; i++)
348 avf_arq_slot_init (ad, i);
350 pa = avf_dma_addr (vm, ad, ad->arq);
352 avf_reg_write (ad, AVF_ARQH, 0); /* Head */
353 avf_reg_write (ad, AVF_ARQT, 0); /* Head */
354 avf_reg_write (ad, AVF_ARQLEN, AVF_MBOX_LEN | (1ULL << 31)); /* len & ena */
355 avf_reg_write (ad, AVF_ARQBAL, (u32) pa); /* Base Address Low */
356 avf_reg_write (ad, AVF_ARQBAH, (u32) (pa >> 32)); /* Base Address High */
357 avf_reg_write (ad, AVF_ARQT, AVF_MBOX_LEN - 1); /* Tail */
359 ad->atq_next_slot = 0;
360 ad->arq_next_slot = 0;
364 avf_send_to_pf (vlib_main_t * vm, avf_device_t * ad, virtchnl_ops_t op,
365 void *in, int in_len, void *out, int out_len)
368 avf_aq_desc_t *d, dt = {.opcode = 0x801,.v_opcode = op };
373 /* supppres interrupt in the next adminq receive slot
374 as we are going to wait for response
375 we only need interrupts when event is received */
376 d = &ad->arq[ad->arq_next_slot];
377 d->flags |= AVF_AQ_F_SI;
379 if ((err = avf_aq_desc_enq (vm, ad, &dt, in, in_len)))
383 head = avf_get_u32 (ad->bar0, AVF_ARQH);
385 if (ad->arq_next_slot == head)
388 return clib_error_return (0, "timeout");
389 vlib_process_suspend (vm, 10e-3);
393 d = &ad->arq[ad->arq_next_slot];
395 if (d->v_opcode == VIRTCHNL_OP_EVENT)
397 void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ;
398 virtchnl_pf_event_t *e;
400 if ((d->datalen != sizeof (virtchnl_pf_event_t)) ||
401 ((d->flags & AVF_AQ_F_BUF) == 0))
402 return clib_error_return (0, "event message error");
404 vec_add2 (ad->events, e, 1);
405 clib_memcpy_fast (e, buf, sizeof (virtchnl_pf_event_t));
406 avf_arq_slot_init (ad, ad->arq_next_slot);
412 if (d->v_opcode != op)
415 clib_error_return (0,
416 "unexpected message receiver [v_opcode = %u, "
417 "expected %u, v_retval %d]", d->v_opcode, op,
424 err = clib_error_return (0, "error [v_opcode = %u, v_retval %d]",
425 d->v_opcode, d->v_retval);
429 if (d->flags & AVF_AQ_F_BUF)
431 void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ;
432 clib_memcpy_fast (out, buf, out_len);
435 avf_arq_slot_init (ad, ad->arq_next_slot);
436 avf_reg_write (ad, AVF_ARQT, ad->arq_next_slot);
438 ad->arq_next_slot = (ad->arq_next_slot + 1) % AVF_MBOX_LEN;
442 if (ad->flags & AVF_DEVICE_F_ELOG)
445 ELOG_TYPE_DECLARE (el) =
447 .format = "avf[%d] send to pf: v_opcode %s (%d) v_retval 0x%x",
448 .format_args = "i4t4i4i4",
449 .n_enum_strings = VIRTCHNL_N_OPS,
451 #define _(v, n) [v] = #n,
463 ed = ELOG_DATA (&vm->elog_main, el);
464 ed->dev_instance = ad->dev_instance;
466 ed->v_opcode_val = op;
467 ed->v_retval = d->v_retval;
474 avf_op_version (vlib_main_t * vm, avf_device_t * ad,
475 virtchnl_version_info_t * ver)
477 clib_error_t *err = 0;
478 virtchnl_version_info_t myver = {
479 .major = VIRTCHNL_VERSION_MAJOR,
480 .minor = VIRTCHNL_VERSION_MINOR,
483 err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_VERSION, &myver,
484 sizeof (virtchnl_version_info_t), ver,
485 sizeof (virtchnl_version_info_t));
494 avf_op_get_vf_resources (vlib_main_t * vm, avf_device_t * ad,
495 virtchnl_vf_resource_t * res)
497 u32 bitmap = (VIRTCHNL_VF_OFFLOAD_L2 | VIRTCHNL_VF_OFFLOAD_RSS_PF |
498 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR | VIRTCHNL_VF_OFFLOAD_VLAN |
499 VIRTCHNL_VF_OFFLOAD_RX_POLLING);
501 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_VF_RESOURCES, &bitmap,
502 sizeof (u32), res, sizeof (virtchnl_vf_resource_t));
506 avf_op_config_rss_lut (vlib_main_t * vm, avf_device_t * ad)
508 int msg_len = sizeof (virtchnl_rss_lut_t) + ad->rss_lut_size - 1;
511 virtchnl_rss_lut_t *rl;
513 clib_memset (msg, 0, msg_len);
514 rl = (virtchnl_rss_lut_t *) msg;
515 rl->vsi_id = ad->vsi_id;
516 rl->lut_entries = ad->rss_lut_size;
517 for (i = 0; i < ad->rss_lut_size; i++)
518 rl->lut[i] = i % ad->n_rx_queues;
520 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_LUT, msg, msg_len, 0,
525 avf_op_config_rss_key (vlib_main_t * vm, avf_device_t * ad)
527 int msg_len = sizeof (virtchnl_rss_key_t) + ad->rss_key_size - 1;
530 virtchnl_rss_key_t *rk;
532 clib_memset (msg, 0, msg_len);
533 rk = (virtchnl_rss_key_t *) msg;
534 rk->vsi_id = ad->vsi_id;
535 rk->key_len = ad->rss_key_size;
536 u32 seed = random_default_seed ();
537 for (i = 0; i < ad->rss_key_size; i++)
538 rk->key[i] = (u8) random_u32 (&seed);
540 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_KEY, msg, msg_len, 0,
545 avf_op_disable_vlan_stripping (vlib_main_t * vm, avf_device_t * ad)
547 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, 0, 0, 0,
552 avf_config_promisc_mode (vlib_main_t * vm, avf_device_t * ad)
554 virtchnl_promisc_info_t pi = { 0 };
556 pi.vsi_id = ad->vsi_id;
558 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE, &pi,
559 sizeof (virtchnl_promisc_info_t), 0, 0);
564 avf_op_config_vsi_queues (vlib_main_t * vm, avf_device_t * ad)
567 int n_qp = clib_max (vec_len (ad->rxqs), vec_len (ad->txqs));
568 int msg_len = sizeof (virtchnl_vsi_queue_config_info_t) + n_qp *
569 sizeof (virtchnl_queue_pair_info_t);
571 virtchnl_vsi_queue_config_info_t *ci;
573 clib_memset (msg, 0, msg_len);
574 ci = (virtchnl_vsi_queue_config_info_t *) msg;
575 ci->vsi_id = ad->vsi_id;
576 ci->num_queue_pairs = n_qp;
578 for (i = 0; i < n_qp; i++)
580 virtchnl_txq_info_t *txq = &ci->qpair[i].txq;
581 virtchnl_rxq_info_t *rxq = &ci->qpair[i].rxq;
583 rxq->vsi_id = ad->vsi_id;
585 rxq->max_pkt_size = ETHERNET_MAX_PACKET_BYTES;
586 if (i < vec_len (ad->rxqs))
588 avf_rxq_t *q = vec_elt_at_index (ad->rxqs, i);
589 rxq->ring_len = q->size;
590 rxq->databuffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
591 rxq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs);
592 avf_reg_write (ad, AVF_QRX_TAIL (i), q->size - 1);
595 avf_txq_t *q = vec_elt_at_index (ad->txqs, i);
596 txq->vsi_id = ad->vsi_id;
597 if (i < vec_len (ad->txqs))
600 txq->ring_len = q->size;
601 txq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs);
605 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_VSI_QUEUES, msg, msg_len,
610 avf_op_config_irq_map (vlib_main_t * vm, avf_device_t * ad)
613 int msg_len = sizeof (virtchnl_irq_map_info_t) +
614 count * sizeof (virtchnl_vector_map_t);
616 virtchnl_irq_map_info_t *imi;
618 clib_memset (msg, 0, msg_len);
619 imi = (virtchnl_irq_map_info_t *) msg;
620 imi->num_vectors = count;
622 imi->vecmap[0].vector_id = 1;
623 imi->vecmap[0].vsi_id = ad->vsi_id;
624 imi->vecmap[0].rxq_map = 1;
625 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_IRQ_MAP, msg, msg_len, 0,
630 avf_op_add_eth_addr (vlib_main_t * vm, avf_device_t * ad, u8 count, u8 * macs)
633 sizeof (virtchnl_ether_addr_list_t) +
634 count * sizeof (virtchnl_ether_addr_t);
636 virtchnl_ether_addr_list_t *al;
639 clib_memset (msg, 0, msg_len);
640 al = (virtchnl_ether_addr_list_t *) msg;
641 al->vsi_id = ad->vsi_id;
642 al->num_elements = count;
643 for (i = 0; i < count; i++)
644 clib_memcpy_fast (&al->list[i].addr, macs + i * 6, 6);
645 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ADD_ETH_ADDR, msg, msg_len, 0,
650 avf_op_enable_queues (vlib_main_t * vm, avf_device_t * ad, u32 rx, u32 tx)
652 virtchnl_queue_select_t qs = { 0 };
654 qs.vsi_id = ad->vsi_id;
657 for (i = 0; i < ad->n_rx_queues; i++)
659 avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
660 avf_reg_write (ad, AVF_QRX_TAIL (i), rxq->n_enqueued);
662 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ENABLE_QUEUES, &qs,
663 sizeof (virtchnl_queue_select_t), 0, 0);
667 avf_op_get_stats (vlib_main_t * vm, avf_device_t * ad,
668 virtchnl_eth_stats_t * es)
670 virtchnl_queue_select_t qs = { 0 };
671 qs.vsi_id = ad->vsi_id;
672 return avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_STATS,
673 &qs, sizeof (virtchnl_queue_select_t),
674 es, sizeof (virtchnl_eth_stats_t));
678 avf_device_reset (vlib_main_t * vm, avf_device_t * ad)
680 avf_aq_desc_t d = { 0 };
686 d.v_opcode = VIRTCHNL_OP_RESET_VF;
687 if ((error = avf_aq_desc_enq (vm, ad, &d, 0, 0)))
691 vlib_process_suspend (vm, 10e-3);
692 rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
694 if (rstat == 2 || rstat == 3)
698 return clib_error_return (0, "reset failed (timeout)");
704 avf_request_queues (vlib_main_t * vm, avf_device_t * ad, u16 num_queue_pairs)
706 virtchnl_vf_res_request_t res_req = { 0 };
711 res_req.num_queue_pairs = num_queue_pairs;
713 error = avf_send_to_pf (vm, ad, VIRTCHNL_OP_REQUEST_QUEUES, &res_req,
714 sizeof (virtchnl_vf_res_request_t), &res_req,
715 sizeof (virtchnl_vf_res_request_t));
718 * if PF respondes, the request failed
719 * else PF initializes restart and avf_send_to_pf returns an error
723 return clib_error_return (0, "requested more than %u queue pairs",
724 res_req.num_queue_pairs);
728 vlib_process_suspend (vm, 10e-3);
729 rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
731 if ((rstat == VIRTCHNL_VFR_COMPLETED) || (rstat == VIRTCHNL_VFR_VFACTIVE))
735 return clib_error_return (0, "reset failed (timeout)");
744 avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad,
745 avf_create_if_args_t * args)
747 virtchnl_version_info_t ver = { 0 };
748 virtchnl_vf_resource_t res = { 0 };
750 vlib_thread_main_t *tm = vlib_get_thread_main ();
753 avf_adminq_init (vm, ad);
755 /* request more queues only if we need them */
756 if ((error = avf_request_queues (vm, ad, tm->n_vlib_mains)))
758 /* we failed to get more queues, but still we want to proceed */
759 clib_error_free (error);
761 if ((error = avf_device_reset (vm, ad)))
765 avf_adminq_init (vm, ad);
770 if ((error = avf_op_version (vm, ad, &ver)))
773 if (ver.major != VIRTCHNL_VERSION_MAJOR ||
774 ver.minor != VIRTCHNL_VERSION_MINOR)
775 return clib_error_return (0, "incompatible protocol version "
776 "(remote %d.%d)", ver.major, ver.minor);
781 if ((error = avf_op_get_vf_resources (vm, ad, &res)))
784 if (res.num_vsis != 1 || res.vsi_res[0].vsi_type != VIRTCHNL_VSI_SRIOV)
785 return clib_error_return (0, "unexpected GET_VF_RESOURCE reply received");
787 ad->vsi_id = res.vsi_res[0].vsi_id;
788 ad->feature_bitmap = res.vf_offload_flags;
789 ad->num_queue_pairs = res.num_queue_pairs;
790 ad->max_vectors = res.max_vectors;
791 ad->max_mtu = res.max_mtu;
792 ad->rss_key_size = res.rss_key_size;
793 ad->rss_lut_size = res.rss_lut_size;
795 clib_memcpy_fast (ad->hwaddr, res.vsi_res[0].default_mac_addr, 6);
798 * Disable VLAN stripping
800 if ((error = avf_op_disable_vlan_stripping (vm, ad)))
803 if ((error = avf_config_promisc_mode (vm, ad)))
809 if (args->rxq_num == 0)
813 else if (args->rxq_num > ad->num_queue_pairs)
815 args->rxq_num = ad->num_queue_pairs;
816 vlib_log_warn (am->log_class, "Requested more rx queues than"
817 "queue pairs available. Using %u rx queues.",
821 for (i = 0; i < args->rxq_num; i++)
822 if ((error = avf_rxq_init (vm, ad, i, args->rxq_size)))
825 for (i = 0; i < tm->n_vlib_mains; i++)
826 if ((error = avf_txq_init (vm, ad, i, args->txq_size)))
829 if ((ad->feature_bitmap & VIRTCHNL_VF_OFFLOAD_RSS_PF) &&
830 (error = avf_op_config_rss_lut (vm, ad)))
833 if ((ad->feature_bitmap & VIRTCHNL_VF_OFFLOAD_RSS_PF) &&
834 (error = avf_op_config_rss_key (vm, ad)))
837 if ((error = avf_op_config_vsi_queues (vm, ad)))
840 if ((error = avf_op_config_irq_map (vm, ad)))
843 avf_irq_0_enable (ad);
844 for (i = 0; i < ad->n_rx_queues; i++)
845 avf_irq_n_enable (ad, i);
847 if ((error = avf_op_add_eth_addr (vm, ad, 1, ad->hwaddr)))
850 if ((error = avf_op_enable_queues (vm, ad, ad->n_rx_queues, 0)))
853 if ((error = avf_op_enable_queues (vm, ad, 0, ad->n_tx_queues)))
856 ad->flags |= AVF_DEVICE_F_INITIALIZED;
861 avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq)
863 avf_main_t *am = &avf_main;
864 vnet_main_t *vnm = vnet_get_main ();
865 virtchnl_pf_event_t *e;
868 if (ad->flags & AVF_DEVICE_F_ERROR)
871 if ((ad->flags & AVF_DEVICE_F_INITIALIZED) == 0)
874 ASSERT (ad->error == 0);
876 /* do not process device in reset state */
877 r = avf_get_u32 (ad->bar0, AVFGEN_RSTAT);
878 if (r != VIRTCHNL_VFR_VFACTIVE)
881 r = avf_get_u32 (ad->bar0, AVF_ARQLEN);
882 if ((r & 0xf0000000) != (1ULL << 31))
884 ad->error = clib_error_return (0, "arq not enabled, arqlen = 0x%x", r);
888 r = avf_get_u32 (ad->bar0, AVF_ATQLEN);
889 if ((r & 0xf0000000) != (1ULL << 31))
891 ad->error = clib_error_return (0, "atq not enabled, atqlen = 0x%x", r);
896 avf_op_get_stats (vm, ad, &ad->eth_stats);
899 vec_foreach (e, ad->events)
901 if (e->event == VIRTCHNL_EVENT_LINK_CHANGE)
903 int link_up = e->event_data.link_event.link_status;
904 virtchnl_link_speed_t speed = e->event_data.link_event.link_speed;
908 if (link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) == 0)
910 ad->flags |= AVF_DEVICE_F_LINK_UP;
911 flags |= (VNET_HW_INTERFACE_FLAG_FULL_DUPLEX |
912 VNET_HW_INTERFACE_FLAG_LINK_UP);
913 if (speed == VIRTCHNL_LINK_SPEED_40GB)
915 else if (speed == VIRTCHNL_LINK_SPEED_25GB)
917 else if (speed == VIRTCHNL_LINK_SPEED_10GB)
919 else if (speed == VIRTCHNL_LINK_SPEED_1GB)
921 else if (speed == VIRTCHNL_LINK_SPEED_100MB)
923 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, flags);
924 vnet_hw_interface_set_link_speed (vnm, ad->hw_if_index, kbps);
925 ad->link_speed = speed;
927 else if (!link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) != 0)
929 ad->flags &= ~AVF_DEVICE_F_LINK_UP;
933 if (ad->flags & AVF_DEVICE_F_ELOG)
935 ELOG_TYPE_DECLARE (el) =
937 .format = "avf[%d] link change: link_status %d "
939 .format_args = "i4i1i1",
947 ed = ELOG_DATA (&vm->elog_main, el);
948 ed->dev_instance = ad->dev_instance;
949 ed->link_status = link_up;
950 ed->link_speed = speed;
955 if (ad->flags & AVF_DEVICE_F_ELOG)
957 ELOG_TYPE_DECLARE (el) =
959 .format = "avf[%d] unknown event: event %d severity %d",
960 .format_args = "i4i4i1i1",
968 ed = ELOG_DATA (&vm->elog_main, el);
969 ed->dev_instance = ad->dev_instance;
970 ed->event = e->event;
971 ed->severity = e->severity;
976 vec_reset_length (ad->events);
981 ad->flags |= AVF_DEVICE_F_ERROR;
982 ASSERT (ad->error != 0);
983 vlib_log_err (am->log_class, "%U", format_clib_error, ad->error);
987 avf_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
989 avf_main_t *am = &avf_main;
990 vlib_log_warn (am->log_class, "TODO");
995 avf_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
997 avf_main_t *am = &avf_main;
999 uword *event_data = 0, event_type;
1000 int enabled = 0, irq;
1001 f64 last_run_duration = 0;
1002 f64 last_periodic_time = 0;
1007 vlib_process_wait_for_event_or_clock (vm, 5.0 - last_run_duration);
1009 vlib_process_wait_for_event (vm);
1011 event_type = vlib_process_get_events (vm, &event_data);
1012 vec_reset_length (event_data);
1018 last_periodic_time = vlib_time_now (vm);
1020 case AVF_PROCESS_EVENT_START:
1023 case AVF_PROCESS_EVENT_STOP:
1026 case AVF_PROCESS_EVENT_AQ_INT:
1034 pool_foreach (ad, am->devices,
1036 avf_process_one_device (vm, ad, irq);
1039 last_run_duration = vlib_time_now (vm) - last_periodic_time;
1045 VLIB_REGISTER_NODE (avf_process_node, static) = {
1046 .function = avf_process,
1047 .type = VLIB_NODE_TYPE_PROCESS,
1048 .name = "avf-process",
1053 avf_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
1055 avf_main_t *am = &avf_main;
1056 uword pd = vlib_pci_get_private_data (vm, h);
1057 avf_device_t *ad = pool_elt_at_index (am->devices, pd);
1060 icr0 = avf_reg_read (ad, AVFINT_ICR0);
1062 if (ad->flags & AVF_DEVICE_F_ELOG)
1065 ELOG_TYPE_DECLARE (el) =
1067 .format = "avf[%d] irq 0: icr0 0x%x",
1068 .format_args = "i4i4",
1077 ed = ELOG_DATA (&vm->elog_main, el);
1078 ed->dev_instance = ad->dev_instance;
1082 avf_irq_0_enable (ad);
1084 /* bit 30 - Send/Receive Admin queue interrupt indication */
1085 if (icr0 & (1 << 30))
1086 vlib_process_signal_event (vm, avf_process_node.index,
1087 AVF_PROCESS_EVENT_AQ_INT, 0);
1091 avf_irq_n_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
1093 vnet_main_t *vnm = vnet_get_main ();
1094 avf_main_t *am = &avf_main;
1095 uword pd = vlib_pci_get_private_data (vm, h);
1096 avf_device_t *ad = pool_elt_at_index (am->devices, pd);
1100 if (ad->flags & AVF_DEVICE_F_ELOG)
1103 ELOG_TYPE_DECLARE (el) =
1105 .format = "avf[%d] irq %d: received",
1106 .format_args = "i4i2",
1115 ed = ELOG_DATA (&vm->elog_main, el);
1116 ed->dev_instance = ad->dev_instance;
1121 if (vec_len (ad->rxqs) > qid && ad->rxqs[qid].int_mode != 0)
1122 vnet_device_input_set_interrupt_pending (vnm, ad->hw_if_index, qid);
1123 for (i = 0; i < vec_len (ad->rxqs); i++)
1124 avf_irq_n_enable (ad, i);
1128 avf_delete_if (vlib_main_t * vm, avf_device_t * ad)
1130 vnet_main_t *vnm = vnet_get_main ();
1131 avf_main_t *am = &avf_main;
1134 if (ad->hw_if_index)
1136 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
1137 vnet_hw_interface_unassign_rx_thread (vnm, ad->hw_if_index, 0);
1138 ethernet_delete_interface (vnm, ad->hw_if_index);
1141 vlib_pci_device_close (vm, ad->pci_dev_handle);
1143 vlib_physmem_free (vm, ad->atq);
1144 vlib_physmem_free (vm, ad->arq);
1145 vlib_physmem_free (vm, ad->atq_bufs);
1146 vlib_physmem_free (vm, ad->arq_bufs);
1149 vec_foreach_index (i, ad->rxqs)
1151 avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
1152 vlib_physmem_free (vm, (void *) rxq->descs);
1153 if (rxq->n_enqueued)
1154 vlib_buffer_free_from_ring (vm, rxq->bufs, rxq->next, rxq->size,
1156 vec_free (rxq->bufs);
1159 vec_free (ad->rxqs);
1162 vec_foreach_index (i, ad->txqs)
1164 avf_txq_t *txq = vec_elt_at_index (ad->txqs, i);
1165 vlib_physmem_free (vm, (void *) txq->descs);
1166 if (txq->n_enqueued)
1168 u16 first = (txq->next - txq->n_enqueued) & (txq->size -1);
1169 vlib_buffer_free_from_ring (vm, txq->bufs, first, txq->size,
1172 vec_free (txq->bufs);
1173 clib_ring_free (txq->rs_slots);
1176 vec_free (ad->txqs);
1177 vec_free (ad->name);
1179 clib_error_free (ad->error);
1180 clib_memset (ad, 0, sizeof (*ad));
1181 pool_put (am->devices, ad);
1185 avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args)
1187 vnet_main_t *vnm = vnet_get_main ();
1188 avf_main_t *am = &avf_main;
1190 vlib_pci_dev_handle_t h;
1191 clib_error_t *error = 0;
1194 /* check input args */
1195 args->rxq_size = (args->rxq_size == 0) ? AVF_RXQ_SZ : args->rxq_size;
1196 args->txq_size = (args->txq_size == 0) ? AVF_TXQ_SZ : args->txq_size;
1198 if ((args->rxq_size & (args->rxq_size - 1))
1199 || (args->txq_size & (args->txq_size - 1)))
1201 args->rv = VNET_API_ERROR_INVALID_VALUE;
1203 clib_error_return (error, "queue size must be a power of two");
1207 pool_get (am->devices, ad);
1208 ad->dev_instance = ad - am->devices;
1209 ad->per_interface_next_index = ~0;
1210 ad->name = vec_dup (args->name);
1212 if (args->enable_elog)
1213 ad->flags |= AVF_DEVICE_F_ELOG;
1215 if ((error = vlib_pci_device_open (vm, &args->addr, avf_pci_device_ids,
1218 pool_put (am->devices, ad);
1219 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1221 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1225 ad->pci_dev_handle = h;
1227 vlib_pci_set_private_data (vm, h, ad->dev_instance);
1229 if ((error = vlib_pci_bus_master_enable (vm, h)))
1232 if ((error = vlib_pci_map_region (vm, h, 0, &ad->bar0)))
1235 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1236 &avf_irq_0_handler)))
1239 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1240 &avf_irq_n_handler)))
1243 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
1246 if (!(ad->atq = vlib_physmem_alloc (vm, sizeof (avf_aq_desc_t) *
1249 error = vlib_physmem_last_error (vm);
1253 if ((error = vlib_pci_map_dma (vm, h, ad->atq)))
1256 if (!(ad->arq = vlib_physmem_alloc (vm, sizeof (avf_aq_desc_t) *
1259 error = vlib_physmem_last_error (vm);
1263 if ((error = vlib_pci_map_dma (vm, h, ad->arq)))
1266 if (!(ad->atq_bufs = vlib_physmem_alloc (vm, AVF_MBOX_BUF_SZ *
1269 error = vlib_physmem_last_error (vm);
1273 if ((error = vlib_pci_map_dma (vm, h, ad->atq_bufs)))
1276 if (!(ad->arq_bufs = vlib_physmem_alloc (vm, AVF_MBOX_BUF_SZ *
1279 error = vlib_physmem_last_error (vm);
1283 if ((error = vlib_pci_map_dma (vm, h, ad->arq_bufs)))
1286 if ((error = vlib_pci_intr_enable (vm, h)))
1289 if (vlib_pci_supports_virtual_addr_dma (vm, h))
1290 ad->flags |= AVF_DEVICE_F_VA_DMA;
1292 if ((error = avf_device_init (vm, am, ad, args)))
1295 /* create interface */
1296 error = ethernet_register_interface (vnm, avf_device_class.index,
1297 ad->dev_instance, ad->hwaddr,
1298 &ad->hw_if_index, avf_flag_change);
1303 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
1304 args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
1306 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, ad->hw_if_index);
1307 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1308 vnet_hw_interface_set_input_node (vnm, ad->hw_if_index,
1309 avf_input_node.index);
1311 for (i = 0; i < ad->n_rx_queues; i++)
1312 vnet_hw_interface_assign_rx_thread (vnm, ad->hw_if_index, i, ~0);
1314 if (pool_elts (am->devices) == 1)
1315 vlib_process_signal_event (vm, avf_process_node.index,
1316 AVF_PROCESS_EVENT_START, 0);
1321 avf_delete_if (vm, ad);
1322 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1323 args->error = clib_error_return (error, "pci-addr %U",
1324 format_vlib_pci_addr, &args->addr);
1325 vlib_log_err (am->log_class, "%U", format_clib_error, args->error);
1328 static clib_error_t *
1329 avf_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1331 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1332 avf_main_t *am = &avf_main;
1333 avf_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
1334 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1336 if (ad->flags & AVF_DEVICE_F_ERROR)
1337 return clib_error_return (0, "device is in error state");
1341 vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
1342 VNET_HW_INTERFACE_FLAG_LINK_UP);
1343 ad->flags |= AVF_DEVICE_F_ADMIN_UP;
1347 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
1348 ad->flags &= ~AVF_DEVICE_F_ADMIN_UP;
1353 static clib_error_t *
1354 avf_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
1355 vnet_hw_interface_rx_mode mode)
1357 avf_main_t *am = &avf_main;
1358 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1359 avf_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
1360 avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
1362 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
1371 avf_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
1374 avf_main_t *am = &avf_main;
1375 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1376 avf_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
1378 /* Shut off redirection */
1379 if (node_index == ~0)
1381 ad->per_interface_next_index = node_index;
1385 ad->per_interface_next_index =
1386 vlib_node_add_next (vlib_get_main (), avf_input_node.index, node_index);
1389 static char *avf_tx_func_error_strings[] = {
1391 foreach_avf_tx_func_error
1396 VNET_DEVICE_CLASS (avf_device_class,) =
1398 .name = "Adaptive Virtual Function (AVF) interface",
1399 .format_device = format_avf_device,
1400 .format_device_name = format_avf_device_name,
1401 .admin_up_down_function = avf_interface_admin_up_down,
1402 .rx_mode_change_function = avf_interface_rx_mode_change,
1403 .rx_redirect_to_node = avf_set_interface_next_node,
1404 .tx_function_n_errors = AVF_TX_N_ERROR,
1405 .tx_function_error_strings = avf_tx_func_error_strings,
1410 avf_init (vlib_main_t * vm)
1412 avf_main_t *am = &avf_main;
1413 clib_error_t *error;
1414 vlib_thread_main_t *tm = vlib_get_thread_main ();
1416 if ((error = vlib_call_init_function (vm, pci_bus_init)))
1419 vec_validate_aligned (am->per_thread_data, tm->n_vlib_mains - 1,
1420 CLIB_CACHE_LINE_BYTES);
1422 am->log_class = vlib_log_register_class ("avf_plugin", 0);
1423 vlib_log_debug (am->log_class, "initialized");
1428 VLIB_INIT_FUNCTION (avf_init);
1431 * fd.io coding-style-patch-verification: ON
1434 * eval: (c-set-style "gnu")