X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fplugins%2Favf%2Fdevice.c;h=8e7bb45a0dbdebe6cc169a9e783c3ca49f74e70b;hb=1839fe165c7ffb834775b8582fe0ee2321ff2ab6;hp=71105e88677e2cb959e3e23d877212447a1a4ab1;hpb=eb1744f1593bf44101bf46b8e3496063a3fb890c;p=vpp.git diff --git a/src/plugins/avf/device.c b/src/plugins/avf/device.c index 71105e88677..8e7bb45a0db 100644 --- a/src/plugins/avf/device.c +++ b/src/plugins/avf/device.c @@ -16,6 +16,7 @@ */ #include +#include #include #include #include @@ -26,7 +27,7 @@ #define AVF_MBOX_BUF_SZ 512 #define AVF_RXQ_SZ 512 #define AVF_TXQ_SZ 512 -#define AVF_ITR_INT 8160 +#define AVF_ITR_INT 32 #define PCI_VENDOR_ID_INTEL 0x8086 #define PCI_DEVICE_ID_INTEL_AVF 0x1889 @@ -42,6 +43,18 @@ static pci_device_id_t avf_pci_device_ids[] = { {0}, }; +const static char *virtchnl_event_names[] = { +#define _(v, n) [v] = #n, + foreach_virtchnl_event_code +#undef _ +}; + +const static char *virtchnl_link_speed_str[] = { +#define _(v, n, s) [v] = s, + foreach_virtchnl_link_speed +#undef _ +}; + static inline void avf_irq_0_disable (avf_device_t * ad) { @@ -100,13 +113,12 @@ clib_error_t * avf_aq_desc_enq (vlib_main_t * vm, avf_device_t * ad, avf_aq_desc_t * dt, void *data, int len) { - avf_main_t *am = &avf_main; clib_error_t *err = 0; avf_aq_desc_t *d, dc; - int n_retry = 5; + f64 t0, suspend_time = AVF_AQ_ENQ_SUSPEND_TIME; d = &ad->atq[ad->atq_next_slot]; - clib_memcpy (d, dt, sizeof (avf_aq_desc_t)); + clib_memcpy_fast (d, dt, sizeof (avf_aq_desc_t)); d->flags |= AVF_AQ_F_RD | AVF_AQ_F_SI; if (len) d->datalen = len; @@ -116,35 +128,38 @@ avf_aq_desc_enq (vlib_main_t * vm, avf_device_t * ad, avf_aq_desc_t * dt, pa = ad->atq_bufs_pa + ad->atq_next_slot * AVF_MBOX_BUF_SZ; d->addr_hi = (u32) (pa >> 32); d->addr_lo = (u32) pa; - clib_memcpy (ad->atq_bufs + ad->atq_next_slot * AVF_MBOX_BUF_SZ, data, - len); + clib_memcpy_fast (ad->atq_bufs + ad->atq_next_slot * AVF_MBOX_BUF_SZ, + data, len); d->flags |= AVF_AQ_F_BUF; } if (ad->flags & AVF_DEVICE_F_ELOG) - clib_memcpy (&dc, d, sizeof (avf_aq_desc_t)); + clib_memcpy_fast (&dc, d, sizeof (avf_aq_desc_t)); CLIB_MEMORY_BARRIER (); - vlib_log_debug (am->log_class, "%U", format_hexdump, data, len); ad->atq_next_slot = (ad->atq_next_slot + 1) % AVF_MBOX_LEN; avf_reg_write (ad, AVF_ATQT, ad->atq_next_slot); avf_reg_flush (ad); + t0 = vlib_time_now (vm); retry: - vlib_process_suspend (vm, 10e-6); + vlib_process_suspend (vm, suspend_time); if (((d->flags & AVF_AQ_F_DD) == 0) || ((d->flags & AVF_AQ_F_CMP) == 0)) { - if (--n_retry == 0) + f64 t = vlib_time_now (vm) - t0; + if (t > AVF_AQ_ENQ_MAX_WAIT_TIME) { + avf_log_err (ad, "aq_desc_enq failed (timeout %.3fs)", t); err = clib_error_return (0, "adminq enqueue timeout [opcode 0x%x]", d->opcode); goto done; } + suspend_time *= 2; goto retry; } - clib_memcpy (dt, d, sizeof (avf_aq_desc_t)); + clib_memcpy_fast (dt, d, sizeof (avf_aq_desc_t)); if (d->flags & AVF_AQ_F_ERR) return clib_error_return (0, "adminq enqueue error [opcode 0x%x, retval " "%d]", d->opcode, d->retval); @@ -215,23 +230,34 @@ avf_cmd_rx_ctl_reg_write (vlib_main_t * vm, avf_device_t * ad, u32 reg, clib_error_t * avf_rxq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 rxq_size) { - avf_main_t *am = &avf_main; + clib_error_t *err; avf_rxq_t *rxq; - clib_error_t *error = 0; u32 n_alloc, i; vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES); rxq = vec_elt_at_index (ad->rxqs, qid); rxq->size = rxq_size; rxq->next = 0; - rxq->descs = vlib_physmem_alloc_aligned (vm, am->physmem_region, &error, - rxq->size * sizeof (avf_rx_desc_t), - 2 * CLIB_CACHE_LINE_BYTES); - memset ((void *) rxq->descs, 0, rxq->size * sizeof (avf_rx_desc_t)); + rxq->descs = vlib_physmem_alloc_aligned_on_numa (vm, rxq->size * + sizeof (avf_rx_desc_t), + 2 * CLIB_CACHE_LINE_BYTES, + ad->numa_node); + + rxq->buffer_pool_index = + vlib_buffer_pool_get_default_for_numa (vm, ad->numa_node); + + if (rxq->descs == 0) + return vlib_physmem_last_error (vm); + + if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) rxq->descs))) + return err; + + clib_memset ((void *) rxq->descs, 0, rxq->size * sizeof (avf_rx_desc_t)); vec_validate_aligned (rxq->bufs, rxq->size, CLIB_CACHE_LINE_BYTES); rxq->qrx_tail = ad->bar0 + AVF_QRX_TAIL (qid); - n_alloc = vlib_buffer_alloc (vm, rxq->bufs, rxq->size - 8); + n_alloc = vlib_buffer_alloc_from_pool (vm, rxq->bufs, rxq->size - 8, + rxq->buffer_pool_index); if (n_alloc == 0) return clib_error_return (0, "buffer allocation error"); @@ -240,14 +266,11 @@ avf_rxq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 rxq_size) avf_rx_desc_t *d = rxq->descs; for (i = 0; i < n_alloc; i++) { - if (ad->flags & AVF_DEVICE_F_IOVA) - { - vlib_buffer_t *b = vlib_get_buffer (vm, rxq->bufs[i]); - d->qword[0] = pointer_to_uword (b->data); - } + vlib_buffer_t *b = vlib_get_buffer (vm, rxq->bufs[i]); + if (ad->flags & AVF_DEVICE_F_VA_DMA) + d->qword[0] = vlib_buffer_get_va (b); else - d->qword[0] = - vlib_get_buffer_data_physical_address (vm, rxq->bufs[i]); + d->qword[0] = vlib_buffer_get_pa (vm, b); d++; } @@ -258,9 +281,8 @@ avf_rxq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 rxq_size) clib_error_t * avf_txq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 txq_size) { - avf_main_t *am = &avf_main; + clib_error_t *err; avf_txq_t *txq; - clib_error_t *error = 0; if (qid >= ad->num_queue_pairs) { @@ -276,12 +298,22 @@ avf_txq_init (vlib_main_t * vm, avf_device_t * ad, u16 qid, u16 txq_size) txq = vec_elt_at_index (ad->txqs, qid); txq->size = txq_size; txq->next = 0; - txq->descs = vlib_physmem_alloc_aligned (vm, am->physmem_region, &error, - txq->size * sizeof (avf_tx_desc_t), - 2 * CLIB_CACHE_LINE_BYTES); + txq->descs = vlib_physmem_alloc_aligned_on_numa (vm, txq->size * + sizeof (avf_tx_desc_t), + 2 * CLIB_CACHE_LINE_BYTES, + ad->numa_node); + if (txq->descs == 0) + return vlib_physmem_last_error (vm); + + if ((err = vlib_pci_map_dma (vm, ad->pci_dev_handle, (void *) txq->descs))) + return err; + vec_validate_aligned (txq->bufs, txq->size, CLIB_CACHE_LINE_BYTES); txq->qtx_tail = ad->bar0 + AVF_QTX_TAIL (qid); + /* initialize ring of pending RS slots */ + clib_ring_new_aligned (txq->rs_slots, 32, CLIB_CACHE_LINE_BYTES); + ad->n_tx_queues = clib_min (ad->num_queue_pairs, qid + 1); return 0; } @@ -298,7 +330,7 @@ avf_arq_slot_init (avf_device_t * ad, u16 slot) avf_aq_desc_t *d; u64 pa = ad->arq_bufs_pa + slot * AVF_MBOX_BUF_SZ; d = &ad->arq[slot]; - memset (d, 0, sizeof (avf_aq_desc_t)); + clib_memset (d, 0, sizeof (avf_aq_desc_t)); d->flags = AVF_AQ_F_BUF; d->datalen = AVF_MBOX_BUF_SZ; d->addr_hi = (u32) (pa >> 32); @@ -308,10 +340,8 @@ avf_arq_slot_init (avf_device_t * ad, u16 slot) static inline uword avf_dma_addr (vlib_main_t * vm, avf_device_t * ad, void *p) { - avf_main_t *am = &avf_main; - return (ad->flags & AVF_DEVICE_F_IOVA) ? - pointer_to_uword (p) : - vlib_physmem_virtual_to_physical (vm, am->physmem_region, p); + return (ad->flags & AVF_DEVICE_F_VA_DMA) ? + pointer_to_uword (p) : vlib_physmem_get_pa (vm, p); } static void @@ -321,7 +351,7 @@ avf_adminq_init (vlib_main_t * vm, avf_device_t * ad) int i; /* VF MailBox Transmit */ - memset (ad->atq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN); + clib_memset (ad->atq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN); ad->atq_bufs_pa = avf_dma_addr (vm, ad, ad->atq_bufs); pa = avf_dma_addr (vm, ad, ad->atq); @@ -332,7 +362,7 @@ avf_adminq_init (vlib_main_t * vm, avf_device_t * ad) avf_reg_write (ad, AVF_ATQBAH, (u32) (pa >> 32)); /* Base Address High */ /* VF MailBox Receive */ - memset (ad->arq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN); + clib_memset (ad->arq, 0, sizeof (avf_aq_desc_t) * AVF_MBOX_LEN); ad->arq_bufs_pa = avf_dma_addr (vm, ad, ad->arq_bufs); for (i = 0; i < AVF_MBOX_LEN; i++) @@ -358,10 +388,9 @@ avf_send_to_pf (vlib_main_t * vm, avf_device_t * ad, virtchnl_ops_t op, clib_error_t *err; avf_aq_desc_t *d, dt = {.opcode = 0x801,.v_opcode = op }; u32 head; - int n_retry = 5; - + f64 t0, suspend_time = AVF_SEND_TO_PF_SUSPEND_TIME; - /* supppres interrupt in the next adminq receive slot + /* suppress interrupt in the next adminq receive slot as we are going to wait for response we only need interrupts when event is received */ d = &ad->arq[ad->arq_next_slot]; @@ -370,14 +399,20 @@ avf_send_to_pf (vlib_main_t * vm, avf_device_t * ad, virtchnl_ops_t op, if ((err = avf_aq_desc_enq (vm, ad, &dt, in, in_len))) return err; + t0 = vlib_time_now (vm); retry: head = avf_get_u32 (ad->bar0, AVF_ARQH); if (ad->arq_next_slot == head) { - if (--n_retry == 0) - return clib_error_return (0, "timeout"); - vlib_process_suspend (vm, 10e-3); + f64 t = vlib_time_now (vm) - t0; + if (t > AVF_SEND_TO_PF_MAX_WAIT_TIME) + { + avf_log_err (ad, "send_to_pf failed (timeout %.3fs)", t); + return clib_error_return (0, "timeout"); + } + vlib_process_suspend (vm, suspend_time); + suspend_time *= 2; goto retry; } @@ -393,10 +428,12 @@ retry: return clib_error_return (0, "event message error"); vec_add2 (ad->events, e, 1); - clib_memcpy (e, buf, sizeof (virtchnl_pf_event_t)); + clib_memcpy_fast (e, buf, sizeof (virtchnl_pf_event_t)); avf_arq_slot_init (ad, ad->arq_next_slot); ad->arq_next_slot++; - n_retry = 5; + /* reset timer */ + t0 = vlib_time_now (vm); + suspend_time = AVF_SEND_TO_PF_SUSPEND_TIME; goto retry; } @@ -420,7 +457,7 @@ retry: if (d->flags & AVF_AQ_F_BUF) { void *buf = ad->arq_bufs + ad->arq_next_slot * AVF_MBOX_BUF_SZ; - clib_memcpy (out, buf, out_len); + clib_memcpy_fast (out, buf, out_len); } avf_arq_slot_init (ad, ad->arq_next_slot); @@ -471,6 +508,8 @@ avf_op_version (vlib_main_t * vm, avf_device_t * ad, .minor = VIRTCHNL_VERSION_MINOR, }; + avf_log_debug (ad, "version: major %u minor %u", myver.major, myver.minor); + err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_VERSION, &myver, sizeof (virtchnl_version_info_t), ver, sizeof (virtchnl_version_info_t)); @@ -485,12 +524,37 @@ clib_error_t * avf_op_get_vf_resources (vlib_main_t * vm, avf_device_t * ad, virtchnl_vf_resource_t * res) { + clib_error_t *err = 0; u32 bitmap = (VIRTCHNL_VF_OFFLOAD_L2 | VIRTCHNL_VF_OFFLOAD_RSS_PF | VIRTCHNL_VF_OFFLOAD_WB_ON_ITR | VIRTCHNL_VF_OFFLOAD_VLAN | VIRTCHNL_VF_OFFLOAD_RX_POLLING); - return avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_VF_RESOURCES, &bitmap, - sizeof (u32), res, sizeof (virtchnl_vf_resource_t)); + avf_log_debug (ad, "get_vf_reqources: bitmap 0x%x", bitmap); + err = avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_VF_RESOURCES, &bitmap, + sizeof (u32), res, sizeof (virtchnl_vf_resource_t)); + + if (err == 0) + { + int i; + avf_log_debug (ad, "get_vf_reqources: num_vsis %u num_queue_pairs %u " + "max_vectors %u max_mtu %u vf_offload_flags 0x%04x " + "rss_key_size %u rss_lut_size %u", + res->num_vsis, res->num_queue_pairs, res->max_vectors, + res->max_mtu, res->vf_offload_flags, res->rss_key_size, + res->rss_lut_size); + for (i = 0; i < res->num_vsis; i++) + avf_log_debug (ad, "get_vf_reqources_vsi[%u]: vsi_id %u " + "num_queue_pairs %u vsi_type %u qset_handle %u " + "default_mac_addr %U", i, + res->vsi_res[i].vsi_id, + res->vsi_res[i].num_queue_pairs, + res->vsi_res[i].vsi_type, + res->vsi_res[i].qset_handle, + format_ethernet_address, + res->vsi_res[i].default_mac_addr); + } + + return err; } clib_error_t * @@ -501,13 +565,17 @@ avf_op_config_rss_lut (vlib_main_t * vm, avf_device_t * ad) u8 msg[msg_len]; virtchnl_rss_lut_t *rl; - memset (msg, 0, msg_len); + clib_memset (msg, 0, msg_len); rl = (virtchnl_rss_lut_t *) msg; rl->vsi_id = ad->vsi_id; rl->lut_entries = ad->rss_lut_size; for (i = 0; i < ad->rss_lut_size; i++) rl->lut[i] = i % ad->n_rx_queues; + avf_log_debug (ad, "config_rss_lut: vsi_id %u rss_lut_size %u lut 0x%U", + rl->vsi_id, rl->lut_entries, format_hex_bytes_no_wrap, + rl->lut, rl->lut_entries); + return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_LUT, msg, msg_len, 0, 0); } @@ -520,7 +588,7 @@ avf_op_config_rss_key (vlib_main_t * vm, avf_device_t * ad) u8 msg[msg_len]; virtchnl_rss_key_t *rk; - memset (msg, 0, msg_len); + clib_memset (msg, 0, msg_len); rk = (virtchnl_rss_key_t *) msg; rk->vsi_id = ad->vsi_id; rk->key_len = ad->rss_key_size; @@ -528,6 +596,10 @@ avf_op_config_rss_key (vlib_main_t * vm, avf_device_t * ad) for (i = 0; i < ad->rss_key_size; i++) rk->key[i] = (u8) random_u32 (&seed); + avf_log_debug (ad, "config_rss_key: vsi_id %u rss_key_size %u key 0x%U", + rk->vsi_id, rk->key_len, format_hex_bytes_no_wrap, rk->key, + rk->key_len); + return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_RSS_KEY, msg, msg_len, 0, 0); } @@ -535,17 +607,26 @@ avf_op_config_rss_key (vlib_main_t * vm, avf_device_t * ad) clib_error_t * avf_op_disable_vlan_stripping (vlib_main_t * vm, avf_device_t * ad) { + avf_log_debug (ad, "disable_vlan_stripping"); + return avf_send_to_pf (vm, ad, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, 0, 0, 0, 0); } clib_error_t * -avf_config_promisc_mode (vlib_main_t * vm, avf_device_t * ad) +avf_config_promisc_mode (vlib_main_t * vm, avf_device_t * ad, int is_enable) { virtchnl_promisc_info_t pi = { 0 }; pi.vsi_id = ad->vsi_id; - pi.flags = 1; + + if (is_enable) + pi.flags = FLAG_VF_UNICAST_PROMISC | FLAG_VF_MULTICAST_PROMISC; + + avf_log_debug (ad, "config_promisc_mode: unicast %s multicast %s", + pi.flags & FLAG_VF_UNICAST_PROMISC ? "on" : "off", + pi.flags & FLAG_VF_MULTICAST_PROMISC ? "on" : "off"); + return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE, &pi, sizeof (virtchnl_promisc_info_t), 0, 0); } @@ -561,11 +642,14 @@ avf_op_config_vsi_queues (vlib_main_t * vm, avf_device_t * ad) u8 msg[msg_len]; virtchnl_vsi_queue_config_info_t *ci; - memset (msg, 0, msg_len); + clib_memset (msg, 0, msg_len); ci = (virtchnl_vsi_queue_config_info_t *) msg; ci->vsi_id = ad->vsi_id; ci->num_queue_pairs = n_qp; + avf_log_debug (ad, "config_vsi_queues: vsi_id %u num_queue_pairs %u", + ad->vsi_id, ci->num_queue_pairs); + for (i = 0; i < n_qp; i++) { virtchnl_txq_info_t *txq = &ci->qpair[i].txq; @@ -573,24 +657,31 @@ avf_op_config_vsi_queues (vlib_main_t * vm, avf_device_t * ad) rxq->vsi_id = ad->vsi_id; rxq->queue_id = i; - rxq->max_pkt_size = 1518; + rxq->max_pkt_size = ETHERNET_MAX_PACKET_BYTES; if (i < vec_len (ad->rxqs)) { avf_rxq_t *q = vec_elt_at_index (ad->rxqs, i); rxq->ring_len = q->size; - rxq->databuffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES; + rxq->databuffer_size = vlib_buffer_get_default_data_size (vm); rxq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs); avf_reg_write (ad, AVF_QRX_TAIL (i), q->size - 1); } + avf_log_debug (ad, "config_vsi_queues_rx[%u]: max_pkt_size %u " + "ring_len %u databuffer_size %u dma_ring_addr 0x%llx", + i, rxq->max_pkt_size, rxq->ring_len, + rxq->databuffer_size, rxq->dma_ring_addr); - avf_txq_t *q = vec_elt_at_index (ad->txqs, i); txq->vsi_id = ad->vsi_id; + txq->queue_id = i; if (i < vec_len (ad->txqs)) { - txq->queue_id = i; + avf_txq_t *q = vec_elt_at_index (ad->txqs, i); txq->ring_len = q->size; txq->dma_ring_addr = avf_dma_addr (vm, ad, (void *) q->descs); } + avf_log_debug (ad, "config_vsi_queues_tx[%u]: ring_len %u " + "dma_ring_addr 0x%llx", i, txq->ring_len, + txq->dma_ring_addr); } return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_VSI_QUEUES, msg, msg_len, @@ -606,13 +697,19 @@ avf_op_config_irq_map (vlib_main_t * vm, avf_device_t * ad) u8 msg[msg_len]; virtchnl_irq_map_info_t *imi; - memset (msg, 0, msg_len); + clib_memset (msg, 0, msg_len); imi = (virtchnl_irq_map_info_t *) msg; imi->num_vectors = count; imi->vecmap[0].vector_id = 1; imi->vecmap[0].vsi_id = ad->vsi_id; - imi->vecmap[0].rxq_map = 1; + imi->vecmap[0].rxq_map = (1 << ad->n_rx_queues) - 1; + imi->vecmap[0].txq_map = (1 << ad->n_tx_queues) - 1; + + avf_log_debug (ad, "config_irq_map: vsi_id %u vector_id %u rxq_map %u", + ad->vsi_id, imi->vecmap[0].vector_id, + imi->vecmap[0].rxq_map); + return avf_send_to_pf (vm, ad, VIRTCHNL_OP_CONFIG_IRQ_MAP, msg, msg_len, 0, 0); } @@ -627,12 +724,20 @@ avf_op_add_eth_addr (vlib_main_t * vm, avf_device_t * ad, u8 count, u8 * macs) virtchnl_ether_addr_list_t *al; int i; - memset (msg, 0, msg_len); + clib_memset (msg, 0, msg_len); al = (virtchnl_ether_addr_list_t *) msg; al->vsi_id = ad->vsi_id; al->num_elements = count; + + avf_log_debug (ad, "add_eth_addr: vsi_id %u num_elements %u", + ad->vsi_id, al->num_elements); + for (i = 0; i < count; i++) - clib_memcpy (&al->list[i].addr, macs + i * 6, 6); + { + clib_memcpy_fast (&al->list[i].addr, macs + i * 6, 6); + avf_log_debug (ad, "add_eth_addr[%u]: %U", i, + format_ethernet_address, &al->list[i].addr); + } return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ADD_ETH_ADDR, msg, msg_len, 0, 0); } @@ -641,14 +746,23 @@ clib_error_t * avf_op_enable_queues (vlib_main_t * vm, avf_device_t * ad, u32 rx, u32 tx) { virtchnl_queue_select_t qs = { 0 }; - int i; + int i = 0; qs.vsi_id = ad->vsi_id; qs.rx_queues = rx; qs.tx_queues = tx; - for (i = 0; i < ad->n_rx_queues; i++) + + avf_log_debug (ad, "enable_queues: vsi_id %u rx_queues %u tx_queues %u", + ad->vsi_id, qs.rx_queues, qs.tx_queues); + + while (rx) { - avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i); - avf_reg_write (ad, AVF_QRX_TAIL (i), rxq->n_enqueued); + if (rx & (1 << i)) + { + avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i); + avf_reg_write (ad, AVF_QRX_TAIL (i), rxq->n_enqueued); + rx &= ~(1 << i); + } + i++; } return avf_send_to_pf (vm, ad, VIRTCHNL_OP_ENABLE_QUEUES, &qs, sizeof (virtchnl_queue_select_t), 0, 0); @@ -660,6 +774,9 @@ avf_op_get_stats (vlib_main_t * vm, avf_device_t * ad, { virtchnl_queue_select_t qs = { 0 }; qs.vsi_id = ad->vsi_id; + + avf_log_debug (ad, "get_stats: vsi_id %u", ad->vsi_id); + return avf_send_to_pf (vm, ad, VIRTCHNL_OP_GET_STATS, &qs, sizeof (virtchnl_queue_select_t), es, sizeof (virtchnl_eth_stats_t)); @@ -671,23 +788,35 @@ avf_device_reset (vlib_main_t * vm, avf_device_t * ad) avf_aq_desc_t d = { 0 }; clib_error_t *error; u32 rstat; - int n_retry = 20; + f64 t0, t = 0, suspend_time = AVF_RESET_SUSPEND_TIME; + + avf_log_debug (ad, "reset"); d.opcode = 0x801; d.v_opcode = VIRTCHNL_OP_RESET_VF; if ((error = avf_aq_desc_enq (vm, ad, &d, 0, 0))) return error; + t0 = vlib_time_now (vm); retry: - vlib_process_suspend (vm, 10e-3); + vlib_process_suspend (vm, suspend_time); + rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT); if (rstat == 2 || rstat == 3) - return 0; + { + avf_log_debug (ad, "reset completed in %.3fs", t); + return 0; + } - if (--n_retry == 0) - return clib_error_return (0, "reset failed (timeout)"); + t = vlib_time_now (vm) - t0; + if (t > AVF_RESET_MAX_WAIT_TIME) + { + avf_log_err (ad, "reset failed (timeout %.3fs)", t); + return clib_error_return (0, "reset failed (timeout)"); + } + suspend_time *= 2; goto retry; } @@ -697,16 +826,18 @@ avf_request_queues (vlib_main_t * vm, avf_device_t * ad, u16 num_queue_pairs) virtchnl_vf_res_request_t res_req = { 0 }; clib_error_t *error; u32 rstat; - int n_retry = 20; + f64 t0, t, suspend_time = AVF_RESET_SUSPEND_TIME; res_req.num_queue_pairs = num_queue_pairs; + avf_log_debug (ad, "request_queues: num_queue_pairs %u", num_queue_pairs); + error = avf_send_to_pf (vm, ad, VIRTCHNL_OP_REQUEST_QUEUES, &res_req, sizeof (virtchnl_vf_res_request_t), &res_req, sizeof (virtchnl_vf_res_request_t)); /* - * if PF respondes, the request failed + * if PF responds, the request failed * else PF initializes restart and avf_send_to_pf returns an error */ if (!error) @@ -715,16 +846,23 @@ avf_request_queues (vlib_main_t * vm, avf_device_t * ad, u16 num_queue_pairs) res_req.num_queue_pairs); } + t0 = vlib_time_now (vm); retry: - vlib_process_suspend (vm, 10e-3); + vlib_process_suspend (vm, suspend_time); + t = vlib_time_now (vm) - t0; + rstat = avf_get_u32 (ad->bar0, AVFGEN_RSTAT); if ((rstat == VIRTCHNL_VFR_COMPLETED) || (rstat == VIRTCHNL_VFR_VFACTIVE)) goto done; - if (--n_retry == 0) - return clib_error_return (0, "reset failed (timeout)"); + if (t > AVF_RESET_MAX_WAIT_TIME) + { + avf_log_err (ad, "request queues failed (timeout %.3f seconds)", t); + return clib_error_return (0, "request queues failed (timeout)"); + } + suspend_time *= 2; goto retry; done: @@ -743,8 +881,8 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad, avf_adminq_init (vm, ad); - /* request more queues only if we need them */ - if ((error = avf_request_queues (vm, ad, tm->n_vlib_mains))) + if ((error = avf_request_queues (vm, ad, clib_max (tm->n_vlib_mains, + args->rxq_num)))) { /* we failed to get more queues, but still we want to proceed */ clib_error_free (error); @@ -767,7 +905,7 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad, "(remote %d.%d)", ver.major, ver.minor); /* - * OP_GET_VF_RESOUCES + * OP_GET_VF_RESOURCES */ if ((error = avf_op_get_vf_resources (vm, ad, &res))) return error; @@ -783,7 +921,7 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad, ad->rss_key_size = res.rss_key_size; ad->rss_lut_size = res.rss_lut_size; - clib_memcpy (ad->hwaddr, res.vsi_res[0].default_mac_addr, 6); + clib_memcpy_fast (ad->hwaddr, res.vsi_res[0].default_mac_addr, 6); /* * Disable VLAN stripping @@ -791,9 +929,6 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad, if ((error = avf_op_disable_vlan_stripping (vm, ad))) return error; - if ((error = avf_config_promisc_mode (vm, ad))) - return error; - /* * Init Queues */ @@ -804,9 +939,8 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad, else if (args->rxq_num > ad->num_queue_pairs) { args->rxq_num = ad->num_queue_pairs; - vlib_log_warn (am->log_class, "Requested more rx queues than" - "queue pairs available. Using %u rx queues.", - args->rxq_num); + avf_log_warn (ad, "Requested more rx queues than queue pairs available." + "Using %u rx queues.", args->rxq_num); } for (i = 0; i < args->rxq_num; i++) @@ -838,10 +972,8 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad, if ((error = avf_op_add_eth_addr (vm, ad, 1, ad->hwaddr))) return error; - if ((error = avf_op_enable_queues (vm, ad, ad->n_rx_queues, 0))) - return error; - - if ((error = avf_op_enable_queues (vm, ad, 0, ad->n_tx_queues))) + if ((error = avf_op_enable_queues (vm, ad, pow2_mask (ad->n_rx_queues), + pow2_mask (ad->n_tx_queues)))) return error; ad->flags |= AVF_DEVICE_F_INITIALIZED; @@ -873,6 +1005,7 @@ avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq) if ((r & 0xf0000000) != (1ULL << 31)) { ad->error = clib_error_return (0, "arq not enabled, arqlen = 0x%x", r); + avf_log_err (ad, "error: %U", format_clib_error, ad->error); goto error; } @@ -880,6 +1013,7 @@ avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq) if ((r & 0xf0000000) != (1ULL << 31)) { ad->error = clib_error_return (0, "atq not enabled, atqlen = 0x%x", r); + avf_log_err (ad, "error: %U", format_clib_error, ad->error); goto error; } @@ -889,11 +1023,19 @@ avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq) /* *INDENT-OFF* */ vec_foreach (e, ad->events) { + avf_log_debug (ad, "event: %s (%u) sev %d", + virtchnl_event_names[e->event], e->event, e->severity); if (e->event == VIRTCHNL_EVENT_LINK_CHANGE) { int link_up = e->event_data.link_event.link_status; virtchnl_link_speed_t speed = e->event_data.link_event.link_speed; u32 flags = 0; + u32 kbps = 0; + + avf_log_debug (ad, "event_link_change: status %d speed '%s' (%d)", + link_up, + speed < ARRAY_LEN (virtchnl_link_speed_str) ? + virtchnl_link_speed_str[speed] : "unknown", speed); if (link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) == 0) { @@ -901,16 +1043,21 @@ avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq) flags |= (VNET_HW_INTERFACE_FLAG_FULL_DUPLEX | VNET_HW_INTERFACE_FLAG_LINK_UP); if (speed == VIRTCHNL_LINK_SPEED_40GB) - flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G; + kbps = 40000000; else if (speed == VIRTCHNL_LINK_SPEED_25GB) - flags |= VNET_HW_INTERFACE_FLAG_SPEED_25G; + kbps = 25000000; else if (speed == VIRTCHNL_LINK_SPEED_10GB) - flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G; + kbps = 10000000; + else if (speed == VIRTCHNL_LINK_SPEED_5GB) + kbps = 5000000; + else if (speed == VIRTCHNL_LINK_SPEED_2_5GB) + kbps = 2500000; else if (speed == VIRTCHNL_LINK_SPEED_1GB) - flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G; + kbps = 1000000; else if (speed == VIRTCHNL_LINK_SPEED_100MB) - flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M; + kbps = 100000; vnet_hw_interface_set_flags (vnm, ad->hw_if_index, flags); + vnet_hw_interface_set_link_speed (vnm, ad->hw_if_index, kbps); ad->link_speed = speed; } else if (!link_up && (ad->flags & AVF_DEVICE_F_LINK_UP) != 0) @@ -975,8 +1122,28 @@ error: static u32 avf_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags) { + vlib_main_t *vm = vlib_get_main (); avf_main_t *am = &avf_main; - vlib_log_warn (am->log_class, "TODO"); + avf_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance); + if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags)) + { + clib_error_t *error; + int promisc_enabled = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0; + u32 new_flags = promisc_enabled ? + ad->flags | AVF_DEVICE_F_PROMISC : ad->flags & ~AVF_DEVICE_F_PROMISC; + + if (new_flags == ad->flags) + return flags; + + if ((error = avf_config_promisc_mode (vm, ad, promisc_enabled))) + { + avf_log_err (ad, "%s: %U", format_clib_error, error); + clib_error_free (error); + return 0; + } + + ad->flags = new_flags; + } return 0; } @@ -1039,11 +1206,10 @@ VLIB_REGISTER_NODE (avf_process_node, static) = { /* *INDENT-ON* */ static void -avf_irq_0_handler (vlib_pci_dev_handle_t h, u16 line) +avf_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line) { - vlib_main_t *vm = vlib_get_main (); avf_main_t *am = &avf_main; - uword pd = vlib_pci_get_private_data (h); + uword pd = vlib_pci_get_private_data (vm, h); avf_device_t *ad = pool_elt_at_index (am->devices, pd); u32 icr0; @@ -1078,12 +1244,11 @@ avf_irq_0_handler (vlib_pci_dev_handle_t h, u16 line) } static void -avf_irq_n_handler (vlib_pci_dev_handle_t h, u16 line) +avf_irq_n_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line) { vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); avf_main_t *am = &avf_main; - uword pd = vlib_pci_get_private_data (h); + uword pd = vlib_pci_get_private_data (vm, h); avf_device_t *ad = pool_elt_at_index (am->devices, pd); u16 qid; int i; @@ -1129,18 +1294,18 @@ avf_delete_if (vlib_main_t * vm, avf_device_t * ad) ethernet_delete_interface (vnm, ad->hw_if_index); } - vlib_pci_device_close (ad->pci_dev_handle); + vlib_pci_device_close (vm, ad->pci_dev_handle); - vlib_physmem_free (vm, am->physmem_region, ad->atq); - vlib_physmem_free (vm, am->physmem_region, ad->arq); - vlib_physmem_free (vm, am->physmem_region, ad->atq_bufs); - vlib_physmem_free (vm, am->physmem_region, ad->arq_bufs); + vlib_physmem_free (vm, ad->atq); + vlib_physmem_free (vm, ad->arq); + vlib_physmem_free (vm, ad->atq_bufs); + vlib_physmem_free (vm, ad->arq_bufs); /* *INDENT-OFF* */ vec_foreach_index (i, ad->rxqs) { avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i); - vlib_physmem_free (vm, am->physmem_region, (void *) rxq->descs); + vlib_physmem_free (vm, (void *) rxq->descs); if (rxq->n_enqueued) vlib_buffer_free_from_ring (vm, rxq->bufs, rxq->next, rxq->size, rxq->n_enqueued); @@ -1153,7 +1318,7 @@ avf_delete_if (vlib_main_t * vm, avf_device_t * ad) vec_foreach_index (i, ad->txqs) { avf_txq_t *txq = vec_elt_at_index (ad->txqs, i); - vlib_physmem_free (vm, am->physmem_region, (void *) txq->descs); + vlib_physmem_free (vm, (void *) txq->descs); if (txq->n_enqueued) { u16 first = (txq->next - txq->n_enqueued) & (txq->size -1); @@ -1161,12 +1326,14 @@ avf_delete_if (vlib_main_t * vm, avf_device_t * ad) txq->n_enqueued); } vec_free (txq->bufs); + clib_ring_free (txq->rs_slots); } /* *INDENT-ON* */ vec_free (ad->txqs); + vec_free (ad->name); clib_error_free (ad->error); - memset (ad, 0, sizeof (*ad)); + clib_memset (ad, 0, sizeof (*ad)); pool_put (am->devices, ad); } @@ -1196,11 +1363,13 @@ avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args) pool_get (am->devices, ad); ad->dev_instance = ad - am->devices; ad->per_interface_next_index = ~0; + ad->name = vec_dup (args->name); if (args->enable_elog) ad->flags |= AVF_DEVICE_F_ELOG; - if ((error = vlib_pci_device_open (&args->addr, avf_pci_device_ids, &h))) + if ((error = vlib_pci_device_open (vm, &args->addr, avf_pci_device_ids, + &h))) { pool_put (am->devices, ad); args->rv = VNET_API_ERROR_INVALID_INTERFACE; @@ -1210,62 +1379,85 @@ avf_create_if (vlib_main_t * vm, avf_create_if_args_t * args) return; } ad->pci_dev_handle = h; + ad->pci_addr = args->addr; + ad->numa_node = vlib_pci_get_numa_node (vm, h); - vlib_pci_set_private_data (h, ad->dev_instance); + vlib_pci_set_private_data (vm, h, ad->dev_instance); - if ((error = vlib_pci_bus_master_enable (h))) + if ((error = vlib_pci_bus_master_enable (vm, h))) goto error; - if ((error = vlib_pci_map_region (h, 0, &ad->bar0))) + if ((error = vlib_pci_map_region (vm, h, 0, &ad->bar0))) goto error; - if ((error = vlib_pci_register_msix_handler (h, 0, 1, &avf_irq_0_handler))) + if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, + &avf_irq_0_handler))) goto error; - if ((error = vlib_pci_register_msix_handler (h, 1, 1, &avf_irq_n_handler))) + if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1, + &avf_irq_n_handler))) goto error; - if ((error = vlib_pci_enable_msix_irq (h, 0, 2))) + if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2))) goto error; - if (am->physmem_region_alloc == 0) + ad->atq = vlib_physmem_alloc_aligned_on_numa (vm, sizeof (avf_aq_desc_t) * + AVF_MBOX_LEN, + CLIB_CACHE_LINE_BYTES, + ad->numa_node); + if (ad->atq == 0) { - u32 flags = VLIB_PHYSMEM_F_INIT_MHEAP | VLIB_PHYSMEM_F_HUGETLB; - error = vlib_physmem_region_alloc (vm, "avf descriptors", 4 << 20, 0, - flags, &am->physmem_region); - if (error) - goto error; - am->physmem_region_alloc = 1; + error = vlib_physmem_last_error (vm); + goto error; } - ad->atq = vlib_physmem_alloc_aligned (vm, am->physmem_region, &error, - sizeof (avf_aq_desc_t) * AVF_MBOX_LEN, - 64); - if (error) + + if ((error = vlib_pci_map_dma (vm, h, ad->atq))) goto error; - ad->arq = vlib_physmem_alloc_aligned (vm, am->physmem_region, &error, - sizeof (avf_aq_desc_t) * AVF_MBOX_LEN, - 64); - if (error) + ad->arq = vlib_physmem_alloc_aligned_on_numa (vm, sizeof (avf_aq_desc_t) * + AVF_MBOX_LEN, + CLIB_CACHE_LINE_BYTES, + ad->numa_node); + if (ad->arq == 0) + { + error = vlib_physmem_last_error (vm); + goto error; + } + + if ((error = vlib_pci_map_dma (vm, h, ad->arq))) goto error; - ad->atq_bufs = vlib_physmem_alloc_aligned (vm, am->physmem_region, &error, - AVF_MBOX_BUF_SZ * AVF_MBOX_LEN, - 64); - if (error) + ad->atq_bufs = vlib_physmem_alloc_aligned_on_numa (vm, AVF_MBOX_BUF_SZ * + AVF_MBOX_LEN, + CLIB_CACHE_LINE_BYTES, + ad->numa_node); + if (ad->atq_bufs == 0) + { + error = vlib_physmem_last_error (vm); + goto error; + } + + if ((error = vlib_pci_map_dma (vm, h, ad->atq_bufs))) goto error; - ad->arq_bufs = vlib_physmem_alloc_aligned (vm, am->physmem_region, &error, - AVF_MBOX_BUF_SZ * AVF_MBOX_LEN, - 64); - if (error) + ad->arq_bufs = vlib_physmem_alloc_aligned_on_numa (vm, AVF_MBOX_BUF_SZ * + AVF_MBOX_LEN, + CLIB_CACHE_LINE_BYTES, + ad->numa_node); + if (ad->arq_bufs == 0) + { + error = vlib_physmem_last_error (vm); + goto error; + } + + if ((error = vlib_pci_map_dma (vm, h, ad->arq_bufs))) goto error; - if ((error = vlib_pci_intr_enable (h))) + if ((error = vlib_pci_intr_enable (vm, h))) goto error; - /* FIXME detect */ - ad->flags |= AVF_DEVICE_F_IOVA; + if (vlib_pci_supports_virtual_addr_dma (vm, h)) + ad->flags |= AVF_DEVICE_F_VA_DMA; if ((error = avf_device_init (vm, am, ad, args))) goto error; @@ -1300,7 +1492,7 @@ error: args->rv = VNET_API_ERROR_INVALID_INTERFACE; args->error = clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, &args->addr); - vlib_log_err (am->log_class, "%U", format_clib_error, args->error); + avf_log_err (ad, "error: %U", format_clib_error, args->error); } static clib_error_t * @@ -1370,10 +1562,20 @@ static char *avf_tx_func_error_strings[] = { #undef _ }; +static void +avf_clear_hw_interface_counters (u32 instance) +{ + avf_main_t *am = &avf_main; + avf_device_t *ad = vec_elt_at_index (am->devices, instance); + clib_memcpy_fast (&ad->last_cleared_eth_stats, + &ad->eth_stats, sizeof (ad->eth_stats)); +} + /* *INDENT-OFF* */ VNET_DEVICE_CLASS (avf_device_class,) = { .name = "Adaptive Virtual Function (AVF) interface", + .clear_counters = avf_clear_hw_interface_counters, .format_device = format_avf_device, .format_device_name = format_avf_device_name, .admin_up_down_function = avf_interface_admin_up_down, @@ -1388,47 +1590,23 @@ clib_error_t * avf_init (vlib_main_t * vm) { avf_main_t *am = &avf_main; - clib_error_t *error; vlib_thread_main_t *tm = vlib_get_thread_main (); - int i; - - if ((error = vlib_call_init_function (vm, pci_bus_init))) - return error; vec_validate_aligned (am->per_thread_data, tm->n_vlib_mains - 1, CLIB_CACHE_LINE_BYTES); - /* initialize ptype based loopup table */ - vec_validate_aligned (am->ptypes, 255, CLIB_CACHE_LINE_BYTES); - - /* *INDENT-OFF* */ - vec_foreach_index (i, am->ptypes) - { - avf_ptype_t *p = vec_elt_at_index (am->ptypes, i); - if ((i >= 22) && (i <= 87)) - { - p->next_node = VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT; - p->flags = VNET_BUFFER_F_IS_IP4; - } - else if ((i >= 88) && (i <= 153)) - { - p->next_node = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; - p->flags = VNET_BUFFER_F_IS_IP6; - } - else - p->next_node = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; - p->buffer_advance = device_input_next_node_advance[p->next_node]; - p->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; - } - /* *INDENT-ON* */ - - am->log_class = vlib_log_register_class ("avf_plugin", 0); + am->log_class = vlib_log_register_class ("avf", 0); vlib_log_debug (am->log_class, "initialized"); return 0; } -VLIB_INIT_FUNCTION (avf_init); +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (avf_init) = +{ + .runs_after = VLIB_INITS ("pci_bus_init"), +}; +/* *INDENT-OFF* */ /* * fd.io coding-style-patch-verification: ON