11738cd3eSNetanel Belgazal /* 21738cd3eSNetanel Belgazal * Copyright 2015 Amazon.com, Inc. or its affiliates. 31738cd3eSNetanel Belgazal * 41738cd3eSNetanel Belgazal * This software is available to you under a choice of one of two 51738cd3eSNetanel Belgazal * licenses. You may choose to be licensed under the terms of the GNU 61738cd3eSNetanel Belgazal * General Public License (GPL) Version 2, available from the file 71738cd3eSNetanel Belgazal * COPYING in the main directory of this source tree, or the 81738cd3eSNetanel Belgazal * BSD license below: 91738cd3eSNetanel Belgazal * 101738cd3eSNetanel Belgazal * Redistribution and use in source and binary forms, with or 111738cd3eSNetanel Belgazal * without modification, are permitted provided that the following 121738cd3eSNetanel Belgazal * conditions are met: 131738cd3eSNetanel Belgazal * 141738cd3eSNetanel Belgazal * - Redistributions of source code must retain the above 151738cd3eSNetanel Belgazal * copyright notice, this list of conditions and the following 161738cd3eSNetanel Belgazal * disclaimer. 171738cd3eSNetanel Belgazal * 181738cd3eSNetanel Belgazal * - Redistributions in binary form must reproduce the above 191738cd3eSNetanel Belgazal * copyright notice, this list of conditions and the following 201738cd3eSNetanel Belgazal * disclaimer in the documentation and/or other materials 211738cd3eSNetanel Belgazal * provided with the distribution. 221738cd3eSNetanel Belgazal * 231738cd3eSNetanel Belgazal * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 241738cd3eSNetanel Belgazal * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 251738cd3eSNetanel Belgazal * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 261738cd3eSNetanel Belgazal * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 271738cd3eSNetanel Belgazal * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 281738cd3eSNetanel Belgazal * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 291738cd3eSNetanel Belgazal * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 301738cd3eSNetanel Belgazal * SOFTWARE. 311738cd3eSNetanel Belgazal */ 321738cd3eSNetanel Belgazal 331738cd3eSNetanel Belgazal #ifndef ENA_ETH_COM_H_ 341738cd3eSNetanel Belgazal #define ENA_ETH_COM_H_ 351738cd3eSNetanel Belgazal 361738cd3eSNetanel Belgazal #include "ena_com.h" 371738cd3eSNetanel Belgazal 381738cd3eSNetanel Belgazal /* head update threshold in units of (queue size / ENA_COMP_HEAD_THRESH) */ 391738cd3eSNetanel Belgazal #define ENA_COMP_HEAD_THRESH 4 401738cd3eSNetanel Belgazal 411738cd3eSNetanel Belgazal struct ena_com_tx_ctx { 421738cd3eSNetanel Belgazal struct ena_com_tx_meta ena_meta; 431738cd3eSNetanel Belgazal struct ena_com_buf *ena_bufs; 441738cd3eSNetanel Belgazal /* For LLQ, header buffer - pushed to the device mem space */ 451738cd3eSNetanel Belgazal void *push_header; 461738cd3eSNetanel Belgazal 471738cd3eSNetanel Belgazal enum ena_eth_io_l3_proto_index l3_proto; 481738cd3eSNetanel Belgazal enum ena_eth_io_l4_proto_index l4_proto; 491738cd3eSNetanel Belgazal u16 num_bufs; 501738cd3eSNetanel Belgazal u16 req_id; 511738cd3eSNetanel Belgazal /* For regular queue, indicate the size of the header 521738cd3eSNetanel Belgazal * For LLQ, indicate the size of the pushed buffer 531738cd3eSNetanel Belgazal */ 541738cd3eSNetanel Belgazal u16 header_len; 551738cd3eSNetanel Belgazal 561738cd3eSNetanel Belgazal u8 meta_valid; 571738cd3eSNetanel Belgazal u8 tso_enable; 581738cd3eSNetanel Belgazal u8 l3_csum_enable; 591738cd3eSNetanel Belgazal u8 l4_csum_enable; 601738cd3eSNetanel Belgazal u8 l4_csum_partial; 611738cd3eSNetanel Belgazal u8 df; /* Don't fragment */ 621738cd3eSNetanel Belgazal }; 631738cd3eSNetanel Belgazal 641738cd3eSNetanel Belgazal struct ena_com_rx_ctx { 651738cd3eSNetanel Belgazal struct ena_com_rx_buf_info *ena_bufs; 661738cd3eSNetanel Belgazal enum ena_eth_io_l3_proto_index l3_proto; 671738cd3eSNetanel Belgazal enum ena_eth_io_l4_proto_index l4_proto; 681738cd3eSNetanel Belgazal bool l3_csum_err; 691738cd3eSNetanel Belgazal bool l4_csum_err; 70*cb36bb36SArthur Kiyanovski u8 l4_csum_checked; 711738cd3eSNetanel Belgazal /* fragmented packet */ 721738cd3eSNetanel Belgazal bool frag; 731738cd3eSNetanel Belgazal u32 hash; 741738cd3eSNetanel Belgazal u16 descs; 751738cd3eSNetanel Belgazal int max_bufs; 761738cd3eSNetanel Belgazal }; 771738cd3eSNetanel Belgazal 781738cd3eSNetanel Belgazal int ena_com_prepare_tx(struct ena_com_io_sq *io_sq, 791738cd3eSNetanel Belgazal struct ena_com_tx_ctx *ena_tx_ctx, 801738cd3eSNetanel Belgazal int *nb_hw_desc); 811738cd3eSNetanel Belgazal 821738cd3eSNetanel Belgazal int ena_com_rx_pkt(struct ena_com_io_cq *io_cq, 831738cd3eSNetanel Belgazal struct ena_com_io_sq *io_sq, 841738cd3eSNetanel Belgazal struct ena_com_rx_ctx *ena_rx_ctx); 851738cd3eSNetanel Belgazal 861738cd3eSNetanel Belgazal int ena_com_add_single_rx_desc(struct ena_com_io_sq *io_sq, 871738cd3eSNetanel Belgazal struct ena_com_buf *ena_buf, 881738cd3eSNetanel Belgazal u16 req_id); 891738cd3eSNetanel Belgazal 908510e1a3SNetanel Belgazal bool ena_com_cq_empty(struct ena_com_io_cq *io_cq); 918510e1a3SNetanel Belgazal 921738cd3eSNetanel Belgazal static inline void ena_com_unmask_intr(struct ena_com_io_cq *io_cq, 931738cd3eSNetanel Belgazal struct ena_eth_io_intr_reg *intr_reg) 941738cd3eSNetanel Belgazal { 951738cd3eSNetanel Belgazal writel(intr_reg->intr_control, io_cq->unmask_reg); 961738cd3eSNetanel Belgazal } 971738cd3eSNetanel Belgazal 98689b2bdaSArthur Kiyanovski static inline int ena_com_free_desc(struct ena_com_io_sq *io_sq) 991738cd3eSNetanel Belgazal { 1001738cd3eSNetanel Belgazal u16 tail, next_to_comp, cnt; 1011738cd3eSNetanel Belgazal 1021738cd3eSNetanel Belgazal next_to_comp = io_sq->next_to_comp; 1031738cd3eSNetanel Belgazal tail = io_sq->tail; 1041738cd3eSNetanel Belgazal cnt = tail - next_to_comp; 1051738cd3eSNetanel Belgazal 1061738cd3eSNetanel Belgazal return io_sq->q_depth - 1 - cnt; 1071738cd3eSNetanel Belgazal } 1081738cd3eSNetanel Belgazal 109689b2bdaSArthur Kiyanovski /* Check if the submission queue has enough space to hold required_buffers */ 110689b2bdaSArthur Kiyanovski static inline bool ena_com_sq_have_enough_space(struct ena_com_io_sq *io_sq, 111689b2bdaSArthur Kiyanovski u16 required_buffers) 112689b2bdaSArthur Kiyanovski { 113689b2bdaSArthur Kiyanovski int temp; 114689b2bdaSArthur Kiyanovski 115689b2bdaSArthur Kiyanovski if (io_sq->mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST) 116689b2bdaSArthur Kiyanovski return ena_com_free_desc(io_sq) >= required_buffers; 117689b2bdaSArthur Kiyanovski 118689b2bdaSArthur Kiyanovski /* This calculation doesn't need to be 100% accurate. So to reduce 119689b2bdaSArthur Kiyanovski * the calculation overhead just Subtract 2 lines from the free descs 120689b2bdaSArthur Kiyanovski * (one for the header line and one to compensate the devision 121689b2bdaSArthur Kiyanovski * down calculation. 122689b2bdaSArthur Kiyanovski */ 123689b2bdaSArthur Kiyanovski temp = required_buffers / io_sq->llq_info.descs_per_entry + 2; 124689b2bdaSArthur Kiyanovski 125689b2bdaSArthur Kiyanovski return ena_com_free_desc(io_sq) > temp; 126689b2bdaSArthur Kiyanovski } 127689b2bdaSArthur Kiyanovski 12837dff155SNetanel Belgazal static inline int ena_com_write_sq_doorbell(struct ena_com_io_sq *io_sq) 1291738cd3eSNetanel Belgazal { 130689b2bdaSArthur Kiyanovski u16 tail = io_sq->tail; 1311738cd3eSNetanel Belgazal 1321738cd3eSNetanel Belgazal pr_debug("write submission queue doorbell for queue: %d tail: %d\n", 1331738cd3eSNetanel Belgazal io_sq->qid, tail); 1341738cd3eSNetanel Belgazal 1351738cd3eSNetanel Belgazal writel(tail, io_sq->db_addr); 1361738cd3eSNetanel Belgazal 1371738cd3eSNetanel Belgazal return 0; 1381738cd3eSNetanel Belgazal } 1391738cd3eSNetanel Belgazal 1401738cd3eSNetanel Belgazal static inline int ena_com_update_dev_comp_head(struct ena_com_io_cq *io_cq) 1411738cd3eSNetanel Belgazal { 1421738cd3eSNetanel Belgazal u16 unreported_comp, head; 1431738cd3eSNetanel Belgazal bool need_update; 1441738cd3eSNetanel Belgazal 1451738cd3eSNetanel Belgazal head = io_cq->head; 1461738cd3eSNetanel Belgazal unreported_comp = head - io_cq->last_head_update; 1471738cd3eSNetanel Belgazal need_update = unreported_comp > (io_cq->q_depth / ENA_COMP_HEAD_THRESH); 1481738cd3eSNetanel Belgazal 1491738cd3eSNetanel Belgazal if (io_cq->cq_head_db_reg && need_update) { 1501738cd3eSNetanel Belgazal pr_debug("Write completion queue doorbell for queue %d: head: %d\n", 1511738cd3eSNetanel Belgazal io_cq->qid, head); 1521738cd3eSNetanel Belgazal writel(head, io_cq->cq_head_db_reg); 1531738cd3eSNetanel Belgazal io_cq->last_head_update = head; 1541738cd3eSNetanel Belgazal } 1551738cd3eSNetanel Belgazal 1561738cd3eSNetanel Belgazal return 0; 1571738cd3eSNetanel Belgazal } 1581738cd3eSNetanel Belgazal 1591738cd3eSNetanel Belgazal static inline void ena_com_update_numa_node(struct ena_com_io_cq *io_cq, 1601738cd3eSNetanel Belgazal u8 numa_node) 1611738cd3eSNetanel Belgazal { 1621738cd3eSNetanel Belgazal struct ena_eth_io_numa_node_cfg_reg numa_cfg; 1631738cd3eSNetanel Belgazal 1641738cd3eSNetanel Belgazal if (!io_cq->numa_node_cfg_reg) 1651738cd3eSNetanel Belgazal return; 1661738cd3eSNetanel Belgazal 1671738cd3eSNetanel Belgazal numa_cfg.numa_cfg = (numa_node & ENA_ETH_IO_NUMA_NODE_CFG_REG_NUMA_MASK) 1681738cd3eSNetanel Belgazal | ENA_ETH_IO_NUMA_NODE_CFG_REG_ENABLED_MASK; 1691738cd3eSNetanel Belgazal 1701738cd3eSNetanel Belgazal writel(numa_cfg.numa_cfg, io_cq->numa_node_cfg_reg); 1711738cd3eSNetanel Belgazal } 1721738cd3eSNetanel Belgazal 1731738cd3eSNetanel Belgazal static inline void ena_com_comp_ack(struct ena_com_io_sq *io_sq, u16 elem) 1741738cd3eSNetanel Belgazal { 1751738cd3eSNetanel Belgazal io_sq->next_to_comp += elem; 1761738cd3eSNetanel Belgazal } 1771738cd3eSNetanel Belgazal 1780e575f85SArthur Kiyanovski static inline void ena_com_cq_inc_head(struct ena_com_io_cq *io_cq) 1790e575f85SArthur Kiyanovski { 1800e575f85SArthur Kiyanovski io_cq->head++; 1810e575f85SArthur Kiyanovski 1820e575f85SArthur Kiyanovski /* Switch phase bit in case of wrap around */ 1830e575f85SArthur Kiyanovski if (unlikely((io_cq->head & (io_cq->q_depth - 1)) == 0)) 1840e575f85SArthur Kiyanovski io_cq->phase ^= 1; 1850e575f85SArthur Kiyanovski } 1860e575f85SArthur Kiyanovski 1870e575f85SArthur Kiyanovski static inline int ena_com_tx_comp_req_id_get(struct ena_com_io_cq *io_cq, 1880e575f85SArthur Kiyanovski u16 *req_id) 1890e575f85SArthur Kiyanovski { 1900e575f85SArthur Kiyanovski u8 expected_phase, cdesc_phase; 1910e575f85SArthur Kiyanovski struct ena_eth_io_tx_cdesc *cdesc; 1920e575f85SArthur Kiyanovski u16 masked_head; 1930e575f85SArthur Kiyanovski 1940e575f85SArthur Kiyanovski masked_head = io_cq->head & (io_cq->q_depth - 1); 1950e575f85SArthur Kiyanovski expected_phase = io_cq->phase; 1960e575f85SArthur Kiyanovski 1970e575f85SArthur Kiyanovski cdesc = (struct ena_eth_io_tx_cdesc *) 1980e575f85SArthur Kiyanovski ((uintptr_t)io_cq->cdesc_addr.virt_addr + 1990e575f85SArthur Kiyanovski (masked_head * io_cq->cdesc_entry_size_in_bytes)); 2000e575f85SArthur Kiyanovski 2010e575f85SArthur Kiyanovski /* When the current completion descriptor phase isn't the same as the 2020e575f85SArthur Kiyanovski * expected, it mean that the device still didn't update 2030e575f85SArthur Kiyanovski * this completion. 2040e575f85SArthur Kiyanovski */ 2050e575f85SArthur Kiyanovski cdesc_phase = READ_ONCE(cdesc->flags) & ENA_ETH_IO_TX_CDESC_PHASE_MASK; 2060e575f85SArthur Kiyanovski if (cdesc_phase != expected_phase) 2070e575f85SArthur Kiyanovski return -EAGAIN; 2080e575f85SArthur Kiyanovski 2090e575f85SArthur Kiyanovski dma_rmb(); 2100e575f85SArthur Kiyanovski 2110e575f85SArthur Kiyanovski *req_id = READ_ONCE(cdesc->req_id); 2120e575f85SArthur Kiyanovski if (unlikely(*req_id >= io_cq->q_depth)) { 2130e575f85SArthur Kiyanovski pr_err("Invalid req id %d\n", cdesc->req_id); 2140e575f85SArthur Kiyanovski return -EINVAL; 2150e575f85SArthur Kiyanovski } 2160e575f85SArthur Kiyanovski 2170e575f85SArthur Kiyanovski ena_com_cq_inc_head(io_cq); 2180e575f85SArthur Kiyanovski 2190e575f85SArthur Kiyanovski return 0; 2200e575f85SArthur Kiyanovski } 2210e575f85SArthur Kiyanovski 2221738cd3eSNetanel Belgazal #endif /* ENA_ETH_COM_H_ */ 223