ena_eth_com.h revision 361467
1320731Szbb/*-
2320731Szbb * BSD LICENSE
3320731Szbb *
4361467Smw * Copyright (c) 2015-2019 Amazon.com, Inc. or its affiliates.
5320731Szbb * All rights reserved.
6320731Szbb *
7320731Szbb * Redistribution and use in source and binary forms, with or without
8320731Szbb * modification, are permitted provided that the following conditions
9320731Szbb * are met:
10320731Szbb *
11320731Szbb * * Redistributions of source code must retain the above copyright
12320731Szbb * notice, this list of conditions and the following disclaimer.
13320731Szbb * * Redistributions in binary form must reproduce the above copyright
14320731Szbb * notice, this list of conditions and the following disclaimer in
15320731Szbb * the documentation and/or other materials provided with the
16320731Szbb * distribution.
17320731Szbb * * Neither the name of copyright holder nor the names of its
18320731Szbb * contributors may be used to endorse or promote products derived
19320731Szbb * from this software without specific prior written permission.
20320731Szbb *
21320731Szbb * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22320731Szbb * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23320731Szbb * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24320731Szbb * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25320731Szbb * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26320731Szbb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27320731Szbb * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28320731Szbb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29320731Szbb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30320731Szbb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31320731Szbb * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32320731Szbb */
33320731Szbb
34320731Szbb#ifndef ENA_ETH_COM_H_
35320731Szbb#define ENA_ETH_COM_H_
36320731Szbb
37320731Szbb#if defined(__cplusplus)
38320731Szbbextern "C" {
39320731Szbb#endif
40320731Szbb#include "ena_com.h"
41320731Szbb
42320731Szbb/* head update threshold in units of (queue size / ENA_COMP_HEAD_THRESH) */
43320731Szbb#define ENA_COMP_HEAD_THRESH 4
44320731Szbb
45320731Szbbstruct ena_com_tx_ctx {
46320731Szbb	struct ena_com_tx_meta ena_meta;
47320731Szbb	struct ena_com_buf *ena_bufs;
48320731Szbb	/* For LLQ, header buffer - pushed to the device mem space */
49320731Szbb	void *push_header;
50320731Szbb
51320731Szbb	enum ena_eth_io_l3_proto_index l3_proto;
52320731Szbb	enum ena_eth_io_l4_proto_index l4_proto;
53320731Szbb	u16 num_bufs;
54320731Szbb	u16 req_id;
55320731Szbb	/* For regular queue, indicate the size of the header
56320731Szbb	 * For LLQ, indicate the size of the pushed buffer
57320731Szbb	 */
58320731Szbb	u16 header_len;
59320731Szbb
60320731Szbb	u8 meta_valid;
61320731Szbb	u8 tso_enable;
62320731Szbb	u8 l3_csum_enable;
63320731Szbb	u8 l4_csum_enable;
64320731Szbb	u8 l4_csum_partial;
65320731Szbb	u8 df; /* Don't fragment */
66320731Szbb};
67320731Szbb
68320731Szbbstruct ena_com_rx_ctx {
69320731Szbb	struct ena_com_rx_buf_info *ena_bufs;
70320731Szbb	enum ena_eth_io_l3_proto_index l3_proto;
71320731Szbb	enum ena_eth_io_l4_proto_index l4_proto;
72320731Szbb	bool l3_csum_err;
73320731Szbb	bool l4_csum_err;
74361467Smw	u8 l4_csum_checked;
75320731Szbb	/* fragmented packet */
76320731Szbb	bool frag;
77320731Szbb	u32 hash;
78320731Szbb	u16 descs;
79320731Szbb	int max_bufs;
80320731Szbb};
81320731Szbb
82320731Szbbint ena_com_prepare_tx(struct ena_com_io_sq *io_sq,
83320731Szbb		       struct ena_com_tx_ctx *ena_tx_ctx,
84320731Szbb		       int *nb_hw_desc);
85320731Szbb
86320731Szbbint ena_com_rx_pkt(struct ena_com_io_cq *io_cq,
87320731Szbb		   struct ena_com_io_sq *io_sq,
88320731Szbb		   struct ena_com_rx_ctx *ena_rx_ctx);
89320731Szbb
90320731Szbbint ena_com_add_single_rx_desc(struct ena_com_io_sq *io_sq,
91320731Szbb			       struct ena_com_buf *ena_buf,
92320731Szbb			       u16 req_id);
93320731Szbb
94361467Smwbool ena_com_cq_empty(struct ena_com_io_cq *io_cq);
95320731Szbb
96320731Szbbstatic inline void ena_com_unmask_intr(struct ena_com_io_cq *io_cq,
97320731Szbb				       struct ena_eth_io_intr_reg *intr_reg)
98320731Szbb{
99320731Szbb	ENA_REG_WRITE32(io_cq->bus, intr_reg->intr_control, io_cq->unmask_reg);
100320731Szbb}
101320731Szbb
102343397Smwstatic inline int ena_com_free_desc(struct ena_com_io_sq *io_sq)
103320731Szbb{
104320731Szbb	u16 tail, next_to_comp, cnt;
105320731Szbb
106320731Szbb	next_to_comp = io_sq->next_to_comp;
107320731Szbb	tail = io_sq->tail;
108320731Szbb	cnt = tail - next_to_comp;
109320731Szbb
110320731Szbb	return io_sq->q_depth - 1 - cnt;
111320731Szbb}
112320731Szbb
113343397Smw/* Check if the submission queue has enough space to hold required_buffers */
114343397Smwstatic inline bool ena_com_sq_have_enough_space(struct ena_com_io_sq *io_sq,
115343397Smw						u16 required_buffers)
116343397Smw{
117343397Smw	int temp;
118343397Smw
119343397Smw	if (io_sq->mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
120343397Smw		return ena_com_free_desc(io_sq) >= required_buffers;
121343397Smw
122343397Smw	/* This calculation doesn't need to be 100% accurate. So to reduce
123343397Smw	 * the calculation overhead just Subtract 2 lines from the free descs
124343397Smw	 * (one for the header line and one to compensate the devision
125343397Smw	 * down calculation.
126343397Smw	 */
127343397Smw	temp = required_buffers / io_sq->llq_info.descs_per_entry + 2;
128343397Smw
129343397Smw	return ena_com_free_desc(io_sq) > temp;
130343397Smw}
131343397Smw
132361467Smwstatic inline bool ena_com_meta_desc_changed(struct ena_com_io_sq *io_sq,
133361467Smw					     struct ena_com_tx_ctx *ena_tx_ctx)
134361467Smw{
135361467Smw	if (!ena_tx_ctx->meta_valid)
136361467Smw		return false;
137361467Smw
138361467Smw	return !!memcmp(&io_sq->cached_tx_meta,
139361467Smw			&ena_tx_ctx->ena_meta,
140361467Smw			sizeof(struct ena_com_tx_meta));
141361467Smw}
142361467Smw
143361467Smwstatic inline bool is_llq_max_tx_burst_exists(struct ena_com_io_sq *io_sq)
144361467Smw{
145361467Smw	return (io_sq->mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) &&
146361467Smw	       io_sq->llq_info.max_entries_in_tx_burst > 0;
147361467Smw}
148361467Smw
149361467Smwstatic inline bool ena_com_is_doorbell_needed(struct ena_com_io_sq *io_sq,
150361467Smw					      struct ena_com_tx_ctx *ena_tx_ctx)
151361467Smw{
152361467Smw	struct ena_com_llq_info *llq_info;
153361467Smw	int descs_after_first_entry;
154361467Smw	int num_entries_needed = 1;
155361467Smw	u16 num_descs;
156361467Smw
157361467Smw	if (!is_llq_max_tx_burst_exists(io_sq))
158361467Smw		return false;
159361467Smw
160361467Smw	llq_info = &io_sq->llq_info;
161361467Smw	num_descs = ena_tx_ctx->num_bufs;
162361467Smw
163361467Smw	if (unlikely(ena_com_meta_desc_changed(io_sq, ena_tx_ctx)))
164361467Smw		++num_descs;
165361467Smw
166361467Smw	if (num_descs > llq_info->descs_num_before_header) {
167361467Smw		descs_after_first_entry = num_descs - llq_info->descs_num_before_header;
168361467Smw		num_entries_needed += DIV_ROUND_UP(descs_after_first_entry,
169361467Smw						   llq_info->descs_per_entry);
170361467Smw	}
171361467Smw
172361467Smw	ena_trc_dbg("queue: %d num_descs: %d num_entries_needed: %d\n",
173361467Smw		    io_sq->qid, num_descs, num_entries_needed);
174361467Smw
175361467Smw	return num_entries_needed > io_sq->entries_in_tx_burst_left;
176361467Smw}
177361467Smw
178320731Szbbstatic inline int ena_com_write_sq_doorbell(struct ena_com_io_sq *io_sq)
179320731Szbb{
180361467Smw	u16 tail = io_sq->tail;
181361467Smw	u16 max_entries_in_tx_burst = io_sq->llq_info.max_entries_in_tx_burst;
182320731Szbb
183320731Szbb	ena_trc_dbg("write submission queue doorbell for queue: %d tail: %d\n",
184320731Szbb		    io_sq->qid, tail);
185320731Szbb
186320731Szbb	ENA_REG_WRITE32(io_sq->bus, tail, io_sq->db_addr);
187320731Szbb
188361467Smw	if (is_llq_max_tx_burst_exists(io_sq)) {
189361467Smw		ena_trc_dbg("reset available entries in tx burst for queue %d to %d\n",
190361467Smw			     io_sq->qid, max_entries_in_tx_burst);
191361467Smw		io_sq->entries_in_tx_burst_left = max_entries_in_tx_burst;
192361467Smw	}
193361467Smw
194320731Szbb	return 0;
195320731Szbb}
196320731Szbb
197320731Szbbstatic inline int ena_com_update_dev_comp_head(struct ena_com_io_cq *io_cq)
198320731Szbb{
199320731Szbb	u16 unreported_comp, head;
200320731Szbb	bool need_update;
201320731Szbb
202320731Szbb	head = io_cq->head;
203320731Szbb	unreported_comp = head - io_cq->last_head_update;
204320731Szbb	need_update = unreported_comp > (io_cq->q_depth / ENA_COMP_HEAD_THRESH);
205320731Szbb
206320731Szbb	if (io_cq->cq_head_db_reg && need_update) {
207320731Szbb		ena_trc_dbg("Write completion queue doorbell for queue %d: head: %d\n",
208320731Szbb			    io_cq->qid, head);
209320731Szbb		ENA_REG_WRITE32(io_cq->bus, head, io_cq->cq_head_db_reg);
210320731Szbb		io_cq->last_head_update = head;
211320731Szbb	}
212320731Szbb
213320731Szbb	return 0;
214320731Szbb}
215320731Szbb
216320731Szbbstatic inline void ena_com_update_numa_node(struct ena_com_io_cq *io_cq,
217320731Szbb					    u8 numa_node)
218320731Szbb{
219320731Szbb	struct ena_eth_io_numa_node_cfg_reg numa_cfg;
220320731Szbb
221320731Szbb	if (!io_cq->numa_node_cfg_reg)
222320731Szbb		return;
223320731Szbb
224320731Szbb	numa_cfg.numa_cfg = (numa_node & ENA_ETH_IO_NUMA_NODE_CFG_REG_NUMA_MASK)
225320731Szbb		| ENA_ETH_IO_NUMA_NODE_CFG_REG_ENABLED_MASK;
226320731Szbb
227320731Szbb	ENA_REG_WRITE32(io_cq->bus, numa_cfg.numa_cfg, io_cq->numa_node_cfg_reg);
228320731Szbb}
229320731Szbb
230320731Szbbstatic inline void ena_com_comp_ack(struct ena_com_io_sq *io_sq, u16 elem)
231320731Szbb{
232320731Szbb	io_sq->next_to_comp += elem;
233320731Szbb}
234320731Szbb
235361467Smwstatic inline void ena_com_cq_inc_head(struct ena_com_io_cq *io_cq)
236361467Smw{
237361467Smw	io_cq->head++;
238361467Smw
239361467Smw	/* Switch phase bit in case of wrap around */
240361467Smw	if (unlikely((io_cq->head & (io_cq->q_depth - 1)) == 0))
241361467Smw		io_cq->phase ^= 1;
242361467Smw}
243361467Smw
244361467Smwstatic inline int ena_com_tx_comp_req_id_get(struct ena_com_io_cq *io_cq,
245361467Smw					     u16 *req_id)
246361467Smw{
247361467Smw	u8 expected_phase, cdesc_phase;
248361467Smw	struct ena_eth_io_tx_cdesc *cdesc;
249361467Smw	u16 masked_head;
250361467Smw
251361467Smw	masked_head = io_cq->head & (io_cq->q_depth - 1);
252361467Smw	expected_phase = io_cq->phase;
253361467Smw
254361467Smw	cdesc = (struct ena_eth_io_tx_cdesc *)
255361467Smw		((uintptr_t)io_cq->cdesc_addr.virt_addr +
256361467Smw		(masked_head * io_cq->cdesc_entry_size_in_bytes));
257361467Smw
258361467Smw	/* When the current completion descriptor phase isn't the same as the
259361467Smw	 * expected, it mean that the device still didn't update
260361467Smw	 * this completion.
261361467Smw	 */
262361467Smw	cdesc_phase = READ_ONCE16(cdesc->flags) & ENA_ETH_IO_TX_CDESC_PHASE_MASK;
263361467Smw	if (cdesc_phase != expected_phase)
264361467Smw		return ENA_COM_TRY_AGAIN;
265361467Smw
266361467Smw	dma_rmb();
267361467Smw
268361467Smw	*req_id = READ_ONCE16(cdesc->req_id);
269361467Smw	if (unlikely(*req_id >= io_cq->q_depth)) {
270361467Smw		ena_trc_err("Invalid req id %d\n", cdesc->req_id);
271361467Smw		return ENA_COM_INVAL;
272361467Smw	}
273361467Smw
274361467Smw	ena_com_cq_inc_head(io_cq);
275361467Smw
276361467Smw	return 0;
277361467Smw}
278361467Smw
279320731Szbb#if defined(__cplusplus)
280320731Szbb}
281320731Szbb#endif
282320731Szbb#endif /* ENA_ETH_COM_H_ */
283